get_with_usage helper

This commit is contained in:
Dzmitry Malyshau 2019-01-18 11:57:03 -05:00
parent 08cd75f38c
commit bb7fee796d
5 changed files with 41 additions and 13 deletions

View File

@ -103,7 +103,7 @@ impl<B: hal::Backend> CommandAllocator<B> {
pool.available.pop().unwrap()
}
pub fn submit(&self, cmd_buf: CommandBuffer<B>) {
pub fn after_submit(&self, cmd_buf: CommandBuffer<B>) {
self.inner.lock().pending.push(cmd_buf);
}
@ -111,7 +111,7 @@ impl<B: hal::Backend> CommandAllocator<B> {
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);

View File

@ -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,
)

View File

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

View File

@ -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<B: hal::Backend> {
//pub raw: B::UnboundBuffer,
pub raw: B::Buffer,
pub memory_properties: hal::memory::Properties,
pub life_guard: LifeGuard,
// TODO: mapping, unmap()
}
impl<B: hal::Backend> Borrow<RefCount> for Buffer<B> {
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<B: hal::Backend> {
pub life_guard: LifeGuard,
}
impl<B: hal::Backend> Borrow<RefCount> for Texture<B> {
fn borrow(&self) -> &RefCount {
&self.life_guard.ref_count
}
}
bitflags! {
#[repr(transparent)]
pub struct TextureAspectFlags: u32 {

View File

@ -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<I: Clone + Hash + Eq, U: Copy + GenericUsage + BitOr<Output = U> + PartialE
self.map.keys().map(|&WeaklyStored(ref id)| id.clone())
}
}
impl<U: Copy + GenericUsage + BitOr<Output = U> + PartialEq> Tracker<Id, U> {
pub(crate) fn get_with_usage<'a, T: 'a + Borrow<RefCount>, V: Items<T>>(
&mut self,
items: &'a V,
id: Id,
usage: U,
permit: TrackPermit,
) -> Result<(&'a T, Tracktion<U>), U> {
let item = items.get(id);
self.transit(id, item.borrow(), usage, permit)
.map(|tracktion| (item, tracktion))
}
}