---
title: "Compile HLSL to SPIR-V and WGSL in one C# call"
url: https://recursiveai.org/oneshader/cookbook/compile-hlsl-to-multiple-targets/
description: "Compile one HLSL entry point to Vulkan SPIR-V and WebGPU WGSL in a single C# call. Save the binary and text outputs separately."
version: "1.7.1"
---

# Compile HLSL to SPIR-V and WGSL in one C# call

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

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneShader) · [GitHub](https://github.com/recursive-ai-oss/oneshader-csharp) · [Docs](https://recursiveai.org/oneshader/docs/) · [Download example (.zip)](https://recursiveai.org/oneshader/cookbook/compile-hlsl-to-multiple-targets/compile-hlsl-to-multiple-targets.zip)

Compile `Color.hlsl` from the zip for Vulkan and WebGPU without running a shader.

```shell
dotnet add package RecursiveAI.OneShader
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneShader;
using static OneShader.CompileTarget;
var result = ShaderCompiler.CompileFile(
    "Color.hlsl",
    new() { Targets = [SpirV, Wgsl] });
if (!result.Success) return 1;
var spv = result.Find("Main", SpirV)!;
var wgsl = result.Find("Main", Wgsl)!;
File.WriteAllBytes("Main.spv", spv.ToArray());
File.WriteAllText("Main.wgsl", wgsl.AsText());
return 0;
```

[The example](https://recursiveai.org/oneshader/cookbook/compile-hlsl-to-multiple-targets/compile-hlsl-to-multiple-targets.zip) is the full program below: unzip it and `dotnet run`. To run only the code above, paste it into Program.cs of a new `dotnet new console` project and `dotnet run`.

Writes `Main.spv` (binary) and `Main.wgsl` (text). The input `Color.hlsl` defines one pixel entry point:

```hlsl
[shader("pixel")]
float4 Main() : SV_Target
{
    return float4(0.25, 0.5, 1.0, 1.0);
}
```

## The full program

```csharp
using OneShader;

var result = ShaderCompiler.CompileFile(
    "Color.hlsl", new CompileOptions
    { Targets = [CompileTarget.SpirV, CompileTarget.Wgsl] });
foreach (var diagnostic in result.Diagnostics)
    Console.Error.WriteLine(diagnostic);
if (!result.Success) return 1;

var spirv = result.Find("Main", CompileTarget.SpirV)!;
var wgsl = result.Find("Main", CompileTarget.Wgsl)!;
File.WriteAllBytes("Main.spv", spirv.ToArray());
File.WriteAllText("Main.wgsl", wgsl.AsText());
Console.WriteLine($"Main.spv: {spirv.Data.Length} bytes");
Console.WriteLine($"Main.wgsl: {wgsl.Data.Length} bytes");
return 0;
```

```text
Main.spv: 344 bytes
Main.wgsl: 227 bytes
```

If either target fails, `Success` is false and the program prints the diagnostics and writes neither file.

## Variations

**Get only the Vulkan file.** In the full program, change `Targets` and keep only the SPIR-V lookup and write:

```csharp
var result = ShaderCompiler.CompileFile(
    "Color.hlsl", new CompileOptions
    { Targets = [CompileTarget.SpirV] });
if (!result.Success) return 1;
var blob = result.Find("Main", CompileTarget.SpirV)!;
File.WriteAllBytes("Main.spv", blob.ToArray());
return 0;
```

## Related recipes

- [Compile HLSL to SPIR-V](https://recursiveai.org/oneshader/cookbook/hlsl-to-spirv/)
- [Convert HLSL to WGSL](https://recursiveai.org/oneshader/cookbook/hlsl-to-wgsl/)
- [Read SPIR-V reflection in C#](https://recursiveai.org/oneshader/cookbook/spirv-reflection-csharp/)

## The short answer

Set `CompileOptions.Targets` to `[CompileTarget.SpirV, CompileTarget.Wgsl]` in one `ShaderCompiler.CompileFile` call. Check `Success` and use `Find(entryPoint, target)` to get each output. The SPIR-V artifact is binary; WGSL is text. The call compiles HLSL directly for both targets, not SPIR-V to WGSL.

## How do I compile one HLSL entry point to multiple targets?

The targets and the entry points are separate selections. `Targets` says which formats to write; it defaults to SPIR-V if omitted or empty. With no `EntryPoints` list, the compiler selects functions marked with a `[shader(...)]` attribute. That’s why the example only needs `[shader("pixel")]` on `Main`. When you set `EntryPoints` explicitly you can select an unannotated function by name and stage, or compile just one function from a file that defines several. See the [compiler API guide](https://recursiveai.org/oneshader/docs/compiler/) for the entry point options.

The result contains a separate blob per entry point and target. Don’t assume the first two items represent the outputs you want if you later add a vertex shader to the same source. `Find("Main", CompileTarget.SpirV)` selects on both names and returns `null` if the pair is absent. The example expects both matches because it supplies the source and requests both targets.

## What’s the difference between the SPIR-V and WGSL outputs?

The `.spv` file contains little-endian SPIR-V words for a Vulkan shader module; write it with `File.WriteAllBytes`. The `.wgsl` file contains WebGPU Shading Language source; `AsText()` decodes that output from UTF-8. `AsText()` throws for SPIR-V, so don’t call it on every blob in a multi-target result.

Both files represent the same HLSL pixel entry point, but they are not interchangeable. Your Vulkan app loads the SPIR-V module; your WebGPU app loads the WGSL source. This sample only produces the artifacts, so there’s no window, device or render pipeline to set up to try it.

## Why can HLSL compile to SPIR-V but fail to compile to WGSL?

Each target has its own rules. For example, the [WGSL backend](https://recursiveai.org/oneshader/docs/wgsl-backend/) reports OSH5202 for constructs it can’t represent. A shader that compiles when you choose only SPIR-V can fail when you add WGSL. Inspect the diagnostics, then either adapt the shader for both targets or compile separate variants and handle each result independently. A failed combined call isn’t a way to get the successful half: OneShader returns no blobs when any diagnostic is an error.
