Skip to content

Commit

Permalink
Expose the counters in wgpu-core.
Browse files Browse the repository at this point in the history
  • Loading branch information
nical committed May 16, 2024
1 parent e758452 commit b15dc6f
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 46 deletions.
15 changes: 15 additions & 0 deletions wgpu-core/src/device/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2361,6 +2361,21 @@ impl Global {
}
}

pub fn device_get_internal_counters<A: HalApi>(
&self,
device_id: DeviceId,
) -> wgt::InternalCounters {
let hub = A::hub(self);
if let Ok(device) = hub.devices.get(device_id) {
wgt::InternalCounters {
hal: device.get_hal_counters(),
core: wgt::CoreCounters {},
}
} else {
Default::default()
}
}

pub fn queue_drop<A: HalApi>(&self, queue_id: QueueId) {
profiling::scope!("Queue::drop");
api_log!("Queue::drop {queue_id:?}");
Expand Down
7 changes: 5 additions & 2 deletions wgpu-core/src/device/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3655,8 +3655,11 @@ impl<A: HalApi> Device<A> {
UsageScope::new_pooled(&self.usage_scopes, &self.tracker_indices)
}

pub fn get_hal_counters(&self) -> &hal::InternalCounters {
self.raw.as_ref().unwrap().get_internal_counters()
pub fn get_hal_counters(&self) -> wgt::HalCounters {
self.raw
.as_ref()
.map(|raw| raw.get_internal_counters())
.unwrap_or_default()
}
}

Expand Down
6 changes: 3 additions & 3 deletions wgpu-hal/src/dx12/device.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
auxil::{self, dxgi::result::HResult as _},
dx12::shader_compilation,
DeviceError, InternalCounters,
DeviceError,
};
use d3d12::ComPtr;

Expand Down Expand Up @@ -1774,7 +1774,7 @@ impl crate::Device for super::Device {
todo!()
}

fn get_internal_counters(&self) -> &InternalCounters {
&self.counters
fn get_internal_counters(&self) -> wgt::HalCounters {
self.counters.clone()
}
}
3 changes: 1 addition & 2 deletions wgpu-hal/src/dx12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ mod view;

use crate::auxil::{self, dxgi::result::HResult as _};

use crate::InternalCounters;
use arrayvec::ArrayVec;
use parking_lot::{Mutex, RwLock};
use std::{ffi, fmt, mem, num::NonZeroU32, sync::Arc};
Expand Down Expand Up @@ -261,7 +260,7 @@ pub struct Device {
null_rtv_handle: descriptor::Handle,
mem_allocator: Option<Mutex<suballocation::GpuAllocatorWrapper>>,
dxc_container: Option<Arc<shader_compilation::DxcContainer>>,
counters: InternalCounters,
counters: wgt::HalCounters,
}

unsafe impl Send for Device {}
Expand Down
4 changes: 2 additions & 2 deletions wgpu-hal/src/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ impl crate::Device for Context {
}
unsafe fn destroy_acceleration_structure(&self, _acceleration_structure: Resource) {}

fn get_internal_counters(&self) -> &crate::InternalCounters {
unreachable!();
fn get_internal_counters(&self) -> wgt::HalCounters {
Default::default()
}
}

Expand Down
4 changes: 2 additions & 2 deletions wgpu-hal/src/gles/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1594,8 +1594,8 @@ impl crate::Device for super::Device {
}
unsafe fn destroy_acceleration_structure(&self, _acceleration_structure: ()) {}

fn get_internal_counters(&self) -> &crate::InternalCounters {
&self.counters
fn get_internal_counters(&self) -> wgt::HalCounters {
self.counters.clone()
}
}

Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/src/gles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ pub struct Device {
main_vao: glow::VertexArray,
#[cfg(all(native, feature = "renderdoc"))]
render_doc: crate::auxil::renderdoc::RenderDoc,
counters: crate::InternalCounters,
counters: wgt::HalCounters,
}

pub struct ShaderClearProgram {
Expand Down
26 changes: 2 additions & 24 deletions wgpu-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ use std::{
use bitflags::bitflags;
use parking_lot::Mutex;
use thiserror::Error;
use wgt::{InternalCounter, WasmNotSendSync};
use wgt::WasmNotSendSync;

// - Vertex + Fragment
// - Compute
Expand Down Expand Up @@ -669,7 +669,7 @@ pub trait Device: WasmNotSendSync {
acceleration_structure: <Self::A as Api>::AccelerationStructure,
);

fn get_internal_counters(&self) -> &InternalCounters;
fn get_internal_counters(&self) -> wgt::HalCounters;
}

pub trait Queue: WasmNotSendSync {
Expand Down Expand Up @@ -2028,25 +2028,3 @@ bitflags::bitflags! {
pub struct AccelerationStructureBarrier {
pub usage: Range<AccelerationStructureUses>,
}

#[derive(Default)]
pub struct InternalCounters {
// API objects
pub buffers: InternalCounter,
pub textures: InternalCounter,
pub texture_views: InternalCounter,
pub bind_groups: InternalCounter,
pub bind_group_layouts: InternalCounter,
pub render_pipelines: InternalCounter,
pub compute_pipelines: InternalCounter,
pub pipeline_layouts: InternalCounter,
pub samplers: InternalCounter,
pub command_encoders: InternalCounter,
pub shader_modules: InternalCounter,
pub query_sets: InternalCounter,
pub fences: InternalCounter,
// Resources
pub buffer_memory: InternalCounter,
pub texture_memory: InternalCounter,
pub memory_allocations: InternalCounter,
}
5 changes: 2 additions & 3 deletions wgpu-hal/src/metal/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::{

use super::conv;
use crate::auxil::map_naga_stage;
use crate::InternalCounters;

type DeviceResult<T> = Result<T, crate::DeviceError>;

Expand Down Expand Up @@ -1325,7 +1324,7 @@ impl crate::Device for super::Device {
unimplemented!()
}

fn get_internal_counters(&self) -> &InternalCounters {
&self.counters
fn get_internal_counters(&self) -> wgt::HalCounters {
self.counters.clone()
}
}
3 changes: 1 addition & 2 deletions wgpu-hal/src/metal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use std::{
thread,
};

use crate::InternalCounters;
use arrayvec::ArrayVec;
use bitflags::bitflags;
use metal::foreign_types::ForeignTypeRef as _;
Expand Down Expand Up @@ -338,7 +337,7 @@ impl Queue {
pub struct Device {
shared: Arc<AdapterShared>,
features: wgt::Features,
counters: InternalCounters,
counters: wgt::HalCounters,
}

pub struct Surface {
Expand Down
6 changes: 2 additions & 4 deletions wgpu-hal/src/vulkan/device.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::InternalCounters;

use super::conv;

use arrayvec::ArrayVec;
Expand Down Expand Up @@ -2405,12 +2403,12 @@ impl crate::Device for super::Device {
}
}

fn get_internal_counters(&self) -> &InternalCounters {
fn get_internal_counters(&self) -> wgt::HalCounters {
self.counters
.memory_allocations
.set(self.shared.memory_allocations_counter.read());

&self.counters
self.counters.clone()
}
}

Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/src/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ pub struct Device {
naga_options: naga::back::spv::Options<'static>,
#[cfg(feature = "renderdoc")]
render_doc: crate::auxil::renderdoc::RenderDoc,
counters: crate::InternalCounters,
counters: wgt::HalCounters,
}

pub struct Queue {
Expand Down
56 changes: 56 additions & 0 deletions wgpu-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7250,6 +7250,7 @@ pub enum DeviceLostReason {
///
/// Internally represented as an atomic isize if the `counters` feature is enabled,
/// or compiles to nothing otherwise.
#[derive(Clone)]
pub struct InternalCounter {
#[cfg(feature = "counters")]
value: AtomicIsize,
Expand Down Expand Up @@ -7341,3 +7342,58 @@ impl std::fmt::Debug for InternalCounter {
self.read().fmt(f)
}
}

/// `wgpu-hal`'s internal counters.
#[derive(Clone, Default)]
pub struct HalCounters {
// API objects
///
pub buffers: InternalCounter,
///
pub textures: InternalCounter,
///
pub texture_views: InternalCounter,
///
pub bind_groups: InternalCounter,
///
pub bind_group_layouts: InternalCounter,
///
pub render_pipelines: InternalCounter,
///
pub compute_pipelines: InternalCounter,
///
pub pipeline_layouts: InternalCounter,
///
pub samplers: InternalCounter,
///
pub command_encoders: InternalCounter,
///
pub shader_modules: InternalCounter,
///
pub query_sets: InternalCounter,
///
pub fences: InternalCounter,

// Resources
/// Amount of allocated gpu memory attributed to buffers, in bytes.
pub buffer_memory: InternalCounter,
/// Amount of allocated gpu memory attributed to textures, in bytes.
pub texture_memory: InternalCounter,
/// Number of gpu memory allocations.
pub memory_allocations: InternalCounter,
}

/// `wgpu-core`'s internal counters.
#[derive(Clone, Default)]
pub struct CoreCounters {
// TODO
}

/// All internal counters, exposed for debugging purposes.
#[derive(Clone, Default)]
pub struct InternalCounters {
/// `wgpu-core` counters.
pub core: CoreCounters,
/// `wgpu-hal` counters.
pub hal: HalCounters,
}

0 comments on commit b15dc6f

Please sign in to comment.