From 0c15582efcec6c7b163ae3a20c04e1aee958d24a Mon Sep 17 00:00:00 2001 From: Yong He Date: Mon, 12 Feb 2024 13:19:35 -0800 Subject: Fix lowering of static consts in a generic function. (#3573) * Fix lowering of static consts in a generic function. * Fix. * Fix. * Fix lowering of shading rate builtin. --- source/slang/slang-ir-glsl-legalize.cpp | 9 ++++- source/slang/slang-lower-to-ir.cpp | 69 +++++++++++++++++---------------- 2 files changed, 43 insertions(+), 35 deletions(-) (limited to 'source') diff --git a/source/slang/slang-ir-glsl-legalize.cpp b/source/slang/slang-ir-glsl-legalize.cpp index dd165769c..0f9e88d20 100644 --- a/source/slang/slang-ir-glsl-legalize.cpp +++ b/source/slang/slang-ir-glsl-legalize.cpp @@ -929,7 +929,14 @@ GLSLSystemValueInfo* getGLSLSystemValueInfo( } else if (semanticName == "sv_shadingrate") { - name = "gl_PrimitiveShadingRateEXT"; + if (kind == LayoutResourceKind::VaryingInput) + { + name = "gl_ShadingRateEXT"; + } + else + { + name = "gl_PrimitiveShadingRateEXT"; + } } if( name ) diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp index f5d743bb1..8fef6f535 100644 --- a/source/slang/slang-lower-to-ir.cpp +++ b/source/slang/slang-lower-to-ir.cpp @@ -724,6 +724,31 @@ LoweredValInfo emitDeclRef( DeclRef declRef, IRType* type); + +bool isFunctionVarDecl(VarDeclBase* decl) +{ + // The immediate parent of a function-scope variable + // declaration will be a `ScopeDecl`. + // + // TODO: right now the parent links for scopes are *not* + // set correctly, so we can't just scan up and look + // for a function in the parent chain... + auto parent = decl->parentDecl; + if (as(parent)) + { + return true; + } + return false; +} + +bool isFunctionStaticVarDecl(VarDeclBase* decl) +{ + // Only a variable marked `static` can be static. + if (!decl->findModifier()) + return false; + return isFunctionVarDecl(decl); +} + IRInst* getSimpleVal(IRGenContext* context, LoweredValInfo lowered); int32_t getIntrinsicOp( @@ -7439,25 +7464,22 @@ struct DeclLoweringVisitor : DeclVisitor NestedContext nested(this); auto subBuilder = nested.getBuilder(); auto subContext = nested.getContext(); - IRGeneric* outerGeneric = emitOuterGenerics(subContext, decl, decl); - // TODO(JS): Is this right? - // - // If we *are* in a generic, then outputting this in the (current) generic scope would be correct. - // If we *aren't* we want to go the level above for insertion - // - // Just inserting into the parent doesn't work with a generic that holds a function that has a static const - // variable. - // + IRGeneric* outerGeneric = nullptr; + + // If we are static, then we need to insert the declaration before the parent. // This tries to match the behavior of previous `lowerFunctionStaticConstVarDecl` functionality - if (!outerGeneric && isFunctionStaticVarDecl(decl)) + if (isFunctionStaticVarDecl(decl)) { // We need to insert the constant at a level above // the function being emitted. This will usually // be the global scope, but it might be an outer // generic if we are lowering a generic function. - - subBuilder->setInsertInto(subBuilder->getFunc()->getParent()); + subBuilder->setInsertBefore(subBuilder->getFunc()); + } + else if (!isFunctionVarDecl(decl)) + { + outerGeneric = emitOuterGenerics(subContext, decl, decl); } auto initExpr = decl->initExpr; @@ -7613,27 +7635,6 @@ struct DeclLoweringVisitor : DeclVisitor return globalVal; } - bool isFunctionStaticVarDecl(VarDeclBase* decl) - { - // Only a variable marked `static` can be static. - if(!decl->findModifier()) - return false; - - // The immediate parent of a function-scope variable - // declaration will be a `ScopeDecl`. - // - // TODO: right now the parent links for scopes are *not* - // set correctly, so we can't just scan up and look - // for a function in the parent chain... - auto parent = decl->parentDecl; - if( as(parent) ) - { - return true; - } - - return false; - } - struct NestedContext { IRGenEnv subEnvStorage; @@ -9782,7 +9783,7 @@ bool canDeclLowerToAGeneric(Decl* decl) { if (varDecl->hasModifier() && varDecl->hasModifier()) { - return true; + return !isFunctionVarDecl(varDecl); } } -- cgit v1.2.3