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 ¶
- func BarycentricFragmentShader(fragment raster.Fragment, _ any) [4]float32
- func DepthFragmentShader(fragment raster.Fragment, _ any) [4]float32
- func Mat4Identity() [16]float32
- func Mat4Mul(a, b [16]float32) [16]float32
- func Mat4MulVec4(m [16]float32, v [4]float32) [4]float32
- func Mat4Ortho(left, right, bottom, top, near, far float32) [16]float32
- func Mat4Scale(x, y, z float32) [16]float32
- func Mat4Translate(x, y, z float32) [16]float32
- func PassthroughVertexShader(vertexIndex int, position [3]float32, attributes []float32, _ any) raster.ClipSpaceVertex
- func SolidColorFragmentShader(frag raster.Fragment, _ any) [4]float32
- func SolidColorVertexShader(_ int, position [3]float32, _ []float32, uniforms any) raster.ClipSpaceVertex
- func TexturedFragmentShader(frag raster.Fragment, uniforms any) [4]float32
- func TexturedVertexShader(_ int, position [3]float32, attributes []float32, uniforms any) raster.ClipSpaceVertex
- func VertexColorFragmentShader(frag raster.Fragment, _ any) [4]float32
- func VertexColorVertexShader(_ int, position [3]float32, attributes []float32, uniforms any) raster.ClipSpaceVertex
- func WhiteFragmentShader(_ raster.Fragment, _ any) [4]float32
- type FragmentShaderFunc
- type ShaderProgram
- type SolidColorUniforms
- type TexturedUniforms
- type Vertex
- type VertexColorUniforms
- type VertexShaderFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BarycentricFragmentShader ¶
BarycentricFragmentShader returns a color based on barycentric coordinates. Useful for debugging triangle rasterization.
func DepthFragmentShader ¶
DepthFragmentShader returns a grayscale color based on fragment depth. Useful for visualizing the depth buffer.
func Mat4MulVec4 ¶
Mat4MulVec4 multiplies a 4x4 matrix by a vec4 (column-major order). This is the standard OpenGL/WebGPU matrix-vector multiplication.
func Mat4Ortho ¶
Mat4Ortho creates an orthographic projection matrix. Parameters define the view volume: left, right, bottom, top, near, far.
func Mat4Translate ¶
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 ¶
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 ¶
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 ¶
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].
Types ¶
type FragmentShaderFunc ¶
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 NewVertexWithColor ¶
NewVertexWithColor creates a vertex with position and RGBA color.
func NewVertexWithColorAndUV ¶
NewVertexWithColorAndUV creates a vertex with position, RGBA color, and UV coordinates.
func NewVertexWithUV ¶
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.