shader-slang / slang

Making it easier to work with shaders
MIT License
1.78k stars 159 forks source link

Does Slang allow for static const global references to `cbuffer` data? #4453

Closed chaoticbob closed 5 days ago

chaoticbob commented 1 week ago

The shade blow generates the following error message:

(0): error 99999: Slang compilation aborted due to an exception of class Slang::InternalError: unimplemented: Unhandled global inst in spirv-emit:
[nameHint(%1)]
let  %Data      : %SDatax5Fstd140_      = load(%2)

CMD

slangc.exe -target spirv -lang slang -D__spirv__ -emit-spirv-directly -profile cs_6_0 -entry main shader.hlsl

In the context of the shader, it sems like the cause is having static in the declaration of Data. If I remove static, the shader compiles. It seems like DXC is just treating SData as a const reference whereas Slang is treating it differently due to the static. Is this even valid syntax for Slang?

Only use case that jumps to mind for this is aliasing cbuffer and possibly ConstantBuffer members for readability. It doesn't seem llike it would provide any optimization benefit.

Shader

struct SData {
                float3x4 mat1[2];
   column_major float3x4 mat2[2];
};

cbuffer SBufferData {
                SData    BufferData;
                float3x4 Mat1[2];
   column_major float3x4 Mat2[2];
   row_major    float3x4 Mat3;
};

static const SData Data = BufferData;

RWStructuredBuffer<float4> Out;

[numthreads(4, 4, 4)]
void main() {
  float3x4 a[2] = Mat1;
  float3x4 b[2] = Mat2;

  float3x4 c = Mat2[1];
  float3x4 d = Mat3;

  Out[0] = Data.mat1[0][0];
}
csyonghe commented 5 days ago

No, a constant buffer content is not a compile time constant, so it cannot be assigned to a static const global.

chaoticbob commented 5 days ago

Thanks! Closing.