recp / cglm

📽 Highly Optimized 2D / 3D Graphics Math (glm) for C

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

glms_vec3_normalize results in noop, different result to glm_vec3_normalize

FrostKiwi opened this issue · comments

This one took a long while to track down.

In my emscripten + C project, I wanted to switch to the struct API, but found myself with a glitched output, regardless if optimizations are enabled or not and is not related to SIMD in any way. It was almost fine, except one calculation seemed to miss its mark. It took some rewriting to find the culprit, but strangely enough, it's the fault of glms_vec3_normalize(), which I find hard to believe, considering everything else works and glm_vec3_normalize(ray.raw); also works.

puts("======================");
printf("GLM = X: %f, Y: %f, Z: %f\n", ray.x, ray.y, ray.z);
printf("GLMS = X: %f, Y: %f, Z: %f\n", ray_temp.x, ray_temp.y, ray_temp.z);
glm_vec3_normalize(ray.raw);
glms_vec3_normalize(ray_temp);
printf("GLM = X: %f, Y: %f, Z: %f\n", ray.x, ray.y, ray.z);
printf("GLMS = X: %f, Y: %f, Z: %f\n", ray_temp.x, ray_temp.y, ray_temp.z);
puts("======================");

These two passages produce different results, even though they should result in the same outcome.

======================
GLM = X: -0.729466, Y: 0.062500, Z: -0.021380
GLMS = X: -0.729466, Y: 0.062500, Z: -0.021380
GLM = X: -0.995925, Y: 0.085330, Z: -0.029189
GLMS = X: -0.729466, Y: 0.062500, Z: -0.021380
======================

glms_vec3_normalize simply does nothing. Looking at the source under the hood, it should be the same:

/*!
 * @brief normalize vec3 and store result in same vec
 *
 * @param[in] v vector
 * @returns     normalized vector
 */
CGLM_INLINE
vec3s
glms_vec3_(normalize)(vec3s v) {
  glm_vec3_normalize(v.raw);
  return v;
}

Simply using glm_vec3_normalize(ray.raw); solves the issue, even though I expected the source to do that conversion automatically.

My Makefile: https://github.com/FrostKiwi/frostorama/blob/main/Makefile
My EMCC version:

Version of emscripten/emsdk
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.38 (9eff02bc816c50ab0e3b70a3bd5b72a8dc2893a2)
clang version 17.0.0 (https://github.com/llvm/llvm-project 004bf170c6cbaa049601bcf92f86a9459aec2dc2)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: C:\<redacted>\min\home\artsimow\build\emsdk\upstream\bin

How embarrassing, I confused the usage. glms returns the normalized vector. Incredibly funny, I understood this after watching, where the exact same thing happend with the exact same normalization, just 20 years ago: