mirror of
https://github.com/seedds/nxnative.git
synced 2026-11-23 11:39:42 -10:00
The NVN serial tables move out of the replay layer into an owner of their own, dm/gfx/nvn/nvn_owner.m, which nvn_mtl.m changes only through its functions, and which a test now runs without nvn_mtl.m: the live table (serial_put, serial_del), the ghost ring of finalized texture serials (tex_put, NGHOST), the live texture objects (mtl_tex_init's part, own_tex_end_locked for a finalize, which tells a copy's finalize from the object's own), texture pins (mtl_tex_pin, mtl_tex_unpin) and program pins (own_prog_pin, mtl_prog_release, mtl_prog_unpin), the live and ghost held counts (serial_table_holds, entry_holds, tex_object_pinned), and renaming an object everywhere (own_rename_locked, the upgrade path's walk over both tables and the pins). The code moved as it was. The owner makes, encodes and releases nothing: what it lets go goes to the replay layer through four hooks nvn_mtl.m provides (own_hook_drop: defer_obj_release / defer_obj_unref, after the submission in flight; own_hook_shared: the sharing table; own_hook_unheld: a retired object waiting for its holders; own_hook_changed: ser_note). Lookups stay borrowed (serial_get, tex_get, inline over own_live_ref / own_resolve_ref): what one returns stays valid until the submission it is encoded in completes, the contract the deferred release already kept; no per-lookup retain. Walks and sizes go through own_each_locked and own_stats_locked (the memory report, the census, nvn_replay); own_check_locked checks the held counts against the tables and pins. The lock stays g_smu, owned here and exported: the sharing table, retirement, the twin path of tex_mtl_ and g_ser_gen stay in nvn_mtl.m under it, so the lock order (g_smu, then g_cpu_tex_mu), the hash tables, fence-delayed release and the mtl_* contracts are unchanged. hm_get_id / hm_put_id / hm_del_id (with the analyzer's map declaration) are nvn_owner.h's. The hosts, the tests, iOS and the lint database take the new file by their wildcards; no chain input changes. dm/gfx/README.md describes the owner, what stays in the replay layer and the rules the next moves keep.
The finalized-twin case stays open, as evidence: tex_put sends a finalized serial's twin view to the ghost ring, and serial_del, called when the view has to go, removes only a live entry (new reference entry, A finalized twin view left in the ghost ring; found in a review, no game seen). New test t_serial_owner: nvn_owner.m alone with stand-ins for the four hooks, 61 checks: registration, replacement and removal (the old object goes to the deferred release, not released by the owner; its last count's end is reported), a shared object kept resident, two serials on one object, where tex_put sends a serial, finalize and a copy's finalize, ghost replacement in place and eviction at NGHOST, texture pins (resolving after the entry goes, following tex_put, a pin before the object), program pins, a view counting its parent, the rename of a live name, a ghost name and a pin, and own_check_locked after every step; DM_TEST_SERIAL_TWIN=1 asserts the open twin case (it fails, as expected), otherwise the run prints what it found. nvn_replay, t_pool_notes, t_upgrade_alias (filling the table through tex_put, so the counts stay right), t_alias_takeover and t_cpu_tex use the owner's functions instead of its tables. t_geometry: two memcpy lines the analyzer reads as a zero-length copy (1 << type is 1, 2 or 4 bytes) are silenced with the reason; the full run had shown one of them (the other appeared once its search took another path), and the baseline shrinks 216 -> 215. Checked: make -C dm/gfx/tests run (exit 0 in 292 s), thermal-tsan, lint_c.py tidy (0 above, 0 below; nvn_owner.m 0 findings), test_docs, the Mac host build, an iOS IPA of Bastion (BUILD SUCCEEDED). Bastion and Return of the Obra Dinn built from their NSPs on chain 8d9afacc48e2, each run with its note's route from a fresh save on this host and on origin/main's host with the same libraries: Bastion p50 16.66 / p95 17.20 ms against 16.64 / 17.39, Obra Dinn 16.67 / 16.72 against 16.67 / 16.70, no red line on either side, the same scenes, the shots differing only where animation and camera timing do. Not run: the iPhone, ASan / UBSan (no target in the graphics tests)
This commit is contained in:
1 parent
6815810dfa
commit
1ee02525ec
16 files changed
+524
-172
No files matched your search
@@ -24,6 +24,8 @@ For the build steps of the whole project see [README.md](../../README.md) and [d
|
||||
- The game's packaged textures are in Tegra block-linear layout and are deswizzled before upload (see `tex/README.md`).
|
||||
- Presentation: the game's swapchain texture is drawn to a `CAMetalLayer`, and presentation timing is scheduled by the pacer in `nvn/nvn_pace.h`.
|
||||
|
||||
- **The serial tables' owner `nvn/nvn_owner.m`** (`nvn_owner.h`): what a texture or program serial resolves to and who holds each object. It owns the live table (`serial_put`, `serial_del`), the ghost ring of finalized texture serials (`tex_put`, `NGHOST`), the live texture objects (`mtl_tex_init`'s part, `own_tex_end_locked` for a finalize), texture pins (`mtl_tex_pin`) and program pins (`own_prog_pin`, `mtl_prog_release`, `mtl_prog_unpin`), the held counts (`serial_table_holds`, `entry_holds`), and renaming an object everywhere (`own_rename_locked`, the upgrade path). It makes, encodes and releases nothing: what it lets go goes to the replay layer through four hooks (`own_hook_drop`: deferred release after the submission in flight; `own_hook_shared`: the sharing table; `own_hook_unheld`: retirement; `own_hook_changed`: `ser_note`). Rules the next moves keep: a lookup is borrowed and stays valid until the submission it is encoded in completes; every change of a serial's object is reported; the held counts match the tables and pins (`own_check_locked`); the lock order is `g_smu`, then `g_cpu_tex_mu`. Still in `nvn_mtl.m` under the same `g_smu`: the sharing table (`g_alias`, `alias_take_over`, which asks `serial_table_holds`), retirement (`defer_drain`, `g_retire_held`, which asks `entry_holds`), the twin path of `tex_mtl_`, and the change generation (`g_ser_gen`, `ser_note`). Open: `serial_del` leaves a finalized serial's ghost entry ([docs/reference/nvn-objects.md](../../docs/reference/nvn-objects.md#a-finalized-twin-view-left-in-the-ghost-ring)).
|
||||
|
||||
For which Metal object each NVN object and command lands on, see [nvn_metal_mapping.md](nvn_metal_mapping.md).
|
||||
|
||||
### Shader translation chain
|
||||
@@ -72,6 +74,7 @@ Binding conventions followed during translation (the NVN driver's layout, the sa
|
||||
|---|---|
|
||||
| `nvn/hle_nvn.c` | Interface layer: NVN takeover, object implementations, command recording, presentation pacing |
|
||||
| `nvn/nvn_mtl.m`, `nvn/nvn_mtl.h` | Replay layer: Metal device, memory pools, textures, shader loading, pipelines, drawing, presentation |
|
||||
| `nvn/nvn_owner.m`, `nvn/nvn_owner.h` | The serial tables' owner: live and ghost serials, live texture objects, texture and program pins, the held counts |
|
||||
| `nvn/nvn_obj.h` | Host-side object layouts and the command record format, shared by both layers |
|
||||
| `nvn/nvn_entry.S`, `nvn/nvn_thunks.S` | Entries for the unimplemented stub and the timing wrapper, per-function thunks |
|
||||
| `nvn/nvn_tex.c`, `nvn/nvn_tex.h` | Storage size, alignment and per-level offsets of block-linear textures (ported from deko3d) |
|
||||
@@ -137,6 +140,7 @@ The tests in `dm/gfx/tests` compile the production code in directly and need a M
|
||||
| `t_deswz_gpu` | GPU deswizzling |
|
||||
| `t_present_crop` | Presentation cropping |
|
||||
| `t_mip_extent` | Effective size when rendering to a mip level other than level 0 |
|
||||
| `t_serial_owner` | The serial tables' owner alone (`nvn_owner.m` with stand-ins for the replay layer's hooks): registration, replacement, removal, finalize, the ghost ring, pins, views and renames, the held counts after every step; `DM_TEST_SERIAL_TWIN=1` asserts the open finalized-twin case |
|
||||
| `t_fuse_check`, `t_reorder_chain`, `t_defer_chain`, `t_zskip`, `t_xv`, `t_ub`, `t_pfv`, `t_rt_range`, `t_rt_range_shape`, `t_rt_range_ro`, `t_rt_depth_ro` | Compile the replay layer `nvn_mtl.m` in directly and check the various optimization and diagnostic logic inside the replay layer |
|
||||
| `test_tbdr_budget.py` | The parsing in `tools/tbdr_budget.py` |
|
||||
|
||||
|
||||
@@ -276,7 +276,7 @@ static void cap_tex_read(const tex_t *t, int rec) { // rec: named by a record
|
||||
}
|
||||
nvn_hm_put(&g_cap_read, &t->serial, (void *)(uintptr_t)(!o && !rec ? 2 : 1));
|
||||
if (!seen) { // how the replay layer holds it
|
||||
pthread_mutex_lock(&g_smu); int live = nvn_hm_get(&g_tex_live, &t->serial) != NULL; pthread_mutex_unlock(&g_smu);
|
||||
int live = own_tex_live(t->serial);
|
||||
int cpu = tex_cpu_written(t->serial) || ([o isKindOfClass:[NvPkg class]] && !t->has_layout);
|
||||
cap_tknd k = {*t, (live ? CAP_TK_LIVE : 0) | (cpu ? CAP_TK_CPU : 0), 0};
|
||||
if (k.kind) { cap_tex_enc(&k.t, "texture kind"); cap_buf_put(&g_cap_kind, &k, sizeof k); }
|
||||
|
||||
+24
-152
@@ -55,6 +55,7 @@ static inline uint64_t tex_slot_h(const uint64_t *tex, const uint64_t *img, unsi
|
||||
}
|
||||
static uint64_t c2_handle(int stage, unsigned b); // the handle c2 holds at byte b (defined with the draw state, TEXA / SAMPA)
|
||||
#include "nvn_hmap.h"
|
||||
#include "nvn_owner.h" // the serial tables (serial_put, tex_put, tex_get, the pins, the held counts) and their lock g_smu
|
||||
#include "../../host/dm.h"
|
||||
#import <CommonCrypto/CommonDigest.h>
|
||||
#include <stdarg.h>
|
||||
@@ -509,7 +510,7 @@ static void defer_obj_at(id obj, int keep_resident, uint64_t fence) {
|
||||
}
|
||||
static void defer_obj_release(id obj) { defer_obj_at(obj, 0, g_fence + 1); }
|
||||
static void defer_obj_unref(id obj) { defer_obj_at(obj, 1, g_fence + 1); }
|
||||
static pthread_mutex_t g_smu; static int entry_holds(const void *o); static void retire_wait(id o); // defined below (the serial table)
|
||||
static void retire_wait(id o); // defined below (the serial table); g_smu and entry_holds are nvn_owner.h's
|
||||
// Its own autorelease pool: mtl_sub_begin drains before it pushes the submission's pool, on the guest submit thread, whose implicit pool is never drained. The weak-table lookups and the residency-set removal
|
||||
// autorelease the retired object, so whatever that thread drained (instead of the completion handler, which wins the race only when the CPU is idle) stayed alive for good: in Metroid Dread every
|
||||
// re-upload of its map mask kept the old texture and its 32 MB upload staging block, and GPU memory grew by several GB within minutes under CPU load.
|
||||
@@ -2592,24 +2593,7 @@ static int tex_need_pfv(MTLPixelFormat base, MTLPixelFormat view) {
|
||||
return 1;
|
||||
}
|
||||
// Cache tables on the replay hot path always use nvn_hmap (C hash, keys not boxed); the table stores CFBridgingRetain'ed objects, CFRelease'd on replace / delete
|
||||
static void hm_rel(void *v) { CFRelease(v); }
|
||||
static __unsafe_unretained id hm_get_id(const nvn_hmap *m, const void *k) { return (__bridge id)nvn_hm_get(m, k); }
|
||||
// The map owns the reference CFBridgingRetain makes until the entry is replaced (old, released here) or removed (hm_rel). The static analyzer cannot follow a reference into
|
||||
// nvn_hmap's C arrays and took every object put here for a leak (clang-analyzer-osx.cocoa.RetainCount): for its eyes only, a declaration with no body hands the reference over
|
||||
// (CF_CONSUMED), which is what the map does. No build compiles it (__clang_analyzer__ is defined only during analysis)
|
||||
#ifdef __clang_analyzer__
|
||||
void dm_analyzer_map_takes(CFTypeRef CF_CONSUMED ref);
|
||||
#endif
|
||||
static void hm_put_id(nvn_hmap *m, const void *k, id o) {
|
||||
CFTypeRef ref = CFBridgingRetain(o);
|
||||
#ifdef __clang_analyzer__
|
||||
dm_analyzer_map_takes(ref);
|
||||
#endif
|
||||
void *old = nvn_hm_put(m, k, (void *)ref); if (old) CFRelease(old);
|
||||
}
|
||||
static void hm_del_id(nvn_hmap *m, const void *k) { void *old = nvn_hm_del(m, k); if (old) CFRelease(old); }
|
||||
static nvn_hmap g_by_serial = {.klen = 8}; // Serial → MTLTexture / NvPkg / NvProg
|
||||
static pthread_mutex_t g_smu = PTHREAD_MUTEX_INITIALIZER;
|
||||
static void hm_rel(void *v) { CFRelease(v); } // hm_get_id, hm_put_id and hm_del_id are nvn_owner.h's
|
||||
// Serial table generation: +1 when entries are added / removed or a packaged texture is replaced. Downstream caches (texture views, programs) record the generation at fetch time; if it is unchanged they can be used directly without another table lookup
|
||||
static _Atomic uint64_t g_ser_gen = 1;
|
||||
// Which serial each change was about, for the texture ID cache (ser_sync): a site that changes one serial's object bumps g_ser_gen through ser_note; any other bump (the sharing table,
|
||||
@@ -2633,13 +2617,6 @@ static void ser_note(uint64_t serial) {
|
||||
typedef struct { uint64_t pool; int64_t off; nvn_texdesc d; } alias_key;
|
||||
static nvn_hmap g_alias = {.klen = sizeof(alias_key)};
|
||||
static nvn_hmap g_pkg_share = {.klen = sizeof(alias_key)}; // see NvPkgRef
|
||||
// How many entries of g_by_serial hold each object (key: its address; a view's parents too, count_add), kept by every writer of g_by_serial (serial_put, serial_del, tex_put, the upgrade's in-place
|
||||
// replacement), so alias_take_over asks whether a live texture object still resolves to a shared texture in one lookup. It walked the serial table's whole capacity for
|
||||
// each candidate instead: in Ocarina of Time's cutscenes, with about 7,500 serials and thousands of shared textures, that walk was the submit thread's hottest function and
|
||||
// frames took up to a second. An entry holds its object (CFBridgingRetain), so a counted address cannot be reused while counted. Under g_smu
|
||||
static nvn_hmap g_by_serial_held = {.klen = sizeof(uintptr_t)};
|
||||
// The same count for the ghost ring (g_ghost, finalized serials a replayed record still resolves; tex_put and the upgrade's in-place replacement write it)
|
||||
static nvn_hmap g_ghost_held = {.klen = sizeof(uintptr_t)};
|
||||
// A retired object a serial entry still holds stays in the residency set until the last such entry lets it go. Several entries can hold one texture that no table owns: at the GPU's
|
||||
// first write over storage a packaged texture was taken from (cpu_unc_drop), every serial of the package gets the package's texture itself, live serials and finalized ones in the ghost
|
||||
// ring. The package's release and each holder's retired it, taking it out of the residency set while the others still drew with it: GPU address faults 15-45 minutes into Ocarina of
|
||||
@@ -2647,26 +2624,6 @@ static nvn_hmap g_ghost_held = {.klen = sizeof(uintptr_t)};
|
||||
// the count's last entry going retires it again (retire_resume). Under g_smu
|
||||
static nvn_hmap g_retire_held = {.klen = sizeof(uintptr_t)};
|
||||
static void retire_resume(const void *o);
|
||||
static int serial_table_holds(const void *o) { uintptr_t k = (uintptr_t)o; return nvn_hm_get(&g_by_serial_held, &k) != NULL; }
|
||||
static int entry_holds(const void *o) { uintptr_t k = (uintptr_t)o; return serial_table_holds(o) || (g_ghost_held.n && nvn_hm_get(&g_ghost_held, &k)); }
|
||||
static void count_one(nvn_hmap *m, const void *o, int d) {
|
||||
if (!o) return;
|
||||
uintptr_t k = (uintptr_t)o, n = (uintptr_t)nvn_hm_get(m, &k);
|
||||
if (d > 0) nvn_hm_put(m, &k, (void *)(n + 1));
|
||||
else if (n > 1) nvn_hm_put(m, &k, (void *)(n - 1));
|
||||
else if (n) { nvn_hm_del(m, &k); if (g_retire_held.n && !entry_holds(o)) retire_resume(o); } // a slot written past these helpers (a test's table) was never counted: nothing to take away
|
||||
}
|
||||
// An entry that holds a view holds the textures the view is of as well, and they are counted with it: an sRGB / linear twin's serial holds a view of the shared texture
|
||||
// (tex_mtl_), so that texture is in use as long as the twin's entry is. Counted alone, the view left its parent looking unheld: a takeover evicted it under a live twin
|
||||
// (alias_take_over), and its retirement took it out of the residency set while pool tables still named the view (GitHub issue #2125). A view's parent never changes, so
|
||||
// what an entry adds it takes away
|
||||
static void count_add(nvn_hmap *m, const void *o, int d) {
|
||||
count_one(m, o, d);
|
||||
id t = (__bridge id)o;
|
||||
if (t && [t respondsToSelector:@selector(parentTexture)]) for (id<MTLTexture> p = ((id<MTLTexture>)t).parentTexture; p; p = p.parentTexture) count_one(m, (__bridge void *)p, d);
|
||||
}
|
||||
static void held_add(const void *o, int d) { count_add(&g_by_serial_held, o, d); }
|
||||
static void ghost_held_add(const void *o, int d) { count_add(&g_ghost_held, o, d); }
|
||||
// The shared textures by where their storage lies, for alias_take_over and the image families' owner lookup (image_texture): {pool, 64 KB cell of the pool} → the entries whose bytes reach into that cell, each with its end. It walked
|
||||
// g_alias's whole capacity for every texture made instead: Mario Golf: Super Rush's All-Star Summit registers 134,400 textures and makes about 90,000 of them as the course loads,
|
||||
// each walk 131,072 slots with a layout and a lookup per entry of the pool, and the load had not finished after 12 minutes on one core. An entry lies in every cell its bytes reach, so one of 8 MB is in 128 of them. Kept by alias_put and alias_del, the only writers of g_alias's keys (the upgrade replaces a value in place). Under g_smu
|
||||
@@ -2712,11 +2669,6 @@ static void image_desc_drop_pool(const void *pool) { // caller holds g_smu
|
||||
} else i++;
|
||||
}
|
||||
}
|
||||
// Deferred command descriptors and persistent bindings retain a texture serial
|
||||
// independently of the guest descriptor and the bounded recently-finalized cache.
|
||||
typedef struct { unsigned refs; void *object; } tex_pin;
|
||||
static nvn_hmap g_tex_pins={.klen=8}, g_tex_pin_objects={.klen=sizeof(uintptr_t)};
|
||||
static int tex_object_pinned(id o) { uintptr_t key=(uintptr_t)(__bridge void *)o; return o && nvn_hm_get(&g_tex_pin_objects,&key)!=NULL; }
|
||||
static CFMutableSetRef g_alias_set; // Textures in the sharing table (non-owning; used to ask "is this shared")
|
||||
static uint64_t g_alias_evicted; // shared textures evicted on pool Finalize so far (for the memory report)
|
||||
static int alias_has(id o) { return tex_object_pinned(o) || (g_alias_set && o && CFSetContainsValue(g_alias_set, (__bridge const void *)o)); }
|
||||
@@ -2813,70 +2765,19 @@ static void alias_take_over(const alias_key *nk) {
|
||||
for (uint32_t j = 0; j < n; j++) alias_del(&ks[j]);
|
||||
if (n) { ser_note(0); g_alias_taken += n; }
|
||||
}
|
||||
static __unsafe_unretained id serial_get(uint64_t k) { pthread_mutex_lock(&g_smu); __unsafe_unretained id o = hm_get_id(&g_by_serial, &k); pthread_mutex_unlock(&g_smu); return o; }
|
||||
static void serial_put(uint64_t k, id o) { pthread_mutex_lock(&g_smu); id old = hm_get_id(&g_by_serial, &k); if (old) { held_add((__bridge void *)old, -1); if (alias_has(old)) defer_obj_unref(old); else defer_obj_release(old); } hm_put_id(&g_by_serial, &k, o); held_add((__bridge void *)o, 1); ser_note(k); pthread_mutex_unlock(&g_smu); }
|
||||
static void serial_del(uint64_t k) { pthread_mutex_lock(&g_smu); id old = hm_get_id(&g_by_serial, &k); if (old) { held_add((__bridge void *)old, -1); if (alias_has(old)) defer_obj_unref(old); else defer_obj_release(old); hm_del_id(&g_by_serial, &k); ser_note(k); } pthread_mutex_unlock(&g_smu); } // nothing to drop: the texture ID caches stay valid
|
||||
// Texture serials owned by a live NVN texture object (TextureInitialize until Finalize or re-initialization, hle_nvn.c). Command records and texture pool registrations snapshot the
|
||||
// texture object (tex_snap in hle_nvn.c, mtl_reg_tex), so replay can resolve a texture the game has already finalized: Breath of the Wild records each frame's transient render targets
|
||||
// (the HUD captures, the 640x360 half-resolution buffers) and finalizes their objects before it submits. Those serials went back into g_by_serial and stayed there for good, as no second
|
||||
// Finalize comes: about 10 a frame, 200,000 after an hour in the first shrine. A dead serial resolves through g_ghost instead, a small table whose oldest entry goes when a new one comes,
|
||||
// so a record replayed again soon still finds its texture
|
||||
enum { NGHOST = 512 };
|
||||
static nvn_hmap g_tex_live = {.klen = 8}; // serial → the texture object it was initialized in (mtl_tex_release), under g_smu
|
||||
static nvn_hmap g_ghost = {.klen = 8}; static uint64_t g_ghost_ring[NGHOST]; static unsigned g_ghost_next; static uint64_t g_ghost_puts;
|
||||
// The serial tables are nvn_owner.m's (nvn_owner.h): serial_put / serial_del / tex_put, serial_get / tex_get, the pins, the held counts. What it asks of the replay layer:
|
||||
void own_hook_drop(id o, int keep_resident) { if (keep_resident) defer_obj_unref(o); else defer_obj_release(o); } // released once the submission in flight completes
|
||||
int own_hook_shared(id o) { return g_alias_set && o && CFSetContainsValue(g_alias_set, (__bridge const void *)o); }
|
||||
void own_hook_unheld(const void *o) { if (g_retire_held.n && !entry_holds(o)) retire_resume(o); } // a retired object no serial entry holds any more leaves the residency set
|
||||
void own_hook_changed(uint64_t serial) { ser_note(serial); }
|
||||
void mtl_tex_init(const tex_t *t) {
|
||||
if (!t->serial) return;
|
||||
tex_t *desc = image_shape(t) ? malloc(sizeof *desc) : NULL; if (desc) *desc = *t;
|
||||
pthread_mutex_lock(&g_smu);
|
||||
nvn_hm_put(&g_tex_live, &t->serial, t);
|
||||
own_tex_live_put_locked(t);
|
||||
if (desc) free(nvn_hm_put(&g_image_desc, &t->serial, desc));
|
||||
pthread_mutex_unlock(&g_smu);
|
||||
}
|
||||
static void obj_drop(id o) { if (alias_has(o)) defer_obj_unref(o); else defer_obj_release(o); } // caller holds g_smu
|
||||
static void tex_pin_set(tex_pin *pin,id object) { // g_smu held
|
||||
id old=(__bridge id)pin->object; if(old==object) return;
|
||||
if(old) {
|
||||
uintptr_t key=(uintptr_t)pin->object, count=(uintptr_t)nvn_hm_get(&g_tex_pin_objects,&key);
|
||||
if(count>1) nvn_hm_put(&g_tex_pin_objects,&key,(void *)(count-1)); else nvn_hm_del(&g_tex_pin_objects,&key);
|
||||
held_add(pin->object,-1); if(alias_has(old) || serial_table_holds((__bridge void *)old)) defer_obj_unref(old); else defer_obj_release(old); CFRelease(pin->object); pin->object=NULL;
|
||||
}
|
||||
if(object) {
|
||||
pin->object=(void *)CFBridgingRetain(object); uintptr_t key=(uintptr_t)pin->object;
|
||||
uintptr_t count=(uintptr_t)nvn_hm_get(&g_tex_pin_objects,&key);
|
||||
nvn_hm_put(&g_tex_pin_objects,&key,(void *)(count+1)); held_add(pin->object,1);
|
||||
}
|
||||
}
|
||||
void mtl_tex_pin(uint64_t serial) {
|
||||
if(!serial) return; pthread_mutex_lock(&g_smu);
|
||||
tex_pin *pin=nvn_hm_get(&g_tex_pins,&serial);
|
||||
if(!pin) { pin=calloc(1,sizeof *pin); if(!pin) MDIE("texture retention allocation failed"); nvn_hm_put(&g_tex_pins,&serial,pin); }
|
||||
pin->refs++;
|
||||
if(!pin->object) { id o=hm_get_id(&g_by_serial,&serial); if(!o) o=hm_get_id(&g_ghost,&serial); tex_pin_set(pin,o); }
|
||||
pthread_mutex_unlock(&g_smu);
|
||||
}
|
||||
void mtl_tex_unpin(uint64_t serial) {
|
||||
if(!serial) return; pthread_mutex_lock(&g_smu); tex_pin *pin=nvn_hm_get(&g_tex_pins,&serial);
|
||||
if(pin && !--pin->refs) { nvn_hm_del(&g_tex_pins,&serial); tex_pin_set(pin,nil); free(pin); }
|
||||
pthread_mutex_unlock(&g_smu);
|
||||
}
|
||||
static __unsafe_unretained id tex_get(uint64_t k) {
|
||||
pthread_mutex_lock(&g_smu); __unsafe_unretained id o = hm_get_id(&g_by_serial, &k); if (!o && g_ghost.n) o = hm_get_id(&g_ghost, &k); if(!o) { tex_pin *pin=nvn_hm_get(&g_tex_pins,&k); if(pin) o=(__bridge id)pin->object; } pthread_mutex_unlock(&g_smu); return o;
|
||||
}
|
||||
static void tex_put(uint64_t k, id o) { // serials from 2^62 up are external textures (mtl_ext_tex), never initialized by the game
|
||||
pthread_mutex_lock(&g_smu);
|
||||
nvn_hmap *m = k >= (1ull << 62) || nvn_hm_get(&g_tex_live, &k) ? &g_by_serial : &g_ghost;
|
||||
id old = hm_get_id(m, &k);
|
||||
if (old) count_add(m == &g_by_serial ? &g_by_serial_held : &g_ghost_held, (__bridge void *)old, -1);
|
||||
if (old) obj_drop(old);
|
||||
else if (m == &g_ghost) {
|
||||
uint64_t *slot = &g_ghost_ring[g_ghost_next++ % NGHOST];
|
||||
if (*slot) { id ev = hm_get_id(&g_ghost, slot); if (ev) { ghost_held_add((__bridge void *)ev, -1); obj_drop(ev); } hm_del_id(&g_ghost, slot); ser_note(*slot); } // the evicted serial resolves anew too
|
||||
*slot = k; g_ghost_puts++;
|
||||
}
|
||||
hm_put_id(m, &k, o); tex_pin *pin=nvn_hm_get(&g_tex_pins,&k); if(pin) tex_pin_set(pin,o); ser_note(k);
|
||||
count_add(m == &g_by_serial ? &g_by_serial_held : &g_ghost_held, (__bridge void *)o, 1);
|
||||
pthread_mutex_unlock(&g_smu);
|
||||
}
|
||||
// Host record of a packaged texture: the current MTLTexture, the upload stamp, and the global write generation at the last check
|
||||
// spare: the texture the last re-upload replaced, kept resident and written by the next re-upload once the GPU has passed spare_fence (a texture the CPU rewrites every frame, a movie frame,
|
||||
// alternates between two textures instead of allocating one per frame: Link's Awakening's title movie made and freed an 8 MB texture a frame)
|
||||
@@ -3105,15 +3006,7 @@ static id<MTLTexture> tex_upgrade(const tex_t *t, id<MTLTexture> asked, MTLTextu
|
||||
// so the others kept reading the old copy while writes went to the new one, and the old texture stayed in g_alias_set. Slots are replaced in place (no insert, so no table resize during the walk);
|
||||
// the old texture is retired once the submissions that used it complete, the view caches follow g_ser_gen (PR #107)
|
||||
pthread_mutex_lock(&g_smu);
|
||||
nvn_hmap *tables[] = {&g_by_serial, &g_ghost}; int named = 0, shared = 0;
|
||||
for (unsigned j = 0; j < 2; j++) for (uint32_t i = 0; i < tables[j]->cap; i++) {
|
||||
if (tables[j]->vals[i] != (__bridge void *)base) continue;
|
||||
uint64_t serial; memcpy(&serial, tables[j]->keys + (size_t)i * tables[j]->klen, sizeof serial);
|
||||
void *old = tables[j]->vals[i]; tables[j]->vals[i] = (void *)CFBridgingRetain(upgraded);
|
||||
nvn_hmap *cnt = j == 0 ? &g_by_serial_held : &g_ghost_held; count_add(cnt, old, -1); count_add(cnt, (__bridge void *)upgraded, 1); // the count moves with the name
|
||||
CFRelease(old); named |= serial == t->serial;
|
||||
}
|
||||
for(uint32_t i=0;i<g_tex_pins.cap;i++) if(g_tex_pins.vals[i]) { tex_pin *pin=g_tex_pins.vals[i]; if(pin->object==(__bridge void *)base) tex_pin_set(pin,upgraded); }
|
||||
int named = own_rename_locked(base, upgraded, t->serial), shared = 0; // the serial tables and the pins (nvn_owner.m), then the sharing table
|
||||
for (uint32_t i = 0; i < g_alias.cap; i++) {
|
||||
if (g_alias.vals[i] != (__bridge void *)base) continue;
|
||||
void *old = g_alias.vals[i]; g_alias.vals[i] = (void *)CFBridgingRetain(upgraded); CFRelease(old); shared = 1;
|
||||
@@ -4301,7 +4194,7 @@ void mtl_tex_cpu_written(const tex_t *t) {
|
||||
// nvn_ft_poll can mark a snapshot after Finalize. Serialize the live check with release (g_smu → g_cpu_tex_mu), so that mark goes straight to retired metadata.
|
||||
pthread_mutex_lock(&g_smu);
|
||||
pthread_mutex_lock(&g_cpu_tex_mu);
|
||||
int live = nvn_hm_get(&g_tex_live, &serial) != NULL;
|
||||
int live = own_tex_live_locked(serial);
|
||||
if ((live || (t->pool && t->mem)) && !nvn_hm_get(&g_cpu_tex, &serial) && !nvn_hm_get(&g_cpu_tex_retired, &serial)) {
|
||||
nvn_hm_put(live ? &g_cpu_tex : &g_cpu_tex_retired, &serial, t->pool ? (void *)t->pool : (void *)1);
|
||||
ser_note(serial); // its texture IDs resolve again through the packaged path (tex_by_id view_pkg)
|
||||
@@ -4749,11 +4642,10 @@ void mtl_mem_stat(unsigned long long frame, unsigned long long rss_mb) {
|
||||
pthread_mutex_lock(&g_deferred_mu); unsigned long nd = (unsigned long)g_deferred.count; pthread_mutex_unlock(&g_deferred_mu);
|
||||
pthread_mutex_lock(&g_res_mu); unsigned long nr = (unsigned long)g_res_objects.count; pthread_mutex_unlock(&g_res_mu);
|
||||
int live = 0, peak = 0, cap = 0; nvn_dirty_stat(&live, &peak, &cap);
|
||||
pthread_mutex_lock(&g_smu); unsigned ns = g_by_serial.n, ashared = g_alias.n, nghost = g_ghost.n, nwait = g_retire_held.n; uint64_t ghost_puts = g_ghost_puts;
|
||||
pthread_mutex_lock(&g_smu); own_stats os = own_stats_locked(); unsigned ns = os.live, ashared = g_alias.n, nghost = os.ghost, nwait = g_retire_held.n; uint64_t ghost_puts = os.ghost_puts;
|
||||
unsigned aorph = 0; // shared textures no live texture object or finalized serial resolves to: kept only until their storage is taken over or their pool goes (alias_take_over)
|
||||
{ CFMutableSetRef held = CFSetCreateMutable(NULL, 0, NULL);
|
||||
for (uint32_t i = 0; i < g_by_serial.cap; i++) if (g_by_serial.vals[i]) CFSetAddValue(held, g_by_serial.vals[i]);
|
||||
for (uint32_t i = 0; i < g_ghost.cap; i++) if (g_ghost.vals[i]) CFSetAddValue(held, g_ghost.vals[i]);
|
||||
own_each_locked(^(uint64_t serial, id o, int ghost) { (void)serial; (void)ghost; CFSetAddValue(held, (__bridge const void *)o); });
|
||||
for (uint32_t i = 0; i < g_alias.cap; i++) if (g_alias.vals[i] && !CFSetContainsValue(held, g_alias.vals[i])) aorph++;
|
||||
CFRelease(held); }
|
||||
pthread_mutex_unlock(&g_smu);
|
||||
@@ -4803,15 +4695,9 @@ static void treg_retire_serial(uint64_t ser);
|
||||
void mtl_tex_release(tex_t *t) {
|
||||
if (!t->serial) return;
|
||||
pthread_mutex_lock(&g_smu);
|
||||
// NVNtexture is plain data, and engines finalize copies of it (Breath of the Wild about 100 a frame, Ocarina of Time every few frames, for textures whose own object lives on). A copy's Finalize ends
|
||||
// only the copy. Taken for the texture's end, it deleted the host texture, which the next read made and uploaded again: with a pool walk touching every ID (pool_gpu), 5,500 uploads and 500 MB to 1 GB
|
||||
// a frame in Ocarina of Time's Kokiri Forest, the batches' memory held until they retired. If the object the texture was initialized in no longer holds it (the game moved it), this was the last one
|
||||
const tex_t *own = nvn_hm_get(&g_tex_live, &t->serial);
|
||||
if (own && own != t) { // read through the kernel: the game may have freed and unmapped that object
|
||||
tex_t seen; vm_size_t got = 0;
|
||||
if (vm_read_overwrite(mach_task_self(), (vm_address_t)(uintptr_t)own, sizeof seen, (vm_address_t)(uintptr_t)&seen, &got) == KERN_SUCCESS && got == sizeof seen && seen.magic == MAGIC('T', 'E', 'X', 0) && seen.serial == t->serial) { pthread_mutex_unlock(&g_smu); return; }
|
||||
}
|
||||
int was_live = nvn_hm_del(&g_tex_live, &t->serial) != NULL; free(nvn_hm_del(&g_image_desc, &t->serial));
|
||||
int was_live = own_tex_end_locked(t); // -1: a copy's Finalize, the texture's own object still holds it (nvn_owner.m)
|
||||
if (was_live < 0) { pthread_mutex_unlock(&g_smu); return; }
|
||||
free(nvn_hm_del(&g_image_desc, &t->serial));
|
||||
pthread_mutex_lock(&g_cpu_tex_mu);
|
||||
void *pool = nvn_hm_del(&g_cpu_tex, &t->serial);
|
||||
if (pool && t->pool && t->mem) nvn_hm_put(&g_cpu_tex_retired, &t->serial, pool);
|
||||
@@ -6469,26 +6355,11 @@ static __unsafe_unretained NvProg *prog_get(const prog_t *cp) { // Called only
|
||||
if (p->serial) serial_put(p->serial, o);
|
||||
return o;
|
||||
}
|
||||
// A submitted descriptor and a queue binding hold the compiled program by its
|
||||
// immutable initialization serial. Retaining it must not mint serial aliases:
|
||||
// alias churn invalidates texture tables and changes composite-program keys.
|
||||
typedef struct { unsigned refs, retired; } prog_pin;
|
||||
static nvn_hmap g_prog_pins={.klen=8}; // protected by the submission lock
|
||||
void mtl_prog_release(prog_t *p) {
|
||||
if(!p->serial) return;
|
||||
prog_pin *pin=nvn_hm_get(&g_prog_pins,&p->serial);
|
||||
if(pin) pin->retired=1; else serial_del(p->serial);
|
||||
}
|
||||
void mtl_prog_unpin(prog_t *p) {
|
||||
prog_pin *pin=nvn_hm_get(&g_prog_pins,&p->serial);
|
||||
if(pin && !--pin->refs) { nvn_hm_del(&g_prog_pins,&p->serial); if(pin->retired) serial_del(p->serial); free(pin); }
|
||||
}
|
||||
// A submitted descriptor and a queue binding pin the program by its serial (own_prog_pin; mtl_prog_release and mtl_prog_unpin are nvn_owner.m's)
|
||||
void mtl_prog_snapshot(prog_t *copy,const prog_t *source) {
|
||||
prog_get(source); *copy=*source;
|
||||
if(!source->serial) MDIE("cannot retain a program without an initialization serial");
|
||||
prog_pin *pin=nvn_hm_get(&g_prog_pins,&source->serial);
|
||||
if(!pin) { pin=calloc(1,sizeof *pin); if(!pin) MDIE("program retention allocation failed"); nvn_hm_put(&g_prog_pins,&source->serial,pin); }
|
||||
pin->refs++;
|
||||
own_prog_pin(source->serial);
|
||||
}
|
||||
|
||||
// ---------- Descriptor pool: texture ID / sampler ID → object ----------
|
||||
@@ -6594,7 +6465,7 @@ static id<MTLTexture> vc_get(uint64_t ser, const vc_key *k, id<MTLTexture> base)
|
||||
return nil;
|
||||
}
|
||||
static vc_set *vc_set_for(uint64_t ser, const void *pool) { // the texture's entry, made if needed; NULL for a texture already finalized (resolved through g_ghost): nothing would drop the entry
|
||||
pthread_mutex_lock(&g_smu); int live = nvn_hm_get(&g_tex_live, &ser) != NULL; pthread_mutex_unlock(&g_smu);
|
||||
int live = own_tex_live(ser);
|
||||
if (!live) return NULL;
|
||||
vc_set *s = nvn_hm_get(&g_vc, &ser);
|
||||
if (!s) {
|
||||
@@ -6763,7 +6634,7 @@ void mtl_reg_tex(int tid, const tex_t *t, const view_t *v) {
|
||||
rr->ok = t != NULL; if (t) rr->t = *t; rr->has_v = v != NULL; if (v) rr->v = *v;
|
||||
rr->retired = 0; rr->transient_sub = 0;
|
||||
if (t && t->serial) {
|
||||
pthread_mutex_lock(&g_smu); rr->retired = !nvn_hm_get(&g_tex_live, &t->serial); pthread_mutex_unlock(&g_smu);
|
||||
rr->retired = !own_tex_live(t->serial);
|
||||
rr->next = (int)(uintptr_t)nvn_hm_get(&g_treg_serial, &t->serial);
|
||||
if (rr->next) g_treg[rr->next - 1].prev = id_ + 1;
|
||||
nvn_hm_put(&g_treg_serial, &t->serial, (void *)(uintptr_t)(id_ + 1));
|
||||
@@ -15652,7 +15523,7 @@ void mtl_res_snapshot(mtl_res_count *out) {
|
||||
out->c1_bufs = (uint32_t)g_c1_banks.count; out->c1_alloc = g_c1_alloc_bytes; out->c1_data = g_c1_data_bytes;
|
||||
os_unfair_lock_unlock(&g_c1_lock);
|
||||
pthread_mutex_lock(&g_smu);
|
||||
out->live_tex = g_by_serial.n;
|
||||
out->live_tex = own_stats_locked().live;
|
||||
pthread_mutex_unlock(&g_smu);
|
||||
pthread_mutex_lock(&g_mu);
|
||||
out->pools = (uint32_t)g_npool;
|
||||
@@ -17042,8 +16913,9 @@ void mem_census(const char *why) { @autoreleasepool {
|
||||
NSHashTable *named_ret = [NSHashTable hashTableWithOptions:NSPointerFunctionsOpaqueMemory | NSPointerFunctionsOpaquePersonality]; // named only through retired descriptors (until a live one names it)
|
||||
#define CN_ADD(o) do { id<MTLTexture> b_ = census_base(o); if (b_) [named addObject:(__bridge id)(__bridge void *)b_]; } while (0)
|
||||
pthread_mutex_lock(&g_smu);
|
||||
const nvn_hmap *maps[] = {&g_by_serial, &g_ghost, &g_alias};
|
||||
for (unsigned m = 0; m < sizeof maps / sizeof maps[0]; m++) for (uint32_t i = 0; i < maps[m]->cap; i++) if (maps[m]->vals[i]) { id o = (__bridge id)maps[m]->vals[i]; CN_ADD(o); if ([o isKindOfClass:[NvPkg class]] && ((NvPkg *)o)->spare) CN_ADD(((NvPkg *)o)->spare); }
|
||||
void (^census_one)(id o) = ^(id o) { CN_ADD(o); if ([o isKindOfClass:[NvPkg class]] && ((NvPkg *)o)->spare) CN_ADD(((NvPkg *)o)->spare); };
|
||||
own_each_locked(^(uint64_t serial, id o, int ghost) { (void)serial; (void)ghost; census_one(o); }); // the serial tables (nvn_owner.m), then the sharing table
|
||||
for (uint32_t i = 0; i < g_alias.cap; i++) if (g_alias.vals[i]) census_one((__bridge id)g_alias.vals[i]);
|
||||
pthread_mutex_unlock(&g_smu);
|
||||
// registered descriptors: their views' bases, and per storage address the distinct bases
|
||||
NSMapTable *by_mem = [NSMapTable mapTableWithKeyOptions:NSPointerFunctionsOpaqueMemory | NSPointerFunctionsOpaquePersonality valueOptions:NSPointerFunctionsStrongMemory];
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
// The serial tables' owner (nvn_owner.m): what an NVN texture or program serial resolves to, and who holds each object. The replay layer (nvn_mtl.m) makes the objects, encodes
|
||||
// with them and retires them; it changes these tables only through the functions below.
|
||||
//
|
||||
// Tables (all under g_smu but the program pins):
|
||||
// live serial → object (MTLTexture, NvPkg, NvProg) of a live serial: serial_put / serial_del, and tex_put for a texture whose object is live or an external texture (2^62 and up)
|
||||
// ghost serial → object of a finalized texture serial a replayed record still resolves (tex_put), a ring of NGHOST: the oldest goes when a new serial comes
|
||||
// tex live serial → the texture object it was initialized in (mtl_tex_init until its finalize), which decides between live and ghost
|
||||
// tex pins serial → a retained object a queued descriptor or saved binding still resolves after the serial's entries go (mtl_tex_pin / mtl_tex_unpin)
|
||||
// prog pins serial → pins on a program a queued descriptor or saved binding holds: its release waits for the last unpin (under the submission lock, not g_smu)
|
||||
// Every live entry and every texture pin counts its object, and each parent of a view, in the live held count; every ghost entry in the ghost held count (entry_holds asks both).
|
||||
// own_check_locked checks the counts against the tables.
|
||||
//
|
||||
// Lifetime of what a lookup returns: borrowed. The owner never releases an object it lets go: it hands it to the replay layer (own_hook_drop), which releases it once the submission in
|
||||
// flight has completed (defer_obj_release / defer_obj_unref). So an object looked up while encoding stays valid at least until the submission it is encoded in completes. A caller that
|
||||
// keeps one longer retains it (a strong reference). Every change of a serial's object is reported (own_hook_changed: ser_note, which bumps g_ser_gen for the replay layer's caches).
|
||||
//
|
||||
// Lock order: g_smu, then the replay layer's g_cpu_tex_mu. Until the sharing table moves here, nvn_mtl.m's sharing table, the upgrade path and the twin check use g_smu as well.
|
||||
#pragma once
|
||||
#import <Foundation/Foundation.h>
|
||||
#include <pthread.h>
|
||||
#include <stdint.h>
|
||||
#include "nvn_hmap.h"
|
||||
#include "nvn_obj.h"
|
||||
|
||||
// Tables from serial (or key) to a retained object
|
||||
#ifdef __clang_analyzer__
|
||||
void dm_analyzer_map_takes(CFTypeRef CF_CONSUMED ref); // a declaration with no body: hands the reference to the map, which is what the map does (the analyzer cannot follow it into nvn_hmap)
|
||||
#endif
|
||||
static inline id hm_get_id(const nvn_hmap *m, const void *k) { return (__bridge id)nvn_hm_get(m, k); }
|
||||
static inline void hm_put_id(nvn_hmap *m, const void *k, id o) { // the map owns the reference CFBridgingRetain makes until the entry is replaced (released here) or removed (hm_del_id)
|
||||
CFTypeRef ref = CFBridgingRetain(o);
|
||||
#ifdef __clang_analyzer__
|
||||
dm_analyzer_map_takes(ref);
|
||||
#endif
|
||||
void *old = nvn_hm_put(m, k, (void *)ref); if (old) CFRelease(old);
|
||||
}
|
||||
static inline void hm_del_id(nvn_hmap *m, const void *k) { void *old = nvn_hm_del(m, k); if (old) CFRelease(old); }
|
||||
|
||||
extern pthread_mutex_t g_smu;
|
||||
enum { NGHOST = 512 };
|
||||
|
||||
// Lookups (borrowed, see above)
|
||||
const void *own_live_ref(uint64_t k); // the live entry
|
||||
const void *own_resolve_ref(uint64_t k); // live, then ghost, then a texture pin
|
||||
const void *own_live_ref_locked(uint64_t k);
|
||||
static inline id serial_get(uint64_t k) { return (__bridge id)own_live_ref(k); }
|
||||
static inline id tex_get(uint64_t k) { return (__bridge id)own_resolve_ref(k); }
|
||||
|
||||
// Changes (each takes g_smu)
|
||||
void serial_put(uint64_t k, id o); // a live serial's object; the one it replaces is dropped
|
||||
void serial_del(uint64_t k); // a live serial's entry goes; a ghost entry of the serial is left (see docs/reference/nvn-objects.md, A finalized twin view left in the ghost ring)
|
||||
void tex_put(uint64_t k, id o); // a texture serial's object: live table for a live texture object or an external texture, else the ghost ring
|
||||
|
||||
// Live texture objects. Caller holds g_smu
|
||||
void own_tex_live_put_locked(const tex_t *t); // mtl_tex_init
|
||||
int own_tex_live_locked(uint64_t serial);
|
||||
int own_tex_live(uint64_t serial);
|
||||
int own_tex_end_locked(tex_t *t); // a finalize: -1 when t is a copy and the object the texture was initialized in still holds it (nothing ends), else whether it was live (it is not now)
|
||||
|
||||
// Who holds an object. Caller holds g_smu
|
||||
int serial_table_holds(const void *o); // a live entry or a texture pin
|
||||
int entry_holds(const void *o); // that, or a ghost entry
|
||||
int tex_object_pinned(id o);
|
||||
|
||||
// Every name of `from` (live and ghost entries, texture pins) moves to `to`, counts with them, in place (no table grows). Caller holds g_smu. → whether `serial` was one of them
|
||||
int own_rename_locked(id from, id to, uint64_t serial);
|
||||
|
||||
// Program pins (the caller holds the submission lock); mtl_prog_release and mtl_prog_unpin (nvn_obj.h) are the other two
|
||||
void own_prog_pin(uint64_t serial);
|
||||
|
||||
// The tables' sizes, and every object they hold (live entries first, then ghost ones). Caller holds g_smu
|
||||
typedef struct { uint32_t live, live_cap, ghost; uint64_t ghost_puts; } own_stats;
|
||||
own_stats own_stats_locked(void);
|
||||
void own_each_locked(void (^fn)(uint64_t serial, id o, int ghost));
|
||||
int own_check_locked(char *why, size_t n); // the held counts against the tables and pins: 1, or 0 with the first difference in why
|
||||
|
||||
// What the replay layer provides (nvn_mtl.m). Called with g_smu held
|
||||
void own_hook_drop(id o, int keep_resident); // an object the owner lets go: released after the submission in flight (keep_resident: it stays in the residency set, as something else holds it)
|
||||
int own_hook_shared(id o); // whether the sharing table holds it
|
||||
void own_hook_unheld(const void *o); // its last count in a table went (a retired object waiting for its holders resumes)
|
||||
void own_hook_changed(uint64_t serial); // a serial's object changed (0: no one serial)
|
||||
@@ -0,0 +1,180 @@
|
||||
// The serial tables' owner: nvn_owner.h says what it holds, what a lookup returns and the lock order. Moved from nvn_mtl.m as it was (only the hooks are new).
|
||||
#import <Metal/Metal.h>
|
||||
#include "nvn_owner.h"
|
||||
#include "../../host/dm.h"
|
||||
#include <mach/mach.h>
|
||||
|
||||
#define ODIE(...) do { dm_log("[dm] [nvn-mtl] FATAL " __VA_ARGS__); abort(); } while (0)
|
||||
|
||||
pthread_mutex_t g_smu = PTHREAD_MUTEX_INITIALIZER;
|
||||
static nvn_hmap g_by_serial = {.klen = 8}; // Serial → MTLTexture / NvPkg / NvProg
|
||||
// How many entries of g_by_serial hold each object (key: its address; a view's parents too, count_add), kept by every writer of g_by_serial (serial_put, serial_del, tex_put, the upgrade's in-place
|
||||
// replacement), so alias_take_over asks whether a live texture object still resolves to a shared texture in one lookup. It walked the serial table's whole capacity for
|
||||
// each candidate instead: in Ocarina of Time's cutscenes, with about 7,500 serials and thousands of shared textures, that walk was the submit thread's hottest function and
|
||||
// frames took up to a second. An entry holds its object (CFBridgingRetain), so a counted address cannot be reused while counted. Under g_smu
|
||||
static nvn_hmap g_by_serial_held = {.klen = sizeof(uintptr_t)};
|
||||
// The same count for the ghost ring (g_ghost, finalized serials a replayed record still resolves; tex_put and the upgrade's in-place replacement write it)
|
||||
static nvn_hmap g_ghost_held = {.klen = sizeof(uintptr_t)};
|
||||
// Texture serials owned by a live NVN texture object (TextureInitialize until Finalize or re-initialization, hle_nvn.c). Command records and texture pool registrations snapshot the
|
||||
// texture object (tex_snap in hle_nvn.c, mtl_reg_tex), so replay can resolve a texture the game has already finalized: Breath of the Wild records each frame's transient render targets
|
||||
// (the HUD captures, the 640x360 half-resolution buffers) and finalizes their objects before it submits. Those serials went back into g_by_serial and stayed there for good, as no second
|
||||
// Finalize comes: about 10 a frame, 200,000 after an hour in the first shrine. A dead serial resolves through g_ghost instead, a small table whose oldest entry goes when a new one comes,
|
||||
// so a record replayed again soon still finds its texture
|
||||
static nvn_hmap g_tex_live = {.klen = 8}; // serial → the texture object it was initialized in (mtl_tex_release), under g_smu
|
||||
static nvn_hmap g_ghost = {.klen = 8}; static uint64_t g_ghost_ring[NGHOST]; static unsigned g_ghost_next; static uint64_t g_ghost_puts;
|
||||
// Deferred command descriptors and persistent bindings retain a texture serial independently of the guest descriptor and the bounded recently-finalized cache
|
||||
typedef struct { unsigned refs; void *object; } tex_pin;
|
||||
static nvn_hmap g_tex_pins = {.klen = 8}, g_tex_pin_objects = {.klen = sizeof(uintptr_t)};
|
||||
|
||||
int serial_table_holds(const void *o) { uintptr_t k = (uintptr_t)o; return nvn_hm_get(&g_by_serial_held, &k) != NULL; }
|
||||
int entry_holds(const void *o) { uintptr_t k = (uintptr_t)o; return serial_table_holds(o) || (g_ghost_held.n && nvn_hm_get(&g_ghost_held, &k)); }
|
||||
int tex_object_pinned(id o) { uintptr_t key = (uintptr_t)(__bridge void *)o; return o && nvn_hm_get(&g_tex_pin_objects, &key) != NULL; }
|
||||
static void count_one(nvn_hmap *m, const void *o, int d) {
|
||||
if (!o) return;
|
||||
uintptr_t k = (uintptr_t)o, n = (uintptr_t)nvn_hm_get(m, &k);
|
||||
if (d > 0) nvn_hm_put(m, &k, (void *)(n + 1));
|
||||
else if (n > 1) nvn_hm_put(m, &k, (void *)(n - 1));
|
||||
else if (n) { nvn_hm_del(m, &k); own_hook_unheld(o); } // a slot written past these helpers (a test's table) was never counted: nothing to take away
|
||||
}
|
||||
// An entry that holds a view holds the textures the view is of as well, and they are counted with it: an sRGB / linear twin's serial holds a view of the shared texture
|
||||
// (tex_mtl_), so that texture is in use as long as the twin's entry is. Counted alone, the view left its parent looking unheld: a takeover evicted it under a live twin
|
||||
// (alias_take_over), and its retirement took it out of the residency set while pool tables still named the view (GitHub issue #2125). A view's parent never changes, so
|
||||
// what an entry adds it takes away
|
||||
static void count_add(nvn_hmap *m, const void *o, int d) {
|
||||
count_one(m, o, d);
|
||||
id t = (__bridge id)o;
|
||||
if (t && [t respondsToSelector:@selector(parentTexture)]) for (id<MTLTexture> p = ((id<MTLTexture>)t).parentTexture; p; p = p.parentTexture) count_one(m, (__bridge void *)p, d);
|
||||
}
|
||||
static void held_add(const void *o, int d) { count_add(&g_by_serial_held, o, d); }
|
||||
static void ghost_held_add(const void *o, int d) { count_add(&g_ghost_held, o, d); }
|
||||
static void obj_drop(id o) { own_hook_drop(o, tex_object_pinned(o) || own_hook_shared(o)); } // caller holds g_smu
|
||||
|
||||
const void *own_live_ref_locked(uint64_t k) { return nvn_hm_get(&g_by_serial, &k); }
|
||||
const void *own_live_ref(uint64_t k) { pthread_mutex_lock(&g_smu); const void *o = nvn_hm_get(&g_by_serial, &k); pthread_mutex_unlock(&g_smu); return o; }
|
||||
void serial_put(uint64_t k, id o) { pthread_mutex_lock(&g_smu); id old = hm_get_id(&g_by_serial, &k); if (old) { held_add((__bridge void *)old, -1); obj_drop(old); } hm_put_id(&g_by_serial, &k, o); held_add((__bridge void *)o, 1); own_hook_changed(k); pthread_mutex_unlock(&g_smu); }
|
||||
void serial_del(uint64_t k) { pthread_mutex_lock(&g_smu); id old = hm_get_id(&g_by_serial, &k); if (old) { held_add((__bridge void *)old, -1); obj_drop(old); hm_del_id(&g_by_serial, &k); own_hook_changed(k); } pthread_mutex_unlock(&g_smu); } // nothing to drop: the texture
|
||||
|
||||
void own_tex_live_put_locked(const tex_t *t) { nvn_hm_put(&g_tex_live, &t->serial, (void *)t); }
|
||||
int own_tex_live_locked(uint64_t serial) { return nvn_hm_get(&g_tex_live, &serial) != NULL; }
|
||||
int own_tex_live(uint64_t serial) { pthread_mutex_lock(&g_smu); int r = own_tex_live_locked(serial); pthread_mutex_unlock(&g_smu); return r; }
|
||||
int own_tex_end_locked(tex_t *t) {
|
||||
// NVNtexture is plain data, and engines finalize copies of it (Breath of the Wild about 100 a frame, Ocarina of Time every few frames, for textures whose own object lives on). A copy's Finalize ends
|
||||
// only the copy. Taken for the texture's end, it deleted the host texture, which the next read made and uploaded again: with a pool walk touching every ID (pool_gpu), 5,500 uploads and 500 MB to 1 GB
|
||||
// a frame in Ocarina of Time's Kokiri Forest, the batches' memory held until they retired. If the object the texture was initialized in no longer holds it (the game moved it), this was the last one
|
||||
const tex_t *own = nvn_hm_get(&g_tex_live, &t->serial);
|
||||
if (own && own != t) { // read through the kernel: the game may have freed and unmapped that object
|
||||
tex_t seen; vm_size_t got = 0;
|
||||
if (vm_read_overwrite(mach_task_self(), (vm_address_t)(uintptr_t)own, sizeof seen, (vm_address_t)(uintptr_t)&seen, &got) == KERN_SUCCESS && got == sizeof seen && seen.magic == MAGIC('T', 'E', 'X', 0) && seen.serial == t->serial) return -1;
|
||||
}
|
||||
return nvn_hm_del(&g_tex_live, &t->serial) != NULL;
|
||||
}
|
||||
|
||||
static void tex_pin_set(tex_pin *pin, id object) { // g_smu held
|
||||
id old = (__bridge id)pin->object; if (old == object) return;
|
||||
if (old) {
|
||||
uintptr_t key = (uintptr_t)pin->object, count = (uintptr_t)nvn_hm_get(&g_tex_pin_objects, &key);
|
||||
if (count > 1) nvn_hm_put(&g_tex_pin_objects, &key, (void *)(count - 1)); else nvn_hm_del(&g_tex_pin_objects, &key);
|
||||
held_add(pin->object, -1); own_hook_drop(old, tex_object_pinned(old) || own_hook_shared(old) || serial_table_holds((__bridge void *)old)); CFRelease(pin->object); pin->object = NULL;
|
||||
}
|
||||
if (object) {
|
||||
pin->object = (void *)CFBridgingRetain(object); uintptr_t key = (uintptr_t)pin->object;
|
||||
uintptr_t count = (uintptr_t)nvn_hm_get(&g_tex_pin_objects, &key);
|
||||
nvn_hm_put(&g_tex_pin_objects, &key, (void *)(count + 1)); held_add(pin->object, 1);
|
||||
}
|
||||
}
|
||||
void mtl_tex_pin(uint64_t serial) {
|
||||
if (!serial) return; pthread_mutex_lock(&g_smu);
|
||||
tex_pin *pin = nvn_hm_get(&g_tex_pins, &serial);
|
||||
if (!pin) { pin = calloc(1, sizeof *pin); if (!pin) ODIE("texture retention allocation failed"); nvn_hm_put(&g_tex_pins, &serial, pin); }
|
||||
pin->refs++;
|
||||
if (!pin->object) { id o = hm_get_id(&g_by_serial, &serial); if (!o) o = hm_get_id(&g_ghost, &serial); tex_pin_set(pin, o); }
|
||||
pthread_mutex_unlock(&g_smu);
|
||||
}
|
||||
void mtl_tex_unpin(uint64_t serial) {
|
||||
if (!serial) return; pthread_mutex_lock(&g_smu); tex_pin *pin = nvn_hm_get(&g_tex_pins, &serial);
|
||||
if (pin && !--pin->refs) { nvn_hm_del(&g_tex_pins, &serial); tex_pin_set(pin, nil); free(pin); }
|
||||
pthread_mutex_unlock(&g_smu);
|
||||
}
|
||||
const void *own_resolve_ref(uint64_t k) {
|
||||
pthread_mutex_lock(&g_smu); const void *o = nvn_hm_get(&g_by_serial, &k); if (!o && g_ghost.n) o = nvn_hm_get(&g_ghost, &k); if (!o) { tex_pin *pin = nvn_hm_get(&g_tex_pins, &k); if (pin) o = pin->object; } pthread_mutex_unlock(&g_smu); return o;
|
||||
}
|
||||
void tex_put(uint64_t k, id o) { // serials from 2^62 up are external textures (mtl_ext_tex), never initialized by the game
|
||||
pthread_mutex_lock(&g_smu);
|
||||
nvn_hmap *m = k >= (1ull << 62) || nvn_hm_get(&g_tex_live, &k) ? &g_by_serial : &g_ghost;
|
||||
id old = hm_get_id(m, &k);
|
||||
if (old) count_add(m == &g_by_serial ? &g_by_serial_held : &g_ghost_held, (__bridge void *)old, -1);
|
||||
if (old) obj_drop(old);
|
||||
else if (m == &g_ghost) {
|
||||
uint64_t *slot = &g_ghost_ring[g_ghost_next++ % NGHOST];
|
||||
if (*slot) { id ev = hm_get_id(&g_ghost, slot); if (ev) { ghost_held_add((__bridge void *)ev, -1); obj_drop(ev); } hm_del_id(&g_ghost, slot); own_hook_changed(*slot); } // the evicted serial resolves anew too
|
||||
*slot = k; g_ghost_puts++;
|
||||
}
|
||||
hm_put_id(m, &k, o); tex_pin *pin = nvn_hm_get(&g_tex_pins, &k); if (pin) tex_pin_set(pin, o); own_hook_changed(k);
|
||||
count_add(m == &g_by_serial ? &g_by_serial_held : &g_ghost_held, (__bridge void *)o, 1);
|
||||
pthread_mutex_unlock(&g_smu);
|
||||
}
|
||||
|
||||
int own_rename_locked(id from, id to, uint64_t serial) {
|
||||
nvn_hmap *tables[] = {&g_by_serial, &g_ghost}; int named = 0;
|
||||
for (unsigned j = 0; j < 2; j++) for (uint32_t i = 0; i < tables[j]->cap; i++) {
|
||||
if (tables[j]->vals[i] != (__bridge void *)from) continue;
|
||||
uint64_t k; memcpy(&k, tables[j]->keys + (size_t)i * tables[j]->klen, sizeof k);
|
||||
void *old = tables[j]->vals[i]; tables[j]->vals[i] = (void *)CFBridgingRetain(to);
|
||||
nvn_hmap *cnt = j == 0 ? &g_by_serial_held : &g_ghost_held; count_add(cnt, old, -1); count_add(cnt, (__bridge void *)to, 1); // the count moves with the name
|
||||
CFRelease(old); named |= k == serial;
|
||||
}
|
||||
for (uint32_t i = 0; i < g_tex_pins.cap; i++) if (g_tex_pins.vals[i]) { tex_pin *pin = g_tex_pins.vals[i]; if (pin->object == (__bridge void *)from) tex_pin_set(pin, to); }
|
||||
return named;
|
||||
}
|
||||
|
||||
// A submitted descriptor and a queue binding hold the compiled program by its immutable initialization serial. Retaining it must not mint serial aliases:
|
||||
// alias churn invalidates texture tables and changes composite-program keys
|
||||
typedef struct { unsigned refs, retired; } prog_pin;
|
||||
static nvn_hmap g_prog_pins = {.klen = 8}; // protected by the submission lock
|
||||
void mtl_prog_release(prog_t *p) {
|
||||
if (!p->serial) return;
|
||||
prog_pin *pin = nvn_hm_get(&g_prog_pins, &p->serial);
|
||||
if (pin) pin->retired = 1; else serial_del(p->serial);
|
||||
}
|
||||
void mtl_prog_unpin(prog_t *p) {
|
||||
prog_pin *pin = nvn_hm_get(&g_prog_pins, &p->serial);
|
||||
if (pin && !--pin->refs) { nvn_hm_del(&g_prog_pins, &p->serial); if (pin->retired) serial_del(p->serial); free(pin); }
|
||||
}
|
||||
void own_prog_pin(uint64_t serial) {
|
||||
prog_pin *pin = nvn_hm_get(&g_prog_pins, &serial);
|
||||
if (!pin) { pin = calloc(1, sizeof *pin); if (!pin) ODIE("program retention allocation failed"); nvn_hm_put(&g_prog_pins, &serial, pin); }
|
||||
pin->refs++;
|
||||
}
|
||||
|
||||
own_stats own_stats_locked(void) { return (own_stats){.live = g_by_serial.n, .live_cap = g_by_serial.cap, .ghost = g_ghost.n, .ghost_puts = g_ghost_puts}; }
|
||||
void own_each_locked(void (^fn)(uint64_t serial, id o, int ghost)) {
|
||||
const nvn_hmap *tables[] = {&g_by_serial, &g_ghost};
|
||||
for (int j = 0; j < 2; j++) for (uint32_t i = 0; i < tables[j]->cap; i++) {
|
||||
if (!tables[j]->vals[i]) continue;
|
||||
uint64_t k; memcpy(&k, tables[j]->keys + (size_t)i * 8, 8); fn(k, (__bridge id)tables[j]->vals[i], j);
|
||||
}
|
||||
}
|
||||
static void expect_one(CFMutableDictionaryRef want, id o) { // o, and each texture it is a view of, once more
|
||||
for (; o; o = [o respondsToSelector:@selector(parentTexture)] ? ((id<MTLTexture>)o).parentTexture : nil)
|
||||
CFDictionarySetValue(want, (__bridge const void *)o, (const void *)((uintptr_t)CFDictionaryGetValue(want, (__bridge const void *)o) + 1));
|
||||
}
|
||||
static int counts_match(const char *what, CFMutableDictionaryRef want, const nvn_hmap *held, char *why, size_t n) {
|
||||
for (uint32_t i = 0; i < held->cap; i++) {
|
||||
if (!held->vals[i]) continue;
|
||||
uintptr_t k; memcpy(&k, held->keys + (size_t)i * held->klen, sizeof k);
|
||||
uintptr_t w = (uintptr_t)CFDictionaryGetValue(want, (const void *)k);
|
||||
if (w != (uintptr_t)held->vals[i]) { snprintf(why, n, "%s count of %#lx: %lu, the tables say %lu", what, (unsigned long)k, (unsigned long)(uintptr_t)held->vals[i], (unsigned long)w); return 0; }
|
||||
}
|
||||
if ((CFIndex)held->n != CFDictionaryGetCount(want)) { snprintf(why, n, "%s counts: %u objects, the tables hold %ld", what, held->n, (long)CFDictionaryGetCount(want)); return 0; }
|
||||
return 1;
|
||||
}
|
||||
int own_check_locked(char *why, size_t n) {
|
||||
CFMutableDictionaryRef live = CFDictionaryCreateMutable(NULL, 0, NULL, NULL), ghost = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
|
||||
for (uint32_t i = 0; i < g_by_serial.cap; i++) if (g_by_serial.vals[i]) expect_one(live, (__bridge id)g_by_serial.vals[i]);
|
||||
for (uint32_t i = 0; i < g_tex_pins.cap; i++) { tex_pin *pin = g_tex_pins.vals[i]; if (pin && pin->object) expect_one(live, (__bridge id)pin->object); }
|
||||
for (uint32_t i = 0; i < g_ghost.cap; i++) if (g_ghost.vals[i]) expect_one(ghost, (__bridge id)g_ghost.vals[i]);
|
||||
int ok = counts_match("live", live, &g_by_serial_held, why, n) && counts_match("ghost", ghost, &g_ghost_held, why, n);
|
||||
if (ok && g_ghost.n > NGHOST) { snprintf(why, n, "%u ghost serials, more than the ring's %d", g_ghost.n, NGHOST); ok = 0; }
|
||||
CFRelease(live); CFRelease(ghost);
|
||||
return ok;
|
||||
}
|
||||
@@ -471,6 +471,9 @@ check_t_upgrade_alias: RUN_ENV = MTL_DEBUG_LAYER=1
|
||||
t_alias_takeover: t_alias_takeover.m $(MTL_T_DEP)
|
||||
clang $(CFLAGS) -fobjc-arc -o $@ t_alias_takeover.m $(MTL_T_SRC) $(FW)
|
||||
check_t_alias_takeover: RUN_ENV = MTL_DEBUG_LAYER=1
|
||||
# The serial tables' owner alone: nvn_owner.m with this test's stand-ins for the replay layer's hooks, no nvn_mtl.m
|
||||
t_serial_owner: t_serial_owner.m ../nvn/nvn_owner.m ../nvn/nvn_owner.h ../nvn/nvn_hmap.h ../nvn/nvn_obj.h $(GAME_H)
|
||||
clang $(CFLAGS) -fobjc-arc -o $@ t_serial_owner.m ../nvn/nvn_owner.m -framework Metal -framework Foundation
|
||||
t_partial_upload: t_partial_upload.m $(MTL_T_DEP)
|
||||
clang $(CFLAGS) -fobjc-arc -o $@ t_partial_upload.m $(MTL_T_SRC) $(FW)
|
||||
check_t_partial_upload: RUN_ENV = MTL_DEBUG_LAYER=1
|
||||
|
||||
@@ -379,12 +379,14 @@ int main(int argc, char **argv) { @autoreleasepool {
|
||||
cap_buf_free(&mine);
|
||||
if (g_td_frame == (long)frame_no && !g_td_done) td_dump();
|
||||
if (rep == 0) { // textures the GPU writes that the frame made itself (no captured content): a second replay starts from what this one left in them
|
||||
uint64_t n = 0, bytes = 0; nvn_hmap *maps[2] = {&g_by_serial, &g_ghost};
|
||||
for (int m = 0; m < 2; m++) for (uint32_t i = 0; i < maps[m]->cap; i++) { if (!maps[m]->vals[i]) continue; id o = (__bridge id)maps[m]->vals[i];
|
||||
if (![o conformsToProtocol:@protocol(MTLTexture)]) continue; id<MTLTexture> t = o; if (t.parentTexture) t = t.parentTexture;
|
||||
uint64_t k = (uint64_t)(uintptr_t)(__bridge void *)t; if (t.buffer || nvn_hm_get(&g_filled, &k)) continue;
|
||||
uint64_t ser; memcpy(&ser, maps[m]->keys + (size_t)i * 8, 8); n++; bytes += t.allocatedSize;
|
||||
if (opt_made) printf(" made by the frame: ser=%llu %lux%lux%lu format %lu levels %lu%s\n", (unsigned long long)ser, (unsigned long)t.width, (unsigned long)t.height, (unsigned long)t.arrayLength, (unsigned long)t.pixelFormat, (unsigned long)t.mipmapLevelCount, m ? " (ghost)" : ""); }
|
||||
__block uint64_t n = 0, bytes = 0;
|
||||
pthread_mutex_lock(&g_smu);
|
||||
own_each_locked(^(uint64_t ser, id o, int ghost) {
|
||||
if (![o conformsToProtocol:@protocol(MTLTexture)]) return; id<MTLTexture> t = o; if (t.parentTexture) t = t.parentTexture;
|
||||
uint64_t k = (uint64_t)(uintptr_t)(__bridge void *)t; if (t.buffer || nvn_hm_get(&g_filled, &k)) return;
|
||||
n++; bytes += t.allocatedSize;
|
||||
if (opt_made) printf(" made by the frame: ser=%llu %lux%lux%lu format %lu levels %lu%s\n", (unsigned long long)ser, (unsigned long)t.width, (unsigned long)t.height, (unsigned long)t.arrayLength, (unsigned long)t.pixelFormat, (unsigned long)t.mipmapLevelCount, ghost ? " (ghost)" : ""); });
|
||||
pthread_mutex_unlock(&g_smu);
|
||||
printf("render targets and images the frame made without captured content: %llu (%.1f MB)\n", (unsigned long long)n, bytes / 1048576.0);
|
||||
}
|
||||
if (1) { // pool blocks after the frame against the live run's: written live but not here = the CPU wrote them while the frame ran (or a GPU write the replay missed)
|
||||
|
||||
@@ -69,9 +69,9 @@ int main(void) { @autoreleasepool {
|
||||
pthread_mutex_lock(&g_smu);
|
||||
uint32_t before = g_alias.n; uint64_t t0 = bp_now();
|
||||
alias_take_over(&nk);
|
||||
double ms = (bp_now() - t0) / 1e6; uint32_t after = g_alias.n;
|
||||
double ms = (bp_now() - t0) / 1e6; uint32_t after = g_alias.n, nser = own_stats_locked().live;
|
||||
pthread_mutex_unlock(&g_smu);
|
||||
printf("a takeover past %d held entries with %u serials in the table: %.3f ms\n", NA, g_by_serial.n, ms);
|
||||
printf("a takeover past %d held entries with %u serials in the table: %.3f ms\n", NA, nser, ms);
|
||||
CHECK(after == before, "held entries are kept");
|
||||
CHECK(ms < 1, "the takeover asks each entry in one lookup (under 1 ms; walking the serial table for each took 4.3 ms on an M5 Max)");
|
||||
for (int i = 0; i < NS; i++) serial_del((1ull << 62) + 100 + i);
|
||||
|
||||
@@ -294,9 +294,9 @@ int main(int argc, const char **argv) { @autoreleasepool {
|
||||
cpy = own; mtl_tex_release(&cpy);
|
||||
CHECK(tex_cpu_written(own.serial) == 1, "finalizing a copy leaves the texture live");
|
||||
mtl_tex_release(&own);
|
||||
CHECK(!nvn_hm_get(&g_tex_live, &own.serial), "finalizing the object itself ends it");
|
||||
CHECK(!own_tex_live(own.serial), "finalizing the object itself ends it");
|
||||
own.serial = 900002; mtl_tex_init(&own); moved = own; own.magic = 0; mtl_tex_release(&moved);
|
||||
CHECK(!nvn_hm_get(&g_tex_live, &moved.serial), "a copy outliving the object it was initialized in ends the texture");
|
||||
CHECK(!own_tex_live(moved.serial), "a copy outliving the object it was initialized in ends the texture");
|
||||
retired_classification(); descriptor_replacement();
|
||||
if (argc > 1 && !strcmp(argv[1], "--metal")) { mtl_init(); finalized_upload(0); finalized_upload(1); finalize_during_walk(); poll_asks_again(); poll_beside(); handover_holders(); }
|
||||
printf("t_cpu_tex: %d checks, %d failed\n", checks, failed); return failed != 0;
|
||||
|
||||
@@ -443,7 +443,7 @@ static void replay(int mode) {
|
||||
p_draw d={.prim=0,.first=7,.count=kind==6?9000:3,.instances=2,.base_instance=5};
|
||||
if((kind>=1&&kind<=3)||kind==5) {
|
||||
int type=kind==5?1:kind-1;uint32_t ix[]={12,10,UINT32_MAX,11};
|
||||
for(int i=0;i<4;i++)memcpy(commands+8192+(i<<type),ix+i,1<<type);
|
||||
for(int i=0;i<4;i++)memcpy(commands+8192+(i<<type),ix+i,1<<type); // NOLINT(clang-analyzer-unix.cstring.UninitializedRead): 1 << type is 1, 2 or 4 bytes of an initialized uint32_t (type 0 to 2), never 0
|
||||
p_prim_restart restart={1,UINT32_MAX};EMIT(OP_PRIM_RESTART,restart);
|
||||
p_draw_idx di={.prim=0,.itype=type,.count=4,.instances=2,.base_vertex=-3,.base_instance=5,.ibuf=(uint64_t)(commands+8192)};
|
||||
if(kind==5){uint32_t args[]={4,2,mode==17?16:0,(uint32_t)-3,5};memcpy(commands+16384,args,sizeof args);di.ind=(uint64_t)(commands+16384);if(mode==17)di.ibuf-=32;}
|
||||
@@ -543,7 +543,7 @@ static void replay_local(int mode) {
|
||||
if((mode>=1&&mode<=3)||mode==5||mode>=7) {
|
||||
int type=mode>=7||mode==5?1:mode-1;uint32_t ix[]={mode==10?80:12,10,UINT32_MAX,11,mode==10?80:12,10};
|
||||
int index_count=mode>=9?6:4;
|
||||
for(int i=0;i<index_count;i++)memcpy(commands+8192+(i<<type),ix+i,1<<type);
|
||||
for(int i=0;i<index_count;i++)memcpy(commands+8192+(i<<type),ix+i,1<<type); // NOLINT(clang-analyzer-unix.cstring.UninitializedRead): 1 << type is 1, 2 or 4 bytes of an initialized uint32_t (type 0 to 2), never 0
|
||||
if(mode==7||mode==8) {
|
||||
memcpy(commands+32768,commands+8192,8); memset(commands+8192,0,8);
|
||||
if(mode==7) { struct {uint64_t src,dst,n;} copy={(uint64_t)(commands+32768),(uint64_t)(commands+8192),8};EMIT(OP_COPY_B2B,copy); }
|
||||
|
||||
@@ -182,7 +182,7 @@ int main(void) { @autoreleasepool {
|
||||
mtl_reg_tex(8, (const tex_t *)&t8, NULL);
|
||||
id<MTLBuffer> b15 = table();
|
||||
uint64_t s8 = ((const tex_t *)&t8)->serial;
|
||||
pthread_mutex_lock(&g_smu); id<MTLTexture> v8 = hm_get_id(&g_by_serial, &s8); pthread_mutex_unlock(&g_smu);
|
||||
id<MTLTexture> v8 = serial_get(s8);
|
||||
id<MTLTexture> base8 = v8.parentTexture;
|
||||
CHECK(v8 && base8 && entry(b15, 8) != dummy_of(b15), "ID 8's serial resolves to a view of the shared texture");
|
||||
table();
|
||||
@@ -193,7 +193,7 @@ int main(void) { @autoreleasepool {
|
||||
mtl_reg_tex(9, (const tex_t *)&t9, NULL);
|
||||
builds = g_stat_pool_builds; taken = g_alias_taken; ep = g_treg_ep;
|
||||
id<MTLBuffer> b16 = table();
|
||||
pthread_mutex_lock(&g_smu); id<MTLTexture> now8 = hm_get_id(&g_by_serial, &s8); int base_shared = CFSetContainsValue(g_alias_set, (__bridge const void *)base8), base_held = serial_table_holds((__bridge void *)base8); pthread_mutex_unlock(&g_smu);
|
||||
pthread_mutex_lock(&g_smu); id<MTLTexture> now8 = (__bridge id)own_live_ref_locked(s8); int base_shared = CFSetContainsValue(g_alias_set, (__bridge const void *)base8), base_held = serial_table_holds((__bridge void *)base8); pthread_mutex_unlock(&g_smu);
|
||||
CHECK(base_held && g_alias_taken == taken && base_shared, "the twin's entry holds its view's parent, and texture 9 over the same storage leaves it shared");
|
||||
CHECK(now8 == v8 && g_treg_ep == ep, "ID 8's serial keeps its view, and no ID went down the slow path");
|
||||
CHECK(g_stat_pool_builds == builds && entry(b16, 8) == entry(b15, 8) && entry(b16, 9) != dummy_of(b16), "no table is built: the patch keeps ID 8's view and gives ID 9 its texture");
|
||||
|
||||
@@ -0,0 +1,200 @@
|
||||
// The serial tables' owner (nvn_owner.m) on its own: linked without the replay layer, whose four hooks are recorded here instead (what is dropped and whether it stays resident,
|
||||
// what the sharing table holds, which object's last count went, which serial changed). What it holds to, as the tables behaved before they moved out of nvn_mtl.m:
|
||||
// a live serial's registration, replacement and removal; where tex_put sends a serial (live table for a live texture object or an external texture from 2^62 up, ghost ring for a
|
||||
// finalized one); a finalize, and a copy's finalize that ends nothing; ghost replacement in place and eviction at NGHOST; texture pins and program pins; a view's entry counting its
|
||||
// parent; renaming an object everywhere; and after every step the held counts against the tables (own_check_locked). The owner releases nothing itself: everything it lets go goes
|
||||
// to own_hook_drop, which nvn_mtl.m releases once the submission in flight completes, so what a lookup returned outlives the change that dropped it.
|
||||
// DM_TEST_SERIAL_TWIN=1 asserts what serial_del should do for a finalized texture serial, which it does not yet (the twin path of tex_mtl_: tex_put sends a finalized serial's view to
|
||||
// the ghost ring, and serial_del, called when the view has to go, removes only a live entry). Without it the run prints what it found and counts no failure for it
|
||||
#import <Metal/Metal.h>
|
||||
#include "../nvn/nvn_owner.h"
|
||||
#include <stdio.h>
|
||||
|
||||
static int checks, failed;
|
||||
#define CHECK(c, label) do { checks++; if (!(c)) { failed++; fprintf(stderr, "FAIL: %s (line %d)\n", label, __LINE__); } } while (0)
|
||||
|
||||
// The replay layer's side, recorded
|
||||
static NSMutableArray *g_dropped; // what the owner let go, kept alive here as the deferred release keeps it until the submission completes
|
||||
static NSMutableArray<NSNumber *> *g_dropped_keep;
|
||||
static NSHashTable *g_shared; // stands for the sharing table
|
||||
static NSMutableArray<NSValue *> *g_unheld;
|
||||
static uint64_t g_changed_last; static int g_changed_n;
|
||||
void own_hook_drop(id o, int keep_resident) { [g_dropped addObject:o]; [g_dropped_keep addObject:@(keep_resident)]; }
|
||||
int own_hook_shared(id o) { return [g_shared containsObject:o]; }
|
||||
void own_hook_unheld(const void *o) { [g_unheld addObject:[NSValue valueWithPointer:o]]; }
|
||||
void own_hook_changed(uint64_t serial) { g_changed_last = serial; g_changed_n++; }
|
||||
static int dropped(id o, int keep) { for (NSUInteger i = 0; i < g_dropped.count; i++) if (g_dropped[i] == o && g_dropped_keep[i].intValue == keep) return 1; return 0; }
|
||||
static int unheld(id o) { for (NSValue *v in g_unheld) if (v.pointerValue == (__bridge void *)o) return 1; return 0; }
|
||||
|
||||
static int live(uint64_t k) { return serial_get(k) != nil; }
|
||||
static int ghost(uint64_t k) { __block int r = 0; pthread_mutex_lock(&g_smu); own_each_locked(^(uint64_t s, id o, int g) { (void)o; if (s == k && g) r = 1; }); pthread_mutex_unlock(&g_smu); return r; }
|
||||
static int holds(id o) { pthread_mutex_lock(&g_smu); int r = entry_holds((__bridge void *)o); pthread_mutex_unlock(&g_smu); return r; }
|
||||
static int live_holds(id o) { pthread_mutex_lock(&g_smu); int r = serial_table_holds((__bridge void *)o); pthread_mutex_unlock(&g_smu); return r; }
|
||||
static void step(const char *label) {
|
||||
char why[256] = ""; pthread_mutex_lock(&g_smu); int ok = own_check_locked(why, sizeof why); pthread_mutex_unlock(&g_smu);
|
||||
checks++; if (!ok) { failed++; fprintf(stderr, "FAIL: held counts against the tables after %s: %s\n", label, why); }
|
||||
}
|
||||
static tex_t texture(uint64_t serial) { return (tex_t){.magic = MAGIC('T','E','X',0), .serial = serial}; }
|
||||
static void tex_live(tex_t *t) { pthread_mutex_lock(&g_smu); own_tex_live_put_locked(t); pthread_mutex_unlock(&g_smu); } // mtl_tex_init's part
|
||||
static int tex_end(tex_t *t) { pthread_mutex_lock(&g_smu); int r = own_tex_end_locked(t); pthread_mutex_unlock(&g_smu); if (r >= 0) serial_del(t->serial); return r; } // mtl_tex_release's part
|
||||
|
||||
int main(void) { @autoreleasepool {
|
||||
g_dropped = [NSMutableArray array]; g_dropped_keep = [NSMutableArray array]; g_unheld = [NSMutableArray array];
|
||||
g_shared = [NSHashTable hashTableWithOptions:NSPointerFunctionsObjectPointerPersonality];
|
||||
step("start");
|
||||
|
||||
{ // a live serial: registration, lookup, replacement, removal
|
||||
uint64_t k = 93001; id a = [NSObject new], b = [NSObject new];
|
||||
int notes = g_changed_n;
|
||||
serial_put(k, a);
|
||||
CHECK(live(k) && serial_get(k) == a && tex_get(k) == a && live_holds(a), "serial_put: the serial resolves to its object, which is held");
|
||||
CHECK(g_changed_n == notes + 1 && g_changed_last == k, "serial_put reports the serial's change");
|
||||
step("serial_put");
|
||||
__weak id wa = a; a = nil;
|
||||
serial_put(k, b);
|
||||
CHECK(serial_get(k) == b && live_holds(b) && !live_holds(wa), "replacement: the new object is held, the old one no longer");
|
||||
CHECK(dropped(wa, 0) && wa != nil, "replacement: the old object goes to the replay layer's deferred release, not released by the owner");
|
||||
CHECK(unheld(wa), "replacement: the old object's last count went, which the replay layer hears (a retired object waiting for its holders)");
|
||||
step("replacement");
|
||||
serial_del(k);
|
||||
CHECK(!live(k) && !tex_get(k) && !live_holds(b) && dropped(b, 0), "serial_del: nothing resolves the serial, its object is dropped and not held");
|
||||
step("serial_del");
|
||||
notes = g_changed_n; serial_del(k);
|
||||
CHECK(g_changed_n == notes, "serial_del of a serial with no entry changes and reports nothing");
|
||||
step("serial_del again");
|
||||
id s = [NSObject new]; [g_shared addObject:s];
|
||||
serial_put(k, s); serial_del(k);
|
||||
CHECK(dropped(s, 1), "an object the sharing table holds stays resident when its entry goes");
|
||||
[g_shared removeObject:s];
|
||||
step("shared object");
|
||||
}
|
||||
|
||||
{ // one object under two serials: held until both go
|
||||
id o = [NSObject new]; uint64_t s1 = (1ull << 62) + 93011, s2 = s1 + 1;
|
||||
tex_put(s1, o); tex_put(s2, o);
|
||||
serial_del(s1);
|
||||
CHECK(live_holds(o) && !unheld(o), "an object two serials name stays held when one of them goes");
|
||||
serial_del(s2);
|
||||
CHECK(!holds(o) && unheld(o), "and is not held once both have gone");
|
||||
step("two serials");
|
||||
}
|
||||
|
||||
{ // where tex_put sends a texture serial, finalizing, and a copy's finalize
|
||||
uint64_t ext = (1ull << 62) + 93021, fk = 93023; tex_t lt = texture(93022); tex_live(<);
|
||||
id e = [NSObject new], l = [NSObject new], f = [NSObject new];
|
||||
tex_put(ext, e); tex_put(lt.serial, l); tex_put(fk, f);
|
||||
CHECK(live(ext) && !ghost(ext), "an external texture (2^62 and up) goes to the live table");
|
||||
CHECK(live(lt.serial) && !ghost(lt.serial) && own_tex_live(lt.serial), "a live texture object's serial goes to the live table");
|
||||
CHECK(!live(fk) && ghost(fk) && tex_get(fk) == f && !serial_get(fk), "a finalized serial goes to the ghost ring: tex_get resolves it, serial_get (live only) does not");
|
||||
CHECK(holds(f) && !live_holds(f), "a ghost entry holds its object, counted apart from the live table");
|
||||
step("tex_put");
|
||||
pthread_mutex_lock(&g_smu); uint64_t puts = own_stats_locked().ghost_puts; pthread_mutex_unlock(&g_smu);
|
||||
id f2 = [NSObject new]; tex_put(fk, f2);
|
||||
pthread_mutex_lock(&g_smu); uint64_t puts2 = own_stats_locked().ghost_puts; pthread_mutex_unlock(&g_smu);
|
||||
CHECK(tex_get(fk) == f2 && !holds(f) && dropped(f, 0) && puts2 == puts, "a ghost serial put again replaces its object in place, taking no new ring slot");
|
||||
step("ghost replacement");
|
||||
tex_t copy = lt;
|
||||
CHECK(tex_end(©) == -1 && own_tex_live(lt.serial) && live(lt.serial), "a copy's finalize ends nothing while the object the texture was initialized in holds it");
|
||||
CHECK(tex_end(<) == 1 && !own_tex_live(lt.serial) && !live(lt.serial) && !ghost(lt.serial) && !holds(l), "the object's own finalize ends the texture; it does not move to the ghost ring");
|
||||
step("finalize");
|
||||
tex_t own = texture(93024), moved; tex_live(&own); moved = own; own.magic = 0; // the object the texture was initialized in no longer holds it (the game moved or freed it)
|
||||
CHECK(tex_end(&moved) == 1 && !own_tex_live(moved.serial), "a copy outliving the object it was initialized in ends the texture");
|
||||
tex_put(lt.serial, l);
|
||||
CHECK(ghost(lt.serial) && tex_get(lt.serial) == l, "the finalized serial put again (a replayed record) goes to the ghost ring");
|
||||
step("tex_put after finalize");
|
||||
serial_del(ext);
|
||||
}
|
||||
|
||||
{ // the ghost ring: the oldest serial goes when NGHOST newer ones have come
|
||||
enum { BASE = 940000 };
|
||||
NSMutableArray *objs = [NSMutableArray array];
|
||||
for (int i = 0; i <= NGHOST; i++) { id o = [NSObject new]; [objs addObject:o]; tex_put(BASE + (uint64_t)i, o); }
|
||||
CHECK(!ghost(BASE) && !tex_get(BASE) && !holds(objs[0]) && dropped(objs[0], 0), "the oldest ghost serial is evicted by the NGHOST + 1st: dropped, no longer held");
|
||||
CHECK(ghost(BASE + 1) && tex_get(BASE + NGHOST) == objs[NGHOST], "the newer ones stay");
|
||||
pthread_mutex_lock(&g_smu); own_stats st = own_stats_locked(); pthread_mutex_unlock(&g_smu);
|
||||
CHECK(st.ghost <= NGHOST, "the ring holds at most NGHOST serials");
|
||||
step("ghost eviction");
|
||||
}
|
||||
|
||||
{ // texture pins
|
||||
uint64_t k = (1ull << 62) + 93031; id o = [NSObject new], o2 = [NSObject new];
|
||||
tex_put(k, o);
|
||||
mtl_tex_pin(k); mtl_tex_pin(k);
|
||||
pthread_mutex_lock(&g_smu); int pinned = tex_object_pinned(o); pthread_mutex_unlock(&g_smu);
|
||||
CHECK(pinned && live_holds(o), "a pinned object is held and marked pinned (no takeover or retirement drops it)");
|
||||
step("pin");
|
||||
serial_del(k);
|
||||
CHECK(!live(k) && tex_get(k) == o && live_holds(o) && dropped(o, 1), "after its entry goes, the pinned serial still resolves to its object, which stays resident");
|
||||
step("pinned serial_del");
|
||||
tex_put(k, o2);
|
||||
pthread_mutex_lock(&g_smu); int moved = tex_object_pinned(o2) && !tex_object_pinned(o); pthread_mutex_unlock(&g_smu);
|
||||
CHECK(tex_get(k) == o2 && moved && !live_holds(o), "tex_put moves the pin to the new object");
|
||||
step("pin follows tex_put");
|
||||
mtl_tex_unpin(k);
|
||||
CHECK(tex_get(k) == o2, "one of two pins goes: the pin stays");
|
||||
serial_del(k); mtl_tex_unpin(k);
|
||||
CHECK(!tex_get(k) && !holds(o2), "the last unpin of a serial with no entry: it resolves to nothing, its object is not held");
|
||||
step("unpin");
|
||||
uint64_t k2 = (1ull << 62) + 93032; id p = [NSObject new];
|
||||
mtl_tex_pin(k2);
|
||||
CHECK(!tex_get(k2), "a pin on a serial with no object yet resolves to nothing");
|
||||
tex_put(k2, p); serial_del(k2);
|
||||
CHECK(tex_get(k2) == p, "and takes the object tex_put gives the serial later");
|
||||
mtl_tex_unpin(k2);
|
||||
step("pin before the object");
|
||||
}
|
||||
|
||||
{ // program pins (the caller holds the submission lock; this test has one thread)
|
||||
uint64_t ps = 93041; id pr = [NSObject new]; prog_t p = {.serial = ps};
|
||||
serial_put(ps, pr); mtl_prog_release(&p);
|
||||
CHECK(!live(ps), "an unpinned program's release removes its entry at once");
|
||||
serial_put(ps, pr); own_prog_pin(ps); own_prog_pin(ps);
|
||||
mtl_prog_release(&p);
|
||||
CHECK(serial_get(ps) == pr, "a pinned program's release keeps its entry");
|
||||
mtl_prog_unpin(&p);
|
||||
CHECK(live(ps), "while one pin remains");
|
||||
mtl_prog_unpin(&p);
|
||||
CHECK(!live(ps), "the last unpin of a released program removes its entry");
|
||||
serial_put(ps, pr); own_prog_pin(ps); mtl_prog_unpin(&p);
|
||||
CHECK(live(ps), "an unpin of a program never released leaves its entry");
|
||||
serial_del(ps);
|
||||
step("program pins");
|
||||
}
|
||||
|
||||
{ // a view counts its parent; renaming an object moves every name and count
|
||||
id<MTLDevice> dev = MTLCreateSystemDefaultDevice();
|
||||
MTLTextureDescriptor *d = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm width:4 height:4 mipmapped:NO];
|
||||
id<MTLTexture> base = [dev newTextureWithDescriptor:d], base2 = [dev newTextureWithDescriptor:d];
|
||||
id<MTLTexture> view = [base newTextureViewWithPixelFormat:MTLPixelFormatRGBA8Unorm_sRGB];
|
||||
CHECK(dev && base && base2 && view, "two textures and an sRGB view of the first");
|
||||
if (view && base2) {
|
||||
uint64_t k = (1ull << 62) + 93051;
|
||||
tex_put(k, view);
|
||||
CHECK(live_holds(view) && live_holds(base), "an entry holding a view holds its parent too");
|
||||
step("view");
|
||||
serial_del(k);
|
||||
CHECK(!holds(view) && !holds(base), "and lets both go with it");
|
||||
step("view removed");
|
||||
uint64_t a = (1ull << 62) + 93052, b = 93053, c = (1ull << 62) + 93054;
|
||||
tex_put(a, base); tex_put(b, base); mtl_tex_pin(c); tex_put(c, base); serial_del(c); // a live name, a ghost name and a pin
|
||||
pthread_mutex_lock(&g_smu); int named = own_rename_locked(base, base2, b); pthread_mutex_unlock(&g_smu);
|
||||
CHECK(named && tex_get(a) == base2 && tex_get(b) == base2 && tex_get(c) == base2, "a rename moves the live name, the ghost name and the pin");
|
||||
CHECK(!holds(base) && live_holds(base2) && holds(base2), "and the counts with them");
|
||||
step("rename");
|
||||
serial_del(a); mtl_tex_unpin(c);
|
||||
}
|
||||
}
|
||||
|
||||
{ // the finalized twin: a finalized serial's view is put in the ghost ring, then has to go, and serial_del removes only a live entry
|
||||
uint64_t k = 93061; id v = [NSObject new];
|
||||
tex_put(k, v); // as tex_mtl_ does for a twin view of a texture whose object is finalized
|
||||
serial_del(k); // as it does when the shared texture left the sharing table meanwhile (kept == 0)
|
||||
int stays = tex_get(k) == v && ghost(k) && holds(v);
|
||||
if (getenv("DM_TEST_SERIAL_TWIN")) CHECK(!stays, "serial_del of a finalized texture serial: it resolves to nothing and its view is not held");
|
||||
else printf("finalized twin: serial_del %s the ghost entry (DM_TEST_SERIAL_TWIN=1 asserts that it goes)\n", stays ? "leaves" : "removes");
|
||||
step("finalized twin");
|
||||
}
|
||||
|
||||
printf("serial owner: %d checks, %d failed\n", checks, failed);
|
||||
return failed != 0;
|
||||
} }
|
||||
@@ -26,12 +26,14 @@ int main(void) { @autoreleasepool {
|
||||
CHECK(tex_mtl(&a) == old && tex_mtl(&b) == old && tex_mtl(&ghost) == old, "IMAGE usage does not split storage across existing descriptors");
|
||||
// Exercise replacement at the hash table's growth threshold: reinserting while iterating would resize and reshuffle it.
|
||||
uint64_t extra = 99400;
|
||||
while ((g_by_serial.n + 1) * 4 <= g_by_serial.cap * 3) { hm_put_id(&g_by_serial, &extra, old); extra++; }
|
||||
uint32_t capacity = g_by_serial.cap, count = g_by_serial.n;
|
||||
pthread_mutex_lock(&g_smu); own_stats st = own_stats_locked(); pthread_mutex_unlock(&g_smu);
|
||||
while ((st.live + 1) * 4 <= st.live_cap * 3) { tex_put((1ull << 62) + extra, old); extra++; pthread_mutex_lock(&g_smu); st = own_stats_locked(); pthread_mutex_unlock(&g_smu); } // live entries (external serials), counted as the owner counts them
|
||||
uint32_t capacity = st.live_cap, count = st.live;
|
||||
id<MTLTexture> upgraded = tex_upgrade_pfv(&a, old, MTLPixelFormatR32Uint);
|
||||
CHECK(upgraded && upgraded != old, "usage upgrade creates a replacement");
|
||||
CHECK(g_by_serial.cap == capacity && g_by_serial.n == count, "replacement does not grow or reshape the serial table");
|
||||
int all = 1; for (uint64_t serial = 99400; serial < extra; serial++) all &= tex_get(serial) == upgraded;
|
||||
pthread_mutex_lock(&g_smu); st = own_stats_locked(); pthread_mutex_unlock(&g_smu);
|
||||
CHECK(st.live_cap == capacity && st.live == count, "replacement does not grow or reshape the serial table");
|
||||
int all = 1; for (uint64_t serial = 99400; serial < extra; serial++) all &= tex_get((1ull << 62) + serial) == upgraded;
|
||||
CHECK(all, "every shared serial at the growth threshold sees the replacement");
|
||||
CHECK(tex_get(b.serial) == upgraded, "other live serial sees the replacement");
|
||||
CHECK(tex_get(ghost.serial) == upgraded, "finalized serial sees the replacement");
|
||||
|
||||
@@ -41,7 +41,6 @@ dm/gfx/tests/t_defer_chain.m clang-analyzer-nullability.NullablePassedToNonnull
|
||||
dm/gfx/tests/t_float_pad.m bugprone-suspicious-memory-comparison 1
|
||||
dm/gfx/tests/t_geometry.m clang-analyzer-nullability.NullablePassedToNonnull 8
|
||||
dm/gfx/tests/t_geometry.m clang-analyzer-security.ArrayBound 1
|
||||
dm/gfx/tests/t_geometry.m clang-analyzer-unix.cstring.UninitializedRead 1
|
||||
dm/gfx/tests/t_geometry_feedback.m clang-analyzer-nullability.NullablePassedToNonnull 1
|
||||
dm/gfx/tests/t_geometry_primitive_id.m clang-analyzer-nullability.NullablePassedToNonnull 1
|
||||
dm/gfx/tests/t_image_alias.m clang-analyzer-nullability.NullablePassedToNonnull 1
|
||||
|
||||
|
@@ -586,6 +586,7 @@ One line per symptom in the [subsystem references](../reference/): what you see
|
||||
- the `[nvn-mtl] pool for handles traced to no slot` line counts thousands of builds while the game streams, and the submit thread spends milliseconds a frame walking the texture pool table again. Ocarina of Time, a seven-minute walk through Kokiri Forest: about 61,000 builds of its 33,025-entry table, 53 s in all. → [Pool tables patched instead of built whole](../reference/nvn-objects.md#pool-tables-patched-instead-of-built-whole)
|
||||
- a pool table keeps naming an sRGB / linear twin's view after the texture it is a view of left the sharing table and, 17 submissions later, the residency set: a texture ID with no residency behind it, which gives GPU address faults in the shaders that sample it (as in issue #1326), and a view that no longer shares its storage's bytes with anything. Seen in `t_pool_notes` only, in no game. → [A view's entry did not hold its parent](../reference/nvn-objects.md#a-views-entry-did-not-hold-its-parent)
|
||||
- the serial table grows by about 10 entries a frame (276,000 after 25,000 frames). → [Dead texture serials kept in a ghost table](../reference/nvn-objects.md#dead-texture-serials-kept-in-a-ghost-table)
|
||||
- none seen on a game. A finalized texture can resolve to an sRGB / linear view of storage that no longer holds its bytes. → [A finalized twin view left in the ghost ring](../reference/nvn-objects.md#a-finalized-twin-view-left-in-the-ghost-ring)
|
||||
- `nvn_replay` aborts on a game's capture with `-[NvProg width]: unrecognized selector` (from `rt_dims`) or `-[NvProg mipmapLevelCount]`: a render target resolves to a program. → [A replay's own objects take serials above a capture's](../reference/nvn-objects.md#a-replays-own-objects-take-serials-above-a-captures)
|
||||
- `DM_NVN_CAPTURE` reports `a record names … (0 bytes), outside every pool … texture registry entry …` for some texture IDs, with addresses that look like data; one capture stopped with `a sparse virtual pool (step 1 does not capture page tables): the capture is incomplete`, another with `straddles a region`. → [A captured texture ID whose memory pool was finalized](../reference/nvn-objects.md#a-captured-texture-id-whose-memory-pool-was-finalized)
|
||||
- `CRASH sig=11 … pc=nvn_sparse_protect+0x40`, an address that looks like data, `x8` 8 below it, after a long session; the backtrace `tex_by_id` ← `tex_mtl` ← `pkg_upload`. The pool need not be sparse. → [A texture read after its memory pool object was freed](../reference/nvn-objects.md#a-texture-read-after-its-memory-pool-object-was-freed)
|
||||
|
||||
@@ -229,6 +229,14 @@ A game may tell NVN objects apart by their bytes, without an NVN call. A host re
|
||||
- **Cause:** a game that records transient render targets and finalizes them before it submits: replay put the dead serials back into `g_by_serial`.
|
||||
- **Fix:** dead serials go to a 512-entry table (`tex_put`, `g_ghost`; the memory line's `finalized textures still referenced`).
|
||||
- **Found on:** [The Legend of Zelda: Breath of the Wild](../../dm/games/01007EF00011E000/README.md) (the HUD captures, the 640×360 buffers; found while fixing GitHub issue #9).
|
||||
- **Note:** the tables are `nvn_owner.m`'s (dm/gfx/README.md). `t_serial_owner` holds them to their behaviour, linked with the owner alone: live registration, replacement and removal, where `tex_put` sends a serial (live table for a live texture object or an external texture, ghost ring for a finalized one), finalizing, ring eviction at 512, texture and program pins, a view's entry counting its parent, and the held counts (`g_by_serial_held`, `g_ghost_held`, pins included) against the tables after every step.
|
||||
|
||||
### A finalized twin view left in the ghost ring
|
||||
|
||||
- **Symptom:** none seen on a game. A finalized texture can resolve to an sRGB / linear view of storage that no longer holds its bytes.
|
||||
- **Cause:** the twin path of `tex_mtl_` puts the view it made for a finalized serial with `tex_put`, which sends a finalized serial to the ghost ring; when the shared texture left the sharing table meanwhile, it drops the view with `serial_del` (`nvn_owner.m`), which removes only a live entry. The ghost entry stays, and holds the view. The next `tex_put` of the serial normally replaces it a few lines later; it stays when making the texture's own one fails (`tex_mtl_` returns nil), and another thread can resolve it in between.
|
||||
- **Fix:** open. `DM_TEST_SERIAL_TWIN=1 ./t_serial_owner` reproduces it at the table level (without the switch the run prints what it finds and counts no failure).
|
||||
- **Found on:** no game; found in a review of the serial tables.
|
||||
|
||||
### A replay's own objects take serials above a capture's
|
||||
|
||||
|
||||
Reference in new issue
Block a user