|
| 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 | +}} |
0 commit comments