shader

package
v0.21.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package shader provides callback-based shader execution for the software backend.

Since there is no SPIR-V interpreter in Go, we use callback functions to define vertex and fragment shaders. This allows testing the rendering pipeline without full shader compilation.

Shader Types

There are two main shader stages:

  • Vertex Shader: Transforms vertices from object space to clip space. Receives vertex position and attributes, outputs clip-space position and interpolated attributes.

  • Fragment Shader: Computes the final color for each fragment (pixel candidate). Receives interpolated fragment data and outputs RGBA color.

Built-in Shaders

The package provides several built-in shaders for common use cases:

  • SolidColor: Renders geometry with a uniform color.
  • VertexColor: Interpolates per-vertex colors across the triangle.

Usage

// Create a shader program
program := shader.ShaderProgram{
    Vertex:   shader.SolidColorVertexShader,
    Fragment: shader.SolidColorFragmentShader,
}

// Prepare uniforms
uniforms := &shader.SolidColorUniforms{
    MVP:   myMVPMatrix,
    Color: [4]float32{1, 0, 0, 1}, // Red
}

// Use with the rasterization pipeline
// (integration code varies based on pipeline implementation)

Custom Shaders

To create custom shaders, implement the VertexShaderFunc and FragmentShaderFunc signatures:

func MyVertexShader(
    vertexIndex int,
    position [3]float32,
    attributes []float32,
    uniforms any,
) raster.ClipSpaceVertex {
    // Transform position and prepare attributes
}

func MyFragmentShader(
    fragment raster.Fragment,
    uniforms any,
) [4]float32 {
    // Compute and return RGBA color
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BarycentricFragmentShader

func BarycentricFragmentShader(fragment raster.Fragment, _ any) [4]float32

BarycentricFragmentShader returns a color based on barycentric coordinates. Useful for debugging triangle rasterization.

func DepthFragmentShader

func DepthFragmentShader(fragment raster.Fragment, _ any) [4]float32

DepthFragmentShader returns a grayscale color based on fragment depth. Useful for visualizing the depth buffer.

func Mat4Identity

func Mat4Identity() [16]float32

Mat4Identity returns a 4x4 identity matrix.

func Mat4Mul

func Mat4Mul(a, b [16]float32) [16]float32

Mat4Mul multiplies two 4x4 matrices (column-major order).

func Mat4MulVec4

func Mat4MulVec4(m [16]float32, v [4]float32) [4]float32

Mat4MulVec4 multiplies a 4x4 matrix by a vec4 (column-major order). This is the standard OpenGL/WebGPU matrix-vector multiplication.

func Mat4Ortho

func Mat4Ortho(left, right, bottom, top, near, far float32) [16]float32

Mat4Ortho creates an orthographic projection matrix. Parameters define the view volume: left, right, bottom, top, near, far.

func Mat4Scale

func Mat4Scale(x, y, z float32) [16]float32

Mat4Scale creates a scale matrix.

func Mat4Translate

func Mat4Translate(x, y, z float32) [16]float32

Mat4Translate creates a translation matrix.

func PassthroughVertexShader

func PassthroughVertexShader(
	vertexIndex int,
	position [3]float32,
	attributes []float32,
	_ any,
) raster.ClipSpaceVertex

PassthroughVertexShader is a simple vertex shader that passes position through without transformation. Useful for screen-space rendering.

func SolidColorFragmentShader

func SolidColorFragmentShader(frag raster.Fragment, _ any) [4]float32

SolidColorFragmentShader returns the uniform color for all fragments.

func SolidColorVertexShader

func SolidColorVertexShader(
	_ int,
	position [3]float32,
	_ []float32,
	uniforms any,
) raster.ClipSpaceVertex

SolidColorVertexShader transforms vertices using the MVP matrix. The color is passed through as attributes for the fragment shader.

func TexturedFragmentShader

func TexturedFragmentShader(frag raster.Fragment, uniforms any) [4]float32

TexturedFragmentShader samples a texture using interpolated UV coordinates.

func TexturedVertexShader

func TexturedVertexShader(
	_ int,
	position [3]float32,
	attributes []float32,
	uniforms any,
) raster.ClipSpaceVertex

TexturedVertexShader transforms vertices and passes UV coordinates as attributes. Expects attrs[0:2] to be UV coordinates.

func VertexColorFragmentShader

func VertexColorFragmentShader(frag raster.Fragment, _ any) [4]float32

VertexColorFragmentShader returns the interpolated vertex color.

func VertexColorVertexShader

func VertexColorVertexShader(
	_ int,
	position [3]float32,
	attributes []float32,
	uniforms any,
) raster.ClipSpaceVertex

VertexColorVertexShader transforms vertices and passes vertex colors as attributes. Expects attrs[0:4] to be RGBA color values in [0, 1].

func WhiteFragmentShader

func WhiteFragmentShader(_ raster.Fragment, _ any) [4]float32

WhiteFragmentShader returns white for all fragments. Useful for testing and debugging.

Types

type FragmentShaderFunc

type FragmentShaderFunc func(
	fragment raster.Fragment,
	uniforms any,
) [4]float32

FragmentShaderFunc computes the final color for a fragment. It receives interpolated fragment data and returns an RGBA color.

Parameters:

  • fragment: The fragment with interpolated position, depth, and attributes.
  • uniforms: User-defined uniform data (textures, colors, etc.).

Returns:

  • [4]float32: RGBA color values in the range [0, 1].

type ShaderProgram

type ShaderProgram struct {
	// Vertex is the vertex shader function.
	Vertex VertexShaderFunc

	// Fragment is the fragment shader function.
	Fragment FragmentShaderFunc
}

ShaderProgram combines vertex and fragment shaders into a complete program.

func (ShaderProgram) IsValid

func (p ShaderProgram) IsValid() bool

IsValid returns true if the shader program has both vertex and fragment shaders.

type SolidColorUniforms

type SolidColorUniforms struct {
	// MVP is the Model-View-Projection matrix in column-major order.
	MVP [16]float32

	// Color is the RGBA color to render with values in [0, 1].
	Color [4]float32
}

SolidColorUniforms contains uniform data for the solid color shader.

type TexturedUniforms

type TexturedUniforms struct {
	// MVP is the Model-View-Projection matrix in column-major order.
	MVP [16]float32

	// TextureData is the RGBA8 texture data.
	TextureData []byte

	// TextureWidth is the width of the texture in pixels.
	TextureWidth int

	// TextureHeight is the height of the texture in pixels.
	TextureHeight int
}

TexturedUniforms contains uniform data for the textured shader.

type Vertex

type Vertex struct {
	// Position in object/model space.
	Position [3]float32

	// Attributes are additional per-vertex data (colors, UVs, normals, etc.).
	Attributes []float32
}

Vertex represents input vertex data for processing.

func NewVertex

func NewVertex(x, y, z float32) Vertex

NewVertex creates a vertex with position only.

func NewVertexWithColor

func NewVertexWithColor(x, y, z, r, g, b, a float32) Vertex

NewVertexWithColor creates a vertex with position and RGBA color.

func NewVertexWithColorAndUV

func NewVertexWithColorAndUV(x, y, z, r, g, b, a, u, v float32) Vertex

NewVertexWithColorAndUV creates a vertex with position, RGBA color, and UV coordinates.

func NewVertexWithUV

func NewVertexWithUV(x, y, z, u, v float32) Vertex

NewVertexWithUV creates a vertex with position and UV texture coordinates.

type VertexColorUniforms

type VertexColorUniforms struct {
	// MVP is the Model-View-Projection matrix in column-major order.
	MVP [16]float32
}

VertexColorUniforms contains uniform data for the per-vertex color shader.

type VertexShaderFunc

type VertexShaderFunc func(
	vertexIndex int,
	position [3]float32,
	attributes []float32,
	uniforms any,
) raster.ClipSpaceVertex

VertexShaderFunc transforms a vertex from object space to clip space. It receives vertex data and returns a clip-space vertex with position and attributes.

Parameters:

  • vertexIndex: The index of the vertex being processed.
  • position: The 3D position of the vertex in object/world space.
  • attributes: Additional vertex attributes (colors, UVs, normals, etc.).
  • uniforms: User-defined uniform data (matrices, colors, etc.).

Returns:

  • ClipSpaceVertex: The transformed vertex with clip-space position and attributes to be interpolated across the triangle.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL