Skip to content

Instantly share code, notes, and snippets.

@selfshadow
Created June 18, 2014 19:27
Show Gist options
  • Save selfshadow/d11280213633b1744d31 to your computer and use it in GitHub Desktop.
Save selfshadow/d11280213633b1744d31 to your computer and use it in GitHub Desktop.
HLSL bug with const parameters and loops (tested with fxc from 7.0, 8.0, 8.1 Windows SDK)
struct PS_INPUT
{
float4 Pos : VS_OUT_POSITION;
};
// removing "const" fixes the problem
float4 Function(const float4 value)
{
return value;
}
float4 main(PS_INPUT IN) : SV_Target
{
float4 unused1 = Function(IN.Pos); // removing this fixes the problem
float4 result = 0.0;
for (float x = -4; x <= 4; x++)
{
float4 pos = IN.Pos - 1;
float4 sample = Function(pos);
float4 unused2 = sample/1.0; // by removing this, the compiler collapses the loop => correct result
result += sample;
}
return result;
}
ps_5_0
dcl_globalFlags refactoringAllowed
dcl_input_ps linear v0.xyzw
dcl_output o0.xyzw
dcl_temps 3
mov r0.xyzw, v0.xyzw // IN.Pos
mov r1.xyzw, l(0,0,0,0)
mov r2.x, l(-4.000000)
loop
lt r2.y, l(4.000000), r2.x
breakc_nz r2.y
add r0.xyzw, r0.xyzw, l(-1.000000, -1.000000, -1.000000, -1.000000) // repeatedly decremented!
add r1.xyzw, r0.xyzw, r1.xyzw
add r2.x, r2.x, l(1.000000) // added to running sum (result)
endloop
mov o0.xyzw, r1.xyzw
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment