diff --git a/dm/gfx/nvn/nvn_dirty.c b/dm/gfx/nvn/nvn_dirty.c index 2a59162b..8f99ecb5 100644 --- a/dm/gfx/nvn/nvn_dirty.c +++ b/dm/gfx/nvn/nvn_dirty.c @@ -274,17 +274,25 @@ void nvn_dirty_remove_range(const void *mem) { if (r) { prep_write((void *)r->base, r->end - r->base); atomic_store_explicit(&r->live, 0, memory_order_release); atomic_fetch_sub(&g_live, 1); } pthread_mutex_unlock(&g_slot_mu); } +// A cached slot can have retired or been reused. Check that it still +// covers the requested span before skipping the range table lookup. +static range_t *resolve_span(const void *span, uintptr_t addr, size_t len) { + range_t *r = (range_t *)span; + if (r && atomic_load_explicit(&r->live, memory_order_acquire) && + addr >= r->base && addr + len <= r->end) return r; + return find_span(addr, len); +} int nvn_dirty_tracked(const void *addr, size_t len) { return find_span((uintptr_t)addr, len) != NULL; } // The tracked range holding [addr, addr+len), as a handle a caller keeps (packaged textures: the scan over every range ran twice per texture lookup). NULL = untracked const void *nvn_dirty_span(const void *addr, size_t len) { return len ? find_span((uintptr_t)addr, len) : NULL; } // Set read-only: mark the pages BUSY first and mark them PROT only after mprotect has landed (for the order and the invariant see the state notes at the top of the file) -void nvn_dirty_protect(const void *addr, size_t len) { +void nvn_dirty_protect_span(const void *span, const void *addr, size_t len) { // Keep this descriptor live through the last mprotect. Retirement restores // RW under this same mutex; it must not finish between lookup and PG_BUSY. pthread_mutex_lock(&g_slot_mu); - range_t *r = len ? find_span((uintptr_t)addr, len) : NULL; + range_t *r = len ? resolve_span(span, (uintptr_t)addr, len) : NULL; if (!r) { pthread_mutex_unlock(&g_slot_mu); return; } atomic_fetch_add(&r->nprot, 1); // PEANUTS(upstream-candidate): in-flight guard: nprot stays non-zero until every transition in this call finishes uintptr_t a = (uintptr_t)addr, p = (a - r->base) >> g_shift, q = (a + len - 1 - r->base) >> g_shift; @@ -341,6 +349,7 @@ void nvn_dirty_protect(const void *addr, size_t len) { nstuck ? "; some could not be made writable again and stay protected (their first write faults and unprotects them)" : ""); } } +void nvn_dirty_protect(const void *addr, size_t len) { nvn_dirty_protect_span(NULL, addr, len); } // A swap of guest pages under the tracker (the Vulkan replay placing a pool whose textures it already tracks, vka_mem.c place): the swap reads the range's protections, // copies the pages and puts the protections back on the new ones, so a page this module protected or unprotected meanwhile would come out with the wrong protection for // its state (read-only and NONE: a writer spins forever; writable and PROT: its writes are missed). So the swap holds the tracker: every protected page of the range is @@ -377,8 +386,7 @@ void nvn_dirty_release_all(void) { uint64_t nvn_dirty_now(void) { return atomic_load(&g_now); } int nvn_dirty_since_span(const void *span, const void *addr, size_t len, uint64_t stamp) { if (!len) return 0; - range_t *r = (range_t *)span; uintptr_t a0 = (uintptr_t)addr; // the kept handle still holds the span (a slot reused by another pool over the same memory is as good): no scan - if (!r || !atomic_load_explicit(&r->live, memory_order_acquire) || a0 < r->base || a0 + len > r->end) r = find_span(a0, len); + range_t *r = resolve_span(span, (uintptr_t)addr, len); if (!r) return 1; // When untracked, conservatively treat as dirty: better to upload more than to show stale content if (stamp < atomic_load_explicit(&r->born, memory_order_relaxed)) return 1; // PEANUTS: checked before this pool was registered over the memory (born) uintptr_t a = (uintptr_t)addr, p = (a - r->base) >> g_shift, q = (a + len - 1 - r->base) >> g_shift; @@ -389,8 +397,8 @@ int nvn_dirty_since_span(const void *span, const void *addr, size_t len, uint64_ int nvn_dirty_bounds_since(const void *span, const void *addr, size_t len, uint64_t stamp, size_t *lo, size_t *hi) { *lo = 0; *hi = len; if (!len) return 0; - range_t *r = (range_t *)span; uintptr_t a = (uintptr_t)addr; - if (!r || !atomic_load_explicit(&r->live, memory_order_acquire) || a < r->base || a + len > r->end) r = find_span(a, len); + uintptr_t a = (uintptr_t)addr; + range_t *r = resolve_span(span, a, len); if (!r) return 1; if (stamp < atomic_load_explicit(&r->born, memory_order_relaxed)) return 1; // PEANUTS: every page counts (born); *lo, *hi are the whole of it already uintptr_t p = (a - r->base) >> g_shift, q = (a + len - 1 - r->base) >> g_shift, f = UINTPTR_MAX, l = 0; diff --git a/dm/gfx/nvn/nvn_dirty.h b/dm/gfx/nvn/nvn_dirty.h index cf66ddd1..2bfb86fe 100644 --- a/dm/gfx/nvn/nvn_dirty.h +++ b/dm/gfx/nvn/nvn_dirty.h @@ -7,6 +7,7 @@ void nvn_dirty_add_range(const void *mem, size_t size); // Register a void nvn_dirty_stat(int *live, int *peak, int *cap); // number of registered memory pools, peak, tracking table capacity (for the memory readout line) void nvn_dirty_remove_range(const void *mem); // Pool destruction: restore protected pages to writable and mark invalid int nvn_dirty_tracked(const void *addr, size_t len); +void nvn_dirty_protect_span(const void *span, const void *addr, size_t len); // Reuse a span from nvn_dirty_span if it still covers the range void nvn_dirty_protect(const void *addr, size_t len); // Set the covered pages read-only void nvn_dirty_hold(const void *addr, size_t len); // Unprotect (and stamp) every protected page of [addr, addr+len) and protect nothing more until nvn_dirty_unhold void nvn_dirty_unhold(void); // (a placement swapping those pages, vka_mem.c place: the tracker's protections must not change under it) diff --git a/dm/gfx/tests/t_dirty_slots.c b/dm/gfx/tests/t_dirty_slots.c index ae1d3d4d..85a5cfac 100644 --- a/dm/gfx/tests/t_dirty_slots.c +++ b/dm/gfx/tests/t_dirty_slots.c @@ -64,16 +64,23 @@ int main(void) { for (int i = 0; i < NMANY; i++) { many[i] = page_map(); nvn_dirty_add_range(many[i], 4096); } void *last = many[NMANY - 1]; EXPECT("5000 live pools: the last one is tracked", 1, nvn_dirty_tracked(last, 4096)); - nvn_dirty_protect(last, 4096); + const void *span = nvn_dirty_span(last, 4096); + nvn_dirty_protect_span(span, last, 4096); stamp = nvn_dirty_now(); EXPECT("pool 5000 not written after protect: clean", 0, nvn_dirty_since(last, 4096, stamp)); ((volatile uint8_t *)last)[0] = 5; EXPECT("pool 5000 written by the game: dirty", 1, nvn_dirty_since(last, 4096, stamp)); + nvn_dirty_remove_range(last); + nvn_dirty_protect_span(span, many[0], 4096); + stamp = nvn_dirty_now(); + ((volatile uint8_t *)many[0])[0] = 6; + EXPECT("a retired span falls back to the live range", 1, nvn_dirty_since(many[0], 4096, stamp)); for (int i = 0; i < NMANY; i++) { nvn_dirty_remove_range(many[i]); munmap(many[i], 4096); } free(many); // Adjacent pools sharing a host page (pools are 4 KB aligned, host pages 16 KB): the first page of SMBW's ASTC texture pool is shared with a small preceding pool, and was judged untracked and re-uploaded on every use size_t pg = (size_t)getpagesize(); + if (pg <= 4096) { puts("4 KiB-aligned adjacent pools cannot share a host page here"); return g_fail != 0; } uint8_t *blk = mmap(NULL, 4 * pg, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); if (blk == MAP_FAILED) { perror("mmap"); exit(2); } uint8_t *pa = blk, *pb = blk + pg - 0x1000; // pool A covers the start of page 0, pool B starts in the last 4 KB of page 0 diff --git a/dm/gfx/vk_android/vka.h b/dm/gfx/vk_android/vka.h index 51878456..c48ebff9 100644 --- a/dm/gfx/vk_android/vka.h +++ b/dm/gfx/vk_android/vka.h @@ -291,7 +291,7 @@ typedef struct vka_img { uint64_t wb_fence; // the fence of the submission that holds the last copy of its GPU content back into its storage (vka_tex_writeback; 0: none): vka_wbfollow.h int wbf_waiting; // the prepare's last scan found it waiting for that copy-back; a scan that only runs for waiting images visits just these (wbf_visit) int wt; // the game writes it through WriteTexels (vka_tex.c mtl_tex_write_region): never tracked again - uint64_t edge_h[2]; int edged; + uint64_t edge_h[2]; uintptr_t edge_block; int edged; uint64_t hash; // DM_VK_NANSCAN: a hash of the storage at the last upload (vka_tex_id_check) uint64_t gw_seen; int from_mem; uint64_t head, up_fence; uint8_t *snap; // head, up_fence: DM_VK_NANSCAN's storage watch // the newest GPU write into guest memory its last upload included (vka_tex.c gw_t); content from guest storage (ST_TRACK / ST_FILL) // In a sparse virtual pool (hle_nvn.c nvn_sparse_*): the storage is read through the pool's 4 KB page table, never at mem (unmapped pages there are inaccessible); diff --git a/dm/gfx/vk_android/vka_draw.c b/dm/gfx/vk_android/vka_draw.c index 7ffeb884..0e9d6d3b 100644 --- a/dm/gfx/vk_android/vka_draw.c +++ b/dm/gfx/vk_android/vka_draw.c @@ -1420,16 +1420,21 @@ static writes_t g_w; // A texture or image read by a shader that declares it in another numeric class (float / uint / sint) than the view the game binds: logged once per shader, stage and binding // (the first 64), with the texture. Such a binding is read through a view of the shader's class of the same texel size (vka_tex.c class_twin) unless its module is retyped static void class_note(const vka_shader *sh, int st, const vka_res *r, int tid, int storage) { - static uint64_t seen[4096]; static int nlog; + static uint64_t seen[4096], logged[64]; static int nlog; // PEANUTS(upstream-candidate): bound the diagnostic lookup. + if (nlog >= 64) return; int want = r->dim & DMSH_VK_DIM_SINT ? 2 : r->dim & DMSH_VK_DIM_UINT ? 1 : 0; uint64_t h = (sh->key ^ (uint64_t)st << 56 ^ (uint64_t)r->binding << 40 ^ (uint64_t)want << 38) | 1; uint32_t s = (uint32_t)(h * 0x9e3779b97f4a7c15ull >> 52); - for (uint32_t n = 0; n < 4096; n++, s = (s + 1) & 4095) { if (seen[s] == h) return; if (!seen[s]) break; } - if (nlog >= 64) return; + // A full diagnostic table must not scan thousands of slots per descriptor. + for (unsigned probe = 0; probe < 8; probe++, s = (s + 1) & 4095) { + if (seen[s] == h) return; + if (!seen[s]) break; + } seen[s] = h; int have = vka_tex_id_class_rec(tid); if (have == want) return; - nlog++; + for (int i = 0; i < nlog; i++) if (logged[i] == h) return; + logged[nlog++] = h; static const char *cn[3] = {"float", "uint", "sint"}; char why[320]; vka_tex_id_desc(tid, why, sizeof why); VLOG("class: shader %016llx stage %d binding %u declares a %s %s, texture ID %d is %s (%s)", (unsigned long long)sh->key, st, r->binding, cn[want], storage ? "image" : "texture", tid, cn[have], why); } diff --git a/dm/gfx/vk_android/vka_tex.c b/dm/gfx/vk_android/vka_tex.c index a80964c2..23c9a7f8 100644 --- a/dm/gfx/vk_android/vka_tex.c +++ b/dm/gfx/vk_android/vka_tex.c @@ -1100,32 +1100,33 @@ static void img_edges_take(vka_img *im) { if (ie <= ia) { r[0][0] = a; r[0][1] = e; r[1][0] = r[1][1] = 0; } else { r[0][0] = a; r[0][1] = ia; r[1][0] = ie; r[1][1] = e; } for (int i = 0; i < 2; i++) im->edge_h[i] = r[i][1] > r[i][0] ? ft_hash(r[i][0], r[i][1]) : 0; + im->edge_block = pg; im->edged = !nvn_dirty_since_span(im->span, im->mem, im->L.size, im->stamp); } static int img_edges_clean(vka_img *im) { if (!im || !im->edged || im->vpool || !im->span || !im->mem) return 0; - if (dm_guest_unmapped((uintptr_t)im->mem, im->L.size)) return 0; uintptr_t pg = nvn_dirty_block_bytes(); if (!pg) pg = (uintptr_t)getpagesize(); + if (pg != im->edge_block || dm_guest_unmapped((uintptr_t)im->mem, im->L.size)) return 0; uintptr_t r[2][2], ia, ie; uintptr_t a = (uintptr_t)im->mem, e = a + im->L.size; ia = (a + pg - 1) & ~(pg - 1); ie = e & ~(pg - 1); if (ie <= ia) { r[0][0] = a; r[0][1] = e; r[1][0] = r[1][1] = 0; ia = ie = 0; } else { r[0][0] = a; r[0][1] = ia; r[1][0] = ie; r[1][1] = e; } uint64_t st = nvn_dirty_now(); - nvn_dirty_protect(im->mem, im->L.size); - int in_dirty = (ie > ia && nvn_dirty_since_span(im->span, (const void *)ia, ie - ia, im->stamp)); - int h0_bad = (r[0][1] > r[0][0] && ft_hash(r[0][0], r[0][1]) != im->edge_h[0]); - int h1_bad = (r[1][1] > r[1][0] && ft_hash(r[1][0], r[1][1]) != im->edge_h[1]); - if (in_dirty || h0_bad || h1_bad) return 0; + nvn_dirty_protect_span(im->span, im->mem, im->L.size); + if (ie > ia && nvn_dirty_since_span(im->span, (const void *)ia, ie - ia, im->stamp)) return 0; + for (int i = 0; i < 2; i++) + if (r[i][1] > r[i][0] && ft_hash(r[i][0], r[i][1]) != im->edge_h[i]) return 0; + // The hashes still describe the GPU copy. Writes during validation + // must remain newer than the stamp for the next prepare. im->stamp = st; - img_edges_take(im); static uint64_t g_edge_skips; if (++g_edge_skips <= 8) VLOG("texture serial %llu %ux%u format %#x: only shared pages written beside it, re-upload skipped (%llu so far)", (unsigned long long)im->serial, im->w, im->h, im->d.format, (unsigned long long)g_edge_skips); return 1; } static void upload_tracked(vka_img *im, const tex_t *t, VkCommandBuffer cb) { // stamp, protect, then read (nvn_mtl.m pkg_upload): a write during the copy is seen at the next check - im->stamp = nvn_dirty_now(); nvn_dirty_protect(t->mem, t->L.size); + im->stamp = nvn_dirty_now(); nvn_dirty_protect_span(im->span, t->mem, t->L.size); img_edges_take(im); if (vka_nan_on()) im->hash = mem_hash(t->mem, t->L.size); uint8_t *c = im->vpool ? NULL : gw_content(t->mem, t->L.size, &im->gw_seen); // (a sparse pool's storage is read through its page table: upload_) @@ -1170,7 +1171,7 @@ static int upload_rows_tracked(vka_img *im, const tex_t *t, VkCommandBuffer cb, if (st < pitch) return 0; uint32_t r0 = (uint32_t)(lo / st), r1 = (uint32_t)((hi + st - 1) / st); if (r1 > by) r1 = by; if (r0 >= r1) r0 = 0, r1 = by; - im->stamp = nvn_dirty_now(); nvn_dirty_protect(t->mem, t->L.size); // stamp, protect, then read (upload_tracked) + im->stamp = nvn_dirty_now(); nvn_dirty_protect_span(im->span, t->mem, t->L.size); // stamp, protect, then read (upload_tracked) img_edges_take(im); if (tbuf) { tbuf_rows_copy(im, t, cb, r0, r1, &g_rows_up); return 1; } // PEANUTS: tbuf (see above) vka_span s; if (!vka_scratch(pitch * (r1 - r0), 16, &s)) return 1; @@ -1193,7 +1194,7 @@ static int upload_bands_tracked(vka_img *im, const tex_t *t, VkCommandBuffer cb, if (c) { free(c); return 0; } if ((c = unmapped_content(t))) { free(c); return 0; } im->gw_seen = seen; - im->stamp = nvn_dirty_now(); nvn_dirty_protect(t->mem, t->L.size); // stamp, protect, then read (upload_tracked) + im->stamp = nvn_dirty_now(); nvn_dirty_protect_span(im->span, t->mem, t->L.size); // stamp, protect, then read (upload_tracked) img_edges_take(im); uint64_t up = 0; uint8_t *bst = NULL; uint32_t bst_cap = 0; diff --git a/dm/host/hle_svc.c b/dm/host/hle_svc.c index d74c1045..49266ba2 100644 --- a/dm/host/hle_svc.c +++ b/dm/host/hle_svc.c @@ -207,9 +207,22 @@ static int guest_page_mapped(uintptr_t p) { // p: a guest address (page aligne return 1; } uint64_t dm_guest_unmapped(uintptr_t a, size_t n) { - uint64_t c = 0; if (!n) return 0; - for (uintptr_t p = a & ~(uintptr_t)0xFFF; p < a + n; p += 0x1000) c += !guest_page_mapped(p); - return c; + if (!n) return 0; + uintptr_t first = a >> 12, last = ((a + n - 1) >> 12) + 1; + uintptr_t alias = (uintptr_t)g_alias >> 12, heap = (uintptr_t)g_heap >> 12; + uintptr_t lo = first > alias ? first : alias; + uintptr_t hi = alias + (g_alias_cap >> 12); + if (hi > last) hi = last; + uint64_t count = g_alias && hi > lo ? alias_unmapped((lo - alias) << 12, (hi - lo) << 12) : 0; + + if (g_heap && last > heap && first < heap + (g_heap_cap >> 12)) { + uintptr_t opened = heap + ((atomic_load(&g_heap_cur) + 0xfff) >> 12); + lo = first > opened ? first : opened; + hi = heap + (g_heap_cap >> 12); + if (hi > last) hi = last; + if (hi > lo) count += hi - lo; + } + return count; } // Copy n bytes of our own memory that may become inaccessible meanwhile: the bytes copied before the first inaccessible one (a system call reads them, so an inaccessible page // is an error, not a signal). If the system refuses the call itself (a seccomp filter: not seen on Android), a plain copy, with a red line once diff --git a/dm/host/tests/t_svc_memory.c b/dm/host/tests/t_svc_memory.c index 59bcb0d3..34518c05 100644 --- a/dm/host/tests/t_svc_memory.c +++ b/dm/host/tests/t_svc_memory.c @@ -186,6 +186,21 @@ static void alias_lifecycle(void) { assert(pages(1, alias + g_alias_cap - P, 2 * P) == BAD); } +static void unmapped_counts(void) { + // Include the edges of both regions and an alias bitmap word boundary. + assert(!pages(1, alias + 63 * P, 2 * P)); + assert(!resize(2 * 1024 * 1024)); + assert(dm_guest_unmapped(alias - 1, 2) == 1); + assert(dm_guest_unmapped(alias + 63 * P - 1, 2 * P + 2) == 2); + assert(dm_guest_unmapped(alias + g_alias_cap - 1, 2) == 1); + uintptr_t heap_end = (uintptr_t)g_heap + g_heap_cur; + assert(dm_guest_unmapped(heap_end - 1, 2) == 1); + assert(dm_guest_unmapped((uintptr_t)g_heap + g_heap_cap - 1, 2) == 1); + assert(dm_guest_unmapped(alias, 0) == 0); + assert(!pages(0, alias + 63 * P, 2 * P)); + assert(!resize(0)); +} + static void ownership(void) { assert(!pages(1, alias, 16 * P)); memset((void *)alias, 0x34, 16 * P); @@ -212,7 +227,9 @@ static void ownership(void) { assert(!pages(0, alias, P) && !pages(1, alias, P)); assert(*(uint8_t *)(alias + P) == 0x34); memset((void *)(stack + P), 0x76, 2 * P); - readonly_base = alias; readonly_size = memory_getpagesize(); + // Protect the host page that contains the start of the copyback source. + readonly_size = memory_getpagesize(); + readonly_base = (alias + P) & ~(uintptr_t)(readonly_size - 1); assert(!mprotect((void *)readonly_base, readonly_size, PROT_READ)); assert(!unmap(stack + P, alias + P, 2 * P) && !readonly_size); for (size_t i = P; i < 3 * P; ++i) assert(((uint8_t *)alias)[i] == 0x76); @@ -549,7 +566,7 @@ int main(int argc, char **argv) { for (unsigned i = 0; i < 8; ++i) assert(!pthread_create(&threads[i], NULL, init_worker, &addresses[i])); for (unsigned i = 0; i < 8; ++i) { assert(!pthread_join(threads[i], NULL)); assert(addresses[i] == addresses[0]); } alias = addresses[0]; stack = info(I_StackAddr); heap = info(I_HeapAddr); - lazy_commit(); alias_lifecycle(); ownership(); module_sources(); module_back_patch(); failures(); release_fallback(); concurrency(); fragmented_queries(); + lazy_commit(); alias_lifecycle(); unmapped_counts(); ownership(); module_sources(); module_back_patch(); failures(); release_fallback(); concurrency(); fragmented_queries(); guest_helper(); read_mapped_race(); if (!DM_GAME_MEM_MAP_LIMIT) metadata_limit(); else reported_lower(); diff --git a/dm/tools/tests/test_vka_tex_edges.py b/dm/tools/tests/test_vka_tex_edges.py new file mode 100644 index 00000000..11656c18 --- /dev/null +++ b/dm/tools/tests/test_vka_tex_edges.py @@ -0,0 +1,97 @@ +"""Exercise tracked edge validation without a Vulkan device.""" +from pathlib import Path +import shutil +import subprocess +import tempfile +import unittest + + +SOURCE = Path(__file__).resolve().parents[3] / 'dm/gfx/vk_android/vka_tex.c' +FIXTURE = r''' +#include +#include +#include +#include +#define VLOG(...) ((void)0) + +typedef struct { + const void *vpool, *span; + const uint8_t *mem; + struct { size_t size; } L; + uint64_t stamp, edge_h[2]; + uintptr_t edge_block; + int edged; +} vka_img; + +static uintptr_t block = 16; +static uintptr_t changed; +static uint64_t generation = 1; +static unsigned protections; + +static int getpagesize(void) { return 16; } +static uintptr_t nvn_dirty_block_bytes(void) { return block; } +static uint64_t nvn_dirty_now(void) { return generation; } +static int dm_guest_unmapped(uintptr_t start, size_t size) { + (void)start; (void)size; + return 0; +} +static void nvn_dirty_protect_span(const void *span, const void *addr, size_t size) { + assert(span && addr && size); + protections++; +} +static int nvn_dirty_since_span(const void *span, const void *addr, size_t size, uint64_t stamp) { + assert(span); + uintptr_t start = (uintptr_t)addr; + return generation > stamp && changed >= start && changed - start < size; +} +''' +MAIN = r''' +int main(void) { + _Alignas(32) uint8_t storage[128] = {0}; + vka_img im = {.span = storage, .mem = storage + 1, .L.size = 62, .stamp = generation}; + img_edges_take(&im); + assert(im.edged && im.edge_block == 16); + + // A dirty shared block with unchanged texture bytes can skip upload. + changed = (uintptr_t)(storage + 1); + generation++; + assert(img_edges_clean(&im)); + assert(im.stamp == generation && protections == 1); + + // Real content changes in either edge or in the tracked interior cannot skip. + storage[62] = 1; + generation++; + assert(!img_edges_clean(&im) && im.stamp == generation - 1); + storage[62] = 0; + changed = (uintptr_t)(storage + 16); + generation++; + assert(!img_edges_clean(&im)); + + // Growth of the tracker block invalidates hashes made for the old edges. + block = 32; + assert(!img_edges_clean(&im) && protections == 3); + im.stamp = generation; + img_edges_take(&im); + assert(im.edged && im.edge_block == 32); + return 0; +} +''' + + +class TextureEdges(unittest.TestCase): + def test_dirty_edges_and_tracking_block_growth(self): + cc = shutil.which('cc') + if not cc: + self.skipTest('needs a C compiler') + source = SOURCE.read_text() + functions = source[source.index('static uint64_t ft_hash('):source.index('static void upload_tracked(')] + with tempfile.TemporaryDirectory() as tmp: + fixture = Path(tmp) / 'edges.c' + binary = Path(tmp) / 'edges' + fixture.write_text(FIXTURE + functions + MAIN) + build = subprocess.run( + [cc, '-std=c11', '-O1', '-fsanitize=undefined', '-fsanitize-undefined-trap-on-error', + str(fixture), '-o', str(binary)], capture_output=True, text=True) + self.assertEqual(build.returncode, 0, build.stderr) + run = subprocess.run([str(binary)], capture_output=True, text=True) + self.assertEqual(run.returncode, 0, run.stderr) diff --git a/docs/agents/symptoms.md b/docs/agents/symptoms.md index ccfe883b..6d115d0f 100644 --- a/docs/agents/symptoms.md +++ b/docs/agents/symptoms.md @@ -819,6 +819,7 @@ One line per symptom in the [subsystem references](../reference/): what you see - (Android, Qualcomm's driver) `SIGSEGV` at `0x48` in `qglinternal::vkUpdateDescriptorSets+376`, called from the host, in Ocarina of Time when Link falls into water or at an area transition; the GPU is left faulted and the system's compositor aborts (`no suitable EGLConfig found, giving up`): the device needs a forced restart. → [Null descriptors written into descriptor sets crash Qualcomm's driver (Android)](../reference/rendering.md#null-descriptors-written-into-descriptor-sets-crash-qualcomms-driver-android) - Mario Kart World on Turnip (Tab S9, Adreno 740; issue #215): after a Grand Prix race (often the second) a hard-edged black rectangle under every kart, coloured glare on alpha-blended effects, and the Mode and Character Select backgrounds missing, until the app restarts. The reporter's runs of the same cup: Turnip V39 and the defaults, the bug; `DM_VK_IMG_LIMIT_MB=3072`, no bug anywhere; `DM_VK_GPL=0` with `DM_VK_PSO_ASYNC=0`, the race effects fine and the menu backgrounds gone. → [A GPU-written image evicted and made again empty (Android)](../reference/rendering.md#a-gpu-written-image-evicted-and-made-again-empty-android) - On Turnip, pond water appears black while the surrounding terrain, floating plants and reflections still draw. → [Precision of fragment depth used by an equal-depth lighting pass](../reference/rendering.md#precision-of-fragment-depth-used-by-an-equal-depth-lighting-pass) +- `pace cost` reports high image descriptor and texture preparation time even when the encoder's descriptor pushes and texture uploads are short. → [Image descriptor diagnostics and dirty texture checks can dominate replay time](../reference/rendering.md#image-descriptor-diagnostics-and-dirty-texture-checks-can-dominate-replay-time) ## Shaders diff --git a/docs/reference/rendering.md b/docs/reference/rendering.md index 1d11f4fc..912bb767 100644 --- a/docs/reference/rendering.md +++ b/docs/reference/rendering.md @@ -1823,3 +1823,11 @@ On the Retroid Pocket Flip 2, PurpleVK T30 (`git-62ac221a33`) reports a 33.11 ns - **Cause:** A fragment shader writes a material class into a 16-bit depth buffer. The lighting pass accepts an exact depth match. Reassociating the classifier's fused multiply-add and the following add can put the result on the other side of a D16 rounding boundary. [TOTKDroid's matching diagnosis](https://github.com/mamcjr/TOTKDroid/commit/1ff1c7b4b2) - **Fix:** The Vulkan replay adds SPIR-V `NoContraction` to the scalar arithmetic feeding a direct `FragDepth` store when it prepares the shader module. An explicit fused multiply-add stays fused. The fix applies when loading existing Android shader packs. - **Found on:** [Tears of the Kingdom](../../dm/games/0100F2C0115B6000/README.md#found-on-this-game), Great Sky Island, AYN Thor with Turnip v26.2.0 R5. + + +### Image descriptor diagnostics and dirty texture checks can dominate replay time + +- **Symptom:** `pace cost` reports high image descriptor and texture preparation time even when the encoder's descriptor pushes and texture uploads are short. +- **Cause:** The image-class diagnostic searched its table even after exhausting its message budget. The probe could scan the whole table at high occupancy. Tracked texture refreshes also repeated range searches for protection, counted unmapped guest pages individually, and hashed unchanged edges twice when skipping an upload. +- **Fix:** Stop the class diagnostic at its budget and bound its probe. Reuse the dirty span under the tracker mutex, count alias pages by bitmap word and heap pages by interval, and retain validated edge hashes with their block size. A block size change forces the usual refresh on the next dirty hit. Concurrent writes keep a generation newer than the stamp taken before validation. +- **Found on:** World War Z (`010099F013898000`).