diff --git a/wgpu-native/src/command/allocator.rs b/wgpu-native/src/command/allocator.rs index b27b8eb1c..d8f776356 100644 --- a/wgpu-native/src/command/allocator.rs +++ b/wgpu-native/src/command/allocator.rs @@ -103,7 +103,7 @@ impl CommandAllocator { pool.available.pop().unwrap() } - pub fn submit(&self, cmd_buf: CommandBuffer) { + pub fn after_submit(&self, cmd_buf: CommandBuffer) { self.inner.lock().pending.push(cmd_buf); } @@ -111,7 +111,7 @@ impl CommandAllocator { self.inner.lock().recycle(cmd_buf); } - pub fn maintain(&self, device: &B::Device, last_done: SubmissionIndex) { + pub fn maintain(&self, last_done: SubmissionIndex) { let mut inner = self.inner.lock(); for i in (0..inner.pending.len()).rev() { let index = inner.pending[i].life_guard.submission_index.load(Ordering::Acquire); diff --git a/wgpu-native/src/command/render.rs b/wgpu-native/src/command/render.rs index f00dfe4d4..66624c8ad 100644 --- a/wgpu-native/src/command/render.rs +++ b/wgpu-native/src/command/render.rs @@ -60,15 +60,15 @@ pub extern "C" fn wgpu_render_pass_set_index_buffer( let buffer_guard = HUB.buffers.read(); let pass = pass_guard.get_mut(pass_id); - let buffer = buffer_guard.get(buffer_id); - pass.buffer_tracker - .transit( + let (buffer, _) = pass.buffer_tracker + .get_with_usage( + &*buffer_guard, buffer_id, - &buffer.life_guard.ref_count, BufferUsageFlags::INDEX, TrackPermit::EXTEND, ) .unwrap(); + buffer_guard.get(buffer_id); let view = hal::buffer::IndexBufferView { buffer: &buffer.raw, @@ -90,11 +90,10 @@ pub extern "C" fn wgpu_render_pass_set_vertex_buffers( let pass = pass_guard.get_mut(pass_id); for &id in buffers { - let buffer = buffer_guard.get(id); pass.buffer_tracker - .transit( + .get_with_usage( + &*buffer_guard, id, - &buffer.life_guard.ref_count, BufferUsageFlags::VERTEX, TrackPermit::EXTEND, ) diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 6060a0c77..b386640f0 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -641,12 +641,12 @@ pub extern "C" fn wgpu_queue_submit( last_done }; - device.com_allocator.maintain(&device.raw, last_done); + device.com_allocator.maintain(last_done); // finally, return the command buffers to the allocator for &cmb_id in command_buffer_ids { let cmd_buf = command_buffer_guard.take(cmb_id); - device.com_allocator.submit(cmd_buf); + device.com_allocator.after_submit(cmd_buf); } } diff --git a/wgpu-native/src/resource.rs b/wgpu-native/src/resource.rs index 42e4f071f..833983a91 100644 --- a/wgpu-native/src/resource.rs +++ b/wgpu-native/src/resource.rs @@ -1,5 +1,5 @@ use crate::{ - Extent3d, LifeGuard, Stored, + Extent3d, LifeGuard, RefCount, Stored, DeviceId, TextureId, }; use crate::swap_chain::{SwapChainLink, SwapImageEpoch}; @@ -8,6 +8,8 @@ use bitflags::bitflags; use hal; use parking_lot::Mutex; +use std::borrow::Borrow; + bitflags! { #[repr(transparent)] @@ -32,13 +34,18 @@ pub struct BufferDescriptor { } pub(crate) struct Buffer { - //pub raw: B::UnboundBuffer, pub raw: B::Buffer, pub memory_properties: hal::memory::Properties, pub life_guard: LifeGuard, // TODO: mapping, unmap() } +impl Borrow for Buffer { + fn borrow(&self) -> &RefCount { + &self.life_guard.ref_count + } +} + #[repr(C)] #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] pub enum TextureDimension { @@ -89,6 +96,12 @@ pub(crate) struct Texture { pub life_guard: LifeGuard, } +impl Borrow for Texture { + fn borrow(&self) -> &RefCount { + &self.life_guard.ref_count + } +} + bitflags! { #[repr(transparent)] pub struct TextureAspectFlags: u32 { diff --git a/wgpu-native/src/track.rs b/wgpu-native/src/track.rs index 087551641..36d69328d 100644 --- a/wgpu-native/src/track.rs +++ b/wgpu-native/src/track.rs @@ -1,6 +1,8 @@ +use crate::registry::{Id, Items}; use crate::resource::{BufferUsageFlags, TextureUsageFlags}; use crate::{BufferId, RefCount, Stored, TextureId, WeaklyStored}; +use std::borrow::Borrow; use std::collections::hash_map::{Entry, HashMap}; use std::hash::Hash; use std::mem; @@ -154,3 +156,17 @@ impl + PartialE self.map.keys().map(|&WeaklyStored(ref id)| id.clone()) } } + +impl + PartialEq> Tracker { + pub(crate) fn get_with_usage<'a, T: 'a + Borrow, V: Items>( + &mut self, + items: &'a V, + id: Id, + usage: U, + permit: TrackPermit, + ) -> Result<(&'a T, Tracktion), U> { + let item = items.get(id); + self.transit(id, item.borrow(), usage, permit) + .map(|tracktion| (item, tracktion)) + } +}