Documentation
¶
Overview ¶
Package shaders holds the GPU renderer's shaders, written in Go against package sl.
This is an ordinary package (no build tag). The renderer only uses its data types (Instance, Frame) to lay out GPU buffers; the shader functions are called only by the shader generator (cmd/tanshader, which compiles them to internal/render/gpu/gen), the CPU executor (internal/render/gpu/cpuexec) and tests, so the linker drops them from normal binaries.
Index ¶
- Constants
- Variables
- func BlurPass(pc sl.Vec2, uv, kind, params, dash sl.Vec4, cov float32) sl.Vec4
- func CatmullRomWeights(f float32) sl.Vec4
- func CheckerSelect(p, anchor sl.Vec2, cell float32) float32
- func Dither4x4(x, y float32) float32
- func DitherPremul(c sl.Vec4, d float32) sl.Vec4
- func FS(in Varyings) sl.Vec4
- func GradientT(kind float32, p, start, end sl.Vec2) float32
- func ImageTexel(p, size sl.Vec2) sl.Vec4
- func LayerComposite(t sl.Vec4, a, cov float32) sl.Vec4
- func RRectCoverage(p, origin, size sl.Vec2, radii sl.Vec4) float32
- func RampU(t float32) float32
- func SampleCatmullRom(uv, size sl.Vec2) sl.Vec4
- func ShadowCoverage(p, origin, size sl.Vec2, radii sl.Vec4, sigma float32) float32
- func StrokeCoverage(p, origin, size sl.Vec2, radii sl.Vec4, width float32, dash sl.Vec4, ...) float32
- type Frame
- type Instance
- type Varyings
Constants ¶
const ( BlurDown = 2 BlurH = 3 BlurV = 4 BlurComposite = 5 )
Backdrop blur passes (paint.BackdropBlur; the algorithm is described at the top of ui/paint/blur.go, whose float32 code these functions mirror operation by operation).
Every pass is a KindPatch instance with Kind.W >= 2 sampling the Layer texture bound to its batch, as a layer composite does: UV is the layer's valid texels (0, 0, w, h normalized by the texture size) and Kind.Y, Kind.Z half a texel, so a device that backs the layer with a larger pooled texture rescales them like a composite's. Dash.XY is where the layer's texel (0, 0) lies in the pass's source space and Dash.ZW that space's size: reads are clamped to the source space, and are transparent where the layer holds nothing. Params.ZW is the device position of the output's texel (0, 0) (the target's Bounds.Min for the passes into reduced layers, the plan's Src.Min for the composite).
Kind.W = BlurDown: Params.X the factor (box average Down×Down)
Kind.W = BlurH, BlurV: Params.X the reduced sigma, Params.Y the taps
Kind.W = BlurComposite: Params.X the factor; Rect, Radii and the clip
are the op's rounded rectangle (coverage as
any instance's)
The reduced passes write whole 8-bit levels (blurQuantize), so the target's unorm conversion is exact and every backend stores the same bytes paint.CPU does.
const ( KindSolid = 0 // solid rounded rectangle in Color KindMask = 1 // R8 mask (glyph) tinted with Color // KindPatch is a premultiplied RGBA texture multiplied by Color: the // patch page (Color), or with Kind.W = 1 an offscreen layer (Layer), // which composites an opacity group. With Kind.W >= 2 it is a pass of // a backdrop blur sampling a layer (BlurPass, blur.go). KindPatch = 2 // KindStroke is a border inside the rounded rectangle (StrokeCoverage) // in Color: Params.X is the width, Params.Y the dash phase, Dash the // dash pattern. KindStroke = 3 // KindShadow is a box shadow (ShadowCoverage) of the rounded rectangle // in Color: Params.X is the blur's sigma and Params.Y how far the // shadow reaches beyond Rect (the quad grows by that much); device // pixels. KindShadow = 4 // KindGradient fills the rounded rectangle with a gradient: Params.XY // is the start point and Params.ZW the end point (device pixels), // Kind.Y the gradient kind (paint.GradientKind: 0 linear, 1 radial, // 2 conic), and UV.Y the vertical texture coordinate of the center of // the gradient's row in the Ramp texture (premultiplied colors at // t = i/255). GradientT gives the parameter t at the pixel center; // RampU maps it to the row; the color is dithered (DitherPremul with // the pixel's Dither4x4 threshold). Color is unused. // With Kind.W = 1 the gradient fills a path instead (paint.Gradient // with a Path): Rect is the path's coverage mask in device pixels // (Radii zero) and Dash its rectangle in the Mask atlas (x, y, w, h, // normalized); the dithered color is multiplied by the mask texel at // the pixel center, as paint.CPU multiplies it by the path's coverage. KindGradient = 5 // KindChecker fills the rounded rectangle with a checkerboard: Params.X // is the cell size and Params.YZ the anchor (the corner of a cell of // the first color; device pixels). The two premultiplied colors reuse // the Color field (the anchor cell's) and the Dash field (the other), // which KindChecker does not otherwise need. KindChecker = 6 // KindImage draws an image into the rounded rectangle: the Img texture // (the image's own premultiplied RGBA8 texture, bound per batch) is // sampled with SampleCatmullRom at the pixel center mapped linearly // from Rect onto the whole texture; Params.Z set (1) reads one texel // per pixel (paint.ImageAligned: whole-pixel position, the texture's // size); Params.XY is the texture size in // texels. Color is unused. KindImage = 7 )
Instance kinds (Instance.Kind.X).
Variables ¶
var Color sl.Texture2D
Color is the RGBA texture sampled by KindPatch.
var Img sl.Texture2D
Img is the image texture sampled by KindImage: one premultiplied RGBA8 texture per image, bound per batch (gpu.Batch.Image). It is read only at texel centers (SampleCatmullRom does the filtering), so nearest.
var Layer sl.Texture2D
Layer is the offscreen layer sampled by a layer composite (KindPatch with Kind.W = 1): premultiplied RGBA8, bound per batch (gpu.Batch.Layer).
var Mask sl.Texture2D
Mask is the R8 coverage atlas page sampled by KindMask.
var Ramp sl.Texture2D
Ramp holds the gradient color ramps sampled by KindGradient: one row of paint.RampWidth premultiplied RGBA16Unorm texels per gradient (16 bits, so low-contrast gradients keep sub-level steps to dither), sampled at the row's center (so the linear filter never mixes rows) and between texel centers horizontally. It is a texture of its own, so the half-texel clamp of the atlas pages (Kind.YZ) does not apply to it.
Functions ¶
func BlurPass ¶
BlurPass shades pixel center pc of a blur pass instance (see above) with coverage cov; uv, kind, params and dash are the instance's fields.
func CatmullRomWeights ¶
CatmullRomWeights returns the Catmull-Rom (B = 0, C = 0.5) weights of the four texels around a sample point whose fraction past the second texel's center is f: paint.CatmullRomWeights, operation by operation.
func CheckerSelect ¶
CheckerSelect is 0 when the pixel centered at p lies in a checkerboard cell an even number of steps from the cell at anchor (cells of size cell), 1 otherwise. It is paint.CheckerSelect's formula.
func Dither4x4 ¶
Dither4x4 is the 4×4 Bayer ordered-dither threshold of the device pixel at integer coordinates (x, y), in (0, 1): (B+0.5)/16, B the Bayer index built from the two low bits of x and y (2×2 Bayer 0 2 / 3 1, nested). It is paint.Dither4x4's table.
func DitherPremul ¶
DitherPremul offsets the color channels of the premultiplied color c by (d-0.5)/255, clamped to 0..alpha (alpha unchanged), so the 8-bit target write spreads sub-level gradient values over neighbouring pixels. It is paint.DitherPremul.
func FS ¶
FS shades one pixel: the instance's paint (Color, the gradient ramp's color for KindGradient, the checkerboard's for KindChecker, the Catmull-Rom filtered image for KindImage) times its coverage (the rounded rect's, the stroke's for KindStroke, the shadow's for KindShadow) times the clip coverage, premultiplied.
func GradientT ¶
GradientT is the gradient parameter of the point p for a gradient of kind (paint.GradientKind as a float: 0 linear, 1 radial, 2 conic; the host sends unknown kinds as 0) from start to end. It is paint.GradientT's formula, operation by operation (a test keeps them equal): linear projects p - start onto end - start; radial is |p - start| / |end - start|; conic is the clockwise angle from end - start to p - start in turns, in [0, 1), with the Abramowitz–Stegun 4.4.49 arctangent (atan2Poly). Degenerate end == start: linear is 0, radial uses radius 1, conic starts along +x.
func ImageTexel ¶
ImageTexel is SampleCatmullRom of an aligned image (paint.ImageAligned) at the pixel whose center is p from the image's corner: the texel there, clamped to valid premultiplied color as SampleCatmullRom's result is (paint.ImageTexelF).
func LayerComposite ¶
LayerComposite returns the premultiplied color (0..1) an opacity group's layer texel t (premultiplied, 8-bit values k/255) contributes when the layer is composited at group alpha a (ia/255) through the clip coverage cov: paint.CPU's compositeLayer arithmetic, operation by operation in exact integers held in floats. The coverage becomes an 8-bit mask (round(cov*255), as paint.CPU's rounded-clip mask stores it), the effective alpha m = ia*mask/255 truncated, and every channel k*m/255 truncated. The fixed-function blend then adds dst*(1-alpha); paint.CPU truncates that product where the GPU rounds, so the two differ by at most 1 level. TestPaintLayerCompositeMatchesShader keeps this equal to paint.CPU.
func RRectCoverage ¶
RRectCoverage returns the coverage of the pixel centered at p by the rectangle at origin with the given size and corner radii (top-left, top-right, bottom-right, bottom-left). Radii are clamped to half the shorter side; an empty rectangle covers nothing.
Away from rounded corners the coverage is the exact area overlap of the unit pixel square with the axis-aligned rectangle (covX*covY), which matches paint.CPU also for sub-pixel-thin rectangles and square corners. When p lies in a rounded corner's quadrant (within r of both edges that meet there, r > 0) it is Clamp(0.5 - sdf, 0, 1), sdf being the signed distance to the rounded boundary (negative inside), capped by the box overlap (Min) so it never exceeds the square-corner coverage.
func RampU ¶
RampU maps the gradient parameter t to the horizontal texture coordinate of the ramp texture (paint.RampWidth texels per row): t is clamped to 0..1 (NaN to 0) and t*255 lands on the texel centers, so t = i/255 samples entry i exactly and the linear filter interpolates between neighbouring entries (paint.RampSample).
func SampleCatmullRom ¶
SampleCatmullRom samples the Img texture (size texels, premultiplied) at the normalized position uv with the Catmull-Rom filter over the 4×4 texels around it, edges clamped, and clamps the result to valid premultiplied color (alpha to 0..1, color to 0..alpha). It is paint.SampleCatmullRomF, step by step; the 16 texels are read with nearest filtering at their centers.
func ShadowCoverage ¶
ShadowCoverage returns the coverage of the pixel centered at p by the rectangle at origin with the given size and corner radii (top-left, top-right, bottom-right, bottom-left) blurred by a Gaussian of standard deviation sigma, in 0..1. It is paint.ShadowCoverage's formula (a test keeps them equal):
- The pixel's own box filter is folded into the blur as a Gaussian of variance 1/12, so s = sqrt(sigma² + 1/12) (sigma 0 is an anti-aliased rounded rectangle; a negative or NaN sigma counts as 0).
- The square-cornered rectangle's blurred coverage is separable and exact: Φx·Φy, each axis the Gaussian mass between the two edges, 0.5·(erf((p-lo)/(s√2)) - erf((p-hi)/(s√2))).
- Each rounded corner (radius clamped to half the shorter side, as in RRectCoverage) subtracts the Gaussian mass of the region it cuts off the square corner (shadowCorner).
An empty rectangle covers nothing. Erf is sl.Erf (Abramowitz–Stegun 7.1.26), the same polynomial on every backend.
func StrokeCoverage ¶
func StrokeCoverage(p, origin, size sl.Vec2, radii sl.Vec4, width float32, dash sl.Vec4, dashPhase float32) float32
StrokeCoverage returns the coverage of the pixel centered at p by a border of the given width drawn inside the rectangle at origin with the given size and corner radii (top-left, top-right, bottom-right, bottom-left), optionally dashed. It is paint.StrokeCoverage's formula (a test keeps them equal):
- The ring is RRectCoverage of the rectangle minus RRectCoverage of the rectangle inset by width with radii reduced by width (not below 0), clamped to 0..1. A width of at least half the shorter side fills the rectangle; a width <= 0 (or NaN) or an empty rectangle draws nothing.
- The dash (strokeDash) is evaluated at the pixel's arc length along the stroke centerline plus dashPhase. Arc length starts where the top-left corner ends on the top edge and runs clockwise; on a rounded corner it is measured along the centerline's circular arc.
Types ¶
type Frame ¶
type Frame struct {
ViewSize sl.Vec2 // render target size in device pixels
// Origin is the device pixel (main-target coordinates) at the render
// target's top-left: an offscreen layer's position, zero for the main
// target. Instance geometry and P stay in main-target coordinates.
Origin sl.Vec2
}
Frame is the per-frame uniform block.
var FrameU Frame
FrameU is the per-frame uniform.
type Instance ¶
type Instance struct {
Rect sl.Vec4 `sl:"loc=0"` // x, y, w, h
Radii sl.Vec4 `sl:"loc=1"` // corner radii: top-left, top-right, bottom-right, bottom-left
// Color is premultiplied RGBA in 0..1. KindPatch multiplies the texel by
// it component-wise: the host sends (1,1,1,1) for no tint/opacity and
// (a,a,a,a) for opacity a (a zero Color draws nothing).
Color sl.Vec4 `sl:"loc=2"`
UV sl.Vec4 `sl:"loc=3"` // texture sub-rectangle x, y, w, h, normalized
// Clip is a rounded clip rectangle (x, y, w, h) with corner radii
// ClipRadii; a width or height <= 0 means no clip.
Clip sl.Vec4 `sl:"loc=4"`
ClipRadii sl.Vec4 `sl:"loc=5"`
// Kind.X is the kind (KindSolid … KindImage). For the textured
// kinds Kind.Y and Kind.Z are half a texel of the sampled texture in
// normalized units (0.5/width, 0.5/height), filled by the host; FS keeps
// the sample point that far inside the UV sub-rectangle so nearest
// sampling never reaches a neighbouring atlas entry. For KindGradient
// Kind.Y is the gradient kind and Kind.W is 1 for a Path-shaped
// gradient (0 otherwise); for KindPatch it is 1 for a layer composite
// (sampling Layer instead of Color) and BlurDown … BlurComposite for a
// backdrop blur pass (also sampling Layer). Kind.W is unused by the
// other kinds.
Kind sl.Vec4 `sl:"loc=6"`
// Params.X is KindStroke's stroke width and Params.Y its dash phase;
// for KindShadow X is sigma and Y the shadow's reach beyond Rect
// (device pixels), Z and W zero; KindGradient's start and end points;
// KindChecker's cell size and anchor; KindImage's texture size.
Params sl.Vec4 `sl:"loc=7"`
// Dash is KindStroke's dash pattern: up to 4 on/off lengths in device
// pixels, the first entry <= 0 ending it; all zero is a solid stroke.
// KindChecker carries its second color here, a Path-shaped
// KindGradient its mask's atlas rectangle.
Dash sl.Vec4 `sl:"loc=8"`
}
Instance is one quad. All geometry is in device pixels with the origin at the top-left of the target.
type Varyings ¶
type Varyings struct {
Pos sl.Vec4 `sl:"position"`
P sl.Vec2 `sl:"loc=0"`
Rect sl.Vec4 `sl:"loc=1,flat"`
Radii sl.Vec4 `sl:"loc=2,flat"`
Color sl.Vec4 `sl:"loc=3,flat"`
UV sl.Vec4 `sl:"loc=4,flat"`
Clip sl.Vec4 `sl:"loc=5,flat"`
ClipRadii sl.Vec4 `sl:"loc=6,flat"`
Kind sl.Vec4 `sl:"loc=7,flat"`
Params sl.Vec4 `sl:"loc=8,flat"`
Dash sl.Vec4 `sl:"loc=9,flat"`
}
Varyings flows from VS to FS. P is the device-pixel position, linearly interpolated; the rest is the instance passed through unchanged.