mirror of
https://github.com/seedds/nxnative.git
synced 2026-11-23 11:39:42 -10:00
A stale sRGB / linear twin view no longer stays reachable in the NVN serial tables: the owner publishes a view only while its parent is shared (own_view_put, the check and the publish under one hold of g_smu) and invalidates a stale view wherever it is (own_view_drop_stale: the live entry, or else the ghost entry and its ring slot, each only while it still names that view). tex_mtl_'s twin path published the view with tex_put, then checked the parent in a second hold and took a miss back with serial_del; a reader between the two (a pool's finalize evicting the parent on the guest thread) got the stale view, and its stale path dropped a stale view with serial_del too. serial_del is a finalize, which removes the live entry and keeps the ghost one for replayed records, so a finalized texture's stale view stayed in the ghost ring until the next tex_put replaced it, and for good if making the texture's own failed. Finalization (serial_del, unchanged) and invalidation (own_view_drop_stale) are now separate operations: no valid ghost entry is flushed and no serial deletion became stronger. A view stays valid while it or its parent is pinned or shared (g_alias_set membership, which equals the old key check since a shared texture is under one key). Each ghost serial keeps exactly one ring slot (a leftover slot would evict a later entry of the serial early); own_check_locked checks it. The twin path's seen mark takes its own hold after own_view_put. Synthetic only: t_serial_owner replays the old sequence (the view reachable in the window, and in the ghost ring after serial_del) and covers publish refused / done, pinned view and parent, stale entries replaced in the meantime, every order of finalize, publish, eviction and resolution, a parent upgrade, the ring slot after a stale drop, teardown counts (102 checks; a kept ring slot, a live-only drop and a missing parent check each fail it); t_srgb_alias gains a finalized twin whose parent leaves the sharing table (integration; the old code passes it too). No game has shown the defect; the reference entry in nvn-objects.md says so, symptom index regenerated, dm/gfx/README.md lists the owner's view operations and what step 13 keeps. Checked: make -C dm/gfx/tests run, ASan/UBSan on t_serial_owner, TSan on t_srgb_alias, thermal-tsan, lint_c.py tidy (nothing above the baseline), test_docs; regress.py check against 1ee02525 (one run per side, headless, M6): Bastion 59.6 / 59.6 fps, Obra Dinn 59.8 / 59.8 fps, CHANGED on screenshots by animation phase only, no new red line; neither reaches the twin path. An iOS IPA of Bastion builds. No chain input changes. Not run: a game that reaches the twin path (Mario Kart World's Grand Prix movie), the iPhone
This commit is contained in:
1 parent
1ee02525ec
commit
2ba06d7656
8 files changed
+196
-34
No files matched your search
+2
-2
@@ -24,7 +24,7 @@ For the build steps of the whole project see [README.md](../../README.md) and [d
|
||||
- The game's packaged textures are in Tegra block-linear layout and are deswizzled before upload (see `tex/README.md`).
|
||||
- Presentation: the game's swapchain texture is drawn to a `CAMetalLayer`, and presentation timing is scheduled by the pacer in `nvn/nvn_pace.h`.
|
||||
|
||||
- **The serial tables' owner `nvn/nvn_owner.m`** (`nvn_owner.h`): what a texture or program serial resolves to and who holds each object. It owns the live table (`serial_put`, `serial_del`), the ghost ring of finalized texture serials (`tex_put`, `NGHOST`), the live texture objects (`mtl_tex_init`'s part, `own_tex_end_locked` for a finalize), texture pins (`mtl_tex_pin`) and program pins (`own_prog_pin`, `mtl_prog_release`, `mtl_prog_unpin`), the held counts (`serial_table_holds`, `entry_holds`), and renaming an object everywhere (`own_rename_locked`, the upgrade path). It makes, encodes and releases nothing: what it lets go goes to the replay layer through four hooks (`own_hook_drop`: deferred release after the submission in flight; `own_hook_shared`: the sharing table; `own_hook_unheld`: retirement; `own_hook_changed`: `ser_note`). Rules the next moves keep: a lookup is borrowed and stays valid until the submission it is encoded in completes; every change of a serial's object is reported; the held counts match the tables and pins (`own_check_locked`); the lock order is `g_smu`, then `g_cpu_tex_mu`. Still in `nvn_mtl.m` under the same `g_smu`: the sharing table (`g_alias`, `alias_take_over`, which asks `serial_table_holds`), retirement (`defer_drain`, `g_retire_held`, which asks `entry_holds`), the twin path of `tex_mtl_`, and the change generation (`g_ser_gen`, `ser_note`). Open: `serial_del` leaves a finalized serial's ghost entry ([docs/reference/nvn-objects.md](../../docs/reference/nvn-objects.md#a-finalized-twin-view-left-in-the-ghost-ring)).
|
||||
- **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)).
|
||||
|
||||
For which Metal object each NVN object and command lands on, see [nvn_metal_mapping.md](nvn_metal_mapping.md).
|
||||
|
||||
@@ -140,7 +140,7 @@ The tests in `dm/gfx/tests` compile the production code in directly and need a M
|
||||
| `t_deswz_gpu` | GPU deswizzling |
|
||||
| `t_present_crop` | Presentation cropping |
|
||||
| `t_mip_extent` | Effective size when rendering to a mip level other than level 0 |
|
||||
| `t_serial_owner` | The serial tables' owner alone (`nvn_owner.m` with stand-ins for the replay layer's hooks): registration, replacement, removal, finalize, the ghost ring, pins, views and renames, the held counts after every step; `DM_TEST_SERIAL_TWIN=1` asserts the open finalized-twin case |
|
||||
| `t_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_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` |
|
||||
|
||||
|
||||
+10
-10
@@ -4552,12 +4552,12 @@ static id<MTLTexture> tex_mtl_(const tex_t *ct) {
|
||||
if (!pk->sparse) pkg_share_put(t, pk);
|
||||
return pk->view ? pk->view : pk->tex;
|
||||
}
|
||||
int stale = 0;
|
||||
if (have && [have conformsToProtocol:@protocol(MTLTexture)] && ((id<MTLTexture>)have).parentTexture) { pthread_mutex_lock(&g_smu); stale = !alias_has(have) && !alias_has(((id<MTLTexture>)have).parentTexture); pthread_mutex_unlock(&g_smu); }
|
||||
if (stale) {
|
||||
// An sRGB / linear view whose parent left the sharing table (upgraded, or evicted with its pool; a takeover leaves a held view's parent) no longer shares the storage's bytes: dropped, and made again below on what holds them now
|
||||
// An sRGB / linear view whose parent left the sharing table (upgraded, or evicted with its pool; a takeover leaves a held view's parent) no longer shares the storage's bytes: dropped,
|
||||
// live or finalized (its ghost entry), and made again below on what holds them now. serial_del took only a live entry, so a finalized serial kept resolving to the stale view
|
||||
// whenever making the new texture failed, and to any thread in between
|
||||
if (have && [have conformsToProtocol:@protocol(MTLTexture)] && ((id<MTLTexture>)have).parentTexture && own_view_drop_stale(t->serial, have)) {
|
||||
static int nstale; if (nstale++ < 4) MLOG("stale sRGB / linear view dropped ser=%llu format %lu (%d so far)", (unsigned long long)t->serial, (unsigned long)((id<MTLTexture>)have).pixelFormat, nstale);
|
||||
serial_del(t->serial); have = nil;
|
||||
have = nil;
|
||||
}
|
||||
if (have) { if (t->pool && nvn_sparse_pool(t->pool)) sparse_gpu_look(t, have); return have; }
|
||||
if (t->d.target == 10) { // Buffer texture (TARGET_BUFFER): create the texture-buffer view directly on the memory pool buffer (zero copy; the GPU reads the pool's current content at execution time, same as NVN semantics)
|
||||
@@ -4601,11 +4601,11 @@ static id<MTLTexture> tex_mtl_(const tex_t *ct) {
|
||||
if (b.pixelFormat == d.pixelFormat || tex_need_pfv(b.pixelFormat, d.pixelFormat) || b.textureType != d.textureType) continue;
|
||||
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. One on another thread since the lookup above found b held by nothing:
|
||||
// then the view is of a texture that no longer shares the storage's bytes, and it is dropped for the next format of the pair or a texture of the twin's own
|
||||
tex_put(t->serial, v);
|
||||
pthread_mutex_lock(&g_smu); int kept = hm_get_id(&g_alias, &k) == b; if (kept) nvn_hm_put(&g_alias_seen, &k, (void *)(uintptr_t)(g_fence + 1)); pthread_mutex_unlock(&g_smu);
|
||||
if (!kept) { serial_del(t->serial); v = nil; }
|
||||
// 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);
|
||||
}
|
||||
if (v) return v;
|
||||
}
|
||||
|
||||
+10
-3
@@ -3,7 +3,8 @@
|
||||
//
|
||||
// Tables (all under g_smu but the program pins):
|
||||
// live serial → object (MTLTexture, NvPkg, NvProg) of a live serial: serial_put / serial_del, and tex_put for a texture whose object is live or an external texture (2^62 and up)
|
||||
// ghost serial → object of a finalized texture serial a replayed record still resolves (tex_put), a ring of NGHOST: the oldest goes when a new serial comes
|
||||
// ghost serial → object of a finalized texture serial a replayed record still resolves (tex_put), a ring of NGHOST: the oldest goes when a new serial comes; a stale view's
|
||||
// entry goes with its slot (own_view_drop_stale). Each ghost serial has one ring slot
|
||||
// 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)
|
||||
@@ -14,13 +15,14 @@
|
||||
// flight has completed (defer_obj_release / defer_obj_unref). So an object looked up while encoding stays valid at least until the submission it is encoded in completes. A caller that
|
||||
// keeps one longer retains it (a strong reference). Every change of a serial's object is reported (own_hook_changed: ser_note, which bumps g_ser_gen for the replay layer's caches).
|
||||
//
|
||||
// Lock order: g_smu, then the replay layer's g_cpu_tex_mu. Until the sharing table moves here, nvn_mtl.m's sharing table, the upgrade path and the twin check use g_smu as well.
|
||||
// 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.
|
||||
#pragma once
|
||||
#import <Foundation/Foundation.h>
|
||||
#include <pthread.h>
|
||||
#include <stdint.h>
|
||||
#include "nvn_hmap.h"
|
||||
#include "nvn_obj.h"
|
||||
@protocol MTLTexture;
|
||||
|
||||
// Tables from serial (or key) to a retained object
|
||||
#ifdef __clang_analyzer__
|
||||
@@ -48,9 +50,14 @@ static inline id tex_get(uint64_t k) { return (__bridge id)own_resolve_ref(k); }
|
||||
|
||||
// Changes (each takes g_smu)
|
||||
void serial_put(uint64_t k, id o); // a live serial's object; the one it replaces is dropped
|
||||
void serial_del(uint64_t k); // a live serial's entry goes; a ghost entry of the serial is left (see docs/reference/nvn-objects.md, A finalized twin view left in the ghost ring)
|
||||
void 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)
|
||||
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
|
||||
|
||||
// Live texture objects. Caller holds g_smu
|
||||
void own_tex_live_put_locked(const tex_t *t); // mtl_tex_init
|
||||
int own_tex_live_locked(uint64_t serial);
|
||||
|
||||
+38
-4
@@ -52,7 +52,8 @@ static void obj_drop(id o) { own_hook_drop(o, tex_object_pinned(o) || own_hook_s
|
||||
const void *own_live_ref_locked(uint64_t k) { return nvn_hm_get(&g_by_serial, &k); }
|
||||
const void *own_live_ref(uint64_t k) { pthread_mutex_lock(&g_smu); const void *o = nvn_hm_get(&g_by_serial, &k); pthread_mutex_unlock(&g_smu); return o; }
|
||||
void serial_put(uint64_t k, id o) { pthread_mutex_lock(&g_smu); id old = hm_get_id(&g_by_serial, &k); if (old) { held_add((__bridge void *)old, -1); obj_drop(old); } hm_put_id(&g_by_serial, &k, o); held_add((__bridge void *)o, 1); own_hook_changed(k); pthread_mutex_unlock(&g_smu); }
|
||||
void serial_del(uint64_t k) { pthread_mutex_lock(&g_smu); id old = hm_get_id(&g_by_serial, &k); if (old) { held_add((__bridge void *)old, -1); obj_drop(old); hm_del_id(&g_by_serial, &k); own_hook_changed(k); } pthread_mutex_unlock(&g_smu); } // nothing to drop: the texture
|
||||
static void live_del_locked(uint64_t k) { id old = hm_get_id(&g_by_serial, &k); if (old) { held_add((__bridge void *)old, -1); obj_drop(old); hm_del_id(&g_by_serial, &k); own_hook_changed(k); } } // nothing to drop: the texture
|
||||
void serial_del(uint64_t k) { pthread_mutex_lock(&g_smu); live_del_locked(k); pthread_mutex_unlock(&g_smu); }
|
||||
|
||||
void own_tex_live_put_locked(const tex_t *t) { nvn_hm_put(&g_tex_live, &t->serial, (void *)t); }
|
||||
int own_tex_live_locked(uint64_t serial) { return nvn_hm_get(&g_tex_live, &serial) != NULL; }
|
||||
@@ -98,8 +99,7 @@ void mtl_tex_unpin(uint64_t serial) {
|
||||
const void *own_resolve_ref(uint64_t k) {
|
||||
pthread_mutex_lock(&g_smu); const void *o = nvn_hm_get(&g_by_serial, &k); if (!o && g_ghost.n) o = nvn_hm_get(&g_ghost, &k); if (!o) { tex_pin *pin = nvn_hm_get(&g_tex_pins, &k); if (pin) o = pin->object; } pthread_mutex_unlock(&g_smu); return o;
|
||||
}
|
||||
void tex_put(uint64_t k, id o) { // serials from 2^62 up are external textures (mtl_ext_tex), never initialized by the game
|
||||
pthread_mutex_lock(&g_smu);
|
||||
static void tex_put_locked(uint64_t k, id o) { // serials from 2^62 up are external textures (mtl_ext_tex), never initialized by the game
|
||||
nvn_hmap *m = k >= (1ull << 62) || nvn_hm_get(&g_tex_live, &k) ? &g_by_serial : &g_ghost;
|
||||
id old = hm_get_id(m, &k);
|
||||
if (old) count_add(m == &g_by_serial ? &g_by_serial_held : &g_ghost_held, (__bridge void *)old, -1);
|
||||
@@ -111,7 +111,33 @@ void tex_put(uint64_t k, id o) { // serials from 2^62 up are external textures
|
||||
}
|
||||
hm_put_id(m, &k, o); tex_pin *pin = nvn_hm_get(&g_tex_pins, &k); if (pin) tex_pin_set(pin, o); own_hook_changed(k);
|
||||
count_add(m == &g_by_serial ? &g_by_serial_held : &g_ghost_held, (__bridge void *)o, 1);
|
||||
pthread_mutex_unlock(&g_smu);
|
||||
}
|
||||
void tex_put(uint64_t k, id o) { pthread_mutex_lock(&g_smu); tex_put_locked(k, o); pthread_mutex_unlock(&g_smu); }
|
||||
|
||||
// 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;
|
||||
}
|
||||
// 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) {
|
||||
id old = hm_get_id(&g_ghost, &k); if (!old) return;
|
||||
for (unsigned i = 0; i < NGHOST; i++) if (g_ghost_ring[i] == k) { g_ghost_ring[i] = 0; break; }
|
||||
ghost_held_add((__bridge void *)old, -1); obj_drop(old); hm_del_id(&g_ghost, &k); own_hook_changed(k);
|
||||
}
|
||||
int own_view_drop_stale(uint64_t k, id view) {
|
||||
pthread_mutex_lock(&g_smu);
|
||||
id p = view_parent(view); int stale = p && !view_kept(view) && !view_kept(p);
|
||||
if (stale) {
|
||||
id live = hm_get_id(&g_by_serial, &k);
|
||||
if (live == view) live_del_locked(k);
|
||||
else if (!live && hm_get_id(&g_ghost, &k) == view) ghost_del_locked(k); // an entry another thread changed meanwhile is not this view's: it stays
|
||||
}
|
||||
pthread_mutex_unlock(&g_smu); return stale;
|
||||
}
|
||||
|
||||
int own_rename_locked(id from, id to, uint64_t serial) {
|
||||
@@ -175,6 +201,14 @@ int own_check_locked(char *why, size_t n) {
|
||||
for (uint32_t i = 0; i < g_ghost.cap; i++) if (g_ghost.vals[i]) expect_one(ghost, (__bridge id)g_ghost.vals[i]);
|
||||
int ok = counts_match("live", live, &g_by_serial_held, why, n) && counts_match("ghost", ghost, &g_ghost_held, why, n);
|
||||
if (ok && g_ghost.n > NGHOST) { snprintf(why, n, "%u ghost serials, more than the ring's %d", g_ghost.n, NGHOST); ok = 0; }
|
||||
uint32_t slots = 0; // each ghost serial has one ring slot, and each slot a ghost serial: a second slot would evict the serial's entry before its time
|
||||
for (unsigned i = 0; ok && i < NGHOST; i++) {
|
||||
if (!g_ghost_ring[i]) continue;
|
||||
slots++;
|
||||
if (!nvn_hm_get(&g_ghost, &g_ghost_ring[i])) { snprintf(why, n, "ring slot %u names serial %llu, which has no ghost entry", i, (unsigned long long)g_ghost_ring[i]); ok = 0; }
|
||||
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; }
|
||||
CFRelease(live); CFRelease(ghost);
|
||||
return ok;
|
||||
}
|
||||
+109
-11
@@ -3,9 +3,10 @@
|
||||
// a live serial's registration, replacement and removal; where tex_put sends a serial (live table for a live texture object or an external texture from 2^62 up, ghost ring for a
|
||||
// finalized one); a finalize, and a copy's finalize that ends nothing; ghost replacement in place and eviction at NGHOST; texture pins and program pins; a view's entry counting its
|
||||
// parent; renaming an object everywhere; and after every step the held counts against the tables (own_check_locked). The owner releases nothing itself: everything it lets go goes
|
||||
// to own_hook_drop, which nvn_mtl.m releases once the submission in flight completes, so what a lookup returned outlives the change that dropped it.
|
||||
// DM_TEST_SERIAL_TWIN=1 asserts what serial_del should do for a finalized texture serial, which it does not yet (the twin path of tex_mtl_: tex_put sends a finalized serial's view to
|
||||
// the ghost ring, and serial_del, called when the view has to go, removes only a live entry). Without it the run prints what it found and counts no failure for it
|
||||
// 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
|
||||
#import <Metal/Metal.h>
|
||||
#include "../nvn/nvn_owner.h"
|
||||
#include <stdio.h>
|
||||
@@ -185,14 +186,111 @@ int main(void) { @autoreleasepool {
|
||||
}
|
||||
}
|
||||
|
||||
{ // the finalized twin: a finalized serial's view is put in the ghost ring, then has to go, and serial_del removes only a live entry
|
||||
uint64_t k = 93061; id v = [NSObject new];
|
||||
tex_put(k, v); // as tex_mtl_ does for a twin view of a texture whose object is finalized
|
||||
serial_del(k); // as it does when the shared texture left the sharing table meanwhile (kept == 0)
|
||||
int stays = tex_get(k) == v && ghost(k) && holds(v);
|
||||
if (getenv("DM_TEST_SERIAL_TWIN")) CHECK(!stays, "serial_del of a finalized texture serial: it resolves to nothing and its view is not held");
|
||||
else printf("finalized twin: serial_del %s the ghost entry (DM_TEST_SERIAL_TWIN=1 asserts that it goes)\n", stays ? "leaves" : "removes");
|
||||
step("finalized twin");
|
||||
{ // 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
|
||||
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) {
|
||||
{ // 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
|
||||
CHECK(tex_get(k) == v, "old sequence: a reader between the publish and the check resolves the view of a parent already gone");
|
||||
serial_del(k);
|
||||
CHECK(tex_get(k) == v && ghost(k) && holds(v) && holds(b), "old sequence: after serial_del the finalized serial still resolves to the stale view, which holds its parent");
|
||||
step("old twin sequence");
|
||||
CHECK(own_view_drop_stale(k, v) == 1 && !tex_get(k) && !ghost(k) && !holds(v) && !holds(b) && dropped(v, 0), "own_view_drop_stale takes the stale view's ghost entry");
|
||||
step("old twin sequence cleared");
|
||||
}
|
||||
{ // own_view_put: published only while the parent is shared
|
||||
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");
|
||||
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");
|
||||
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");
|
||||
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");
|
||||
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);
|
||||
CHECK(own_view_drop_stale(fk, v) == 0 && tex_get(fk) == v, "a pinned view stays valid after its parent leaves the sharing table");
|
||||
mtl_tex_unpin(fk);
|
||||
uint64_t pk = (1ull << 62) + 93064; tex_put(pk, b); mtl_tex_pin(pk); serial_del(pk);
|
||||
CHECK(own_view_drop_stale(fk, v) == 0, "and so does a view whose parent is pinned");
|
||||
mtl_tex_unpin(pk);
|
||||
step("valid views");
|
||||
// stale-entry rejection: the view a caller resolved is stale, but the serial's entry changed meanwhile: the new entry stays
|
||||
id y = [NSObject new]; tex_put(lt.serial, y);
|
||||
CHECK(own_view_drop_stale(lt.serial, lv) == 1 && serial_get(lt.serial) == y && live_holds(y), "a stale view whose live entry was replaced meanwhile: the replacement stays");
|
||||
tex_t again = texture(fk); tex_live(&again); tex_put(fk, y); // the serial is live again, and its live entry is another object
|
||||
CHECK(own_view_drop_stale(fk, v) == 1 && serial_get(fk) == y && ghost(fk) && tex_get(fk) == y, "with a live entry that is not the view, the ghost entry is not touched (the live one resolves)");
|
||||
CHECK(tex_end(&again) == 1, "the serial's object finalizes again");
|
||||
CHECK(own_view_drop_stale(fk, v) == 1 && !ghost(fk) && !tex_get(fk) && !holds(v), "then the stale view's ghost entry goes");
|
||||
step("stale views");
|
||||
CHECK(own_view_drop_stale(lt.serial, y) == 0, "an object that is no view is never stale");
|
||||
tex_end(<);
|
||||
step("views removed");
|
||||
}
|
||||
{ // orders of finalize, publish, the parent leaving and resolution
|
||||
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");
|
||||
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");
|
||||
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");
|
||||
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);
|
||||
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");
|
||||
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);
|
||||
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);
|
||||
own_view_drop_stale(k, v); tex_put(k, x);
|
||||
step("ring slot after a stale view");
|
||||
NSMutableArray *objs = [NSMutableArray array];
|
||||
for (int i = 0; i < NGHOST - 1; i++) { id o = [NSObject new]; [objs addObject:o]; tex_put(950000 + (uint64_t)i, o); }
|
||||
CHECK(tex_get(k) == x, "the serial put again after its stale view went stays for NGHOST - 1 newer serials");
|
||||
tex_put(950000 + NGHOST, [NSObject new]);
|
||||
CHECK(!tex_get(k), "and goes with the NGHOST-th, in its turn");
|
||||
step("ring after a stale view");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{ // 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);
|
||||
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);
|
||||
CHECK(st.ghost == 0 && st.live == 0 && !holds(p), "after teardown no serial resolves and nothing is held");
|
||||
step("teardown");
|
||||
}
|
||||
}
|
||||
|
||||
printf("serial owner: %d checks, %d failed\n", checks, failed);
|
||||
|
||||
@@ -41,6 +41,29 @@ int main(void) { @autoreleasepool {
|
||||
// An upgrade asked through the view upgrades the parent; the view is made again on it
|
||||
id<MTLTexture> up2 = tex_upgrade(&srgb, view2, MTLTextureUsageRenderTarget, view2.pixelFormat);
|
||||
CHECK(up2.pixelFormat == MTLPixelFormatRGBA8Unorm_sRGB && up2.parentTexture == tex_mtl(&lin) && tex_mtl(&lin) != up && tex_mtl(&srgb) == up2, "an upgrade through the view upgrades the parent and keeps the pair sharing");
|
||||
{ // A finalized sRGB texture resolved again (a replayed record): its view goes to the ghost ring. Its parent then leaves the sharing table: the next resolution takes the stale
|
||||
// view's ghost entry (own_view_drop_stale) and makes the texture's own, and the held counts match the tables throughout
|
||||
tex_t l2 = lin; l2.serial = 99511; l2.d.width = l2.d.height = 8; nvn_tex_layout(&l2.d, &l2.L);
|
||||
tex_t s2 = l2; s2.serial = 99512; s2.d.format = NVN_FORMAT_RGBA8_SRGB;
|
||||
mtl_tex_init(&l2); mtl_tex_init(&s2);
|
||||
id<MTLTexture> p = tex_mtl(&l2), v = tex_mtl(&s2);
|
||||
CHECK(p && v.parentTexture == p && serial_get(s2.serial) == v, "a live sRGB texture is a view of the shared linear one");
|
||||
mtl_tex_release(&s2);
|
||||
CHECK(!tex_get(s2.serial), "finalized: its live entry goes");
|
||||
id<MTLTexture> gv = tex_mtl(&s2);
|
||||
CHECK(gv.parentTexture == p && tex_get(s2.serial) == gv && !serial_get(s2.serial), "resolved again after its finalize: a view of the shared texture, in the ghost ring");
|
||||
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
|
||||
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);
|
||||
CHECK(own && own != gv && !own.parentTexture && tex_get(s2.serial) == own && !held, "the next resolution drops the stale view and makes the texture's own, in its ghost entry");
|
||||
pthread_mutex_lock(&g_smu); ok = own_check_locked(why, sizeof why); pthread_mutex_unlock(&g_smu);
|
||||
CHECK(ok, why);
|
||||
mtl_tex_release(&l2);
|
||||
}
|
||||
printf("sRGB / linear aliases: %d checks, %d failed\n", checks, failed);
|
||||
return failed != 0;
|
||||
} }
|
||||
@@ -586,7 +586,7 @@ One line per symptom in the [subsystem references](../reference/): what you see
|
||||
- the `[nvn-mtl] pool for handles traced to no slot` line counts thousands of builds while the game streams, and the submit thread spends milliseconds a frame walking the texture pool table again. Ocarina of Time, a seven-minute walk through Kokiri Forest: about 61,000 builds of its 33,025-entry table, 53 s in all. → [Pool tables patched instead of built whole](../reference/nvn-objects.md#pool-tables-patched-instead-of-built-whole)
|
||||
- a pool table keeps naming an sRGB / linear twin's view after the texture it is a view of left the sharing table and, 17 submissions later, the residency set: a texture ID with no residency behind it, which gives GPU address faults in the shaders that sample it (as in issue #1326), and a view that no longer shares its storage's bytes with anything. Seen in `t_pool_notes` only, in no game. → [A view's entry did not hold its parent](../reference/nvn-objects.md#a-views-entry-did-not-hold-its-parent)
|
||||
- the serial table grows by about 10 entries a frame (276,000 after 25,000 frames). → [Dead texture serials kept in a ghost table](../reference/nvn-objects.md#dead-texture-serials-kept-in-a-ghost-table)
|
||||
- none seen on a game. A finalized texture can resolve to an sRGB / linear view of storage that no longer holds its bytes. → [A finalized twin view left in the ghost ring](../reference/nvn-objects.md#a-finalized-twin-view-left-in-the-ghost-ring)
|
||||
- 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)
|
||||
- `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)
|
||||
|
||||
@@ -233,9 +233,9 @@ A game may tell NVN objects apart by their bytes, without an NVN call. A host re
|
||||
|
||||
### A finalized twin view left in the ghost ring
|
||||
|
||||
- **Symptom:** none seen on a game. A finalized texture can resolve to an sRGB / linear view of storage that no longer holds its bytes.
|
||||
- **Cause:** the twin path of `tex_mtl_` puts the view it made for a finalized serial with `tex_put`, which sends a finalized serial to the ghost ring; when the shared texture left the sharing table meanwhile, it drops the view with `serial_del` (`nvn_owner.m`), which removes only a live entry. The ghost entry stays, and holds the view. The next `tex_put` of the serial normally replaces it a few lines later; it stays when making the texture's own one fails (`tex_mtl_` returns nil), and another thread can resolve it in between.
|
||||
- **Fix:** open. `DM_TEST_SERIAL_TWIN=1 ./t_serial_owner` reproduces it at the table level (without the switch the run prints what it finds and counts no failure).
|
||||
- **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_`.
|
||||
- **Found on:** no game; found in a review of the serial tables.
|
||||
|
||||
### A replay's own objects take serials above a capture's
|
||||
|
||||
Reference in new issue
Block a user