From d3d2bc0c97e5bd29094e1031391ada2ffb0a7153 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Fri, 31 Oct 2025 19:57:46 -0700 Subject: Remove unnecessary heap allocations --- src/asset/model.c | 151 +++++++++++++++--------------------------------------- 1 file changed, 42 insertions(+), 109 deletions(-) diff --git a/src/asset/model.c b/src/asset/model.c index f5131d2..60e7ad1 100644 --- a/src/asset/model.c +++ b/src/asset/model.c @@ -1580,7 +1580,6 @@ static void load_nodes( // Add SceneObject, Camera or Lights. // TODO: Handle lights once they are implemented in the gfx library. - assert(!nodes[n]); if (node->mesh) { const cgltf_size mesh_index = node->mesh - data->meshes; assert(mesh_index < data->meshes_count); @@ -1715,39 +1714,21 @@ static Model* load_scene( LOGD("Filepath: %s", mstring_cstr(filepath)); LOGD("Directory: %s", mstring_cstr(&directory)); - Buffer** tangent_buffers = 0; - Buffer** buffers = 0; - LoadTextureCmd* load_texture_cmds = 0; - const Texture** textures = 0; // Textures are owned by asset cache. - Material** materials = 0; - Geometry** geometries = 0; - Mesh** meshes = 0; - AnimaDesc* anima_desc = 0; - SceneObject** scene_objects = 0; - SceneNode** scene_nodes = 0; - Anima* anima = 0; - SceneNode* root_node = 0; - Model* model = 0; - - tangent_buffers = calloc(num_tangent_buffers, sizeof(Buffer*)); - buffers = calloc(data->buffers_count, sizeof(Buffer*)); - textures = calloc(data->textures_count, sizeof(Texture*)); - materials = calloc(data->materials_count, sizeof(Material*)); - geometries = calloc(primitive_count, sizeof(Geometry*)); - meshes = calloc(primitive_count, sizeof(Mesh*)); - scene_objects = calloc(data->meshes_count, sizeof(SceneObject*)); - scene_nodes = calloc(data->nodes_count, sizeof(SceneNode*)); // A glTF scene does not necessarily have textures. Materials can be given // as constants, for example. - if (data->textures_count > 0) { - load_texture_cmds = calloc(data->textures_count, sizeof(LoadTextureCmd)); - } - - if (!buffers || !tangent_buffers || - ((data->textures_count > 0) && !load_texture_cmds) || !textures || - !materials || !geometries || !meshes || !scene_objects || !scene_nodes) { - goto cleanup; - } + // gfx textures are owned by asset cache. + Buffer* tangent_buffers[num_tangent_buffers]; + Buffer* buffers[data->buffers_count]; + LoadTextureCmd load_texture_cmds[data->textures_count]; + const Texture* textures[data->textures_count]; + Material* materials[data->materials_count]; + Geometry* geometries[primitive_count]; + Mesh* meshes[primitive_count]; + SceneObject* scene_objects[data->meshes_count]; + SceneNode* scene_nodes[data->nodes_count]; + Anima* anima = 0; + SceneNode* root_node = 0; + Model* model = 0; if ((num_tangent_buffers > 0) && !load_tangent_buffers( @@ -1780,22 +1761,18 @@ static Model* load_scene( // This is an anima node if the scene has skins; otherwise it is a logical // node. if (data->skins_count > 0) { - anima_desc = calloc(1, sizeof(AnimaDesc)); - if (!anima_desc) { - goto cleanup; - } - const cgltf_size base = find_base_joint_index(data); - anima_desc->num_skeletons = data->skins_count; - anima_desc->num_animations = data->animations_count; - anima_desc->num_joints = load_skins(data, buffers, base, anima_desc); - load_animations(data, base, anima_desc); + AnimaDesc anima_desc = + (AnimaDesc){.num_skeletons = data->skins_count, + .num_animations = data->animations_count, + .num_joints = load_skins(data, buffers, base, &anima_desc)}; + load_animations(data, base, &anima_desc); compute_joint_bounding_boxes( - data, anima_desc->num_joints, anima_desc->joints); + data, anima_desc.num_joints, anima_desc.joints); - anima = gfx_make_anima(anima_desc); + anima = gfx_make_anima(&anima_desc); root_node = gfx_make_anima_node(anima); } else { root_node = gfx_make_node(); @@ -1812,86 +1789,42 @@ static Model* load_scene( cleanup: // The arrays of resources are no longer needed. The resources themselves are // destroyed only if this function fails. - if (tangent_buffers) { - if (!success) { - for (cgltf_size i = 0; i < num_tangent_buffers; ++i) { - if (tangent_buffers[i]) { - gfx_destroy_buffer(gfxcore, &tangent_buffers[i]); - } + if (!success) { + for (cgltf_size i = 0; i < num_tangent_buffers; ++i) { + if (tangent_buffers[i]) { + gfx_destroy_buffer(gfxcore, &tangent_buffers[i]); } } - free(tangent_buffers); - } - if (buffers) { - if (!success) { - for (cgltf_size i = 0; i < data->buffers_count; ++i) { - if (buffers[i]) { - gfx_destroy_buffer(gfxcore, &buffers[i]); - } + for (cgltf_size i = 0; i < data->buffers_count; ++i) { + if (buffers[i]) { + gfx_destroy_buffer(gfxcore, &buffers[i]); } } - free(buffers); - } - if (load_texture_cmds) { - free(load_texture_cmds); - } - if (textures) { - free(textures); - } - if (materials) { - if (!success) { - for (cgltf_size i = 0; i < data->materials_count; ++i) { - if (materials[i]) { - gfx_destroy_material(&materials[i]); - } + for (cgltf_size i = 0; i < data->materials_count; ++i) { + if (materials[i]) { + gfx_destroy_material(&materials[i]); } } - free(materials); - } - if (geometries) { - if (!success) { - for (size_t i = 0; i < primitive_count; ++i) { - if (geometries[i]) { - gfx_destroy_geometry(gfxcore, &geometries[i]); - } + for (size_t i = 0; i < primitive_count; ++i) { + if (geometries[i]) { + gfx_destroy_geometry(gfxcore, &geometries[i]); } } - free(geometries); - } - if (meshes) { - if (!success) { - for (size_t i = 0; i < primitive_count; ++i) { - if (meshes[i]) { - gfx_destroy_mesh(&meshes[i]); - } + for (size_t i = 0; i < primitive_count; ++i) { + if (meshes[i]) { + gfx_destroy_mesh(&meshes[i]); } } - free(meshes); - } - if (anima_desc) { - free(anima_desc); - } - if (scene_objects) { - if (!success) { - for (cgltf_size i = 0; i < data->meshes_count; ++i) { - if (scene_objects[i]) { - gfx_destroy_object(&scene_objects[i]); - } + for (cgltf_size i = 0; i < data->meshes_count; ++i) { + if (scene_objects[i]) { + gfx_destroy_object(&scene_objects[i]); } } - free(scene_objects); - } - if (scene_nodes) { - if (!success) { - for (cgltf_size i = 0; i < data->nodes_count; ++i) { - if (scene_nodes[i]) { - gfx_destroy_node(&scene_nodes[i]); - } + for (cgltf_size i = 0; i < data->nodes_count; ++i) { + if (scene_nodes[i]) { + gfx_destroy_node(&scene_nodes[i]); } } - free(scene_nodes); - } - if (!success) { if (root_node) { gfx_destroy_node(&root_node); // Node owns the anima. } else if (anima) { -- cgit v1.2.3