Skip to content

Commit 2ba6458

Browse files
cheneym2slangbot
andauthored
Fix combined sampler documentation and warning (#6207)
* Fix combined sampler documentation and warning * Update comment, show detailed '-fvk-t-shift' message in warning instead of generic '-fvk-xxx-shift' * format code --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com>
1 parent 6052043 commit 2ba6458

9 files changed

+98
-8
lines changed

docs/user-guide/a2-01-spirv-target-specific.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ Combined texture sampler
2020
Slang supports Combined texture sampler such as `Sampler2D`.
2121
Slang emits SPIR-V code with `OpTypeSampledImage` instruction.
2222

23-
You can specify two different register numbers for each: one for the texture register and another for the sampler register.
23+
For SPIR-V targets, explicit bindings may be provided through a single `vk::binding` decoration.
24+
```
25+
[[vk::binding(1,2)]]
26+
Sampler2D explicitBindingSampler;
27+
```
28+
29+
For other targets (HLSL or others) where combined texture samplers are _not_ supported intrinsicly, they are emulated by Slang using separate objects for Texture and Sampler.
30+
For explicit binding on such targets, you can specify two different register numbers for each: one for the texture register and another for the sampler register.
2431
```
2532
Sampler2D explicitBindingSampler : register(t4): register(s3);
2633
```

source/slang/slang-diagnostic-defs.h

+7
Original file line numberDiff line numberDiff line change
@@ -2019,6 +2019,13 @@ DIAGNOSTIC(
20192019
notValidVaryingParameter,
20202020
"parameter '$0' is not a valid varying parameter.")
20212021

2022+
DIAGNOSTIC(
2023+
39029,
2024+
Warning,
2025+
registerModifierButNoVkBindingNorShift,
2026+
"shader parameter '$0' has a 'register' specified for D3D, but no '[[vk::binding(...)]]` "
2027+
"specified for Vulkan, nor is `-fvk-$1-shift` used.")
2028+
20222029
//
20232030

20242031
// 4xxxx - IL code generation.

source/slang/slang-parameter-binding.cpp

+31-3
Original file line numberDiff line numberDiff line change
@@ -1078,10 +1078,32 @@ static void _maybeDiagnoseMissingVulkanLayoutModifier(
10781078
// oversight on their part.
10791079
if (auto registerModifier = varDecl.getDecl()->findModifier<HLSLRegisterSemantic>())
10801080
{
1081+
auto varType = getType(context->getASTBuilder(), varDecl.as<VarDeclBase>());
1082+
if (auto textureType = as<TextureType>(varType))
1083+
{
1084+
if (textureType->isCombined())
1085+
{
1086+
// Recommend [[vk::binding]] but not '-fvk-xxx-shift` for combined texture samplers
1087+
getSink(context)->diagnose(
1088+
registerModifier,
1089+
Diagnostics::registerModifierButNoVulkanLayout,
1090+
varDecl.getName());
1091+
return;
1092+
}
1093+
}
1094+
1095+
UnownedStringSlice registerClassName;
1096+
UnownedStringSlice registerIndexDigits;
1097+
splitNameAndIndex(
1098+
registerModifier->registerName.getContent(),
1099+
registerClassName,
1100+
registerIndexDigits);
1101+
10811102
getSink(context)->diagnose(
10821103
registerModifier,
1083-
Diagnostics::registerModifierButNoVulkanLayout,
1084-
varDecl.getName());
1104+
Diagnostics::registerModifierButNoVkBindingNorShift,
1105+
varDecl.getName(),
1106+
registerClassName);
10851107
}
10861108
}
10871109

@@ -1256,7 +1278,6 @@ static void addExplicitParameterBindings_GLSL(
12561278
return;
12571279
}
12581280

1259-
12601281
const auto hlslInfo = _extractLayoutSemanticInfo(context, hlslRegSemantic);
12611282
if (hlslInfo.kind == LayoutResourceKind::None)
12621283
{
@@ -1270,7 +1291,14 @@ static void addExplicitParameterBindings_GLSL(
12701291
if (auto textureType = as<TextureType>(varType))
12711292
{
12721293
if (textureType->isCombined())
1294+
{
1295+
if (!warnedMissingVulkanLayoutModifier)
1296+
{
1297+
_maybeDiagnoseMissingVulkanLayoutModifier(context, varDecl.as<VarDeclBase>());
1298+
warnedMissingVulkanLayoutModifier = true;
1299+
}
12731300
return;
1301+
}
12741302
}
12751303

12761304
// Can we map to a Vulkan kind in principal?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//DIAGNOSTIC_TEST:SIMPLE:-target glsl -profile ps_4_0 -entry main -fvk-t-shift 5 all -fvk-t-shift 7 2 -fvk-s-shift -3 0 -fvk-u-shift 1 2 -no-codegen
2+
//DIAGNOSTIC_TEST:SIMPLE:-target glsl -profile ps_4_0 -entry main -no-codegen
3+
4+
// This tests that combined texture sampler objects which have D3D style register assignments, but no vk::binding,
5+
// show an appropriate warning.
6+
// The warning should appear even if the user used -fvk-xxx-shift options, because those options do not serve to map
7+
// two register assignments of different types into a single vulkan binding.
8+
9+
struct Data
10+
{
11+
float a;
12+
int b;
13+
};
14+
15+
// Neither vk::binding, nor register, no warning
16+
Sampler2D cs0;
17+
18+
// Only vk::binding, no warning
19+
[[vk::binding(0,0)]]
20+
Sampler2D cs1;
21+
22+
// Both vk::binding and register, no warning
23+
[[vk::binding(1,0)]]
24+
Sampler2D cs2 : register(s0): register(t0);
25+
26+
// Only register, should warn without recommending vk-xxx-shift, since that would not help map 2 d3d registers to one vk binding.
27+
Sampler2D cs3 : register(s1): register(t1);
28+
29+
float4 main() : SV_TARGET
30+
{
31+
return float4(1, 1, 1, 0);
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
result code = 0
2+
standard error = {
3+
tests/diagnostics/hlsl-to-vulkan-sampler-diagnostic.hlsl(25): warning 39013: shader parameter 'cs3' has a 'register' specified for D3D, but no '[[vk::binding(...)]]` specified for Vulkan
4+
Sampler2D cs3 : register(s1): register(t1);
5+
^~~~~~~~
6+
}
7+
standard output = {
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
result code = 0
2+
standard error = {
3+
tests/diagnostics/hlsl-to-vulkan-sampler-diagnostic.hlsl(25): warning 39013: shader parameter 'cs3' has a 'register' specified for D3D, but no '[[vk::binding(...)]]` specified for Vulkan
4+
Sampler2D cs3 : register(s1): register(t1);
5+
^~~~~~~~
6+
}
7+
standard output = {
8+
}

tests/diagnostics/hlsl-to-vulkan-shift-diagnostic.hlsl

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ RWStructuredBuffer<int> u2 : register(u3, space2);
1818
float4 main() : SV_TARGET
1919
{
2020
return float4(1, 1, 1, 0);
21-
}
21+
}

tests/diagnostics/hlsl-to-vulkan-shift-diagnostic.hlsl.expected

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
result code = -1
22
standard error = {
3-
tests/diagnostics/hlsl-to-vulkan-shift-diagnostic.hlsl(11): warning 39013: shader parameter 'c' has a 'register' specified for D3D, but no '[[vk::binding(...)]]` specified for Vulkan
3+
tests/diagnostics/hlsl-to-vulkan-shift-diagnostic.hlsl(11): warning 39029: shader parameter 'c' has a 'register' specified for D3D, but no '[[vk::binding(...)]]` specified for Vulkan, nor is `-fvk-b-shift` used.
44
ConstantBuffer<Data> c : register(b2);
55
^~~~~~~~
6-
tests/diagnostics/hlsl-to-vulkan-shift-diagnostic.hlsl(15): warning 39013: shader parameter 'u' has a 'register' specified for D3D, but no '[[vk::binding(...)]]` specified for Vulkan
6+
tests/diagnostics/hlsl-to-vulkan-shift-diagnostic.hlsl(15): warning 39029: shader parameter 'u' has a 'register' specified for D3D, but no '[[vk::binding(...)]]` specified for Vulkan, nor is `-fvk-u-shift` used.
77
RWStructuredBuffer<Data> u : register(u11);
88
^~~~~~~~
99
tests/diagnostics/hlsl-to-vulkan-shift-diagnostic.hlsl(15): error 39025: conflicting vulkan inferred binding for parameter 'c' overlap is 0 and 0

tests/diagnostics/vk-bindings.slang.expected

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
result code = -1
22
standard error = {
3-
tests/diagnostics/vk-bindings.slang(6): warning 39013: shader parameter 't' has a 'register' specified for D3D, but no '[[vk::binding(...)]]` specified for Vulkan
3+
tests/diagnostics/vk-bindings.slang(6): warning 39029: shader parameter 't' has a 'register' specified for D3D, but no '[[vk::binding(...)]]` specified for Vulkan, nor is `-fvk-t-shift` used.
44
Texture2D t : register(t0);
55
^~~~~~~~
66
tests/diagnostics/vk-bindings.slang(14): error 39015: shader parameter 'b' consumes whole descriptor sets, so the binding must be in the form '[[vk::binding(0, ...)]]'; the non-zero binding '2' is not allowed

0 commit comments

Comments
 (0)