1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
//! 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]);
}
|