Math behind math:: with the engine's own storage types #12

Closed
UnflavoredMeson/Primal_Plus_Plus
opened 2026-11-24 20:15:50 +00:00 by UnflavoredMeson · 0 comments
Owner

Why

DirectXMath leaked into every header, including the game API. Isolating it keeps the option to replace functions where measurement says so, and gives game code one math API.

Decided

  • DirectXMath stays the SIMD implementation behind math:: (GLM, rtm and an own SSE prototype were measured: Math sections 2-5).
  • Storage types (v2-v4, m4x4, ...) are the engine's own plain aggregates in Engine/Utilities/MathTypes.h, with DirectXMath's layout; matrices have members _11 ... _44.
  • Calculations use math::vec / math::mat with load()/store()/to_v3(); DirectXMath is named only in Engine/Utilities/Math*.h (Tools/check_math_usage.py).
  • A function is replaced by own code only when it measures faster, with a test against the previous result (rotate3(), math::Frustum).
  • The game API exposes the math functions (EngineAPI/ includes Utilities/Math.h); the fork forgot this (UnflavoredMeson/Primal#68).

Read first

  • Math, sections 5 and 6.
  • Engine/Utilities/Math.h and MathTypes.h on master.

Steps

  • 1. Write UnitTests/MathTests.cpp for what the engine uses today (quaternions from Euler angles, matrix multiply, inverse) against DirectXMath's results.
  • 2. Write MathSimd.h with the vec/mat wrappers and forwarders until the tests pass.
  • 3. Replace the aliases in MathTypes.h with own structs; fix the compile errors.
  • 4. Write check_math_usage.py and move every DirectXMath use behind math::.
  • 5. Include the math functions in the game API and use one from an EngineTest script.

Done when

  • check_math_usage.py passes; MathTests pass in Debug and Release; 0 warnings both OSes; tidy 0.

Claude can help with

  • Writing a benchmark case if you want to measure one function yourself.
  • Checking the XM_CALLCONV details on MSVC.

Learn

  • What __vectorcall changes for passing SIMD registers.
  • Why storage types and calculation types are different types.
## Why DirectXMath leaked into every header, including the game API. Isolating it keeps the option to replace functions where measurement says so, and gives game code one math API. ## Decided - DirectXMath stays the SIMD implementation behind `math::` (GLM, rtm and an own SSE prototype were measured: [Math](https://forgejo.unflavoredmeson.com/UnflavoredMeson/Primal_Plus_Plus/wiki/Math) sections 2-5). - Storage types (`v2`-`v4`, `m4x4`, ...) are the engine's own plain aggregates in `Engine/Utilities/MathTypes.h`, with DirectXMath's layout; matrices have members `_11` ... `_44`. - Calculations use `math::vec` / `math::mat` with `load()`/`store()`/`to_v3()`; DirectXMath is named only in `Engine/Utilities/Math*.h` (`Tools/check_math_usage.py`). - A function is replaced by own code only when it measures faster, with a test against the previous result (`rotate3()`, `math::Frustum`). - **The game API exposes the math functions** (`EngineAPI/` includes `Utilities/Math.h`); the fork forgot this (`UnflavoredMeson/Primal#68`). ## Read first - [Math](https://forgejo.unflavoredmeson.com/UnflavoredMeson/Primal_Plus_Plus/wiki/Math), sections 5 and 6. - `Engine/Utilities/Math.h` and `MathTypes.h` on `master`. ## Steps - [x] 1. Write `UnitTests/MathTests.cpp` for what the engine uses today (quaternions from Euler angles, matrix multiply, inverse) against DirectXMath's results. - [x] 2. Write `MathSimd.h` with the `vec`/`mat` wrappers and forwarders until the tests pass. - [x] 3. Replace the aliases in `MathTypes.h` with own structs; fix the compile errors. - [x] 4. Write `check_math_usage.py` and move every DirectXMath use behind `math::`. - [x] 5. Include the math functions in the game API and use one from an EngineTest script. ## Done when - [x] `check_math_usage.py` passes; MathTests pass in Debug and Release; 0 warnings both OSes; tidy 0. ## Claude can help with - Writing a benchmark case if you want to measure one function yourself. - Checking the `XM_CALLCONV` details on MSVC. ## Learn - What `__vectorcall` changes for passing SIMD registers. - Why storage types and calculation types are different types.
Sign in to join this conversation.
No assignees
No project
Depends on
#5 Unit tests with doctest
UnflavoredMeson/Primal_Plus_Plus
Notifications
1 participant
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
UnflavoredMeson/Primal_Plus_Plus#12
No description provided.