shaders

package
v0.0.0-...-c858641 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2025 License: MIT Imports: 12 Imported by: 0

README

Write Shaders In Go

Shaders can be written by calling shader-specific functions defined by the packages in this directory. The shader is then run once in Go which essentially records itself as an AST which is compiled down into Godot's GLSL variant.

This sort of approach is often referred to as a language-hosted DSL. It's well suited for branchless shaders as any Go branches or side effects will only be evaluated at shader 'compile time'.

The key benefits are the type safety, composition, language familiarity and IDE integration that result from this.

Example:

package main

import (
	"graphics.gd/shaders"
	"graphics.gd/shaders/vec2"
	"graphics.gd/shaders/vec4"
	"graphics.gd/shaders/rgba"
	"graphics.gd/shaders/pipeline/CanvasItem"
)

type MyShader struct {
	CanvasItem.Shader[MyShader]
}

// The pipeline functions are named after what they return, not what they accept as
// input.

// Fragment runs for each point/vertex of the shape/mesh being rendered, should return
// fragment parameters for each point (also known as a vertex shader).
func (MyShader) Fragment(vertex CanvasItem.Vertex) CanvasItem.Fragment {
	return CanvasItem.Fragment{
		Position: vertex.Position,
	}
}

// Material runs for each pixel on each face of the shape being rendered, should return
// the surface parameters for each pixel (also known as a fragment shader). The input
// fragment is a blend of each contributing vertex point.
func (MyShader) Material(fragment CanvasItem.Fragment) CanvasItem.Material {
	return CanvasItem.Material{
		Color: rgba.New(1, 0, 0, 1),
	}
}

// Lighting runs for each light, per pixel for each face of the shape being rendered, should
// return the final color for each pixel (also known as a lighting pass).
func (MyShader) Lighting(material CanvasItem.Material) CanvasItem.Lighting {
	return CanvasItem.Lighting{
		Color: material.Color,
	}
}

Uniforms

Uniforms are added as fields to the shader struct. They can be written with [Set] and read with [Get]. Uniforms wrapped inside the [PerInstance] generic type need to be accessed through the RenderingServer/GeometryInstance3D packages.

	type MyShader struct {
		CanvasItem.Shader[MyShader]

		MyUniform vec2.XY `gd:"my_uniform"`

		Color shaders.PerInstance[vec4.XYZW] `gd:"color"`
	}

	ms.Color.Value()

	var shader = new(MyShader)
	shaders.Set(&shader.MyUniform, Vector2.New(1, 2))

Documentation

Overview

Package shaders provides a ShaderMaterial.Instance with the shader pipeline written within Go.

Shaders are multi-stage programs executed on the GPU. They can be used to gain precise control over rendering calculations, such as lighting, shadows, and post-processing effects.

To create a new 2D shader in Go, define a struct that embeds shaders.Type2D and implements the pipeline methods you would like to overide. For example:

type MyShader struct {
	CanvasItem.Shader
}

// The pipeline functions are named after what they return, not what they accept as
// input.

// Fragment runs for each point/vertex of the shape/mesh being rendered, should return
// fragment parameters for each point (also known as a vertex shader).
func (MyShader) Fragment(vertex CanvasItem.Vertex) CanvasItem.Fragment {
	return CanvasItem.Fragment{
		Position: vertex.Position,
	}
}

// Material runs for each pixel on each face of the shape being rendered, should return
// the surface parameters for each pixel (also known as a fragment shader). The input
// fragment is a blend of each contributing vertex point.
func (MyShader) Material(fragment CanvasItem.Fragment) CanvasItem.Material {
	return CanvasItem.Material{
		Color: rgba.New(1, 0, 0, 1),
	}
}

// Lighting runs for each light, per pixel for each face of the shape being rendered, should
// return the final color for each pixel (also known as a lighting pass).
func (MyShader) Lighting(material CanvasItem.Material) CanvasItem.Lighting {
	return CanvasItem.Lighting{
		Color: material.Color,
	}
}

Each sub-package provides GPU-specific shader types that can be used within a shader pipeline. Keep in mind that the Go code is compiled to run on the GPU, so non-GPU values, function calls or branches will only take affect during compilation and not when rendering.

All for loops will be unrolled. The shaders package does not currently support non-constant loops.

Uniforms

Uniforms are added as fields to the shader struct. They can be written with Set and read with Get. Uniforms wrapped inside the PerInstance generic type need to be accessed through the RenderingServer/GeometryInstance3D packages.

type MyShader struct {
	CanvasItem.Shader[MyShader]

	MyUniform vec2.XY `gd:"my_uniform"`

	Color shaders.PerInstance[vec4.XYZW] `gd:"color"`
}

var shader = new(MyShader)
shaders.Compile(&shader)
shaders.Set(&shader.MyUniform, Vector2.New(1, 2))

Index

Constants

This section is empty.

Variables

Globals are available everywhere, including custom functions.

Functions

func Compile

func Compile[V, F, M, L comparable, RM ~string](prog Program[V, F, M, L, RM])

func CompileAny

func CompileAny(val Any)

func Get

func Get[T gpu.EquivalentTo[G], G any](uniform *T) G

Get gets the value of a uniform.

func Set

func Set[T gpu.EquivalentTo[G], G any](uniform *T, value G)

Set sets the value of a uniform.

Types

type Any

type Any interface {
	ShaderMaterial.Any

	ShaderType() string
	Pipeline() [3]string
}

type PerInstance

type PerInstance[T gpu.Evaluator] struct {
	// contains filtered or unexported fields
}

PerInstance uniforms can be added inside a shader struct.

func (PerInstance[T]) Value

func (p PerInstance[T]) Value() T

Value returns the value of the uniform for the current instance.

type Program

type Program[Vertex, Fragment, Material, Lighting any, RenderMode ~string] interface {
	gdclass.Pointer

	ShaderMaterial.Any

	ShaderType() string
	RenderMode() []RenderMode

	Pipeline() [3]string

	Fragment(Vertex) Fragment
	Material(Fragment) Material
	Lighting(Material) Lighting
}

Directories

Path Synopsis
Package bool provides GPU operations on boolean values.
Package bool provides GPU operations on boolean values.
Pacakge bvec2 provides GPU operations on two-component boolean vectors.
Pacakge bvec2 provides GPU operations on two-component boolean vectors.
Pacakge bvec3 provides GPU operations on three-component boolean vectors.
Pacakge bvec3 provides GPU operations on three-component boolean vectors.
Pacakge bvec4 provides GPU operations on four-component boolean vectors.
Pacakge bvec4 provides GPU operations on four-component boolean vectors.
Package float provides GPU operations on floating-point values.
Package float provides GPU operations on floating-point values.
Package int provides GPU operations on signed integer values.
Package int provides GPU operations on signed integer values.
internal
builtins command
builtins checks gdmaths and gdvalue packages for builtin class methods and reports any builtin methods that are missing from gd or any duplicates.
builtins checks gdmaths and gdvalue packages for builtin class methods and reports any builtin methods that are missing from gd or any duplicates.
gpu
Package ivec2 provides GPU operations on two-component signed integer vectors.
Package ivec2 provides GPU operations on two-component signed integer vectors.
Package ivec3 provides GPU operations on three-component signed integer vectors.
Package ivec3 provides GPU operations on three-component signed integer vectors.
Package ivec4 provides GPU operations on four-component signed integer vectors.
Package ivec4 provides GPU operations on four-component signed integer vectors.
Package mat2 provides GPU operations on 2x2 matrices.
Package mat2 provides GPU operations on 2x2 matrices.
Package mat3 provides GPU operations on 3x3 matrices.
Package mat3 provides GPU operations on 3x3 matrices.
Package mat4 provides GPU operations on 4x4 matrices.
Package mat4 provides GPU operations on 4x4 matrices.
pipeline
CanvasItem
Package CanvasItem provides a canvas item shader pipeline used for shading 2D objects.
Package CanvasItem provides a canvas item shader pipeline used for shading 2D objects.
Fog
Package Fog provides a fog shader pipeline used for shading 3D objects.
Package Fog provides a fog shader pipeline used for shading 3D objects.
Particle
Package Particle provides a particle shader pipeline used for shading 2D and 3D particles.
Package Particle provides a particle shader pipeline used for shading 2D and 3D particles.
Sky
Package Sky provides a sky shader pipeline used for shading 3D objects.
Package Sky provides a sky shader pipeline used for shading 3D objects.
Spatial
Package Spatial provides the spatial shader pipeline used for shading 3D objects.
Package Spatial provides the spatial shader pipeline used for shading 3D objects.
Package rgb provides GPU operations on three-component floating-point colors.
Package rgb provides GPU operations on three-component floating-point colors.
Package rgba provides a constructor for vec4.RGBA values.
Package rgba provides a constructor for vec4.RGBA values.
Package uint provides GPU operations on unsigned integer values.
Package uint provides GPU operations on unsigned integer values.
Package uvec2 provides GPU operations on two-component unsigned integer vectors.
Package uvec2 provides GPU operations on two-component unsigned integer vectors.
Package uvec3 provides GPU operations on three-component unsigned integer vectors.
Package uvec3 provides GPU operations on three-component unsigned integer vectors.
Package uvec4 provides GPU operations on four-component unsigned integer vectors.
Package uvec4 provides GPU operations on four-component unsigned integer vectors.
Package vec2 provides GPU operations on two-component floating-point vectors.
Package vec2 provides GPU operations on two-component floating-point vectors.
Package vec3 provides GPU operations on three-component floating-point vectors.
Package vec3 provides GPU operations on three-component floating-point vectors.
Package vec4 provides GPU operations on four-component floating-point vectors.
Package vec4 provides GPU operations on four-component floating-point vectors.

Jump to

Keyboard shortcuts

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