From 82c03019867ed57d35932e5eb015c7e77f8bf1ea Mon Sep 17 00:00:00 2001 From: quou Date: Sat, 28 Dec 2024 22:56:26 +1100 Subject: Improve memory efficiency of model loading --- model.cpp | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/model.cpp b/model.cpp index 7ae69f3..dac91e4 100644 --- a/model.cpp +++ b/model.cpp @@ -21,9 +21,9 @@ Asset* Model_Loader::load(Arena* a, Arena* s, Pack_File* f) { uint16_t* indices; Model* r; Mesh* meshes; - void* vram; Context* ctx; Buffer_Id stage_verts, stage_indices; + (void)s; pack_read(f, magic, 4); if ( magic[0] != 'M' || @@ -37,11 +37,21 @@ Asset* Model_Loader::load(Arena* a, Arena* s, Pack_File* f) { pack_read(f, &vbo_size, 4); pack_read(f, &ibo_size, 4); pack_read(f, &node_count, 4); - verts = (uint8_t*)arena_alloc(s, vbo_size); - indices = (uint16_t*)arena_alloc(s, ibo_size); r = (Model*)arena_alloc(a, sizeof *r + node_count * sizeof(Mesh)); r->mesh_count = node_count; meshes = r->get_meshes(); + stage_verts = dev->create_buffer( + vbo_size, + Buffer_Flags::copy_src | + Buffer_Flags::cpu_readwrite + ); + stage_indices = dev->create_buffer( + ibo_size, + Buffer_Flags::copy_src | + Buffer_Flags::cpu_readwrite + ); + verts = (uint8_t*)dev->map_buffer(stage_verts, 0, vbo_size); + indices = (uint16_t*)dev->map_buffer(stage_indices, 0, ibo_size); for (i = 0, coff = 0, icoff = 0, vcoff = 0; i < node_count; i++) { Mesh& mesh = meshes[i]; int vertex_size, vertex_count; @@ -68,21 +78,7 @@ Asset* Model_Loader::load(Arena* a, Arena* s, Pack_File* f) { coff += vertex_size * vertex_count; vcoff += vertex_count; } - stage_verts = dev->create_buffer( - vbo_size, - Buffer_Flags::copy_src | - Buffer_Flags::cpu_readwrite - ); - stage_indices = dev->create_buffer( - ibo_size, - Buffer_Flags::copy_src | - Buffer_Flags::cpu_readwrite - ); - vram = dev->map_buffer(stage_verts, 0, vbo_size); - memcpy(vram, verts, vbo_size); dev->unmap_buffer(stage_verts); - vram = dev->map_buffer(stage_indices, 0, ibo_size); - memcpy(vram, indices, ibo_size); dev->unmap_buffer(stage_indices); r->vbo = dev->create_buffer( vbo_size, -- cgit v1.2.3-54-g00ecf