From 3192f34f57abd3245995342a0a5971ebbbbd945c Mon Sep 17 00:00:00 2001 From: Yong He Date: Mon, 15 Apr 2024 23:28:28 -0700 Subject: [GFX] Fix d3d12 buffer view creation logic for StructuredBuffers. (#3954) --- source/core/slang-array.h | 4 ++-- source/slang/slang-check-conformance.cpp | 10 --------- source/slang/slang-check-decl.cpp | 10 +++++++++ source/slang/slang-check-impl.h | 3 ++- source/slang/slang-parameter-binding.cpp | 36 +++++++++++++++++++------------- source/slang/slang-reflection-api.cpp | 32 ++++++++++++++++++++++------ source/slang/slang-type-layout.cpp | 13 +++++++++++- source/slang/slang-type-layout.h | 8 +++---- 8 files changed, 77 insertions(+), 39 deletions(-) (limited to 'source') diff --git a/source/core/slang-array.h b/source/core/slang-array.h index f1ed4fe0d..d24ff0970 100644 --- a/source/core/slang-array.h +++ b/source/core/slang-array.h @@ -159,14 +159,14 @@ namespace Slang void insertArray(Array& arr, const T& val, TArgs... args) { arr.add(val); - insertArray(arr, args...); + insertArray(arr, args...); } template auto makeArray(TArgs ...args) -> Array::Type, sizeof...(args)> { Array::Type, Index(sizeof...(args))> rs; - insertArray(rs, args...); + insertArray::Type>(rs, args...); return rs; } diff --git a/source/slang/slang-check-conformance.cpp b/source/slang/slang-check-conformance.cpp index ff4a40031..a0e51b180 100644 --- a/source/slang/slang-check-conformance.cpp +++ b/source/slang/slang-check-conformance.cpp @@ -227,16 +227,6 @@ namespace Slang return nullptr; } - bool SemanticsVisitor::isInterfaceType(Type* type) - { - if (auto declRefType = as(type)) - { - if (auto interfaceDeclRef = declRefType->getDeclRef().as()) - return true; - } - return false; - } - bool SemanticsVisitor::isValidGenericConstraintType(Type* type) { if (auto andType = as(type)) diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp index 5190f8c0e..4f8dd3dc5 100644 --- a/source/slang/slang-check-decl.cpp +++ b/source/slang/slang-check-decl.cpp @@ -1329,6 +1329,16 @@ namespace Slang return arrayType->isUnsized(); } + bool isInterfaceType(Type* type) + { + if (auto declRefType = as(type)) + { + if (auto interfaceDeclRef = declRefType->getDeclRef().as()) + return true; + } + return false; + } + EnumDecl* isEnumType(Type* type) { if (auto declRefType = as(type)) diff --git a/source/slang/slang-check-impl.h b/source/slang/slang-check-impl.h index e6e980fe8..ff7ce6978 100644 --- a/source/slang/slang-check-impl.h +++ b/source/slang/slang-check-impl.h @@ -2040,7 +2040,6 @@ namespace Slang SubtypeWitness* checkAndConstructSubtypeWitness(Type* subType, Type* superType); - bool isInterfaceType(Type* type); bool isValidGenericConstraintType(Type* type); bool isTypeDifferentiable(Type* type); @@ -2763,6 +2762,8 @@ namespace Slang bool isUnsizedArrayType(Type* type); + bool isInterfaceType(Type* type); + EnumDecl* isEnumType(Type* type); DeclVisibility getDeclVisibility(Decl* decl); diff --git a/source/slang/slang-parameter-binding.cpp b/source/slang/slang-parameter-binding.cpp index 6725380f2..516b9eb44 100644 --- a/source/slang/slang-parameter-binding.cpp +++ b/source/slang/slang-parameter-binding.cpp @@ -935,13 +935,13 @@ static void addExplicitParameterBinding( if (overlappedVarLayout) { //legal if atomicUint - if(parameterInfo->varLayout->varDecl.getDecl()->getType()->astNodeType == ASTNodeType::GLSLAtomicUintType - && overlappedVarLayout->varDecl.getDecl()->getType()->astNodeType == ASTNodeType::GLSLAtomicUintType) + if(parameterInfo->varLayout->getVariable()->getType()->astNodeType == ASTNodeType::GLSLAtomicUintType + && overlappedVarLayout->getVariable()->getType()->astNodeType == ASTNodeType::GLSLAtomicUintType) { return; } - auto paramA = parameterInfo->varLayout->varDecl.getDecl(); - auto paramB = overlappedVarLayout->varDecl.getDecl(); + auto paramA = parameterInfo->varLayout->getVariable(); + auto paramB = overlappedVarLayout->getVariable(); auto& diagnosticInfo = Diagnostics::parameterBindingsOverlap; @@ -1024,7 +1024,8 @@ static void addExplicitParameterBindings_HLSL( // TODO: warning here! } - addExplicitParameterBinding(context, parameterInfo, varDecl.getDecl(), semanticInfo, count); + if (auto varDeclBase = varDecl.as()) + addExplicitParameterBinding(context, parameterInfo, varDeclBase.getDecl(), semanticInfo, count); } } @@ -1133,10 +1134,12 @@ static void addExplicitParameterBindings_GLSL( auto count = resInfo->count; semanticInfo.kind = kind; - addExplicitParameterBinding(context, parameterInfo, varDecl.getDecl(), semanticInfo, count); - if (foundSubpass) - addExplicitParameterBinding(context, parameterInfo, varDecl.getDecl(), subpassSemanticInfo, count); - + if (auto varDeclBase = varDecl.as()) + { + addExplicitParameterBinding(context, parameterInfo, varDeclBase.getDecl(), semanticInfo, count); + if (foundSubpass) + addExplicitParameterBinding(context, parameterInfo, varDeclBase.getDecl(), subpassSemanticInfo, count); + } return; } @@ -1147,7 +1150,7 @@ static void addExplicitParameterBindings_GLSL( // If we have the options, but cannot infer bindings, we don't need to go further if (hlslToVulkanLayoutOptions == nullptr || !hlslToVulkanLayoutOptions->canInferBindings()) { - _maybeDiagnoseMissingVulkanLayoutModifier(context, varDecl); + _maybeDiagnoseMissingVulkanLayoutModifier(context, varDecl.as()); return; } @@ -1169,7 +1172,7 @@ static void addExplicitParameterBindings_GLSL( // We can't infer TextureSampler from HLSL (it's not an HLSL concept) // So use default layout - auto varType = varDecl.getDecl()->getType(); + auto varType = getType(context->getASTBuilder(), varDecl.as()); if (auto textureType = as(varType)) { if (textureType->isCombined()) @@ -1187,7 +1190,7 @@ static void addExplicitParameterBindings_GLSL( // If inference is not enabled for this kind, we can issue a warning if (!hlslToVulkanLayoutOptions->canInfer(vulkanKind, hlslInfo.space)) { - _maybeDiagnoseMissingVulkanLayoutModifier(context, varDecl); + _maybeDiagnoseMissingVulkanLayoutModifier(context, varDecl.as()); return; } @@ -1201,7 +1204,7 @@ static void addExplicitParameterBindings_GLSL( const LayoutSize count = resInfo->count; - addExplicitParameterBinding(context, parameterInfo, varDecl.getDecl(), semanticInfo, count); + addExplicitParameterBinding(context, parameterInfo, as(varDecl.getDecl()), semanticInfo, count); } // Given a single parameter, collect whatever information we have on @@ -2408,7 +2411,10 @@ struct ScopeLayoutBuilder { auto rules = m_layoutContext.rules; m_pendingDataTypeLayoutBuilder.beginLayoutIfNeeded(nullptr, rules); - auto fieldPendingDataVarLayout = m_pendingDataTypeLayoutBuilder.addField(varLayout->varDecl, fieldPendingDataTypeLayout); + auto varDeclBase = varLayout->varDecl.as(); + if (!varDeclBase) + return; + auto fieldPendingDataVarLayout = m_pendingDataTypeLayoutBuilder.addField(varDeclBase, fieldPendingDataTypeLayout); m_structLayout->pendingDataTypeLayout = m_pendingDataTypeLayoutBuilder.getTypeLayout(); @@ -4011,7 +4017,7 @@ RefPtr generateParameterBindings( if( varLayout->typeLayout->FindResourceInfo(LayoutResourceKind::Uniform) ) { needDefaultConstantBuffer = true; - diagnoseGlobalUniform(&sharedContext, varLayout->varDecl.getDecl()); + diagnoseGlobalUniform(&sharedContext, as(varLayout->varDecl.getDecl())); } } } diff --git a/source/slang/slang-reflection-api.cpp b/source/slang/slang-reflection-api.cpp index 90103d8d9..ab73ce7f4 100644 --- a/source/slang/slang-reflection-api.cpp +++ b/source/slang/slang-reflection-api.cpp @@ -55,12 +55,12 @@ static inline SpecializationParamLayout* convert(SlangReflectionTypeParameter * return (SpecializationParamLayout*) typeParam; } -static inline VarDeclBase* convert(SlangReflectionVariable* var) +static inline Decl* convert(SlangReflectionVariable* var) { - return (VarDeclBase*) var; + return (Decl*) var; } -static inline SlangReflectionVariable* convert(VarDeclBase* var) +static inline SlangReflectionVariable* convert(Decl* var) { return (SlangReflectionVariable*) var; } @@ -932,7 +932,7 @@ SLANG_API SlangInt spReflectionTypeLayout_findFieldIndexByName(SlangReflectionTy for(Index f = 0; f < fieldCount; ++f) { auto field = structTypeLayout->fields[f]; - if(getReflectionName(field->varDecl.getDecl())->text.getUnownedSlice() == name) + if(getReflectionName(field->getVariable())->text.getUnownedSlice() == name) return f; } } @@ -1059,6 +1059,18 @@ SLANG_API SlangParameterCategory spReflectionTypeLayout_GetParameterCategory(Sla return getParameterCategory(typeLayout); } +SLANG_API uint32_t spReflectionTypeLayout_GetFieldCount(SlangReflectionTypeLayout* inTypeLayout) +{ + auto typeLayout = convert(inTypeLayout); + if (!typeLayout) return 0; + + if (auto structTypeLayout = as(typeLayout)) + { + return (uint32_t)structTypeLayout->fields.getCount(); + } + return 0; +} + SLANG_API unsigned spReflectionTypeLayout_GetCategoryCount(SlangReflectionTypeLayout* inTypeLayout) { auto typeLayout = convert(inTypeLayout); @@ -2508,6 +2520,9 @@ SLANG_API SlangInt spReflectionTypeLayout_getSubObjectRangeDescriptorRangeSpaceO SLANG_API char const* spReflectionVariable_GetName(SlangReflectionVariable* inVar) { auto var = convert(inVar); + if (as(var)) + return "$base"; + if(!var) return nullptr; // If the variable is one that has an "external" name that is supposed @@ -2521,14 +2536,19 @@ SLANG_API char const* spReflectionVariable_GetName(SlangReflectionVariable* inVa SLANG_API SlangReflectionType* spReflectionVariable_GetType(SlangReflectionVariable* inVar) { auto var = convert(inVar); + + if (auto inheritanceDecl = as(var)) + return convert(inheritanceDecl->base.type); + if(!var) return nullptr; - return convert(var->getType()); + return convert(as(var)->getType()); } SLANG_API SlangReflectionModifier* spReflectionVariable_FindModifier(SlangReflectionVariable* inVar, SlangModifierID modifierID) { auto var = convert(inVar); + if(!var) return nullptr; Modifier* modifier = nullptr; @@ -2571,7 +2591,7 @@ SLANG_API SlangReflectionVariable* spReflectionVariableLayout_GetVariable(SlangR auto varLayout = convert(inVarLayout); if(!varLayout) return nullptr; - return convert(varLayout->varDecl.getDecl()); + return (SlangReflectionVariable*)(varLayout->varDecl.getDecl()); } SLANG_API SlangReflectionTypeLayout* spReflectionVariableLayout_GetTypeLayout(SlangReflectionVariableLayout* inVarLayout) diff --git a/source/slang/slang-type-layout.cpp b/source/slang/slang-type-layout.cpp index e791efadb..30c41d0fe 100644 --- a/source/slang/slang-type-layout.cpp +++ b/source/slang/slang-type-layout.cpp @@ -3,6 +3,7 @@ #include "slang-syntax.h" #include "slang-ir-insts.h" +#include "slang-check-impl.h" #include "../compiler-core/slang-artifact-desc-util.h" @@ -3331,7 +3332,7 @@ void StructTypeLayoutBuilder::beginLayoutIfNeeded( } RefPtr StructTypeLayoutBuilder::addField( - DeclRef field, + DeclRef field, TypeLayoutResult fieldResult) { SLANG_ASSERT(m_typeLayout); @@ -4082,6 +4083,16 @@ static TypeLayoutResult _createTypeLayout( _addLayout(context, type, typeLayout); + // Add all base fields first. + for (auto inheritanceDeclRef : getMembersOfType(context.astBuilder, structDeclRef)) + { + auto baseType = getSup(context.astBuilder, inheritanceDeclRef); + if (isInterfaceType(baseType)) + continue; + auto baseTypeLayout = _createTypeLayout(context, baseType); + typeLayoutBuilder.addField(inheritanceDeclRef, baseTypeLayout); + } + // First, add all fields with explicit offsets. for (auto field : getFields(context.astBuilder, structDeclRef, MemberFilterStyle::Instance)) { diff --git a/source/slang/slang-type-layout.h b/source/slang/slang-type-layout.h index eb0884287..a44efb085 100644 --- a/source/slang/slang-type-layout.h +++ b/source/slang/slang-type-layout.h @@ -514,8 +514,8 @@ class VarLayout : public Layout { public: // The variable we are laying out - DeclRef varDecl; - VarDeclBase* getVariable() { return varDecl.getDecl(); } + DeclRef varDecl; + VarDeclBase* getVariable() { return varDecl.as().getDecl(); } Name* getName() { return getVariable()->getName(); } @@ -721,7 +721,7 @@ public: // TODO: This should map from a declaration to the *index* // in the array above, rather than to the actual pointer, // so that we - Dictionary> mapVarToLayout; + Dictionary> mapVarToLayout; }; class GenericParamTypeLayout : public TypeLayout @@ -1207,7 +1207,7 @@ public: /// One of the `beginLayout*()` functions must have been called previously. /// RefPtr addField( - DeclRef field, + DeclRef field, TypeLayoutResult fieldResult); RefPtr addExplicitUniformField( -- cgit v1.2.3