From afe00aa90f9ae6b8ee8acf07bf4ff0969c364108 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Fri, 1 Feb 2019 09:59:51 -0500 Subject: [PATCH] Borrow temporary queus and frames on Rust side --- examples/hello_triangle_rust/main.rs | 9 +++--- wgpu-native/src/swap_chain.rs | 10 +++--- wgpu-rs/src/lib.rs | 46 ++++++++++++++++------------ 3 files changed, 35 insertions(+), 30 deletions(-) diff --git a/examples/hello_triangle_rust/main.rs b/examples/hello_triangle_rust/main.rs index ba6ed735c2..b1c800cc4a 100644 --- a/examples/hello_triangle_rust/main.rs +++ b/examples/hello_triangle_rust/main.rs @@ -9,7 +9,7 @@ fn main() { let adapter = instance.get_adapter(&wgpu::AdapterDescriptor { power_preference: wgpu::PowerPreference::LowPower, }); - let device = adapter.create_device(&wgpu::DeviceDescriptor { + let mut device = adapter.create_device(&wgpu::DeviceDescriptor { extensions: wgpu::Extensions { anisotropic_filtering: false, }, @@ -68,7 +68,7 @@ fn main() { .to_physical(window.get_hidpi_factor()); let surface = instance.create_surface(&window); - let swap_chain = device.create_swap_chain(&surface, &wgpu::SwapChainDescriptor { + let mut swap_chain = device.create_swap_chain(&surface, &wgpu::SwapChainDescriptor { usage: wgpu::TextureUsageFlags::OUTPUT_ATTACHMENT, format: wgpu::TextureFormat::B8g8r8a8Unorm, width: size.width as u32, @@ -95,12 +95,12 @@ fn main() { _ => {} } - let (_, view) = swap_chain.get_next_texture(); + let frame = swap_chain.get_next_texture(); let mut cmd_buf = device.create_command_buffer(&wgpu::CommandBufferDescriptor { todo: 0 }); { let mut rpass = cmd_buf.begin_render_pass(&wgpu::RenderPassDescriptor { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { - attachment: &view, + attachment: &frame.view, load_op: wgpu::LoadOp::Clear, store_op: wgpu::StoreOp::Store, clear_color: wgpu::Color::GREEN, @@ -116,7 +116,6 @@ fn main() { .get_queue() .submit(&[cmd_buf]); - swap_chain.present(); ControlFlow::Continue }); } diff --git a/wgpu-native/src/swap_chain.rs b/wgpu-native/src/swap_chain.rs index e9cabeb948..1ad3e3c7a8 100644 --- a/wgpu-native/src/swap_chain.rs +++ b/wgpu-native/src/swap_chain.rs @@ -154,13 +154,11 @@ pub extern "C" fn wgpu_swap_chain_present( }; device.raw.reset_fence(&frame.fence).unwrap(); - device.queue_group.queues[0] - .submit(submission, Some(&frame.fence)); - - swap_chain.raw + let queue = &mut device.queue_group.queues[0]; + queue.submit(submission, Some(&frame.fence)); + queue .present( - &mut device.queue_group.queues[0], - image_index, + iter::once((&swap_chain.raw, image_index)), iter::once(&frame.sem_present), ) .unwrap(); diff --git a/wgpu-rs/src/lib.rs b/wgpu-rs/src/lib.rs index 09defc2ebf..b2dc5b4e4b 100644 --- a/wgpu-rs/src/lib.rs +++ b/wgpu-rs/src/lib.rs @@ -4,6 +4,7 @@ extern crate wgpu_native as wgn; use arrayvec::ArrayVec; use std::ffi::CString; +use std::marker::PhantomData; use std::ops::Range; use std::ptr; @@ -112,8 +113,9 @@ pub struct ComputePass<'a> { parent: &'a mut CommandBuffer, } -pub struct Queue { +pub struct Queue<'a> { id: wgn::QueueId, + _marker: PhantomData<&'a Self>, } pub struct BindGroupLayoutDescriptor<'a> { @@ -155,6 +157,12 @@ pub struct RenderPassDescriptor<'a> { Option>, } +pub struct SwapChainOutput<'a> { + pub texture: Texture, + pub view: TextureView, + swap_chain_id: &'a wgn::SwapChainId, +} + impl Instance { pub fn new() -> Self { Instance { @@ -197,10 +205,10 @@ impl Device { } } - //TODO: borrow instead of new object? - pub fn get_queue(&self) -> Queue { + pub fn get_queue(&mut self) -> Queue { Queue { id: wgn::wgpu_device_get_queue(self.id), + _marker: PhantomData, } } @@ -476,8 +484,8 @@ impl<'a> ComputePass<'a> { } } -impl Queue { - pub fn submit(&self, command_buffers: &[CommandBuffer]) { +impl<'a> Queue<'a> { + pub fn submit(&mut self, command_buffers: &[CommandBuffer]) { wgn::wgpu_queue_submit( self.id, command_buffers.as_ptr() as *const _, @@ -486,19 +494,19 @@ impl Queue { } } -impl SwapChain { - //TODO: borrow instead of new object? - pub fn get_next_texture(&self) -> (Texture, TextureView) { - let output = wgn::wgpu_swap_chain_get_next_texture(self.id); - ( - Texture { - id: output.texture_id, - }, - TextureView { id: output.view_id }, - ) - } - - pub fn present(&self) { - wgn::wgpu_swap_chain_present(self.id); +impl<'a> Drop for SwapChainOutput<'a> { + fn drop(&mut self) { + wgn::wgpu_swap_chain_present(*self.swap_chain_id); + } +} + +impl SwapChain { + pub fn get_next_texture(&mut self) -> SwapChainOutput { + let output = wgn::wgpu_swap_chain_get_next_texture(self.id); + SwapChainOutput { + texture: Texture { id: output.texture_id }, + view: TextureView { id: output.view_id }, + swap_chain_id: &self.id, + } } }