Small header-only math library for C++ with the same syntax as the hlsl shading language. It supports any SSE (x86/x64 devices like PC, Mac, PS4, Xbox One) and NEON (ARM devices like Android, iOS, Switch) platforms. 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 and extended vectors such as float8 (8-component float) that take advantage of wide SSE registers.
hlsl++ allows you to be as expressive in C++ as when programming in the shader language. Constructs such as the following are possible.
float4 foo4 = float4(1, 2, 3, 4); float3 bar3 = foo4.xzy; float2 logFoo2 = log(bar3.xz); foo4.wx = logFoo2.yx; float4 baz4 = float4(logFoo2, foo4.zz); float4x4 fooMatrix4x4 = float4x4( 1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1); float4 myTransformedVector = mul(fooMatrix4x4, baz4); int2 ifoo2 = int2(1, 2); int4 ifoo4 = int4(1, 2, 3, 4) + ifoo2.xyxy; float4 fooCast4 = ifoo4.wwyx; float8 foo8 = float8(1, 2, 3, 4, 5, 6, 7, 8); float8 bar8 = float8(1, 2, 3, 4, 5, 6, 7, 8); float8 add8 = foo8 + bar8;
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 SSE or NEON depending on your target platform. 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 also add an include path to "hlslpp/include". hlsl++.h pulls in other headers that live in the same folder. To force the scalar version of the library, define HLSLPP_SCALAR.