HLSL 中的数组长度?
Array length in HLSL?
我找不到任何关于如何在 HLSL 中获取数组长度的文档或示例。
我将一组灯光推送到 hlsl 着色器,我想执行 for(int i=0; i<foo.length; i++)
(- 语法不正确)。
难道不能获取数组的长度?我必须从 C++ 端将数组长度整数推送到 hlsl 着色器吗?
此外,我如何才能知道我的着色器中允许的最大 for 循环数是多少? (因为循环是展开的,所以应该有一个限制。)
据我所知,您必须声明具有预定义长度的数组 (Array syntax needs a positive integer as size)。如果您现在用任意数量的数据从 extern 填充这个固定数组,HLSL 无法知道这一点。所以你需要将数组长度传递给着色器。
如果您使用的着色器模型 3.0+ 且之前未指定 [unroll]
,则循环不会自动展开。 (doc)
When no attribute is specified the compiler will first attempt to emit a rolled version of the loop, and if that fails, or if some operations would be easier if the loop was unrolled, will fall back to an unrolled version of the loop.
因此没有最大允许循环数,但根据您使用的着色器模型,存在最大执行指令数 (wiki) and additionally there is a watchdog of windows resetting your graphic device if it need more than 2 seconds for rendering (doc),例如无限循环。
我找不到任何关于如何在 HLSL 中获取数组长度的文档或示例。
我将一组灯光推送到 hlsl 着色器,我想执行 for(int i=0; i<foo.length; i++)
(- 语法不正确)。
难道不能获取数组的长度?我必须从 C++ 端将数组长度整数推送到 hlsl 着色器吗?
此外,我如何才能知道我的着色器中允许的最大 for 循环数是多少? (因为循环是展开的,所以应该有一个限制。)
据我所知,您必须声明具有预定义长度的数组 (Array syntax needs a positive integer as size)。如果您现在用任意数量的数据从 extern 填充这个固定数组,HLSL 无法知道这一点。所以你需要将数组长度传递给着色器。
如果您使用的着色器模型 3.0+ 且之前未指定 [unroll]
,则循环不会自动展开。 (doc)
When no attribute is specified the compiler will first attempt to emit a rolled version of the loop, and if that fails, or if some operations would be easier if the loop was unrolled, will fall back to an unrolled version of the loop.
因此没有最大允许循环数,但根据您使用的着色器模型,存在最大执行指令数 (wiki) and additionally there is a watchdog of windows resetting your graphic device if it need more than 2 seconds for rendering (doc),例如无限循环。