Yotsuba Shader
English

SPIR-V syntax

Teams that think in SPIR-V and Vulkan descriptors. Inspired by SPIR-V (Vulkan).

Package and namespace YotsubaShader.SpirvSyntax · 34 attributes


Why choose it

Names mirror the SPIR-V BuiltIn decorations (BuiltIn_Position, BuiltIn_GlobalInvocationId), so the mapping to the binary is the most literal of the five.

Resources use Uniform, StorageBuffer, DescriptorSet(s) and Binding(n).

The same shader

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

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

public class Triangle
{
    [StorageBuffer, DescriptorSet(0), @Binding(0)]
    public float[] Output;

    [ShaderStage(Stage.Vertex)]
    public VertexOutput Vertex([BuiltIn_VertexIndex] 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);
    }

    // Workgroup size is set by the host
    [ShaderStage(Stage.Compute)]
    public void Compute([BuiltIn_GlobalInvocationId] UInt3 id)
    {
    }
}

Common built-ins

BuiltIns.cs
using YotsubaShader.Language;
using YotsubaShader.SpirvSyntax;

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_VertexIndex] uint vertex,
        // Instance index
        [BuiltIn_InstanceIndex] 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_FragCoord] Vector4 coord)
    {
        return new Vector4(coord.X, coord.Y, 0, 1);
    }

    [ShaderStage(Stage.Compute)]
    // No method attribute: the workgroup size is a built-in value
    public void Compute(
        // Global invocation id
        [BuiltIn_GlobalInvocationId] uint id)
    {
        uint index = id;
    }
}

Resources

Resources.cs
using YotsubaShader.Language;
using YotsubaShader.SpirvSyntax;
using YotsubaShader.SpirvSyntax.Resources;

public struct Material
{
    public float Exposure;
}

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

    // Read/write storage buffer
    [StorageBuffer, DescriptorSet(0), @Binding(1)]
    public float[] Output;

    // Read-only storage buffer (same attribute)
    [StorageBuffer, DescriptorSet(0), @Binding(2)]
    public float[] Input;
}

All SPIR-V syntax attributes

Other syntaxes: HLSL syntax · GLSL syntax · MSL syntax · WGSL syntax