From a88055c6f5190ca62bb4aa853b4f0fa11546278f Mon Sep 17 00:00:00 2001 From: Yong He Date: Thu, 5 Sep 2024 10:26:59 -0700 Subject: Respect matrix layout in uniform and in/out parameters for HLSL target. (#5013) * Respect matrix layout in uniform and in/out parameters for HLSL target. * Update test. * Fix test. * fix test. * Fix metal layout calculation. * Fix compile error. * Fix compiler error. --------- Co-authored-by: Yong He --- tests/compute/constant-buffer-memory-packing.slang | 93 +++++++++++----------- .../types/matrix-layout-uniform.slang | 19 +++++ 2 files changed, 64 insertions(+), 48 deletions(-) create mode 100644 tests/language-feature/types/matrix-layout-uniform.slang (limited to 'tests') diff --git a/tests/compute/constant-buffer-memory-packing.slang b/tests/compute/constant-buffer-memory-packing.slang index 5246c4d33..fb7a78fc1 100644 --- a/tests/compute/constant-buffer-memory-packing.slang +++ b/tests/compute/constant-buffer-memory-packing.slang @@ -3,31 +3,31 @@ // Metal/CPP/CUDA do not deal with packing currently, different results will occur. //TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-cpu -compute -xslang -DTARGET_WITHOUT_PACKING //TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-cuda -compute -xslang -DTARGET_WITHOUT_PACKING -//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-mtl -compute -xslang -DTARGET_WITHOUT_PACKING -//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-slang -compute -dx12 -//TEST(compute, vulkan):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-vk -compute //TEST(compute, vulkan):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-vk -compute -emit-spirv-via-glsl +//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-slang -compute +//TEST(compute, vulkan):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-vk -compute +//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=BUF):-mtl -compute -// CPP/Metal/CUDA due to not having memory packing will recieve the following ROW matrix: +// CPP/CUDA due to natural layout rules will recieve the following ROW matrix: // {1,2,3} // {0,4,5} // {6,0,7} -// GLSL/SPIRV/HLSL due to having memory packing will recieve the following ROW/COL matrix: +// GLSL/SPIRV/HLSL/Metal with cbuffer layout rules will recieve the following ROW/COL matrix: // {1,2,3} -// {0,4,5} -// {6,0,7} +// {4,5,6} +// {7,8,9} -//TEST_INPUT:cbuffer(data=[1.0 2.0 3.0 0.0 4.0 5.0 6.0 0.0 7.0 8.0 9.0 0]):name matrixTestCBuf1 +//TEST_INPUT:cbuffer(data=[1.0 2.0 3.0 0.0 4.0 5.0 6.0 0.0 7.0 8.0 9.0 0.0]):name matrixTestCBuf1 ConstantBuffer matrixTestCBuf1; // CPP/Metal/CUDA due to not having memory packing will recieve the following COL matrix post-transpose: -// {1,0,8} -// {4,2,0} -// {7,5,3} +// {1,0,6} +// {2,4,0} +// {3,5,7} -//TEST_INPUT:cbuffer(data=[1.0 4.0 7.0 0.0 2.0 5.0 8.0 0.0 3.0 6.0 9.0 0.0]):name matrixTestCBuf2 +//TEST_INPUT:cbuffer(data=[1.0 2.0 3.0 0.0 4.0 5.0 6.0 0.0 7.0 8.0 9.0 0.0]):name matrixTestCBuf2 ConstantBuffer matrixTestCBuf2; //TEST_INPUT:cbuffer(data=[1.0 2.0 3.0 0.0 4.0 5.0 6.0 0.0]):name NeedsPadding @@ -55,34 +55,7 @@ void computeMain(uint3 tid : SV_DispatchThreadID) matrixTest2 = matrixTestCBuf2; output[0] = uint(true -#ifndef TARGET_WITHOUT_PACKING - && floatCheck(matrixTest1[0][0], 1) - && floatCheck(matrixTest1[0][1], 2) - && floatCheck(matrixTest1[0][2], 3) - && floatCheck(matrixTest1[1][0], 4) - && floatCheck(matrixTest1[1][1], 5) - && floatCheck(matrixTest1[1][2], 6) - && floatCheck(matrixTest1[2][0], 7) - && floatCheck(matrixTest1[2][1], 8) - && floatCheck(matrixTest1[2][2], 9) - - && floatCheck(matrixTest2[0][0], 1) - && floatCheck(matrixTest2[0][1], 2) - && floatCheck(matrixTest2[0][2], 3) - && floatCheck(matrixTest2[1][0], 4) - && floatCheck(matrixTest2[1][1], 5) - && floatCheck(matrixTest2[1][2], 6) - && floatCheck(matrixTest2[2][0], 7) - && floatCheck(matrixTest2[2][1], 8) - && floatCheck(matrixTest2[2][2], 9) - - && floatCheck(data1[0], 1) - && floatCheck(data1[1], 2) - && floatCheck(data1[2], 3) - && floatCheck(data2[0], 4) - && floatCheck(data2[1], 5) - && floatCheck(data2[2], 6) -#else +#ifdef TARGET_WITHOUT_PACKING && floatCheck(matrixTest1[0][0], 1) && floatCheck(matrixTest1[0][1], 2) && floatCheck(matrixTest1[0][2], 3) @@ -95,13 +68,13 @@ void computeMain(uint3 tid : SV_DispatchThreadID) && floatCheck(matrixTest2[0][0], 1) && floatCheck(matrixTest2[0][1], 0) - && floatCheck(matrixTest2[0][2], 8) - && floatCheck(matrixTest2[1][0], 4) - && floatCheck(matrixTest2[1][1], 2) + && floatCheck(matrixTest2[0][2], 6) + && floatCheck(matrixTest2[1][0], 2) + && floatCheck(matrixTest2[1][1], 4) && floatCheck(matrixTest2[1][2], 0) - && floatCheck(matrixTest2[2][0], 7) + && floatCheck(matrixTest2[2][0], 3) && floatCheck(matrixTest2[2][1], 5) - && floatCheck(matrixTest2[2][2], 3) + && floatCheck(matrixTest2[2][2], 7) && floatCheck(data1[0], 1) && floatCheck(data1[1], 2) @@ -109,10 +82,34 @@ void computeMain(uint3 tid : SV_DispatchThreadID) && floatCheck(data2[0], 0) && floatCheck(data2[1], 4) && floatCheck(data2[2], 5) +#else + && floatCheck(matrixTest1[0][0], 1) + && floatCheck(matrixTest1[0][1], 2) + && floatCheck(matrixTest1[0][2], 3) + && floatCheck(matrixTest1[1][0], 4) + && floatCheck(matrixTest1[1][1], 5) + && floatCheck(matrixTest1[1][2], 6) + && floatCheck(matrixTest1[2][0], 7) + && floatCheck(matrixTest1[2][1], 8) + && floatCheck(matrixTest1[2][2], 9) + + && floatCheck(matrixTest2[0][0], 1) + && floatCheck(matrixTest2[0][1], 4) + && floatCheck(matrixTest2[0][2], 7) + && floatCheck(matrixTest2[1][0], 2) + && floatCheck(matrixTest2[1][1], 5) + && floatCheck(matrixTest2[1][2], 8) + && floatCheck(matrixTest2[2][0], 3) + && floatCheck(matrixTest2[2][1], 6) + && floatCheck(matrixTest2[2][2], 9) + + && floatCheck(data1[0], 1) + && floatCheck(data1[1], 2) + && floatCheck(data1[2], 3) + && floatCheck(data2[0], 4) + && floatCheck(data2[1], 5) + && floatCheck(data2[2], 6) #endif ); - output[1] = (uint)matrixTest2[0][0]; - output[2] = (uint)matrixTest2[0][1]; - output[3] = (uint)matrixTest2[0][2]; //BUF: 1 } diff --git a/tests/language-feature/types/matrix-layout-uniform.slang b/tests/language-feature/types/matrix-layout-uniform.slang new file mode 100644 index 000000000..ed72ccdca --- /dev/null +++ b/tests/language-feature/types/matrix-layout-uniform.slang @@ -0,0 +1,19 @@ +//TEST:SIMPLE(filecheck=HLSL): -target hlsl -profile vs_6_0 -entry Main + +// HLSL: #pragma pack_matrix(column_major) +// HLSL-NOT: row_major float4x4 worldToClip +// HLSL: row_major float4x4 instanceMeshToWorld + +[shader("vertex")] +void Main( + uniform column_major float4x4 worldToClip, + out float4 SV_Position : SV_Position, + in row_major float4x4 instanceMeshToWorld : instanceMeshToWorld, + in float3 vertexPositionInMesh : vertexPositionInMesh +) +{ + SV_Position = mul( + mul(worldToClip, instanceMeshToWorld), + float4(vertexPositionInMesh, 1.f), + ); +} \ No newline at end of file -- cgit v1.2.3