Add quad_index, which draws an indexed quad list as two triangles per quad and closes an indexed line loop, from the game's own indices, and log the first such draw either way.

Vulkan has no quad or line-loop topology. Draws without indices were rewritten already; an indexed quad list went to the GPU with its indices under the triangle list topology, three at a time, joining corners of neighbouring quads, and an indexed loop lacked its closing line. Off by default and read at start; no log of Ocarina has shown such a draw so far, and the new line says when one comes. tests/index_rewrite_check.py checks the index lists.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 08:33:47 -05:00
parent 9d5f16bda4
commit 5faa1687f4
7 changed files with 199 additions and 3 deletions

View File

@@ -53,7 +53,7 @@ static const char *const known[] = {
"HDR_BLOWOUT", "HDR_CONTRAST", "HDR_EXPOSURE", "HDR_FLARE", "HDR_GAMMA", "HDR_GRADE", "HDR_HIGHLIGHT_SAT",
"HDR_HIGHLIGHTS", "HDR_HUE", "HDR_HUE_CLIP", "HDR_MATCH_SLOPE", "HDR_MIDGRAY", "HDR_SATURATION", "HDR_SHADOWS",
"HDR_TONEMAP", "HDR_TONE_SLOPE", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD",
"BIG_BUILD_FREE_MB", "BIG_BUILD_MAX_MB", "BUILD_FREE_MB", "BUILD_RESERVE_MB", "LIB_PREBUILD_BIG", "LIB_PREBUILD_BIG_BG", "COMPUTE_WARM_REUSE", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LIB_WARM", "LINE_WIDTH", "POOL_SHOWN_FIRST", "POOL_DATA_FIRST", "TEX_REUSE_NAMED", "TEX_OVERWRITTEN_USED", "PIPE_LINK_MS", "FRAME_HOLD_MS", "PACE_SMOOTH", "OQ_BATCH", "OQ_HOLD", "CLEAR_SMALL_CPU", "GATE_WAIT_MS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MEMORY", "MEM_CENSUS", "MEM_NEED_MB", "MENU_LANG", "MENU_SOUND_VOLUME", "MENU_TEST",
"BIG_BUILD_FREE_MB", "BIG_BUILD_MAX_MB", "BUILD_FREE_MB", "BUILD_RESERVE_MB", "LIB_PREBUILD_BIG", "LIB_PREBUILD_BIG_BG", "COMPUTE_WARM_REUSE", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LIB_WARM", "LINE_WIDTH", "POOL_SHOWN_FIRST", "POOL_DATA_FIRST", "TEX_REUSE_NAMED", "TEX_OVERWRITTEN_USED", "PIPE_LINK_MS", "FRAME_HOLD_MS", "PACE_SMOOTH", "OQ_BATCH", "OQ_HOLD", "QUAD_INDEX", "CLEAR_SMALL_CPU", "GATE_WAIT_MS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MEMORY", "MEM_CENSUS", "MEM_NEED_MB", "MENU_LANG", "MENU_SOUND_VOLUME", "MENU_TEST",
"MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_CLICK", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MICROPHONE", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MONITOR", "MOTION", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOUSE_TRACE", "MOVIE_HOST", "MOVIE_PACE", "MOVIE_CLOCK", "TEST_HITCH", "SPRINT_BUTTON", "SPRINT_MODE", "SPRINT_BUTTON_PATCH", "TEXT_SPEED", "TEXT_SPEED_PATCH",
"MOTION_GUARD_DEGREES", "MOTION_POLICY_TRACE", "SELECTIVE_MOTION", "SELECTIVE_MOTION_RAW", // motion_owner.c, vk_fg_pose.inc
"SKIN_MOTION", // vk_skin_motion.inc
@@ -150,7 +150,7 @@ static const struct { const char *key, *section; } sections[] = {
{"PIPE_FRAME_WAIT_MS", "performance"}, {"PIPE_SKIP_STEADY", "performance"}, {"DRAW_WRITERS_WAIT", "performance"},
{"CACHE_DIE", "performance"}, {"ONE_PER_CORE", "performance"},
{"PIPE_SEED_BOOT", "performance"}, {"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"LIB_WARM", "performance"}, {"PIPE_LINK_MS", "performance"}, {"PIPE_LINK_DRAW_MS", "performance"}, {"PIPE_AHEAD", "performance"}, {"LINE_WIDTH", "performance"}, {"POOL_SHOWN_FIRST", "performance"}, {"POOL_DATA_FIRST", "performance"}, {"TEX_REUSE_NAMED", "performance"}, {"TEX_OVERWRITTEN_USED", "performance"}, {"PIPE_FAST_WORKERS", "performance"}, {"GPL_FEEDBACK", "performance"}, {"GPL_MESH", "performance"}, {"FRAME_HOLD_MS", "performance"}, {"PACE_SMOOTH", "performance"}, {"OQ_BATCH", "performance"}, {"CLEAR_SMALL_CPU", "performance"}, {"PATCH_OCCLUSION_OFF", "performance"}, {"GATE_WAIT_MS", "performance"}, {"PIPE_PREBUILD", "performance"}, {"PIPE_SEED", "performance"},
{"OQ_HOLD", "performance"},
{"OQ_HOLD", "performance"}, {"QUAD_INDEX", "performance"},
{"COMPUTE_KEEP", "performance"},
{"PREWARM_XLATE", "performance"}, {"GPL", "performance"}, {"ASTC", "performance"}, {"MEMORY", "performance"},
{"TEX_ALIAS", "performance"}, {"DEPTH_INIT_INLINE", "performance"}, {"DEPTH_ALIAS_LOG", "diagnostics"}, {"TEX_DISUSE", "performance"}, {"TEX_DISUSE_EVICT", "performance"}, {"TEX_GEN_UNDO", "performance"}, {"TEX_REUSE", "performance"}, {"TEX_SPLIT_FILL", "performance"}, {"TEX_VRAM_PRESSURE", "performance"}, {"TEX_RELEASED_HOLD", "performance"}, {"TEX_PROC_COMMIT_MB", "performance"}, {"ASTC_BC", "performance"}, {"LOD_GUARD", "performance"}, {"FIX_C0", "performance"}, {"FIX_ZERO", "performance"}, {"WATER_PROJECTIVE", "performance"},
@@ -344,6 +344,8 @@ static const CfgMeta metas[] = {
// vk_exec_xfer.c oq_report_count: a samples-passed report missing draws the full query ring left uncounted
// repeats its record's last complete count where that is more than the part counted
{"OQ_HOLD", "0", "0|1", RESTART},
// vk_exec_draw.c draw_idx_rewritten: indexed quads drawn as two triangles each, indexed line loops closed
{"QUAD_INDEX", "0", "0|1", RESTART},
// the patch set occlusion_off (recomp/patches-v111.txt): the game makes no occlusion queries. The row is what
// gives the set its default (patches.c rt_patches_init).
{"PATCH_OCCLUSION_OFF", "0", "0|1", RESTART},

View File

@@ -33,6 +33,7 @@
#include "mesh_fb.h"
#include "vk_mesh_fb.h"
#include "vk_scratch_lru.h"
#include "vk_index_rewrite.h"
#include "platform/platform.h"
#include "stutter_map.h"
#include "vk_dlaa.h"
@@ -93,7 +94,8 @@ static int oneshot_budget_on(void) {
return on;
}
enum { W_PROGRAM, W_SHADER, W_PIPELINE, W_NO_VS, W_INDEX, W_UB, W_BIND, W_SSBO_ALIGN, W_SSBO_SLOT, W_RESTART, W_INDIRECT, W_MESH, W_COUNT };
enum { W_PROGRAM, W_SHADER, W_PIPELINE, W_NO_VS, W_INDEX, W_UB, W_BIND, W_SSBO_ALIGN, W_SSBO_SLOT, W_RESTART, W_INDIRECT, W_MESH, W_QUAD,
W_COUNT };
static void warn_once(int key, const char *fmt, ...) {
static volatile int32_t seen[W_COUNT];
@@ -6759,6 +6761,53 @@ static int restart_for(const VkExec *ex, int prim, int itype) {
return 0;
}
// NX2NAVI_QUAD_INDEX (default 0): an indexed quad list is drawn as two triangles per quad and an indexed line loop as a
// strip that comes back to its first vertex, from the game's indices (vk_index_rewrite.h), as the non-indexed ones are
// (op_draw). Off, they go to the GPU with the game's indices under the triangle list and line strip topologies, and the
// first such draw is logged either way. Read once.
static int quad_index_on(void) {
static int on = -1;
if (on < 0) on = cfg_bool("NX2NAVI_QUAD_INDEX");
return on;
}
// An indexed quad list or line loop, drawn with indices made from the game's (NX2NAVI_QUAD_INDEX). 0: not one of those,
// or left as it was (setting off, indices the CPU cannot read, a loop with primitive restart); the caller draws it.
static int draw_idx_rewritten(VkExec *ex, const p_draw_idx *d, const VkMemSpan *sp, int restart) {
static const uint32_t isize[3] = {1, 2, 4};
int quads = d->prim == NVN_DRAW_PRIMITIVE_QUADS, loop = d->prim == NVN_DRAW_PRIMITIVE_LINE_LOOP;
if (!quads && !loop) return 0;
if (!quad_index_on()) {
warn_once(W_QUAD, "an indexed %s (%d indices): drawn with its indices as a %s (quad_index = 1 rewrites them)",
quads ? "quad list" : "line loop", d->count, quads ? "triangle list" : "line strip");
return 0;
}
if (sp->alloc || (loop && restart)) {
warn_once(W_QUAD, "an indexed %s (%d indices, %s): drawn with its indices as they are", quads ? "quad list" : "line loop",
d->count, sp->alloc ? "indices in device-local memory" : "primitive restart");
return 0;
}
uint32_t n = (uint32_t)d->count, unit = isize[d->itype];
if (quads && n < 4) return 1; // no whole quad: nothing to draw
if (loop && n < 2) return 0;
uint32_t cap = quads ? 6 * (n / 4) : n + 1;
uint32_t *ix = malloc(sizeof *ix * (size_t)cap);
if (!ix) rt_fatal("vk: out of memory");
const uint8_t *src = (const uint8_t *)(uintptr_t)d->ibuf;
uint32_t m = quads ? idx_quads(src, unit, n, ix) : idx_loop(src, unit, n, ix);
vk_mem_count(sp->cls, VK_MUSE_CPU_READ, (uint64_t)n * unit);
vk_mem_count(VK_MCLS_SCRATCH, VK_MUSE_IB, sizeof *ix * (uint64_t)m);
bind_index(ex, ix, sizeof *ix * (uint64_t)m, VK_INDEX_TYPE_UINT32);
free(ix);
static int told;
if (!told++)
rt_log("vk: an indexed %s of %u indices drawn as %u indices of a %s (NX2NAVI_QUAD_INDEX)", quads ? "quad list" : "line loop",
n, m, quads ? "triangle list" : "line strip");
vkCmdDrawIndexed(ex->cb, m, d->instances > 0 ? (uint32_t)d->instances : 1, 0, d->base_vertex, (uint32_t)d->base_instance);
ex->recorded++;
return 1;
}
static void op_draw_idx(VkExec *ex, const p_draw_idx *d) {
ex->dc->c0_binst = (uint32_t)d->base_instance;
static const uint32_t isize[3] = {1, 2, 4};
@@ -6769,6 +6818,7 @@ static void op_draw_idx(VkExec *ex, const p_draw_idx *d) {
uint64_t bytes = (uint64_t)d->count * isize[d->itype];
VkMemSpan sp;
if (!vk_mem_resolve(d->ibuf, bytes, &sp)) return warn_once(W_INDEX, "index buffer %#llx is not backed: draws with it are skipped", (unsigned long long)d->ibuf);
if (draw_idx_rewritten(ex, d, &sp, restart)) return;
if (d->itype == NVN_INDEX_TYPE_UNSIGNED_BYTE) { // no uint8 index type here: widen (host-readable backing only)
if (sp.alloc) return warn_once(W_INDEX, "byte indices in device-local memory: draw skipped");
vk_mem_count(sp.cls, VK_MUSE_CPU_READ, bytes);

View File

@@ -0,0 +1,44 @@
// Index lists for the NVN primitives Vulkan has no topology for, made from an indexed draw's own indices
// (vk_exec_draw.c op_draw_idx, NX2NAVI_QUAD_INDEX). Header only, so that tests/index_rewrite_check.c checks it without a
// device.
//
// The replay draws a quad list as a triangle list and a line loop as a line strip (vk_pipeline.c vk_pipeline_topology).
// For a draw without indices it makes the indices itself (op_draw); an indexed one went to the GPU with the game's
// indices as they are: a quad list read three indices at a time, which joins corners of neighbouring quads into
// triangles across the mesh, and a line loop without its closing line. These turn the game's indices into the list the
// Vulkan topology wants: two triangles (0, 1, 2) and (0, 2, 3) per quad, as the non-indexed rewrite makes them, and the
// loop's first index again at its end. Indices are read at their size (1, 2 or 4 bytes, little-endian) and written as
// 32-bit ones; a base vertex stays the draw's.
#pragma once
#include <stdint.h>
#include <string.h>
static inline uint32_t idx_read(const uint8_t *src, uint32_t unit, uint32_t i) {
if (unit == 1) return src[i];
if (unit == 2) {
uint16_t v;
memcpy(&v, src + 2 * (size_t)i, 2);
return v;
}
uint32_t v;
memcpy(&v, src + 4 * (size_t)i, 4);
return v;
}
// Indices of a quad list as triangles: out holds 6 * (count / 4); a trailing partial quad is left out, as NVN draws no
// incomplete primitive. Returns how many were written.
static inline uint32_t idx_quads(const uint8_t *src, uint32_t unit, uint32_t count, uint32_t *out) {
static const uint8_t k[6] = {0, 1, 2, 0, 2, 3};
uint32_t q = count / 4;
for (uint32_t i = 0; i < q; i++)
for (int j = 0; j < 6; j++) out[6 * i + (uint32_t)j] = idx_read(src, unit, 4 * i + k[j]);
return 6 * q;
}
// Indices of a line loop as a strip that comes back to its first vertex: out holds count + 1 (count >= 2). Returns how
// many were written.
static inline uint32_t idx_loop(const uint8_t *src, uint32_t unit, uint32_t count, uint32_t *out) {
for (uint32_t i = 0; i < count; i++) out[i] = idx_read(src, unit, i);
out[count] = out[0];
return count + 1;
}