From fdf061e278720ec066a1fac8f1f35a22e817bf2d Mon Sep 17 00:00:00 2001 From: Yong He Date: Thu, 21 Nov 2024 14:07:23 -0800 Subject: Add datalayout for constant buffers. (#5608) * Add datalayout for constant buffers. * Fixes. * Fix test. * Fix glsl codegen. * Update spirv-specific doc. * Fix test. * Fix binding in the presense of specialization constants. * address comments. * Add a test for constant buffer layout. --- tests/bugs/vk-shift-uniform-issue.slang | 2 +- .../binding-push-constant-gl.hlsl.expected | 2 +- tests/spirv/constant-buffer-layout.slang | 45 ++++++++++++++++++++++ tests/spirv/push-constant-layout.slang | 26 +++++++++++++ tests/spirv/spec-constant-space.slang | 17 ++++++++ 5 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 tests/spirv/constant-buffer-layout.slang create mode 100644 tests/spirv/push-constant-layout.slang create mode 100644 tests/spirv/spec-constant-space.slang (limited to 'tests') diff --git a/tests/bugs/vk-shift-uniform-issue.slang b/tests/bugs/vk-shift-uniform-issue.slang index bc5963fe0..7f27d2c56 100644 --- a/tests/bugs/vk-shift-uniform-issue.slang +++ b/tests/bugs/vk-shift-uniform-issue.slang @@ -13,7 +13,7 @@ // CHECK-NEXT:uniform sampler sampler1_0; // CHECK: layout(push_constant) -// CHECK-NEXT: layout(std140) uniform +// CHECK-NEXT: layout(std430) uniform // CHECK:layout(binding = 1004) // CHECK-NEXT:layout(std140) uniform diff --git a/tests/reflection/binding-push-constant-gl.hlsl.expected b/tests/reflection/binding-push-constant-gl.hlsl.expected index 13dfe2fcd..0b906d461 100644 --- a/tests/reflection/binding-push-constant-gl.hlsl.expected +++ b/tests/reflection/binding-push-constant-gl.hlsl.expected @@ -138,7 +138,7 @@ standard output = { } ] }, - "binding": {"kind": "uniform", "offset": 0, "size": 16} + "binding": {"kind": "uniform", "offset": 0, "size": 8} } } }, diff --git a/tests/spirv/constant-buffer-layout.slang b/tests/spirv/constant-buffer-layout.slang new file mode 100644 index 000000000..90aa1eef8 --- /dev/null +++ b/tests/spirv/constant-buffer-layout.slang @@ -0,0 +1,45 @@ +//TEST:SIMPLE(filecheck=SPIRV): -target spirv -emit-spirv-directly + +//SPIRV: ArrayStride 12 + +struct Test +{ +//SPIRV: Offset 0 + uint v0; + +//SPIRV: Offset 4 +// matrix always start on a new register + float3x3 v1; +//SPIRV: Offset 40 +// Non-matrix can pack with a partially filled register + uint v2; +}; + +ConstantBuffer buffer; + +//TEST_INPUT:ubuffer(data=[0], stride=4):out,name outputBuffer +RWStructuredBuffer outputBuffer; + +__generic +bool comp(vector v1, vector v2) +{ + for (uint i = 0; i < N; i++) + if (v1[i] != v2[i]) + return false; + + return true; +} + +[shader("compute")] +[numthreads(2, 2, 1)] +void computeMain() +{ + // CHECK: 64 + outputBuffer[0] = (true + && buffer.v0 == 1 + && comp(buffer.v1[0], float3(2, 3, 4)) + && comp(buffer.v1[1], float3(5, 6, 7)) + && comp(buffer.v1[2], float3(8, 9, 10)) + && buffer.v2 == 11 + ) ? 100 : 0; +} \ No newline at end of file diff --git a/tests/spirv/push-constant-layout.slang b/tests/spirv/push-constant-layout.slang new file mode 100644 index 000000000..eb7d80f75 --- /dev/null +++ b/tests/spirv/push-constant-layout.slang @@ -0,0 +1,26 @@ +//TEST:SIMPLE(filecheck=CHECK): -target spirv -fvk-use-entrypoint-name +// CHECK-NOT: std140 +struct Transform +{ + float4 Tint; + float2x2 ScaleRot; + float2 Translation; +}; + +[[vk::push_constant]] +ConstantBuffer transform1; + +RWStructuredBuffer outputBuffer; + +[numthreads(1,1,1)] +void computeMain1() +{ + outputBuffer[0] = transform1.Translation.x; +} + +[numthreads(1,1,1)] +void computeMain2( + [vk::push_constant] ConstantBuffer transform2) +{ + outputBuffer[0] = transform2.Translation.x; +} \ No newline at end of file diff --git a/tests/spirv/spec-constant-space.slang b/tests/spirv/spec-constant-space.slang new file mode 100644 index 000000000..b4dd865c7 --- /dev/null +++ b/tests/spirv/spec-constant-space.slang @@ -0,0 +1,17 @@ +// Test that use of specialization constants does not cause space 0 to be reserved. + +//TEST:SIMPLE(filecheck=CHECK): -target spirv + +// CHECK-NOT: DescriptorSet 1 + +struct MyData { float4 val; RWStructuredBuffer outputBuffer; } + + +[vk::specialization_constant] +const int kSpecializationConstant = 0; + +[NumThreads(1,1,1)] +void main(ParameterBlock g_data) +{ + g_data.outputBuffer[0] = g_data.val.x; +} \ No newline at end of file -- cgit v1.2.3