//! The GPU backend, as data: every fill becomes a stencil draw and a cover //! draw in an `abi.gpu.Pass` that the host runs. Stencil-then-cover: //! - stencil: the contours as triangle fans, counting windings in the //! stencil buffer (increment/decrement by facing for nonzero, invert for //! even-odd), color writes off; //! - cover: the bounding box, painted where the stencil is set, zeroing the //! stencil as it goes so the next fill starts clean. //! Anti-aliasing is 8x multisampling. Paint runs in `cover.frag`, the GPU //! twin of `Paint.zig`. const Gpu = @This(); const std = @import("std"); const abi = @import("abi"); const geom = @import("geom.zig"); const Drawing = @import("Drawing.zig"); const Polygons = @import("Polygons.zig"); const Paint = @import("Paint.zig"); const layouts = @import("vector_layouts"); const shaders = @import("vector_shaders"); pub const samples = 8; const vertex: abi.gpu.Shader = .{ .spirv = shaders.path_vert, .uniform_buffers = 1 }; const stencil_only: abi.gpu.Shader = .{ .spirv = shaders.stencil_frag }; const pipelines = [_]abi.gpu.Pipeline{ // 0: nonzero winding. abi.gpu.pipeline(layouts.Vertex, .{ .vertex = vertex, .fragment = stencil_only, .blend = .none, .write_color = false, .stencil = .{ .front = .{ .pass = .increment_and_wrap }, .back = .{ .pass = .decrement_and_wrap } }, }), // 1: even-odd. abi.gpu.pipeline(layouts.Vertex, .{ .vertex = vertex, .fragment = stencil_only, .blend = .none, .write_color = false, .stencil = .{ .front = .{ .pass = .invert }, .back = .{ .pass = .invert } }, }), // 2: cover. abi.gpu.pipeline(layouts.Vertex, .{ .vertex = vertex, .fragment = .{ .spirv = shaders.cover_frag, .uniform_buffers = 1 }, .stencil = .{ .front = .{ .compare = .not_equal, .pass = .zero }, .back = .{ .compare = .not_equal, .pass = .zero }, }, }), }; width: u32, height: u32, image: abi.Image, /// Frame lifetime: the pass's vertices and uniforms live here until the /// host consumes the frame. arena: std.mem.Allocator = undefined, draws: std.ArrayList(abi.gpu.Draw) = .empty, clear_color: [4]f32 = .{ 0, 0, 0, 0 }, viewport: []const u8 = &.{}, pub fn begin(g: *Gpu, arena: std.mem.Allocator, clear: Drawing.Color) void { g.arena = arena; g.draws = .empty; g.clear_color = Paint.premultiply(clear); g.viewport = abi.gpu.uniform(arena, layouts.Viewport{ .size = .{ @floatFromInt(g.width), @floatFromInt(g.height) } }); } pub fn fill(g: *Gpu, polys: *const Polygons, rule: Drawing.FillRule, paint: *const Paint, alpha: f32) void { if (polys.empty() or alpha <= 0) return; g.record(polys, rule, paint, alpha) catch {}; } fn record(g: *Gpu, polys: *const Polygons, rule: Drawing.FillRule, paint: *const Paint, alpha: f32) !void { // Fans from each contour's first point. Their overlaps cancel or add // exactly as the winding rule needs, so no triangulation is required. var triangles: usize = 0; var it = polys.iterator(); while (it.next()) |pts| triangles += pts.len - 2; const fan = try g.arena.alloc(layouts.Vertex, triangles * 3); var i: usize = 0; it = polys.iterator(); while (it.next()) |pts| for (1..pts.len - 1) |k| { fan[i + 0] = .{ .position = pts[0] }; fan[i + 1] = .{ .position = pts[k] }; fan[i + 2] = .{ .position = pts[k + 1] }; i += 3; }; const b = polys.bounds(); const lo = @max(@floor(b.min), geom.splat(0)); const hi = @min(@ceil(b.max), geom.Vec{ @floatFromInt(g.width), @floatFromInt(g.height) }); if (lo[0] >= hi[0] or lo[1] >= hi[1]) return; const quad = try g.arena.dupe(layouts.Vertex, &.{ .{ .position = .{ lo[0], lo[1] } }, .{ .position = .{ hi[0], lo[1] } }, .{ .position = .{ hi[0], hi[1] } }, .{ .position = .{ lo[0], lo[1] } }, .{ .position = .{ hi[0], hi[1] } }, .{ .position = .{ lo[0], hi[1] } }, }); try g.draws.append(g.arena, .{ .pipeline = if (rule == .nonzero) 0 else 1, .vertices = std.mem.sliceAsBytes(fan), .vertex_count = @intCast(fan.len), .vertex_uniforms = g.viewport, }); try g.draws.append(g.arena, .{ .pipeline = 2, .vertices = std.mem.sliceAsBytes(quad), .vertex_count = 6, .vertex_uniforms = g.viewport, .fragment_uniforms = abi.gpu.uniform(g.arena, block(paint, alpha)), }); } /// `Paint` as the shader's uniform block. More stops than the block holds /// are resampled evenly. fn block(paint: *const Paint, alpha: f32) layouts.Paint { const m = paint.to_gradient; var out: layouts.Paint = .{ .color = paint.color, .row0 = .{ m.a, m.c, m.e, @floatFromInt(@intFromEnum(paint.kind)) }, .row1 = .{ m.b, m.d, m.f, @floatFromInt(@intFromEnum(paint.spread)) }, .points = .{ paint.p0[0], paint.p0[1], paint.p1[0], paint.p1[1] }, .extra = .{ paint.radius, alpha, 0, 0 }, .offsets = @splat(@splat(0)), .colors = @splat(@splat(0)), }; const stops = paint.stops; const n = @min(stops.len, layouts.max_stops); for (0..n) |i| { const t = if (stops.len <= layouts.max_stops) stops[i].offset else @as(f32, @floatFromInt(i)) / @as(f32, @floatFromInt(n - 1)); const color = if (stops.len <= layouts.max_stops) stops[i].color else unpremultiply(paint.stopColor(t)); out.offsets[i / 4][i % 4] = t; out.colors[i] = color; } out.extra[2] = @floatFromInt(n); return out; } fn unpremultiply(c: @Vector(4, f32)) [4]f32 { if (c[3] <= 0) return .{ 0, 0, 0, 0 }; return .{ c[0] / c[3], c[1] / c[3], c[2] / c[3], c[3] }; } /// The pass that draws everything recorded since `begin`. pub fn pass(g: *const Gpu) abi.gpu.Pass { return .{ .image = g.image, .width = g.width, .height = g.height, .samples = samples, .stencil = true, .clear = g.clear_color, .pipelines = &pipelines, .draws = g.draws.items, }; } test "a fill is a stencil fan and a covering quad with its paint" { var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); var g: Gpu = .{ .width = 100, .height = 50, .image = @enumFromInt(1) }; g.begin(arena.allocator(), .{ 0, 0, 0, 1 }); var p: Polygons = .init(arena.allocator()); try p.points.appendSlice(arena.allocator(), &.{ .{ 10, 10 }, .{ 40, 10 }, .{ 40, 30 }, .{ 10, 30 } }); try p.contours.append(arena.allocator(), 4); const red: Paint = .solid(.{ 1, 0, 0, 1 }); g.fill(&p, .evenodd, &red, 0.5); const pass_ = g.pass(); try std.testing.expectEqual(@as(usize, 2), pass_.draws.len); try std.testing.expectEqual(@as(u32, 1), pass_.draws[0].pipeline); try std.testing.expectEqual(@as(u32, 6), pass_.draws[0].vertex_count); const block_ = std.mem.bytesAsValue(layouts.Paint, pass_.draws[1].fragment_uniforms); try std.testing.expectEqual(@as(f32, 0.5), block_.extra[1]); try std.testing.expectEqual(@as(f32, 1), block_.color[0]); }