Skip to content

Commit

Permalink
Fix parameter location reflection for pure data paramblocks. (#5956)
Browse files Browse the repository at this point in the history
  • Loading branch information
csyonghe authored Jan 3, 2025
1 parent d48cd13 commit 5df3a74
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
43 changes: 38 additions & 5 deletions source/slang/slang-ir-metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ void collectMetadataFromInst(IRInst* param, ArtifactPostEmitMetadata& outMetadat
if (!varLayout)
return;

UInt spaceOffset = 0;
if (auto spaceAttr = varLayout->findOffsetAttr(LayoutResourceKind::RegisterSpace))
{
spaceOffset = spaceAttr->getOffset();
}
for (auto sizeAttr : varLayout->getTypeLayout()->getSizeAttrs())
{
auto kind = sizeAttr->getResourceKind();
Expand All @@ -65,11 +70,39 @@ void collectMetadataFromInst(IRInst* param, ArtifactPostEmitMetadata& outMetadat
if (auto offsetAttr = varLayout->findOffsetAttr(kind))
{
// Get the binding information from this attribute and insert it into the list
auto spaceIndex = offsetAttr->getSpace();
if (auto spaceAttr = varLayout->findOffsetAttr(LayoutResourceKind::RegisterSpace))
{
spaceIndex += spaceAttr->getOffset();
}
auto spaceIndex = spaceOffset + offsetAttr->getSpace();
auto registerIndex = offsetAttr->getOffset();
auto size = sizeAttr->getSize();
auto count = size.isFinite() ? size.getFiniteValue() : 0;
_insertBinding(outMetadata.m_usedBindings, kind, spaceIndex, registerIndex, count);
}
}

// If the global parameter is a parameter block, make sure to collect bindings for its
// default constant buffer, if there is one.
// The default constant buffer binding will be represented in the container var layout.
//
auto paramGroupTypeLayout = as<IRParameterGroupTypeLayout>(varLayout->getTypeLayout());
if (!paramGroupTypeLayout)
return;
auto containerVarLayout = paramGroupTypeLayout->getContainerVarLayout();
if (!containerVarLayout)
return;
auto containerSpaceOffset =
varLayout->findOffsetAttr(LayoutResourceKind::SubElementRegisterSpace);
if (!containerSpaceOffset)
return;
spaceOffset += containerSpaceOffset->getOffset();
for (auto sizeAttr : containerVarLayout->getTypeLayout()->getSizeAttrs())
{
auto kind = sizeAttr->getResourceKind();

if (!ShaderBindingRange::isUsageTracked(kind))
continue;

if (auto offsetAttr = containerVarLayout->findOffsetAttr(kind))
{
auto spaceIndex = spaceOffset + offsetAttr->getSpace();
auto registerIndex = offsetAttr->getOffset();
auto size = sizeAttr->getSize();
auto count = size.isFinite() ? size.getFiniteValue() : 0;
Expand Down
15 changes: 14 additions & 1 deletion tools/slang-unit-test/unit-test-parameter-usage-reflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@ SLANG_UNIT_TEST(isParameterLocationUsedReflection)
Texture2D tex2;
Texture2D tex3;
};
struct Material
{
float2 uvScale;
float2 uvBias;
}
ParameterBlock<Params> gParams;
ConstantBuffer<Material> gcMaterial;
ParameterBlock<Material> gMaterial;
[shader("fragment")]
float4 fragMain(float4 pos:SV_Position, float unused:COLOR0, float4 used:COLOR1) : SV_Target
{
return g_tex.Load(int3(0, 0, 0)) + gParams.tex3.Load(int3(0)) + used;
return g_tex.Load(int3(0, 0, 0)) + gParams.tex3.Load(int3(0)) + used + gMaterial.uvScale.x + gcMaterial.uvBias.x;
}
)";

Expand Down Expand Up @@ -79,6 +86,9 @@ SLANG_UNIT_TEST(isParameterLocationUsedReflection)
SLANG_CHECK(isUsed);

metadata->isParameterLocationUsed(SLANG_PARAMETER_CATEGORY_DESCRIPTOR_TABLE_SLOT, 0, 1, isUsed);
SLANG_CHECK(isUsed);

metadata->isParameterLocationUsed(SLANG_PARAMETER_CATEGORY_DESCRIPTOR_TABLE_SLOT, 0, 2, isUsed);
SLANG_CHECK(!isUsed);

metadata->isParameterLocationUsed(SLANG_PARAMETER_CATEGORY_DESCRIPTOR_TABLE_SLOT, 1, 0, isUsed);
Expand All @@ -87,6 +97,9 @@ SLANG_UNIT_TEST(isParameterLocationUsedReflection)
metadata->isParameterLocationUsed(SLANG_PARAMETER_CATEGORY_DESCRIPTOR_TABLE_SLOT, 1, 1, isUsed);
SLANG_CHECK(isUsed);

metadata->isParameterLocationUsed(SLANG_PARAMETER_CATEGORY_DESCRIPTOR_TABLE_SLOT, 2, 0, isUsed);
SLANG_CHECK(isUsed);

metadata->isParameterLocationUsed(SLANG_PARAMETER_CATEGORY_VARYING_INPUT, 0, 0, isUsed);
SLANG_CHECK(!isUsed);

Expand Down

0 comments on commit 5df3a74

Please sign in to comment.