Mesh emulation: size the task records from the arena, and log what each submission asked for.

The task records were a fixed 4096 per submission (3 KiB each): a dense patch of grass that launches more task
workgroups in one submission lost the rest silently. Their count is now a push constant (NX2NAVI_MESH_EMU_TASKS,
default 16384) and the arena defaults to 256 MiB. The header counts what the stages asked for past the capacities too;
before the counters start over they go to a readback ring, and the perf line reports the most one submission used of
the vertex slots, indices and task records, and the mesh workgroups that did not fit. The first draws the emulation
skips are logged with the reason.

(cherry picked from commit 81cfd91bf468f13e4debf287d080f96637a4c382)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Tobim6
2026-11-12 16:22:05 +01:00
parent aec5b5a840
commit 1534dc570a
4 changed files with 73 additions and 0 deletions

View File

@@ -423,7 +423,11 @@
# mirror_hot = 0
# GPUs without mesh shaders (VK_EXT_mesh_shader: AMD before RDNA 2, NVIDIA before Turing): 1 runs the game's mesh
# and task shaders (grass and other foliage) as compute shaders and draws what they make; 0 skips those draws. The
# arena for their vertices and triangles is mesh_emu_mb MiB of video memory, with mesh_emu_tasks task records (3 KiB
# each) per submission; with trace = perf the log says how much a submission used. Read at start.
# mesh_emulation = 0
# mesh_emu_mb = 256
# mesh_emu_tasks = 16384
# Scaling to the window when it is larger than the game resolution: fsr (AMD FSR 1) or bilinear, and FSR's sharpening
# from 0 (off) to 100. Changes while the game runs take effect at once.
# upscaler = fsr

View File

@@ -313,6 +313,12 @@ static void task_epilogue(Em *e, const MeshEmuInfo *info, uint32_t payload, uint
uint32_t t = add(e, wx, add(e, mul(e, wy, nx), mul(e, wz, nxy)));
uint32_t base = words_base(e), drw = pc_u32(e, 1);
uint32_t tb = word_load(e, base, add(e, drw, cu(e, 8)));
uint32_t trecs = pc_u32(e, 6), slot = add(e, tb, t), m1;
uint32_t mz;
if_begin(e, E2(e, OpIEqual, m->t_bool, lid, cu(e, 0)), &mz);
atomic(e, OpAtomicUMax, base, cu(e, 2), add(e, slot, cu(e, 1))); // records asked for, past the capacity too
if_end(e, mz);
if_begin(e, ult(e, slot, trecs), &m1);
{
uint32_t recw = add(e, cu(e, MESH_EMU_TOFF / 4), mul(e, slot, cu(e, MESH_EMU_TWORDS)));
uint32_t v = ld(e, m->t_u32, E2(e, OpAccessChain, p_wg_u32, payload, k));
@@ -331,6 +337,7 @@ static void task_epilogue(Em *e, const MeshEmuInfo *info, uint32_t payload, uint
{
uint32_t total = mul(e, nxy, nz);
// task records past the area are not written: their mesh groups are not dispatched
uint32_t room = E3(e, OpSelect, m->t_u32, ult(e, tb, trecs), E2(e, 130 /* OpISub */, m->t_u32, trecs, tb), cu(e, 0));
uint32_t ty = E3(e, OpSelect, m->t_u32, ult(e, total, room), total, room);
word_store(e, base, add(e, drw, cu(e, 5)), cu(e, info->max_mesh));
word_store(e, base, add(e, drw, cu(e, 6)), ty);
@@ -587,6 +594,7 @@ uint32_t *mesh_emu_compute(const uint32_t *w, size_t n, MeshEmuInfo *info, size_
uint32_t x = E2(&e, OpCompositeExtract, m->t_u32, wg, 0u), y = E2(&e, OpCompositeExtract, m->t_u32, wg, 1u);
uint32_t base = words_base(&e), drw = pc_u32(&e, 1);
uint32_t tb = word_load(&e, base, add(&e, drw, cu(&e, 8)));
uint32_t slot = add(&e, tb, y), ok = ult(&e, slot, pc_u32(&e, 6));
uint32_t slotc = E3(&e, OpSelect, m->t_u32, ok, slot, cu(&e, 0));
uint32_t recw = add(&e, cu(&e, MESH_EMU_TOFF / 4), mul(&e, slotc, cu(&e, MESH_EMU_TWORDS)));
uint32_t cnt = word_load(&e, base, recw);

View File

@@ -15,8 +15,13 @@
#define MESH_EMU_DWORDS 16u
#define MESH_EMU_DRECS 4096u
#define MESH_EMU_TOFF (MESH_EMU_DOFF + MESH_EMU_DRECS * MESH_EMU_DWORDS * 4u)
// task records: as many as the push constants say (the replay sizes them, NX2NAVI_MESH_EMU_TASKS); the index area
// follows them
// Push constants of every emulation stage (compute and the vertex shader), 32 bytes:
// u64 arena address; uint draw record (word index); uint index area (word index); uint index capacity;
// uint vertex area (16-byte slot index); uint vertex capacity (slots); uint task record capacity
// Header words 0-2 count what the stages asked for, beyond the capacities too (the replay logs the most per
// submission); word 3 counts mesh workgroups that did not fit.
#define MESH_EMU_PC_BYTES 32u
typedef struct MeshEmuInfo {

View File

@@ -5786,8 +5786,18 @@ static void op_draw_mesh_ind(VkExec *ex, const p_draw_mesh_ind *d) {
// and mesh stages as compute pipelines (vk_pipeline_mesh_emu) outside the pass: they write vertices and triangles into
// the arena (mesh_emu_spirv.h) and the draw record's DrawIndexedIndirect, which the generated vertex shader and the
// game's fragment shader then draw. The arena's counters are zeroed at the first emulated draw of each submission;
// the draw records go round a ring of MESH_EMU_DRECS. NX2NAVI_MESH_EMU_MB sizes the arena (default 256 MiB),
// NX2NAVI_MESH_EMU_TASKS its task records per submission (default 16384, 3 KiB each). Before the counters start over
// they are copied to a readback ring; the perf log reports the most a submission asked for against the capacities.
#define ME_RB 64
VkBuffer buf, rb;
VmaAllocation alloc, rb_alloc;
uint32_t *rb_map;
uint64_t addr, resets;
uint32_t iw, icap, vw, vcap, trecs, epoch, rec;
int failed;
uint64_t draws, skipped;
uint32_t max[4];
} ME;
return 1;
@@ -5796,6 +5806,13 @@ static void op_draw_mesh_ind(VkExec *ex, const p_draw_mesh_ind *d) {
static int me_arena(void) {
if (ME.buf) return 1;
if (ME.failed) return 0;
const char *v = cfg_get("NX2NAVI_MESH_EMU_MB"), *t = cfg_get("NX2NAVI_MESH_EMU_TASKS");
int mb = v && atoi(v) >= 32 ? atoi(v) : 256;
if (mb > 2048) mb = 2048;
ME.trecs = t && atoi(t) >= 256 ? (uint32_t)atoi(t) : 16384;
if (ME.trecs > 262144) ME.trecs = 262144;
uint64_t tbytes = (uint64_t)MESH_EMU_TOFF + (uint64_t)ME.trecs * MESH_EMU_TWORDS * 4;
if (tbytes + (32ull << 20) > ((uint64_t)mb << 20)) mb = (int)((tbytes >> 20) + 64); // room for vertices and indices
VkDeviceSize size = (VkDeviceSize)mb << 20;
VkBufferCreateInfo bi = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = size,
.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT |
@@ -5803,14 +5820,26 @@ static int me_arena(void) {
VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT};
VmaAllocationCreateInfo ai = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE};
return 0;
}
if (vmaCreateBuffer(vk_ctx()->vma, &bi, &ai, &ME.buf, &ME.alloc, NULL) != VK_SUCCESS) {
ME.buf = VK_NULL_HANDLE, ME.failed = 1;
rt_log("vk: mesh emulation: the %d MiB arena could not be created: mesh draws skipped", mb);
return 0;
}
VkBufferDeviceAddressInfo dai = {.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO, .buffer = ME.buf};
ME.addr = vkGetBufferDeviceAddress(vk_ctx()->device, &dai);
uint64_t ioff = (tbytes + 4095) & ~4095ull, ibytes = ((size - ioff) / 4) & ~4095ull;
uint64_t voff = ioff + ibytes;
ME.iw = (uint32_t)(ioff / 4), ME.icap = (uint32_t)(ibytes / 4), ME.vw = (uint32_t)(voff / 16), ME.vcap = (uint32_t)((size - voff) / 16);
rt_log("vk: mesh emulation arena: %d MiB, %u task records, %u indices, %u vertex slots (16 bytes)", mb, ME.trecs,
ME.icap, ME.vcap);
VkBufferCreateInfo rbi = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = ME_RB * 16,
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT};
VmaAllocationCreateInfo rai = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_HOST,
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT};
VmaAllocationInfo rinfo;
if (vmaCreateBuffer(vk_ctx()->vma, &rbi, &rai, &ME.rb, &ME.rb_alloc, &rinfo) == VK_SUCCESS) ME.rb_map = rinfo.pMappedData;
else ME.rb = VK_NULL_HANDLE;
return 1;
static void me_barrier(VkCommandBuffer cb, VkPipelineStageFlags2 ss, VkAccessFlags2 sa, VkPipelineStageFlags2 ds, VkAccessFlags2 da) {
VkDependencyInfo di = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .memoryBarrierCount = 1, .pMemoryBarriers = &mb};
@@ -5826,6 +5855,12 @@ static void me_barrier(VkCommandBuffer cb, VkPipelineStageFlags2 ss, VkAccessFla
MeshEmuInfo mi;
if (!info || !info->emu || !ex->dc->B.st_valid || !vk_pipeline_mesh_emu(ms, ts, info->layout, &mp, &tp, &mi) || !!ts != !!tp ||
(info->push && !info->emu_tpl_c) || (!info->push && !ex->dc->B.ds_bound[0])) {
static int logged;
if (logged++ < 8)
rt_log("vk: mesh emulation: a draw of program %llu skipped: %s", ex->prog ? (unsigned long long)ex->prog->serial : 0ull,
!info || !info->emu ? "its pipeline is not an emulated one"
: !mp ? "its stages are not emulated (see vk_pipeline: mesh emulation)"
: "its descriptors cannot be bound for compute");
ME.skipped++;
return 0;
VkPipelineLayout layout = info->layout;
@@ -5833,16 +5868,31 @@ static void me_barrier(VkCommandBuffer cb, VkPipelineStageFlags2 ss, VkAccessFla
VkDescriptorUpdateTemplate tpl_c = info->emu_tpl_c;
VkDescriptorSet set0 = ex->dc->B.ds_bound[0];
}
}
}
uint32_t ep = vk_texture_epoch();
if (ep != ME.epoch) { // the first emulated draw of this submission: the counters start over
ME.epoch = ep;
if (ME.rb && ME.rb_map) { // the last submission's counters to the readback ring; one 32 resets back is read
if (ME.resets >= 32) {
vmaInvalidateAllocation(vk_ctx()->vma, ME.rb_alloc, 0, VK_WHOLE_SIZE);
const uint32_t *h = ME.rb_map + 4 * ((ME.resets - 32) % ME_RB);
for (int k = 0; k < 4; k++)
if (h[k] > ME.max[k]) ME.max[k] = h[k];
VkBufferCopy rc = {0, 16 * (ME.resets % ME_RB), 16};
vkCmdCopyBuffer(cb, ME.buf, ME.rb, 1, &rc);
ME.resets++;
me_barrier(cb, VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT, VK_ACCESS_2_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT,
VK_ACCESS_2_TRANSFER_WRITE_BIT);
me_barrier(cb, VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT,
VK_ACCESS_2_TRANSFER_READ_BIT | VK_ACCESS_2_TRANSFER_WRITE_BIT);
uint32_t rec = ME.rec++ % MESH_EMU_DRECS;
VkDeviceSize ro = MESH_EMU_DOFF + (VkDeviceSize)rec * MESH_EMU_DWORDS * 4;
vkCmdFillBuffer(cb, ME.buf, ro, 4, 0);
vkCmdFillBuffer(cb, ME.buf, ro + 4, 4, 1);
uint32_t pc[8] = {(uint32_t)ME.addr, (uint32_t)(ME.addr >> 32), (uint32_t)(ro / 4), ME.iw, ME.icap, ME.vw, ME.vcap, ME.trecs};
}
}
cb = ex->cb;
vkCmdBindIndexBuffer(cb, ME.buf, (VkDeviceSize)ME.iw * 4, VK_INDEX_TYPE_UINT32);
vkCmdPushConstants(cb, layout, VK_SHADER_STAGE_COMPUTE_BIT | VK_SHADER_STAGE_VERTEX_BIT, 0, MESH_EMU_PC_BYTES, pc);
@@ -5857,7 +5907,13 @@ static void me_barrier(VkCommandBuffer cb, VkPipelineStageFlags2 ss, VkAccessFla
uint64_t f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED);
if ((rt_trace & RT_TRACE_PERF) && f / 600 != log_frame / 600) {
log_frame = f;
rt_log("perf: frame %llu: mesh emulation: %llu draws, %llu skipped (not emulated or not ready); most in one submission: "
"%u of %u vertex slots, %u of %u indices, %u of %u task records, %u mesh workgroups that did not fit",
(unsigned long long)f, (unsigned long long)ME.draws, (unsigned long long)ME.skipped, ME.max[0], ME.vcap,
ME.max[1], ME.icap, ME.max[2], ME.trecs, ME.max[3]);
ME.draws = ME.skipped = 0;
memset(ME.max, 0, sizeof ME.max);
}
return 1;
}