Skip to content

Commit 8b3e3be

Browse files
Tim Foleytangent-vector
Tim Foley
authored andcommitted
Fix layout for structured buffers of matrices (shader-slang#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.
1 parent b3e0b0d commit 8b3e3be

14 files changed

+509
-27
lines changed

source/slang/slang-emit.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "slang-ir-ssa.h"
1616
#include "slang-ir-union.h"
1717
#include "slang-ir-validate.h"
18+
#include "slang-ir-wrap-structured-buffers.h"
1819
#include "slang-legalize-types.h"
1920
#include "slang-lower-to-ir.h"
2021
#include "slang-mangle.h"
@@ -384,6 +385,28 @@ Result linkAndOptimizeIR(
384385
#endif
385386
validateIRModuleIfEnabled(compileRequest, irModule);
386387

388+
// For HLSL (and fxc/dxc) only, we need to "wrap" any
389+
// structured buffers defined over matrix types so
390+
// that they instead use an intermediate `struct`.
391+
// This is required to get those targets to respect
392+
// the options for matrix layout set via `#pragma`
393+
// or command-line options.
394+
//
395+
switch(target)
396+
{
397+
case CodeGenTarget::HLSL:
398+
{
399+
wrapStructuredBuffersOfMatrices(irModule);
400+
#if 0
401+
dumpIRIfEnabled(compileRequest, irModule, "STRUCTURED BUFFERS WRAPPED");
402+
#endif
403+
validateIRModuleIfEnabled(compileRequest, irModule);
404+
}
405+
break;
406+
407+
default:
408+
break;
409+
}
387410

388411
// For GLSL only, we will need to perform "legalization" of
389412
// the entry point and any entry-point parameters.

source/slang/slang-ir-insts.h

+3
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,9 @@ struct IRBuilder
17701770
IRInst* const* operands);
17711771
IRType* getType(
17721772
IROp op);
1773+
IRType* getType(
1774+
IROp op,
1775+
IRInst* operand0);
17731776

17741777
/// Create an empty basic block.
17751778
///

0 commit comments

Comments
 (0)