diff options
| author | Almogaver295 <133548141+guimsolutions@users.noreply.github.com> | 2026-11-23 09:31:12 +0100 |
|---|---|---|
| committer | Almogaver295 <133548141+guimsolutions@users.noreply.github.com> | 2026-11-23 09:45:07 +0100 |
| commit | 57cb669f52ba6ca4bb58bf2e09492ffbe2b8ee60 (patch) | |
| tree | 2c186fc0ca9722c7bd5a87c3c5edc03eacca4dbc /runtime | |
| parent | 1e63e096805a779a61f31715da5498d9864da116 (diff) | |
fix(vulkan): require formatless reads for all D3D-78 UAVs
Coherent 1D and 3D loads and all 1D-array loads can issue OpImageRead on Unknown-format storage views. Require both per-format access bits before registering the view or writing its descriptor, while preserving the formatted RGB9E5 exception.
AI-assisted. Verified the synthetic UAV readback and format refusals with Vulkan validation on RTX 4090 (621.01) and UHD 770 (101.7088).
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/shims/d3d12_vulkan_device.cpp | 12 | ||||
| -rw-r--r-- | runtime/test/d3d12_texture_uav_test.cpp | 89 | ||||
| -rw-r--r-- | runtime/test/shaders/cs_texture_uav.asm | 14 |
3 files changed, 106 insertions, 9 deletions
diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 1af93b406..f8ba8d1ad 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp | |||
| @@ -7319,17 +7319,17 @@ private: | |||
| 7319 | return; | 7319 | return; |
| 7320 | } | 7320 | } |
| 7321 | if (new_uav && state_.info.robust_image_access) { | 7321 | if (new_uav && state_.info.robust_image_access) { |
| 7322 | const bool storage_read = view.type == g::ImageType::image_1d_array; | ||
| 7323 | if (sampled && !state_.sampled_usable(view_format)) { | 7322 | if (sampled && !state_.sampled_usable(view_format)) { |
| 7324 | log_once("texture view format does not support sampled access:", view_format); | 7323 | log_once("texture view format does not support sampled access:", view_format); |
| 7325 | return; | 7324 | return; |
| 7326 | } | 7325 | } |
| 7327 | // Vulkan 1.3 allows formatless access based on the actual view's features, even on caller-owned | 7326 | // All three dimensions use Unknown-format storage writes and can use Unknown-format reads |
| 7328 | // devices. Packed RGB9E5 only exposes formatted raw R32_UINT storage, so it does not need these. | 7327 | // (image_load glc, or any image_load on a 1D array). Check the actual storage view's format. |
| 7328 | // Packed RGB9E5 only exposes formatted raw R32_UINT storage, so it does not need these bits. | ||
| 7329 | if (!packed_storage) { | 7329 | if (!packed_storage) { |
| 7330 | const auto features = state_.optimal_features(storage_format); | 7330 | constexpr auto required = VK_FORMAT_FEATURE_2_STORAGE_READ_WITHOUT_FORMAT_BIT | |
| 7331 | if (!(features & VK_FORMAT_FEATURE_2_STORAGE_WRITE_WITHOUT_FORMAT_BIT) || | 7331 | VK_FORMAT_FEATURE_2_STORAGE_WRITE_WITHOUT_FORMAT_BIT; |
| 7332 | (storage_read && !(features & VK_FORMAT_FEATURE_2_STORAGE_READ_WITHOUT_FORMAT_BIT))) { | 7332 | if ((state_.optimal_features(storage_format) & required) != required) { |
| 7333 | log_once("texture view format lacks the required formatless storage image access:", storage_format); | 7333 | log_once("texture view format lacks the required formatless storage image access:", storage_format); |
| 7334 | return; | 7334 | return; |
| 7335 | } | 7335 | } |
diff --git a/runtime/test/d3d12_texture_uav_test.cpp b/runtime/test/d3d12_texture_uav_test.cpp index 1b096b399..08092f569 100644 --- a/runtime/test/d3d12_texture_uav_test.cpp +++ b/runtime/test/d3d12_texture_uav_test.cpp | |||
| @@ -25,6 +25,28 @@ namespace { | |||
| 25 | using Microsoft::WRL::ComPtr; | 25 | using Microsoft::WRL::ComPtr; |
| 26 | namespace g = leonida::gfx10; | 26 | namespace g = leonida::gfx10; |
| 27 | constexpr auto uav_heap = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; | 27 | constexpr auto uav_heap = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; |
| 28 | constexpr auto formatless_read = VK_FORMAT_FEATURE_2_STORAGE_READ_WITHOUT_FORMAT_BIT; | ||
| 29 | constexpr auto formatless_write = VK_FORMAT_FEATURE_2_STORAGE_WRITE_WITHOUT_FORMAT_BIT; | ||
| 30 | |||
| 31 | // Only the layer sees the masked capabilities. The real device and validation layer see the unmodified driver. | ||
| 32 | PFN_vkGetInstanceProcAddr real_instance_proc = nullptr; | ||
| 33 | PFN_vkGetPhysicalDeviceFormatProperties2 real_format_properties = nullptr; | ||
| 34 | VkFormatFeatureFlags2 missing_format_features = 0; | ||
| 35 | |||
| 36 | VKAPI_ATTR void VKAPI_CALL masked_format_properties(VkPhysicalDevice physical, VkFormat format, | ||
| 37 | VkFormatProperties2* properties) { | ||
| 38 | real_format_properties(physical, format, properties); | ||
| 39 | if (format != VK_FORMAT_R32_UINT) return; | ||
| 40 | for (auto* next = static_cast<VkBaseOutStructure*>(properties->pNext); next; next = next->pNext) | ||
| 41 | if (next->sType == VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_3) | ||
| 42 | reinterpret_cast<VkFormatProperties3*>(next)->optimalTilingFeatures &= ~missing_format_features; | ||
| 43 | } | ||
| 44 | |||
| 45 | VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL masked_instance_proc(VkInstance instance, const char* name) { | ||
| 46 | if (std::strcmp(name, "vkGetPhysicalDeviceFormatProperties2") == 0) | ||
| 47 | return reinterpret_cast<PFN_vkVoidFunction>(masked_format_properties); | ||
| 48 | return real_instance_proc(instance, name); | ||
| 49 | } | ||
| 28 | 50 | ||
| 29 | // Numeric values are also the branches selected by cs_texture_uav.asm. | 51 | // Numeric values are also the branches selected by cs_texture_uav.asm. |
| 30 | enum class Kind : UINT { one_d, array, volume, two_d, float_volume, float_array, atomic_volume }; | 52 | enum class Kind : UINT { one_d, array, volume, two_d, float_volume, float_array, atomic_volume }; |
| @@ -359,6 +381,65 @@ bool check_refusals(ID3D12Device* device, D3D12_CPU_DESCRIPTOR_HANDLE slot) { | |||
| 359 | return true; | 381 | return true; |
| 360 | } | 382 | } |
| 361 | 383 | ||
| 384 | bool check_formatless_features(ID3D12Device* device, D3D12_CPU_DESCRIPTOR_HANDLE slot, bool sliced) { | ||
| 385 | using enum Kind; | ||
| 386 | const auto check = [&](Kind kind, const D3D12_RESOURCE_DESC& shape, UINT first, UINT count) { | ||
| 387 | ComPtr<ID3D12Resource> resource; | ||
| 388 | EXPECT(create_texture(device, shape, &resource)); | ||
| 389 | const auto view = uav(kind, 1, first, count); | ||
| 390 | device->CreateUnorderedAccessView(resource.Get(), nullptr, &view, slot); | ||
| 391 | const auto valid = read_slot(slot); | ||
| 392 | g::Image decoded{}; | ||
| 393 | EXPECT(g::decode(valid, decoded) && decoded.first_mip == 1 && decoded.mip_count == 1); | ||
| 394 | for (const auto missing : {formatless_read, formatless_write, formatless_read | formatless_write}) { | ||
| 395 | std::memcpy(reinterpret_cast<void*>(slot.ptr), valid.data(), sizeof(valid)); | ||
| 396 | missing_format_features = missing; | ||
| 397 | device->CreateUnorderedAccessView(resource.Get(), nullptr, &view, slot); | ||
| 398 | missing_format_features = 0; | ||
| 399 | if (read_slot(slot) != g::ImageWords{}) { | ||
| 400 | std::fprintf(stderr, "kind %u first slice %u: accepted mip 1 UAV without format features %#llx\n", | ||
| 401 | UINT(kind), first, static_cast<unsigned long long>(missing)); | ||
| 402 | return false; | ||
| 403 | } | ||
| 404 | } | ||
| 405 | return true; | ||
| 406 | }; | ||
| 407 | EXPECT(check(one_d, texture(one_d, 1), 0, 1)); | ||
| 408 | EXPECT(check(array, texture(array, 4), 1, 2)); | ||
| 409 | // The mask applies to the R32_UINT view, not its typeless resource's backing format. | ||
| 410 | const auto volume_shape = texture(volume, 8, DXGI_FORMAT_R32_TYPELESS); | ||
| 411 | EXPECT(check(volume, volume_shape, 0, ~0u)); | ||
| 412 | if (sliced) EXPECT(check(volume, volume_shape, 1, 2)); | ||
| 413 | return true; | ||
| 414 | } | ||
| 415 | |||
| 416 | bool check_packed_formatless_exemption(ID3D12Device* device, vktest::Device& vk, | ||
| 417 | D3D12_CPU_DESCRIPTOR_HANDLE slot, bool sliced) { | ||
| 418 | VkFormatProperties features{}; | ||
| 419 | vk.instance_fn<PFN_vkGetPhysicalDeviceFormatProperties>("vkGetPhysicalDeviceFormatProperties")( | ||
| 420 | vk.physical, VK_FORMAT_E5B9G9R9_UFLOAT_PACK32, &features); | ||
| 421 | if (features.optimalTilingFeatures & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT) { | ||
| 422 | std::puts("Packed R32_UINT exemption not exercised: native RGB9E5 storage"); | ||
| 423 | return true; | ||
| 424 | } | ||
| 425 | constexpr auto format = DXGI_FORMAT_R9G9B9E5_SHAREDEXP; | ||
| 426 | ComPtr<ID3D12Resource> resource; | ||
| 427 | if (!create_texture(device, texture(Kind::volume, 8, format), &resource)) { | ||
| 428 | std::puts("Packed R32_UINT exemption not exercised: no emulated RGB9E5 3D storage"); | ||
| 429 | return true; | ||
| 430 | } | ||
| 431 | const UINT first = sliced ? 1 : 0, count = sliced ? 2 : 4; | ||
| 432 | const auto view = uav(Kind::volume, 1, first, count, format); | ||
| 433 | missing_format_features = formatless_read | formatless_write; | ||
| 434 | device->CreateUnorderedAccessView(resource.Get(), nullptr, &view, slot); | ||
| 435 | missing_format_features = 0; | ||
| 436 | const auto key = read_slot(slot); | ||
| 437 | g::Image decoded{}; | ||
| 438 | EXPECT(g::decode(key, decoded) && decoded.first_mip == 1 && (key[4] & g::volume_uav_flag)); | ||
| 439 | EXPECT(key[7] == (g::volume_uav_word(first, count) | g::packed_uav_flag)); | ||
| 440 | return true; | ||
| 441 | } | ||
| 442 | |||
| 362 | bool check_standalone(bool sliced) { | 443 | bool check_standalone(bool sliced) { |
| 363 | ComPtr<ID3D12Device> device; | 444 | ComPtr<ID3D12Device> device; |
| 364 | EXPECT(SUCCEEDED(rt::d3d12::create_vulkan_device_standalone(IID_PPV_ARGS(&device)))); | 445 | EXPECT(SUCCEEDED(rt::d3d12::create_vulkan_device_standalone(IID_PPV_ARGS(&device)))); |
| @@ -428,7 +509,11 @@ bool check_cpu_clear(ID3D12Device* device, vktest::Device& vk, D3D12_CPU_DESCRIP | |||
| 428 | 509 | ||
| 429 | bool test(vktest::Device& vk, const char* shader, bool enable_slices) { | 510 | bool test(vktest::Device& vk, const char* shader, bool enable_slices) { |
| 430 | rt::d3d12::VulkanDeviceInfo info{}; | 511 | rt::d3d12::VulkanDeviceInfo info{}; |
| 431 | info.get_instance_proc = vk.instance_proc; | 512 | real_instance_proc = vk.instance_proc; |
| 513 | real_format_properties = vk.instance_fn<PFN_vkGetPhysicalDeviceFormatProperties2>( | ||
| 514 | "vkGetPhysicalDeviceFormatProperties2"); | ||
| 515 | EXPECT(real_format_properties); | ||
| 516 | info.get_instance_proc = masked_instance_proc; | ||
| 432 | info.instance = vk.instance; | 517 | info.instance = vk.instance; |
| 433 | info.physical = vk.physical; | 518 | info.physical = vk.physical; |
| 434 | info.device = vk.device; | 519 | info.device = vk.device; |
| @@ -463,6 +548,8 @@ bool test(vktest::Device& vk, const char* shader, bool enable_slices) { | |||
| 463 | 548 | ||
| 464 | using enum Kind; | 549 | using enum Kind; |
| 465 | EXPECT(check_refusals(device.Get(), source)); | 550 | EXPECT(check_refusals(device.Get(), source)); |
| 551 | EXPECT(check_formatless_features(device.Get(), source, info.sliced_3d_uav)); | ||
| 552 | EXPECT(check_packed_formatless_exemption(device.Get(), vk, source, info.sliced_3d_uav)); | ||
| 466 | Compute compute(vk); | 553 | Compute compute(vk); |
| 467 | if (shader) EXPECT(compute.create(device.Get(), shader)); | 554 | if (shader) EXPECT(compute.create(device.Get(), shader)); |
| 468 | auto* dispatch = shader ? &compute : nullptr; | 555 | auto* dispatch = shader ? &compute : nullptr; |
diff --git a/runtime/test/shaders/cs_texture_uav.asm b/runtime/test/shaders/cs_texture_uav.asm index 4c1a7c8fc..b5180a881 100644 --- a/runtime/test/shaders/cs_texture_uav.asm +++ b/runtime/test/shaders/cs_texture_uav.asm | |||
| @@ -14,8 +14,13 @@ v_lshl_add_u32 v3, v1, 4, v3 | |||
| 14 | v_add_nc_u32_e32 v3, v0, v3 | 14 | v_add_nc_u32_e32 v3, v0, v3 |
| 15 | v_add_nc_u32_e32 v3, 1000, v3 | 15 | v_add_nc_u32_e32 v3, 1000, v3 |
| 16 | s_cmp_eq_u32 s8, 0 | 16 | s_cmp_eq_u32 s8, 0 |
| 17 | s_cbranch_scc0 6 | 17 | s_cbranch_scc0 11 |
| 18 | image_load v4, v0, s[0:7] dmask:0x1 dim:SQ_RSRC_IMG_1D unorm | 18 | image_load v4, v0, s[0:7] dmask:0x1 dim:SQ_RSRC_IMG_1D unorm |
| 19 | ; The sampled and formatless coherent reads must agree; a mismatch changes the result. | ||
| 20 | image_load v5, v0, s[0:7] dmask:0x1 dim:SQ_RSRC_IMG_1D unorm glc dlc | ||
| 21 | s_waitcnt vmcnt(0) | ||
| 22 | v_xor_b32_e32 v5, v4, v5 | ||
| 23 | v_add_nc_u32_e32 v3, v3, v5 | ||
| 19 | v_add_nc_u32_e32 v3, v3, v4 | 24 | v_add_nc_u32_e32 v3, v3, v4 |
| 20 | image_store v3, v0, s[0:7] dmask:0x1 dim:SQ_RSRC_IMG_1D unorm | 25 | image_store v3, v0, s[0:7] dmask:0x1 dim:SQ_RSRC_IMG_1D unorm |
| 21 | s_endpgm | 26 | s_endpgm |
| @@ -27,8 +32,13 @@ v_add_nc_u32_e32 v3, v3, v4 | |||
| 27 | image_store v3, v[0:1], s[0:7] dmask:0x1 dim:SQ_RSRC_IMG_1D_ARRAY unorm | 32 | image_store v3, v[0:1], s[0:7] dmask:0x1 dim:SQ_RSRC_IMG_1D_ARRAY unorm |
| 28 | s_endpgm | 33 | s_endpgm |
| 29 | s_cmp_eq_u32 s8, 2 | 34 | s_cmp_eq_u32 s8, 2 |
| 30 | s_cbranch_scc0 6 | 35 | s_cbranch_scc0 11 |
| 31 | image_load v4, v[0:2], s[0:7] dmask:0x1 dim:SQ_RSRC_IMG_3D unorm | 36 | image_load v4, v[0:2], s[0:7] dmask:0x1 dim:SQ_RSRC_IMG_3D unorm |
| 37 | ; A sliced UAV must see the same texel via the rebased sampled alias and the sliced storage view. | ||
| 38 | image_load v5, v[0:2], s[0:7] dmask:0x1 dim:SQ_RSRC_IMG_3D unorm glc dlc | ||
| 39 | s_waitcnt vmcnt(0) | ||
| 40 | v_xor_b32_e32 v5, v4, v5 | ||
| 41 | v_add_nc_u32_e32 v3, v3, v5 | ||
| 32 | v_add_nc_u32_e32 v3, v3, v4 | 42 | v_add_nc_u32_e32 v3, v3, v4 |
| 33 | image_store v3, v[0:2], s[0:7] dmask:0x1 dim:SQ_RSRC_IMG_3D unorm | 43 | image_store v3, v[0:2], s[0:7] dmask:0x1 dim:SQ_RSRC_IMG_3D unorm |
| 34 | s_endpgm | 44 | s_endpgm |