luv

Workshop wiki

lattice.lisp

hal/shader/lattice.lisp

system luv · 3 definitions · on GitHub

An exact box-filtered lattice ink: shapes that are constant on unit cells (a QR code is the canonical one), rendered with the Slug text renderer's philosophy -- recompute exact analytic coverage at the pixel's true derivative footprint, every frame -- but with the outline integral replaced by a lattice integral.

The compositing rule is the whole point. Drawing a module grid as many rectangles gives every shared edge two half-coverage fragments that OVER-composite to three quarters of either ink: a light hairline on every module boundary, which shimmers as the surface moves. Here the entire grid is one primitive whose fragment computes one coverage value for the whole footprint, so there is no interior boundary to leak.

The closed form is a summed-area table. The SAT of a function constant on unit cells is exactly bilinear within each cell, so the continuous integral over [0,x]*[0,y] is four integer texel loads and two lerps -- not approximately, exactly. A box filter needs that integral at its four corners; sixteen loads make the coverage exact at every scale, magnified or minified, one code path, no mip chain. Where Slug pays a fold over a band's curve list per fragment, the lattice pays O(1).

The filter footprint and width come from the same derivatives and the same knobs as slug-pixels-per-em, so a code and the text beside it on one panel are filtered identically.

in-package#:luv.analytic
shader:define-shader-abstractionlattice-tap
latticecolumnrow
"One summed-area node as a float: the count of inked cells below-left."`
shader:define-shader-abstractionlattice-node
latticeijab

The continuous summed-area value at cell (I,J) plus offset (A,B). Bilinear over the cell's four nodes, which for a unit-cell-constant integrand is the integral itself, not an interpolation of it.

`

Vertex lanes arrive through the analytic family's own vertex stage: the local coordinate carries cell units, the half-size lanes carry the grid's column and row counts, and the color is the premultiplied ink of the INKED cells. The zero cells are white paper, and the paper's own outer edge is part of the same integral: alpha is the footprint's overlap with the grid rectangle, so even the card boundary is filtered, and no part of the drawing is composited against another part of itself.

shader:define-live-shaderlattice-fragment-specification
:stage:fragment:inputs
coordinate:vec2:location0
lattice-size:vec3:location1
color:vec4:location2
:resources
lattice-data:uint-texture-2d:binding0
:outputs
color-output:vec4:location0
let*
columns
shader:swizzlelattice-size:x
rows
shader:swizzlelattice-size:y

The pixel's footprint in cell units, per axis, exactly as slug-pixels-per-em measures it in em units.

coordinate-dx
coordinate-dy
x-gradient
shader:vec2
shader:swizzlecoordinate-dx:x
shader:swizzlecoordinate-dy:x
y-gradient
shader:vec2
shader:swizzlecoordinate-dx:y
shader:swizzlecoordinate-dy:y
length-footprint
shader:vec2
sqrt
shader:dotx-gradientx-gradient
sqrt
shader:doty-gradienty-gradient
width-footprint
+
abscoordinate-dx
abscoordinate-dy
footprint
shader:mixlength-footprintwidth-footprintluv.slug:slug-footprint-norm
area
*
*half-xhalf-y
4.0

The box's corners, clamped to the grid: the integrand is zero outside, so clamping the integration bound changes nothing.

x0
shader:clamp
-
shader:swizzlecoordinate:x
half-x
0.0columns
x1
shader:clamp
+
shader:swizzlecoordinate:x
half-x
0.0columns
y0
shader:clamp
-
shader:swizzlecoordinate:y
half-y
0.0rows
y1
shader:clamp
+
shader:swizzlecoordinate:y
half-y
0.0rows

Cell addresses, clamped to the last cell so an offset of exactly one stays on the lattice; bilinear is exact on the closed cell.

column0
min
-columns1.0
column1
min
-columns1.0
row0
min
-rows1.0
row1
min
-rows1.0
a0
-x0column0
a1
-x1column1
b0
-y0row0
b1
-y1row1
i0
i1
s00
lattice-nodelattice-datai0j0a0b0
s10
lattice-nodelattice-datai1j0a1b0
s01
lattice-nodelattice-datai0j1a0b1
s11
lattice-nodelattice-datai1j1a1b1

Ink fraction and grid (paper) fraction of the footprint.

inked
/
+
-s11s10
-s00s01
area
card
/
*
-x1x0
-y1y0
area
alpha
paper
*
max
-cardinked
0.0
alpha
shader:set-outputcolor-output
+
shader:vec4paperpaperpaper
*cardalpha