Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 41 additions & 7 deletions dm/gfx/nvn/nvn_mtl.m
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@
while (j < g_naddr && g_addr[j].lo <= hi) { lo = MIN(lo, g_addr[j].lo); hi = MAX(hi, g_addr[j].hi); j++; }
if (i == j && g_naddr == g_addr_cap) { g_addr_cap = g_addr_cap ? g_addr_cap * 2 : 256; g_addr = realloc_or_die(g_addr, (size_t)g_addr_cap * sizeof *g_addr, "the addressed range table"); }
if (j != i + 1) memmove(g_addr + i + 1, g_addr + j, (size_t)(g_naddr - j) * sizeof *g_addr);
g_naddr += 1 - (j - i); g_addr[i].lo = lo; g_addr[i].hi = hi;

Check failure on line 591 in dm/gfx/nvn/nvn_mtl.m

View workflow job for this annotation

GitHub Actions / clang-tidy (ratchet)

Out of bound access to memory preceding the heap area [clang-analyzer-security.ArrayBound] (3 in this file, the baseline has 2)
atomic_fetch_add(&g_pool_gen, 1); // the table rebuilds before the next draw that uses it
}
pthread_mutex_unlock(&g_mu);
Expand Down Expand Up @@ -949,6 +949,7 @@
if (!g_warm_started && prepare_on()) { mtl_init(); shmap_load(); }
}
static void sub_cut_begin(void); static void ts_sub_begin(void), ts_boundary(void); static int ts_on(void);
static void gmem_commit_patch(void); // the raw pool table words of the arguments encoded so far follow the pools (gmem_slot_note)
void mtl_sub_begin(void) {
mtl_init(); g_sub_serial++; g_seg = 0; seg_clk_put(g_sclk); g_sclk = NULL; g_rc_npend = 0; g_rc_skip = 0;
sub_cut_begin();
Expand Down Expand Up @@ -1796,6 +1797,7 @@
g_ts_pend = -1; // nothing after it: the last segment's end is the command buffer's own (rep_ticks)
if (g_blit) { enc_b_end(g_blit); g_blit = nil; }
up_flush();
gmem_commit_patch();
g_cut_copy = 0;
treg_finish_submission(); // Late descriptor views retire behind this submission's fence.
uint64_t fence = ++g_fence;
Expand Down Expand Up @@ -2099,7 +2101,7 @@
}
static void pkg_share_put(const tex_t *t, NvPkg *pk) {
alias_key k = tex_alias_key(t); NvPkgRef *r = [NvPkgRef new]; r->pkg = pk;
pthread_mutex_lock(&g_smu); hm_put_id(&g_pkg_share, &k, r); pthread_mutex_unlock(&g_smu);

Check failure on line 2104 in dm/gfx/nvn/nvn_mtl.m

View workflow job for this annotation

GitHub Actions / clang-tidy (ratchet)

Potential leak of an object stored into 'r' [clang-analyzer-osx.cocoa.RetainCount] (2 in this file, the baseline has 1)
}
// A sparse texture's CPU-write check walks its page table (nvn_sparse_since). A submission sees the CPU writes and the mappings made before it (as NVN's: a map is a CPU call,
// not a command), so the replay checks each one once a submission (sub: the submission, g_sub_serial, of its last check). Games remap streamed pages all the time, so the
Expand Down Expand Up @@ -3586,6 +3588,8 @@
MLOG("GPU allocation growth by function: pass end %llu MB, pass begin %llu MB, pipeline fetch %llu MB, argument buffers %llu MB, texture fetch %llu MB", g_ap_fn[0] >> 20, g_ap_fn[1] >> 20, g_ap_fn[2] >> 20, g_ap_fn[3] >> 20, g_ap_fn[4] >> 20); }
if (g_allocprof > 0) { int top[4] = {-1, -1, -1, -1}; for (int o = 0; o < 64; o++) for (int k = 0; k < 4; k++) if (top[k] < 0 || g_ap_bytes[o] > g_ap_bytes[top[k]]) { for (int j = 3; j > k; j--) top[j] = top[j - 1]; top[k] = o; break; }
MLOG("GPU allocation growth by op: op%d %llu MB, op%d %llu MB, op%d %llu MB, op%d %llu MB", top[0], g_ap_bytes[top[0]] >> 20, top[1], g_ap_bytes[top[1]] >> 20, top[2], g_ap_bytes[top[2]] >> 20, top[3], g_ap_bytes[top[3]] >> 20); }
{ void mtl_gmem_words(uint64_t *, uint64_t *, uint64_t *); uint64_t gw, go, gb; mtl_gmem_words(&gw, &go, &gb);
if (gw) MLOG("raw pool table words at commit: %llu, of them %llu held an older table than the current one and %llu the current one behind the pools", (unsigned long long)gw, (unsigned long long)go, (unsigned long long)gb); }
}
void mtl_pkg_stats(uint64_t *uploads, uint64_t *reuploads) { *uploads = g_pkg_uploads; *reuploads = g_pkg_reuploads; }
uint64_t mtl_pkg_edge_skips(void) { return g_pkg_edge_skips; }
Expand Down Expand Up @@ -8354,6 +8358,32 @@
return b;
}
static uint64_t gmem_map_gpu(void) { return gmem_map().gpuAddress; }
// An argument block holds the pool table that was current when it was encoded. A pool made after that, later in the submission or on another thread while it is encoded, is not in it, and a raw
// read of that pool gets 0: the failure that hung agl's task shaders (docs/reference/shaders.md, More memory pools than the raw pool table had entries). The GPU has not read the blocks of the
// command buffer yet, so at each commit, whole or segment, their table words are pointed at a table of the pools as they are then (as #1407 does). A new table is built when the pools changed,
// not refilled in place, as earlier segments may be reading the old one. A block's write log is a bitmap of the entries of the table the shader searches: its word follows its table, and the log is
// read against that table (NvGmlLog), else a bit of the newer table would be decoded against the older one. Counted in the memory report: the words, those that held an older table than the
// current one, and those that held the current one behind the pools
@interface NvGmlLog : NSObject { @public id<MTLBuffer> b, map; } @end // a log and the table its bits index, as gml_read reads them: gmem_commit_patch may change the table before the GPU starts
@implementation NvGmlLog @end
static __unsafe_unretained NvGmlLog *g_gml_last; // gml_take's: the submission's commit blocks hold it until it completes
typedef struct { uint64_t *w; void *log; } gmem_slot; // a block's table word, and the NvGmlLog of its write log (NULL: the shader has none)
static gmem_slot *g_gmem_slots; static int g_gmem_nslots, g_gmem_cslots; static uint64_t g_gmem_words, g_gmem_older, g_gmem_behind;
static void gmem_slot_note(uint64_t *w, void *log) {
if (g_gmem_nslots == g_gmem_cslots) { g_gmem_cslots = g_gmem_cslots ? 2 * g_gmem_cslots : 256; g_gmem_slots = realloc_or_die(g_gmem_slots, (size_t)g_gmem_cslots * sizeof *g_gmem_slots, "the raw pool table's word list"); }
g_gmem_slots[g_gmem_nslots++] = (gmem_slot){w, log};
}
void mtl_gmem_words(uint64_t *words, uint64_t *older, uint64_t *behind) { *words = g_gmem_words; *older = g_gmem_older; *behind = g_gmem_behind; }
static void gmem_commit_patch(void) {
int n = g_gmem_nslots; g_gmem_nslots = 0; if (!n || !g_gmem_buf) return;
uint64_t was = g_gmem_buf.gpuAddress; int lag = atomic_load(&g_pool_gen) != g_gmem_gen;
id<MTLBuffer> map = gmem_map(); uint64_t cur = map.gpuAddress;
g_gmem_words += (uint64_t)n;
for (int i = 0; i < n; i++) { const gmem_slot *k = &g_gmem_slots[i]; if (*k->w == cur) continue;
if (*k->w != was) g_gmem_older++; else if (lag) g_gmem_behind++;
*k->w = cur;
if (k->log) ((__bridge NvGmlLog *)k->log)->map = map; }
}
// Where those stores land is known only once the GPU has run them, while the host's uniform shadows and its CPU reads of guest memory must not miss a GPU write (sbw / gw). Counted as a
// write anywhere, they refused every shadow of the submission and of the next ones: Donkey Kong Bananza's tunnel ran at 70 ms a frame, 4,533 passes a frame ended to land deferred updates.
// So each store also sets the bit of its pool table entry in a write log (DMSH_KIND_GMEM_LOG, a bitmap), each draw or dispatch with such stores gets its own log from a small pool, and the
Expand Down Expand Up @@ -8421,7 +8451,8 @@
if (!b) { b = [g_dev newBufferWithLength:GML_BYTES options:MTLResourceStorageModeShared]; res_add(b); res_commit(); }
memset(b.contents, 0, GML_BYTES);
g_gml_on = 1; uint64_t fence = g_fence + 1; // this submission's fence (mtl_sub_commit)
[g_commit_blocks addObject:^{ gml_read(b, map, predict, fence); }];
NvGmlLog *log = [NvGmlLog new]; log->b = b; log->map = map; g_gml_last = log;
[g_commit_blocks addObject:^{ gml_read(log->b, log->map, predict, fence); }];
return b.gpuAddress;
}
static id<MTLBuffer> zero_bank(void) { static __strong id<MTLBuffer> b; if (!b) { b = [g_dev newBufferWithLength:65536 options:MTLResourceStorageModeShared]; res_add(b); } return b; }
Expand Down Expand Up @@ -8605,6 +8636,7 @@
return 1;
}
NSUInteger arg_len = sh->arg_len ? sh->arg_len : sh->ae.encodedLength;
int gmem_aid = -1; id<MTLBuffer> raw_map = nil; int gml_taken = 0; // the slot that holds the raw pool table (gmem_slot_note), and the table this block's log and table slot share
uint64_t slots[NVN_REFL_MAXARG]; memset(slots, 0, arg_len < sizeof slots ? arg_len : sizeof slots); // only the bytes that are copied out (they used to be zeroed in full, before the reuse check too)
#define USE(r, u) do { (void)(u); if (__builtin_expect(g_res_check_on, 0)) res_check((r), sh->key, reg); } while (0)
// DM_NVN_PASSDUMP: on the first new encode per pass per stage, records the shader key (K) and the actual source of each binding (B: UBO address / length / first 4 floats, texture ID and serial, or "dummy")
Expand Down Expand Up @@ -8797,11 +8829,11 @@
id<MTLBuffer> cb = sh->c1buf ? sh->c1buf : zero_bank(); // c1 has length 0 (600 of them in the whole library) yet the shader still declares it: reads 0
slots[aid] = (sh->c1buf ? sh->c1_gpu : zero_bank_gpu()) + (sh->c1buf ? sh->c1off : 0);
USE(cb, MTLResourceUsageRead);
} else if (b->kind == NVN_RB_GMEM) { // B: the memory pool table for addresses traced to no storage buffer (gmem_map_gpu)
slots[aid] = gmem_map_gpu();
if (bl) PD("B %llu st%d gmem %s", g_stat_pass, nvn_stage, b->rw ? "rw" : "ro");
} else if (b->kind == NVN_RB_GMEML) { // B: the write log of those stores; their writes are noted as gml_take learned them
slots[aid] = gml_take(gmem_map()); // the table of this encode: the pool table's binding made it just before or just after, under the same g_pool_gen unless a pool came in between (a bit then names an entry of the newer table, one more reason a late pool is checked)
} else if (b->kind == NVN_RB_GMEM || b->kind == NVN_RB_GMEML) { // B: the memory pool table for addresses traced to no storage buffer (gmem_map), and the write log of those stores (gml_take)
if (!raw_map) raw_map = gmem_map(); // the log and the shader use the same table even if a pool comes in during the encode
slots[aid] = b->kind == NVN_RB_GMEM ? raw_map.gpuAddress : gml_take(raw_map);
if (b->kind == NVN_RB_GMEM) gmem_aid = aid; else gml_taken = 1;
if (bl && b->kind == NVN_RB_GMEM) PD("B %llu st%d gmem %s", g_stat_pass, nvn_stage, b->rw ? "rw" : "ro");
} else if (b->kind == NVN_RB_SUPPORT) { // support_buffer: scale 1.0, BGRA swap 0, viewport transform not disabled (viewport fields are not read)
slots[aid] = support_gpu();
USE(support_buf(), MTLResourceUsageRead);
Expand Down Expand Up @@ -8875,7 +8907,8 @@
#undef USE
os_unfair_lock_unlock(&g_rmu);
id<MTLBuffer> ab; size_t off = arena_alloc(arg_len, &ab);
memcpy(g_cur_arena->cpu + off, slots, arg_len); // the arena arena_alloc took it from (no contents message: see ubo_read)
memcpy(g_cur_arena->cpu + off, slots, arg_len);
if (gmem_aid >= 0) gmem_slot_note((uint64_t *)(void *)(g_cur_arena->cpu + off) + gmem_aid, gml_taken ? (__bridge void *)g_gml_last : NULL); // the arena arena_alloc took it from (no contents message: see ubo_read)
bind_args(en, nvn_stage, metal_vertex, ab, g_cur_arena->gpu + off, off, sh->rf.abi);
sh->ab_buf = ab; sh->ab_gpu = g_cur_arena->gpu + off; sh->ab_off = off; sh->ab_pass = g_pass.serial; sh->ab_binst = g_draw_binst; sh->ab_pgen = g_psnap_gen; g_args_dirty[nvn_stage] = 0;
g_stat_args_enc++;
Expand Down Expand Up @@ -12514,6 +12547,7 @@
if (cb.error) { atomic_store(&g_last_commit_error, 1); static int k; if (k++ < 8) MLOG("[RED] segment command buffer execution error: %s", cb.error.description.UTF8String); res_top_once(cb.error); }
char where[64]; snprintf(where, sizeof where, "fence %llu, segment %u", (unsigned long long)cbs_fence, k); cbs_feedback(cbs, cb.error, where);
}];
gmem_commit_patch();
res_commit();
[g_cb endCommandBuffer];
rec_commit(g_cb, o);
Expand Down
5 changes: 5 additions & 0 deletions dm/gfx/tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ check_t_tex_fit_root: t_tex_fit_root
MTL_DEBUG_LAYER=1 ./t_tex_fit_root
t_pool_chunks: t_pool_chunks.m $(MTL_T_DEP)
clang $(CFLAGS) -fobjc-arc -o $@ t_pool_chunks.m $(MTL_T_SRC) $(FW)
t_gmem_words: t_gmem_words.m $(MTL_T_DEP)
clang $(CFLAGS) -fobjc-arc -o $@ t_gmem_words.m $(MTL_T_SRC) $(FW)
.PHONY: check_t_gmem_words
check_t_gmem_words: t_gmem_words
MTL_DEBUG_LAYER=1 ./t_gmem_words
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)
Expand Down
69 changes: 69 additions & 0 deletions dm/gfx/tests/t_gmem_words.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// The raw pool table words of the argument blocks encoded so far follow the pools at commit (nvn_mtl.m gmem_slot_note, gmem_commit_patch): a pool made after a block was encoded is in the
// table its word points at when the GPU reads it, a word behind the pools or holding an older table is rewritten and counted, and a word that is current is left alone.
#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(label, c) do { checks++; if (!(c)) { failed++; fprintf(stderr, "FAIL: %s (line %d)\n", label, __LINE__); } } while (0)
// Whether the table the GPU address names has an entry that starts at the guest address (every table is the current one's or an older, still alive one)
static int table_has(uint64_t table_gpu, uint64_t guest) {
if (!g_gmem_buf || g_gmem_buf.gpuAddress != table_gpu) return 0;
const uint8_t *t = g_gmem_buf.contents;
for (unsigned k = 1, n = (unsigned)(g_gmem_buf.length / DMSH_GMEM_MAP_ENTRY); k < n; k++) { uint64_t lo; memcpy(&lo, t + (size_t)k * DMSH_GMEM_MAP_ENTRY, sizeof lo); if (lo == guest) return 1; }
return 0;
}
int main(void) { @autoreleasepool {
mtl_init();
const uint64_t MB = 1ull << 20;
uint8_t *mem = mmap(NULL, 64 * MB, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); if (mem == MAP_FAILED) abort();
uint64_t a = (uint64_t)(uintptr_t)mem;
mtl_pool_add(mem, 4 * MB); mtl_pool_addressed(mem, 4 * MB);
mtl_sub_begin();
uint64_t t0 = gmem_map_gpu(); // the table two argument blocks were encoded with
uint64_t w0 = t0, w1 = t0, w2 = 0;
uint64_t words0, older0, behind0; mtl_gmem_words(&words0, &older0, &behind0);
gmem_slot_note(&w0, NULL); gmem_slot_note(&w1, NULL);
gmem_commit_patch();
uint64_t words, older, behind; mtl_gmem_words(&words, &older, &behind);
CHECK("an unchanged table leaves the words alone", w0 == t0 && w1 == t0 && g_gmem_buf.gpuAddress == t0);
CHECK("two words counted, none out of date", words - words0 == 2 && older == older0 && behind == behind0);
mtl_pool_add(mem + 8 * MB, 4 * MB); mtl_pool_addressed(mem + 8 * MB, 4 * MB); // a pool made after the blocks were encoded
CHECK("the table the words hold lacks it", !table_has(t0, a + 8 * MB));
gmem_slot_note(&w0, NULL); gmem_slot_note(&w1, NULL);
gmem_commit_patch();
uint64_t t1 = g_gmem_buf.gpuAddress;
CHECK("a new table was built, not the old one refilled", t1 != t0);
CHECK("both words point at it", w0 == t1 && w1 == t1);
CHECK("it holds the pool made after they were encoded", table_has(t1, a + 8 * MB));
mtl_gmem_words(&words, &older, &behind);
CHECK("two words counted as behind the pools", behind - behind0 == 2 && older == older0);
mtl_pool_add(mem + 16 * MB, 4 * MB); mtl_pool_addressed(mem + 16 * MB, 4 * MB);
uint64_t t2 = gmem_map_gpu(); // another block is encoded with the newer table, then the pools change again
mtl_pool_add(mem + 24 * MB, 4 * MB); mtl_pool_addressed(mem + 24 * MB, 4 * MB);
w2 = t1; // a block encoded with the older table
uint64_t w3 = t2; // and one with the table that now lags the pools
gmem_slot_note(&w2, NULL); gmem_slot_note(&w3, NULL);
gmem_commit_patch();
uint64_t t3 = g_gmem_buf.gpuAddress;
CHECK("the words follow the last table", w2 == t3 && w3 == t3 && t3 != t2);
CHECK("it holds all four added pools", table_has(t3, a + 8 * MB) && table_has(t3, a + 16 * MB) && table_has(t3, a + 24 * MB));
mtl_gmem_words(&words, &older, &behind);
CHECK("one word counted as older, one as behind", older - older0 == 1 && behind - behind0 == 3);
gmem_commit_patch(); // nothing noted since
CHECK("a commit with no words changes nothing", g_gmem_buf.gpuAddress == t3);
// a write log is read against the table the shader searched: when its block's word is repointed at a newer table, the log follows it
id<MTLBuffer> t3buf = g_gmem_buf;
uint64_t w4 = t3; (void)gml_take(t3buf); NvGmlLog *lg = g_gml_last;
CHECK("a log is taken with the table its block holds", lg && lg->map == t3buf);
mtl_pool_add(mem + 32 * MB, 4 * MB); mtl_pool_addressed(mem + 32 * MB, 4 * MB); // a pool the block's table lacks
gmem_slot_note(&w4, (__bridge void *)lg);
gmem_commit_patch();
CHECK("its word follows the newer table", w4 == g_gmem_buf.gpuAddress && w4 != t3);
CHECK("and its log is read against that table, not the older one", lg->map == g_gmem_buf && lg->map != t3buf);
CHECK("submission completes", mtl_wait(mtl_sub_commit(), 5000000000ull));
CHECK("GPU has no submission error", !mtl_last_commit_error());
printf("t_gmem_words: %d checks, %d failed\n", checks, failed); return failed != 0;
} }
1 change: 1 addition & 0 deletions docs/agents/symptoms.md
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,7 @@ One line per symptom in the [subsystem references](../reference/): what you see
- geometry preparation dominates rendering CPU even after geometry passes have been batched; primitive assembly and capture-index deduplication repeat for unchanged meshes. → [Prepared geometry indices reused after byte comparison](../reference/shaders.md#prepared-geometry-indices-reused-after-byte-comparison)
- geometry vertex-capture passes consume substantial GPU time before geometry compute or depth rasterization. → [Geometry capture initializes only missing words](../reference/shaders.md#geometry-capture-initializes-only-missing-words)
- Apple's performance HUD (`DM_METAL_HUD=1`) says `Compiled 2 shaders. Total compilation time is 102.70ms. Consider adopting MTL4Compiler for more fine grained compilations` during play, with a dip in the frame-rate graph at that moment, for example when a new room first issues a scissored clear. → [The host's own shaders compiled from source at run time](../reference/shaders.md#the-hosts-own-shaders-compiled-from-source-at-run-time)
- a raw global memory read of a memory pool made after the shader's arguments were encoded gets 0, though the pool exists when the GPU runs it; a task shader that reads both ends of a range this way loops `(0 - 1) >> 5` times and hangs the GPU. The memory report's `raw pool table words at commit` line counts the words that were out of date. → [Raw pool table words that predate the pools](../reference/shaders.md#raw-pool-table-words-that-predate-the-pools)

## Game state

Expand Down
Loading
Loading