Build HLSL into SPIR-V with CMake 3.20+ and Ninja. Ninja is required for this example’s depfile setup.
OneShader outputs SPIR-V, Metal (MSL), or WGSL, but not DXIL for Direct3D.
dotnet new tool-manifest dotnet tool install RecursiveAI.OneShader.Tool
Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
cmake -S . -B build -G Ninja cmake --build build
Run in the directory with CMakeLists.txt, fill.hlsl, and value.hlsli (shown below). This writes build/fill.spv and build/fill.spv.d. The zip restores the pinned tool; skip the install commands if you use it.
The example is the full program below: unzip it and dotnet run.
The full program
CMake runs dotnet oneshader from the source directory so .NET can find the local tool manifest. Put the same output path in OUTPUT and -Fo, and pass the same depfile path to OneShader’s --depfile and CMake’s DEPFILE.
CMakeLists.txt
cmake_minimum_required(VERSION 3.20) project(ShaderExample NONE) set(src "${CMAKE_CURRENT_SOURCE_DIR}") set(out "${CMAKE_CURRENT_BINARY_DIR}/fill.spv") add_custom_command( OUTPUT "${out}" COMMAND dotnet oneshader -T cs_6_0 "${src}/fill.hlsl" -Fo "${out}" --depfile "${out}.d" DEPENDS "${src}/fill.hlsl" DEPFILE "${out}.d" WORKING_DIRECTORY "${src}" VERBATIM) add_custom_target(shaders ALL DEPENDS "${out}")
fill.hlsl
#include "value.hlsli" RWStructuredBuffer<uint> Output : register(u0); [numthreads(1, 1, 1)] void main() { Output[0] = VALUE; }
value.hlsli
#define VALUE 42uThe zip’s Program.cs runs the same CMake commands as the snippet above, after restoring the tool from dotnet-tools.json.
using System.Diagnostics; Run("dotnet", "tool", "restore"); Run("cmake", "-S", ".", "-B", "build", "-G", "Ninja"); Run("cmake", "--build", "build"); Console.WriteLine( "Shader output: build/fill.spv"); static void Run( string command, params string[] arguments) { var start = new ProcessStartInfo(command); foreach (var argument in arguments) start.ArgumentList.Add(argument); using var process = Process.Start(start)!; process.WaitForExit(); if (process.ExitCode != 0) Environment.Exit(process.ExitCode); }
On a clean build, the last two lines are:
[1/1] Generating fill.spv Shader output: build/fill.spv
If the shader doesn’t compile, OneShader writes the diagnostic to standard error and exits with code 1, so the build fails.
Variations
Build just the shader target. In a larger CMake project, you can generate shaders without building the other targets:
cmake --build build --target shadersRelated recipes
The short answer
Use the oneshader command-line HLSL compiler in a CMake add_custom_command that produces a shader file. Pass --depfile when you invoke the compiler, and give CMake that file with DEPFILE. The main shader and its included files then drive the same build rule. You don’t need a C# project for that integration; dotnet run in the download wraps the tool restore and CMake commands so you can try it as shipped.
How do you compile HLSL shaders during a CMake build?
Use add_custom_command(OUTPUT ...) for each shader output. Here -T cs_6_0 selects the compute stage, main is the default entry point, and -Fo names the output. OneShader defaults to SPIR-V if you don’t select another target. The shaders target brings fill.spv into the default build with ALL; the compile command itself only runs when the file is missing or out of date. In a project with an application target, make it depend on shaders if the binary needs the shader even when you build just the application.
How does a CMake depfile track HLSL includes?
A depfile is a text file that names the output and the input files the compiler read. In this example the rule for fill.spv lists fill.hlsl and value.hlsli. DEPENDS explicitly names fill.hlsl so CMake knows about it before the first compile. Once the compiler has written its dependency file, CMake’s Ninja generator uses it to track value.hlsli without a second list of headers in CMakeLists.txt. This depfile setup was checked with Ninja.
Run cmake --build build twice without edits and Ninja reports ninja: no work to do. on the second run. Change VALUE in value.hlsli and build again: Ninja regenerates fill.spv even though the main .hlsl file hasn’t changed. Keep the depfile on the compile rule, not on a command that only runs during CMake configuration.
What is HLSL used for?
HLSL is a language for GPU programs, including vertex, pixel, and compute shaders. This example only compiles a compute shader: it doesn’t need a GPU and doesn’t dispatch the shader. Pick the format your graphics API expects. OneShader’s command line emits SPIR-V for Vulkan 1.2 or later, Metal Shading Language, or WGSL. It does not produce DXIL for Direct3D. Changing the output filename alone doesn’t change the format.