CaffeineViking / vkhr

Real-Time Hybrid Hair Rendering using Vulkan™
MIT License
446 stars 34 forks source link

Write the GPU Voxelization Algorithm #24

Closed CaffeineViking closed 5 years ago

CaffeineViking commented 5 years ago

We should eventually port the CPU voxelization algorithm to the GPU if we want to update the densities in real-time. Right now CPU voxelization of 1.8M hair vertices takes around 15ms which I guess is "real-time", but it would be nice to do this on the GPU as well. As part of porting Dominik's HLSL shaders I've used the image2D, so allocating an read/write image3D shouldn't be too different. The only part I see as somewhat problematic is that we'll need to use imageAtomicAdd as part of the voxelization algorithm, which requires the underlying format to be r32i or r32u as shown here, and can't be used with r8i and r8ui as we'd have liked (there is talk about format conversion, so it might still be possible). I also don't know the cost of imageAtomicAdd, so I need to find out if its usable. Otherwise it should be quite straightforward, like this:

#version 460 core

#include "volume.glsl"

layout(std430, binding = 0) buffer Vertices {
    vec3 positions[];
};

layout(local_size_x = 512) in;

layout(binding = 2, r32ui) uniform uimage3D density;

void main() {
    vec3 voxel_size = volume_bounds / volume_resolution;
    vec3 voxel = (positions[gl_GlobalInvocationID.x] - volume_origin) / voxel_size;
    imageAtomicAdd(density, ivec3(floor(voxel)), 1);
}