mirror of
https://github.com/seedds/nxnative.git
synced 2026-11-23 11:39:42 -10:00
A pool texture the walk leaves out holds a stand-in of its own colours until its first read: Ocarina of Time's cliff beside the castle town wall, and other streamed scenery, no longer draws dark for one frame when the game gives it a sharper texture (PR #2075)
Once pool_budget() is spent a walk makes only the textures a shader has read (pool_seen), and an ID no shader has read held the dummy, which reads 0: the first read set its bit and pool_seen_scan made the texture for the next frame, so that frame was dark. 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 BC6H / BC7 arrays) and registers a new set 12 to 969 frames before the material names it. Which registrations will be read cannot be known ahead (14,455 stayed unread in 58 s of play, 9.8 GB, against 10 first reads), and making them all is what lazy making exists to avoid, so the miss frame stays; this changes what it reads. The entry holds a stand-in (pool_stand): the texture's own smallest levels, from the first of at most 1 KB on, or 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; blocks as they are, so no decoder). It is read from the storage on the CPU into a small shared texture cut from a heap resident from the start, for 2D textures and 2D arrays of up to 8 layers with the view's type, layers, format and swizzle (the swizzle rule moves to treg_swizzle, which tex_by_id uses too). Only once the budget is full; at most 512 a frame, the rest written into the tables in place. DM_NVN_POOL_STANDIN=0: the dummy, as before; DM_NVN_POOL_FIRST=1 logs each first read of a texture not made and what it found, including a stand-in whose storage the game wrote since. Rebased onto 6d9edb07's read-mark rule (the PR carried #2022's earlier form); the test's readback buffer is checked for nil (lint_c). Checked: t_pool_standin 51/51 with each encoder setting (31/31 with DM_NVN_POOL_STANDIN=0), t_pool_chunks 151, t_pool_notes 81 (both settings), t_pool_seen_replace 44, t_view_swizzle 554, t_cpu_tex, t_stencil_view, t_pfv, t_int_float_view; t_tex 5,382 (P: picked blocks against the whole level, both GOB orders), also under ASan/UBSan; the host builds; lint_c at the baseline; docs_symptoms --check, test_docs, test_env_switches. The PR's replays of a player's route (M4 Pro): after the area load 31 of 31 first reads found the dummy with stand-ins off, 0 of 49 with them on (none stale); memory and frame times within run-to-run range. A MacBook Air M5 tester: three black flashes in Kakariko in 8 minutes before, none in 4 after. No game run here: only pool-table games reach it, and none has libraries on the current chain. Fixes #1969 Co-authored-by: sleepyeyedjoe <[email protected]>
This commit is contained in:
1 parent
6d9edb079b
commit
6c676444c6
12 files changed
+470
-39
No files matched your search
@@ -122,7 +122,8 @@ From an empty save: at about 45 s `A` (title), `A` (New Game), then the movie. F
|
||||
- [Repository patches missing under bracketed paths](../../../docs/reference/rewriter.md#repository-patches-missing-under-bracketed-paths): resolved, a build in a folder named with brackets lost this game's frame-step stubs and cuts without an error.
|
||||
- [Retired pool buffers pile up while a game submits nothing](../../../docs/reference/memory.md#retired-pool-buffers-pile-up-while-a-game-submits-nothing): out of focus GPU allocated went from 11 to 20 GB in 12 s with swap following (PR #1392); the likely cause of a session that stayed frozen after a window switch. Not reproduced on the village-deck route (2026-11-09, M6): 40 s out of focus, during the load or standing on the deck, made no pool deletions at all, so the scene that remakes a virtual pool's runs out of focus is still to be found.
|
||||
- [The pool walk's statistics made chunks of a large pool](../../../docs/reference/memory.md#the-pool-walks-statistics-made-chunks-of-a-large-pool): resolved, GPU allocated 3.3 to 15.8 GB in the area load on a 16 GB Mac (issue #2068); 77 overlapping 80 MB chunks of the 2,432 MB pool, made by two log statistics.
|
||||
- [Models black for one frame when a streaming step replaces their textures](../../../docs/reference/rendering.md#models-black-for-one-frame-when-a-streaming-step-replaces-their-textures): resolved, the castle town wall, Hyrule Castle, rocks and trees near the town and Lon Lon Ranch's barn black for one frame, twice, 4-5 frames apart, at the streaming steps that replace their material sets (PR #2022; seen on an M4 Pro and an M5 Max). The single dark frame at a texture's first read in an unread ID (the cliff left of the wall) is [issue #1969](https://github.com/seedds/nxnative/issues/1969), open.
|
||||
- [Models black for one frame when a streaming step replaces their textures](../../../docs/reference/rendering.md#models-black-for-one-frame-when-a-streaming-step-replaces-their-textures): resolved, the castle town wall, Hyrule Castle, rocks and trees near the town and Lon Lon Ranch's barn black for one frame, twice, 4-5 frames apart, at the streaming steps that replace their material sets (PR #2022; seen on an M4 Pro and an M5 Max).
|
||||
- [A streamed texture's first read drew its surface dark for one frame](../../../docs/reference/rendering.md#a-streamed-textures-first-read-drew-its-surface-dark-for-one-frame): resolved, the cliff beside the castle town wall in Hyrule Field darker for one frame as the game streamed it a sharper texture set ([issue #1969](https://github.com/seedds/nxnative/issues/1969), PR #2075); the first read now samples the texture's own colours, coarse, for that frame. Seen also in Kakariko Village and its graveyard on a MacBook Air M5 (three flashes in 8 minutes before, none in 4 after, by eye).
|
||||
- [Vertex shaders with layer entry points left out of the prebuild](../../../docs/reference/shaders.md#vertex-shaders-with-layer-entry-points-left-out-of-the-prebuild): resolved, a 230 ms frame at a camera turn in Hyrule Field. The effect vertex shaders (no stage inputs, layer entry points of their own) had no base in the bundled archive and were compiled at their first draw; with the fix (measured with PR #2065's form of it, the same bases as the reflection's `layer_out` gives) the archive holds 33,365 bases instead of 30,690, and the worst frame on the `field-run` route is 39 ms (M6).
|
||||
|
||||
- [Position invariance across shaders](../../../docs/reference/shaders.md#position-invariance-across-shaders): white shapes on plants and bushes for single frames in Kokiri Forest, from the foliage's mesh pre-pass and mesh colour pass rounding the position apart.
|
||||
|
||||
+192
-27
@@ -6090,7 +6090,8 @@ void mtl_prog_snapshot(prog_t *copy,const prog_t *source) {
|
||||
typedef struct { tex_t t; int ok; view_t v; int has_v; void *view; uint64_t view_id, view_rid, view_ser, view_dirty, view_stamp; const void *view_span; int view_pkg; // view_pkg: CPU-written (packaged); only those depend on guest writes (view_dirty)
|
||||
void *alt, *alt_of; uint64_t alt_rid; uint8_t alt_type; // alt: compatible view used when the shader's declared type differs from view (tex_fit), alt_of = the view it was built from
|
||||
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; } treg_t; // prev / next: descriptor index + 1, by texture serial; gone: its memory pool was finalized (treg_drop_pool_views)
|
||||
int prev, next, retired, gone; uint64_t transient_sub; uint8_t tt1; // prev / next: descriptor index + 1, by texture serial; gone: its memory pool was finalized (treg_drop_pool_views)
|
||||
void *stand; uint64_t stand_rid, stand_sum, reg_frame; uint8_t stand_tt1, stand_no; } 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
|
||||
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 (ser_note) and sends only the IDs registered with a noted serial
|
||||
@@ -6120,6 +6121,12 @@ static void treg_drop_views(treg_t *r) {
|
||||
if (r->alt) { defer_obj_unref((__bridge id)r->alt); CFRelease(r->alt); r->alt = r->alt_of = NULL; r->alt_rid = 0; }
|
||||
treg_drop_im(r);
|
||||
}
|
||||
// The stand-in goes with the registration, or when the ID's texture is made (tex_by_id): tables that still name it are read by submissions up to the open one at most (defer_obj_unref)
|
||||
static uint64_t g_stand_live, g_stand_done; // held now; let go because the texture was made
|
||||
static void treg_drop_stand(treg_t *r) {
|
||||
if (r->stand) { defer_obj_unref((__bridge id)r->stand); CFRelease(r->stand); r->stand = NULL; r->stand_rid = 0; g_stand_live--; }
|
||||
r->stand_no = 0;
|
||||
}
|
||||
static void treg_unlink(int id_) {
|
||||
treg_t *r = &g_treg[id_];
|
||||
if (!r->ok || !r->t.serial) return;
|
||||
@@ -6302,6 +6309,7 @@ static void treg_gone(treg_t *r) {
|
||||
if (!r->gone) reg_bump((int)(r - g_treg), 0); // its pool table entries become the dummy: the tables take the ID again (pool_gpu)
|
||||
r->gone = 1;
|
||||
if (r->view) { defer_obj_unref((__bridge id)r->view); CFRelease(r->view); r->view = NULL; r->view_id = r->view_rid = 0; treg_drop_im(r); }
|
||||
treg_drop_stand(r);
|
||||
}
|
||||
static void treg_drop_pool_views(const void *pool) {
|
||||
os_unfair_lock_lock(&g_rmu);
|
||||
@@ -6340,10 +6348,10 @@ void mtl_reg_tex(int tid, const tex_t *t, const view_t *v) {
|
||||
// one under the read budget (POOL_REQ_BYTES). Cleared on every change, it left a streamed replacement to the walk's room (pool_room), which is 0 once pool_budget() is spent:
|
||||
// Ocarina of Time allocates 15-18 GB against a 909 MB budget on a 24 GB Mac, and when its streaming replaced the 17 textures of the castle town wall's material set (3-7 MB each,
|
||||
// each step registered twice, 4-5 frames apart), each registration's first read found the dummy, and the wall, the rocks and the trees drew black for one frame, twice (PR #2022).
|
||||
// A render target no pass has written stays the dummy all the same (pool_unwritten_target). A texture streamed into an ID no shader has read yet still reads the dummy once (issue #1969)
|
||||
// A render target no pass has written stays the dummy all the same (pool_unwritten_target). A texture streamed into an ID no shader has read yet holds its stand-in until its first read (pool_stand, issue #1969)
|
||||
reg_bump(id_, 0); if (!t) pool_seen_clear(id_);
|
||||
treg_unlink(id_); treg_drop_views(rr);
|
||||
rr->tt1 = 0;
|
||||
treg_unlink(id_); treg_drop_views(rr); treg_drop_stand(rr);
|
||||
rr->tt1 = 0; rr->reg_frame = atomic_load(&g_cur_frame);
|
||||
rr->ok = t != NULL; if (t) rr->t = *t; rr->has_v = v != NULL; if (v) rr->v = *v;
|
||||
rr->retired = 0; rr->transient_sub = 0;
|
||||
if (t && t->serial) {
|
||||
@@ -6420,6 +6428,30 @@ static void treg_pkg_stamp(treg_t *rp, id<MTLTexture> base) {
|
||||
id h = tex_get(rp->t.serial); NvPkg *pk = [h isKindOfClass:[NvPkg class]] ? h : nil;
|
||||
if (pk && !pk->sparse && pk->span && pk->tex == base) { rp->view_span = pk->span; rp->view_stamp = pk->stamp; }
|
||||
}
|
||||
// The channel swizzle of an ID's view (sw; returns whether it is not the identity) and the Metal format its view asks for (*view_pf, 0: the texture's own), over a base texture of format
|
||||
// base_pf; in_place: the base reads guest memory in place (a linear or buffer texture). The rule is described where tex_by_id applies it; an ID's stand-in takes the same (stand_make)
|
||||
static int treg_swizzle(const treg_t *r, MTLPixelFormat base_pf, int in_place, uint8_t sw[4], uint32_t *view_pf) {
|
||||
uint8_t fsw[4]; int need_sw = 0; *view_pf = 0;
|
||||
sw[0] = MTLTextureSwizzleRed; sw[1] = MTLTextureSwizzleGreen; sw[2] = MTLTextureSwizzleBlue; sw[3] = MTLTextureSwizzleAlpha;
|
||||
if (!(r->has_v && r->v.set_format && nvn_fmt_to_mtl(r->v.format, view_pf, fsw) && *view_pf)) nvn_fmt_to_mtl(r->t.d.format, NULL, fsw);
|
||||
if (!in_place) for (int c = 0; c < 4; c++) if (fsw[c] >= MTLTextureSwizzleRed) fsw[c] = (uint8_t)(MTLTextureSwizzleRed + c);
|
||||
// Metal cannot cast a depth texture to its single-channel colour equivalent. Use the same depth storage,
|
||||
// but present (R, 0, 0, 1) to the NVN swizzle: a float shader reading Metal depth can see depth in G and B too.
|
||||
if ((base_pf == MTLPixelFormatDepth32Float && *view_pf == MTLPixelFormatR32Float) ||
|
||||
(base_pf == MTLPixelFormatDepth16Unorm && *view_pf == MTLPixelFormatR16Unorm)) {
|
||||
*view_pf = (uint32_t)base_pf;
|
||||
fsw[1] = fsw[2] = MTLTextureSwizzleZero;
|
||||
fsw[3] = MTLTextureSwizzleOne;
|
||||
}
|
||||
if (r->has_v && r->v.set_swizzle) memcpy(sw, r->v.swz, 4);
|
||||
else if (r->t.set_swz) for (int c = 0; c < 4; c++) sw[c] = (uint8_t)(r->t.swz >> (3 * c) & 7);
|
||||
for (int c = 0; c < 4; c++) {
|
||||
if (sw[c] > MTLTextureSwizzleAlpha) sw[c] = (uint8_t)(MTLTextureSwizzleRed + c);
|
||||
if (sw[c] >= MTLTextureSwizzleRed) sw[c] = fsw[sw[c] - MTLTextureSwizzleRed];
|
||||
if (sw[c] != MTLTextureSwizzleRed + c) need_sw = 1;
|
||||
}
|
||||
return need_sw;
|
||||
}
|
||||
static id<MTLTexture> tex_by_id(int tid) { // Caller holds g_rmu
|
||||
if (g_tpool_n && tid >= g_tpool_n) { if (!g_stat_tex_oob++) MLOG("texture ID %d outside the pool (capacity %d) treated as an empty slot: reads 0", tid, g_tpool_n); return nil; }
|
||||
if (tid < 0 || tid >= g_ntreg || !g_treg[tid].ok) { RED_ONCE(10, "texture ID %d not registered", tid); return nil; }
|
||||
@@ -6449,24 +6481,7 @@ static id<MTLTexture> tex_by_id(int tid) { // Caller holds g_rmu
|
||||
// the view format's if Metal has it, else the texture's (GitHub PR #66: The Legend of Zelda: Echoes of Wisdom's hearts are an RGB565 mask, drawn as red rectangles without the swap).
|
||||
// The swap is for bits in the guest's order, so only for textures that read guest memory in place (linear and buffer textures): every other host texture holds Metal's order, uploads
|
||||
// included (rb_packed_shift): for them only the format's alpha 1 remains
|
||||
uint8_t sw[4] = {MTLTextureSwizzleRed, MTLTextureSwizzleGreen, MTLTextureSwizzleBlue, MTLTextureSwizzleAlpha}, fsw[4]; int need_sw = 0; uint32_t view_pf = 0;
|
||||
if (!(r.has_v && r.v.set_format && nvn_fmt_to_mtl(r.v.format, &view_pf, fsw) && view_pf)) nvn_fmt_to_mtl(r.t.d.format, NULL, fsw);
|
||||
if (!base.buffer) for (int c = 0; c < 4; c++) if (fsw[c] >= MTLTextureSwizzleRed) fsw[c] = (uint8_t)(MTLTextureSwizzleRed + c);
|
||||
// Metal cannot cast a depth texture to its single-channel colour equivalent. Use the same depth storage,
|
||||
// but present (R, 0, 0, 1) to the NVN swizzle: a float shader reading Metal depth can see depth in G and B too.
|
||||
if ((base.pixelFormat == MTLPixelFormatDepth32Float && view_pf == MTLPixelFormatR32Float) ||
|
||||
(base.pixelFormat == MTLPixelFormatDepth16Unorm && view_pf == MTLPixelFormatR16Unorm)) {
|
||||
view_pf = (uint32_t)base.pixelFormat;
|
||||
fsw[1] = fsw[2] = MTLTextureSwizzleZero;
|
||||
fsw[3] = MTLTextureSwizzleOne;
|
||||
}
|
||||
if (r.has_v && r.v.set_swizzle) memcpy(sw, r.v.swz, 4);
|
||||
else if (r.t.set_swz) for (int c = 0; c < 4; c++) sw[c] = (uint8_t)(r.t.swz >> (3 * c) & 7);
|
||||
for (int c = 0; c < 4; c++) {
|
||||
if (sw[c] > MTLTextureSwizzleAlpha) sw[c] = (uint8_t)(MTLTextureSwizzleRed + c);
|
||||
if (sw[c] >= MTLTextureSwizzleRed) sw[c] = fsw[sw[c] - MTLTextureSwizzleRed];
|
||||
if (sw[c] != MTLTextureSwizzleRed + c) need_sw = 1;
|
||||
}
|
||||
uint8_t sw[4]; uint32_t view_pf = 0; int need_sw = treg_swizzle(&r, base.pixelFormat, base.buffer != nil, sw, &view_pf);
|
||||
// Stencil mode (nvnTextureViewSetDepthStencilMode STENCIL): the view reads the stencil of a depth-stencil texture, through Metal's stencil-only format. The shader declares a float texture and
|
||||
// takes the raw bits (the translator has no integer texture type): a read of the integer view gives the stencil value in those bits. Pokémon Legends: Z-A's deferred lighting tests stencil bit 6
|
||||
// this way and discards; reading depth instead, the bit followed the depth value and the whole lit picture was striped
|
||||
@@ -6521,6 +6536,7 @@ static id<MTLTexture> tex_by_id(int tid) { // Caller holds g_rmu
|
||||
g_treg_transient[g_ntreg_transient++] = tid; rp->transient_sub = g_sub_serial;
|
||||
}
|
||||
rp->view = (void *)CFBridgingRetain(v); rp->view_rid = v.gpuResourceID._impl; rp->view_ser = sg; rp->view_dirty = dg; treg_pkg_stamp(rp, base);
|
||||
if (rp->stand) { treg_drop_stand(rp); g_stand_done++; } // the tables take the ID again (ser_note), or are patched in place (pool_seen_scan)
|
||||
return v;
|
||||
}
|
||||
// Hot-path object fetch: if the table entry is valid, return the table-held pointer directly (without ARC retain / autorelease), otherwise take the slow path to build / validate. Caller holds g_rmu.
|
||||
@@ -9567,7 +9583,7 @@ static uint64_t dummy_samp_rid(void) { static uint64_t rid; if (!rid) rid = dumm
|
||||
// takes them again. Walking through Ocarina of Time's Kokiri Forest, about 100 of its 33,025 IDs hold such a view
|
||||
enum { POOL_IDS_MAX = 1024 };
|
||||
typedef struct { int *v, n, cap; } pool_ids;
|
||||
typedef struct { __strong id<MTLBuffer> buf; uint64_t gpu, reg, ser, frame, sub; int samp, dtt, dpf, base, left, left_req; pool_ids dead, trans; } pool_snap;
|
||||
typedef struct { __strong id<MTLBuffer> buf; uint64_t gpu, reg, ser, frame, sub; int samp, dtt, dpf, base, left, left_req; pool_ids dead, trans; int over; } pool_snap; // over: its last walk found the budget full, so what it left out has its stand-in (pool_stand)
|
||||
static pool_snap g_psnap[POOL_SNAPS]; static unsigned g_psnap_next;
|
||||
static uint64_t g_psnap_gen = 1; // bumped by each rebuild or patch: an argument block holding a pool address is not reused past it (encode_args_)
|
||||
static uint64_t g_stat_pool_builds; static double g_stat_pool_ms;
|
||||
@@ -9667,14 +9683,163 @@ static int pool_ds_read(const tex_t *t) {
|
||||
if (t->serial != ser || g_pool_ds_gen != gen) { alias_key k = tex_alias_key(t); read = nvn_hm_get(&g_pool_ds, &k) != NULL; ser = t->serial; gen = g_pool_ds_gen; }
|
||||
return read;
|
||||
}
|
||||
// A stand-in for a texture the walk leaves out. Once the budget is full (pool_budget) a texture no shader has read is not made until one reads its ID, and until then its table entry
|
||||
// was the dummy: the first frame that sampled it read 0, the read set the ID's bit, and the texture was there for the next frame. Ocarina of Time keeps a texture set 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) and registers a new set 12 to 969 frames before the
|
||||
// material names it, so each such step drew its surface dark for one frame: the cliff beside the castle town wall (issue #1969). Which registrations will be 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. So the entry holds a small picture of the texture instead: its own
|
||||
// levels from the first of at most STAND_LEVEL bytes on, or, when it has no level that small, its last level cut down to at most STAND_PICK bytes a layer, every n-th block of every
|
||||
// n-th block row (nvn_deswizzle_pick: the blocks stay as they are, so any format Metal has goes). It is read from the storage on the CPU into a shared texture cut from a heap that
|
||||
// is resident from the start: no upload batch and no residency commit, and a table may name it at once. The first read then samples the texture's own colours, coarse, for the one
|
||||
// frame before the texture is there. 2D textures and 2D arrays get one, of the type, layers, format and swizzle of the view tex_by_id makes for the ID; other types, sparse pools'
|
||||
// textures and depth formats hold the dummy as before. Only once the budget is full: below it a later walk makes the texture (and a table walked then is walked once more when the
|
||||
// budget has filled, pool_snap.over). Made where the walk leaves the ID out, at most STAND_FRAME a frame, as an area load registers thousands at once (Ocarina of Time's Hyrule
|
||||
// Field: 5,152, 82 ms in one frame without the limit); the rest wait in g_stand_todo and go into the tables in place (pool_stand_scan), as a newly read ID's texture does. In play
|
||||
// the game registers about 7 a frame, 6 us each on an M4 Pro, and about 6,000 are held at a time, in 16 MB of heaps. DM_NVN_POOL_STANDIN=0: the dummy, as before
|
||||
enum { STAND_LEVEL = 1024, STAND_PICK = 4096, STAND_LAYERS = 8, STAND_HEAP = 4u << 20, STAND_HEAPS = 32, STAND_FRAME = 512 };
|
||||
static __strong id<MTLHeap> g_stand_heap[STAND_HEAPS]; static int g_nstand_heap, g_stand_heap_at;
|
||||
static uint32_t g_stand_todo[POOL_SEEN_WORDS]; static unsigned g_stand_todo_n, g_stand_frame_n; static uint64_t g_stand_frame = ~0ull;
|
||||
static uint64_t g_stand_made, g_stand_picked, g_stand_full, g_stand_ns, g_first_stand, g_first_dummy, g_first_dummy_big, g_first_stale; // first reads of a texture not made: those that found a stand-in, the dummy (and of 256 KB or more), a stand-in whose levels the game has written since
|
||||
static int stand_on(void) { static int v = -1; if (v < 0) { const char *e = getenv("DM_NVN_POOL_STANDIN"); v = !(e && !strcmp(e, "0")); } return v; }
|
||||
static id<MTLTexture> stand_alloc(MTLTextureDescriptor *d) {
|
||||
for (int k = 0; k < g_nstand_heap; k++) { // the heap that last had room first
|
||||
id<MTLTexture> t = [g_stand_heap[(g_stand_heap_at + k) % g_nstand_heap] newTextureWithDescriptor:d];
|
||||
if (t) { g_stand_heap_at = (g_stand_heap_at + k) % g_nstand_heap; return t; }
|
||||
}
|
||||
if (g_nstand_heap == STAND_HEAPS) { g_stand_full++; return nil; }
|
||||
MTLHeapDescriptor *hd = [MTLHeapDescriptor new]; hd.type = MTLHeapTypeAutomatic; hd.size = STAND_HEAP; hd.storageMode = MTLStorageModeShared;
|
||||
id<MTLHeap> h = [g_dev newHeapWithDescriptor:hd];
|
||||
if (!h) { g_stand_full++; return nil; }
|
||||
res_add(h); res_commit(); // before any table names a texture in it
|
||||
g_stand_heap_at = g_nstand_heap; g_stand_heap[g_nstand_heap++] = h;
|
||||
return [h newTextureWithDescriptor:d];
|
||||
}
|
||||
// What a stand-in holds of a texture: its levels from l0 on as they are (n of them, the first of w x h texels), or, when it has no level of STAND_LEVEL bytes or less, its last level
|
||||
// cut down to nx x ny blocks, every step-th block of every step-th block row (nvn_deswizzle_pick). 0: none (a format without a block size, more levels than its size holds)
|
||||
typedef struct { unsigned l0, n; uint32_t w, h, step, nx, ny; } stand_shape;
|
||||
static int stand_shape_of(const tex_t *t, stand_shape *s) {
|
||||
int bw, bh, bpb = nvn_fmt_bpb(t->d.format, &bw, &bh);
|
||||
uint32_t W = MAX(t->d.width, 1u), H = MAX(t->d.height, 1u), levels = MAX(t->d.levels, 1u), fit = 1;
|
||||
while (MAX(W, H) >> fit) fit++;
|
||||
if (!bpb || levels > fit) return 0;
|
||||
memset(s, 0, sizeof *s);
|
||||
unsigned l = 0; uint32_t bx = 0, by = 0;
|
||||
for (; l < levels; l++) { bx = (MAX(W >> l, 1u) + bw - 1) / bw; by = (MAX(H >> l, 1u) + bh - 1) / bh; if ((size_t)bx * by * bpb <= STAND_LEVEL) break; }
|
||||
if (l < levels) { s->l0 = l; s->n = levels - l; s->w = MAX(W >> l, 1u); s->h = MAX(H >> l, 1u); return 1; }
|
||||
if (bpb > 16 || (bpb & (bpb - 1))) return 0; // (a block in one 16-byte piece of the storage, nvn_deswizzle_pick)
|
||||
uint32_t step = (uint32_t)sqrt((double)bx * by * bpb / STAND_PICK); if (step < 2) step = 2;
|
||||
while ((size_t)MAX(bx / step, 1u) * MAX(by / step, 1u) * bpb > STAND_PICK) step++;
|
||||
s->l0 = levels - 1; s->n = 1; s->step = step; s->nx = MAX(bx / step, 1u); s->ny = MAX(by / step, 1u); s->w = s->nx * bw; s->h = s->ny * bh;
|
||||
return 1;
|
||||
}
|
||||
// The layers an ID's stand-in holds and its texture type, which are those of the view tex_by_id makes for it: a 2D texture or a 2D array, read as either. 0: another type, or layers the texture lacks
|
||||
static int stand_layers(const treg_t *r, unsigned *s0, unsigned *ns, MTLTextureType *tt) {
|
||||
const tex_t *t = &r->t; int arr = t->d.target == NVN_TEXTURE_TARGET_2D_ARRAY;
|
||||
if (!arr && t->d.target != NVN_TEXTURE_TARGET_2D && t->d.target != NVN_TEXTURE_TARGET_RECTANGLE) return 0;
|
||||
unsigned depth = arr ? MAX(t->d.depth, 1u) : 1;
|
||||
*s0 = r->has_v && r->v.set_layers ? r->v.min_layer : 0; if (*s0 >= depth) return 0;
|
||||
*ns = r->has_v && r->v.set_layers && r->v.nlayers ? r->v.nlayers : depth - *s0;
|
||||
MTLTextureType base = arr ? MTLTextureType2DArray : MTLTextureType2D; *tt = r->has_v && r->v.set_target ? ttype(r->v.target, base) : base;
|
||||
if (*tt == MTLTextureType2D) *ns = 1; else if (*tt != MTLTextureType2DArray) return 0;
|
||||
return *s0 + *ns <= depth && *ns <= STAND_LAYERS;
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
if (sum) *sum = h;
|
||||
return 1;
|
||||
}
|
||||
static int pool_first_log(void) { static int v = -1; if (v < 0) v = getenv("DM_NVN_POOL_FIRST") != NULL; return v; }
|
||||
static int stand_make_(int id_);
|
||||
static int stand_make(int id_) { // Caller holds g_rmu
|
||||
uint64_t t0 = clock_gettime_nsec_np(CLOCK_UPTIME_RAW); int made = stand_make_(id_); g_stand_ns += clock_gettime_nsec_np(CLOCK_UPTIME_RAW) - t0;
|
||||
return made;
|
||||
}
|
||||
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
|
||||
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;
|
||||
unsigned s0, ns; MTLTextureType tt; stand_shape sh;
|
||||
if (!stand_layers(r, &s0, &ns, &tt) || !stand_shape_of(t, &sh)) 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);
|
||||
if (view_pf && view_pf != pf) { if (tex_need_pfv((MTLPixelFormat)pf, (MTLPixelFormat)view_pf)) return 0; pf = view_pf; } // the sRGB or linear twin holds the same bytes; another format is a view the stand-in does not make
|
||||
MTLTextureDescriptor *d = [MTLTextureDescriptor new];
|
||||
d.textureType = tt; d.pixelFormat = (MTLPixelFormat)pf; d.width = sh.w; d.height = sh.h; d.mipmapLevelCount = sh.n; if (tt == MTLTextureType2DArray) d.arrayLength = ns;
|
||||
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;
|
||||
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;
|
||||
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
|
||||
static uint64_t pool_stand(int id_, int dtt) {
|
||||
treg_t *r = &g_treg[id_];
|
||||
if (!r->stand && !r->stand_no && g_walk_over && stand_on()) {
|
||||
uint64_t f = atomic_load(&g_cur_frame); if (f != g_stand_frame) { g_stand_frame = f; g_stand_frame_n = 0; }
|
||||
if (g_stand_frame_n < STAND_FRAME) g_stand_frame_n += (unsigned)stand_make(id_);
|
||||
else if (id_ < (int)(POOL_SEEN_WORDS * 32) && !(g_stand_todo[id_ >> 5] >> (id_ & 31) & 1)) { g_stand_todo[id_ >> 5] |= 1u << (id_ & 31); g_stand_todo_n++; }
|
||||
}
|
||||
return r->stand && r->stand_tt1 - 1 == dtt ? r->stand_rid : 0;
|
||||
}
|
||||
// The stand-ins that waited (pool_stand), into the tables in place where the entry is still the dummy. Once a submission; caller holds g_rmu
|
||||
static void pool_stand_scan(void) {
|
||||
uint64_t f = atomic_load(&g_cur_frame); if (f != g_stand_frame) { g_stand_frame = f; g_stand_frame_n = 0; }
|
||||
unsigned words = MIN((unsigned)g_ntreg / 32 + 1, (unsigned)POOL_SEEN_WORDS);
|
||||
for (unsigned i = 0; i < words && g_stand_todo_n && g_stand_frame_n < STAND_FRAME; i++)
|
||||
for (uint32_t bits = g_stand_todo[i]; bits && g_stand_frame_n < STAND_FRAME; bits &= bits - 1) {
|
||||
int b = __builtin_ctz(bits), id_ = (int)i * 32 + b;
|
||||
g_stand_todo[i] &= ~(1u << b); g_stand_todo_n--; // NOLINT(clang-analyzer-core.BitwiseShift): bits is nonzero, so ctz returns 0..31
|
||||
if (id_ <= 0 || id_ >= g_ntreg) continue;
|
||||
treg_t *r = &g_treg[id_];
|
||||
if (!r->ok || r->gone || r->view || r->stand || r->stand_no || !tex_storage_live(&r->t) || pool_unwritten_target(&r->t) || pkg_shared(&r->t) || tex_get(r->t.serial)) continue;
|
||||
if (!stand_make(id_)) continue;
|
||||
g_stand_frame_n++;
|
||||
for (int k = 0; k < POOL_SNAPS; k++) { pool_snap *q = &g_psnap[k]; int at = id_ - q->base;
|
||||
if (!q->buf || q->samp || at < 0 || (uint32_t)at >= *(uint32_t *)q->buf.contents || q->dtt != r->stand_tt1 - 1) continue;
|
||||
uint64_t *e = (uint64_t *)((uint8_t *)q->buf.contents + DMSH_POOL_HEAD);
|
||||
if (e[at] == e[*(uint32_t *)q->buf.contents]) e[at] = r->stand_rid; }
|
||||
}
|
||||
}
|
||||
// A first read of an ID whose texture is not made: what it found. DM_NVN_POOL_FIRST=1 logs each (up to 32 a frame); the totals go to the log every 300 frames in which they changed
|
||||
static void pool_first_read(int id_, uint64_t f) {
|
||||
if (id_ <= 0 || id_ >= g_ntreg || !g_treg[id_].ok || g_treg[id_].gone) return;
|
||||
treg_t *r = &g_treg[id_]; const tex_t *t = &r->t;
|
||||
if (r->view || !tex_storage_live(t) || pool_unwritten_target(t) || pkg_shared(t) || tex_get(t->serial)) return;
|
||||
int log_on = pool_first_log(), stale = 0;
|
||||
if (r->stand && log_on) { unsigned s0, ns; MTLTextureType tt; stand_shape sh; uint64_t sum = 0; stale = !stand_layers(r, &s0, &ns, &tt) || !stand_shape_of(t, &sh) || !stand_fill(t, &sh, s0, ns, nil, &sum) || sum != r->stand_sum; }
|
||||
if (r->stand) { g_first_stand++; g_first_stale += (unsigned)stale; } else { g_first_dummy++; g_first_dummy_big += pool_tex_size(t) >= (256u << 10); }
|
||||
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)(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) {
|
||||
if (!g_seen_buf || g_seen_sub == g_sub_serial) return;
|
||||
g_seen_sub = g_sub_serial;
|
||||
uint64_t f = atomic_load(&g_cur_frame); if (f != g_walk_frame) { g_walk_frame = f; g_walk_spent = 0; g_walk_req = 0; }
|
||||
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,
|
||||
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++) {
|
||||
uint32_t fresh = w[i] & ~g_seen_done[i];
|
||||
if (fresh) { g_seen_done[i] |= fresh; g_seen_todo[i] |= fresh; for (uint32_t bits = fresh; bits; bits &= bits - 1) pool_ds_note((int)i * 32 + __builtin_ctz(bits)); }
|
||||
if (fresh) { g_seen_done[i] |= fresh; g_seen_todo[i] |= fresh; for (uint32_t bits = fresh; bits; bits &= bits - 1) { pool_ds_note((int)i * 32 + __builtin_ctz(bits)); pool_first_read((int)i * 32 + __builtin_ctz(bits), f); } }
|
||||
for (uint32_t bits = g_seen_todo[i]; bits; bits &= bits - 1) {
|
||||
int b = __builtin_ctz(bits), id_ = (int)i * 32 + b;
|
||||
if (id_ <= 0 || id_ >= g_ntreg || !g_treg[id_].ok || g_treg[id_].gone || (!g_treg[id_].view && !tex_storage_live(&g_treg[id_].t))) { g_seen_todo[i] &= ~(1u << b); continue; } // NOLINT(clang-analyzer-core.BitwiseShift): bits is nonzero, so ctz returns 0..31
|
||||
@@ -9720,7 +9885,7 @@ static uint64_t pool_entry(int samp, int dtt, int id_, uint64_t limit, int *gone
|
||||
if (r->gone) { ++*gone; return 0; }
|
||||
if (!r->view && !tex_storage_live(&r->t)) { ++*gone; *dead = 1; return 0; }
|
||||
if (!r->view && pool_unwritten_target(&r->t)) return 0; // dummy until a pass writes it (its new texture is noted: ser_note)
|
||||
if (!r->view && pool_tex_size(&r->t) > limit && !pkg_shared(&r->t) && !tex_get(r->t.serial) && !(pool_seen(id_) && g_walk_req < POOL_REQ_BYTES)) { ++*left; *left_req += pool_seen(id_); return 0; } // a texture a shader read is made whatever the budget, so it reads the dummy for a few frames at most
|
||||
if (!r->view && pool_tex_size(&r->t) > limit && !pkg_shared(&r->t) && !tex_get(r->t.serial) && !(pool_seen(id_) && g_walk_req < POOL_REQ_BYTES)) { ++*left; *left_req += pool_seen(id_); return pool_stand(id_, dtt); } // a texture a shader read is made whatever the budget, so it reads its stand-in or the dummy for a few frames at most
|
||||
if (r->tt1 && r->tt1 - 1 != dtt) return 0; // an ID of another texture type, seen before (the table holds only its own type): nothing to look up or charge (the idea is from PR 1428)
|
||||
if (!r->view && !pkg_shared(&r->t)) { g_walk_spent += pool_tex_size(&r->t); if (pool_seen(id_)) g_walk_req += pool_tex_size(&r->t); }
|
||||
void *tv = NULL; uint64_t tr = tex_fast_rid(id_, &tv);
|
||||
@@ -9738,7 +9903,7 @@ static uint64_t pool_gpu(int samp, int dtt, int dpf, int base, __unsafe_unretain
|
||||
if (!q->buf) { if (!free_) free_ = q; }
|
||||
else if (q->samp == samp && q->dtt == dtt && q->dpf == dpf && q->base == base) p = q; }
|
||||
int stale = p && p->sub && p->sub != g_sub_serial; // its transient entries name views that went at the end of submission p->sub
|
||||
int walk_on = p && p->left && p->frame != atomic_load(&g_cur_frame) && (p->left_req || !pool_budget_full()); // left: the walk goes on next frame, unless the budget has no room and nothing read waits: another walk would build the same table
|
||||
int walk_on = p && p->left && p->frame != atomic_load(&g_cur_frame) && (p->left_req || !pool_budget_full() || (!p->over && stand_on())); // left: the walk goes on next frame, unless the budget has no room and nothing read waits: another walk would build the same table (but one, when the budget has filled since the last: what that left out for a later walk gets its stand-in)
|
||||
if (p && p->reg == rg && p->ser == sg && !stale && !walk_on) { *out = p->buf; return p->gpu; }
|
||||
// Only registrations and noted storage changes since the table was built (each generation in its log), the IDs without storage after a pool change and the transient IDs in a
|
||||
// new submission: take those IDs again, by the walk's rules and within what this frame may still make. The table is kept when none of them changes an entry; otherwise the old
|
||||
@@ -9822,7 +9987,7 @@ static uint64_t pool_gpu(int samp, int dtt, int dpf, int base, __unsafe_unretain
|
||||
e[n] = dummy;
|
||||
pool_table_give(p->buf); free(p->dead.v); free(p->trans.v);
|
||||
// Retired registrations drop their views at submission end; their resource IDs cannot outlive that submission (trans, taken again by the next one)
|
||||
*p = (pool_snap){b, b.gpuAddress, rg, sg, atomic_load(&g_cur_frame), nt.n ? g_sub_serial : 0, samp, dtt, dpf, base, left, left_req, nd, nt};
|
||||
*p = (pool_snap){b, b.gpuAddress, rg, sg, atomic_load(&g_cur_frame), nt.n ? g_sub_serial : 0, samp, dtt, dpf, base, left, left_req, nd, nt, samp || g_walk_over};
|
||||
g_psnap_gen++;
|
||||
static mach_timebase_info_data_t tb; if (!tb.denom) mach_timebase_info(&tb);
|
||||
double ms = (double)(mach_absolute_time() - t0) * tb.numer / tb.denom / 1e6; g_stat_pool_ms += ms;
|
||||
|
||||
@@ -591,3 +591,12 @@ t_pool_seen_replace: t_pool_seen_replace.m $(MTL_T_DEP)
|
||||
check_t_pool_seen_replace: t_pool_seen_replace
|
||||
MTL_DEBUG_LAYER=1 DM_NVN_ENC_THREAD=0 ./t_pool_seen_replace
|
||||
MTL_DEBUG_LAYER=1 DM_NVN_ENC_THREAD=1 ./t_pool_seen_replace
|
||||
|
||||
# A texture the pool walk leaves out for good holds a stand-in of its own smallest levels until its first read (issue #1969)
|
||||
t_pool_standin: t_pool_standin.m $(MTL_T_DEP)
|
||||
clang $(CFLAGS) -fobjc-arc -o $@ t_pool_standin.m $(MTL_T_SRC) $(FW)
|
||||
.PHONY: check_t_pool_standin
|
||||
check_t_pool_standin: t_pool_standin
|
||||
MTL_DEBUG_LAYER=1 DM_NVN_ENC_THREAD=0 ./t_pool_standin
|
||||
MTL_DEBUG_LAYER=1 DM_NVN_ENC_THREAD=1 ./t_pool_standin
|
||||
MTL_DEBUG_LAYER=1 DM_NVN_ENC_THREAD=0 DM_NVN_POOL_STANDIN=0 ./t_pool_standin
|
||||
@@ -110,7 +110,8 @@ int main(void) { @autoreleasepool {
|
||||
uint64_t r[2]; memcpy(r, map + (size_t)entry * DMSH_GMEM_MAP_ENTRY, sizeof r);
|
||||
CHECK("the entry holds the whole chunk", r[0] <= chunk << DMSH_GMEM_HINT_SHIFT && r[1] >= (chunk + 1) << DMSH_GMEM_HINT_SHIFT);
|
||||
}
|
||||
// A texture a shader read is made, and goes into the table in place: no new table, and the budget does not apply. One nobody read stays the dummy, as does a render target no pass has written
|
||||
// A texture a shader read is made, and goes into the table in place: no new table, and the budget does not apply. One nobody read is not made: its entry is its stand-in (pool_stand; the
|
||||
// dummy with DM_NVN_POOL_STANDIN=0). A render target no pass has written stays the dummy
|
||||
{
|
||||
tex_t rd = {.magic = MAGIC('T', 'E', 'X', 0), .has_layout = 1, .serial = 99801}, un = rd, rt = rd;
|
||||
rd.d = (nvn_texdesc){.target = 1, .format = NVN_FORMAT_RGBA8, .width = 2048, .height = 2048, .depth = 1, .levels = 1, .samples = 1}; nvn_tex_layout(&rd.d, &rd.L);
|
||||
@@ -123,17 +124,19 @@ int main(void) { @autoreleasepool {
|
||||
__unsafe_unretained id<MTLBuffer> tb = nil, tb2 = nil;
|
||||
mtl_sub_begin(); os_unfair_lock_lock(&g_rmu);
|
||||
pool_gpu(0, MTLTextureType2D, 0, 0, &tb);
|
||||
const uint64_t *e = (const uint64_t *)((const uint8_t *)tb.contents + DMSH_POOL_HEAD); uint64_t dummy = e[302];
|
||||
const uint64_t *e = (const uint64_t *)((const uint8_t *)tb.contents + DMSH_POOL_HEAD); uint64_t dummy = e[*(const uint32_t *)tb.contents]; // the entry past the table's IDs
|
||||
#define UNREAD(x, i) (!g_treg[i].view && (stand_on() ? g_treg[i].stand && (x)[i] == g_treg[i].stand_rid : (x)[i] == dummy))
|
||||
CHECK("the table's head holds the bitmap's address", *(const uint64_t *)((const uint8_t *)tb.contents + 8) == g_seen_buf.gpuAddress);
|
||||
CHECK("unread textures read the dummy", e[301] == dummy && e[303] == dummy);
|
||||
CHECK("unread textures are not made: a stand-in, and the dummy for a render target no pass has written", UNREAD(e, 301) && UNREAD(e, 302) && e[303] == dummy && !g_treg[303].stand);
|
||||
uint64_t was = e[301]; // its stand-in, or the dummy
|
||||
os_unfair_lock_unlock(&g_rmu); finish();
|
||||
((uint32_t *)g_seen_buf.contents)[301 / 32] |= 1u << (301 % 32) | 1u << (303 % 32);
|
||||
mtl_sub_begin(); os_unfair_lock_lock(&g_rmu);
|
||||
pool_gpu(0, MTLTextureType2D, 0, 0, &tb2);
|
||||
const uint64_t *e2 = (const uint64_t *)((const uint8_t *)tb2.contents + DMSH_POOL_HEAD);
|
||||
CHECK("a read texture goes in, past the budget", e2[301] != dummy);
|
||||
CHECK("and into the table a draw already encoded reads", e[301] != dummy);
|
||||
CHECK("an unread one stays the dummy", e2[302] == dummy);
|
||||
CHECK("a read texture goes in, past the budget", g_treg[301].view && e2[301] == g_treg[301].view_rid && e2[301] != dummy);
|
||||
CHECK("and into the table a draw already encoded reads", e[301] != dummy && e[301] != was && !g_treg[301].stand);
|
||||
CHECK("an unread one is still not made", UNREAD(e2, 302));
|
||||
CHECK("a read render target no pass wrote stays the dummy", e2[303] == dummy);
|
||||
os_unfair_lock_unlock(&g_rmu); finish();
|
||||
// Another texture in a read ID keeps the ID's mark, so the next walk makes the replacement; an emptied ID is cleared, and an ID given its first texture has not been read
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// The tables of handles traced to no slot (pool_gpu) over noted changes: a pool added or removed records REG_POOL and no ID, so a table built before keeps its buffer when nothing
|
||||
// it holds changed, takes again an ID whose storage came with a new pool, and the IDs of a finalized pool go to the dummy through notes of their own (treg_gone). A new submission
|
||||
// takes again only the IDs whose entries were views of retired registrations, which went at the end of the last one, and a storage takeover builds nothing. A patch follows the
|
||||
// walk's rules: past the budget an unread texture stays the dummy, and a view the shader-read scan writes in place is taken again at the next submission. Each patched table holds
|
||||
// walk's rules: past the budget an unread texture is not made (its entry is its stand-in, pool_stand), and a view the shader-read scan writes in place is taken again at the next submission. Each patched table holds
|
||||
// what a table built whole holds.
|
||||
// Ocarina of Time maps and unmaps virtual pool runs dozens of times a second and registers something between most of its draws; each of those walked its 33,025-entry texture table
|
||||
#define nvn_game_fps nvn_game_fps_mtl_default
|
||||
@@ -137,7 +137,7 @@ int main(void) { @autoreleasepool {
|
||||
CHECK(g_stat_pool_builds == builds && entry(b11, 4) != dummy_of(b11), "the next submission takes ID 4 again without building the table");
|
||||
same_as_whole(b11, "the patched table holds what a table built whole does (takeover)");
|
||||
|
||||
// Past the budget: a texture registered since the walk that no shader has read stays the dummy, as in a walk, and the table is patched rather than built whole. ID 6 names a
|
||||
// Past the budget: a texture registered since the walk that no shader has read is not made, as in a walk (its entry is its stand-in, or the dummy with DM_NVN_POOL_STANDIN=0), and the table is patched rather than built whole. ID 6 names a
|
||||
// texture the game finalized before anything made it. Once a shader reads ID 6, the scan of the read bitmap makes it whatever the budget and writes it into the table in place:
|
||||
// a view of a retired registration, which the table lists, so the next submission takes it again
|
||||
mtl_reg_tex(3, NULL, NULL); mtl_reg_tex(4, NULL, NULL); table(); table(); // no retired IDs left: the table lists none
|
||||
@@ -150,9 +150,10 @@ int main(void) { @autoreleasepool {
|
||||
builds = g_stat_pool_builds; patched = g_stat_pool_patched; kept = g_stat_pool_kept;
|
||||
id<MTLBuffer> b12 = table();
|
||||
CHECK(g_stat_pool_builds == builds && g_stat_pool_patched + g_stat_pool_kept > patched + kept, "a registration past the budget patches the table");
|
||||
CHECK(entry(b12, 6) == dummy_of(b12) && snap()->left > 0 && !snap()->left_req, "the unread texture is left out");
|
||||
#define LEFT_OUT(buf, i) (!g_treg[i].view && (stand_on() ? g_treg[i].stand && entry(buf, i) == g_treg[i].stand_rid : entry(buf, i) == dummy_of(buf))) // not the texture: its stand-in
|
||||
CHECK(LEFT_OUT(b12, 6) && snap()->left > 0 && !snap()->left_req, "the unread texture is left out");
|
||||
h_nvnTextureFinalize(&t6); table();
|
||||
CHECK(g_treg[6].retired && !tex_get(((const tex_t *)&t6)->serial) && entry(snap()->buf, 6) == dummy_of(snap()->buf), "finalized, it is still left out: nothing made it");
|
||||
CHECK(g_treg[6].retired && !tex_get(((const tex_t *)&t6)->serial) && LEFT_OUT(snap()->buf, 6), "finalized, it is still left out: nothing made it");
|
||||
((uint32_t *)g_seen_buf.contents)[6 / 32] |= 1u << (6 % 32);
|
||||
mtl_sub_begin(); os_unfair_lock_lock(&g_rmu); pool_seen_scan();
|
||||
pool_snap *s = snap(); id<MTLBuffer> b13 = s->buf;
|
||||
|
||||
@@ -0,0 +1,191 @@
|
||||
// A texture the pool walk leaves out for good (the budget is full, no shader has read its ID) holds a stand-in in the tables until its first read: its own smallest levels, or its
|
||||
// last level cut down to every n-th block when it has none that small (pool_stand, issue #1969). Below the budget the entry stays the dummy, as the walk goes on and makes the
|
||||
// texture; so does a texture of a type that has no stand-in and an unwritten render target. A read makes the texture and lets the stand-in go, as does another registration.
|
||||
// DM_NVN_POOL_STANDIN=0: the dummy everywhere, as before
|
||||
#include "../nvn/nvn_mtl.m"
|
||||
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(c, msg) do { checks++; if (!(c)) { failed++; fprintf(stderr, "FAIL: %s (line %d)\n", msg, __LINE__); } } while (0)
|
||||
static uint64_t entry(id<MTLBuffer> b, int i) { return ((const uint64_t *)((const uint8_t *)b.contents + DMSH_POOL_HEAD))[i]; }
|
||||
static uint64_t dummy(id<MTLBuffer> b) { return entry(b, (int)*(const uint32_t *)b.contents); }
|
||||
static pool_snap *snap(void) { for (int k = 0; k < POOL_SNAPS; k++) if (g_psnap[k].buf && !g_psnap[k].samp && g_psnap[k].dtt == MTLTextureType2D && g_psnap[k].dpf == NVN_DPF_FLOAT && !g_psnap[k].base) return &g_psnap[k]; return NULL; }
|
||||
// The 2D float texture table at base 0, in a submission of its own, as a draw that reads it would ask. room0: with this frame's share spent, so the walk makes nothing
|
||||
static id<MTLBuffer> table(int room0) {
|
||||
mtl_sub_begin();
|
||||
if (room0) { g_walk_frame = atomic_load(&g_cur_frame); g_walk_spent = POOL_FRAME_BYTES; g_walk_req = 0; }
|
||||
__unsafe_unretained id<MTLBuffer> b = nil;
|
||||
os_unfair_lock_lock(&g_rmu); pool_gpu(0, MTLTextureType2D, NVN_DPF_FLOAT, 0, &b); os_unfair_lock_unlock(&g_rmu);
|
||||
id<MTLBuffer> held = b;
|
||||
CHECK(mtl_wait(mtl_sub_commit(), 5000000000ull), "the submission completed");
|
||||
CHECK(!mtl_last_commit_error(), "the submission has no GPU error");
|
||||
return held;
|
||||
}
|
||||
static void read_mark(int i) { pool_seen_gpu(); atomic_fetch_or((_Atomic uint32_t *)g_seen_buf.contents + i / 32, 1u << (i % 32)); }
|
||||
// A texture's storage: every byte of level l of layer y is v + l + 8 * y; pattern: byte k of block (x, y) of level 0 is 3 * x + 5 * y + k instead (its storage is block linear, so through nvn_swizzle)
|
||||
static uint8_t pat(uint32_t x, uint32_t y, uint32_t k) { return (uint8_t)(3 * x + 5 * y + k); }
|
||||
static void fill(const tex_t *t, uint8_t v, int pattern) {
|
||||
int bw, bh, bpb = nvn_fmt_bpb(t->d.format, &bw, &bh);
|
||||
for (unsigned l = 0; l < t->d.levels; l++) {
|
||||
uint32_t w = MAX(t->d.width >> l, 1u), h = MAX(t->d.height >> l, 1u), z = t->d.target == NVN_TEXTURE_TARGET_3D ? MAX(t->d.depth >> l, 1u) : 1; size_t pitch = (size_t)((w + bw - 1) / bw) * bpb, rows = (h + bh - 1) / bh;
|
||||
uint8_t *lin = malloc(pitch * rows * z);
|
||||
for (unsigned y = 0; y < (t->d.target == NVN_TEXTURE_TARGET_2D_ARRAY ? t->d.depth : 1); y++) {
|
||||
memset(lin, v + (int)l + 8 * (int)y, pitch * rows * z);
|
||||
if (pattern && !l) for (size_t r = 0; r < rows; r++) for (size_t i = 0; i < pitch; i++) lin[r * pitch + i] = pat((uint32_t)(i / bpb), (uint32_t)r, (uint32_t)(i % bpb));
|
||||
if (nvn_swizzle(&t->d, l, y, lin, pitch, t->mem)) abort();
|
||||
}
|
||||
free(lin);
|
||||
}
|
||||
}
|
||||
// The bytes of a level of a layer of a Metal texture, read back on the GPU (n of them, rows of row bytes) into a buffer; nil when the copy failed
|
||||
static id<MTLBuffer> level_of(id<MTLTexture> t, unsigned level, unsigned slice, size_t n, size_t row) {
|
||||
if (!t || level >= t.mipmapLevelCount || slice >= t.arrayLength) return nil;
|
||||
id<MTLBuffer> b = [g_dev newBufferWithLength:n options:MTLResourceStorageModeShared];
|
||||
if (!b) return nil;
|
||||
id<MTLCommandBuffer> cb = q_cb(); id<MTLBlitCommandEncoder> bl = [cb blitCommandEncoder];
|
||||
[bl copyFromTexture:t sourceSlice:slice sourceLevel:level sourceOrigin:MTLOriginMake(0, 0, 0) sourceSize:MTLSizeMake(MAX(t.width >> level, 1u), MAX(t.height >> level, 1u), 1)
|
||||
toBuffer:b destinationOffset:0 destinationBytesPerRow:row destinationBytesPerImage:n];
|
||||
[bl endEncoding]; [cb commit]; [cb waitUntilCompleted];
|
||||
return cb.status == MTLCommandBufferStatusCompleted ? b : nil;
|
||||
}
|
||||
static int layer_is(id<MTLTexture> t, unsigned level, unsigned slice, size_t n, size_t row, uint8_t v) { // every byte v
|
||||
id<MTLBuffer> b = level_of(t, level, slice, n, row); if (!b) return 0;
|
||||
for (size_t i = 0; i < n; i++) if (((const uint8_t *)b.contents)[i] != v) return 0;
|
||||
return 1;
|
||||
}
|
||||
static int level_is(id<MTLTexture> t, unsigned level, size_t n, size_t row, uint8_t v) { return layer_is(t, level, 0, n, row, v); }
|
||||
// Whether a stand-in's one level holds block (step * i + step / 2, step * j + step / 2) of the pattern at its block (i, j)
|
||||
static int picked_is(id<MTLTexture> t, uint32_t nx, uint32_t ny, uint32_t bpb, uint32_t step) {
|
||||
id<MTLBuffer> b = level_of(t, 0, 0, (size_t)nx * ny * bpb, (size_t)nx * bpb); if (!b || t.mipmapLevelCount != 1) return 0;
|
||||
const uint8_t *q = b.contents;
|
||||
for (uint32_t j = 0; j < ny; j++) for (uint32_t i = 0; i < nx; i++) for (uint32_t k = 0; k < bpb; k++) if (q[((size_t)j * nx + i) * bpb + k] != pat(step * i + step / 2, step * j + step / 2, k)) return 0;
|
||||
return 1;
|
||||
}
|
||||
static id<MTLTexture> stand(int i) { return (__bridge id<MTLTexture>)g_treg[i].stand; }
|
||||
int main(void) { @autoreleasepool {
|
||||
mtl_init();
|
||||
const char *e = getenv("DM_NVN_POOL_STANDIN"); int on = !(e && !strcmp(e, "0"));
|
||||
enum { SLOT = 1 << 17, N = 10, LEN = N * SLOT, MANY = STAND_FRAME + 40 };
|
||||
uint8_t *mem = mmap(NULL, LEN, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); if (mem == MAP_FAILED) abort();
|
||||
pool_t p = {.magic = MAGIC('P', 'O', 'O', 'L'), .mem = mem, .size = LEN, .flags = NVN_MEMORY_POOL_FLAGS_CPU_UNCACHED | NVN_MEMORY_POOL_FLAGS_GPU_CACHED};
|
||||
mtl_pool_add(mem, LEN);
|
||||
// 0, 1: RGBA8 64x64 with all 7 levels (16x16, 1024 bytes, is the first a stand-in takes); 2: one level, 16 KB (cut down to every 2nd texel: 32x32, 4096 bytes); 3: BC1 with
|
||||
// 7 levels (its 32x32, 512 bytes, is the first); 4: a 2D array of 2 layers; 5: a render target no pass has written; 6: as 0, registered through an sRGB view with red and blue
|
||||
// exchanged; 7: as 0, for the IDs past a frame's share; 8: a 3D texture; 9: BC7 256x256 with one level, 64 KB (cut down to every 4th block: 16x16 blocks)
|
||||
tex_t t[N] = {0};
|
||||
for (int n = 0; n < N; n++) {
|
||||
t[n].magic = MAGIC('T', 'E', 'X', 0); t[n].has_layout = 1; t[n].serial = 990200 + n; t[n].pool = &p; t[n].off = n * SLOT; t[n].mem = mem + t[n].off;
|
||||
t[n].d = (nvn_texdesc){.target = NVN_TEXTURE_TARGET_2D, .format = NVN_FORMAT_RGBA8, .width = 64, .height = 64, .depth = 1, .levels = 7, .samples = 1};
|
||||
if (n == 2) t[n].d.levels = 1;
|
||||
if (n == 3) t[n].d.format = NVN_FORMAT_RGBA_DXT1;
|
||||
if (n == 4) { t[n].d.target = NVN_TEXTURE_TARGET_2D_ARRAY; t[n].d.depth = 2; }
|
||||
if (n == 5) { t[n].has_layout = 0; t[n].d.flags = NVN_TEXTURE_FLAGS_COMPRESSIBLE; t[n].d.levels = 1; }
|
||||
if (n == 8) { t[n].d.target = NVN_TEXTURE_TARGET_3D; t[n].d.depth = 2; t[n].d.levels = 1; }
|
||||
if (n == 9) { t[n].d.format = NVN_FORMAT_BPTC_UNORM; t[n].d.width = t[n].d.height = 256; t[n].d.levels = 1; }
|
||||
nvn_tex_layout(&t[n].d, &t[n].L); if (t[n].L.size > SLOT) abort();
|
||||
fill(&t[n], (uint8_t)(0x10 * (n + 1)), n == 2 || n == 9); mtl_tex_init(&t[n]);
|
||||
}
|
||||
view_t srgb = {.set_format = 1, .format = NVN_FORMAT_RGBA8_SRGB, .set_swizzle = 1, .swz = {MTLTextureSwizzleBlue, MTLTextureSwizzleGreen, MTLTextureSwizzleRed, MTLTextureSwizzleAlpha}};
|
||||
view_t layer1 = {.set_target = 1, .target = NVN_TEXTURE_TARGET_2D, .set_layers = 1, .min_layer = 1, .nlayers = 1};
|
||||
|
||||
// Below the budget a texture this frame has no room for is made by a later walk: the dummy until then, and no stand-in
|
||||
mtl_reg_tex(10, &t[0], NULL);
|
||||
id<MTLBuffer> b0 = table(1);
|
||||
CHECK(entry(b0, 10) == dummy(b0) && !g_treg[10].stand && !g_treg[10].view, "below the budget a texture left out holds the dummy");
|
||||
CHECK(snap() && snap()->left > 0 && !snap()->over, "and its table goes on walking");
|
||||
|
||||
// Past the budget no walk makes an unread texture. The table walks once more, and what it left out gets its stand-in
|
||||
id<MTLBuffer> hog = [g_dev newBufferWithLength:pool_budget() + (1u << 20) options:MTLResourceStorageModePrivate];
|
||||
CHECK(hog && pool_budget_full(), "over the budget");
|
||||
mtl_reg_tex(11, &t[2], NULL); mtl_reg_tex(12, &t[3], NULL); mtl_reg_tex(13, &t[4], NULL); mtl_reg_tex(14, &t[5], NULL); mtl_reg_tex(15, &t[6], &srgb);
|
||||
mtl_reg_tex(16, &t[4], &layer1); mtl_reg_tex(17, &t[8], NULL); mtl_reg_tex(18, &t[9], NULL);
|
||||
uint64_t builds = g_stat_pool_builds; atomic_fetch_add(&g_cur_frame, 1); // a later frame
|
||||
id<MTLBuffer> b1 = table(0);
|
||||
if (on) { CHECK(g_stat_pool_builds == builds + 1 && snap()->over, "the budget filled: the table is walked once more"); builds++; }
|
||||
else CHECK(g_stat_pool_builds == builds, "DM_NVN_POOL_STANDIN=0: the registrations patch the table, with no walk");
|
||||
CHECK(!g_treg[10].view && !tex_get(t[0].serial), "the walk makes no unread texture");
|
||||
if (on) {
|
||||
id<MTLTexture> s = stand(10);
|
||||
CHECK(s && entry(b1, 10) == g_treg[10].stand_rid && entry(b1, 10) != dummy(b1), "the texture left out holds its stand-in");
|
||||
CHECK(s.width == 16 && s.height == 16 && s.mipmapLevelCount == 5 && s.pixelFormat == MTLPixelFormatRGBA8Unorm && s.textureType == MTLTextureType2D, "the stand-in is the texture from its 16x16 level on");
|
||||
CHECK(level_is(s, 0, 16 * 16 * 4, 64, 0x12) && level_is(s, 1, 8 * 8 * 4, 32, 0x13) && level_is(s, 4, 4, 4, 0x16), "and holds those levels' bytes");
|
||||
id<MTLTexture> c = stand(12);
|
||||
CHECK(c && entry(b1, 12) == g_treg[12].stand_rid, "a compressed texture has one");
|
||||
CHECK(c.width == 32 && c.height == 32 && c.mipmapLevelCount == 6 && c.pixelFormat == MTLPixelFormatBC1_RGBA, "from its 32x32 level on");
|
||||
CHECK(level_is(c, 0, 8 * 8 * 8, 64, 0x41) && level_is(c, 5, 8, 8, 0x46), "with those levels' blocks");
|
||||
id<MTLTexture> v = stand(15); MTLTextureSwizzleChannels sw = v.swizzle;
|
||||
CHECK(v && entry(b1, 15) == g_treg[15].stand_rid && v.pixelFormat == MTLPixelFormatRGBA8Unorm_sRGB, "a registration through an sRGB view has one of the view's format");
|
||||
CHECK(sw.red == MTLTextureSwizzleBlue && sw.green == MTLTextureSwizzleGreen && sw.blue == MTLTextureSwizzleRed && sw.alpha == MTLTextureSwizzleAlpha, "and the view's swizzle");
|
||||
id<MTLTexture> k = stand(11);
|
||||
CHECK(k && entry(b1, 11) == g_treg[11].stand_rid && k.width == 32 && k.height == 32 && k.mipmapLevelCount == 1, "a texture with no level small enough has its last level cut down: 32x32 of 64x64");
|
||||
CHECK(picked_is(k, 32, 32, 4, 2), "every 2nd texel of every 2nd row, from the middle of each cell");
|
||||
id<MTLTexture> k7 = stand(18);
|
||||
CHECK(k7 && entry(b1, 18) == g_treg[18].stand_rid && k7.width == 64 && k7.height == 64 && k7.pixelFormat == MTLPixelFormatBC7_RGBAUnorm, "a compressed one keeps its format: 16x16 blocks of 64x64");
|
||||
CHECK(picked_is(k7, 16, 16, 16, 4), "every 4th block of every 4th block row");
|
||||
id<MTLTexture> a = stand(13), a1 = stand(16);
|
||||
CHECK(a && a.textureType == MTLTextureType2DArray && a.arrayLength == 2 && layer_is(a, 0, 1, 16 * 16 * 4, 64, 0x5a), "a 2D array has a stand-in of its layers");
|
||||
CHECK(entry(b1, 13) == dummy(b1), "which a table of 2D textures does not take");
|
||||
CHECK(a1 && a1.textureType == MTLTextureType2D && entry(b1, 16) == g_treg[16].stand_rid && level_is(a1, 0, 16 * 16 * 4, 64, 0x5a), "a layer of it registered as a 2D texture has that layer's");
|
||||
} else {
|
||||
int none = 1; for (int i = 10; i <= 18; i++) none &= entry(b1, i) == dummy(b1) && !g_treg[i].stand;
|
||||
CHECK(none && !g_stand_made, "DM_NVN_POOL_STANDIN=0: the dummy, and no stand-in is made");
|
||||
}
|
||||
CHECK(entry(b1, 17) == dummy(b1) && !g_treg[17].stand, "a 3D texture holds the dummy");
|
||||
CHECK(entry(b1, 14) == dummy(b1) && !g_treg[14].stand, "a render target no pass has written holds the dummy");
|
||||
atomic_fetch_add(&g_cur_frame, 1);
|
||||
id<MTLBuffer> b2 = table(0);
|
||||
CHECK(b2 == b1 && g_stat_pool_builds == builds, "nothing changed: the table stands, in a later frame too");
|
||||
|
||||
// Another texture into the unread ID: the stand-in is the new texture's
|
||||
uint64_t rid0 = g_treg[10].stand_rid;
|
||||
mtl_reg_tex(10, &t[1], NULL);
|
||||
id<MTLBuffer> b3 = table(0);
|
||||
if (on) {
|
||||
CHECK(stand(10) && entry(b3, 10) == g_treg[10].stand_rid && level_is(stand(10), 0, 16 * 16 * 4, 64, 0x22), "another texture registered into the ID: its stand-in holds the new texture's levels");
|
||||
CHECK(entry(b1, 10) == rid0, "the earlier table keeps its entry");
|
||||
} else CHECK(entry(b3, 10) == dummy(b3), "DM_NVN_POOL_STANDIN=0: the dummy");
|
||||
CHECK(!pool_seen(10) && !g_treg[10].view, "the ID is still unread, and its texture not made");
|
||||
|
||||
// The first read: the scan of the read bitmap makes the texture and writes it into the table in place, and the stand-in goes
|
||||
uint64_t first = g_first_stand, firstd = g_first_dummy;
|
||||
read_mark(10);
|
||||
id<MTLBuffer> b4 = table(0);
|
||||
CHECK(g_treg[10].view && entry(b4, 10) == g_treg[10].view_rid && entry(b4, 10) != dummy(b4) && !g_treg[10].stand, "a read makes the texture, and the stand-in goes");
|
||||
CHECK(level_is((__bridge id<MTLTexture>)g_treg[10].view, 0, 64 * 64 * 4, 256, 0x20), "the table names the texture itself");
|
||||
CHECK(on ? g_first_stand == first + 1 && g_first_dummy == firstd : g_first_stand == first && g_first_dummy == firstd + 1, "the first read is counted: it found a stand-in, or the dummy");
|
||||
|
||||
// An emptied ID and an ID whose pool is finalized hold the dummy, and their stand-ins go
|
||||
mtl_reg_tex(12, NULL, NULL); mtl_reg_tex_gone(15);
|
||||
id<MTLBuffer> b5 = table(0);
|
||||
CHECK(entry(b5, 12) == dummy(b5) && !g_treg[12].stand && entry(b5, 15) == dummy(b5) && !g_treg[15].stand, "an emptied ID and one whose storage went hold the dummy");
|
||||
|
||||
// More registrations than a frame's share of stand-ins: the rest hold the dummy until the next submission, which writes theirs into the table in place
|
||||
for (int i = 0; i < MANY; i++) mtl_reg_tex(100 + i, &t[7], NULL);
|
||||
uint64_t made = g_stand_made; atomic_fetch_add(&g_cur_frame, 1);
|
||||
id<MTLBuffer> b6 = table(0);
|
||||
int with = 0; for (int i = 0; i < MANY; i++) with += entry(b6, 100 + i) != dummy(b6);
|
||||
if (on) {
|
||||
CHECK(g_stand_made == made + STAND_FRAME && g_stand_todo_n == MANY - STAND_FRAME, "a frame makes its share of stand-ins, and the rest wait");
|
||||
// (b6's entries are written in place below, so the count is taken before)
|
||||
CHECK(with == STAND_FRAME, "the rest hold the dummy for now");
|
||||
atomic_fetch_add(&g_cur_frame, 1);
|
||||
mtl_sub_begin(); os_unfair_lock_lock(&g_rmu); pool_seen_scan(); os_unfair_lock_unlock(&g_rmu); // as the next submission's first pool draw does, before it asks for the table
|
||||
int all = 1; for (int i = 0; i < MANY; i++) all &= g_treg[100 + i].stand && entry(b6, 100 + i) == g_treg[100 + i].stand_rid;
|
||||
CHECK(all && !g_stand_todo_n && g_stand_made == made + MANY, "the next frame makes the rest");
|
||||
CHECK(snap()->buf == b6, "written into the table in place: the same buffer");
|
||||
CHECK(mtl_wait(mtl_sub_commit(), 5000000000ull), "the submission completed");
|
||||
CHECK(level_is(stand(100 + MANY - 1), 0, 16 * 16 * 4, 64, 0x82), "and they hold the texture's levels");
|
||||
} else CHECK(!with && !g_stand_made, "DM_NVN_POOL_STANDIN=0: the dummy");
|
||||
|
||||
for (int i = 0; i < MANY; i++) mtl_reg_tex(100 + i, NULL, NULL);
|
||||
for (int i = 10; i <= 18; i++) mtl_reg_tex(i, NULL, NULL);
|
||||
CHECK(!g_stand_live, "no stand-in is left when every ID is emptied");
|
||||
hog = nil;
|
||||
for (int n = 0; n < N; n++) mtl_tex_release(&t[n]);
|
||||
mtl_pool_del(mem); mtl_drain(); munmap(mem, LEN);
|
||||
fprintf(stderr, "t_pool_standin: %d checks, %d failed\n", checks, failed);
|
||||
return failed ? 1 : 0;
|
||||
} }
|
||||
@@ -89,6 +89,21 @@ int nvn_swizzle(const nvn_texdesc *d, unsigned level, unsigned layer, const void
|
||||
return move(d, level, layer, (uint8_t *)dst, 0, (uint8_t *)src, src_pitch, 0);
|
||||
}
|
||||
|
||||
// Every step-th block of every step-th block row of a level and layer (2D, one slice), from half a step in: the block in the middle of each step x step cell. nx x ny blocks, rows of
|
||||
// nx blocks in dst. A block lies in one 16-byte piece of the storage, so only block sizes of 1, 2, 4, 8 and 16 bytes (-1 for others, and what nvn_deswizzle refuses)
|
||||
int nvn_deswizzle_pick(const nvn_texdesc *d, unsigned level, unsigned layer, const void *src, uint32_t step, void *dst, uint32_t nx, uint32_t ny) {
|
||||
uint32_t row_bytes, rows, slices, bpb; uint64_t base; unsigned th, td;
|
||||
if (!step || nvn_deswizzle_geom(d, level, layer, &row_bytes, &rows, &slices, &base, &th, &td, &bpb) || slices != 1 || bpb > 16 || (bpb & (bpb - 1))) return -1;
|
||||
uint32_t lb = nvn_tex_level_gobs(d, level) * 64, bx = row_bytes / bpb; // lb: the row as laid out (move)
|
||||
if (lb < row_bytes) lb = row_bytes;
|
||||
const uint8_t *bl = (const uint8_t *)src + base; uint8_t *o = dst;
|
||||
for (uint32_t j = 0; j < ny; j++) {
|
||||
uint32_t y = j * step + step / 2; if (y >= rows) y = rows - 1;
|
||||
for (uint32_t i = 0; i < nx; i++, o += bpb) { uint32_t x = i * step + step / 2; if (x >= bx) x = bx - 1; memcpy(o, bl + nvn_bl_offset(x * bpb, y, 0, lb, rows, th, td), bpb); }
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// One 64 KB tile of a sparse texture's tiled level (2D, one slice). Such a level is laid out with its width padded to whole tiles (nvn_tex.c level_walk), and a tile is one block row of
|
||||
// 128 / block height GOB columns: 64 KB of its own, at the level's start + (ty × tiles across + tx) × 64 KB. So it decodes as a block-linear surface of that size; the part inside the level
|
||||
// goes to dst (block rows of pitch bytes). *wb and *rows: the bytes and block rows written (0 for a tile past the level)
|
||||
|
||||
@@ -11,6 +11,9 @@
|
||||
int nvn_deswizzle(const nvn_texdesc *d, unsigned level, unsigned layer, const void *src_storage, void *dst_linear, size_t dst_pitch);
|
||||
// Reverse (for unit test round trips): linear src_linear → written into the corresponding level / layer of the whole-texture storage dst_storage
|
||||
int nvn_swizzle(const nvn_texdesc *d, unsigned level, unsigned layer, const void *src_linear, size_t src_pitch, void *dst_storage);
|
||||
// Every step-th block of every step-th block row of a level and layer, the block in the middle of each step x step cell: nx x ny blocks into dst, rows of nx blocks (a small picture of a
|
||||
// level too large to read whole, for nvn_mtl.m's stand-ins). Block sizes of 1, 2, 4, 8 and 16 bytes; -1 for others and for what nvn_deswizzle does not support
|
||||
int nvn_deswizzle_pick(const nvn_texdesc *d, unsigned level, unsigned layer, const void *src_storage, uint32_t step, void *dst_linear, uint32_t nx, uint32_t ny);
|
||||
// One 64 KB tile (tx, ty) of a sparse texture's tiled level, given as its own 64 KB, into dst (block rows of dst_pitch bytes): *wb bytes of *rows block rows, the part inside the level
|
||||
int nvn_deswizzle_tile(const nvn_texdesc *d, unsigned level, uint32_t tx, uint32_t ty, const void *tile, void *dst_linear, size_t dst_pitch, uint32_t *wb, uint32_t *rows);
|
||||
// nvn_deswizzle from storage given from byte src_at of the whole texture's on (src points at that byte); the level and layer must lie at or after it
|
||||
|
||||
@@ -246,6 +246,38 @@ int main(void) {
|
||||
(unsigned long long)nvn_tex_level_offset(&d, 1), (unsigned long long)L.size);
|
||||
}
|
||||
|
||||
// ---------- P pick: every step-th block of every step-th block row (nvn_deswizzle_pick), against the level decoded whole, in both GOB orders ----------
|
||||
printf("P pick (every step-th block, against the whole level)\n");
|
||||
for (int sw2 = 0; sw2 < 2; sw2++) {
|
||||
nvn_gob_switch2 = sw2;
|
||||
static const struct { int target, fmt; uint32_t w, h, depth, levels; unsigned level, layer; uint32_t step; } c[] = {
|
||||
{1, 0x4D, 1048, 1088, 1, 1, 0, 0, 17}, {4, 0x50, 524, 544, 3, 1, 0, 2, 9}, {1, 0x42, 256, 256, 1, 9, 1, 0, 4}, {1, 0x25, 64, 40, 1, 1, 0, 0, 2}, {1, 0x25, 200, 320, 1, 3, 2, 0, 3}, {4, 0x4B, 2448, 3056, 2, 1, 0, 1, 48}};
|
||||
for (unsigned k = 0; k < sizeof c / sizeof c[0]; k++) {
|
||||
nvn_texdesc d = {c[k].target, c[k].fmt, 0, 0, c[k].w, c[k].h, c[k].depth, c[k].levels, 0, 0};
|
||||
nvn_texlayout L; nvn_tex_layout(&d, &L);
|
||||
uint8_t *st = malloc(L.size); for (uint64_t i = 0; i < L.size; i++) st[i] = rnd();
|
||||
uint32_t rb, rows, sl; dims(&d, c[k].level, &rb, &rows, &sl);
|
||||
int bw, bh, bpb = nvn_fmt_bpb(d.format, &bw, &bh); uint32_t bx = rb / (uint32_t)bpb, nx = bx / c[k].step, ny = rows / c[k].step;
|
||||
uint8_t *lin = malloc((size_t)rb * rows), *got = malloc((size_t)nx * ny * (size_t)bpb + 16); memset(got + (size_t)nx * ny * (size_t)bpb, 0xAB, 16);
|
||||
CHECK(!nvn_deswizzle(&d, c[k].level, c[k].layer, st, lin, rb), "P case %u decode failed", k);
|
||||
CHECK(!nvn_deswizzle_pick(&d, c[k].level, c[k].layer, st, c[k].step, got, nx, ny), "P case %u pick failed", k);
|
||||
int ok = 1;
|
||||
for (uint32_t j = 0; j < ny; j++) for (uint32_t i = 0; i < nx; i++)
|
||||
ok &= !memcmp(got + ((size_t)j * nx + i) * (size_t)bpb, lin + (size_t)(j * c[k].step + c[k].step / 2) * rb + (size_t)(i * c[k].step + c[k].step / 2) * (size_t)bpb, (size_t)bpb);
|
||||
for (int i = 0; i < 16; i++) ok &= got[(size_t)nx * ny * (size_t)bpb + (size_t)i] == 0xAB;
|
||||
CHECK(ok && nx && ny, "P case %u (GOB order %d): %ux%u format %#x level %u layer %u step %u: a picked block is not the level's", k, sw2, c[k].w, c[k].h, c[k].fmt, c[k].level, c[k].layer, c[k].step);
|
||||
free(st); free(lin); free(got);
|
||||
}
|
||||
}
|
||||
nvn_gob_switch2 = 0;
|
||||
{ // refused: a block size that is not 1, 2, 4, 8 or 16 bytes (RGB8), a 3D texture, step 0
|
||||
uint8_t st[4096] = {0}, out[64];
|
||||
nvn_texdesc rgb = {1, 0x19, 0, 0, 8, 8, 1, 1, 0, 0}, vol = {2, 0x25, 0, 0, 8, 8, 4, 1, 0, 0}, ok8 = {1, 0x25, 0, 0, 8, 8, 1, 1, 0, 0};
|
||||
int bw, bh; CHECK(nvn_fmt_bpb(0x19, &bw, &bh) == 3, "P format 0x19 (RGB8) is expected to be 3 bytes a texel");
|
||||
CHECK(nvn_deswizzle_pick(&rgb, 0, 0, st, 2, out, 4, 4) == -1 && nvn_deswizzle_pick(&vol, 0, 0, st, 2, out, 4, 4) == -1 && nvn_deswizzle_pick(&ok8, 0, 0, st, 0, out, 4, 4) == -1
|
||||
&& !nvn_deswizzle_pick(&ok8, 0, 0, st, 2, out, 4, 4), "P pick refuses a 3-byte block, a 3D texture and step 0, and takes RGBA8");
|
||||
}
|
||||
|
||||
printf("%s: %d/%d checks passed\n", fails ? "RED" : "all passed", checks - fails, checks);
|
||||
return fails ? 1 : 0;
|
||||
}
|
||||
@@ -668,6 +668,7 @@ One line per symptom in the [subsystem references](../reference/): what you see
|
||||
- the process ends at once (the window vanishes), with `-[MTLTextureDescriptorInternal validateWithDevice:]` failing an assertion such as `requests 9 mipmap levels, but the dimensions (1, 1, 1) can only support 1`, or `sampleCount must be > 1 for multisample textures`. → [Texture fields Metal refuses](../reference/rendering.md#texture-fields-metal-refuses)
|
||||
- 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)
|
||||
- ≈13 MB/frame leak. → [Transient render targets shared by storage](../reference/rendering.md#transient-render-targets-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)
|
||||
- a surface that Unreal draws from a runtime virtual texture (a landscape floor) stays black; its BC tile caches read all zero in a texture dump, and nothing writes them on the GPU or the CPU; no red line. → [Copies into an uncompressed alias of a compressed texture](../reference/rendering.md#copies-into-an-uncompressed-alias-of-a-compressed-texture)
|
||||
|
||||
@@ -177,6 +177,8 @@ DM_NVN_PASSDUMP_ALL diagnostic `=1` makes `DM_NVN_PASSDUMP` record every draw an
|
||||
DM_NVN_PASSGPU diagnostic `=<file>` writes the GPU time of each render and compute pass from stage-boundary timestamp counters (off under Metal 4); also gives each dispatch its own encoder
|
||||
DM_NVN_PASSGPU_SERIAL diagnostic `=1` makes each encoder on the main command buffer wait until all earlier work has finished, so `DM_NVN_PASSGPU` times read each pass's exclusive time (totals read high)
|
||||
DM_NVN_POOL_CHUNKS switch `0` wraps a memory pool of 512 MB or more whole in one buffer, as before; by default it is wrapped in overlapping 16 MB chunks made as draws reach them, so only the parts in use are wired
|
||||
DM_NVN_POOL_FIRST diagnostic `1` logs every first read through the pool table of a texture ID whose texture was not made yet (up to 32 a frame): what the read found (the ID's stand-in, or the dummy, which reads 0), the texture, its view and how many frames before it was registered; it also checks at each such read whether the storage the stand-in was made from has been written since. The totals go to the log every 300 frames without it (`pool stand-ins up to frame`)
|
||||
DM_NVN_POOL_STANDIN switch `0` leaves the dummy (reads 0) in the pool tables for a texture the walk does not make, as before; by default, once the walk's budget is full, such a texture holds a stand-in until a shader reads its ID: its own smallest levels, or its last level cut down to every n-th block, so the first read draws the texture's colours coarse for one frame instead of black (Ocarina of Time's cliff beside the castle town wall dark for one frame when the game streams a sharper texture into it, issue #1969)
|
||||
DM_NVN_PREDICT switch `0` turns off the parallel precompile look-ahead: a pipeline miss compiles only the pipeline it needs
|
||||
DM_NVN_PREFETCH switch `0` turns off loading a program's shader libraries in the background when the game makes it (`nvnProgramSetShaders`); they load at its first draw instead
|
||||
DM_NVN_PROF diagnostic `=1` wraps the implemented NVN functions in a timing wrapper and collects the time each takes during recording
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 19 and column 104.
|
@@ -304,12 +304,20 @@ The replay on Metal (`dm/gfx/nvn/nvn_mtl.m`) and the Vulkan and OpenGL layers. E
|
||||
|
||||
- **Symptom:** 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.
|
||||
- **Cause:** the models' shaders read their textures through the bindless pool table (`pool_gpu`). When a walk writes an ID's entry (`pool_entry`) it makes the ID's Metal texture if it fits the walk's room (`pool_room`, within `pool_budget()`), or, whatever the room, if a shader has read the ID (`pool_seen`, under `POOL_REQ_BYTES`); otherwise the entry is the dummy (black), and `pool_seen_scan` makes the texture after the first read. In Ocarina of Time the room is always 0: `pool_budget()` is a twentieth of Metal's working set (909 MB on a 24 GB Mac) against 15-18 GB of `currentAllocatedSize`, so only read IDs are made ahead. `mtl_reg_tex` cleared the mark whenever the game registered another texture into the ID, so when the game's streaming replaced the 17 textures of a material set at once (3-7 MB each, finalizing the old texture first; each step registered twice, 4-5 frames apart), the replacements counted as unread and their first reads found the dummy, one frame per registration.
|
||||
- **Fix:** the read mark is the ID's, not its texture's: it stays while the ID holds a texture and is cleared only when the ID is emptied, so the next walk makes a replacement under the read budget. A render target no pass has written stays the dummy all the same (`pool_unwritten_target`), which is what kept transient targets from being made ahead. A texture streamed into an ID no shader has read yet still reads the dummy once: a single dark frame, not a pair (issue #1969).
|
||||
- **Fix:** the read mark is the ID's, not its texture's: it stays while the ID holds a texture and is cleared only when the ID is emptied, so the next walk makes a replacement under the read budget. A render target no pass has written stays the dummy all the same (`pool_unwritten_target`), which is what kept transient targets from being made ahead. A texture streamed into an ID no shader has read yet is not covered by this: its first read finds the ID's stand-in ([next entry](#a-streamed-textures-first-read-drew-its-surface-dark-for-one-frame)).
|
||||
- **Check:** a replay of a route past the castle town wall, counting single-frame dips of a per-frame luminance grid, each checked by picture: 4-5 flagged per run before (2-4 at the wall's streaming steps), 0 after (PR #2022); `t_pool_chunks` checks which registrations keep the mark, `t_pool_seen_replace` the replacement's pixels in the first table with the walk's room at 0 (a live replacement, one after Finalize, one for an ID whose texture waited for the read budget).
|
||||
- **Found on:** [The Legend of Zelda: Ocarina of Time](../../dm/games/0400486024DEC000/README.md) (a player's recording, M4 Pro, macOS 27.0.1).
|
||||
|
||||
## Render targets, passes and clears
|
||||
|
||||
### A streamed texture's first read drew its surface dark for one frame
|
||||
|
||||
- **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.
|
||||
- **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).
|
||||
|
||||
### Transient render targets shared by storage
|
||||
|
||||
- **Symptom:** ≈13 MB/frame leak.
|
||||
|
||||
Reference in new issue
Block a user