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++)