3

我正在尝试做类似的事情

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

它记录了一个错误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) 所以现在我有两个选择,一个是添加 [unroll(100)] 以限制循环时间,另一个是使用 tex2Dlod 而不是 tex2D。

我很好奇为什么会这样?

此外,为什么 tex2Dlod 可以循环使用?

4

1 回答 1

3

tex2D 必须计算局部导数以确定样本的正确 LOD。由于通常如何计算导数(作为相邻计算单元之间的差异),它们只能为可预测的控制流计算。

您的循环不会对相邻片段执行相同数量的 tex2D 调用,因此无法预测计算导数。

有关更多详细信息,请查看GLSL 规范。搜索“导数”和“统一控制流”

于 2019-09-18T14:38:46.937 回答