Yotsuba Shader
English

Write Shaders in C#.

Yotsuba Shader is an SDK for writing shaders in C#. It covers Vertex, Fragment and Compute declarations, and the connected compiler emits SPIR-V plus backend source for the supported shader subset. Geometry, Hull, Domain and Ray Tracing are planned.

using YotsubaShader.Language;
using YotsubaShader.HlslSyntax;
using YotsubaShader.HlslSyntax.Types;
using YotsubaShader.SpirvSyntax.Resources;
using static YotsubaShader.HlslSyntax.Methods.Math;
using static YotsubaShader.HlslSyntax.Methods.Constructors;

namespace Ocean;

// structs
public struct OceanParams
{
    public float Time;
    public float2 Resolution;
    public float2 CameraAngles;   // x = yaw, y = pitch (precomputed in C#)
    public float3 CameraPosition;
}

public struct VSInput
{
    [POSITION] public float2 Position;
    [TEXCOORD(0)] public float2 TexCoord;
}

public struct VSOutput
{
    [SV_Position] public float4 Position;
    [TEXCOORD(0)] public float2 TexCoord;
}

public partial class ProceduralOcean
{
    // fields
    const float DRAG_MULT = 0.38f;
    const int ITERATIONS_RAYMARCH = 12;
    const int ITERATIONS_NORMAL = 36;
    const float WATER_DEPTH = 1.0f;

    [Uniform] [DescriptorSet(0)] [Binding(0)]
    public OceanParams Globals;

    // stages
    // Full-screen quad: the position already comes in clip space.
    [ShaderStage(Stage.Vertex)]
    public VSOutput MainVS(VSInput input)
    {
        VSOutput output;
        output.Position = new float4(input.Position.X, input.Position.Y, 0.0f, 1.0f);
        output.TexCoord = input.TexCoord;
        return output;
    }

    [ShaderStage(Stage.Fragment)]
    [return: SV_Target]
    public float4 MainPS(VSOutput input)
    {
        float3 ray = GetRay(input.TexCoord);

        if (ray.Y >= 0.0f)
        {
            // ray going up: sky
            float3 sky = AcesTonemap(GetAtmosphere(ray) * 2.0f);
            return new float4(sky.X, sky.Y, sky.Z, 1.0f);
        }

        // ray going down: it hits the water
        float3 waterPlaneHigh = new float3(0.0f, 0.0f, 0.0f);
        float3 waterPlaneLow = new float3(0.0f, -WATER_DEPTH, 0.0f);
        float3 up = new float3(0.0f, 1.0f, 0.0f);

        float3 origin = Globals.CameraPosition;

        float highPlaneHit = IntersectPlane(origin, ray, waterPlaneHigh, up);
        float lowPlaneHit = IntersectPlane(origin, ray, waterPlaneLow, up);
        float3 highHitPos = origin + ray * highPlaneHit;
        float3 lowHitPos = origin + ray * lowPlaneHit;

        float dist = RaymarchWater(origin, highHitPos, lowHitPos, WATER_DEPTH);
        float3 waterHitPos = origin + ray * dist;

        float3 n = WaterNormal(new float2(waterHitPos.X, waterHitPos.Z), 0.01f, WATER_DEPTH);

        // smooths the normal with the distance to avoid high-frequency noise
        n = lerp(n, up, float3(0.8f * min(1.0f, sqrt(dist * 0.01f) * 1.1f)));

        float fresnel = 0.04f + (1.0f - 0.04f) * pow(1.0f - max(0.0f, -dot(n, ray)), 5.0f);

        // reflects the ray and forces it to bounce upwards
        float3 r = normalize(reflect(ray, n));
        r = new float3(r.X, abs(r.Y), r.Z);

        float3 reflection = GetAtmosphere(r);
        float3 scattering = new float3(0.016f, 0.043f, 0.122f)
            * (0.085f * (0.2f + (waterHitPos.Y + WATER_DEPTH) / WATER_DEPTH));

        float3 color = reflection * fresnel + scattering;
        float3 mapped = AcesTonemap(color * 2.0f);
        return new float4(mapped.X, mapped.Y, mapped.Z, 1.0f);
    }
}

ENGINES AND FRAMEWORKS

Write the shader once. Use it in your engine.

Guides and reference for MonoGame, KNI, FNA, Godot, raylib-cs and Silk.NET.

Or compile it for your own engine, for the graphics API you use.

SHADER STAGES

The pipeline today. The stages coming next.

Yotsuba Shader is an SDK for authoring shaders in C#. Vertex, Fragment and Compute are declared and compile through the supported frontend and SPIR-V path. Geometry, Hull and Domain are planned for March 2027.

Now · Supported path

Vertex, Fragment, Compute

These entry points can compile through the supported C# shader subset and emit the available backend formats.

A triangle being rasterized into a grid of pixels
Rasterization diagram by Wojciech Mula, via Wikimedia Commons · Public domain

March 2027 · Planned

Geometry, Hull, Domain

Planned support for geometry processing and GPU tessellation.

A tessellation pipeline turning a cube into a smooth shaded sphere
Tessellation diagram by Romainbehar and Mark Viking, via Wikimedia Commons · CC BY-SA 3.0

October 2027 · Planned

4. Ray tracing pipeline

Planned support for the programmable stages that make ray tracing extensible.

  • Ray Generation
  • Intersection
  • Any-Hit
  • Closest-Hit
  • Miss
  • Callable
Rays traveling from a camera to a scene object and a light source
Ray-tracing diagram by Henrik, via Wikimedia Commons · CC BY-SA 4.0

FIVE C# SYNTAX LIBRARIES

Use the shader-language vocabulary you already know.

Yotsuba Shader includes five C# libraries that reproduce the vocabulary of HLSL, GLSL, MSL, SPIR-V and WGSL. Use the familiar names for attributes, types and resources; the compiler resolves them to the same semantic model and lowers supported declarations to the available outputs. WGSL is also one of the output formats.

Triangle.cs
using YotsubaShader.Language;
using YotsubaShader.HlslSyntax;
using YotsubaShader.HlslSyntax.Resources;

public struct VertexOutput
{
    [SV_Position]
    public Vector4 Position;
}

public class Triangle
{
    [register(RegisterPrefix.u, 0)]
    public RWStructuredBuffer<float> Output;

    [ShaderStage(Stage.Vertex)]
    public VertexOutput Vertex([SV_VertexID] uint id)
    {
        VertexOutput vertexOutput = new VertexOutput();
        vertexOutput.Position = new Vector4(0, 0, 0, 1);
        return vertexOutput;
    }

    [ShaderStage(Stage.Fragment)]
    [return: SV_Target(0)]
    public Vector4 Fragment(VertexOutput vertex)
    {
        return new Vector4(1, 0.4f, 0.7f, 1);
    }

    [ShaderStage(Stage.Compute)]
    [numthreads(8, 8, 1)]
    public void Compute([SV_DispatchThreadID] UInt3 id)
    {
    }
}

Choosing a syntax

CPU + GPU

Use your own types. Bring your own libraries.

Your shaders are part of your C# project, so they share its types. Mark the vectors, matrices and textures you already have with an attribute and they become native GPU types, with no parallel set of shader-only structs to keep in sync.

  • Map your project's CPU types to native GPU types with attributes
  • Build shaders on external libraries and NuGet packages
  • One codebase for CPU and GPU: the same types on both sides

Reusing your types and libraries

Types.cs
// Your own type, as a GPU vector
[Vector]
public struct Vector3
{
    [VectorComponent(0)] public float X;
    [VectorComponent(1)] public float Y;
    [VectorComponent(2)] public float Z;
}

// ...and used directly by a shader
[ShaderStage(Stage.Vertex)]
public VertexOutput Vertex(
    Vector3 position) { /* ... */ }

Available now

Write shaders once. Compile to multiple formats.

The current CLI turns one supported C# shader into SPIR-V, HLSL, GLSL, GLSL ES, MSL, WGSL and C++. The compile command writes target files to an output directory, while add writes a readable YotsubaShader.g.cs accessor file beside the project. Add a language version after the name, like --target glsl:430, when a driver needs it.

Explore the current compilation status

Available now

Generate accessors from the .csproj.

A project build can invoke the CLI add command and generate YotsubaShader.g.cs beside the project. The file contains the compiled SPIR-V bytes and readable HLSL, GLSL, GLSL ES, MSL and, if you ask for it with --target wgsl, WGSL accessors for each discovered shader stage.

Read the current build workflow

YourProject.csproj
<Target Name="GenerateYotsubaShaderAccessors"
        AfterTargets="Build">
  <Exec Command="dotnet run --project Core/YotsubaShader.Compiler.CLI/YotsubaShader.Compiler.CLI.csproj -- add $(MSBuildProjectFullPath) --target spirv" />
</Target>

Planned

Catch shader errors while you code.

The playground exposes C# diagnostics and shader discovery today. Shader-specific IDE diagnostics and per-backend compatibility analysis are planned.

Inputs, outputs, resources, locations, bindings and stages are already declared explicitly with attributes. Those declarations are the foundation for reporting incompatible functions and interface mistakes before a shader reaches the GPU.

  • Locations and built-in values on every stage boundary
  • Uniforms, storage buffers, textures and samplers with explicit bindings
  • Planned compatibility checks for each selected backend

Read the current validation status

Interfaces.cs
using Microsoft.Xna.Framework;
using YotsubaShader.HlslSyntax;
using YotsubaShader.HlslSyntax.Resources;

public struct VertexOutput
{
    [SV_Position]
    public Vector4 Position;

    [COLOR]
    public Vector4 Color;
}

public sealed class Material
{
    [cbuffer, register(RegisterPrefix.b, 0)]
    public float Exposure;
}

SPONSORS

Sponsors help decide what comes next.

Yotsuba Shader is free for everyone. Sponsorship pays for the time that keeps it maintained, and gives you a say in where that time goes.

Sponsoring never unlocks features or changes the license. Everyone gets the same code.

Ready to write your first shader?

Build the repository, read the walkthrough, and see how a C# class becomes a shader stage.