Skip to content

Commit

Permalink
Fix the internal buffer and texture count (#6223)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nical authored Sep 5, 2024
1 parent ec21000 commit 81963e2
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions wgpu-core/src/device/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -689,6 +691,8 @@ impl Device {
hal_buffer: Box<dyn hal::DynBuffer>,
desc: &resource::BufferDescriptor,
) -> Arc<Buffer> {
unsafe { self.raw().add_raw_buffer(&*hal_buffer) };

let buffer = Buffer {
raw: Snatchable::new(hal_buffer),
device: self.clone(),
Expand Down
8 changes: 8 additions & 0 deletions wgpu-hal/src/dx12/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions wgpu-hal/src/dynamic/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub trait DynDevice: DynResource {
) -> Result<Box<dyn DynBuffer>, DeviceError>;

unsafe fn destroy_buffer(&self, buffer: Box<dyn DynBuffer>);
unsafe fn add_raw_buffer(&self, buffer: &dyn DynBuffer);

unsafe fn map_buffer(
&self,
Expand All @@ -41,6 +42,8 @@ pub trait DynDevice: DynResource {
desc: &TextureDescriptor,
) -> Result<Box<dyn DynTexture>, DeviceError>;
unsafe fn destroy_texture(&self, texture: Box<dyn DynTexture>);
unsafe fn add_raw_texture(&self, texture: &dyn DynTexture);

unsafe fn create_texture_view(
&self,
texture: &dyn DynTexture,
Expand Down Expand Up @@ -177,6 +180,10 @@ impl<D: Device + DynResource> DynDevice for D {
unsafe fn destroy_buffer(&self, buffer: Box<dyn DynBuffer>) {
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,
Expand Down Expand Up @@ -217,6 +224,11 @@ impl<D: Device + DynResource> 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,
Expand Down
4 changes: 4 additions & 0 deletions wgpu-hal/src/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions wgpu-hal/src/gles/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions wgpu-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,9 @@ pub trait Device: WasmNotSendSync {
/// - The given `buffer` must not currently be mapped.
unsafe fn destroy_buffer(&self, buffer: <Self::A as Api>::Buffer);

/// A hook for when a wgpu-core buffer is created from a raw wgpu-hal buffer.
unsafe fn add_raw_buffer(&self, buffer: &<Self::A as Api>::Buffer);

/// Return a pointer to CPU memory mapping the contents of `buffer`.
///
/// Buffer mappings are persistent: the buffer may remain mapped on the CPU
Expand Down Expand Up @@ -814,6 +817,10 @@ pub trait Device: WasmNotSendSync {
desc: &TextureDescriptor,
) -> Result<<Self::A as Api>::Texture, DeviceError>;
unsafe fn destroy_texture(&self, texture: <Self::A as Api>::Texture);

/// A hook for when a wgpu-core texture is created from a raw wgpu-hal texture.
unsafe fn add_raw_texture(&self, texture: &<Self::A as Api>::Texture);

unsafe fn create_texture_view(
&self,
texture: &<Self::A as Api>::Texture,
Expand Down
8 changes: 8 additions & 0 deletions wgpu-hal/src/metal/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions wgpu-hal/src/vulkan/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 81963e2

Please sign in to comment.