Skip to main content

wgpu/api/
query_set.rs

1use crate::*;
2
3/// Handle to a query set.
4///
5/// A `QuerySet` is an opaque, mutable storage location for the results of queries:
6/// which are small pieces of information extracted from other operations such as render passes.
7/// See [`QueryType`] for what types of information can be collected.
8///
9/// Each query writes data into one or more result slots in the `QuerySet`, which must be created
10/// with a sufficient number of slots for that usage. Each result slot is a an unsigned 64-bit
11/// number.
12///
13/// Using queries consists of the following steps:
14///
15/// 1. Create a `QuerySet` of the appropriate type and number of query result slots
16///    using [`Device::create_query_set()`].
17/// 2. Pass the `QuerySet` to the commands which will write to it.
18///    See [`QueryType`] for the possible commands.
19/// 3. Execute the command [`CommandEncoder::resolve_query_set()`].
20///    This converts the opaque data stored in a `QuerySet` into [`u64`]s stored in a [`Buffer`].
21/// 4. Make use of that buffer, such as by copying its contents to the CPU
22///    or reading it from a compute shader.
23///
24/// Corresponds to [WebGPU `GPUQuerySet`](https://gpuweb.github.io/gpuweb/#queryset).
25#[derive(Debug, Clone)]
26pub struct QuerySet {
27    pub(crate) inner: dispatch::DispatchQuerySet,
28}
29#[cfg(send_sync)]
30#[cfg(send_sync)]
31static_assertions::assert_impl_all!(QuerySet: Send, Sync);
32
33crate::cmp::impl_eq_ord_hash_proxy!(QuerySet => .inner);
34
35impl QuerySet {
36    #[cfg(custom)]
37    /// Returns custom implementation of QuerySet (if custom backend and is internally T)
38    pub fn as_custom<T: custom::QuerySetInterface>(&self) -> Option<&T> {
39        self.inner.as_custom()
40    }
41
42    /// Destroys the [`QuerySet`], releasing its resources.
43    pub fn destroy(&self) {
44        self.inner.destroy();
45    }
46
47    /// Returns the type of queries stored.
48    pub fn ty(&self) -> QueryType {
49        self.inner.ty()
50    }
51
52    /// Returns the number of query result slots.
53    pub fn count(&self) -> u32 {
54        self.inner.count()
55    }
56}
57
58#[cfg(wgpu_core)]
59impl QuerySet {
60    /// Create a new query set of wgpu from a wgpu-core query set.
61    ///
62    /// # Arguments
63    ///
64    /// - `core_query_set` - wgpu-core query set.
65    pub fn from_core(core_query_set: alloc::sync::Arc<wgc::resource::QuerySet>) -> Self {
66        Self {
67            inner: crate::backend::wgpu_core::CoreQuerySet::from_core(core_query_set).into(),
68        }
69    }
70
71    /// Returns the underlying wgpu-core query set if this `QuerySet` is on the wgpu-core backend, otherwise `None`.
72    pub fn as_core(&self) -> Option<alloc::sync::Arc<wgc::resource::QuerySet>> {
73        self.inner.as_core_opt().map(|qs| qs.as_core())
74    }
75}
76
77/// Describes a [`QuerySet`].
78///
79/// For use with [`Device::create_query_set`].
80///
81/// Corresponds to [WebGPU `GPUQuerySetDescriptor`](
82/// https://gpuweb.github.io/gpuweb/#dictdef-gpuquerysetdescriptor).
83pub type QuerySetDescriptor<'a> = wgt::QuerySetDescriptor<Label<'a>>;
84static_assertions::assert_impl_all!(QuerySetDescriptor<'_>: Send, Sync);