/* 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 mip_policy_checks(void) { int feedback=G.feedback_layout;G.feedback_layout=0; int subresources=G.subresources;G.subresources=1; VTexture t={.serial=9001,.format=VK_FORMAT_R8G8B8A8_UNORM}; t.source.serial=t.serial;t.source.d.target=NVN_TEXTURE_TARGET_2D;t.source.d.levels=4;t.source.L.layers=1; G.state.rt.n=1;G.state.rt.color[0]=&t.source;G.state.rt.has_cview[0]=1; G.state.rt.cview[0]=(view_t){.set_levels=1,.base_level=1,.nlevels=1}; TSlot *slots=G.tslot;size_t capacity=G.texture_slots;int pool_n=G.texture_pool_n; G.tslot=calloc(2,sizeof *G.tslot);assert(G.tslot);G.texture_slots=2;G.texture_pool_n=2; G.tslot[1]=(TSlot){.live=1,.texture=t.source,.has_view=1,.view={.set_levels=1,.base_level=0,.nlevels=1}}; VShader vs={0},ps={0};vs.h.nb=ps.h.nb=1;ps.h.stage=1;vs.b[0].kind=ps.b[0].kind=4; G.state.tex[0][0]=G.state.tex[1][0]=1;G.nimage_uses=0; collect_image_uses(&vs);assert(disjoint_mip_sampling(&t)); /* Another stage's overlapping view must disable the optimization for both. */ G.tslot[1].view.base_level=1;collect_image_uses(&ps);assert(!disjoint_mip_sampling(&t)); G.nimage_uses=0;G.tslot[1].view.base_level=0;collect_image_uses(&vs); G.image_uses[0].roles|=2;assert(!disjoint_mip_sampling(&t));G.image_uses[0].roles=1; G.image_uses[0].roles|=4;assert(!disjoint_mip_sampling(&t));G.image_uses[0].roles=1; G.feedback_layout=1;assert(!disjoint_mip_sampling(&t));G.feedback_layout=0; G.tslot[1].has_view=0;G.nimage_uses=0;collect_image_uses(&vs);assert(!disjoint_mip_sampling(&t)); G.tslot[1].has_view=1;G.nimage_uses=0;collect_image_uses(&vs);assert(disjoint_mip_sampling(&t)); G.state.rt.n=2;G.state.rt.color[1]=&t.source;G.state.rt.has_cview[1]=0;assert(!disjoint_mip_sampling(&t));G.state.rt.n=1; t.source.L.layers=2;assert(!disjoint_mip_sampling(&t));t.source.L.layers=1; G.subresources=0;assert(!disjoint_mip_sampling(&t));G.subresources=subresources; free(G.tslot);G.tslot=slots;G.texture_slots=capacity;G.texture_pool_n=pool_n; memset(&G.state.rt,0,sizeof G.state.rt);G.state.tex[0][0]=G.state.tex[1][0]=0;G.nimage_uses=0;G.feedback_layout=feedback; } /* Real GPU readback verifies both mips survive split layouts and a later * whole-image copy transition; this fixture needs no game/shader assets. */ static void mip_gpu_checks(void) { mip_policy_checks(); VTexture t={.serial=9002,.format=VK_FORMAT_R8G8B8A8_UNORM,.width=8,.height=8,.depth=1}; t.source.d.levels=4;t.source.L.layers=1; VkImageCreateInfo ci={.sType=VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,.imageType=VK_IMAGE_TYPE_2D,.format=t.format, .extent={8,8,1},.mipLevels=4,.arrayLayers=1,.samples=VK_SAMPLE_COUNT_1_BIT,.tiling=VK_IMAGE_TILING_OPTIMAL, .usage=VK_IMAGE_USAGE_TRANSFER_SRC_BIT|VK_IMAGE_USAGE_TRANSFER_DST_BIT|VK_IMAGE_USAGE_SAMPLED_BIT|VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT}; CHECK(vkCreateImage(G.device,&ci,NULL,&t.image));VkMemoryRequirements req;vkGetImageMemoryRequirements(G.device,t.image,&req); VkMemoryAllocateInfo ai={.sType=VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,.allocationSize=req.size,.memoryTypeIndex=memory_type(req.memoryTypeBits,VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)}; CHECK(vkAllocateMemory(G.device,&ai,NULL,&t.memory));CHECK(vkBindImageMemory(G.device,t.image,t.memory,0)); VBuffer readback=buffer_make(320,VK_BUFFER_USAGE_TRANSFER_DST_BIT); mtl_sub_begin();texture_transition(&t,VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); for(unsigned i=0;i<4;i++) { VkClearColorValue value={.float32={i==0?1:0,i==1?1:0,0,1}}; VkImageSubresourceRange range={VK_IMAGE_ASPECT_COLOR_BIT,i,1,0,1}; vkCmdClearColorImage(G.batch->command,t.image,VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,&value,1,&range); } texture_transition_levels(&t,0,1,VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); texture_transition_levels(&t,1,1,VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);assert(t.layouts.mixed); texture_transition_levels(&t,0,1,VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); texture_transition_levels(&t,1,1,VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); texture_transition(&t,VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);assert(!t.layouts.mixed); for(unsigned i=0;i<2;i++) { VkBufferImageCopy copy={.bufferOffset=i?256:0,.imageSubresource={VK_IMAGE_ASPECT_COLOR_BIT,i,0,1},.imageExtent={8>>i,8>>i,1}}; vkCmdCopyImageToBuffer(G.batch->command,t.image,t.layouts.uniform,readback.b,1,©); } wait_current(); for(unsigned i=0;i<80;i++){const uint8_t *p=readback.map+4*i;assert(p[0]==(i<64?255:0)&&p[1]==(i<64?0:255)&&p[2]==0&&p[3]==255);} buffer_free(&readback);vkDestroyImage(G.device,t.image,NULL);vkFreeMemory(G.device,t.memory,NULL); fprintf(stderr,"VK_MIP_GPU_PASS disjoint_views cross_stage_overlap storage_fallback split_layouts preserved_pixels\n"); } 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],"mips")){mtl_init();mip_gpu_checks();return 0;} if(argc>1 && !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); mip_gpu_checks(); fprintf(stderr,"VK_PERF_TEST_PASS snapshots collisions GPU_writebacks fences ring_rotation gpu_timestamps events descriptor_chunks capacity_split temporary_recycle mip_layouts\n");return 0; }