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.
Go for loops are unrolled. Go if statements are decided while compiling, so to branch on a value that is only known on the GPU, use [bool.X.Evaluate], and to loop for a number of times that is only known on the GPU, range over graphics.gd/shaders/over.Int.
Branches and Loops ¶
Evaluate returns which way a GPU condition goes, so that a Go if statement can branch on it. The pipeline function is run once for each way that its conditions can go, and the values that differ between the runs are computed inside of an if statement on the GPU. Assigning to Go variables inside of the branches works as usual:
color := s.Background
if float.Gt(density, 0.0).Evaluate() {
color = s.lighting(density) // only computed for pixels inside of a cloud.
}
Each condition doubles the number of runs that follow it, so to choose between values that are cheap to compute, use [bool.Select], which computes both of them.
A Go for statement over over.Int loops on the GPU. The loop body is written like any other Go loop body: values assigned to variables declared before the loop carry over to the next iteration, values computed before the loop are computed once, outside of it, and continue and break (inside of a branch on a condition evaluated with Evaluate) skip the rest of the iteration and stop the loop:
pos, density := start, float.New(0.0)
for range over.Int(steps) {
pos = vec3.Add(pos, step)
density = float.Add(density, s.density(pos))
if float.Gt(density, 1.0).Evaluate() {
break
}
}
Varyings ¶
The value returned by Fragment is what Material receives, blended between the vertices of each face. Fields tagged with a built-in (such as UV or COLOR) use that built-in. To pass a value of your own, return a struct that embeds the pipeline's Fragment and adds untagged fields, then accept that same struct in Material. Each untagged field becomes a varying. In the same way, untagged fields on the value returned by Material are passed to Lighting.
type SmokeFragment struct {
Spatial.Fragment
Voronoi float.X // becomes: varying float voronoi;
}
func (s *SmokeBall) Fragment(vertex Spatial.Vertex) SmokeFragment {
voronoi := float.X(s.VoronoiSampler.Sample(vertex.UV).X)
return SmokeFragment{
Fragment: Spatial.Fragment{
VertexPosition: vec3.Add(vertex.Position, vec3.Mul(vertex.Normal, voronoi)),
},
Voronoi: voronoi,
}
}
func (s *SmokeBall) Material(frag SmokeFragment) Spatial.Material {
return Spatial.Material{
Alpha: float.SmoothStep(0.4, 0.6, frag.Voronoi),
}
}
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 ¶
var ( Time = gpu.NewFloatExpression(gpu.New(gpu.Identifier("TIME"))) PI = gpu.NewFloatExpression(gpu.New(gpu.Identifier("PI"))) TAU = gpu.NewFloatExpression(gpu.New(gpu.Identifier("TAU"))) E = gpu.NewFloatExpression(gpu.New(gpu.Identifier("E"))) )
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)
CompileAny compiles the shader of a material, the code for each shader class is generated once and shared by all of its materials. It starts being generated in the background when the class is registered.
func Set ¶
func Set[T gpu.EquivalentTo[G], G any](uniform *T, value G)
Set sets the value of a uniform.
Types ¶
type PerInstance ¶
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.
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. |
|
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. |
|
Package over provides loops that run on the GPU, for use with a Go for statement.
|
Package over provides loops that run on the GPU, for use with a Go for statement. |
|
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. |