summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSwoorup Joshi <swoorupj@gmail.com>2025-06-25 17:35:12 +1000
committerGitHub <noreply@github.com>2025-06-25 07:35:12 +0000
commit7e8c85e85440c1fea42236a8ef8286e1ce1638ce (patch)
tree17a6b34dacfa3412e6a234779ea44cf1a4b59c9f
parent47475114d036a391bacdb53a9a1c60e5d35b61bc (diff)
Fix generation of wgsl case arms (#7374)
* Fix generation of wgsl case arms * Added test case to test generation of switch case --------- Co-authored-by: Harsh Aggarwal (NVIDIA) <haaggarwal@nvidia.com>
-rw-r--r--source/slang/slang-emit-wgsl.cpp20
-rw-r--r--tests/wgsl/switch-case.slang86
2 files changed, 100 insertions, 6 deletions
diff --git a/source/slang/slang-emit-wgsl.cpp b/source/slang/slang-emit-wgsl.cpp
index d8a243b32..61f34d408 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 000000000..c4ff0996e
--- /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<ShapeDataBlob> shapes;
+ IShape getShape(uint index)
+ {
+ uint type = shapes[index].type;
+ return createDynamicObject<IShape, ShapeDataBlob>(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: }