Skip to content

Commit ffed898

Browse files
Preserve initial pool texture content in the legacy renderer
1 parent 57beeb6 commit ffed898

6 files changed

Lines changed: 132 additions & 3 deletions

File tree

‎.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ dm/gfx/tests/t_framegen_present
1515
dm/gfx/tests/t_framegen*.dSYM/
1616
/dm/gfx/tests/t_legacy_pipeline
1717
/dm/gfx/tests/t_legacy_pipeline.dSYM/
18+
19+
/dm/gfx/tests/t_legacy_initial_content
20+
/dm/gfx/tests/t_legacy_initial_content.dSYM/

‎dm/games/010029B0118E8000/README.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,20 @@ usage. A focused regression preserves all 32,768 original blocks and verifies
105105
the GPU-to-CPU ownership transitions. This is the first candidate that repairs
106106
the observed frame-218 data loss; a successful build is not yet a corrected
107107
picture claim, which still requires the owner's device test.
108+
109+
### Initial-pool-content parity (2026-10-30)
110+
111+
The agent reproduced the current black UI after confirmed A inputs on M2.
112+
The intro video is visible. Two sampled inputs (serials 207 and 238) have
113+
nonzero CPU storage but both GPU RGB and alpha read zero. This alone does
114+
not prove that the CPU bytes are valid image content; the bounded source
115+
decode/ownership diagnostic is pending.
116+
117+
A separate production-recorder regression exposed missing
118+
[initial pool content](../../../docs/reference/nvn-objects.md#a-pools-storage-at-creation-is-its-textures-content)
119+
semantics in the legacy backend: published initial data was zeroed instead of
120+
uploaded. The modern path already supports it. The compatibility port passes
121+
the GPU regression while keeping unflushed cached storage and COMPRESSIBLE
122+
images excluded, and never reinitializes an existing GPU allocation. This
123+
is a concrete host fix, not yet a verified correction of this title's picture.
124+
No shader chain or game resources are changed.

‎dm/gfx/nvn/nvn_mtl_legacy.inc‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,8 +2092,9 @@ static id<MTLTexture> pkg_upload_(const tex_t *t, uint64_t *stamp, id<MTLTexture
20922092
tex_tag_format(mt, t->d.format);
20932093
if (g_allocprof > 0) { static int n; if (n++ < 30 || n % 500 == 0) MLOG("新建贴图(%s)#%d ser=%llu %ux%ux%u 格式 %#x 目标 %d 级 %u 存储 %p", __func__, n, (unsigned long long)t->serial, t->d.width, t->d.height, t->d.depth, t->d.format, t->d.target, t->d.levels, (void *)t->mem); }
20942094
res_add(mt); // Both first upload and re-upload register residency here (Metal 4 does not make resources resident automatically; a missed registration = GPU address page fault)
2095-
nvn_dirty_protect(t->mem, t->L.size);
2096-
*stamp = nvn_dirty_now();
2095+
// A null stamp is a one-time initialization of an ordinary GPU texture,
2096+
// not a CPU-owned package. Do not install dirty-page/reupload tracking.
2097+
if (stamp) { nvn_dirty_protect(t->mem, t->L.size); *stamp = nvn_dirty_now(); }
20972098
nvn_updiag_capture(diag);
20982099
int linear_bpb = 0; size_t linear_row = 0;
20992100
if (linear_needs_upload(t, &linear_bpb, &linear_row)) {
@@ -2397,6 +2398,31 @@ static int tex_cpu_written(uint64_t serial) {
23972398
pthread_mutex_lock(&g_cpu_tex_mu); int r = g_cpu_tex && CFSetContainsValue(g_cpu_tex, (const void *)(uintptr_t)serial); pthread_mutex_unlock(&g_cpu_tex_mu);
23982399
return r;
23992400
}
2401+
// Match the modern renderer's initial pool-content semantics. Inherit only
2402+
// published CPU-cached storage, CPU-uncached storage or a pool's initial
2403+
// CPU_NO_ACCESS data. A later GPU write remains authoritative: this is called
2404+
// only for a fresh allocation, never for an existing texture or storage alias.
2405+
int nvn_flushed(const void *p, uint64_t n);
2406+
static int legacy_mem_any(const uint8_t *p, uint64_t n) {
2407+
uint64_t i=0, word;
2408+
for (; i+8<=n; i+=8) { memcpy(&word,p+i,8); if(word)return 1; }
2409+
for (; i<n; i++) if(p[i])return 1;
2410+
return 0;
2411+
}
2412+
static int legacy_initial_content(const tex_t *t, id<MTLTexture> mt) {
2413+
if (!t->mem || !t->pool || (t->pool->flags & NVN_MEMORY_POOL_FLAGS_VIRTUAL) || t->has_layout) return 0;
2414+
if (t->d.samples>1 || (t->d.flags & NVN_TEXTURE_FLAGS_COMPRESSIBLE) || ((int)mt.pixelFormat>=250 && (int)mt.pixelFormat<=262)) return 0;
2415+
int no_cpu=(t->pool->flags & NVN_MEMORY_POOL_FLAGS_CPU_NO_ACCESS)!=0;
2416+
int uncached=(t->pool->flags & NVN_MEMORY_POOL_FLAGS_CPU_UNCACHED)!=0;
2417+
static int flush_fill=-1;
2418+
if(flush_fill<0) { const char *e=getenv("DM_NVN_FLUSH_FILL");flush_fill=!e||atoi(e)!=0; }
2419+
if(!no_cpu && !uncached && (!flush_fill || !nvn_flushed(t->mem,t->L.size)))return 0;
2420+
if(!atomic_load(&g_sub_thread_set) || !on_sub_thread() || !legacy_mem_any(t->mem,t->L.size))return 0;
2421+
if(pkg_upload(t,NULL,mt,0)!=mt)return 0;
2422+
static unsigned reports;
2423+
if(reports++<8)MLOG("initial pool content: ser=%llu %ux%u format=%#x source=%s",(unsigned long long)t->serial,t->d.width,t->d.height,t->d.format,no_cpu?"CPU_NO_ACCESS":uncached?"CPU_UNCACHED":"CPU-flushed");
2424+
return 1;
2425+
}
24002426
static id<MTLTexture> tex_mtl_(const tex_t *ct);
24012427
static id<MTLTexture> tex_mtl(const tex_t *ct) { AP_B; id<MTLTexture> r = tex_mtl_(ct); AP_E(4); return r; }
24022428
static uint64_t g_texinfo_ser[8]; static int g_texinfo_n;
@@ -2481,7 +2507,7 @@ static id<MTLTexture> tex_mtl_(const tex_t *ct) {
24812507
mt = [g_dev newTextureWithDescriptor:d];
24822508
if (!mt) { RED_ONCE(2, "建 MTLTexture 失败 %ux%u 格式 %#x 目标 %d", t->d.width, t->d.height, t->d.format, t->d.target); return nil; }
24832509
tex_tag_format(mt, t->d.format);
2484-
res_add(mt); { uint32_t apf; tex_zero_fill(mt, bc_astc(t, &apf)); } // register residency before zeroing (the copy destination must be resident too)
2510+
res_add(mt); if (!legacy_initial_content(t, mt)) { uint32_t apf; tex_zero_fill(mt, bc_astc(t, &apf)); } // register residency before initialization
24852511
}
24862512
tex_tag_format(mt, t->d.format);
24872513
if (g_allocprof > 0) { static int n; if (n++ < 30 || n % 500 == 0) MLOG("新建贴图(%s)#%d ser=%llu %ux%ux%u 格式 %#x 目标 %d 级 %u 存储 %p", __func__, n, (unsigned long long)t->serial, t->d.width, t->d.height, t->d.depth, t->d.format, t->d.target, t->d.levels, (void *)t->mem); }

‎dm/gfx/tests/Makefile‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,13 @@ t_atomic_image: t_atomic_image.m $(MTL_T_DEP)
398398
clang $(CFLAGS) -fobjc-arc -o $@ t_atomic_image.m $(MTL_T_SRC) $(FW)
399399

400400
# Exercise the production traditional Metal descriptor on the host GPU.
401+
t_legacy_initial_content: t_legacy_initial_content.m $(MTL_T_DEP)
402+
clang $(CFLAGS) -fobjc-arc -o $@ t_legacy_initial_content.m $(MTL_T_SRC) $(FW)
403+
.PHONY: check_t_legacy_initial_content
404+
check_t_legacy_initial_content: t_legacy_initial_content
405+
MTL_DEBUG_LAYER=1 ./t_legacy_initial_content
406+
run: check_t_legacy_initial_content
407+
401408
t_legacy_pipeline: t_legacy_pipeline.m $(MTL_T_DEP)
402409
clang $(CFLAGS) -fobjc-arc -o $@ t_legacy_pipeline.m $(MTL_T_SRC) $(FW)
403410

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// A fresh private Metal texture must inherit published NVN pool content.
2+
#define DM_NVN_LEGACY_RENDERER_TEST 1
3+
#include "../nvn/nvn_mtl.m"
4+
#include "../nvn/nvn_protos.h"
5+
#include "../../host/hle.h"
6+
#include <mach-o/getsect.h>
7+
#include <mach-o/ldsyms.h>
8+
uint64_t g_t0_ns;
9+
int dm_guest_as_contains(uintptr_t p) { (void)p; return 0; }
10+
int front_screen_max_fps(void) { return 60; }
11+
void *front_metal_layer(void) { return NULL; }
12+
module_t *module_of(const void *p) { (void)p; return NULL; }
13+
module_t *g_mod; int g_nmod;
14+
void svc_thread_dump(void) {}
15+
uint64_t now_ns(void) { return bp_now(); }
16+
static int checks, failed;
17+
#define CHECK(c,label) do { checks++; if (!(c)) { failed++; fprintf(stderr,"FAIL: %s\n",label); } } while(0)
18+
static void initial_case(int cpu_flags,int flags,int flush,int filled,int want) {
19+
const size_t length=262144;
20+
uint8_t *mem=mmap(NULL,length,PROT_READ|PROT_WRITE,MAP_ANON|MAP_PRIVATE,-1,0);
21+
CHECK(mem!=MAP_FAILED,"source allocation");if(mem==MAP_FAILED)return;
22+
// A constant block-linear image has the same bytes regardless of GOB padding.
23+
memset(mem+65536,filled?0x5a:0,65536);
24+
unsigned long bytes=0;const hle_force_ent *entries=(const hle_force_ent *)getsectiondata(&_mh_execute_header,"__DATA","swforce",&bytes);
25+
void *(*boot)(const char*)=NULL;
26+
for(size_t i=0;i<bytes/sizeof *entries;i++)if(!strcmp(entries[i].name,"nvnBootstrapLoader"))boot=entries[i].fn;
27+
CHECK(boot!=NULL,"production bootstrap");if(!boot){munmap(mem,length);return;}
28+
PFNNVNDEVICEGETPROCADDRESSPROC gpa=(PFNNVNDEVICEGETPROCADDRESSPROC)boot("nvnDeviceGetProcAddress");
29+
#define API(name) ((NVNT_##name)gpa(NULL,#name))
30+
NVNmemoryPoolBuilder pb;NVNmemoryPool pool;NVNtextureBuilder tb;NVNtexture tex={0};
31+
API(nvnMemoryPoolBuilderSetDefaults)(&pb);API(nvnMemoryPoolBuilderSetStorage)(&pb,mem,length);API(nvnMemoryPoolBuilderSetFlags)(&pb,cpu_flags|NVN_MEMORY_POOL_FLAGS_GPU_CACHED);
32+
CHECK(API(nvnMemoryPoolInitialize)(&pool,&pb),"source pool initializes");
33+
if(flush==1)API(nvnMemoryPoolFlushMappedRange)(&pool,65536,65536);
34+
API(nvnTextureBuilderSetDefaults)(&tb);API(nvnTextureBuilderSetWidth)(&tb,4);API(nvnTextureBuilderSetHeight)(&tb,4);API(nvnTextureBuilderSetFormat)(&tb,NVN_FORMAT_RGBA8);API(nvnTextureBuilderSetFlags)(&tb,flags);API(nvnTextureBuilderSetStorage)(&tb,&pool,65536);
35+
CHECK(API(nvnTextureInitialize)(&tex,&tb),"ordinary texture initializes");tex_t *t=(tex_t *)&tex;
36+
CHECK(!t->has_layout,"source has no packaged layout");
37+
if(flush==2)API(nvnMemoryPoolFlushMappedRange)(&pool,65536,t->L.size);
38+
uint64_t uploads=g_bp_up_bytes;
39+
mtl_reg_tex(1,t,NULL);mtl_sub_begin();id<MTLTexture> texture=tex_by_id(1);
40+
CHECK(texture!=nil,"sampled descriptor resolves");mtl_sub_commit();mtl_drain();
41+
CHECK(!tex_cpu_written(t->serial),"one-time fill keeps GPU ownership instead of becoming a tracked package");
42+
CHECK((g_bp_up_bytes>uploads)==(want&&filled),"only eligible nonzero initial content is uploaded");
43+
id<MTLBuffer> out=[g_dev newBufferWithLength:1024 options:MTLResourceStorageModeShared];
44+
id<MTLCommandBuffer> cb=[g_q commandBuffer];id<MTLBlitCommandEncoder>b=[cb blitCommandEncoder];
45+
[b copyFromTexture:texture sourceSlice:0 sourceLevel:0 sourceOrigin:MTLOriginMake(0,0,0) sourceSize:MTLSizeMake(4,4,1) toBuffer:out destinationOffset:0 destinationBytesPerRow:256 destinationBytesPerImage:1024];[b endEncoding];[cb commit];[cb waitUntilCompleted];
46+
CHECK(cb.status==MTLCommandBufferStatusCompleted,"direct GPU readback completes");
47+
for(int y=0;y<4;y++)for(int x=0;x<16;x++)CHECK(((uint8_t*)out.contents)[y*256+x]==(want&&filled?0x5a:0),"first-use GPU bytes equal published source or excluded zero");
48+
// A later lookup cannot overwrite a GPU-written texture with old pool data.
49+
mtl_sub_begin();id<MTLTexture> again=tex_by_id(1);CHECK(again==texture,"initial fill is performed only for a new allocation");mtl_sub_commit();mtl_drain();
50+
CHECK(g_bp_up_bytes==uploads+(want&&filled?64:0),"cached lookup never repeats the initial upload");
51+
mtl_reg_tex(1,NULL,NULL);API(nvnTextureFinalize)(&tex);API(nvnMemoryPoolFinalize)(&pool);defer_drain();munmap(mem,length);
52+
#undef API
53+
}
54+
int main(void){@autoreleasepool{
55+
mtl_init();
56+
initial_case(NVN_MEMORY_POOL_FLAGS_CPU_NO_ACCESS,0,0,1,1);
57+
initial_case(NVN_MEMORY_POOL_FLAGS_CPU_UNCACHED,0,0,1,1);
58+
initial_case(NVN_MEMORY_POOL_FLAGS_CPU_CACHED,0,1,1,1);
59+
if(!DM_GAME_TEX_FLUSH_UPLOAD) initial_case(NVN_MEMORY_POOL_FLAGS_CPU_CACHED,0,2,1,1);
60+
initial_case(NVN_MEMORY_POOL_FLAGS_CPU_CACHED,0,0,1,0);
61+
initial_case(NVN_MEMORY_POOL_FLAGS_CPU_UNCACHED,NVN_TEXTURE_FLAGS_COMPRESSIBLE,0,1,0);
62+
initial_case(NVN_MEMORY_POOL_FLAGS_CPU_NO_ACCESS,0,0,0,0);
63+
printf("legacy initial content: %d checks, %d failed\n",checks,failed);return failed!=0;
64+
}}

‎docs/reference/nvn-objects.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,18 @@ Games read this count as a position in their own command memory and size their h
284284
- **Fix:** `tex_init_fill` (`nvn_mtl.m`) also fills a new texture in a `CPU_UNCACHED` pool from its storage once, at first use, when the storage holds data; the same exclusions as for `CPU_NO_ACCESS` pools apply (packaged layouts, `COMPRESSIBLE`, multisample, depth / stencil, sparse). The log says `initial content: … a CPU_UNCACHED pool's`. A texture the CPU writes again after its first use still needs the profile's keys.
285285
- **Found on:** [Donkey Kong Country: Tropical Freeze](../../dm/games/0100C1F0051B6000/README.md): the 912×512 ASTC 5×5 title logo (242,374 of 376,832 storage bytes non-zero at first use, no packaged layout) and the menus' ASTC panels; with the fill and neither profile key, the title and every menu draw.
286286

287+
The legacy renderer now applies the same one-time initialization through
288+
`legacy_initial_content`. Previously it unconditionally zeroed new private
289+
textures, including published pool content. `t_legacy_initial_content` uses
290+
production NVN pool/texture entry points and direct GPU readback to cover
291+
CPU_NO_ACCESS, CPU_UNCACHED, cached storage flushed before or after texture
292+
creation, and negative controls for unflushed cached storage, COMPRESSIBLE
293+
textures and zero content. A subsequent lookup keeps the allocation and never
294+
repeats the upload. The initial regression had 264 failures; the repaired
295+
seven-case GPU run passes (Mac M4, not an iPad gameplay result). It does not
296+
enable per-game dirty-page upload flags or establish that every black-screen
297+
cause is resolved.
298+
287299
### Mapped-range flushes taken as uploads (`tex_flush_upload`)
288300

289301
- **Symptom:** a movie plays black (Age of Calamity: its intro movie under the "+ Skip" prompt).

0 commit comments

Comments
 (0)