Skip to content

Commit 07279aa

Browse files
committed
Fix combined sampler documentation and warning
1 parent a5b1aa0 commit 07279aa

8 files changed

+86
-6
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-xxx-shift` used.")
2028+
20222029
//
20232030

20242031
// 4xxxx - IL code generation.

source/slang/slang-parameter-binding.cpp

+22-2
Original file line numberDiff line numberDiff line change
@@ -1078,9 +1078,23 @@ 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+
10811095
getSink(context)->diagnose(
10821096
registerModifier,
1083-
Diagnostics::registerModifierButNoVulkanLayout,
1097+
Diagnostics::registerModifierButNoVkBindingNorShift,
10841098
varDecl.getName());
10851099
}
10861100
}
@@ -1256,7 +1270,6 @@ static void addExplicitParameterBindings_GLSL(
12561270
return;
12571271
}
12581272

1259-
12601273
const auto hlslInfo = _extractLayoutSemanticInfo(context, hlslRegSemantic);
12611274
if (hlslInfo.kind == LayoutResourceKind::None)
12621275
{
@@ -1270,7 +1283,14 @@ static void addExplicitParameterBindings_GLSL(
12701283
if (auto textureType = as<TextureType>(varType))
12711284
{
12721285
if (textureType->isCombined())
1286+
{
1287+
if (!warnedMissingVulkanLayoutModifier)
1288+
{
1289+
_maybeDiagnoseMissingVulkanLayoutModifier(context, varDecl.as<VarDeclBase>());
1290+
warnedMissingVulkanLayoutModifier = true;
1291+
}
12731292
return;
1293+
}
12741294
}
12751295

12761296
// Can we map to a Vulkan kind in principal?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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, show an appropriate
5+
// warning, even in the presence of -fvk-xxx-shift options. Warning should not recommend using -fvk-xxx-shift.
6+
7+
struct Data
8+
{
9+
float a;
10+
int b;
11+
};
12+
13+
// Neither vk::binding, nor register, no warning
14+
Sampler2D cs0;
15+
16+
// Only vk::binding, no warning
17+
[[vk::binding(0,0)]]
18+
Sampler2D cs1;
19+
20+
// Both vk::binding and register, no warning
21+
[[vk::binding(1,0)]]
22+
Sampler2D cs2 : register(s0): register(t0);
23+
24+
// Only register, should warn without recommending vk-xxx-shift, since that would not help map 2 d3d registers to one vk binding.
25+
Sampler2D cs3 : register(s1): register(t1);
26+
27+
float4 main() : SV_TARGET
28+
{
29+
return float4(1, 1, 1, 0);
30+
}
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-xxx-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-xxx-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

0 commit comments

Comments
 (0)