ShaderView
Draws a Metal fragment shader every display frame, like SwiftUI's
<ShaderView ... />
Overview
TimelineView(.animation) around a ShaderLibrary color effect.
The source defines fragment float4 name(ShaderVertex in [[stage_in]],
constant ShaderInputs &inputs [[buffer(0)]]); inputs carries size
(pixels), time (seconds), count and values[256]. Assign values
from Lua to animate it. The view is transparent where the shader is.
A program can instead be linked from sources, {path = …} files and
{code = …} snippets compiled in order as one translation unit, so a
shared library, plugin-contributed functions and an entry point can live
in separate files. Each chunk opens with a #line directive, so compiler
errors name the file and line they came from.
Meshes: with layers, the view first renders draws into that many
offscreen HDR layers (RGBA16Float, cleared to transparent black, with a
depth buffer), and function finishes the picture from them: layer i is
texture2d<float> [[texture(i - 1)]], mipmapped every frame so coarse
levels serve as wide blurs. Each draw is a table:
vertex, fragment (function names), count (vertices), instances
(1), layer (1), primitive (triangle, triangleStrip, line,
lineStrip, point), blend (opaque, alpha premultiplied, add),
depth (none, test, write), cull (none, back, front),
data (floats at const device float *data [[buffer(1)]]) and params
(up to 64 floats at constant float *params [[buffer(2)]]). Both stages
get inputs at buffer(0). Vertices usually come from vertex_id and
instance_id; fullscreenVertex covers the view with ShaderVertex.
Assigning draws compiles their pipelines, and a bad list raises
without replacing the running one.
Topics
Properties
| Name | Type | Required | Description |
|---|---|---|---|
source |
string | optional | Path of a .metal file with the fragment function. |
sources |
table | optional | <ShaderSource path="…"> / <ShaderSource code="…"> chunks, in order. |
function |
string | required | Fragment function name; with layers, the finishing pass. |
values |
table | optional | Initial floats for inputs.values. |
layers |
number | optional | Offscreen layers the draws render into, 0…4 (0). |
draws |
table | optional | Mesh passes, in order (see above). |
onClick |
function | optional | onClick(view, x, y) in the view's top-left points. |
onScroll |
function | optional | onScroll(view, dx, dy) in points, the user's scrolling direction applied. |
Platform notes
- AppKit.
Source: lua/ui/shader.lua:14 (ns.ShaderView)