Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions source/slang/slang-emit-wgsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
86 changes: 86 additions & 0 deletions tests/wgsl/switch-case.slang
Original file line number Diff line number Diff line change
@@ -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: }
Loading