diff --git a/wgpu-native/src/command/render.rs b/wgpu-native/src/command/render.rs index 62057071b..8b3b772d3 100644 --- a/wgpu-native/src/command/render.rs +++ b/wgpu-native/src/command/render.rs @@ -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(); } diff --git a/wgpu-native/src/command/transfer.rs b/wgpu-native/src/command/transfer.rs index d86e39b83..309ffd518 100644 --- a/wgpu-native/src/command/transfer.rs +++ b/wgpu-native/src/command/transfer.rs @@ -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 { diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index aed76415e..fff23b1c2 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -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); diff --git a/wgpu-native/src/track.rs b/wgpu-native/src/track.rs index 6b9cdace5..cddbd2587 100644 --- a/wgpu-native/src/track.rs +++ b/wgpu-native/src/track.rs @@ -190,7 +190,7 @@ impl + PartialE } impl + PartialEq> Tracker { - pub(crate) fn get_with_usage<'a, T: 'a + Borrow, V: Items>( + fn get_with_usage<'a, T: 'a + Borrow, V: Items>( &mut self, items: &'a V, id: Id, @@ -201,4 +201,31 @@ impl + PartialEq> Tracker { self.transit(id, item.borrow(), usage, permit) .map(|tracktion| (item, tracktion)) } + + pub(crate) fn get_with_extended_usage<'a, T: 'a + Borrow, V: Items>( + &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, V: Items>( + &mut self, + items: &'a V, + id: Id, + usage: U, + ) -> Result<(&'a T, Option), 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), + })) + } }