diff --git a/source/slang/slang-emit-wgsl.cpp b/source/slang/slang-emit-wgsl.cpp index d8a243b3228..61f34d40843 100644 --- a/source/slang/slang-emit-wgsl.cpp +++ b/source/slang/slang-emit-wgsl.cpp @@ -66,16 +66,24 @@ void WGSLSourceEmitter::emitSwitchCaseSelectorsImpl( // "case 2, 3, 4: ...;" instead of the C-like syntax // "case 2: case 3: case 4: ...;". - m_writer->emit("case "); - for (auto caseVal : currentCase->values) + if (!isDefault) { - emitOperand(caseVal, getInfo(EmitOp::General)); - m_writer->emit(", "); + m_writer->emit("case "); + auto& values = currentCase->values; + for (Index i = 0; i < values.getCount(); ++i) + { + emitOperand(values[i], getInfo(EmitOp::General)); + if (i < values.getCount() - 1) + { + m_writer->emit(", "); + } + } } - if (isDefault) + else { - m_writer->emit("default, "); + m_writer->emit("default "); } + m_writer->emit(":\n"); } diff --git a/tests/wgsl/switch-case.slang b/tests/wgsl/switch-case.slang new file mode 100644 index 00000000000..c4ff0996e30 --- /dev/null +++ b/tests/wgsl/switch-case.slang @@ -0,0 +1,86 @@ +//TEST:SIMPLE(filecheck=WGSL): -target wgsl -conformance "Circle:IShape=0" -conformance "Rectangle:IShape=1" + +[anyValueSize(16)] +interface IShape +{ + float getArea(); +} + +struct Circle : IShape +{ + float radius; + + float getArea() { return 3.14159 * radius * radius; } +} + +struct Rectangle : IShape +{ + float width; + float height; + + float getArea() { return width * height; } +} + +struct ShapeDataBlob { uint type; uint payload[16]; } + +struct ShapeBuffer +{ + StructuredBuffer shapes; + IShape getShape(uint index) + { + uint type = shapes[index].type; + return createDynamicObject(type, shapes[index]); + } +} + +struct VertexInput +{ + float2 position; + float3 color; +} + +struct VertexOutput +{ + float4 position : SV_POSITION; + float3 color : TEXCOORD0; + float area : TEXCOORD1; +} + + +[shader("vertex")] +func vs_main( input: VertexInput, shapes: ShapeBuffer)->VertexOutput +{ + VertexOutput output; + output.position = float4(input.position, 0.0, 1.0); + output.color = input.color; + output.area = shapes.getShape(0).getArea(); + return output; +} + +struct FragmentOutput +{ + float4 color : SV_TARGET; +} + +[shader("fragment")] +func fs_main(VertexOutput input)->FragmentOutput +{ + FragmentOutput output; + output.color = float4(input.color * input.area, 1.0); + return output; +} + +//WGSL: fn _S9( _S10 : Tuple_0) -> f32 +//WGSL-NEXT: { +//WGSL-NEXT: switch(_S10.value1_0.x) +//WGSL-NEXT: { +//WGSL-NEXT: case u32(0): +//WGSL-NEXT: { +//WGSL-NEXT: return Circle_getArea_0(unpackAnyValue16_0(_S10.value2_0)); +//WGSL-NEXT: } +//WGSL-NEXT: default : +//WGSL-NEXT: { +//WGSL-NEXT: return Rectangle_getArea_0(unpackAnyValue16_1(_S10.value2_0)); +//WGSL-NEXT: } +//WGSL-NEXT: } +//WGSL-NEXT: }