shader-slang / slang

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

Requesting clarification how the global namespace operator `::` works in Slang #4457

Closed chaoticbob closed 3 hours ago

chaoticbob commented 5 days ago

I wasn't able to find in the documentation any discussion of the global namespace scope resolution operator ::. I'm running into a slightly confusing case with it. For the shader below, it seems to be okay with the declaration of foo but not bar using the :: operator:

shader.hlsl(23): error 20001: unexpected identifier, expected ';'
  static ::Number bar = Second;

However, if I change the declaration of bar to the following, Slang compiles the shader without issue:

static Number bar = Second;

CMD

slangc.exe -target spirv -lang slang -D__spirv__ -emit-spirv-directly -profile lib_6_3 shader.hlsl

Shader

RaytracingAccelerationStructure rs;

struct Payload
{
  float4 color;
};
struct CallData
{
  float4 data;
};

[UnscopedEnum]
enum Number {
  First,
  Second,
};

static ::Number foo = First;

[shader("raygeneration")]
void main() {
  static ::Number bar = Second;

  uint3 a = DispatchRaysIndex();
  uint3 b = DispatchRaysDimensions();

  Payload myPayload = { float4(0.0f,0.0f,0.0f,0.0f) };
  CallData myCallData = { float4(0.0f,0.0f,0.0f,0.0f) };
  RayDesc rayDesc;
  rayDesc.Origin = float3(0.0f, 0.0f, 0.0f);
  rayDesc.Direction = float3(0.0f, 0.0f, -1.0f);
  rayDesc.TMin = 0.0f;
  rayDesc.TMax = (foo == First) ? 1000.0f : 10.0f;
  TraceRay(rs, 0x0, 0xff, 0, 1, 0, rayDesc, myPayload);
  CallShader(0, myCallData);
}
csyonghe commented 5 days ago

This is likely due to :: isn't being recognized in certain places during parsing. We should just fix the parser.