Advanced HLSL · Part 2
Wait, we have templates in GPU shaders?
Why our HLSL library looks heavily templated.
A lot of people are surprised when they see our HLSL shader library, which is heavily templated. And yeah, at first it can look a bit intimidating, just like the internals of the C++ standard library. But it’s all for a reason.
Also, even though we compile HLSL to SPIR-V for Vulkan, most of this post applies to anyone using DXC HLSL 202x for DirectX/DXIL as well.
By the way, you can switch from light mode to dark mode with the button in the top right corner, if you’re more comfortable reading code in blog posts that way.
So why do we have the ugly templates?
Most of the reasons are the same as why you’d want templates in C++ on the CPU side, but here are some of our most common patterns:
1. Specializing on each GPU feature or extension
A lot of Vulkan features and extensions tell you what you can do in a shader. For example, the subgroupArithmetic feature enables doing a scan or a reduction over a subgroup. For DirectX people a subgroup is a wave, and it’s the same as having WaveActiveSum and WavePrefixSum.
If the feature is not there, we implement one ourselves easily, using subgroupShuffleUp in a loop of steps.
shaderSubgroupArithmeticUse it
subgroupAdd: 1 instruction
Do it ourselves
subgroupShuffleUp: log2(8) = 3 steps
So what we do is have a struct of device capabilities generated at runtime, and we write specializations for these:
You can override this struct with your minimum requirements, and it doesn’t have to be just-in-time generated.
The full code of both versions is in subgroup/arithmetic_portability_impl.hlsl.
2. Accessors instead of global resources
We want the users of our algorithms to input their data however they want. The data you want to scan might come from a Uniform Buffer, a StructuredBuffer, a Buffer Device Address, or hell, even an Image or Workgroup memory.
- Uniform buffer
ConstantBufferUniform<T> - Structured buffer
RWStructuredBufferStorageBuffer<T> - Buffer device address
vk::BufferPointerPhysicalStorage<T> Buffer - Image
RWTexture2DUniformConstant<T> - Workgroup memory
groupsharedWorkgroupT data[N]
It all affects how the algorithm has to place execution and memory barriers, or even do atomics! And we don’t want to rewrite the logic based on where that data comes from. Welp, we don’t have generic pointers, and everything comes from a storage class.
So the trick we did is an accessor pattern. It’s basically an opaque thing to the algorithm, which implements some functions like get and set.
And we enforce that layout using our own implementation of Concepts! (I’m getting ahead of myself now, that’s for a later blog.)
3. Other nice things
numeric_limits, like in C++:
Type traits, to ask questions about a type while compiling:
And enable_if_t, to make a function exist only for the types it makes sense for:
error: no matching function for call to 'isPowerOfTwo' note: candidate template ignored: disabled by 'enable_if' [with T = float]
4. SPIR-V
Now we get to the juicy part, which follows Advanced HLSL, Part 1: Inline SPIR-V. Remember when we called SPIR-V intrinsics directly, like this?
counters[0], scope Device (1), semantics Relaxed (0), and the value 1.This is literally the same as calling the SPIR-V atomic instruction directly. But notice the I: it means it only works on integral types like uint32_t, so if you want floats you need atomicFAdd (OpAtomicFAddEXT). That’s usually not a problem with higher-level languages, they will just deduce your type and call the correct instruction. We can do that here too, with templated overloads and enable_if_t!
T is uint32_t, an integer.T is float32_t.Both declarations have the same name. enable_if_t removes the one that doesn’t fit the type, so each call ends up on the correct instruction.
There is more to it: sometimes using some types on some functions requires you to declare the usage of a capability or an extension. Want to call atomicExchange on a 64-bit integer? You need to declare the Int64Atomics capability. Need to do an atomic min on a 32-bit float? You need to declare the AtomicFloat32MinMaxEXT capability and the SPV_EXT_shader_atomic_float_min_max extension.
big[0]The capability sits on the 64-bit overload only, so a shader that never calls it never declares it.
These are of course the core internals of our HLSL library; they’re there so that users can use it properly.
Closing remarks
Templates have been in HLSL since HLSL 2021, but I had rarely seen people use them, or take advantage of them like we did. That’s why this series exists.
See you in the next one!
References
- Nabla’s HLSL library: the templated library this post is about
- The Vulkan SPIR-V and its Fundamental Limits: Shading Languages Symposium 2026, Francisco Letterio
- Beyond SPIR-V: Single Source C++ and Shader Programming: Vulkanised 2024, Mateusz Kielan
- Inline SPIR-V: Part 1 of this series, the attributes behind the SPIR-V section
- godbolt.devsh.eu: Compiler Explorer with Nabla’s compiler and HLSL library, where most of the examples here open
- HLSL 2021: DXC wiki, the language version that brought templates
- Vulkan Subgroup Tutorial: Khronos, what subgroups are and what each group of operations does
- SPIR-V specification: Khronos, every instruction and capability