Small header-only math library for C++ with the same syntax as the hlsl shading language. It features swizzling and all the operators and functions from the hlsl documentation. The library is aimed mainly at game developers as it's meant to ease the C++ to shader bridge by providing common syntax, but can be used for any application requiring fast, portable math. It also adds some functionality that hlsl doesn't natively provide, such as convenient matrix functions, quaternions, data packing functions and extended vectors such as float8 (8-component float) that take advantage of wide SIMD registers.
- SSE/AVX/AVX2/AVX512: x86/x64 devices like PC, Intel Mac, PS4/5, Xbox One/Series
- NEON: ARM devices like Android, Mac M1, iOS, Switch
- WASM
hlsl++ allows you to be as expressive in C++ as when programming in the shader language. Constructs such as the following are possible.
The natvis files provided for Visual Studio debugging allow you to see both vectors and the result of the swizzling in the debugging window in a programmer-friendly way.
The only required features are a C++ compiler supporting anonymous unions, and SIMD extensions depending on your target platform (SSE/NEON/WASM). If your target platform does not have SIMD support, it can also fall back to a scalar implementation. As a curiosity it also includes an Xbox 360 implementation.
- Remember to add an include path to "include". IMPORTANT NOTE: The include structure has changed to remove prefixes and move towards a sensible folder hierarchy. Compatibility includes will stay around for a bit but will eventually be removed (probably in version 4.0)
- Windows has defines for min and max so if you're using this library and the <windows.h> header remember to #define NOMINMAX before including it
- To force the scalar version of the library, define HLSLPP_SCALAR globally. The scalar library is only different from the SIMD version in its use of regular floats to represent vectors. It should only be used if your platform (e.g. embedded) does not have native SIMD support. It can also be used to compare performance
- To enable the transforms feature, define HLSLPP_FEATURE_TRANSFORM globally
- The [ ] operators make use of the union directly, so the generated code is up to the compiler. Use with care
- Individual swizzle members (such as .x, .y) have the & operator overridden to take the address of the individual component. This is very useful to pass to libraries that expect data pointers like imgui
- SSE/AVX/AVX2/AVX512, NEON, Xbox360, WebAssembly and scalar versions
- float1, float2, float3, float4, float8
- int1, int2, int3, int4
- uint1, uint2, uint3, uint4
- double1, double2, double3, double4
- floatNxM
- quaternion
- Conversion construction and assignment, e.g. float4(float2, float2) and int4(float2, int2)
- Efficient swizzling for all vector types
- Basic operators +, *, -, / for all vector and matrix types
- Per-component comparison operators ==, !=, >, <, >=, <= (no ternary operator as overloading is disallowed in C++)
- HLSL float functions: abs, acos, all, any, asin, atan, atan2, ceil, clamp, cos, cosh, cross, degrees, distance, dot, exp, exp2, floor, fmod, frac, isfinite, isinf, isnan, length, lerp, log, log2, log10, mad, max, min, modf, normalize, pow, radians, rcp, reflect, refract, round, rsqrt, saturate, select, sign, sin, sincos, sinh, smoothstep, sqrt, step, tan, tanh, trunc
- HLSL int/uint functions: countbits, firstbithigh, firstbitlow, reversebits
- Additional functions not in HLSL: copysign
- Additional matrix functions: determinant, transpose, inverse, adjoint
- Matrix multiplication for all NxM matrix combinations
- Data packing functions such as pack_float4_rgba8_unorm or pack_float3_rg11b10f
- Transformation matrices for scale, rotation and translation, as well as world-to-view look_at and view-to-projection orthographic/perspective coordinate transformations. These static functions are optionally available for matrix types float2x2, float3x3, float4x4 when hlsl++.h is compiled with HLSLPP_FEATURE_TRANSFORM definition.
- Native visualizers for Visual Studio (.natvis files) which correctly parse with both MSVC and Clang in Windows
Missing/planned:
- boolN types
.png)

