r/GraphicsProgramming • u/Mioliths • Aug 11 '26
Video Editing millions of voxels in a single CPU thread (C++/Vulkan)
The dynamic ellipses you see when I drag my mouse are just ray-marched ellipse SDFs that I render as voxels by clamping the ray to the nearest voxel position when it's getting close to the surface. So not a single voxel is stored in memory during this phase.
The storing happens when I release the click. The voxels end up actually committed to the world terrain when the dragging ends, which is still very fast due to the data structure I'm using : a sparse 64-tree , an octree with 64 children per node (so a tetrahexacontree I guess ?) and only non-empty nodes are represented in memory. Which implies:
- Voxels are stored in a single buffer of tree nodes
- I'm not actually storing millions of individual voxels, the inside of the ellipses is probably just few KB of tree leaves.
That buffer containing all the nodes is allocated via virtual memory, I first reserve something like 6 GB of virtual addresses via VirtualAlloc / mmap, and commit addresses progressively to physical memory with VirtualAlloc / mprotect only when I need it.
To achieve that real-time performance, the storing algorithm is quite straightforward :
Starting from the root node of the 64-tree, I evaluate a coverage test between the ellipse and the AABB of the node :
- If fully covered, the node becomes a voxel leaf
- If partially covered, recurse into children and repeat
- otherwise, do nothing and stop
Then I upload the whole thing to the GPU unapologetically (will change eventually).
Everything is rendered with a single real time path tracing compute shader written in Slang. Which means that sharing light data between the SDFs, the voxel world and any data structure is quite simple as long as the rendering of these structures is ray-based (ray-marching, ray-tracing, you get it).
GCC and -O3 are doing a lot of heavy lifting though, in debug mode these ellipses would generate a 0.5s lag spike when releasing the click.
Conversely the unique Slang shader is faster when compiled to spirv with -O0 rather than -O3 somehow lol.
30
u/Zerf2k2 Aug 12 '26
Can you spare some more details about this? Which algo are you using?
22
u/Alexxis91 Aug 12 '26
This is some bullshit (positive)
14
u/Dry-Meal-6316 Aug 12 '26
Ohhhh myyyy pppppcccccc
8
u/MacksNotCool Aug 12 '26
This probably would make a good minecraft world editor tool as long as you could identically convert the sdfs back into voxels (which shouldn't be the biggest problem in the world). And it would probably be great for massive terraforming.
1
u/ThanosFisherman Aug 12 '26
Would it be just as fast if you had used OpenGL?
1
u/wrathgod62 Aug 13 '26
How are you handling the shadows
3
u/Le_9k_Redditor Aug 14 '26 edited Aug 14 '26
So after seeing this I've spent some time writing a bunch of different algos and benchmarking with criterion. For my engine to try and optimise the SDF -> sparse voxel grid process, only keeping SDF samples around a surface, discarding for empty space. I figured since you also had to do this process to figure out which voxels are on the SDF surface you'd be interested to know about the results and a faster algo to find surface voxels from a signed distance function.
So, originally a 1m radius sphere SDF took 21832 nano seconds (22ms), dense sampling without caching even when sampling for repeated border values between bricks. First optimisation to prevent repeat samplings got dense down to 17313 nano seconds. This is hitting the SDF function ~275k times, simply because that's how many data points are within a 2mx2mx2m AABB in my engine. But anyway, here are the fun numbers from alternative algos:
Only 3-4ms to get through ~275k possible grid points. Around ~19.2k of those actually being surface values I want in the output, the rest able to be discarded.
But yeah if you wanted to make it even faster to do real time calculations it seems that ray firing + flood fill is worth your time. The only downside is it of course only works for SDF authors with a single connected surface, if the SDF produces multiple disconnected surfaces it stops working. It's mitigable with firing lots of rays to find seed points for separated surfaces of course but after a point this becomes painful and costs too much performance.
Also testing against SDF authors with denser surfaces such a gyroid, then suddenly the octree method is slower than dense sampling, and the ray + flood fill is only slightly faster than dense sampling. I plan to implement all of these: dense, octree, adaptive carve, and a few versions of the ray firing algo. And depending on the author type I'll use the optimal choice.
Testing against bumpy shapes, octree becomes faster than adaptive carve method. Adaptive carve is only faster on simple shapes. In general the numbers coming out of those two methods were always very similar though with minor differences. And the ray firing + flood fill method is always faster for any SDF author that isn't something stupid like 100 separate spheres floating in space without touching each other.
There are also some other weird algo variants that only win for extreme edge cases. So 64-ary vs octree, always highly similar results, but normally octree wins slightly, 64-ary wins if you have folded surfaces or surfaces super close to other surfaces, such as nested spheres. But also for nested spheres it actually ends up faster if you do 64-ary at higher tiers and then just give up after a few tiers and switch back to dense sampling. Probably just due to the fact that when one surface is going to be hit, another surface is probably right next to it in most directions anyway so the overhead of octree costs more than just sampling everything densely. Actually talking about the gyroid again, earlier I mentioned due to the whacky high surface area shape dense sampling becomes faster than octree again. Well the hybrid method of doing 64-ary at a couple of high tiers then switching to dense after that is more performant than both again. Although yet again of course, still slower than ray + flood fill haha
1
u/AccomplishedKey4774 Aug 12 '26
Is slang like glsl?
1
u/Le_9k_Redditor Aug 12 '26
Oh this is nice, pretty much the same thing I'm doing in quite a lot of ways, also written in rust, a 64-ary tree of voxel data, per pixel rendering via a compute shader written in slang. Most of the tree going over the SSBO although I have payloads pointing to bricks in a texture at the bottom as I'm not storing lightweight voxel shaped render data per voxel, but instead storing an SDF value, albedo value, and material property values for each voxel corner. Since I have no virtual memory stuff going on dumping the memory demanding part of my data into textures via bricks instead of keeping it with the rest of the tree in the SSBO just sounded sensible when I originally did it. And yeah only actually storing data for surface voxels, sparse implementation with the 64-ary tree with no wasted memory from storing voxels for empty space internally or externally.
Good to see how rapidly you're able to make edits to this as I haven't yet attempted much in the way of animation or live changes as I've been focused on nailing the static initial data and various code infrastructure for the future, mainly lots of work on multithreading and queues. I was getting concerned for the future though since my acceleration data is a single global tree / nested grid instead of being something like a BVH to an acceleration structure per author, the latter being way easier to make rapid changes to than a single global grid, but BVH to local acceleration structures is way slower to traverse and I guess I'm an idealist. You're giving me hope that I didn't screw up by implementing it this way and rapid edits are perfectly possible
Out of curiosity, how did you choose to structure/pack your tree into the SSBO? I went depth first so that when freeing up old data ranges when making edits I know that it's taking all of its children with it if I just cut up until the next sibling. Not so ideal for me though as I still need to traverse that cut out data to find the old bricks which now need to be freed but whatever, still faster than having to make lots of smaller cuts all over the SSBO. I'm really disliking that I even went with bricks instead of putting everything in a single unified tree structure though as it would've been so much cleaner that way and faster to make changes to
Super impressive performance you've gotten out of it for how rapidly you can make these edits for such huge and smooth per frame changes. Definitely highlights for me that I should stop being lazy with my sampling code which currently just samples everything densely because I haven't yet gotten around to doing a per tree level breakdown to only sample fine details where the parent had a surface intersect with the SDF. I was also considering maybe doing some kind of flood fill to find surface samples at each tier rather than dense sampling every point. At the moment each of my chunks, (like 3 million voxels each roughly) can take half a second to sample and then another quarter of a second to pack down into bricks and words to upload which is concerning. Very curious to see the performance gains to be made there by just improving that sampling algo.
1
u/thesharkguy25 Aug 12 '26
My neurons are activated with all of these shapes, so?
1
u/Still_Explorer Aug 12 '26
This would be really good if you turn it to a sculpting application.
1
u/Melodic-Candy191 Aug 12 '26
Cool
-5
u/DaveAstator2020 Aug 12 '26
you should not edit voxels on cpu at all. bye.