From ea82291c90c9dbdb15e175f4b36f69d01348228c Mon Sep 17 00:00:00 2001 From: DaveH355 <101005658+DaveH355@users.noreply.github.com> Date: Mon, 2 Oct 2023 15:25:54 +0800 Subject: [PATCH 1/4] Optimize m3d mesh creation --- src/rmodels.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/rmodels.c b/src/rmodels.c index f9ceec1372bf..2e836b338dfc 100644 --- a/src/rmodels.c +++ b/src/rmodels.c @@ -5567,6 +5567,15 @@ static Model LoadVOX(const char *fileName) unsigned char *m3d_loaderhook(char *fn, unsigned int *len) { return LoadFileData((const char *)fn, (int *)len); } void m3d_freehook(void *data) { UnloadFileData((unsigned char *)data); } +// Comparison function for qsort +static int m3d_compare_faces(const void *a, const void *b) +{ + m3df_t *fa = (m3df_t *)a; + m3df_t *fb = (m3df_t *)b; + + return (fa->materialid - fb->materialid); +} + // Load M3D mesh data static Model LoadM3D(const char *fileName) { @@ -5614,6 +5623,9 @@ static Model LoadM3D(const char *fileName) // We always need a default material, so we add +1 model.materialCount++; + // Sort faces by material. + qsort(m3d->face, m3d->numface, sizeof(m3df_t), m3d_compare_faces); + model.meshes = (Mesh *)RL_CALLOC(model.meshCount, sizeof(Mesh)); model.meshMaterial = (int *)RL_CALLOC(model.meshCount, sizeof(int)); model.materials = (Material *)RL_CALLOC(model.materialCount + 1, sizeof(Material)); From dc1bd559fdda8d338d480dd7d9c3d77bb1ec5ac2 Mon Sep 17 00:00:00 2001 From: DaveH355 <101005658+DaveH355@users.noreply.github.com> Date: Mon, 2 Oct 2023 16:44:42 +0800 Subject: [PATCH 2/4] Avoid qsort() in rmodels.c --- src/rmodels.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/rmodels.c b/src/rmodels.c index 2e836b338dfc..f2fab8467d3b 100644 --- a/src/rmodels.c +++ b/src/rmodels.c @@ -5567,15 +5567,6 @@ static Model LoadVOX(const char *fileName) unsigned char *m3d_loaderhook(char *fn, unsigned int *len) { return LoadFileData((const char *)fn, (int *)len); } void m3d_freehook(void *data) { UnloadFileData((unsigned char *)data); } -// Comparison function for qsort -static int m3d_compare_faces(const void *a, const void *b) -{ - m3df_t *fa = (m3df_t *)a; - m3df_t *fb = (m3df_t *)b; - - return (fa->materialid - fb->materialid); -} - // Load M3D mesh data static Model LoadM3D(const char *fileName) { @@ -5623,8 +5614,19 @@ static Model LoadM3D(const char *fileName) // We always need a default material, so we add +1 model.materialCount++; - // Sort faces by material. - qsort(m3d->face, m3d->numface, sizeof(m3df_t), m3d_compare_faces); + // Sort faces by material (insertion) + for (i = 1; i < m3d->numface; i++) + { + m3df_t key = m3d->face[i]; + j = i - 1; + + while (j >= 0 && m3d->face[j].materialid > key.materialid) + { + m3d->face[j+1] = m3d->face[j]; + j = j - 1; + } + m3d->face[j+1] = key; + } model.meshes = (Mesh *)RL_CALLOC(model.meshCount, sizeof(Mesh)); model.meshMaterial = (int *)RL_CALLOC(model.meshCount, sizeof(int)); From e01db93e24ae2cc90b4f0571b2f1d89748337662 Mon Sep 17 00:00:00 2001 From: HU Date: Tue, 3 Oct 2023 16:41:16 +0800 Subject: [PATCH 3/4] Revert "Avoid qsort() in rmodels.c" This reverts commit dc1bd559fdda8d338d480dd7d9c3d77bb1ec5ac2. --- src/rmodels.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/rmodels.c b/src/rmodels.c index f2fab8467d3b..2e836b338dfc 100644 --- a/src/rmodels.c +++ b/src/rmodels.c @@ -5567,6 +5567,15 @@ static Model LoadVOX(const char *fileName) unsigned char *m3d_loaderhook(char *fn, unsigned int *len) { return LoadFileData((const char *)fn, (int *)len); } void m3d_freehook(void *data) { UnloadFileData((unsigned char *)data); } +// Comparison function for qsort +static int m3d_compare_faces(const void *a, const void *b) +{ + m3df_t *fa = (m3df_t *)a; + m3df_t *fb = (m3df_t *)b; + + return (fa->materialid - fb->materialid); +} + // Load M3D mesh data static Model LoadM3D(const char *fileName) { @@ -5614,19 +5623,8 @@ static Model LoadM3D(const char *fileName) // We always need a default material, so we add +1 model.materialCount++; - // Sort faces by material (insertion) - for (i = 1; i < m3d->numface; i++) - { - m3df_t key = m3d->face[i]; - j = i - 1; - - while (j >= 0 && m3d->face[j].materialid > key.materialid) - { - m3d->face[j+1] = m3d->face[j]; - j = j - 1; - } - m3d->face[j+1] = key; - } + // Sort faces by material. + qsort(m3d->face, m3d->numface, sizeof(m3df_t), m3d_compare_faces); model.meshes = (Mesh *)RL_CALLOC(model.meshCount, sizeof(Mesh)); model.meshMaterial = (int *)RL_CALLOC(model.meshCount, sizeof(int)); From bd33baef170c7552bb1975c645fc1544fb7046eb Mon Sep 17 00:00:00 2001 From: HU Date: Tue, 3 Oct 2023 17:01:21 +0800 Subject: [PATCH 4/4] Add comment --- src/rmodels.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rmodels.c b/src/rmodels.c index 2e836b338dfc..c25fe3342340 100644 --- a/src/rmodels.c +++ b/src/rmodels.c @@ -5623,7 +5623,7 @@ static Model LoadM3D(const char *fileName) // We always need a default material, so we add +1 model.materialCount++; - // Sort faces by material. + // failsafe, model should already have faces grouped by material qsort(m3d->face, m3d->numface, sizeof(m3df_t), m3d_compare_faces); model.meshes = (Mesh *)RL_CALLOC(model.meshCount, sizeof(Mesh));