From 4f1d07f9a873ca3b6670fa0393483034e4fdf1e4 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 17 Feb 2024 15:46:13 -0800 Subject: Fix constness in model loader. --- gfx/src/asset/model.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/gfx/src/asset/model.c b/gfx/src/asset/model.c index e0df078..73ce731 100644 --- a/gfx/src/asset/model.c +++ b/gfx/src/asset/model.c @@ -673,7 +673,7 @@ static void load_textures_lazy( /// the texture, and then defines the sampler shader uniform. static bool load_texture_and_uniform( const cgltf_data* data, Gfx* gfx, const cgltf_texture_view* texture_view, - TextureType texture_type, Texture** textures, + TextureType texture_type, const Texture** textures, LoadTextureCmd* load_texture_cmds, int* next_uniform, MaterialDesc* desc) { assert(data); assert(gfx); @@ -726,7 +726,7 @@ static bool load_texture_and_uniform( /// materials and the textures used by them. static bool load_materials( const cgltf_data* data, Gfx* gfx, LoadTextureCmd* load_texture_cmds, - Texture** textures, Material** materials) { + const Texture** textures, Material** materials) { assert(data); assert(gfx); assert(materials); @@ -946,8 +946,10 @@ static bool load_meshes( const cgltf_accessor* accessor = prim->indices; const cgltf_buffer_view* view = prim->indices->buffer_view; const cgltf_size buffer_index = view->buffer - data->buffers; + assert(buffer_index < data->buffers_count); - const Buffer* buffer = buffers[buffer_index]; + Buffer* buffer = buffers[buffer_index]; + const cgltf_size component_size = get_component_size(accessor->component_type); switch (component_size) { @@ -984,8 +986,9 @@ static bool load_meshes( const cgltf_buffer_view* view = accessor->buffer_view; const cgltf_size offset = accessor->offset + view->offset; const cgltf_size buffer_index = view->buffer - data->buffers; + assert(buffer_index < data->buffers_count); - const Buffer* buffer = buffers[buffer_index]; + Buffer* buffer = buffers[buffer_index]; BufferView2d* buffer_view_2d = 0; BufferView3d* buffer_view_3d = 0; @@ -1532,7 +1535,7 @@ static Model* load_scene( Buffer** tangent_buffers = 0; Buffer** buffers = 0; LoadTextureCmd* load_texture_cmds = 0; - Texture** textures = 0; + const Texture** textures = 0; // Textures are owned by asset cache. Material** materials = 0; Geometry** geometries = 0; Mesh** meshes = 0; @@ -1659,13 +1662,6 @@ cleanup: free(load_texture_cmds); } if (textures) { - if (!success) { - for (cgltf_size i = 0; i < data->textures_count; ++i) { - if (textures[i]) { - gfx_destroy_texture(render_backend, &textures[i]); - } - } - } free(textures); } if (materials) { -- cgit v1.2.3