Refactor get_with_usage for better ergonomics

This commit is contained in:
Dzmitry Malyshau 2019-02-06 09:15:57 -05:00
parent 2f0601c92c
commit dc7bf3404e
4 changed files with 66 additions and 67 deletions

View File

@ -1,7 +1,7 @@
use crate::command::bind::Binder;
use crate::resource::BufferUsageFlags;
use crate::registry::{Items, HUB};
use crate::track::{BufferTracker, TextureTracker, TrackPermit};
use crate::track::{BufferTracker, TextureTracker};
use crate::{
CommandBuffer, Stored,
BindGroupId, BufferId, CommandBufferId, RenderPassId, RenderPipelineId,
@ -65,12 +65,11 @@ 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, _) = pass.buffer_tracker
.get_with_usage(
let buffer = pass.buffer_tracker
.get_with_extended_usage(
&*buffer_guard,
buffer_id,
BufferUsageFlags::INDEX,
TrackPermit::EXTEND,
)
.unwrap();
@ -104,11 +103,10 @@ pub extern "C" fn wgpu_render_pass_set_vertex_buffers(
let pass = pass_guard.get_mut(pass_id);
for &id in buffers {
pass.buffer_tracker
.get_with_usage(
.get_with_extended_usage(
&*buffer_guard,
id,
BufferUsageFlags::VERTEX,
TrackPermit::EXTEND,
)
.unwrap();
}

View File

@ -1,7 +1,6 @@
use crate::device::{all_buffer_stages, all_image_stages};
use crate::registry::{Items, HUB};
use crate::swap_chain::SwapChainLink;
use crate::track::{TrackPermit, Tracktion};
use crate::conv;
use crate::{
BufferId, CommandBufferId, TextureId,
@ -46,45 +45,33 @@ pub extern "C" fn wgpu_command_buffer_copy_buffer_to_buffer(
let cmb = cmb_guard.get_mut(command_buffer_id);
let buffer_guard = HUB.buffers.read();
let (src_buffer, src_tracktion) = cmb.buffer_tracker
.get_with_usage(
let (src_buffer, src_usage) = cmb.buffer_tracker
.get_with_replaced_usage(
&*buffer_guard,
src,
BufferUsageFlags::TRANSFER_SRC,
TrackPermit::REPLACE,
)
.unwrap();
let src_barrier = match src_tracktion {
Tracktion::Init |
Tracktion::Keep => None,
Tracktion::Extend { .. } => unreachable!(),
Tracktion::Replace { old } => Some(hal::memory::Barrier::Buffer {
states: conv::map_buffer_state(old) .. hal::buffer::Access::TRANSFER_READ,
target: &src_buffer.raw,
families: None,
range: None .. None,
}),
};
let src_barrier = src_usage.map(|old| hal::memory::Barrier::Buffer {
states: conv::map_buffer_state(old) .. hal::buffer::Access::TRANSFER_READ,
target: &src_buffer.raw,
families: None,
range: None .. None,
});
let (dst_buffer, dst_tracktion) = cmb.buffer_tracker
.get_with_usage(
let (dst_buffer, dst_usage) = cmb.buffer_tracker
.get_with_replaced_usage(
&*buffer_guard,
dst,
BufferUsageFlags::TRANSFER_DST,
TrackPermit::REPLACE,
)
.unwrap();
let dst_barrier = match dst_tracktion {
Tracktion::Init |
Tracktion::Keep => None,
Tracktion::Extend { .. } => unreachable!(),
Tracktion::Replace { old } => Some(hal::memory::Barrier::Buffer {
states: conv::map_buffer_state(old) .. hal::buffer::Access::TRANSFER_WRITE,
target: &dst_buffer.raw,
families: None,
range: None .. None,
}),
};
let dst_barrier = dst_usage.map(|old| hal::memory::Barrier::Buffer {
states: conv::map_buffer_state(old) .. hal::buffer::Access::TRANSFER_WRITE,
target: &dst_buffer.raw,
families: None,
range: None .. None,
});
let region = hal::command::BufferCopy {
src: src_offset as hal::buffer::Offset,
@ -118,47 +105,35 @@ pub extern "C" fn wgpu_command_buffer_copy_buffer_to_texture(
let buffer_guard = HUB.buffers.read();
let texture_guard = HUB.textures.read();
let (src_buffer, src_tracktion) = cmb.buffer_tracker
.get_with_usage(
let (src_buffer, src_state) = cmb.buffer_tracker
.get_with_replaced_usage(
&*buffer_guard,
source.buffer,
BufferUsageFlags::TRANSFER_SRC,
TrackPermit::REPLACE,
)
.unwrap();
let src_barrier = match src_tracktion {
Tracktion::Init |
Tracktion::Keep => None,
Tracktion::Extend { .. } => unreachable!(),
Tracktion::Replace { old } => Some(hal::memory::Barrier::Buffer {
states: conv::map_buffer_state(old) .. hal::buffer::Access::TRANSFER_READ,
target: &src_buffer.raw,
families: None,
range: None .. None,
}),
};
let src_barrier = src_state.map(|old| hal::memory::Barrier::Buffer {
states: conv::map_buffer_state(old) .. hal::buffer::Access::TRANSFER_READ,
target: &src_buffer.raw,
families: None,
range: None .. None,
});
let (dst_texture, dst_tracktion) = cmb.texture_tracker
.get_with_usage(
let (dst_texture, dst_usage) = cmb.texture_tracker
.get_with_replaced_usage(
&*texture_guard,
destination.texture,
TextureUsageFlags::TRANSFER_DST,
TrackPermit::REPLACE,
)
.unwrap();
let aspects = dst_texture.full_range.aspects;
let dst_texture_state = conv::map_texture_state(TextureUsageFlags::TRANSFER_DST, aspects);
let dst_barrier = match dst_tracktion {
Tracktion::Init |
Tracktion::Keep => None,
Tracktion::Extend { .. } => unreachable!(),
Tracktion::Replace { old } => Some(hal::memory::Barrier::Image {
states: conv::map_texture_state(old, aspects) .. dst_texture_state,
target: &dst_texture.raw,
families: None,
range: dst_texture.full_range.clone(),
}),
};
let dst_barrier = dst_usage.map(|old| hal::memory::Barrier::Image {
states: conv::map_texture_state(old, aspects) .. dst_texture_state,
target: &dst_texture.raw,
families: None,
range: dst_texture.full_range.clone(),
});
if let Some(ref link) = dst_texture.swap_chain_link {
cmb.swap_chain_links.push(SwapChainLink {

View File

@ -668,12 +668,11 @@ pub extern "C" fn wgpu_device_create_bind_group(
for b in bindings {
let descriptor = match b.resource {
binding_model::BindingResource::Buffer(ref bb) => {
let (buffer, _) = used_buffers
.get_with_usage(
let buffer = used_buffers
.get_with_extended_usage(
&*buffer_guard,
bb.buffer,
resource::BufferUsageFlags::UNIFORM,
TrackPermit::EXTEND,
)
.unwrap();
let range = Some(bb.offset as u64) .. Some((bb.offset + bb.size) as u64);

View File

@ -190,7 +190,7 @@ impl<I: Clone + Hash + Eq, U: Copy + GenericUsage + BitOr<Output = U> + PartialE
}
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>>(
fn get_with_usage<'a, T: 'a + Borrow<RefCount>, V: Items<T>>(
&mut self,
items: &'a V,
id: Id,
@ -201,4 +201,31 @@ impl<U: Copy + GenericUsage + BitOr<Output = U> + PartialEq> Tracker<Id, U> {
self.transit(id, item.borrow(), usage, permit)
.map(|tracktion| (item, tracktion))
}
pub(crate) fn get_with_extended_usage<'a, T: 'a + Borrow<RefCount>, V: Items<T>>(
&mut self,
items: &'a V,
id: Id,
usage: U,
) -> Result<&'a T, U> {
let item = items.get(id);
self.transit(id, item.borrow(), usage, TrackPermit::EXTEND)
.map(|_tracktion| item)
}
pub(crate) fn get_with_replaced_usage<'a, T: 'a + Borrow<RefCount>, V: Items<T>>(
&mut self,
items: &'a V,
id: Id,
usage: U,
) -> Result<(&'a T, Option<U>), U> {
let item = items.get(id);
self.transit(id, item.borrow(), usage, TrackPermit::REPLACE)
.map(|tracktion| (item, match tracktion {
Tracktion::Init |
Tracktion::Keep => None,
Tracktion::Extend { ..} => unreachable!(),
Tracktion::Replace { old } => Some(old),
}))
}
}