Yotsuba Shader
English

WGSL syntax

Teams coming from WebGPU and WGSL. Inspired by WGSL (WebGPU).

Package and namespace YotsubaShader.WgslSyntax · 24 attributes


Why choose it

The @builtin(...), @group and @binding vocabulary. In C# the names carry an @ in the identifier, as in [@builtin_position], and remain valid C#.

Workgroup size is [@workgroup_size(x, y = 1, z = 1)]. Storage access uses storage(storage_use.read) or storage_use.read_write.

The same shader

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

public struct VertexOutput
{
    [@builtin_position]
    public Vector4 Position;
}

public class Triangle
{
    [storage(storage_use.read_write), @group(0), @binding(0)]
    public float[] Output;

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

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

    [ShaderStage(Stage.Compute)]
    [@workgroup_size(8, 8, 1)]
    public void Compute([@builtin_global_invocation_id] UInt3 id)
    {
    }
}

Common built-ins

BuiltIns.cs
using YotsubaShader.Language;
using YotsubaShader.WgslSyntax;

public struct VertexOutput
{
    // Clip-space position (vertex output)
    [@builtin_position]
    public Vector4 Position;
}

public class BuiltIns
{
    [ShaderStage(Stage.Vertex)]
    public VertexOutput Vertex(
        // Vertex index
        [@builtin_vertex_index] uint vertex,
        // Instance index
        [@builtin_instance_index] uint instance)
    {
        VertexOutput output = new VertexOutput();
        output.Position = new Vector4(vertex, instance, 0, 1);
        return output;
    }

    [ShaderStage(Stage.Fragment)]
    public Vector4 Fragment(
        // Fragment coordinate
        [@builtin_position] Vector4 coord)
    {
        return new Vector4(coord.X, coord.Y, 0, 1);
    }

    [ShaderStage(Stage.Compute)]
    // Workgroup size (method attribute)
    [@workgroup_size(8, 8, 1)]
    public void Compute(
        // Global invocation id
        [@builtin_global_invocation_id] uint id)
    {
        uint index = id;
    }
}

Resources

Resources.cs
using YotsubaShader.Language;
using YotsubaShader.WgslSyntax;
using YotsubaShader.WgslSyntax.Resources;

public struct Material
{
    public float Exposure;
}

public class Resources
{
    // Uniform buffer, with its binding slot
    [uniform, @group(0), @binding(0)]
    public Material Material;

    // Read/write storage buffer
    [storage(storage_use.read_write), @group(0), @binding(1)]
    public float[] Output;

    // Read-only storage buffer
    [storage(storage_use.read), @group(0), @binding(2)]
    public float[] Input;
}

All WGSL syntax attributes

Other syntaxes: HLSL syntax · GLSL syntax · MSL syntax · SPIR-V syntax