Yotsuba Shader
English

Your first shader

A color shader in C#: the vertex stage receives a position and a color per vertex, passes the color along, and the fragment stage returns it.


The HLSL syntax is a good place to start because the relationship between stages, inputs, position and fragment color is easy to see.

BasicColorShader.cs
using Microsoft.Xna.Framework;
using YotsubaShader.Language;
using YotsubaShader.HlslSyntax;

public class BasicColorShader
{
    public struct VS_Input
    {
        [POSITION]
        public Vector2 Position;

        [COLOR]
        public Vector4 Color;
    }

    public struct VS_Output
    {
        [SV_Position]
        public Vector4 Position;

        [COLOR]
        public Vector4 Color;
    }

    [ShaderStage(Stage.Vertex)]
    public VS_Output VS(VS_Input input)
    {
        VS_Output output = new VS_Output();

        output.Position = new Vector4(
            input.Position.X,
            input.Position.Y,
            0.0f,
            1.0f
        );

        output.Color = input.Color;

        return output;
    }

    [ShaderStage(Stage.Fragment)]
    [return: SV_Target(0)]
    public Vector4 FS(VS_Output input)
    {
        return input.Color;
    }
}

What each piece does

  • VertexInput defines the data supplied by the engine.
  • [POSITION] and [COLOR] identify the vertex inputs.
  • VertexOutput defines what the vertex shader passes to the next stage.
  • [SV_Position] marks the position required by the GPU.
  • [ShaderStage] identifies the vertex and fragment stages.
  • [COLOR] carries color between the stages; [return: SV_Target(0)] identifies the fragment color output.

What the compiler sees

The compiler starts by finding BasicColorShader, because two of its methods have [ShaderStage]. For each one it reads which stage it is, what it receives and what it returns.

The attributes describe user semantics and built-ins. [Location(n)] is available separately from YotsubaShader.SpirvSyntax. Syntax declarations cover more concepts than the current processor can lower.

Grow it

Resources use the namespace of the chosen syntax, for example YotsubaShader.SpirvSyntax.Resources. Compute declarations exist, while complete shader body compilation remains unfinished.

Read the full article