upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
authoriFeyz <62035583+iFeyz@users.noreply.github.com>2026-11-24 21:16:09 +0100
committeriFeyz <62035583+iFeyz@users.noreply.github.com>2026-11-24 21:16:09 +0100
commitbfe8d5c4d83f22570b604dbf70b80c226d3e91b0 (patch)
tree86300dee2d5886a2cbb101509b54062e677c97ec /runtime/src
parent00fc23b9a9eeb1a75f1fec2b58024bea15c26663 (diff)
parent7c47fd946ba19461ed7ff0b12383b55e23bf79c7 (diff)
Merge commit 'refs/pull/1064/head' of https://github.com/ubiquitarianism/leonida into feat/direct-heap-graphics
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/cpu_emulation.h97
-rw-r--r--runtime/src/mods.h4
-rw-r--r--runtime/src/shader_library.h23
-rw-r--r--runtime/src/vulkan_backend.h14
-rw-r--r--runtime/src/vulkan_replay.h56
-rw-r--r--runtime/src/xdxr_build.h14
-rw-r--r--runtime/src/xdxr_descs.h7
7 files changed, 184 insertions, 31 deletions
diff --git a/runtime/src/cpu_emulation.h b/runtime/src/cpu_emulation.h
index fac13eeee..8df3b55d0 100644
--- a/runtime/src/cpu_emulation.h
+++ b/runtime/src/cpu_emulation.h
@@ -272,6 +272,84 @@ inline bool apply(const Decoded& d, CONTEXT* c, EXCEPTION_RECORD* fault = nullpt
272 return true; 272 return true;
273} 273}
274 274
275inline uint64_t gpr_read(const CONTEXT* c, int r) {
276 switch (r) {
277 case 0: return c->Rax; case 1: return c->Rcx; case 2: return c->Rdx; case 3: return c->Rbx;
278 case 4: return c->Rsp; case 5: return c->Rbp; case 6: return c->Rsi; case 7: return c->Rdi;
279 case 8: return c->R8; case 9: return c->R9; case 10: return c->R10; case 11: return c->R11;
280 case 12: return c->R12; case 13: return c->R13; case 14: return c->R14; case 15: return c->R15;
281 default: return 0;
282 }
283}
284
285enum class ClzeroLoopResult { no_match, ok, faulted };
286
287inline void apply_cmp_flags(CONTEXT* c, uint64_t lhs, uint64_t rhs) {
288 const uint64_t diff = lhs - rhs;
289 uint32_t flags = c->EFlags & ~DWORD(0x8D5u); // clear CF(0), PF(2), AF(4), ZF(6), SF(7), OF(11)
290 if (lhs < rhs) flags |= 0x1u; // CF
291 uint8_t lo8 = static_cast<uint8_t>(diff);
292 lo8 ^= lo8 >> 4;
293 lo8 ^= lo8 >> 2;
294 lo8 ^= lo8 >> 1;
295 if ((lo8 & 1u) == 0) flags |= 0x4u; // PF
296 if ((lhs ^ rhs ^ diff) & 0x10u) flags |= 0x10u; // AF
297 if (diff == 0) flags |= 0x40u; // ZF
298 if (diff >> 63) flags |= 0x80u; // SF
299 if (((lhs ^ rhs) & (lhs ^ diff)) >> 63) flags |= 0x800u; // OF
300 c->EFlags = flags;
301}
302
303// Fast-path for the Zen 2 zeroing loop: `clzero; add rax, 0x40; cmp rax, reg64; jb -12` (12 bytes).
304// Zeroes the entire writable span in one memset instead of raising #UD + VirtualQuery per 64-byte line.
305inline ClzeroLoopResult try_clzero_loop(const uint8_t* code, std::size_t avail, CONTEXT* c, EXCEPTION_RECORD* fault) {
306 if (avail < 12) return ClzeroLoopResult::no_match;
307 if (code[0] != 0x0F || code[1] != 0x01 || code[2] != 0xFC) return ClzeroLoopResult::no_match;
308 if (code[3] != 0x48 || code[4] != 0x83 || code[5] != 0xC0 || code[6] != 0x40) return ClzeroLoopResult::no_match;
309 if ((code[7] != 0x48 && code[7] != 0x4C) || code[8] != 0x39 || (code[9] & 0xC7) != 0xC0)
310 return ClzeroLoopResult::no_match;
311 if (code[10] != 0x72 || code[11] != 0xF4) return ClzeroLoopResult::no_match;
312
313 const int limit_reg = ((code[9] >> 3) & 7) | ((code[7] & 4) << 1);
314 if (limit_reg == 0 || limit_reg == 4) return ClzeroLoopResult::no_match;
315
316 const uint64_t start = c->Rax;
317 const uint64_t limit = gpr_read(c, limit_reg);
318 if ((start & 63) != 0 || limit <= start + 64) return ClzeroLoopResult::no_match;
319
320 uint64_t total = (limit - start + 63) & ~uint64_t(63);
321 constexpr uint64_t kMaxBatch = uint64_t(64) << 20;
322 if (total > kMaxBatch) total = kMaxBatch;
323 const uint64_t end = start + total;
324
325 for (uint64_t cur = start; cur < end;) {
326 const DWORD err = writable(cur);
327 if (err) {
328 if (cur > start) {
329 std::memset(reinterpret_cast<void*>(start), 0, std::size_t(cur - start));
330 c->Rax = cur;
331 apply_cmp_flags(c, cur, limit);
332 }
333 if (fault) {
334 std::memset(fault, 0, sizeof *fault);
335 fault->ExceptionCode = err;
336 fault->NumberParameters = 2;
337 fault->ExceptionInformation[0] = 1;
338 fault->ExceptionInformation[1] = ULONG_PTR(cur);
339 }
340 return ClzeroLoopResult::faulted;
341 }
342 const uint64_t next_page = (cur & ~uint64_t(0xFFF)) + 0x1000;
343 cur = next_page < end ? next_page : end;
344 }
345
346 std::memset(reinterpret_cast<void*>(start), 0, std::size_t(total));
347 c->Rax = end;
348 apply_cmp_flags(c, end, limit);
349 if (end >= limit) c->Rip += 12;
350 return ClzeroLoopResult::ok;
351}
352
275inline LONG CALLBACK handler(EXCEPTION_POINTERS* info) { 353inline LONG CALLBACK handler(EXCEPTION_POINTERS* info) {
276 if (info->ExceptionRecord->ExceptionCode != STATUS_ILLEGAL_INSTRUCTION) return EXCEPTION_CONTINUE_SEARCH; 354 if (info->ExceptionRecord->ExceptionCode != STATUS_ILLEGAL_INSTRUCTION) return EXCEPTION_CONTINUE_SEARCH;
277 CONTEXT* c = info->ContextRecord; 355 CONTEXT* c = info->ContextRecord;
@@ -281,6 +359,25 @@ inline LONG CALLBACK handler(EXCEPTION_POINTERS* info) {
281 const Decoded d = decode(code, avail); 359 const Decoded d = decode(code, avail);
282 if (d.op == Op::none) return EXCEPTION_CONTINUE_SEARCH; 360 if (d.op == Op::none) return EXCEPTION_CONTINUE_SEARCH;
283 EXCEPTION_RECORD fault; 361 EXCEPTION_RECORD fault;
362 if (d.op == Op::clzero && !d.addr32 && d.length == 3) {
363 const uint64_t fault_rip = c->Rip;
364 const ClzeroLoopResult lr = try_clzero_loop(code, avail, c, &fault);
365 if (lr == ClzeroLoopResult::ok) {
366 if (count(d.op).fetch_add(1, std::memory_order_relaxed) == 0)
367 std::fprintf(stderr, "[rt] cpu: this CPU has no %s; emulating it (first at %p)\n", name(d.op),
368 reinterpret_cast<void*>(fault_rip));
369 return EXCEPTION_CONTINUE_EXECUTION;
370 }
371 if (lr == ClzeroLoopResult::faulted) {
372 EXCEPTION_RECORD* r = info->ExceptionRecord;
373 r->ExceptionCode = fault.ExceptionCode;
374 r->ExceptionFlags = 0;
375 r->NumberParameters = fault.NumberParameters;
376 r->ExceptionInformation[0] = fault.ExceptionInformation[0];
377 r->ExceptionInformation[1] = fault.ExceptionInformation[1];
378 return EXCEPTION_CONTINUE_SEARCH;
379 }
380 }
284 if (!apply(d, c, &fault)) { 381 if (!apply(d, c, &fault)) {
285 // CLZERO on a line it cannot write: the CPU would raise that fault at this instruction, so the next handlers 382 // CLZERO on a line it cannot write: the CPU would raise that fault at this instruction, so the next handlers
286 // see it instead of the #UD (RIP still points at the instruction). 383 // see it instead of the #UD (RIP still points at the instruction).
diff --git a/runtime/src/mods.h b/runtime/src/mods.h
index 10155bf19..bd635e9a0 100644
--- a/runtime/src/mods.h
+++ b/runtime/src/mods.h
@@ -78,8 +78,8 @@ struct LoadResult {
78// "0x..." -> that value; anything else -> joaat(name). Returns false for an empty name or a malformed hex value. 78// "0x..." -> that value; anything else -> joaat(name). Returns false for an empty name or a malformed hex value.
79inline bool parse_model_id(const std::string& text, uint32_t& out) { 79inline bool parse_model_id(const std::string& text, uint32_t& out) {
80 if (text.empty()) return false; 80 if (text.empty()) return false;
81 if (text.size() > 2 && text[0] == '0' && (text[1] == 'x' || text[1] == 'X')) { 81 if (text.size() >= 2 && text[0] == '0' && (text[1] == 'x' || text[1] == 'X')) {
82 if (text.size() > 10) return false; 82 if (text.size() == 2 || text.size() > 10) return false;
83 uint32_t v = 0; 83 uint32_t v = 0;
84 for (std::size_t i = 2; i < text.size(); ++i) { 84 for (std::size_t i = 2; i < text.size(); ++i) {
85 const char c = text[i]; 85 const char c = text[i];
diff --git a/runtime/src/shader_library.h b/runtime/src/shader_library.h
index 9cfb902ef..a4471143d 100644
--- a/runtime/src/shader_library.h
+++ b/runtime/src/shader_library.h
@@ -246,7 +246,7 @@ inline ZlibError inflate_codes(BitReader& in, std::vector<std::uint8_t>& out, co
246 if (d < 0 || d >= 30) return ZlibError::data; 246 if (d < 0 || d >= 30) return ZlibError::data;
247 const std::size_t distance = std::size_t(distance_base[d]) + in.bits(distance_extra[d]); 247 const std::size_t distance = std::size_t(distance_base[d]) + in.bits(distance_extra[d]);
248 if (in.failed() || distance > out.size()) return ZlibError::data; 248 if (in.failed() || distance > out.size()) return ZlibError::data;
249 if (out.size() + length > limit) return ZlibError::limit; 249 if (out.size() > limit || length > limit - out.size()) return ZlibError::limit;
250 const std::size_t from = out.size() - distance; 250 const std::size_t from = out.size() - distance;
251 for (std::size_t i = 0; i < length; ++i) out.push_back(out[from + i]); 251 for (std::size_t i = 0; i < length; ++i) out.push_back(out[from + i]);
252 } 252 }
@@ -284,7 +284,7 @@ inline bool zlib_inflate(std::span<const std::uint8_t> in, std::vector<std::uint
284 if (!reader.byte(x)) return fail(ZlibError::data); 284 if (!reader.byte(x)) return fail(ZlibError::data);
285 const unsigned length = b[0] | (b[1] << 8), complement = b[2] | (b[3] << 8); 285 const unsigned length = b[0] | (b[1] << 8), complement = b[2] | (b[3] << 8);
286 if (length != (~complement & 0xffff)) return fail(ZlibError::data); 286 if (length != (~complement & 0xffff)) return fail(ZlibError::data);
287 if (out.size() + length > limit) return fail(ZlibError::limit); 287 if (out.size() > limit || length > limit - out.size()) return fail(ZlibError::limit);
288 for (unsigned i = 0; i < length; ++i) { 288 for (unsigned i = 0; i < length; ++i) {
289 std::uint8_t x; 289 std::uint8_t x;
290 if (!reader.byte(x)) return fail(ZlibError::data); 290 if (!reader.byte(x)) return fail(ZlibError::data);
@@ -349,7 +349,7 @@ inline bool zlib_inflate(std::span<const std::uint8_t> in, std::vector<std::uint
349 } 349 }
350 // Adler-32, big-endian, after the deflate data's last whole byte. 350 // Adler-32, big-endian, after the deflate data's last whole byte.
351 const std::size_t at = 2 + reader.position(); 351 const std::size_t at = 2 + reader.position();
352 if (at + 4 > in.size()) return fail(ZlibError::data); 352 if (at > in.size() || in.size() - at < 4) return fail(ZlibError::data);
353 std::uint32_t a = 1, b = 0; 353 std::uint32_t a = 1, b = 0;
354 for (const std::uint8_t x : out) { 354 for (const std::uint8_t x : out) {
355 a = (a + x) % 65521; 355 a = (a + x) % 65521;
@@ -1054,9 +1054,20 @@ private:
1054 if (const Json* fh = meta.find("f16_halves"); fh && fh->kind == Json::Kind::object) { 1054 if (const Json* fh = meta.find("f16_halves"); fh && fh->kind == Json::Kind::object) {
1055 for (const auto& [key, val] : fh->object) { 1055 for (const auto& [key, val] : fh->object) {
1056 if (key.empty() || val.kind != Json::Kind::array || val.array.size() != 2) continue; 1056 if (key.empty() || val.kind != Json::Kind::array || val.array.size() != 2) continue;
1057 char* end = nullptr; 1057 std::uint64_t param = 0;
1058 const unsigned long param = std::strtoul(key.c_str(), &end, 10); 1058 bool ok = true;
1059 if (!end || *end != '\0') continue; 1059 for (const char c : key) {
1060 if (c < '0' || c > '9') {
1061 ok = false;
1062 break;
1063 }
1064 param = param * 10u + std::uint64_t(c - '0');
1065 if (param > UINT32_MAX) {
1066 ok = false;
1067 break;
1068 }
1069 }
1070 if (!ok) continue;
1060 e.f16_halves[std::uint32_t(param)] = {u32(&val.array[0]), u32(&val.array[1])}; 1071 e.f16_halves[std::uint32_t(param)] = {u32(&val.array[0]), u32(&val.array[1])};
1061 } 1072 }
1062 } 1073 }
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h
index f90dd6a3f..a95994256 100644
--- a/runtime/src/vulkan_backend.h
+++ b/runtime/src/vulkan_backend.h
@@ -1017,7 +1017,7 @@ public:
1017 // binding's host pointer is null on DEFAULT and READBACK heaps, whose mapped bytes the GPU can write. 1017 // binding's host pointer is null on DEFAULT and READBACK heaps, whose mapped bytes the GPU can write.
1018 replay_.host_at = [this](std::uint64_t va, std::uint64_t bytes, const std::uint8_t*& host) { 1018 replay_.host_at = [this](std::uint64_t va, std::uint64_t bytes, const std::uint8_t*& host) {
1019 BufferVaIndex<ResourceId>::Hit hit{}; 1019 BufferVaIndex<ResourceId>::Hit hit{};
1020 if (!buffer_vas_.find(va, hit)) return HostReadStatus::absent; 1020 if (!buffer_vas_.find(va, hit, bytes)) return HostReadStatus::absent;
1021 const ResourceBinding* b = resolve(hit.id); 1021 const ResourceBinding* b = resolve(hit.id);
1022 if (!b) return HostReadStatus::absent; 1022 if (!b) return HostReadStatus::absent;
1023 if (!b->host) return HostReadStatus::not_cpu_written; 1023 if (!b->host) return HostReadStatus::not_cpu_written;
@@ -2553,6 +2553,18 @@ private:
2553 discard(); 2553 discard();
2554 return ReplayResult::unsupported; 2554 return ReplayResult::unsupported;
2555 } 2555 }
2556 // D3D12 ends every ExecuteCommandLists with all its work done and its writes visible to the next one on the
2557 // queue (implicit state decay relies on it), and a list may then touch the same memory without a barrier:
2558 // two submissions copying into one buffer, say. Vulkan orders submissions on a queue only by what their
2559 // barriers say, so this one starts with a full memory barrier against everything submitted before it
2560 // (synchronization validation: WRITE_AFTER_WRITE between vk_queue_test's back-to-back copies).
2561 if (context_.vk.vkCmdPipelineBarrier) { // a mocked context may leave it out
2562 VkMemoryBarrier full{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
2563 full.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT;
2564 full.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT;
2565 context_.vk.vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
2566 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 1, &full, 0, nullptr, 0, nullptr);
2567 }
2556 if (!tracker_lock.owns_lock()) tracker_lock.lock(); 2568 if (!tracker_lock.owns_lock()) tracker_lock.lock();
2557 if (!transfer_only && context_.bindless_prepare_ && !context_.bindless_prepare_(command_buffer)) { 2569 if (!transfer_only && context_.bindless_prepare_ && !context_.bindless_prepare_(command_buffer)) {
2558 const bool ended = context_.vk.vkEndCommandBuffer(command_buffer) == VK_SUCCESS; 2570 const bool ended = context_.vk.vkEndCommandBuffer(command_buffer) == VK_SUCCESS;
diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h
index bda530a7a..d5a73b909 100644
--- a/runtime/src/vulkan_replay.h
+++ b/runtime/src/vulkan_replay.h
@@ -1607,6 +1607,7 @@ inline ReplayResult prepare_buffer_uav_clear(const ClearUnorderedAccess& clear,
1607 if (offset > UINT64_MAX - p.binding.buffer_offset || 1607 if (offset > UINT64_MAX - p.binding.buffer_offset ||
1608 bytes > UINT64_MAX - (p.binding.buffer_offset + offset)) return ReplayResult::invalid_clear; 1608 bytes > UINT64_MAX - (p.binding.buffer_offset + offset)) return ReplayResult::invalid_clear;
1609 offset += p.binding.buffer_offset; // the view is relative to the resource, which starts at buffer_offset 1609 offset += p.binding.buffer_offset; // the view is relative to the resource, which starts at buffer_offset
1610 if (offset % std::min(e, 4u)) return ReplayResult::unsupported;
1610 if (e > 4 && bytes % 4) { 1611 if (e > 4 && bytes % 4) {
1611 return ReplayResult::unsupported; 1612 return ReplayResult::unsupported;
1612 } 1613 }
@@ -1878,27 +1879,48 @@ inline ReplayResult prepare_texture_copy(const CopyTexture& copy, Prepared& p) {
1878 return ReplayResult::success; 1879 return ReplayResult::success;
1879} 1880}
1880 1881
1881// RGBA8 CopyTiles accepts arbitrary byte offsets into its linear buffer, while VkBufferImageCopy requires texel 1882// CopyTiles accepts arbitrary byte offsets into its linear buffer, while VkBufferImageCopy requires texel/block
1882// alignment. Stage only the occupied texels through aligned transfer scratch; leave partial-tile padding alone. 1883// alignment (VUID-vkCmdCopyBufferToImage-dstImage-07975). Stage only the occupied texels/blocks through aligned
1883inline bool stage_unaligned_tile(const ReplayFunctions& vk, Prepared& p, bool upload) { 1884// transfer scratch; leave partial-tile padding alone.
1885inline bool stage_unaligned_tile(const ReplayFunctions& vk, Prepared& p, const ResourceBinding& image,
1886 const BlockFormat& fmt, bool upload) {
1884 auto& region = p.buffer_image_copies.front(); 1887 auto& region = p.buffer_image_copies.front();
1885 const VkDeviceSize row_bytes = VkDeviceSize(region.imageExtent.width) * 4; 1888 if (!fmt.bytes || !fmt.block || image.rgb96 ||
1886 const VkDeviceSize pitch = VkDeviceSize(region.bufferRowLength) * 4; 1889 (image.depth24_as_float && region.imageSubresource.aspectMask == VK_IMAGE_ASPECT_DEPTH_BIT))
1887 const auto rows = region.imageExtent.height; 1890 return false;
1891 const VkDeviceSize blocks_w = (VkDeviceSize(region.imageExtent.width) + fmt.block - 1) / fmt.block;
1892 const VkDeviceSize rows = (VkDeviceSize(region.imageExtent.height) + fmt.block - 1) / fmt.block;
1893 const VkDeviceSize depth = region.imageExtent.depth;
1894 const VkDeviceSize row_bytes = blocks_w * fmt.bytes;
1895 const VkDeviceSize pitch =
1896 region.bufferRowLength ? VkDeviceSize(region.bufferRowLength / fmt.block) * fmt.bytes : row_bytes;
1897 const VkDeviceSize slice_rows =
1898 region.bufferImageHeight ? VkDeviceSize(region.bufferImageHeight / fmt.block) : rows;
1899 const VkDeviceSize slice_pitch = slice_rows * pitch;
1900 const VkDeviceSize slice_bytes = row_bytes * rows;
1901 const VkDeviceSize total_bytes = slice_bytes * depth;
1888 VkDeviceSize scratch_offset = 0; 1902 VkDeviceSize scratch_offset = 0;
1889 if (!vk.copy_scratch || !vk.copy_scratch(row_bytes * rows, p.tile_scratch, scratch_offset)) return false; 1903 if (!total_bytes || !vk.copy_scratch || !vk.copy_scratch(total_bytes, p.tile_scratch, scratch_offset)) return false;
1890 const auto app_offset = region.bufferOffset; 1904 const auto app_offset = region.bufferOffset;
1891 const auto add_copy = [&](VkDeviceSize linear, VkDeviceSize scratch, VkDeviceSize bytes) { 1905 const auto add_copy = [&](VkDeviceSize linear, VkDeviceSize scratch, VkDeviceSize bytes) {
1892 p.tile_rows.push_back(upload ? VkBufferCopy{linear, scratch, bytes} : VkBufferCopy{scratch, linear, bytes}); 1906 p.tile_rows.push_back(upload ? VkBufferCopy{linear, scratch, bytes} : VkBufferCopy{scratch, linear, bytes});
1893 }; 1907 };
1894 if (row_bytes == pitch) { 1908 if (row_bytes == pitch && (depth == 1 || slice_rows == rows)) {
1895 add_copy(app_offset, scratch_offset, row_bytes * rows); 1909 add_copy(app_offset, scratch_offset, total_bytes);
1910 } else if (row_bytes == pitch) {
1911 for (VkDeviceSize z = 0; z < depth; ++z)
1912 add_copy(app_offset + z * slice_pitch, scratch_offset + z * slice_bytes, slice_bytes);
1896 } else { 1913 } else {
1897 for (std::uint32_t row = 0; row < rows; ++row) 1914 for (VkDeviceSize z = 0; z < depth; ++z) {
1898 add_copy(app_offset + row * pitch, scratch_offset + row * row_bytes, row_bytes); 1915 const VkDeviceSize linear_slice = app_offset + z * slice_pitch;
1916 const VkDeviceSize scratch_slice = scratch_offset + z * slice_bytes;
1917 for (VkDeviceSize row = 0; row < rows; ++row)
1918 add_copy(linear_slice + row * pitch, scratch_slice + row * row_bytes, row_bytes);
1919 }
1899 } 1920 }
1900 region.bufferOffset = scratch_offset; 1921 region.bufferOffset = scratch_offset;
1901 region.bufferRowLength = 0; 1922 region.bufferRowLength = 0;
1923 region.bufferImageHeight = 0;
1902 return true; 1924 return true;
1903} 1925}
1904 1926
@@ -2812,9 +2834,15 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun
2812 // A depth plane footprint of a D24 texture stored as D32: words of 24-bit UNORM, floats in the image. 2834 // A depth plane footprint of a D24 texture stored as D32: words of 24-bit UNORM, floats in the image.
2813 const bool upload = texture_copy->src.footprint; 2835 const bool upload = texture_copy->src.footprint;
2814 const ResourceBinding& image = upload ? p.binding : p.source; 2836 const ResourceBinding& image = upload ? p.binding : p.source;
2815 if (result == ReplayResult::success && texture_copy->tile_footprint && !p.buffer_image_copies.empty() && 2837 if (result == ReplayResult::success && texture_copy->tile_footprint && !p.buffer_image_copies.empty()) {
2816 p.buffer_image_copies.front().bufferOffset % 4 && !detail::stage_unaligned_tile(vk, p, upload)) 2838 const auto& region = p.buffer_image_copies.front();
2817 return ReplayResult::unsupported; 2839 const BlockFormat fmt = copy_block_format(image, region.imageSubresource.aspectMask);
2840 const std::uint32_t alignment =
2841 (region.imageSubresource.aspectMask & depth_stencil_aspects) || image.rgb96 ? 4 : fmt.bytes;
2842 if (alignment > 1 && region.bufferOffset % alignment &&
2843 !detail::stage_unaligned_tile(vk, p, image, fmt, upload))
2844 return ReplayResult::unsupported;
2845 }
2818 const bool depth24_footprint = result == ReplayResult::success && image.depth24_as_float && 2846 const bool depth24_footprint = result == ReplayResult::success && image.depth24_as_float &&
2819 !p.buffer_image_copies.empty() && 2847 !p.buffer_image_copies.empty() &&
2820 p.buffer_image_copies[0].imageSubresource.aspectMask == VK_IMAGE_ASPECT_DEPTH_BIT; 2848 p.buffer_image_copies[0].imageSubresource.aspectMask == VK_IMAGE_ASPECT_DEPTH_BIT;
diff --git a/runtime/src/xdxr_build.h b/runtime/src/xdxr_build.h
index e0851098f..e1c1a233e 100644
--- a/runtime/src/xdxr_build.h
+++ b/runtime/src/xdxr_build.h
@@ -133,7 +133,13 @@ public:
133 // update ignores `pair`. 133 // update ignores `pair`.
134 bool record_blas(VkCommandBuffer cb, const XdxrLayout& layout, const std::vector<XdxrTriangles>& geometries, 134 bool record_blas(VkCommandBuffer cb, const XdxrLayout& layout, const std::vector<XdxrTriangles>& geometries,
135 std::uint64_t dst, std::uint64_t scratch, std::uint64_t update_from = 0, bool pair = false) { 135 std::uint64_t dst, std::uint64_t scratch, std::uint64_t update_from = 0, bool pair = false) {
136 if (!loaded_ || layout.tlas) return false; 136 if (!loaded_ || layout.tlas || !layout.size || !dst || !scratch) return false;
137 std::uint64_t total = 0;
138 for (const auto& geo : geometries) {
139 total += geo.triangle_count;
140 if (total > layout.elements) return false;
141 }
142 if (total != layout.elements) return false;
137 Push p = base(layout, dst, scratch); 143 Push p = base(layout, dst, scratch);
138 if (pair && !update_from) p.flags |= flag_pair; 144 if (pair && !update_from) p.flags |= flag_pair;
139 if (update_from) refit_begin(cb, layout, p, update_from); else begin(cb, layout, p); 145 if (update_from) refit_begin(cb, layout, p, update_from); else begin(cb, layout, p);
@@ -155,7 +161,6 @@ public:
155 dispatch(cb, t, t.count); 161 dispatch(cb, t, t.count);
156 first += geo.triangle_count; 162 first += geo.triangle_count;
157 } 163 }
158 if (first != layout.elements) return false;
159 if (update_from) refit_finish(cb, layout, p); else finish(cb, layout, p); 164 if (update_from) refit_finish(cb, layout, p); else finish(cb, layout, p);
160 return true; 165 return true;
161 } 166 }
@@ -164,7 +169,8 @@ public:
164 // `update_from`: as for record_blas; the instances may move, and name other BLASes. 169 // `update_from`: as for record_blas; the instances may move, and name other BLASes.
165 bool record_tlas(VkCommandBuffer cb, const XdxrLayout& layout, std::uint64_t instances, std::uint64_t dst, 170 bool record_tlas(VkCommandBuffer cb, const XdxrLayout& layout, std::uint64_t instances, std::uint64_t dst,
166 std::uint64_t scratch, std::uint64_t bias = 0, std::uint64_t update_from = 0) { 171 std::uint64_t scratch, std::uint64_t bias = 0, std::uint64_t update_from = 0) {
167 if (!loaded_ || !layout.tlas) return false; 172 if (!loaded_ || !layout.tlas || !layout.size || !dst || !scratch || (layout.elements && !instances))
173 return false;
168 Push p = base(layout, dst, scratch); 174 Push p = base(layout, dst, scratch);
169 p.bias = split(bias); 175 p.bias = split(bias);
170 if (update_from) refit_begin(cb, layout, p, update_from); else begin(cb, layout, p); 176 if (update_from) refit_begin(cb, layout, p, update_from); else begin(cb, layout, p);
@@ -341,7 +347,7 @@ private:
341 // dependency on its results to the caller's barrier (the application's UAV or AS barrier). 347 // dependency on its results to the caller's barrier (the application's UAV or AS barrier).
342 void dispatch(VkCommandBuffer cb, const Push& p, std::uint32_t threads, bool barrier = true) { 348 void dispatch(VkCommandBuffer cb, const Push& p, std::uint32_t threads, bool barrier = true) {
343 vk_.vkCmdPushConstants(cb, layout_, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(Push), &p); 349 vk_.vkCmdPushConstants(cb, layout_, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(Push), &p);
344 vk_.vkCmdDispatch(cb, (threads + 63) / 64, 1, 1); 350 vk_.vkCmdDispatch(cb, std::uint32_t((std::uint64_t(threads) + 63u) / 64u), 1, 1);
345 if (!barrier) return; 351 if (!barrier) return;
346 VkMemoryBarrier b{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; 352 VkMemoryBarrier b{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
347 b.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT; 353 b.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
diff --git a/runtime/src/xdxr_descs.h b/runtime/src/xdxr_descs.h
index b44ce0372..3798b0fe7 100644
--- a/runtime/src/xdxr_descs.h
+++ b/runtime/src/xdxr_descs.h
@@ -99,12 +99,11 @@ constexpr uint32_t kPostbuildInfoOfflineStatistics = 0x10;
99// The record a setting is read from, the way the DLL finds it: the first record of `type` from the start, stepping 99// The record a setting is read from, the way the DLL finds it: the first record of `type` from the start, stepping
100// over types 0, 1 and 2, and stopping at any other type. Null when there is none. 100// over types 0, 1 and 2, and stopping at any other type. Null when there is none.
101inline const StreamRecord* find_stream_record(const StreamDesc& desc, uint32_t type) { 101inline const StreamRecord* find_stream_record(const StreamDesc& desc, uint32_t type) {
102 if (desc.size_in_bytes == 0 || desc.stream == nullptr) 102 if (desc.size_in_bytes < sizeof(StreamRecord) || desc.stream == nullptr)
103 return nullptr; 103 return nullptr;
104 const auto* record = static_cast<const StreamRecord*>(desc.stream); 104 const auto* record = static_cast<const StreamRecord*>(desc.stream);
105 const auto* end = reinterpret_cast<const StreamRecord*>(static_cast<const uint8_t*>(desc.stream) + 105 const uint64_t count = desc.size_in_bytes / sizeof(StreamRecord);
106 desc.size_in_bytes); 106 for (uint64_t i = 0; i < count; ++i, ++record) {
107 for (; record < end; ++record) {
108 if (record->type == type) 107 if (record->type == type)
109 return record; 108 return record;
110 if (record->type > 2) 109 if (record->type > 2)