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
4 changes: 3 additions & 1 deletion dm/gfx/nvn/dmsh_abi.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@
// Raw global memory: the translator reserves that table as a storage buffer under this constant buffer slot, which no game has (Ryujinx GlobalToStorage.GmemMapCbSlot), finds the pool
// holding an address by a binary search over the table's starts and accesses the address plus the pool's delta through a physical storage buffer pointer
#define DMSH_GMEM_MAP_CB_SLOT 0xFF
#define DMSH_GMEM_MAP_ENTRIES 4096 // entries of the table (GlobalToStorage.GmemMapLog2 = 12), sorted by start: entry 0 is empty (start 0, end 0) and unused entries start at ~0
// The table is sorted by start: entry 0 is empty (start 0, end 0) and unused entries start at ~0. The host gives it a power of two entries, as many as its pools need and at least
// DMSH_GMEM_MAP_ENTRIES, and writes that count into entry 0's delta (low word, GlobalToStorage.GmemMapCountWord), which the shader's binary search takes
#define DMSH_GMEM_MAP_ENTRIES 4096 // the least count of entries
#define DMSH_GMEM_MAP_ENTRY 32 // bytes of an entry: start, end (exclusive) and delta (the pool's Metal GPU address minus its guest address), 64 bits each, then 8 bytes of padding
// Where such a store or atomic lands is known only when the GPU runs it, so each one also puts its guest page (address >> 12) in a write log, reserved under the same slot at offset 1
// (Ryujinx GlobalToStorage.GmemLogCbOffset, GmemLogLog2): 32-bit words, word 0 set when a page found no free slot, then a hash set of 1 << DMSH_GMEM_LOG_LOG2 pages (0 = empty).
Expand Down
2 changes: 1 addition & 1 deletion dm/gfx/nvn/dmsh_chain.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Generated file (dm/gfx/tools/dmsh_chain.py --header): the B shader library translation chain version this source tree expects (default options), one for every game;
// the host loads only <library dir>/b-<V>/. Run dmsh_chain.py --header after changing the chain's tools and commit this file with the change
#define DMSH_CHAIN "1d53805bef4f"
#define DMSH_CHAIN "6fe832f2db50"
28 changes: 20 additions & 8 deletions dm/gfx/nvn/nvn_mtl.m
Original file line number Diff line number Diff line change
Expand Up @@ -8032,31 +8032,43 @@ static void bind_args(id<MTLCommandEncoder> en, int nvn_stage, int metal_vertex,
// Layout (dmsh_abi.h DMSH_GMEM_MAP_*): sorted by start, entry 0 empty, then the pools as disjoint ranges (a pool overlapping an earlier one keeps only its part past that one's end: every
// pool wraps the guest's own pages, so any pool holding an address gives its GPU address), unused entries starting at ~0. Rebuilt when the pools change; the replaced table goes through
// deferred release, so submissions encoded with it keep it. Every pool is made resident, as the shader may reach any of them
// As many entries as the pools need: a power of two of at least DMSH_GMEM_MAP_ENTRIES, the count in entry 0's delta, where the shader's binary search starts. The table used to have
// 4,096 entries whatever the pools: loading a save in Ocarina of Time made up to 4,522 pools, the ones past the last entry were left out, and a task shader that read both ends of a
// range through them as 0 looped (0 - 1) >> 5 times, a GPU hang (docs/reference/shaders.md, More memory pools than the raw pool table had entries)
enum { GMEM_MAP_MAX = 1 << 20 }; // entries at most (a 32 MB table, if ever a game had that many pools); pools past them are still left out, with a red line
static id<MTLBuffer> g_gmem_buf; static uint64_t g_gmem_gen;
static uint64_t gmem_map_gpu(void) {
uint64_t gen = atomic_load(&g_pool_gen);
if (g_gmem_buf && g_gmem_gen == gen) return g_gmem_buf.gpuAddress;
id<MTLBuffer> b = [g_dev newBufferWithLength:DMSH_GMEM_MAP_ENTRIES * DMSH_GMEM_MAP_ENTRY options:MTLResourceStorageModeShared];
uint8_t *t = (uint8_t *)b.contents; memset(t, 0, b.length);
for (int k = 1; k < DMSH_GMEM_MAP_ENTRIES; k++) memset(t + (size_t)k * DMSH_GMEM_MAP_ENTRY, 0xff, 8);
int n = 1, over = 0; uint64_t end = 0;
int n = 1, over = 0; uint64_t end = 0, over_lo = 0, over_bytes = 0;
pthread_mutex_lock(&g_mu);
for (int k = 0; k < g_nbig; k++) if (!g_big[k].full) { int n = big_new(&g_big[k], g_big[k].a, g_big[k].b, g_big[k].b, 0); if (n >= 0) g_big[k].full = n + 1; } // the pool table reaches all of a large pool
gen = atomic_load(&g_pool_gen); pidx_sort();
uint64_t (*rng)[3] = malloc(((size_t)g_npool + 1) * sizeof *rng); if (!rng) abort(); // the ranges (start, end, delta), taken under g_mu and written into the table after it
for (int k = 0; k < g_npool; k++) {
pool_ent *p = &g_pools[g_pidx[k]];
if (!p->res) { res_add(p->buf); p->res = 1; g_pool_res_n++; }
uint64_t lo = MAX((uint64_t)p->lo, end), hi = p->hi;
if (lo >= hi) continue;
if (n == DMSH_GMEM_MAP_ENTRIES) { over++; continue; }
uint64_t e[3] = {lo, hi, p->gpu - p->base}; memcpy(t + (size_t)n++ * DMSH_GMEM_MAP_ENTRY, e, sizeof e); end = hi;
if (n == GMEM_MAP_MAX) { if (!over++) over_lo = lo; over_bytes += hi - lo; continue; }
rng[n][0] = lo; rng[n][1] = hi; rng[n][2] = p->gpu - p->base; n++; end = hi;
}
pthread_mutex_unlock(&g_mu);
if (over) RED_ONCE(140, "raw global memory: %d more memory pools than the %d entries of the pool table; addresses in them are not reached", over, DMSH_GMEM_MAP_ENTRIES - 1);
unsigned cap = DMSH_GMEM_MAP_ENTRIES; while (cap < (unsigned)n) cap <<= 1;
id<MTLBuffer> b = [g_dev newBufferWithLength:(NSUInteger)cap * DMSH_GMEM_MAP_ENTRY options:MTLResourceStorageModeShared];
uint8_t *t = (uint8_t *)b.contents; memset(t, 0, b.length);
{ uint32_t count = cap; memcpy(t + 4 * sizeof(uint32_t), &count, sizeof count); } // entry 0's delta: the count (entry 0 is never a pool: it ends at 0)
for (unsigned k = 1; k < cap; k++) memset(t + (size_t)k * DMSH_GMEM_MAP_ENTRY, 0xff, 8);
for (int k = 1; k < n; k++) memcpy(t + (size_t)k * DMSH_GMEM_MAP_ENTRY, rng[k], sizeof rng[k]);
free(rng);
if (over) RED_ONCE(140, "raw global memory: %d more pool ranges than the %d entries the pool table may have; addresses in them are not reached (the first at %#llx, %llu KB in all)",
over, GMEM_MAP_MAX - 1, (unsigned long long)over_lo, (unsigned long long)(over_bytes >> 10));
res_add(b); res_commit();
unsigned old = g_gmem_buf ? (unsigned)(g_gmem_buf.length / DMSH_GMEM_MAP_ENTRY) : 0;
if (g_gmem_buf) defer_obj_release(g_gmem_buf);
g_gmem_buf = b; g_gmem_gen = gen;
{ static int said; if (!said++) MLOG("raw global memory: pool table of %d ranges (pool generation %llu), rebuilt whenever a pool comes or goes", n - 1, (unsigned long long)gen); }
{ static int said; if (!said++) MLOG("raw global memory: pool table of %d ranges in %u entries (pool generation %llu), rebuilt whenever a pool comes or goes", n - 1, cap, (unsigned long long)gen); }
if (old && cap > old) MLOG("raw global memory: the pool table grows to %u entries for %d ranges", cap, n - 1);
return b.gpuAddress;
}
// 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
Expand Down
8 changes: 8 additions & 0 deletions dm/gfx/tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ 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)
# Raw global memory on the GPU through the host's pool table (t_gmem_raw.m), with the shader test_dmsh_ampere_mem.py translates (DMSH_TEST_XLATE, default the in-repo translator);
# skipped only when the translator does not run (its exit code 77)
t_gmem_raw: t_gmem_raw.m $(MTL_T_DEP)
clang $(CFLAGS) -fobjc-arc -o $@ t_gmem_raw.m $(MTL_T_SRC) $(FW)
.PHONY: check_t_gmem_raw
check_t_gmem_raw: t_gmem_raw
@set -e; work=$$(mktemp -d); trap 'rm -rf "$$work"' EXIT; rc=0; DMSH_TEST_XLATE=$${DMSH_TEST_XLATE:-$(CURDIR)/../tools/dmsh_xlate_bin/dmsh_xlate} python3 test_dmsh_ampere_mem.py --fixture "$$work" || rc=$$?; \
if [ $$rc = 77 ]; then echo "t_gmem_raw: skipped, the translator does not run"; elif [ $$rc != 0 ]; then exit $$rc; else MTL_DEBUG_LAYER=1 ./t_gmem_raw "$$work/raw.metallib"; fi
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
51 changes: 51 additions & 0 deletions dm/gfx/tests/t_gmem_raw.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// A translated load at an address the shader read from memory (GlobalToStorage.TryGenerateRawStorageOp), run on the GPU against the host's own pool table (nvn_mtl.m gmem_map_gpu) with more
// pools than its least count: each probe must read its pool's word, including the pools past entry 4,096, and an address in no pool reads 0. The shader is test_dmsh_ampere_mem.py's
// RawGlobal one: it loads the address from words 0 and 1 of storage slot 0 and stores what it read at word 2.
// Usage: t_gmem_raw <metallib>
#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)
int main(int argc, const char **argv) { @autoreleasepool {
if (argc != 2) { fprintf(stderr, "usage: t_gmem_raw <metallib>\n"); return 2; }
mtl_init();
enum { MANY = 5000, PG = 16384 };
const uint64_t MB = 1ull << 20;
uint8_t *many = mmap(NULL, (size_t)MANY * 2 * PG, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0), *large = mmap(NULL, 64 * MB, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
if (many == MAP_FAILED || large == MAP_FAILED) abort();
for (int i = 0; i < MANY; i++) { uint8_t *p = many + (size_t)i * 2 * PG; mtl_pool_add(p, PG); uint32_t v = 0x10000u + (unsigned)i; memcpy(p + (size_t)(i * 8 % PG), &v, 4); }
mtl_pool_add(large, 64 * MB); { uint32_t v = 0xb16b00b5u; memcpy(large + 40 * MB + 12, &v, 4); }
mtl_sub_begin(); uint64_t map_gpu = gmem_map_gpu(); CHECK("submission completes", mtl_wait(mtl_sub_commit(), 5000000000ull));
id<MTLBuffer> map = g_gmem_buf; unsigned entries = (unsigned)(map.length / DMSH_GMEM_MAP_ENTRY);
CHECK("the table holds more than the least count", entries > DMSH_GMEM_MAP_ENTRIES && map_gpu == map.gpuAddress);
NSError *error = nil;
id<MTLLibrary> lib = [g_dev newLibraryWithURL:[NSURL fileURLWithPath:@(argv[1])] error:&error]; if (!lib) { fprintf(stderr, "library: %s\n", error.description.UTF8String); return 1; }
id<MTLComputePipelineState> pso = [g_dev newComputePipelineStateWithFunction:[lib newFunctionWithName:@"main0"] error:&error]; if (!pso) { fprintf(stderr, "pipeline: %s\n", error.description.UTF8String); return 1; }
id<MTLBuffer> slot = [g_dev newBufferWithLength:16 options:MTLResourceStorageModeShared], args = [g_dev newBufferWithLength:16 options:MTLResourceStorageModeShared];
uint64_t *a = args.contents; a[0] = slot.gpuAddress; a[1] = map.gpuAddress; // the argument buffer: storage slot 0 [[id(0)]], the pool table [[id(1)]]
uint32_t (^probe)(uint64_t) = ^uint32_t(uint64_t addr) {
uint32_t *w = slot.contents; memcpy(w, &addr, 8); w[2] = 0xdeadbeefu;
id<MTLCommandBuffer> cb = q_cb(); id<MTLComputeCommandEncoder> ce = [cb computeCommandEncoder];
[ce setComputePipelineState:pso]; [ce setBuffer:args offset:0 atIndex:0];
[ce useResource:slot usage:MTLResourceUsageRead | MTLResourceUsageWrite]; [ce useResource:map usage:MTLResourceUsageRead];
[ce dispatchThreadgroups:MTLSizeMake(1, 1, 1) threadsPerThreadgroup:MTLSizeMake(32, 1, 1)];
[ce endEncoding]; [cb commit]; [cb waitUntilCompleted];
CHECK("the dispatch completes", cb.status == MTLCommandBufferStatusCompleted);
return w[2];
};
int hits = 0, n = 0;
for (int i = 0; i < MANY; i += 97) { n++; uint64_t p = (uint64_t)(uintptr_t)(many + (size_t)i * 2 * PG); hits += probe(p + (uint64_t)(i * 8 % PG)) == 0x10000u + (unsigned)i; }
{ int i = MANY - 1; n++; uint64_t p = (uint64_t)(uintptr_t)(many + (size_t)i * 2 * PG); hits += probe(p + (uint64_t)(i * 8 % PG)) == 0x10000u + (unsigned)i; }
CHECK("small pools: each probe reads its pool", hits == n);
{ uint64_t last = (uint64_t)(uintptr_t)(many + (size_t)(MANY - 1) * 2 * PG); unsigned e = 0;
for (unsigned k = 1; k < entries; k++) if (((const uint64_t *)map.contents)[k * (DMSH_GMEM_MAP_ENTRY / 8)] == last) e = k;
CHECK("the last small pool lies past the least table", e >= DMSH_GMEM_MAP_ENTRIES); }
CHECK("a large pool", probe((uint64_t)(uintptr_t)large + 40 * MB + 12) == 0xb16b00b5u);
CHECK("an address in no pool reads 0", probe((uint64_t)(uintptr_t)many + PG + 8) == 0);
printf("t_gmem_raw: %d checks, %d failed (%u table entries, %d probes)\n", checks, failed, entries, n + 2);
return failed != 0;
}}
35 changes: 34 additions & 1 deletion dm/gfx/tests/t_pool_chunks.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// A large memory pool in chunks (nvn_mtl.m BIG_MIN): a lookup gets a zero-copy buffer that holds what it asked and starts at its stride, so only what lookups reach is wired. The buffers must
// say the same memory as the guest address, a read of unknown extent must have BIG_UNKNOWN past it, a chunk asked for more is lengthened, a texture's upload wires no chunk (up_big_src),
// raw global memory's pool table covers the whole pool (big_full), the pool's removal takes every entry, and a smaller pool stays one buffer.
// raw global memory's pool table covers the whole pool (big_full), the pool's removal takes every entry, and a smaller pool stays one buffer. With more pools than the table's least
// count, the table grows and the shader's lookup (gmem_find: the translator's GlobalToStorage.TryGenerateRawStorageOp, on the CPU) still finds every one.
#include "../nvn/nvn_mtl.m"
void *front_metal_layer(void) { return NULL; }
module_t *module_of(const void *p) { (void)p; return NULL; }
Expand All @@ -9,6 +10,14 @@ 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)
// The entry a translated shader takes for addr, as it does: the binary search over the count in entry 0's delta; 0 when no pool holds addr
static unsigned gmem_find(id<MTLBuffer> map, uint64_t addr) {
const uint32_t *w = map.contents; enum { W = DMSH_GMEM_MAP_ENTRY / sizeof(uint32_t) };
unsigned k = 0, step = w[4] >> 1;
do { unsigned c = k + step; uint64_t start; memcpy(&start, w + (size_t)c * W, sizeof start); if (start <= addr) k = c; step >>= 1; } while (step);
uint64_t end; memcpy(&end, w + (size_t)k * W + 2, sizeof end);
return addr < end ? k : 0;
}
static void finish(void) { CHECK("submission completes", mtl_wait(mtl_sub_commit(), 5000000000ull)); CHECK("GPU has no submission error", !mtl_last_commit_error()); }
// The lookup holds [a, a + n) in one buffer whose contents are the guest's memory there, and the GPU address agrees
static id<MTLBuffer> look(uint64_t a, uint64_t n) {
Expand Down Expand Up @@ -86,6 +95,30 @@ int main(void) { @autoreleasepool {
CHECK("only the small pool's entry is left of those added", g_npool == pools0 + 1);
CHECK("the small pool still answers, its entry moved", look((uint64_t)(uintptr_t)small + 20, 16).length == 64 * MB);
CHECK("the large pool's addresses do not", !mtl_gpu_addr(base + 100, 4, &mb, &mo, &gpu));
// More pools than DMSH_GMEM_MAP_ENTRIES (Ocarina of Time loads its save with up to about 4,500): each still has an entry, found by the search
enum { MANY = 5000, PG = 16384 };
uint8_t *many = mmap(NULL, (size_t)MANY * 2 * PG, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); if (many == MAP_FAILED) abort();
for (int i = 0; i < MANY; i++) mtl_pool_add(many + (size_t)i * 2 * PG, PG); // a page apart: a range each
mtl_sub_begin(); CHECK("pool table rebuilt", gmem_map_gpu() != 0); finish();
id<MTLBuffer> map = g_gmem_buf; unsigned cap = (unsigned)(map.length / DMSH_GMEM_MAP_ENTRY); const uint32_t *mw = map.contents;
CHECK("the table grows to a power of two that holds every pool", cap >= MANY + 2 && !(cap & (cap - 1)) && cap < 2 * (MANY + 2) && mw[4] == cap);
int found = 0, beyond = 0, gaps = 0; unsigned last = 0;
for (int i = 0; i < MANY; i++) {
uint64_t p = (uint64_t)(uintptr_t)(many + (size_t)i * 2 * PG), a = p + (uint64_t)(i * 8 % PG);
unsigned e = gmem_find(map, a); void *pb; uint64_t po, pg;
const uint64_t *r = (const uint64_t *)map.contents + e * (DMSH_GMEM_MAP_ENTRY / sizeof(uint64_t));
if (e && mtl_gpu_addr(a, 1, &pb, &po, &pg) && a + r[2] == pg) found++;
if (e >= DMSH_GMEM_MAP_ENTRIES) beyond++;
if (i == MANY - 1) last = e;
gaps += gmem_find(map, p + PG) == 0; // the page after the pool: no pool
}
CHECK("every pool's address reaches its memory", found == MANY);
CHECK("pools past the least table are found", beyond > 0 && last >= DMSH_GMEM_MAP_ENTRIES);
CHECK("an address in no pool finds none", gaps == MANY);
for (int i = 0; i < MANY; i++) mtl_pool_del(many + (size_t)i * 2 * PG);
mtl_drain();
mtl_sub_begin(); gmem_map_gpu(); finish();
CHECK("with the pools gone the table is the least again", g_gmem_buf.length / DMSH_GMEM_MAP_ENTRY == DMSH_GMEM_MAP_ENTRIES);
mtl_pool_del(small); mtl_drain();
printf("t_pool_chunks: %d checks, %d failed\n", checks, failed); return failed != 0;
}}
Loading
Loading