Skip to content

Commit 0f5a2ce

Browse files
authored
Fix metadata of register space and varying params. (#5906)
1 parent ae04e60 commit 0f5a2ce

File tree

5 files changed

+69
-33
lines changed

5 files changed

+69
-33
lines changed

examples/reflection-api/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ Slang Reflection API Example
44
This example shows basic usage of the Slang reflection API to
55
traverse the parameters of a program, including type and
66
layout information.
7+
8+
This example is a companion Slang reflection API documentation:
9+
https://shader-slang.org/slang/user-guide/compiling.html

examples/reflection-api/main.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
// This example uses the Slang reflection API to travserse the structure
88
// of the parameters of a Slang program and their types.
99
//
10-
// This program is a companion to a document on the Slang reflection API.
11-
// Once that document is published, this paragraph will be updated to
12-
// properly link to that document.
10+
// This program is a companion Slang reflection API documentation:
11+
// https://shader-slang.org/slang/user-guide/compiling.html
1312
//
1413
// Boilerplate
1514
// -----------

source/compiler-core/slang-artifact-associated-impl.h

+3
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ struct ShaderBindingRange
163163
case slang::UnorderedAccess:
164164
case slang::SamplerState:
165165
case slang::DescriptorTableSlot:
166+
case slang::VaryingInput:
167+
case slang::VaryingOutput:
168+
case slang::SpecializationConstant:
166169
return true;
167170
default:
168171
return false;

source/slang/slang-ir-metadata.cpp

+42-28
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,41 @@ static void _insertBinding(
4343
ranges.add(newRange);
4444
}
4545

46+
void collectMetadataFromInst(IRInst* param, ArtifactPostEmitMetadata& outMetadata)
47+
{
48+
auto layoutDecoration = param->findDecoration<IRLayoutDecoration>();
49+
if (!layoutDecoration)
50+
return;
51+
52+
auto varLayout = as<IRVarLayout>(layoutDecoration->getLayout());
53+
if (!varLayout)
54+
return;
55+
56+
for (auto sizeAttr : varLayout->getTypeLayout()->getSizeAttrs())
57+
{
58+
auto kind = sizeAttr->getResourceKind();
59+
60+
// Only track resource types that we can reliably track, such as textures.
61+
// Do not track individual uniforms, for example.
62+
if (!ShaderBindingRange::isUsageTracked(kind))
63+
continue;
64+
65+
if (auto offsetAttr = varLayout->findOffsetAttr(kind))
66+
{
67+
// Get the binding information from this attribute and insert it into the list
68+
auto spaceIndex = offsetAttr->getSpace();
69+
if (auto spaceAttr = varLayout->findOffsetAttr(LayoutResourceKind::RegisterSpace))
70+
{
71+
spaceIndex += spaceAttr->getOffset();
72+
}
73+
auto registerIndex = offsetAttr->getOffset();
74+
auto size = sizeAttr->getSize();
75+
auto count = size.isFinite() ? size.getFiniteValue() : 0;
76+
_insertBinding(outMetadata.m_usedBindings, kind, spaceIndex, registerIndex, count);
77+
}
78+
}
79+
}
80+
4681
// Collects the metadata from the provided IR module, saves it in outMetadata.
4782
void collectMetadata(const IRModule* irModule, ArtifactPostEmitMetadata& outMetadata)
4883
{
@@ -57,39 +92,18 @@ void collectMetadata(const IRModule* irModule, ArtifactPostEmitMetadata& outMeta
5792
auto name = func->findDecoration<IRExportDecoration>()->getMangledName();
5893
outMetadata.m_exportedFunctionMangledNames.add(name);
5994
}
95+
96+
// Collect metadata from entrypoint params.
97+
for (auto param : func->getParams())
98+
{
99+
collectMetadataFromInst(param, outMetadata);
100+
}
60101
}
61102

62103
auto param = as<IRGlobalParam>(inst);
63104
if (!param)
64105
continue;
65-
66-
auto layoutDecoration = param->findDecoration<IRLayoutDecoration>();
67-
if (!layoutDecoration)
68-
continue;
69-
70-
auto varLayout = as<IRVarLayout>(layoutDecoration->getLayout());
71-
if (!varLayout)
72-
continue;
73-
74-
for (auto sizeAttr : varLayout->getTypeLayout()->getSizeAttrs())
75-
{
76-
auto kind = sizeAttr->getResourceKind();
77-
78-
// Only track resource types that we can reliably track, such as textures.
79-
// Do not track individual uniforms, for example.
80-
if (!ShaderBindingRange::isUsageTracked(kind))
81-
continue;
82-
83-
if (auto offsetAttr = varLayout->findOffsetAttr(kind))
84-
{
85-
// Get the binding information from this attribute and insert it into the list
86-
auto spaceIndex = offsetAttr->getSpace();
87-
auto registerIndex = offsetAttr->getOffset();
88-
auto size = sizeAttr->getSize();
89-
auto count = size.isFinite() ? size.getFiniteValue() : 0;
90-
_insertBinding(outMetadata.m_usedBindings, kind, spaceIndex, registerIndex, count);
91-
}
92-
}
106+
collectMetadataFromInst(param, outMetadata);
93107
}
94108
}
95109

tools/slang-unit-test/unit-test-parameter-usage-reflection.cpp

+19-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ SLANG_UNIT_TEST(isParameterLocationUsedReflection)
1818
// Source for a module that contains an undecorated entrypoint.
1919
const char* userSourceBody = R"(
2020
Texture2D g_tex : register(t0);
21+
struct Params {
22+
Texture2D tex2;
23+
Texture2D tex3;
24+
};
25+
ParameterBlock<Params> gParams;
2126
[shader("fragment")]
22-
float4 fragMain(float4 pos:SV_Position) : SV_Target
27+
float4 fragMain(float4 pos:SV_Position, float unused:COLOR0, float4 used:COLOR1) : SV_Target
2328
{
24-
return g_tex.Load(int3(0, 0, 0));
29+
return g_tex.Load(int3(0, 0, 0)) + gParams.tex3.Load(int3(0)) + used;
2530
}
2631
)";
2732

@@ -75,4 +80,16 @@ SLANG_UNIT_TEST(isParameterLocationUsedReflection)
7580

7681
metadata->isParameterLocationUsed(SLANG_PARAMETER_CATEGORY_DESCRIPTOR_TABLE_SLOT, 0, 1, isUsed);
7782
SLANG_CHECK(!isUsed);
83+
84+
metadata->isParameterLocationUsed(SLANG_PARAMETER_CATEGORY_DESCRIPTOR_TABLE_SLOT, 1, 0, isUsed);
85+
SLANG_CHECK(!isUsed);
86+
87+
metadata->isParameterLocationUsed(SLANG_PARAMETER_CATEGORY_DESCRIPTOR_TABLE_SLOT, 1, 1, isUsed);
88+
SLANG_CHECK(isUsed);
89+
90+
metadata->isParameterLocationUsed(SLANG_PARAMETER_CATEGORY_VARYING_INPUT, 0, 0, isUsed);
91+
SLANG_CHECK(!isUsed);
92+
93+
metadata->isParameterLocationUsed(SLANG_PARAMETER_CATEGORY_VARYING_INPUT, 0, 1, isUsed);
94+
SLANG_CHECK(isUsed);
7895
}

0 commit comments

Comments
 (0)