mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 16:48:37 +00:00
d3d12: slot-133 buffers, title windows and pool extents onto the page mirror; GPU writes into mirrored pages go back; hot/cold tracking; homes by budget (RT-PAGE-MIRROR)
Step C, with the previous WIP commit. - Resource::create_mirror_buffer: a slot-133 buffer over title memory the mirror holds in one chunk is bound to that chunk at heap_base + its title address: no import, no shadow, no per-submission walk or guard of its own. Its GPU VA is the title address; V#s and root views naming it keep that address. Title windows and stream-pool extents are not made for mirrored memory (resolve_title_memory answers the mirror first, since step B). - Write-back (DeviceState::page_mirror_write_back, from write_back_titles after the reaching submission retired): the 256-byte pieces whose home differs from the title's page are written back through kernel_writes, on pages nothing wrote since they were last armed (TitleMirrorTracking::unwritten); a page written on both sides keeps the title's bytes, which are uploaded over the home before the next submission. Ranges are noted (note_title_write, with its caps) where a V#, a writable geometry range, a title-memory resolution or a BVH range is answered by the mirror. - The mirror's tracking is HotColdTracking (src/title_mirror_hot_cold.h); it now answers unwritten() for cold pages from the version its guard was armed at (an additive change to that file and to the interface, default false). - Homes go to device-local memory while VK_EXT_memory_budget keeps 1.5 GiB (or an eighth of the budget) for the title's textures and render targets after a unit, to host memory otherwise, decided per 32 MiB unit and logged when it switches; the mirror's line and the mem-probe item count units of each kind. - The mirror's line adds hot and cold pages, write faults, hot and cold copies, and KiB uploaded a submission and a frame. Not moved: the XMemAlloc heap (HeapCreate places it). RtlCreateHeap over a reservation in the mirror's range faults under Wine (it writes the reserved base), so it stays outside the mirror; slot-133 buffers there keep their shadows. Tests: d3d12_page_mirror_test (Wine, iGPU): a slot-133 buffer over mirrored memory has the title's address as its GPU VA and is no shadow; a GPU write into its home goes back to the title's page (one 256-byte piece), a page written on both sides keeps the title's write. Linux ctest (llvmpipe) 122/122; xmem_test OK. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@ -430,6 +430,12 @@ bool page_mirror_gpu_read(ID3D12Device* device, std::uint64_t title_va, void* ou
|
||||
bool page_mirror_buffer_at(ID3D12Device* device, std::uint64_t va, std::uint64_t& size);
|
||||
// Names [va, va + bytes) to the page mirror as a reader would (page_mirror_resolve). For tests.
|
||||
bool page_mirror_name(ID3D12Device* device, std::uint64_t va, std::uint64_t bytes);
|
||||
// Writes `bytes` (at most 64 KiB) at heap_base + `title_va` with the GPU, as a shader's store into the mirror would,
|
||||
// and waits for it. For tests.
|
||||
bool page_mirror_gpu_write(ID3D12Device* device, std::uint64_t title_va, const void* data, std::size_t bytes);
|
||||
// The copy back of what the GPU wrote into the mirror's homes over [va, va + bytes) to the title's pages, as
|
||||
// write_back_titles makes it once a submission has retired. Returns the bytes written. For tests.
|
||||
std::uint64_t page_mirror_write_back(ID3D12Device* device, std::uint64_t va, std::uint64_t bytes);
|
||||
|
||||
// The blocks of device memory that textures share (src/image_memory.h): how many, their bytes, and the textures in
|
||||
// them. For tests.
|
||||
|
||||
@ -64,6 +64,7 @@
|
||||
#include "../src/title_mirror.h"
|
||||
#include "../src/title_mirror_space.h"
|
||||
#include "../src/title_mirror_tracking.h"
|
||||
#include "../src/title_mirror_hot_cold.h"
|
||||
#include "../src/guarded_copy.h"
|
||||
#include "../src/title_geometry_watch.h"
|
||||
#include "../src/title_texture_memory.h"
|
||||
@ -2670,6 +2671,8 @@ UnboundTsharpTracker& unbound_tsharp_tracker() {
|
||||
|
||||
// What a device's children need from it, kept apart from the COM class so children can hold a plain pointer.
|
||||
class Resource;
|
||||
leonida::VramBudget read_vram_budget(const VulkanDeviceInfo& info); // VK_EXT_memory_budget (defined below)
|
||||
|
||||
struct DeviceState {
|
||||
std::mutex mapped_texture_mutex;
|
||||
std::set<Resource*> mapped_textures;
|
||||
@ -4565,7 +4568,7 @@ struct DeviceState {
|
||||
std::vector<Change> taken;
|
||||
// Which mirrored pages the host wrote (src/title_mirror_tracking.h): a policy behind a small interface, the
|
||||
// plainest correct one (a guard on every page) until a measured one replaces it.
|
||||
std::unique_ptr<leonida::TitleMirrorTracking> tracking;
|
||||
std::unique_ptr<leonida::HotColdTracking> tracking;
|
||||
std::vector<std::uint64_t> written;
|
||||
std::unordered_set<std::uintptr_t> fed_now; // pages a change of this refresh mapped (filled already)
|
||||
// Ordinary title memory (not physical pages) in the mirror's range: base -> end of each such reservation, the
|
||||
@ -4580,6 +4583,10 @@ struct DeviceState {
|
||||
std::uint64_t refreshes = 0, fed = 0, dirty = 0, uploads = 0, batches = 0, binds = 0, arm_failures = 0;
|
||||
std::uint64_t resolved = 0, resolve_refused = 0, identity_mapped = 0, identity_released = 0, clipped = 0;
|
||||
std::uint64_t written_back = 0, buffers = 0;
|
||||
std::uint64_t units_local = 0, units_host = 0; // home units by memory, all so far
|
||||
bool last_local = true;
|
||||
std::uint64_t presents = 0; // present serial at the last log line
|
||||
std::uint64_t faults_seen = 0, hot_seen = 0, cold_seen = 0; // the tracking's counters at the last log line
|
||||
double ms = 0;
|
||||
std::chrono::steady_clock::time_point since = std::chrono::steady_clock::now();
|
||||
} page_mirror;
|
||||
@ -4646,14 +4653,46 @@ struct DeviceState {
|
||||
page_mirror.chunks.push_back(out);
|
||||
return true;
|
||||
};
|
||||
// Where a unit of homes goes, decided per unit as it is made: device-local memory while the device's budget
|
||||
// (VK_EXT_memory_budget) keeps room for the title's textures and render targets after it (kHomeHeadroom, or an
|
||||
// eighth of the budget if more), host memory otherwise; the GPU then reads those homes over the bus. A
|
||||
// device without the budget extension, or with every heap device-local (an integrated GPU), keeps device-local.
|
||||
hooks.make_unit = [this](VkDeviceMemory& out, std::uint8_t*& host) {
|
||||
VkMemoryRequirements requirements{};
|
||||
requirements.size = leonida::TitleMirror::kUnitBytes;
|
||||
requirements.alignment = leonida::TitleMirror::kPage;
|
||||
requirements.memoryTypeBits = sparse_buffer_memory_types;
|
||||
std::uint32_t host_types = 0; // the sparse buffers' memory types outside device-local heaps
|
||||
for (std::uint32_t t = 0; t < memory.memoryTypeCount; ++t)
|
||||
if ((sparse_buffer_memory_types >> t & 1) &&
|
||||
!(memory.memoryTypes[t].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT))
|
||||
host_types |= 1u << t;
|
||||
bool local = true;
|
||||
if (host_types) {
|
||||
const leonida::VramBudget budget = read_vram_budget(info);
|
||||
if (budget.available) {
|
||||
constexpr std::uint64_t kHomeHeadroom = std::uint64_t(1536) << 20;
|
||||
const std::uint64_t headroom = std::max(kHomeHeadroom, budget.local_budget / 8);
|
||||
local = budget.local_usage + leonida::TitleMirror::kUnitBytes + headroom <= budget.local_budget;
|
||||
}
|
||||
}
|
||||
if (!local) requirements.memoryTypeBits = host_types;
|
||||
leonida::memory_breakdown::Owner owner(leonida::memory_breakdown::Consumer::mirror);
|
||||
out = allocate(requirements, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
||||
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
||||
local ? VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT : VK_MEMORY_PROPERTY_HOST_CACHED_BIT);
|
||||
if (out) {
|
||||
++(local ? page_mirror.units_local : page_mirror.units_host);
|
||||
if (local != page_mirror.last_local) {
|
||||
page_mirror.last_local = local;
|
||||
std::fprintf(stderr, "[rt] d3d12: page mirror: homes go to %s from now on (%llu units in device-local "
|
||||
"memory, %llu in host memory so far)\n",
|
||||
local ? "device-local memory again: the budget has room"
|
||||
: "host memory (the GPU reads them over the bus): device-local memory is near "
|
||||
"its budget",
|
||||
static_cast<unsigned long long>(page_mirror.units_local),
|
||||
static_cast<unsigned long long>(page_mirror.units_host));
|
||||
}
|
||||
}
|
||||
void* mapped = nullptr;
|
||||
if (!out || context->vk.vkMapMemory(info.device, out, 0, VK_WHOLE_SIZE, 0, &mapped) != VK_SUCCESS) {
|
||||
if (out) context->vk.vkFreeMemory(info.device, out, nullptr);
|
||||
@ -4684,7 +4723,7 @@ struct DeviceState {
|
||||
return copy_guarded_host(to, from, bytes, false);
|
||||
};
|
||||
page_mirror.mirror = std::make_unique<leonida::TitleMirror>(std::move(hooks));
|
||||
page_mirror.tracking = std::make_unique<leonida::GuardTracking>();
|
||||
page_mirror.tracking = std::make_unique<leonida::HotColdTracking>();
|
||||
leonida::geometry_writes::subscribe(page_mirror.released);
|
||||
// A vertex, index, copy or indirect-argument address no buffer of ours holds is looked up in the mirror.
|
||||
context->set_mirror_buffer([this](std::uint64_t va, std::uint64_t& size, VkBuffer& buffer, VkDeviceSize& offset) {
|
||||
@ -4926,13 +4965,19 @@ struct DeviceState {
|
||||
m.ms += std::chrono::duration<double, std::milli>(now - start).count();
|
||||
if (now - m.since >= std::chrono::seconds(10)) {
|
||||
const auto c = m.mirror->counters();
|
||||
const auto policy = m.tracking->stats();
|
||||
const std::uint64_t presents =
|
||||
leonida::submit_phases::totals().present_serial.load(std::memory_order_relaxed);
|
||||
const std::uint64_t frames = presents - m.presents;
|
||||
std::fprintf(stderr,
|
||||
"[rt] d3d12: page mirror: %llu pages mirrored, %llu homes in %llu units (%llu MiB), %llu chunks "
|
||||
"(%llu not placed); in %.0f s: %llu refreshes, %llu changes fed, %llu pages written, %llu "
|
||||
"uploads, %llu binds in %llu batches, %llu arms refused, %.3f ms a refresh; changes outside "
|
||||
"the chunks %llu; readers: %llu ranges resolved into it, %llu refused, %llu ordinary pages "
|
||||
"mirrored and %llu released, %llu buffer bindings cut at a chunk's end; %llu slot-133 buffers "
|
||||
"made on it, %llu KiB of GPU writes copied back\n",
|
||||
"made on it, %llu KiB of GPU writes copied back; homes in device-local memory %llu units, in "
|
||||
"host memory %llu; tracking: %zu pages hot, %zu cold, %llu write faults, %llu hot and %llu "
|
||||
"cold copies; %.1f KiB uploaded a submission, %.1f KiB a frame\n",
|
||||
static_cast<unsigned long long>(c.pages), static_cast<unsigned long long>(c.homes),
|
||||
static_cast<unsigned long long>(c.units),
|
||||
static_cast<unsigned long long>(c.units * (leonida::TitleMirror::kUnitBytes >> 20)),
|
||||
@ -4946,7 +4991,17 @@ struct DeviceState {
|
||||
static_cast<unsigned long long>(m.resolved), static_cast<unsigned long long>(m.resolve_refused),
|
||||
static_cast<unsigned long long>(m.identity_mapped),
|
||||
static_cast<unsigned long long>(m.identity_released), static_cast<unsigned long long>(m.clipped),
|
||||
static_cast<unsigned long long>(m.buffers), static_cast<unsigned long long>(m.written_back >> 10));
|
||||
static_cast<unsigned long long>(m.buffers), static_cast<unsigned long long>(m.written_back >> 10),
|
||||
static_cast<unsigned long long>(m.units_local), static_cast<unsigned long long>(m.units_host),
|
||||
policy.hot, policy.cold, static_cast<unsigned long long>(policy.faults - m.faults_seen),
|
||||
static_cast<unsigned long long>(policy.hot_copies - m.hot_seen),
|
||||
static_cast<unsigned long long>(policy.cold_copies - m.cold_seen),
|
||||
m.refreshes ? double(m.uploads) * 64.0 / double(m.refreshes) : 0.0,
|
||||
frames ? double(m.uploads) * 64.0 / double(frames) : 0.0);
|
||||
m.faults_seen = policy.faults;
|
||||
m.hot_seen = policy.hot_copies;
|
||||
m.cold_seen = policy.cold_copies;
|
||||
m.presents = presents;
|
||||
m.refreshes = m.fed = m.dirty = m.uploads = m.batches = m.binds = m.arm_failures = 0;
|
||||
m.resolved = m.resolve_refused = m.identity_mapped = m.identity_released = m.clipped = 0;
|
||||
m.written_back = m.buffers = 0;
|
||||
@ -15715,6 +15770,8 @@ public:
|
||||
share.mirror_homes = c.homes;
|
||||
share.mirror_pages = c.pages;
|
||||
share.mirror_chunks = c.chunks;
|
||||
share.mirror_units_local = d.page_mirror.units_local;
|
||||
share.mirror_units_host = d.page_mirror.units_host;
|
||||
}
|
||||
std::lock_guard lock(d.descriptor_mutex); // the bindless set's lock (ExecuteCommandLists' flush)
|
||||
if (const auto* bindless = d.bindless.get()) share.bindless_table_bytes = bindless->table_buffer_bytes();
|
||||
@ -22738,6 +22795,64 @@ bool page_mirror_buffer_at(ID3D12Device* device, std::uint64_t va, std::uint64_t
|
||||
return const_cast<DeviceState&>(d->state()).page_mirror_buffer(va, size, buffer, offset);
|
||||
}
|
||||
|
||||
bool page_mirror_gpu_write(ID3D12Device* device, std::uint64_t title_va, const void* data, std::size_t bytes) {
|
||||
ID3D12Device* real = device ? rt::d3d12_xs::device_proxy_real_device(device) : nullptr;
|
||||
if (!real) real = device;
|
||||
const Device* d = real ? Device::from<Device>(real) : nullptr;
|
||||
if (!d || !data || !bytes || bytes > 65536 || (bytes & 3) || (title_va & 3)) return false;
|
||||
auto& state = const_cast<DeviceState&>(d->state());
|
||||
if (!state.page_mirror.mirror || !state.context) return false;
|
||||
state.refresh_page_mirror();
|
||||
state.flush_page_mirror();
|
||||
VkBuffer chunk = VK_NULL_HANDLE;
|
||||
VkDeviceSize offset = 0;
|
||||
std::uint64_t wait = 0;
|
||||
{
|
||||
std::lock_guard lock(state.page_mirror.mutex);
|
||||
if (!state.page_mirror.mirror->chunk_at(title_va, chunk, offset)) return false;
|
||||
wait = state.page_mirror.value;
|
||||
}
|
||||
auto& vk = state.context->vk;
|
||||
const VkDevice dev = state.info.device;
|
||||
VkCommandPool pool = VK_NULL_HANDLE;
|
||||
VkCommandBuffer commands = VK_NULL_HANDLE;
|
||||
VkCommandPoolCreateInfo pool_info{VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO};
|
||||
pool_info.queueFamilyIndex = state.info.queue_family;
|
||||
bool ok = vk.vkCreateCommandPool(dev, &pool_info, nullptr, &pool) == VK_SUCCESS;
|
||||
VkCommandBufferAllocateInfo allocate{VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO};
|
||||
allocate.commandPool = pool;
|
||||
allocate.commandBufferCount = 1;
|
||||
ok = ok && vk.vkAllocateCommandBuffers(dev, &allocate, &commands) == VK_SUCCESS;
|
||||
if (ok) {
|
||||
VkCommandBufferBeginInfo begin{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO};
|
||||
vk.vkBeginCommandBuffer(commands, &begin);
|
||||
vk.vkCmdUpdateBuffer(commands, chunk, offset, bytes, data);
|
||||
vk.vkEndCommandBuffer(commands);
|
||||
VkTimelineSemaphoreSubmitInfo values{VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO};
|
||||
values.waitSemaphoreValueCount = 1;
|
||||
values.pWaitSemaphoreValues = &wait;
|
||||
const VkPipelineStageFlags stage = VK_PIPELINE_STAGE_TRANSFER_BIT;
|
||||
VkSubmitInfo submit{VK_STRUCTURE_TYPE_SUBMIT_INFO, &values};
|
||||
submit.waitSemaphoreCount = 1;
|
||||
submit.pWaitSemaphores = &state.page_mirror.timeline;
|
||||
submit.pWaitDstStageMask = &stage;
|
||||
submit.commandBufferCount = 1;
|
||||
submit.pCommandBuffers = &commands;
|
||||
std::lock_guard lock(state.context->submit_mutex());
|
||||
ok = vk.vkQueueSubmit(state.info.queue, 1, &submit, VK_NULL_HANDLE) == VK_SUCCESS &&
|
||||
vk.vkQueueWaitIdle(state.info.queue) == VK_SUCCESS;
|
||||
}
|
||||
if (pool) vk.vkDestroyCommandPool(dev, pool, nullptr);
|
||||
return ok;
|
||||
}
|
||||
|
||||
std::uint64_t page_mirror_write_back(ID3D12Device* device, std::uint64_t va, std::uint64_t bytes) {
|
||||
ID3D12Device* real = device ? rt::d3d12_xs::device_proxy_real_device(device) : nullptr;
|
||||
if (!real) real = device;
|
||||
const Device* d = real ? Device::from<Device>(real) : nullptr;
|
||||
return d ? const_cast<DeviceState&>(d->state()).page_mirror_write_back(va, va + bytes) : 0;
|
||||
}
|
||||
|
||||
bool page_mirror_name(ID3D12Device* device, std::uint64_t va, std::uint64_t bytes) {
|
||||
ID3D12Device* real = device ? rt::d3d12_xs::device_proxy_real_device(device) : nullptr;
|
||||
if (!real) real = device;
|
||||
|
||||
@ -94,27 +94,8 @@ HANDLE title_heap() {
|
||||
// Set LEONIDA_TITLE_HEAP=0 to use the CRT heap.
|
||||
const char* v = std::getenv("LEONIDA_TITLE_HEAP");
|
||||
if (v && std::strcmp(v, "0") == 0) return HANDLE(nullptr);
|
||||
// In the page mirror's range (src/title_mirror_space.h), so a V# or pointer the title writes into a block
|
||||
// reaches the mirror: a heap over a reservation there (ntdll's RtlCreateHeap with a base), up to kTitleHeapBytes.
|
||||
// The host's own heap where that is refused.
|
||||
HANDLE h = nullptr;
|
||||
using RtlCreateHeapFn = PVOID(NTAPI*)(ULONG, PVOID, SIZE_T, SIZE_T, PVOID, PVOID);
|
||||
const auto create = reinterpret_cast<RtlCreateHeapFn>(
|
||||
reinterpret_cast<void*>(GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "RtlCreateHeap")));
|
||||
constexpr SIZE_T kTitleHeapBytes = SIZE_T(8) << 30;
|
||||
if (create)
|
||||
leonida::title_mirror_space::take(kTitleHeapBytes, 0x10000, true, [&](std::uint64_t at) {
|
||||
void* const base = VirtualAlloc(reinterpret_cast<void*>(static_cast<uintptr_t>(at)), kTitleHeapBytes,
|
||||
MEM_RESERVE, PAGE_READWRITE);
|
||||
if (!base) return false; // something of the host's there: the next address
|
||||
h = create(0x2 /* HEAP_GROWABLE */, base, kTitleHeapBytes, 0, nullptr, nullptr);
|
||||
if (!h) VirtualFree(base, 0, MEM_RELEASE); // refused with a base: not tried again
|
||||
return true;
|
||||
});
|
||||
const bool placed = h != nullptr;
|
||||
if (!h) h = HeapCreate(0, 0, 0);
|
||||
std::fprintf(stderr, "[rt] xmem: XMemAlloc blocks come from a heap of their own (%p, %s)\n", h,
|
||||
placed ? "in the page mirror's range" : "placed by the host");
|
||||
const HANDLE h = HeapCreate(0, 0, 0);
|
||||
std::fprintf(stderr, "[rt] xmem: XMemAlloc blocks come from a heap of their own (%p)\n", h);
|
||||
return h;
|
||||
}();
|
||||
return heap;
|
||||
|
||||
@ -116,6 +116,7 @@ struct DeviceShare {
|
||||
std::uint64_t bindless_table_bytes = 0; // allocated by BindlessSet through its own function table
|
||||
bool mirror = false; // the page mirror is on: its homes, the pages bound to them, its chunks
|
||||
std::uint64_t mirror_homes = 0, mirror_pages = 0, mirror_chunks = 0;
|
||||
std::uint64_t mirror_units_local = 0, mirror_units_host = 0; // its units of homes by memory
|
||||
};
|
||||
|
||||
inline double mib(std::uint64_t bytes) { return double(bytes) / 1048576.0; }
|
||||
@ -144,9 +145,12 @@ inline std::string format(const Tally& tally, const DeviceShare& device, std::ui
|
||||
out += item;
|
||||
}
|
||||
if (Consumer(c) == Consumer::mirror && device.mirror) {
|
||||
std::snprintf(item, sizeof item, " [homes bound %.0f MiB (%llu), pages mirrored %llu, chunks %llu]",
|
||||
std::snprintf(item, sizeof item,
|
||||
" [homes bound %.0f MiB (%llu), pages mirrored %llu, chunks %llu, units %llu device-local %llu "
|
||||
"host]",
|
||||
mib(device.mirror_homes * 0x10000), (unsigned long long)device.mirror_homes,
|
||||
(unsigned long long)device.mirror_pages, (unsigned long long)device.mirror_chunks);
|
||||
(unsigned long long)device.mirror_pages, (unsigned long long)device.mirror_chunks,
|
||||
(unsigned long long)device.mirror_units_local, (unsigned long long)device.mirror_units_host);
|
||||
out += item;
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,6 +33,7 @@ public:
|
||||
const auto it = guard_.cells.find(page);
|
||||
if (it == guard_.cells.end()) return;
|
||||
policy_.remove(std::uintptr_t(page), std::uintptr_t(page) + Policy::kPage);
|
||||
guard_.armed.erase(page);
|
||||
geometry_writes::release(std::uintptr_t(page));
|
||||
guard_.cells.erase(it);
|
||||
}
|
||||
@ -53,13 +54,30 @@ public:
|
||||
return now - std::exchange(refused_seen_, now);
|
||||
}
|
||||
const auto& stats() const { return policy_.stats(); }
|
||||
// A cold page whose guard no write has reached since it was armed (TitleMirrorTracking::unwritten). A hot page is
|
||||
// never: it is left writable.
|
||||
bool unwritten(std::uint64_t page) override {
|
||||
if (policy_.hot(std::uintptr_t(page))) return false;
|
||||
const auto cell = guard_.cells.find(page);
|
||||
const auto armed = guard_.armed.find(page);
|
||||
return cell != guard_.cells.end() && armed != guard_.armed.end() &&
|
||||
cell->second->version.load(std::memory_order_acquire) == armed->second;
|
||||
}
|
||||
|
||||
private:
|
||||
struct CellGuard {
|
||||
std::unordered_map<std::uint64_t, geometry_writes::Cell*> cells;
|
||||
std::unordered_map<std::uint64_t, std::uint64_t> armed; // page -> its cell's version when last armed
|
||||
bool arm(std::uintptr_t page) {
|
||||
const auto it = cells.find(page);
|
||||
return it != cells.end() && geometry_writes::arm(page, *it->second);
|
||||
if (it == cells.end()) return false;
|
||||
const std::uint64_t version = it->second->version.load(std::memory_order_acquire);
|
||||
if (!geometry_writes::arm(page, *it->second)) {
|
||||
armed.erase(page);
|
||||
return false;
|
||||
}
|
||||
armed[page] = version;
|
||||
return true;
|
||||
}
|
||||
// Writable again (a page turning hot): the cell reopens what it guarded.
|
||||
void disarm(std::uintptr_t page) {
|
||||
|
||||
@ -10,7 +10,9 @@
|
||||
// 4. a page of the reservation nothing is mapped at reads zero, with no fault;
|
||||
// 5. ordinary memory (committed XMemVirtualAlloc pages) is mirrored when a reader names it: a constant-buffer view
|
||||
// keeps the title's address as its base, the GPU reads the bytes and a later title write there, and a binding of
|
||||
// it is served by the mirror's chunk.
|
||||
// it is served by the mirror's chunk;
|
||||
// 6. a slot-133 buffer over mirrored memory is the mirror's chunk (no shadow); the GPU's writes into it go back to
|
||||
// the title's page, and a page written on both sides keeps the title's bytes.
|
||||
// Exit 77 when the device makes no page mirror (no sparse aliased strict buffers or capture-replay addresses: llvmpipe).
|
||||
// No shaders, no game data.
|
||||
#include "../shims/d3d12_com.h"
|
||||
@ -145,6 +147,38 @@ int test() {
|
||||
std::uint64_t pool_binding = 256;
|
||||
EXPECT(rt::d3d12::page_mirror_buffer_at(device.p, std::uintptr_t(dst), pool_binding));
|
||||
|
||||
// 6. A slot-133 buffer over mirrored memory is the mirror's chunk there: its GPU VA is the title's address, no
|
||||
// shadow. What the GPU writes into its home goes back to the title's page (the pieces that differ), and a page
|
||||
// the title wrote too keeps the title's bytes.
|
||||
auto* placed = static_cast<std::uint8_t*>(reserve(nullptr, kPage64k, MEM_RESERVE | MEM_COMMIT, 0, PAGE_READWRITE));
|
||||
EXPECT(placed);
|
||||
auto* placed_words = reinterpret_cast<volatile std::uint32_t*>(placed);
|
||||
for (unsigned i = 0; i < kPage64k / 4; i += 64) placed_words[i] = 0x71000000u + i;
|
||||
D3D12_RESOURCE_DESC desc{};
|
||||
desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
|
||||
desc.Width = kPage64k;
|
||||
desc.Height = desc.DepthOrArraySize = desc.MipLevels = 1;
|
||||
desc.SampleDesc.Count = 1;
|
||||
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
|
||||
desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
|
||||
Ref<ID3D12Resource> buffer;
|
||||
EXPECT(SUCCEEDED(rt::d3d12::create_buffer_at_host_address(device.p, placed, desc, 0xFFFFFFFFu, IID_PPV_ARGS(&buffer.p))));
|
||||
EXPECT(buffer.p->GetGPUVirtualAddress() == std::uintptr_t(placed) && !rt::d3d12::title_shadow_buffer(buffer.p));
|
||||
EXPECT(gpu_word(device.p, std::uintptr_t(placed) + 0x100) == 0x71000040u); // and the page is guarded now
|
||||
const std::uint32_t result[4] = {0xC0DE0001u, 0xC0DE0002u, 0xC0DE0003u, 0xC0DE0004u};
|
||||
EXPECT(rt::d3d12::page_mirror_gpu_write(device.p, std::uintptr_t(placed) + 0x200, result, sizeof(result)));
|
||||
EXPECT(placed_words[0x80] == 0x71000080u); // not yet: after the submission retires
|
||||
EXPECT(rt::d3d12::page_mirror_write_back(device.p, std::uintptr_t(placed), kPage64k) == 256);
|
||||
EXPECT(placed_words[0x80] == 0xC0DE0001u && placed_words[0x83] == 0xC0DE0004u && placed_words[0x40] == 0x71000040u);
|
||||
EXPECT(gpu_word(device.p, std::uintptr_t(placed) + 0x200) == 0xC0DE0001u); // the home agrees after the refill
|
||||
// Written on both sides: the title's write wins.
|
||||
const std::uint32_t other[4] = {0xBAD00001u, 0xBAD00002u, 0xBAD00003u, 0xBAD00004u};
|
||||
EXPECT(rt::d3d12::page_mirror_gpu_write(device.p, std::uintptr_t(placed) + 0x400, other, sizeof(other)));
|
||||
std::thread([&] { placed_words[0x100] = 0x7171u; }).join();
|
||||
EXPECT(rt::d3d12::page_mirror_write_back(device.p, std::uintptr_t(placed), kPage64k) == 0);
|
||||
EXPECT(placed_words[0x100] == 0x7171u);
|
||||
EXPECT(gpu_word(device.p, std::uintptr_t(placed) + 0x400) == 0x7171u);
|
||||
|
||||
const auto end = rt::d3d12::page_mirror_state(device.p);
|
||||
std::printf("page mirror: heap_base 0x%llx, %llu pages in %llu homes over %llu chunk(s); slot-31 copies, a title "
|
||||
"write through its own view and a remap reach heap_base + the GPU-only address; an unmapped page reads "
|
||||
|
||||
@ -100,8 +100,9 @@ int text() {
|
||||
EXPECT(contains(line, " + title windows and shadows 0 (0)"));
|
||||
EXPECT(contains(line, " + upload heaps 0 (0) + staging 0 (0) + texture blocks 0 (0) + sampled textures 0 (0)"));
|
||||
EXPECT(contains(line, " + render targets and depth 1180 (1) + page mirror 0 (0)"));
|
||||
EXPECT(contains(line, " chunks 1] + other 819 (1);"));
|
||||
EXPECT(contains(line, "page mirror 0 (0) [homes bound 2 MiB (32), pages mirrored 40, chunks 1]"));
|
||||
EXPECT(contains(line, " 0 host] + other 819 (1);"));
|
||||
EXPECT(contains(line, "page mirror 0 (0) [homes bound 2 MiB (32), pages mirrored 40, chunks 1, units 0 "
|
||||
"device-local 0 host]"));
|
||||
EXPECT(contains(line, "imported 206 MiB (1)"));
|
||||
EXPECT(contains(line, "bindless tables 2 MiB"));
|
||||
EXPECT(contains(line, "shader modules 12 MiB SPIR-V"));
|
||||
|
||||
Reference in New Issue
Block a user