OneShader is a free, open-source HLSL compiler for .NET from Recursive AI, with no native dependencies. It’s licensed MIT or Apache-2.0. NuGet · GitHub · Docs
dotnet add package RecursiveAI.OneShader --version 1.7.1
For a DXC migration whose output is SPIR-V, replace the compilation step with ShaderCompiler.CompileFile, check diagnostics and write the returned blob. Keep the HLSL source and explicitly review entry points, bindings and output targets. OneShader does not produce DXIL.
Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneShader; var options = new CompileOptions { EntryPoints = [new("PSMain", ShaderStage.Pixel)] }; var result = ShaderCompiler.CompileFile( "Shader.hlsl", options); if (!result.Success) throw new Exception(); File.WriteAllBytes("shader.spv", result.Blobs[0].ToArray()); Console.WriteLine("Wrote shader.spv");
Wrote shader.spvUnzip the example project, then run dotnet run in that directory.
The full program
Shader.hlsl is in the zip and shown below. It deliberately has no [shader("pixel")] attribute. EntryPointRequest("PSMain", ShaderStage.Pixel) names the function and stage, the roles of -E PSMain and -T ps_6_0 in DXC.
float4 PSMain(float4 position : SV_Position) : SV_Target { float value = position.x * 0.25 + position.y; return float4(value, value, value, 1); }
using OneShader; var options = new CompileOptions { EntryPoints = [new EntryPointRequest("PSMain", ShaderStage.Pixel)], Targets = [CompileTarget.SpirV] }; var result = ShaderCompiler.CompileFile("Shader.hlsl", options); foreach (var diagnostic in result.Diagnostics) Console.WriteLine(diagnostic); if (!result.Success) throw new Exception("Shader compilation failed"); File.WriteAllBytes("shader.spv", result.Blobs[0].ToArray()); Console.WriteLine("Wrote shader.spv");
dotnet run prints:
Wrote shader.spvCompilation errors are printed as diagnostics and stop this program before it writes a shader file.
Map the DXC entry point, includes, and defines
This before/after maps a typical command from DXC. The official DXC SPIR-V documentation describes that command-line form:
dxc -spirv -T ps_6_0 -E PSMain -I inc -D X=1 Variant.hlsl -Fo variant.spv| Before | OneShader API equivalent |
|---|---|
-spirv | Targets = [CompileTarget.SpirV] (also the default) |
-E PSMain -T ps_6_0 | EntryPoints = [new EntryPointRequest("PSMain", ShaderStage.Pixel)] |
-I inc | IncludeDirectories = ["inc"] |
-D X=1 | Defines = [new KeyValuePair<string, string?>("X", "1")] |
Variant.hlsl | The path passed to ShaderCompiler.CompileFile |
-Fo variant.spv | File.WriteAllBytes("variant.spv", result.Blobs[0].ToArray()) after checking success |
Save these two inputs, or use their copies in the zip. inc/Scale.hlsli uses X, so the include search path and definition are both needed. Neither entry point has a shader attribute.
#include <Scale.hlsli> float4 PSMain(float4 position : SV_Position) : SV_Target { float value = Scale(position.x) + position.y; return float4(value, value, value, 1); }
float Scale(float value) { return value * X; }
Replace Program.cs in the downloaded project with the equivalent C# program:
using OneShader; var options = new CompileOptions { EntryPoints = [new EntryPointRequest("PSMain", ShaderStage.Pixel)], Targets = [CompileTarget.SpirV], IncludeDirectories = ["inc"], Defines = [new KeyValuePair<string, string?>("X", "1")] }; var result = ShaderCompiler.CompileFile("Variant.hlsl", options); foreach (var diagnostic in result.Diagnostics) Console.WriteLine(diagnostic); if (!result.Success) throw new Exception("Shader compilation failed"); File.WriteAllBytes("variant.spv", result.Blobs[0].ToArray()); Console.WriteLine("Wrote variant.spv");
Wrote variant.spvMap Vulkan and SPIR-V flags explicitly
The pinned command-line guide and SPIR-V heap documentation document these mappings; they are not a promise that arbitrary flags have equivalents.
| DXC setting | Pinned OneShader support |
|---|---|
-fspv-target-env=vulkan1.2 or vulkan1.3 | Accepted by the tool. The API’s CompileTarget.SpirV produces output for Vulkan 1.2 and newer; there is no separate target-environment property in CompileOptions. |
-fspv-debug=vulkan-with-source or vulkan | DebugInfo = DebugInfoLevel.Full. Add Optimize = false for stepping through locals. |
-fspv-debug=file, source, line or tool; -Zi | DebugInfo = DebugInfoLevel.Lines. |
-fvk-bind-resource-heap <binding> <set> | ResourceHeapBinding = new HeapBinding(set, binding). Notice the API constructor’s reversed argument order. |
-fvk-bind-sampler-heap <binding> <set> | SamplerHeapBinding = new HeapBinding(set, binding). |
-fvk-bind-counter-heap | Not supported, as the SPIR-V backend documents. |
Other layout/remapping flags, such as -fvk-b-shift or -fvk-use-dx-layout | No corresponding layout/remapping option. The command-line guide says other -fvk-* and -fspv-* options are rejected; the heap switches above are separately documented exceptions. Use [[vk::binding]], [[vk::location]] and [[vk::offset]] in the shader for explicit layout. |
The tool also rejects separate debug output with -Fd; debug information is embedded in the shader. The guide’s accepted profile model numbers do not select a different language implementation: the stage comes from the profile, while the model number is accepted and otherwise unused.
Keep the shader contract when you switch
Begin with the intent of the existing build command. Microsoft’s official DXC SPIR-V documentation describes DXC’s -spirv, profile, entry-point and output arguments. Identify those settings in your build before translating them into a library call. A change of invocation is not a change of shader stage or resource contract.
The sample keeps that contract small: one pixel shader, one entry point and no bound resources. It keeps the HLSL free of shader attributes and uses CompileOptions.EntryPoints to select PSMain as ShaderStage.Pixel. Select the same function and stage your renderer already uses; don’t infer the stage from a filename convention that only your old build understood.
SPIR-V is the default target of CompileOptions. A successful result contains blobs for the requested entries and targets, along with diagnostics and reflection. The snippet checks Success before writing its only blob. In a larger shader build, enumerate the outputs and name them by entry point and target rather than always taking index zero. Otherwise adding an entry point can change which artifact a hard-coded output path receives.
Preserve include search paths and preprocessor definitions as part of the migration. The API exposes IncludeDirectories and Defines; these are inputs to compilation, not settings recovered from previously compiled bytes. Matrix packing and 16-bit type handling also have explicit options. The compiler guide documents the available controls.
Check shader profiles and resource layouts
OneShader supports vertex, pixel and compute entry points. Geometry, tessellation, mesh, ray-tracing and library profiles are outside this compiler’s documented scope. Check that boundary before replacing a whole build pipeline. A project needing one of those stages cannot migrate that stage through the API shown here.
Resource layout is another migration checkpoint. Keep explicit bindings and offsets where the host depends on them, then compare reflection with the descriptors and buffers your application creates. A binary compiling successfully doesn’t establish that the host uses the correct descriptor set, buffer stride or vertex layout. Reflection is information for constructing that contract, not a substitute for it.
The variation preserves explicit entry-point selection while adding an include directory and a preprocessor definition. Metal and WGSL are other available backends, with their own supported feature sets. Switching targets doesn’t create missing GPU features or automatically adapt a renderer’s coordinate conventions. Review the target backend’s documentation before sharing a shader across APIs.
Treat the change as a build integration: preserve input settings, handle errors, place outputs where the application expects them and test the resulting pipeline with its actual resources. This example verifies compilation and file writing on the published package. It does not run a Vulkan draw or claim that a renderer needs no further integration work.