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
// Where a scene's pass draws. A scene of one sample in each pixel draws onto
// the page, over what it holds. A multisampled scene cannot: a pass loads
// what an attachment holds only into an attachment as many samples deep,
// while the page holds one. It draws into targets of its own, several
// samples deep, cleared to transparent. Each pass averages them into a
// texture, which a composite then lays over the page where the scene shows.
// Premultiplied colour laid over transparent and then over the page is the
// colour laid over the page. Glow, which adds colour and keeps alpha, adds
// the same way. The targets take the scene's size, within the page's, so a
// scene that scrolls finds the same targets each frame. They lie over the
// part of the scene the page shows.
use std::rc::Rc;
use cranpose_ui_graphics::{BlendMode, space::Multisample};
use super::super::{FrameExecutor, LayerPass};
use crate::{
collect::SceneDraw,
draw_pass::{ResolvedComposite, ResolvedCompositeKind, SourceContent},
effect_renderer::CompositeSampleMode,
frame_graph::FrameCommandRecorder,
offscreen::OffscreenTarget,
scene_pass::{ScenePage, scene_scissor},
};
// The colour, depth and stencil a scene's passes draw into, the page as its
// shaders see it, and the samples they take.
pub(super) struct SceneTarget {
pub(super) page: ScenePage,
pub(super) samples: Multisample,
depth: wgpu::TextureView,
colour: SceneColour,
}
enum SceneColour {
// The page itself, which the passes draw over.
Page(Rc<OffscreenTarget>),
// Colour several samples deep, cleared at each pass, whose average each
// pass lays over the page at `z`, where `shown`, the pixels of the
// targets the page shows, lie.
Sampled {
view: wgpu::TextureView,
z: usize,
shown: (u32, u32, u32, u32),
},
}
impl SceneTarget {
// The z below which a capture of the page sees what the scene's passes
// have drawn, for a scene whose first surface lies at `z`.
pub(super) fn drawn_below(&self, z: usize) -> usize {
match self.colour {
SceneColour::Page(_) => z,
SceneColour::Sampled { .. } => z + 1,
}
}
pub(super) fn depth(&self) -> &wgpu::TextureView {
&self.depth
}
// The colour a pass draws into: the page, which keeps what it holds, or
// the samples, cleared, then averaged into `resolved` and dropped.
pub(super) fn colour<'a>(
&'a self,
resolved: Option<&'a OffscreenTarget>,
) -> wgpu::RenderPassColorAttachment<'a> {
let (view, resolve_target, ops) = match &self.colour {
SceneColour::Page(page) => (
&page.view,
None,
wgpu::Operations {
load: wgpu::LoadOp::Load,
store: wgpu::StoreOp::Store,
},
),
SceneColour::Sampled { view, .. } => (
view,
resolved.map(|resolved| &resolved.view),
wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
store: wgpu::StoreOp::Discard,
},
),
};
wgpu::RenderPassColorAttachment {
view,
resolve_target,
depth_slice: None,
ops,
}
}
// The composite that lays `resolved`, a pass's averaged samples, over the
// page where the scene shows.
pub(super) fn composite(
&self,
resolved: Option<Rc<OffscreenTarget>>,
) -> Option<ResolvedComposite> {
let (&SceneColour::Sampled { z, shown, .. }, Some(source)) = (&self.colour, resolved)
else {
return None;
};
let (x, y, width, height) = (
shown.0 as f32,
shown.1 as f32,
shown.2 as f32,
shown.3 as f32,
);
Some(ResolvedComposite {
z_index: z,
source,
content: SourceContent::Transient,
dest: (
self.page.offset[0] + x,
self.page.offset[1] + y,
width,
height,
),
scissor: None,
kind: ResolvedCompositeKind::Blit {
alpha: 1.0,
blend_mode: BlendMode::SrcOver,
rounded_mask: None,
sample_mode: CompositeSampleMode::Nearest,
source_viewport: Some((x, y, width, height)),
},
})
}
}
#[expect(clippy::multiple_inherent_impl, reason = "3D steps in their files")]
impl<C: FrameCommandRecorder> FrameExecutor<'_, '_, C> {
// The target the passes of `scene`, whose first surface lies at `z` in
// `pass`, draw into; `None` when a multisampled scene covers no pixel of
// the page.
pub(super) fn scene_target(
&mut self,
pass: &LayerPass<'_>,
scene: &SceneDraw,
z: usize,
) -> Option<SceneTarget> {
let texture = &pass.page.texture;
let page = ScenePage {
scale: pass.scale,
offset: pass.page.offset,
size: (texture.width, texture.height),
};
let renderer = &mut *self.renderer;
let samples = scene.multisample;
if samples == Multisample::Off {
return Some(SceneTarget {
page,
samples,
depth: renderer.depth_targets.scene(&renderer.device, page.size),
colour: SceneColour::Page(Rc::clone(texture)),
});
}
let (left, top, width, height) = scene_scissor(scene, None, page)?;
// A pixel more than the scene's size holds it at any offset.
let fit = |extent: f32, page: u32| ((extent * pass.scale).ceil() as u32 + 1).min(page);
let size = (
fit(scene.rect.width, page.size.0),
fit(scene.rect.height, page.size.1),
);
let origin = (
left.min(page.size.0 - size.0),
top.min(page.size.1 - size.1),
);
let format = renderer.pipelines.composition_format;
let (view, depth) = renderer
.sampled_targets
.scene(&renderer.device, (size, format, samples));
Some(SceneTarget {
page: ScenePage {
scale: page.scale,
offset: [
page.offset[0] + origin.0 as f32,
page.offset[1] + origin.1 as f32,
],
size,
},
samples,
depth,
colour: SceneColour::Sampled {
view,
z,
shown: (left - origin.0, top - origin.1, width, height),
},
})
}
// The texture the next pass into `target` averages its samples into;
// none for a target that is the page.
pub(super) fn scene_resolved(&mut self, target: &SceneTarget) -> Option<Rc<OffscreenTarget>> {
match target.colour {
SceneColour::Page(_) => None,
SceneColour::Sampled { .. } => Some(self.acquire_transient(
"Scene Samples Averaged",
target.page.size.0,
target.page.size.1,
)),
}
}
}