From ed8503ed6c053f9a20b2ef5a86f7908496b6af76 Mon Sep 17 00:00:00 2001 From: yanchith Date: Mon, 23 Sep 2019 12:13:18 +0200 Subject: [PATCH 1/2] Track and destroy samplers This PR adds `ResourceId::Sampler`, `NativeResource::Sampler` and `TrackerSet::samplers`. Samplers are (similarly to bind groups) created with their own life guard and a device id to keep the device ref count alive. Also added are `extern wgpu_sampler_destroy` and `sampler_destroy`. The rest of the implementation was guided by compiler errors, so there is a good chance I didn't find something crucial that also needed doing. Please check :) --- ffi/wgpu.h | 2 ++ wgpu-native/src/device.rs | 40 +++++++++++++++++++++++++++++++++++- wgpu-native/src/resource.rs | 2 ++ wgpu-native/src/track/mod.rs | 15 ++++++++++++-- 4 files changed, 56 insertions(+), 3 deletions(-) diff --git a/ffi/wgpu.h b/ffi/wgpu.h index e4f36c4b7..14803ea1c 100644 --- a/ffi/wgpu.h +++ b/ffi/wgpu.h @@ -899,6 +899,8 @@ void wgpu_render_pass_set_viewport(WGPURenderPassId pass_id, WGPUAdapterId wgpu_request_adapter(const WGPURequestAdapterOptions *desc); #endif +void wgpu_sampler_destroy(WGPUSamplerId sampler_id); + #if !defined(WGPU_REMOTE) WGPUSwapChainOutput wgpu_swap_chain_get_next_texture(WGPUSwapChainId swap_chain_id); #endif diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 79a2d7271..77843a002 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -135,6 +135,7 @@ enum ResourceId { Texture(TextureId), TextureView(TextureViewId), BindGroup(BindGroupId), + Sampler(SamplerId), } #[derive(Debug)] @@ -144,6 +145,7 @@ enum NativeResource { ImageView(B::ImageView), Framebuffer(B::Framebuffer), DescriptorSet(DescriptorSet), + Sampler(B::Sampler), } #[derive(Debug)] @@ -257,6 +259,9 @@ impl PendingResources { NativeResource::DescriptorSet(raw) => unsafe { descriptor_allocator.free(iter::once(raw)); }, + NativeResource::Sampler(raw) => unsafe { + device.destroy_sampler(raw); + }, } } @@ -279,7 +284,8 @@ impl PendingResources { let (mut bind_group_guard, mut token) = hub.bind_groups.write(&mut token); let (mut buffer_guard, mut token) = hub.buffers.write(&mut token); let (mut texture_guard, mut token) = hub.textures.write(&mut token); - let (mut teview_view_guard, _) = hub.texture_views.write(&mut token); + let (mut teview_view_guard, mut token) = hub.texture_views.write(&mut token); + let (mut sampler_guard, _) = hub.samplers.write(&mut token); for i in (0 .. self.referenced.len()).rev() { let num_refs = self.referenced[i].1.load(); @@ -329,6 +335,13 @@ impl PendingResources { NativeResource::DescriptorSet(bind_group.raw), ) } + ResourceId::Sampler(id) => { + trackers.samplers.remove(id); + let sampler = sampler_guard.remove(id).unwrap(); + #[cfg(not(feature = "remote"))] + hub.samplers.identity.lock().free(id); + (sampler.life_guard, NativeResource::Sampler(sampler.raw)) + } }; let submit_index = life_guard.submission_index.load(Ordering::Acquire); @@ -1041,6 +1054,11 @@ pub fn device_create_sampler( let sampler = resource::Sampler { raw: unsafe { device.raw.create_sampler(info).unwrap() }, + device_id: Stored { + value: device_id, + ref_count: device.life_guard.ref_count.clone(), + }, + life_guard: LifeGuard::new(), }; hub.samplers.register_identity(id_in, sampler, &mut token) } @@ -1054,6 +1072,26 @@ pub extern "C" fn wgpu_device_create_sampler( gfx_select!(device_id => device_create_sampler(device_id, desc, PhantomData)) } +pub fn sampler_destroy(sampler_id: SamplerId) { + let hub = B::hub(); + let mut token = Token::root(); + let (device_guard, mut token) = hub.devices.read(&mut token); + let (sampler_guard, _) = hub.samplers.read(&mut token); + let sampler = &sampler_guard[sampler_id]; + device_guard[sampler.device_id.value] + .pending + .lock() + .destroy( + ResourceId::Sampler(sampler_id), + sampler.life_guard.ref_count.clone(), + ); +} + +#[no_mangle] +pub extern "C" fn wgpu_sampler_destroy(sampler_id: SamplerId) { + gfx_select!(sampler_id => sampler_destroy(sampler_id)) +} + pub fn device_create_bind_group_layout( device_id: DeviceId, desc: &binding_model::BindGroupLayoutDescriptor, diff --git a/wgpu-native/src/resource.rs b/wgpu-native/src/resource.rs index dcec925df..fd0c67c80 100644 --- a/wgpu-native/src/resource.rs +++ b/wgpu-native/src/resource.rs @@ -360,4 +360,6 @@ pub struct SamplerDescriptor { #[derive(Debug)] pub struct Sampler { pub(crate) raw: B::Sampler, + pub(crate) device_id: Stored, + pub(crate) life_guard: LifeGuard, } diff --git a/wgpu-native/src/track/mod.rs b/wgpu-native/src/track/mod.rs index 03d26dd97..b1d1a4470 100644 --- a/wgpu-native/src/track/mod.rs +++ b/wgpu-native/src/track/mod.rs @@ -2,7 +2,17 @@ mod buffer; mod range; mod texture; -use crate::{hub::Storage, Backend, BindGroupId, Epoch, Index, RefCount, TextureViewId, TypedId}; +use crate::{ + hub::Storage, + Backend, + BindGroupId, + Epoch, + Index, + RefCount, + SamplerId, + TextureViewId, + TypedId, +}; use hal::backend::FastHashMap; @@ -410,7 +420,7 @@ pub struct TrackerSet { pub textures: ResourceTracker, pub views: ResourceTracker>, pub bind_groups: ResourceTracker>, - //TODO: samplers + pub samplers: ResourceTracker>, } impl TrackerSet { @@ -421,6 +431,7 @@ impl TrackerSet { textures: ResourceTracker::new(backend), views: ResourceTracker::new(backend), bind_groups: ResourceTracker::new(backend), + samplers: ResourceTracker::new(backend), } } From 758bb16d014af327b71af6b236d232a14aafff45 Mon Sep 17 00:00:00 2001 From: yanchith Date: Tue, 24 Sep 2019 19:04:35 +0200 Subject: [PATCH 2/2] Make sure submissions are tracked --- wgpu-native/src/command/mod.rs | 1 + wgpu-native/src/device.rs | 13 +++++++++++-- wgpu-native/src/resource.rs | 6 ++++++ wgpu-native/src/track/mod.rs | 3 +++ 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/wgpu-native/src/command/mod.rs b/wgpu-native/src/command/mod.rs index baf4cfc05..56f77e1e3 100644 --- a/wgpu-native/src/command/mod.rs +++ b/wgpu-native/src/command/mod.rs @@ -160,6 +160,7 @@ impl CommandBuffer { }); base.views.merge_extend(&head.views).unwrap(); base.bind_groups.merge_extend(&head.bind_groups).unwrap(); + base.samplers.merge_extend(&head.samplers).unwrap(); let stages = all_buffer_stages() | all_image_stages(); unsafe { diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 77843a002..7a1407764 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -1273,7 +1273,9 @@ pub fn device_create_bind_group( } binding_model::BindingResource::Sampler(id) => { assert_eq!(decl.ty, binding_model::BindingType::Sampler); - let sampler = &sampler_guard[id]; + let sampler = used.samplers + .use_extend(&*sampler_guard, id, (), ()) + .unwrap(); hal::pso::Descriptor::Sampler(&sampler.raw) } binding_model::BindingResource::TextureView(id) => { @@ -1471,7 +1473,8 @@ pub fn queue_submit(queue_id: QueueId, command_buffer_ids: &[Comm let (bind_group_guard, mut token) = hub.bind_groups.read(&mut token); let (buffer_guard, mut token) = hub.buffers.read(&mut token); let (texture_guard, mut token) = hub.textures.read(&mut token); - let (mut texture_view_guard, _) = hub.texture_views.write(&mut token); + let (mut texture_view_guard, mut token) = hub.texture_views.write(&mut token); + let (sampler_guard, _) = hub.samplers.read(&mut token); //TODO: if multiple command buffers are submitted, we can re-use the last // native command buffer of the previous chain instead of always creating @@ -1524,6 +1527,12 @@ pub fn queue_submit(queue_id: QueueId, command_buffer_ids: &[Comm .submission_index .store(submit_index, Ordering::Release); } + for id in comb.trackers.samplers.used() { + sampler_guard[id] + .life_guard + .submission_index + .store(submit_index, Ordering::Release); + } // execute resource transitions let mut transit = device.com_allocator.extend(comb); diff --git a/wgpu-native/src/resource.rs b/wgpu-native/src/resource.rs index fd0c67c80..a543a5244 100644 --- a/wgpu-native/src/resource.rs +++ b/wgpu-native/src/resource.rs @@ -363,3 +363,9 @@ pub struct Sampler { pub(crate) device_id: Stored, pub(crate) life_guard: LifeGuard, } + +impl Borrow for Sampler { + fn borrow(&self) -> &RefCount { + &self.life_guard.ref_count + } +} diff --git a/wgpu-native/src/track/mod.rs b/wgpu-native/src/track/mod.rs index b1d1a4470..105a35086 100644 --- a/wgpu-native/src/track/mod.rs +++ b/wgpu-native/src/track/mod.rs @@ -441,6 +441,7 @@ impl TrackerSet { self.textures.clear(); self.views.clear(); self.bind_groups.clear(); + self.samplers.clear(); } /// Try to optimize the tracking representation. @@ -449,6 +450,7 @@ impl TrackerSet { self.textures.optimize(); self.views.optimize(); self.bind_groups.optimize(); + self.samplers.optimize(); } /// Merge all the trackers of another instance by extending @@ -458,6 +460,7 @@ impl TrackerSet { self.textures.merge_extend(&other.textures).unwrap(); self.views.merge_extend(&other.views).unwrap(); self.bind_groups.merge_extend(&other.bind_groups).unwrap(); + self.samplers.merge_extend(&other.samplers).unwrap(); } pub fn backend(&self) -> Backend {