summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArielG-NV <159081215+ArielG-NV@users.noreply.github.com>2024-07-23 11:08:05 -0400
committerGitHub <noreply@github.com>2024-07-23 11:08:05 -0400
commitc28d8b6aec721fa3350fc52647f1572a353f6151 (patch)
treee4570b2b4c4bb3efaf7259fb543c056963f5bfe6
parent509bfd8bbaaf021507c4045b5fd9eaf43276dc0a (diff)
Warn if providing explicit bindings to a object using uniform locations (#4708)
fixes: #4700 Changes: * If a uniform object (which uses uniform locations) has explicit bindings we will warn to use `ConstantBuffer<T>` instead. We check for a warning specifically when we know an object uses uniform layouts because objects may not use a uniform-layout register even if tagged with `uniform`. A good example of this is `uniform ConstantBuffer<T>`.
-rw-r--r--source/slang/slang-diagnostic-defs.h1
-rw-r--r--source/slang/slang-parameter-binding.cpp2
-rw-r--r--tests/bugs/gh-4700.slang27
3 files changed, 30 insertions, 0 deletions
diff --git a/source/slang/slang-diagnostic-defs.h b/source/slang/slang-diagnostic-defs.h
index 1a863351c..5c508a541 100644
--- a/source/slang/slang-diagnostic-defs.h
+++ b/source/slang/slang-diagnostic-defs.h
@@ -416,6 +416,7 @@ DIAGNOSTIC(31101, Error, unknownImageFormatName, "unknown image format '$0'")
DIAGNOSTIC(31101, Error, unknownDiagnosticName, "unknown diagnostic '$0'")
DIAGNOSTIC(31102, Error, nonPositiveNumThreads, "expected a positive integer in 'numthreads' attribute, got '$0'")
DIAGNOSTIC(31103, Error, invalidWaveSize, "expected a power of 2 between 4 and 128, inclusive, in 'WaveSize' attribute, got '$0'")
+DIAGNOSTIC(31104, Warning, explicitUniformLocation, "Explicit binding of uniform locations is discouraged. Prefer 'ConstantBuffer<$0>' over 'uniform $0'")
DIAGNOSTIC(31120, Error, invalidAttributeTarget, "invalid syntax target for user defined attribute")
diff --git a/source/slang/slang-parameter-binding.cpp b/source/slang/slang-parameter-binding.cpp
index fd7fae430..6f8504a31 100644
--- a/source/slang/slang-parameter-binding.cpp
+++ b/source/slang/slang-parameter-binding.cpp
@@ -4097,6 +4097,8 @@ RefPtr<ProgramLayout> generateParameterBindings(
if( varLayout->typeLayout->FindResourceInfo(LayoutResourceKind::Uniform) )
{
needDefaultConstantBuffer = true;
+ if(varLayout->varDecl.getDecl()->hasModifier<GLSLBindingAttribute>() || varLayout->varDecl.getDecl()->hasModifier<GLSLLocationLayoutModifier>())
+ sink->diagnose(varLayout->varDecl, Diagnostics::explicitUniformLocation, as<VarDecl>(varLayout->varDecl).getDecl()->getType());
diagnoseGlobalUniform(&sharedContext, as<VarDeclBase>(varLayout->varDecl.getDecl()));
}
}
diff --git a/tests/bugs/gh-4700.slang b/tests/bugs/gh-4700.slang
new file mode 100644
index 000000000..44b06c6c0
--- /dev/null
+++ b/tests/bugs/gh-4700.slang
@@ -0,0 +1,27 @@
+//TEST:SIMPLE(filecheck=PASS): -entry computeMain -stage compute -target spirv -DPASS1
+//TEST:SIMPLE(filecheck=WARN): -entry computeMain -stage compute -target spirv -DWARN1
+//TEST:SIMPLE(filecheck=WARN): -entry computeMain -stage compute -target spirv -DWARN2
+
+//PASS-NOT: 31104
+#ifdef PASS
+[[vk_binding(0,0)]] uniform ConstantBuffer<uint> addresses;
+layout(location = 1) uniform ConstantBuffer<uint> addresses1;
+#endif
+
+//WARN: 31104
+#ifdef WARN1
+[[vk_binding(0,0)]] uniform uint addresses;
+[[vk_binding(1,0)]] uniform uint addresses1;
+#endif
+#ifdef WARN2
+layout(location = 0) uniform uint addresses;
+layout(location = 1) uniform uint addresses1;
+#endif
+
+[shader("compute")]
+[numthreads(1, 1, 1)]
+void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID, uint groupIndex: SV_GroupIndex)
+{
+ printf("\nfrom gpu: %u", addresses);
+ printf("\nfrom gpu: %u", addresses1);
+} \ No newline at end of file