Skip to content

Commit

Permalink
model: Drop the passed reference to model on failure
Browse files Browse the repository at this point in the history
When calling model3dtx_new_*() with ref_pass(model) and it fails, the
caller still has to clean up the last reference of model, even though
(and it's also a hint) the model at that point would be NULL. Currently,
we're employing a hack around that.

Instead of this, drop the passed reference to model in model3dtx_new_*()
functions directly.

Signed-off-by: Alexander Shishkin <[email protected]>
  • Loading branch information
virtuoso committed Oct 5, 2024
1 parent 1d742b6 commit 9673533
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
1 change: 0 additions & 1 deletion core/gltf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,6 @@ int gltf_instantiate_one(struct gltf_data *gd, int mesh)
if (!txm) {
warn("failed to load texture(s) for mesh '%s'\n", gltf_mesh_name(gd, mesh));
ref_put_last(me);
ref_put_last(gd->scene->_model);
return -1;
}

Expand Down
38 changes: 31 additions & 7 deletions core/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ static int model3dtx_make(struct ref *ref)
txm->texture = &txm->_texture;
txm->normals = &txm->_normals;
list_init(&txm->entities);
list_init(&txm->entry);
return 0;
}

Expand Down Expand Up @@ -171,16 +172,24 @@ struct model3dtx *model3dtx_new2(struct model3d *model, const char *tex, const c
struct model3dtx *txm = ref_new(model3dtx);

if (!txm)
return NULL;
goto err;

txm->model = ref_get(model);
model3d_add_texture(txm, tex);
if (model3d_add_texture(txm, tex)) {
ref_put_last(txm);
return NULL;
}

if (norm)
model3d_add_texture_at(txm, GL_TEXTURE1, norm);
txm->roughness = 0.65;
txm->metallic = 0.45;

return txm;

err:
ref_put_passed(model);
return NULL;
}

struct model3dtx *model3dtx_new(struct model3d *model, const char *name)
Expand All @@ -191,48 +200,63 @@ struct model3dtx *model3dtx_new(struct model3d *model, const char *name)
struct model3dtx *model3dtx_new_from_buffer(struct model3d *model, void *buffer, size_t length)
{
if (!buffer || !length)
return NULL;
goto err;

struct model3dtx *txm = ref_new(model3dtx);

if (!txm)
return NULL;
goto err;

txm->model = ref_get(model);
model3d_add_texture_from_buffer(txm, GL_TEXTURE0, buffer, length);

return txm;

err:
ref_put_passed(model);
return NULL;
}

struct model3dtx *model3dtx_new_from_buffers(struct model3d *model, void *tex, size_t texsz, void *norm, size_t normsz)
{
if (!tex || !texsz || !norm || !normsz)
return NULL;
goto err;

struct model3dtx *txm = ref_new(model3dtx);

if (!txm)
return NULL;
goto err;

txm->model = ref_get(model);
model3d_add_texture_from_buffer(txm, GL_TEXTURE0, tex, texsz);
model3d_add_texture_from_buffer(txm, GL_TEXTURE1, norm, normsz);

return txm;

err:
ref_put_passed(model);
return NULL;
}

struct model3dtx *model3dtx_new_texture(struct model3d *model, texture_t *tex)
{
if (!tex)
goto err;

struct model3dtx *txm = ref_new(model3dtx);

if (!txm)
return NULL;
goto err;

txm->model = ref_get(model);
txm->texture = tex;
txm->external_tex = true;

return txm;

err:
ref_put_passed(model);
return NULL;
}

static void load_gl_buffer(GLint loc, void *data, GLuint type, size_t sz, GLuint *obj,
Expand Down
10 changes: 10 additions & 0 deletions core/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,16 @@ static inline void _ref_put(struct ref *ref)

#define ref_put(obj) ref_put_ref(&(obj)->ref)

#define ref_put_passed_ref(r) do { \
if ((r)->consume) { \
(r)->consume = false; \
ref_dbg("ref_put_passed_ref(%s): %d\n", _ref_name(r), (r)->count - 1); \
_ref_put(r); \
} \
} while (0)

#define ref_put_passed(obj) ref_put_passed_ref(&(obj)->ref)

#define ref_put_last_ref(r) do { \
ref_dbg("ref_put_last_ref(%s): %d\n", _ref_name(r), (r)->count - 1); \
err_on(!!--(r)->count, "'%s': %d\n", _ref_name(r), (r)->count); \
Expand Down
2 changes: 2 additions & 0 deletions core/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,8 @@ struct ui_element *ui_pocket_new(struct ui *ui, const char **tex, int nr)
model = model3d_new_quad(ui->prog, 0, 0, 0.0, 1, 1);
model3d_set_name(model, "ui_pocket_element");
txm = model3dtx_new(ref_pass(model), tex[i]);
if (!txm)
continue;
ui_add_model(ui, txm);

pic = ui_element_new(ui, p, txm, UI_AF_LEFT | UI_AF_TOP, 0, 100 * i, 100, 100);
Expand Down

0 comments on commit 9673533

Please sign in to comment.