1//! 2D Vector Shape Pipeline (`sRender`, `sNGon`, `sRectangle`, `sEllipse`, `sStar`, `sPolygon`,
2//! `sBSpline`, `sText`, `sChangeStyle`, `sGrid`, `sMerge`, `sTransform`, `sDuplicate`, `sJitter`,
3//! `sExpand`, `sOffset`, `sBoolean`, `sOutline`).
4
5mod collect;
6mod collect_prims;
7mod geometry;
8mod modifiers;
9mod raster;
10#[cfg(test)]
11mod tests;
12
13use super::warps_masks::bspline::BSplinePoint;
14use super::*;
15
16use collect::collect_shape_tree_by_name;
17use raster::rasterize_shape_tree_rgba8;
18
19/// Safety cap on total collected leaf primitives so nested `sDuplicate`/`sGrid` cannot blow up memory.
20const MAX_SHAPE_PRIMITIVES: usize = 4096;
21
22#[derive(Clone, Copy, Debug)]
23struct Affine2D {
24 // [a, b, c, d, tx, ty] => x' = a*x + b*y + tx, y' = c*x + d*y + ty
25 m: [f32; 6],
26}
27
28impl Affine2D {
29 fn identity() -> Self {
30 Self { m: [1.0, 0.0, 0.0, 1.0, 0.0, 0.0] }
31 }
32
33 fn translate(tx: f32, ty: f32) -> Self {
34 Self { m: [1.0, 0.0, 0.0, 1.0, tx, ty] }
35 }
36
37 fn scale(sx: f32, sy: f32) -> Self {
38 Self { m: [sx, 0.0, 0.0, sy, 0.0, 0.0] }
39 }
40
41 fn rotate_deg(deg: f32) -> Self {
42 let r = deg.to_radians();
43 let (s, c) = r.sin_cos();
44 Self { m: [c, -s, s, c, 0.0, 0.0] }
45 }
46
47 fn mul(self, rhs: Self) -> Self {
48 let [a1, b1, c1, d1, tx1, ty1] = self.m;
49 let [a2, b2, c2, d2, tx2, ty2] = rhs.m;
50 Self {
51 m: [
52 a1 * a2 + b1 * c2,
53 a1 * b2 + b1 * d2,
54 c1 * a2 + d1 * c2,
55 c1 * b2 + d1 * d2,
56 a1 * tx2 + b1 * ty2 + tx1,
57 c1 * tx2 + d1 * ty2 + ty1,
58 ],
59 }
60 }
61
62 fn apply(self, p: [f32; 2]) -> [f32; 2] {
63 [
64 self.m[0] * p[0] + self.m[1] * p[1] + self.m[4],
65 self.m[2] * p[0] + self.m[3] * p[1] + self.m[5],
66 ]
67 }
68
69 fn avg_scale(self) -> f32 {
70 let sx = self.m[0].hypot(self.m[2]);
71 let sy = self.m[1].hypot(self.m[3]);
72 ((sx + sy) * 0.5).max(1e-4)
73 }
74
75 fn hash_into(self, h: &mut DefaultHasher) {
76 for v in self.m {
77 v.to_bits().hash(h);
78 }
79 }
80}
81
82#[derive(Clone, Copy, Debug)]
83enum ShapeCmd {
84 MoveTo([f32; 2]),
85 LineTo([f32; 2]),
86 QuadTo([f32; 2], [f32; 2]),
87 CurveTo([f32; 2], [f32; 2], [f32; 2]),
88 Close,
89}
90
91impl ShapeCmd {
92 fn hash_into(self, h: &mut DefaultHasher) {
93 match self {
94 Self::MoveTo(p) => {
95 0u8.hash(h);
96 p[0].to_bits().hash(h);
97 p[1].to_bits().hash(h);
98 }
99 Self::LineTo(p) => {
100 1u8.hash(h);
101 p[0].to_bits().hash(h);
102 p[1].to_bits().hash(h);
103 }
104 Self::QuadTo(a, p) => {
105 2u8.hash(h);
106 a[0].to_bits().hash(h);
107 a[1].to_bits().hash(h);
108 p[0].to_bits().hash(h);
109 p[1].to_bits().hash(h);
110 }
111 Self::CurveTo(a, b, p) => {
112 3u8.hash(h);
113 a[0].to_bits().hash(h);
114 a[1].to_bits().hash(h);
115 b[0].to_bits().hash(h);
116 b[1].to_bits().hash(h);
117 p[0].to_bits().hash(h);
118 p[1].to_bits().hash(h);
119 }
120 Self::Close => 4u8.hash(h),
121 }
122 }
123}
124
125#[derive(Clone, Debug)]
126enum ShapePrimKind {
127 NGon { sides: u32, width: f32, height: f32 },
128 Rectangle { width: f32, height: f32, corner_radius: f32 },
129 Ellipse { width: f32, height: f32 },
130 Star { points: u32, depth: f32, width: f32, height: f32, corner_radius: f32 },
131 Polygon { points: Vec<model::PolyPoint>, closed: bool },
132 BSpline { points: Vec<BSplinePoint>, closed: bool, tessellation: f32 },
133 Text { cmds: Vec<ShapeCmd> },
134}
135
136#[derive(Clone, Copy, Debug)]
137enum ShapeBoolOp {
138 Union,
139 Intersect,
140 Subtract,
141 Xor,
142}
143
144#[derive(Clone, Copy, Debug)]
145struct OutlineStyle {
146 thickness: f32,
147 join: u8,
148 cap: u8,
149 miter_limit: f32,
150 write_pos: f32,
151 write_len: f32,
152}
153
154#[derive(Clone, Copy, Debug, Default)]
155struct StyleOverride {
156 color: Option<[f32; 4]>,
157 solid: Option<bool>,
158 border_width: Option<f32>,
159 join_style: Option<u8>,
160 cap_style: Option<u8>,
161 line_type: Option<u8>,
162}
163
164impl StyleOverride {
165 /// Combines an inner override with an outer override (outer takes precedence per field).
166 fn merge_outer(inner: Option<Self>, outer: Option<Self>) -> Option<Self> {
167 match (inner, outer) {
168 (None, None) => None,
169 (Some(a), None) => Some(a),
170 (None, Some(b)) => Some(b),
171 (Some(a), Some(b)) => Some(Self {
172 color: b.color.or(a.color),
173 solid: b.solid.or(a.solid),
174 border_width: b.border_width.or(a.border_width),
175 join_style: b.join_style.or(a.join_style),
176 cap_style: b.cap_style.or(a.cap_style),
177 line_type: b.line_type.or(a.line_type),
178 }),
179 }
180 }
181}
182
183#[derive(Clone, Copy, Debug)]
184enum PathModifier {
185 Expand {
186 amount: f32,
187 join_style: u8,
188 miter_limit: f32,
189 },
190 Jitter {
191 x_amount: f32,
192 y_amount: f32,
193 seed: i64,
194 smoothness: f32,
195 per_shape: bool,
196 },
197 Offset {
198 offset_x: f32,
199 offset_y: f32,
200 contour_offset: f32,
201 position: f32,
202 join_style: u8,
203 miter_limit: f32,
204 },
205}
206
207impl PathModifier {
208 fn hash_into(self, h: &mut DefaultHasher) {
209 match self {
210 PathModifier::Expand { amount, join_style, miter_limit } => {
211 0u8.hash(h);
212 amount.to_bits().hash(h);
213 join_style.hash(h);
214 miter_limit.to_bits().hash(h);
215 }
216 PathModifier::Jitter { x_amount, y_amount, seed, smoothness, per_shape } => {
217 1u8.hash(h);
218 x_amount.to_bits().hash(h);
219 y_amount.to_bits().hash(h);
220 seed.hash(h);
221 smoothness.to_bits().hash(h);
222 per_shape.hash(h);
223 }
224 PathModifier::Offset {
225 offset_x,
226 offset_y,
227 contour_offset,
228 position,
229 join_style,
230 miter_limit,
231 } => {
232 2u8.hash(h);
233 offset_x.to_bits().hash(h);
234 offset_y.to_bits().hash(h);
235 contour_offset.to_bits().hash(h);
236 position.to_bits().hash(h);
237 join_style.hash(h);
238 miter_limit.to_bits().hash(h);
239 }
240 }
241 }
242}
243
244#[derive(Clone, Debug)]
245enum ShapeTree {
246 Primitive {
247 kind: ShapePrimKind,
248 xform: Affine2D,
249 color: [f32; 4],
250 solid: bool,
251 border_width: f32,
252 join_style: u8,
253 cap_style: u8,
254 miter_limit: f32,
255 write_pos: f32,
256 write_len: f32,
257 fill_even_odd: bool,
258 line_type: u8,
259 },
260 Group(Vec<ShapeTree>),
261 CombineGroup(Vec<ShapeTree>),
262 Boolean {
263 op: ShapeBoolOp,
264 color: [f32; 4],
265 opacity: f32,
266 keep_style: bool,
267 a: Box<ShapeTree>,
268 b: Box<ShapeTree>,
269 },
270 Modify {
271 op: PathModifier,
272 post_xform: Affine2D,
273 child: Box<ShapeTree>,
274 },
275}
276
277impl ShapeTree {
278 fn hash_into(&self, h: &mut DefaultHasher) {
279 match self {
280 ShapeTree::Primitive {
281 kind,
282 xform,
283 color,
284 solid,
285 border_width,
286 join_style,
287 cap_style,
288 miter_limit,
289 write_pos,
290 write_len,
291 fill_even_odd,
292 line_type,
293 } => {
294 0u8.hash(h);
295 match kind {
296 ShapePrimKind::NGon { sides, width, height } => {
297 1u8.hash(h);
298 sides.hash(h);
299 width.to_bits().hash(h);
300 height.to_bits().hash(h);
301 }
302 ShapePrimKind::Rectangle { width, height, corner_radius } => {
303 2u8.hash(h);
304 width.to_bits().hash(h);
305 height.to_bits().hash(h);
306 corner_radius.to_bits().hash(h);
307 }
308 ShapePrimKind::Ellipse { width, height } => {
309 3u8.hash(h);
310 width.to_bits().hash(h);
311 height.to_bits().hash(h);
312 }
313 ShapePrimKind::Star { points, depth, width, height, corner_radius } => {
314 4u8.hash(h);
315 points.hash(h);
316 depth.to_bits().hash(h);
317 width.to_bits().hash(h);
318 height.to_bits().hash(h);
319 corner_radius.to_bits().hash(h);
320 }
321 ShapePrimKind::Polygon { points, closed } => {
322 5u8.hash(h);
323 closed.hash(h);
324 points.len().hash(h);
325 for pt in points {
326 pt.x.to_bits().hash(h);
327 pt.y.to_bits().hash(h);
328 pt.lx.to_bits().hash(h);
329 pt.ly.to_bits().hash(h);
330 pt.rx.to_bits().hash(h);
331 pt.ry.to_bits().hash(h);
332 pt.linear.hash(h);
333 }
334 }
335 ShapePrimKind::BSpline { points, closed, tessellation } => {
336 6u8.hash(h);
337 closed.hash(h);
338 tessellation.to_bits().hash(h);
339 points.len().hash(h);
340 for pt in points {
341 pt.x.to_bits().hash(h);
342 pt.y.to_bits().hash(h);
343 pt.w.to_bits().hash(h);
344 }
345 }
346 ShapePrimKind::Text { cmds } => {
347 7u8.hash(h);
348 cmds.len().hash(h);
349 for cmd in cmds {
350 cmd.hash_into(h);
351 }
352 }
353 }
354 xform.hash_into(h);
355 for c in color {
356 c.to_bits().hash(h);
357 }
358 solid.hash(h);
359 border_width.to_bits().hash(h);
360 join_style.hash(h);
361 cap_style.hash(h);
362 miter_limit.to_bits().hash(h);
363 write_pos.to_bits().hash(h);
364 write_len.to_bits().hash(h);
365 fill_even_odd.hash(h);
366 line_type.hash(h);
367 }
368 ShapeTree::Group(children) => {
369 1u8.hash(h);
370 children.len().hash(h);
371 for ch in children {
372 ch.hash_into(h);
373 }
374 }
375 ShapeTree::CombineGroup(children) => {
376 4u8.hash(h);
377 children.len().hash(h);
378 for ch in children {
379 ch.hash_into(h);
380 }
381 }
382 ShapeTree::Boolean { op, color, opacity, keep_style, a, b } => {
383 2u8.hash(h);
384 (*op as u8).hash(h);
385 for c in color {
386 c.to_bits().hash(h);
387 }
388 opacity.to_bits().hash(h);
389 keep_style.hash(h);
390 a.hash_into(h);
391 b.hash_into(h);
392 }
393 ShapeTree::Modify { op, post_xform, child } => {
394 3u8.hash(h);
395 op.hash_into(h);
396 post_xform.hash_into(h);
397 child.hash_into(h);
398 }
399 }
400 }
401}
402
403impl Engine {
404 #[allow(clippy::too_many_arguments)]
405 pub(super) fn eval_s_render(
406 &mut self,
407 comp: &Comp,
408 node: &Node,
409 time: f64,
410 frame_i: i64,
411 comp_dims: [u32; 2],
412 enc: &mut wgpu::CommandEncoder,
413 visiting: &mut HashSet<(NodeId, i64)>,
414 stats: &mut EvalStats,
415 ) -> CachedTexture {
416 let w = comp.eval_number(node, "Width", time, comp.width as f64, &self.expr).round().max(1.0) as u32;
417 let h = comp.eval_number(node, "Height", time, comp.height as f64, &self.expr).round().max(1.0) as u32;
418 let dims = if w > 1 && h > 1 { self.scaled_dims(w, h) } else { comp_dims };
419 let dst_depth = self.node_depth(comp, node, time, None);
420 // Fusion sRender ignores Blend (always renders at full 1.0 opacity).
421 let blend = 1.0_f32;
422
423 let mut prim_budget = MAX_SHAPE_PRIMITIVES;
424 let shape_tree = node.connected_node("Input").and_then(|src_name| {
425 collect_shape_tree_by_name(
426 comp,
427 src_name,
428 time,
429 &self.expr,
430 Affine2D::identity(),
431 None,
432 None,
433 0,
434 &mut prim_budget,
435 )
436 });
437
438 let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(dims), enc, visiting, stats);
439 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);
440
441 let mut h_key = DefaultHasher::new();
442 self.cache.gpu_salt.hash(&mut h_key);
443 self.hiq.hash(&mut h_key);
444 self.cache.node_salt(node.id).hash(&mut h_key);
445 "sRender".hash(&mut h_key);
446 dims.hash(&mut h_key);
447 dst_depth.hash(&mut h_key);
448 blend.to_bits().hash(&mut h_key);
449 mask_hash.hash(&mut h_key);
450 mask_inv.hash(&mut h_key);
451 if let Some(ref tree) = shape_tree {
452 1u8.hash(&mut h_key);
453 tree.hash_into(&mut h_key);
454 } else {
455 0u8.hash(&mut h_key);
456 }
457 let key = h_key.finish();
458
459 if let Some(hit) = self.cache.get(key, node.id, frame_i) {
460 stats.cache_hits += 1;
461 return hit;
462 }
463 stats.cache_misses += 1;
464 stats.nodes_evaluated += 1;
465
466 let rgba = rasterize_shape_tree_rgba8(shape_tree.as_ref(), dims, blend);
467 let needs_post = dst_depth != BitDepth::Int8 || mask.is_some();
468 let base_tex = create_texture(&self.device, dims, if needs_post { 0 } else { key });
469 self.queue.write_texture(
470 base_tex.texture.as_image_copy(),
471 &rgba,
472 wgpu::TexelCopyBufferLayout {
473 offset: 0,
474 bytes_per_row: Some(dims[0] * 4),
475 rows_per_image: Some(dims[1]),
476 },
477 wgpu::Extent3d {
478 width: dims[0],
479 height: dims[1],
480 depth_or_array_layers: 1,
481 },
482 );
483
484 let mut out = if dst_depth != BitDepth::Int8 {
485 let promoted = self.alloc_tex(dims, dst_depth, if mask.is_some() { 0 } else { key });
486 let u = UniformParams::default();
487 self.dispatch_op(
488 enc,
489 ShaderOp::ChangeDepth,
490 &base_tex.view,
491 &self.dummy.view,
492 &self.dummy.view,
493 &promoted,
494 &u,
495 );
496 promoted
497 } else {
498 base_tex
499 };
500
501 if let Some(ref mtex) = mask {
502 let masked = self.alloc_tex(dims, out.depth, key);
503 let mut u = UniformParams::default();
504 u.p[0] = [if mask_inv { 1.0 } else { 0.0 }, 0.0, 0.0, 0.0];
505 self.dispatch_op(
506 enc,
507 ShaderOp::MaskApply,
508 &out.view,
509 &self.dummy.view,
510 &mtex.view,
511 &masked,
512 &u,
513 );
514 out = masked;
515 }
516
517 self.cache.insert(key, out.clone(), node.id, frame_i);
518 out
519 }
520}