/* Runs real Vulkan buffer/fence regressions on an Android device. The include * exposes the backend's private cache/batch state without a production API. */ #include "../../gfx/vulkan/nvn_vk.c" #include static void wait_current(void) { uint64_t ticket=mtl_sub_commit();assert(mtl_wait(ticket,UINT64_MAX)); } static void copy_input(uint32_t *guest,VBuffer *readback) { uint64_t offset=0;VPool *input=input_read((uint64_t)(uintptr_t)guest,16,&offset); all_barrier();VkBufferCopy copy={offset,0,16};vkCmdCopyBuffer(G.batch->command,input->v.b,readback->b,1,©); } /* Zero-copy pools (host import or mremap of Vulkan memory): GPU reads the guest bytes * current at execution and writes land in guest memory without writeback. */ static int direct_main(void) { mtl_init(); if(!G.host_import && !G.get_memory_fd && !G.get_ahb_properties){fprintf(stderr,"VK_PERF_TEST_SKIP direct: no zero-copy path\n");return 0;} uint32_t *guest=aligned_alloc(4096,4096);assert(guest);memset(guest,0,4096);mtl_pool_add(guest,4096); uint64_t offset=0;VPool *pool=pool_find((uint64_t)(uintptr_t)guest,16,&offset);assert(pool && pool->direct && offset==0); VBuffer first=buffer_make(16,VK_BUFFER_USAGE_TRANSFER_DST_BIT); for(unsigned i=0;i<4;i++)guest[i]=i+10; mtl_sub_begin();copy_input(guest,&first);wait_current(); for(unsigned i=0;i<4;i++)assert(((uint32_t *)first.map)[i]==i+10); uint32_t update[4]={31,32,33,34}; mtl_sub_begin();all_barrier();vkCmdUpdateBuffer(G.batch->command,pool->v.b,offset,16,update);mark_write((uint64_t)(uintptr_t)guest,16); assert(!G.batch->nwrites);wait_current();assert(!memcmp(guest,update,16)); /* Identical driver tables reuse one slot within a batch; changes get a new one. */ uint8_t table[TABLE_BYTES]={0};mtl_sub_begin(); VkDescriptorBufferInfo t0=table_bind(0,0,table),t1=table_bind(0,0,table);table[0x168]=7;VkDescriptorBufferInfo t2=table_bind(0,0,table); assert(t0.buffer==t1.buffer && t0.offset==t1.offset && t2.offset!=t0.offset && t2.range==65536);wait_current(); /* Coherence over a cached mapping: lines the CPU holds must observe GPU * fills, and CPU stores must reach a GPU copy without explicit flushes. */ size_t big=8<<20;uint32_t *wide=aligned_alloc(4096,big);assert(wide); for(size_t i=0;idirect); uint64_t sum=0;for(size_t i=0;icommand,wp->v.b,at,sizeof(fill),fill);wait_current(); for(size_t i=0;icommand,wp->v.b,check.b,1,&whole);wait_current(); for(size_t i=0;icommand,b.b,dst.b,1,&c); t=dm_android_nvn_clock_ns();wait_current();gpu+=ms_since(t);}gpu/=4;assert(!memcmp(dst.map,src,n)); fprintf(stderr,"VK_BENCH type=%u flags=%#x MiB=16 alloc_map_ms=%.3f memset_ms=%.3f write_ms=%.3f read_cmp_ms=%.3f gpu_copy_ms=%.3f\n",type,f,alloc,zero,write,read,gpu); buffer_free(&b);} mtl_finish();buffer_free(&dst);free(src);return 0; } /* Game-shaped input load: ~11k distinct ~3 KiB snapshots over 4 batches per frame. */ static double input_frames(int frames) { size_t n=64<<20;uint8_t *guest=aligned_alloc(4096,n);assert(guest);memset(guest,1,n);mtl_pool_add(guest,n); uint64_t t=dm_android_nvn_clock_ns(); for(int f=0;f1 && !strcmp(argv[1],"bench"))return bench_main(); if(argc>1 && !strcmp(argv[1],"input")){setenv("DM_VK_POOL_COPY","1",1);mtl_init(); double recycled=input_frames(60);temporary_keep_bytes=0;double fresh=input_frames(60); fprintf(stderr,"VK_BENCH input 60 frames recycled_ms=%.1f fresh_ms=%.1f\n",recycled,fresh);return 0;} if(argc>1 && !strcmp(argv[1],"direct"))return direct_main(); setenv("DM_VK_POOL_COPY","1",1); /* staged copy path: snapshot semantics */ mtl_init(); /* Deliberate hash collision must never conflate distinct effective states. */ PipelineKey a={0},b={0};key_word(&a,71);key_word(&b,72); Pso *pa=pso_insert(&a,12345),*pb=pso_insert(&b,12345); assert(pso_find(&a,12345)==pa && pso_find(&b,12345)==pb); uint32_t *guest=aligned_alloc(4096,4096);assert(guest);memset(guest,0,4096);mtl_pool_add(guest,4096); VBuffer first=buffer_make(16,VK_BUFFER_USAGE_TRANSFER_DST_BIT),second=buffer_make(16,VK_BUFFER_USAGE_TRANSFER_DST_BIT); for(unsigned i=0;i<4;i++)guest[i]=i+10; mtl_sub_begin();copy_input(guest,&first);uint64_t ticket=mtl_sub_commit(); assert(G.batch->recording && !G.batch->pending); /* Logical commit stays batched. */ for(unsigned i=0;i<4;i++)guest[i]=i+20; mtl_sub_begin();copy_input(guest,&second);wait_current(); assert(mtl_wait(ticket,0)); for(unsigned i=0;i<4;i++)assert(((uint32_t *)first.map)[i]==i+10 && ((uint32_t *)second.map)[i]==i+20); /* GPU updates override stale guest bytes in a subsequent input snapshot. */ mtl_sub_begin();uint64_t offset=0;VPool *pool=pool_find((uint64_t)(uintptr_t)guest,16,&offset);pool_materialize(pool); uint32_t update[4]={31,32,33,34};all_barrier();vkCmdUpdateBuffer(G.batch->command,pool->v.b,offset,16,update);mark_write((uint64_t)(uintptr_t)guest,16); copy_input(guest,&first);wait_current();assert(!memcmp(first.map,update,16));assert(!memcmp(guest,update,16)); /* Across separately submitted batches the newer write must not contaminate * the older batch's CPU-visible writeback snapshot. */ mtl_sub_begin();all_barrier();update[0]=41;vkCmdUpdateBuffer(G.batch->command,pool->v.b,offset,16,update);mark_write((uint64_t)(uintptr_t)guest,16); ticket=mtl_sub_commit();pthread_mutex_lock(&G.lock);flush_locked();Batch *old=G.batch;pthread_mutex_unlock(&G.lock); mtl_sub_begin();copy_input(guest,&second);all_barrier();update[0]=51;vkCmdUpdateBuffer(G.batch->command,pool->v.b,offset,16,update);mark_write((uint64_t)(uintptr_t)guest,16); uint64_t newer=mtl_sub_commit();pthread_mutex_lock(&G.lock);flush_locked(); if(old->pending){assert(retire_locked(old,UINT64_MAX));assert(guest[0]==41);} pthread_mutex_unlock(&G.lock);assert(mtl_wait(newer,UINT64_MAX));assert(guest[0]==51 && ((uint32_t *)second.map)[0]==41); assert(!mtl_wait(newer+100,0)); /* An event must reach a guest that polls memory without another API call. */ volatile uint32_t event=0;mtl_sub_begin();mtl_sub_event(&event,0,73);mtl_sub_commit(); uint64_t deadline=dm_android_nvn_clock_ns()+1000000000; while(!atomic_load((_Atomic uint32_t *)&event) && dm_android_nvn_clock_ns()nextra_descriptors==1); /* Capacity splits keep the logical fence reserved for the final piece. */ uint64_t before=G.fence;G.batch->ncounter=3000; for(int i=0;i<3000;i++)G.batch->counter_ops[i]=(CounterOp){0,0,0}; batch_checkpoint();assert(G.fence==before && G.batch->recording);wait_current(); PFN_vkDestroyDescriptorSetLayout destroy_layout=(PFN_vkDestroyDescriptorSetLayout)dm_vk_get(G.instance,"vkDestroyDescriptorSetLayout");assert(destroy_layout);destroy_layout(G.device,shader.layout,NULL); /* Slot rotation and destruction drain work before recycling resources. */ G.profile_gpu_ns=0;unsigned pending_max=0; for(unsigned i=0;i<2*BATCH_SLOTS+4;i++){mtl_sub_begin();copy_input(guest,&first);mtl_sub_commit();pthread_mutex_lock(&G.lock);flush_locked(); unsigned pending=0;for(unsigned j=0;jpending_max)pending_max=pending;pthread_mutex_unlock(&G.lock);} /* Every retired batch contributes its begin/end GPU span. */ mtl_finish();assert(!G.timestamp_bits || G.profile_gpu_ns>0); fprintf(stderr,"ring slots=%u pending_max=%u gpu_ms=%.3f\n",BATCH_SLOTS,pending_max,G.profile_gpu_ns/1e6); /* Retired transient buffers are recycled by size class, not reallocated. */ mtl_finish();mtl_sub_begin();VBuffer *recycled=temporary(5000,VK_BUFFER_USAGE_TRANSFER_SRC_BIT);VkBuffer handle=recycled->b;assert(recycled->size==8192);wait_current(); mtl_finish();mtl_sub_begin();assert(temporary(8192,VK_BUFFER_USAGE_TRANSFER_DST_BIT)->b==handle);wait_current();mtl_finish(); mtl_pool_del(guest);mtl_finish();buffer_free(&first);buffer_free(&second);free(guest); fprintf(stderr,"VK_PERF_TEST_PASS snapshots collisions GPU_writebacks fences ring_rotation gpu_timestamps events descriptor_chunks capacity_split temporary_recycle\n");return 0; }