Borrow temporary queus and frames on Rust side

This commit is contained in:
Dzmitry Malyshau
2019-02-01 09:59:51 -05:00
parent a2b6ec167f
commit afe00aa90f
3 changed files with 35 additions and 30 deletions

View File

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

View File

@@ -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();

View File

@@ -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<RenderPassDepthStencilAttachmentDescriptor<&'a TextureView>>,
}
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,
}
}
}