From 81963e24ca9da4cb3c092e65374453bee3ecd731 Mon Sep 17 00:00:00 2001 From: Nicolas Silva Date: Thu, 5 Sep 2024 20:53:01 +0200 Subject: [PATCH] Fix the internal buffer and texture count (#6223) Currently, we only increment the internal buffer/texture counters when creating them in the regular way (not when creating them from externally built hal objects (create_texture_from_hal/create_buffer_from_hal). However we decrement the counter in all cases, which makes the counters incorrect when these externally created resources are involved. This commit fixes it by adding hooks (add_raw_buffer and add_raw_texture) in the hal device abstractions to inform when buffer or textures are created externally. --- wgpu-core/src/device/resource.rs | 4 ++++ wgpu-hal/src/dx12/device.rs | 8 ++++++++ wgpu-hal/src/dynamic/device.rs | 12 ++++++++++++ wgpu-hal/src/empty.rs | 4 ++++ wgpu-hal/src/gles/device.rs | 8 ++++++++ wgpu-hal/src/lib.rs | 7 +++++++ wgpu-hal/src/metal/device.rs | 8 ++++++++ wgpu-hal/src/vulkan/device.rs | 8 ++++++++ 8 files changed, 59 insertions(+) diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index 8620f4eeec..cfa0a460ed 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 c5286c12d8..43425bb5f1 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 1386196d60..ba7ec47b66 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 4d8868c360..72d9784d65 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 490f73259b..1abe2b8103 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 1446177f9b..6578252c1a 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 077c10f517..347a97a086 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 f21a243645..54905b4ba7 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,