Native Odin curve geometry for vector renderers: shapes built as curves
whose coverage of a pixel is computed exactly, on the CPU and (with
curves.glsl) on the GPU, and booleans of filled contours that keep their
curves. One package, curves, core only (no C library, no other
dependency). MIT licensed (LICENSE).
ovg fills its paths, glyphs, strokes and markers with it, and o3d its curved shapes and text in 3D.
- Shapes (
curves.odin): aBuildertakes contours (move_to,line_to,quad_to,cubic_to,close,end_open,orient) andbuild/build_append(in its own frame or a givenFrame) write the shape as one block of 32-bit words: quadratic pieces, each monotone in x and y, cut into the columns of a grid of cells, each cell listing its pieces and its backdrop (the winding number right of it), in time linear in the pieces. Reading the words:coverageof a box footprint,coverage_affineof any parallelogram,coverage_jacobianfrom a 3D footprint's Jacobian,windingat a point,boundsandhairline(the contours as thin lines);markercovers a pixel by a square or circle marker without any words. A pixel's coverage is the area of its footprint inside the shape, in closed form (Green's theorem) over the pieces in the cells it covers plus the backdrop over its rows: exact under any affine footprint, and a pixel costs the pieces near it, not the shape's size. The layout is documented at the top of curves.odin. - The GPU twin (
curves.glsl, shipped as the stringcurves.GLSL): the same reads in GLSL ES 3.0 as oksl compiles it, for a host to put ahead of its shader (oksl'sOptions.prelude). It binds nothing, and reads any number of shapes from one buffer: the shader definesuint curves_word(uint i)over the buffer holding the words (a read-only storage buffer, oksl v0.0.8 on). Fill rules are the intsCURVES_NON_ZEROandCURVES_EVEN_ODD(0 and 1,Fill_Rule's order). - Contours (
contours.odin): lines and cubics in one flat table (every contour's start point and then its segments' points, a line its end and a cubic its two controls and end; each segment's kind; a row per contour; neighbours share their end point by index), written bycontours_begin,contours_line,contours_cubic,contours_quad(a quadratic as the cubic it is) andcontours_end, copied by range (contours_append), and read in order withcontour_segments/segments_next, which give each segment as the cubic it is (a line's controls its thirds);contours_windingat a point andcontours_area, a contour's signed area (a line and its reverse cancel exactly).builder_add_contoursbuilds a shape from them. - Fill rules:
Fill_Rule(non-zero, even-odd) is what coverage, the booleans' operands andfill_insidetake. - Booleans (
bool.odin,bool_kernel.odin):bool_rungives the union, difference, intersection or xor of any number of operands (ranges of aContourstable, each with its fill rule; one operand simplifies), andbool_divideevery face they make, each with the operands it lies inside. The result keeps the operands' own curves: each output segment is a range of an input line or cubic, cut where the operands meet, and the scratch records where each came from. Output contours are Solid-wound with holes reversed and never cross, each one boundary, andfacesgroups them by region (bool_run: the parts of the result joined across curves;bool_divide: each face), its outline first, then its holes. One arrangement of every operand's pieces in f64: pieces are cut wherever two come within eps = 8 f32 ulps of the largest coordinate (a near-set search that drops a pair only once it has proved them farther apart), cut points within eps merge into vertices, edges along the same curve between the same vertices merge with their windings summed, faces are walked and each face's winding per operand propagated. Every table is the caller'sBool_Scratch: no allocation once it is warm (bool_scratch_initputs all its tables on one allocator). The work is bounded by a budget (DEFAULT_BUDGET, 4,000,000 kernel cell pairs and snap-round points) against degenerate input; every glyph of Bitstream Vera (264) and Adwaita Sans (2,915), simplified, takes at most 268 and 8,549. - Cubics (
cubic.odin): a cubic's point at t, sub-range, extrema, bounds, signed area, its split into quadratics within a tolerance and the cubic a quadratic is (cubic_from_quad), for f32 and f64 points alike.
FORMAT numbers the layout of the words build writes and curves.glsl
reads, and is bumped whenever a reader of one layout would misread the
other's words (test_format pins one shape's words). curves.GLSL comes
with the words from one package, so a shader compiled from it at load
always matches them; it defines CURVES_FORMAT for a shader that wants to
check what it was written against (#if CURVES_FORMAT != 1 / #error).
A renderer that commits shaders compiled ahead of time records the
FORMAT it compiled them against and #asserts it equals
curves.FORMAT, so it refuses to build against an ocurves its shaders
would misread.
git clone https://github.com/defnotmid/ocurvesimport cv "deps:ocurves" // -collection:deps=.. (the directory holding ocurves)
b: cv.Builder
defer cv.builder_destroy(&b)
cv.move_to(&b, {0, 0})
cv.line_to(&b, {0, 10})
cv.quad_to(&b, {10, 10}, {10, 0})
cv.close(&b)
words, origin, extent, ok := cv.build(&b)
defer delete(words)
// The coverage of a pixel at p (source units) with sides of 1 unit: shape
// units are the source's divided by extent, starting at origin.
p := [2]f32{4.5, 4.5}
cov := cv.coverage(words, (p - origin) / extent, {1, 1} / extent, .Non_Zero)odin test . -vet -strict-style
odin test glsl_test -collection:deps=.. -vet -strict-style # with oksl cloned beside ocurvesglsl_test checks curves.glsl against curves.odin. It compiles the
GLSL with oksl (v0.1.0 in CI) and runs
it on oksl's CPU runner, with the words in a read-only storage buffer. The
inputs are 96 random outlined shapes (open and closed contours, both fill
rules) under six pixel maps (axis-aligned, flipped, rotated, skewed, zoomed
in and out 9x), 32 x 32 pixels each. For every pixel it compares
coverage_affine, winding, hairline and marker (filled and ringed,
square and circle). It is its own package because curves is core only.
The package's history before v0.0.1 is ovg's: it was ovg's curves/
package and shaders/curves.slang, moved as of ovg 9cb03834 (v0.0.42).
curves.slang, the readers in Slang, shipped until v0.0.17; curves.glsl
replaced it once ovg (v0.0.75) and o3d (v0.0.60) compiled their shaders
with oksl.
ovg's tools/bool_suite fuzzes the booleans through its path API.