Mirror the iOS layout instead of nesting Android inside dm/host:
- dm/android/app GameActivity app (manifest, java, res, build_apk.sh)
- dm/android/Sources front_app.c, touch_app.c, front_headless.c
- dm/android/Makefile entry point (apk / lib / install / headless / tests);
rules in build.mk (was dm/host/Makefile.android),
run with dm/host as cwd; outputs in dm/android/build/
- dm/android/tests t_vk_perf
- dm/gfx/vulkan nvn_vk.c and friends, next to the Metal backend
Only include paths, build paths and docs change; no code changes.
The Mac / iOS source lists do not pick up gfx/vulkan.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
148 lines
13 KiB
C
148 lines
13 KiB
C
/* 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 <assert.h>
|
|
|
|
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;i<big/4;i++)wide[i]=(uint32_t)i;mtl_pool_add(wide,big);
|
|
VPool *wp=pool_find((uint64_t)(uintptr_t)wide,big,&offset);assert(wp && wp->direct);
|
|
uint64_t sum=0;for(size_t i=0;i<big/4;i++)sum+=wide[i];assert(sum==(uint64_t)(big/4)*(big/4-1)/2);
|
|
static uint32_t fill[16384];for(size_t i=0;i<16384;i++)fill[i]=0x5a5a5a5au;
|
|
mtl_sub_begin();all_barrier();for(size_t at=0;at<big;at+=sizeof(fill))vkCmdUpdateBuffer(G.batch->command,wp->v.b,at,sizeof(fill),fill);wait_current();
|
|
for(size_t i=0;i<big/4;i++)assert(wide[i]==0x5a5a5a5au);
|
|
for(size_t i=0;i<big/4;i++)wide[i]=(uint32_t)i*3;
|
|
VBuffer check=buffer_make(big,VK_BUFFER_USAGE_TRANSFER_DST_BIT);
|
|
mtl_sub_begin();all_barrier();VkBufferCopy whole={0,0,big};vkCmdCopyBuffer(G.batch->command,wp->v.b,check.b,1,&whole);wait_current();
|
|
for(size_t i=0;i<big/4;i++)assert(((uint32_t *)check.map)[i]==(uint32_t)i*3);
|
|
mtl_pool_del(wide);buffer_free(&check);free(wide);
|
|
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()<deadline){struct timespec pause={0,1000000};nanosleep(&pause,NULL);}
|
|
assert(event==73);
|
|
guest[5]=99;mtl_pool_del(guest);assert(guest[0]==31 && guest[5]==99);guest[6]=1; /* bytes survive unremap, pages stay writable */
|
|
mtl_finish();buffer_free(&first);free(guest);
|
|
fprintf(stderr,"VK_PERF_TEST_PASS direct_pool gpu_write_in_place cached_coherence driver_table_reuse events\n");return 0;
|
|
}
|
|
/* Memory-type microbenchmark for upload arenas: CPU write, CPU read-back
|
|
* (snapshot dedupe memcmp) and GPU copy throughput per host-visible type. */
|
|
static double ms_since(uint64_t t){return (dm_android_nvn_clock_ns()-t)/1e6;}
|
|
static int bench_main(void) {
|
|
mtl_init();size_t n=16<<20;uint8_t *src=malloc(n);assert(src);for(size_t i=0;i<n;i++)src[i]=(uint8_t)(i*7);
|
|
VBuffer dst=buffer_make(n,VK_BUFFER_USAGE_TRANSFER_DST_BIT);
|
|
for(uint32_t type=0;type<G.memory.memoryTypeCount;type++){VkMemoryPropertyFlags f=G.memory.memoryTypes[type].propertyFlags;
|
|
|
|
if(!(f&VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)&&type)continue;VBuffer b={.size=n};VkBufferCreateInfo info={.sType=VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,.size=n,.usage=VK_BUFFER_USAGE_TRANSFER_SRC_BIT|VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT|VK_BUFFER_USAGE_STORAGE_BUFFER_BIT|VK_BUFFER_USAGE_VERTEX_BUFFER_BIT|VK_BUFFER_USAGE_INDEX_BUFFER_BIT|VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT|VK_BUFFER_USAGE_TRANSFER_DST_BIT};
|
|
CHECK(vkCreateBuffer(G.device,&info,NULL,&b.b));VkMemoryRequirements r;vkGetBufferMemoryRequirements(G.device,b.b,&r);
|
|
if(type==0)fprintf(stderr,"VK_BENCH memoryTypeBits=%#x\n",r.memoryTypeBits);
|
|
if(!(r.memoryTypeBits&(1u<<type))){vkDestroyBuffer(G.device,b.b,NULL);continue;}
|
|
if(!(f&VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)){vkDestroyBuffer(G.device,b.b,NULL);continue;}
|
|
VkMemoryAllocateInfo a={.sType=VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,.allocationSize=r.size,.memoryTypeIndex=type};
|
|
uint64_t t=dm_android_nvn_clock_ns();CHECK(vkAllocateMemory(G.device,&a,NULL,&b.m));CHECK(vkBindBufferMemory(G.device,b.b,b.m,0));
|
|
void *m;CHECK(vkMapMemory(G.device,b.m,0,n,0,&m));b.map=m;double alloc=ms_since(t);
|
|
t=dm_android_nvn_clock_ns();memset(b.map,0,n);double zero=ms_since(t);
|
|
t=dm_android_nvn_clock_ns();for(int k=0;k<4;k++)memcpy(b.map,src,n);double write=ms_since(t)/4;
|
|
t=dm_android_nvn_clock_ns();int diff=0;for(int k=0;k<4;k++)diff|=memcmp(b.map,src,n);double read=ms_since(t)/4;assert(!diff);
|
|
double gpu=0;for(int k=0;k<4;k++){mtl_sub_begin();VkBufferCopy c={0,0,n};vkCmdCopyBuffer(G.batch->command,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;f<frames;f++)for(int b=0;b<4;b++){mtl_sub_begin();
|
|
for(int i=0;i<2750;i++){uint64_t at=((uint64_t)(f*11000+b*2750+i)*3072)%(n-4096),off;input_read((uint64_t)(uintptr_t)guest+at,3072,&off);}
|
|
mtl_sub_commit();pthread_mutex_lock(&G.lock);flush_locked();pthread_mutex_unlock(&G.lock);}
|
|
mtl_finish();double ms=ms_since(t);mtl_pool_del(guest);free(guest);return ms;
|
|
}
|
|
int main(int argc,char **argv) {
|
|
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()<deadline){struct timespec pause={0,1000000};nanosleep(&pause,NULL);}
|
|
assert(event==73);
|
|
/* Descriptor chunks grow within a batch without invalidating old sets. */
|
|
VkDescriptorSetLayoutBinding binding={.binding=0,.descriptorType=VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,.descriptorCount=1,.stageFlags=VK_SHADER_STAGE_VERTEX_BIT};
|
|
VkDescriptorSetLayoutCreateInfo layout_info={.sType=VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,.bindingCount=1,.pBindings=&binding};
|
|
VShader shader={0};shader.h.nb=1;shader.b[0].type=VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
|
CHECK(vkCreateDescriptorSetLayout(G.device,&layout_info,NULL,&shader.layout));
|
|
mtl_sub_begin();for(unsigned i=0;i<8200;i++)assert(shader_set_allocate(&shader));assert(G.batch->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;j<BATCH_SLOTS;j++)pending+=G.batches[j].pending;if(pending>pending_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;
|
|
}
|