mirror of
https://github.com/seedds/nxnative.git
synced 2026-11-23 11:39:42 -10:00
nvn/stand-in: textures in a sparse pool get stand-ins read through the page table (PR #2477)
Past the pool budget, a texture in a sparse (virtual) pool got no stand-in at all: its first read found the dummy, which reads 0, and PRAGMATA's cut to the tablet showed a dark frame. The stand-in's levels are now gathered through the page table (nvn_sparse_copy) one layer at a time, and a SPARSE-flag texture takes one only from its mip tail. A stand-in refused because a page is unmapped waits (STAND_WAIT) until a mapping over the texture's pages notes it (treg_sparse_notes); walks and table patches do not look at it in between, and refusals are not charged to the frame's 512. stand_span computes each layer's span once from the texture's layout. t_pool_standin_sparse checks it on the GPU (25 checks). On Resident Evil 4 (M6, 330 s from the title to the forest path) first reads that found the dummy fell from 695 to 27; stand-in making took 42 ms over the run instead of 20 ms, at a steady 60 fps. Co-authored-by: computerguy526 <[email protected]> Co-authored-by: uncraftablexd <[email protected]>
This commit is contained in:
1 parent
89ea16e32b
commit
74181da849
7 files changed
+168
-17
No files matched your search
@@ -37,6 +37,7 @@ Measured on the M6 (16 GB), in a 1280x720 window: 60 fps at the title (frame tim
|
||||
- [YIELD](../../../docs/reference/shaders.md#yield) in 21 compute shaders.
|
||||
- `nvnCommandBufferSetRepresentativeFragmentTest` was unimplemented ([Representative fragment test](../../../docs/reference/nvn-objects.md#representative-fragment-test-nvncommandbuffersetrepresentativefragmenttest)).
|
||||
- Its streamed textures are sampled through views from level 1 or 2 while only their smallest levels are mapped; the host counted those levels from the first mapped one, so the views were left out or read a smaller level ([Texture views of a sparse texture bound from a later level](../../../docs/reference/rendering.md#texture-views-of-a-sparse-texture-bound-from-a-later-level)).
|
||||
- Its streamed textures live in sparse pools and it is past the pool walk's budget, so a streamed texture's first read found the dummy, about 700 times on the way to the forest path; they now hold stand-ins read through the page table, and 27 first reads found the dummy ([A sparse pool's streamed textures read 0 at their first read](../../../docs/reference/rendering.md#a-sparse-pools-streamed-textures-read-0-at-their-first-read)).
|
||||
- It puts its 1x1 default texture into the IDs of cubes it lets go, which the host reported as read through a cube table ([Read marks of a texture type kept when an ID changes type](../../../docs/reference/nvn-objects.md#read-marks-of-a-texture-type-kept-when-an-id-changes-type)).
|
||||
- At frame 5 one shader is pushed a buffer address on a page of a sparse pool the game has not mapped yet: it reads 0, as on the console ([A buffer address on an unmapped page of a sparse pool](../../../docs/reference/nvn-objects.md#a-buffer-address-on-an-unmapped-page-of-a-sparse-pool)).
|
||||
- It binds compute uniform slot 1 empty (address 0, size 0) before every dispatch of one shader, which read as 0 on the console; the host reported it as unbound, in red ([A uniform slot the game binds empty](../../../docs/reference/rendering.md#a-uniform-slot-the-game-binds-empty)).
|
||||
|
||||
@@ -36,6 +36,7 @@ Measured on the M4 Pro, in a 1920x1080 window: 60 fps at the title, 44 to 52 fps
|
||||
- [More than 1,024 mapped runs in a sparse pool](../../../docs/reference/nvn-objects.md#more-than-1024-mapped-runs-in-a-sparse-pool): the GPU hang 3 to 4 minutes into the intro.
|
||||
- [Pages changed under a buffer the GPU still uses](../../../docs/reference/nvn-objects.md#pages-changed-under-a-buffer-the-gpu-still-uses): absurd indirect dispatches and a hang while it maps and unmaps sparse pages.
|
||||
- [A sparse pool's runs replaced in two steps](../../../docs/reference/nvn-objects.md#a-sparse-pools-runs-replaced-in-two-steps): mesh shaders `765e6d9d2588ce70` and `5aab38bb5ba67ef7` bound to a dummy buffer for a frame.
|
||||
- [A sparse pool's streamed textures read 0 at their first read](../../../docs/reference/rendering.md#a-sparse-pools-streamed-textures-read-0-at-their-first-read): a dark frame at the cut from the tower to the tablet.
|
||||
- [Queue builder size getters](../../../docs/reference/nvn-objects.md#queue-builder-size-getters): read after `SetDefaults` as it makes its queue.
|
||||
- [Float blend clamp](../../../docs/reference/nvn-objects.md#float-blend-clamp-nvncommandbuffersetfloatblendclamp): it sets 1.
|
||||
- [A sync never fenced since its Initialize is signalled](../../../docs/reference/nvn-objects.md#a-sync-never-fenced-since-its-initialize-is-signalled).
|
||||
|
||||
+55
-16
@@ -6626,7 +6626,8 @@ typedef struct { tex_t t; int ok; view_t v; int has_v; void *view; uint64_t view
|
||||
void *im, *im_of; uint64_t im_rid; // im: the view an image slot reads in the shader's pixel class (img_fit), im_of = the view it was built from
|
||||
int prev, next, retired, gone; uint64_t transient_sub; uint8_t tt1, sp; // prev / next: descriptor index + 1, by texture serial; gone: its memory pool was finalized (treg_drop_pool_views); sp: in g_treg_sp
|
||||
void *stand; uint64_t stand_rid, stand_sum, reg_frame; uint8_t stand_tt1, stand_no;
|
||||
uint32_t used; } treg_t; // stand: what its pool table entries hold while its texture is not made (pool_stand; retained), stand_tt1 its texture type + 1, stand_no: it can have none; reg_frame: the frame of the registration
|
||||
uint32_t used; } treg_t; // stand: what its pool table entries hold while its texture is not made (pool_stand; retained), stand_tt1 its texture type + 1, stand_no: it can have none (STAND_NO), or none until a mapping notes its pages (STAND_WAIT); reg_frame: the frame of the registration
|
||||
enum { STAND_NO = 1, STAND_WAIT = 2 }; // treg_t.stand_no: no stand-in while the registration stands; none until a mapping over its pages notes it (treg_sparse_notes)
|
||||
static treg_t *g_treg; static int g_ntreg;
|
||||
static nvn_hmap g_treg_serial = {.klen = 8}; // serial → first registered descriptor index + 1, under g_rmu
|
||||
// Texture ID views are valid for an epoch (view_ser), not for every serial table change: ser_sync takes the notes (own_note) and sends only the IDs registered with a noted serial
|
||||
@@ -6670,7 +6671,9 @@ static void treg_sparse_notes(void) {
|
||||
uint64_t lo = (uint64_t)(uintptr_t)rp->t.mem, hi = lo + rp->t.L.size;
|
||||
int hit = n < 0;
|
||||
if (!hit) { int a = 0, b = m; while (a < b) { int c = (a + b) / 2; if (r[c][1] <= lo) a = c + 1; else b = c; } hit = a < m && r[a][0] < hi; }
|
||||
if (!hit || !rp->t.serial) continue;
|
||||
if (!hit) continue;
|
||||
if (rp->stand_no == STAND_WAIT) rp->stand_no = 0; // a stand-in refused for an unmapped page: looked at again (stand_make_)
|
||||
if (!rp->t.serial) continue;
|
||||
if (ns == ser_cap) { ser_cap = ser_cap ? ser_cap * 2 : 64; ser = realloc_or_die(ser, ser_cap * sizeof *ser, "the sparse mapping notes"); }
|
||||
ser[ns++] = rp->t.serial;
|
||||
}
|
||||
@@ -11100,15 +11103,38 @@ static int stand_layers(const treg_t *r, unsigned *s0, unsigned *ns, MTLTextureT
|
||||
if (*tt == MTLTextureType2D) *ns = 1; else if (*tt != MTLTextureType2DArray) return 0;
|
||||
return *s0 + *ns <= depth && *ns <= STAND_LAYERS;
|
||||
}
|
||||
// A texture in a sparse pool gets a stand-in too, from the levels it reads when every page behind them is mapped. Such a pool's storage is read through its page table (nvn_sparse_copy:
|
||||
// an unmapped page of its address range stops the host with SIGBUS), each layer's bytes from the stand-in's first level to the layer's end (at most STAND_SPARSE_GATHER). PRAGMATA puts its
|
||||
// streamed textures in sparse pools and maps their small levels first: left out by the walk (12 GB allocated against a budget of 0.9 GB), the materials first read at the tower-to-tablet
|
||||
// cut read the dummy, and the G-buffer's base colour was 0.068 on that frame against 0.18 on the next
|
||||
enum { STAND_SPARSE_GATHER = 256u << 10 };
|
||||
static uint64_t g_stand_sparse, g_stand_sparse_unmapped;
|
||||
// The bytes of layer y of t from level l0 to the layer's end: [*lo, *hi) of its storage. 0 when that is empty or more than STAND_SPARSE_GATHER
|
||||
static int stand_span(const tex_t *t, unsigned y, unsigned l0, uint64_t *lo, uint64_t *hi) {
|
||||
*lo = (uint64_t)y * t->L.layer_size + nvn_tex_level_offset(&t->d, l0); *hi = MIN((uint64_t)(y + 1) * t->L.layer_size, t->L.size);
|
||||
return *hi > *lo && *hi - *lo <= STAND_SPARSE_GATHER;
|
||||
}
|
||||
// The stand-in's bytes, read from the texture's storage: into the stand-in, and their hash when asked for (pool_first_read). 0 when the storage cannot be read so
|
||||
static int stand_fill(const tex_t *t, const stand_shape *s, unsigned s0, unsigned ns, id<MTLTexture> into, uint64_t *sum) {
|
||||
static uint8_t buf[STAND_PICK]; int bw, bh, bpb = nvn_fmt_bpb(t->d.format, &bw, &bh); uint64_t h = 1469598103934665603ull;
|
||||
for (unsigned y = 0; y < ns; y++) for (unsigned k = 0; k < s->n; k++) {
|
||||
uint32_t w = s->step ? s->w : MAX(s->w >> k, 1u), hh = s->step ? s->h : MAX(s->h >> k, 1u); size_t pitch = (size_t)((w + bw - 1) / bw) * bpb, rows = (hh + bh - 1) / bh;
|
||||
if (pitch * rows > sizeof buf) return 0;
|
||||
if (s->step ? nvn_deswizzle_pick(&t->d, s->l0, s0 + y, t->mem, s->step, buf, s->nx, s->ny) : nvn_deswizzle(&t->d, s->l0 + k, s0 + y, t->mem, buf, pitch)) return 0;
|
||||
if (into) [into replaceRegion:MTLRegionMake2D(0, 0, w, hh) mipmapLevel:k slice:y withBytes:buf bytesPerRow:pitch bytesPerImage:pitch * rows];
|
||||
if (sum) for (size_t i = 0; i < pitch * rows; i++) h = (h ^ buf[i]) * 1099511628211ull;
|
||||
static uint8_t buf[STAND_PICK], *gat; int bw, bh, bpb = nvn_fmt_bpb(t->d.format, &bw, &bh); uint64_t h = 1469598103934665603ull;
|
||||
int sp = t->pool && nvn_sparse_pool(t->pool);
|
||||
for (unsigned y = 0; y < ns; y++) {
|
||||
const uint8_t *src = t->mem; uint64_t at = 0, lo, hi;
|
||||
if (sp) { // this layer's bytes from the stand-in's first level on, gathered through the page table; any page unmapped: no stand-in
|
||||
if (!stand_span(t, s0 + y, s->l0, &lo, &hi)) return 0;
|
||||
if (!gat && !(gat = malloc(STAND_SPARSE_GATHER))) return 0;
|
||||
if (nvn_sparse_copy(t->pool, gat, (uint64_t)(uintptr_t)t->mem + lo, hi - lo)) { g_stand_sparse_unmapped++; return 0; }
|
||||
src = gat; at = lo;
|
||||
}
|
||||
for (unsigned k = 0; k < s->n; k++) {
|
||||
uint32_t w = s->step ? s->w : MAX(s->w >> k, 1u), hh = s->step ? s->h : MAX(s->h >> k, 1u); size_t pitch = (size_t)((w + bw - 1) / bw) * bpb, rows = (hh + bh - 1) / bh;
|
||||
if (pitch * rows > sizeof buf) return 0;
|
||||
// a gathered layer starts at byte at of the storage: nvn_deswizzle_pick adds the level's whole-storage offset, so it is handed the storage's would-be start
|
||||
if (s->step ? nvn_deswizzle_pick(&t->d, s->l0, s0 + y, (const void *)((uintptr_t)src - at), s->step, buf, s->nx, s->ny)
|
||||
: sp ? nvn_deswizzle_from(&t->d, s->l0 + k, s0 + y, src, at, buf, pitch) : nvn_deswizzle(&t->d, s->l0 + k, s0 + y, src, buf, pitch)) return 0;
|
||||
if (into) [into replaceRegion:MTLRegionMake2D(0, 0, w, hh) mipmapLevel:k slice:y withBytes:buf bytesPerRow:pitch bytesPerImage:pitch * rows];
|
||||
if (sum) for (size_t i = 0; i < pitch * rows; i++) h = (h ^ buf[i]) * 1099511628211ull;
|
||||
}
|
||||
}
|
||||
if (sum) *sum = h;
|
||||
return 1;
|
||||
@@ -11121,11 +11147,20 @@ static int stand_make(int id_) { // Caller holds g_rmu
|
||||
}
|
||||
static int stand_make_(int id_) {
|
||||
treg_t *r = &g_treg[id_]; const tex_t *t = &r->t;
|
||||
r->stand_no = 1; // looked at: not again while this registration stands
|
||||
r->stand_no = STAND_NO; // looked at: not again while this registration stands
|
||||
if (!t->mem || t->d.samples > 1 || t->d.width > 16384 || t->d.height > 16384 || fmt_depth(t->d.format) || rb_packed_shift(t->d.format)
|
||||
|| (t->d.flags & (NVN_TEXTURE_FLAGS_LINEAR | NVN_TEXTURE_FLAGS_LINEAR_RENDER_TARGET | NVN_TEXTURE_FLAGS_SPARSE)) || (t->pool && nvn_sparse_pool(t->pool))) return 0;
|
||||
|| (t->d.flags & (NVN_TEXTURE_FLAGS_LINEAR | NVN_TEXTURE_FLAGS_LINEAR_RENDER_TARGET))) return 0;
|
||||
int sp = t->pool && nvn_sparse_pool(t->pool); // a sparse pool's: read through its page table (stand_fill)
|
||||
if ((t->d.flags & NVN_TEXTURE_FLAGS_SPARSE) && !sp) return 0;
|
||||
unsigned s0, ns; MTLTextureType tt; stand_shape sh;
|
||||
if (!stand_layers(r, &s0, &ns, &tt) || !stand_shape_of(t, &sh)) return 0;
|
||||
if (t->d.flags & NVN_TEXTURE_FLAGS_SPARSE) { uint32_t tw, th, td; int nt = nvn_tex_sparse_tiles(&t->d, &tw, &th, &td); if (nt < 0 || sh.l0 < (unsigned)nt) return 0; } // a sparse texture's levels from its mip tail on (laid out as any texture's, sparse_make), not its tiled ones
|
||||
// A sparse pool's: every page behind the layers' levels mapped, checked before a texture is cut from the heap. A page not mapped yet (the game maps a streamed texture's small
|
||||
// levels after registering it: PRAGMATA's tablet materials, an ID registered 1,542 frames before the cut, held the dummy at the cut while its twins held their stand-ins) waits for
|
||||
// a mapping over its pages, the only thing that can map it: treg_sparse_notes lets it be looked at again, so walks and table patches do not look at it in between
|
||||
if (sp) for (unsigned y = 0; y < ns; y++) { uint64_t lo, hi;
|
||||
if (!stand_span(t, s0 + y, sh.l0, &lo, &hi)) return 0;
|
||||
if (!nvn_sparse_mapped(t->pool, (uint64_t)(uintptr_t)t->mem + lo, hi - lo)) { g_stand_sparse_unmapped++; r->stand_no = STAND_WAIT; return 0; } }
|
||||
uint32_t pf = 0, view_pf = 0; uint8_t sw[4];
|
||||
if (!nvn_fmt_to_mtl(t->d.format, &pf, NULL) || !pf) return 0;
|
||||
int need_sw = treg_swizzle(r, (MTLPixelFormat)pf, 0, sw, &view_pf);
|
||||
@@ -11135,9 +11170,13 @@ static int stand_make_(int id_) {
|
||||
d.storageMode = MTLStorageModeShared; d.usage = MTLTextureUsageShaderRead;
|
||||
if (need_sw) d.swizzle = MTLTextureSwizzleChannelsMake(sw[0], sw[1], sw[2], sw[3]);
|
||||
id<MTLTexture> mt = stand_alloc(d);
|
||||
if (!mt || !stand_fill(t, &sh, s0, ns, mt, pool_first_log() ? &r->stand_sum : NULL)) return 0;
|
||||
uint64_t um0 = g_stand_sparse_unmapped;
|
||||
if (!mt || !stand_fill(t, &sh, s0, ns, mt, pool_first_log() ? &r->stand_sum : NULL)) { // the texture cut from the heap goes with mt
|
||||
if (g_stand_sparse_unmapped != um0) r->stand_no = STAND_WAIT; // a page unmapped since the check above: that unmapping is a mapping note, and so is the one that maps it again
|
||||
return 0;
|
||||
}
|
||||
r->stand = (void *)CFBridgingRetain(mt); r->stand_rid = mt.gpuResourceID._impl; r->stand_tt1 = (uint8_t)tt + 1;
|
||||
g_stand_made++; g_stand_live++; g_stand_picked += sh.step != 0;
|
||||
g_stand_made++; g_stand_live++; g_stand_picked += sh.step != 0; g_stand_sparse += sp;
|
||||
return 1;
|
||||
}
|
||||
// What a table of type dtt holds for an ID the walk leaves out: its stand-in's resource ID, or 0 (the dummy). Caller holds g_rmu
|
||||
@@ -11180,7 +11219,7 @@ static void pool_first_read(int id_, uint64_t f) {
|
||||
static uint64_t lf = ~0ull; static int ln; if (f != lf) { lf = f; ln = 0; }
|
||||
if (log_on && ln++ < 32)
|
||||
MLOG("[pool] frame %llu: texture ID %d read before its texture was made, found %s: ser=%llu %ux%u format %#x target %d levels %u flags %#x, %llu KB, registered %llu frames before; view %d (format %d:%#x levels %d:%u+%u layers %d:%u+%u target %d:%d swizzle %d)",
|
||||
(unsigned long long)f, id_, r->stand ? (stale ? "its stand-in, written since" : "its stand-in") : r->stand_no ? "the dummy (no stand-in for it)" : "the dummy", (unsigned long long)t->serial, t->d.width, t->d.height, t->d.format, t->d.target, t->d.levels, t->d.flags,
|
||||
(unsigned long long)f, id_, r->stand ? (stale ? "its stand-in, written since" : "its stand-in") : r->stand_no == STAND_WAIT ? "the dummy (its stand-in waits for a mapping)" : r->stand_no ? "the dummy (no stand-in for it)" : "the dummy", (unsigned long long)t->serial, t->d.width, t->d.height, t->d.format, t->d.target, t->d.levels, t->d.flags,
|
||||
(unsigned long long)(pool_tex_size(t) >> 10), (unsigned long long)(f - r->reg_frame), r->has_v, r->v.set_format, r->v.format, r->v.set_levels, r->v.base_level, r->v.nlevels, r->v.set_layers, r->v.min_layer, r->v.nlayers, r->v.set_target, r->v.target, r->v.set_swizzle);
|
||||
}
|
||||
static void pool_seen_scan(void) {
|
||||
@@ -11190,8 +11229,8 @@ static void pool_seen_scan(void) {
|
||||
if (g_stand_todo_n) pool_stand_scan();
|
||||
{ static uint64_t at, was; uint64_t now = g_stand_made + g_first_stand + g_first_dummy; // the totals, every 300 frames in which they changed
|
||||
if (f >= at + 300 && now != was) { at = f; was = now;
|
||||
MLOG("pool stand-ins up to frame %llu: %llu made in %.1f ms (%llu of them a level cut down), %llu gave way to their texture, %llu held now in %d heaps of %u MB (%llu not made: the heaps were full), %u waiting; first reads of a texture not made found a stand-in %llu times%s and the dummy %llu times (%llu of them textures of 256 KB or more)",
|
||||
(unsigned long long)f, (unsigned long long)g_stand_made, g_stand_ns / 1e6, (unsigned long long)g_stand_picked, (unsigned long long)g_stand_done, (unsigned long long)g_stand_live, g_nstand_heap, (unsigned)(STAND_HEAP >> 20), (unsigned long long)g_stand_full, g_stand_todo_n, (unsigned long long)g_first_stand,
|
||||
MLOG("pool stand-ins up to frame %llu: %llu made in %.1f ms (%llu of them a level cut down, %llu in a sparse pool; %llu of a sparse pool not made: a page unmapped), %llu gave way to their texture, %llu held now in %d heaps of %u MB (%llu not made: the heaps were full), %u waiting; first reads of a texture not made found a stand-in %llu times%s and the dummy %llu times (%llu of them textures of 256 KB or more)",
|
||||
(unsigned long long)f, (unsigned long long)g_stand_made, g_stand_ns / 1e6, (unsigned long long)g_stand_picked, (unsigned long long)g_stand_sparse, (unsigned long long)g_stand_sparse_unmapped, (unsigned long long)g_stand_done, (unsigned long long)g_stand_live, g_nstand_heap, (unsigned)(STAND_HEAP >> 20), (unsigned long long)g_stand_full, g_stand_todo_n, (unsigned long long)g_first_stand,
|
||||
g_first_stale ? [NSString stringWithFormat:@" (%llu written since the stand-in was made)", (unsigned long long)g_first_stale].UTF8String : "", (unsigned long long)g_first_dummy, (unsigned long long)g_first_dummy_big); } }
|
||||
const uint32_t *w = (const uint32_t *)g_seen_buf.contents; unsigned words = MIN((unsigned)g_ntreg / 32 + 1, (unsigned)POOL_SEEN_WORDS);
|
||||
for (unsigned i = 0; i < words; i++) {
|
||||
|
||||
@@ -209,6 +209,9 @@ check_t_pool_notes: RUN_ENV = MTL_DEBUG_LAYER=1
|
||||
t_htab_update: t_htab_update.m $(MTL_T_DEP)
|
||||
clang $(CFLAGS) -fobjc-arc -o $@ t_htab_update.m $(MTL_T_SRC) $(FW)
|
||||
check_t_htab_update: RUN_ENV = MTL_DEBUG_LAYER=1
|
||||
t_pool_standin_sparse: t_pool_standin_sparse.m $(MTL_T_DEP)
|
||||
clang $(CFLAGS) -fobjc-arc -o $@ t_pool_standin_sparse.m ../../host/crash.c ../../host/hle_bigstack.c t_host_stubs.c $(filter-out ../nvn/nvn_mtl.m,$(GFX)) $(FW)
|
||||
check_t_pool_standin_sparse: RUN_ENV = MTL_DEBUG_LAYER=1
|
||||
t_sparse_tiles: t_sparse_tiles.m $(MTL_T_DEP)
|
||||
clang $(CFLAGS) -fobjc-arc -o $@ t_sparse_tiles.m ../../host/crash.c ../../host/hle_bigstack.c t_host_stubs.c $(filter-out ../nvn/nvn_mtl.m,$(GFX)) $(FW)
|
||||
t_readback: t_readback.m $(MTL_T_DEP)
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
// Stand-ins (pool_stand, t_pool_standin) for textures in a sparse pool, through the real NVN entry points: past the walk's budget, a sparse pool's texture no shader has read holds a
|
||||
// stand-in of its smallest levels, gathered through the pool's page table (stand_fill), not the dummy. One whose small levels are not mapped yet holds the dummy and waits, with no
|
||||
// look at it from walks, for a mapping over its pages, which makes its stand-in (PRAGMATA maps a streamed texture's small levels after registering it)
|
||||
#define nvn_game_fps nvn_game_fps_mtl_default
|
||||
#include "../nvn/nvn_mtl.m"
|
||||
#undef nvn_game_fps
|
||||
#define g_ft g_ft_hle
|
||||
#include "../nvn/hle_nvn.c"
|
||||
#undef g_ft
|
||||
uint64_t g_t0_ns;
|
||||
void *front_metal_layer(void) { return NULL; }
|
||||
module_t *module_of(const void *p) { (void)p; return NULL; }
|
||||
module_t *g_mod; int g_nmod;
|
||||
void svc_thread_dump(void) {}
|
||||
uint64_t now_ns(void) { return bp_now(); }
|
||||
static int checks, failed;
|
||||
#define CHECK(label, c) do { checks++; if (!(c)) { failed++; fprintf(stderr, "FAIL: %s (line %d)\n", label, __LINE__); } } while (0)
|
||||
static pool_t g_phys, g_sparse;
|
||||
static NVNtexture g_tx[4];
|
||||
// A 256x256 RGBA8 sparse texture of 5 levels at voff in the sparse pool; the first map bytes of it mapped to the same offset of the physical pool (0: all of it)
|
||||
static const tex_t *tex_at(int k, uint64_t voff, uint64_t map) {
|
||||
NVNtextureBuilder tb; h_nvnTextureBuilderSetDefaults(&tb); h_nvnTextureBuilderSetTarget(&tb, NVN_TEXTURE_TARGET_2D); h_nvnTextureBuilderSetWidth(&tb, 256); h_nvnTextureBuilderSetHeight(&tb, 256);
|
||||
h_nvnTextureBuilderSetLevels(&tb, 5); h_nvnTextureBuilderSetFormat(&tb, NVN_FORMAT_RGBA8); h_nvnTextureBuilderSetFlags(&tb, NVN_TEXTURE_FLAGS_SPARSE); h_nvnTextureBuilderSetStorage(&tb, (NVNmemoryPool *)&g_sparse, (int64_t)voff);
|
||||
CHECK("texture", h_nvnTextureInitialize(&g_tx[k], &tb));
|
||||
const tex_t *T = (const tex_t *)&g_tx[k];
|
||||
map_req r = {.phys = (NVNmemoryPool *)&g_phys, .poff = (int64_t)voff, .voff = (int64_t)voff, .size = map ? map : (T->L.size + 0xFFFF) & ~0xFFFFull};
|
||||
CHECK("mapping request", h_nvnMemoryPoolMapVirtual((NVNmemoryPool *)&g_sparse, 1, (const NVNmappingRequest *)&r));
|
||||
return T;
|
||||
}
|
||||
// One submission that looks at the 2D float texture table: the entry of ID id_, and the dummy's in *dummy
|
||||
static uint64_t entry(int id_, uint64_t *dummy) {
|
||||
mtl_sub_begin();
|
||||
os_unfair_lock_lock(&g_rmu);
|
||||
__unsafe_unretained id<MTLBuffer> b = nil; pool_gpu(0, (int)MTLTextureType2D, NVN_DPF_FLOAT, 0, &b);
|
||||
const uint8_t *c = b.contents; uint32_t n = *(const uint32_t *)c; const uint64_t *e = (const uint64_t *)(c + DMSH_POOL_HEAD);
|
||||
uint64_t r = (uint32_t)id_ < n ? e[id_] : 0; *dummy = e[n];
|
||||
os_unfair_lock_unlock(&g_rmu);
|
||||
CHECK("the submission completed", mtl_wait(mtl_sub_commit(), 5000000000ull));
|
||||
return r;
|
||||
}
|
||||
int main(void) { @autoreleasepool {
|
||||
mtl_init();
|
||||
size_t plen = 16u << 20;
|
||||
uint8_t *phys = mmap(NULL, plen, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); if (phys == MAP_FAILED) abort();
|
||||
for (size_t i = 0; i < plen; i++) phys[i] = (uint8_t)(i * 7 + 1);
|
||||
pool_builder_t pb = {.mem = phys, .size = plen, .flags = 0x24};
|
||||
CHECK("physical pool", h_nvnMemoryPoolInitialize((NVNmemoryPool *)&g_phys, (const NVNmemoryPoolBuilder *)&pb));
|
||||
pb = (pool_builder_t){.size = DM_GAME_APP_POOL + (1ull << 32), .flags = NVN_MEMORY_POOL_FLAGS_VIRTUAL};
|
||||
CHECK("sparse pool", h_nvnMemoryPoolInitialize((NVNmemoryPool *)&g_sparse, (const NVNmemoryPoolBuilder *)&pb));
|
||||
id<MTLBuffer> hog = [g_dev newBufferWithLength:pool_budget() + (1u << 20) options:MTLResourceStorageModePrivate]; // past the walk's budget, as PRAGMATA is from its first scene
|
||||
CHECK("over the budget", hog && pool_budget_full());
|
||||
const tex_t *B = tex_at(0, 1u << 20, 0);
|
||||
atomic_store(&g_cur_frame, 100);
|
||||
|
||||
// 1. a sparse pool's texture no shader has read: its stand-in, level 4 (16x16, 1 KB) gathered through the page table
|
||||
mtl_reg_tex(6, B, NULL);
|
||||
uint64_t dummy, e = entry(6, &dummy);
|
||||
atomic_store(&g_cur_frame, 101); e = entry(6, &dummy); // the walk past the budget: what it left out gets its stand-in
|
||||
CHECK("the walk makes no unread texture", !tex_get(B->serial));
|
||||
id<MTLTexture> st = (__bridge id<MTLTexture>)g_treg[6].stand;
|
||||
CHECK("a sparse pool's texture left out holds its stand-in, not the dummy", st && e == g_treg[6].stand_rid && e != dummy);
|
||||
if (st) {
|
||||
uint8_t want[16 * 16 * 4], got[16 * 16 * 4];
|
||||
CHECK("level 4 decodes", !nvn_deswizzle(&B->d, 4, 0, phys + (1u << 20), want, 16 * 4));
|
||||
[st getBytes:got bytesPerRow:16 * 4 fromRegion:MTLRegionMake2D(0, 0, 16, 16) mipmapLevel:0];
|
||||
CHECK("the stand-in holds the texture's level 4, read through the page table", st.width == 16 && st.mipmapLevelCount == 1 && !memcmp(want, got, sizeof want));
|
||||
}
|
||||
CHECK("counted as made in a sparse pool", g_stand_sparse >= 1);
|
||||
|
||||
// 2. one whose small levels are not mapped yet (level 0's first 64 KB only): the dummy, not a stand-in that would read 0
|
||||
const tex_t *U = tex_at(1, 6u << 20, 0x10000);
|
||||
uint64_t un0 = g_stand_sparse_unmapped;
|
||||
mtl_reg_tex(9, U, NULL); atomic_store(&g_cur_frame, 200);
|
||||
e = entry(9, &dummy);
|
||||
CHECK("a sparse texture whose small levels are unmapped holds the dummy, no stand-in", e == dummy && !g_treg[9].stand && g_stand_sparse_unmapped > un0);
|
||||
CHECK("it waits for a mapping, charged nothing of the frame's stand-in budget", g_treg[9].stand_no == STAND_WAIT && g_stand_frame == 200 && g_stand_frame_n == 0);
|
||||
un0 = g_stand_sparse_unmapped;
|
||||
atomic_store(&g_cur_frame, 201); e = entry(9, &dummy); // another walk with no mapping: not looked at
|
||||
CHECK("a walk with no mapping does not look at it again", e == dummy && g_stand_sparse_unmapped == un0 && g_treg[9].stand_no == STAND_WAIT);
|
||||
|
||||
// 2b. the game maps the rest within the frame: no walk, but the mapping notes the texture (treg_sparse_notes), which ends its wait, the table entry is patched, and the ID gets
|
||||
// its stand-in then
|
||||
map_req r2 = {.phys = (NVNmemoryPool *)&g_phys, .poff = (6u << 20) + 0x10000, .voff = (6u << 20) + 0x10000, .size = ((U->L.size + 0xFFFF) & ~0xFFFFull) - 0x10000};
|
||||
CHECK("rest mapped", h_nvnMemoryPoolMapVirtual((NVNmemoryPool *)&g_sparse, 1, (const NVNmappingRequest *)&r2));
|
||||
e = entry(9, &dummy);
|
||||
CHECK("mapped in the same frame: its stand-in when the mapping notes it", g_treg[9].stand && e == g_treg[9].stand_rid && e != dummy);
|
||||
mtl_reg_tex(9, NULL, NULL); mtl_reg_tex(9, U, NULL); // registered again: looked at afresh in step 3
|
||||
|
||||
// 3. registered again with every page mapped: the next walk makes its stand-in
|
||||
atomic_store(&g_cur_frame, 202);
|
||||
e = entry(9, &dummy);
|
||||
CHECK("mapped later: its stand-in at the next walk", g_treg[9].stand && e == g_treg[9].stand_rid && e != dummy);
|
||||
|
||||
mtl_reg_tex(6, NULL, NULL); mtl_reg_tex(9, NULL, NULL);
|
||||
CHECK("no stand-in is left when the IDs are emptied", !g_stand_live);
|
||||
hog = nil;
|
||||
printf("t_pool_standin_sparse: %d checks, %d failed\n", checks, failed); return failed != 0;
|
||||
}}
|
||||
@@ -837,6 +837,7 @@ One line per symptom in the [subsystem references](../reference/): what you see
|
||||
- in an Unreal Engine game, textures and lighting turn blurry and muddy about a second after an area loads and stay that way, whatever the frame rate, render scale or upscaling; no red line. → [Unreal's texture streaming pool](../reference/rendering.md#unreals-texture-streaming-pool)
|
||||
- large static and mesh-shader models (Ocarina of Time's castle town wall, rocks, trees, Lon Lon Ranch's barn) draw black for exactly one frame, with the fog in front and the terrain untouched, in pairs four or five frames apart; a few times a minute in play, at fixed places of a route. → [Models black for one frame when a streaming step replaces their textures](../reference/rendering.md#models-black-for-one-frame-when-a-streaming-step-replaces-their-textures)
|
||||
- a patch of scenery (Ocarina of Time's cliff beside the castle town wall) draws darker for exactly one frame, once, as the game gives it a sharper texture; a few times a minute while walking through Hyrule Field. Single frames, not the pairs of the entry above; no red line. → [A streamed texture's first read drew its surface dark for one frame](../reference/rendering.md#a-streamed-textures-first-read-drew-its-surface-dark-for-one-frame)
|
||||
- the first frame after a scene cut is dark where newly visible materials are: PRAGMATA's cut from the tower to the tablet draws the cockpit and tablet with their G-buffer base colour at 0.068 on that frame against 0.18 on the next. → [A sparse pool's streamed textures read 0 at their first read](../reference/rendering.md#a-sparse-pools-streamed-textures-read-0-at-their-first-read)
|
||||
- ≈13 MB/frame leak. → [Transient render targets shared by storage](../reference/rendering.md#transient-render-targets-shared-by-storage)
|
||||
- the residency line adds and removes thousands of allocations every 300 frames, nearly all from one line, the `res_add` of a buffer texture's view in `tex_mtl_`; the memory line's `finalized textures still referenced` stays at the ghost table's 512 while its puts in total climb by about a hundred or more a frame. No red line and no visible effect: the cost is submit-thread time in residency commits. → [Transient buffer textures shared by storage](../reference/rendering.md#transient-buffer-textures-shared-by-storage)
|
||||
- a model has correct geometry, base colour and normals but appears as a black silhouette; deferred lighting's material-ID depth texture stays zero. → [IMAGE usage does not split texture storage](../reference/rendering.md#image-usage-does-not-split-texture-storage)
|
||||
|
||||
@@ -366,10 +366,18 @@ The replay on Metal (`dm/gfx/nvn/nvn_mtl.m`) and the Vulkan and OpenGL layers. E
|
||||
|
||||
- **Symptom:** a patch of scenery (Ocarina of Time's cliff beside the castle town wall) draws darker for exactly one frame, once, as the game gives it a sharper texture; a few times a minute while walking through Hyrule Field. Single frames, not the pairs of the entry above; no red line.
|
||||
- **Cause:** the surface's shader reads its textures through the bindless pool table (`pool_gpu`). Once `pool_budget()` is spent a walk makes only the textures a shader has read (`pool_seen`), so an ID no shader has read holds the dummy, which reads 0; the first read sets the ID's bit, and `pool_seen_scan` makes the texture for the next frame. Ocarina of Time keeps a texture set of its own for each piece of distant scenery and resolution (one level each: a BC5 texture, and two three-layer arrays, BC6H and BC7, of half its size), registers a new set 12 to 969 frames before the material names it, and so the first frame of each such step read the dummy. Which registrations a shader will read is not known ahead: in 58 s of play the game registered 14,455 textures that stayed unread, 9.8 GB, against 10 first reads, and making textures ahead by age and size cost 0.7-1.9 GB for one hit in three (issue #1969).
|
||||
- **Fix:** a texture the walk leaves out for good holds a stand-in in the tables until its first read (`pool_stand`): the texture's own smallest levels, from the first of at most 1 KB on, or, when it has no level that small, its last level cut down to at most 4 KB a layer, every n-th block of every n-th block row (`nvn_deswizzle_pick`; the blocks stay as they are, compressed or not). The host reads them from the storage on the CPU into a small shared texture cut from a heap that is resident from the start, so making one takes no upload batch and no residency commit, and the first read samples the texture's own colours, coarse, for the one frame before the texture is there. 2D textures and 2D arrays of up to 8 layers get one, with the format and channel swizzle of their view (`treg_swizzle`); other types, textures in sparse pools, depth formats and render targets no pass has written hold the dummy as before. At most 512 are made a frame (an area load registers thousands at once); the rest wait and are written into the tables in place (`pool_stand_scan`). A table walked while the budget had room is walked once more when the budget has filled. `DM_NVN_POOL_STANDIN=0` keeps the dummy.
|
||||
- **Fix:** a texture the walk leaves out for good holds a stand-in in the tables until its first read (`pool_stand`): the texture's own smallest levels, from the first of at most 1 KB on, or, when it has no level that small, its last level cut down to at most 4 KB a layer, every n-th block of every n-th block row (`nvn_deswizzle_pick`; the blocks stay as they are, compressed or not). The host reads them from the storage on the CPU into a small shared texture cut from a heap that is resident from the start, so making one takes no upload batch and no residency commit, and the first read samples the texture's own colours, coarse, for the one frame before the texture is there. 2D textures and 2D arrays of up to 8 layers get one, with the format and channel swizzle of their view (`treg_swizzle`); other types, depth formats and render targets no pass has written hold the dummy as before ([textures in sparse pools](#a-sparse-pools-streamed-textures-read-0-at-their-first-read) have their own rule). At most 512 are made a frame (an area load registers thousands at once); the rest wait and are written into the tables in place (`pool_stand_scan`). A table walked while the budget had room is walked once more when the budget has filled. `DM_NVN_POOL_STANDIN=0` keeps the dummy.
|
||||
- **Check:** `t_pool_standin` (which registrations get a stand-in and what it holds, read back from the GPU; the first read; a frame's share), `t_tex` P (the picked blocks against the level decoded whole, in both GOB orders). `DM_NVN_POOL_FIRST=1` logs each first read of a texture not made yet and what it found; the totals go to the log every 300 frames (`pool stand-ins up to frame`). Replays of a player's route past the cliff, 10 with `DM_NVN_POOL_STANDIN=0` and 8 without: after the area load, 31 first reads of a texture not made found the dummy in the 10 runs and none in the 8, where 49 found a stand-in (none of them made from storage the game wrote afterwards). On a 32x18 luminance grid of every present, the cliff's first read is a connected group of 9-12 cells at 0.70-0.79 of the presents before and after in each of the 4 baseline runs in which its set streamed, and at most 2 cells, the grid's noise, in the 5 runs with stand-ins; by picture the cliff is dark for one frame in the one and a little soft in the other. At the area load 39 first reads a run found the dummy, and 5-11 with stand-ins (the frame's share). A run makes about 57,000 stand-ins in 355 ms (6 µs each on an M4 Pro) and holds about 6,000 at a time, in 16 MB of heaps. Resident memory, GPU allocated, the residency set, frame times and submit+present stay within the run-to-run range (medians: resident 10,830 and 10,883 MB; frame p95 16.85 and 17.19 ms, with runs of both kinds between 16.8 and 19.4).
|
||||
- **Found on:** [The Legend of Zelda: Ocarina of Time](../../dm/games/0400486024DEC000/README.md) (a player's recording, M4 Pro, macOS 27.0.1).
|
||||
|
||||
### A sparse pool's streamed textures read 0 at their first read
|
||||
|
||||
- **Symptom:** the first frame after a scene cut is dark where newly visible materials are: PRAGMATA's cut from the tower to the tablet draws the cockpit and tablet with their G-buffer base colour at 0.068 on that frame against 0.18 on the next.
|
||||
- **Cause:** [the stand-ins](#a-streamed-textures-first-read-drew-its-surface-dark-for-one-frame) left out every texture in a sparse pool. Such a pool's address range is read through its page table (an unmapped page stops the host with SIGBUS when read directly), so `stand_make` refused them. PRAGMATA puts all its streamed textures in sparse pools and is past the walk's budget from its first scene (12 GB allocated against a budget of about 0.9 GB), so the materials first read at a cut found the dummy. A texture whose small levels the game maps only after registering it was also refused for the registration's life.
|
||||
- **Fix:** `stand_fill` gathers each layer's bytes from the stand-in's first level to the layer's end through the page table (`nvn_sparse_copy`, at most 256 KB a layer) and decodes them with `nvn_deswizzle_from` / `nvn_deswizzle_pick`. A texture with the SPARSE flag gets one only from its mip tail on, which is laid out as any texture's. When a page behind those levels is not mapped yet, the ID keeps the dummy and waits for a mapping over its pages, the only thing that can map it: the mapping's note (`treg_sparse_notes`, the notes of PR #1721) lets it be looked at again, and walks and table patches do not look at it in between, so an unmapped texture costs nothing until the game maps over it. The `pool stand-ins up to frame` line counts those made in a sparse pool and those not made for an unmapped page.
|
||||
- **Check:** `make -C dm/gfx/tests GAME=<id> check_t_pool_standin_sparse` (Metal validation): a sparse pool's texture left out holds its stand-in, with its level 4 read back from the GPU against the level decoded from the physical pages; one whose small levels are unmapped holds the dummy, charges nothing of the frame's 512, and a walk with no mapping does not look at it again; mapped later in the same frame, it holds its stand-in once the mapping notes it, with no walk; registered again, it holds its stand-in at the next walk. A refused ID looked at on every walk fails two checks, and one whose wait the note does not end fails the same-frame check. `t_pool_standin` (51 checks; 31 with `DM_NVN_POOL_STANDIN=0`) and `t_pool_notes` pass.
|
||||
- **Found on:** [PRAGMATA](../../dm/games/0400C76024710000/README.md) (M4 Pro, macOS 26.6.2, a main-line host with the first version of this change, which looked at an unmapped texture again at every walk, two runs a side): the whole-picture luma at the cut from the tower to the tablet was 65 / 65 / 65 (before / cut / next) with the sparse stand-ins and 65 / 22 / 50 without; 4,872 of the 5,694 stand-ins were in sparse pools; the resident set peaked at 6.5 to 6.6 GB against 6.7 GB without. An earlier build with a G-buffer dump read the cut frame's base colour at 0.181 against 0.181 on the next (0.068 without). [Resident Evil 4](../../dm/games/0400569027BF4000/README.md) (M6, a host at chain `d254f45e8002` with and without this change, title to the forest path in 330 s, one run a side): first reads of a texture not made found the dummy 695 times and a stand-in never without it, and the dummy 27 times and a stand-in 999 times with it; 6,834 of its 7,486 stand-ins were in sparse pools, made in 42 ms over the run (20 ms for 655 without), and 149 were not made for an unmapped page, each looked at again only after a mapping over it.
|
||||
|
||||
### Transient render targets shared by storage
|
||||
|
||||
- **Symptom:** ≈13 MB/frame leak.
|
||||
|
||||
Reference in new issue
Block a user