upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 11:41:37 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 11:41:37 -0500
commit2fe08d7e18d8a11307ced0b7c04eb650b72bc157 (patch)
tree032470a666cd2f94257323c9f8c84fe09e59ebe8 /runtime
parentad021b4a2f4617bd36f10f4a5075acaa0107f8ed (diff)
parent8af232c5e54906b3b5cc98417d7d3904c82c7b99 (diff)
Merge pull request #2084 from Luis Baker
Diffstat (limited to 'runtime')
-rw-r--r--runtime/shims/d3d12_vulkan_device.cpp8
-rw-r--r--runtime/src/vulkan_backend.h6
-rw-r--r--runtime/src/vulkan_replay.h34
-rw-r--r--runtime/test/d3d12_uav_clear_test.cpp42
-rw-r--r--runtime/test/replay_uav_clear_offset_test.cpp165
5 files changed, 245 insertions, 10 deletions
diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp
index 3e73d2e82..60a685eef 100644
--- a/runtime/shims/d3d12_vulkan_device.cpp
+++ b/runtime/shims/d3d12_vulkan_device.cpp
@@ -3476,6 +3476,14 @@ public:
3476 info.conditional_rendering, info.ray_tracing, 3476 info.conditional_rendering, info.ray_tracing,
3477 info.xdxr_acceleration_structures && 3477 info.xdxr_acceleration_structures &&
3478 info.buffer_device_address); 3478 info.buffer_device_address);
3479 if (info.physical && state_.format_properties) {
3480 state_.context->set_color_attachment_format(
3481 [physical = info.physical, get_props = state_.format_properties](VkFormat format) {
3482 VkFormatProperties properties{};
3483 get_props(physical, format, &properties);
3484 return (properties.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) != 0;
3485 });
3486 }
3479 if (info.buffer_device_address && state_.context->loaded() && !create_arena()) 3487 if (info.buffer_device_address && state_.context->loaded() && !create_arena())
3480 std::fprintf(stderr, "[rt] d3d12: the GPU address arena could not be created\n"); 3488 std::fprintf(stderr, "[rt] d3d12: the GPU address arena could not be created\n");
3481 if (info.conditional_rendering) { 3489 if (info.conditional_rendering) {
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h
index 93d7e5259..57615f6f8 100644
--- a/runtime/src/vulkan_backend.h
+++ b/runtime/src/vulkan_backend.h
@@ -1017,6 +1017,12 @@ public:
1017 replay_.zero_offset = offset; 1017 replay_.zero_offset = offset;
1018 replay_.zero_size = size; 1018 replay_.zero_size = size;
1019 } 1019 }
1020
1021 // Physical-device COLOR_ATTACHMENT format check for reinterpreted UAV clears.
1022 void set_color_attachment_format(std::function<bool(VkFormat)> check) {
1023 std::unique_lock lock(resource_mutex_);
1024 replay_.color_attachment_format = std::move(check);
1025 }
1020 std::uint64_t heap_base() { 1026 std::uint64_t heap_base() {
1021 std::shared_lock lock(resource_mutex_); 1027 std::shared_lock lock(resource_mutex_);
1022 return replay_.heap_base; 1028 return replay_.heap_base;
diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h
index 8ece08d1e..967b310d4 100644
--- a/runtime/src/vulkan_replay.h
+++ b/runtime/src/vulkan_replay.h
@@ -855,6 +855,9 @@ struct ReplayFunctions {
855 std::function<void(VkCommandBuffer, const std::vector<PredicatedCopyDispatch>&)> emit_predicated_copy; 855 std::function<void(VkCommandBuffer, const std::vector<PredicatedCopyDispatch>&)> emit_predicated_copy;
856 // Returns a 2D-array view of the range (caller-owned, alive until execution completes); rectangle clears. 856 // Returns a 2D-array view of the range (caller-owned, alive until execution completes); rectangle clears.
857 std::function<VkImageView(const ResourceBinding&, const VkImageSubresourceRange&)> image_view; 857 std::function<VkImageView(const ResourceBinding&, const VkImageSubresourceRange&)> image_view;
858 // Optional format check for color-attachment reinterpretation on mutable UAV clears; unset assumes every format
859 // except E5B9G9R9_UFLOAT_PACK32 supports COLOR_ATTACHMENT.
860 std::function<bool(VkFormat)> color_attachment_format;
858 // Footprint copies of a depth24_as_float texture (depth24.h): `depth24_prepare` makes the scratch and descriptors 861 // Footprint copies of a depth24_as_float texture (depth24.h): `depth24_prepare` makes the scratch and descriptors
859 // for a job while the list is checked, `depth24_run` records its dispatch. Without them such a copy is unsupported. 862 // for a job while the list is checked, `depth24_run` records its dispatch. Without them such a copy is unsupported.
860 std::function<bool(Depth24Job&, std::vector<Depth24Job>&)> depth24_prepare; // whole copy in, dispatches out 863 std::function<bool(Depth24Job&, std::vector<Depth24Job>&)> depth24_prepare; // whole copy in, dispatches out
@@ -917,7 +920,7 @@ struct Prepared {
917 ResourceBinding binding; 920 ResourceBinding binding;
918 VkImageSubresourceRange range{}; // the barrier range, or the cleared view (cleared aspects only) 921 VkImageSubresourceRange range{}; // the barrier range, or the cleared view (cleared aspects only)
919 VkImageView view = VK_NULL_HANDLE; 922 VkImageView view = VK_NULL_HANDLE;
920 VkFormat clear_format = VK_FORMAT_UNDEFINED; // The RTV format, without changing the resource binding. 923 VkFormat clear_format = VK_FORMAT_UNDEFINED; // The clear view format (RTV or reinterpreted UAV), without changing the resource binding.
921 std::vector<VkClearRect> rects; // rectangle clears, clipped to the mip 924 std::vector<VkClearRect> rects; // rectangle clears, clipped to the mip
922 bool skip = false; // every rectangle was empty 925 bool skip = false; // every rectangle was empty
923 ResourceBinding source; // copies: the source resource 926 ResourceBinding source; // copies: the source resource
@@ -2234,18 +2237,35 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun
2234 const TargetView target{view.resource, view.mip, view.first_layer, view.layer_count}; 2237 const TargetView target{view.resource, view.mip, view.first_layer, view.layer_count};
2235 if (!detail::view_range(p.binding, target, VK_IMAGE_ASPECT_COLOR_BIT, p.range)) 2238 if (!detail::view_range(p.binding, target, VK_IMAGE_ASPECT_COLOR_BIT, p.range))
2236 return ReplayResult::invalid_subresource; 2239 return ReplayResult::invalid_subresource;
2237 // The view's format turns the values into bytes; the image's own format clears them in. 2240 // The view's format turns the values into bytes. When the view format differs from the image's backing
2241 // format (a typeless or castable texture) and can be cleared as a color attachment, clear through a view of
2242 // view_format instead of decoding into the backing format (which turns non-float bit patterns like
2243 // R16G16_UNORM 1.0 = 0xffff or R32_UINT 0x7f800001 into SFLOAT NaNs that vkCmdClearColorImage quiets).
2244 // Emulated R9G9B9E5_SHAREDEXP images (backed by R32_UINT) lack COLOR_ATTACHMENT on E5B9G9R9 and keep the
2245 // bit-exact R32_UINT backing path.
2238 const VkFormat view_format = view.format ? VkFormat(view.format) : p.binding.format; 2246 const VkFormat view_format = view.format ? VkFormat(view.format) : p.binding.format;
2239 if (!uav_clear::encode(view_format, uav->is_float, uav->values, p.element) || 2247 if (!uav_clear::encode(view_format, uav->is_float, uav->values, p.element))
2240 !uav_clear::decode(p.binding.format, p.element, p.clear_color))
2241 return ReplayResult::unsupported; 2248 return ReplayResult::unsupported;
2242 if (!uav->rects.empty() && (!p.binding.color_attachment || p.binding.volume)) 2249 const auto* image_layout = uav_clear::layout(p.binding.format);
2250 if (!image_layout || image_layout->bytes != p.element.size)
2251 return ReplayResult::unsupported;
2252 const bool attachment = p.binding.color_attachment && !p.binding.volume;
2253 const bool view_attachable = vk.color_attachment_format
2254 ? vk.color_attachment_format(view_format)
2255 : view_format != VK_FORMAT_E5B9G9R9_UFLOAT_PACK32;
2256 const bool reinterpret = view_format != p.binding.format && attachment && view_attachable;
2257 const VkFormat clear_format = reinterpret ? view_format : p.binding.format;
2258 if (!uav_clear::decode(clear_format, p.element, p.clear_color))
2259 return ReplayResult::unsupported;
2260 if (reinterpret) p.clear_format = view_format;
2261 if (!uav->rects.empty() && !attachment)
2243 return ReplayResult::unsupported; 2262 return ReplayResult::unsupported;
2244 // Under a predicate a whole view is cleared as one rectangle where the image can be an attachment, as 2263 // Under a predicate a whole view is cleared as one rectangle where the image can be an attachment, as
2245 // render targets are; otherwise vkCmdClearColorImage runs unconditionally and is counted. 2264 // render targets are; otherwise vkCmdClearColorImage runs unconditionally and is counted.
2246 const bool attachment = p.binding.color_attachment && !p.binding.volume;
2247 if (predicated && uav->rects.empty() && !attachment) ++out.unpredicated_copies; 2265 if (predicated && uav->rects.empty() && !attachment) ++out.unpredicated_copies;
2248 const auto rects = attachment ? clear_rects(uav->rects) : std::span<const Rect>(uav->rects); 2266 const auto rects = (reinterpret && uav->rects.empty())
2267 ? std::span<const Rect>(&whole_view, 1)
2268 : attachment ? clear_rects(uav->rects) : std::span<const Rect>(uav->rects);
2249 if (const auto r = rect_view(rects, target, VK_IMAGE_ASPECT_COLOR_BIT); r != ReplayResult::success) 2269 if (const auto r = rect_view(rects, target, VK_IMAGE_ASPECT_COLOR_BIT); r != ReplayResult::success)
2250 return r; 2270 return r;
2251 if (!p.skip && p.rects.empty() && !vk.clear_color_image) return ReplayResult::unsupported; 2271 if (!p.skip && p.rects.empty() && !vk.clear_color_image) return ReplayResult::unsupported;
diff --git a/runtime/test/d3d12_uav_clear_test.cpp b/runtime/test/d3d12_uav_clear_test.cpp
index 1968dc371..27664696e 100644
--- a/runtime/test/d3d12_uav_clear_test.cpp
+++ b/runtime/test/d3d12_uav_clear_test.cpp
@@ -574,9 +574,47 @@ int main() {
574 {"RGBA8_UNORM, Float and Uint", DXGI_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R8G8B8A8_UNORM, 8, 8, 1, 1, false, 574 {"RGBA8_UNORM, Float and Uint", DXGI_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R8G8B8A8_UNORM, 8, 8, 1, 1, false,
575 {texture_clear(VK_FORMAT_R8G8B8A8_UNORM, true, floats(1.0f, 0.5f, 0.0f, 1.0f), 0, 0, 1), 575 {texture_clear(VK_FORMAT_R8G8B8A8_UNORM, true, floats(1.0f, 0.5f, 0.0f, 1.0f), 0, 0, 1),
576 texture_clear(VK_FORMAT_R8G8B8A8_UNORM, false, {0x10, 0x20, 0x130, 0x40}, 0, 0, 1, {{1, 1, 3, 7}})}}, 576 texture_clear(VK_FORMAT_R8G8B8A8_UNORM, false, {0x10, 0x20, 0x130, 0x40}, 0, 0, 1, {{1, 1, 3, 7}})}},
577 {"R32_TYPELESS through an R32_UINT view", DXGI_FORMAT_R32_TYPELESS, VK_FORMAT_R32_SFLOAT, 8, 4, 2, 1, false, 577 {"R32_TYPELESS through R32_UINT / R32_SINT / R32_SFLOAT (including NaN bits)", DXGI_FORMAT_R32_TYPELESS,
578 VK_FORMAT_R32_SFLOAT, 8, 4, 2, 1, false,
578 {texture_clear(VK_FORMAT_R32_UINT, false, {0x40490fdb, 0, 0, 0}, 0, 0, 1), 579 {texture_clear(VK_FORMAT_R32_UINT, false, {0x40490fdb, 0, 0, 0}, 0, 0, 1),
579 texture_clear(VK_FORMAT_R32_SFLOAT, true, floats(-0.25f, 0, 0, 0), 1, 0, 1)}}, 580 texture_clear(VK_FORMAT_R32_UINT, false, {0x7f800001u, 0, 0, 0}, 0, 0, 1),
581 texture_clear(VK_FORMAT_R32_UINT, false, {0xffffffffu, 0, 0, 0}, 0, 0, 1, {{1, 1, 6, 3}}),
582 texture_clear(VK_FORMAT_R32_SINT, false, {0xff800001u, 0, 0, 0}, 1, 0, 1),
583 texture_clear(VK_FORMAT_R32_SFLOAT, true, floats(-0.25f, 0, 0, 0), 1, 0, 1, {{0, 0, 2, 2}})}},
584 {"R32G32_TYPELESS through R32G32_UINT (NaN bit patterns)", DXGI_FORMAT_R32G32_TYPELESS,
585 VK_FORMAT_R32G32_SFLOAT, 8, 4, 1, 1, false,
586 {texture_clear(VK_FORMAT_R32G32_UINT, false, {0x7f800001u, 0xffffffffu, 0, 0}, 0, 0, 1),
587 texture_clear(VK_FORMAT_R32G32_SINT, false, {0xff800001u, 0x7fffffffu, 0, 0}, 0, 0, 1, {{2, 1, 6, 3}})}},
588 {"R32G32B32A32_TYPELESS through R32G32B32A32_UINT (NaN bit patterns)", DXGI_FORMAT_R32G32B32A32_TYPELESS,
589 VK_FORMAT_R32G32B32A32_SFLOAT, 4, 4, 1, 1, false,
590 {texture_clear(VK_FORMAT_R32G32B32A32_UINT, false, {0x7f800001u, 0xffffffffu, 0x7fffffffu, 0xff800001u},
591 0, 0, 1)}},
592 {"R16G16_TYPELESS through UNORM, UINT, SINT and SNORM", DXGI_FORMAT_R16G16_TYPELESS,
593 VK_FORMAT_R16G16_SFLOAT, 8, 8, 2, 2, false,
594 {texture_clear(VK_FORMAT_R16G16_UNORM, true, floats(1.0f, 1.0f, 0.0f, 0.0f), 0, 0, 2),
595 texture_clear(VK_FORMAT_R16G16_UNORM, true, floats(0.5f, 1.0f, 0.0f, 0.0f), 0, 0, 1, {{1, 1, 5, 4}}),
596 texture_clear(VK_FORMAT_R16G16_UINT, false, {0xffffu, 0x7fffu, 0, 0}, 0, 1, 1),
597 texture_clear(VK_FORMAT_R16G16_UINT, false, {0x7c01u, 0xfc01u, 0, 0}, 0, 1, 1, {{2, 2, 6, 6}}),
598 texture_clear(VK_FORMAT_R16G16_SINT, false, {0xffffu, 0x8000u, 0, 0}, 1, 0, 1),
599 texture_clear(VK_FORMAT_R16G16_SNORM, true, floats(1.0f, -1.0f, 0.0f, 0.0f), 1, 1, 1)}},
600 {"R16G16B16A16_TYPELESS through UNORM, UINT, SINT and SNORM", DXGI_FORMAT_R16G16B16A16_TYPELESS,
601 VK_FORMAT_R16G16B16A16_SFLOAT, 8, 8, 2, 2, false,
602 {texture_clear(VK_FORMAT_R16G16B16A16_UNORM, true, floats(1.0f, 0.5f, 0.0f, 1.0f), 0, 0, 2),
603 texture_clear(VK_FORMAT_R16G16B16A16_UINT, false, {0xffffu, 0x7fffu, 0x7c01u, 0xfc01u}, 0, 1, 1),
604 texture_clear(VK_FORMAT_R16G16B16A16_SINT, true, floats(-1.0f, 32767.0f, -32768.0f, 0.0f), 0, 0, 1,
605 {{1, 2, 6, 5}}),
606 texture_clear(VK_FORMAT_R16G16B16A16_SNORM, true, floats(1.0f, -1.0f, 0.5f, 0.0f), 1, 0, 2)}},
607 {"R8G8B8A8_TYPELESS through UINT, SINT and SNORM", DXGI_FORMAT_R8G8B8A8_TYPELESS,
608 VK_FORMAT_R8G8B8A8_UNORM, 8, 8, 2, 1, false,
609 {texture_clear(VK_FORMAT_R8G8B8A8_UINT, false, {0xffu, 0x80u, 0x7fu, 0x01u}, 0, 0, 1),
610 texture_clear(VK_FORMAT_R8G8B8A8_SINT, true, floats(-128.0f, -1.0f, 127.0f, 0.0f), 0, 0, 1,
611 {{1, 1, 5, 6}}),
612 texture_clear(VK_FORMAT_R8G8B8A8_SNORM, true, floats(1.0f, -1.0f, 0.5f, -0.5f), 1, 0, 1)}},
613 {"R16_TYPELESS through UINT, SFLOAT and SNORM", DXGI_FORMAT_R16_TYPELESS,
614 VK_FORMAT_R16_UNORM, 8, 4, 2, 1, false,
615 {texture_clear(VK_FORMAT_R16_UINT, false, {0xffffu, 0, 0, 0}, 0, 0, 1),
616 texture_clear(VK_FORMAT_R16_SFLOAT, true, floats(1.0f, 0, 0, 0), 0, 0, 1, {{2, 1, 6, 3}}),
617 texture_clear(VK_FORMAT_R16_SNORM, true, floats(1.0f, 0, 0, 0), 1, 0, 1)}},
580 {"RGBA16_FLOAT rectangles", DXGI_FORMAT_R16G16B16A16_FLOAT, VK_FORMAT_R16G16B16A16_SFLOAT, 16, 16, 2, 2, 618 {"RGBA16_FLOAT rectangles", DXGI_FORMAT_R16G16B16A16_FLOAT, VK_FORMAT_R16G16B16A16_SFLOAT, 16, 16, 2, 2,
581 false, 619 false,
582 {texture_clear(VK_FORMAT_R16G16B16A16_SFLOAT, true, floats(0.25f, -8.0f, 1.0f, 0.0f), 1, 0, 2, 620 {texture_clear(VK_FORMAT_R16G16B16A16_SFLOAT, true, floats(0.25f, -8.0f, 1.0f, 0.0f), 1, 0, 2,
diff --git a/runtime/test/replay_uav_clear_offset_test.cpp b/runtime/test/replay_uav_clear_offset_test.cpp
index 260237e69..6b59c0245 100644
--- a/runtime/test/replay_uav_clear_offset_test.cpp
+++ b/runtime/test/replay_uav_clear_offset_test.cpp
@@ -3,6 +3,8 @@
3// the resource, as copies and atomic copies already treat it. No Vulkan loader, GPU or game data: the transfer 3// the resource, as copies and atomic copies already treat it. No Vulkan loader, GPU or game data: the transfer
4// functions are fakes that record the ranges they are given. 4// functions are fakes that record the ranges they are given.
5#include "../src/vulkan_replay.h" 5#include "../src/vulkan_replay.h"
6#include <array>
7#include <bit>
6#include <cstdint> 8#include <cstdint>
7#include <cstdio> 9#include <cstdio>
8#include <vector> 10#include <vector>
@@ -14,6 +16,24 @@ struct Write {
14}; 16};
15std::vector<Write> writes; 17std::vector<Write> writes;
16 18
19unsigned image_clears = 0, attachment_clears = 0;
20VkFormat requested_view_format = VK_FORMAT_UNDEFINED;
21VkImageSubresourceRange requested_view_range{};
22VkClearColorValue captured_color{};
23VkClearRect captured_rect{};
24VkRect2D captured_render_area{};
25std::uint32_t captured_render_layers = 0;
26
27void reset_texture_capture() {
28 image_clears = attachment_clears = 0;
29 requested_view_format = VK_FORMAT_UNDEFINED;
30 requested_view_range = {};
31 captured_color = {};
32 captured_rect = {};
33 captured_render_area = {};
34 captured_render_layers = 0;
35}
36
17VKAPI_ATTR void VKAPI_CALL barrier(VkCommandBuffer, VkPipelineStageFlags, VkPipelineStageFlags, VkDependencyFlags, 37VKAPI_ATTR void VKAPI_CALL barrier(VkCommandBuffer, VkPipelineStageFlags, VkPipelineStageFlags, VkDependencyFlags,
18 std::uint32_t, const VkMemoryBarrier*, std::uint32_t, const VkBufferMemoryBarrier*, 38 std::uint32_t, const VkMemoryBarrier*, std::uint32_t, const VkBufferMemoryBarrier*,
19 std::uint32_t, const VkImageMemoryBarrier*) {} 39 std::uint32_t, const VkImageMemoryBarrier*) {}
@@ -26,6 +46,24 @@ VKAPI_ATTR void VKAPI_CALL update(VkCommandBuffer, VkBuffer, VkDeviceSize offset
26VKAPI_ATTR void VKAPI_CALL copy(VkCommandBuffer, VkBuffer, VkBuffer, std::uint32_t count, const VkBufferCopy* r) { 46VKAPI_ATTR void VKAPI_CALL copy(VkCommandBuffer, VkBuffer, VkBuffer, std::uint32_t count, const VkBufferCopy* r) {
27 for (std::uint32_t i = 0; i < count; ++i) writes.push_back({'c', r[i].dstOffset, r[i].size, r[i].srcOffset}); 47 for (std::uint32_t i = 0; i < count; ++i) writes.push_back({'c', r[i].dstOffset, r[i].size, r[i].srcOffset});
28} 48}
49VKAPI_ATTR void VKAPI_CALL clear_image(VkCommandBuffer, VkImage, VkImageLayout, const VkClearColorValue* value,
50 std::uint32_t, const VkImageSubresourceRange*) {
51 captured_color = *value;
52 ++image_clears;
53}
54VKAPI_ATTR void VKAPI_CALL begin_rendering(VkCommandBuffer, const VkRenderingInfo* info) {
55 captured_render_area = info->renderArea;
56 captured_render_layers = info->layerCount;
57}
58VKAPI_ATTR void VKAPI_CALL end_rendering(VkCommandBuffer) {}
59VKAPI_ATTR void VKAPI_CALL clear_attachments(VkCommandBuffer, std::uint32_t count, const VkClearAttachment* values,
60 std::uint32_t rect_count, const VkClearRect* rects) {
61 if (count == 1 && rect_count >= 1) {
62 captured_color = values[0].clearValue.color;
63 captured_rect = rects[0];
64 ++attachment_clears;
65 }
66}
29 67
30int failures = 0; 68int failures = 0;
31void expect(bool ok, const char* what) { 69void expect(bool ok, const char* what) {
@@ -88,7 +126,132 @@ int main() {
88 "rectangle clear replays"); 126 "rectangle clear replays");
89 expect(inside(at + 16, at + 28), "elements 2..5 of a view at 8 are [at + 16, at + 28)"); 127 expect(inside(at + 16, at + 28), "elements 2..5 of a view at 8 are [at + 16, at + 28)");
90 128
129 // Typeless / mutable texture UAV clears where view_format != binding.format must clear through an attachment view
130 // of view_format with clear_color decoded in view_format, avoiding SFLOAT NaN round-trips (issue #1902).
131 ReplayFunctions tex_vk = vk;
132 tex_vk.clear_color_image = clear_image;
133 tex_vk.begin_rendering = begin_rendering;
134 tex_vk.end_rendering = end_rendering;
135 tex_vk.clear_attachments = clear_attachments;
136 tex_vk.image_view = [](const ResourceBinding& b, const VkImageSubresourceRange& r) {
137 requested_view_format = b.format;
138 requested_view_range = r;
139 return reinterpret_cast<VkImageView>(std::uintptr_t{0x2000});
140 };
141
142 const auto run_tex = [&](const ResourceBinding& binding, const UavView& view, bool is_float,
143 std::array<std::uint32_t, 4> values, std::span<const Rect> rects = {},
144 const ReplayFunctions* custom_vk = nullptr) {
145 reset_texture_capture();
146 CommandList list;
147 list.clear_unordered_access(view, is_float, values, rects);
148 list.close();
149 return replay(list, VK_NULL_HANDLE, [&](ResourceId id) { return id == 2 ? &binding : nullptr; },
150 custom_vk ? *custom_vk : tex_vk);
151 };
152
153 ResourceBinding tex{};
154 tex.image = reinterpret_cast<VkImage>(std::uintptr_t{0x3000});
155 tex.format = VK_FORMAT_R16G16_SFLOAT;
156 tex.aspects = VK_IMAGE_ASPECT_COLOR_BIT;
157 tex.width = 8;
158 tex.height = 6;
159 tex.mip_levels = 2;
160 tex.array_layers = 4;
161 tex.color_attachment = true;
162
163 const auto fbits = [](float f) { return std::bit_cast<std::uint32_t>(f); };
164 // R16G16_TYPELESS (SFLOAT) cleared as R16G16_UNORM Float(1.0, 1.0): uses R16G16_UNORM view and float32 {1, 1}.
165 expect(run_tex(tex, {2, false, VK_FORMAT_R16G16_UNORM, 0, 0, 0, 0, 1, 2}, true, {fbits(1.0f), fbits(1.0f), 0, 0}) ==
166 ReplayResult::success,
167 "R16G16_TYPELESS -> R16G16_UNORM whole-view clear replays");
168 expect(image_clears == 0 && attachment_clears == 1,
169 "R16G16_TYPELESS -> R16G16_UNORM uses attachment clear, not vkCmdClearColorImage");
170 expect(requested_view_format == VK_FORMAT_R16G16_UNORM,
171 "R16G16_TYPELESS -> R16G16_UNORM creates image view with view_format");
172 expect(requested_view_range.baseMipLevel == 0 && requested_view_range.baseArrayLayer == 1 &&
173 requested_view_range.layerCount == 2 && captured_render_layers == 2,
174 "reinterpreted UAV clear preserves mip and layer range");
175 expect(captured_color.float32[0] == 1.0f && captured_color.float32[1] == 1.0f,
176 "R16G16_UNORM clear value is decoded in view_format (not half-float NaN)");
177 expect(captured_rect.rect.offset.x == 0 && captured_rect.rect.offset.y == 0 &&
178 captured_rect.rect.extent.width == 8 && captured_rect.rect.extent.height == 6 &&
179 captured_rect.layerCount == 2,
180 "reinterpreted whole-view clear covers full mip extent and layers");
181
182 // R16G16_TYPELESS (SFLOAT) cleared as R16G16_UINT with half-float NaN bit patterns 0xffff and 0x7fff.
183 const Rect sub_rect{1, 1, 4, 3};
184 expect(run_tex(tex, {2, false, VK_FORMAT_R16G16_UINT, 0, 0, 0, 1, 0, 1}, false, {0xffff, 0x7fff, 0, 0},
185 std::span<const Rect>(&sub_rect, 1)) == ReplayResult::success,
186 "R16G16_TYPELESS -> R16G16_UINT rect clear replays");
187 expect(image_clears == 0 && attachment_clears == 1 && requested_view_format == VK_FORMAT_R16G16_UINT,
188 "R16G16_UINT rect clear uses R16G16_UINT view");
189 expect(captured_color.uint32[0] == 0xffff && captured_color.uint32[1] == 0x7fff,
190 "R16G16_UINT clear passes exact uint32 values");
191 expect(captured_render_area.extent.width == 4 && captured_render_area.extent.height == 3 &&
192 captured_rect.rect.extent.width == 3 && captured_rect.rect.extent.height == 2,
193 "R16G16_UINT rect clear clips to mip 1 extent");
194
195 // Same-format whole-view unpredicated clear stays on vkCmdClearColorImage.
196 expect(run_tex(tex, {2, false, VK_FORMAT_R16G16_SFLOAT, 0, 0, 0, 0, 0, 1}, true, {fbits(0.5f), fbits(-2.0f), 0, 0}) ==
197 ReplayResult::success,
198 "same-format R16G16_SFLOAT whole-view clear replays");
199 expect(image_clears == 1 && attachment_clears == 0, "same-format whole-view clear uses vkCmdClearColorImage");
200
201 // R32_TYPELESS (SFLOAT) cleared as R32_UINT with sNaN bit pattern 0x7f800001.
202 ResourceBinding r32 = tex;
203 r32.format = VK_FORMAT_R32_SFLOAT;
204 expect(run_tex(r32, {2, false, VK_FORMAT_R32_UINT, 0, 0, 0, 0, 0, 1}, false, {0x7f800001u, 0, 0, 0}) ==
205 ReplayResult::success,
206 "R32_TYPELESS -> R32_UINT sNaN clear replays");
207 expect(image_clears == 0 && attachment_clears == 1 && requested_view_format == VK_FORMAT_R32_UINT &&
208 captured_color.uint32[0] == 0x7f800001u,
209 "R32_UINT clear uses R32_UINT attachment view with exact sNaN bits");
210
211 // R8G8B8A8_TYPELESS (UNORM) cleared as R8G8B8A8_UINT and R8G8B8A8_SINT uses view_format directly.
212 ResourceBinding rgba8 = tex;
213 rgba8.format = VK_FORMAT_R8G8B8A8_UNORM;
214 expect(run_tex(rgba8, {2, false, VK_FORMAT_R8G8B8A8_UINT, 0, 0, 0, 0, 0, 1}, false, {0xff, 0x80, 0x7f, 1}) ==
215 ReplayResult::success,
216 "R8G8B8A8_TYPELESS -> R8G8B8A8_UINT clear replays");
217 expect(image_clears == 0 && attachment_clears == 1 && requested_view_format == VK_FORMAT_R8G8B8A8_UINT &&
218 captured_color.uint32[0] == 0xff && captured_color.uint32[1] == 0x80 &&
219 captured_color.uint32[2] == 0x7f && captured_color.uint32[3] == 1,
220 "R8G8B8A8_UINT clear uses R8G8B8A8_UINT attachment view");
221
222 // Emulated R9G9B9E5_SHAREDEXP (backed by R32_UINT) must NOT request an E5B9G9R9 color attachment view.
223 ResourceBinding r9e5 = tex;
224 r9e5.format = VK_FORMAT_R32_UINT;
225 expect(run_tex(r9e5, {2, false, VK_FORMAT_E5B9G9R9_UFLOAT_PACK32, 0, 0, 0, 0, 0, 1}, true,
226 {fbits(1.0f), fbits(2.0f), fbits(4.0f), 0}) == ReplayResult::success,
227 "emulated R9G9B9E5 whole-view clear replays");
228 expect(image_clears == 1 && attachment_clears == 0 && captured_color.uint32[0] == 0x94010040u,
229 "emulated R9G9B9E5 whole-view clear stays on R32_UINT vkCmdClearColorImage");
230 expect(run_tex(r9e5, {2, false, VK_FORMAT_E5B9G9R9_UFLOAT_PACK32, 0, 0, 0, 0, 0, 1}, true,
231 {fbits(1.0f), fbits(2.0f), fbits(4.0f), 0}, std::span<const Rect>(&sub_rect, 1)) ==
232 ReplayResult::success,
233 "emulated R9G9B9E5 rect clear replays");
234 expect(image_clears == 0 && attachment_clears == 1 && requested_view_format == VK_FORMAT_R32_UINT &&
235 captured_color.uint32[0] == 0x94010040u,
236 "emulated R9G9B9E5 rect clear uses R32_UINT attachment view");
237
238 // When dynamic rendering is unavailable, reinterpretation refuses instead of corrupting via vkCmdClearColorImage.
239 ReplayFunctions no_render = tex_vk;
240 no_render.begin_rendering = nullptr;
241 expect(run_tex(tex, {2, false, VK_FORMAT_R16G16_UNORM, 0, 0, 0, 0, 0, 1}, true, {fbits(1.0f), fbits(1.0f), 0, 0},
242 {}, &no_render) == ReplayResult::unsupported,
243 "reinterpreted UAV clear without dynamic rendering returns unsupported");
244
245 // When color_attachment_format reports a view format unsupported as an attachment, keep the backing format path.
246 ReplayFunctions no_unorm_rt = tex_vk;
247 no_unorm_rt.color_attachment_format = [](VkFormat f) { return f != VK_FORMAT_R16G16_UNORM; };
248 expect(run_tex(tex, {2, false, VK_FORMAT_R16G16_UNORM, 0, 0, 0, 0, 0, 1}, true, {fbits(0.0f), fbits(0.0f), 0, 0},
249 {}, &no_unorm_rt) == ReplayResult::success,
250 "non-attachable view format falls back to backing format");
251 expect(image_clears == 1 && attachment_clears == 0,
252 "non-attachable view format whole-view clear uses vkCmdClearColorImage");
253
91 if (failures) return 1; 254 if (failures) return 1;
92 std::printf("replay_uav_clear_offset: buffer UAV clears write their resource's range of a shared buffer\n"); 255 std::printf("replay_uav_clear_offset: buffer offsets and typeless texture view-format clears all passed\n");
93 return 0; 256 return 0;
94} 257}