I have been working on a small Raytracing software recently. As I have been doing it so far, I send out a Ray in the fragment shader and test for an intersection with the scene. My problem is that this requires some sort of dynamic branching. Because you want to stop advancing the ray as soon as you hit something.
In my RayTracer I am dealing with just a 64x64x64 cube of Voxels, which are either set or not. So the actual intersection algorithm is basically free.
Pseudocode:
while(true)
Ray r;
d = distanceToNextVoxel(r)
r.pos += r.direction * d
if(hitVoxel(r.pos))
break;
However this test, if the ray has hit a voxel is slowing down the shader by a lot.
I would like to know, if there are some solutions around this dynamic branching problem.
I have though about doing a normal for loop which goes through some constant number of iterations, and just setting a integer to 0 or 1 depending on if I actually hit a voxel or not, and continuing my logic with that, but I don’t think, that’s the way to go.
I hope you understand my problem and are able to help me! Thank you!