mirror of
https://github.com/gfx-rs/wgpu.git
synced 2025-02-14 16:02:47 +00:00
Destroy temporary buffers
This commit is contained in:
parent
35f2e8b70e
commit
786ead9701
@ -402,12 +402,21 @@ impl GlobalExt for wgc::hub::Global<IdentityPassThroughFactory> {
|
||||
A::DestroyRenderPipeline(id) => {
|
||||
self.render_pipeline_destroy::<B>(id);
|
||||
}
|
||||
A::WriteBuffer { id, data, range } => {
|
||||
A::WriteBuffer {
|
||||
id,
|
||||
data,
|
||||
range,
|
||||
queued,
|
||||
} => {
|
||||
let bin = std::fs::read(dir.join(data)).unwrap();
|
||||
let size = (range.end - range.start) as usize;
|
||||
if queued {
|
||||
self.queue_write_buffer::<B>(device, &bin, id, range.start);
|
||||
} else {
|
||||
self.device_wait_for_buffer::<B>(device, id);
|
||||
self.device_set_buffer_sub_data::<B>(device, id, range.start, &bin[..size]);
|
||||
}
|
||||
}
|
||||
A::Submit(_index, commands) => {
|
||||
let encoder = self.device_create_command_encoder::<B>(
|
||||
device,
|
||||
|
@ -212,12 +212,15 @@ impl<B: hal::Backend> LifetimeTracker<B> {
|
||||
index: SubmissionIndex,
|
||||
fence: B::Fence,
|
||||
new_suspects: &SuspectedResources,
|
||||
temp_buffers: impl Iterator<Item = (B::Buffer, MemoryBlock<B>)>,
|
||||
) {
|
||||
let mut last_resources = NonReferencedResources::new();
|
||||
last_resources.buffers.extend(temp_buffers);
|
||||
self.suspected_resources.extend(new_suspects);
|
||||
self.active.alloc().init(ActiveSubmission {
|
||||
index,
|
||||
fence,
|
||||
last_resources: NonReferencedResources::new(),
|
||||
last_resources,
|
||||
mapped: Vec::new(),
|
||||
});
|
||||
}
|
||||
|
@ -201,7 +201,7 @@ pub struct Device<B: hal::Backend> {
|
||||
pub(crate) private_features: PrivateFeatures,
|
||||
limits: wgt::Limits,
|
||||
extensions: wgt::Extensions,
|
||||
pending_write_command_buffer: Option<B::CommandBuffer>,
|
||||
pending_writes: queue::PendingWrites<B>,
|
||||
#[cfg(feature = "trace")]
|
||||
pub(crate) trace: Option<Mutex<Trace>>,
|
||||
}
|
||||
@ -273,15 +273,22 @@ impl<B: GfxBackend> Device<B> {
|
||||
},
|
||||
limits: desc.limits.clone(),
|
||||
extensions: desc.extensions.clone(),
|
||||
pending_write_command_buffer: None,
|
||||
pending_writes: queue::PendingWrites::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn lock_life_internal<'this, 'token: 'this>(
|
||||
tracker: &'this Mutex<life::LifetimeTracker<B>>,
|
||||
_token: &mut Token<'token, Self>,
|
||||
) -> MutexGuard<'this, life::LifetimeTracker<B>> {
|
||||
tracker.lock()
|
||||
}
|
||||
|
||||
fn lock_life<'this, 'token: 'this>(
|
||||
&'this self,
|
||||
_token: &mut Token<'token, Self>,
|
||||
token: &mut Token<'token, Self>,
|
||||
) -> MutexGuard<'this, life::LifetimeTracker<B>> {
|
||||
self.life_tracker.lock()
|
||||
Self::lock_life_internal(&self.life_tracker, token)
|
||||
}
|
||||
|
||||
fn maintain<'this, 'token: 'this, G: GlobalIdentityHandlerFactory>(
|
||||
@ -688,6 +695,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
|
||||
id: buffer_id,
|
||||
data: data_path,
|
||||
range: offset..offset + data.len() as BufferAddress,
|
||||
queued: false,
|
||||
});
|
||||
}
|
||||
None => (),
|
||||
@ -2451,6 +2459,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
|
||||
id: buffer_id,
|
||||
data,
|
||||
range: sub_range.offset..sub_range.offset + size,
|
||||
queued: false,
|
||||
});
|
||||
}
|
||||
None => (),
|
||||
|
@ -8,11 +8,26 @@ use crate::{
|
||||
id,
|
||||
resource::{BufferMapState, BufferUse},
|
||||
};
|
||||
use gfx_memory::Block;
|
||||
use gfx_memory::{Block, MemoryBlock};
|
||||
use hal::{command::CommandBuffer as _, device::Device as _, queue::CommandQueue as _};
|
||||
use smallvec::SmallVec;
|
||||
use std::{iter, sync::atomic::Ordering};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub(crate) struct PendingWrites<B: hal::Backend> {
|
||||
pub command_buffer: Option<B::CommandBuffer>,
|
||||
pub temp_buffers: Vec<(B::Buffer, MemoryBlock<B>)>,
|
||||
}
|
||||
|
||||
impl<B: hal::Backend> PendingWrites<B> {
|
||||
pub fn new() -> Self {
|
||||
PendingWrites {
|
||||
command_buffer: None,
|
||||
temp_buffers: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<G: GlobalIdentityHandlerFactory> Global<G> {
|
||||
pub fn queue_write_buffer<B: GfxBackend>(
|
||||
&self,
|
||||
@ -36,6 +51,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
|
||||
id: buffer_id,
|
||||
data: data_path,
|
||||
range: buffer_offset..buffer_offset + data.len() as wgt::BufferAddress,
|
||||
queued: true,
|
||||
});
|
||||
}
|
||||
None => {}
|
||||
@ -88,7 +104,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
|
||||
.slice
|
||||
.copy_from_slice(data);
|
||||
|
||||
let mut comb = match device.pending_write_command_buffer.take() {
|
||||
let mut comb = match device.pending_writes.command_buffer.take() {
|
||||
Some(comb) => comb,
|
||||
None => {
|
||||
let mut comb = device.com_allocator.allocate_internal();
|
||||
@ -117,7 +133,8 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
|
||||
);
|
||||
comb.copy_buffer(&src_raw, &dst.raw, iter::once(region));
|
||||
}
|
||||
device.pending_write_command_buffer = Some(comb);
|
||||
device.pending_writes.temp_buffers.push((src_raw, memory));
|
||||
device.pending_writes.command_buffer = Some(comb);
|
||||
}
|
||||
|
||||
pub fn queue_submit<B: GfxBackend>(
|
||||
@ -127,11 +144,11 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
|
||||
) {
|
||||
let hub = B::hub(self);
|
||||
|
||||
let (submit_index, fence) = {
|
||||
let callbacks = {
|
||||
let mut token = Token::root();
|
||||
let (mut device_guard, mut token) = hub.devices.write(&mut token);
|
||||
let device = &mut device_guard[queue_id];
|
||||
let pending_write_command_buffer = device.pending_write_command_buffer.take();
|
||||
let pending_write_command_buffer = device.pending_writes.command_buffer.take();
|
||||
device.temp_suspected.clear();
|
||||
|
||||
let submit_index = 1 + device
|
||||
@ -139,8 +156,12 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
|
||||
.submission_index
|
||||
.fetch_add(1, Ordering::Relaxed);
|
||||
|
||||
let fence = {
|
||||
let mut signal_swapchain_semaphores = SmallVec::<[_; 1]>::new();
|
||||
let (mut swap_chain_guard, mut token) = hub.swap_chains.write(&mut token);
|
||||
let (mut command_buffer_guard, mut token) = hub.command_buffers.write(&mut token);
|
||||
|
||||
{
|
||||
let (bind_group_guard, mut token) = hub.bind_groups.read(&mut token);
|
||||
let (compute_pipe_guard, mut token) = hub.compute_pipelines.read(&mut token);
|
||||
let (render_pipe_guard, mut token) = hub.render_pipelines.read(&mut token);
|
||||
@ -150,7 +171,6 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
|
||||
let (sampler_guard, _) = hub.samplers.read(&mut token);
|
||||
|
||||
//Note: locking the trackers has to be done after the storages
|
||||
let mut signal_swapchain_semaphores = SmallVec::<[_; 1]>::new();
|
||||
let mut trackers = device.trackers.lock();
|
||||
|
||||
//TODO: if multiple command buffers are submitted, we can re-use the last
|
||||
@ -231,7 +251,8 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
|
||||
unsafe {
|
||||
// the last buffer was open, closing now
|
||||
comb.raw.last_mut().unwrap().finish();
|
||||
transit.begin_primary(hal::command::CommandBufferFlags::ONE_TIME_SUBMIT);
|
||||
transit
|
||||
.begin_primary(hal::command::CommandBufferFlags::ONE_TIME_SUBMIT);
|
||||
}
|
||||
log::trace!("Stitching command buffer {:?} before submission", cmb_id);
|
||||
CommandBuffer::insert_barriers(
|
||||
@ -248,6 +269,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
|
||||
}
|
||||
|
||||
log::debug!("Device after submission {}: {:#?}", submit_index, trackers);
|
||||
}
|
||||
|
||||
// now prepare the GPU submission
|
||||
let fence = device.raw.create_fence(false).unwrap();
|
||||
@ -266,26 +288,21 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
|
||||
unsafe {
|
||||
device.queue_group.queues[0].submit(submission, Some(&fence));
|
||||
}
|
||||
fence
|
||||
};
|
||||
|
||||
if let Some(comb_raw) = pending_write_command_buffer {
|
||||
device
|
||||
.com_allocator
|
||||
.after_submit_internal(comb_raw, submit_index);
|
||||
}
|
||||
(submit_index, fence)
|
||||
};
|
||||
|
||||
// No need for write access to the device from here on out
|
||||
let callbacks = {
|
||||
let mut token = Token::root();
|
||||
let (device_guard, mut token) = hub.devices.read(&mut token);
|
||||
let device = &device_guard[queue_id];
|
||||
|
||||
let callbacks = device.maintain(self, false, &mut token);
|
||||
device.lock_life(&mut token).track_submission(
|
||||
super::Device::lock_life_internal(&device.life_tracker, &mut token).track_submission(
|
||||
submit_index,
|
||||
fence,
|
||||
&device.temp_suspected,
|
||||
device.pending_writes.temp_buffers.drain(..),
|
||||
);
|
||||
|
||||
// finally, return the command buffers to the allocator
|
||||
|
@ -166,6 +166,7 @@ pub enum Action {
|
||||
id: id::BufferId,
|
||||
data: FileName,
|
||||
range: Range<wgt::BufferAddress>,
|
||||
queued: bool,
|
||||
},
|
||||
Submit(crate::SubmissionIndex, Vec<Command>),
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user