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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use cranpose_app_shell::{AppShell, FrameSchedule, RootId, default_root_key};
use cranpose_core::NodeId;
use cranpose_render_common::{RenderScene, Renderer, space::MeshVisit};
use cranpose_render_wgpu::{GpuTarget, WgpuRenderer, WgpuTextSystem};
use crate::{AppSettings, GpuContext};
/// Failure to size or draw a [`GuestView`].
#[derive(Debug, thiserror::Error)]
pub enum GuestViewError {
/// Dimensions must be nonzero and fit the device's texture limit, and the
/// density must be finite and positive.
#[error("invalid guest view size {width} x {height} at density {density}")]
InvalidSize {
/// Requested pixel width.
width: u32,
/// Requested pixel height.
height: u32,
/// Requested pixels per logical point.
density: f32,
},
/// Cranpose could not render the scene.
#[error("render: {0}")]
Render(String),
}
/// A Cranpose composition drawn into textures on another program's GPU
/// device, such as a game engine's HUD texture.
///
/// It starts no window or event loop. The host routes input through
/// [`Self::shell`] in logical coordinates, calls [`Self::draw`] once per host
/// frame and shows the texture it drew into. The frame is submitted to the
/// context's queue, so work the host submits there afterwards sees it.
pub struct GuestView {
shell: AppShell<WgpuRenderer>,
gpu: GpuContext,
format: wgpu::TextureFormat,
text_system: WgpuTextSystem,
max_dimension: u32,
size: (u32, u32),
density: f32,
dirty: bool,
visible: bool,
}
impl GuestView {
/// Creates a composition that draws `width` by `height` pixel textures of
/// `format` at `density` pixels per logical point.
pub fn new(
settings: &AppSettings,
gpu: &GpuContext,
format: wgpu::TextureFormat,
width: u32,
height: u32,
density: f32,
content: impl FnMut() + 'static,
) -> Result<Self, GuestViewError> {
let max_dimension = gpu.device.limits().max_texture_dimension_2d;
validate_size(width, height, density, max_dimension)?;
let text_system = WgpuTextSystem::from_text_fonts(settings.resolve_fonts());
let mut renderer = WgpuRenderer::with_text_system(text_system.clone());
renderer.init_gpu(GpuTarget::on_adapter(
&gpu.adapter,
std::sync::Arc::clone(&gpu.device),
std::sync::Arc::clone(&gpu.queue),
format,
));
let shell = AppShell::new_with_size_and_density(
renderer,
default_root_key(),
crate::gpu_context::provide_gpu_context(gpu.clone(), content),
(width, height),
(width as f32 / density, height as f32 / density),
density,
);
shell.use_system_clipboard();
Ok(Self {
shell,
gpu: gpu.clone(),
format,
text_system,
max_dimension,
size: (width, height),
density,
dirty: true,
visible: true,
})
}
/// Accesses input, scheduling, semantics, text input and the app context.
pub fn shell(&mut self) -> &mut AppShell<WgpuRenderer> {
&mut self.shell
}
/// Sets the size of the textures [`Self::draw`] draws into. Returns
/// whether the pixel size changed. Invalid values leave the previous size.
pub fn resize(
&mut self,
width: u32,
height: u32,
density: f32,
) -> Result<bool, GuestViewError> {
validate_size(width, height, density, self.max_dimension)?;
let size_changed = self.size != (width, height);
if !size_changed && self.density == density {
return Ok(false);
}
self.shell
.primary()
.set_viewport(width as f32 / density, height as f32 / density);
self.shell.set_density(density);
self.shell.set_buffer_size(width, height);
self.size = (width, height);
self.density = density;
self.dirty = true;
Ok(size_changed)
}
/// Suspends drawing while hidden and cancels any active gesture.
/// Becoming visible draws the next frame whatever changed.
pub fn set_visible(&mut self, visible: bool) {
if self.visible == visible {
return;
}
self.visible = visible;
if visible {
self.shell.notify_app_resumed();
self.dirty = true;
} else {
self.shell.cancel_gesture();
self.shell.notify_app_paused();
}
}
/// What the view asks its host for: the shell's schedule while visible,
/// and nothing while hidden, as a hidden view neither updates nor draws.
pub fn frame_schedule(&self) -> FrameSchedule {
if self.visible {
self.shell.frame_schedule()
} else {
FrameSchedule::default()
}
}
/// Advances the composition and, when it looks different, draws it into
/// `texture` through `view`. The texture must have
/// [`pixel_size`](Self::pixel_size) and the format the view was created
/// with. Returns whether it drew.
pub fn draw(
&mut self,
texture: &wgpu::Texture,
view: &wgpu::TextureView,
) -> Result<bool, GuestViewError> {
if !self.visible {
return Ok(false);
}
// The update runs for the whole composition. What it changed in a window root owes that
// window a frame, and the primary root owes one only for what it changed itself.
self.shell.update();
let owed = self.shell.take_frame_owed();
if !(self.dirty || owed || self.shell.needs_redraw()) {
return Ok(false);
}
let (width, height) = self.size;
self.shell
.renderer()
.render(texture, view, width, height)
.map_err(|error| GuestViewError::Render(format!("{error:?}")))?;
self.dirty = false;
Ok(true)
}
/// Physical dimensions of the textures [`Self::draw`] draws into.
pub fn pixel_size(&self) -> (u32, u32) {
self.size
}
/// Gives the window root `root` a view of its own, drawn by [`Self::draw_window`] into
/// textures of `width` by `height` pixels at `density` pixels per logical point, with a
/// transparent background. `root` is the node of a
/// [`Modifier::window_root`](crate::Modifier::window_root) in this composition, as
/// [`Self::shell`]'s `window_roots` lists it. The root lays out into the size that its
/// [`WindowRootDescriptor`](crate::WindowRootDescriptor) reports, which the caller sets to the
/// view's logical size (`width / density` by `height / density`) before the next frame.
///
/// A window root with a view of its own is not part of the textures [`Self::draw`] draws, and
/// the composition stays one: state, effects and locals are shared with the primary root.
pub fn open_window(
&mut self,
root: NodeId,
(width, height): (u32, u32),
density: f32,
) -> Result<(), GuestViewError> {
validate_size(width, height, density, self.max_dimension)?;
let mut renderer = WgpuRenderer::with_text_system(self.text_system.clone());
renderer.set_root_scale(density);
renderer.set_transparent_background(true);
renderer.init_gpu(GpuTarget::on_adapter(
&self.gpu.adapter,
std::sync::Arc::clone(&self.gpu.device),
std::sync::Arc::clone(&self.gpu.queue),
self.format,
));
self.shell.add_window_surface(
root,
renderer,
(width, height),
(width as f32 / density, height as f32 / density),
);
Ok(())
}
/// Visits each mesh of the `mesh_surface` nodes under the node `under` of the window root
/// `root`, in the frame of `under`, as the root's last draw left them. A node's mesh comes with
/// the map from its own space to `under`'s frame, so a caller picks along a ray or finds what a
/// point touches. The visit allocates nothing. A scene under `under` opens a space of its
/// own, which the visit leaves out. Returns whether the window root has a view.
pub fn visit_window_meshes(
&mut self,
(root, under): (NodeId, NodeId),
visit: &mut dyn FnMut(&MeshVisit<'_>),
) -> bool {
let Some(mut surface) = self.shell.surface(RootId::Window(root)) else {
return false;
};
surface.renderer().scene().visit_meshes(under, visit);
true
}
/// Takes the view of the window root `root` away.
pub fn close_window(&mut self, root: NodeId) {
self.shell.remove_window_surface(root);
}
/// Updates the composition and draws the window root `root` into `texture` through `view`,
/// whatever changed. The texture has the size that [`Self::open_window`] was given and the
/// format of the view. The update runs no frame callbacks: the frame's one tick is that of
/// [`Self::draw`], so animations and effects that wait for a frame advance once for each
/// frame of the host, whatever the number of draws of window roots.
///
/// With `depth`, the root's 3D scenes draw against it and leave their depth in it: a view of
/// a texture of [`SCENE_DEPTH_FORMAT`](cranpose_render_wgpu::SCENE_DEPTH_FORMAT) of the same
/// size, with the render attachment usage. A scene's camera and the states its graphics
/// layers read apply to the next draw, so one composition drawn twice with a state set
/// between the draws shows two views of the same content, such as the two eyes of a headset.
pub fn draw_window(
&mut self,
root: NodeId,
texture: &wgpu::Texture,
view: &wgpu::TextureView,
depth: Option<&wgpu::TextureView>,
) -> Result<(), GuestViewError> {
self.shell.update_without_frame();
let mut surface = self.shell.surface(RootId::Window(root)).ok_or_else(|| {
GuestViewError::Render(format!("no view for the window root {root:?}"))
})?;
let size = (texture.width(), texture.height());
let renderer = surface.renderer();
match depth {
Some(depth) => renderer.render_with_scene_depth(texture, view, depth, size),
None => renderer.render(texture, view, size.0, size.1),
}
.map_err(|error| GuestViewError::Render(format!("{error:?}")))
}
}
fn validate_size(width: u32, height: u32, density: f32, limit: u32) -> Result<(), GuestViewError> {
if width == 0
|| height == 0
|| width > limit
|| height > limit
|| !density.is_finite()
|| density <= 0.0
{
return Err(GuestViewError::InvalidSize {
width,
height,
density,
});
}
Ok(())
}