mirror of
https://github.com/seedds/nxnative.git
synced 2026-11-23 11:39:42 -10:00
The NVN sharing table (textures shared by storage: pool + offset + description) moves from the replay layer into the resource owner, dm/gfx/nvn/nvn_owner.m, with its index by 64 KB cell, the last lookup of each key, the set of shared textures and the eviction counters, and every change of it that publishes a serial's object is one operation under one hold of g_smu: own_alias_take (lookup, last-lookup mark, publication; tex_mtl_ let g_smu go between the lookup and its tex_put, so a pool's Finalize on the game's thread in between published an evicted, retired texture), own_alias_publish (takeover, entry, publication; the publication was a second hold), own_view_put (an sRGB / linear twin's view only while its parent is still the shared texture of the parent's key, which it marks looked up in the same hold; that mark was a second hold), own_upgrade (the rename of the serial tables, pins and sharing table, then the asking serial's object; the twin's view made again was published after the rename's hold, so a reader in between found the old view of a parent no longer shared) and own_alias_drop_pool_locked (mtl_alias_drop_pool's part). Two resolutions change: a texture another thread registered under a key since the lookup missed is the one the serial takes, and the caller retires its own (logged "made twice over one storage"; replacing it left the first texture in the set of shared textures for good and its serials on a texture no longer shared), and an entry put over a key's entry retires the old one with its set membership (it was released at once and stayed in the set). Matching is unchanged (the key, ALIAS_IDLE, which entries a takeover spares). nvn_mtl.m reads the table only through the owner: image_texture through own_alias_at_locked, the memory report through own_alias_stats_locked / own_alias_unref_locked, the census through own_alias_each_locked, the capture through own_alias_peek; retirement asks own_keeps_locked. The hook own_hook_shared goes (the owner knows the set); own_hook_submission gives it the submission being encoded (g_fence + 1), which last lookups count from. own_check_locked also checks the sharing table's index, set and last lookups against its entries. Still in nvn_mtl.m under g_smu: g_pkg_share, g_image_desc, retirement (defer_drain, g_retire_held), tex_alias_key and g_ser_gen. New reference entry (Sharing-table lookups and registrations in separate holds; none seen on a game), the twin entry updated, symptom index regenerated; dm/gfx/README.md has the owner's interface, an ownership map (who registers, invalidates, retains, releases) and the lifetime access left for the retirement move. Checked: t_serial_owner with the owner alone, now with the real sharing table (145 checks: lookups, registrations, a second registration under one key, takeovers sparing held and lately looked-up entries and taking a ghost-held one, replacement, pool drops, a twin refused after its parent's pool drop or upgrade between lookup and publication, own_upgrade; seven deliberate breaks of the owner each fail it), t_alias_takeover, t_pool_notes, t_srgb_alias, t_upgrade_alias, make -C dm/gfx/tests run, ASan/UBSan on t_serial_owner, TSan on t_srgb_alias and t_alias_takeover, thermal-tsan, lint_c.py tidy (nothing above the baseline), test_docs; regress.py check against 2ba06d76 (one run per side, headless, M6): Bastion PASS 59.7 / 59.4 fps, Obra Dinn 59.0 / 59.0 fps, CHANGED on screenshots by the rower's stroke only, the same sharing counts at every memory line (shared textures 49 → 73, 12 → 19 unreferenced, takeovers 4 → 18), GPU allocated 1030 / 1022 MB, no new red line. An iOS IPA of Bastion builds. No chain input changes. Not run: two threads at once, a game that reaches the twin path (Mario Kart World), the iPhone
This commit is contained in:
1 parent
09a91c5c3d
commit
96259d45e4
13 files changed
+488
-236
No files matched your search
+11
-3
@@ -24,7 +24,15 @@ 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`), renaming an object everywhere (`own_rename_locked`, the upgrade path), and the validity of an sRGB / linear view of a shared texture, decided in the same hold of `g_smu` as its entry (`own_view_put`: published only while its parent is shared; `own_view_drop_stale`: a stale view's live or ghost entry goes, an invalidation distinct from a finalize's `serial_del`). 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's search for a shared texture of the other format, the upgrade's renaming in the sharing table and its view made again on the upgraded parent (still a plain `tex_put` after the rename's lock is let go), and the change generation (`g_ser_gen`, `ser_note`). Invariants the alias move keeps: a view is published only while its parent is shared, a stale view goes wherever it is published and only while its entry names it, and each ghost serial has one ring slot ([docs/reference/nvn-objects.md](../../docs/reference/nvn-objects.md#a-finalized-twin-view-left-in-the-ghost-ring)).
|
||||
- **The resource owner `nvn/nvn_owner.m`** (`nvn_owner.h`): what a texture or program serial resolves to, which texture holds a storage's bytes, 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`), the sharing table (key: pool, offset, description; with its index by 64 KB cell, last lookups and set of shared textures), and the operations that change a serial's object together with the sharing table in one hold of `g_smu`: `own_alias_take` (a lookup), `own_alias_publish` (a new texture: takeover, entry, publication), `own_view_put` and `own_view_drop_stale` (an sRGB / linear twin's view: published only while its parent is still its key's texture; a stale view's live or ghost entry goes, an invalidation distinct from a finalize's `serial_del`), `own_upgrade` (every name of an upgraded texture, the twin's view made again), `own_alias_drop_pool_locked` (a pool's Finalize). 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_unheld`: retirement; `own_hook_changed`: `ser_note`; `own_hook_submission`: the submission being encoded, which last lookups are counted in). 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, and the sharing table's index, set and last lookups match its entries (`own_check_locked`); a view is published only while its parent is its key's texture, and a stale view goes wherever it is published and only while its entry names it; each ghost serial has one ring slot; the lock order is `g_smu`, then `g_cpu_tex_mu` ([docs/reference/nvn-objects.md](../../docs/reference/nvn-objects.md#sharing-table-lookups-and-registrations-in-separate-holds)).
|
||||
|
||||
| NVN texture state | Registers | Invalidates | Retains | Releases |
|
||||
|---|---|---|---|---|
|
||||
| A serial's object (live, ghost) | `nvn_mtl.m` through `serial_put`, `tex_put`, `own_alias_take` / `own_alias_publish`, `own_view_put`, `own_upgrade` | a finalize (`serial_del`), a stale view (`own_view_drop_stale`), the ghost ring's eviction | the owner's entry (and each parent of a view, `count_add`), texture pins | `own_hook_drop` → `defer_obj_release` / `defer_obj_unref` after the submission in flight |
|
||||
| A shared texture (sharing table) | `own_alias_publish` (the takeover first) | a pool's Finalize (`mtl_alias_drop_pool` → `own_alias_drop_pool_locked`), a takeover, an entry replaced under its key | the sharing table | `own_hook_drop` (retired); a serial entry still holding it keeps it resident (`defer_drain` → `g_retire_held` until `own_hook_unheld`) |
|
||||
| A shared packaged texture (`g_pkg_share`, weak) | `nvn_mtl.m`'s `pkg_share_put` | a pool's Finalize (`mtl_alias_drop_pool`) | the serials' entries (the `NvPkg`) | the `NvPkg`'s release |
|
||||
|
||||
Still in `nvn_mtl.m` under the same `g_smu`: the shared packaged textures (`g_pkg_share`, weak `NvPkg` references by the sharing key), retirement (`defer_drain`, `g_retire_held`, `retire_resume`, which asks `entry_holds` and `own_keeps_locked`), the image descriptions (`g_image_desc`), the key's derivation (`tex_alias_key`) and the change generation (`g_ser_gen`, which the upgrade and a pool drop bump with no note). Lifetime access left for the retirement move: `defer_drain`'s `entry_holds` (a try-lock on `g_smu` from completion handlers), `retire_wait` / `retire_resume` and `own_hook_unheld`, the upgrade's `defer_obj_release` of the old texture, `NvPkg`'s release, and `cpu_unc_drop`'s publication of a sparse texture's view (`pk->view`), whose parent is in no sharing table.
|
||||
|
||||
For which Metal object each NVN object and command lands on, see [nvn_metal_mapping.md](nvn_metal_mapping.md).
|
||||
|
||||
@@ -74,7 +82,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_owner.m`, `nvn/nvn_owner.h` | The resource owner: live and ghost serials, live texture objects, texture and program pins, the held counts, the sharing table and its index |
|
||||
| `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) |
|
||||
@@ -140,7 +148,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 and the ghost ring's slots after every step; sRGB / linear views: the old publish-then-`serial_del` sequence (the evidence), `own_view_put` and `own_view_drop_stale` in each order of finalize, publish, eviction and resolution |
|
||||
| `t_serial_owner` | The resource 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, the ghost ring's slots and the sharing table's index and set after every step; sRGB / linear views: the old publish-then-`serial_del` sequence (the evidence), `own_view_put` and `own_view_drop_stale` in each order of finalize, publish, eviction and resolution; the sharing table: lookups, registrations (a second under one key takes the first), the takeover's spared and taken entries, pool drops, a twin refused after its parent's pool drop or upgrade, `own_upgrade` |
|
||||
| `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` |
|
||||
|
||||
|
||||
@@ -271,8 +271,7 @@ static void cap_tex_read(const tex_t *t, int rec) { // rec: named by a record
|
||||
// their content from the frame before was not captured, the replay made them empty, and the G-buffer differed from the live run in about 50 pixels (and
|
||||
// a second replay from the first). Only for textures a record names: through the registry (even only those the GPU writes, or
|
||||
// only those shared within the last 16 submissions) it reached about 19,500 textures (18.5 GB) in Breath of the Wild
|
||||
alias_key ak; memset(&ak, 0, sizeof ak); ak.pool = (uint64_t)(uintptr_t)t->pool; ak.off = t->off; ak.d = t->d; ak.d.flags &= ~NVN_TEXTURE_FLAGS_IMAGE;
|
||||
if (rec) { pthread_mutex_lock(&g_smu); o = hm_get_id(&g_alias, &ak); pthread_mutex_unlock(&g_smu); }
|
||||
if (rec) { alias_key ak = tex_alias_key(t); o = own_alias_peek(&ak); }
|
||||
}
|
||||
nvn_hm_put(&g_cap_read, &t->serial, (void *)(uintptr_t)(!o && !rec ? 2 : 1));
|
||||
if (!seen) { // how the replay layer holds it
|
||||
|
||||
+39
-147
@@ -306,7 +306,7 @@ static void res_remove(id<MTLAllocation> r) { @autoreleasepool {
|
||||
} }
|
||||
// Recycled texture memory (GitHub issues #1831, #1823). An engine that keeps its render targets in a GPU-only pool and moves or resizes them with its dynamic resolution names new
|
||||
// storage or a new size every few frames, and each (pool, offset, description) gets a Metal texture of its own: Ocarina of Time's 305 MB target pool made 3,468 textures in about
|
||||
// 1,000 frames of its village, 450-575 MB added to the residency set every 300 frames, while the storage takeover (alias_take_over) let as many go. Metal does not lay a texture out
|
||||
// 1,000 frames of its village, 450-575 MB added to the residency set every 300 frames, while the storage takeover (nvn_owner.m's alias_take_over) let as many go. Metal does not lay a texture out
|
||||
// as NVN does, so these targets cannot share the pool's memory as on the console: a heap that mirrors the pool corrupts the picture, as the game keeps live targets in the part of
|
||||
// the full-size scene target its current resolution leaves unused (PR #1453). What can be kept is the memory of a texture the host has already let go. Each such texture is made in
|
||||
// a placement heap of its own; when its texture is freed (after the submissions that used it completed, defer_drain), the heap waits here, still resident, for the next texture it fits.
|
||||
@@ -2610,12 +2610,9 @@ static void ser_note(uint64_t serial) {
|
||||
uint64_t g = atomic_fetch_add(&g_ser_gen, 1) + 1; g_ser_log[g % SER_LOG] = (ser_lnote){g, serial};
|
||||
os_unfair_lock_unlock(&g_snote_lk);
|
||||
}
|
||||
// Textures shared by storage (non-packaged): key = memory pool + offset + texture description. An NVN texture object is only a descriptor; an object rebuilt over the same storage with the same description is the same texture;
|
||||
// SMBW rebuilds its transient render targets on new objects every frame and never Finalizes them, so building one per serial leaked about 13 MB of GPU memory per frame. Textures here are kept resident by the sharing table,
|
||||
// and when the serial table replaces / deletes them it only drops its reference (defer_obj_unref), never removing them from the residency set. Entries go when their pool is finalized (mtl_alias_drop_pool)
|
||||
// or another texture takes over their storage (alias_take_over).
|
||||
typedef struct { uint64_t pool; int64_t off; nvn_texdesc d; } alias_key;
|
||||
static nvn_hmap g_alias = {.klen = sizeof(alias_key)};
|
||||
// The sharing table (textures shared by storage: pool + offset + description) is nvn_owner.m's: own_alias_take, own_alias_publish, own_view_put, own_upgrade,
|
||||
// own_alias_drop_pool_locked (nvn_owner.h). What stays here under g_smu: the shared packaged textures (g_pkg_share, weak, by the same key), retirement's waiting objects
|
||||
// (g_retire_held) and the image descriptions (g_image_desc)
|
||||
static nvn_hmap g_pkg_share = {.klen = sizeof(alias_key)}; // see NvPkgRef
|
||||
// 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
|
||||
@@ -2624,33 +2621,6 @@ static nvn_hmap g_pkg_share = {.klen = sizeof(alias_key)}; // see NvPkgRef
|
||||
// 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);
|
||||
// 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
|
||||
enum { ALIAS_CELL = 16 };
|
||||
typedef struct { alias_key k; int64_t end; } alias_ent;
|
||||
typedef struct { uint32_t n, cap; alias_ent e[]; } alias_cell;
|
||||
static nvn_hmap g_alias_cells = {.klen = 16};
|
||||
static void alias_index(const alias_key *k, int add) {
|
||||
nvn_texlayout L; nvn_tex_layout(&k->d, &L);
|
||||
int64_t end = k->off + (int64_t)L.size, c1 = (end > k->off ? end - 1 : k->off) >> ALIAS_CELL;
|
||||
for (int64_t c = k->off >> ALIAS_CELL; c <= c1; c++) {
|
||||
uint64_t ck[2] = {k->pool, (uint64_t)c}; alias_cell *v = nvn_hm_get(&g_alias_cells, ck);
|
||||
uint32_t i = 0; if (v) for (; i < v->n; i++) if (!memcmp(&v->e[i].k, k, sizeof *k)) break;
|
||||
if (add) {
|
||||
if (v && i < v->n) continue;
|
||||
if (!v || v->n == v->cap) {
|
||||
uint32_t cap = v ? v->cap * 2 : 4; alias_cell *nv = realloc(v, sizeof *nv + cap * sizeof nv->e[0]); if (!nv) abort();
|
||||
if (!v) nv->n = 0; nv->cap = cap; v = nv; nvn_hm_put(&g_alias_cells, ck, v);
|
||||
}
|
||||
v->e[v->n].k = *k; v->e[v->n++].end = end;
|
||||
} else if (v && i < v->n) {
|
||||
v->e[i] = v->e[--v->n];
|
||||
if (!v->n) { nvn_hm_del(&g_alias_cells, ck); free(v); }
|
||||
}
|
||||
}
|
||||
}
|
||||
static nvn_hmap g_alias_seen = {.klen = sizeof(alias_key)}; // key → g_fence + 1 at its last lookup or creation (alias_take_over spares entries looked up lately)
|
||||
// Live GPU-only image descriptions, including those not used yet. A dynamic-resolution family declares its largest image before any particular resolution is used.
|
||||
// Keeping only descriptions here lets its first allocation cover that family exactly, without allocating every resolution or rounding to a power of two.
|
||||
static nvn_hmap g_image_desc = {.klen = 8}; // serial → malloc'ed tex_t, under g_smu
|
||||
@@ -2669,37 +2639,17 @@ static void image_desc_drop_pool(const void *pool) { // caller holds g_smu
|
||||
} else i++;
|
||||
}
|
||||
}
|
||||
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)); }
|
||||
// The sharing table's two writers: an entry goes in and out with its cells (alias_index), its place in g_alias_set and its last lookup (g_alias_seen), so none of them can be
|
||||
// left out. A removed texture is retired (deferred release). Caller holds g_smu
|
||||
static void alias_put(const alias_key *k, id o) {
|
||||
if (!g_alias_set) g_alias_set = CFSetCreateMutable(NULL, 0, NULL);
|
||||
hm_put_id(&g_alias, k, o); alias_index(k, 1); CFSetAddValue(g_alias_set, (__bridge const void *)o); nvn_hm_put(&g_alias_seen, k, (void *)(uintptr_t)(g_fence + 1));
|
||||
}
|
||||
static int alias_del(const alias_key *k) {
|
||||
void *old = nvn_hm_del(&g_alias, k);
|
||||
if (!old) return 0;
|
||||
alias_index(k, 0); nvn_hm_del(&g_alias_seen, k);
|
||||
id o = CFBridgingRelease(old);
|
||||
if (g_alias_set) CFSetRemoveValue(g_alias_set, (__bridge const void *)o);
|
||||
defer_obj_release(o);
|
||||
return 1;
|
||||
}
|
||||
static void retire_wait(id o) { // defer_drain: a serial entry still holds what it retires. Caller holds g_smu
|
||||
uintptr_t k = (uintptr_t)(__bridge void *)o;
|
||||
if (!nvn_hm_get(&g_retire_held, &k)) nvn_hm_put(&g_retire_held, &k, (void *)CFBridgingRetain(o));
|
||||
}
|
||||
static void retire_resume(const void *o) { // the last serial entry holding a waiting object let it go: retired again, unless the sharing table took it meanwhile (it retires its own). Caller holds g_smu
|
||||
static void retire_resume(const void *o) { // the last serial entry holding a waiting object let it go: retired again, unless it is pinned or the sharing table took it meanwhile (that retires its own). Caller holds g_smu
|
||||
uintptr_t k = (uintptr_t)o; void *v = nvn_hm_del(&g_retire_held, &k);
|
||||
if (!v) return;
|
||||
id obj = CFBridgingRelease(v);
|
||||
if (!alias_has(obj)) defer_obj_release(obj);
|
||||
if (!own_keeps_locked(obj)) defer_obj_release(obj);
|
||||
}
|
||||
// On memory pool Finalize, evict the textures built on that pool from the sharing table (collect the keys first, then delete: deletion shifts entries back). The sharing table used not to evict: Mario's pools stay for good, so entries are bounded;
|
||||
// Engage frees pools on scene changes and rebuilds on new ones, so the old pools' render targets and other private textures stayed in the table forever (about 0.8 GB more GPU memory over 850 s of the title demo), and linear textures built directly on a pool also kept the whole pool buffer alive.
|
||||
// Also, when a pool object's address is reused, old entries would hand a texture on the old storage to the new pool's texture at the same offset and description. Evicted textures go through deferred release; if the serial table still holds one, that is just one more reference
|
||||
// On memory pool Finalize, the pool's textures leave the sharing table (own_alias_drop_pool_locked says why), its shared packaged textures and image descriptions go, then its texture IDs
|
||||
static void treg_drop_pool_views(const void *pool);
|
||||
static void cpu_tex_drop_pool(const void *pool);
|
||||
static void gws_drop_pool(const pool_t *p);
|
||||
@@ -2712,17 +2662,8 @@ void mtl_alias_drop_pool(const void *pool) {
|
||||
if (k.pool == (uint64_t)(uintptr_t)pool) { alias_key *nk = realloc(ks, (ns + 1) * sizeof *ks); if (!nk) break; ks = nk; ks[ns++] = k; } }
|
||||
for (uint32_t i = 0; i < ns; i++) hm_del_id(&g_pkg_share, &ks[i]);
|
||||
free(ks); }
|
||||
uint32_t n = 0; alias_key *ks = NULL;
|
||||
for (uint32_t i = 0; i < g_alias.cap; i++) {
|
||||
if (!g_alias.vals[i]) continue;
|
||||
alias_key k; memcpy(&k, g_alias.keys + (size_t)i * g_alias.klen, sizeof k);
|
||||
if (k.pool != (uint64_t)(uintptr_t)pool) continue;
|
||||
alias_key *nk = realloc(ks, (n + 1) * sizeof *ks); if (!nk) break; ks = nk; ks[n++] = k;
|
||||
}
|
||||
for (uint32_t j = 0; j < n; j++) alias_del(&ks[j]);
|
||||
if (n) { g_ser_gen++; g_alias_evicted += n; }
|
||||
if (own_alias_drop_pool_locked(pool)) g_ser_gen++; // the sharing table's entries of the pool (nvn_owner.m); no note: every texture ID takes the slow path once
|
||||
pthread_mutex_unlock(&g_smu);
|
||||
free(ks);
|
||||
// The pool's texture IDs are marked gone before their CPU-written classification goes: a pool walk (pool_gpu, holding g_rmu) reaching an ID in between found it not gone, the
|
||||
// pool's magic still set and the texture no longer CPU-written, and tex_mtl_ handed out its packaged entry (an NvPkg) as the texture: "-[NvPkg buffer]: unrecognized selector"
|
||||
// in tex_by_id (Ocarina of Time, see reference/nvn-objects.md). Marked first, a reader holding g_rmu is done before, or finds the ID gone and takes tex_made
|
||||
@@ -2730,44 +2671,9 @@ void mtl_alias_drop_pool(const void *pool) {
|
||||
cpu_tex_drop_pool(pool);
|
||||
gws_drop_pool(pool);
|
||||
}
|
||||
// A new shared texture takes over storage: entries of the same pool whose storage overlaps the new texture's under another description go, unless a live texture object still resolves
|
||||
// to them (an object rebuilt over the same storage with the same description must keep finding it: SMBW's transient render targets) or a lookup found them in the last ALIAS_IDLE submissions
|
||||
// (Breath of the Wild finalizes its transient render targets before it submits and makes them again the next frame: targets sharing storage would evict each other every frame). On NVN the memory now belongs to the new texture;
|
||||
// the old private Metal texture only held a copy nothing can name again. STEINS;GATE RE:BOOT decodes every image it shows into one scratch region of a pool that lives for good and
|
||||
// finalizes the texture before the next one: each image added an entry, 468 of them and 1.58 GB of GPU memory 24,000 frames into a new game. The texture is retired (deferred until
|
||||
// the submissions that used it complete); one a finalized object still resolves through g_ghost stays resident until the ring lets it go (defer_drain, g_retire_held).
|
||||
// The change records a note that names no texture (ser_note(0)), as it changes no serial's texture: an entry a live serial holds is not evicted (serial_table_holds), and one a finalized
|
||||
// serial holds through g_ghost keeps its texture. A live sRGB / linear twin's serial holds a view of the shared texture, which counts as holding the texture (count_add), so a twin
|
||||
// keeps its parent as a texture of the parent's own format would; with the view alone counted the parent went, and no note sent the twin's texture IDs to tex_mtl_'s stale-view
|
||||
// check (GitHub issue #2125). A retired ID's view goes at the submission's end (treg_finish_submission), and the
|
||||
// pool tables take those IDs again at the next one (pool_snap.trans). With no note, every takeover (about 8,400 in six minutes walking through Ocarina of Time's Kokiri Forest) sent
|
||||
// every texture ID down the slow path and made the next walk build the 33,025-entry table whole. Caller holds g_smu
|
||||
enum { ALIAS_IDLE = 16 };
|
||||
static uint64_t g_alias_taken; // shared textures evicted when another texture took over their storage (for the memory report)
|
||||
static void alias_take_over(const alias_key *nk) {
|
||||
nvn_texlayout L; nvn_tex_layout(&nk->d, &L);
|
||||
if (!L.size) return;
|
||||
int64_t lo = nk->off, hi = nk->off + (int64_t)L.size;
|
||||
uint32_t n = 0; alias_key ks[16];
|
||||
for (int64_t c = lo >> ALIAS_CELL; c <= (hi - 1) >> ALIAS_CELL && n < 16; c++) { // the entries whose storage reaches into this one's cells (alias_index)
|
||||
uint64_t ck[2] = {nk->pool, (uint64_t)c}; const alias_cell *v = nvn_hm_get(&g_alias_cells, ck);
|
||||
for (uint32_t i = 0; v && i < v->n && n < 16; i++) {
|
||||
const alias_key *k = &v->e[i].k;
|
||||
if (!memcmp(k, nk, sizeof *k) || k->off >= hi || v->e[i].end <= lo) continue;
|
||||
uint32_t j = 0; while (j < n && memcmp(&ks[j], k, sizeof *k)) j++;
|
||||
if (j < n) continue; // met in an earlier cell
|
||||
void *val = nvn_hm_get(&g_alias, k); if (!val) continue;
|
||||
uint64_t seen = (uint64_t)(uintptr_t)nvn_hm_get(&g_alias_seen, k);
|
||||
if ((seen && g_fence + 1 - seen < ALIAS_IDLE) || serial_table_holds(val)) continue;
|
||||
ks[n++] = *k;
|
||||
}
|
||||
}
|
||||
for (uint32_t j = 0; j < n; j++) alias_del(&ks[j]);
|
||||
if (n) { ser_note(0); g_alias_taken += n; }
|
||||
}
|
||||
// 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:
|
||||
// The serial tables and the sharing table are nvn_owner.m's (nvn_owner.h): serial_put / serial_del / tex_put, serial_get / tex_get, the pins, the held counts, own_alias_*. 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); }
|
||||
uint64_t own_hook_submission(void) { return g_fence + 1; }
|
||||
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) {
|
||||
@@ -3003,22 +2909,16 @@ static id<MTLTexture> tex_upgrade(const tex_t *t, id<MTLTexture> asked, MTLTextu
|
||||
if (((NvPkg *)holder)->view) { defer_obj_unref(((NvPkg *)holder)->view); ((NvPkg *)holder)->view = nil; ((NvPkg *)holder)->vbase = 0; } } // a sparse texture's view of the old one: bound whole until its next upload
|
||||
else {
|
||||
// Every name of the old texture moves to the upgraded one: other serials sharing its storage (live in g_by_serial, finalized in g_ghost) and the sharing table. Only the asking serial moved before,
|
||||
// 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);
|
||||
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;
|
||||
}
|
||||
if (shared) { CFSetRemoveValue(g_alias_set, (__bridge const void *)base); CFSetAddValue(g_alias_set, (__bridge const void *)upgraded); }
|
||||
g_ser_gen++;
|
||||
pthread_mutex_unlock(&g_smu);
|
||||
// so the others kept reading the old copy while writes went to the new one, and the old texture stayed shared. 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). An sRGB / linear twin's view is made again on the upgraded
|
||||
// texture first and published in the rename's hold (own_upgrade), so no reader finds the serial's old view once its parent has left the sharing table
|
||||
if (view) {
|
||||
id<MTLTexture> nv = [upgraded newTextureViewWithPixelFormat:view.pixelFormat textureType:view.textureType levels:NSMakeRange(0, upgraded.mipmapLevelCount) slices:NSMakeRange(0, upgraded.arrayLength) swizzle:view.swizzle];
|
||||
upgraded_view = [upgraded newTextureViewWithPixelFormat:view.pixelFormat textureType:view.textureType levels:NSMakeRange(0, upgraded.mipmapLevelCount) slices:NSMakeRange(0, upgraded.arrayLength) swizzle:view.swizzle];
|
||||
static int nview; if (nview++ < 4) MLOG("sRGB / linear view ser=%llu made again on its upgraded parent (%d so far)", (unsigned long long)t->serial, nview);
|
||||
if (nv) { tex_put(t->serial, nv); upgraded_view = nv; } else RED_ONCE(151, "cannot make the sRGB / linear view ser=%llu again on its upgraded parent: it keeps the old storage", (unsigned long long)t->serial);
|
||||
} else if (!named) tex_put(t->serial, upgraded);
|
||||
if (!upgraded_view) RED_ONCE(151, "cannot make the sRGB / linear view ser=%llu again on its upgraded parent: it keeps the old storage", (unsigned long long)t->serial);
|
||||
}
|
||||
own_upgrade(base, upgraded, t->serial, view ? upgraded_view : upgraded); // the serial tables, the pins and the sharing table (nvn_owner.m)
|
||||
g_ser_gen++;
|
||||
defer_obj_release(base);
|
||||
}
|
||||
MLOG("texture upgraded to allow %s ser=%llu base format %lu → view format %lu (upgrades=%llu)", add & MTLTextureUsageShaderWrite ? "image writes" : add & MTLTextureUsageRenderTarget ? "render-target use" : "format views", t->serial, (unsigned long)base.pixelFormat, (unsigned long)view_pf, ++g_pfv_upgrades);
|
||||
@@ -4221,7 +4121,7 @@ static int tex_from_storage(const tex_t *t) {
|
||||
// Storage the CPU wrote before the texture's first use (every game): until the GPU first uses a texture it has not written it, and a block-compressed one it can never render into
|
||||
// (BC and ASTC formats are not colour-renderable; GPU copies into it are written into its storage too, tex_storage_dst). So a sampled block-compressed texture in a pool the CPU can
|
||||
// write, with no packaged layout, whose storage holds data at its first use (tex_mtl_, submit thread) holds what the CPU wrote, flushed or not, before or after nvnTextureInitialize:
|
||||
// it is made CPU-written, uploaded now and re-uploaded when its pages change. A one-time fill (tex_init_fill) is not enough: that GPU texture goes into the sharing table (g_alias),
|
||||
// it is made CPU-written, uploaded now and re-uploaded when its pages change. A one-time fill (tex_init_fill) is not enough: that GPU texture goes into the sharing table (own_alias_publish),
|
||||
// and a new texture object over the same storage with the same description gets it back after the CPU has rewritten the storage. The Great Ace Attorney Chronicles (Japanese release
|
||||
// 0100D7F00FB1A000, GitHub issue #568) decodes its court-record pictures (256x256 to 2048x2048 BC1, a CPU_CACHED pool, no flush) into storage it reuses, building a new texture over
|
||||
// it for each item: they were black, and once filled once, showed the previous item's picture. An uncompressed 2D texture cannot be told from a render target by its flags (NVN has
|
||||
@@ -4380,18 +4280,16 @@ static id<MTLTexture> image_texture(const tex_t *t, MTLTextureDescriptor *d0) {
|
||||
MTLTextureDescriptor *d = d0; if (nocomp) { d = [d0 copy]; d.allowGPUOptimizedContents = NO; }
|
||||
MTLSizeAndAlign need = [g_dev heapTextureSizeAndAlignWithDescriptor:d];
|
||||
if (!need.size) return nil;
|
||||
NvImageHeap *owner = nil; MTLTextureDescriptor *largest = d; NSUInteger capacity = need.size; int family = 0;
|
||||
__block NvImageHeap *owner = nil; MTLTextureDescriptor *largest = d; NSUInteger capacity = need.size; int family = 0;
|
||||
pthread_mutex_lock(&g_smu);
|
||||
uint64_t ck[2] = {(uint64_t)(uintptr_t)t->pool, (uint64_t)(t->off >> ALIAS_CELL)}; const alias_cell *cell = nvn_hm_get(&g_alias_cells, ck); // every entry at this offset lies in its cell (alias_index)
|
||||
for (uint32_t i = 0; cell && i < cell->n; i++) {
|
||||
const alias_key *k = &cell->e[i].k;
|
||||
if (k->off != t->off || k->d.format != t->d.format) continue;
|
||||
id<MTLTexture> other = hm_get_id(&g_alias, k); if (!other) continue;
|
||||
own_alias_at_locked(t->pool, t->off, ^(const alias_key *k, id shared) { // the shared textures that start at this offset (the owner's index)
|
||||
if (k->d.format != t->d.format) return;
|
||||
id<MTLTexture> other = shared;
|
||||
NvImageHeap *o = objc_getAssociatedObject(other, &g_image_heap_owner_key);
|
||||
// Metal omits ShaderAtomic from usage; tex_desc gives every writable R32 integer image that capability.
|
||||
MTLTextureUsage usage = other.usage | (tex_atomic_capable(other) ? MTLTextureUsageShaderAtomic : 0);
|
||||
if (o && o->heap.size >= need.size && usage == d.usage && (!owner || o->heap.size < owner->heap.size)) owner = o;
|
||||
}
|
||||
});
|
||||
if (!owner) for (uint32_t i = 0; i < g_image_desc.cap; i++) if (g_image_desc.vals[i]) {
|
||||
const tex_t *other = g_image_desc.vals[i];
|
||||
if (other->pool != t->pool || other->off != t->off || other->d.format != t->d.format) continue;
|
||||
@@ -4586,15 +4484,15 @@ static id<MTLTexture> tex_mtl_(const tex_t *ct) {
|
||||
return mt;
|
||||
}
|
||||
alias_key ak = tex_alias_key(t);
|
||||
pthread_mutex_lock(&g_smu); __unsafe_unretained id shared = hm_get_id(&g_alias, &ak); if (shared) nvn_hm_put(&g_alias_seen, &ak, (void *)(uintptr_t)(g_fence + 1)); pthread_mutex_unlock(&g_smu);
|
||||
if (shared) { tex_put(t->serial, shared); return shared; }
|
||||
id shared = own_alias_take(t->serial, &ak); // found and published in one hold (nvn_owner.m)
|
||||
if (shared) return shared;
|
||||
{ // NVN textures over the same storage share its bytes. One that differs from a shared texture only by the other format of an sRGB / linear pair is a view of it: as its own Metal
|
||||
// texture, Mario Kart World's Grand Prix movie (an RGBA8 render target the YUV pass writes, sampled by the composite through an RGBA8_SRGB texture) showed a frozen frame
|
||||
// The other formats of the pair are looked up directly (srgb_twins), with the rest of the key unchanged; the descriptor is built only when one is shared
|
||||
const uint8_t *tw = srgb_twins(ak.d.format); id<MTLTexture> v = nil;
|
||||
for (int j = 0; j < 4 && tw[j] && !v; j++) {
|
||||
alias_key k; memcpy(&k, &ak, sizeof k); k.d.format = tw[j]; // memcpy keeps the zeroed padding the table hashes
|
||||
pthread_mutex_lock(&g_smu); id<MTLTexture> b = hm_get_id(&g_alias, &k); pthread_mutex_unlock(&g_smu);
|
||||
id<MTLTexture> b = own_alias_peek(&k); // borrowed: a texture the table lets go meanwhile is released only after the submission in flight
|
||||
if (!b) continue;
|
||||
MTLTextureDescriptor *d = tex_desc(t, 0);
|
||||
if (!d || d.sampleCount > 1 || (d.textureType != MTLTextureType2D && d.textureType != MTLTextureType2DArray)) break;
|
||||
@@ -4602,10 +4500,9 @@ static id<MTLTexture> tex_mtl_(const tex_t *ct) {
|
||||
v = [b newTextureViewWithPixelFormat:d.pixelFormat textureType:b.textureType levels:NSMakeRange(0, b.mipmapLevelCount) slices:NSMakeRange(0, b.arrayLength) swizzle:b.swizzle];
|
||||
if (!v) continue;
|
||||
// The entry counts b as held from here on (count_add), so no takeover evicts it under the twin. b may have left the sharing table on another thread since the lookup
|
||||
// above (a pool's finalize on the game's thread): then the view is of a texture that no longer shares the storage's bytes, so it is never published (own_view_put
|
||||
// checks b in the same hold of g_smu as the entry), and the next format of the pair or a texture of the twin's own is tried
|
||||
if (!own_view_put(t->serial, v)) { v = nil; continue; }
|
||||
pthread_mutex_lock(&g_smu); if (hm_get_id(&g_alias, &k) == b) nvn_hm_put(&g_alias_seen, &k, (void *)(uintptr_t)(g_fence + 1)); pthread_mutex_unlock(&g_smu);
|
||||
// above (a pool's finalize on the game's thread, an upgrade): then the view is of a texture that no longer shares the storage's bytes, so it is never published (own_view_put
|
||||
// checks that k still names b, and marks k looked up, in the same hold of g_smu as the entry), and the next format of the pair or a texture of the twin's own is tried
|
||||
if (!own_view_put(t->serial, &k, v)) { v = nil; continue; }
|
||||
}
|
||||
if (v) return v;
|
||||
}
|
||||
@@ -4625,12 +4522,11 @@ static id<MTLTexture> tex_mtl_(const tex_t *ct) {
|
||||
if (g_allocprof > 0) { static int n; if (n++ < 30 || n % 500 == 0) MLOG("new texture (%s) #%d ser=%llu %ux%ux%u format %#x target %d levels %u storage %p", __func__, n, (unsigned long long)t->serial, t->d.width, t->d.height, t->d.depth, t->d.format, t->d.target, t->d.levels, (void *)t->mem); }
|
||||
if (getenv("DM_NVN_TEXCPU") && t->pool && !(t->pool->flags & NVN_MEMORY_POOL_FLAGS_CPU_NO_ACCESS)) MLOG("non-swizzled texture on a CPU-visible pool ser=%llu %ux%ux%u format %#x target %d levels %u texture flags %#x pool flags %#x storage %p", (unsigned long long)t->serial, t->d.width, t->d.height, t->d.depth, t->d.format, t->d.target, t->d.levels, t->d.flags, t->pool->flags, (void *)t->mem);
|
||||
res_add(mt);
|
||||
pthread_mutex_lock(&g_smu);
|
||||
alias_take_over(&ak);
|
||||
alias_put(&ak, mt);
|
||||
pthread_mutex_unlock(&g_smu);
|
||||
tex_put(t->serial, mt);
|
||||
return mt;
|
||||
// The takeover of the storage, the entry and the serial's publication in one hold (nvn_owner.m). A texture another thread registered under the key since the lookup above is the
|
||||
// one the storage's serials share: this one is retired
|
||||
id<MTLTexture> got = own_alias_publish(t->serial, &ak, mt);
|
||||
if (got != mt) { static int nrace; if (nrace++ < 4) MLOG("texture ser=%llu made twice over one storage: the first one registered is shared (%d so far)", (unsigned long long)t->serial, nrace); defer_obj_release(mt); }
|
||||
return got;
|
||||
}
|
||||
// Memory readout line (always on, low frequency, capped): resident memory, GPU allocated, deferred-release queue, residency set object count, registered memory pools / peak / tracking table capacity, cumulative texture re-uploads.
|
||||
// Purpose: pin down "crashes with no log". The system gives no notice before killing a process for memory, so only readouts left beforehand can show which number kept climbing.
|
||||
@@ -4642,12 +4538,8 @@ 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); 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);
|
||||
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_lock(&g_smu); own_stats os = own_stats_locked(); own_alias_stats as = own_alias_stats_locked(); unsigned ns = os.live, ashared = as.n, nghost = os.ghost, nwait = g_retire_held.n; uint64_t ghost_puts = os.ghost_puts;
|
||||
unsigned aorph = own_alias_unref_locked(); // shared textures no live texture object or finalized serial resolves to: kept only until their storage is taken over or their pool goes
|
||||
pthread_mutex_unlock(&g_smu);
|
||||
unsigned long dead_n = 0; unsigned long long dead_mb = 0;
|
||||
// Objects read from a weak table come back retained + autoreleased: the frame hook runs on a guest thread with no autorelease pool that drains, so without this wrapper every buffer counted would be pinned for good (the readout creating the leak itself)
|
||||
@@ -4658,7 +4550,7 @@ void mtl_mem_stat(unsigned long long frame, unsigned long long rss_mb) {
|
||||
#if TARGET_OS_IPHONE
|
||||
snprintf(avail, sizeof avail, "%llu MB", (unsigned long long)(os_proc_available_memory() >> 20));
|
||||
#endif
|
||||
MLOG("memory up to frame %llu: resident %llu MB, GPU allocated %llu MB, deferred-release queue %lu, residency set %lu, serial objects %u, memory pools %d (resident %d; peak %d / tracking table capacity %d; freed but alive %lu, %llu MB), shared textures %u (%u of them unreferenced; evicted with pools %llu, on storage takeover %llu in total), texture re-uploads %llu in total, per-frame arena blocks created %llu in total, finalized textures still referenced %u (small table, %llu put in total), retired but held by a serial %u, recycled texture heaps made %llu, reused %llu, freed %llu, waiting %u (%llu MB); headroom to the system memory limit %s", frame, rss_mb, gpu, nd, nr, ns, live, pres, peak, cap, dead_n, dead_mb, ashared, aorph, (unsigned long long)g_alias_evicted, (unsigned long long)g_alias_taken, (unsigned long long)g_pkg_reuploads, (unsigned long long)g_arena_made, nghost, (unsigned long long)ghost_puts, nwait,
|
||||
MLOG("memory up to frame %llu: resident %llu MB, GPU allocated %llu MB, deferred-release queue %lu, residency set %lu, serial objects %u, memory pools %d (resident %d; peak %d / tracking table capacity %d; freed but alive %lu, %llu MB), shared textures %u (%u of them unreferenced; evicted with pools %llu, on storage takeover %llu in total), texture re-uploads %llu in total, per-frame arena blocks created %llu in total, finalized textures still referenced %u (small table, %llu put in total), retired but held by a serial %u, recycled texture heaps made %llu, reused %llu, freed %llu, waiting %u (%llu MB); headroom to the system memory limit %s", frame, rss_mb, gpu, nd, nr, ns, live, pres, peak, cap, dead_n, dead_mb, ashared, aorph, (unsigned long long)as.evicted, (unsigned long long)as.taken, (unsigned long long)g_pkg_reuploads, (unsigned long long)g_arena_made, nghost, (unsigned long long)ghost_puts, nwait,
|
||||
(unsigned long long)g_rh_made, (unsigned long long)g_rh_reused, (unsigned long long)g_rh_trimmed, rh_n, (unsigned long long)(rh_bytes >> 20), avail);
|
||||
{ void mtl_vc_stats(unsigned *, uint64_t *, uint64_t *), mtl_tb2_stats(uint64_t *, uint64_t *, uint64_t *, uint64_t *, uint64_t *); uint64_t mtl_tex_mis_count(void); unsigned vn; uint64_t vh, vm, tb, tm, tf, tc, tcb;
|
||||
mtl_vc_stats(&vn, &vh, &vm); mtl_tb2_stats(&tb, &tm, &tf, &tc, &tcb);
|
||||
@@ -16915,7 +16807,7 @@ void mem_census(const char *why) { @autoreleasepool {
|
||||
pthread_mutex_lock(&g_smu);
|
||||
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]);
|
||||
own_alias_each_locked(^(const alias_key *k, id o) { (void)k; census_one(o); });
|
||||
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];
|
||||
|
||||
+40
-9
@@ -1,5 +1,5 @@
|
||||
// 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.
|
||||
// The NVN resource owner (nvn_owner.m): what an NVN texture or program serial resolves to, which texture holds a storage's bytes (the sharing table), 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)
|
||||
@@ -8,14 +8,18 @@
|
||||
// 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)
|
||||
// sharing alias_key (pool, offset, description) → the texture that holds those bytes (own_alias_*): NVN textures over the same storage with the same description are one
|
||||
// texture. Kept resident by the table; an entry goes when its pool is finalized (own_alias_drop_pool_locked) or a new texture takes its bytes over (own_alias_publish).
|
||||
// With it: an index by pool and 64 KB cell (the takeover's candidates, the image families' lookup), the submission of each key's last lookup, and the set of its textures
|
||||
// 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.
|
||||
// The sharing table holds its textures apart from those counts (own_shared_locked). own_check_locked checks the counts against the tables, and the sharing table's index and set.
|
||||
//
|
||||
// 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 and the upgrade path use g_smu as well.
|
||||
// Lock order: g_smu, then the replay layer's g_cpu_tex_mu. nvn_mtl.m holds g_smu for its own tables beside these (the shared packaged textures g_pkg_share, retirement's
|
||||
// g_retire_held, the image descriptions), and calls the _locked functions under it.
|
||||
#pragma once
|
||||
#import <Foundation/Foundation.h>
|
||||
#include <pthread.h>
|
||||
@@ -53,10 +57,37 @@ void serial_put(uint64_t k, id o); // a live serial's object; the one it repla
|
||||
void serial_del(uint64_t k); // a live serial's entry goes (a finalize, a program's release); a ghost entry of the serial stays, as a replayed record may still resolve it
|
||||
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
|
||||
|
||||
// sRGB / linear views of a shared texture (tex_mtl_'s twins). A view is valid while its parent is in the sharing table (own_hook_shared) or it or its parent is pinned; each decision
|
||||
// is taken in the same hold of g_smu as the entry it changes (docs/reference/nvn-objects.md, A finalized twin view left in the ghost ring)
|
||||
int own_view_put(uint64_t k, id<MTLTexture> view); // tex_put of the view only while its parent is shared: 1 published, 0 nothing changed (the parent left the sharing table)
|
||||
// The sharing table. A key's padding is zero (it is hashed): build one with memset or a zeroed initializer, and change a copy's fields
|
||||
typedef struct { uint64_t pool; int64_t off; nvn_texdesc d; } alias_key;
|
||||
enum { ALIAS_CELL = 16, // the index's cells: 64 KB of a pool
|
||||
ALIAS_IDLE = 16 }; // a takeover spares an entry a lookup found in the last ALIAS_IDLE submissions
|
||||
// Resolution: each takes g_smu, and a texture it publishes under a serial is published in the same hold as the lookup or the check (borrowed, as a serial lookup)
|
||||
id own_alias_take(uint64_t serial, const alias_key *k); // k's shared texture, marked looked up and published under serial (tex_put); nil (nothing changed) when k has none
|
||||
id own_alias_publish(uint64_t serial, const alias_key *k, id o); // registration: o takes k's bytes over (the entries of the pool it overlaps under another description go, unless a
|
||||
// live entry or a pin holds them or a lookup found them lately), goes in under k and is published under serial.
|
||||
// → what serial resolves to now: o, or the texture another thread registered under k first (o is then the caller's to retire)
|
||||
id own_alias_peek(const alias_key *k); // k's shared texture (not marked looked up)
|
||||
// sRGB / linear views of a shared texture (tex_mtl_'s twins). A view is valid while its parent is in the sharing table or it or its parent is pinned; each decision is taken in the
|
||||
// same hold of g_smu as the entry it changes (docs/reference/nvn-objects.md, A finalized twin view left in the ghost ring)
|
||||
int own_view_put(uint64_t k, const alias_key *pk, id<MTLTexture> view); // tex_put of the view only while its parent is pk's shared texture (pk marked looked up): 1 published, 0 nothing changed
|
||||
int own_view_drop_stale(uint64_t k, id view); // a view no longer valid: 1, and k's live entry, or with none its ghost entry (and ring slot), goes if it still names the view; 0 valid
|
||||
// An upgrade (a texture made again with more usage): every name of `from`, the serial tables, the pins and the sharing table, moves to `to` in place; then `put` (when not nil) is published under
|
||||
// serial unless the rename already named it there (an sRGB / linear twin passes its view made again on `to`). One hold of g_smu. → whether serial was one of from's names
|
||||
int own_upgrade(id from, id to, uint64_t serial, id put);
|
||||
// Caller holds g_smu
|
||||
id own_alias_get_locked(const alias_key *k);
|
||||
int own_shared_locked(id o); // the sharing table holds o
|
||||
int own_keeps_locked(id o); // o stays resident whatever its entries do: pinned or shared
|
||||
uint32_t own_alias_drop_pool_locked(const void *pool); // a pool's Finalize: its entries go (retired, own_hook_drop). → how many
|
||||
void own_alias_at_locked(const void *pool, int64_t off, void (^fn)(const alias_key *k, id o)); // the entries that start at off in the pool (its cell of the index)
|
||||
void own_alias_each_locked(void (^fn)(const alias_key *k, id o));
|
||||
typedef struct { uint32_t n, cap, cells; uint64_t evicted, taken; } own_alias_stats; // entries, capacity, index cells, evicted with pools / on takeover in total
|
||||
own_alias_stats own_alias_stats_locked(void);
|
||||
uint32_t own_alias_unref_locked(void); // entries no live or ghost entry resolves to (a walk: the memory report)
|
||||
// Seams for the tests (the replay layer registers through own_alias_publish and invalidates through own_alias_drop_pool_locked). Caller holds g_smu
|
||||
void own_alias_put_locked(const alias_key *k, id o, uint64_t seen); // o under k, retiring an entry it replaces; seen: the submission of its last lookup (0: none)
|
||||
int own_alias_del_locked(const alias_key *k); // 1 when k had an entry (retired), 0
|
||||
uint32_t own_alias_take_over_locked(const alias_key *k); // the takeover alone. → how many went
|
||||
|
||||
// Live texture objects. Caller holds g_smu
|
||||
void own_tex_live_put_locked(const tex_t *t); // mtl_tex_init
|
||||
@@ -69,7 +100,7 @@ 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
|
||||
// Every name of `from` (live and ghost entries, texture pins, the sharing table) 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
|
||||
@@ -83,6 +114,6 @@ int own_check_locked(char *why, size_t n); // the held counts against the tabl
|
||||
|
||||
// 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
|
||||
uint64_t own_hook_submission(void); // the submission being encoded (g_fence + 1): a lookup's mark, which ALIAS_IDLE counts from
|
||||
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)
|
||||
+194
-6
@@ -1,6 +1,7 @@
|
||||
// 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).
|
||||
// The NVN resource owner: nvn_owner.h says what it holds, what a lookup returns and the lock order. Moved from nvn_mtl.m (the serial tables, then the sharing table).
|
||||
#import <Metal/Metal.h>
|
||||
#include "nvn_owner.h"
|
||||
#include "nvn_tex.h"
|
||||
#include "../../host/dm.h"
|
||||
#include <mach/mach.h>
|
||||
|
||||
@@ -47,7 +48,129 @@ static void count_add(nvn_hmap *m, const void *o, int 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
|
||||
|
||||
// The sharing table. Textures shared by storage (non-packaged): key = memory pool + offset + texture description. An NVN texture object is only a descriptor; an object rebuilt over the
|
||||
// same storage with the same description is the same texture; SMBW rebuilds its transient render targets on new objects every frame and never Finalizes them, so building one per serial
|
||||
// leaked about 13 MB of GPU memory per frame. Textures here are kept resident by the sharing table, and when the serial table replaces / deletes them it only drops its reference
|
||||
// (own_hook_drop keeping it resident), never removing them from the residency set. Entries go when their pool is finalized (own_alias_drop_pool_locked) or another texture takes over
|
||||
// their storage (alias_take_over). Under g_smu
|
||||
static nvn_hmap g_alias = {.klen = sizeof(alias_key)};
|
||||
// The shared textures by where their storage lies, for alias_take_over and the image families' owner lookup (own_alias_at_locked): {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 (an upgrade replaces a value in place)
|
||||
typedef struct { alias_key k; int64_t end; } alias_ent;
|
||||
typedef struct { uint32_t n, cap; alias_ent e[]; } alias_cell;
|
||||
static nvn_hmap g_alias_cells = {.klen = 16};
|
||||
static void alias_index(const alias_key *k, int add) {
|
||||
nvn_texlayout L; nvn_tex_layout(&k->d, &L);
|
||||
int64_t end = k->off + (int64_t)L.size, c1 = (end > k->off ? end - 1 : k->off) >> ALIAS_CELL;
|
||||
for (int64_t c = k->off >> ALIAS_CELL; c <= c1; c++) {
|
||||
uint64_t ck[2] = {k->pool, (uint64_t)c}; alias_cell *v = nvn_hm_get(&g_alias_cells, ck);
|
||||
uint32_t i = 0; if (v) for (; i < v->n; i++) if (!memcmp(&v->e[i].k, k, sizeof *k)) break;
|
||||
if (add) {
|
||||
if (v && i < v->n) continue;
|
||||
if (!v || v->n == v->cap) {
|
||||
uint32_t cap = v ? v->cap * 2 : 4; alias_cell *nv = realloc(v, sizeof *nv + cap * sizeof nv->e[0]); if (!nv) abort();
|
||||
if (!v) nv->n = 0; nv->cap = cap; v = nv; nvn_hm_put(&g_alias_cells, ck, v);
|
||||
}
|
||||
v->e[v->n].k = *k; v->e[v->n++].end = end;
|
||||
} else if (v && i < v->n) {
|
||||
v->e[i] = v->e[--v->n];
|
||||
if (!v->n) { nvn_hm_del(&g_alias_cells, ck); free(v); }
|
||||
}
|
||||
}
|
||||
}
|
||||
static nvn_hmap g_alias_seen = {.klen = sizeof(alias_key)}; // key → the submission of its last lookup or creation (own_hook_submission; alias_take_over spares entries looked up lately)
|
||||
static CFMutableSetRef g_alias_set; // the textures in the sharing table (non-owning; "is this shared")
|
||||
static uint64_t g_alias_evicted, g_alias_taken; // shared textures evicted on pool Finalize / when another texture took over their storage, in total (the memory report)
|
||||
int own_shared_locked(id o) { return g_alias_set && o && CFSetContainsValue(g_alias_set, (__bridge const void *)o); }
|
||||
int own_keeps_locked(id o) { return tex_object_pinned(o) || own_shared_locked(o); }
|
||||
static void obj_drop(id o) { own_hook_drop(o, own_keeps_locked(o)); } // caller holds g_smu
|
||||
static void alias_seen(const alias_key *k, uint64_t now) { nvn_hm_put(&g_alias_seen, k, (void *)(uintptr_t)now); }
|
||||
// The sharing table's two writers: an entry goes in and out with its cells (alias_index), its place in g_alias_set and its last lookup (g_alias_seen), so none of them can be left out.
|
||||
// A removed texture is retired (deferred release, own_hook_drop). Caller holds g_smu
|
||||
static int alias_del(const alias_key *k) {
|
||||
void *old = nvn_hm_del(&g_alias, k);
|
||||
if (!old) return 0;
|
||||
alias_index(k, 0); nvn_hm_del(&g_alias_seen, k);
|
||||
id o = CFBridgingRelease(old);
|
||||
if (g_alias_set) CFSetRemoveValue(g_alias_set, (__bridge const void *)o);
|
||||
own_hook_drop(o, 0);
|
||||
return 1;
|
||||
}
|
||||
static void alias_put(const alias_key *k, id o, uint64_t seen) { // an entry k had is retired first: replaced in the map alone, it stayed in g_alias_set and was released at once
|
||||
if (nvn_hm_get(&g_alias, k)) alias_del(k);
|
||||
if (!g_alias_set) g_alias_set = CFSetCreateMutable(NULL, 0, NULL);
|
||||
hm_put_id(&g_alias, k, o); alias_index(k, 1); CFSetAddValue(g_alias_set, (__bridge const void *)o); if (seen) alias_seen(k, seen);
|
||||
}
|
||||
id own_alias_get_locked(const alias_key *k) { return hm_get_id(&g_alias, k); }
|
||||
id own_alias_peek(const alias_key *k) { pthread_mutex_lock(&g_smu); id o = hm_get_id(&g_alias, k); pthread_mutex_unlock(&g_smu); return o; }
|
||||
void own_alias_put_locked(const alias_key *k, id o, uint64_t seen) { alias_put(k, o, seen); }
|
||||
int own_alias_del_locked(const alias_key *k) { return alias_del(k); }
|
||||
// On memory pool Finalize, the textures built on that pool leave the sharing table (the keys collected first, then deleted: deletion shifts entries back). The sharing table used not to
|
||||
// evict: Mario's pools stay for good, so entries are bounded; Engage frees pools on scene changes and rebuilds on new ones, so the old pools' render targets and other private textures
|
||||
// stayed in the table forever (about 0.8 GB more GPU memory over 850 s of the title demo), and linear textures built directly on a pool also kept the whole pool buffer alive. Also, when a
|
||||
// pool object's address is reused, old entries would hand a texture on the old storage to the new pool's texture at the same offset and description. Evicted textures go through deferred
|
||||
// release; if a serial entry still holds one, that is just one more reference
|
||||
uint32_t own_alias_drop_pool_locked(const void *pool) {
|
||||
uint32_t n = 0; alias_key *ks = NULL;
|
||||
for (uint32_t i = 0; i < g_alias.cap; i++) {
|
||||
if (!g_alias.vals[i]) continue;
|
||||
alias_key k; memcpy(&k, g_alias.keys + (size_t)i * g_alias.klen, sizeof k);
|
||||
if (k.pool != (uint64_t)(uintptr_t)pool) continue;
|
||||
alias_key *nk = realloc(ks, (n + 1) * sizeof *ks); if (!nk) break; ks = nk; ks[n++] = k;
|
||||
}
|
||||
for (uint32_t j = 0; j < n; j++) alias_del(&ks[j]);
|
||||
free(ks); g_alias_evicted += n;
|
||||
return n;
|
||||
}
|
||||
// A new shared texture takes over storage: entries of the same pool whose storage overlaps the new texture's under another description go, unless a live texture object still resolves
|
||||
// to them (an object rebuilt over the same storage with the same description must keep finding it: SMBW's transient render targets) or a lookup found them in the last ALIAS_IDLE submissions
|
||||
// (Breath of the Wild finalizes its transient render targets before it submits and makes them again the next frame: targets sharing storage would evict each other every frame). On NVN
|
||||
// the memory now belongs to the new texture; the old private Metal texture only held a copy nothing can name again. STEINS;GATE RE:BOOT decodes every image it shows into one scratch
|
||||
// region of a pool that lives for good and finalizes the texture before the next one: each image added an entry, 468 of them and 1.58 GB of GPU memory 24,000 frames into a new game.
|
||||
// The texture is retired (deferred until the submissions that used it complete); one a finalized object still resolves through the ghost ring stays resident until the ring lets it go
|
||||
// (nvn_mtl.m's defer_drain, g_retire_held). The change reports no serial (own_hook_changed(0)), as it changes no serial's texture: an entry a live serial holds is not evicted
|
||||
// (serial_table_holds), and one a finalized serial holds through the ghost ring keeps its texture. A live sRGB / linear twin's serial holds a view of the shared texture, which counts as
|
||||
// holding the texture (count_add), so a twin keeps its parent as a texture of the parent's own format would; with the view alone counted the parent went, and no note sent the twin's
|
||||
// texture IDs to tex_mtl_'s stale-view check (GitHub issue #2125). A retired ID's view goes at the submission's end (nvn_mtl.m's treg_finish_submission), and the pool tables take
|
||||
// those IDs again at the next one (pool_snap.trans). With no note, every takeover (about 8,400 in six minutes walking through Ocarina of Time's Kokiri Forest) sent every
|
||||
// texture ID down the slow path and made the next pool walk build the 33,025-entry table whole. Caller holds g_smu
|
||||
static uint32_t alias_take_over(const alias_key *nk, uint64_t now) {
|
||||
nvn_texlayout L; nvn_tex_layout(&nk->d, &L);
|
||||
if (!L.size) return 0;
|
||||
int64_t lo = nk->off, hi = nk->off + (int64_t)L.size;
|
||||
uint32_t n = 0; alias_key ks[16];
|
||||
for (int64_t c = lo >> ALIAS_CELL; c <= (hi - 1) >> ALIAS_CELL && n < 16; c++) { // the entries whose storage reaches into this one's cells (alias_index)
|
||||
uint64_t ck[2] = {nk->pool, (uint64_t)c}; const alias_cell *v = nvn_hm_get(&g_alias_cells, ck);
|
||||
for (uint32_t i = 0; v && i < v->n && n < 16; i++) {
|
||||
const alias_key *k = &v->e[i].k;
|
||||
if (!memcmp(k, nk, sizeof *k) || k->off >= hi || v->e[i].end <= lo) continue;
|
||||
uint32_t j = 0; while (j < n && memcmp(&ks[j], k, sizeof *k)) j++;
|
||||
if (j < n) continue; // met in an earlier cell
|
||||
void *val = nvn_hm_get(&g_alias, k); if (!val) continue;
|
||||
uint64_t seen = (uint64_t)(uintptr_t)nvn_hm_get(&g_alias_seen, k);
|
||||
if ((seen && now - seen < ALIAS_IDLE) || serial_table_holds(val)) continue;
|
||||
ks[n++] = *k;
|
||||
}
|
||||
}
|
||||
for (uint32_t j = 0; j < n; j++) alias_del(&ks[j]);
|
||||
if (n) { own_hook_changed(0); g_alias_taken += n; }
|
||||
return n;
|
||||
}
|
||||
uint32_t own_alias_take_over_locked(const alias_key *k) { return alias_take_over(k, own_hook_submission()); }
|
||||
void own_alias_at_locked(const void *pool, int64_t off, void (^fn)(const alias_key *k, id o)) {
|
||||
uint64_t ck[2] = {(uint64_t)(uintptr_t)pool, (uint64_t)(off >> ALIAS_CELL)}; const alias_cell *cell = nvn_hm_get(&g_alias_cells, ck); // every entry at this offset lies in its cell
|
||||
for (uint32_t i = 0; cell && i < cell->n; i++) {
|
||||
const alias_key *k = &cell->e[i].k; if (k->off != off) continue;
|
||||
id o = hm_get_id(&g_alias, k); if (o) fn(k, o);
|
||||
}
|
||||
}
|
||||
void own_alias_each_locked(void (^fn)(const alias_key *k, id o)) {
|
||||
for (uint32_t i = 0; i < g_alias.cap; i++) if (g_alias.vals[i]) fn((const alias_key *)(const void *)(g_alias.keys + (size_t)i * g_alias.klen), (__bridge id)g_alias.vals[i]);
|
||||
}
|
||||
own_alias_stats own_alias_stats_locked(void) { return (own_alias_stats){.n = g_alias.n, .cap = g_alias.cap, .cells = g_alias_cells.n, .evicted = g_alias_evicted, .taken = g_alias_taken}; }
|
||||
|
||||
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; }
|
||||
@@ -75,7 +198,7 @@ static void tex_pin_set(tex_pin *pin, id object) { // g_smu held
|
||||
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;
|
||||
held_add(pin->object, -1); own_hook_drop(old, own_keeps_locked(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;
|
||||
@@ -114,14 +237,35 @@ static void tex_put_locked(uint64_t k, id o) { // serials from 2^62 up are ext
|
||||
}
|
||||
void tex_put(uint64_t k, id o) { pthread_mutex_lock(&g_smu); tex_put_locked(k, o); pthread_mutex_unlock(&g_smu); }
|
||||
|
||||
// A serial's shared texture is found and published in one hold: a pool's Finalize on the game's thread between the two (as when tex_mtl_ looked up, let g_smu go and then called
|
||||
// tex_put) gave the serial a texture already evicted and retired
|
||||
id own_alias_take(uint64_t serial, const alias_key *k) {
|
||||
pthread_mutex_lock(&g_smu);
|
||||
id o = hm_get_id(&g_alias, k); if (o) { alias_seen(k, own_hook_submission()); tex_put_locked(serial, o); }
|
||||
pthread_mutex_unlock(&g_smu); return o;
|
||||
}
|
||||
// The takeover, the entry and the serial's publication in one hold. A texture another thread registered under the key since the caller's lookup missed is the one the storage's
|
||||
// serials share: the serial takes it, and the caller retires its own (replacing it instead, as the table did, left the first serial a texture no longer shared)
|
||||
id own_alias_publish(uint64_t serial, const alias_key *k, id o) {
|
||||
pthread_mutex_lock(&g_smu);
|
||||
uint64_t now = own_hook_submission(); id cur = hm_get_id(&g_alias, k);
|
||||
if (cur) { alias_seen(k, now); o = cur; }
|
||||
else { alias_take_over(k, now); alias_put(k, o, now); }
|
||||
tex_put_locked(serial, o);
|
||||
pthread_mutex_unlock(&g_smu); return o;
|
||||
}
|
||||
|
||||
// sRGB / linear views (tex_mtl_'s twin of a shared texture). A view shares its parent's bytes only while the parent is in the sharing table: a parent that left it (evicted with its
|
||||
// pool, taken over, upgraded) no longer holds the storage's bytes, and neither does the view. The parent's place there is checked in the same hold of g_smu as the view's entry
|
||||
// changes, so no entry names a view whose parent had already gone. Publishing first and taking the entry back with serial_del when the parent turned out gone (as tex_mtl_ did)
|
||||
// let another thread resolve the view in between, and for a finalized serial left it in the ghost ring for good: serial_del ends a live entry only, which is what a finalize wants.
|
||||
static id view_parent(id o) { return [o respondsToSelector:@selector(parentTexture)] ? ((id<MTLTexture>)o).parentTexture : nil; }
|
||||
static int view_kept(id o) { return tex_object_pinned(o) || own_hook_shared(o); } // nvn_mtl.m's alias_has: a pinned or shared object keeps its view valid
|
||||
int own_view_put(uint64_t k, id<MTLTexture> view) {
|
||||
pthread_mutex_lock(&g_smu); id p = view_parent(view); int ok = p && own_hook_shared(p); if (ok) tex_put_locked(k, view); pthread_mutex_unlock(&g_smu); return ok;
|
||||
static int view_kept(id o) { return own_keeps_locked(o); } // a pinned or shared object keeps its view valid
|
||||
int own_view_put(uint64_t k, const alias_key *pk, id<MTLTexture> view) {
|
||||
pthread_mutex_lock(&g_smu);
|
||||
id p = view_parent(view); int ok = p && hm_get_id(&g_alias, pk) == p;
|
||||
if (ok) { alias_seen(pk, own_hook_submission()); tex_put_locked(k, view); }
|
||||
pthread_mutex_unlock(&g_smu); return ok;
|
||||
}
|
||||
// The ring slot goes with the entry: left behind, it would evict a later ghost entry of the serial early. Caller holds g_smu
|
||||
static void ghost_del_locked(uint64_t k) {
|
||||
@@ -150,8 +294,22 @@ int own_rename_locked(id from, id to, uint64_t serial) {
|
||||
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); }
|
||||
int shared = 0; // the sharing table: values replaced in place (its keys, cells and last lookups stay)
|
||||
for (uint32_t i = 0; i < g_alias.cap; i++) {
|
||||
if (g_alias.vals[i] != (__bridge void *)from) continue;
|
||||
void *old = g_alias.vals[i]; g_alias.vals[i] = (void *)CFBridgingRetain(to); CFRelease(old); shared = 1;
|
||||
}
|
||||
if (shared) { CFSetRemoveValue(g_alias_set, (__bridge const void *)from); CFSetAddValue(g_alias_set, (__bridge const void *)to); }
|
||||
return named;
|
||||
}
|
||||
// The rename and the asking serial's new object in one hold: published after g_smu was let go (as the upgrade did), a reader in between found the serial's old view, of a parent no
|
||||
// longer shared
|
||||
int own_upgrade(id from, id to, uint64_t serial, id put) {
|
||||
pthread_mutex_lock(&g_smu);
|
||||
int named = own_rename_locked(from, to, serial);
|
||||
if (put && !(named && put == to)) tex_put_locked(serial, put);
|
||||
pthread_mutex_unlock(&g_smu); 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
|
||||
@@ -172,6 +330,12 @@ void own_prog_pin(uint64_t serial) {
|
||||
pin->refs++;
|
||||
}
|
||||
|
||||
uint32_t own_alias_unref_locked(void) {
|
||||
CFMutableSetRef held = CFSetCreateMutable(NULL, 0, NULL); uint32_t n = 0;
|
||||
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])) n++;
|
||||
CFRelease(held); return n;
|
||||
}
|
||||
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};
|
||||
@@ -209,6 +373,30 @@ int own_check_locked(char *why, size_t n) {
|
||||
for (unsigned j = i + 1; ok && j < NGHOST; j++) if (g_ghost_ring[j] == g_ghost_ring[i]) { snprintf(why, n, "ring slots %u and %u both name serial %llu", i, j, (unsigned long long)g_ghost_ring[i]); ok = 0; }
|
||||
}
|
||||
if (ok && slots != g_ghost.n) { snprintf(why, n, "%u ring slots for %u ghost serials", slots, g_ghost.n); ok = 0; }
|
||||
// The sharing table: g_alias_set is the set of its textures, each key lies in every cell its bytes reach and in no other, and a last lookup is kept for entries only
|
||||
CFMutableSetRef vals = CFSetCreateMutable(NULL, 0, NULL); uint64_t want_ents = 0;
|
||||
for (uint32_t i = 0; ok && i < g_alias.cap; i++) {
|
||||
if (!g_alias.vals[i]) continue;
|
||||
const alias_key *k = (const alias_key *)(const void *)(g_alias.keys + (size_t)i * g_alias.klen);
|
||||
CFSetAddValue(vals, g_alias.vals[i]);
|
||||
if (!own_shared_locked((__bridge id)g_alias.vals[i])) { snprintf(why, n, "shared texture %p (pool %#llx offset %#llx) is not in the set of shared textures", g_alias.vals[i], (unsigned long long)k->pool, (unsigned long long)k->off); ok = 0; break; }
|
||||
nvn_texlayout L; nvn_tex_layout(&k->d, &L);
|
||||
int64_t end = k->off + (int64_t)L.size, c1 = (end > k->off ? end - 1 : k->off) >> ALIAS_CELL;
|
||||
for (int64_t c = k->off >> ALIAS_CELL; ok && c <= c1; c++) {
|
||||
uint64_t ck[2] = {k->pool, (uint64_t)c}; const alias_cell *v = nvn_hm_get(&g_alias_cells, ck); uint32_t j = 0;
|
||||
if (v) for (; j < v->n; j++) if (!memcmp(&v->e[j].k, k, sizeof *k)) break;
|
||||
if (!v || j == v->n) { snprintf(why, n, "shared entry (pool %#llx offset %#llx) is missing from cell %lld of the index", (unsigned long long)k->pool, (unsigned long long)k->off, (long long)c); ok = 0; }
|
||||
want_ents++;
|
||||
}
|
||||
}
|
||||
uint64_t ents = 0;
|
||||
for (uint32_t i = 0; ok && i < g_alias_cells.cap; i++) if (g_alias_cells.vals[i]) ents += ((const alias_cell *)g_alias_cells.vals[i])->n;
|
||||
if (ok && ents != want_ents) { snprintf(why, n, "%llu index entries for the %llu cells the shared entries reach", (unsigned long long)ents, (unsigned long long)want_ents); ok = 0; }
|
||||
CFIndex nset = g_alias_set ? CFSetGetCount(g_alias_set) : 0;
|
||||
if (ok && nset != CFSetGetCount(vals)) { snprintf(why, n, "%ld textures in the set of shared textures, %ld in the table", (long)nset, (long)CFSetGetCount(vals)); ok = 0; }
|
||||
for (uint32_t i = 0; ok && i < g_alias_seen.cap; i++)
|
||||
if (g_alias_seen.vals[i] && !nvn_hm_get(&g_alias, g_alias_seen.keys + (size_t)i * g_alias_seen.klen)) { snprintf(why, n, "a last lookup kept for a key with no shared entry"); ok = 0; }
|
||||
CFRelease(vals);
|
||||
CFRelease(live); CFRelease(ghost);
|
||||
return ok;
|
||||
}
|
||||
@@ -471,9 +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
|
||||
# The resource owner alone: nvn_owner.m (and the texture layouts it sizes shared entries with) 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_tex.c ../nvn/nvn_tex.h ../nvn/nvn_hmap.h ../nvn/nvn_obj.h $(GAME_H)
|
||||
clang $(CFLAGS) -fobjc-arc -o $@ t_serial_owner.m ../nvn/nvn_owner.m ../nvn/nvn_tex.c -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
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// alias_take_over (nvn_mtl.m): when a new shared texture takes over storage, an entry a live texture object still resolves to stays, and one nothing resolves to any more goes once
|
||||
// The sharing table's takeover (nvn_owner.m, own_alias_take_over_locked; own_alias_publish runs it): when a new shared texture takes over storage, an entry a live texture object still resolves to stays, and one nothing resolves to any more goes once
|
||||
// no lookup has found it for ALIAS_IDLE submissions. Whether a live object resolves to it is one lookup in g_by_serial_held, which every writer of g_by_serial keeps (serial_put,
|
||||
// serial_del, tex_put and the upgrade's in-place replacement): two serials naming one object count twice and the object stays held until both go. Last, a takeover past 1,000
|
||||
// held entries with 20,000 serials in the table, which walked the serial table for each entry before (it prints the time).
|
||||
// The candidates come from an index by pool and 64 KB cell (alias_index), which alias_put and alias_del keep: an entry that starts in an earlier cell is found, one in the same
|
||||
// The candidates come from the owner's index by pool and 64 KB cell, which its writers keep (own_alias_put_locked, own_alias_del_locked, the takeover, a pool drop): an entry that starts in an earlier cell is found, one in the same
|
||||
// cell that ends before the new texture is not, one that spans several of the new texture's cells goes once, another pool's entries are not met, and a removal or a pool drop
|
||||
// leaves no cell behind. Last, a takeover with 100,000 entries in the table, which walked the table's whole capacity before (it prints the time).
|
||||
#include "../nvn/nvn_mtl.m"
|
||||
@@ -20,12 +20,15 @@ static tex_t tex(uint64_t serial, NVNformat format, int w) {
|
||||
nvn_tex_layout(&t.d, &t.L);
|
||||
return t;
|
||||
}
|
||||
static int shared(id<MTLTexture> t) { pthread_mutex_lock(&g_smu); int r = alias_has(t); pthread_mutex_unlock(&g_smu); return r; }
|
||||
static int shared(id<MTLTexture> t) { pthread_mutex_lock(&g_smu); int r = own_keeps_locked(t); pthread_mutex_unlock(&g_smu); return r; }
|
||||
static int held(id o) { pthread_mutex_lock(&g_smu); int r = serial_table_holds((__bridge void *)o); pthread_mutex_unlock(&g_smu); return r; }
|
||||
static alias_key akey(const void *pool, int64_t off, NVNformat format, int w) { alias_key k; memset(&k, 0, sizeof k); k.pool = (uint64_t)(uintptr_t)pool; k.off = off; k.d = tex(0, format, w).d; return k; }
|
||||
static int64_t ksize(const alias_key *k) { nvn_texlayout L; nvn_tex_layout(&k->d, &L); return (int64_t)L.size; }
|
||||
static void idle_put(const alias_key *k, id o) { alias_put(k, o); nvn_hm_del(&g_alias_seen, k); } // an entry no lookup found lately. Under g_smu
|
||||
static int has(const alias_key *k) { return nvn_hm_get(&g_alias, k) != NULL; }
|
||||
static void idle_put(const alias_key *k, id o) { own_alias_put_locked(k, o, 0); } // an entry no lookup found lately. Under g_smu
|
||||
static void seen_put(const alias_key *k, id o) { own_alias_put_locked(k, o, own_hook_submission()); } // one looked up in this submission. Under g_smu
|
||||
static own_alias_stats ast(void) { return own_alias_stats_locked(); } // under g_smu
|
||||
static int consistent(void) { char why[256] = ""; int ok = own_check_locked(why, sizeof why); if (!ok) fprintf(stderr, "owner: %s\n", why); return ok; } // under g_smu
|
||||
static int has(const alias_key *k) { return own_alias_get_locked(k) != nil; }
|
||||
static void next_submission(void) { CHECK(mtl_wait(mtl_sub_commit(), 5000000000ull), "submission completes"); mtl_sub_begin(); }
|
||||
|
||||
int main(void) { @autoreleasepool {
|
||||
@@ -67,9 +70,9 @@ int main(void) { @autoreleasepool {
|
||||
for (int i = 0; i < NS; i++) tex_put((1ull << 62) + 100 + i, i < NA ? keep[i] : [NSObject new]);
|
||||
tex_t big = tex(91010, NVN_FORMAT_RGBA8, 1024); alias_key nk; memset(&nk, 0, sizeof nk); nk.d = big.d;
|
||||
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, nser = own_stats_locked().live;
|
||||
uint32_t before = ast().n; uint64_t t0 = bp_now();
|
||||
own_alias_take_over_locked(&nk);
|
||||
double ms = (bp_now() - t0) / 1e6; uint32_t after = ast().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, nser, ms);
|
||||
CHECK(after == before, "held entries are kept");
|
||||
@@ -80,40 +83,40 @@ int main(void) { @autoreleasepool {
|
||||
static pool_t pl, other; const void *P = &pl, *Q = &other; // zeroed pools: only their addresses are used
|
||||
NSMutableArray *keep = [NSMutableArray array]; id (^obj)(void) = ^{ id e = [NSObject new]; [keep addObject:e]; return e; };
|
||||
pthread_mutex_lock(&g_smu);
|
||||
uint32_t cells0 = g_alias_cells.n, n0 = g_alias.n;
|
||||
uint32_t cells0 = ast().cells, n0 = ast().n;
|
||||
alias_key big = akey(P, 0, NVN_FORMAT_RGBA8, 512); int64_t bs = ksize(&big), last = (bs - 1) >> ALIAS_CELL;
|
||||
CHECK(last >= 4, "the large texture spans several cells");
|
||||
idle_put(&big, obj());
|
||||
CHECK(g_alias_cells.n == cells0 + (uint32_t)last + 1, "an entry lies in every cell its bytes reach");
|
||||
CHECK(ast().cells == cells0 + (uint32_t)last + 1, "an entry lies in every cell its bytes reach");
|
||||
alias_key far = akey(Q, 0, NVN_FORMAT_RGBA8, 512); idle_put(&far, obj()); // the same bytes of another pool
|
||||
alias_key tail = akey(P, last << ALIAS_CELL, NVN_FORMAT_R32F, 16);
|
||||
CHECK(ksize(&tail) <= 0x8000 && bs > tail.off, "the small texture fits half a cell and lies in the large one's last");
|
||||
alias_take_over(&tail); alias_put(&tail, obj());
|
||||
own_alias_take_over_locked(&tail); idle_put(&tail, obj());
|
||||
CHECK(!has(&big), "an entry that starts in an earlier cell and reaches into the new texture's goes");
|
||||
CHECK(has(&far), "another pool's entry over the same offsets stays");
|
||||
CHECK(g_alias_cells.n == cells0 + (uint32_t)last + 1 + 1, "the entry taken over left every cell it lay in");
|
||||
nvn_hm_del(&g_alias_seen, &tail);
|
||||
CHECK(ast().cells == cells0 + (uint32_t)last + 1 + 1, "the entry taken over left every cell it lay in");
|
||||
alias_key after = akey(P, tail.off + 0x8000, NVN_FORMAT_RGBA8, 16);
|
||||
alias_take_over(&after); idle_put(&after, obj());
|
||||
own_alias_take_over_locked(&after); idle_put(&after, obj());
|
||||
CHECK(has(&tail), "an entry in the same cell that ends before the new texture stays");
|
||||
alias_key before = akey(P, tail.off, NVN_FORMAT_RGBA8, 16); // the small one's bytes under another description
|
||||
alias_take_over(&before); idle_put(&before, obj());
|
||||
own_alias_take_over_locked(&before); idle_put(&before, obj());
|
||||
CHECK(!has(&tail) && has(&after), "and goes when its bytes are taken");
|
||||
alias_key big2 = akey(P, 0, NVN_FORMAT_R32F, 512); int over = after.off < ksize(&big2); // its last cell holds the two small entries
|
||||
alias_take_over(&big2); idle_put(&big2, obj());
|
||||
own_alias_take_over_locked(&big2); idle_put(&big2, obj());
|
||||
CHECK(!has(&before) && has(&after) == !over, "a larger texture takes the entries in its cells that it overlaps");
|
||||
alias_key big3 = akey(P, 0, NVN_FORMAT_RG16F, 512); uint64_t t2 = g_alias_taken; uint32_t n2 = g_alias.n;
|
||||
alias_take_over(&big3);
|
||||
CHECK(!has(&big2) && g_alias_taken - t2 == n2 - g_alias.n && n2 - g_alias.n == 1u + (has(&after) ? 0 : !over), "an entry that spans several of the new texture's cells is taken once");
|
||||
alias_put(&big3, obj());
|
||||
alias_key big3 = akey(P, 0, NVN_FORMAT_RG16F, 512); uint64_t t2 = ast().taken; uint32_t n2 = ast().n;
|
||||
own_alias_take_over_locked(&big3);
|
||||
CHECK(!has(&big2) && ast().taken - t2 == n2 - ast().n && n2 - ast().n == 1u + (has(&after) ? 0 : !over), "an entry that spans several of the new texture's cells is taken once");
|
||||
seen_put(&big3, obj());
|
||||
pthread_mutex_unlock(&g_smu);
|
||||
mtl_alias_drop_pool(P);
|
||||
pthread_mutex_lock(&g_smu);
|
||||
CHECK(!has(&big3) && !has(&after) && has(&far), "a pool drop takes that pool's entries only");
|
||||
CHECK(g_alias_cells.n == cells0 + (uint32_t)((ksize(&far) - 1) >> ALIAS_CELL) + 1, "and leaves none of its cells");
|
||||
alias_del(&far);
|
||||
CHECK(g_alias_cells.n == cells0 && g_alias.n == n0, "a removal leaves the index as it was");
|
||||
CHECK(!alias_del(&far), "removing an entry that is not there does nothing");
|
||||
CHECK(ast().cells == cells0 + (uint32_t)((ksize(&far) - 1) >> ALIAS_CELL) + 1, "and leaves none of its cells");
|
||||
own_alias_del_locked(&far);
|
||||
CHECK(ast().cells == cells0 && ast().n == n0, "a removal leaves the index as it was");
|
||||
CHECK(!own_alias_del_locked(&far), "removing an entry that is not there does nothing");
|
||||
CHECK(consistent(), "the index, the set and the last lookups agree with the table");
|
||||
pthread_mutex_unlock(&g_smu);
|
||||
}
|
||||
{ // The cost: 100,000 entries in the table, one per cell of a pool, and a texture over 16 of them that were looked up lately (so they stay)
|
||||
@@ -121,17 +124,17 @@ int main(void) { @autoreleasepool {
|
||||
static pool_t pl; const void *P = &pl;
|
||||
NSMutableArray *keep = [NSMutableArray arrayWithCapacity:NA];
|
||||
pthread_mutex_lock(&g_smu);
|
||||
for (int i = 0; i < NA; i++) { id e = [NSObject new]; [keep addObject:e]; alias_key k = akey(P, (int64_t)i << ALIAS_CELL, NVN_FORMAT_RGBA8, 32); alias_put(&k, e); }
|
||||
for (int i = 0; i < NA; i++) { id e = [NSObject new]; [keep addObject:e]; alias_key k = akey(P, (int64_t)i << ALIAS_CELL, NVN_FORMAT_RGBA8, 32); seen_put(&k, e); }
|
||||
alias_key nk = akey(P, 5000ll << ALIAS_CELL, NVN_FORMAT_R32F, 512);
|
||||
uint32_t before = g_alias.n; uint64_t t0 = bp_now();
|
||||
for (int i = 0; i < 100; i++) alias_take_over(&nk);
|
||||
double ms = (bp_now() - t0) / 1e6 / 100; uint32_t after = g_alias.n;
|
||||
printf("a takeover with %u entries in the table (capacity %u): %.4f ms\n", g_alias.n, g_alias.cap, ms);
|
||||
uint32_t before = ast().n; uint64_t t0 = bp_now();
|
||||
for (int i = 0; i < 100; i++) own_alias_take_over_locked(&nk);
|
||||
double ms = (bp_now() - t0) / 1e6 / 100; uint32_t after = ast().n;
|
||||
printf("a takeover with %u entries in the table (capacity %u): %.4f ms\n", ast().n, ast().cap, ms);
|
||||
CHECK(after == before, "entries looked up lately are kept");
|
||||
CHECK(ms < 0.1, "the takeover reads only its own cells (under 0.1 ms; walking the table took 0.65 ms on an M6)");
|
||||
pthread_mutex_unlock(&g_smu);
|
||||
mtl_alias_drop_pool(P);
|
||||
pthread_mutex_lock(&g_smu); CHECK(g_alias.n == before - NA, "the pool's entries go with it"); pthread_mutex_unlock(&g_smu);
|
||||
pthread_mutex_lock(&g_smu); CHECK(ast().n == before - NA, "the pool's entries go with it"); pthread_mutex_unlock(&g_smu);
|
||||
}
|
||||
mtl_tex_release(&b); mtl_tex_release(&c);
|
||||
next_submission(); defer_drain();
|
||||
|
||||
@@ -20,6 +20,7 @@ static int checks, failed;
|
||||
#define CHECK(c, label) do { checks++; if (!(c)) { failed++; fprintf(stderr, "FAIL: %s (line %d)\n", label, __LINE__); } } while (0)
|
||||
|
||||
enum { LEN = 1 << 20, IDS = 10 };
|
||||
static uint64_t alias_taken(void) { pthread_mutex_lock(&g_smu); uint64_t n = own_alias_stats_locked().taken; pthread_mutex_unlock(&g_smu); return n; } // shared textures a takeover evicted, in total
|
||||
static uint8_t *pages(void) { uint8_t *m = mmap(NULL, LEN, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); if (m == MAP_FAILED) abort(); return m; }
|
||||
// The 2D float texture table at base 0, in a submission of its own, as a draw that reads it would ask
|
||||
static id<MTLBuffer> table(void) {
|
||||
@@ -128,9 +129,9 @@ int main(void) { @autoreleasepool {
|
||||
NVNtexture t5 = {0}; h_nvnTextureBuilderSetFormat(&tb, NVN_FORMAT_R32F); h_nvnTextureBuilderSetStorage(&tb, (NVNmemoryPool *)&dpool, 0);
|
||||
CHECK(h_nvnTextureInitialize(&t5, &tb), "texture 5 over the same storage initialized");
|
||||
mtl_reg_tex(5, (const tex_t *)&t5, NULL);
|
||||
builds = g_stat_pool_builds; uint64_t taken = g_alias_taken, ep = g_treg_ep;
|
||||
builds = g_stat_pool_builds; uint64_t taken = alias_taken(), ep = g_treg_ep;
|
||||
id<MTLBuffer> b10 = table();
|
||||
CHECK(g_alias_taken > taken, "texture 5 took the storage over");
|
||||
CHECK(alias_taken() > taken, "texture 5 took the storage over");
|
||||
CHECK(g_stat_pool_builds == builds && entry(b10, 5) != dummy_of(b10) && entry(b10, 4) == entry(b9, 4), "the takeover builds no table, and ID 4 keeps its entry in that submission");
|
||||
CHECK(g_treg_ep == ep, "nor sends every texture ID down the slow path");
|
||||
CHECK(!g_treg[4].view, "ID 4's view went at the submission's end");
|
||||
@@ -191,10 +192,10 @@ int main(void) { @autoreleasepool {
|
||||
h_nvnTextureBuilderSetFormat(&tb, NVN_FORMAT_R32F);
|
||||
CHECK(h_nvnTextureInitialize(&t9, &tb), "texture 9 over the same storage initialized");
|
||||
mtl_reg_tex(9, (const tex_t *)&t9, NULL);
|
||||
builds = g_stat_pool_builds; taken = g_alias_taken; ep = g_treg_ep;
|
||||
builds = g_stat_pool_builds; taken = alias_taken(); ep = g_treg_ep;
|
||||
id<MTLBuffer> b16 = table();
|
||||
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");
|
||||
pthread_mutex_lock(&g_smu); id<MTLTexture> now8 = (__bridge id)own_live_ref_locked(s8); int base_shared = own_shared_locked(base8), base_held = serial_table_holds((__bridge void *)base8); pthread_mutex_unlock(&g_smu);
|
||||
CHECK(base_held && 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");
|
||||
same_as_whole(b16, "the patched table holds what a table built whole does (a texture over a live twin's base)");
|
||||
@@ -209,10 +210,10 @@ int main(void) { @autoreleasepool {
|
||||
h_nvnTextureBuilderSetFormat(&tb, NVN_FORMAT_RG16F);
|
||||
CHECK(h_nvnTextureInitialize(&t10, &tb), "texture 10 over the same storage initialized");
|
||||
mtl_reg_tex(7, (const tex_t *)&t10, NULL);
|
||||
builds = g_stat_pool_builds; taken = g_alias_taken; ep = g_treg_ep;
|
||||
builds = g_stat_pool_builds; taken = alias_taken(); ep = g_treg_ep;
|
||||
id<MTLBuffer> b18 = table();
|
||||
pthread_mutex_lock(&g_smu); 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, "with the twin finalized, texture 10 took the storage over and evicted the base");
|
||||
pthread_mutex_lock(&g_smu); base_shared = own_shared_locked(base8); base_held = serial_table_holds((__bridge void *)base8); pthread_mutex_unlock(&g_smu);
|
||||
CHECK(!base_held && alias_taken() > taken && !base_shared, "with the twin finalized, texture 10 took the storage over and evicted the base");
|
||||
CHECK(g_stat_pool_builds == builds && g_treg_ep == ep && entry(b18, 8) == dummy_of(b18) && entry(b18, 7) != dummy_of(b18) && entry(b18, 9) != dummy_of(b18),
|
||||
"no table is built and no ID went down the slow path: the takeover's note names no ID");
|
||||
same_as_whole(b18, "the patched table holds what a table built whole does (a finalized twin's base taken over)");
|
||||
|
||||
+145
-25
@@ -1,12 +1,16 @@
|
||||
// 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:
|
||||
// The NVN resource owner (nvn_owner.m) on its own: linked without the replay layer, whose four hooks are recorded or set here instead (what is dropped and whether it stays resident,
|
||||
// which object's last count went, which serial changed, the submission being encoded). 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. Each ghost serial has one ring slot.
|
||||
// sRGB / linear views (tex_mtl_'s twins): the old sequence, publish then serial_del when the parent turned out gone, left a finalized serial resolving to the stale view (the evidence,
|
||||
// kept as a case); own_view_put publishes only while the parent is shared, and own_view_drop_stale takes a stale view's live or ghost entry (with its ring slot) only while it still
|
||||
// names the view, in each order of finalize, publish, the parent leaving and resolution
|
||||
// names the view, in each order of finalize, publish, the parent leaving and resolution.
|
||||
// The sharing table (own_alias_*): a lookup publishes in its own hold, a registration takes the storage over, goes in and publishes in one hold (and a second registration under the
|
||||
// key takes the first), the takeover spares entries a live entry or pin holds or a lookup found within ALIAS_IDLE submissions and takes the rest (a ghost entry keeps its texture
|
||||
// resolving), a pool drop takes that pool's entries, an upgrade renames the sharing table with the serial tables and publishes the asking serial's view in the same hold; a pool drop
|
||||
// or an upgrade between a twin's lookup and its publication refuses it; own_check_locked also checks the sharing table's index, set and last lookups
|
||||
#import <Metal/Metal.h>
|
||||
#include "../nvn/nvn_owner.h"
|
||||
#include <stdio.h>
|
||||
@@ -17,11 +21,11 @@ static int checks, failed;
|
||||
// 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;
|
||||
static uint64_t g_sub = 1; // the submission being encoded
|
||||
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]; }
|
||||
uint64_t own_hook_submission(void) { return g_sub; }
|
||||
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; }
|
||||
@@ -38,10 +42,24 @@ static void step(const char *label) {
|
||||
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
|
||||
// Sharing-table keys: a 4x4 texture of a format at an offset of a (fake) pool, padding zeroed
|
||||
static alias_key akey(uint64_t pool, int64_t off, NVNformat f) {
|
||||
alias_key k; memset(&k, 0, sizeof k); k.pool = pool; k.off = off;
|
||||
k.d = (nvn_texdesc){.target = NVN_TEXTURE_TARGET_2D, .format = f, .width = 4, .height = 4, .depth = 1, .levels = 1, .samples = 1}; return k;
|
||||
}
|
||||
static NSMapTable *g_keys; // object → the key share() put it under (each object its own 64 KB cell of pool 0x5000)
|
||||
static alias_key key_of(id o) {
|
||||
NSNumber *n = [g_keys objectForKey:o]; if (!n) { n = @(g_keys.count); [g_keys setObject:n forKey:o]; }
|
||||
return akey(0x5000, (int64_t)n.intValue << ALIAS_CELL, NVN_FORMAT_RGBA8);
|
||||
}
|
||||
static void share(id o, int on) { alias_key k = key_of(o); pthread_mutex_lock(&g_smu); if (on) own_alias_put_locked(&k, o, 0); else own_alias_del_locked(&k); pthread_mutex_unlock(&g_smu); } // as a registration / a pool drop would
|
||||
static int shared(id o) { pthread_mutex_lock(&g_smu); int r = own_shared_locked(o); pthread_mutex_unlock(&g_smu); return r; }
|
||||
static int has(const alias_key *k) { pthread_mutex_lock(&g_smu); int r = own_alias_get_locked(k) != nil; pthread_mutex_unlock(&g_smu); return r; }
|
||||
static own_alias_stats ast(void) { pthread_mutex_lock(&g_smu); own_alias_stats s = own_alias_stats_locked(); pthread_mutex_unlock(&g_smu); return s; }
|
||||
|
||||
int main(void) { @autoreleasepool {
|
||||
g_dropped = [NSMutableArray array]; g_dropped_keep = [NSMutableArray array]; g_unheld = [NSMutableArray array];
|
||||
g_shared = [NSHashTable hashTableWithOptions:NSPointerFunctionsObjectPointerPersonality];
|
||||
g_keys = [NSMapTable mapTableWithKeyOptions:NSPointerFunctionsObjectPointerPersonality valueOptions:NSPointerFunctionsStrongMemory];
|
||||
step("start");
|
||||
|
||||
{ // a live serial: registration, lookup, replacement, removal
|
||||
@@ -63,10 +81,11 @@ int main(void) { @autoreleasepool {
|
||||
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];
|
||||
id s = [NSObject new]; share(s, 1);
|
||||
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];
|
||||
share(s, 0);
|
||||
CHECK(dropped(s, 0) && !shared(s), "and is retired when the sharing table lets it go");
|
||||
step("shared object");
|
||||
}
|
||||
|
||||
@@ -186,15 +205,15 @@ int main(void) { @autoreleasepool {
|
||||
}
|
||||
}
|
||||
|
||||
{ // sRGB / linear views of a shared texture (tex_mtl_'s twins); g_shared stands for the sharing table, changed under g_smu as nvn_mtl.m changes it
|
||||
{ // sRGB / linear views of a shared texture (tex_mtl_'s twins); share() puts the parent in the sharing table and takes it out, as a registration and a pool drop do
|
||||
id<MTLDevice> dev = MTLCreateSystemDefaultDevice();
|
||||
MTLTextureDescriptor *d = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm width:4 height:4 mipmapped:NO];
|
||||
d.usage = MTLTextureUsageShaderRead | MTLTextureUsagePixelFormatView;
|
||||
id<MTLTexture> b = [dev newTextureWithDescriptor:d], b2 = [dev newTextureWithDescriptor:d];
|
||||
id<MTLTexture> (^view_of)(id<MTLTexture>) = ^(id<MTLTexture> p) { return [p newTextureViewWithPixelFormat:MTLPixelFormatRGBA8Unorm_sRGB]; };
|
||||
void (^share)(id, int) = ^(id o, int on) { pthread_mutex_lock(&g_smu); if (on) [g_shared addObject:o]; else [g_shared removeObject:o]; pthread_mutex_unlock(&g_smu); };
|
||||
CHECK(b && b2 && view_of(b), "two shared textures and a view of one");
|
||||
if (b && b2) {
|
||||
alias_key kb = key_of(b); // the parent's key
|
||||
{ // the evidence: the old sequence (publish, find the parent gone, serial_del) on a finalized serial leaves the stale view reachable, as serial_del ends a live entry only
|
||||
uint64_t k = 93061; id<MTLTexture> v = view_of(b);
|
||||
share(b, 1); tex_put(k, v); share(b, 0); // the parent leaves on another thread between the lookup and the check
|
||||
@@ -209,14 +228,14 @@ int main(void) { @autoreleasepool {
|
||||
uint64_t fk = 93062; tex_t lt = texture(93063); tex_live(<);
|
||||
id<MTLTexture> v = view_of(b);
|
||||
int notes = g_changed_n;
|
||||
CHECK(own_view_put(fk, v) == 0 && !tex_get(fk) && !holds(v) && !holds(b) && g_changed_n == notes, "a parent not shared: the view is not published, nothing is held or reported");
|
||||
CHECK(own_view_put(fk, &kb, v) == 0 && !tex_get(fk) && !holds(v) && !holds(b) && g_changed_n == notes, "a parent not shared: the view is not published, nothing is held or reported");
|
||||
id x = [NSObject new]; tex_put(fk, x);
|
||||
CHECK(own_view_put(fk, v) == 0 && tex_get(fk) == x, "and an entry the serial has stays as it was");
|
||||
CHECK(own_view_put(fk, &kb, v) == 0 && tex_get(fk) == x, "and an entry the serial has stays as it was");
|
||||
step("view refused");
|
||||
share(b, 1);
|
||||
CHECK(own_view_put(fk, v) == 1 && tex_get(fk) == v && ghost(fk) && holds(b) && !holds(x) && dropped(x, 0), "a shared parent: a finalized serial's view goes to the ghost ring in place of its old entry, holding its parent");
|
||||
CHECK(own_view_put(fk, &kb, v) == 1 && tex_get(fk) == v && ghost(fk) && holds(b) && !holds(x) && dropped(x, 0), "a shared parent: a finalized serial's view goes to the ghost ring in place of its old entry, holding its parent");
|
||||
id<MTLTexture> lv = view_of(b);
|
||||
CHECK(own_view_put(lt.serial, lv) == 1 && serial_get(lt.serial) == lv && live_holds(b), "a live texture object's view goes to the live table");
|
||||
CHECK(own_view_put(lt.serial, &kb, lv) == 1 && serial_get(lt.serial) == lv && live_holds(b), "a live texture object's view goes to the live table");
|
||||
step("view published");
|
||||
CHECK(own_view_drop_stale(fk, v) == 0 && own_view_drop_stale(lt.serial, lv) == 0 && tex_get(fk) == v && tex_get(lt.serial) == lv, "a view whose parent is shared is valid: its entries stay");
|
||||
share(b, 0); mtl_tex_pin(fk);
|
||||
@@ -242,30 +261,33 @@ int main(void) { @autoreleasepool {
|
||||
tex_t t = texture(93065); tex_live(&t);
|
||||
id<MTLTexture> v = view_of(b);
|
||||
share(b, 1);
|
||||
CHECK(own_view_put(t.serial, v) == 1 && serial_get(t.serial) == v, "published live");
|
||||
CHECK(own_view_put(t.serial, &kb, v) == 1 && serial_get(t.serial) == v, "published live");
|
||||
tex_end(&t);
|
||||
CHECK(!tex_get(t.serial) && !holds(v) && !holds(b), "finalized: the live entry goes (a finalize, not an invalidation)");
|
||||
CHECK(own_view_put(t.serial, v) == 1 && ghost(t.serial) && tex_get(t.serial) == v, "a replayed record publishes it again: ghost ring");
|
||||
CHECK(own_view_put(t.serial, &kb, v) == 1 && ghost(t.serial) && tex_get(t.serial) == v, "a replayed record publishes it again: ghost ring");
|
||||
share(b, 0);
|
||||
CHECK(tex_get(t.serial) == v, "the parent leaves after the publish: the entry still names the view until a resolution finds it stale");
|
||||
CHECK(own_view_drop_stale(t.serial, v) == 1 && !tex_get(t.serial) && !holds(b), "that resolution takes it");
|
||||
CHECK(own_view_put(t.serial, view_of(b)) == 0 && !tex_get(t.serial), "the parent gone before the publish: nothing is published");
|
||||
CHECK(own_view_put(t.serial, &kb, view_of(b)) == 0 && !tex_get(t.serial), "the parent gone before the publish: nothing is published");
|
||||
step("orders");
|
||||
// parent replacement (an upgrade): every name of the parent moves to the upgraded texture, which takes its place in the sharing table; a view of the old one is stale
|
||||
share(b, 1); tex_t u = texture(93066); tex_live(&u); id<MTLTexture> uv = view_of(b); uint64_t pn = (1ull << 62) + 93067;
|
||||
tex_put(pn, b); CHECK(own_view_put(u.serial, uv) == 1, "a view and its parent's own name");
|
||||
pthread_mutex_lock(&g_smu); own_rename_locked(b, b2, pn); [g_shared removeObject:b]; [g_shared addObject:b2]; pthread_mutex_unlock(&g_smu);
|
||||
tex_put(pn, b); CHECK(own_view_put(u.serial, &kb, uv) == 1, "a view and its parent's own name");
|
||||
pthread_mutex_lock(&g_smu); own_rename_locked(b, b2, pn); id under = own_alias_get_locked(&kb); pthread_mutex_unlock(&g_smu);
|
||||
CHECK(tex_get(pn) == b2 && serial_get(u.serial) == uv && live_holds(b), "after the upgrade the parent's name is the new texture; the view still holds the old one");
|
||||
CHECK(under == b2 && shared(b2) && !shared(b), "the rename moves the parent's place in the sharing table: its key names the new texture");
|
||||
step("parent upgraded");
|
||||
[g_unheld removeAllObjects];
|
||||
CHECK(own_view_drop_stale(u.serial, uv) == 1 && !serial_get(u.serial) && !holds(b) && unheld(b), "the view of the old parent is stale: it goes, and the old parent with it");
|
||||
CHECK(own_view_put(u.serial, view_of(b2)) == 1 && [(id<MTLTexture>)serial_get(u.serial) parentTexture] == b2, "made again on the upgraded parent");
|
||||
tex_end(&u); serial_del(pn); share(b2, 0);
|
||||
CHECK(own_view_put(u.serial, &kb, view_of(b)) == 0, "a view of the old parent made after the rename is refused (its key names the new texture)");
|
||||
CHECK(own_view_put(u.serial, &kb, view_of(b2)) == 1 && [(id<MTLTexture>)serial_get(u.serial) parentTexture] == b2, "made again on the upgraded parent");
|
||||
tex_end(&u); serial_del(pn); share(b, 0); // b's key, which names b2 now
|
||||
CHECK(!shared(b2) && !has(&kb), "the upgraded parent leaves with its key");
|
||||
step("parent upgraded, view made again");
|
||||
}
|
||||
{ // a ghost entry taken away leaves no ring slot: the serial put again keeps its entry for NGHOST - 1 newer serials
|
||||
uint64_t k = 93070; id<MTLTexture> v = view_of(b); id x = [NSObject new];
|
||||
share(b, 1); own_view_put(k, v); share(b, 0);
|
||||
share(b, 1); own_view_put(k, &kb, v); share(b, 0);
|
||||
own_view_drop_stale(k, v); tex_put(k, x);
|
||||
step("ring slot after a stale view");
|
||||
NSMutableArray *objs = [NSMutableArray array];
|
||||
@@ -278,17 +300,115 @@ int main(void) { @autoreleasepool {
|
||||
}
|
||||
}
|
||||
|
||||
{ // the sharing table: lookups, registration, takeover, pool drops, upgrades, and their interleavings with finalize and resolution
|
||||
enum { P = 0x7000, Q = 0x8000 };
|
||||
alias_key ka = akey(P, 0, NVN_FORMAT_RGBA8), kr = akey(P, 0, NVN_FORMAT_R32F), kg = akey(P, 0, NVN_FORMAT_RG16F), kq = akey(Q, 0, NVN_FORMAT_RGBA8);
|
||||
id a = [NSObject new], a2 = [NSObject new], r = [NSObject new];
|
||||
tex_t s1 = texture(97001), s2 = texture(97002); tex_live(&s1); tex_live(&s2);
|
||||
int notes = g_changed_n;
|
||||
CHECK(!own_alias_take(s1.serial, &ka) && !tex_get(s1.serial) && g_changed_n == notes, "a lookup of a key with no shared texture finds nothing and publishes nothing");
|
||||
CHECK(own_alias_publish(s1.serial, &ka, a) == a && serial_get(s1.serial) == a && shared(a) && live_holds(a), "a registration shares the texture and publishes it under the serial, in one hold");
|
||||
CHECK(own_alias_publish(s2.serial, &ka, a2) == a && serial_get(s2.serial) == a && !shared(a2) && !holds(a2), "a second registration under the key (another thread's) takes the first; the caller's own is shared nowhere");
|
||||
step("registration");
|
||||
tex_t s3 = texture(97003); tex_live(&s3);
|
||||
CHECK(own_alias_take(s3.serial, &ka) == a && serial_get(s3.serial) == a, "a lookup publishes the shared texture under the serial");
|
||||
tex_t f = texture(97004); // finalized: no live texture object
|
||||
CHECK(own_alias_take(f.serial, &ka) == a && ghost(f.serial) && tex_get(f.serial) == a, "a finalized serial's lookup goes to the ghost ring");
|
||||
step("lookups");
|
||||
// the takeover: another description over the bytes. a is held by live entries: spared whatever its last lookup
|
||||
g_sub += ALIAS_IDLE + 1;
|
||||
tex_t s4 = texture(97005); tex_live(&s4);
|
||||
CHECK(own_alias_publish(s4.serial, &kr, r) == r && has(&ka) && has(&kr), "the takeover spares an entry live serials hold");
|
||||
step("takeover spares held");
|
||||
tex_end(&s1); tex_end(&s2); tex_end(&s3);
|
||||
CHECK(!live_holds(a) && holds(a), "its live serials finalized: only the ghost entry holds it");
|
||||
id g = [NSObject new]; tex_t s5 = texture(97006), f2 = texture(97012); tex_live(&s5);
|
||||
CHECK(own_alias_take(f2.serial, &ka) == a && ghost(f2.serial), "a replayed record looks it up (a finalized serial: ghost ring)");
|
||||
pthread_mutex_lock(&g_smu); uint32_t took = own_alias_take_over_locked(&kg); pthread_mutex_unlock(&g_smu);
|
||||
CHECK(took == 0 && has(&ka), "an entry no live serial holds is still spared within ALIAS_IDLE submissions of its last lookup");
|
||||
g_sub += ALIAS_IDLE;
|
||||
uint64_t taken = ast().taken; notes = g_changed_n; [g_dropped removeAllObjects]; [g_dropped_keep removeAllObjects];
|
||||
CHECK(own_alias_publish(s5.serial, &kg, g) == g && !has(&ka) && !shared(a) && has(&kg), "past ALIAS_IDLE submissions an entry only a ghost entry holds is taken over");
|
||||
CHECK(dropped(a, 0) && tex_get(f.serial) == a && holds(a), "it is retired (out of the residency set unless held: nvn_mtl.m's retirement waits for the ghost entry), and the ghost entry still resolves it");
|
||||
CHECK(ast().taken == taken + 1 + !has(&kr) && g_changed_n > notes, "the takeover is counted and reported");
|
||||
CHECK(has(&kr), "the entry s4 holds live stays");
|
||||
step("takeover");
|
||||
// a lookup refreshes an entry's last lookup
|
||||
tex_end(&s4); g_sub += ALIAS_IDLE + 1;
|
||||
tex_t s6 = texture(97007); tex_live(&s6);
|
||||
CHECK(own_alias_take(s6.serial, &kr) == r, "a lookup of the idle entry");
|
||||
tex_end(&s6);
|
||||
id a3 = [NSObject new]; tex_t s7 = texture(97008); tex_live(&s7);
|
||||
CHECK(own_alias_publish(s7.serial, &ka, a3) == a3 && has(&kr), "the lookup's mark spares it from the next takeover");
|
||||
step("lookup marks");
|
||||
{ // replacement under a key retires the old texture: replaced in the map alone, it stayed in the set of shared textures and was released at once
|
||||
alias_key kx = akey(Q, 1 << 20, NVN_FORMAT_RGBA8); id x1 = [NSObject new], x2 = [NSObject new];
|
||||
pthread_mutex_lock(&g_smu); own_alias_put_locked(&kx, x1, g_sub); own_alias_put_locked(&kx, x2, g_sub); id now = own_alias_get_locked(&kx); pthread_mutex_unlock(&g_smu);
|
||||
CHECK(now == x2 && shared(x2) && !shared(x1) && dropped(x1, 0), "a texture put over a key's entry replaces it, and the old one is retired");
|
||||
step("replacement");
|
||||
pthread_mutex_lock(&g_smu); int del = own_alias_del_locked(&kx), again = own_alias_del_locked(&kx); pthread_mutex_unlock(&g_smu);
|
||||
CHECK(del && !again && !shared(x2), "removal: once");
|
||||
step("removal");
|
||||
}
|
||||
// a pool drop: that pool's entries go, another pool's stay; a lookup after it finds nothing, and the serial it had published keeps its texture (held)
|
||||
id q = [NSObject new]; tex_t s8 = texture(97009); tex_live(&s8);
|
||||
own_alias_publish(s8.serial, &kq, q);
|
||||
own_alias_stats before = ast();
|
||||
pthread_mutex_lock(&g_smu); uint32_t gone = own_alias_drop_pool_locked((const void *)(uintptr_t)P); pthread_mutex_unlock(&g_smu);
|
||||
own_alias_stats after = ast();
|
||||
CHECK(gone == before.n - 1 && after.n == 1 && has(&kq) && after.evicted == before.evicted + gone, "a pool drop takes the pool's entries, counted, and leaves another pool's");
|
||||
CHECK(!own_alias_take(s7.serial + 100, &ka) && serial_get(s7.serial) == a3 && live_holds(a3) && !shared(a3), "after it a lookup finds nothing; a serial keeps the texture it had, no longer shared");
|
||||
step("pool drop");
|
||||
// a twin's lookup (peek), then its parent's pool goes, then the publication: refused
|
||||
id<MTLDevice> dev = MTLCreateSystemDefaultDevice();
|
||||
MTLTextureDescriptor *d = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm width:4 height:4 mipmapped:NO];
|
||||
d.usage = MTLTextureUsageShaderRead | MTLTextureUsagePixelFormatView;
|
||||
id<MTLTexture> pt = [dev newTextureWithDescriptor:d], up = [dev newTextureWithDescriptor:d];
|
||||
if (pt && up) {
|
||||
alias_key kt = akey(P, 1 << 20, NVN_FORMAT_RGBA8); tex_t base = texture(97010), twin = texture(97011); tex_live(&base); tex_live(&twin);
|
||||
own_alias_publish(base.serial, &kt, pt);
|
||||
id<MTLTexture> b = own_alias_peek(&kt), v = [b newTextureViewWithPixelFormat:MTLPixelFormatRGBA8Unorm_sRGB];
|
||||
pthread_mutex_lock(&g_smu); own_alias_drop_pool_locked((const void *)(uintptr_t)P); pthread_mutex_unlock(&g_smu);
|
||||
CHECK(b == pt && own_view_put(twin.serial, &kt, v) == 0 && !tex_get(twin.serial), "a twin whose parent's pool went between its lookup and its publication is not published");
|
||||
own_alias_publish(base.serial, &kt, pt);
|
||||
v = [pt newTextureViewWithPixelFormat:MTLPixelFormatRGBA8Unorm_sRGB];
|
||||
pthread_mutex_lock(&g_smu); own_rename_locked(pt, up, base.serial); pthread_mutex_unlock(&g_smu);
|
||||
CHECK(own_view_put(twin.serial, &kt, v) == 0, "nor one whose parent was upgraded in between");
|
||||
pthread_mutex_lock(&g_smu); own_rename_locked(up, pt, base.serial); pthread_mutex_unlock(&g_smu);
|
||||
CHECK(own_view_put(twin.serial, &kt, v) == 1 && serial_get(twin.serial) == v && live_holds(pt), "a twin of the shared parent is published");
|
||||
step("twin interleavings");
|
||||
// own_upgrade: the rename and the twin's view made again, in one hold
|
||||
id<MTLTexture> nv = [up newTextureViewWithPixelFormat:MTLPixelFormatRGBA8Unorm_sRGB];
|
||||
CHECK(own_upgrade(pt, up, twin.serial, nv) == 0 && serial_get(twin.serial) == nv && serial_get(base.serial) == up && shared(up) && !shared(pt) && !holds(pt),
|
||||
"an upgrade through the twin: every name of the parent moves, the twin's serial gets its new view, the old parent is held by nothing");
|
||||
CHECK(own_view_drop_stale(twin.serial, nv) == 0, "and the new view is valid");
|
||||
id<MTLTexture> up2 = [dev newTextureWithDescriptor:d];
|
||||
CHECK(own_upgrade(up, up2, base.serial, up2) == 1 && serial_get(base.serial) == up2 && shared(up2), "an upgrade through the parent's own serial: renamed, nothing more to publish");
|
||||
CHECK(own_view_drop_stale(twin.serial, nv) == 1 && !serial_get(twin.serial), "the twin's view of the replaced parent is stale");
|
||||
step("upgrade");
|
||||
tex_end(&base); tex_end(&twin);
|
||||
pthread_mutex_lock(&g_smu); own_alias_drop_pool_locked((const void *)(uintptr_t)P); pthread_mutex_unlock(&g_smu);
|
||||
}
|
||||
pthread_mutex_lock(&g_smu); own_alias_drop_pool_locked((const void *)(uintptr_t)Q); pthread_mutex_unlock(&g_smu);
|
||||
tex_end(&s5); tex_end(&s7); tex_end(&s8);
|
||||
pthread_mutex_lock(&g_smu); int keeps = own_keeps_locked(a3); pthread_mutex_unlock(&g_smu);
|
||||
CHECK(!keeps && !has(&kq) && !has(&kg), "the pools gone, nothing is shared");
|
||||
step("sharing table torn down");
|
||||
}
|
||||
|
||||
{ // teardown: every ghost serial evicted by NGHOST new ones, then those taken away as stale views; nothing is held, every count is gone
|
||||
id<MTLDevice> dev = MTLCreateSystemDefaultDevice();
|
||||
id<MTLTexture> p = [dev newTextureWithDescriptor:[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm width:4 height:4 mipmapped:NO]];
|
||||
NSMutableArray *views = [NSMutableArray array];
|
||||
if (p) {
|
||||
pthread_mutex_lock(&g_smu); [g_shared addObject:p]; pthread_mutex_unlock(&g_smu);
|
||||
for (int i = 0; i < NGHOST; i++) { id<MTLTexture> v = [p newTextureViewWithPixelFormat:MTLPixelFormatRGBA8Unorm]; [views addObject:v]; own_view_put(960000 + (uint64_t)i, v); }
|
||||
pthread_mutex_lock(&g_smu); [g_shared removeObject:p]; pthread_mutex_unlock(&g_smu);
|
||||
share(p, 1); alias_key kp = key_of(p);
|
||||
for (int i = 0; i < NGHOST; i++) { id<MTLTexture> v = [p newTextureViewWithPixelFormat:MTLPixelFormatRGBA8Unorm]; [views addObject:v]; own_view_put(960000 + (uint64_t)i, &kp, v); }
|
||||
share(p, 0);
|
||||
for (int i = 0; i < NGHOST; i++) own_view_drop_stale(960000 + (uint64_t)i, views[i]);
|
||||
pthread_mutex_lock(&g_smu); own_stats st = own_stats_locked(); pthread_mutex_unlock(&g_smu);
|
||||
own_alias_stats as = ast();
|
||||
CHECK(st.ghost == 0 && st.live == 0 && !holds(p), "after teardown no serial resolves and nothing is held");
|
||||
CHECK(as.n == 0 && as.cells == 0 && !shared(p), "and the sharing table and its index are empty");
|
||||
step("teardown");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ int main(void) { @autoreleasepool {
|
||||
char why[256] = ""; pthread_mutex_lock(&g_smu); int ok = own_check_locked(why, sizeof why); pthread_mutex_unlock(&g_smu);
|
||||
CHECK(ok, why);
|
||||
alias_key ak = tex_alias_key(&l2);
|
||||
pthread_mutex_lock(&g_smu); int gone = alias_del(&ak); pthread_mutex_unlock(&g_smu); // as a pool's finalize evicts it
|
||||
pthread_mutex_lock(&g_smu); int gone = own_alias_del_locked(&ak); pthread_mutex_unlock(&g_smu); // as a pool's finalize evicts it
|
||||
CHECK(gone && tex_get(s2.serial) == gv, "the parent leaves the sharing table; the ghost entry still names the view");
|
||||
id<MTLTexture> own = tex_mtl(&s2);
|
||||
pthread_mutex_lock(&g_smu); int held = entry_holds((__bridge void *)gv); pthread_mutex_unlock(&g_smu);
|
||||
|
||||
@@ -37,7 +37,8 @@ int main(void) { @autoreleasepool {
|
||||
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");
|
||||
CHECK(!alias_has(old) && alias_has(upgraded), "sharing membership follows the replacement");
|
||||
pthread_mutex_lock(&g_smu); int moved = !own_shared_locked(old) && own_shared_locked(upgraded); pthread_mutex_unlock(&g_smu);
|
||||
CHECK(moved, "sharing membership follows the replacement");
|
||||
uint32_t pixels[16]; for (int i = 0; i < 16; i++) pixels[i] = 0x44332211u;
|
||||
id<MTLBuffer> src = [g_dev newBufferWithBytes:pixels length:sizeof pixels options:MTLResourceStorageModeShared]; res_add(src);
|
||||
id<MTL4ComputeCommandEncoder> encoder = blit();
|
||||
|
||||
@@ -589,6 +589,7 @@ One line per symptom in the [subsystem references](../reference/): what you see
|
||||
- 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 could resolve to an sRGB / linear view of storage that no longer held its bytes. → [A finalized twin view left in the ghost ring](../reference/nvn-objects.md#a-finalized-twin-view-left-in-the-ghost-ring)
|
||||
- none seen on a game. Windows in which a texture serial could resolve to a texture the sharing table had already let go, or two serials over one storage to two textures. → [Sharing-table lookups and registrations in separate holds](../reference/nvn-objects.md#sharing-table-lookups-and-registrations-in-separate-holds)
|
||||
- `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)
|
||||
|
||||
@@ -235,9 +235,17 @@ A game may tell NVN objects apart by their bytes, without an NVN call. A host re
|
||||
|
||||
- **Symptom:** none seen on a game. A finalized texture could resolve to an sRGB / linear view of storage that no longer held its bytes.
|
||||
- **Cause:** two paths of `tex_mtl_` published or kept such a view and took it back with `serial_del`, which ends a live entry only (what a finalize wants: a replayed record may still resolve the finalized serial through the ghost ring). The twin path published the view first (`tex_put`, which sends a finalized serial to the ghost ring) and checked its parent's place in the sharing table afterwards, so a parent evicted on another thread in between (a pool's finalize on the game's thread) left the view published until the check, and for a finalized serial in the ghost ring after it. The stale-view path (a view whose parent left the sharing table since) dropped it the same way. Each time the serial's next `tex_put` replaced the ghost entry a few lines later, unless making the texture's own one failed (`tex_mtl_` returns nil); and any thread resolving the serial in between got the stale view. Reproduced by the old sequence in `t_serial_owner` (synthetic, at the table level: the view resolves between the publish and the check, and after `serial_del`); not seen on a game.
|
||||
- **Fix:** a view's validity is decided in the same hold of `g_smu` as its entry (`nvn_owner.m`). `own_view_put` publishes a view only while its parent is in the sharing table, so the twin path never publishes one whose parent has gone and has nothing to take back. `own_view_drop_stale` is the invalidation, distinct from a finalize: a view whose parent is neither shared nor pinned (and that is not pinned itself) loses its live entry, or with none its ghost entry and that entry's ring slot, and only while the entry still names that view (a replacement made meanwhile stays). `serial_del` is unchanged. Each ghost serial now has exactly one ring slot (`own_check_locked` checks it): a slot left behind by a removed entry would evict the serial's later entry early. `t_serial_owner` covers publication against a shared and an evicted parent, pinned views and parents, a replaced entry, a live entry beside a ghost one, each order of finalize, publish, eviction and resolution, a parent's upgrade, the ring after a removal, and teardown; `t_srgb_alias` runs a finalized twin whose parent is evicted through the real `tex_mtl_`.
|
||||
- **Fix:** a view's validity is decided in the same hold of `g_smu` as its entry (`nvn_owner.m`). `own_view_put` publishes a view only while its parent is still the shared texture of the parent's key (and marks that key looked up in the same hold), so the twin path never publishes one whose parent has gone or been upgraded and has nothing to take back. `own_view_drop_stale` is the invalidation, distinct from a finalize: a view whose parent is neither shared nor pinned (and that is not pinned itself) loses its live entry, or with none its ghost entry and that entry's ring slot, and only while the entry still names that view (a replacement made meanwhile stays). `serial_del` is unchanged. Each ghost serial now has exactly one ring slot (`own_check_locked` checks it): a slot left behind by a removed entry would evict the serial's later entry early. `t_serial_owner` covers publication against a shared and an evicted parent, pinned views and parents, a replaced entry, a live entry beside a ghost one, each order of finalize, publish, eviction and resolution, a parent's upgrade, the ring after a removal, and teardown; `t_srgb_alias` runs a finalized twin whose parent is evicted through the real `tex_mtl_`.
|
||||
- **Found on:** no game; found in a review of the serial tables.
|
||||
|
||||
### Sharing-table lookups and registrations in separate holds
|
||||
|
||||
- **Symptom:** none seen on a game. Windows in which a texture serial could resolve to a texture the sharing table had already let go, or two serials over one storage to two textures.
|
||||
- **Cause:** `tex_mtl_`, which also runs off the submit thread (it checks `on_sub_thread`), changed the sharing table and the serial tables in separate holds of `g_smu`. A lookup let go of the lock before its `tex_put`, so a pool's finalize in between (the game's thread) published an evicted, retired texture under the serial. A new texture's takeover and entry were made in one hold and its `tex_put` in another; a second texture registered under a key already present replaced the entry in the map alone, so the first stayed in the set of shared textures (kept resident as shared, never retired) and its serials kept a texture no longer shared with the storage's other serials. An upgrade renamed the tables in one hold and published the twin's view made again in another, so a reader in between found the old view of a parent no longer shared (which the stale-view check then dropped).
|
||||
- **Fix:** the sharing table moved into the resource owner (`nvn_owner.m`), and each change it makes with a serial's publication is one operation under one hold: `own_alias_take` (lookup, last-lookup mark, publication), `own_alias_publish` (takeover, entry, publication; a key registered meanwhile by another thread is taken and the caller retires its own), `own_view_put` (the parent still the key's texture), `own_upgrade` (the rename of the serial tables, pins and sharing table, then the asking serial's object). A replaced entry is retired with its set membership (`alias_put`). `own_check_locked` checks the sharing table's index, set and last lookups against it. Matching (the key, `ALIAS_IDLE`, which entries a takeover spares) is unchanged.
|
||||
- **Check:** `t_serial_owner` (owner alone): lookups and registrations against a shared and an absent key, a second registration under one key, a takeover sparing held and lately looked-up entries and taking a ghost-held one, a pool drop, a twin refused after its parent's pool drop or upgrade between lookup and publication, `own_upgrade` through a twin and through the parent; seven deliberate breaks of the owner each fail it. `t_alias_takeover`, `t_pool_notes`, `t_srgb_alias` and `t_upgrade_alias` through the real `tex_mtl_`. Not checked: two threads at once (the cases replay each interleaving in order).
|
||||
- **Found on:** no game; found while moving the sharing table into the owner.
|
||||
|
||||
### A replay's own objects take serials above a capture's
|
||||
|
||||
- **Symptom:** `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.
|
||||
|
||||
Reference in new issue
Block a user