Skip to content

Wrong array lookup for uniform floats on generated HLSL #74

@MoritzBrueckner

Description

@MoritzBrueckner

Minimal GLSL example:

#version 450

#define BUFFER_SIZE 16

uniform float radius[BUFFER_SIZE];

out vec4 fragColor;

void main() {
	fragColor = vec4(radius[1], radius[1], radius[1], 1.0);
}

The radius array looks like the following in the Visual Studio graphics debugger:
ConstantBufferFloat

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:

//
// Generated by Microsoft (R) HLSL Shader Compiler 10.1
//
//
// Buffer Definitions: 
//
// cbuffer $Globals
// {
//
//   float radius[16];                  // Offset:    0 Size:   244
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim      HLSL Bind  Count
// ------------------------------ ---------- ------- ----------- -------------- ------
// $Globals                          cbuffer      NA          NA            cb0      1 
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// TEXCOORD                 0   xyzw        0     NONE   float       
// TEXCOORD                 1   x           1     NONE     int       
// SV_Position              0   xyzw        2      POS   float       
//
//
// Output signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_Target                0   xyzw        0   TARGET   float   xyzw
//
      ps_4_0
      dcl_constantbuffer CB0[2], immediateIndexed
      dcl_output o0.xyzw
   0: mov o0.xyz, cb0[1].xxxx
   1: mov o0.w, l(1.000000)
   2: ret 
// Approximately 3 instruction slots used

I think the problematic line is this:

0: mov o0.xyz, cb0[1].xxxx

It looks as if the values were interpreted as float4 instead of float and thus the pointer offset for the array access is 4 floats big instead of one.

The array interpreted as float4s:
ConstantBufferFloat4

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions