Skip to content

Commit 83f4bd5

Browse files
authored
Enable exprs for all supported GLSL layout qualifiers (shader-slang#5857)
1 parent 9d608b9 commit 83f4bd5

7 files changed

+194
-143
lines changed

source/slang/slang-ast-modifier.h

+66-42
Original file line numberDiff line numberDiff line change
@@ -263,29 +263,6 @@ class SharedModifiers : public Modifier
263263
SLANG_AST_CLASS(SharedModifiers)
264264
};
265265

266-
267-
// A GLSL `layout` modifier
268-
//
269-
// We use a distinct modifier for each key that
270-
// appears within the `layout(...)` construct,
271-
// and each key might have an optional value token.
272-
//
273-
// TODO: We probably want a notion of "modifier groups"
274-
// so that we can recover good source location info
275-
// for modifiers that were part of the same vs.
276-
// different constructs.
277-
class GLSLLayoutModifier : public Modifier
278-
{
279-
SLANG_ABSTRACT_AST_CLASS(GLSLLayoutModifier)
280-
281-
282-
// The token used to introduce the modifier is stored
283-
// as the `nameToken` field.
284-
285-
// TODO: may want to accept a full expression here
286-
Token valToken;
287-
};
288-
289266
// AST nodes to represent the begin/end of a `layout` modifier group
290267
class GLSLLayoutModifierGroupMarker : public Modifier
291268
{
@@ -302,29 +279,12 @@ class GLSLLayoutModifierGroupEnd : public GLSLLayoutModifierGroupMarker
302279
SLANG_AST_CLASS(GLSLLayoutModifierGroupEnd)
303280
};
304281

305-
306-
// We divide GLSL `layout` modifiers into those we have parsed
307-
// (in the sense of having some notion of their semantics), and
308-
// those we have not.
309-
class GLSLParsedLayoutModifier : public GLSLLayoutModifier
310-
{
311-
SLANG_ABSTRACT_AST_CLASS(GLSLParsedLayoutModifier)
312-
};
313-
314-
class GLSLUnparsedLayoutModifier : public GLSLLayoutModifier
282+
class GLSLUnparsedLayoutModifier : public Modifier
315283
{
316284
SLANG_AST_CLASS(GLSLUnparsedLayoutModifier)
317285
};
318286

319-
320-
// Specific cases for known GLSL `layout` modifiers that we need to work with
321-
322-
class GLSLLocationLayoutModifier : public GLSLParsedLayoutModifier
323-
{
324-
SLANG_AST_CLASS(GLSLLocationLayoutModifier)
325-
};
326-
327-
class GLSLBufferDataLayoutModifier : public GLSLParsedLayoutModifier
287+
class GLSLBufferDataLayoutModifier : public Modifier
328288
{
329289
SLANG_AST_CLASS(GLSLBufferDataLayoutModifier)
330290
};
@@ -725,6 +685,70 @@ class UncheckedGLSLOffsetLayoutAttribute : public UncheckedGLSLLayoutAttribute
725685
SLANG_UNREFLECTED
726686
};
727687

688+
class UncheckedGLSLInputAttachmentIndexLayoutAttribute : public UncheckedGLSLLayoutAttribute
689+
{
690+
SLANG_AST_CLASS(UncheckedGLSLInputAttachmentIndexLayoutAttribute)
691+
692+
SLANG_UNREFLECTED
693+
};
694+
695+
class UncheckedGLSLLocationLayoutAttribute : public UncheckedGLSLLayoutAttribute
696+
{
697+
SLANG_AST_CLASS(UncheckedGLSLLocationLayoutAttribute)
698+
699+
SLANG_UNREFLECTED
700+
};
701+
702+
class UncheckedGLSLIndexLayoutAttribute : public UncheckedGLSLLayoutAttribute
703+
{
704+
SLANG_AST_CLASS(UncheckedGLSLIndexLayoutAttribute)
705+
706+
SLANG_UNREFLECTED
707+
};
708+
709+
class UncheckedGLSLConstantIdAttribute : public UncheckedGLSLLayoutAttribute
710+
{
711+
SLANG_AST_CLASS(UncheckedGLSLConstantIdAttribute)
712+
713+
SLANG_UNREFLECTED
714+
};
715+
716+
class UncheckedGLSLRayPayloadAttribute : public UncheckedGLSLLayoutAttribute
717+
{
718+
SLANG_AST_CLASS(UncheckedGLSLRayPayloadAttribute)
719+
720+
SLANG_UNREFLECTED
721+
};
722+
723+
class UncheckedGLSLRayPayloadInAttribute : public UncheckedGLSLLayoutAttribute
724+
{
725+
SLANG_AST_CLASS(UncheckedGLSLRayPayloadInAttribute)
726+
727+
SLANG_UNREFLECTED
728+
};
729+
730+
731+
class UncheckedGLSLHitObjectAttributesAttribute : public UncheckedGLSLLayoutAttribute
732+
{
733+
SLANG_AST_CLASS(UncheckedGLSLHitObjectAttributesAttribute)
734+
735+
SLANG_UNREFLECTED
736+
};
737+
738+
class UncheckedGLSLCallablePayloadAttribute : public UncheckedGLSLLayoutAttribute
739+
{
740+
SLANG_AST_CLASS(UncheckedGLSLCallablePayloadAttribute)
741+
742+
SLANG_UNREFLECTED
743+
};
744+
745+
class UncheckedGLSLCallablePayloadInAttribute : public UncheckedGLSLLayoutAttribute
746+
{
747+
SLANG_AST_CLASS(UncheckedGLSLCallablePayloadInAttribute)
748+
749+
SLANG_UNREFLECTED
750+
};
751+
728752
// A `[name(arg0, ...)]` style attribute that has been validated.
729753
class Attribute : public AttributeBase
730754
{

source/slang/slang-ast-print.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,6 @@ void ASTPrinter::addDeclKindPrefix(Decl* decl)
434434
continue;
435435
if (as<RequiredGLSLExtensionModifier>(modifier))
436436
continue;
437-
if (as<GLSLLayoutModifier>(modifier))
438-
continue;
439437
if (as<GLSLLayoutModifierGroupMarker>(modifier))
440438
continue;
441439
if (as<HLSLLayoutSemantic>(modifier))

source/slang/slang-check-modifier.cpp

+57-24
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,17 @@ Modifier* SemanticsVisitor::validateAttribute(
513513

514514
inputAttachmentIndexLayoutAttribute->location = location->getValue();
515515
}
516+
else if (auto locationLayoutAttr = as<GLSLLocationAttribute>(attr))
517+
{
518+
if (attr->args.getCount() != 1)
519+
return nullptr;
520+
521+
auto location = checkConstantIntVal(attr->args[0]);
522+
if (!location)
523+
return nullptr;
524+
525+
locationLayoutAttr->value = int32_t(location->getValue());
526+
}
516527
else if (auto bindingAttr = as<GLSLBindingAttribute>(attr))
517528
{
518529
// This must be vk::binding or gl::binding (as specified in core.meta.slang under
@@ -1242,12 +1253,22 @@ ASTNodeType getModifierConflictGroupKind(ASTNodeType modifierType)
12421253
return ASTNodeType::OutModifier;
12431254

12441255
// Modifiers that are their own exclusive group.
1245-
case ASTNodeType::GLSLLayoutModifier:
1246-
case ASTNodeType::GLSLParsedLayoutModifier:
1247-
case ASTNodeType::GLSLLocationLayoutModifier:
12481256
case ASTNodeType::GLSLInputAttachmentIndexLayoutAttribute:
12491257
case ASTNodeType::GLSLOffsetLayoutAttribute:
12501258
case ASTNodeType::GLSLUnparsedLayoutModifier:
1259+
case ASTNodeType::UncheckedGLSLBindingLayoutAttribute:
1260+
case ASTNodeType::UncheckedGLSLSetLayoutAttribute:
1261+
case ASTNodeType::UncheckedGLSLOffsetLayoutAttribute:
1262+
case ASTNodeType::UncheckedGLSLInputAttachmentIndexLayoutAttribute:
1263+
case ASTNodeType::UncheckedGLSLLocationLayoutAttribute:
1264+
case ASTNodeType::UncheckedGLSLIndexLayoutAttribute:
1265+
case ASTNodeType::UncheckedGLSLConstantIdAttribute:
1266+
case ASTNodeType::UncheckedGLSLRayPayloadAttribute:
1267+
case ASTNodeType::UncheckedGLSLRayPayloadInAttribute:
1268+
case ASTNodeType::UncheckedGLSLHitObjectAttributesAttribute:
1269+
case ASTNodeType::UncheckedGLSLCallablePayloadAttribute:
1270+
case ASTNodeType::UncheckedGLSLCallablePayloadInAttribute:
1271+
case ASTNodeType::GLSLBufferDataLayoutModifier:
12511272
case ASTNodeType::GLSLLayoutModifierGroupMarker:
12521273
case ASTNodeType::GLSLLayoutModifierGroupBegin:
12531274
case ASTNodeType::GLSLLayoutModifierGroupEnd:
@@ -1321,12 +1342,10 @@ bool isModifierAllowedOnDecl(bool isGLSLInput, ASTNodeType modifierType, Decl* d
13211342
case ASTNodeType::InModifier:
13221343
case ASTNodeType::InOutModifier:
13231344
case ASTNodeType::OutModifier:
1324-
case ASTNodeType::GLSLLayoutModifier:
1325-
case ASTNodeType::GLSLParsedLayoutModifier:
1326-
case ASTNodeType::GLSLLocationLayoutModifier:
13271345
case ASTNodeType::GLSLInputAttachmentIndexLayoutAttribute:
13281346
case ASTNodeType::GLSLOffsetLayoutAttribute:
13291347
case ASTNodeType::GLSLUnparsedLayoutModifier:
1348+
case ASTNodeType::UncheckedGLSLLayoutAttribute:
13301349
case ASTNodeType::GLSLLayoutModifierGroupMarker:
13311350
case ASTNodeType::GLSLLayoutModifierGroupBegin:
13321351
case ASTNodeType::GLSLLayoutModifierGroupEnd:
@@ -1502,14 +1521,28 @@ AttributeBase* SemanticsVisitor::checkGLSLLayoutAttribute(
15021521

15031522
SLANG_ASSERT(uncheckedAttr->args.getCount() == 2);
15041523
}
1505-
else if (as<UncheckedGLSLOffsetLayoutAttribute>(uncheckedAttr))
1506-
{
1507-
attr = m_astBuilder->create<GLSLOffsetLayoutAttribute>();
1524+
1525+
#define CASE(UncheckedType, CheckedType) \
1526+
else if (as<UncheckedType>(uncheckedAttr)) \
1527+
{ \
1528+
attr = m_astBuilder->create<CheckedType>(); \
15081529
}
1530+
1531+
CASE(UncheckedGLSLOffsetLayoutAttribute, GLSLOffsetLayoutAttribute)
1532+
CASE(UncheckedGLSLInputAttachmentIndexLayoutAttribute, GLSLInputAttachmentIndexLayoutAttribute)
1533+
CASE(UncheckedGLSLLocationLayoutAttribute, GLSLLocationAttribute)
1534+
CASE(UncheckedGLSLIndexLayoutAttribute, GLSLIndexAttribute)
1535+
CASE(UncheckedGLSLConstantIdAttribute, VkConstantIdAttribute)
1536+
CASE(UncheckedGLSLRayPayloadAttribute, VulkanRayPayloadAttribute)
1537+
CASE(UncheckedGLSLRayPayloadInAttribute, VulkanRayPayloadInAttribute)
1538+
CASE(UncheckedGLSLHitObjectAttributesAttribute, VulkanHitObjectAttributesAttribute)
1539+
CASE(UncheckedGLSLCallablePayloadAttribute, VulkanCallablePayloadAttribute)
1540+
CASE(UncheckedGLSLCallablePayloadInAttribute, VulkanCallablePayloadInAttribute)
15091541
else
15101542
{
15111543
getSink()->diagnose(uncheckedAttr, Diagnostics::unrecognizedGLSLLayoutQualifier);
15121544
}
1545+
#undef CASE
15131546

15141547
if (attr)
15151548
{
@@ -1559,21 +1592,6 @@ Modifier* SemanticsVisitor::checkModifier(
15591592
return checkedAttr;
15601593
}
15611594

1562-
if (auto glslLayoutAttribute = as<UncheckedGLSLLayoutAttribute>(m))
1563-
{
1564-
return checkGLSLLayoutAttribute(glslLayoutAttribute, syntaxNode);
1565-
}
1566-
1567-
if (const auto glslImplicitOffsetAttribute = as<GLSLImplicitOffsetLayoutAttribute>(m))
1568-
{
1569-
auto offsetAttr = m_astBuilder->create<GLSLOffsetLayoutAttribute>();
1570-
offsetAttr->loc = glslImplicitOffsetAttribute->loc;
1571-
1572-
// Offset constant folding computation is deferred until all other modifiers are checked to
1573-
// ensure bindinig is checked first.
1574-
return offsetAttr;
1575-
}
1576-
15771595
if (auto decl = as<Decl>(syntaxNode))
15781596
{
15791597
auto moduleDecl = getModuleDecl(decl);
@@ -1591,6 +1609,21 @@ Modifier* SemanticsVisitor::checkModifier(
15911609
}
15921610
}
15931611

1612+
if (auto glslLayoutAttribute = as<UncheckedGLSLLayoutAttribute>(m))
1613+
{
1614+
return checkGLSLLayoutAttribute(glslLayoutAttribute, syntaxNode);
1615+
}
1616+
1617+
if (const auto glslImplicitOffsetAttribute = as<GLSLImplicitOffsetLayoutAttribute>(m))
1618+
{
1619+
auto offsetAttr = m_astBuilder->create<GLSLOffsetLayoutAttribute>();
1620+
offsetAttr->loc = glslImplicitOffsetAttribute->loc;
1621+
1622+
// Offset constant folding computation is deferred until all other modifiers are checked to
1623+
// ensure bindinig is checked first.
1624+
return offsetAttr;
1625+
}
1626+
15941627
MemoryQualifierSetModifier::Flags::MemoryQualifiersBit memoryQualifierBit =
15951628
MemoryQualifierSetModifier::Flags::kNone;
15961629
if (as<GloballyCoherentModifier>(m))

source/slang/slang-lower-to-ir.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -2290,14 +2290,12 @@ void addVarDecorations(IRGenContext* context, IRInst* inst, Decl* decl)
22902290
{
22912291
builder->addSimpleDecoration<IRGlobalInputDecoration>(inst);
22922292
}
2293-
else if (auto glslLocationMod = as<GLSLLocationLayoutModifier>(mod))
2293+
else if (auto glslLocationMod = as<GLSLLocationAttribute>(mod))
22942294
{
22952295
builder->addDecoration(
22962296
inst,
22972297
kIROp_GLSLLocationDecoration,
2298-
builder->getIntValue(
2299-
builder->getIntType(),
2300-
stringToInt(glslLocationMod->valToken.getContent())));
2298+
builder->getIntValue(builder->getIntType(), glslLocationMod->value));
23012299
}
23022300
else if (auto glslOffsetMod = as<GLSLOffsetLayoutAttribute>(mod))
23032301
{

source/slang/slang-parameter-binding.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -4250,7 +4250,7 @@ RefPtr<ProgramLayout> generateParameterBindings(TargetProgram* targetProgram, Di
42504250
{
42514251
needDefaultConstantBuffer = true;
42524252
if (varLayout->varDecl.getDecl()->hasModifier<GLSLBindingAttribute>() ||
4253-
varLayout->varDecl.getDecl()->hasModifier<GLSLLocationLayoutModifier>())
4253+
varLayout->varDecl.getDecl()->hasModifier<GLSLLocationAttribute>())
42544254
sink->diagnose(
42554255
varLayout->varDecl,
42564256
Diagnostics::explicitUniformLocation,

0 commit comments

Comments
 (0)