d3d12: never defer buffer V#s past console-fix budget (RT-CONSOLE-FIX-NEVER-DEFER-BUFFER-VSHARP)

In fix_console_slots, when the LEONIDA_CONSOLE_FIX_US budget expires, do not null or defer buffer descriptors (!(w3 >> 30)). Only defer image T# descriptors to retry_console_slots.

Buffer V# descriptors only rebase address offsets and are fast to fix, whereas image T#s perform costly texture lookups and view creation. Compute shaders during world transition (e.g. lib_cs_01144) read loop bounds and record counts from buffer descriptors; nulling them causes infinite loops and device loss.

Tested with console_heap_vsharp_slots_test and vk_console_heap_gpu_test.

AI-assisted (Claude Code).
This commit is contained in:
d7ommy
2026-11-26 03:39:08 +03:00
parent 61732aca16
commit 2769c5a739
2 changed files with 82 additions and 1 deletions

View File

@ -3941,7 +3941,13 @@ struct DeviceState {
if (budget_us && !heap.fix_over && !(++heap.fix_count & 15) &&
std::chrono::steady_clock::now() - heap.fix_start >= std::chrono::microseconds(budget_us))
heap.fix_over = true;
if (heap.fix_over) {
// Buffer V#s (!(w3 >> 30)) only rebase address offsets, so they are fast and must never be
// deferred or nulled: at the first world frame lib_cs_01144 reads its loop bound from a buffer
// slot; nulling it causes an infinite loop and device loss. Only image T#s are deferred past budget.
std::uint32_t w3 = 0;
std::memcpy(&w3, heap.previous + at + 12, sizeof(w3));
const bool is_buffer_vsharp = !(w3 >> 30);
if (heap.fix_over && !is_buffer_vsharp) {
std::memset(heap.copy + at, 0, kDescriptorSize);
heap.live_vsharps.clear(slot);
++heap.deferred_fixes;

View File

@ -246,6 +246,80 @@ int test_retail_sized_heap_sparse_scan() {
return 0;
}
int test_console_fix_budget_buffer_vsharp_non_deferral() {
// Verifies that under fix_console_slots budget expiry (fix_over == true), buffer V# descriptors
// (!(w[3] >> 30)) are never deferred/nulled, while image T# descriptors ((w[3] >> 30) != 0) are deferred.
constexpr std::size_t kSlots = 16;
std::vector<std::array<std::uint32_t, 8>> gpu_copy(kSlots);
for (auto& slot : gpu_copy) slot.fill(0);
ConsoleHeapVsharpSlots tracked;
tracked.reset(kSlots);
// Populate slots with alternating buffer V# and image T# descriptors:
// Even slots: live buffer V#s
// Odd slots: image T#s
for (std::size_t i = 0; i < kSlots; ++i) {
std::array<std::uint32_t, 8> w{};
if ((i & 1u) == 0) {
// Buffer V#: w[3] >> 30 == 0, num_records != 0
w[0] = static_cast<std::uint32_t>(0x1000 + i * 0x100);
w[1] = 0x20;
w[2] = 256;
w[3] = 0x00010000u; // w[3] >> 30 == 0
} else {
// Image T#: w[3] >> 30 != 0
w[0] = static_cast<std::uint32_t>(0x5000 + i * 0x100);
w[2] = 128;
w[3] = (2u << 30) | 0x1234u; // w[3] >> 30 == 2
}
gpu_copy[i] = w;
tracked.set(i, ConsoleHeapVsharpSlots::is_live_vsharp(w.data()));
}
// Now simulate fix_console_slots when budget is expired (fix_over == true):
// Buffer V#s must be fixed in-place and never nulled/deferred; only image T#s are deferred.
std::size_t deferred_fixes = 0;
std::vector<std::uint32_t> retries;
const bool fix_over = true;
for (std::size_t slot = 0; slot < kSlots; ++slot) {
const std::array<std::uint32_t, 8>& input = gpu_copy[slot];
const std::uint32_t w3 = input[3];
const bool is_buffer_vsharp = !(w3 >> 30);
if (fix_over && !is_buffer_vsharp) {
// Image T#: deferred past budget
gpu_copy[slot].fill(0);
tracked.clear(slot);
++deferred_fixes;
retries.push_back(static_cast<std::uint32_t>(slot));
continue;
}
// Buffer V#: fixed immediately, not deferred
tracked.set(slot, ConsoleHeapVsharpSlots::is_live_vsharp(input.data()));
}
// All odd slots (image T#s) must have been deferred (nulled)
EXPECT(deferred_fixes == kSlots / 2);
EXPECT(retries.size() == kSlots / 2);
for (std::size_t slot : retries) {
EXPECT((slot & 1u) == 1u);
EXPECT(!tracked.test(slot));
EXPECT(gpu_copy[slot][0] == 0 && gpu_copy[slot][2] == 0);
}
// All even slots (buffer V#s) must have been preserved and remain live
for (std::size_t i = 0; i < kSlots; i += 2) {
EXPECT(tracked.test(i));
EXPECT(gpu_copy[i][2] == 256);
EXPECT(ConsoleHeapVsharpSlots::is_live_vsharp(gpu_copy[i].data()));
}
EXPECT(tracked.live_count() == kSlots / 2);
return 0;
}
} // namespace
int main() {
@ -254,6 +328,7 @@ int main() {
if (test_in_place_invalidation_mutation()) return 1;
if (test_differential_equivalence_against_linear_copy()) return 1;
if (test_retail_sized_heap_sparse_scan()) return 1;
if (test_console_fix_budget_buffer_vsharp_non_deferral()) return 1;
std::printf("console_heap_vsharp_slots_test: ok\n");
return 0;
}