From 8603b0e1e9f6d2839372a0707bb83cf24bdaf739 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 09:38:07 -0500 Subject: [PATCH 1/3] Add the texture_detail setting, which makes large ASTC textures without their largest one or two mip levels on cards with little video memory. The textures of a loaded area are what fills a 6 GiB card: with the budget taken as 4,730 MiB a walk through Castle Town held 6,907 MiB of device memory, 4,302 MiB of it textures, 201 pools were refused their mirrors and the game ran at 20 fps. With texture_detail = reduced a mipmapped 2D or 2D-array ASTC texture of 256x256 and more that the game's CPU fills is made without its largest level, with low without its two largest when it is 1024x1024 or more. The same walk then held 5,472 MiB (textures 2,357) and 4,987 MiB (textures 1,997), and the ASTC texels decoded fell from 2,589 M to 826 M and 503 M. auto, the default, picks full from 8,192 MiB of device-local budget, low under 6,144 MiB and reduced between. full changes nothing. The image holds the game's levels from the one it starts at (VkTexImage.skip), and the upload with its ASTC regions and BC3 encoding, WriteTexels, the write-back, the views and the sampler LOD clamp of combined bindings count from there. Render targets, storage images, 3D and cube textures, small and unmipped textures and textures the device samples as ASTC itself are left whole, as is a texture made over a live one that holds GPU-written content. A shader that fetches texels by position or uses a texture's size gets the texture whole: gfx_shader.c marks such bindings from the SPIR-V (the layer count of an array, which the translator's layer clamp asks for, does not count), and the replay makes a reduced texture bound there again with every level. Not run on a card with 6 GiB: the figures come from an RTX 5090 with NX2NAVI_VRAM_BUDGET_MB=4730. Co-Authored-By: Claude Fable 5.2 --- docs/architecture.md | 5 + nx2navi.ini.example | 8 ++ runtime/cfg.c | 4 +- runtime/gfx.h | 3 + runtime/gfx_shader.c | 96 ++++++++++++++++ runtime/vk.h | 13 +++ runtime/vk_exec_draw.c | 50 +++++--- runtime/vk_texture.c | 253 ++++++++++++++++++++++++++++++++++++----- 8 files changed, 389 insertions(+), 43 deletions(-) diff --git a/docs/architecture.md b/docs/architecture.md index a2d98cd4..91b8c9eb 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -144,6 +144,11 @@ keeps per-core data without locks and relies on that, so the host models it (`ru once. `NX2NAVI_HDR_LIVE=0` leaves the HDR path out of a run that starts with HDR off. - ASTC textures are uploaded as they are where the device samples ASTC, and decoded by a compute shader everywhere else (`vk_astc.c`); under 20 GiB of video memory the decoder encodes them to BC3. +- Texture detail (`texture_detail`, `vk_texture.c` "texture detail"): on a card with little video memory the large + mipmapped ASTC textures the CPU fills are made without their largest mip level (reduced) or their two largest (low), + which is three quarters and fifteen sixteenths of their memory. The image holds the game's levels from the one it + starts at, and uploads, views and sampler LOD clamps count from there. A texture a shader reads by texel position, or + whose size it uses, is made whole (`gfx_shader.c` `reflect_exact`). `auto` picks by the device-local budget. - Occlusion queries never block the CPU; a result that is not ready counts as visible. - Allocation: Vulkan Memory Allocator. Debugging: Vulkan validation layers, per-pass frame dumps (`NX2NAVI_DUMP`, `vk_dump.c`), a sampling profiler (`NX2NAVI_PROFILE`, `tools/prof_report.py`) and a flight recorder diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 6df619aa..f3c66344 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -328,6 +328,14 @@ # are raised; shadow maps, data maps and screen-sized buffers keep the game's setting. Changes while the game runs take # effect at once. # aniso = game +# Texture detail: auto, full, reduced or low. The textures of a loaded area are what fills a card with little video +# memory, and three quarters of a texture is its largest mip level. reduced makes the large surface textures (256x256 and +# more, with smaller levels) without that level, low without the two largest when they are 1024x1024 or more. Surfaces +# seen up close look softer; interface art, small textures, shadow maps and everything the game draws itself keep their +# size. auto picks by the video memory Windows grants the game: full from 8192 MiB (cards of 10 GB and more), low under +# 6144 MiB (cards of 6 GB and less), reduced between (8 GB cards). The log says what was picked ("texture detail:"). +# Read at start. +# texture_detail = auto # Sun shadow map size: game (the game's own, which changes with the view and drops to about 400 near the ground, so # leaf shadows look blocky and jump) or a fixed 256, 512, 768, 1024, 1536 or 2048. Above 960 the texture heap grows with # it. 256 is coarser than the game's own and is for integrated GPUs short of memory bandwidth. Read at start. diff --git a/runtime/cfg.c b/runtime/cfg.c index b7edc4e9..ad561be9 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -67,7 +67,7 @@ static const char *const known[] = { "WALK_KEY", "WALK_SCALE", "WB_EXACT", "WB_FENCE_LIMIT", "WB_MERGE", "WB_NO_REUP", "WB_MERGE_MAX", "WB_PROBE", "WB_SPLIT", "WB_LAZY", "WB_LAZY_TOUCH", "WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_KEEP", "XLATE_PROCS", "ZSTD_CHECK", "ZSTD_NATIVE", "BIND_REUSE", "DYN_DELTA", "STG_ARENA", "SUBMIT_BATCH", "ASTC_GPU_DESWIZZLE", "ASTC_SCRATCH_KEEP", "ASTC_ARENA", - "TEXUP_LOG_MS", "DYN_RES", "DYN_RES_TARGET", "DYN_RES_STEP", + "TEXUP_LOG_MS", "DYN_RES", "DYN_RES_TARGET", "DYN_RES_STEP", "TEXTURE_DETAIL", "RECORDER_CORE", "RECORDER_PRIO", "REPLAY_TIME", "LOCK_TIME", "POOL_CREATE_BUDGET", "POOL_CREATE_MS", "POOL_LOAD_BACKLOG", "POOL_LOAD_MS", "VMA_BLOCK_MB", "ASTC_BC_PROBE", "SYNC_WAKE_ALL", "LOG_ASYNC", }; @@ -80,6 +80,7 @@ static const struct { const char *key, *section; } sections[] = { {"FULLSCREEN", "display"}, {"WINDOW_SIZE", "display"}, {"PERF_OVERLAY", "display"}, {"VK_PRESENT", "display"}, {"GPU_INDEX", "display"}, {"MONITOR", "display"}, {"OUTPUT_RES", "display"}, {"GAME_ASPECT", "display"}, {"GAME_RES", "display"}, {"RENDER_SCALE", "display"}, {"UPSCALER", "display"}, {"SHARPNESS", "display"}, {"ASPECT", "display"}, {"FOV", "display"}, {"ANISO", "display"}, + {"TEXTURE_DETAIL", "display"}, {"COLOR_PRESET", "display"}, {"COLOR_BRIGHTNESS", "display"}, {"COLOR_CONTRAST", "display"}, {"COLOR_SATURATION", "display"}, {"COLOR_GAMMA", "display"}, {"HDR", "display"}, {"HDR_LIVE", "display"}, {"HDR_PEAK_NITS", "display"}, {"HDR_GAME_NITS", "display"}, @@ -152,6 +153,7 @@ static const CfgMeta metas[] = { {"GAME_RES", "1440", "1080|1440|1800|2160|2880|window", RESTART}, // widescreen.c: the picture's height {"ASPECT", "fit", "fit|stretch", LIVE}, {"FOV", "100", "70..140", LIVE}, {"ANISO", "game", "game|2|4|8|16", LIVE}, // vk_texture.c sampler_features, vk_sampler_aniso_live + {"TEXTURE_DETAIL", "auto", "auto|full|reduced|low", RESTART}, // vk_texture.c "texture detail", read at the first texture // colour adjustment (vk_color.c); the preset is the menu's, the four values are what the present uses {"COLOR_PRESET", "default", "default|vivid|clarity|custom", LIVE}, {"COLOR_BRIGHTNESS", "0", "-30..30", LIVE}, {"COLOR_CONTRAST", "100", "70..150", LIVE}, {"COLOR_SATURATION", "100", "50..200", LIVE}, diff --git a/runtime/gfx.h b/runtime/gfx.h index 762404de..0619209f 100644 --- a/runtime/gfx.h +++ b/runtime/gfx.h @@ -121,10 +121,13 @@ typedef struct { uint8_t pool; // TEX: 1 the texture pool, 2 the sampler pool, indexed by bindless handles (set 2) uint16_t ptr; // SSBO: 0, or 1 + the byte offset of this buffer's address inside the storage buffer that // cbuf_slot and handle name (a pointer table; the translator's patch 0022, res.json "ptr") + uint8_t exact; // TEX: the module fetches its texels by position or uses its size or level count, so it + // needs every level of the texture as the game made it (gfx_shader.c reflect_exact) } GfxShaderRes; typedef struct { uint8_t type, comps, packed; } GfxShaderIo; // GFX_TYPE_*, components, 1 with a Component decoration int gfx_shader_stage(const GfxShader *s); // GFX_STAGE_VS, GFX_STAGE_FS or GFX_STAGE_CS int gfx_shader_resources(const GfxShader *s, int kind, const GfxShaderRes **res); // count of GFX_RES_* kind; array in *res +uint32_t gfx_shader_exact_seen(void); // shaders loaded so far with a GfxShaderRes.exact texture (the texture census line) uint32_t gfx_shader_slot_mask(const GfxShader *s); // texture/image slots whose type its translation read uint32_t gfx_shader_inputs(const GfxShader *s, GfxShaderIo io[32]); // Input Locations (VS attributes, FS varyings) as a mask uint32_t gfx_shader_outputs(const GfxShader *s, GfxShaderIo io[32]); diff --git a/runtime/gfx_shader.c b/runtime/gfx_shader.c index 4eda40b8..c822fc0c 100644 --- a/runtime/gfx_shader.c +++ b/runtime/gfx_shader.c @@ -1078,6 +1078,101 @@ out: free(cc); } +// Texture detail (vk_texture.c, "texture detail") makes large material textures without their top mip levels, which a +// shader that samples with normalised coordinates cannot tell. One that reads texels by position can, and so can one +// that works with the size or the level count. This finds the sampled textures a module reads so and sets +// GfxShaderRes.exact on their bindings; the replay then binds such a texture whole (vk_exec_draw.c tex_info_full): +// - OpImageFetch; +// - OpImageQuerySize and OpImageQueryLevels; +// - OpImageQuerySizeLod, unless the image is arrayed and every use of the result is an OpCompositeExtract of +// component 2, the layer count: the translator clamps the layer of an array sample to it, and no level changes it. +// The image of each read is followed back to its variable through the loads, access chains, OpSampledImage and OpImage +// the translator emits. A read that leads to no variable marks every texture of the module. One that leads into the +// texture pool array (a bindless handle picks the texture, so no binding names it) cannot be acted on and is logged: +// none of the 44,081 shaders prepared for game 1.1.1 has one. +static volatile int32_t n_exact_shaders; +uint32_t gfx_shader_exact_seen(void) { return (uint32_t)__atomic_load_n(&n_exact_shaders, __ATOMIC_RELAXED); } + +static void reflect_exact(GfxShader *s) { + enum { X_FUNCTION = 54, X_TYPE_IMAGE = 25, X_TYPE_SAMPLED_IMAGE = 27, X_LOAD = 61, X_ACCESS_CHAIN = 65, + X_IN_BOUNDS_ACCESS_CHAIN = 66, X_COMPOSITE_EXTRACT = 81, X_COPY_OBJECT = 83, X_SAMPLED_IMAGE = 86, X_FETCH = 95, + X_IMAGE = 100, X_QUERY_SIZE_LOD = 103, X_QUERY_SIZE = 104, X_QUERY_LEVELS = 106, X_DEC_BINDING = 33 }; + static volatile int32_t n_pool_logged; + const uint32_t *w = s->spirv; + uint64_t n = s->words, fn = 0; + if (!s->nres[GFX_RES_TEX] || n < 5) return; + int any = 0; // nine shaders in ten have no such read: they cost this walk over the instruction headers + for (uint64_t i = 5; i < n && !any;) { + uint32_t op = w[i] & 0xffff, len = w[i] >> 16; + if (!len) break; + any = op == X_FETCH || op == X_QUERY_SIZE || op == X_QUERY_LEVELS || op == X_QUERY_SIZE_LOD; + i += len; + } + if (!any) return; + uint32_t bound = w[3] < (1u << 20) ? w[3] : (1u << 20); + // per id: a type's or variable's image is arrayed; a variable's Binding + 1; the variable a value comes from; the + // variable a pending size query read (+ 1); and per variable whether it is read exactly + uint8_t *arrayed = calloc(bound, 1), *need = calloc(bound, 1); + uint32_t *bind = calloc(bound, sizeof *bind), *org = calloc(bound, sizeof *org), *query = calloc(bound, sizeof *query); + int all = 0, queries = 0; + if (!arrayed || !need || !bind || !org || !query) goto out; + for (uint64_t i = 5; i < n;) { + uint32_t op = w[i] & 0xffff, len = w[i] >> 16; + if (!len || i + len > n) break; + const uint32_t *a = w + i; + if (op == X_FUNCTION && !fn) fn = i; + #define ID(x) ((x) < bound) + if (op == OP_DECORATE && len >= 4 && ID(a[1]) && a[2] == X_DEC_BINDING) bind[a[1]] = a[3] + 1; + else if (op == X_TYPE_IMAGE && len >= 9 && ID(a[1])) arrayed[a[1]] = a[5] != 0; + else if (op == X_TYPE_SAMPLED_IMAGE && len >= 3 && ID(a[1]) && ID(a[2])) arrayed[a[1]] = arrayed[a[2]]; + else if (op == OP_TYPE_ARRAY && len >= 4 && ID(a[1]) && ID(a[2])) arrayed[a[1]] = arrayed[a[2]]; + else if (op == OP_TYPE_POINTER && len >= 4 && ID(a[1]) && ID(a[3])) arrayed[a[1]] = arrayed[a[3]]; + else if (op == OP_VARIABLE && len >= 4 && ID(a[1]) && ID(a[2])) { + arrayed[a[2]] = arrayed[a[1]]; + if (bind[a[2]]) org[a[2]] = a[2]; + } else if ((op == X_ACCESS_CHAIN || op == X_IN_BOUNDS_ACCESS_CHAIN || op == X_LOAD || op == X_COPY_OBJECT || + op == X_SAMPLED_IMAGE || op == X_IMAGE) && len >= 4 && ID(a[2]) && ID(a[3])) { + org[a[2]] = org[a[3]]; + } else if ((op == X_FETCH || op == X_QUERY_SIZE || op == X_QUERY_LEVELS || op == X_QUERY_SIZE_LOD) && len >= 4) { + uint32_t var = ID(a[3]) ? org[a[3]] : 0; + if (!var) all = 1; + else if (op == X_QUERY_SIZE_LOD && arrayed[var] && ID(a[2])) query[a[2]] = var + 1, queries++; + else need[var] = 1; + } + #undef ID + i += len; + } + // every use of a pending size query: anything but the layer count makes its texture exact. Any word that equals + // the result's id counts as a use, so a literal that happens to equal it errs towards exact. + for (uint64_t i = fn; queries && fn && i < n;) { + uint32_t op = w[i] & 0xffff, len = w[i] >> 16; + if (!len || i + len > n) break; + int layer_count = op == X_COMPOSITE_EXTRACT && len == 5 && w[i + 4] == 2; + int defines = op == X_QUERY_SIZE_LOD && len >= 4; + for (uint32_t j = 1; j < len; j++) { + uint32_t x = w[i + j]; + if (x >= bound || !query[x]) continue; + if ((defines && j == 2) || (layer_count && j == 3)) continue; + need[query[x] - 1] = 1; + } + i += len; + } + int pooled = 0, some = 0; + for (int k = 0; k < s->nres[GFX_RES_TEX]; k++) { + GfxShaderRes *r = &s->res[GFX_RES_TEX][k]; + int hit = 0; + for (uint32_t id = 0; id < bound && !hit; id++) hit = need[id] && bind[id] == (uint32_t)r->binding + 1; + if (r->pool) pooled |= hit; + else r->exact = (uint8_t)(hit || all), some |= r->exact; + } + if (some) plat_inc32(&n_exact_shaders); + if (pooled && plat_inc32(&n_pool_logged) <= 4) + rt_log("gfx_shader: a shader reads a texture of the bindless pool by texel position or uses its size; with " + "texture_detail below full such a texture may be read at the wrong level (vk_texture.c, texture detail)"); +out: + free(arrayed), free(need), free(bind), free(org), free(query); +} + static void reflect_stage(GfxShader *s) { const char *js = s->reflection; char stage[8]; @@ -1098,6 +1193,7 @@ static void reflect(GfxShader *s) { json_objects(js, kinds[k], add_res, s, k); } reflect_spirv(s); + reflect_exact(s); patch_txd_transpose(s); } diff --git a/runtime/vk.h b/runtime/vk.h index f1d72b46..7aa2a4a5 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -474,6 +474,9 @@ typedef struct { uint8_t scale; // 1, or NX2NAVI_SHADOW_SCALE for a shadow map made larger than the game's (vk_texture.c // shadow_scale_of): width and height above are the larger ones, the game's regions // are multiplied by it + uint8_t skip; // texture detail (vk_texture.c detail_skip): the game's top mip levels the image was made + // without. Image level i holds the game's level i + skip, and width, height and levels + // above are the image's, so the game's level skip is their base. 0 everywhere else. uint8_t gone; // destroyed (vk_texture.c destroy_tex, texture lock held): the record stays readable until // the GPU is past the submission being recorded (vk_defer_free), as the replay and DLSS // keep pointers to render targets across a command buffer; no view of it is cached @@ -498,6 +501,16 @@ VkSampler vk_sampler_point_by_id_locked(int id); // The slot's sampler with its compare on, for a depth-compare read (vk_texture.c, at the definition); the slot's own // sampler when it compares already. VK_NULL_HANDLE for an empty slot. VkSampler vk_sampler_compare_by_id_locked(int id); +// Texture detail (vk_texture.c): what texture_detail = auto picks on this device, 0 full, 1 reduced, 2 low (the budget is +// read at the first call). For the menu's row. +int vk_texture_detail_auto(void); +// The slot's sampler for a texture made without its top `skip` mip levels (VkTexImage.skip): its LOD clamp counts the +// game's levels, so it moves down by skip. The slot's own sampler when its clamp is open or skip is 0. +VkSampler vk_sampler_detail_by_id_locked(int id, uint32_t skip); +// Slot id resolved to a texture made without its top levels, and a shader binding that reads texels by position or +// asks for the size wants it: the texture is made again with every level and stays so (vk_texture.c "texture detail"). +// Returns what the slot resolves to now, as vk_texture_by_id_locked does. +const VkTexImage *vk_texture_exact_by_id_locked(int id, view_t *view, int *has_view); uint32_t vk_texture_slots_gen(void); int vk_texture_slot_count_locked(void); void vk_texture_create_budget_locked(int n); // slot lookups create at most n entries (mapped storage only); -1 any diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index da3bd741..66685a5d 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -653,9 +653,23 @@ static VkImageView get_view(VkExec *ex, const VkTexImage *img, const view_t *v, } if (!ok) return VK_NULL_HANDLE; uint32_t l0 = hv && v->set_levels ? v->base_level : 0; - if (l0 >= img->levels) return VK_NULL_HANDLE; - uint32_t nl = hv && v->set_levels && v->nlevels ? v->nlevels : img->levels - l0; - if (l0 + nl > img->levels) nl = img->levels - l0; + uint32_t nl; + if (img->skip) { + // Texture detail (vk_texture.c): the image was made without the game's top img->skip levels, so the view's + // levels [l0, end) of the game are the image's [l0 - skip, end - skip). A view that names only levels the image + // lacks shows the first one it has: the nearest content, where a stand-in would show none. + uint32_t sk = img->skip, all = img->levels + sk; + if (l0 >= all) return VK_NULL_HANDLE; + uint32_t end = hv && v->set_levels && v->nlevels ? l0 + v->nlevels : all; + if (end > all) end = all; + l0 = l0 > sk ? l0 - sk : 0; + end = end > sk ? end - sk : 1; + nl = end > l0 ? end - l0 : 1; + } else { + if (l0 >= img->levels) return VK_NULL_HANDLE; + nl = hv && v->set_levels && v->nlevels ? v->nlevels : img->levels - l0; + if (l0 + nl > img->levels) nl = img->levels - l0; + } if (storage) nl = 1; uint32_t y0 = 0, ny = 1; if (vt != VK_IMAGE_VIEW_TYPE_3D) { @@ -1610,8 +1624,8 @@ static void feedback_written(VkExec *ex, const VkPipeState *s, uint32_t sampled) ex->fb_read |= sampled; } -static void tex_info_full(VkExec *ex, const GfxShaderRes *r, uint64_t h, int storage, int separate, - VkDescriptorImageInfo *out, VkImage *image); +static int tex_info_full(VkExec *ex, const GfxShaderRes *r, uint64_t h, int storage, int separate, + VkDescriptorImageInfo *out, VkImage *image); // Sampled-texture descriptors per (handle, resource type, separate), kept while nothing they derive from changed: the // slot's resolution and texture entry (slot_still) and the samplers (vk_sampler_gen); the layout is set again on every @@ -1627,6 +1641,7 @@ static struct { uint32_t gen, epoch, sgen; uint16_t type; uint8_t separate, valid; + uint8_t reduced; // the image was made without its top levels and the resource did not mind (GfxShaderRes.exact) VkImage image; VkDescriptorImageInfo out; } TD[TD_N]; @@ -1635,11 +1650,11 @@ static void tex_info_lookup(VkExec *ex, const GfxShaderRes *r, uint64_t h, int s VkDescriptorImageInfo *out) { int td_on = td_mode(); VkImage image; - if (!h || storage || !td_on) return tex_info_full(ex, r, h, storage, separate, out, &image); + if (!h || storage || !td_on) return (void)tex_info_full(ex, r, h, storage, separate, out, &image); uint32_t gen = vk_texture_slots_gen(), ep = vk_texture_epoch(), sg = vk_sampler_gen(); // before any lookup typeof(TD[0]) *m = &TD[(h * 0x9E3779B97F4A7C15ull ^ (uint64_t)r->type << 7 ^ (uint64_t)separate) >> 51]; if (m->valid && m->h == h && m->type == r->type && m->separate == separate && m->sgen == sg && - slot_still(&m->gen, m->tok, h, gen)) { + !(m->reduced && r->exact) && slot_still(&m->gen, m->tok, h, gen)) { if (m->epoch != ep) { // the slot's first binding in this submission: only its storage check is due vk_texture_by_id_locked((int)(h & 0xfffff), NULL, NULL); m->epoch = ep; @@ -1657,14 +1672,17 @@ static void tex_info_lookup(VkExec *ex, const GfxShaderRes *r, uint64_t h, int s return; } uint64_t tok = token_of(h); // before the lookup - tex_info_full(ex, r, h, storage, separate, out, &image); + int reduced = tex_info_full(ex, r, h, storage, separate, out, &image); + // a lookup that made the texture whole again changed the slot's resolution after the stamps above were read: the + // entry never matches, and the next binding fills it *m = (typeof(*m)){.h = h, .tok = tok, .gen = gen, .epoch = ep, .sgen = sg, .type = r->type, - .separate = (uint8_t)separate, .valid = 1, .image = image, .out = *out}; + .separate = (uint8_t)separate, .valid = 1, .reduced = (uint8_t)reduced, .image = image, .out = *out}; } -static void tex_info_full(VkExec *ex, const GfxShaderRes *r, uint64_t h, int storage, int separate, - VkDescriptorImageInfo *out, VkImage *image) { - int ms = (r->type & 0x200) != 0; +// Returns whether the image bound was made without its top mip levels (texture detail, VkTexImage.skip). +static int tex_info_full(VkExec *ex, const GfxShaderRes *r, uint64_t h, int storage, int separate, + VkDescriptorImageInfo *out, VkImage *image) { + int ms = (r->type & 0x200) != 0, reduced = 0; *image = VK_NULL_HANDLE; VkImageView view = VK_NULL_HANDLE; VkSampler samp = VK_NULL_HANDLE; @@ -1673,6 +1691,10 @@ static void tex_info_full(VkExec *ex, const GfxShaderRes *r, uint64_t h, int sto view_t v; int hv; const VkTexImage *img = vk_texture_by_id_locked((int)(h & 0xfffff), &v, &hv); + // A shader that reads this binding's texels by position, or asks for its size, needs every level of the + // texture where the game put it (gfx_shader.c reflect_exact): one made without its top levels is made again. + if (img && img->skip && r->exact) img = vk_texture_exact_by_id_locked((int)(h & 0xfffff), &v, &hv); + reduced = img && img->skip; if (img) view = get_view(ex, img, &v, hv, r->type, storage), *image = img->image; if (img && storage) vk_texture_storage_bound_locked(img); if (img && !storage && ex->nfbl && in_fbl(ex, img->image)) layout = VK_IMAGE_LAYOUT_ATTACHMENT_FEEDBACK_LOOP_OPTIMAL_EXT; @@ -1683,7 +1705,8 @@ static void tex_info_full(VkExec *ex, const GfxShaderRes *r, uint64_t h, int sto int point = img && (img->aspect & VK_IMAGE_ASPECT_DEPTH_BIT) && !(r->type & 0x400) && vk_exec_view_aspect(img, &v, hv) != VK_IMAGE_ASPECT_STENCIL_BIT; samp = point ? vk_sampler_point_by_id_locked(sid) - : r->type & 0x400 ? vk_sampler_compare_by_id_locked(sid) : vk_sampler_by_id_locked(sid); + : r->type & 0x400 ? vk_sampler_compare_by_id_locked(sid) + : reduced ? vk_sampler_detail_by_id_locked(sid, img->skip) : vk_sampler_by_id_locked(sid); } } if (!view) { @@ -1692,6 +1715,7 @@ static void tex_info_full(VkExec *ex, const GfxShaderRes *r, uint64_t h, int sto } if (!samp) samp = r->type & 0x400 ? Z.samp_shadow : Z.samp; *out = (VkDescriptorImageInfo){separate || storage ? VK_NULL_HANDLE : samp, view, layout}; + return reduced; } // ---- texture and sampler pools (bindless) diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index 1ea5661b..f787d2f8 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -108,9 +108,11 @@ typedef struct { view_t v; tex_t t; } TexSlot; -typedef struct { samp_par p; VkSampler s, sp, sc; uint8_t used; } SampSlot; // s: vk_sampler_for(&p) once looked up; sp: +typedef struct { samp_par p; VkSampler s, sp, sc, sd[2]; uint8_t used; } SampSlot; // s: vk_sampler_for(&p) once looked up; sp: // its point-sampled form (vk_sampler_point_by_id_locked), - // sc its comparing form (vk_sampler_compare_by_id_locked) + // sc its comparing form (vk_sampler_compare_by_id_locked), + // sd[n - 1] its form for a texture made without n + // levels (vk_sampler_detail_by_id_locked) static PlatRMutex tex_lock; static PlatOnce tex_once = PLAT_ONCE_INIT; @@ -692,6 +694,104 @@ static void astc_probe_alpha(const tex_t *t, const TexVkFormat *f) { (unsigned long long)n_probe_alpha, n_probe_bytes_alpha / 1048576.0); } +// ---- texture detail (NX2NAVI_TEXTURE_DETAIL = auto | full | reduced | low, texture_detail in nx2navi.ini; read at +// the first texture, so a change waits for a restart) +// The material textures of a loaded area are what fills a small card, and three quarters of a mipmapped texture is its +// largest level. Over the budget Windows moves device memory into system memory and the frame rate falls to a +// fraction (a tester's RTX 3060 Laptop, 6 GiB with a budget of 4,730 MiB, ran the world at 5 to 15 fps). +// +// Measured on an RTX 5090 with the budget taken as those 4,730 MiB (NX2NAVI_VRAM_BUDGET_MB), a walk through Castle +// Town at 1440p with DLSS Balanced, device memory in use at its peak and the textures in it: +// full 6,907 MiB, textures 4,302 MiB (2,852 MiB of them ASTC as BC3), 2,589 M ASTC texels decoded +// reduced 5,472 MiB, textures 2,357 MiB (908 MiB), 826 M texels +// low 4,987 MiB, textures 1,997 MiB (548 MiB), 503 M texels +// About 1.1 GiB of each is outside the textures and buffers (DLSS alone reports 0.7 GiB). What a level does not touch +// stays: about 0.8 GiB of BC4, BC5 and BC7 textures the game ships in those formats, 0.65 GiB of render targets and +// 0.9 GiB of buffer mirrors. +// +// reduced makes such a texture without its largest level, low without its two largest when it is 1024x1024 or more +// (detail_skip decides). The image then holds the game's levels from VkTexImage.skip on: image level i is the game's +// level i + skip, at that level's own size, so a level and a texel rectangle inside it mean the same in both and only +// the level index moves. Every path from a game level to the image subtracts skip: the upload and its ASTC regions +// (upload, astc_bl_regions), WriteTexels and the write-back (image_texels, write_back_n), the views (vk_exec_draw.c +// get_view) and the sampler's LOD clamp (vk_sampler_detail_by_id_locked). Shaders sample with normalised coordinates +// and derivatives, which pick the same game level as before wherever it is kept. +// +// Only a texture for which that holds is reduced: an ASTC texture this runtime decodes (not one the device samples as +// ASTC, whose copies run on the image, vk_exec_xfer.c sub_resolve), a 2D texture or 2D array, block-linear, one +// sample, mipmapped, DETAIL_MIN texels or more each way, whose content the CPU provides (tex_mirrored). That leaves +// out render targets and storage images (no ASTC format can be either), 3D and cube textures, the small and unmipped +// ones (interface art, glyph atlases, lookup tables, ramps) and whatever the GPU copies, as texture copies of a +// decoded ASTC texture are not replayed at all. Two more are left whole: +// - a texture made over a live one that holds GPU-written content, which start_at copies in texel for texel; +// - a texture a shader reads by texel position or whose size it asks for (exact, below): a fetch at a level the image +// lacks, or coordinates worked out from the size of another texture, would read the wrong texels. The translated +// shaders do that only through fixed bindings (gfx_shader.c reflect_exact; none of 44,081 prepared shaders of game +// 1.1.1 does it through the texture pool), so the replay sees the texture at the binding and asks for it whole +// (vk_texture_exact_by_id_locked). The key then stays whole for the session. +// The size of a 2D array is also asked for by the layer clamp the translator puts before array samples; that reads +// the layer count alone, which no level changes, and does not count (reflect_exact). +// +// auto: full from DETAIL_FULL_MB of device-local budget on, low under DETAIL_LOW_MB, reduced between. Textures give +// way from 80% of the budget on (sweep_pressure), so a level wants its use at or under that: full, 6.9 to 7.5 GiB in +// the town and in Hyrule Field, on cards of 10 GiB and more (their budgets start near 9 GiB; an 8 GiB card's is about +// 7.2 GiB); reduced, 5.5 GiB, on 8 GiB cards; and every 6 GiB card, whose budget stays under 5.5 GiB, gets low. +#define DETAIL_MIN 256 +#define DETAIL_LOW_MIN 1024 +#define DETAIL_FULL_MB 8192 +#define DETAIL_LOW_MB 6144 +#define EXACT_MAX 65536 +static nvn_hmap exact = {.klen = sizeof(TexKey)}; // keys made whole whatever the setting; value 1 +static uint64_t n_exact; // textures made whole again so far + +static int alias_has_gpu_content(const struct Tex *src); + +static int64_t detail_budget_mb = -1; // the device-local budget auto went by, in MiB; 0: the device reports none + +int vk_texture_detail_auto(void) { + int64_t mb = __atomic_load_n(&detail_budget_mb, __ATOMIC_RELAXED); + if (mb < 0 && !vk_ctx()) return 0; // asked before there is a device: nothing is kept + if (mb < 0) { // read once: the budget moves with what else uses the card, the choice must not + uint64_t used = 0, budget = 0; + int exact_budget = 0; + mb = vk_mem_vram(&used, &budget, &exact_budget) ? (int64_t)(budget >> 20) : 0; + __atomic_store_n(&detail_budget_mb, mb, __ATOMIC_RELAXED); + } + return !mb || mb >= DETAIL_FULL_MB ? 0 : mb >= DETAIL_LOW_MB ? 1 : 2; +} + +static int detail_most(void) { // the most levels a texture is made without: 0 full, 1 reduced, 2 low + static const char *const names[3] = {"full", "reduced", "low"}; + static int most = -1; + if (most >= 0) return most; + const char *v = cfg_value("NX2NAVI_TEXTURE_DETAIL"); + most = !strcmp(v, "full") ? 0 : !strcmp(v, "reduced") ? 1 : !strcmp(v, "low") ? 2 : -1; + if (most >= 0) { + rt_log("vk: texture detail: %s (texture_detail)", v); + } else { + most = vk_texture_detail_auto(); + if (!detail_budget_mb) + rt_log("vk: texture detail: full (texture_detail = %s; the device reports no video memory budget)", v); + else + rt_log("vk: texture detail: %s, chosen for a video memory budget of %lld MiB (texture_detail = %s; full from " + "%d MiB, low under %d MiB)", names[most], (long long)detail_budget_mb, v, DETAIL_FULL_MB, DETAIL_LOW_MB); + } + return most; +} + +// The top mip levels the image of t is made without (the rule above). w, h, levels: the image as it would be whole. +static uint32_t detail_skip(const tex_t *t, const TexKey *k, const TexVkFormat *f, uint32_t w, uint32_t h, uint32_t levels, + uint32_t samples) { + int most = detail_most(); + const nvn_texdesc *d = &t->d; + if (!most || !f->astc_bw || samples > 1 || levels < 2 || w < DETAIL_MIN || h < DETAIL_MIN) return 0; + if ((d->target != NVN_TEXTURE_TARGET_2D && d->target != NVN_TEXTURE_TARGET_2D_ARRAY) || nvn_tex_is_linear(d)) return 0; + if (!tex_mirrored(t) || (exact.n && nvn_hm_get(&exact, k))) return 0; + for (const struct Tex *p = nvn_hm_get(&by_addr, &k->addr); p; p = p->next_at) + if (!p->released && alias_has_gpu_content(p)) return 0; + return most >= 2 && levels >= 4 && w >= DETAIL_LOW_MIN && h >= DETAIL_LOW_MIN ? 2 : 1; +} + // defer_init: the caller records the image's first transition (init_image or alias_copy). static Tex *create_tex(const tex_t *t, const TexKey *k, int defer_init) { static volatile int32_t n_unmapped, n_unsupported, n_failed, n_unbound, n_thick; @@ -730,6 +830,8 @@ static Tex *create_tex(const tex_t *t, const TexKey *k, int defer_init) { if (levels > most) levels = most; uint32_t samples = d->samples > 1 ? (uint32_t)d->samples : 1; if (samples > 1) levels = 1; + uint32_t skip = detail_skip(t, k, &f, w, h, levels, samples); // texture detail: the image starts at this level + if (skip) w = mip(w, skip), h = mip(h, skip), levels -= skip; VkFormatFeatureFlags ff = features_of(f.vk); VkImageUsageFlags usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; int ds = f.depth || f.stencil; @@ -935,6 +1037,7 @@ static Tex *create_tex(const tex_t *t, const TexKey *k, int defer_init) { p->block_views = (uint8_t)block_views; p->fb_usage = (uint8_t)fb_usage; p->scale = (uint8_t)scale; + p->skip = (uint8_t)skip; if (!defer_init) init_new(e); return e; @@ -1732,7 +1835,8 @@ static uint64_t content_hash(const void *p, uint64_t n) { // The CPU deswizzle moved every 16 bytes through the GOB arithmetic and was most of an ASTC upload's CPU time when an // area loads. Fills ar (n = levels * layers, level-major as upload's regions) and returns 1 when every level and layer // qualifies: block-linear, starting 16-byte aligned, its layout inside the storage. NX2NAVI_ASTC_GPU_DESWIZZLE=0 keeps -// the CPU deswizzle. +// the CPU deswizzle. levels counts the image's levels: level lv of the image is the game's level lv + skip (texture +// detail), which is where its blocks lie and what its size is. static int astc_bl_regions(const Tex *e, uint32_t levels, uint32_t layers, int bw, int bh, VkAstcRegion *ar) { static int on = -1; if (on < 0) { @@ -1741,16 +1845,16 @@ static int astc_bl_regions(const Tex *e, uint32_t levels, uint32_t layers, int b } const nvn_texdesc *d = &e->t.d; if (!on || nvn_tex_is_linear(d)) return 0; - uint32_t k = 0; + uint32_t k = 0, sk = e->pub.skip; for (uint32_t lv = 0; lv < levels; lv++) for (uint32_t ly = 0; ly < layers; ly++) { uint32_t row_bytes, rows, slices, bpb; uint64_t base; unsigned th, td; - if (nvn_deswizzle_geom(d, lv, ly, &row_bytes, &rows, &slices, &base, &th, &td, &bpb) || (base & 15) || th > 5 || - td > 5) + if (nvn_deswizzle_geom(d, lv + sk, ly, &row_bytes, &rows, &slices, &base, &th, &td, &bpb) || (base & 15) || + th > 5 || td > 5) return 0; - ar[k] = (VkAstcRegion){.src = base, .w = mip(d->width, lv), .h = mip(d->height, lv), .slices = slices, + ar[k] = (VkAstcRegion){.src = base, .w = mip(d->width, lv + sk), .h = mip(d->height, lv + sk), .slices = slices, .level = lv, .layer = ly, .bl = 1, .th = (uint8_t)th, .td = (uint8_t)td}; if (base + vk_astc_region_bytes(&ar[k], (uint32_t)bw, (uint32_t)bh) > e->t.L.size) return 0; k++; @@ -1774,7 +1878,9 @@ static int astc_arena(void) { return on; } -// Copies the whole storage into the image (every level and layer the image has). +// Copies the whole storage into the image (every level and layer the image has). An image made without its top levels +// (texture detail, VkTexImage.skip) has the game's level lv + skip at its level lv: the loops below run over the image's +// levels and read the storage at the game's. static void upload(Tex *e) { static volatile int32_t n_bad, n_undecoded, n_split; const tex_t *t = &e->t; @@ -1800,20 +1906,23 @@ static void upload(Tex *e) { uint32_t levels = linear ? 1 : e->pub.levels, layers = linear ? 1 : e->pub.layers; int astc = e->f.astc_bw != 0, rgb = e->f.expand_rgb; int agpu = astc && vk_astc_gpu(), acheck = agpu && vk_astc_mode() == VK_ASTC_CHECK; // raw blocks to the GPU - uint32_t n = levels * layers, k = 0; + uint32_t n = levels * layers, k = 0, sk = linear ? 0 : e->pub.skip; VkBufferImageCopy *rg = calloc(n, sizeof *rg); VkAstcRegion *ar = agpu ? calloc(n, sizeof *ar) : NULL; VkDeviceSize total = 0, decoded = 0; + uint64_t src_bytes = 0; // the storage bytes the image's levels take (all of the storage unless levels are left out) for (uint32_t lv = 0; lv < levels; lv++) for (uint32_t ly = 0; ly < layers; ly++) { - uint32_t w = mip(d->width, lv), h = one_d ? 1 : mip(d->height, lv), sl = is3d ? mip(d->depth, lv) : 1; + uint32_t w = mip(d->width, lv + sk), h = one_d ? 1 : mip(d->height, lv + sk), sl = is3d ? mip(d->depth, lv + sk) : 1; uint64_t texels = (uint64_t)w * h * sl, raw = (uint64_t)(w + bw - 1) / bw * bpb * ((h + bh - 1) / bh) * sl; uint64_t out = agpu ? raw : astc ? texels * 4 : rgb ? texels * (uint64_t)bpb / 3 * 4 : raw; rg[k++] = (VkBufferImageCopy){.bufferOffset = total, .imageSubresource = {e->pub.aspect, lv, ly, 1}, .imageExtent = {w, h, sl}}; total += (out + 15) & ~15ull; decoded += texels * 4; + src_bytes += raw; } + if (!sk) src_bytes = t->L.size; VkBuffer sb, rbuf = VK_NULL_HANDLE; VmaAllocation ralloc = NULL; uint8_t *rmap = NULL, *raw_copy = NULL; @@ -1828,6 +1937,14 @@ static void upload(Tex *e) { for (k1 = k0 + 1; k1 < n && (agpu || (k1 + 1 < n ? rg[k1 + 1].bufferOffset : total) - base <= UPLOAD_PART);) k1++; size = (k1 < n ? rg[k1].bufferOffset : total) - base; if (gdsw) size = t->L.size; // the storage as it is (one part: the GPU decoder takes every region at once) + // Without its top levels, only the levels the image has: each region's blocks as they lie in the storage, one + // after the other (the top level is three quarters of the bytes, and an array keeps each layer's levels + // together, so the kept levels are no single range). + if (gdsw && sk) { + size = 0; + for (k = 0; k < n; k++) size += vk_astc_region_bytes(&ar[k], (uint32_t)bw, (uint32_t)bh); + src_bytes = size; + } if (k0) parts++; VkCommandBuffer cb = vk_upload_begin(); // The part stages in a range of the staging arena (vk_staging_at, its offset added to the regions below), or in @@ -1851,9 +1968,17 @@ static void upload(Tex *e) { log_limited(&n_gone, "texture %#llx (%llu bytes): storage released during the upload; uploaded as zeros", (unsigned long long)e->pub.addr, (unsigned long long)e->t.L.size); } - if (gdsw) { + if (gdsw && !sk) { if (held) memcpy(stg, t->mem, size); else memset(stg, 0, size); + } else if (gdsw) { + VkDeviceSize at = 0; + for (k = 0; k < n; k++) { // astc_bl_regions checked that each lies inside the storage + VkDeviceSize nb = vk_astc_region_bytes(&ar[k], (uint32_t)bw, (uint32_t)bh); + if (held) memcpy(stg + at, t->mem + ar[k].src, nb); + else memset(stg + at, 0, nb); + ar[k].src = at, at += nb; + } } for (k = k0; k < k1 && !gdsw; k++) { uint32_t lv = rg[k].imageSubresource.mipLevel, ly = rg[k].imageSubresource.baseArrayLayer; @@ -1865,9 +1990,9 @@ static void upload(Tex *e) { tmps[k - k0] = tmp; if (!held) { memset(tmp ? tmp : dst, 0, raw); - } else if (nvn_deswizzle(d, lv, ly, t->mem, tmp ? tmp : dst, rb)) { + } else if (nvn_deswizzle(d, lv + sk, ly, t->mem, tmp ? tmp : dst, rb)) { log_limited(&n_bad, "texture %#llx fmt %#x target %d: level %u layer %u has no CPU layout; uploaded as zeros", - (unsigned long long)e->pub.addr, d->format, d->target, lv, ly); + (unsigned long long)e->pub.addr, d->format, d->target, lv + sk, ly); memset(tmp ? tmp : dst, 0, raw); } } @@ -1939,13 +2064,13 @@ static void upload(Tex *e) { uint64_t dt = kern_now_ns() - u0; if (e->uploaded) { n_reuploads++, perf_count(PERF_CNT_TEX_REUP); - ru_n++, ru_bytes += t->L.size, ru_ns += dt; + ru_n++, ru_bytes += src_bytes, ru_ns += dt; if (e->wt_n) { - ru_wt_n++, ru_wt_bytes += t->L.size, ru_wt_ns += dt; + ru_wt_n++, ru_wt_bytes += src_bytes, ru_wt_ns += dt; if (rt_trace & RT_TRACE_PERF) rt_log("perf: frame %llu: re-upload after %u WriteTexels: texture %#llx fmt %#x %ux%u, %.1f KiB, %.3f ms", (unsigned long long)__atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED), e->wt_n, - (unsigned long long)e->pub.addr, d->format, d->width, d->height, t->L.size / 1024.0, dt / 1e6); + (unsigned long long)e->pub.addr, d->format, d->width, d->height, src_bytes / 1024.0, dt / 1e6); } } else { n_uploads++, perf_count(PERF_CNT_TEX_UP); @@ -1955,7 +2080,7 @@ static void upload(Tex *e) { e->n_up++; perf_end(PERF_TEX, p0); vk_texstat_time(VK_TS_UPLOAD, u0); - vk_texstat_add(VK_TS_UPLOAD, 0, t->L.size); + vk_texstat_add(VK_TS_UPLOAD, 0, src_bytes); } // Whether e's storage fills no 4 KiB page of its own, so the write tracking can only see writes to pages it shares. @@ -2050,6 +2175,7 @@ static int alias_compatible(const Tex *e, const Tex *src, const char **why) { if (e->f.depth || e->f.stencil || src->f.depth || src->f.stencil || p->samples > 1 || q->samples > 1) return *why = "depth, stencil or multisample", 0; if (p->type != q->type) return *why = "another image type", 0; + if (p->skip || q->skip) return *why = "made without its top levels (texture detail)", 0; // level i is not level i if (nvn_tex_is_linear(&e->t.d) != nvn_tex_is_linear(&src->t.d)) return *why = "linear and block-linear", 0; int ew, eh, sw, sh, eb = img_block(e, &ew, &eh), sb = img_block(src, &sw, &sh); if (!eb || eb != sb || ew != sw || eh != sh) return *why = "another block size", 0; @@ -2369,6 +2495,12 @@ void vk_texture_gpu_written(const tex_t *t) { } e->pub.gpu_owned = 1; e->chash_ok = 0; // the image now holds the GPU's content: matching storage bytes no longer mean "unchanged" + if (e->pub.skip) { // texture detail leaves out what the GPU can write (detail_skip); said if it ever does + static volatile int32_t n_skip_written; + log_limited(&n_skip_written, "texture %#llx (fmt %#x %ux%u), made without its top %u levels, was written by " + "the GPU: its levels are not the game's", (unsigned long long)e->pub.addr, e->t.d.format, + e->t.d.width, e->t.d.height, e->pub.skip); + } uint64_t f = vk_exec_recording_fence(); if (f > e->wr_fence) e->wr_fence = f; if (e->pub.mirrored) { @@ -2673,6 +2805,27 @@ const VkTexImage *vk_texture_by_id_locked(int id, view_t *view, int *has_view) { return e ? &e->pub : NULL; } +// Texture detail: the texture slot id resolves to was made without its top levels, and a shader binding reads its +// texels by position or asks for its size (vk_exec_draw.c tex_info_full, GfxShaderRes.exact). Its key joins exact, the +// entry goes and the lookup makes it again whole, uploaded from its storage as a texture seen for the first time. A +// lookup that is out of its creation budget leaves the stand-in for now, as for any new texture. Texture lock held. +const VkTexImage *vk_texture_exact_by_id_locked(int id, view_t *view, int *has_view) { + if (id < 0 || id >= ntslots || !tslots[id].used) return NULL; + TexSlot *s = &tslots[id]; + Tex *e = s->ce && s->cgen == agen[sinfo[id] & 0xffff] ? s->ce : NULL; // what the caller's lookup just resolved + if (e && e->pub.skip) { + if (exact.n >= EXACT_MAX) nvn_hm_clear(&exact, NULL); // never seen; they would be found again one by one + nvn_hm_put(&exact, &e->key, (void *)(uintptr_t)1); + n_exact++; + static volatile int32_t n_logged; + log_limited(&n_logged, "texture detail: texture %#llx (fmt %#x %ux%u, %u levels) is read by texel position or its " + "size is asked for: made whole again (%llu so far)", (unsigned long long)e->pub.addr, e->t.d.format, + e->t.d.width, e->t.d.height, e->pub.levels + e->pub.skip, (unsigned long long)n_exact); + destroy_tex(e); + } + return vk_texture_by_id_locked(id, view, has_view); +} + // vk_dump: the swizzle selectors of the descriptor registered in slot id; 0 when the slot is empty. int vk_texture_slot_swz(int id, uint8_t swz[4]) { lock(); @@ -2804,8 +2957,8 @@ static int texels_read_record(Tex *e, const Region *g, const NVNcopyRegion *r, T log_limited(&n_ds, "texels of packed depth-stencil format %#x are not copied", e->t.d.format); return 0; } - if (g->level >= e->pub.levels) return 0; - uint32_t w = (uint32_t)r->width, h = (uint32_t)r->height; + if (g->level < e->pub.skip || g->level - e->pub.skip >= e->pub.levels) return 0; // a level the image lacks + uint32_t w = (uint32_t)r->width, h = (uint32_t)r->height, il = g->level - e->pub.skip; t->w = w; t->ib = e->f.expand_rgb ? (size_t)g->bpb / 3 * 4 : 0; t->slice = t->ib ? (size_t)w * h * t->ib : g->wb * g->hb; @@ -2814,7 +2967,7 @@ static int texels_read_record(Tex *e, const Region *g, const NVNcopyRegion *r, T if (!rg) rt_fatal("vk: out of memory for a texture read"); for (int z = 0; z < g->nz; z++) rg[z] = (VkBufferImageCopy){.bufferOffset = t->slice * (size_t)z, - .imageSubresource = {aspect, g->level, g->is3d ? 0 : g->layer0 + (uint32_t)r->zoffset + (uint32_t)z, 1}, + .imageSubresource = {aspect, il, g->is3d ? 0 : g->layer0 + (uint32_t)r->zoffset + (uint32_t)z, 1}, .imageOffset = {r->xoffset, r->yoffset, g->is3d ? r->zoffset + z : 0}, .imageExtent = {w, h, 1}}; VkCommandBuffer cb = vk_upload_begin(); @@ -2857,7 +3010,11 @@ static int image_texels(Tex *e, const Region *g, const NVNcopyRegion *r, uint8_t log_limited(&n_ds, "texels of packed depth-stencil format %#x are not copied", e->t.d.format); return 0; } - if (g->level >= e->pub.levels) return 0; + // Texture detail: a level the image was made without takes no texels (the storage holds them, vk_write_texels); the + // others are the image's level g->level - skip, the rectangle unchanged. + if (g->level < e->pub.skip) return 1; + uint32_t il = g->level - e->pub.skip; + if (il >= e->pub.levels) return 0; uint32_t w = (uint32_t)r->width, h = (uint32_t)r->height; int rgb = e->f.expand_rgb, astc = e->f.astc_bw != 0, agpu = astc && vk_astc_gpu(); size_t texels = (size_t)w * h, ib = agpu ? 0 : astc ? 4 : rgb ? (size_t)g->bpb / 3 * 4 : 0; @@ -2884,7 +3041,7 @@ static int image_texels(Tex *e, const Region *g, const NVNcopyRegion *r, uint8_t for (size_t y = 0; y < g->hb; y++) memcpy(s + y * g->wb, m + y * g->row, g->wb); } rg[z] = (VkBufferImageCopy){.bufferOffset = slice * (size_t)z, - .imageSubresource = {aspect, g->level, g->is3d ? 0 : g->layer0 + (uint32_t)r->zoffset + (uint32_t)z, 1}, + .imageSubresource = {aspect, il, g->is3d ? 0 : g->layer0 + (uint32_t)r->zoffset + (uint32_t)z, 1}, .imageOffset = {r->xoffset, r->yoffset, g->is3d ? r->zoffset + z : 0}, .imageExtent = {w, h, 1}}; } @@ -3007,8 +3164,8 @@ static void write_back_n(Tex **v, int n) { Tex *e = v[i]; const nvn_texdesc *d = &e->t.d; int linear = nvn_tex_is_linear(d), is3d = d->target == NVN_TEXTURE_TARGET_3D; - uint32_t levels = linear ? 1 : e->pub.levels, layers = linear || is3d ? 1 : e->pub.layers; - for (uint32_t lv = 0; lv < levels; lv++) + uint32_t levels = linear ? 1 : e->pub.levels + e->pub.skip, layers = linear || is3d ? 1 : e->pub.layers; + for (uint32_t lv = linear ? 0 : e->pub.skip; lv < levels; lv++) // the game's levels the image holds for (uint32_t ly = 0; ly < layers; ly++) { if (np == cap) { cap = cap ? cap * 2 : 16; @@ -3338,7 +3495,7 @@ void vk_sampler_register(int id, const samp_par *s) { sslots = p, nsslots = n; } sslots[id].p = *s; - sslots[id].s = sslots[id].sp = sslots[id].sc = VK_NULL_HANDLE; + sslots[id].s = sslots[id].sp = sslots[id].sc = sslots[id].sd[0] = sslots[id].sd[1] = VK_NULL_HANDLE; sslots[id].used = 1; samp_gen++; unlock(); @@ -3360,7 +3517,8 @@ void vk_sampler_dlss_bias(float b) { rt_log("vk: texture LOD bias %.2f on the scene's tiled textures (the upscaler's render scale; was %.2f)", b, dlss_bias); dlss_bias = b; - for (int i = 0; i < nsslots; i++) sslots[i].s = sslots[i].sp = sslots[i].sc = VK_NULL_HANDLE; // looked up again (the key moves) + for (int i = 0; i < nsslots; i++) // looked up again (the key moves) + sslots[i].s = sslots[i].sp = sslots[i].sc = sslots[i].sd[0] = sslots[i].sd[1] = VK_NULL_HANDLE; samp_gen++; } unlock(); @@ -3381,7 +3539,8 @@ void vk_sampler_aniso_live(void) { if (sf.init && a != sf.aniso) { sf.aniso = a; aniso_log(); - for (int i = 0; i < nsslots; i++) sslots[i].s = sslots[i].sp = sslots[i].sc = VK_NULL_HANDLE; // looked up again (the key moves) + for (int i = 0; i < nsslots; i++) // looked up again (the key moves) + sslots[i].s = sslots[i].sp = sslots[i].sc = sslots[i].sd[0] = sslots[i].sd[1] = VK_NULL_HANDLE; samp_gen++; } unlock(); @@ -3464,6 +3623,35 @@ VkSampler vk_sampler_compare_by_id_locked(int id) { return s->sc; } +// Texture detail: a texture made without its top `skip` levels has the game's level i + skip at its level i, and the +// LOD the device works out for it is the game's less skip, so the level a sample lands on is the game's wherever that +// level is kept. A sampler's LOD clamp counts the game's levels and has to move down with them: a clamp to levels 0-2 +// left alone would let the image's level 2 through, the game's level 4. The bias is relative and stays. A clamp that is +// open (from 0, up to more levels than a texture has) is the same sampler. Only draws that bind texture and sampler +// as one resource come here (vk_exec_draw.c tex_info_full); the sampler pool's entries serve every texture and keep +// the game's clamp. +VkSampler vk_sampler_detail_by_id_locked(int id, uint32_t skip) { + VkCtx *c = vk_ctx(); + if (!skip || skip > 2) return vk_sampler_by_id_locked(id); + if (!c || id < 0 || id >= nsslots || !sslots[id].used) return VK_NULL_HANDLE; + SampSlot *s = &sslots[id]; + if (!s->sd[skip - 1]) { + samp_par k = s->p; + if (k.lod_min > 0) k.lod_min = fmaxf(k.lod_min - (float)skip, 0); + if (k.lod_max < 32) k.lod_max = fmaxf(k.lod_max - (float)skip, 0); // 32 and more: above any texture's last level + if (k.lod_min == s->p.lod_min && k.lod_max == s->p.lod_max) { + s->sd[skip - 1] = vk_sampler_by_id_locked(id); + } else { + static int told; // a clamp that leaves levels out; one up to a texture's last level changes nothing + if ((s->p.lod_min > 0 || s->p.lod_max < 8) && told++ < 8) + rt_log("vk: texture detail: sampler slot %d clamps the LOD to %g..%g; %g..%g for a texture made without %u " + "levels", id, s->p.lod_min, s->p.lod_max, k.lod_min, k.lod_max, skip); + s->sd[skip - 1] = sampler_for(c, &k); + } + } + return s->sd[skip - 1]; +} + VkSampler vk_sampler_by_id(int id) { lock(); VkSampler v = vk_sampler_by_id_locked(id); @@ -3517,6 +3705,7 @@ void vk_texture_census_log(uint64_t frame) { enum { K_BC3, K_RGBA8, K_CPU, K_GPU, K_N }; static const char *const kn[K_N] = {"ASTC as BC3", "ASTC as RGBA8", "other CPU-written", "GPU-written"}; uint64_t n[K_N] = {0}, b[K_N] = {0}, in[K_N] = {0}, ib[K_N] = {0}, bound = 0, dup_n = 0, dup_b = 0; + uint64_t sk_n[3] = {0}, sk_b[3] = {0}; // texture detail: entries made without 1 and 2 levels, and their memory struct { int fmt; uint64_t n, b; } fm[48]; int nfm = 0; struct { uint64_t b; uint32_t w, h, d, levels; int fmt, gpu; } big[4] = {{0}}; // copied: read after unlock @@ -3531,6 +3720,7 @@ void vk_texture_census_log(uint64_t frame) { if (!e->bytes) { bound += e->bound_to != NULL; continue; } int k = !comes_back_intact(e) ? K_GPU : e->f.astc_bw ? (e->f.astc_bc ? K_BC3 : K_RGBA8) : K_CPU; n[k]++, b[k] += e->bytes; + if (e->pub.skip < 3) sk_n[e->pub.skip]++, sk_b[e->pub.skip] += e->bytes; if (idle(e, now)) in[k]++, ib[k] += e->bytes; int j = 0; while (j < nfm && fm[j].fmt != (int)e->f.vk) j++; @@ -3576,8 +3766,13 @@ void vk_texture_census_log(uint64_t frame) { for (int k = 0; k < K_N; k++) o += snprintf(line + o, sizeof line - (size_t)o, " %s %llu %.0f MiB (%llu %.0f MiB);", kn[k], (unsigned long long)n[k], b[k] / M, (unsigned long long)in[k], ib[k] / M); - snprintf(line + o, sizeof line - (size_t)o, " bound into pool memory %llu; sharing an address with another %llu %.0f MiB", - (unsigned long long)bound, (unsigned long long)dup_n, dup_b / M); + o += snprintf(line + o, sizeof line - (size_t)o, " bound into pool memory %llu; sharing an address with another %llu %.0f MiB", + (unsigned long long)bound, (unsigned long long)dup_n, dup_b / M); + if (detail_most()) // nothing more in the line at full detail + snprintf(line + o, sizeof line - (size_t)o, "; texture detail: made without their top level %llu %.0f MiB, without " + "two %llu %.0f MiB, made whole again for a shader %llu (%u shaders loaded read a texture by texel " + "position or size)", (unsigned long long)sk_n[1], sk_b[1] / M, (unsigned long long)sk_n[2], sk_b[2] / M, + (unsigned long long)n_exact, gfx_shader_exact_seen()); rt_log("%s", line); for (int x = 0; x < nfm; x++) // by bytes, largest first for (int y = x + 1; y < nfm; y++) From 364a40ae9b4c7425a6368523715516fcc8e29e07 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 09:38:08 -0500 Subject: [PATCH 2/3] Add the Texture detail row to the settings menu's Screen tab, in the four menu languages. The row offers Automatic, Full, Reduced and Low, names the level Automatic picks on this card, and notes that a change takes effect after a restart. Co-Authored-By: Claude Fable 5.2 --- docs/README-BuildKit.md | 4 +++- runtime/ui/menu.cpp | 15 +++++++++++++++ runtime/ui_text.c | 19 +++++++++++++++++++ runtime/ui_text.h | 3 +++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/docs/README-BuildKit.md b/docs/README-BuildKit.md index 04cb8871..348a3415 100644 --- a/docs/README-BuildKit.md +++ b/docs/README-BuildKit.md @@ -144,7 +144,9 @@ beside the game. "Preparing shaders" once while the driver rebuilds them. With a driver that has Vulkan's graphics pipeline library it lasts at most about 20 seconds once the shaders are translated, and the remaining pipelines are built in the background while you play. -- **Screen** in the menu has Texture filtering (anisotropic, up to 16x, at once), Upscaling (DLSS on NVIDIA RTX cards, +- **Screen** in the menu has Texture filtering (anisotropic, up to 16x, at once), Texture detail (Reduced and Low + leave out the largest versions of the big surface textures so that the game fits the video memory of 6 and 8 GB + cards; Automatic picks by the card), Upscaling (DLSS on NVIDIA RTX cards, FSR 3.1 on any card where `amd_fidelityfx_vk.dll` loads), Frame generation (NVIDIA DLSS on RTX 40 and 50 cards, with a DLSS multiplier of 2x, or 3x and 4x on RTX 50 cards; FSR 3.1 on any card where it loads; it needs DLSS or FSR 3.1 upscaling on, and the frame limit then counts the frames on screen; with no limit chosen it is the display's refresh diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index 48cf9f29..27cf6810 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -71,6 +71,7 @@ int vk_ngxfg_wanted(void); // vk_dlaa.c: DLSS's frame generation th int vk_ngxfg_max(void); // ... and how many frames it may generate per rendered one (0: it cannot) int vk_ffx_available(void); // vk_ffx.cpp: FSR 3.1 upscaling can run (asked without loading the library) int vk_ffx_framegen_available(void); // ... and FSR 3.1 frame generation +int vk_texture_detail_auto(void); // vk_texture.c: the texture detail auto picks on this card, 0 full to 2 low double frame_step_limit_hz(void); int frame_limit_follows_display(void); int frame_limit_adjustable(void); @@ -672,6 +673,19 @@ static void row_shadow_res(void) { opt_choice_list("NX2NAVI_SHADOW_RES", UI_MENU_SHADOW_RES, vals, names, N, UI_MENU_SHADOW_RES_DESC); } +// Texture detail (vk_texture.c "texture detail"): large surface textures made without their largest one or two mip +// levels. Read when the first texture is made, so the row notes the restart. The automatic entry names the level it +// picks on this card. +static void row_texture_detail(void) { + static const char *const vals[] = {"auto", "full", "reduced", "low"}; + static const UiTextId level[] = {UI_MENU_TEX_DETAIL_FULL, UI_MENU_TEX_DETAIL_REDUCED, UI_MENU_TEX_DETAIL_LOW}; + static char picked[96]; // the row keeps the pointer for the frame + int a = vk_texture_detail_auto(); + snprintf(picked, sizeof picked, menu_text(UI_MENU_TEX_DETAIL_AUTO), menu_text(level[a < 0 || a > 2 ? 0 : a])); + const char *names[] = {picked, menu_text(level[0]), menu_text(level[1]), menu_text(level[2])}; + opt_choice_list("NX2NAVI_TEXTURE_DETAIL", UI_MENU_TEX_DETAIL, vals, names, 4, UI_MENU_TEX_DETAIL_DESC); +} + static void row_shadow_scale(void) { static const char *const vals[] = {"1", "2", "3", "4"}; static const char *const names[] = {"960 (1x)", "1920 (2x)", "2880 (3x)", "3840 (4x)"}; @@ -1061,6 +1075,7 @@ static void tab_screen(float scale) { static const char *const aniso[] = {"game", "2", "4", "8", "16"}; const char *aniso_names[] = {menu_text(UI_MENU_ANISO_GAME), "2x", "4x", "8x", "16x"}; opt_choice_list("NX2NAVI_ANISO", UI_MENU_ANISO, aniso, aniso_names, 5, UI_MENU_ANISO_DESC); + row_texture_detail(); opt_end(); } diff --git a/runtime/ui_text.c b/runtime/ui_text.c index 14f09063..d121a208 100644 --- a/runtime/ui_text.c +++ b/runtime/ui_text.c @@ -84,6 +84,10 @@ static const char *const text_en[UI_TEXT_COUNT] = { [UI_MENU_ANISO] = "Texture filtering", [UI_MENU_ANISO_DESC] = "Anisotropic filtering of surface textures: ground, roads and walls seen at a shallow angle stay sharp into the distance instead of blurring. Game: the level the game asks for, as on the Switch.", [UI_MENU_ANISO_GAME] = "Game", + [UI_MENU_TEX_DETAIL] = "Texture detail", + [UI_MENU_TEX_DETAIL_DESC] = "Sharpness of large surface textures. Reduced and Low leave out their largest versions, so that an area fits the video memory of 6 and 8 GB cards; only surfaces seen up close look softer. Automatic picks by the card's video memory.", + [UI_MENU_TEX_DETAIL_AUTO] = "Automatic: %s", + [UI_MENU_TEX_DETAIL_FULL] = "Full", [UI_MENU_TEX_DETAIL_REDUCED] = "Reduced", [UI_MENU_TEX_DETAIL_LOW] = "Low", [UI_MENU_FPS_BOOST] = "Experimental FPS boost", [UI_MENU_FPS_BOOST_DESC] = "What the game's shaders write into its memory is copied back from the video card only " "when the game's own code needs it, instead of after every step of a frame. About a " @@ -648,6 +652,10 @@ static const char *const text_ru[UI_TEXT_COUNT] = { [UI_MENU_ANISO] = "Фильтрация текстур", [UI_MENU_ANISO_DESC] = "Анизотропная фильтрация текстур поверхностей: земля, дороги и стены под острым углом остаются чёткими вдали, а не размываются. Игра: уровень, который запрашивает игра, как на Switch.", [UI_MENU_ANISO_GAME] = "Игра", + [UI_MENU_TEX_DETAIL] = "Детализация текстур", + [UI_MENU_TEX_DETAIL_DESC] = "Чёткость больших текстур поверхностей. «Сниженная» и «Низкая» не загружают их самые крупные версии, чтобы область помещалась в видеопамять карт на 6 и 8 ГБ; мягче выглядят только поверхности вблизи. «Авто» выбирает по объёму видеопамяти.", + [UI_MENU_TEX_DETAIL_AUTO] = "Авто: %s", + [UI_MENU_TEX_DETAIL_FULL] = "Полная", [UI_MENU_TEX_DETAIL_REDUCED] = "Сниженная", [UI_MENU_TEX_DETAIL_LOW] = "Низкая", [UI_MENU_FPS_BOOST] = "Экспериментальное ускорение FPS", [UI_MENU_FPS_BOOST_DESC] = "То, что шейдеры игры записывают в её память, копируется с видеокарты обратно только " "тогда, когда это нужно коду самой игры, а не после каждого этапа кадра. Примерно на " @@ -1458,6 +1466,13 @@ static const char *const text_pt_br[UI_TEXT_COUNT] = { "ângulo raso continuam nítidos ao longe em vez de borrar. Jogo: o nível que o jogo pede, " "como no Switch.", [UI_MENU_ANISO_GAME] = "Jogo", + [UI_MENU_TEX_DETAIL] = "Detalhe das texturas", + [UI_MENU_TEX_DETAIL_DESC] = "Nitidez das texturas grandes de superfície. Reduzido e Baixo deixam de fora as versões " + "maiores delas, para que uma área caiba na memória de vídeo de placas de 6 e 8 GB; só as " + "superfícies vistas de perto ficam mais suaves. Automático escolhe pela memória de vídeo " + "da placa.", + [UI_MENU_TEX_DETAIL_AUTO] = "Automático: %s", + [UI_MENU_TEX_DETAIL_FULL] = "Completo", [UI_MENU_TEX_DETAIL_REDUCED] = "Reduzido", [UI_MENU_TEX_DETAIL_LOW] = "Baixo", [UI_MENU_FPS_BOOST] = "Aumento de FPS experimental", [UI_MENU_FPS_BOOST_DESC] = "O que os shaders do jogo escrevem na memória dele só é copiado de volta da placa de " "vídeo quando o próprio código do jogo precisa, em vez de após cada etapa do quadro. " @@ -2142,6 +2157,10 @@ static const char *const text_zh_hans[UI_TEXT_COUNT] = { [UI_MENU_ANISO] = "纹理过滤", [UI_MENU_ANISO_DESC] = "表面纹理的各向异性过滤:以小角度看到的地面、道路和墙壁在远处依然清晰,而不会模糊。游戏:使用游戏请求的级别,与 Switch 上相同。", [UI_MENU_ANISO_GAME] = "游戏", + [UI_MENU_TEX_DETAIL] = "纹理细节", + [UI_MENU_TEX_DETAIL_DESC] = "大型表面纹理的清晰度。“降低”和“低”不加载它们最大的版本,使一个区域能放进 6 GB 和 8 GB 显卡的显存;只有近处的表面看起来略微柔和。“自动”根据显卡的显存选择。", + [UI_MENU_TEX_DETAIL_AUTO] = "自动:%s", + [UI_MENU_TEX_DETAIL_FULL] = "完整", [UI_MENU_TEX_DETAIL_REDUCED] = "降低", [UI_MENU_TEX_DETAIL_LOW] = "低", [UI_MENU_FPS_BOOST] = "实验性帧率提升", [UI_MENU_FPS_BOOST_DESC] = "游戏着色器写入其内存的数据,只有在游戏自身的代码需要时才从显卡复制回来,而不是在每帧的每个阶段之后都" "复制。在大型开放区域中帧率提高约三分之一。实验性功能:开启后个别帧可能闪烁。默认关闭。", diff --git a/runtime/ui_text.h b/runtime/ui_text.h index 9b45edf2..6e185527 100644 --- a/runtime/ui_text.h +++ b/runtime/ui_text.h @@ -41,6 +41,9 @@ typedef enum { UI_MENU_FOV, UI_MENU_FOV_DESC, UI_MENU_SHADOW_RES, UI_MENU_SHADOW_RES_DESC, UI_MENU_SHADOW_RES_GAME, UI_MENU_ANISO, UI_MENU_ANISO_DESC, UI_MENU_ANISO_GAME, + UI_MENU_TEX_DETAIL, UI_MENU_TEX_DETAIL_DESC, + UI_MENU_TEX_DETAIL_AUTO, // "Automatic: %s", %s the level it picks on this card + UI_MENU_TEX_DETAIL_FULL, UI_MENU_TEX_DETAIL_REDUCED, UI_MENU_TEX_DETAIL_LOW, UI_MENU_FPS_BOOST, UI_MENU_FPS_BOOST_DESC, UI_MENU_SHARPNESS, UI_MENU_SHARPNESS_DESC, UI_MENU_SHARPNESS_NEEDS_FSR, UI_MENU_COLOR, // group heading over the colour rows From e3f623942bae1bb3f33062e76c81a5e5f620894f Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 09:38:08 -0500 Subject: [PATCH 3/3] Check the texture detail in --probe-vkmem: ASTC textures are uploaded and rewritten, and every level and layer of their images must hold the game's level it stands for. The probe takes the level from NX2NAVI_TEXTURE_DETAIL. It covers a 1024x1024 texture, a 256x256 array of three layers, and a 128x128 and an unmipped texture that stay whole, with the images as RGBA8 or BC3, and the sampler LOD clamp of a reduced texture. It passes at full, reduced and low on an RTX 5090, with NX2NAVI_ASTC_BC 0 and 1. Co-Authored-By: Claude Fable 5.2 --- docs/building.md | 3 + runtime/tex_probe.c | 137 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 139 insertions(+), 1 deletion(-) diff --git a/docs/building.md b/docs/building.md index 516edbe0..9dc30332 100644 --- a/docs/building.md +++ b/docs/building.md @@ -340,6 +340,9 @@ Runtime probes, each a mode of `nx2navi.exe` (the list is at the top of `runtime part of `--probe-vkexec` translate shaders of the census below: they look for `build//shaders/jobs.json` of the exe's own version first, then of the other one. `--probe-vkmem` also checks CPU-backed texture uploads and rewrites, GPU-written texture aliases, and memory write watches. +A change to the texture detail (`runtime/vk_texture.c` "texture detail": `detail_skip`, `upload`, `astc_bl_regions`) +passes it three times, with `NX2NAVI_TEXTURE_DETAIL` set to `full`, `reduced` and `low`: each time ASTC textures are +uploaded and rewritten, and every level and layer of their images must hold the game's level it stands for. `--probe-prewarm` runs the boot's pipeline stages without the game, from the caches next to the exe (it skips when there are none): the pipeline cache load, the shader prewarm, the recorded pipelines and the compute pipeline prebuild, with a `mem: boot stage` log line after each that gives the device memory and the process commit so far. It then asks diff --git a/runtime/tex_probe.c b/runtime/tex_probe.c index f7c64eb6..49d85853 100644 --- a/runtime/tex_probe.c +++ b/runtime/tex_probe.c @@ -1,12 +1,14 @@ // nx2navi.exe --probe-vkmem: texture memory checks. The CPU part: block-linear copies against an // independent reference, ASTC void-extent blocks, and the NVN -> VkFormat table against GPU0. The GPU part: // guest heap imported as a VkBuffer, write watch against CPU, GPU and ReadFile writes, the virtual-pool page table, -// and textures uploaded from block-linear storage and read back. +// and textures uploaded from block-linear storage and read back, among them ASTC textures at the texture detail in +// effect (NX2NAVI_TEXTURE_DETAIL=reduced or low to check the levels left out). // The reference swizzler walks the GOBs in storage order and places each byte by bit operations, so it shares no // addressing code with nvn_bl_offset. #include #include #include +#include "cfg.h" #include "host.h" #include "nvn_api.h" #include "tex.h" @@ -659,6 +661,134 @@ static int image_is_color(VkImage image, uint32_t w, uint32_t h, const uint8_t w return same; } +// ---- texture detail (vk_texture.c "texture detail"): an ASTC texture made without its top mip levels +// Every level and layer of t's storage becomes ASTC void-extent blocks of one colour, each channel 0 or 255 so that +// the RGBA8 decode and its BC3 encoding both hold it exactly. The colour says which level and layer it is. +static void detail_color(unsigned lv, unsigned layer, unsigned round, uint8_t c[4]) { + unsigned k = 1 + (lv * 3 + layer + round * 2) % 7; // never black: 1 to 7 + c[0] = k & 1 ? 255 : 0, c[1] = k & 2 ? 255 : 0, c[2] = k & 4 ? 255 : 0, c[3] = 255; +} + +static int detail_fill(const tex_t *t, unsigned layers, unsigned round) { + for (unsigned lv = 0; lv < t->d.levels; lv++) + for (unsigned ly = 0; ly < layers; ly++) { + uint32_t rb, rows, sl, bpb; uint64_t base; unsigned th, td; + GCHECK(!nvn_deswizzle_geom(&t->d, lv, ly, &rb, &rows, &sl, &base, &th, &td, &bpb), "ASTC geometry of level %u", lv); + size_t n = (size_t)rb * rows; + uint8_t *tight = malloc(n), c[4]; + detail_color(lv, ly, round, c); + for (size_t i = 0; i < n; i += 16) + void_block(tight + i, (uint16_t)(c[0] * 257), (uint16_t)(c[1] * 257), (uint16_t)(c[2] * 257), 0xFFFF, 0); + ref_swizzle(tight, t->mem + base, rb, rows, 1, th, td); + free(tight); + } + return 0; +} + +// Whether level lv, layer ly of image (RGBA8 or BC3, w x h at that level) is `want` in every texel. +static int detail_level_is(const VkTexImage *img, uint32_t lv, uint32_t ly, uint32_t w, uint32_t h, const uint8_t want[4]) { + VkCtx *c = vk_ctx(); + int bc = img->format == VK_FORMAT_BC3_UNORM_BLOCK || img->format == VK_FORMAT_BC3_SRGB_BLOCK; + size_t n = bc ? (size_t)((w + 3) / 4) * ((h + 3) / 4) * 16 : (size_t)w * h * 4; + VkBuffer rb; + VmaAllocation ra; + uint8_t *r = vk_staging(n, &rb, &ra); + VkCommandBuffer cb = vk_upload_begin(); + VkMemoryBarrier2 mb = {.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, .srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, + .srcAccessMask = VK_ACCESS_2_MEMORY_WRITE_BIT, .dstStageMask = VK_PIPELINE_STAGE_2_COPY_BIT, + .dstAccessMask = VK_ACCESS_2_TRANSFER_READ_BIT}; + VkDependencyInfo di = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .memoryBarrierCount = 1, .pMemoryBarriers = &mb}; + vkCmdPipelineBarrier2(cb, &di); + VkBufferImageCopy rg = {.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, lv, ly, 1}, .imageExtent = {w, h, 1}}; + vkCmdCopyImageToBuffer(cb, img->image, VK_IMAGE_LAYOUT_GENERAL, rb, 1, &rg); + mb = (VkMemoryBarrier2){.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, .srcStageMask = VK_PIPELINE_STAGE_2_COPY_BIT, + .srcAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT, .dstStageMask = VK_PIPELINE_STAGE_2_HOST_BIT, + .dstAccessMask = VK_ACCESS_2_HOST_READ_BIT}; + vkCmdPipelineBarrier2(cb, &di); + vk_upload_end(); + vk_upload_flush(1); + vmaInvalidateAllocation(c->vma, ra, 0, VK_WHOLE_SIZE); + int same = 1; + if (bc) { // BC3: two 5:6:5 endpoints at bytes 8 to 11; both are the colour, so every texel is + uint16_t e = (uint16_t)((want[0] >> 3) << 11 | (want[1] >> 2) << 5 | want[2] >> 3); + for (size_t i = 0; i < n; i += 16) { + uint16_t e0, e1; + memcpy(&e0, r + i + 8, 2), memcpy(&e1, r + i + 10, 2); + same &= e0 == e && e1 == e; + } + } else { + for (size_t i = 0; i < n; i += 4) same &= !memcmp(r + i, want, 4); + } + vmaDestroyBuffer(c->vma, rb, ra); + return same; +} + +// The image of an ASTC texture has the game's levels from the skip the setting gives it: its size is that level's, and +// each of its levels and layers holds that level's blocks, after the upload and after a rewrite of the storage. +static int detail_case(pool_t *pool, uint64_t off, uint64_t room, uint32_t size, uint32_t levels, unsigned layers, + uint64_t serial, uint32_t want_skip) { + tex_t t = make_tex(pool, off, 0x80, size, size, levels, serial); // ASTC 8x8 + if (layers > 1) { + t.d.target = NVN_TEXTURE_TARGET_2D_ARRAY, t.d.depth = layers; + nvn_tex_layout(&t.d, &t.L); + } + GCHECK(t.L.size <= room && off + room <= pool->size, "ASTC %ux%u storage of %llu bytes does not fit its %llu in the " + "probe's pool", size, size, (unsigned long long)t.L.size, (unsigned long long)room); + for (unsigned round = 0; round < 2; round++) { + if (detail_fill(&t, layers, round)) return 1; + const VkTexImage *img = vk_texture_get(&t); + GCHECK(img && img->mirrored, "ASTC %ux%u texture image", size, size); + if (img->format != VK_FORMAT_R8G8B8A8_UNORM && img->format != VK_FORMAT_BC3_UNORM_BLOCK) return 0; // native ASTC + GCHECK(img->skip == want_skip && img->levels == levels - want_skip && img->width == size >> want_skip && + img->height == size >> want_skip && img->layers == layers, + "ASTC %ux%u, %u levels, %u layers: image %ux%u with %u levels, %u layers, made without %u (want %u)", size, size, + levels, layers, img->width, img->height, img->levels, img->layers, img->skip, want_skip); + for (uint32_t lv = 0; lv < img->levels; lv++) + for (uint32_t ly = 0; ly < layers; ly++) { + uint8_t want[4]; + uint32_t w = size >> (lv + img->skip) ? size >> (lv + img->skip) : 1; + detail_color(lv + img->skip, ly, round, want); + GCHECK(detail_level_is(img, lv, ly, w, w, want), + "ASTC %ux%u made without %u levels: image level %u layer %u is not the game's level %u (%s)", size, size, + img->skip, lv, ly, lv + img->skip, round ? "after a rewrite of the storage" : "first upload"); + g_gpu_checks++; + } + } + vk_texture_release(&t); + return 0; +} + +static int test_detail(pool_t *pool) { + const char *v = cfg_value("NX2NAVI_TEXTURE_DETAIL"); + int most = !strcmp(v, "full") ? 0 : !strcmp(v, "reduced") ? 1 : !strcmp(v, "low") ? 2 : vk_texture_detail_auto(); + // 1024x1024 with 6 levels (two levels less at low), a 256x256 array of 3 layers (one less below full), and the + // ones the rule leaves whole: 128x128, and 512x512 without smaller levels + if (detail_case(pool, 0, 0x80000, 1024, 6, 1, 20, most > 1 ? 2 : (uint32_t)most) || + detail_case(pool, 0x80000, 0x40000, 256, 4, 3, 21, most ? 1 : 0) || + detail_case(pool, 0xc0000, 0x10000, 128, 4, 1, 22, 0) || detail_case(pool, 0xd0000, 0x20000, 512, 1, 1, 23, 0)) + return 1; + // the sampler of a texture made without levels: an open LOD clamp is the slot's sampler, a closed one moves down + samp_par sp; + memset(&sp, 0, sizeof sp); + sp.minf = NVN_MIN_FILTER_LINEAR_MIPMAP_LINEAR, sp.magf = NVN_MAG_FILTER_LINEAR, sp.ws = sp.wt = sp.wr = NVN_WRAP_MODE_REPEAT; + sp.lod_max = 1000; + vk_sampler_register(2, &sp); + sp.lod_min = 1, sp.lod_max = 4; + vk_sampler_register(3, &sp); + sp.lod_min = 0, sp.lod_max = 3; + vk_texture_lock(); + VkSampler open1 = vk_sampler_detail_by_id_locked(2, 1), shut1 = vk_sampler_detail_by_id_locked(3, 1); + vk_texture_unlock(); + GCHECK(open1 && open1 == vk_sampler_by_id(2), "the sampler of a reduced texture with an open LOD clamp is not the slot's"); + GCHECK(shut1 && shut1 != vk_sampler_by_id(3) && shut1 == vk_sampler_for(&sp), + "the sampler of a reduced texture with LOD 1..4 does not clamp to 0..3"); + g_gpu_checks += 2; + printf("probe-vkmem: texture detail %s: ASTC images hold the game's levels from the one they start at, after upload " + "and re-upload (1024x1024, a 256x256 array, and 128x128 and unmipped left whole); sampler LOD clamps follow\n", + most == 0 ? "full" : most == 1 ? "reduced" : "low"); + return 0; +} + static int test_textures(pool_t *tpool, pool_t *phys) { uint8_t *lin[4] = {0}; // block-linear RGBA8 with 3 levels in a CPU-visible pool: uploaded through the GPU, read back from the image @@ -753,6 +883,11 @@ static int test_gpu(void) { if (test_import(&pool) || test_readfile(pool.mem + 0x38000) || test_virtual(&phys, &virt)) return 1; GCHECK(vk_pool_init(&tpool), "vk_pool_init of the texture pool"); if (test_textures(&tpool, &phys)) return 1; + pool_t dpool = {.magic = NVN_POOL_MAGIC, .flags = 0x22, .mem = h + 0x500000, .size = 0x100000, .game_mem = h + 0x500000}; + GCHECK(vk_pool_init(&dpool), "vk_pool_init of the texture detail pool"); + int detail_bad = test_detail(&dpool); + vk_pool_fini(&dpool); + if (detail_bad) return 1; VkMemStats st; vk_mem_stats(&st); GCHECK(st.imported_pools == 2 && st.physical_pools == 1 && st.virtual_pools == 1 && !st.import_failures,