Search code examples
unity-game-enginehlslshaderlab

Why I can't use tex2D inside a loop in Unity ShaderLab?


I am trying to do something like

   while (currentLayerDepth < currentDepth)
   {
       currentUV -= step;
       currentDepth = tex2D(_HeightTex,currentUV).a;
       currentLayerDepth += eachLayer;
   }

It logged a error Shader error in 'Unlit/CustomParallax': unable to unroll loop, loop does not appear to terminate in a timely manner (1024 iterations) at line 76 (on metal) So now I have two choices, one is to add [unroll(100)] to limit loop times and the other is using tex2Dlod instead of tex2D.

I'm curious why this happened?

Besides, why tex2Dlod can be used in a loop?


Solution

  • tex2D has to compute a local derivative to determine the correct LOD for the sample. Because of how derivatives are usually computed (as difference between neighbouring computation units), they can only be computed for predictable control flow.

    Your loop doesn't predictably do the same number of calls to tex2D for neighbouring fragments, so the derivative can't be predictably computed.

    For more details have a look at the GLSL specs. Search for "derivative" and "uniform control flow"