1use crate::{Color, Corners, Point, Rect};
2use std::ptr;
3
4const SHADOW_REACH: f32 = 28.0;
5const HEADER: usize = 4;
6const DIAGONAL_RIM_ANGLE: f32 = -0.643_501_1;
8pub(crate) const PASSTHROUGH_WGSL: &str = include_str!("shaders/passthrough.wgsl");
9
10#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
11pub struct Inputs(u8);
12
13impl Inputs {
14 pub const CONTENT: Inputs = Inputs(1);
15 pub const BACKDROP: Inputs = Inputs(2);
16 pub const BOTH: Inputs = Inputs(3);
17
18 pub const fn content(self) -> bool {
19 self.0 & 1 != 0
20 }
21
22 pub const fn backdrop(self) -> bool {
23 self.0 & 2 != 0
24 }
25}
26
27pub trait ShaderEffect: 'static {
28 const WGSL: &'static str;
29 const INPUTS: Inputs;
30 const CLOCK: Option<usize> = None;
31
32 fn uniforms(&self, _uniforms: &mut [f32; EffectInstance::UNIFORMS]) {}
33
34 fn margin(&self) -> f32 {
35 0.0
36 }
37
38 fn content_blur(&self) -> f32 {
39 0.0
40 }
41
42 fn backdrop_blur(&self) -> f32 {
43 0.0
44 }
45
46 fn backdrop_reach(&self) -> Option<f32> {
47 None
48 }
49}
50
51#[derive(Clone, Copy, Debug, PartialEq)]
52pub struct EffectInstance {
53 wgsl: &'static str,
54 inputs: Inputs,
55 clock: Option<usize>,
56 values: [f32; EffectInstance::VALUES],
57}
58
59impl EffectInstance {
60 pub const UNIFORMS: usize = 16;
61 pub const VALUES: usize = Self::UNIFORMS + HEADER;
62
63 pub fn new<T: ShaderEffect>(effect: &T) -> Self {
64 assert!(
65 T::CLOCK.is_none_or(|index| index < Self::UNIFORMS),
66 "rouge: an effect's clock is one of its {} uniform values",
67 Self::UNIFORMS
68 );
69 let mut uniforms = [0.0; Self::UNIFORMS];
70 effect.uniforms(&mut uniforms);
71 let mut values = [0.0; Self::VALUES];
72 values[0] = effect.margin().max(0.0);
73 values[1] = effect.content_blur().max(0.0);
74 values[2] = effect.backdrop_blur().max(0.0);
75 values[3] = effect.backdrop_reach().map_or(-1.0, |reach| reach.max(0.0));
76 values[HEADER..].copy_from_slice(&uniforms);
77 EffectInstance {
78 wgsl: T::WGSL,
79 inputs: T::INPUTS,
80 clock: T::CLOCK,
81 values,
82 }
83 }
84
85 pub fn with_values(self, values: [f32; Self::VALUES]) -> Self {
86 EffectInstance { values, ..self }
87 }
88
89 pub fn at(mut self, seconds: f32) -> Self {
90 if let Some(index) = self.clock {
91 self.values[HEADER + index] = seconds;
92 }
93 self
94 }
95
96 pub fn wgsl(&self) -> &'static str {
97 self.wgsl
98 }
99
100 pub fn same_shader(&self, other: &EffectInstance) -> bool {
101 self.inputs == other.inputs
102 && self.clock == other.clock
103 && (ptr::eq(self.wgsl, other.wgsl) || self.wgsl == other.wgsl)
104 }
105
106 pub fn inputs(&self) -> Inputs {
107 self.inputs
108 }
109
110 pub fn clock(&self) -> Option<usize> {
111 self.clock
112 }
113
114 pub fn values(&self) -> &[f32; Self::VALUES] {
115 &self.values
116 }
117
118 pub fn margin(&self) -> f32 {
119 self.values[0]
120 }
121
122 pub fn content_blur(&self) -> f32 {
123 self.values[1]
124 }
125
126 pub fn backdrop_blur(&self) -> f32 {
127 self.values[2]
128 }
129
130 pub fn backdrop_reach(&self) -> Option<f32> {
131 Some(self.values[3]).filter(|reach| *reach >= 0.0)
132 }
133
134 pub fn uniforms(&self) -> [f32; Self::UNIFORMS] {
135 let mut uniforms = [0.0; Self::UNIFORMS];
136 uniforms.copy_from_slice(&self.values[HEADER..]);
137 uniforms
138 }
139}
140
141#[derive(Clone, Copy, Debug, PartialEq)]
142pub struct EffectDraw {
143 pub effect: EffectInstance,
144 pub bounds: Rect,
145 pub corner_radii: Corners,
146}
147
148impl EffectDraw {
149 pub fn new<T: ShaderEffect>(effect: &T, bounds: Rect) -> Self {
150 EffectDraw {
151 effect: EffectInstance::new(effect),
152 bounds,
153 corner_radii: Corners::ZERO,
154 }
155 }
156}
157
158pub(crate) struct Passthrough;
159
160impl ShaderEffect for Passthrough {
161 const WGSL: &'static str = PASSTHROUGH_WGSL;
162 const INPUTS: Inputs = Inputs::CONTENT;
163}
164
165pub(crate) struct SurfaceSample {
166 pub opaque: bool,
167}
168
169impl ShaderEffect for SurfaceSample {
170 const WGSL: &'static str = include_str!("shaders/surface.wgsl");
171 const INPUTS: Inputs = Inputs::CONTENT;
172
173 fn uniforms(&self, uniforms: &mut [f32; EffectInstance::UNIFORMS]) {
174 uniforms[0] = f32::from(u8::from(self.opaque));
175 }
176}
177
178#[derive(Clone, Copy, Debug, PartialEq)]
179pub struct Blur {
180 pub radius: f32,
181}
182
183impl Blur {
184 pub const fn new(radius: f32) -> Self {
185 Blur { radius }
186 }
187}
188
189impl ShaderEffect for Blur {
190 const WGSL: &'static str = PASSTHROUGH_WGSL;
191 const INPUTS: Inputs = Inputs::CONTENT;
192
193 fn content_blur(&self) -> f32 {
194 self.radius
195 }
196}
197
198#[derive(Clone, Copy, Debug, PartialEq)]
199pub struct BackdropBlur {
200 pub radius: f32,
201}
202
203impl BackdropBlur {
204 pub const fn new(radius: f32) -> Self {
205 BackdropBlur { radius }
206 }
207}
208
209impl ShaderEffect for BackdropBlur {
210 const WGSL: &'static str = include_str!("shaders/backdrop_blur.wgsl");
211 const INPUTS: Inputs = Inputs::BACKDROP;
212
213 fn margin(&self) -> f32 {
214 1.0
215 }
216
217 fn backdrop_blur(&self) -> f32 {
218 self.radius
219 }
220
221 fn backdrop_reach(&self) -> Option<f32> {
222 Some(0.0)
223 }
224}
225
226#[derive(Clone, Copy, Debug, PartialEq)]
227pub struct ColorMatrix {
228 pub rows: [[f32; 4]; 3],
229}
230
231const LUMA: [f32; 3] = [0.2126, 0.7152, 0.0722];
232
233impl ColorMatrix {
234 pub const IDENTITY: ColorMatrix = ColorMatrix {
235 rows: [
236 [1.0, 0.0, 0.0, 0.0],
237 [0.0, 1.0, 0.0, 0.0],
238 [0.0, 0.0, 1.0, 0.0],
239 ],
240 };
241
242 pub const fn new(rows: [[f32; 4]; 3]) -> Self {
243 ColorMatrix { rows }
244 }
245
246 pub fn saturate(amount: f32) -> Self {
247 let mut rows = [[0.0; 4]; 3];
248 for (row, cells) in rows.iter_mut().enumerate() {
249 for (column, luma) in LUMA.into_iter().enumerate() {
250 cells[column] = luma * (1.0 - amount) + if row == column { amount } else { 0.0 };
251 }
252 }
253 ColorMatrix { rows }
254 }
255
256 pub fn grayscale() -> Self {
257 Self::saturate(0.0)
258 }
259
260 pub fn brightness(amount: f32) -> Self {
261 Self::scale(amount, 0.0)
262 }
263
264 pub fn contrast(amount: f32) -> Self {
265 Self::scale(amount, 0.5 - 0.5 * amount)
266 }
267
268 pub fn invert() -> Self {
269 Self::scale(-1.0, 1.0)
270 }
271
272 pub fn tint(color: Color) -> Self {
273 let mut matrix = Self::IDENTITY;
274 for (cells, channel) in matrix.rows.iter_mut().zip([color.r, color.g, color.b]) {
275 for cell in cells.iter_mut().take(3) {
276 *cell *= 1.0 - color.a;
277 }
278 cells[3] = channel * color.a;
279 }
280 matrix
281 }
282
283 pub fn then(&self, next: &ColorMatrix) -> ColorMatrix {
284 let mut rows = [[0.0; 4]; 3];
285 for (out, by) in rows.iter_mut().zip(next.rows) {
286 for (column, cell) in out.iter_mut().enumerate() {
287 let through: f32 = (0..3).map(|k| by[k] * self.rows[k][column]).sum();
288 *cell = through + if column == 3 { by[3] } else { 0.0 };
289 }
290 }
291 ColorMatrix { rows }
292 }
293
294 fn scale(factor: f32, offset: f32) -> Self {
295 let mut matrix = Self::IDENTITY;
296 for (index, cells) in matrix.rows.iter_mut().enumerate() {
297 cells[index] = factor;
298 cells[3] = offset;
299 }
300 matrix
301 }
302}
303
304impl ShaderEffect for ColorMatrix {
305 const WGSL: &'static str = include_str!("shaders/color_matrix.wgsl");
306 const INPUTS: Inputs = Inputs::CONTENT;
307
308 fn uniforms(&self, uniforms: &mut [f32; EffectInstance::UNIFORMS]) {
309 uniforms[..12].copy_from_slice(self.rows.as_flattened());
310 }
311}
312
313#[derive(Clone, Copy, Debug, PartialEq)]
314enum MaskKind {
315 Linear { from: Point, to: Point },
316 Radial { center: Point, radius: f32 },
317 Shape,
318}
319
320#[derive(Clone, Copy, Debug, PartialEq)]
321pub struct Mask {
322 kind: MaskKind,
323 stops: (f32, f32),
324 inverted: bool,
325}
326
327impl Mask {
328 const fn of(kind: MaskKind) -> Self {
329 Mask {
330 kind,
331 stops: (0.0, 1.0),
332 inverted: false,
333 }
334 }
335
336 pub const fn linear_gradient(from: Point, to: Point) -> Self {
337 Mask::of(MaskKind::Linear { from, to })
338 }
339
340 pub const fn radial_gradient(center: Point, radius: f32) -> Self {
341 Mask::of(MaskKind::Radial { center, radius })
342 }
343
344 pub const fn shape() -> Self {
345 Mask::of(MaskKind::Shape)
346 }
347
348 pub const fn stops(mut self, opaque_until: f32, clear_from: f32) -> Self {
349 self.stops = (opaque_until, clear_from);
350 self
351 }
352
353 pub const fn invert(mut self) -> Self {
354 self.inverted = true;
355 self
356 }
357}
358
359impl ShaderEffect for Mask {
360 const WGSL: &'static str = include_str!("shaders/mask.wgsl");
361 const INPUTS: Inputs = Inputs::CONTENT;
362
363 fn uniforms(&self, uniforms: &mut [f32; EffectInstance::UNIFORMS]) {
364 let (kind, shape) = match self.kind {
365 MaskKind::Linear { from, to } => (0.0, [from.x, from.y, to.x, to.y]),
366 MaskKind::Radial { center, radius } => (1.0, [center.x, center.y, radius, 0.0]),
367 MaskKind::Shape => (2.0, [0.0; 4]),
368 };
369 uniforms[..4].copy_from_slice(&[
370 kind,
371 f32::from(u8::from(self.inverted)),
372 self.stops.0,
373 self.stops.1,
374 ]);
375 uniforms[4..8].copy_from_slice(&shape);
376 }
377}
378
379#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
380pub enum BlendMode {
381 Multiply,
382 Screen,
383 Overlay,
384 Darken,
385 Lighten,
386 ColorDodge,
387 ColorBurn,
388 HardLight,
389 SoftLight,
390 Difference,
391 Exclusion,
392 PlusLighter,
393}
394
395impl ShaderEffect for BlendMode {
396 const WGSL: &'static str = include_str!("shaders/blend.wgsl");
397 const INPUTS: Inputs = Inputs::BOTH;
398
399 fn uniforms(&self, uniforms: &mut [f32; EffectInstance::UNIFORMS]) {
400 uniforms[0] = f32::from(*self as u8);
401 }
402}
403
404#[derive(Clone, Copy, Debug, PartialEq)]
405pub struct Glass {
406 blur: f32,
407 tint: Color,
408 refraction: f32,
409 dispersion: f32,
410 highlight: f32,
411 edge_width: f32,
412 shadow: f32,
413 saturation: f32,
414 light: Point,
415 light_strength: f32,
416 light_reach: f32,
417 rim_angle: f32,
418 rim_fill: f32,
419}
420
421impl Default for Glass {
422 fn default() -> Self {
423 Self::new()
424 }
425}
426
427impl Glass {
428 pub const fn new() -> Self {
429 Glass {
430 blur: 10.0,
431 tint: Color::new(1.0, 1.0, 1.0, 0.08),
432 refraction: 0.7,
433 dispersion: 0.12,
434 highlight: 0.35,
435 edge_width: 14.0,
436 shadow: 0.25,
437 saturation: 1.0,
438 light: Point::new(0.0, 0.0),
439 light_strength: 0.0,
440 light_reach: 1.0,
441 rim_angle: DIAGONAL_RIM_ANGLE,
442 rim_fill: 0.6,
443 }
444 }
445
446 pub const fn frosted(blur: f32) -> Self {
447 Glass {
448 blur,
449 tint: Color::TRANSPARENT,
450 refraction: 0.0,
451 dispersion: 0.0,
452 highlight: 0.0,
453 edge_width: 1.0,
454 shadow: 0.0,
455 ..Glass::new()
456 }
457 }
458
459 pub const fn blur(mut self, radius: f32) -> Self {
460 self.blur = radius;
461 self
462 }
463
464 pub const fn tint(mut self, tint: Color) -> Self {
465 self.tint = tint;
466 self
467 }
468
469 pub const fn refraction(mut self, amount: f32) -> Self {
470 self.refraction = amount;
471 self
472 }
473
474 pub const fn dispersion(mut self, amount: f32) -> Self {
475 self.dispersion = amount;
476 self
477 }
478
479 pub const fn highlight(mut self, amount: f32) -> Self {
480 self.highlight = amount;
481 self
482 }
483
484 pub const fn edge_width(mut self, width: f32) -> Self {
485 self.edge_width = width;
486 self
487 }
488
489 pub const fn shadow(mut self, amount: f32) -> Self {
490 self.shadow = amount;
491 self
492 }
493
494 pub const fn saturation(mut self, amount: f32) -> Self {
495 self.saturation = amount;
496 self
497 }
498
499 pub const fn light(mut self, at: Point, strength: f32, reach: f32) -> Self {
500 self.light = at;
501 self.light_strength = strength;
502 self.light_reach = reach;
503 self
504 }
505
506 pub const fn rim_light(mut self, angle: f32, fill: f32) -> Self {
509 self.rim_angle = angle;
510 self.rim_fill = fill;
511 self
512 }
513}
514
515impl ShaderEffect for Glass {
516 const WGSL: &'static str = include_str!("shaders/glass.wgsl");
517 const INPUTS: Inputs = Inputs::BACKDROP;
518
519 fn uniforms(&self, uniforms: &mut [f32; EffectInstance::UNIFORMS]) {
520 uniforms[..4].copy_from_slice(&self.tint.to_array());
521 uniforms[4..].copy_from_slice(&[
522 self.refraction,
523 self.dispersion,
524 self.highlight,
525 self.edge_width,
526 self.shadow,
527 self.saturation,
528 self.light.x,
529 self.light.y,
530 self.light_strength,
531 self.light_reach,
532 self.rim_angle,
533 self.rim_fill,
534 ]);
535 }
536
537 fn margin(&self) -> f32 {
538 let outward =
539 (-self.refraction).max(0.0) * self.edge_width * 2.0 * (1.0 + self.dispersion.abs());
540 let shadow = if self.shadow > 0.0 { SHADOW_REACH } else { 1.0 };
541 shadow.max(outward)
542 }
543
544 fn backdrop_blur(&self) -> f32 {
545 self.blur
546 }
547
548 fn backdrop_reach(&self) -> Option<f32> {
549 Some(self.refraction.abs() * self.edge_width * 2.0 * (1.0 + self.dispersion.abs()))
550 }
551}