summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArielG-NV <159081215+ArielG-NV@users.noreply.github.com>2024-06-12 12:39:58 -0400
committerGitHub <noreply@github.com>2024-06-12 09:39:58 -0700
commit7a4757d2e97f92aae3ddb5dc353a54f72e23351e (patch)
tree1f759f8449f4672c51ef408708b89d3561d400f9
parent180d6b13c6794a0bbb209df023301701cda15158 (diff)
Implicit register binding for hlsl to non-hlsl targets (#4338)
* implicit register binding for hlsl to non-hlsl targets * fix regressions only warn once with `_maybeDiagnoseMissingVulkanLayoutModifier` remove unneeded else * address review and change bindings address review comments on testing to make the tests indiscriminate over order of code emitted. Change bindings to 1:1 map to vulkan bindings * don't set bindings 1:1 with vulkan if command line option was added --------- Co-authored-by: Yong He <yonghe@outlook.com>
-rw-r--r--source/slang/slang-hlsl-to-vulkan-layout-options.h1
-rw-r--r--source/slang/slang-parameter-binding.cpp38
-rw-r--r--tests/hlsl/register-syntax.slang49
3 files changed, 77 insertions, 11 deletions
diff --git a/source/slang/slang-hlsl-to-vulkan-layout-options.h b/source/slang/slang-hlsl-to-vulkan-layout-options.h
index 1b7daf330..b8ae40da0 100644
--- a/source/slang/slang-hlsl-to-vulkan-layout-options.h
+++ b/source/slang/slang-hlsl-to-vulkan-layout-options.h
@@ -75,6 +75,7 @@ public:
{
enum Enum : KindFlags
{
+ None = 0,
UnorderedAccess = KindFlags(1) << Index(Kind::UnorderedAccess),
Sampler = KindFlags(1) << Index(Kind::Sampler),
ShaderResource = KindFlags(1) << Index(Kind::ShaderResource),
diff --git a/source/slang/slang-parameter-binding.cpp b/source/slang/slang-parameter-binding.cpp
index cfbd7f2c5..523ba9287 100644
--- a/source/slang/slang-parameter-binding.cpp
+++ b/source/slang/slang-parameter-binding.cpp
@@ -1048,7 +1048,7 @@ static void addExplicitParameterBindings_GLSL(
RefPtr<VarLayout> varLayout)
{
// We only want to apply GLSL-style layout modifers
- // when compiling for a Khronos-related target.
+ // when compiling for a Khronos-related target.
//
// TODO: This should have some finer granularity
// so that we are able to distinguish between
@@ -1143,15 +1143,13 @@ static void addExplicitParameterBindings_GLSL(
return;
}
- // See if we can infer vulkan binding from HLSL if we have such options set, we know
- // we can't map
auto hlslToVulkanLayoutOptions = context->getTargetProgram()->getHLSLToVulkanLayoutOptions();
-
- // If we have the options, but cannot infer bindings, we don't need to go further
+ bool warnedMissingVulkanLayoutModifier = false;
+ // If we are not told how to infer bindings with a compile option, we warn
if (hlslToVulkanLayoutOptions == nullptr || !hlslToVulkanLayoutOptions->canInferBindings())
{
+ warnedMissingVulkanLayoutModifier = true;
_maybeDiagnoseMissingVulkanLayoutModifier(context, varDecl.as<VarDeclBase>());
- return;
}
// We need an HLSL register semantic to to infer from
@@ -1188,20 +1186,38 @@ static void addExplicitParameterBindings_GLSL(
}
// If inference is not enabled for this kind, we can issue a warning
- if (!hlslToVulkanLayoutOptions->canInfer(vulkanKind, hlslInfo.space))
+ if (hlslToVulkanLayoutOptions && !hlslToVulkanLayoutOptions->canInfer(vulkanKind, hlslInfo.space))
{
- _maybeDiagnoseMissingVulkanLayoutModifier(context, varDecl.as<VarDeclBase>());
- return;
+ if(!warnedMissingVulkanLayoutModifier)
+ {
+ _maybeDiagnoseMissingVulkanLayoutModifier(context, varDecl.as<VarDeclBase>());
+ warnedMissingVulkanLayoutModifier = true;
+ }
}
// 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);
-
+
+ if (warnedMissingVulkanLayoutModifier)
+ {
+ // If we warn due to invalid bindings and user did not set how to interpret 'hlsl style bindings', we should map
+ // `register` 1:1 with equivlent vulkan bindings.
+ if(!hlslToVulkanLayoutOptions
+ || hlslToVulkanLayoutOptions->getKindShiftEnabledFlags() == HLSLToVulkanLayoutOptions::KindFlag::None)
+ {
+ resInfo->kind = LayoutResourceKind::DescriptorTableSlot;
+ resInfo->count = 1;
+ }
+ else
+ {
+ return;
+ }
+ }
+
semanticInfo.kind = resInfo->kind;
semanticInfo.index = UInt(hlslInfo.index);
semanticInfo.space = UInt(hlslInfo.space);
-
const LayoutSize count = resInfo->count;
addExplicitParameterBinding(context, parameterInfo, as<VarDeclBase>(varDecl.getDecl()), semanticInfo, count);
diff --git a/tests/hlsl/register-syntax.slang b/tests/hlsl/register-syntax.slang
new file mode 100644
index 000000000..f3633028d
--- /dev/null
+++ b/tests/hlsl/register-syntax.slang
@@ -0,0 +1,49 @@
+//TEST:SIMPLE(filecheck=CHECK_GLSL): -target glsl -stage compute -entry computeMain
+//TEST:SIMPLE(filecheck=CHECK_HLSL): -target hlsl -stage compute -entry computeMain
+
+//CHECK_GLSL-DAG: binding = 4, set = 2
+//CHECK_GLSL-DAG: binding = 5, set = 1
+//CHECK_GLSL-DAG: binding = 6
+//CHECK_GLSL-DAG: binding = 7
+//CHECK_GLSL-DAG: binding = 15
+//CHECK_GLSL-DAG: binding = 12
+
+//CHECK_HLSL-DAG: u4, space2
+//CHECK_HLSL-DAG: u5, space1
+//CHECK_HLSL-DAG: u6
+//CHECK_HLSL-DAG: u7
+//CHECK_HLSL-DAG: u9
+//CHECK_HLSL-DAG: u12
+
+[[vk::binding(4,2)]]
+RWStructuredBuffer<int> b0 : register(u4, space2);
+
+RWStructuredBuffer<int> b1 : register(u5, space1);
+
+RWStructuredBuffer<int> b2 : register(u6);
+
+[[vk::binding(7,0)]]
+RWStructuredBuffer<int> b3 : register(u7, space0);
+
+[[vk::binding(15,0)]]
+RWStructuredBuffer<int> b4[2] : register(u9);
+
+RWStructuredBuffer<int> b5[2] : register(u12);
+
+[numthreads(1, 1, 1)]
+void computeMain(int3 dispatchThreadID : SV_DispatchThreadID)
+{
+ int tid = dispatchThreadID.x;
+ b0[0] = 1;
+
+ b1[0] = 1;
+
+ b2[0] = 1;
+
+ b3[0] = 1;
+
+ b4[0][0] = 1;
+ b4[0][1] = 1;
+
+ b5[0][1] = 1;
+}