summaryrefslogtreecommitdiffstats
path: root/tests/bindings
diff options
context:
space:
mode:
authorTim Foley <tfoleyNV@users.noreply.github.com>2019-12-06 09:29:09 -0800
committerGitHub <noreply@github.com>2019-12-06 09:29:09 -0800
commit54d3c5f6657b1099326e1ce7ec0f0692e7025442 (patch)
treeaa9baed99939fc3b068f3c330e4a59f5147020e4 /tests/bindings
parent4e2cfc95fb02fb47f02b8702494929e7cca3bec7 (diff)
Remove legacy feature for merging global shader parameters (#1139)
* Remove legacy feature for merging global shader parameters There is a fair amount of special-case code in the Slang compiler today to deal with the scenario where a programmer declares the "same" shader parameter across two different translation units: ```hlsl // a.hlsl Texture2D a; cbuffer C { float4 c; } ``` ```hlsl // b.hlsl cbuffer C { float4 c; } Texture2D b; ``` An important note here is that the declaration of `C` may be in a header file that both `a.hlsl` and `b.hlsl` `#include`, because from the standpoint of the parser and later stages of the compiler, there is no difference between `C` being in an included file vs. it being copy-pasted across both `a.hlsl` and `b.hlsl`. When a user invokes `slangc a.hlsl b.hlsl` (or the equivalent via the API), then they may decide that it is "obvious" that the shader parameter `C` is the "same" in both `a.hlsl` and `b.hlsl`. Knowing that the parameter is the "same" may lead them to make certain assumptions: * They may assume that generated code for entry points in `a.hlsl` and `b.hlsl` will both agree on the exact `register`/`binding` occupied by `C`. * They may assume that reflection information for their program will only reflect `C` once, and it will reflect it in a way that is applicable to entry points in both `a.hlsl` and `b.hlsl` * They may assume that the compiler can and should handle this use case even when `C` contains fields with `struct` types that are declared in both `a.hlsl` and `b.hlsl` that have the "same" definition. * They may assume that in cases where `C` is declared inconsistently between `a.hlsl` and `b.hlsl` the compiler can and will diagnose an error. Making these assumptions work in practice required a lot of special-case code: * When composing/linking programs was `ComponentType`s we had to include a special case `LegacyProgram` type that could provide these "do what I mean" semantics, since they are *not* what one would want in the general case for a `CompositeComponentType`. * During enumeration of global shader parameter in a `LegacyProgram`, we had to detect parameters from distinct modules (translation units) with the same name, and then enforce that they must have the "same" type (via an ad hoc recursive structural type match). No other semantic checking logic needs or uses that kind of structural check. * During parameter binding generation, we need to handle the case where a single global shader parameter might have multiple declarations, and make sure to collect explicit bindings from all of them (checking for inconsistency) and also to apply generated bindings to all of them. * The `mapVarToLayout` member in `StructTypeLayout` is a concession to the fact that we might have multiple `VarDecl`s for each field of the struct that represents the global scope, we might need to look up a field and its layout using any of those declarations (much of the need for this field had gone away now that IR passes are largely using IR-based layout). All of these different special cases added more complex code in many places in the compiler, all to support a scenario that isn't especially common. Most users won't be affected by the original issue, because they will do one of several things that rule it out: * Anybody using `slangc` like a stand-in for `fxc` or `dxc` and compiling one translation unit at a time will not suffer from any problems. If/when such users want consistent bindings across translation units, they already use either explicit binding or rely on consistent ordering and implicit binding. * Anybody who puts all the entry points that get combined into a pass/pipeline in a single file will not have problems. They will automatically get consistent bindings because of Slang's guarantees, and there can't be duplicated declarations when there is only one translation unit. * Anybody using `import` to factor out common declarations while compiling multiple translation units at once will not be affected. Parameters declared in an `import`ed module are the "same" in a much deeper way that it is trivial for Slang to support. Only users of the Falcor framework are likely to be affected by this, and they have two easy migration paths: either put related entry points into the same file, or factor common parameters into an `import`ed module. (It is also worth noting that for command-line `slangc`, it is possible to have a single module with multiple `.slang` files in it, which can all see global declarations like parameters across all the files. Anybody who buys into doing things the Slang Way should have no problem avoiding duplicated declarations) With the rationale out of the way, the actual change mostly just amounts to deleting lots of code that is no longer needed. An astute reviewer might notice several `assert`-fail conditions where complex Slang features were never actually made to work correctly with this legacy behavior. A small number of test cases broke with the code changes, but these were tests that specifically exercised the behavior being removed. In the case of the tests around binding/reflection generating, I rewrote the tests to use one of the idomatic workarounds (putting the shared parameters into an `import`ed module), but doing so required me to add support for `#include` when doing pass-through compilation with `fxc`. That logic added a bit more cruft than I had originally hoped to this commit, but having `#include` support when doing pass-through compilation is probably a net win. * fixup: 64-bit warning
Diffstat (limited to 'tests/bindings')
-rw-r--r--tests/bindings/multi-file-defines.h44
-rw-r--r--tests/bindings/multi-file-extra.hlsl77
-rw-r--r--tests/bindings/multi-file-shared.slang25
-rw-r--r--tests/bindings/multi-file.hlsl80
4 files changed, 83 insertions, 143 deletions
diff --git a/tests/bindings/multi-file-defines.h b/tests/bindings/multi-file-defines.h
new file mode 100644
index 000000000..52a5826b6
--- /dev/null
+++ b/tests/bindings/multi-file-defines.h
@@ -0,0 +1,44 @@
+// multi-file-defines.h
+
+#ifdef __SLANG__
+#define R(X) /**/
+#define BEGIN_CBUFFER(NAME) cbuffer NAME
+#define END_CBUFFER(NAME, REG) /**/
+#define CBUFFER_REF(NAME, FIELD) FIELD
+#else
+#define R(X) X
+#define BEGIN_CBUFFER(NAME) struct SLANG_ParameterGroup_##NAME
+#define END_CBUFFER(NAME, REG) ; cbuffer NAME : REG { SLANG_ParameterGroup_##NAME NAME; }
+#define CBUFFER_REF(NAME, FIELD) NAME.FIELD
+
+#define sharedC sharedC_0
+#define sharedCA sharedCA_0
+#define sharedCB sharedCB_0
+#define sharedCC sharedCC_0
+#define sharedCD sharedCD_0
+
+#define vertexC vertexC_0
+#define vertexCA vertexCA_0
+#define vertexCB vertexCB_0
+#define vertexCC vertexCC_0
+#define vertexCD vertexCD_0
+
+#define fragmentC fragmentC_0
+#define fragmentCA fragmentCA_0
+#define fragmentCB fragmentCB_0
+#define fragmentCC fragmentCC_0
+#define fragmentCD fragmentCD_0
+
+#define sharedS sharedS_0
+#define sharedT sharedT_0
+#define sharedTV sharedTV_0
+#define sharedTF sharedTF_0
+
+#define vertexS vertexS_0
+#define vertexT vertexT_0
+
+#define fragmentS fragmentS_0
+#define fragmentT fragmentT_0
+
+#endif
+
diff --git a/tests/bindings/multi-file-extra.hlsl b/tests/bindings/multi-file-extra.hlsl
index fe8766dcd..9be7a34a0 100644
--- a/tests/bindings/multi-file-extra.hlsl
+++ b/tests/bindings/multi-file-extra.hlsl
@@ -5,74 +5,16 @@
// This file provides the fragment shader, and is only meant to be tested in combination with `multi-file.hlsl`
+#include "multi-file-defines.h"
+
#ifdef __SLANG__
-#define R(X) /**/
-#define BEGIN_CBUFFER(NAME) cbuffer NAME
-#define END_CBUFFER(NAME, REG) /**/
-#define CBUFFER_REF(NAME, FIELD) FIELD
+import multi_file_shared;
#else
-#define R(X) X
-#define BEGIN_CBUFFER(NAME) struct SLANG_ParameterGroup_##NAME
-#define END_CBUFFER(NAME, REG) ; cbuffer NAME : REG { SLANG_ParameterGroup_##NAME NAME; }
-#define CBUFFER_REF(NAME, FIELD) NAME.FIELD
-
-#define sharedC sharedC_0
-#define sharedCA sharedCA_0
-#define sharedCB sharedCB_0
-#define sharedCC sharedCC_0
-#define sharedCD sharedCD_0
-
-#define vertexC vertexC_0
-#define vertexCA vertexCA_0
-#define vertexCB vertexCB_0
-#define vertexCC vertexCC_0
-#define vertexCD vertexCD_0
-
-#define fragmentC fragmentC_0
-#define fragmentCA fragmentCA_0
-#define fragmentCB fragmentCB_0
-#define fragmentCC fragmentCC_0
-#define fragmentCD fragmentCD_0
-
-#define sharedS sharedS_0
-#define sharedT sharedT_0
-#define sharedTV sharedTV_0
-#define sharedTF sharedTF_0
-
-#define vertexS vertexS_0
-#define vertexT vertexT_0
-
-#define fragmentS fragmentS_0
-#define fragmentT fragmentT_0
-
+#include "multi-file-shared.slang"
#endif
-float4 use(float val) { return val; };
-float4 use(float2 val) { return float4(val,0.0,0.0); };
-float4 use(float3 val) { return float4(val,0.0); };
-float4 use(float4 val) { return val; };
-float4 use(Texture2D t, SamplerState s) { return t.Sample(s, 0.0); }
-
-// Start with some parameters that will appear in both shaders
-Texture2D sharedT R(: register(t0));
-SamplerState sharedS R(: register(s0));
-
-BEGIN_CBUFFER(sharedC)
-{
- float3 sharedCA;
- float sharedCB;
- float3 sharedCC;
- float2 sharedCD;
-}
-END_CBUFFER(sharedC, register(b0))
-
-// Then some parameters specific to this shader.
-// These will be placed *after* the ones from the main file,
-// and even after the parameters further down in this file
-// that end up being shared between the two files.
-
-Texture2D fragmentT R(: register(t4));
-SamplerState fragmentS R(: register(s2));
+Texture2D fragmentT R(: register(t1));
+SamplerState fragmentS R(: register(s1));
BEGIN_CBUFFER(fragmentC)
{
@@ -81,12 +23,7 @@ BEGIN_CBUFFER(fragmentC)
float3 fragmentCC;
float2 fragmentCD;
}
-END_CBUFFER(fragmentC, register(b2))
-
-// And end with some shared parameters again
-Texture2D sharedTV R(: register(t2));
-Texture2D sharedTF R(: register(t3));
-
+END_CBUFFER(fragmentC, register(b1))
float4 main() : SV_TARGET
{
diff --git a/tests/bindings/multi-file-shared.slang b/tests/bindings/multi-file-shared.slang
new file mode 100644
index 000000000..af91d5251
--- /dev/null
+++ b/tests/bindings/multi-file-shared.slang
@@ -0,0 +1,25 @@
+// multi-file-shared.slang
+//TEST_IGNORE_FILE:
+
+#include "multi-file-defines.h"
+
+float4 use(float val) { return val; };
+float4 use(float2 val) { return float4(val,0.0,0.0); };
+float4 use(float3 val) { return float4(val,0.0); };
+float4 use(float4 val) { return val; };
+float4 use(Texture2D t, SamplerState s) { return t.SampleLevel(s, 0.0, 0.0); }
+
+Texture2D sharedT R(: register(t2));
+SamplerState sharedS R(: register(s2));
+
+BEGIN_CBUFFER(sharedC)
+{
+ float3 sharedCA;
+ float sharedCB;
+ float3 sharedCC;
+ float2 sharedCD;
+}
+END_CBUFFER(sharedC, register(b2))
+
+Texture2D sharedTV R(: register(t3));
+Texture2D sharedTF R(: register(t4));
diff --git a/tests/bindings/multi-file.hlsl b/tests/bindings/multi-file.hlsl
index 8c719bbcf..73ce181b0 100644
--- a/tests/bindings/multi-file.hlsl
+++ b/tests/bindings/multi-file.hlsl
@@ -6,77 +6,16 @@
// This file provides the vertex shader, while the fragment shader resides in
// the file `multi-file-extra.hlsl`
+#include "multi-file-defines.h"
+
#ifdef __SLANG__
-#define R(X) /**/
-#define BEGIN_CBUFFER(NAME) cbuffer NAME
-#define END_CBUFFER(NAME, REG) /**/
-#define CBUFFER_REF(NAME, FIELD) FIELD
+import multi_file_shared;
#else
-#define R(X) X
-#define BEGIN_CBUFFER(NAME) struct SLANG_ParameterGroup_##NAME
-#define END_CBUFFER(NAME, REG) ; cbuffer NAME : REG { SLANG_ParameterGroup_##NAME NAME; }
-#define CBUFFER_REF(NAME, FIELD) NAME.FIELD
-
-#define sharedC sharedC_0
-#define sharedCA sharedCA_0
-#define sharedCB sharedCB_0
-#define sharedCC sharedCC_0
-#define sharedCD sharedCD_0
-
-#define vertexC vertexC_0
-#define vertexCA vertexCA_0
-#define vertexCB vertexCB_0
-#define vertexCC vertexCC_0
-#define vertexCD vertexCD_0
-
-#define fragmentC fragmentC_0
-#define fragmentCA fragmentCA_0
-#define fragmentCB fragmentCB_0
-#define fragmentCC fragmentCC_0
-#define fragmentCD fragmentCD_0
-
-#define sharedS sharedS_0
-#define sharedT sharedT_0
-#define sharedTV sharedTV_0
-#define sharedTF sharedTF_0
-
-#define vertexS vertexS_0
-#define vertexT vertexT_0
-
-#define fragmentS fragmentS_0
-#define fragmentT fragmentT_0
-
+#include "multi-file-shared.slang"
#endif
-float4 use(float val) { return val; };
-float4 use(float2 val) { return float4(val,0.0,0.0); };
-float4 use(float3 val) { return float4(val,0.0); };
-float4 use(float4 val) { return val; };
-float4 use(Texture2D t, SamplerState s)
-{
- // This is the vertex shader, so we can't do implicit-gradient sampling
- return t.SampleGrad(s, 0.0, 0.0, 0.0);
-}
-
-// Start with some parameters that will appear in both shaders
-Texture2D sharedT R(: register(t0));
-SamplerState sharedS R(: register(s0));
-
-BEGIN_CBUFFER(sharedC)
-{
- float3 sharedCA;
- float sharedCB;
- float3 sharedCC;
- float2 sharedCD;
-}
-END_CBUFFER(sharedC, register(b0))
-
-// Then some parameters specific to this shader
-// (these will get placed before the ones in the `extra` file,
-// based on how they get named on the command-line)
-
-Texture2D vertexT R(: register(t1));
-SamplerState vertexS R(: register(s1));
+Texture2D vertexT R(: register(t0));
+SamplerState vertexS R(: register(s0));
BEGIN_CBUFFER(vertexC)
{
@@ -85,12 +24,7 @@ BEGIN_CBUFFER(vertexC)
float3 vertexCC;
float2 vertexCD;
}
-END_CBUFFER(vertexC, register(b1))
-
-// And end with some shared parameters again
-Texture2D sharedTV R(: register(t2));
-Texture2D sharedTF R(: register(t3));
-
+END_CBUFFER(vertexC, register(b0))
float4 main() : SV_POSITION
{