mirror of
https://github.com/italicsjenga/vello.git
synced 2025-01-22 09:26:33 +11:00
Merge pull request #227 from rosefromthedead/multiadapter
Multi-adapter support
This commit is contained in:
commit
c57db1d25b
2 changed files with 81 additions and 28 deletions
|
@ -23,10 +23,13 @@ use winit::{event_loop::EventLoop, window::Window};
|
||||||
|
|
||||||
async fn run(event_loop: EventLoop<()>, window: Window) {
|
async fn run(event_loop: EventLoop<()>, window: Window) {
|
||||||
use winit::{event::*, event_loop::ControlFlow};
|
use winit::{event::*, event_loop::ControlFlow};
|
||||||
let render_cx = RenderContext::new().await.unwrap();
|
let mut render_cx = RenderContext::new().unwrap();
|
||||||
let size = window.inner_size();
|
let size = window.inner_size();
|
||||||
let mut surface = render_cx.create_surface(&window, size.width, size.height);
|
let mut surface = render_cx
|
||||||
let mut renderer = Renderer::new(&render_cx.device).unwrap();
|
.create_surface(&window, size.width, size.height)
|
||||||
|
.await;
|
||||||
|
let device_handle = &render_cx.devices[surface.dev_id];
|
||||||
|
let mut renderer = Renderer::new(&device_handle.device).unwrap();
|
||||||
let mut simple_text = simple_text::SimpleText::new();
|
let mut simple_text = simple_text::SimpleText::new();
|
||||||
let mut current_frame = 0usize;
|
let mut current_frame = 0usize;
|
||||||
let mut scene_ix = 0usize;
|
let mut scene_ix = 0usize;
|
||||||
|
@ -59,6 +62,7 @@ async fn run(event_loop: EventLoop<()>, window: Window) {
|
||||||
current_frame += 1;
|
current_frame += 1;
|
||||||
let width = surface.config.width;
|
let width = surface.config.width;
|
||||||
let height = surface.config.height;
|
let height = surface.config.height;
|
||||||
|
let device_handle = &render_cx.devices[surface.dev_id];
|
||||||
let mut builder = SceneBuilder::for_scene(&mut scene);
|
let mut builder = SceneBuilder::for_scene(&mut scene);
|
||||||
const N_SCENES: usize = 6;
|
const N_SCENES: usize = 6;
|
||||||
match scene_ix % N_SCENES {
|
match scene_ix % N_SCENES {
|
||||||
|
@ -76,8 +80,8 @@ async fn run(event_loop: EventLoop<()>, window: Window) {
|
||||||
.expect("failed to get surface texture");
|
.expect("failed to get surface texture");
|
||||||
renderer
|
renderer
|
||||||
.render_to_surface(
|
.render_to_surface(
|
||||||
&render_cx.device,
|
&device_handle.device,
|
||||||
&render_cx.queue,
|
&device_handle.queue,
|
||||||
&scene,
|
&scene,
|
||||||
&surface_texture,
|
&surface_texture,
|
||||||
width,
|
width,
|
||||||
|
@ -85,7 +89,7 @@ async fn run(event_loop: EventLoop<()>, window: Window) {
|
||||||
)
|
)
|
||||||
.expect("failed to render to surface");
|
.expect("failed to render to surface");
|
||||||
surface_texture.present();
|
surface_texture.present();
|
||||||
render_cx.device.poll(wgpu::Maintain::Wait);
|
device_handle.device.poll(wgpu::Maintain::Wait);
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
});
|
});
|
||||||
|
|
93
src/util.rs
93
src/util.rs
|
@ -19,41 +19,33 @@
|
||||||
use super::Result;
|
use super::Result;
|
||||||
|
|
||||||
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
|
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
|
||||||
use wgpu::{Device, Instance, Limits, Queue, Surface, SurfaceConfiguration};
|
use wgpu::{
|
||||||
|
Adapter, Device, Instance, Limits, Queue, RequestAdapterOptions, Surface, SurfaceConfiguration,
|
||||||
|
};
|
||||||
|
|
||||||
/// Simple render context that maintains wgpu state for rendering the pipeline.
|
/// Simple render context that maintains wgpu state for rendering the pipeline.
|
||||||
pub struct RenderContext {
|
pub struct RenderContext {
|
||||||
pub instance: Instance,
|
pub instance: Instance,
|
||||||
|
pub devices: Vec<DeviceHandle>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct DeviceHandle {
|
||||||
|
adapter: Adapter,
|
||||||
pub device: Device,
|
pub device: Device,
|
||||||
pub queue: Queue,
|
pub queue: Queue,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RenderContext {
|
impl RenderContext {
|
||||||
pub async fn new() -> Result<Self> {
|
pub fn new() -> Result<Self> {
|
||||||
let instance = Instance::new(wgpu::Backends::PRIMARY);
|
let instance = Instance::new(wgpu::Backends::PRIMARY);
|
||||||
let adapter = instance.request_adapter(&Default::default()).await.unwrap();
|
|
||||||
let features = adapter.features();
|
|
||||||
let mut limits = Limits::default();
|
|
||||||
let (device, queue) = adapter
|
|
||||||
.request_device(
|
|
||||||
&wgpu::DeviceDescriptor {
|
|
||||||
label: None,
|
|
||||||
features: features
|
|
||||||
& (wgpu::Features::TIMESTAMP_QUERY | wgpu::Features::CLEAR_TEXTURE),
|
|
||||||
limits,
|
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
instance,
|
instance,
|
||||||
device,
|
devices: Vec::new(),
|
||||||
queue,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new surface for the specified window and dimensions.
|
/// Creates a new surface for the specified window and dimensions.
|
||||||
pub fn create_surface<W>(&self, window: &W, width: u32, height: u32) -> RenderSurface
|
pub async fn create_surface<W>(&mut self, window: &W, width: u32, height: u32) -> RenderSurface
|
||||||
where
|
where
|
||||||
W: HasRawWindowHandle + HasRawDisplayHandle,
|
W: HasRawWindowHandle + HasRawDisplayHandle,
|
||||||
{
|
{
|
||||||
|
@ -67,15 +59,71 @@ impl RenderContext {
|
||||||
present_mode: wgpu::PresentMode::Fifo,
|
present_mode: wgpu::PresentMode::Fifo,
|
||||||
alpha_mode: wgpu::CompositeAlphaMode::Auto,
|
alpha_mode: wgpu::CompositeAlphaMode::Auto,
|
||||||
};
|
};
|
||||||
surface.configure(&self.device, &config);
|
let dev_id = self.device(Some(&surface)).await.unwrap();
|
||||||
RenderSurface { surface, config }
|
surface.configure(&self.devices[dev_id].device, &config);
|
||||||
|
RenderSurface {
|
||||||
|
surface,
|
||||||
|
config,
|
||||||
|
dev_id,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resizes the surface to the new dimensions.
|
/// Resizes the surface to the new dimensions.
|
||||||
pub fn resize_surface(&self, surface: &mut RenderSurface, width: u32, height: u32) {
|
pub fn resize_surface(&self, surface: &mut RenderSurface, width: u32, height: u32) {
|
||||||
surface.config.width = width;
|
surface.config.width = width;
|
||||||
surface.config.height = height;
|
surface.config.height = height;
|
||||||
surface.surface.configure(&self.device, &surface.config);
|
surface
|
||||||
|
.surface
|
||||||
|
.configure(&self.devices[surface.dev_id].device, &surface.config);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Finds or creates a compatible device handle id.
|
||||||
|
async fn device(&mut self, compatible_surface: Option<&Surface>) -> Option<usize> {
|
||||||
|
let compatible = match compatible_surface {
|
||||||
|
Some(s) => self
|
||||||
|
.devices
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.find(|(_, d)| d.adapter.is_surface_supported(s))
|
||||||
|
.map(|(i, _)| i),
|
||||||
|
None => (!self.devices.is_empty()).then_some(0),
|
||||||
|
};
|
||||||
|
if compatible.is_none() {
|
||||||
|
return self.new_device(compatible_surface).await;
|
||||||
|
}
|
||||||
|
return compatible;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a compatible device handle id.
|
||||||
|
async fn new_device(&mut self, compatible_surface: Option<&Surface>) -> Option<usize> {
|
||||||
|
let adapter = self
|
||||||
|
.instance
|
||||||
|
.request_adapter(&RequestAdapterOptions {
|
||||||
|
compatible_surface,
|
||||||
|
..Default::default()
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
let features = adapter.features();
|
||||||
|
let limits = Limits::default();
|
||||||
|
let (device, queue) = adapter
|
||||||
|
.request_device(
|
||||||
|
&wgpu::DeviceDescriptor {
|
||||||
|
label: None,
|
||||||
|
features: features
|
||||||
|
& (wgpu::Features::TIMESTAMP_QUERY | wgpu::Features::CLEAR_TEXTURE),
|
||||||
|
limits,
|
||||||
|
},
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.ok()?;
|
||||||
|
let device_handle = DeviceHandle {
|
||||||
|
adapter,
|
||||||
|
device,
|
||||||
|
queue,
|
||||||
|
};
|
||||||
|
self.devices.push(device_handle);
|
||||||
|
Some(self.devices.len() - 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,4 +131,5 @@ impl RenderContext {
|
||||||
pub struct RenderSurface {
|
pub struct RenderSurface {
|
||||||
pub surface: Surface,
|
pub surface: Surface,
|
||||||
pub config: SurfaceConfiguration,
|
pub config: SurfaceConfiguration,
|
||||||
|
pub dev_id: usize,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue