diff --git a/docs/maxwell.md b/docs/maxwell.md index c3fa2b11..d9aaad67 100644 --- a/docs/maxwell.md +++ b/docs/maxwell.md @@ -108,6 +108,7 @@ Source shorthands: | 5.10 | **Transform feedback**, rasterizer discard. | `VK_EXT_transform_feedback`. | Ours: rasterizer discard without XFB = no draw (`nvn_api_cmd.c`). | OGD 0x0744 | | 5.11 | **Depth bounds, sample mask, conservative raster**. | Optional features. | Ours: ignored, logged (`nvn_ext.c`). | OGD 0x0fa4 | | 5.12 | **Blend factors on targets without alpha** (RGBX emulated as RGBA). | Destination alpha reads the stored value. | DST_ALPHA factors need replacing on no-alpha targets. | — | +| 5.13 | **Quads, quad strips, polygons and line loops** are primitive types of their own, indexed or not. | No such topologies: a quad list read as a triangle list takes three indices at a time and joins corners of neighbouring quads; a line loop drawn as a strip lacks its closing line. A quad strip covers what a triangle strip of the same vertices covers, a convex polygon what a fan covers. | Ours: quad strips as triangle strips, polygons as fans (`vk_pipeline.c` `vk_pipeline_topology`); non-indexed quads and loops get indices made by the replay (`vk_exec_draw.c` `op_draw`); indexed ones are drawn with the game's indices unless `NX2NAVI_QUAD_INDEX=1` rewrites them (`draw_idx_rewritten`, `vk_index_rewrite.h`). The first indexed one is logged either way; no log of Ocarina has shown one so far. | — | ## 6. Checks for our runtime diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 13c9ff57..0887f05d 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -147,6 +147,12 @@ # full count of the same object (or what was counted, if that is more). The log says how often it happens either way # ("samples-passed reports so far missed draws", at the 1st, 10th, 100th...). Read at start. # oq_hold = 0 +# Indexed quads and line loops, 0 or 1. Default: off. Vulkan has no quad or line-loop topology: draws without indices +# are rewritten already, and 1 does the same for indexed ones (two triangles per quad, a closing line for a loop), which +# otherwise are drawn with the game's indices as triangles and an open line, joining corners of neighbouring quads. +# The log names the first such draw either way ("an indexed quad list"); without that line the game has drawn none. +# Read at start. +# quad_index = 0 # EXPERIMENT, 0 or 1. Default: off. The game asks the video card how much of an object would show behind what is # already drawn (an occlusion query) and leaves hidden objects out. With 1 it asks nothing and draws them all: more # draws in a frame, and no queries to issue or results to wait for and read back. Whether that gains or loses frames diff --git a/runtime/cfg.c b/runtime/cfg.c index 6b0235c0..ce293662 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -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}, diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 74dd0bd0..18824ed7 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -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); diff --git a/runtime/vk_index_rewrite.h b/runtime/vk_index_rewrite.h new file mode 100644 index 00000000..af1f20fd --- /dev/null +++ b/runtime/vk_index_rewrite.h @@ -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 +#include + +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; +} diff --git a/tests/index_rewrite_check.c b/tests/index_rewrite_check.c new file mode 100644 index 00000000..fc17f741 --- /dev/null +++ b/tests/index_rewrite_check.c @@ -0,0 +1,52 @@ +// runtime/vk_index_rewrite.h: the index lists an indexed quad list or line loop is drawn with (vk_exec_draw.c +// op_draw_idx, NX2NAVI_QUAD_INDEX). Built and run by tests/index_rewrite_check.py; prints each failed check and exits 1 +// on any. +#include +#include + +#include "vk_index_rewrite.h" + +static int fails; +#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0) + +int main(void) { + uint32_t out[64]; + // ---- 16-bit quads: two triangles per quad, from the quad's own indices, the same split as the non-indexed rewrite + uint16_t q16[8] = {10, 11, 12, 13, 40, 41, 42, 43}; + memset(out, 0xff, sizeof out); + uint32_t n = idx_quads((const uint8_t *)q16, 2, 8, out); + static const uint32_t want16[12] = {10, 11, 12, 10, 12, 13, 40, 41, 42, 40, 42, 43}; + CHECK(n == 12 && !memcmp(out, want16, sizeof want16), "two 16-bit quads made %u indices, or the wrong ones", n); + CHECK(out[12] == 0xffffffffu, "the quads wrote past their 12 indices"); + // ---- a trailing partial quad is not drawn + n = idx_quads((const uint8_t *)q16, 2, 7, out); + CHECK(n == 6, "seven indices made %u, not one quad's 6", n); + n = idx_quads((const uint8_t *)q16, 2, 3, out); + CHECK(n == 0, "three indices made %u, not none", n); + // ---- byte and 32-bit indices are read at their own size + uint8_t q8[4] = {200, 3, 255, 7}; + n = idx_quads(q8, 1, 4, out); + static const uint32_t want8[6] = {200, 3, 255, 200, 255, 7}; + CHECK(n == 6 && !memcmp(out, want8, sizeof want8), "byte quad read wrong"); + uint32_t q32[4] = {70000, 70001, 0x01000002u, 70003}; + n = idx_quads((const uint8_t *)q32, 4, 4, out); + static const uint32_t want32[6] = {70000, 70001, 0x01000002u, 70000, 0x01000002u, 70003}; + CHECK(n == 6 && !memcmp(out, want32, sizeof want32), "32-bit quad read wrong"); + // ---- unaligned source (a byte offset into a pool): read without alignment assumptions + uint8_t raw[1 + 8]; + uint16_t v[4] = {5, 6, 7, 8}; + memcpy(raw + 1, v, 8); + n = idx_quads(raw + 1, 2, 4, out); + static const uint32_t wantu[6] = {5, 6, 7, 5, 7, 8}; + CHECK(n == 6 && !memcmp(out, wantu, sizeof wantu), "unaligned 16-bit quad read wrong"); + // ---- a line loop comes back to its first index + uint16_t l16[3] = {9, 4, 2}; + n = idx_loop((const uint8_t *)l16, 2, 3, out); + static const uint32_t wantl[4] = {9, 4, 2, 9}; + CHECK(n == 4 && !memcmp(out, wantl, sizeof wantl), "the line loop was not closed"); + uint8_t l8[2] = {1, 250}; + n = idx_loop(l8, 1, 2, out); + CHECK(n == 3 && out[0] == 1 && out[1] == 250 && out[2] == 1, "the two-index loop was not closed"); + if (!fails) printf("index_rewrite_check: all checks passed\n"); + return fails ? 1 : 0; +} diff --git a/tests/index_rewrite_check.py b/tests/index_rewrite_check.py new file mode 100644 index 00000000..05b1e2e8 --- /dev/null +++ b/tests/index_rewrite_check.py @@ -0,0 +1,41 @@ +"""Build and run tests/index_rewrite_check.c: runtime/vk_index_rewrite.h, the index lists an indexed quad list or line +loop is drawn with (vk_exec_draw.c op_draw_idx, NX2NAVI_QUAD_INDEX): two triangles per quad from the quad's own +indices, the loop's first index again at its end. + +No GPU, no game files, any Python. + +Usage: index_rewrite_check.py +""" +import os +import shutil +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +MSYS_BIN = Path(r'C:\msys64\ucrt64\bin') + + +def compiler(): + """$CC, else MSYS2 UCRT64's clang, else clang on PATH (as tests/diff/common.py picks it, without its Unicorn).""" + if os.environ.get('CC'): + return os.environ['CC'] + if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists(): + return str(MSYS_BIN / 'clang.exe') + return shutil.which('clang') or 'clang' + + +def main(): + work = ROOT / 'build' / 'test' / 'index_rewrite' + work.mkdir(parents=True, exist_ok=True) + exe = work / ('index_rewrite_check.exe' if os.name == 'nt' else 'index_rewrite_check') + env = dict(os.environ) + if os.name == 'nt' and MSYS_BIN.exists(): + env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '') + subprocess.check_call([compiler(), '-std=gnu11', '-O1', '-Wall', '-Werror', '-I', str(ROOT / 'runtime'), + str(ROOT / 'tests' / 'index_rewrite_check.c'), '-o', str(exe)], env=env) + return subprocess.run([str(exe)], env=env).returncode + + +if __name__ == '__main__': + sys.exit(main())