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
//! Materials in a pass: registration, the clock of animated ones, how a drawn material
//! reaches its element, and the repaint it asks for while it is visible.
use super::{Context, DiagnosticKind, Id};
use crate::{
material::{block, FrameData, MaterialProgram},
time::Instant,
Material, MaterialError, MaterialId, Vec2,
};
use std::sync::Arc;
/// Seconds after which `in.time` starts again at zero: keeps f32 precision for long sessions.
const TIME_WRAP: f64 = 3600.0;
/// A material applied to an element, with everything that varies per frame. Compared by
/// value between passes: a change rebuilds the draw commands, never the mesh.
#[derive(Clone, Debug)]
pub struct MaterialUse {
pub(crate) program: Arc<MaterialProgram>,
/// Packed parameters, as long as the material's layout says.
pub(crate) params: Arc<[u8]>,
/// Size of the shape in logical pixels, after the scale of visuals around it.
pub(crate) size: Vec2,
/// Displayed part of the widget texture: origin and size in 0..1.
pub(crate) texture_rect: [f32; 4],
pub(crate) time: f32,
pub(crate) delta: f32,
/// This draw reads the clock: it keeps frames coming while it is visible.
pub(crate) animated: bool,
}
impl PartialEq for MaterialUse {
fn eq(&self, other: &Self) -> bool {
self.program.source.id == other.program.source.id
&& self.params == other.params
&& self.size == other.size
&& self.texture_rect == other.texture_rect
&& self.time == other.time
&& self.delta == other.delta
&& self.animated == other.animated
}
}
impl MaterialUse {
pub fn id(&self) -> MaterialId {
self.program.source.id
}
pub fn size(&self) -> Vec2 {
self.size
}
/// The packed parameters of this draw.
pub fn params(&self) -> &[u8] {
&self.params
}
/// The uniform block of a draw: frame data, then the parameters.
pub(crate) fn block(&self) -> Vec<u8> {
block(
&FrameData {
size: self.size.to_array(),
time: self.time,
delta: self.delta,
texture_rect: self.texture_rect,
},
&self.params,
)
}
/// Scale the shape by a visual transform around it.
pub(crate) fn scale(&mut self, factor: f32) {
self.size *= factor;
}
}
/// The clock animated materials read.
#[derive(Default)]
pub(crate) struct MaterialState {
epoch: Option<Instant>,
/// Time of the previous pass, for `in.delta`.
previous: Option<Instant>,
/// A visible animated material was drawn in the last finished pass.
pub(crate) animating: bool,
}
impl Context {
/// Register `material` and return its id; see
/// [`SharedResources::register_material`](crate::SharedResources::register_material).
/// A rejected description is reported once through [`Self::diagnostics`] and gets an id
/// whose draws use their fallback.
pub fn register_material(&mut self, material: &Material) -> MaterialId {
if let Err(error) = self.shared.try_register_material(material) {
self.report_material_error(&error);
}
self.shared.register_material(material)
}
/// Register `material`, returning why it is invalid instead of reporting it.
pub fn try_register_material(
&mut self,
material: &Material,
) -> Result<MaterialId, MaterialError> {
self.shared.try_register_material(material)
}
/// Why the material `id` cannot be drawn, if it cannot.
pub fn material_error(&self, id: MaterialId) -> Option<MaterialError> {
self.shared.material_error(id)
}
pub(crate) fn report_material_error(&mut self, error: &MaterialError) {
// One report per distinct material and message, whatever the pass.
let id = Id::new(("material-error", error.label(), error.message()));
self.report(DiagnosticKind::InvalidShader, Some(id), None, || {
error.to_string()
});
}
/// `in.time` and `in.delta` for a draw now: zeros unless it is animated, and when motion is
/// reduced.
pub(crate) fn material_clock(&mut self, animated: bool) -> (f32, f32) {
if !animated || self.style.motion.reduced_motion {
return (0.0, 0.0);
}
let now = self.frame_time;
let epoch = *self.materials.epoch.get_or_insert(now);
let time = now.saturating_duration_since(epoch).as_secs_f64() % TIME_WRAP;
let delta = self.materials.previous.map_or(0.0, |p| {
now.saturating_duration_since(p).as_secs_f64().min(1.0)
});
(time as f32, delta as f32)
}
/// Make the element just painted under `id` a draw of `material`, wherever it went. A
/// backdrop material is also a backdrop effect of `backdrop` sigma.
pub(crate) fn mark_material(&mut self, id: Id, material: MaterialUse, backdrop: Option<f32>) {
if let Some(paint) = self
.placements
.stack
.last_mut()
.and_then(|p| p.paints.last_mut())
.filter(|p| p.id == id)
{
paint.material = Some(material);
paint.blur = backdrop;
} else if let Some(paint) = self.scrolling.pending.last_mut().filter(|p| p.id == id) {
paint.material = Some(material);
paint.blur = backdrop;
} else if let Some(element) = self.paint_state.last_element_mut(id) {
element.material = Some(material);
element.blur = backdrop;
}
}
/// End of pass: ask for the next frame while an animated material is on screen. Only
/// elements that were emitted count, so a material scrolled out of its clip or hidden
/// with its window stops asking.
pub(super) fn finish_materials(&mut self) {
let animating = !self.style.motion.reduced_motion
&& self
.paint_state
.elements
.iter()
.any(|e| e.material.as_ref().is_some_and(|m| m.animated));
self.materials.animating = animating;
self.materials.previous = Some(self.frame_time);
if animating {
let interval = self
.style
.motion
.frame_interval
.max(std::time::Duration::from_millis(1));
self.request_repaint_after(interval);
}
}
}