diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index 8620f4eee..cfa0a460e 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -664,6 +664,8 @@ impl Device { .describe_format_features(desc.format) .map_err(|error| resource::CreateTextureError::MissingFeatures(desc.format, error))?; + unsafe { self.raw().add_raw_texture(&*hal_texture) }; + let texture = Texture::new( self, resource::TextureInner::Native { raw: hal_texture }, @@ -689,6 +691,8 @@ impl Device { hal_buffer: Box, desc: &resource::BufferDescriptor, ) -> Arc { + unsafe { self.raw().add_raw_buffer(&*hal_buffer) }; + let buffer = Buffer { raw: Snatchable::new(hal_buffer), device: self.clone(), diff --git a/wgpu-hal/src/dx12/device.rs b/wgpu-hal/src/dx12/device.rs index c5286c12d..43425bb5f 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -444,6 +444,10 @@ impl crate::Device for super::Device { self.counters.buffers.sub(1); } + unsafe fn add_raw_buffer(&self, _buffer: &super::Buffer) { + self.counters.buffers.add(1); + } + unsafe fn map_buffer( &self, buffer: &super::Buffer, @@ -531,6 +535,10 @@ impl crate::Device for super::Device { self.counters.textures.sub(1); } + unsafe fn add_raw_texture(&self, _texture: &super::Texture) { + self.counters.textures.add(1); + } + unsafe fn create_texture_view( &self, texture: &super::Texture, diff --git a/wgpu-hal/src/dynamic/device.rs b/wgpu-hal/src/dynamic/device.rs index 1386196d6..ba7ec47b6 100644 --- a/wgpu-hal/src/dynamic/device.rs +++ b/wgpu-hal/src/dynamic/device.rs @@ -24,6 +24,7 @@ pub trait DynDevice: DynResource { ) -> Result, DeviceError>; unsafe fn destroy_buffer(&self, buffer: Box); + unsafe fn add_raw_buffer(&self, buffer: &dyn DynBuffer); unsafe fn map_buffer( &self, @@ -41,6 +42,8 @@ pub trait DynDevice: DynResource { desc: &TextureDescriptor, ) -> Result, DeviceError>; unsafe fn destroy_texture(&self, texture: Box); + unsafe fn add_raw_texture(&self, texture: &dyn DynTexture); + unsafe fn create_texture_view( &self, texture: &dyn DynTexture, @@ -177,6 +180,10 @@ impl DynDevice for D { unsafe fn destroy_buffer(&self, buffer: Box) { unsafe { D::destroy_buffer(self, buffer.unbox()) }; } + unsafe fn add_raw_buffer(&self, buffer: &dyn DynBuffer) { + let buffer = buffer.expect_downcast_ref(); + unsafe { D::add_raw_buffer(self, buffer) }; + } unsafe fn map_buffer( &self, @@ -217,6 +224,11 @@ impl DynDevice for D { unsafe { D::destroy_texture(self, texture.unbox()) }; } + unsafe fn add_raw_texture(&self, texture: &dyn DynTexture) { + let texture = texture.expect_downcast_ref(); + unsafe { D::add_raw_buffer(self, texture) }; + } + unsafe fn create_texture_view( &self, texture: &dyn DynTexture, diff --git a/wgpu-hal/src/empty.rs b/wgpu-hal/src/empty.rs index 4d8868c36..72d9784d6 100644 --- a/wgpu-hal/src/empty.rs +++ b/wgpu-hal/src/empty.rs @@ -168,6 +168,8 @@ impl crate::Device for Context { Ok(Resource) } unsafe fn destroy_buffer(&self, buffer: Resource) {} + unsafe fn add_raw_buffer(&self, _buffer: &Resource) {} + unsafe fn map_buffer( &self, buffer: &Resource, @@ -183,6 +185,8 @@ impl crate::Device for Context { Ok(Resource) } unsafe fn destroy_texture(&self, texture: Resource) {} + unsafe fn add_raw_texture(&self, _texture: &Resource) {} + unsafe fn create_texture_view( &self, texture: &Resource, diff --git a/wgpu-hal/src/gles/device.rs b/wgpu-hal/src/gles/device.rs index 490f73259..1abe2b810 100644 --- a/wgpu-hal/src/gles/device.rs +++ b/wgpu-hal/src/gles/device.rs @@ -647,6 +647,10 @@ impl crate::Device for super::Device { self.counters.buffers.sub(1); } + unsafe fn add_raw_buffer(&self, _buffer: &super::Buffer) { + self.counters.buffers.add(1); + } + unsafe fn map_buffer( &self, buffer: &super::Buffer, @@ -982,6 +986,10 @@ impl crate::Device for super::Device { self.counters.textures.sub(1); } + unsafe fn add_raw_texture(&self, _buffer: &super::Texture) { + self.counters.textures.add(1); + } + unsafe fn create_texture_view( &self, texture: &super::Texture, diff --git a/wgpu-hal/src/lib.rs b/wgpu-hal/src/lib.rs index 1446177f9..6578252c1 100644 --- a/wgpu-hal/src/lib.rs +++ b/wgpu-hal/src/lib.rs @@ -723,6 +723,9 @@ pub trait Device: WasmNotSendSync { /// - The given `buffer` must not currently be mapped. unsafe fn destroy_buffer(&self, buffer: ::Buffer); + /// A hook for when a wgpu-core buffer is created from a raw wgpu-hal buffer. + unsafe fn add_raw_buffer(&self, buffer: &::Buffer); + /// Return a pointer to CPU memory mapping the contents of `buffer`. /// /// Buffer mappings are persistent: the buffer may remain mapped on the CPU @@ -814,6 +817,10 @@ pub trait Device: WasmNotSendSync { desc: &TextureDescriptor, ) -> Result<::Texture, DeviceError>; unsafe fn destroy_texture(&self, texture: ::Texture); + + /// A hook for when a wgpu-core texture is created from a raw wgpu-hal texture. + unsafe fn add_raw_texture(&self, texture: &::Texture); + unsafe fn create_texture_view( &self, texture: &::Texture, diff --git a/wgpu-hal/src/metal/device.rs b/wgpu-hal/src/metal/device.rs index 077c10f51..347a97a08 100644 --- a/wgpu-hal/src/metal/device.rs +++ b/wgpu-hal/src/metal/device.rs @@ -356,6 +356,10 @@ impl crate::Device for super::Device { self.counters.buffers.sub(1); } + unsafe fn add_raw_buffer(&self, _buffer: &super::Buffer) { + self.counters.buffers.add(1); + } + unsafe fn map_buffer( &self, buffer: &super::Buffer, @@ -436,6 +440,10 @@ impl crate::Device for super::Device { self.counters.textures.sub(1); } + unsafe fn add_raw_texture(&self, _texture: &super::Texture) { + self.counters.textures.add(1); + } + unsafe fn create_texture_view( &self, texture: &super::Texture, diff --git a/wgpu-hal/src/vulkan/device.rs b/wgpu-hal/src/vulkan/device.rs index f21a24364..54905b4ba 100644 --- a/wgpu-hal/src/vulkan/device.rs +++ b/wgpu-hal/src/vulkan/device.rs @@ -956,6 +956,10 @@ impl crate::Device for super::Device { self.counters.buffers.sub(1); } + unsafe fn add_raw_buffer(&self, _buffer: &super::Buffer) { + self.counters.buffers.add(1); + } + unsafe fn map_buffer( &self, buffer: &super::Buffer, @@ -1127,6 +1131,10 @@ impl crate::Device for super::Device { self.counters.textures.sub(1); } + unsafe fn add_raw_texture(&self, _texture: &super::Texture) { + self.counters.textures.add(1); + } + unsafe fn create_texture_view( &self, texture: &super::Texture,