Skip to content

Commit e5d49cf

Browse files
authored
Allow multiple _AttributeTargets for attribute declaration (shader-slang#4087)
The syntax like: [__AttributeUsage(_AttributeTargets.Var)] [__AttributeUsage(_AttributeTargets.Param)] struct DefaultValueAttribute { int iParam; }; is allowed. For user-defined attribute, we can specify more attribute targets on the attribute declaration. So one attribute can be used in more than one situations.
1 parent f7d54af commit e5d49cf

File tree

3 files changed

+88
-12
lines changed

3 files changed

+88
-12
lines changed

source/slang/slang-check-modifier.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,14 @@ namespace Slang
212212
attrDecl->nameAndLoc.loc = structDecl->nameAndLoc.loc;
213213
attrDecl->loc = structDecl->loc;
214214

215-
AttributeTargetModifier* targetModifier = m_astBuilder->create<AttributeTargetModifier>();
216-
targetModifier->syntaxClass = attrUsageAttr->targetSyntaxClass;
217-
targetModifier->loc = attrUsageAttr->loc;
218-
addModifier(attrDecl, targetModifier);
215+
while(attrUsageAttr)
216+
{
217+
AttributeTargetModifier* targetModifier = m_astBuilder->create<AttributeTargetModifier>();
218+
targetModifier->syntaxClass = attrUsageAttr->targetSyntaxClass;
219+
targetModifier->loc = attrUsageAttr->loc;
220+
addModifier(attrDecl, targetModifier);
221+
attrUsageAttr = as<AttributeUsageAttribute>(attrUsageAttr->next);
222+
}
219223

220224
// Every attribute declaration is associated with the type
221225
// of syntax nodes it constructs (via reflection/RTTI).

tests/reflection/attribute.slang

+23-7
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,10 @@ struct MyStructAttribute
1010
int iParam;
1111
float fParam;
1212
};
13-
[__AttributeUsage(_AttributeTargets.Var)]
14-
struct DefaultValueAttribute
15-
{
16-
int iParam;
17-
};
1813

14+
[__AttributeUsage(_AttributeTargets.Var)]
1915
[__AttributeUsage(_AttributeTargets.Param)]
20-
struct DefaultFuncParamAttribute
16+
struct DefaultValueAttribute
2117
{
2218
int iParam;
2319
};
@@ -43,9 +39,29 @@ ParameterBlock<B> param2;
4339

4440
[DefaultValue(2)] int globalInt;
4541

42+
[__AttributeUsage(_AttributeTargets.Struct)]
43+
[__AttributeUsage(_AttributeTargets.Var)]
44+
[__AttributeUsage(_AttributeTargets.Param)]
45+
struct StructVarParamAttribute
46+
{
47+
int iParam;
48+
};
49+
50+
[StructVarParam(0)]
51+
struct D
52+
{
53+
int a;
54+
};
55+
56+
D param3;
57+
58+
[StructVarParam(1)] int globalInt2;
59+
60+
4661
[numthreads(1, 1, 1)]
4762
void main(
4863
uint3 dispatchThreadID : SV_DispatchThreadID,
49-
[DefaultFuncParam(3)] float a)
64+
[DefaultValue(3)] float a,
65+
[StructVarParam(2)] float b)
5066
{
5167
}

tests/reflection/attribute.slang.expected

+57-1
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,46 @@ standard output = {
220220
"kind": "scalar",
221221
"scalarType": "int32"
222222
}
223+
},
224+
{
225+
"name": "param3",
226+
"binding": {"kind": "uniform", "offset": 16, "size": 4},
227+
"type": {
228+
"kind": "struct",
229+
"name": "D",
230+
"fields": [
231+
{
232+
"name": "a",
233+
"type": {
234+
"kind": "scalar",
235+
"scalarType": "int32"
236+
},
237+
"binding": {"kind": "uniform", "offset": 0, "size": 4}
238+
}
239+
],
240+
"userAttribs": [{
241+
"name": "StructVarParam",
242+
"arguments": [
243+
0
244+
]
245+
}
246+
]
247+
}
248+
},
249+
{
250+
"name": "globalInt2",
251+
"userAttribs": [{
252+
"name": "StructVarParam",
253+
"arguments": [
254+
1
255+
]
256+
}
257+
],
258+
"binding": {"kind": "uniform", "offset": 20, "size": 4},
259+
"type": {
260+
"kind": "scalar",
261+
"scalarType": "int32"
262+
}
223263
}
224264
],
225265
"entryPoints": [
@@ -242,7 +282,7 @@ standard output = {
242282
{
243283
"name": "a",
244284
"userAttribs": [{
245-
"name": "DefaultFuncParam",
285+
"name": "DefaultValue",
246286
"arguments": [
247287
3
248288
]
@@ -254,6 +294,22 @@ standard output = {
254294
"kind": "scalar",
255295
"scalarType": "float32"
256296
}
297+
},
298+
{
299+
"name": "b",
300+
"userAttribs": [{
301+
"name": "StructVarParam",
302+
"arguments": [
303+
2
304+
]
305+
}
306+
],
307+
"stage": "compute",
308+
"binding": {"kind": "varyingInput", "index": 1},
309+
"type": {
310+
"kind": "scalar",
311+
"scalarType": "float32"
312+
}
257313
}
258314
],
259315
"threadGroupSize": [1, 1, 1]

0 commit comments

Comments
 (0)