Skip to content

Commit

Permalink
Fix issue where inter-stage parameters without semantics don't get lo…
Browse files Browse the repository at this point in the history
…cation attributes (#5670)

* wgsl: Make sure each shader input field has a semantic

This closes #5633.

* Add test for inter-stage variables without semantics

This verifies the fix of the second issue identified in
#5633

* format code

---------

Co-authored-by: slangbot <[email protected]>
  • Loading branch information
aleino-nv and slangbot authored Nov 25, 2024
1 parent aaca2d2 commit 044b52c
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 6 deletions.
17 changes: 11 additions & 6 deletions source/slang/slang-ir-wgsl-legalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ struct LegalizeWGSLEntryPointContext
semanticInfoToRemove);
// Validate/rearange all semantics which overlap in our flat struct.
fixFieldSemanticsOfFlatStruct(flattenedStruct);
ensureStructHasUserSemantic<LayoutResourceKind::VaryingInput>(
flattenedStruct,
layout);
if (flattenedStruct != structType)
{
// Replace the 'old IRParam type' with a 'new IRParam type'
Expand Down Expand Up @@ -381,7 +384,8 @@ struct LegalizeWGSLEntryPointContext
semanticName);
}

void ensureResultStructHasUserSemantic(IRStructType* structType, IRVarLayout* varLayout)
template<LayoutResourceKind K>
void ensureStructHasUserSemantic(IRStructType* structType, IRVarLayout* varLayout)
{
// Ensure each field in an output struct type has either a system semantic or a user
// semantic, so that signature matching can happen correctly.
Expand Down Expand Up @@ -415,11 +419,10 @@ struct LegalizeWGSLEntryPointContext
}
typeLayout->getFieldLayout(index);
auto fieldLayout = typeLayout->getFieldLayout(index);
if (auto offsetAttr = fieldLayout->findOffsetAttr(LayoutResourceKind::VaryingOutput))
if (auto offsetAttr = fieldLayout->findOffsetAttr(K))
{
UInt varOffset = 0;
if (auto varOffsetAttr =
varLayout->findOffsetAttr(LayoutResourceKind::VaryingOutput))
if (auto varOffsetAttr = varLayout->findOffsetAttr(K))
varOffset = varOffsetAttr->getOffset();
varOffset += offsetAttr->getOffset();
builder.addSemanticDecoration(key, toSlice("_slang_attr"), (int)varOffset);
Expand Down Expand Up @@ -1101,7 +1104,9 @@ struct LegalizeWGSLEntryPointContext
}
// Ensure non-overlapping semantics
fixFieldSemanticsOfFlatStruct(flattenedStruct);
ensureResultStructHasUserSemantic(flattenedStruct, resultLayout);
ensureStructHasUserSemantic<LayoutResourceKind::VaryingOutput>(
flattenedStruct,
resultLayout);
return;
}

Expand All @@ -1121,7 +1126,7 @@ struct LegalizeWGSLEntryPointContext
auto typeLayout = structTypeLayoutBuilder.build();
IRVarLayout::Builder varLayoutBuilder(&builder, typeLayout);
auto varLayout = varLayoutBuilder.build();
ensureResultStructHasUserSemantic(structType, varLayout);
ensureStructHasUserSemantic<LayoutResourceKind::VaryingOutput>(structType, varLayout);

_replaceAllReturnInst(
builder,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// TODO: Investigate failures on non-WebGPU backends
//TEST(smoke,render):COMPARE_HLSL_RENDER: -wgpu

cbuffer Uniforms
{
float4x4 modelViewProjection;
}

struct AssembledVertex
{
float3 position;
float3 color;
};

struct Fragment
{
float4 color;
};

// Vertex Shader

struct VertexStageInput
{
AssembledVertex assembledVertex : A;
};

struct VertexStageOutput
{
float3 color;
float3 localPosition;
float4 sv_position : SV_Position;
};

VertexStageOutput vertexMain(VertexStageInput input)
{
VertexStageOutput output;

float3 position = input.assembledVertex.position;
float3 color = input.assembledVertex.color;

output.color = color;
output.sv_position = mul(modelViewProjection, float4(position, 1.0));
output.localPosition = position;

return output;
}

// Fragment Shader

struct FragmentStageInput
{
float3 color;
float3 localPosition;
};

struct FragmentStageOutput
{
Fragment fragment : SV_Target;
};

FragmentStageOutput fragmentMain(FragmentStageInput input)
{
FragmentStageOutput output;

float3 color = input.color;

if (input.color.y < input.color.z)
{
output.fragment.color = float4(input.localPosition, 1.0);
}
else
{
output.fragment.color = float4(input.color, 1.0);
}

return output;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
result code = 0
standard error = {
}
standard output = {
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 044b52c

Please sign in to comment.