diff options
| author | Vonsek <45567075+Vonsek@users.noreply.github.com> | 2026-11-22 15:51:40 +0100 |
|---|---|---|
| committer | Vonsek <45567075+Vonsek@users.noreply.github.com> | 2026-11-22 15:51:40 +0100 |
| commit | df91c6624b69ee4e6c6eeca20ed68d331ed0e728 (patch) | |
| tree | 323c88b9df89bf8abaa2b98d10808474ab675dc2 /runtime/test/vk_bc_zero_init_test.cpp | |
| parent | b40d8039a98f57b26bd05860febbabbe59b97873 (diff) | |
| parent | 6a6704df1592e1dc4a3215ddf1423a78adb52045 (diff) | |
Merge main into test/valu-int-misc (#1492)
Conflicted files: runtime/test/shaders/check.py, scripts/run_gpu_tests.py
AI-assisted.
Diffstat (limited to 'runtime/test/vk_bc_zero_init_test.cpp')
| -rw-r--r-- | runtime/test/vk_bc_zero_init_test.cpp | 388 |
1 files changed, 388 insertions, 0 deletions
diff --git a/runtime/test/vk_bc_zero_init_test.cpp b/runtime/test/vk_bc_zero_init_test.cpp new file mode 100644 index 000000000..6edc36fcb --- /dev/null +++ b/runtime/test/vk_bc_zero_init_test.cpp | |||
| @@ -0,0 +1,388 @@ | |||
| 1 | // Committed block-compressed (BC*) texture zero-initialization regression (D3D-220, #1725): | ||
| 2 | // D3D12 committed textures start zeroed unless D3D12_HEAP_FLAG_CREATE_NOT_ZEROED is set. Vulkan forbids | ||
| 3 | // vkCmdClearColorImage on BC formats, so detail::initialize() must zero-fill them via vkCmdCopyBufferToImage | ||
| 4 | // from either the device zero page (vk.zero_buffer) or a zeroed transfer-scratch chunk (vk.copy_scratch). | ||
| 5 | // | ||
| 6 | // Cases (all on explicitly poisoned 0x5A backing memory when host-visible, plus default device memory): | ||
| 7 | // 1. Path A (vk.zero_buffer): CopyTextureRegion R32G32_UINT 2x2 -> BC1 16x16 at (8, 4) — untouched blocks are 0x00. | ||
| 8 | // 2. Path A (vk.zero_buffer): CopyResource R32G32_UINT 3x2 -> BC1 12x8, 2 mips — mip 1 untouched block (bytes 56..63) is 0x00. | ||
| 9 | // 3. Path A (tiny 32-byte vk.zero_buffer): 32x32 BC7 image tiled in multiple row/column chunks — all 1024 bytes are 0x00. | ||
| 10 | // 4. Path B (enable_copy_scratch, no zero_buffer — matches d3d12_copy_xfmt): both D3D-220 cases match bit for bit. | ||
| 11 | // 5. not_zeroed = true (CREATE_NOT_ZEROED): zero-fill is skipped. | ||
| 12 | // Skips with exit code 77 when no Vulkan 1.3 device with textureCompressionBC is available. No game data. | ||
| 13 | #include "vk_test_device.h" | ||
| 14 | #include "../src/vulkan_backend.h" | ||
| 15 | |||
| 16 | #include <algorithm> | ||
| 17 | #include <cstdint> | ||
| 18 | #include <cstdio> | ||
| 19 | #include <cstring> | ||
| 20 | #include <vector> | ||
| 21 | |||
| 22 | #define EXPECT(condition) do { \ | ||
| 23 | if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \ | ||
| 24 | } while (0) | ||
| 25 | |||
| 26 | namespace { | ||
| 27 | |||
| 28 | struct OwnedImage { | ||
| 29 | VkDevice device = VK_NULL_HANDLE; | ||
| 30 | PFN_vkDestroyImage destroy_image = nullptr; | ||
| 31 | PFN_vkFreeMemory free_memory = nullptr; | ||
| 32 | VkImage image = VK_NULL_HANDLE; | ||
| 33 | VkDeviceMemory memory = VK_NULL_HANDLE; | ||
| 34 | bool poisoned = false; | ||
| 35 | |||
| 36 | ~OwnedImage() { | ||
| 37 | if (image && destroy_image) destroy_image(device, image, nullptr); | ||
| 38 | if (memory && free_memory) free_memory(device, memory, nullptr); | ||
| 39 | } | ||
| 40 | OwnedImage(const OwnedImage&) = delete; | ||
| 41 | OwnedImage& operator=(const OwnedImage&) = delete; | ||
| 42 | OwnedImage() = default; | ||
| 43 | }; | ||
| 44 | |||
| 45 | bool create_image(const vktest::Device& d, VkFormat format, std::uint32_t width, std::uint32_t height, | ||
| 46 | std::uint32_t mips, VkBuffer poison_buf, OwnedImage& out) { | ||
| 47 | out.device = d.device; | ||
| 48 | out.destroy_image = d.fn<PFN_vkDestroyImage>("vkDestroyImage"); | ||
| 49 | out.free_memory = d.fn<PFN_vkFreeMemory>("vkFreeMemory"); | ||
| 50 | |||
| 51 | VkImageCreateInfo info{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO}; | ||
| 52 | info.imageType = VK_IMAGE_TYPE_2D; | ||
| 53 | info.format = format; | ||
| 54 | info.extent = {width, height, 1}; | ||
| 55 | info.mipLevels = mips; | ||
| 56 | info.arrayLayers = 1; | ||
| 57 | info.samples = VK_SAMPLE_COUNT_1_BIT; | ||
| 58 | info.tiling = VK_IMAGE_TILING_OPTIMAL; | ||
| 59 | info.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; | ||
| 60 | if (d.fn<PFN_vkCreateImage>("vkCreateImage")(d.device, &info, nullptr, &out.image) != VK_SUCCESS) return false; | ||
| 61 | |||
| 62 | VkMemoryRequirements req{}; | ||
| 63 | d.fn<PFN_vkGetImageMemoryRequirements>("vkGetImageMemoryRequirements")(d.device, out.image, &req); | ||
| 64 | VkPhysicalDeviceMemoryProperties mp{}; | ||
| 65 | d.instance_fn<PFN_vkGetPhysicalDeviceMemoryProperties>("vkGetPhysicalDeviceMemoryProperties")(d.physical, &mp); | ||
| 66 | |||
| 67 | std::uint32_t type = UINT32_MAX; | ||
| 68 | for (std::uint32_t i = 0; i < mp.memoryTypeCount; ++i) { | ||
| 69 | if (!(req.memoryTypeBits & (1u << i))) continue; | ||
| 70 | if (type == UINT32_MAX) type = i; | ||
| 71 | if (mp.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) { | ||
| 72 | type = i; | ||
| 73 | break; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | if (type == UINT32_MAX) return false; | ||
| 77 | |||
| 78 | VkMemoryAllocateInfo alloc{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO}; | ||
| 79 | alloc.allocationSize = req.size; | ||
| 80 | alloc.memoryTypeIndex = type; | ||
| 81 | if (d.fn<PFN_vkAllocateMemory>("vkAllocateMemory")(d.device, &alloc, nullptr, &out.memory) != VK_SUCCESS) | ||
| 82 | return false; | ||
| 83 | if (d.fn<PFN_vkBindImageMemory>("vkBindImageMemory")(d.device, out.image, out.memory, 0) != VK_SUCCESS) | ||
| 84 | return false; | ||
| 85 | |||
| 86 | // Pre-fill every mip on the GPU with 0x5A so stale recycled VRAM is modeled deterministically on every GPU | ||
| 87 | // (discrete and integrated alike), before registering the image in LayoutTracker as UNDEFINED. | ||
| 88 | if (poison_buf) { | ||
| 89 | const bool ok = const_cast<vktest::Device&>(d).run([&](VkCommandBuffer cb) { | ||
| 90 | VkImageMemoryBarrier to_dst{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER}; | ||
| 91 | to_dst.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; | ||
| 92 | to_dst.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; | ||
| 93 | to_dst.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; | ||
| 94 | to_dst.srcQueueFamilyIndex = to_dst.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; | ||
| 95 | to_dst.image = out.image; | ||
| 96 | to_dst.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mips, 0, 1}; | ||
| 97 | d.fn<PFN_vkCmdPipelineBarrier>("vkCmdPipelineBarrier")(cb, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, | ||
| 98 | VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, | ||
| 99 | nullptr, 1, &to_dst); | ||
| 100 | std::vector<VkBufferImageCopy> regions; | ||
| 101 | for (std::uint32_t m = 0; m < mips; ++m) { | ||
| 102 | VkBufferImageCopy r{}; | ||
| 103 | r.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, m, 0, 1}; | ||
| 104 | r.imageExtent = {std::max(1u, width >> m), std::max(1u, height >> m), 1}; | ||
| 105 | regions.push_back(r); | ||
| 106 | } | ||
| 107 | d.fn<PFN_vkCmdCopyBufferToImage>("vkCmdCopyBufferToImage")( | ||
| 108 | cb, poison_buf, out.image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, std::uint32_t(regions.size()), | ||
| 109 | regions.data()); | ||
| 110 | return true; | ||
| 111 | }); | ||
| 112 | if (!ok) return false; | ||
| 113 | out.poisoned = true; | ||
| 114 | } | ||
| 115 | return true; | ||
| 116 | } | ||
| 117 | |||
| 118 | VkDeviceMemory allocate_device_local(const vktest::Device& d, const VkMemoryRequirements& req) { | ||
| 119 | VkPhysicalDeviceMemoryProperties mp{}; | ||
| 120 | d.instance_fn<PFN_vkGetPhysicalDeviceMemoryProperties>("vkGetPhysicalDeviceMemoryProperties")(d.physical, &mp); | ||
| 121 | std::uint32_t chosen = UINT32_MAX; | ||
| 122 | for (std::uint32_t i = 0; i < mp.memoryTypeCount; ++i) { | ||
| 123 | if (!(req.memoryTypeBits & (1u << i))) continue; | ||
| 124 | if (chosen == UINT32_MAX) chosen = i; | ||
| 125 | if (mp.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) { | ||
| 126 | chosen = i; | ||
| 127 | break; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | if (chosen == UINT32_MAX) return VK_NULL_HANDLE; | ||
| 131 | VkMemoryAllocateInfo info{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO}; | ||
| 132 | info.allocationSize = req.size; | ||
| 133 | info.memoryTypeIndex = chosen; | ||
| 134 | VkDeviceMemory mem = VK_NULL_HANDLE; | ||
| 135 | if (d.fn<PFN_vkAllocateMemory>("vkAllocateMemory")(d.device, &info, nullptr, &mem) != VK_SUCCESS) | ||
| 136 | return VK_NULL_HANDLE; | ||
| 137 | return mem; | ||
| 138 | } | ||
| 139 | |||
| 140 | // Runs the two D3D-220 partial uncompressed -> BC1 copies on a given context/queue: | ||
| 141 | // (a) CopyTextureRegion R32G32_UINT 2x2 -> BC1 16x16 at (8, 4) | ||
| 142 | // (b) CopyResource R32G32_UINT 3x2 -> BC1 12x8, 2 mips | ||
| 143 | int run_d3d220_cases(const vktest::Device& d, leonida::VulkanContext& ctx, leonida::VulkanQueue& q, | ||
| 144 | VkBuffer poison_buf, VkBuffer up_buf, void* up_ptr, VkBuffer rb_buf, void* rb_ptr, | ||
| 145 | const char* label) { | ||
| 146 | using namespace leonida; | ||
| 147 | |||
| 148 | auto* up = static_cast<std::uint8_t*>(up_ptr); | ||
| 149 | auto* rb = static_cast<std::uint8_t*>(rb_ptr); | ||
| 150 | for (std::size_t i = 0; i < 4096; ++i) up[i] = static_cast<std::uint8_t>(i * 7 + 3); | ||
| 151 | |||
| 152 | // Case (a): CopyTextureRegion R32G32_UINT 2x2 -> BC1 16x16 at (8, 4). | ||
| 153 | // Destination has 4x4 BC1 blocks (8 bytes/block). Only blocks (2,1), (3,1), (2,2), (3,2) are written; | ||
| 154 | // the other 12 blocks (including block (0,0) at byte 0) must be 0x00. | ||
| 155 | { | ||
| 156 | OwnedImage src_img, dst_img; | ||
| 157 | EXPECT(create_image(d, VK_FORMAT_R32G32_UINT, 2, 2, 1, VK_NULL_HANDLE, src_img)); | ||
| 158 | EXPECT(create_image(d, VK_FORMAT_BC1_RGBA_UNORM_BLOCK, 16, 16, 1, poison_buf, dst_img)); | ||
| 159 | |||
| 160 | ResourceBinding src_rb{}; | ||
| 161 | src_rb.image = src_img.image; | ||
| 162 | src_rb.format = VK_FORMAT_R32G32_UINT; | ||
| 163 | src_rb.aspects = VK_IMAGE_ASPECT_COLOR_BIT; | ||
| 164 | src_rb.width = 2; src_rb.height = 2; src_rb.mip_levels = 1; src_rb.array_layers = 1; src_rb.depth = 1; src_rb.samples = 1; | ||
| 165 | |||
| 166 | ResourceBinding dst_rb{}; | ||
| 167 | dst_rb.image = dst_img.image; | ||
| 168 | dst_rb.format = VK_FORMAT_BC1_RGBA_UNORM_BLOCK; | ||
| 169 | dst_rb.aspects = VK_IMAGE_ASPECT_COLOR_BIT; | ||
| 170 | dst_rb.width = 16; dst_rb.height = 16; dst_rb.mip_levels = 1; dst_rb.array_layers = 1; dst_rb.depth = 1; dst_rb.samples = 1; | ||
| 171 | |||
| 172 | ctx.add_resource(10, src_rb, VK_IMAGE_LAYOUT_UNDEFINED); | ||
| 173 | ctx.add_resource(11, dst_rb, VK_IMAGE_LAYOUT_UNDEFINED); | ||
| 174 | std::memset(rb, 0xCD, 4096); | ||
| 175 | |||
| 176 | CommandList list; | ||
| 177 | CopyLocation up_loc{1, true, 0, 0, 2, 2, 1, 256}; | ||
| 178 | CopyLocation src_loc{10, false, 0, 0, 0, 0, 0, 0}; | ||
| 179 | CopyLocation dst_loc{11, false, 0, 0, 0, 0, 0, 0}; | ||
| 180 | CopyLocation rb_loc{2, true, 0, 0, 16, 16, 1, 256}; | ||
| 181 | EXPECT(list.copy_texture({src_loc, 0, 0, 0, up_loc, false, {}})); | ||
| 182 | EXPECT(list.transition({10, all_subresources, d3d12_state::copy_dest, d3d12_state::copy_source})); | ||
| 183 | EXPECT(list.copy_texture({dst_loc, 8, 4, 0, src_loc, false, {}})); | ||
| 184 | EXPECT(list.transition({11, all_subresources, d3d12_state::copy_dest, d3d12_state::copy_source})); | ||
| 185 | EXPECT(list.copy_texture({rb_loc, 0, 0, 0, dst_loc, false, {}})); | ||
| 186 | EXPECT(list.close()); | ||
| 187 | |||
| 188 | const CommandList* ptr = &list; | ||
| 189 | EXPECT(q.execute({&ptr, 1}) == ReplayResult::success); | ||
| 190 | EXPECT(q.idle()); | ||
| 191 | |||
| 192 | // Check every 8-byte block across the 4x4 block grid (row pitch 256). | ||
| 193 | for (std::uint32_t by = 0; by < 4; ++by) { | ||
| 194 | for (std::uint32_t bx = 0; bx < 4; ++bx) { | ||
| 195 | const std::uint8_t* got = rb + std::size_t(by) * 256 + std::size_t(bx) * 8; | ||
| 196 | if (bx >= 2 && bx < 4 && by >= 1 && by < 3) { | ||
| 197 | const std::uint8_t* want = up + std::size_t(by - 1) * 256 + std::size_t(bx - 2) * 8; | ||
| 198 | EXPECT(std::memcmp(got, want, 8) == 0); | ||
| 199 | } else { | ||
| 200 | for (int k = 0; k < 8; ++k) EXPECT(got[k] == 0x00); | ||
| 201 | } | ||
| 202 | } | ||
| 203 | } | ||
| 204 | ctx.remove_resource(10); | ||
| 205 | ctx.remove_resource(11); | ||
| 206 | } | ||
| 207 | |||
| 208 | // Case (b): CopyResource R32G32_UINT 3x2 -> BC1 12x8, 2 mips. | ||
| 209 | // Mip 0: 3x2 -> 12x8 (3x2 = 6 blocks written). | ||
| 210 | // Mip 1: 1x1 -> 6x4 (2x1 = 2 blocks; block 0 is written, block 1 at bytes 8..15 of mip 1 is untouched -> 0x00). | ||
| 211 | { | ||
| 212 | OwnedImage src_img, dst_img; | ||
| 213 | EXPECT(create_image(d, VK_FORMAT_R32G32_UINT, 3, 2, 2, VK_NULL_HANDLE, src_img)); | ||
| 214 | EXPECT(create_image(d, VK_FORMAT_BC1_RGBA_UNORM_BLOCK, 12, 8, 2, poison_buf, dst_img)); | ||
| 215 | |||
| 216 | ResourceBinding src_rb{}; | ||
| 217 | src_rb.image = src_img.image; | ||
| 218 | src_rb.format = VK_FORMAT_R32G32_UINT; | ||
| 219 | src_rb.aspects = VK_IMAGE_ASPECT_COLOR_BIT; | ||
| 220 | src_rb.width = 3; src_rb.height = 2; src_rb.mip_levels = 2; src_rb.array_layers = 1; src_rb.depth = 1; src_rb.samples = 1; | ||
| 221 | |||
| 222 | ResourceBinding dst_rb{}; | ||
| 223 | dst_rb.image = dst_img.image; | ||
| 224 | dst_rb.format = VK_FORMAT_BC1_RGBA_UNORM_BLOCK; | ||
| 225 | dst_rb.aspects = VK_IMAGE_ASPECT_COLOR_BIT; | ||
| 226 | dst_rb.width = 12; dst_rb.height = 8; dst_rb.mip_levels = 2; dst_rb.array_layers = 1; dst_rb.depth = 1; dst_rb.samples = 1; | ||
| 227 | |||
| 228 | ctx.add_resource(12, src_rb, VK_IMAGE_LAYOUT_UNDEFINED); | ||
| 229 | ctx.add_resource(13, dst_rb, VK_IMAGE_LAYOUT_UNDEFINED); | ||
| 230 | std::memset(rb, 0xCD, 4096); | ||
| 231 | |||
| 232 | // Upload mip 0 at offset 0 (2 rows * 256 = 512 bytes) and mip 1 at offset 512 (1 row * 256). | ||
| 233 | CommandList list; | ||
| 234 | CopyLocation up0{1, true, 0, 0, 3, 2, 1, 256}; | ||
| 235 | CopyLocation up1{1, true, 0, 512, 1, 1, 1, 256}; | ||
| 236 | CopyLocation src0{12, false, 0, 0, 0, 0, 0, 0}; | ||
| 237 | CopyLocation src1{12, false, 1, 0, 0, 0, 0, 0}; | ||
| 238 | CopyLocation dst0{13, false, 0, 0, 0, 0, 0, 0}; | ||
| 239 | CopyLocation dst1{13, false, 1, 0, 0, 0, 0, 0}; | ||
| 240 | CopyLocation rb0{2, true, 0, 0, 12, 8, 1, 256}; | ||
| 241 | CopyLocation rb1{2, true, 0, 512, 6, 4, 1, 256}; | ||
| 242 | EXPECT(list.copy_texture({src0, 0, 0, 0, up0, false, {}})); | ||
| 243 | EXPECT(list.copy_texture({src1, 0, 0, 0, up1, false, {}})); | ||
| 244 | EXPECT(list.transition({12, all_subresources, d3d12_state::copy_dest, d3d12_state::copy_source})); | ||
| 245 | EXPECT(list.copy_resource({13, 12})); | ||
| 246 | EXPECT(list.transition({13, all_subresources, d3d12_state::copy_dest, d3d12_state::copy_source})); | ||
| 247 | EXPECT(list.copy_texture({rb0, 0, 0, 0, dst0, false, {}})); | ||
| 248 | EXPECT(list.copy_texture({rb1, 0, 0, 0, dst1, false, {}})); | ||
| 249 | EXPECT(list.close()); | ||
| 250 | |||
| 251 | const CommandList* ptr = &list; | ||
| 252 | EXPECT(q.execute({&ptr, 1}) == ReplayResult::success); | ||
| 253 | EXPECT(q.idle()); | ||
| 254 | |||
| 255 | // Mip 0: 2 rows of 3 blocks (24 bytes per row). | ||
| 256 | EXPECT(std::memcmp(rb + 0, up + 0, 24) == 0); | ||
| 257 | EXPECT(std::memcmp(rb + 256, up + 256, 24) == 0); | ||
| 258 | // Mip 1: block 0 (bytes 0..7) matches up + 512; untouched block 1 (bytes 8..15) must be 0x00. | ||
| 259 | EXPECT(std::memcmp(rb + 512, up + 512, 8) == 0); | ||
| 260 | for (int k = 8; k < 16; ++k) EXPECT(rb[512 + k] == 0x00); | ||
| 261 | |||
| 262 | ctx.remove_resource(12); | ||
| 263 | ctx.remove_resource(13); | ||
| 264 | } | ||
| 265 | |||
| 266 | (void)up_buf; | ||
| 267 | (void)rb_buf; | ||
| 268 | std::printf("bc zero-init (%s): partial CopyTextureRegion and 2-mip CopyResource ok\n", label); | ||
| 269 | return 0; | ||
| 270 | } | ||
| 271 | |||
| 272 | } // namespace | ||
| 273 | |||
| 274 | int main() { | ||
| 275 | using namespace leonida; | ||
| 276 | |||
| 277 | vktest::Device d; | ||
| 278 | if (!d.create(false) || !d.bc) { | ||
| 279 | std::fprintf(stderr, "vk_bc_zero_init_test: no Vulkan 1.3 device with BC support, skipped\n"); | ||
| 280 | return 77; | ||
| 281 | } | ||
| 282 | |||
| 283 | void* up_ptr = nullptr; | ||
| 284 | void* rb_ptr = nullptr; | ||
| 285 | void* zero_ptr = nullptr; | ||
| 286 | void* poison_ptr = nullptr; | ||
| 287 | const VkBuffer up_buf = d.buffer(4096, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, &up_ptr); | ||
| 288 | const VkBuffer rb_buf = d.buffer(8192, VK_BUFFER_USAGE_TRANSFER_DST_BIT, &rb_ptr); | ||
| 289 | const VkBuffer zero_buf = d.buffer(65536, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, &zero_ptr); | ||
| 290 | const VkBuffer poison_buf = d.buffer(4096, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, &poison_ptr); | ||
| 291 | EXPECT(up_buf && rb_buf && zero_buf && poison_buf); | ||
| 292 | std::memset(zero_ptr, 0, 65536); | ||
| 293 | std::memset(poison_ptr, 0x5A, 4096); | ||
| 294 | |||
| 295 | ResourceBinding up_rb{}; | ||
| 296 | up_rb.buffer = up_buf; | ||
| 297 | up_rb.size = 4096; | ||
| 298 | ResourceBinding rb_rb{}; | ||
| 299 | rb_rb.buffer = rb_buf; | ||
| 300 | rb_rb.size = 8192; | ||
| 301 | rb_rb.host_readable = true; | ||
| 302 | |||
| 303 | // Path A: device arena zero page (vk.zero_buffer). | ||
| 304 | { | ||
| 305 | VulkanContext ctx(d.device_proc, d.device, true); | ||
| 306 | EXPECT(ctx.loaded()); | ||
| 307 | ctx.set_zero_page(zero_buf, 0, 65536); | ||
| 308 | ctx.add_resource(1, up_rb); | ||
| 309 | ctx.add_resource(2, rb_rb); | ||
| 310 | VulkanQueue q(ctx, d.queue, d.family); | ||
| 311 | EXPECT(q.ok()); | ||
| 312 | EXPECT(run_d3d220_cases(d, ctx, q, poison_buf, up_buf, up_ptr, rb_buf, rb_ptr, "zero_buffer") == 0); | ||
| 313 | |||
| 314 | // Multi-tile chunking: shrink zero_page to 32 bytes (2 BC7 blocks) and zero-init a 32x32 BC7 image (64 blocks). | ||
| 315 | ctx.set_zero_page(zero_buf, 16, 32); | ||
| 316 | OwnedImage bc7_img; | ||
| 317 | EXPECT(create_image(d, VK_FORMAT_BC7_UNORM_BLOCK, 32, 32, 1, poison_buf, bc7_img)); | ||
| 318 | ResourceBinding bc7_rb{}; | ||
| 319 | bc7_rb.image = bc7_img.image; | ||
| 320 | bc7_rb.format = VK_FORMAT_BC7_UNORM_BLOCK; | ||
| 321 | bc7_rb.aspects = VK_IMAGE_ASPECT_COLOR_BIT; | ||
| 322 | bc7_rb.width = 32; bc7_rb.height = 32; bc7_rb.mip_levels = 1; bc7_rb.array_layers = 1; bc7_rb.depth = 1; bc7_rb.samples = 1; | ||
| 323 | ctx.add_resource(20, bc7_rb, VK_IMAGE_LAYOUT_UNDEFINED); | ||
| 324 | std::memset(rb_ptr, 0xCD, 8192); | ||
| 325 | |||
| 326 | CommandList list; | ||
| 327 | EXPECT(list.transition({20, all_subresources, d3d12_state::copy_dest, d3d12_state::copy_source})); | ||
| 328 | CopyLocation bc7_loc{20, false, 0, 0, 0, 0, 0, 0}; | ||
| 329 | CopyLocation rb_loc{2, true, 0, 0, 32, 32, 1, 256}; | ||
| 330 | EXPECT(list.copy_texture({rb_loc, 0, 0, 0, bc7_loc, false, {}})); | ||
| 331 | EXPECT(list.close()); | ||
| 332 | const CommandList* ptr = &list; | ||
| 333 | EXPECT(q.execute({&ptr, 1}) == ReplayResult::success); | ||
| 334 | EXPECT(q.idle()); | ||
| 335 | |||
| 336 | auto* rb = static_cast<std::uint8_t*>(rb_ptr); | ||
| 337 | for (std::uint32_t row = 0; row < 8; ++row) | ||
| 338 | for (std::uint32_t x = 0; x < 8 * 16; ++x) | ||
| 339 | EXPECT(rb[row * 256 + x] == 0x00); | ||
| 340 | ctx.remove_resource(20); | ||
| 341 | std::printf("bc zero-init (tiny 32-byte zero_buffer chunking on 32x32 BC7): ok\n"); | ||
| 342 | |||
| 343 | // CREATE_NOT_ZEROED (not_zeroed = true) must skip zero-fill. | ||
| 344 | ctx.set_zero_page(zero_buf, 0, 65536); | ||
| 345 | OwnedImage nz_img; | ||
| 346 | EXPECT(create_image(d, VK_FORMAT_BC1_RGBA_UNORM_BLOCK, 16, 16, 1, poison_buf, nz_img)); | ||
| 347 | EXPECT(nz_img.poisoned); | ||
| 348 | ResourceBinding nz_rb{}; | ||
| 349 | nz_rb.image = nz_img.image; | ||
| 350 | nz_rb.format = VK_FORMAT_BC1_RGBA_UNORM_BLOCK; | ||
| 351 | nz_rb.aspects = VK_IMAGE_ASPECT_COLOR_BIT; | ||
| 352 | nz_rb.width = 16; nz_rb.height = 16; nz_rb.mip_levels = 1; nz_rb.array_layers = 1; nz_rb.depth = 1; nz_rb.samples = 1; | ||
| 353 | nz_rb.not_zeroed = true; | ||
| 354 | ctx.add_resource(21, nz_rb, VK_IMAGE_LAYOUT_UNDEFINED); | ||
| 355 | std::memset(rb_ptr, 0xCD, 8192); | ||
| 356 | |||
| 357 | CommandList nz_list; | ||
| 358 | EXPECT(nz_list.transition({21, all_subresources, d3d12_state::copy_dest, d3d12_state::copy_source})); | ||
| 359 | CopyLocation nz_loc{21, false, 0, 0, 0, 0, 0, 0}; | ||
| 360 | CopyLocation nz_rb_loc{2, true, 0, 0, 16, 16, 1, 256}; | ||
| 361 | EXPECT(nz_list.copy_texture({nz_rb_loc, 0, 0, 0, nz_loc, false, {}})); | ||
| 362 | EXPECT(nz_list.close()); | ||
| 363 | const CommandList* nz_ptr = &nz_list; | ||
| 364 | EXPECT(q.execute({&nz_ptr, 1}) == ReplayResult::success); | ||
| 365 | EXPECT(q.idle()); | ||
| 366 | EXPECT(rb[0] == 0x5A); | ||
| 367 | ctx.remove_resource(21); | ||
| 368 | std::printf("bc zero-init (not_zeroed = true skips zero-fill): ok\n"); | ||
| 369 | } | ||
| 370 | |||
| 371 | // Path B: copy_scratch + vkCmdFillBuffer fallback (no zero_buffer, matching d3d12_copy_xfmt without BDA). | ||
| 372 | { | ||
| 373 | VulkanContext ctx(d.device_proc, d.device, true); | ||
| 374 | EXPECT(ctx.loaded()); | ||
| 375 | ctx.enable_copy_scratch(d.device_proc, [&](const VkMemoryRequirements& req) { | ||
| 376 | return allocate_device_local(d, req); | ||
| 377 | }); | ||
| 378 | ctx.add_resource(1, up_rb); | ||
| 379 | ctx.add_resource(2, rb_rb); | ||
| 380 | VulkanQueue q(ctx, d.queue, d.family); | ||
| 381 | EXPECT(q.ok()); | ||
| 382 | EXPECT(run_d3d220_cases(d, ctx, q, poison_buf, up_buf, up_ptr, rb_buf, rb_ptr, "copy_scratch") == 0); | ||
| 383 | } | ||
| 384 | |||
| 385 | EXPECT(d.validation_errors() == 0); | ||
| 386 | std::printf("vk_bc_zero_init_test: ok\n"); | ||
| 387 | return 0; | ||
| 388 | } | ||