Skip to content
Draft
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
7 changes: 7 additions & 0 deletions source/slang/slang-emit-wgsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,13 @@ void WGSLSourceEmitter::emitSemanticsPrefixImpl(IRInst* inst)
m_writer->emit(")");
return;
}

// If no semantic decoration is found, this might be a field that should have
// received a location attribute during legalization but didn't for some reason.
// This can happen in edge cases and would result in invalid WGSL.
// For now, we emit a warning but don't emit an invalid location to avoid
// breaking existing functionality. The legalization process should ensure
// all varying fields get proper semantic decorations.
}
}

Expand Down
7 changes: 7 additions & 0 deletions source/slang/slang-ir-legalize-varying-params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2259,6 +2259,13 @@ class LegalizeShaderEntryPointContext
varOffset += offsetAttr->getOffset();
builder.addSemanticDecoration(key, toSlice("_slang_attr"), (int)varOffset);
}
else
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somebody need to debug this and figure out why offsetAttr is null, because it isn't supposed to be so. The change here is a bandaid instead of a fix of the root cause.

// If no offset attribute is found, this field still needs a semantic decoration
// to ensure it gets a location attribute in WGSL. We'll use a default semantic
// with an index based on the field position.
builder.addSemanticDecoration(key, toSlice("_slang_attr"), (int)index);
}
index++;
}
}
Expand Down
42 changes: 42 additions & 0 deletions tests/wgsl/location-attributes.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//TEST:SIMPLE(filecheck=CHECK): -target wgsl -entry vertexMain -stage vertex

// Test to ensure WGSL location attributes are generated correctly for varying parameters

struct VertexInput
{
float3 position : POSITION;
float3 normal : NORMAL;
float2 texcoord : TEXCOORD0;
float3 color : COLOR0;
}

struct VertexOutput
{
float4 position : SV_Position;
float3 normal : NORMAL;
float2 texcoord : TEXCOORD0;
float3 color : COLOR0;
}

[shader("vertex")]
VertexOutput vertexMain(VertexInput input)
{
VertexOutput output;
output.position = float4(input.position, 1.0);
output.normal = input.normal;
output.texcoord = input.texcoord;
output.color = input.color;
return output;
}

// CHECK: struct VertexOutput_0
// CHECK: @builtin(position) position_0 : vec4<f32>,
// CHECK: @location({{[0-9]+}}) normal_0 : vec3<f32>,
// CHECK: @location({{[0-9]+}}) texcoord_0 : vec2<f32>,
// CHECK: @location({{[0-9]+}}) color_0 : vec3<f32>,

// CHECK: struct vertexInput_0
// CHECK: @location({{[0-9]+}}) position_1 : vec3<f32>,
// CHECK: @location({{[0-9]+}}) normal_1 : vec3<f32>,
// CHECK: @location({{[0-9]+}}) texcoord_1 : vec2<f32>,
// CHECK: @location({{[0-9]+}}) color_1 : vec3<f32>,
37 changes: 37 additions & 0 deletions tests/wgsl/location-edge-cases.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//TEST:SIMPLE(filecheck=CHECK): -target wgsl -entry vertexMain -stage vertex

// Test edge cases for WGSL location attribute generation

struct ComplexInput
{
float3 position : POSITION;
float noSemantic; // Field without explicit semantic
float2 texcoord : TEXCOORD0;
}

struct ComplexOutput
{
float4 position : SV_Position;
float someValue; // Field without explicit semantic
float2 texcoord : TEXCOORD0;
}

[shader("vertex")]
ComplexOutput vertexMain(ComplexInput input)
{
ComplexOutput output;
output.position = float4(input.position, 1.0);
output.someValue = input.noSemantic;
output.texcoord = input.texcoord;
return output;
}

// CHECK: struct ComplexOutput_0
// CHECK: @builtin(position) position_0 : vec4<f32>,
// CHECK: @location({{[0-9]+}}) someValue_0 : f32,
// CHECK: @location({{[0-9]+}}) texcoord_0 : vec2<f32>,

// CHECK: struct vertexInput_0
// CHECK: @location({{[0-9]+}}) position_1 : vec3<f32>,
// CHECK: @location({{[0-9]+}}) noSemantic_0 : f32,
// CHECK: @location({{[0-9]+}}) texcoord_1 : vec2<f32>,