Update winit example to use new multi-adapter API

This commit is contained in:
Rose Hudson 2022-12-13 23:38:50 +00:00
parent f0faadc356
commit c5401e777e
2 changed files with 15 additions and 11 deletions

View file

@ -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);
} }
_ => {} _ => {}
}); });

View file

@ -26,13 +26,13 @@ use wgpu::{
/// 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,
devices: Vec<DeviceHandle>, pub devices: Vec<DeviceHandle>,
} }
struct DeviceHandle { pub struct DeviceHandle {
adapter: Adapter, adapter: Adapter,
device: Device, pub device: Device,
queue: Queue, pub queue: Queue,
} }
impl RenderContext { impl RenderContext {
@ -131,5 +131,5 @@ impl RenderContext {
pub struct RenderSurface { pub struct RenderSurface {
pub surface: Surface, pub surface: Surface,
pub config: SurfaceConfiguration, pub config: SurfaceConfiguration,
dev_id: usize, pub dev_id: usize,
} }