我遇到了 HLSL 的奇怪行为。我正在尝试使用包含在结构中的数组,如下所示(像素着色器代码):
struct VSOUT {
float4 projected : SV_POSITION;
float3 pos: POSITION;
float3 normal : NORMAL;
};
struct Something {
float a[17];
};
float4 shMain (VSOUT input) : SV_Target {
Something s;
for (int i = 0; i < (int)(input.pos.x * 800); ++i)
s.a[(int)input.pos.x] = input.pos.x;
return col * s.a[(int)input.pos.x];
}
该代码在逻辑上没有任何意义,它只是一个示例。问题是,当我尝试编译此代码时,出现以下错误(第 25 行是 for 循环行):
(25,7): error X3511: Forced to unroll loop, but unrolling failed.
但是,当我将数组放在结构之外时(只需在 shMain
中声明 float a[17]
),一切都会按预期工作。
我的问题是,为什么 DirectX 在使用该结构时尝试展开(不可展开的)for 循环?这是有记录的行为吗?除了将数组放在结构之外是否有任何可用的解决方法?
我使用的是 2010 年 6 月起的着色器模型 4.0、DirectX 10 SDK。
编辑:
为了澄清起见,我添加了工作代码,它仅用普通数组替换了结构 Something
的使用:
struct VSOUT {
float4 projected : SV_POSITION;
float3 pos: POSITION;
float3 normal : NORMAL;
};
float4 shMain (VSOUT input) : SV_Target {
float a[17]; // Direct declaration of the array
for (int i = 0; i < (int)(input.pos.x * 800); ++i)
a[(int)input.pos.x] = input.pos.x;
return col * a[(int)input.pos.x];
}
此代码按预期编译并运行。即使我在 for 循环前面添加 [loop]
属性,它也能工作,这意味着它不会展开(这是正确的行为)。
最佳答案
我不确定,但我知道硬件按 2x2 block 调度和处理片段(用于计算导数)。这可能是 fxc
尝试展开 for 循环以便着色器程序以锁步模式执行的原因。
您是否还尝试使用[loop]
属性来生成使用流量控制的代码?
关于directx - HLSL:在结构内使用数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3546165/