mirror of
https://github.com/italicsjenga/vello.git
synced 2025-01-10 12:41:30 +11:00
Re-use render context in headless
This commit is contained in:
parent
19552ad819
commit
56939df615
|
@ -10,11 +10,11 @@ use scenes::{SceneParams, SceneSet, SimpleText};
|
||||||
use vello::{
|
use vello::{
|
||||||
block_on_wgpu,
|
block_on_wgpu,
|
||||||
kurbo::{Affine, Vec2},
|
kurbo::{Affine, Vec2},
|
||||||
|
util::RenderContext,
|
||||||
Scene, SceneBuilder, SceneFragment,
|
Scene, SceneBuilder, SceneFragment,
|
||||||
};
|
};
|
||||||
use wgpu::{
|
use wgpu::{
|
||||||
util::initialize_adapter_from_env_or_default, Backends, BufferDescriptor, BufferUsages,
|
BufferDescriptor, BufferUsages, CommandEncoderDescriptor, Extent3d, ImageCopyBuffer,
|
||||||
CommandEncoderDescriptor, DeviceDescriptor, Extent3d, ImageCopyBuffer, Instance, Limits,
|
|
||||||
TextureDescriptor, TextureFormat, TextureUsages,
|
TextureDescriptor, TextureFormat, TextureUsages,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -77,23 +77,15 @@ fn main() -> Result<()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn render(mut scenes: SceneSet, index: usize, args: &Args) -> Result<()> {
|
async fn render(mut scenes: SceneSet, index: usize, args: &Args) -> Result<()> {
|
||||||
let instance = Instance::new(wgpu::InstanceDescriptor {
|
let mut context = RenderContext::new()
|
||||||
backends: wgpu::Backends::PRIMARY,
|
.or_else(|_| bail!("Got non-Send/Sync error from creating render context"))?;
|
||||||
dx12_shader_compiler: wgpu::Dx12Compiler::Fxc,
|
let device_id = context
|
||||||
});
|
.device(None)
|
||||||
let adapter = initialize_adapter_from_env_or_default(&instance, Backends::PRIMARY, None)
|
|
||||||
.await
|
.await
|
||||||
.ok_or_else(|| anyhow!("Failed to intiialise adapter"))?;
|
.ok_or_else(|| anyhow!("No compatible device found"))?;
|
||||||
let (device, queue) = adapter
|
let device_handle = &mut context.devices[device_id];
|
||||||
.request_device(
|
let device = &device_handle.device;
|
||||||
&DeviceDescriptor {
|
let queue = &device_handle.queue;
|
||||||
label: Some("Vello Headless"),
|
|
||||||
features: wgpu::Features::TIMESTAMP_QUERY | wgpu::Features::CLEAR_TEXTURE,
|
|
||||||
limits: Limits::default(),
|
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
let mut renderer = vello::Renderer::new(&device)
|
let mut renderer = vello::Renderer::new(&device)
|
||||||
.or_else(|_| bail!("Got non-Send/Sync error from creating renderer"))?;
|
.or_else(|_| bail!("Got non-Send/Sync error from creating renderer"))?;
|
||||||
let mut fragment = SceneFragment::new();
|
let mut fragment = SceneFragment::new();
|
||||||
|
|
|
@ -90,6 +90,7 @@ fn example_scene_of(file: PathBuf) -> ExampleScene {
|
||||||
let mut builder = SceneBuilder::for_fragment(&mut new_scene);
|
let mut builder = SceneBuilder::for_fragment(&mut new_scene);
|
||||||
vello_svg::render_tree(&mut builder, &svg);
|
vello_svg::render_tree(&mut builder, &svg);
|
||||||
let resolution = Vec2::new(svg.size.width(), svg.size.height());
|
let resolution = Vec2::new(svg.size.width(), svg.size.height());
|
||||||
|
// TODO: Handle svg.view_box
|
||||||
(new_scene, resolution)
|
(new_scene, resolution)
|
||||||
});
|
});
|
||||||
builder.append(&scene_frag, None);
|
builder.append(&scene_frag, None);
|
||||||
|
|
|
@ -84,7 +84,7 @@ impl RenderContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Finds or creates a compatible device handle id.
|
/// Finds or creates a compatible device handle id.
|
||||||
async fn device(&mut self, compatible_surface: Option<&Surface>) -> Option<usize> {
|
pub async fn device(&mut self, compatible_surface: Option<&Surface>) -> Option<usize> {
|
||||||
let compatible = match compatible_surface {
|
let compatible = match compatible_surface {
|
||||||
Some(s) => self
|
Some(s) => self
|
||||||
.devices
|
.devices
|
||||||
|
|
Loading…
Reference in a new issue