Skip to content

Commit

Permalink
Allow requiring glsl language extensions on structs (#6173)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Jan 28, 2025
1 parent a5b1aa0 commit c356d3a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
8 changes: 4 additions & 4 deletions source/slang/slang-emit-c-like.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand Down
33 changes: 33 additions & 0 deletions source/slang/slang-lower-to-ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9101,6 +9101,7 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo>
subBuilder->addAutoDiffBuiltinDecoration(irAggType);
}

addTargetRequirementDecorations(irAggType, decl);

return LoweredValInfo::simple(finalFinishedVal);
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -10340,6 +10371,8 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo>

addCatchAllIntrinsicDecorationIfNeeded(irFunc, decl);

addTargetRequirementDecorations(irFunc, decl);

bool isInline = false;

addBitFieldAccessorDecorations(irFunc, decl);
Expand Down
27 changes: 27 additions & 0 deletions tests/language-feature/glsl-extension.slang
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit c356d3a

Please sign in to comment.