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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//! Blurred rounded rectangle encoding: `Command::BlurredRoundedRect` to a
//! `vello_common` [`EncodedPaint::BlurredRoundedRect`] entry, plus the padded
//! bounding rectangle a strip generator rasterizes it through.
//!
//! A gaussian-blurred shadow has no hard edge, so it is compiled in two
//! halves that meet only at the draw the compiler records: the *coverage*
//! this file names — a plain axis-aligned rectangle, inflated past `rect` by
//! the kernel's own falloff distance ([`inflated_bounds`]) — decides which
//! pixels the draw can paint at all, while the *shape* (the rounding, the
//! standard deviation, the falloff curve itself) is baked into the encoded
//! paint ([`encode_blurred_rounded_rect`]) and evaluated per pixel by the
//! fragment shader (`calculate_blurred_rounded_rect` in `helpers.wgsl`,
//! dispatched by the `PaintType::BlurredRoundedRect` branch in `strip.wgsl` —
//! both already carry this primitive's GPU half, ported ahead of this file
//! from `helpers/blurred_rounded_rect.wesl`; there is nothing of theirs left
//! to add here). Generating strip coverage for a plain rectangle rather than
//! for the rounded shape itself is deliberate, not a shortcut: the corners
//! the shader rounds are *inside* that rectangle, and the blur reaches
//! outside the rectangle the un-blurred shadow would occupy, so a strip
//! generator that rasterized the rounded shape instead would clip exactly
//! the pixels the blur exists to reach.
//!
//! ## Radius arity
//!
//! `vello_common` 0.2.0's [`BlurredRoundedRectangle`] takes a single `f32`
//! `radius` — the same arity vello_cpu's `fill_blurred_rounded_rect` carries,
//! which is why the CPU oracle collapses a per-corner shadow through
//! [`CornerRadii::largest`] (see `frust-testing::oracle_cpu`; recorded as the
//! accepted limitation `render-blurred-shadow-corner-collapse`). No vello 0.2 type in
//! this dependency line expresses a per-corner blurred rectangle, so this
//! encoder collapses the same way, through the same method, for the same
//! reason `frust_scene::CornerRadii::largest`'s own doc gives: a shadow
//! rounded *more* than its caster only lightens a square corner, while one
//! rounded *less* pushes a hard wedge out through a rounded corner's notch —
//! collapsing to the largest radius is the direction that costs the least
//! fidelity. There is no fidelity win to report here: this engine tier is
//! bounded by the identical single-radius vocabulary those tiers
//! already are, not by a choice made in this file.
//!
//! ## Invert
//!
//! [`BlurredRoundedRectangle::invert`] lets `vello_hybrid` paint the inverse
//! of the blur coverage for an inset shadow (`vello_hybrid` 0.2.0
//! `scene.rs:639-650`), but [`frust_scene::Command::BlurredRoundedRect`]
//! carries no `invert` field — `frust-scene`'s display list has no
//! inset-shadow command yet — so every shadow this compiler lowers is
//! encoded with `invert: false`. Adding an inset variant is a `frust-scene`
//! change, out of this file's scope.
use Color;
use BlurredRoundedRectangle;
use ;
use ;
use Paint;
use CornerRadii;
/// How many standard deviations of blur kernel the coverage rectangle in
/// [`inflated_bounds`] is padded by, on every side.
///
/// `vello_hybrid` 0.2.0's own `Scene::fill_blurred_rounded_rect`
/// (`scene.rs:670`) inflates by this same `2.5 * std_dev` before
/// rasterizing, and this compiler matches it exactly rather than deriving a
/// tolerance of its own: past this distance the gaussian falloff
/// `calculate_blurred_rounded_rect` evaluates is close enough to zero that a
/// visible difference from a wider pad is not expected, and a narrower one
/// risks clipping a visible tail — the two render tiers and this one should
/// agree on where a shadow ends.
const BLUR_KERNEL_STD_DEVS: f64 = 2.5;
/// The widest pad [`inflated_bounds`] will apply, in the same pre-transform
/// units as the rectangle it inflates.
///
/// The compiler accepts any finite `std_dev`, and an extreme one inflates
/// the coverage rectangle to coordinates that degrade the strip generator's
/// tile arithmetic into malformed strips outside the tile-snapped viewport.
/// The cap bounds the *pad* — never the standard deviation the shader
/// evaluates — at a distance no addressable surface approaches
/// (`max_texture_size` tops out at 16384), so it cannot change a rendered
/// pixel: coverage past the surface is invisible, and every realistic blur
/// keeps its full falloff.
const MAX_KERNEL_PAD: f64 = 1.0e6;
/// The axis-aligned rectangle a blurred rounded rectangle's strip coverage is
/// generated over, in the same (pre-transform) coordinate space as `rect`.
///
/// Padding by [`BLUR_KERNEL_STD_DEVS`] standard deviations on every side is
/// what keeps the strip generator from clipping the blur's own falloff tail
/// — see the module doc for why this is a plain rectangle pad rather than a
/// rounded one. The pad is capped at [`MAX_KERNEL_PAD`], a coverage-only
/// bound that no visible blur reaches. A non-finite or negative `std_dev`
/// is not guarded here: it cannot reach this function at all, because
/// [`super::check_geometry`] refuses a non-finite `std_dev` before any
/// command is compiled, and `frust_scene::SceneBuilder`'s blurred-rect
/// constructors take a caller-supplied standard deviation on the same terms
/// every other geometry parameter is — a negative one is nonsensical but
/// not this compiler's to reject.
/// Encode a blurred rounded rectangle into `encoded_paints`, returning the
/// paint the draw that rasterizes [`inflated_bounds`] references.
///
/// `radii` collapses through [`CornerRadii::largest`] — see the module doc's
/// "Radius arity" section for why. `transform` is the paint transform in
/// effect for the draw, the same composed transform the coverage rectangle
/// is rasterized under; `BlurredRoundedRectangle::encode_into` folds its
/// inverse into the encoded entry, which is the direction the shader samples
/// in.
///
/// Unlike an image, this can never fail: every field a
/// [`Command::BlurredRoundedRect`](frust_scene::Command::BlurredRoundedRect)
/// carries reaches here already checked finite by
/// [`super::check_geometry`], and `vello_common`'s own encoding clamps a
/// degenerate radius or a vanishing standard deviation internally rather
/// than refusing them — so, unlike [`crate::compile::paint::encode_brush`]'s
/// image arm, there is no skip case for a caller here to route around.