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
//! The engine's UI pass: `crcbl_frame`'s, with the engine's menu art in its
//! image atlas from the first frame.
//!
//! [`crcbl_frame::ui_pass`] draws a [`DrawList`] with whatever pictures its
//! caller registered, and knows nothing of the engine's menu. [`UiRenderer`] here is that pass plus the shipped menu skin
//! ([`menu_skin`](crate::menu::menu_skin)), registered at start-up so the art is
//! on the page before a menu is ever opened — which is what every game's pause
//! menu, the debug panel and the console draw with. Every other method forwards
//! to the frame core's renderer, so a caller writes `renderer.begin_frame(…)`
//! and `renderer.add_passes(…)` as it always has.
use crcbl_frame::counters::FrameCounters;
use crcbl_frame::graph::{ImageId, RenderGraph};
use crcbl_hal::{Device, Format, HalError, QueueHandle};
use crcbl_ui::draw_list::DrawList;
use crcbl_ui::font::atlas::GlyphAtlas;
use crcbl_ui::image::ImageAtlas;
use crcbl_ui::menu::MenuSkin;
use crcbl_ui::text::FontAtlas;
pub use crcbl_frame::ui_pass::{
CONSTANTS_BINDING, GLYPH_PAGES_BINDING, IMAGE_ATLAS_BINDING, IMAGE_SAMPLER_BINDING,
TEXTURE_SET, UiTexture,
};
/// The UI compositing renderer, with the engine's menu art registered.
#[derive(Debug)]
pub struct UiRenderer {
pass: crcbl_frame::UiRenderer,
/// The shipped menu art, registered in the pass's image atlas at start-up.
menu_skin: MenuSkin,
}
impl UiRenderer {
/// The most passes a frame of this renderer records:
/// [`crcbl_frame::UiRenderer::MAX_PASSES`], since the menu art adds none.
pub const MAX_PASSES: u32 = crcbl_frame::UiRenderer::MAX_PASSES;
/// Builds the UI pass with the menu art on its image page.
///
/// `target_format` is the format of the image the pass composites onto, as
/// [`crcbl_frame::UiRenderer::new`] describes.
///
/// # Errors
///
/// [`HalError`] from any seam call, with everything already created
/// released, and [`HalError::InvalidDescriptor`] if the menu art does not
/// fit an empty image page — a shipped-art defect, not a device one.
pub fn new(
device: &dyn Device,
queue: QueueHandle,
target_format: Format,
) -> Result<Self, HalError> {
let mut images = ImageAtlas::new();
let menu_skin = crate::menu::menu_skin(&mut images).map_err(|error| {
HalError::InvalidDescriptor(format!("ui image atlas: the menu art: {error}"))
})?;
let pass = crcbl_frame::UiRenderer::with_images(device, queue, target_format, images)?;
Ok(Self { pass, menu_skin })
}
/// The shipped menu art, for [`Menu::render`](crcbl_ui::menu::Menu::render).
#[must_use]
pub const fn menu_skin(&self) -> &MenuSkin {
&self.menu_skin
}
/// The format the compositing pass renders into.
#[must_use]
pub const fn target_format(&self) -> Format {
self.pass.target_format()
}
/// The image atlas the pass draws pictures from, the menu art among them.
#[must_use]
pub const fn images(&self) -> &ImageAtlas {
self.pass.images()
}
/// The image atlas, to register pictures into, as
/// [`crcbl_frame::UiRenderer::images_mut`] describes.
pub const fn images_mut(&mut self) -> &mut ImageAtlas {
self.pass.images_mut()
}
/// The glyph atlas the pass rasterises text into.
#[must_use]
pub const fn glyphs(&self) -> &GlyphAtlas {
self.pass.glyphs()
}
/// Uploads the frame's draw list, as
/// [`crcbl_frame::UiRenderer::begin_frame`] does.
///
/// # Errors
///
/// As [`crcbl_frame::UiRenderer::begin_frame`].
pub fn begin_frame(
&mut self,
device: &dyn Device,
draw_list: &DrawList,
atlas: &FontAtlas,
scale: f32,
) -> Result<(), HalError> {
self.pass.begin_frame(device, draw_list, atlas, scale)
}
/// What the last frame recorded, as [`crcbl_frame::UiRenderer::counters`]
/// reports it.
#[must_use]
pub fn counters(&self) -> FrameCounters {
self.pass.counters()
}
/// Adds the frame's passes to `graph`, as
/// [`crcbl_frame::UiRenderer::add_passes`] does.
pub fn add_passes<'a>(
&'a self,
graph: &mut RenderGraph<'a>,
target: ImageId,
extent: (u32, u32),
) {
self.pass.add_passes(graph, target, extent);
}
/// Adds the frame's passes with renderer-drawn textures bound, as
/// [`crcbl_frame::UiRenderer::add_passes_with_textures`] does.
pub fn add_passes_with_textures<'a>(
&'a self,
graph: &mut RenderGraph<'a>,
target: ImageId,
extent: (u32, u32),
textures: &[UiTexture],
) {
self.pass
.add_passes_with_textures(graph, target, extent, textures);
}
/// Destroys the pass's GPU resources, as
/// [`crcbl_frame::UiRenderer::destroy`] does.
pub fn destroy(self, device: &dyn Device) {
self.pass.destroy(device);
}
}
#[cfg(test)]
mod tests {
use crcbl_hal::null::{Event, NullInstance, Recorder};
use crcbl_hal::{DeviceDesc, Format, Instance, QueueKind};
use crcbl_ui::image::ImageAtlas;
use crcbl_ui::text::FontAtlas;
use super::UiRenderer;
/// **The menu art is on the page from construction**: start-up writes the
/// built-in font's bitmap and the menu art's rectangle, and the skin it
/// hands back names images already in the atlas.
#[test]
fn construction_registers_and_uploads_the_menu_art() {
let recorder = Recorder::new();
let instance = NullInstance::gpu_driven().with_recorder(recorder.clone());
let adapter = instance.adapters().remove(0);
let device = instance
.create_device(&DeviceDesc::for_adapter(adapter.id))
.expect("the null backend always opens");
let queue = device.queue(QueueKind::Graphics).expect("always present");
let renderer =
UiRenderer::new(device.as_ref(), queue, Format::Bgra8UnormSrgb).expect("built");
let mut images = ImageAtlas::new();
crate::menu::menu_skin(&mut images).expect("the menu art fits an empty page");
let rect = images
.dirty()
.expect("registering the menu art dirtied the page");
let art = rect.width as usize * rect.height as usize * 4;
let (_, _, font) = FontAtlas::built_in().glyph_bitmap();
let written: usize = recorder
.events()
.into_iter()
.filter_map(|event| match event {
Event::BufferWritten { len, .. } => Some(len),
_ => None,
})
.sum();
assert_eq!(
written,
font.len() + art,
"the font bitmap is {} bytes and the menu art {art}",
font.len()
);
assert_eq!(renderer.images().dirty(), None, "the page owes nothing");
assert!(
renderer
.images()
.images()
.contains(&renderer.menu_skin().scrim),
"the skin names an image the atlas holds"
);
renderer.destroy(device.as_ref());
recorder.assert_valid();
}
}