diff options
| author | Tim Foley <tfoleyNV@users.noreply.github.com> | 2020-01-28 12:35:13 -0800 |
|---|---|---|
| committer | Tim Foley <tim.foley.is@gmail.com> | 2020-01-28 12:35:13 -0800 |
| commit | 8b3e3beea66d9773adf11ea2e163577d649f3d7c (patch) | |
| tree | 51b4f3967d5db5494f98f6f9bf870ef2096929f5 /source/slang/slang-emit.cpp | |
| parent | b3e0b0d491c55bfdc1c40d26a421910103c1b9f2 (diff) | |
Fix layout for structured buffers of matrices (#1184)
When using row-major layout (via command-line or API option), the following sort of declaration:
```hlsl
StructuredBuffer<float4x4> gBuffer;
... gBuffer[i] ...
```
Generates unexpected results when compiled to DXBC via fxc or DXIL via dxc, because the fxc/dxc compilers do not respect the matrix layout mode in this specific case (a structured buffer of matrices). Instead, they always use column-major layout, even if row-major was requested by the user.
A user can work around this behavior by wrapping the matrix in a `struct`:
```hlsl
struct Wrapper { float4x4 wrapped; }
SturcturedBuffer<Wrapper> gBuffer;
... gBuffer[i].wrapped ...
```
This change simply automates that workaround when compiling for an HLSL-based downstream compiler, so that we get the same behavior across all our backends.
The change adds a test case to confirm the behavior across multiple targets, but it turns out we also had a test checked in that confirmed the buggy (or at least surprising) fxc/dxc behavior, so that one had its baselines changed and can work as a regression test for this fix as well.
Diffstat (limited to 'source/slang/slang-emit.cpp')
| -rw-r--r-- | source/slang/slang-emit.cpp | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/source/slang/slang-emit.cpp b/source/slang/slang-emit.cpp index 2a216994b..6e3ce9c56 100644 --- a/source/slang/slang-emit.cpp +++ b/source/slang/slang-emit.cpp @@ -15,6 +15,7 @@ #include "slang-ir-ssa.h" #include "slang-ir-union.h" #include "slang-ir-validate.h" +#include "slang-ir-wrap-structured-buffers.h" #include "slang-legalize-types.h" #include "slang-lower-to-ir.h" #include "slang-mangle.h" @@ -384,6 +385,28 @@ Result linkAndOptimizeIR( #endif validateIRModuleIfEnabled(compileRequest, irModule); + // For HLSL (and fxc/dxc) only, we need to "wrap" any + // structured buffers defined over matrix types so + // that they instead use an intermediate `struct`. + // This is required to get those targets to respect + // the options for matrix layout set via `#pragma` + // or command-line options. + // + switch(target) + { + case CodeGenTarget::HLSL: + { + wrapStructuredBuffersOfMatrices(irModule); +#if 0 + dumpIRIfEnabled(compileRequest, irModule, "STRUCTURED BUFFERS WRAPPED"); +#endif + validateIRModuleIfEnabled(compileRequest, irModule); + } + break; + + default: + break; + } // For GLSL only, we will need to perform "legalization" of // the entry point and any entry-point parameters. |
