From e49419b0637a357d2e713a0435f0c5ad0c102487 Mon Sep 17 00:00:00 2001 From: ArielG-NV <159081215+ArielG-NV@users.noreply.github.com> Date: Fri, 28 Jun 2024 04:07:12 -0400 Subject: Implement HLSL resource bindings and default type `float4` to `SubpassInput` (#4462) * Add case to `emitVectorReshape` for `vector<>` type, `scalar` value 1. Add new case 2. Add test * fix warning * fix warning * Implement HLSL resource bindings and default type `float4` to `SubpassInput` fixes: #4440 1. Removed GLSLInputAttachmentIndexLayout modifier and the somewhat 'hacky' binding model 'Input Attachment' previously relied upon. This was changed to work with the slang-type-layout rules system. This change allows Slang automatic bindings, HLSL bindings, GLSL bindings, and translation of GLSL to and from HLSL bindings to work. 2. Added default argument `float4` to SubpassInput. 3. Merged glsl.meta and hlsl.meta SubpassInput logic. * fix InputAttachment attribute checks fix InputAttachment attribute checks for HLSL and GLSL syntax * remove unused var * validate attribute correctly Attributes do not have type information. We must check the type expression to validate attribute usage. * remove hacky validation type based validation before types are fully resolved is quite hacky and unstable to changes and wrapped types * fix warning * remove redundant `!= nullptr` * remove extra `!= nullptr` * fix some warnings/errors * subpass capability to limit to dxc & remove default values in some functions * revert logic to previous logic revert logic to return if we have a binding regardless of if a VarDecl is given the binding --- source/slang/slang-parameter-binding.cpp | 138 ++++++++++++++++++------------- 1 file changed, 81 insertions(+), 57 deletions(-) (limited to 'source/slang/slang-parameter-binding.cpp') diff --git a/source/slang/slang-parameter-binding.cpp b/source/slang/slang-parameter-binding.cpp index d76dda4e1..6a1080b0e 100644 --- a/source/slang/slang-parameter-binding.cpp +++ b/source/slang/slang-parameter-binding.cpp @@ -996,6 +996,16 @@ static void addExplicitParameterBindings_HLSL( // If the declaration has explicit binding modifiers, then // here is where we want to extract and apply them... + if (auto inputAttachmentIndexLayoutAttribute = varDecl.getDecl()->findModifier()) + { + LayoutSemanticInfo semanticInfo; + semanticInfo.index = (UInt)inputAttachmentIndexLayoutAttribute->location; + semanticInfo.space = 0; + semanticInfo.kind = LayoutResourceKind::InputAttachmentIndex; + + if (auto varDeclBase = varDecl.as()) + addExplicitParameterBinding(context, parameterInfo, varDeclBase.getDecl(), semanticInfo, 1); + } // Look for HLSL `register` or `packoffset` semantics. for (auto semantic : varDecl.getDecl()->getModifiersOfType()) @@ -1065,82 +1075,96 @@ static void addExplicitParameterBindings_GLSL( // the index/offset/etc. // - TypeLayout::ResourceInfo* resInfo = nullptr; - TypeLayout::ResourceInfo* foundResInfo = nullptr; - - LayoutSemanticInfo semanticInfo; - semanticInfo.index = 0; - semanticInfo.space = 0; + enum + { + kResInfo = 0, + kSubpassResInfo, + kMaxResCount, + }; - LayoutSemanticInfo subpassSemanticInfo; - bool foundBinding = false; - bool foundSubpass = false; + TypeLayout::ResourceInfo* foundResInfo = nullptr; + struct ResAndSemanticInfo + { + TypeLayout::ResourceInfo* resInfo = nullptr; + LayoutSemanticInfo semanticInfo; + ResAndSemanticInfo() + { + semanticInfo.index = 0; + semanticInfo.space = 0; + } + }; + ResAndSemanticInfo info[kMaxResCount] = {}; + + if (auto foundInputAttachmentIndex = typeLayout->FindResourceInfo(LayoutResourceKind::InputAttachmentIndex)) + { + foundResInfo = foundInputAttachmentIndex; + // Try to find `input_attachment_index` + if (auto glslAttachmentIndexAttr = varDecl.getDecl()->findModifier()) + { + info[kSubpassResInfo].resInfo = foundResInfo; + // Subpass fills semantic info of a descriptor and subpass + info[kSubpassResInfo].semanticInfo.index = (UInt)glslAttachmentIndexAttr->location; + info[kSubpassResInfo].semanticInfo.space = 0; + } + } - if( (foundResInfo = typeLayout->FindResourceInfo(LayoutResourceKind::DescriptorTableSlot)) != nullptr ) + if(auto foundDescriptorTableSlot = typeLayout->FindResourceInfo(LayoutResourceKind::DescriptorTableSlot)) { - for (auto dec : varDecl.getDecl()->modifiers) + foundResInfo = foundDescriptorTableSlot; + // Try to find `binding` and `set` + if (auto glslBindingAttr = varDecl.getDecl()->findModifier()) { - // Try to find `binding` and `set` - if (auto glslBindingAttr = as(dec)) - { - resInfo = foundResInfo; - semanticInfo.index = glslBindingAttr->binding; - semanticInfo.space = glslBindingAttr->set; - foundBinding = true; - if (foundSubpass) - break; - } - // Try to find `input_attachment_index` - else if (auto glslAttachmentIndexAttr = as(dec)) - { - // Subpass fills semantic info of a descriptor & subpass - subpassSemanticInfo.index = stringToInt(glslAttachmentIndexAttr->valToken.getContent()); - subpassSemanticInfo.space = 0; - subpassSemanticInfo.kind = LayoutResourceKind::InputAttachmentIndex; - foundSubpass = true; - if (foundBinding) - break; - } + info[kResInfo].resInfo = foundResInfo; + info[kResInfo].semanticInfo.index = glslBindingAttr->binding; + info[kResInfo].semanticInfo.space = glslBindingAttr->set; } } - else if( (foundResInfo = typeLayout->FindResourceInfo(LayoutResourceKind::SubElementRegisterSpace)) != nullptr ) + else if(auto foundSubElementRegisterSpace = typeLayout->FindResourceInfo(LayoutResourceKind::SubElementRegisterSpace)) { + foundResInfo = foundSubElementRegisterSpace; // Try to find `set` if (auto attr = varDecl.getDecl()->findModifier()) { - resInfo = foundResInfo; + info[kResInfo].resInfo = foundResInfo; if (attr->binding != 0) { getSink(context)->diagnose(attr, Diagnostics::wholeSpaceParameterRequiresZeroBinding, varDecl.getName(), attr->binding); } - semanticInfo.index = attr->set; - semanticInfo.space = 0; + info[kResInfo].semanticInfo.index = attr->set; + info[kResInfo].semanticInfo.space = 0; } } - else if( (resInfo = typeLayout->FindResourceInfo(LayoutResourceKind::SpecializationConstant)) != nullptr ) + else if(auto foundSpecializationConstant = typeLayout->FindResourceInfo(LayoutResourceKind::SpecializationConstant)) { + info[kResInfo].resInfo = foundSpecializationConstant; DeclRef varDecl2(varDecl); // Try to find `constant_id` binding - if(!findLayoutArg(varDecl2, &semanticInfo.index)) + if(!findLayoutArg(varDecl2, &info[kResInfo].semanticInfo.index)) return; } - // if we found resInfo, we add the explicit binding - if (resInfo) + + auto varDeclBase = as(varDecl); + bool hasABinding = false; + for (int i = 0; i < kMaxResCount; i++) { - auto kind = resInfo->kind; - auto count = resInfo->count; + auto* resInfoItem = info[i].resInfo; + auto& semanticInfo = info[i].semanticInfo; + if (!resInfoItem) + continue; + + auto kind = resInfoItem->kind; + auto count = resInfoItem->count; semanticInfo.kind = kind; + hasABinding = true; + if(!varDeclBase) + break; - if (auto varDeclBase = varDecl.as()) - { - addExplicitParameterBinding(context, parameterInfo, varDeclBase.getDecl(), semanticInfo, count); - if (foundSubpass) - addExplicitParameterBinding(context, parameterInfo, varDeclBase.getDecl(), subpassSemanticInfo, count); - } - return; + addExplicitParameterBinding(context, parameterInfo, varDeclBase.getDecl(), semanticInfo, count); } + if(hasABinding) + return; auto hlslToVulkanLayoutOptions = context->getTargetProgram()->getHLSLToVulkanLayoutOptions(); bool warnedMissingVulkanLayoutModifier = false; @@ -1196,7 +1220,7 @@ static void addExplicitParameterBindings_GLSL( // We use the HLSL binding directly (even though this notionally for GLSL/Vulkan) // We'll do the shifting at later later point in _maybeApplyHLSLToVulkanShifts - resInfo = typeLayout->findOrAddResourceInfo(hlslInfo.kind); + info[kResInfo].resInfo = typeLayout->findOrAddResourceInfo(hlslInfo.kind); if (warnedMissingVulkanLayoutModifier) { @@ -1205,8 +1229,8 @@ static void addExplicitParameterBindings_GLSL( if(!hlslToVulkanLayoutOptions || hlslToVulkanLayoutOptions->getKindShiftEnabledFlags() == HLSLToVulkanLayoutOptions::KindFlag::None) { - resInfo->kind = LayoutResourceKind::DescriptorTableSlot; - resInfo->count = 1; + info[kResInfo].resInfo->kind = LayoutResourceKind::DescriptorTableSlot; + info[kResInfo].resInfo->count = 1; } else { @@ -1214,12 +1238,12 @@ static void addExplicitParameterBindings_GLSL( } } - semanticInfo.kind = resInfo->kind; - semanticInfo.index = UInt(hlslInfo.index); - semanticInfo.space = UInt(hlslInfo.space); - const LayoutSize count = resInfo->count; + info[kResInfo].semanticInfo.kind = info[kResInfo].resInfo->kind; + info[kResInfo].semanticInfo.index = UInt(hlslInfo.index); + info[kResInfo].semanticInfo.space = UInt(hlslInfo.space); + const LayoutSize count = info[kResInfo].resInfo->count; - addExplicitParameterBinding(context, parameterInfo, as(varDecl.getDecl()), semanticInfo, count); + addExplicitParameterBinding(context, parameterInfo, as(varDecl.getDecl()), info[kResInfo].semanticInfo, count); } // Given a single parameter, collect whatever information we have on -- cgit v1.2.3