Skip to content

Commit faadfcc

Browse files
committed
Refactor vello::util
Signed-off-by: Nico Burns <[email protected]>
1 parent 36c6659 commit faadfcc

File tree

5 files changed

+191
-159
lines changed

5 files changed

+191
-159
lines changed

examples/headless/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ fn main() -> Result<()> {
9090
async fn render(mut scenes: SceneSet, index: usize, args: &Args) -> Result<()> {
9191
let mut context = RenderContext::new();
9292
let device_id = context
93-
.device(None)
93+
.find_or_create_device(None)
9494
.await
9595
.ok_or_else(|| anyhow!("No compatible device found"))?;
96-
let device_handle = &mut context.devices[device_id];
96+
let device_handle = &mut context.device_pool[device_id];
9797
let device = &device_handle.device;
9898
let queue = &device_handle.queue;
9999
let mut renderer = vello::Renderer::new(

examples/simple/src/main.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl ApplicationHandler for SimpleVelloApp<'_> {
7070

7171
// Create a vello Renderer for the surface (using its device id)
7272
self.renderers
73-
.resize_with(self.context.devices.len(), || None);
73+
.resize_with(self.context.device_pool.len(), || None);
7474
self.renderers[surface.dev_id]
7575
.get_or_insert_with(|| create_vello_renderer(&self.context, &surface));
7676

@@ -105,8 +105,7 @@ impl ApplicationHandler for SimpleVelloApp<'_> {
105105

106106
// Resize the surface when the window is resized
107107
WindowEvent::Resized(size) => {
108-
self.context
109-
.resize_surface(surface, size.width, size.height);
108+
surface.resize(size.width, size.height);
110109
}
111110

112111
// This is where all the rendering happens
@@ -123,7 +122,7 @@ impl ApplicationHandler for SimpleVelloApp<'_> {
123122
let height = surface.config.height;
124123

125124
// Get a handle to the device
126-
let device_handle = &self.context.devices[surface.dev_id];
125+
let device_handle = &self.context.device_pool[surface.dev_id];
127126

128127
// Render to a texture, which we will later copy into the surface
129128
self.renderers[surface.dev_id]
@@ -204,7 +203,7 @@ fn create_winit_window(event_loop: &ActiveEventLoop) -> Arc<Window> {
204203
/// Helper function that creates a vello `Renderer` for a given `RenderContext` and `RenderSurface`
205204
fn create_vello_renderer(render_cx: &RenderContext, surface: &RenderSurface<'_>) -> Renderer {
206205
Renderer::new(
207-
&render_cx.devices[surface.dev_id].device,
206+
&render_cx.device_pool[surface.dev_id].device,
208207
RendererOptions::default(),
209208
)
210209
.expect("Couldn't create renderer")

examples/with_winit/src/lib.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,10 @@ impl ApplicationHandler<UserEvent> for VelloApp<'_> {
203203
self.state = {
204204
let render_state = RenderState { window, surface };
205205
self.renderers
206-
.resize_with(self.context.devices.len(), || None);
206+
.resize_with(self.context.device_pool.len(), || None);
207207
let id = render_state.surface.dev_id;
208208
self.renderers[id].get_or_insert_with(|| {
209-
let device_handle = &self.context.devices[id];
209+
let device_handle = &self.context.device_pool[id];
210210
let cache = if let Some((dir, tx)) = self.cache_data.as_ref() {
211211
// Safety: Hoping for the best. Given that we're using as private a cache directory as possible, it's
212212
// probably fine?
@@ -349,14 +349,11 @@ impl ApplicationHandler<UserEvent> for VelloApp<'_> {
349349
}
350350
"v" => {
351351
self.vsync_on = !self.vsync_on;
352-
self.context.set_present_mode(
353-
&mut render_state.surface,
354-
if self.vsync_on {
355-
wgpu::PresentMode::AutoVsync
356-
} else {
357-
wgpu::PresentMode::AutoNoVsync
358-
},
359-
);
352+
render_state.surface.set_present_mode(if self.vsync_on {
353+
wgpu::PresentMode::AutoVsync
354+
} else {
355+
wgpu::PresentMode::AutoNoVsync
356+
});
360357
}
361358
debug_layer @ ("1" | "2" | "3" | "4") => {
362359
match debug_layer {
@@ -426,8 +423,7 @@ impl ApplicationHandler<UserEvent> for VelloApp<'_> {
426423
}
427424
WindowEvent::Resized(size) => {
428425
if let Some(RenderState { surface, window }) = &mut self.state {
429-
self.context
430-
.resize_surface(surface, size.width, size.height);
426+
surface.resize(size.width, size.height);
431427
window.request_redraw();
432428
}
433429
}
@@ -481,7 +477,7 @@ impl ApplicationHandler<UserEvent> for VelloApp<'_> {
481477
};
482478
let width = surface.config.width;
483479
let height = surface.config.height;
484-
let device_handle = &self.context.devices[surface.dev_id];
480+
let device_handle = &self.context.device_pool[surface.dev_id];
485481
let snapshot = self.stats.snapshot();
486482

487483
// Allow looping forever
@@ -671,7 +667,7 @@ impl ApplicationHandler<UserEvent> for VelloApp<'_> {
671667
let Some(render_state) = &mut self.state else {
672668
return;
673669
};
674-
let device_handle = &self.context.devices[render_state.surface.dev_id];
670+
let device_handle = &self.context.device_pool[render_state.surface.dev_id];
675671
log::info!("==============\nReloading shaders");
676672
let start = Instant::now();
677673
let result = self.renderers[render_state.surface.dev_id]
@@ -717,9 +713,9 @@ fn run(
717713
#[cfg(target_arch = "wasm32")]
718714
let (render_state, renderers) = {
719715
let mut renderers = vec![];
720-
renderers.resize_with(render_cx.devices.len(), || None);
716+
renderers.resize_with(render_cx.device_pool.len(), || None);
721717
let id = render_state.surface.dev_id;
722-
let device_handle = &render_cx.devices[id];
718+
let device_handle = &render_cx.device_pool[id];
723719
let cache: Option<(PipelineCache, PathBuf)> = if let Some(dir) = cache_directory.as_ref() {
724720
// Safety: Hoping for the best. Given that we're using as private a cache directory as possible, it's
725721
// probably fine?

0 commit comments

Comments
 (0)