From 56939df615af8726906718e2888aea1841a51d54 Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Thu, 9 Feb 2023 09:34:53 +0000 Subject: [PATCH] Re-use render context in headless --- examples/headless/src/main.rs | 28 ++++++++++------------------ examples/scenes/src/svg.rs | 1 + src/util.rs | 2 +- 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/examples/headless/src/main.rs b/examples/headless/src/main.rs index b5c258e..c82318e 100644 --- a/examples/headless/src/main.rs +++ b/examples/headless/src/main.rs @@ -10,11 +10,11 @@ use scenes::{SceneParams, SceneSet, SimpleText}; use vello::{ block_on_wgpu, kurbo::{Affine, Vec2}, + util::RenderContext, Scene, SceneBuilder, SceneFragment, }; use wgpu::{ - util::initialize_adapter_from_env_or_default, Backends, BufferDescriptor, BufferUsages, - CommandEncoderDescriptor, DeviceDescriptor, Extent3d, ImageCopyBuffer, Instance, Limits, + BufferDescriptor, BufferUsages, CommandEncoderDescriptor, Extent3d, ImageCopyBuffer, TextureDescriptor, TextureFormat, TextureUsages, }; @@ -77,23 +77,15 @@ fn main() -> Result<()> { } async fn render(mut scenes: SceneSet, index: usize, args: &Args) -> Result<()> { - let instance = Instance::new(wgpu::InstanceDescriptor { - backends: wgpu::Backends::PRIMARY, - dx12_shader_compiler: wgpu::Dx12Compiler::Fxc, - }); - let adapter = initialize_adapter_from_env_or_default(&instance, Backends::PRIMARY, None) + let mut context = RenderContext::new() + .or_else(|_| bail!("Got non-Send/Sync error from creating render context"))?; + let device_id = context + .device(None) .await - .ok_or_else(|| anyhow!("Failed to intiialise adapter"))?; - let (device, queue) = adapter - .request_device( - &DeviceDescriptor { - label: Some("Vello Headless"), - features: wgpu::Features::TIMESTAMP_QUERY | wgpu::Features::CLEAR_TEXTURE, - limits: Limits::default(), - }, - None, - ) - .await?; + .ok_or_else(|| anyhow!("No compatible device found"))?; + let device_handle = &mut context.devices[device_id]; + let device = &device_handle.device; + let queue = &device_handle.queue; let mut renderer = vello::Renderer::new(&device) .or_else(|_| bail!("Got non-Send/Sync error from creating renderer"))?; let mut fragment = SceneFragment::new(); diff --git a/examples/scenes/src/svg.rs b/examples/scenes/src/svg.rs index 56d7f1c..9f4f5c0 100644 --- a/examples/scenes/src/svg.rs +++ b/examples/scenes/src/svg.rs @@ -90,6 +90,7 @@ fn example_scene_of(file: PathBuf) -> ExampleScene { let mut builder = SceneBuilder::for_fragment(&mut new_scene); vello_svg::render_tree(&mut builder, &svg); let resolution = Vec2::new(svg.size.width(), svg.size.height()); + // TODO: Handle svg.view_box (new_scene, resolution) }); builder.append(&scene_frag, None); diff --git a/src/util.rs b/src/util.rs index 4d5b09c..ac1070e 100644 --- a/src/util.rs +++ b/src/util.rs @@ -84,7 +84,7 @@ impl RenderContext { } /// Finds or creates a compatible device handle id. - async fn device(&mut self, compatible_surface: Option<&Surface>) -> Option { + pub async fn device(&mut self, compatible_surface: Option<&Surface>) -> Option { let compatible = match compatible_surface { Some(s) => self .devices