mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-20 16:49:58 -04:00
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 <noreply@anthropic.com>
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user