From fdf061e278720ec066a1fac8f1f35a22e817bf2d Mon Sep 17 00:00:00 2001 From: Yong He Date: Thu, 21 Nov 2024 14:07:23 -0800 Subject: Add datalayout for constant buffers. (#5608) * Add datalayout for constant buffers. * Fixes. * Fix test. * Fix glsl codegen. * Update spirv-specific doc. * Fix test. * Fix binding in the presense of specialization constants. * address comments. * Add a test for constant buffer layout. --- source/slang/slang-check-decl.cpp | 43 ++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 12 deletions(-) (limited to 'source/slang/slang-check-decl.cpp') diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp index 07d5dd1fa..251ce6a69 100644 --- a/source/slang/slang-check-decl.cpp +++ b/source/slang/slang-check-decl.cpp @@ -110,6 +110,7 @@ struct SemanticsDeclHeaderVisitor : public SemanticsDeclVisitorBase, void checkMeshOutputDecl(VarDeclBase* varDecl); void maybeApplyLayoutModifier(VarDeclBase* varDecl); void checkVarDeclCommon(VarDeclBase* varDecl); + void checkPushConstantBufferType(VarDeclBase* varDecl); void visitVarDecl(VarDecl* varDecl) { checkVarDeclCommon(varDecl); } @@ -1707,6 +1708,10 @@ void SemanticsDeclHeaderVisitor::maybeApplyLayoutModifier(VarDeclBase* varDecl) addModifier(varDecl, formatAttrib); } } + else + { + checkPushConstantBufferType(varDecl); + } } bool isSpecializationConstant(VarDeclBase* varDecl) @@ -1721,6 +1726,32 @@ bool isSpecializationConstant(VarDeclBase* varDecl) return false; } +void SemanticsDeclHeaderVisitor::checkPushConstantBufferType(VarDeclBase* varDecl) +{ + if (varDecl->findModifier()) + { + // If we see a ConstantBuffer parameter marked as "push_constant", we need + // to set its type to ConstantBuffer. + if (auto cbufferType = as(varDecl->type)) + { + if (cbufferType->getLayoutType() == m_astBuilder->getDefaultLayoutType()) + { + varDecl->type.type = getConstantBufferType( + cbufferType->getElementType(), + m_astBuilder->getStd430LayoutType()); + } + } + else if (isGlobalShaderParameter(varDecl)) + { + // If this is a global variable with [vk::push_constant] attribute, + // we need to make sure to wrap it in a `ConstantBuffer`. + // + varDecl->type.type = + getConstantBufferType(varDecl->type, m_astBuilder->getStd430LayoutType()); + } + } +} + void SemanticsDeclHeaderVisitor::checkVarDeclCommon(VarDeclBase* varDecl) { // A variable that didn't have an explicit type written must @@ -1935,20 +1966,8 @@ void SemanticsDeclHeaderVisitor::checkVarDeclCommon(VarDeclBase* varDecl) } } - if (as(varDecl->parentDecl)) { - // If this is a global variable with [vk::push_constant] attribute, - // we need to make sure to wrap it in a `ConstantBuffer`. - - if (!as(varDecl->type)) - { - if (varDecl->findModifier()) - { - varDecl->type.type = m_astBuilder->getConstantBufferType(varDecl->type); - } - } - if (getModuleDecl(varDecl)->hasModifier()) { // If we are in GLSL compatiblity mode, we want to treat all global variables -- cgit v1.2.3