summaryrefslogtreecommitdiff
path: root/source/slang/slang-parameter-binding.cpp
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2024-12-06 00:55:35 -0800
committerGitHub <noreply@github.com>2024-12-06 00:55:35 -0800
commit8ce7c6f6958f9f5ed750ef1a823b9e9ed8c042d8 (patch)
treee4cb0ff1639ea0cf8209b466bd7d7537db783599 /source/slang/slang-parameter-binding.cpp
parent22b64a446c8c37cc0b3670eb117b64575fc54d2f (diff)
Support specialization constant on WGSL and Metal. (#5780)
Diffstat (limited to 'source/slang/slang-parameter-binding.cpp')
-rw-r--r--source/slang/slang-parameter-binding.cpp134
1 files changed, 69 insertions, 65 deletions
diff --git a/source/slang/slang-parameter-binding.cpp b/source/slang/slang-parameter-binding.cpp
index f9c4f0c94..1303a59f1 100644
--- a/source/slang/slang-parameter-binding.cpp
+++ b/source/slang/slang-parameter-binding.cpp
@@ -1081,25 +1081,9 @@ static void addExplicitParameterBindings_GLSL(
RefPtr<ParameterInfo> parameterInfo,
RefPtr<VarLayout> varLayout)
{
- // We only want to apply GLSL-style layout modifers
- // when compiling for a Khronos-related target.
- //
- // TODO: This should have some finer granularity
- // so that we are able to distinguish between
- // Vulkan and OpenGL as targets.
- //
- if (!isKhronosTarget(context->getTargetRequest()) && !isWGPUTarget(context->getTargetRequest()))
- return;
-
auto typeLayout = varLayout->typeLayout;
auto varDecl = varLayout->varDecl;
- // The catch in GLSL is that the expected resource type
- // is implied by the parameter declaration itself, and
- // the `layout` modifier is only allowed to adjust
- // the index/offset/etc.
- //
-
enum
{
kResInfo = 0,
@@ -1120,56 +1104,12 @@ static void addExplicitParameterBindings_GLSL(
};
ResAndSemanticInfo info[kMaxResCount] = {};
- if (auto foundInputAttachmentIndex =
- typeLayout->FindResourceInfo(LayoutResourceKind::InputAttachmentIndex))
- {
- foundResInfo = foundInputAttachmentIndex;
- // Try to find `input_attachment_index`
- if (auto glslAttachmentIndexAttr =
- varDecl.getDecl()->findModifier<GLSLInputAttachmentIndexLayoutAttribute>())
- {
- 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;
- }
- }
+ // First, we want to apply offsets of specialization constants for platforms that supports them.
+ if (!isKhronosTarget(context->getTargetRequest()) &&
+ !isWGPUTarget(context->getTargetRequest()) && !isMetalTarget(context->getTargetRequest()))
+ return;
- if (auto foundDescriptorTableSlot =
- typeLayout->FindResourceInfo(LayoutResourceKind::DescriptorTableSlot))
- {
- foundResInfo = foundDescriptorTableSlot;
- // Try to find `binding` and `set`
- if (auto glslBindingAttr = varDecl.getDecl()->findModifier<GLSLBindingAttribute>())
- {
- info[kResInfo].resInfo = foundResInfo;
- info[kResInfo].semanticInfo.index = glslBindingAttr->binding;
- info[kResInfo].semanticInfo.space = glslBindingAttr->set;
- }
- }
- else if (
- auto foundSubElementRegisterSpace =
- typeLayout->FindResourceInfo(LayoutResourceKind::SubElementRegisterSpace))
- {
- foundResInfo = foundSubElementRegisterSpace;
- // Try to find `set`
- if (auto attr = varDecl.getDecl()->findModifier<GLSLBindingAttribute>())
- {
- info[kResInfo].resInfo = foundResInfo;
- if (attr->binding != 0)
- {
- getSink(context)->diagnose(
- attr,
- Diagnostics::wholeSpaceParameterRequiresZeroBinding,
- varDecl.getName(),
- attr->binding);
- }
- info[kResInfo].semanticInfo.index = attr->set;
- info[kResInfo].semanticInfo.space = 0;
- }
- }
- else if (
- auto foundSpecializationConstant =
+ if (auto foundSpecializationConstant =
typeLayout->FindResourceInfo(LayoutResourceKind::SpecializationConstant))
{
info[kResInfo].resInfo = foundSpecializationConstant;
@@ -1180,6 +1120,70 @@ static void addExplicitParameterBindings_GLSL(
return;
}
+ // For remaining cases, we only want to apply GLSL-style layout modifers
+ // when compiling for Khronos and WGSL targets.
+ //
+ // TODO: This should have some finer granularity
+ // so that we are able to distinguish between
+ // Vulkan and OpenGL as targets.
+ //
+ if (isKhronosTarget(context->getTargetRequest()) || isWGPUTarget(context->getTargetRequest()))
+ {
+ // The catch in GLSL is that the expected resource type
+ // is implied by the parameter declaration itself, and
+ // the `layout` modifier is only allowed to adjust
+ // the index/offset/etc.
+ //
+
+ if (auto foundInputAttachmentIndex =
+ typeLayout->FindResourceInfo(LayoutResourceKind::InputAttachmentIndex))
+ {
+ foundResInfo = foundInputAttachmentIndex;
+ // Try to find `input_attachment_index`
+ if (auto glslAttachmentIndexAttr =
+ varDecl.getDecl()->findModifier<GLSLInputAttachmentIndexLayoutAttribute>())
+ {
+ 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 (auto foundDescriptorTableSlot =
+ typeLayout->FindResourceInfo(LayoutResourceKind::DescriptorTableSlot))
+ {
+ foundResInfo = foundDescriptorTableSlot;
+ // Try to find `binding` and `set`
+ if (auto glslBindingAttr = varDecl.getDecl()->findModifier<GLSLBindingAttribute>())
+ {
+ info[kResInfo].resInfo = foundResInfo;
+ info[kResInfo].semanticInfo.index = glslBindingAttr->binding;
+ info[kResInfo].semanticInfo.space = glslBindingAttr->set;
+ }
+ }
+ else if (
+ auto foundSubElementRegisterSpace =
+ typeLayout->FindResourceInfo(LayoutResourceKind::SubElementRegisterSpace))
+ {
+ foundResInfo = foundSubElementRegisterSpace;
+ // Try to find `set`
+ if (auto attr = varDecl.getDecl()->findModifier<GLSLBindingAttribute>())
+ {
+ info[kResInfo].resInfo = foundResInfo;
+ if (attr->binding != 0)
+ {
+ getSink(context)->diagnose(
+ attr,
+ Diagnostics::wholeSpaceParameterRequiresZeroBinding,
+ varDecl.getName(),
+ attr->binding);
+ }
+ info[kResInfo].semanticInfo.index = attr->set;
+ info[kResInfo].semanticInfo.space = 0;
+ }
+ }
+ }
auto varDeclBase = as<VarDeclBase>(varDecl);
bool hasABinding = false;