'How do I run a loop within a Metal fragment shader?
Is it possible to do this? I don't want a a loop that can possibly go on for infinity, and I have experience using for loops in GLSL. However, in Metal, I can't find a way to loop inside the fragment shader. I tried for and while, but they both did not work. Then, I implemented a recursive function
rayHit ray(vector_float3 origin, vector_float3 direction, int steps, int maxSteps, float depth)
{
vector_float3 dir = vector_normalized(direction);
vector_float3 p = origin; //point or direction vector times depth
rayHit hitData;
if (steps < maxSteps)
{
hitData = ray(origin, direction, steps+1, maxSteps, depth);
}
else
{
hitData.pos = origin;
hitData.normal = -direction;
hitData.depth = vector_length(direction);
hitData.steps = 100;
}
return hitData;
}
but I got the error "Fragment function calls recursive code". From all this, it appears that loops are intended to be impossible in Metal. Is there any sort of workaround for this? I'm trying to build some raymarching code, but I don't know how to do it without loops.
EDIT: Syntax was wrong, things seem to be working now.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
