Atomic ops on images with Unknown type will cause SPIR-V validation failure, and assertion failure in Mesa debug builds. D3D12 allows atomics on typed buffers, and this requires a distinction to be made between UAV reads and atomic ops.
-- v2: vkd3d-shader: Introduce DESCRIPTOR_INFO_FLAG_UAV_ATOMICS and always declare UAV images with known type for atomic ops. tests: Test a typed UAV buffer in test_atomic_instructions().
From: Conor McCarthy cmccarthy@codeweavers.com
Until vkd3d-shader is patched, an atomic op on a typed buffer where StorageImageReadWithoutFormat is available will cause SPIR-V validation failure, and assertion in Mesa debug builds, because the image will be declared with Unknown format. --- tests/d3d12.c | 129 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 85 insertions(+), 44 deletions(-)
diff --git a/tests/d3d12.c b/tests/d3d12.c index 483c7888..1e995aa8 100644 --- a/tests/d3d12.c +++ b/tests/d3d12.c @@ -23107,6 +23107,7 @@ static void test_atomic_instructions(void) { #if 0 RWByteAddressBuffer u; + RWBuffer<uint> u1;
uint4 v; int4 i; @@ -23122,29 +23123,32 @@ static void test_atomic_instructions(void) u.InterlockedMax(6 * 4, v.x); u.InterlockedMin(7 * 4, v.x); u.InterlockedXor(8 * 4, v.x); + /* Test a typed buffer to ensure it's declared typed. */ + InterlockedAnd(u1[9], v.x); } #endif - 0x43425844, 0x24c6a30c, 0x2ce4437d, 0xdee8a0df, 0xd18cb4bc, 0x00000001, 0x000001ac, 0x00000003, + 0x43425844, 0x56fe03ba, 0x9eb179fd, 0x02af0886, 0xd4983e25, 0x00000001, 0x000001dc, 0x00000003, 0x0000002c, 0x0000003c, 0x0000004c, 0x4e475349, 0x00000008, 0x00000000, 0x00000008, 0x4e47534f, - 0x00000008, 0x00000000, 0x00000008, 0x58454853, 0x00000158, 0x00000050, 0x00000056, 0x0100086a, - 0x04000059, 0x00208e46, 0x00000000, 0x00000002, 0x0300009d, 0x0011e000, 0x00000000, 0x080000a9, - 0x0011e000, 0x00000000, 0x00004001, 0x00000000, 0x0020800a, 0x00000000, 0x00000000, 0x0b0000ac, - 0x0011e000, 0x00000000, 0x00004001, 0x00000004, 0x0020801a, 0x00000000, 0x00000000, 0x0020800a, - 0x00000000, 0x00000000, 0x080000ad, 0x0011e000, 0x00000000, 0x00004001, 0x00000008, 0x0020800a, - 0x00000000, 0x00000000, 0x080000aa, 0x0011e000, 0x00000000, 0x00004001, 0x0000000c, 0x0020800a, - 0x00000000, 0x00000000, 0x080000ae, 0x0011e000, 0x00000000, 0x00004001, 0x00000010, 0x0020800a, - 0x00000000, 0x00000001, 0x080000af, 0x0011e000, 0x00000000, 0x00004001, 0x00000014, 0x0020800a, - 0x00000000, 0x00000001, 0x080000b0, 0x0011e000, 0x00000000, 0x00004001, 0x00000018, 0x0020800a, - 0x00000000, 0x00000000, 0x080000b1, 0x0011e000, 0x00000000, 0x00004001, 0x0000001c, 0x0020800a, - 0x00000000, 0x00000000, 0x080000ab, 0x0011e000, 0x00000000, 0x00004001, 0x00000020, 0x0020800a, - 0x00000000, 0x00000000, 0x0100003e, + 0x00000008, 0x00000000, 0x00000008, 0x58454853, 0x00000188, 0x00000050, 0x00000062, 0x0100086a, + 0x04000059, 0x00208e46, 0x00000000, 0x00000002, 0x0300009d, 0x0011e000, 0x00000000, 0x0400089c, + 0x0011e000, 0x00000001, 0x00004444, 0x080000a9, 0x0011e000, 0x00000000, 0x00004001, 0x00000000, + 0x0020800a, 0x00000000, 0x00000000, 0x0b0000ac, 0x0011e000, 0x00000000, 0x00004001, 0x00000004, + 0x0020801a, 0x00000000, 0x00000000, 0x0020800a, 0x00000000, 0x00000000, 0x080000ad, 0x0011e000, + 0x00000000, 0x00004001, 0x00000008, 0x0020800a, 0x00000000, 0x00000000, 0x080000aa, 0x0011e000, + 0x00000000, 0x00004001, 0x0000000c, 0x0020800a, 0x00000000, 0x00000000, 0x080000ae, 0x0011e000, + 0x00000000, 0x00004001, 0x00000010, 0x0020800a, 0x00000000, 0x00000001, 0x080000af, 0x0011e000, + 0x00000000, 0x00004001, 0x00000014, 0x0020800a, 0x00000000, 0x00000001, 0x080000b0, 0x0011e000, + 0x00000000, 0x00004001, 0x00000018, 0x0020800a, 0x00000000, 0x00000000, 0x080000b1, 0x0011e000, + 0x00000000, 0x00004001, 0x0000001c, 0x0020800a, 0x00000000, 0x00000000, 0x080000ab, 0x0011e000, + 0x00000000, 0x00004001, 0x00000020, 0x0020800a, 0x00000000, 0x00000000, 0x080000a9, 0x0011e000, + 0x00000001, 0x00004001, 0x00000009, 0x0020800a, 0x00000000, 0x00000000, 0x0100003e, }; static const D3D12_SHADER_BYTECODE ps_atomics = {ps_atomics_code, sizeof(ps_atomics_code)}; static const DWORD cs_atomics_code[] = { #if 0 RWByteAddressBuffer u; - RWByteAddressBuffer u2; + RWBuffer<uint> u2;
uint4 v; int4 i; @@ -23154,45 +23158,59 @@ static void test_atomic_instructions(void) { uint r; u.InterlockedAnd(0 * 4, v.x, r); - u2.Store(0 * 4, r); + u2[0] = r; u.InterlockedCompareExchange(1 * 4, v.y, v.x, r); - u2.Store(1 * 4, r); + u2[1] = r; u.InterlockedAdd(2 * 4, v.x, r); - u2.Store(2 * 4, r); + u2[2] = r; u.InterlockedOr(3 * 4, v.x, r); - u2.Store(3 * 4, r); + u2[3] = r; u.InterlockedMax(4 * 4, i.x, r); - u2.Store(4 * 4, r); + u2[4] = r; u.InterlockedMin(5 * 4, i.x, r); - u2.Store(5 * 4, r); + u2[5] = r; u.InterlockedMax(6 * 4, v.x, r); - u2.Store(6 * 4, r); + u2[6] = r; u.InterlockedMin(7 * 4, v.x, r); - u2.Store(7 * 4, r); + u2[7] = r; u.InterlockedXor(8 * 4, v.x, r); - u2.Store(8 * 4, r); + u2[8] = r; + /* Test a typed buffer to ensure it's declared typed. */ + InterlockedXor(u2[10], v.x, r); + u2[11] = r; } #endif - 0x43425844, 0x859a96e3, 0x1a35e463, 0x1e89ce58, 0x5cfe430a, 0x00000001, 0x0000026c, 0x00000003, + 0x43425844, 0x67bb8e2f, 0x78243d95, 0x7e90faa7, 0xaa9cc6e5, 0x00000001, 0x000003d4, 0x00000003, 0x0000002c, 0x0000003c, 0x0000004c, 0x4e475349, 0x00000008, 0x00000000, 0x00000008, 0x4e47534f, - 0x00000008, 0x00000000, 0x00000008, 0x58454853, 0x00000218, 0x00050050, 0x00000086, 0x0100086a, - 0x04000059, 0x00208e46, 0x00000000, 0x00000002, 0x0300009d, 0x0011e000, 0x00000000, 0x0300009d, - 0x0011e000, 0x00000001, 0x02000068, 0x00000001, 0x0400009b, 0x00000001, 0x00000001, 0x00000001, - 0x0a0000b5, 0x00100012, 0x00000000, 0x0011e000, 0x00000000, 0x00004001, 0x00000000, 0x0020800a, - 0x00000000, 0x00000000, 0x0d0000b9, 0x00100022, 0x00000000, 0x0011e000, 0x00000000, 0x00004001, - 0x00000004, 0x0020801a, 0x00000000, 0x00000000, 0x0020800a, 0x00000000, 0x00000000, 0x0a0000b4, - 0x00100042, 0x00000000, 0x0011e000, 0x00000000, 0x00004001, 0x00000008, 0x0020800a, 0x00000000, - 0x00000000, 0x0a0000b6, 0x00100082, 0x00000000, 0x0011e000, 0x00000000, 0x00004001, 0x0000000c, - 0x0020800a, 0x00000000, 0x00000000, 0x070000a6, 0x0011e0f2, 0x00000001, 0x00004001, 0x00000000, - 0x00100e46, 0x00000000, 0x0a0000ba, 0x00100012, 0x00000000, 0x0011e000, 0x00000000, 0x00004001, - 0x00000010, 0x0020800a, 0x00000000, 0x00000001, 0x0a0000bb, 0x00100022, 0x00000000, 0x0011e000, - 0x00000000, 0x00004001, 0x00000014, 0x0020800a, 0x00000000, 0x00000001, 0x0a0000bc, 0x00100042, - 0x00000000, 0x0011e000, 0x00000000, 0x00004001, 0x00000018, 0x0020800a, 0x00000000, 0x00000000, - 0x0a0000bd, 0x00100082, 0x00000000, 0x0011e000, 0x00000000, 0x00004001, 0x0000001c, 0x0020800a, - 0x00000000, 0x00000000, 0x070000a6, 0x0011e0f2, 0x00000001, 0x00004001, 0x00000010, 0x00100e46, - 0x00000000, 0x0a0000b7, 0x00100012, 0x00000000, 0x0011e000, 0x00000000, 0x00004001, 0x00000020, - 0x0020800a, 0x00000000, 0x00000000, 0x070000a6, 0x0011e012, 0x00000001, 0x00004001, 0x00000020, - 0x0010000a, 0x00000000, 0x0100003e, + 0x00000008, 0x00000000, 0x00000008, 0x58454853, 0x00000380, 0x00050050, 0x000000e0, 0x0100086a, + 0x04000059, 0x00208e46, 0x00000000, 0x00000002, 0x0300009d, 0x0011e000, 0x00000000, 0x0400089c, + 0x0011e000, 0x00000001, 0x00004444, 0x02000068, 0x00000001, 0x0400009b, 0x00000001, 0x00000001, + 0x00000001, 0x0a0000b5, 0x00100012, 0x00000000, 0x0011e000, 0x00000000, 0x00004001, 0x00000000, + 0x0020800a, 0x00000000, 0x00000000, 0x0a0000a4, 0x0011e0f2, 0x00000001, 0x00004002, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00100006, 0x00000000, 0x0d0000b9, 0x00100012, 0x00000000, + 0x0011e000, 0x00000000, 0x00004001, 0x00000004, 0x0020801a, 0x00000000, 0x00000000, 0x0020800a, + 0x00000000, 0x00000000, 0x0a0000a4, 0x0011e0f2, 0x00000001, 0x00004002, 0x00000001, 0x00000001, + 0x00000001, 0x00000001, 0x00100006, 0x00000000, 0x0a0000b4, 0x00100012, 0x00000000, 0x0011e000, + 0x00000000, 0x00004001, 0x00000008, 0x0020800a, 0x00000000, 0x00000000, 0x0a0000a4, 0x0011e0f2, + 0x00000001, 0x00004002, 0x00000002, 0x00000002, 0x00000002, 0x00000002, 0x00100006, 0x00000000, + 0x0a0000b6, 0x00100012, 0x00000000, 0x0011e000, 0x00000000, 0x00004001, 0x0000000c, 0x0020800a, + 0x00000000, 0x00000000, 0x0a0000a4, 0x0011e0f2, 0x00000001, 0x00004002, 0x00000003, 0x00000003, + 0x00000003, 0x00000003, 0x00100006, 0x00000000, 0x0a0000ba, 0x00100012, 0x00000000, 0x0011e000, + 0x00000000, 0x00004001, 0x00000010, 0x0020800a, 0x00000000, 0x00000001, 0x0a0000a4, 0x0011e0f2, + 0x00000001, 0x00004002, 0x00000004, 0x00000004, 0x00000004, 0x00000004, 0x00100006, 0x00000000, + 0x0a0000bb, 0x00100012, 0x00000000, 0x0011e000, 0x00000000, 0x00004001, 0x00000014, 0x0020800a, + 0x00000000, 0x00000001, 0x0a0000a4, 0x0011e0f2, 0x00000001, 0x00004002, 0x00000005, 0x00000005, + 0x00000005, 0x00000005, 0x00100006, 0x00000000, 0x0a0000bc, 0x00100012, 0x00000000, 0x0011e000, + 0x00000000, 0x00004001, 0x00000018, 0x0020800a, 0x00000000, 0x00000000, 0x0a0000a4, 0x0011e0f2, + 0x00000001, 0x00004002, 0x00000006, 0x00000006, 0x00000006, 0x00000006, 0x00100006, 0x00000000, + 0x0a0000bd, 0x00100012, 0x00000000, 0x0011e000, 0x00000000, 0x00004001, 0x0000001c, 0x0020800a, + 0x00000000, 0x00000000, 0x0a0000a4, 0x0011e0f2, 0x00000001, 0x00004002, 0x00000007, 0x00000007, + 0x00000007, 0x00000007, 0x00100006, 0x00000000, 0x0a0000b7, 0x00100012, 0x00000000, 0x0011e000, + 0x00000000, 0x00004001, 0x00000020, 0x0020800a, 0x00000000, 0x00000000, 0x0a0000a4, 0x0011e0f2, + 0x00000001, 0x00004002, 0x00000008, 0x00000008, 0x00000008, 0x00000008, 0x00100006, 0x00000000, + 0x0a0000b7, 0x00100012, 0x00000000, 0x0011e000, 0x00000001, 0x00004001, 0x0000000a, 0x0020800a, + 0x00000000, 0x00000000, 0x0a0000a4, 0x0011e0f2, 0x00000001, 0x00004002, 0x0000000b, 0x0000000b, + 0x0000000b, 0x0000000b, 0x00100006, 0x00000000, 0x0100003e, }; static D3D12_SHADER_BYTECODE cs_atomics = {cs_atomics_code, sizeof(cs_atomics_code)}; static const char * const instructions[] = @@ -23217,6 +23235,18 @@ static void test_atomic_instructions(void) {{ 1, 0 }, {-1}, {0xffff, 0, 1, 0, 0, 0, 0, 0, 0xff }, { 1, 1, 2, 1, 0, ~0u, 1, 0, 0xfe}}, {{~0u, ~0u}, { 0}, {0xffff, 0xf, 1, 0, 0, 0, 0, 9, ~0u}, {0xffff, 0xf, 0, ~0u, 0, 0, ~0u, 9, 0}}, }; + static const struct typed_test + { + struct uvec4 v; + struct ivec4 i; + unsigned int input[3]; + unsigned int expected_result[3]; + } + typed_tests[] = + { + {{ 1, 0 }, {-1}, {0xffff, 0xff, 0}, { 1, 0xfe, 0xff}}, + {{~0u, ~0u}, { 0}, {0xffff, ~0u, 0}, {0xffff, 0, ~0u}}, + };
memset(&desc, 0, sizeof(desc)); desc.rt_width = 1; @@ -23254,7 +23284,7 @@ static void test_atomic_instructions(void) D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_DEST); cs_buffer = create_default_buffer(device, sizeof(tests->input), D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_DEST); - cs_buffer2 = create_default_buffer(device, sizeof(tests->input), + cs_buffer2 = create_default_buffer(device, sizeof(tests->input) + sizeof(typed_tests->input), D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_DEST);
context.pipeline_state = create_pipeline_state(context.device, context.root_signature, 0, NULL, &ps_atomics, NULL); @@ -23263,6 +23293,7 @@ static void test_atomic_instructions(void)
for (i = 0; i < ARRAY_SIZE(tests); ++i) { + const struct typed_test *typed_test = &typed_tests[i]; const struct test *test = &tests[i];
upload_buffer_data(ps_buffer, 0, sizeof(test->input), test->input, queue, command_list); @@ -23270,6 +23301,9 @@ static void test_atomic_instructions(void)
upload_buffer_data(cs_buffer, 0, sizeof(test->input), test->input, queue, command_list); reset_command_list(command_list, context.allocator); + upload_buffer_data(cs_buffer2, sizeof(test->input), sizeof(typed_test->input), typed_test->input, + queue, command_list); + reset_command_list(command_list, context.allocator);
transition_sub_resource_state(command_list, ps_buffer, 0, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_UNORDERED_ACCESS); @@ -23285,7 +23319,7 @@ static void test_atomic_instructions(void) ID3D12GraphicsCommandList_SetGraphicsRootUnorderedAccessView(command_list, 0, ID3D12Resource_GetGPUVirtualAddress(ps_buffer)); ID3D12GraphicsCommandList_SetGraphicsRootUnorderedAccessView(command_list, - 1, ID3D12Resource_GetGPUVirtualAddress(cs_buffer)); + 1, ID3D12Resource_GetGPUVirtualAddress(cs_buffer2)); ID3D12GraphicsCommandList_SetGraphicsRoot32BitConstants(command_list, 2, 4, &test->v, 0); ID3D12GraphicsCommandList_SetGraphicsRoot32BitConstants(command_list, 2, 4, &test->i, 4);
@@ -23354,6 +23388,13 @@ static void test_atomic_instructions(void) ok(out_value == test->input[j], "Got original value %u, expected %u for '%s'.\n", out_value, test->input[j], imm_instructions[j]); } + for (j = 0; j < 2; ++j) + { + unsigned int value = get_readback_uint(&rb.rb, ARRAY_SIZE(instructions) + j, 0, 0); + unsigned int expected = typed_test->expected_result[j]; + + ok(value == expected, "Test %u,%u: Got %#x, expected %#x.\n", i, j, value, expected); + } release_resource_readback(&rb); reset_command_list(command_list, context.allocator);
From: Conor McCarthy cmccarthy@codeweavers.com
Atomic ops on images with Unknown type will cause SPIR-V validation failure, and assertion failure in Mesa debug builds. D3D12 allows atomics on typed buffers, and this requires a distinction to be made between UAV reads and atomic ops.
Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=53874 --- include/vkd3d_shader.h | 3 +++ libs/vkd3d-shader/spirv.c | 5 +++-- libs/vkd3d-shader/vkd3d_shader_main.c | 28 +++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-)
diff --git a/include/vkd3d_shader.h b/include/vkd3d_shader.h index 7178ac5f..939ab42d 100644 --- a/include/vkd3d_shader.h +++ b/include/vkd3d_shader.h @@ -1263,6 +1263,9 @@ enum vkd3d_shader_descriptor_info_flag VKD3D_SHADER_DESCRIPTOR_INFO_FLAG_UAV_READ = 0x00000002, /** The descriptor is a comparison sampler. */ VKD3D_SHADER_DESCRIPTOR_INFO_FLAG_SAMPLER_COMPARISON_MODE = 0x00000004, + /** The descriptor is a UAV resource, on which the shader performs + * atomic ops. \since 1.6 */ + VKD3D_SHADER_DESCRIPTOR_INFO_FLAG_UAV_ATOMICS = 0x00000008,
VKD3D_FORCE_32_BIT_ENUM(VKD3D_SHADER_DESCRIPTOR_INFO_FLAG), }; diff --git a/libs/vkd3d-shader/spirv.c b/libs/vkd3d-shader/spirv.c index 734ba315..2d1691c6 100644 --- a/libs/vkd3d-shader/spirv.c +++ b/libs/vkd3d-shader/spirv.c @@ -5864,9 +5864,9 @@ static uint32_t spirv_compiler_get_image_type_id(struct spirv_compiler *compiler { struct vkd3d_spirv_builder *builder = &compiler->spirv_builder; const struct vkd3d_shader_descriptor_info *d; + bool uav_read, uav_atomics; uint32_t sampled_type_id; SpvImageFormat format; - bool uav_read;
format = SpvImageFormatUnknown; if (reg->type == VKD3DSPR_UAV) @@ -5874,7 +5874,8 @@ static uint32_t spirv_compiler_get_image_type_id(struct spirv_compiler *compiler d = spirv_compiler_get_descriptor_info(compiler, VKD3D_SHADER_DESCRIPTOR_TYPE_UAV, range); uav_read = !!(d->flags & VKD3D_SHADER_DESCRIPTOR_INFO_FLAG_UAV_READ); - if (raw_structured || (uav_read && !compiler->uav_read_without_format)) + uav_atomics = !!(d->flags & VKD3D_SHADER_DESCRIPTOR_INFO_FLAG_UAV_ATOMICS); + if (raw_structured || uav_atomics || (uav_read && !compiler->uav_read_without_format)) format = image_format_for_image_read(data_type); else if (uav_read) vkd3d_spirv_enable_capability(builder, SpvCapabilityStorageImageReadWithoutFormat); diff --git a/libs/vkd3d-shader/vkd3d_shader_main.c b/libs/vkd3d-shader/vkd3d_shader_main.c index 5e25dde8..72a6d2d8 100644 --- a/libs/vkd3d-shader/vkd3d_shader_main.c +++ b/libs/vkd3d-shader/vkd3d_shader_main.c @@ -692,6 +692,25 @@ static void vkd3d_shader_scan_record_uav_counter(struct vkd3d_shader_scan_contex d->flags |= VKD3D_SHADER_DESCRIPTOR_INFO_FLAG_UAV_COUNTER; }
+static bool vkd3d_shader_instruction_is_uav_atomic_op(const struct vkd3d_shader_instruction *instruction) +{ + enum vkd3d_shader_opcode handler_idx = instruction->handler_idx; + return (VKD3DSIH_ATOMIC_AND <= handler_idx && handler_idx <= VKD3DSIH_ATOMIC_XOR) + || (VKD3DSIH_IMM_ATOMIC_ALLOC <= handler_idx && handler_idx <= VKD3DSIH_IMM_ATOMIC_XOR); +} + +static void vkd3d_shader_scan_record_uav_atomic_op(struct vkd3d_shader_scan_context *context, + const struct vkd3d_shader_register *reg) +{ + struct vkd3d_shader_descriptor_info *d; + + if (!context->scan_descriptor_info) + return; + + d = vkd3d_shader_scan_get_uav_descriptor_info(context, reg->idx[0].offset); + d->flags |= VKD3D_SHADER_DESCRIPTOR_INFO_FLAG_UAV_ATOMICS; +} + static bool vkd3d_shader_scan_add_descriptor(struct vkd3d_shader_scan_context *context, enum vkd3d_shader_descriptor_type type, const struct vkd3d_shader_register_range *range, enum vkd3d_shader_resource_type resource_type, enum vkd3d_shader_resource_data_type resource_data_type, @@ -1019,6 +1038,15 @@ static int vkd3d_shader_scan_instruction(struct vkd3d_shader_scan_context *conte if (vkd3d_shader_instruction_is_uav_counter(instruction)) vkd3d_shader_scan_record_uav_counter(context, &instruction->src[0].reg);
+ if (vkd3d_shader_instruction_is_uav_atomic_op(instruction)) + { + for (i = 0; i < instruction->dst_count; ++i) + { + if (instruction->dst[i].reg.type == VKD3DSPR_UAV) + vkd3d_shader_scan_record_uav_atomic_op(context, &instruction->dst[i].reg); + } + } + ++context->location.line; return VKD3D_OK; }
Tests now cover both test shaders. IMM_ATOMIC_* operations are handled.
This merge request was approved by Henri Verbeet.