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.
hlsl++ allows you to be as expressive in C++ as when programming in the shader language. Constructs such as the following are possible.
// Native types float4 foo4 = float4(1, 2, 3, 4); // Swizzling float3 bar3 = foo4.xzy; // HLSL functions float2 logFoo2 = log(bar3.xz); // Swizzle of swizzle foo4.wx = logFoo2.yx; // Combined constructors float4 baz4 = float4(logFoo2, foo4.zz); // Matrices float4x4 fooMatrix4x4 = float4x4( 1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1); // Matrix transformations float4 myTransformedVector = mul(fooMatrix4x4, baz4); // Integer operations int2 ifoo2 = int2(1, 2); int4 ifoo4 = int4(1, 2, 3, 4) + ifoo2.xyxy; // Casts float4 fooCast4 = ifoo4.wwyx; // Float8 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; // Data packing uint rgba8Packed = pack_float4_rgba8_unorm(foo4); float4 rgba8Unpacked = unpack_rgba8_unorm_float4(rgba8Packed);
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.
// The quickest way, expensive in compile times but good for fast iteration #include "hlsl++.h" // If you care about compile times in your cpp files #include "hlsl++/vector_float.h" #include "hlsl++/matrix_float.h" // If you only need type information (e.g. in header files) and don't use any functions #include "hlsl++/vector_float_type.h" #include "hlsl++/quaternion_type.h"
"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)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 performanceHLSLPP_FEATURE_TRANSFORM
globallyThis section mainly applies to shaders. As HLSL++'s internal types are SIMD vectors, care has to be taken when uploading or copying the memory around for systems that expect different data layouts. Shader metadata can be used to create C++ structs to make it convenient to upload to the GPU, so we allow for a subset of HLSL++ to be declared in a way that will be compatible with constant buffers. The interop namespace provides alternate classes that can copy from the SIMD vectors, and don't assume anything about alignment. For example, a struct declared in this way
struct alignas(16) MyData { hlslpp::interop::float4 data0; hlslpp::interop::float3 data1; float data2; hlslpp::interop::float3x4 data3; };
will work correctly between C++ and shaders as the packing rules are compatible, whereas with the non-interop versions of these classes this would have been an error. As always, be careful to add the necessary padding between types and align as necessary. A reflection system should be able to align structs appropriately (e.g. for constant buffers) and ensure sizes are respected. For many more details on data layout rules for HLSL read this.
Missing/planned:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4