Sync the device address table's shadows once per guest submission that binds the table, and take out the port of pull request #353.

The author of pull request #353 closed it on 2026-11-11 with the words that they give no permission to use its code. Its port (3134f524) goes: the mark of a pipeline that declares the table (VkPipeInfo.gpa, vk_pipeline.c pipe_add) and of the part that draws with it (vk_mem_gpa_use in vk_exec_draw.c bind_resources and mo_emit), the sync of the table's pools in every such part's prologue (vk_mem_gpa_sync), the submission with a callback in the replay's place as it stood (vk_sync_probe_submit), and --probe-vkmem's read through the table with its shader (test_gpa_read, runtime/shaders/gpa_read.spvasm).

In its place, after pull request #551, which was written without that code: a shadow pool goes into the table and onto a list of the table's shadows (vk_memory.c shadow_table_add); the record of a binding of the table notes the guest submission (vk_exec_draw.c ssbo_info, vk_mem_shadow_table_read); and vk_mem_shadow_sync brings the CPU's writes into every shadow on the list once for that guest submission, before the part is submitted. Once is enough, because the copy-ins read the guest submission's write-watch snapshots, which a second sync would find unchanged. Measured by #551's author on Linux (RTX 4090, Kokiri Forest): 113 and 76 frames over 25 ms in 75 s against 217 and 429 with the port, 43 against 41 fps, the picture complete in both.

What this line had built on the port stays, on the new sync: the test that passes over a pool with nothing written (shadow_pool_clean), the upload of the same pages into a table pool's VRAM mirror, the write-back of stores through the table (NX2NAVI_SHADOW_TABLE_STORES, table_wb_cb, now asked once per guest submission that binds the table) and the counts of NX2NAVI_SHADOW_CHECK. The probes that record their own commands (the lost-device probe, --probe-vkmem's table parts) call vk_sync_submit_own, written anew. vk_mem_shadow_storage stays: one line that returns the mode. Finalized pools in the table were already kept for vk_mem_shutdown here.

--probe-vkmem has a check of its own for the sync (test_table_sync): the shadow of a pool that is never bound keeps its old word through a CPU write and a guest submission that does not bind the table, and holds the new one after a guest submission that does. It passes with NX2NAVI_SHADOW=1 on Windows (RTX 5090), as do the store check with NX2NAVI_SHADOW_TABLE_STORES=1 and the probe without shadow storage; --probe-devlost ends as before. Not run: the game, with or without shadow storage, and anything on Linux.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-11 06:05:07 -05:00
parent 31e0a02901
commit 295922c224
10 changed files with 218 additions and 375 deletions

View File

@@ -15,7 +15,6 @@
#include "vk.h"
#include "vk_exec.h"
#include "vk_gpa.h"
#include "gpa_read_spv.h"
#include "platform/platform.h"
static uint64_t g_rng = 0x9E3779B97F4A7C15ull;
@@ -372,7 +371,7 @@ static int test_import(pool_t *pool) {
// second sync in the same epoch would not see the CPU writes made since the first), then the part's CPU inputs
// go in before its GPU work.
vk_texture_new_submit();
vk_mem_shadow_sync();
vk_mem_shadow_sync(0);
copy_wait(sb, 0, sp.buffer, sp.offset, 0x20000);
vk_gpu_written(a + 0x1000, 0x20000);
GCHECK(!memcmp(m + 0x1000, s, 0x20000), "GPU copy into the imported heap range differs from the source");
@@ -391,7 +390,7 @@ static int test_import(pool_t *pool) {
// add to and the CPU resets (the game's lighting accumulators) depends on the reset reaching the GPU first.
vk_mem_gpu_pending(a + 0x30000, 0x4000);
vk_texture_new_submit();
vk_mem_shadow_sync();
vk_mem_shadow_sync(0);
copy_wait(sp.buffer, sp.offset, sb, 0, 0x4000);
vmaInvalidateAllocation(c->vma, sa, 0, VK_WHOLE_SIZE);
GCHECK(!memcmp(s, m + 0x30000, 0x4000), "GPU read of CPU-written heap differs");
@@ -439,145 +438,83 @@ static int test_remade(uint8_t *guest) {
return 0;
}
// test_gpa_read's shader (runtime/shaders/gpa_read.spvasm): one invocation follows the device address table from a
// guest address (the push constant) to the buffer that holds it and stores the 32-bit word found there in the result
// buffer. Storage bindings: 0 the table itself, 1 the result. The pool that holds the word is never a descriptor, a
// transfer source or a resolved span of the submission.
typedef struct {
VkPipeline pipe;
VkPipelineLayout layout;
VkDescriptorSet set;
uint64_t source;
int setup_mirror, mirrored;
} GpaRead;
static void gpa_read_record(VkCommandBuffer cb, void *arg) {
GpaRead *r = arg;
// Match vk_backend.c submit_now and vk_exec.c vk_exec_record, including epochs on descriptor reuse.
vk_texture_new_submit();
vk_mem_mirror_begin();
// The only explicit use of the source pool is making its mirror, before the first read: that submission takes its
// shadow touch and its first upload. After the CPU's write nothing resolves, uses or flushes the pool.
if (r->setup_mirror) r->mirrored = vk_mem_mirror_probe(r->source), r->setup_mirror = 0;
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE, r->pipe);
vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE, r->layout, 0, 1, &r->set, 0, NULL);
vkCmdPushConstants(cb, r->layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof r->source, &r->source);
vk_mem_gpa_use(); // the shader reads the table, in the second submission through the same descriptor set
vkCmdDispatch(cb, 1, 1, 1);
VkMemoryBarrier2 mb = {.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2,
.srcStageMask = VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT, .srcAccessMask = VK_ACCESS_2_SHADER_STORAGE_WRITE_BIT,
.dstStageMask = VK_PIPELINE_STAGE_2_HOST_BIT, .dstAccessMask = VK_ACCESS_2_HOST_READ_BIT};
VkDependencyInfo di = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .memoryBarrierCount = 1, .pMemoryBarriers = &mb};
vkCmdPipelineBarrier2(cb, &di);
vk_mem_mirror_end(cb, vk_sync_issued() + 1); // called under the submission lock, before the value is issued
}
static int test_gpa_read(uint8_t *guest) {
VkCtx *c = vk_ctx();
pool_t pool = {.magic = NVN_POOL_MAGIC, .flags = 0x24, .mem = guest + 0xA00000, .size = 0x10000,
.game_mem = guest + 0xA00000};
if (!vk_gpa_wanted()) { // no buffer device addresses, or NX2NAVI_GPA=0: shaders read zeros through it by design
printf("probe-vkmem: no device address table: the read through it is skipped\n");
return 0;
}
volatile uint32_t *source = (volatile uint32_t *)pool.mem;
*source = 0x13579BDFu; // before vk_pool_init: a shadow copy starts with the bytes an import would show
GCHECK(vk_gpa_on() && vk_pool_init(&pool), "the device address table and a CPU-visible pool for the read through it");
VkBuffer out = VK_NULL_HANDLE;
VmaAllocation alloc = NULL;
VkShaderModule module = VK_NULL_HANDLE;
VkDescriptorSetLayout dsl = VK_NULL_HANDLE;
VkDescriptorPool dp = VK_NULL_HANDLE;
// With shadow storage the pool also gets a VRAM mirror, which the table then names: the prologue of a part that
// uses the table keeps both up to date (vk_mem_gpa_sync). Without it the pool stays an import, guest memory itself:
// a mirror is not synced for the table's readers there (vk_exec_draw.c ssbo_info).
int want_mirror = vk_mirror_on && vk_mem_shadow_storage();
GpaRead r = {.source = (uintptr_t)pool.mem, .setup_mirror = want_mirror};
int bad = 1;
#define GPA_CHECK(cond, what) do { if (!(cond)) { printf("probe-vkmem: FAIL GPA " what "\n"); goto done; } } while (0)
VkBufferCreateInfo bi = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = 4, .usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT};
VmaAllocationCreateInfo ai = {.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT,
.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT};
VmaAllocationInfo info;
GPA_CHECK(vmaCreateBuffer(c->vma, &bi, &ai, &out, &alloc, &info) == VK_SUCCESS && info.pMappedData, "result buffer");
VkDescriptorSetLayoutBinding bindings[2] = {
{.binding = 0, .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, .descriptorCount = 1, .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT},
{.binding = 1, .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, .descriptorCount = 1, .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT}};
VkDescriptorSetLayoutCreateInfo dl = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, .bindingCount = 2,
.pBindings = bindings};
GPA_CHECK(vkCreateDescriptorSetLayout(c->device, &dl, NULL, &dsl) == VK_SUCCESS, "descriptor layout");
VkPushConstantRange push = {VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof r.source};
VkPipelineLayoutCreateInfo pl = {.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, .setLayoutCount = 1,
.pSetLayouts = &dsl, .pushConstantRangeCount = 1, .pPushConstantRanges = &push};
GPA_CHECK(vkCreatePipelineLayout(c->device, &pl, NULL, &r.layout) == VK_SUCCESS, "pipeline layout");
VkShaderModuleCreateInfo sm = {.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, .codeSize = sizeof gpa_read_spv,
.pCode = gpa_read_spv};
GPA_CHECK(vkCreateShaderModule(c->device, &sm, NULL, &module) == VK_SUCCESS, "shader module");
VkComputePipelineCreateInfo cp = {.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, .layout = r.layout,
.stage = {.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .stage = VK_SHADER_STAGE_COMPUTE_BIT,
.module = module, .pName = "main"}};
GPA_CHECK(vkCreateComputePipelines(c->device, VK_NULL_HANDLE, 1, &cp, NULL, &r.pipe) == VK_SUCCESS, "compute pipeline");
VkDescriptorPoolSize ps = {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 2};
VkDescriptorPoolCreateInfo pi = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, .maxSets = 1,
.poolSizeCount = 1, .pPoolSizes = &ps};
GPA_CHECK(vkCreateDescriptorPool(c->device, &pi, NULL, &dp) == VK_SUCCESS, "descriptor pool");
VkDescriptorSetAllocateInfo da = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, .descriptorPool = dp,
.descriptorSetCount = 1, .pSetLayouts = &dsl};
GPA_CHECK(vkAllocateDescriptorSets(c->device, &da, &r.set) == VK_SUCCESS, "descriptor set");
VkDescriptorBufferInfo buffers[2] = {vk_gpa_info(), {out, 0, 4}};
VkWriteDescriptorSet writes[2];
for (unsigned i = 0; i < 2; i++)
writes[i] = (VkWriteDescriptorSet){.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstSet = r.set, .dstBinding = i,
.descriptorCount = 1, .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.pBufferInfo = &buffers[i]};
vkUpdateDescriptorSets(c->device, 2, writes, 0, NULL);
static const uint32_t want[2] = {0x13579BDFu, 0x2468ACE0u};
for (unsigned pass = 0; pass < 2; pass++) {
if (pass) *source = want[pass]; // a plain CPU write: no resolve, use, flush or shadow sync of the pool
uint64_t fence = vk_sync_probe_submit(gpa_read_record, &r);
GPA_CHECK(vk_sync_fence_wait(fence, 5000000000ull), "shader fence timed out");
GPA_CHECK(!want_mirror || r.mirrored, "source mirror setup");
vmaInvalidateAllocation(c->vma, alloc, 0, VK_WHOLE_SIZE);
uint32_t got = *(volatile uint32_t *)info.pMappedData;
if (got != want[pass]) {
printf("probe-vkmem: FAIL GPA/BDA %s read: got %#010x, expected %#010x (mirror %d)\n",
pass ? "after CPU mutation" : "initial", got, want[pass], r.mirrored);
goto done;
}
}
g_gpu_checks += 2;
printf("probe-vkmem: GPA/BDA CPU write -> shader read: initial %#010x and mutation %#010x exact (mirror %d)\n",
want[0], want[1], r.mirrored);
bad = 0;
done:
vk_sync_finish(); // after a timeout too: the objects below must outlive the shader
if (dp) vkDestroyDescriptorPool(c->device, dp, NULL);
if (r.pipe) vkDestroyPipeline(c->device, r.pipe, NULL);
if (module) vkDestroyShaderModule(c->device, module, NULL);
if (r.layout) vkDestroyPipelineLayout(c->device, r.layout, NULL);
if (dsl) vkDestroyDescriptorSetLayout(c->device, dsl, NULL);
if (out) vmaDestroyBuffer(c->vma, out, alloc);
vk_pool_fini(&pool);
#undef GPA_CHECK
return bad;
}
// test_table_store: a submission whose one part uses the device address table and records nothing else, as a part of
// the replay with such a shader ends: its prologue brings every pool of the table up to date (vk_mem_gpa_sync).
// test_table_sync, test_table_store: a guest submission of one part that records nothing. bound: its record binds the
// device address table, as the replay of a draw whose shader reads through the table does (vk_exec_draw.c ssbo_info),
// so its prologue brings every shadow of the table up to date (vk_memory.c vk_mem_shadow_sync).
static void table_part_record(VkCommandBuffer cb, void *arg) {
(void)arg;
vk_texture_new_submit();
vk_mem_mirror_begin();
vk_mem_gpa_use();
if (arg) vk_mem_shadow_table_read();
vk_mem_mirror_end(cb, vk_sync_issued() + 1);
}
static int table_part(void) {
uint64_t fence = vk_sync_probe_submit(table_part_record, NULL);
static int table_part_of(int bound) {
static int binds;
uint64_t fence = vk_sync_submit_own(table_part_record, bound ? &binds : NULL);
return vk_sync_fence_wait(fence, 5000000000ull); // its completion callbacks have run when the wait returns
}
static int table_part(void) { return table_part_of(1); }
// Shadow storage: a pool that no part ever binds or resolves, as the pools a shader reaches only through a pointer it
// read from another buffer. Its shadow, which the device address table names, starts with the bytes guest memory held
// when the pool was made; a CPU write reaches it at the next guest submission whose record binds the table, and not
// at one that does not (nothing else brings it there, which is what left the foliage's instances stale). The shadow
// is read back with a GPU copy out of the buffer a lookup names, which does not touch the pool.
static int test_table_sync(uint8_t *guest) {
VkCtx *c = vk_ctx();
if (!vk_mem_shadow_storage() || !vk_gpa_wanted()) {
printf("probe-vkmem: the sync of the device address table's shadows: skipped (%s)\n",
!vk_mem_shadow_storage() ? "no shadow storage" : "no device address table");
return 0;
}
pool_t pool = {.magic = NVN_POOL_MAGIC, .flags = 0x24, .mem = guest + 0xA00000, .size = 0x10000,
.game_mem = guest + 0xA00000};
volatile uint32_t *w = (volatile uint32_t *)(pool.mem + 0x3000);
*w = 0x13579BDFu; // before vk_pool_init: a shadow starts with the bytes an import would show
GCHECK(vk_gpa_on() && vk_pool_init(&pool), "a CPU-visible pool in the device address table");
VkBuffer sb;
VmaAllocation sa;
volatile uint32_t *s = vk_staging(0x1000, &sb, &sa);
VkMemSpan sp;
int bad = 1;
uint32_t got;
#define TY_CHECK(cond, ...) do { if (!(cond)) { printf("probe-vkmem: FAIL table sync: "); printf(__VA_ARGS__); printf("\n"); goto done; } } while (0)
#define TY_SHADOW(into) \
do { \
TY_CHECK(vk_mem_lookup((uintptr_t)w, 4, &sp) && sp.buffer, "the pool has no buffer"); \
copy_wait(sp.buffer, sp.offset, sb, 0, 4); \
vmaInvalidateAllocation(c->vma, sa, 0, VK_WHOLE_SIZE); \
(into) = s[0]; \
} while (0)
TY_SHADOW(got);
TY_CHECK(got == 0x13579BDFu, "the new shadow holds %#010x, expected the pool's 0x13579bdf", got);
*w = 0x2468ACE0u; // a plain CPU write: nothing resolves, uses or flushes the pool
TY_CHECK(table_part_of(0), "fence timed out");
TY_SHADOW(got);
TY_CHECK(got == 0x13579BDFu, "a guest submission that does not bind the table left %#010x in the shadow, expected "
"the old 0x13579bdf (the check tests nothing if something else syncs the pool)", got);
TY_CHECK(table_part_of(1), "fence timed out");
TY_SHADOW(got);
TY_CHECK(got == 0x2468ACE0u, "after a guest submission that binds the table the shadow holds %#010x, expected the "
"CPU's 0x2468ace0", got);
*w = 0x0F1E2D3Cu; // and again, a guest submission later
TY_CHECK(table_part_of(1), "fence timed out");
TY_SHADOW(got);
TY_CHECK(got == 0x0F1E2D3Cu, "after the second such guest submission the shadow holds %#010x, expected 0x0f1e2d3c", got);
g_gpu_checks += 4;
printf("probe-vkmem: a pool that is never bound: its shadow in the device address table takes the CPU's writes at "
"each guest submission that binds the table, and at no other\n");
bad = 0;
done:
vk_sync_finish();
vmaDestroyBuffer(c->vma, sb, sa);
vk_pool_fini(&pool);
#undef TY_CHECK
#undef TY_SHADOW
return bad;
}
// Shadow storage: what a shader stores through the device address table lands in the pool's shadow copy and nothing
// notes it (vk_memory.c "Pages a shader stores into"). A GPU copy into the shadow buffer that is not noted stands in
// for the store. Word X of a page takes the stores, the CPU rewrites word Y of the same page before every part:
@@ -1143,7 +1080,7 @@ static int split_case(pool_t *pool, uint64_t off, uint64_t serial, int both, int
if (both) vk_texture_release(&d2);
if (recorded) {
SplitRec r = {6, NULL};
uint64_t fence = vk_sync_probe_submit(split_record, &r);
uint64_t fence = vk_sync_submit_own(split_record, &r);
GCHECK(vk_sync_fence_wait(fence, 5000000000ull), "the submission that looks the second descriptor's slot up timed out");
s2 = r.got;
} else {
@@ -1256,7 +1193,7 @@ static int test_gpu(void) {
vk_mem_stats(&st);
GCHECK(!st.imported_pools && !st.physical_pools && !st.virtual_pools, "pools left after fini");
g_gpu_checks += 2;
return test_gpa_read(h) || test_table_store(h) || test_remade(h);
return test_table_sync(h) || test_table_store(h) || test_remade(h);
}
int probe_vkmem(void) {