summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEllie Hermaszewska <ellieh@nvidia.com>2025-01-29 07:55:39 +0800
committerGitHub <noreply@github.com>2025-01-28 15:55:39 -0800
commitc356d3ac2956671b6cb7af667d8b35c7ba8b75a7 (patch)
tree5a9ceaebad7f199588659de19440729119288b0d
parenta5b1aa08e05eb4b24b28b60a1cd10875353a9a92 (diff)
Allow requiring glsl language extensions on structs (#6173)
* Allow requiring glsl language extensions on structs * format code --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> Co-authored-by: Yong He <yonghe@outlook.com>
-rw-r--r--source/slang/slang-emit-c-like.cpp8
-rw-r--r--source/slang/slang-lower-to-ir.cpp33
-rw-r--r--tests/language-feature/glsl-extension.slang27
3 files changed, 64 insertions, 4 deletions
diff --git a/source/slang/slang-emit-c-like.cpp b/source/slang/slang-emit-c-like.cpp
index 98bddad9b..180ef9909 100644
--- a/source/slang/slang-emit-c-like.cpp
+++ b/source/slang/slang-emit-c-like.cpp
@@ -3722,10 +3722,6 @@ void CLikeSourceEmitter::emitSimpleFuncImpl(IRFunc* func)
emitEntryPointAttributes(func, entryPointDecor);
}
- // Deal with required features/capabilities of the function
- //
- handleRequiredCapabilitiesImpl(func);
-
emitFunctionPreambleImpl(func);
emitFuncDecorations(func);
@@ -4885,6 +4881,10 @@ void CLikeSourceEmitter::emitGlobalInstImpl(IRInst* inst)
{
m_writer->advanceToSourceLocation(inst->sourceLoc);
+ // Deal with required features/capabilities of the global inst
+ //
+ handleRequiredCapabilitiesImpl(inst);
+
switch (inst->getOp())
{
case kIROp_GlobalHashedStringLiterals:
diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp
index 76b8e38e9..ba6b3413d 100644
--- a/source/slang/slang-lower-to-ir.cpp
+++ b/source/slang/slang-lower-to-ir.cpp
@@ -9101,6 +9101,7 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo>
subBuilder->addAutoDiffBuiltinDecoration(irAggType);
}
+ addTargetRequirementDecorations(irAggType, decl);
return LoweredValInfo::simple(finalFinishedVal);
}
@@ -9774,6 +9775,36 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo>
}
}
+ void addTargetRequirementDecorations(IRInst* inst, Decl* decl)
+ {
+ // If this declaration requires certain GLSL extension (or a particular GLSL version)
+ // for it to be usable, then declare that here. Similarly for SPIR-V or CUDA
+ //
+ // TODO: We should wrap this an `SpecializedForTargetModifier` together into a single
+ // case for enumerating the "capabilities" that a declaration requires.
+ //
+ for (auto extensionMod : decl->getModifiersOfType<RequiredGLSLExtensionModifier>())
+ {
+ getBuilder()->addRequireGLSLExtensionDecoration(
+ inst,
+ extensionMod->extensionNameToken.getContent());
+ }
+ for (auto versionMod : decl->getModifiersOfType<RequiredGLSLVersionModifier>())
+ {
+ getBuilder()->addRequireGLSLVersionDecoration(
+ inst,
+ Int(getIntegerLiteralValue(versionMod->versionNumberToken)));
+ }
+ for (auto versionMod : decl->getModifiersOfType<RequiredSPIRVVersionModifier>())
+ {
+ getBuilder()->addRequireSPIRVVersionDecoration(inst, versionMod->version);
+ }
+ for (auto versionMod : decl->getModifiersOfType<RequiredCUDASMVersionModifier>())
+ {
+ getBuilder()->addRequireCUDASMVersionDecoration(inst, versionMod->version);
+ }
+ }
+
void addBitFieldAccessorDecorations(IRInst* irFunc, Decl* decl)
{
// If this is an accessor and the parent is describing some bitfield,
@@ -10340,6 +10371,8 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo>
addCatchAllIntrinsicDecorationIfNeeded(irFunc, decl);
+ addTargetRequirementDecorations(irFunc, decl);
+
bool isInline = false;
addBitFieldAccessorDecorations(irFunc, decl);
diff --git a/tests/language-feature/glsl-extension.slang b/tests/language-feature/glsl-extension.slang
new file mode 100644
index 000000000..dbf8fe86c
--- /dev/null
+++ b/tests/language-feature/glsl-extension.slang
@@ -0,0 +1,27 @@
+//TEST:SIMPLE(filecheck=CHECK): -target glsl -entry main -stage compute
+
+RWStructuredBuffer<float> outputBuffer;
+
+// Check that extensions on types work
+__glsl_extension(GL_BAR_bar)
+// CHECK-DAG: #extension GL_BAR_bar : require
+struct S
+{
+ float a;
+}
+
+// Check that extensions on functions work
+__glsl_extension(GL_FOO_foo)
+// CHECK-DAG: #extension GL_FOO_foo : require
+float foo(S s)
+{
+ return s.a;
+}
+
+[numthreads(4, 1, 1)]
+void main(uint3 dispatchThreadID : SV_DispatchThreadID)
+{
+ S s;
+ s.a = 0;
+ outputBuffer[dispatchThreadID.x] = foo(s);
+}