Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

perf(math): vectorize GLM with wasm SIMD on the main module (RC8-1)

esengine8a1cdec00f41
diff --git a/cmake/Emscripten.cmake b/cmake/Emscripten.cmake
index 8dd9d18c8..2a2d0f6cc 100644
--- a/cmake/Emscripten.cmake
+++ b/cmake/Emscripten.cmake
@@ -18,6 +18,18 @@ set(ES_EMSCRIPTEN_COMPILE_FLAGS
-fno-exceptions
)
+# Wasm SIMD for the main module + render + math (RC8-1). `-msse2` makes emscripten
+# define __SSE2__, which GLM auto-detects to vectorize vec4/mat4 ops; emscripten
+# lowers the SSE intrinsics to wasm SIMD (`-msimd128`). We deliberately do NOT force
+# aligned gentypes — GLM uses unaligned load/store, so component layouts and the
+# zero-copy ptr offsets are unchanged (ABI_LAYOUT_HASH stays put). The physics module
+# already ships SIMD to web AND wechat, so this is on by default with a compat
+# fallback (`-DES_MAIN_DISABLE_SIMD=ON`) mirroring BOX2D_DISABLE_SIMD.
+option(ES_MAIN_DISABLE_SIMD "Disable wasm SIMD for the main module (compat fallback)" OFF)
+if(NOT ES_MAIN_DISABLE_SIMD)
+ list(APPEND ES_EMSCRIPTEN_COMPILE_FLAGS -msimd128 -msse2)
+endif()
+
# Standard (monolithic) link flags
set(ES_EMSCRIPTEN_LINK_FLAGS
--bind # Enable embind for C++ bindings
diff --git a/src/esengine/math/Math.hpp b/src/esengine/math/Math.hpp
index aed96d91c..336e0a13e 100644
--- a/src/esengine/math/Math.hpp
+++ b/src/esengine/math/Math.hpp
@@ -19,6 +19,11 @@
// GLM Configuration
// =============================================================================
+// SIMD is enabled via the build flags (-msse2 -msimd128), not here: GLM auto-detects
+// __SSE2__ and vectorizes vec4/mat4 ops with unaligned load/store. We intentionally do
+// NOT define GLM_FORCE_DEFAULT_ALIGNED_GENTYPES — aligning would bloat vec3-heavy
+// components and change the ptr-layout ABI for marginal load-speed gain. See
+// docs/REARCH_SIMD.md and cmake/Emscripten.cmake (ES_MAIN_DISABLE_SIMD).
#ifndef GLM_FORCE_RADIANS
#define GLM_FORCE_RADIANS
#endif