-
-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrong array lookup for uniform floats on generated HLSL #74
Comments
Those could be the problematic lines: krafix/Sources/SpirVTranslator.cpp Lines 234 to 237 in 68149aa
I'm not really sure about the contents of |
That code is unrelated to HLSL output. |
I don't know anything about Krafix so if you can point me to where to look that would be awesome (it's like finding a needle in a haystack to me). Also, is there a way to look into the generated .hlsl other than its assembly code? The generated shaders are in .d3d11 format. |
@MoritzBrueckner To look into the generated .hlsl shaders, check the |
And hlsl is generated in SPIRV-Cross/spirv_hlsl.cpp |
Thanks :) |
This looks like it could be the cause: https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-packing-rules. It says:
Unfortunately I'm by far not capable to see if this is a Kha, Krafix, SPIR-V or a user issue. But I can certainly say that there are visible differences between OpenGl and DirectX which likely should not exist. This currently keeps me from working on a project because I can't really build for DirectX. Here is the fragment .hlsl of the example above, maybe it helps you: uniform float radius[16];
static float4 fragColor;
static float4 color;
static int instanceID;
struct SPIRV_Cross_Input
{
float4 color : TEXCOORD0;
nointerpolation int instanceID : TEXCOORD1;
};
struct SPIRV_Cross_Output
{
float4 fragColor : SV_Target0;
};
void frag_main()
{
// I filled the radius array with values > 1, only the fifth (index 4) element is 0.
// On OpenGL, the output is white, on directX it is black.
fragColor = float4(radius[1], radius[1], radius[1], 1.0f);
}
SPIRV_Cross_Output main(SPIRV_Cross_Input stage_input)
{
color = stage_input.color;
instanceID = stage_input.instanceID;
frag_main();
SPIRV_Cross_Output stage_output;
stage_output.fragColor = fragColor;
return stage_output;
} |
If someone else stumbles on this issue: as a workaround, you can pass the float array uniforms as packed vec4 arrays, even if some entries might be unused. The lookup will be correct then. |
Minimal GLSL example:
The radius array looks like the following in the Visual Studio graphics debugger:
So the shader should output the value at the second position of the array, which is 4. But instead the output is 0, it is read from the fifth position. If I change the index in the glsl source to 2, the value from the 9th position is used. In OpenGL, everything works as expected, so there is some problem with cross-compiling to HLSL.
Full HLSL assembly:
I think the problematic line is this:
It looks as if the values were interpreted as
float4
instead offloat
and thus the pointer offset for the array access is 4 floats big instead of one.The array interpreted as float4s:
Thanks!
The text was updated successfully, but these errors were encountered: