mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-19 14:01:08 +01:00
Track async mapping properly
This commit is contained in:
1 parent
f5ad76726b
commit
1f4d67aeb2
5 files changed
+94
-53
No files matched your search
@@ -91,13 +91,8 @@ fn main() {
|
||||
device.get_queue().submit(&[encoder.finish()]);
|
||||
|
||||
staging_buffer.map_read_async(0, size, |result: wgpu::BufferMapAsyncResult<&[u32]>| {
|
||||
if let wgpu::BufferMapAsyncResult::Success(data) = result {
|
||||
println!("Times: {:?}", data);
|
||||
if let Ok(mapping) = result {
|
||||
println!("Times: {:?}", mapping.data);
|
||||
}
|
||||
});
|
||||
|
||||
let encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 });
|
||||
device.get_queue().submit(&[encoder.finish()]);
|
||||
|
||||
staging_buffer.unmap(); // TODO: staging_buffer can't be referenced from the callback
|
||||
}
|
||||
@@ -754,10 +754,12 @@ WGPUSwapChainId wgpu_device_create_swap_chain(WGPUDeviceId device_id,
|
||||
|
||||
WGPUTextureId wgpu_device_create_texture(WGPUDeviceId device_id, const WGPUTextureDescriptor *desc);
|
||||
|
||||
void wgpu_device_destroy(WGPUBufferId device_id);
|
||||
void wgpu_device_destroy(WGPUDeviceId device_id);
|
||||
|
||||
WGPUQueueId wgpu_device_get_queue(WGPUDeviceId device_id);
|
||||
|
||||
void wgpu_device_wait_idle(WGPUDeviceId device_id);
|
||||
|
||||
WGPUSurfaceId wgpu_instance_create_surface_from_macos_layer(WGPUInstanceId instance_id,
|
||||
void *layer);
|
||||
|
||||
|
||||
@@ -131,4 +131,17 @@ impl<B: hal::Backend> CommandAllocator<B> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn destroy(self, device: &B::Device) {
|
||||
let mut inner = self.inner.lock();
|
||||
while let Some(cmd_buf) = inner.pending.pop() {
|
||||
inner.recycle(cmd_buf);
|
||||
}
|
||||
for (_, mut pool) in inner.pools.drain() {
|
||||
unsafe {
|
||||
pool.raw.free(pool.available);
|
||||
device.destroy_command_pool(pool.raw);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+38
-26
@@ -141,6 +141,7 @@ impl<B: hal::Backend> DestroyedResources<B> {
|
||||
trace!("Active submission {} is done", a.index);
|
||||
last_done = last_done.max(a.index);
|
||||
self.free.extend(a.resources.into_iter().map(|(_, r)| r));
|
||||
self.ready_to_map.extend(a.mapped);
|
||||
unsafe {
|
||||
device.destroy_fence(a.fence);
|
||||
}
|
||||
@@ -459,6 +460,22 @@ impl<B: hal::Backend> Device<B> {
|
||||
}
|
||||
}
|
||||
|
||||
impl Device<back::Backend> {
|
||||
fn maintain(&self) {
|
||||
let mut destroyed = self.destroyed.lock();
|
||||
let mut trackers = self.trackers.lock();
|
||||
|
||||
destroyed.triage_referenced(&mut *trackers);
|
||||
destroyed.triage_framebuffers(&mut *self.framebuffers.lock());
|
||||
let last_done = destroyed.cleanup(&self.raw);
|
||||
destroyed.handle_mapping(&self.raw, &self.limits);
|
||||
|
||||
if last_done != 0 {
|
||||
self.com_allocator.maintain(last_done);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ShaderModule<B: hal::Backend> {
|
||||
pub(crate) raw: B::ShaderModule,
|
||||
}
|
||||
@@ -1094,13 +1111,8 @@ pub extern "C" fn wgpu_queue_submit(
|
||||
let (submit_index, fence) = {
|
||||
let mut device_guard = HUB.devices.write();
|
||||
let device = &mut device_guard[queue_id];
|
||||
|
||||
let mut swap_chain_links = Vec::new();
|
||||
|
||||
let mut trackers = device.trackers.lock();
|
||||
let mut destroyed = device.destroyed.lock();
|
||||
destroyed.triage_referenced(&mut *trackers);
|
||||
destroyed.triage_framebuffers(&mut *device.framebuffers.lock());
|
||||
let mut swap_chain_links = Vec::new();
|
||||
|
||||
let submit_index = 1 + device
|
||||
.life_guard
|
||||
@@ -1213,24 +1225,13 @@ pub extern "C" fn wgpu_queue_submit(
|
||||
let device_guard = HUB.devices.read();
|
||||
let device = &device_guard[queue_id];
|
||||
|
||||
let last_done = {
|
||||
let mut destroyed = device.destroyed.lock();
|
||||
let last_done = destroyed.cleanup(&device.raw);
|
||||
destroyed.handle_mapping(&device.raw, &device.limits);
|
||||
|
||||
destroyed.active.alloc().init(ActiveSubmission {
|
||||
index: submit_index,
|
||||
fence,
|
||||
resources: Vec::new(),
|
||||
mapped: Vec::new(),
|
||||
});
|
||||
|
||||
last_done
|
||||
};
|
||||
|
||||
if last_done != 0 {
|
||||
device.com_allocator.maintain(last_done);
|
||||
}
|
||||
device.maintain();
|
||||
device.destroyed.lock().active.alloc().init(ActiveSubmission {
|
||||
index: submit_index,
|
||||
fence,
|
||||
resources: Vec::new(),
|
||||
mapped: Vec::new(),
|
||||
});
|
||||
|
||||
// finally, return the command buffers to the allocator
|
||||
for &cmb_id in command_buffer_ids {
|
||||
@@ -1800,8 +1801,19 @@ pub extern "C" fn wgpu_buffer_set_sub_data(
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wgpu_device_destroy(device_id: BufferId) {
|
||||
HUB.devices.unregister(device_id);
|
||||
pub extern "C" fn wgpu_device_wait_idle(device_id: DeviceId) {
|
||||
let device_guard = HUB.devices.read();
|
||||
let device = &device_guard[device_id];
|
||||
device.raw.wait_idle().unwrap();
|
||||
device.maintain();
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wgpu_device_destroy(device_id: DeviceId) {
|
||||
let device = HUB.devices.unregister(device_id);
|
||||
device.raw.wait_idle().unwrap();
|
||||
device.maintain();
|
||||
device.com_allocator.destroy(&device.raw);
|
||||
}
|
||||
|
||||
pub type BufferMapReadCallback =
|
||||
|
||||
+38
-19
@@ -493,15 +493,24 @@ impl Device {
|
||||
|
||||
impl Drop for Device {
|
||||
fn drop(&mut self) {
|
||||
wgn::wgpu_device_wait_idle(self.id);
|
||||
//TODO: make this work in general
|
||||
#[cfg(feature = "metal-auto-capture")]
|
||||
wgn::wgpu_device_destroy(self.id);
|
||||
}
|
||||
}
|
||||
|
||||
pub enum BufferMapAsyncResult<T> {
|
||||
Success(T),
|
||||
Error,
|
||||
pub struct BufferAsyncMapping<T> {
|
||||
pub data: T,
|
||||
buffer_id: wgn::BufferId,
|
||||
}
|
||||
//TODO: proper error type
|
||||
pub type BufferMapAsyncResult<T> = Result<BufferAsyncMapping<T>, ()>;
|
||||
|
||||
impl<T> Drop for BufferAsyncMapping<T> {
|
||||
fn drop(&mut self) {
|
||||
wgn::wgpu_buffer_unmap(self.buffer_id);
|
||||
}
|
||||
}
|
||||
|
||||
struct BufferMapReadAsyncUserData<T, F>
|
||||
@@ -510,6 +519,7 @@ where
|
||||
{
|
||||
size: u32,
|
||||
callback: F,
|
||||
buffer_id: wgn::BufferId,
|
||||
phantom: std::marker::PhantomData<T>,
|
||||
}
|
||||
|
||||
@@ -519,6 +529,7 @@ where
|
||||
{
|
||||
size: u32,
|
||||
callback: F,
|
||||
buffer_id: wgn::BufferId,
|
||||
phantom: std::marker::PhantomData<T>,
|
||||
}
|
||||
|
||||
@@ -539,28 +550,32 @@ impl Buffer {
|
||||
extern "C" fn buffer_map_read_callback_wrapper<T, F>(
|
||||
status: wgn::BufferMapAsyncStatus,
|
||||
data: *const u8,
|
||||
userdata: *mut u8,
|
||||
user_data: *mut u8,
|
||||
) where
|
||||
F: FnOnce(BufferMapAsyncResult<&[T]>),
|
||||
{
|
||||
let userdata =
|
||||
unsafe { Box::from_raw(userdata as *mut BufferMapReadAsyncUserData<T, F>) };
|
||||
let user_data =
|
||||
unsafe { Box::from_raw(user_data as *mut BufferMapReadAsyncUserData<T, F>) };
|
||||
let data = unsafe {
|
||||
slice::from_raw_parts(
|
||||
data as *const T,
|
||||
userdata.size as usize / std::mem::size_of::<T>(),
|
||||
user_data.size as usize / std::mem::size_of::<T>(),
|
||||
)
|
||||
};
|
||||
if let wgn::BufferMapAsyncStatus::Success = status {
|
||||
(userdata.callback)(BufferMapAsyncResult::Success::<&[T]>(data));
|
||||
(user_data.callback)(Ok(BufferAsyncMapping {
|
||||
data,
|
||||
buffer_id: user_data.buffer_id,
|
||||
}));
|
||||
} else {
|
||||
(userdata.callback)(BufferMapAsyncResult::Error);
|
||||
(user_data.callback)(Err(()))
|
||||
}
|
||||
}
|
||||
|
||||
let userdata = Box::new(BufferMapReadAsyncUserData {
|
||||
let user_data = Box::new(BufferMapReadAsyncUserData {
|
||||
size,
|
||||
callback,
|
||||
buffer_id: self.id,
|
||||
phantom: std::marker::PhantomData,
|
||||
});
|
||||
wgn::wgpu_buffer_map_read_async(
|
||||
@@ -568,7 +583,7 @@ impl Buffer {
|
||||
start,
|
||||
size,
|
||||
buffer_map_read_callback_wrapper::<T, F>,
|
||||
Box::into_raw(userdata) as *mut u8,
|
||||
Box::into_raw(user_data) as *mut u8,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -584,28 +599,32 @@ impl Buffer {
|
||||
extern "C" fn buffer_map_write_callback_wrapper<T, F>(
|
||||
status: wgn::BufferMapAsyncStatus,
|
||||
data: *mut u8,
|
||||
userdata: *mut u8,
|
||||
user_data: *mut u8,
|
||||
) where
|
||||
F: FnOnce(BufferMapAsyncResult<&mut [T]>),
|
||||
{
|
||||
let userdata =
|
||||
unsafe { Box::from_raw(userdata as *mut BufferMapWriteAsyncUserData<T, F>) };
|
||||
let user_data =
|
||||
unsafe { Box::from_raw(user_data as *mut BufferMapWriteAsyncUserData<T, F>) };
|
||||
let data = unsafe {
|
||||
slice::from_raw_parts_mut(
|
||||
data as *mut T,
|
||||
userdata.size as usize / std::mem::size_of::<T>(),
|
||||
user_data.size as usize / std::mem::size_of::<T>(),
|
||||
)
|
||||
};
|
||||
if let wgn::BufferMapAsyncStatus::Success = status {
|
||||
(userdata.callback)(BufferMapAsyncResult::Success::<&mut [T]>(data));
|
||||
(user_data.callback)(Ok(BufferAsyncMapping {
|
||||
data,
|
||||
buffer_id: user_data.buffer_id,
|
||||
}));
|
||||
} else {
|
||||
(userdata.callback)(BufferMapAsyncResult::Error);
|
||||
(user_data.callback)(Err(()))
|
||||
}
|
||||
}
|
||||
|
||||
let userdata = Box::new(BufferMapWriteAsyncUserData {
|
||||
let user_data = Box::new(BufferMapWriteAsyncUserData {
|
||||
size,
|
||||
callback,
|
||||
buffer_id: self.id,
|
||||
phantom: std::marker::PhantomData,
|
||||
});
|
||||
wgn::wgpu_buffer_map_write_async(
|
||||
@@ -613,7 +632,7 @@ impl Buffer {
|
||||
start,
|
||||
size,
|
||||
buffer_map_write_callback_wrapper::<T, F>,
|
||||
Box::into_raw(userdata) as *mut u8,
|
||||
Box::into_raw(user_data) as *mut u8,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user