mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 12:28:38 +00:00
d3d12: host-RAM mirror of a console heap's copy for the release sweep
A console resource heap's translated copy lies in the GPU address arena when the import of host memory is not used, and the arena prefers DEVICE_LOCAL | HOST_VISIBLE memory (ReBAR): the filtered release sweep (LEONIDA_CONSOLE_RELEASE_FILTER=1) read words 0-2 of both V# halves of every live slot from it, uncached across PCIe, 0.9-1.0 s a rescan in game runs. ConsoleHeap::copy is now a leonida::ConsoleHeapCopy (src/console_heap_copy.h): the copy plus an optional host-RAM mirror of the heap's bytes. Every CPU write goes through write()/zero(), which write both; CPU reads use view(). The raw pointer is no longer reachable by accident, so a missed write site does not compile. No mirror for a copy in an import of host memory, for a sampler heap, with LEONIDA_HEAP_MIRROR=0, or once the LEONIDA_CONSOLE_HEAP_GPU pass writes the copy (dropped at attach). LEONIDA_HEAP_MIRROR_CHECK=1 compares the mirror with the copy at every release sweep and logs the first differing slots and a count per heap log line. The sweep over 536k live slots of an 18 MiB heap with the mirror: about 5 ms (console_heap_copy_test, host RAM). Tests: console_heap_copy (portable) and d3d12_console_heap_mirror (first refresh, title writes and zeroes, a filtered release sweep; again with LEONIDA_HEAP_MIRROR=0). AI-assisted (Claude Code, Claude Opus 5.5). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@ -3656,6 +3656,18 @@ if(TARGET Vulkan::Headers)
|
||||
target_compile_options(d3d12_unmapped_vsharp_test PRIVATE /W4 /permissive- /utf-8)
|
||||
add_test(NAME d3d12_unmapped_vsharp COMMAND d3d12_unmapped_vsharp_test)
|
||||
set_tests_properties(d3d12_unmapped_vsharp PROPERTIES SKIP_RETURN_CODE 77 ENVIRONMENT "LEONIDA_CONSOLE_FIX_US=0")
|
||||
# The host-RAM mirror of a console heap's GPU copy (src/console_heap_copy.h) agrees with the copy through the first
|
||||
# refresh, title writes and a filtered release sweep; run again without the mirror, where the copy must not change.
|
||||
add_executable(d3d12_console_heap_mirror_test test/d3d12_console_heap_mirror_test.cpp shims/d3d12_xs_device.cpp)
|
||||
target_link_libraries(d3d12_console_heap_mirror_test PRIVATE gta6rt_d3d12_com d3d12 dxgi)
|
||||
target_compile_definitions(d3d12_console_heap_mirror_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX LEONIDA_VULKAN_DEVICE)
|
||||
target_compile_options(d3d12_console_heap_mirror_test PRIVATE /W4 /permissive- /utf-8)
|
||||
add_test(NAME d3d12_console_heap_mirror COMMAND d3d12_console_heap_mirror_test)
|
||||
set_tests_properties(d3d12_console_heap_mirror PROPERTIES SKIP_RETURN_CODE 77 ENVIRONMENT
|
||||
"LEONIDA_CONSOLE_FIX_US=0;LEONIDA_CONSOLE_RELEASE_FILTER=1;LEONIDA_HEAP_MIRROR_CHECK=1")
|
||||
add_test(NAME d3d12_console_heap_mirror_off COMMAND d3d12_console_heap_mirror_test)
|
||||
set_tests_properties(d3d12_console_heap_mirror_off PROPERTIES SKIP_RETURN_CODE 77 ENVIRONMENT
|
||||
"LEONIDA_CONSOLE_FIX_US=0;LEONIDA_CONSOLE_RELEASE_FILTER=1;LEONIDA_HEAP_MIRROR=0")
|
||||
add_executable(d3d12_indirect_zero_test test/d3d12_indirect_zero_test.cpp)
|
||||
target_link_libraries(d3d12_indirect_zero_test PRIVATE gta6rt_d3d12_com d3d12 dxgi)
|
||||
target_compile_definitions(d3d12_indirect_zero_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
|
||||
@ -4590,6 +4602,12 @@ if(TARGET Vulkan::Headers)
|
||||
target_compile_options(console_heap_vsharp_slots_test PRIVATE /W4 /permissive- /utf-8)
|
||||
endif()
|
||||
add_test(NAME console_heap_vsharp_slots COMMAND console_heap_vsharp_slots_test)
|
||||
# Host-RAM mirror of a console resource heap's GPU copy (src/console_heap_copy.h).
|
||||
add_executable(console_heap_copy_test test/console_heap_copy_test.cpp)
|
||||
if(WIN32 AND MSVC)
|
||||
target_compile_options(console_heap_copy_test PRIVATE /W4 /permissive- /utf-8)
|
||||
endif()
|
||||
add_test(NAME console_heap_copy COMMAND console_heap_copy_test)
|
||||
# Queue construction cleans its Vulkan handles if holder registration runs out of host memory.
|
||||
add_executable(vk_host_texture_order_test test/vk_host_texture_order_test.cpp)
|
||||
target_link_libraries(vk_host_texture_order_test PRIVATE Vulkan::Headers ${CMAKE_DL_LIBS})
|
||||
|
||||
@ -423,6 +423,8 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w
|
||||
| `LEONIDA_CONSOLE_GPU_TSHARPS` | on | With `LEONIDA_CONSOLE_HEAP_GPU=1`, the T#s of changed slots are registered by the resolver thread and the refresh only decodes them. `0` registers them on the submitting thread, under the fix budget. |
|
||||
| `LEONIDA_CONSOLE_GPU_RETRY_MS` | `2000` | With `LEONIDA_CONSOLE_HEAP_GPU=1`, how often V# words that got no backing are asked for again, on a timer that bounds the cost per submission. |
|
||||
| `LEONIDA_CONSOLE_GPU_FILTER` | `LEONIDA_CONSOLE_RELEASE_FILTER`'s value | With `LEONIDA_CONSOLE_HEAP_GPU=1`, `1`: a released backing, an alias range that went or a word answered since asks for a rescan of only the slots it touches, not of every V# slot. Overflows and key resets still rescan everything. |
|
||||
| `LEONIDA_HEAP_MIRROR` | `1` | `0`: no host-RAM mirror of a console resource heap's copy in the arena (`src/console_heap_copy.h`). With the mirror, the CPU reads of the copy (the `LEONIDA_CONSOLE_RELEASE_FILTER` sweep) come from host RAM, not uncached device memory across PCIe. A copy in an import of host memory, and one the `LEONIDA_CONSOLE_HEAP_GPU` pass writes, never has one. |
|
||||
| `LEONIDA_HEAP_MIRROR_CHECK` | unset | `1`: at every release sweep the mirror is compared with the copy (a read of the whole copy); the first differing slots are logged, and the heap log line counts the checks and differing slots. |
|
||||
| `LEONIDA_CONSOLE_RELEASE_FILTER` | unset | `1`: when a backing is released, only the console-heap slots whose V#s point into it are fixed again (by actual Vulkan address), instead of every V# slot of the heap. The heap log line counts the sweeps, live slots, fixes and full fallbacks. |
|
||||
| `LEONIDA_SHADOW_GPU` | unset | `1`: aliased pages of the slot-133 shadows are copied on the GPU at the start of a submission, from an import of the mapped title section (`VK_EXT_external_memory_host`, per 2 MiB window, kept while xmem's map journal shows no change over it). Destinations outside the arena and pages that do not import are copied by the CPU as before. |
|
||||
| `LEONIDA_SHADOW_GPU_MB` | `1024` | With `LEONIDA_SHADOW_GPU=1`, the MiB of title memory imported for those copies at most. |
|
||||
|
||||
@ -387,6 +387,10 @@ std::uint64_t device_arena_size(ID3D12Device* device);
|
||||
// Copies `bytes` of the device's GPU address arena at console VA `va` (what shaders read there: a heap's GPU copy,
|
||||
// the null slots after it) into `out`. False without an arena or when the range is not inside it. For tests.
|
||||
bool read_arena(ID3D12Device* device, std::uint64_t va, void* out, std::size_t bytes);
|
||||
// The slots whose host-RAM mirror differs from the GPU copy of the console descriptor heap at `console_va`
|
||||
// (src/console_heap_copy.h), compared now: 0 when they agree, -1 when there is no such heap or it has no mirror.
|
||||
// For tests.
|
||||
long long console_heap_mirror_mismatches(ID3D12Device* device, std::uint64_t console_va);
|
||||
|
||||
// The blocks of device memory that textures share (src/image_memory.h): how many, their bytes, and the textures in
|
||||
// them. For tests.
|
||||
|
||||
@ -195,7 +195,7 @@ bool gpu_words_now(State& state, std::uint64_t at, std::array<std::uint32_t, 8>&
|
||||
for (const auto& heap : state.console_heaps) {
|
||||
const auto delta = at - heap->shader_va;
|
||||
if (heap->copy && delta <= heap->bytes && sizeof(out) <= heap->bytes - delta) {
|
||||
std::memcpy(out.data(), heap->copy + delta, sizeof(out));
|
||||
std::memcpy(out.data(), heap->copy.gpu() + delta, sizeof(out));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -249,7 +249,7 @@ void recheck(State& state) {
|
||||
if (!lock.owns_lock()) std::fputs("[rt] compute-input: after loss, console heaps locked\n", stderr);
|
||||
else
|
||||
for (const auto& heap : state.console_heaps)
|
||||
if (heap->copy) scan("console", heap->shader_va, heap->copy, heap->bytes);
|
||||
if (heap->copy) scan("console", heap->shader_va, heap->copy.gpu(), heap->bytes);
|
||||
}
|
||||
{
|
||||
std::unique_lock lock(state.visible_heap_mutex, std::try_to_lock);
|
||||
@ -294,7 +294,7 @@ Backing descriptor(State& state, std::uint64_t seq, std::size_t list, std::size_
|
||||
for (const auto& heap : state.console_heaps) {
|
||||
const auto delta = at - heap->shader_va;
|
||||
if (heap->copy && delta <= heap->bytes && sizeof(gpu) <= heap->bytes - delta) {
|
||||
std::memcpy(gpu.data(), heap->copy + delta, sizeof(gpu));
|
||||
std::memcpy(gpu.data(), heap->copy.view() + delta, sizeof(gpu));
|
||||
have_gpu = true;
|
||||
break;
|
||||
}
|
||||
|
||||
@ -65,6 +65,7 @@
|
||||
#include "../src/spirv_f16_halves.h"
|
||||
#include "../src/spirv_stores.h"
|
||||
#include "../src/title_va_alias.h"
|
||||
#include "../src/console_heap_copy.h"
|
||||
#include "../src/console_heap_vsharp_slots.h"
|
||||
#include "../src/title_window_plan.h"
|
||||
#include "../src/vulkan_present.h"
|
||||
@ -3798,7 +3799,10 @@ struct DeviceState {
|
||||
VkDeviceSize guard = 0; // null bytes after the GPU copy (heap_guard_bytes/heap_guard_for), its allocation
|
||||
bool resource_heap = false; // a CBV/SRV/UAV heap; a sampler heap's S#s are copied as written
|
||||
bool imported = false;
|
||||
std::uint8_t* copy = nullptr; // writable GPU mirror of the console heap
|
||||
// The translated copy shaders read, and its host-RAM mirror (src/console_heap_copy.h): written only through
|
||||
// copy.write()/copy.zero(), read on the CPU only through copy.view().
|
||||
leonida::ConsoleHeapCopy copy;
|
||||
std::uint64_t mirror_checks = 0, mirror_mismatches = 0; // LEONIDA_HEAP_MIRROR_CHECK, since the last log line
|
||||
// Keep large scratch allocations out of the low addresses the title can use for its heap.
|
||||
std::unique_ptr<std::uint8_t, FreeConsoleScratch> scratch;
|
||||
std::uint8_t* input{};
|
||||
@ -3977,7 +3981,7 @@ struct DeviceState {
|
||||
}
|
||||
if (empty) heap.empty_slots.insert(std::uint32_t(slot));
|
||||
else if (!heap.empty_slots.empty()) heap.empty_slots.erase(std::uint32_t(slot));
|
||||
std::memcpy(heap.copy + slot * kDescriptorSize, w, kDescriptorSize);
|
||||
heap.copy.write(slot * kDescriptorSize, w, kDescriptorSize);
|
||||
heap.live_vsharps.set(slot, leonida::ConsoleHeapVsharpSlots::is_live_vsharp(w));
|
||||
return settled;
|
||||
}
|
||||
@ -4025,7 +4029,7 @@ struct DeviceState {
|
||||
}();
|
||||
const std::size_t end = std::min<std::size_t>(offset + bytes, std::size_t(heap.bytes));
|
||||
if (!heap.resource_heap) {
|
||||
std::memcpy(heap.copy + offset, heap.input + offset, end - offset);
|
||||
heap.copy.write(offset, heap.input + offset, end - offset);
|
||||
return;
|
||||
}
|
||||
for (std::size_t at = offset & ~std::size_t(kDescriptorSize - 1); at + kDescriptorSize <= end;
|
||||
@ -4039,7 +4043,7 @@ struct DeviceState {
|
||||
std::chrono::steady_clock::now() - heap.fix_start >= std::chrono::microseconds(budget_us))
|
||||
heap.fix_over = true;
|
||||
if (heap.fix_over) {
|
||||
std::memset(heap.copy + at, 0, kDescriptorSize);
|
||||
heap.copy.zero(at, kDescriptorSize);
|
||||
heap.live_vsharps.clear(slot);
|
||||
++heap.deferred_fixes;
|
||||
if (!heap.retry[slot]) {
|
||||
@ -4132,6 +4136,59 @@ struct DeviceState {
|
||||
unbound_tsharp_tracker().note(heap.console_va, offset, words, why);
|
||||
}
|
||||
|
||||
// LEONIDA_HEAP_MIRROR=0: no host-RAM mirror of a console heap's copy in the arena (src/console_heap_copy.h); the
|
||||
// release sweep then reads the copy itself, as before the mirror.
|
||||
static bool heap_mirror_enabled() {
|
||||
static const bool on = [] {
|
||||
char value[4] = {};
|
||||
return !(GetEnvironmentVariableA("LEONIDA_HEAP_MIRROR", value, sizeof(value)) == 1 && value[0] == '0');
|
||||
}();
|
||||
return on;
|
||||
}
|
||||
// LEONIDA_HEAP_MIRROR_CHECK=1: the mirror compared with the copy at every release sweep (it reads the whole copy,
|
||||
// so it costs what the mirror saves), the first slots that differ logged.
|
||||
static bool heap_mirror_check_enabled() {
|
||||
static const bool on = [] {
|
||||
char value[4] = {};
|
||||
return GetEnvironmentVariableA("LEONIDA_HEAP_MIRROR_CHECK", value, sizeof(value)) == 1 && value[0] == '1';
|
||||
}();
|
||||
return on;
|
||||
}
|
||||
// The slots whose mirror differs from the copy (0: none, or no mirror); the first few logged, for the first
|
||||
// sweeps that find any.
|
||||
std::size_t check_console_heap_mirror(ConsoleHeap& heap) { // console_heap_mutex held
|
||||
if (!heap.copy.mirrored()) return 0;
|
||||
constexpr std::size_t kShown = 8;
|
||||
std::vector<std::size_t> slots;
|
||||
const std::size_t count = heap.copy.mismatches(kDescriptorSize, kShown, slots);
|
||||
++heap.mirror_checks;
|
||||
heap.mirror_mismatches += count;
|
||||
static std::atomic<unsigned> reports{0};
|
||||
if (count && reports.fetch_add(1, std::memory_order_relaxed) < 16) {
|
||||
std::fprintf(stderr, "[rt] d3d12: LEONIDA_HEAP_MIRROR_CHECK: console descriptor heap 0x%llx: %zu slots of "
|
||||
"the host mirror differ from the copy\n",
|
||||
static_cast<unsigned long long>(heap.console_va), count);
|
||||
for (const std::size_t slot : slots) {
|
||||
std::uint32_t mirror[8], copy[8];
|
||||
std::memcpy(mirror, heap.copy.view() + slot * kDescriptorSize, sizeof(mirror));
|
||||
std::memcpy(copy, heap.copy.gpu() + slot * kDescriptorSize, sizeof(copy));
|
||||
std::fprintf(stderr, "[rt] d3d12: slot %zu: mirror %08x %08x %08x %08x %08x %08x %08x %08x, copy %08x "
|
||||
"%08x %08x %08x %08x %08x %08x %08x\n",
|
||||
slot, mirror[0], mirror[1], mirror[2], mirror[3], mirror[4], mirror[5], mirror[6], mirror[7],
|
||||
copy[0], copy[1], copy[2], copy[3], copy[4], copy[5], copy[6], copy[7]);
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
// Tests: the mirror of the console heap at `console_va` compared with its copy now. -1: no such heap, or no mirror.
|
||||
long long console_heap_mirror_mismatches(std::uint64_t console_va) {
|
||||
std::lock_guard lock(console_heap_mutex);
|
||||
for (const auto& heap : console_heaps)
|
||||
if (heap->console_va == console_va)
|
||||
return heap->copy.mirrored() ? static_cast<long long>(check_console_heap_mirror(*heap)) : -1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
void refresh_console_heap(ConsoleHeap& heap) { // console_heap_mutex held
|
||||
if (heap.imported) return; // directly imported sampler heap
|
||||
if (heap.gpu >= 0) return refresh_console_heap_gpu(heap);
|
||||
@ -4142,7 +4199,7 @@ struct DeviceState {
|
||||
const auto t0 = clock::now();
|
||||
heap.copied += heap.pages->refresh();
|
||||
heap.pages->for_each_zeroed([&](std::size_t offset, std::size_t bytes) {
|
||||
std::memset(heap.copy + offset, 0, bytes);
|
||||
heap.copy.zero(offset, bytes);
|
||||
std::memset(heap.previous + offset, 0, bytes);
|
||||
const auto first = std::uint32_t(offset / kDescriptorSize);
|
||||
const auto end = std::uint32_t((offset + bytes) / kDescriptorSize);
|
||||
@ -4193,13 +4250,16 @@ struct DeviceState {
|
||||
++heap.rescans;
|
||||
const auto rescan_start = clock::now();
|
||||
// Even an unchanged title slot may name a buffer released since the last submission.
|
||||
// Walk the host-RAM V# bitset instead of reading every slot of `heap.copy` (which may be uncached WC VRAM).
|
||||
// Walk the host-RAM V# bitset instead of reading every slot of `heap.copy` (which may be uncached WC VRAM),
|
||||
// and read the copy's words from its host-RAM mirror.
|
||||
if (heap_mirror_check_enabled()) check_console_heap_mirror(heap);
|
||||
const std::uint8_t* const translated = heap.copy.view();
|
||||
heap.live_vsharps.for_each([&](std::size_t slot) {
|
||||
++heap.release_candidates;
|
||||
if (filtered) {
|
||||
const auto at = slot * kDescriptorSize;
|
||||
const auto* src = reinterpret_cast<const uint32_t*>(heap.previous + at);
|
||||
const auto* dst = reinterpret_cast<const uint32_t*>(heap.copy + at);
|
||||
const auto* dst = reinterpret_cast<const uint32_t*>(translated + at);
|
||||
if (!affected(src, dst) && !affected(src + 4, dst + 4)) return;
|
||||
}
|
||||
++heap.release_fixes;
|
||||
@ -4288,6 +4348,13 @@ struct DeviceState {
|
||||
(unsigned long long)heap->release_fixes, (unsigned long long)heap->release_rebases,
|
||||
(unsigned long long)heap->release_full);
|
||||
heap->release_sweeps = heap->release_candidates = heap->release_fixes = heap->release_rebases = heap->release_full = 0;
|
||||
if (heap_mirror_check_enabled() && heap->copy.mirrored())
|
||||
std::fprintf(stderr, "[rt] d3d12: LEONIDA_HEAP_MIRROR_CHECK: console descriptor heap 0x%llx: %llu "
|
||||
"checks, %llu slots differing in all\n",
|
||||
static_cast<unsigned long long>(heap->console_va),
|
||||
static_cast<unsigned long long>(heap->mirror_checks),
|
||||
static_cast<unsigned long long>(heap->mirror_mismatches));
|
||||
heap->mirror_checks = heap->mirror_mismatches = 0;
|
||||
if (heap->resource_heap) {
|
||||
std::uint64_t paths[kVsharpPathCount];
|
||||
for (unsigned i = 0; i < kVsharpPathCount; ++i)
|
||||
@ -4445,7 +4512,7 @@ struct DeviceState {
|
||||
heap->input = heap->scratch.get();
|
||||
heap->previous = heap->scratch.get() + bytes;
|
||||
heap->shader_va = offset;
|
||||
heap->copy = arena.mapped + offset;
|
||||
heap->copy.attach(arena.mapped + offset, std::size_t(bytes), heap_mirror_enabled());
|
||||
heap->guard = guard;
|
||||
heap->imported = false;
|
||||
in_arena = true;
|
||||
@ -4468,7 +4535,7 @@ struct DeviceState {
|
||||
if (heap->scratch) {
|
||||
auto* backing = heap->scratch.get();
|
||||
if (import_console_heap(*heap, reinterpret_cast<std::uintptr_t>(backing), bytes + guard)) {
|
||||
heap->copy = backing;
|
||||
heap->copy.attach(backing, std::size_t(bytes), false); // host RAM already: no mirror
|
||||
heap->guard = guard;
|
||||
}
|
||||
heap->input = backing + bytes + guard;
|
||||
@ -4505,9 +4572,10 @@ struct DeviceState {
|
||||
"a heap index past it reads the arena's next bytes; guard MiB",
|
||||
int(guard >> 20));
|
||||
heap->shader_va = offset;
|
||||
heap->copy = arena.mapped + offset;
|
||||
// A sampler heap's copy is read by no CPU sweep: no mirror.
|
||||
heap->copy.attach(arena.mapped + offset, std::size_t(bytes), resource_heap && heap_mirror_enabled());
|
||||
}
|
||||
std::memset(heap->copy, 0, std::size_t(bytes + heap->guard));
|
||||
heap->copy.zero(0, std::size_t(bytes + heap->guard));
|
||||
if (!heap->input) {
|
||||
heap->mirror.assign(std::size_t(bytes), 0);
|
||||
heap->made.assign(std::size_t(bytes), 0);
|
||||
@ -5049,6 +5117,7 @@ struct DeviceState {
|
||||
return;
|
||||
}
|
||||
heap.gpu = index;
|
||||
heap.copy.drop_mirror(); // the pass writes the copy from now on; the CPU neither writes nor reads it
|
||||
heap.vsharp_bits.assign((heap.bytes / kDescriptorSize + 63) / 64, 0);
|
||||
std::fprintf(stderr, "[rt] d3d12: LEONIDA_CONSOLE_HEAP_GPU: console descriptor heap 0x%llx (%llu slots) is "
|
||||
"translated by the pass (heap %d)\n",
|
||||
@ -20433,6 +20502,14 @@ bool read_arena(ID3D12Device* device, std::uint64_t va, void* out, std::size_t b
|
||||
return true;
|
||||
}
|
||||
|
||||
long long console_heap_mirror_mismatches(ID3D12Device* device, std::uint64_t console_va) {
|
||||
if (!device) return -1;
|
||||
ID3D12Device* real = rt::d3d12_xs::device_proxy_real_device(device);
|
||||
if (!real) real = device;
|
||||
Device* d = Device::from<Device>(real);
|
||||
return d ? d->state().console_heap_mirror_mismatches(console_va) : -1;
|
||||
}
|
||||
|
||||
namespace {
|
||||
const leonida::BindlessSet* bindless_of(ID3D12Device* device) {
|
||||
if (!device) return nullptr;
|
||||
|
||||
97
runtime/src/console_heap_copy.h
Normal file
97
runtime/src/console_heap_copy.h
Normal file
@ -0,0 +1,97 @@
|
||||
#pragma once
|
||||
// A console resource heap's translated copy (DeviceState::ConsoleHeap::copy): the bytes shaders read, and a host-RAM
|
||||
// mirror of them that the CPU reads instead.
|
||||
//
|
||||
// The copy lies in the GPU address arena when the import of host memory is not used (arena.mapped + offset), and the
|
||||
// arena prefers DEVICE_LOCAL | HOST_VISIBLE memory (ReBAR): a CPU read of it is an uncached read across PCIe. The
|
||||
// release sweep of refresh_console_heap with LEONIDA_CONSOLE_RELEASE_FILTER=1 reads words 0-2 of both V# halves of
|
||||
// every live V# slot (about 470k in a game run) to decide whether the slot needs fixing, and that read cost
|
||||
// 0.9-1.0 s a sweep. Every CPU write goes through write() and zero(), which write both the copy and the mirror, so
|
||||
// view() (the mirror, or the copy when there is none) holds what the GPU reads.
|
||||
//
|
||||
// No mirror when the copy is already host RAM (the import of the heap's own scratch), when LEONIDA_HEAP_MIRROR=0, or
|
||||
// once the copy is written by the GPU pass (LEONIDA_CONSOLE_HEAP_GPU: drop_mirror(); the CPU neither writes nor reads
|
||||
// the copy then). The mirror covers the heap's bytes only, not the null guard after them (written once, at creation).
|
||||
//
|
||||
// LEONIDA_HEAP_MIRROR_CHECK=1: refresh_console_heap compares the mirror with the copy at every release sweep
|
||||
// (mismatches()) and logs the first slots that differ.
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
namespace leonida {
|
||||
|
||||
class ConsoleHeapCopy {
|
||||
public:
|
||||
// `gpu`: the copy shaders read; `bytes`: the heap's bytes (the mirror's size, when `mirrored`).
|
||||
void attach(std::uint8_t* gpu, std::size_t bytes, bool mirrored) {
|
||||
gpu_ = gpu;
|
||||
bytes_ = bytes;
|
||||
mirror_.clear();
|
||||
mirror_.shrink_to_fit();
|
||||
if (gpu && mirrored) mirror_.assign(bytes, 0);
|
||||
}
|
||||
// The mirror goes; view() is the copy from now on.
|
||||
void drop_mirror() {
|
||||
mirror_.clear();
|
||||
mirror_.shrink_to_fit();
|
||||
}
|
||||
|
||||
explicit operator bool() const noexcept { return gpu_ != nullptr; }
|
||||
std::uint8_t* gpu() const noexcept { return gpu_; }
|
||||
bool mirrored() const noexcept { return !mirror_.empty(); }
|
||||
std::size_t bytes() const noexcept { return bytes_; }
|
||||
|
||||
// What the CPU reads: the mirror, or the copy itself.
|
||||
const std::uint8_t* view() const noexcept { return mirror_.empty() ? gpu_ : mirror_.data(); }
|
||||
|
||||
// [at, at + n) of the copy, and of the mirror where it reaches.
|
||||
void write(std::size_t at, const void* bytes, std::size_t n) {
|
||||
std::memcpy(gpu_ + at, bytes, n);
|
||||
if (const std::size_t m = mirror_span(at, n)) std::memcpy(mirror_.data() + at, bytes, m);
|
||||
}
|
||||
void zero(std::size_t at, std::size_t n) {
|
||||
std::memset(gpu_ + at, 0, n);
|
||||
if (const std::size_t m = mirror_span(at, n)) std::memset(mirror_.data() + at, 0, m);
|
||||
}
|
||||
|
||||
// LEONIDA_HEAP_MIRROR_CHECK: the slots of `slot_bytes` whose mirror bytes differ from the copy's, at most `limit`
|
||||
// of them in `out` (in ascending order); returns how many slots differ in all. 0 without a mirror.
|
||||
std::size_t mismatches(std::size_t slot_bytes, std::size_t limit, std::vector<std::size_t>& out) const {
|
||||
out.clear();
|
||||
if (mirror_.empty() || !slot_bytes) return 0;
|
||||
// Read the copy in large blocks: one pass over device memory, compared in host RAM.
|
||||
constexpr std::size_t kBlock = std::size_t(1) << 16;
|
||||
std::vector<std::uint8_t> block;
|
||||
std::size_t count = 0;
|
||||
const std::size_t step = std::max(slot_bytes, kBlock / slot_bytes * slot_bytes);
|
||||
for (std::size_t at = 0; at < bytes_; at += step) {
|
||||
const std::size_t n = std::min(step, bytes_ - at);
|
||||
block.resize(n);
|
||||
std::memcpy(block.data(), gpu_ + at, n);
|
||||
if (!std::memcmp(block.data(), mirror_.data() + at, n)) continue;
|
||||
for (std::size_t s = 0; s < n; s += slot_bytes) {
|
||||
const std::size_t k = std::min(slot_bytes, n - s);
|
||||
if (!std::memcmp(block.data() + s, mirror_.data() + at + s, k)) continue;
|
||||
if (out.size() < limit) out.push_back((at + s) / slot_bytes);
|
||||
++count;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
private:
|
||||
std::size_t mirror_span(std::size_t at, std::size_t n) const noexcept {
|
||||
if (mirror_.empty() || at >= mirror_.size()) return 0;
|
||||
return std::min(n, mirror_.size() - at);
|
||||
}
|
||||
|
||||
std::uint8_t* gpu_ = nullptr;
|
||||
std::size_t bytes_ = 0;
|
||||
std::vector<std::uint8_t> mirror_;
|
||||
};
|
||||
|
||||
} // namespace leonida
|
||||
168
runtime/test/console_heap_copy_test.cpp
Normal file
168
runtime/test/console_heap_copy_test.cpp
Normal file
@ -0,0 +1,168 @@
|
||||
// Portable CPU-only unit test for leonida::ConsoleHeapCopy (runtime/src/console_heap_copy.h): the host-RAM mirror of a
|
||||
// console heap's translated copy. A plain buffer stands in for the copy in the GPU address arena. Checks that every
|
||||
// write and zero reaches both, that the null guard past the heap stays out of the mirror, that mismatches() finds a
|
||||
// write that bypassed the helpers (the LEONIDA_HEAP_MIRROR_CHECK case), and that without a mirror view() is the copy.
|
||||
// Then times a filtered release sweep over a large heap reading the mirror (the work refresh_console_heap does per
|
||||
// rescan with LEONIDA_CONSOLE_RELEASE_FILTER=1), for reference; no game data.
|
||||
#include "../src/console_heap_copy.h"
|
||||
#include "../src/console_heap_vsharp_slots.h"
|
||||
#include "../src/descriptor_range_changes.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
#define EXPECT(condition) \
|
||||
do { \
|
||||
if (!(condition)) { \
|
||||
std::fprintf(stderr, "FAIL line %d: %s\n", __LINE__, #condition); \
|
||||
return 1; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
namespace {
|
||||
|
||||
using leonida::ConsoleHeapCopy;
|
||||
constexpr std::size_t kSlot = 32;
|
||||
|
||||
int test_writes_reach_both() {
|
||||
constexpr std::size_t slots = 256, bytes = slots * kSlot, guard = 4096;
|
||||
std::vector<std::uint8_t> gpu(bytes + guard, 0xCD);
|
||||
ConsoleHeapCopy copy;
|
||||
EXPECT(!copy);
|
||||
copy.attach(gpu.data(), bytes, true);
|
||||
EXPECT(copy && copy.mirrored() && copy.gpu() == gpu.data() && copy.bytes() == bytes);
|
||||
EXPECT(copy.view() != gpu.data());
|
||||
// Creation: the copy and its guard zeroed; the mirror covers the heap only.
|
||||
copy.zero(0, bytes + guard);
|
||||
for (const auto b : gpu) EXPECT(b == 0);
|
||||
std::vector<std::size_t> differ;
|
||||
EXPECT(copy.mismatches(kSlot, 8, differ) == 0 && differ.empty());
|
||||
|
||||
std::uint32_t w[8] = {0x1000, 0x20, 64, 0x3fffu, 0, 0, 0, 0};
|
||||
for (std::size_t s = 0; s < slots; s += 3) {
|
||||
w[0] = std::uint32_t(0x1000 + s * 0x40);
|
||||
copy.write(s * kSlot, w, sizeof(w));
|
||||
}
|
||||
copy.zero(9 * kSlot, kSlot);
|
||||
copy.zero(64 * kSlot, 32 * kSlot); // a zeroed range of slots
|
||||
// A sampler heap's whole range copied at once.
|
||||
std::vector<std::uint8_t> run(5 * kSlot + 7, 0x5A);
|
||||
copy.write(200 * kSlot + 3, run.data(), run.size());
|
||||
EXPECT(std::memcmp(copy.view(), gpu.data(), bytes) == 0);
|
||||
EXPECT(copy.mismatches(kSlot, 8, differ) == 0);
|
||||
// Past the heap: the copy's guard only, nothing out of the mirror's bounds.
|
||||
copy.zero(bytes - kSlot, kSlot + guard);
|
||||
std::uint32_t tail[8];
|
||||
std::memcpy(tail, copy.view() + bytes - kSlot, sizeof(tail));
|
||||
for (const auto t : tail) EXPECT(t == 0);
|
||||
EXPECT(copy.mismatches(kSlot, 8, differ) == 0);
|
||||
std::puts("writes and zeros reach the copy and the mirror; the guard stays out of the mirror");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_mismatch_found() {
|
||||
constexpr std::size_t slots = 8192, bytes = slots * kSlot; // 256 KiB: more than one compare block
|
||||
std::vector<std::uint8_t> gpu(bytes, 0);
|
||||
ConsoleHeapCopy copy;
|
||||
copy.attach(gpu.data(), bytes, true);
|
||||
// Writes that bypass the helpers (a missed write site, or the GPU writing the copy).
|
||||
gpu[5 * kSlot + 4] = 1;
|
||||
gpu[4000 * kSlot] = 2;
|
||||
gpu[4001 * kSlot + 31] = 3;
|
||||
gpu[8191 * kSlot + 16] = 4;
|
||||
std::vector<std::size_t> differ;
|
||||
EXPECT(copy.mismatches(kSlot, 3, differ) == 4);
|
||||
EXPECT(differ.size() == 3 && differ[0] == 5 && differ[1] == 4000 && differ[2] == 4001);
|
||||
EXPECT(copy.mismatches(kSlot, 8, differ) == 4 && differ.size() == 4 && differ[3] == 8191);
|
||||
std::puts("mismatches() finds writes that bypassed the mirror, in slot order");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_without_mirror() {
|
||||
constexpr std::size_t bytes = 64 * kSlot;
|
||||
std::vector<std::uint8_t> gpu(bytes, 0);
|
||||
ConsoleHeapCopy copy;
|
||||
copy.attach(gpu.data(), bytes, false); // the copy is host RAM already
|
||||
EXPECT(!copy.mirrored() && copy.view() == gpu.data());
|
||||
const std::uint32_t w[8] = {1, 2, 3, 4, 5, 6, 7, 8};
|
||||
copy.write(3 * kSlot, w, sizeof(w));
|
||||
EXPECT(!std::memcmp(gpu.data() + 3 * kSlot, w, sizeof(w)));
|
||||
std::vector<std::size_t> differ;
|
||||
EXPECT(copy.mismatches(kSlot, 8, differ) == 0);
|
||||
|
||||
copy.attach(gpu.data(), bytes, true);
|
||||
EXPECT(copy.mirrored());
|
||||
copy.drop_mirror(); // the GPU pass writes the copy from now on
|
||||
EXPECT(!copy.mirrored() && copy.view() == gpu.data());
|
||||
std::puts("without a mirror view() is the copy");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The filtered release sweep of refresh_console_heap over a heap of 576k slots (18 MiB) with 470k live V# slots, one
|
||||
// released range touching about 2%: the time per sweep with its words read from host RAM.
|
||||
int bench_filtered_sweep() {
|
||||
constexpr std::size_t slots = 576 * 1024, bytes = slots * kSlot;
|
||||
std::vector<std::uint8_t> previous(bytes, 0), gpu(bytes, 0);
|
||||
ConsoleHeapCopy copy;
|
||||
copy.attach(gpu.data(), bytes, true);
|
||||
leonida::ConsoleHeapVsharpSlots live;
|
||||
live.reset(slots);
|
||||
constexpr std::uint64_t kBuffers = 4096, kBufferBytes = 1 << 20, kArena = 1ull << 32;
|
||||
for (std::size_t s = 0; s < slots; ++s) {
|
||||
if (s % 11 == 10) continue; // T#s and empty slots
|
||||
const std::uint64_t base = 0x100000 + (s * 2654435761u % kBuffers) * kBufferBytes + (s % 64) * 256;
|
||||
std::uint32_t w[8] = {std::uint32_t(base), std::uint32_t(base >> 32) & 0xffffu, 256, 0x00027fac, 0, 0, 0, 0};
|
||||
std::memcpy(previous.data() + s * kSlot, w, sizeof(w));
|
||||
copy.write(s * kSlot, w, sizeof(w));
|
||||
live.set(s, leonida::ConsoleHeapVsharpSlots::is_live_vsharp(w));
|
||||
}
|
||||
leonida::DescriptorRanges backing, aliases;
|
||||
for (std::uint64_t b = 0; b < kBuffers; b += 50) // about 2% of the buffers released
|
||||
backing.ranges.push_back({kArena + 0x100000 + b * kBufferBytes, kArena + 0x100000 + (b + 1) * kBufferBytes - 1});
|
||||
backing.merge();
|
||||
const auto bda = [&](std::uint64_t base) { return kArena + base; };
|
||||
const auto affected = [&](const std::uint32_t* src, const std::uint32_t* dst) {
|
||||
if ((src[3] >> 30) || !src[2]) return false;
|
||||
const std::uint64_t base = std::uint64_t(src[0]) | (std::uint64_t(src[1] & 0xffff) << 32);
|
||||
const std::uint64_t fixed = std::uint64_t(dst[0]) | (std::uint64_t(dst[1] & 0xffff) << 32);
|
||||
const auto stride = (src[1] >> 16) & 0x3fff;
|
||||
const std::uint64_t n = std::uint64_t(src[2]) * (stride ? stride : 1);
|
||||
return aliases.overlaps(base, 1) || backing.overlaps(bda(base), n) || (dst[2] && backing.overlaps(bda(fixed), n));
|
||||
};
|
||||
constexpr int kRuns = 9;
|
||||
double best = 1e30, first = 0;
|
||||
std::size_t hits = 0;
|
||||
for (int run = 0; run < kRuns; ++run) {
|
||||
hits = 0;
|
||||
const auto start = std::chrono::steady_clock::now();
|
||||
const std::uint8_t* const translated = copy.view();
|
||||
live.for_each([&](std::size_t slot) {
|
||||
const auto at = slot * kSlot;
|
||||
const auto* src = reinterpret_cast<const std::uint32_t*>(previous.data() + at);
|
||||
const auto* dst = reinterpret_cast<const std::uint32_t*>(translated + at);
|
||||
if (affected(src, dst) || affected(src + 4, dst + 4)) ++hits;
|
||||
});
|
||||
const double took = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - start).count();
|
||||
if (!run) first = took;
|
||||
best = std::min(best, took);
|
||||
}
|
||||
EXPECT(hits > 0 && hits < live.live_count() / 10);
|
||||
std::printf("filtered sweep over %zu live slots of %zu MiB, %zu affected: %.2f ms (best of %d, first %.2f), words "
|
||||
"from the host mirror\n",
|
||||
live.live_count(), bytes >> 20, hits, best, kRuns, first);
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
if (test_writes_reach_both()) return 1;
|
||||
if (test_mismatch_found()) return 1;
|
||||
if (test_without_mirror()) return 1;
|
||||
if (bench_filtered_sweep()) return 1;
|
||||
std::puts("ok");
|
||||
return 0;
|
||||
}
|
||||
215
runtime/test/d3d12_console_heap_mirror_test.cpp
Normal file
215
runtime/test/d3d12_console_heap_mirror_test.cpp
Normal file
@ -0,0 +1,215 @@
|
||||
// The host-RAM mirror of a console descriptor heap's GPU copy (src/console_heap_copy.h) through a heap's life, with
|
||||
// LEONIDA_CONSOLE_RELEASE_FILTER=1 (the release sweep reads the mirror to pick the slots to fix): the first refresh,
|
||||
// slots the title rewrites and zeroes after it, and a released buffer whose V#s the sweep must find and null. After
|
||||
// each step the GPU copy holds what it should and the mirror agrees with it byte for byte. Run twice by ctest: with
|
||||
// the mirror, and with LEONIDA_HEAP_MIRROR=0 (no mirror; the sweep reads the copy, as before it), where the copy must
|
||||
// come out the same. Nothing dispatches through a descriptor. No game data.
|
||||
#include "vk_test_device.h"
|
||||
#include "../shims/d3d12_com.h"
|
||||
#include "../src/gfx10_descriptors.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
#define EXPECT(condition) do { \
|
||||
if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \
|
||||
} while (0)
|
||||
|
||||
namespace {
|
||||
|
||||
template<class T>
|
||||
struct Ref {
|
||||
T* p = nullptr;
|
||||
~Ref() { reset(); }
|
||||
void reset() {
|
||||
if (p) p->Release();
|
||||
p = nullptr;
|
||||
}
|
||||
T* operator->() const { return p; }
|
||||
};
|
||||
|
||||
constexpr UINT kDescriptor = 32;
|
||||
constexpr UINT kViewBytes = 256;
|
||||
constexpr UINT kBufferBytes = 64 * 1024;
|
||||
|
||||
D3D12_RESOURCE_DESC buffer_desc(UINT64 bytes) {
|
||||
D3D12_RESOURCE_DESC desc{};
|
||||
desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
|
||||
desc.Width = bytes;
|
||||
desc.Height = 1;
|
||||
desc.DepthOrArraySize = 1;
|
||||
desc.MipLevels = 1;
|
||||
desc.SampleDesc.Count = 1;
|
||||
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
|
||||
return desc;
|
||||
}
|
||||
|
||||
leonida::gfx10::BufferWords raw_vsharp(std::uint64_t address) {
|
||||
leonida::gfx10::Buffer b{};
|
||||
b.address = address;
|
||||
b.num_records = kViewBytes;
|
||||
b.format = leonida::gfx10::format_from_vulkan(VK_FORMAT_R32_UINT);
|
||||
b.oob_select = leonida::gfx10::OobSelect::raw;
|
||||
return leonida::gfx10::encode(b);
|
||||
}
|
||||
|
||||
std::uint64_t base_of(const std::uint32_t* w) { return std::uint64_t(w[0]) | (std::uint64_t(w[1] & 0xFFFFu) << 32); }
|
||||
bool zero_size(const std::uint32_t* w) { return base_of(w) == 0 && w[2] == 0; }
|
||||
|
||||
struct Heap {
|
||||
static constexpr UINT slots = 4096;
|
||||
static constexpr std::uint64_t bytes = std::uint64_t(slots) * kDescriptor;
|
||||
};
|
||||
|
||||
// What a slot of the copy is expected to hold: the buffer it names, or nothing.
|
||||
enum class Want { a, b, none, zero };
|
||||
|
||||
int check(ID3D12Device* device, std::uint64_t shader_va, std::uint64_t title, const Want* want, std::uint64_t a_va,
|
||||
std::uint64_t b_va, bool mirrored, const char* step) {
|
||||
static std::uint32_t copy[Heap::slots][kDescriptor / 4];
|
||||
EXPECT(rt::d3d12::read_arena(device, shader_va, copy, sizeof(copy)));
|
||||
for (UINT s = 0; s < Heap::slots; ++s) {
|
||||
const std::uint32_t* w = copy[s];
|
||||
bool ok = true;
|
||||
switch (want[s]) {
|
||||
case Want::a: ok = base_of(w) >= a_va && base_of(w) < a_va + kBufferBytes && w[2] == kViewBytes; break;
|
||||
case Want::b: ok = base_of(w) >= b_va && base_of(w) < b_va + kBufferBytes && w[2] == kViewBytes; break;
|
||||
case Want::none: ok = zero_size(w); break;
|
||||
case Want::zero: for (int i = 0; i < 8; ++i) ok = ok && !w[i]; break;
|
||||
}
|
||||
if (!ok) {
|
||||
std::fprintf(stderr, "%s: slot %u holds %08x %08x %08x %08x, wanted %d\n", step, s, w[0], w[1], w[2], w[3],
|
||||
int(want[s]));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
const long long differ = rt::d3d12::console_heap_mirror_mismatches(device, title);
|
||||
if (mirrored) EXPECT(differ == 0);
|
||||
else EXPECT(differ == -1);
|
||||
std::printf("%s: copy as expected, %s\n", step, mirrored ? "mirror agrees with it" : "no mirror");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int run(ID3D12Device* device, bool mirrored) {
|
||||
const D3D12_COMMAND_QUEUE_DESC queue_desc{};
|
||||
Ref<ID3D12CommandQueue> queue;
|
||||
Ref<ID3D12Fence> fence;
|
||||
EXPECT(SUCCEEDED(device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&queue.p))));
|
||||
EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p))));
|
||||
UINT64 fence_value = 0;
|
||||
Ref<ID3D12CommandAllocator> allocator;
|
||||
Ref<ID3D12GraphicsCommandList> list;
|
||||
EXPECT(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator.p))));
|
||||
EXPECT(SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr,
|
||||
IID_PPV_ARGS(&list.p))));
|
||||
EXPECT(SUCCEEDED(list->Close()));
|
||||
ID3D12CommandList* lists[] = {list.p};
|
||||
const auto submit = [&]() -> int { // every submission refreshes the console heaps first
|
||||
queue->ExecuteCommandLists(1, lists);
|
||||
EXPECT(SUCCEEDED(queue->Signal(fence.p, ++fence_value)));
|
||||
EXPECT(SUCCEEDED(fence->SetEventOnCompletion(fence_value, nullptr)));
|
||||
return 0;
|
||||
};
|
||||
|
||||
const D3D12_HEAP_PROPERTIES default_heap{D3D12_HEAP_TYPE_DEFAULT};
|
||||
const auto desc = buffer_desc(kBufferBytes);
|
||||
Ref<ID3D12Resource> a, b;
|
||||
EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &desc,
|
||||
D3D12_RESOURCE_STATE_COMMON, nullptr, IID_PPV_ARGS(&a.p))));
|
||||
EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &desc,
|
||||
D3D12_RESOURCE_STATE_COMMON, nullptr, IID_PPV_ARGS(&b.p))));
|
||||
const std::uint64_t a_va = a->GetGPUVirtualAddress(), b_va = b->GetGPUVirtualAddress();
|
||||
EXPECT(a_va && b_va);
|
||||
|
||||
auto* title = static_cast<std::uint8_t*>(VirtualAlloc(nullptr, Heap::bytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE));
|
||||
EXPECT(title);
|
||||
static Want want[Heap::slots];
|
||||
const auto put = [&](UINT slot, std::uint64_t va, Want w) {
|
||||
const auto words = raw_vsharp(va);
|
||||
std::memcpy(title + slot * kDescriptor, words.data(), sizeof(words));
|
||||
want[slot] = w;
|
||||
};
|
||||
for (UINT s = 0; s < Heap::slots; ++s) want[s] = Want::zero;
|
||||
// Before the heap exists: slots 0-1023 name A, 1024-2047 B, the rest empty but for one data slot.
|
||||
for (UINT s = 0; s < 1024; ++s) put(s, a_va + (s % 256) * kViewBytes, Want::a);
|
||||
for (UINT s = 1024; s < 2048; ++s) put(s, b_va + (s % 256) * kViewBytes, Want::b);
|
||||
const std::uint32_t data[8] = {0x12345000u, 0x00007777u, 0x00000400u, 0x40000000u, 1, 2, 3, 4};
|
||||
std::memcpy(title + 3000 * kDescriptor, data, sizeof(data));
|
||||
want[3000] = Want::none; // its descriptor half null in the copy (fix_console_slot)
|
||||
|
||||
const D3D12_DESCRIPTOR_HEAP_DESC heap_desc{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, Heap::slots,
|
||||
D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 0};
|
||||
Ref<ID3D12DescriptorHeap> heap;
|
||||
const auto title_va = reinterpret_cast<std::uint64_t>(title);
|
||||
EXPECT(SUCCEEDED(rt::d3d12::create_console_descriptor_heap(device, &heap_desc, title_va, Heap::bytes,
|
||||
IID_PPV_ARGS(&heap.p))));
|
||||
D3D12_DESCRIPTOR_HEAP_DESC got{};
|
||||
D3D12_GPU_VIRTUAL_ADDRESS shader_va = 0;
|
||||
EXPECT(rt::d3d12::descriptor_heap_info(heap.p, got, shader_va));
|
||||
if (check(device, shader_va, title_va, want, a_va, b_va, mirrored, "first refresh")) return 1;
|
||||
|
||||
// The title rewrites slots after the heap is made (the write watch finds them): a slot of B now names A, new V#s
|
||||
// past what it filled, and a page of A's slots zeroed (128 slots).
|
||||
put(1500, a_va + 0x400, Want::a);
|
||||
for (UINT s = 2048; s < 2112; ++s) put(s, b_va + (s % 256) * kViewBytes, Want::b);
|
||||
std::memset(title + 128 * kDescriptor, 0, 4096);
|
||||
for (UINT s = 128; s < 256; ++s) want[s] = Want::zero;
|
||||
if (submit()) return 1;
|
||||
if (check(device, shader_va, title_va, want, a_va, b_va, mirrored, "title writes")) return 1;
|
||||
|
||||
// A goes: the release sweep (filtered: only the V#s naming A) nulls the slots that name it, and only those. Its
|
||||
// memory is given back once its GPU work retires, so a few submissions may pass first.
|
||||
a.reset();
|
||||
for (UINT s = 0; s < Heap::slots; ++s)
|
||||
if (want[s] == Want::a) want[s] = Want::none;
|
||||
bool nulled = false;
|
||||
for (int pass = 0; pass < 32 && !nulled; ++pass) {
|
||||
if (submit()) return 1;
|
||||
std::uint32_t w[8];
|
||||
EXPECT(rt::d3d12::read_arena(device, shader_va + 1500 * kDescriptor, w, sizeof(w)));
|
||||
nulled = zero_size(w);
|
||||
}
|
||||
EXPECT(nulled);
|
||||
if (check(device, shader_va, title_va, want, a_va, b_va, mirrored, "A released")) return 1;
|
||||
|
||||
heap.reset();
|
||||
VirtualFree(title, 0, MEM_RELEASE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
char value[4] = {};
|
||||
const bool mirrored = !(GetEnvironmentVariableA("LEONIDA_HEAP_MIRROR", value, sizeof(value)) == 1 && value[0] == '0');
|
||||
vktest::Device d;
|
||||
const auto status = d.initialize();
|
||||
if (status == vktest::DeviceStatus::unavailable) {
|
||||
std::fprintf(stderr, "SKIP: no usable Vulkan 1.3 device\n");
|
||||
return 77;
|
||||
}
|
||||
EXPECT(status == vktest::DeviceStatus::ready);
|
||||
{
|
||||
rt::d3d12::VulkanDeviceInfo info{};
|
||||
info.get_instance_proc = d.instance_proc;
|
||||
info.instance = d.instance;
|
||||
info.physical = d.physical;
|
||||
info.device = d.device;
|
||||
info.queue = d.queue;
|
||||
info.queue_family = d.family;
|
||||
info.buffer_device_address = true;
|
||||
info.subgroup = d.subgroup;
|
||||
Ref<ID3D12Device> device;
|
||||
EXPECT(SUCCEEDED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&device.p))));
|
||||
EXPECT(rt::d3d12::device_arena_size(device.p) > 0);
|
||||
if (run(device.p, mirrored)) return 1;
|
||||
}
|
||||
if (d.validation_errors()) {
|
||||
std::fprintf(stderr, "%u validation errors\n", d.validation_errors());
|
||||
return 1;
|
||||
}
|
||||
std::puts("ok");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user