diff --git a/runtime/cfg.c b/runtime/cfg.c index 3f33579a..11fc432d 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -56,7 +56,7 @@ static const char *const known[] = { "REPLAY_CHECK", "RESOLVE_CHECK", "RESTART_WAIT", "ROMFS", "ROMFS_INDEX", "ROMFS_MMAP", "ROMFS_MODS", "ROMFS_PATCHES", "ROMFS_PROOF_DIR", "ROMFS_VERIFY", "RSTICK_DEADZONE", "RUMBLE", "RUMBLE_CURVE", "RUMBLE_STRENGTH", "RUMBLE_TRIGGERS", "SHADER_CACHE", "SHADER_MEMO", "SHADOW_COMPARE", "SHADOW_RANGE", "SHADOW_RES", "SHADOW_SCALE", "SHARED_FONTS", "SHARPNESS", "SHOTS", "SHOTS_DIR", - "SHOTS_FULL", "SPEED_PROBE", "SPIN", "SPIN_HANDOFF_US", "SPLIT_DRAWS", "SPLIT_US", "STICK_DEADZONE", "STICK_SENS", + "SHOTS_FULL", "SPEED_PROBE", "SPIN", "SPIN_HANDOFF_US", "SPLIT_DRAWS", "SPLIT_US", "SPV_OVERRIDE", "STICK_DEADZONE", "STICK_SENS", "SWKBD_ANSWER", "SWKBD_TEST", "TAA", "TEST_PAD", "TEST_PAD_AT", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_MEMO", "TEX_VRAM_PRESSURE", "TIMER_1MS", "TRACE", "TXD_TRANSPOSE", "UBO_SNAPSHOT", "UPSCALER", "VK_DYN_A2C", "VK_DYN_VERTEX", "VK_NOOPT", "VK_NO_FLOAT64", "VK_PRESENT", "VK_RECORDER", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION", "VOLUME", "VRAM_HEADROOM", "VS_OUTS", diff --git a/runtime/gfx.h b/runtime/gfx.h index 15e55e2d..75889671 100644 --- a/runtime/gfx.h +++ b/runtime/gfx.h @@ -89,6 +89,7 @@ size_t gfx_shader_warm_list(GfxWarmEnt **out); // malloc'd; recorde const GfxShader *gfx_shader_load_private(const uint8_t iid[32], int *owned); void gfx_shader_release_private(const GfxShader *s); const uint32_t *gfx_shader_spirv(const GfxShader *s, uint64_t *words); +int gfx_shader_write_code(const GfxShader *s, const char *path); // the guest program, from the stored inputs // A shader of the runtime's own (SPIR-V and a .res.json text as the translator writes them), outside the cache and // never freed; its key is the sha256 of both. NULL when the SPIR-V is malformed. const GfxShader *gfx_shader_own(const uint32_t *spirv, uint64_t words, const char *res_json); diff --git a/runtime/gfx_shader.c b/runtime/gfx_shader.c index 879cb0a6..e804fe3c 100644 --- a/runtime/gfx_shader.c +++ b/runtime/gfx_shader.c @@ -1875,6 +1875,17 @@ const uint32_t *gfx_shader_spirv(const GfxShader *s, uint64_t *words) { return s->spirv; } +// The guest program a shader was translated from, as its stored inputs hold it (the whole program: NVN2 code from its +// first byte), written to path for a disassembler (NX2NAVI_DUMP's sh_.code). 0: no stored inputs for it. +int gfx_shader_write_code(const GfxShader *s, const char *path) { + Inputs in; + paths(); + if (!s || !cache_dir[0] || !in_load(s->iid, &in)) return 0; + int ok = write_file(path, in.code, (size_t)in.length); + free(in.buf); + return ok; +} + int gfx_shader_stage(const GfxShader *s) { return s->stage; } int gfx_shader_resources(const GfxShader *s, int kind, const GfxShaderRes **res) { diff --git a/runtime/vk_dump.c b/runtime/vk_dump.c index dc9b9189..75d9f21d 100644 --- a/runtime/vk_dump.c +++ b/runtime/vk_dump.c @@ -487,7 +487,7 @@ __attribute__((format(printf, 2, 3))) static void sp(Str *b, const char *fmt, .. b->n += (size_t)k; } -// SPIR-V hash of a shader; the first time, its SPIR-V goes to sh_.spv. +// SPIR-V hash of a shader; the first time, its SPIR-V goes to sh_.spv and its guest program to sh_.code. static uint64_t shader_hash(const GfxShader *s) { static struct { const GfxShader *s; uint64_t h; } tab[1024]; static int n; @@ -502,6 +502,8 @@ static uint64_t shader_hash(const GfxShader *s) { snprintf(path, sizeof path, "%s/sh_%016llx.spv", dir, (unsigned long long)h); FILE *f = fopen(path, "wb"); if (f) fwrite(w, 4, (size_t)words, f), fclose(f); + snprintf(path, sizeof path, "%s/sh_%016llx.code", dir, (unsigned long long)h); // the guest program it came from + gfx_shader_write_code(s, path); if (n < 1024) tab[n].s = s, tab[n].h = h, n++; return h; } diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index bf7dfd2b..b7384b57 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -2836,12 +2836,37 @@ static void prebuild_start(void) { plat_mutex_unlock(&P.lock); } +// NX2NAVI_SPV_OVERRIDE= (test): a compute shader whose SPIR-V hashes (FNV-1a over its bytes, the name vk_dump.c +// gives it) to a file /.spv runs that SPIR-V instead, an edited copy with the same interface, to see a +// shader's intermediate values in a frame dump. NULL: no override (malloc'd words otherwise). +static uint32_t *spv_override(const uint32_t *code, uint64_t words, uint64_t *out) { + const char *dir = cfg_get("NX2NAVI_SPV_OVERRIDE"); + if (!dir || !*dir) return NULL; + uint64_t h = 0xcbf29ce484222325ull; + for (uint64_t i = 0; i < words * 4; i++) h = (h ^ ((const uint8_t *)code)[i]) * 0x100000001b3ull; + char path[600]; + snprintf(path, sizeof path, "%s/%016llx.spv", dir, (unsigned long long)h); + FILE *f = fopen(path, "rb"); + if (!f) return NULL; + fseek(f, 0, SEEK_END); + long n = ftell(f); + fseek(f, 0, SEEK_SET); + uint32_t *w = n > 20 && n % 4 == 0 ? malloc((size_t)n) : NULL; + if (w && fread(w, 1, (size_t)n, f) != (size_t)n) free(w), w = NULL; + fclose(f); + if (w) *out = (uint64_t)n / 4, rt_log("vk_pipeline: compute shader %016llx replaced by %s", (unsigned long long)h, path); + return w; +} + // A compute pipeline for cs in layout l; VK_NULL_HANDLE on failure. No lock needed (P.cache is driver-synchronised). static VkPipeline compute_create(const GfxShader *cs, const Layout *l) { uint64_t words; const uint32_t *code = gfx_shader_spirv(cs, &words); + uint64_t ow = 0; + uint32_t *over = spv_override(code, words, &ow); + if (over) code = over, words = ow; VkShaderModule m = make_module(code, words); - if (!m) return VK_NULL_HANDLE; + if (!m) return free(over), VK_NULL_HANDLE; VkPipelineShaderStageRequiredSubgroupSizeCreateInfo rqc; VkComputePipelineCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, .stage = {.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, @@ -2859,6 +2884,7 @@ static VkPipeline compute_create(const GfxShader *cs, const Layout *l) { if (vkCreateComputePipelines(P.device, P.cache, 1, &ci, NULL, &pipe) != VK_SUCCESS) pipe = VK_NULL_HANDLE; plat_crash_context(NULL); vkDestroyShaderModule(P.device, m, NULL); + free(over); return pipe; }