Recursive AI

Compile HLSL with native 16-bit types in C#

OneShader is a free, open-source HLSL compiler library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Set CompileOptions.Enable16BitTypes to make HLSL half use two bytes instead of four.

Terminal
dotnet add package RecursiveAI.OneShader

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneShader;
 
var result = ShaderCompiler.CompileFile(
    "half.hlsl", new CompileOptions
    {
        Enable16BitTypes = true
    });
if (!result.Success)
    throw new InvalidOperationException(
        string.Join("\n", result.Diagnostics));
Console.WriteLine(result.Reflection!
    .EntryPoints[0].RequiredFeatures);

dotnet run compiles the shader to SPIR-V in memory and prints its required GPU features. There’s no GPU needed to compile it.

Text
Float16, StorageBuffer16

If compilation fails, Success is false; the example stops with the compiler diagnostics instead of trying to read the reflection.

Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.

Save this input as half.hlsl beside Program.cs. It’s already in the download.

HLSL
RWStructuredBuffer<half> Values;
[shader("compute")]
[numthreads(1, 1, 1)]
void Main(uint i : SV_DispatchThreadID)
{
    Values[i] *= (half)0.5;
}

Variations

Compare with the default. In the same program, replace the compile call with this one. The same half shader now prints None for RequiredFeatures.

C#
var result = ShaderCompiler.CompileFile("half.hlsl");

The short answer

Set CompileOptions.Enable16BitTypes = true when compiling with OneShader. Without the option, OneShader stores and computes half and min16* as 32-bit values. With it on, the example uses 16-bit arithmetic and a 16-bit storage buffer. If you compile shaders at build time instead of calling the compiler from C#, the build package accepts the project property OneShaderEnable16BitTypes or the Enable16BitTypes setting on an individual OneShader item.

Is HLSL half a 16-bit float?

Not by itself. Think of half as a spelling whose size depends on how you compile. A buffer producer that assumes each half is two bytes can disagree with a shader built without the option. min16float expresses minimum precision in HLSL; it doesn’t set the layout of a buffer on its own. With this switch enabled, OneShader makes both half and min16float 16-bit. An explicit float16_t, int16_t or uint16_t requires the switch; OneShader diagnoses those names if you leave it off.

Changing the switch can change both the math and the bytes a shader reads. If you turn it on for an existing shader, check the buffer stride and member offsets as well as the numeric results. The generated bindings use System.Half, short and ushort for the matching sized members. Read the reflected offsets for the build you’re shipping rather than assuming that replacing float with half halves the stride of a whole struct.

What Vulkan features do 16-bit HLSL shaders require?

Check RequiredFeatures for the entry point you’ll put in the pipeline. The example reports Float16 because it multiplies half values, and StorageBuffer16 because it reads and writes them in a storage buffer. In Vulkan those correspond to shaderFloat16 and storageBuffer16BitAccess. They are separate: support for arithmetic does not tell you whether the same width is available in a particular buffer storage class. The Vulkan feature definitions describe both switches.

A successful compile doesn’t tell you whether your target GPU supports those features. Check the physical device and enable the required features when creating the logical device. If you need a fallback for this example, compile again with the flag off and use a matching 32-bit buffer. Reusing the same two-byte input with the 32-bit variant would change which bytes the shader reads.

All oneshader recipes