mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-16 09:39:58 -04:00
A shader whose texture pool array has 32768 entries reads slot 32767 for every texture the game numbers from 32768 up (Ocarina's pool has 33068), but nothing in the log said such a shader was in use: vk_texture_register warned only for slots past VK_POOL_TEX (40960), which Ocarina never registers, and vk_pipeline.c spirv_pool_rebind accepts the shorter arrays silently. gfx_shader.c pool_short_note now counts the shaders entering the memory cache with such an array (carry_usable keeps carried ones out, so these come from a translator older than the runtime) and logs the first; vk_texture_register logs once, with that count, when a slot between 32768 and 40959 is registered while one is in use. The count is kept per shader in gfx_shader.c (gfx_shader_pool_short_seen) rather than per pipeline stage in spirv_pool_rebind, which runs once per module built. Diagnostics only. tests/carry_pool_check.py now also checks that pool_short_note counts only shaders of the earlier length and logs once; every runtime file compiles, with no new warnings in gfx_shader.c and vk_texture.c. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
206 lines
8.3 KiB
Python
206 lines
8.3 KiB
Python
"""Check runtime/gfx_shader.c's carry refusal: a previous translator's translation whose texture pool array has the
|
|
earlier length (32768, vk.h VK_POOL_TEX_CARRIED) is never served, because its clamp draws every texture numbered from
|
|
32768 up with slot 32767. The check cuts pool_short, carry_usable, carry_load, carry_find and carry_find_iid out of
|
|
gfx_shader.c, compiles them against stand-ins for the disk and memory caches, and walks the lookups: a refused entry
|
|
is freed and the lookup goes on to the next previous identity, the owned branch (gfx_shader_load_private) frees it as
|
|
well, and entries with the current length, the sampler pool or no pool are served as before. pool_short_note, which
|
|
counts the shaders of the earlier length that enter the memory cache for vk_texture.c's warning, counts those only.
|
|
|
|
No GPU, no game files, any Python.
|
|
|
|
Usage: carry_pool_check.py
|
|
"""
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
|
|
|
|
|
|
def compiler():
|
|
"""$CC, else MSYS2 UCRT64's clang, else clang on PATH."""
|
|
if os.environ.get('CC'):
|
|
return os.environ['CC']
|
|
if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists():
|
|
return str(MSYS_BIN / 'clang.exe')
|
|
return shutil.which('clang') or 'clang'
|
|
|
|
|
|
HARNESS = r'''
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
#include "gfx.h"
|
|
|
|
typedef struct GfxShader {
|
|
GfxShaderRes *res[GFX_RES_KINDS];
|
|
int nres[GFX_RES_KINDS];
|
|
int stage;
|
|
uint8_t carried;
|
|
int pool_len; /* the stand-in disk entry's texture pool length, -1 none */
|
|
int keyj; /* the previous identity whose key it was read under */
|
|
} GfxShader;
|
|
typedef struct { uint32_t slots, attrs, fouts; } UseMask;
|
|
|
|
|
|
static struct {
|
|
int n_prev;
|
|
uint8_t prev[8][32], prev_eff[8][32];
|
|
volatile int64_t refused;
|
|
} C;
|
|
static int carry_on(void) { return C.n_prev > 0; }
|
|
static int64_t plat_inc64(volatile int64_t *p) { return ++*p; }
|
|
static int32_t plat_inc32(volatile int32_t *p) { return ++*p; }
|
|
static int logs;
|
|
static char last_log[512];
|
|
void rt_log(const char *fmt, ...) {
|
|
logs++;
|
|
snprintf(last_log, sizeof last_log, "%s", fmt);
|
|
}
|
|
|
|
/* The disk cache: entry j of the previous identities (key byte 0 = j) holds a shader whose texture pool array has
|
|
disk_len[j] entries (0: no entry), of stage disk_stage[j]. */
|
|
static int disk_len[8], disk_stage[8], loads, frees, live;
|
|
static GfxShader *mem; /* the memory cache: at most one shader here */
|
|
static void key_of_input_id(const uint8_t id[32], const uint8_t iid[32], uint8_t key[32]) {
|
|
memset(key, 0, 32);
|
|
key[0] = id[0];
|
|
}
|
|
static void iid_of(const uint8_t h[32], const GfxShaderAnswers *a, const UseMask *m, uint8_t iid[32]) { memset(iid, 0, 32); }
|
|
static int use_lookup(const uint8_t id[32], const uint8_t h[32], UseMask *m) { return 1; }
|
|
static GfxShader *mem_find(const uint8_t key[32]) { return mem && mem->keyj == key[0] ? mem : NULL; }
|
|
static GfxShader *mem_insert(GfxShader *s) { mem = s; return s; }
|
|
static void shader_free(GfxShader *s) { frees++, live--; free(s->res[GFX_RES_TEX]); free(s); }
|
|
static GfxShader *disk_load(const uint8_t key[32], const uint8_t *iid) {
|
|
int j = key[0];
|
|
if (!disk_len[j]) return NULL;
|
|
loads++, live++;
|
|
GfxShader *s = calloc(1, sizeof *s);
|
|
s->stage = disk_stage[j];
|
|
s->keyj = j;
|
|
s->pool_len = disk_len[j];
|
|
s->res[GFX_RES_TEX] = calloc(3, sizeof(GfxShaderRes));
|
|
s->nres[GFX_RES_TEX] = 3;
|
|
s->res[GFX_RES_TEX][0] = (GfxShaderRes){.slot = 2, .count = 1}; /* a bound texture */
|
|
s->res[GFX_RES_TEX][1] = (GfxShaderRes){.slot = -1, .count = 4096, .pool = 2}; /* the sampler pool */
|
|
if (disk_len[j] > 0) s->res[GFX_RES_TEX][2] = (GfxShaderRes){.slot = -1, .count = (uint16_t)disk_len[j], .pool = 1};
|
|
else s->nres[GFX_RES_TEX] = 2;
|
|
return s;
|
|
}
|
|
'''
|
|
|
|
MAIN = r'''
|
|
static void reset(void) {
|
|
if (mem) shader_free(mem), mem = NULL;
|
|
for (int j = 0; j < 8; j++) disk_len[j] = 0, disk_stage[j] = 1;
|
|
C.refused = 0, logs = 0, loads = 0, frees = 0, live = 0;
|
|
}
|
|
|
|
int main(void) {
|
|
uint8_t h[32] = {0}, iid[32] = {0};
|
|
GfxShaderAnswers a = {0};
|
|
C.n_prev = 2;
|
|
C.prev[0][0] = 0x4d, C.prev_eff[0][0] = 0; /* key byte 0 = identity index */
|
|
C.prev[1][0] = 0x9c, C.prev_eff[1][0] = 1;
|
|
|
|
/* The earlier length: refused, freed, nothing served; counted once, logged once. */
|
|
reset();
|
|
disk_len[0] = 32768;
|
|
assert(!carry_find(h, &a, 1, 1));
|
|
assert(loads == 1 && frees == 1 && live == 0 && !mem);
|
|
assert(C.refused == 1 && logs == 1 && strstr(last_log, "texture pool"));
|
|
assert(!carry_find_iid(iid, 1, NULL));
|
|
assert(C.refused == 2 && logs == 1 && live == 0);
|
|
|
|
/* The lookup goes on to the next previous identity, whose translation has the current length. */
|
|
reset();
|
|
disk_len[0] = 32768, disk_len[1] = 40960;
|
|
GfxShader *s = carry_find(h, &a, 1, 1);
|
|
assert(s && s->pool_len == 40960 && s->carried == 2 && mem == s);
|
|
assert(C.refused == 1 && frees == 1 && live == 1);
|
|
|
|
/* The current length, a shader with only the sampler pool and bound textures: served as before. */
|
|
reset();
|
|
disk_len[0] = 40960;
|
|
s = carry_find(h, &a, 1, 1);
|
|
assert(s && s->carried == 1 && C.refused == 0 && logs == 0);
|
|
reset();
|
|
disk_len[0] = -1; /* no texture pool array */
|
|
s = carry_find_iid(iid, 1, NULL);
|
|
assert(s && s->carried == 1 && C.refused == 0);
|
|
|
|
/* A stage mismatch is still refused for its own reason, before the pool is looked at. */
|
|
reset();
|
|
disk_len[0] = 32768, disk_stage[0] = 2;
|
|
assert(!carry_find(h, &a, 1, 1) && C.refused == 0 && frees == 1);
|
|
|
|
/* The owned branch (gfx_shader_load_private): refused and freed, or handed over with *owned 1. */
|
|
reset();
|
|
disk_len[0] = 32768;
|
|
int owned = 0;
|
|
assert(!carry_find_iid(iid, 1, &owned) && !owned && C.refused == 1 && frees == 1 && live == 0);
|
|
reset();
|
|
disk_len[0] = 32768, disk_len[1] = 40960;
|
|
owned = 0;
|
|
s = carry_find_iid(iid, 1, &owned);
|
|
assert(s && owned && s->carried == 2 && !mem && C.refused == 1 && live == 1);
|
|
shader_free(s);
|
|
|
|
/* Memory only (disk 0) never reads the disk. */
|
|
reset();
|
|
disk_len[0] = 32768;
|
|
assert(!carry_find(h, &a, 1, 0) && !carry_find_iid(iid, 0, NULL) && loads == 0);
|
|
|
|
/* pool_short_note (what mem_insert calls for every shader entering the memory cache): only shaders with a pool
|
|
array of the earlier length count, the first is logged, gfx_shader_pool_short_seen gives the count. */
|
|
reset();
|
|
uint8_t k[32] = {0};
|
|
disk_len[0] = 40960, disk_len[1] = 32768, disk_len[2] = -1;
|
|
GfxShader *cur = disk_load(k, NULL);
|
|
k[0] = 1;
|
|
GfxShader *old = disk_load(k, NULL);
|
|
k[0] = 2;
|
|
GfxShader *none = disk_load(k, NULL);
|
|
pool_short_note(cur), pool_short_note(none);
|
|
assert(gfx_shader_pool_short_seen() == 0 && logs == 0);
|
|
pool_short_note(old), pool_short_note(old);
|
|
assert(gfx_shader_pool_short_seen() == 2 && logs == 1 && strstr(last_log, "older than this runtime"));
|
|
shader_free(cur), shader_free(old), shader_free(none);
|
|
|
|
reset();
|
|
printf("carry pool check: earlier-length pool translations refused and freed, the lookup goes on, others served; "
|
|
"shaders of the earlier length in use counted\n");
|
|
return 0;
|
|
}
|
|
'''
|
|
|
|
|
|
def cut(source, start, end):
|
|
i = source.index(start)
|
|
return source[i:source.index(end, i)]
|
|
|
|
|
|
def main():
|
|
src = (ROOT / 'runtime' / 'gfx_shader.c').read_text(encoding='utf-8')
|
|
production = cut(src, '#define POOL_TEX_SHORT', '// Whether the cache holds a previous translator\'s entry of input iid.')
|
|
work = ROOT / 'build' / 'test' / 'carry_pool'
|
|
work.mkdir(parents=True, exist_ok=True)
|
|
c = work / 'check.c'
|
|
c.write_text(HARNESS + production + MAIN, encoding='utf-8')
|
|
exe = work / ('check.exe' if os.name == 'nt' else 'check')
|
|
env = dict(os.environ)
|
|
if os.name == 'nt' and MSYS_BIN.exists():
|
|
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
|
|
subprocess.check_call([compiler(), '-O1', '-g', '-Wall', '-Wno-unused-function', '-I', str(ROOT / 'runtime'),
|
|
str(c), '-o', str(exe)], env=env)
|
|
return subprocess.run([str(exe)], env=env, timeout=60).returncode
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|