Documentation
¶
Overview ¶
Package shader draws with shaders an app writes: fragment shaders in Go's syntax, which run on the GPU, whichever renderer draws the window (Metal, Direct3D 11, OpenGL or WebGL 2), and on the CPU where none can.
A shader is a Go file of package main whose function Fragment returns the color of a pixel, premultiplied, from its position:
package main
var Time float
func Fragment(dst vec4, pos vec2) vec4 {
v := 0.5 + 0.5*sin(pos.x/20+Time)
return vec4(v, v, 1, 1)
}
dst is the pixel's center in pixels of the window (or the canvas drawn into), and pos the same point in DIPs from the top left corner of the rectangle the shader fills. The language is Ebitengine's Kage, whose compiler Golet's is forked from: Go's syntax with the types bool, int, uint, float, vec2 to vec4, ivec2 to ivec4, uvec2 to uvec4, mat2 to mat4 and arrays of them, GLSL's built-in functions (sin, mix, smoothstep, dot, ...), functions of its own, if, and for loops. Exported variables at the package level are uniforms, which Options.Uniforms sets by name, or Values by variables looked up once (Shader.Uniform).
A shader reads up to four images, bitmaps or canvases (ui.Canvas), through these functions, in the images' pixels from their top left corners:
image0At(p vec2) vec4 // the pixel at p, transparent outside image0Sample(p vec2) vec4 // blended between the pixels around p image0Size() vec2 // the size in pixels
and image1At to image3At and so on. rectSize returns the size of the rectangle the shader fills, in DIPs, and pixelScale the pixels a DIP.
A shader that draws into a canvas makes the input of another, as the passes of an effect: one computes a field into a small canvas, the next reads it to draw the window.
Index ¶
- type Effect
- type Options
- type Shader
- func (s *Shader) Draw(p *ui.Painter, r ui.Rect, o Options)
- func (s *Shader) Effect(o Options) Effect
- func (s *Shader) Fill(p *ui.Painter, box ui.Rect, o Options, fn func())
- func (s *Shader) FillPath(p *ui.Painter, path *ui.Path, o Options)
- func (s *Shader) NewValues() *Values
- func (s *Shader) StrokePath(p *ui.Painter, path *ui.Path, width float32, o Options)
- func (s *Shader) Text(p *ui.Painter, x, y float32, text string, size float32, o Options)
- func (s *Shader) Uniform(name string) Uniform
- type Uniform
- type Values
- func (v *Values) Bool(u Uniform, b bool) *Values
- func (v *Values) Color(u Uniform, c ui.Color) *Values
- func (v *Values) Float(u Uniform, x float32) *Values
- func (v *Values) Floats(u Uniform, xs ...float32) *Values
- func (v *Values) Int(u Uniform, x int32) *Values
- func (v *Values) Ints(u Uniform, xs ...int32) *Values
- func (v *Values) Uint(u Uniform, x uint32) *Values
- func (v *Values) Uints(u Uniform, xs ...uint32) *Values
- func (v *Values) Vec2(u Uniform, x, y float32) *Values
- func (v *Values) Vec3(u Uniform, x, y, z float32) *Values
- func (v *Values) Vec4(u Uniform, x, y, z, w float32) *Values
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Effect ¶
type Effect struct {
// contains filtered or unexported fields
}
Effect is a shader drawing an element in place of the element as it is (ui.Element.Effect), as Shader.Effect returns.
func (Effect) EffectMargin ¶
EffectMargin returns how far the effect reads its content beyond the element's box, its MaxSampleOffset.
func (Effect) PaintEffect ¶
PaintEffect paints the element from its content, the box's corner at inset in the content's pixels: the shader over all of the content, the box and the margin around it, reading the content as its last image.
type Options ¶
type Options struct {
// Uniforms are the values of the shader's uniform variables, by name:
// a number of any of Go's kinds (bool, an integer, a float) for one of
// a scalar type, or a slice or an array of numbers, or a ui.Color (its
// components from 0 to 1, not premultiplied), for a vector, a matrix
// (by columns) or an array (elements in order); a variable missing is
// zero, and a name the shader has not is left out. Integers set ints
// and uints by their low 32 bits, as Go converts them.
Uniforms map[string]any
// Values, unless nil, are values of the shader's uniform variables set
// by Uniform (Shader.NewValues), with no map and no conversions each
// frame; Uniforms then sets the variables it names over them.
Values *Values
// Images are the images image0 to image3 read: bitmaps, or canvases'
// (ui.Canvas.Bitmap); nil for none, which reads as transparent.
Images [4]*ui.Bitmap
// Radius rounds the corners of the rectangle, in DIPs.
Radius float32
// MaxSampleOffset is how far, at most, across and down in DIPs, an
// effect (Effect) reads its content away from the pixel it draws, as
// a distortion moves pixels: the content renders that much beyond the
// element's box, as its shadows, the effect draws over all of it, and
// it reads as transparent further away.
MaxSampleOffset ui.Point
// BackdropBlur, for a shader reading its backdrop (backdropAt,
// backdropSample), is the standard deviation of the backdrop's blur,
// in DIPs, 0 for none.
BackdropBlur float32
}
Options are what a shader draws with.
type Shader ¶
type Shader struct {
// contains filtered or unexported fields
}
Shader is a compiled shader, which draws in any window, from any goroutine's views.
func Compile ¶
Compile compiles a shader, src the source of a Go file as the package's documentation describes. It reports the errors of the source with their lines.
func MustCompile ¶
MustCompile is Compile for a shader known to compile, as a package's variable: it panics when it does not.
func (*Shader) Draw ¶
Draw fills r with the shader, in DIPs of p, as p's other drawings are: clipped, transformed and faded with the painter.
func (*Shader) Effect ¶
Effect returns the shader drawing an element with o, for ui.Element.Effect: the shader of an effect, whose entry point reads the element's content, rendered with its children, in DIPs from the corner of its box, as SwiftUI's effects do:
// The content's colors, changed pixel by pixel (colorEffect). func ColorEffect(pos vec2, color vec4) vec4 // Where to read the content for the pixel at pos (distortionEffect), // transparent beyond it. func DistortionEffect(pos vec2) vec2 // The pixel's color, from the content read anywhere with layerAt(p), // its pixel at p, or layerSample(p), blended between pixels // (layerEffect). func LayerEffect(pos vec2) vec4
rectSize returns the size of the element's box. An effect's shader reads images 0 to 2 of its own (image3 is the content), and moves pixels at most o.MaxSampleOffset away. It panics for a shader whose entry point is Fragment, which Draw and Fill draw.
func (*Shader) Fill ¶
Fill paints the text, paths and icons that fn paints with p with the shader in place of their colors, as a gradient fills them: each pixel the shader's color times the shape's coverage, the shader filling box, in DIPs, whose corner its positions start from and whose size rectSize returns. Emoji keep their colors, and the rest fn paints, as rectangles and images, paints as it would. Options.Radius does not apply.
func (*Shader) FillPath ¶
FillPath fills path with the shader (non-zero winding), the shader filling the path's box (Path.Bounds).
func (*Shader) StrokePath ¶
StrokePath strokes the outline of path with the shader, width DIPs wide, with round joins and caps, the shader filling the path's box (Path.Bounds) grown by half the width.
type Uniform ¶
type Uniform struct {
// contains filtered or unexported fields
}
Uniform is a uniform variable of a shader, found by its name once (Shader.Uniform), which Values sets.
type Values ¶
type Values struct {
// contains filtered or unexported fields
}
Values are the values of a shader's uniform variables, which Options.Values draws with, as Flutter's FragmentShader and Skia's SkRuntimeShaderBuilder hold them: set them once or each frame, by Uniform, and draw with them as often as needed; drawing copies them. Variables not set are zero. The setters panic for a variable of another type or another shader, and return v.
func (*Values) Color ¶
Color sets a vec3 or vec4 to c's components, from 0 to 1, not premultiplied: its sRGB ones, or, where the window draws a wide gamut, those of a color outside sRGB in extended sRGB (below 0 or above 1).
func (*Values) Floats ¶
Floats sets the components of a vector, a matrix (by columns) or an array of floats or of them (elements in order), from the first; it panics for more numbers than the variable holds.
func (*Values) Ints ¶
Ints sets the components of an ivec or an array of ints or of them, as Floats does.