use crate::{
check_errors,
device::{Device, DeviceOwned},
DeviceSize, Error, OomError, Success, VulkanObject,
};
use std::{
error,
ffi::c_void,
fmt,
hash::{Hash, Hasher},
mem::{size_of_val, MaybeUninit},
ops::Range,
ptr,
sync::Arc,
};
#[derive(Debug)]
pub struct QueryPool {
handle: ash::vk::QueryPool,
device: Arc<Device>,
query_type: QueryType,
query_count: u32,
}
impl QueryPool {
pub fn new(
device: Arc<Device>,
create_info: QueryPoolCreateInfo,
) -> Result<Arc<QueryPool>, QueryPoolCreationError> {
let QueryPoolCreateInfo {
query_type,
query_count,
_ne: _,
} = create_info;
assert!(query_count != 0);
let pipeline_statistics = match query_type {
QueryType::PipelineStatistics(flags) => {
if !device.enabled_features().pipeline_statistics_query {
return Err(QueryPoolCreationError::PipelineStatisticsQueryFeatureNotEnabled);
}
flags.into()
}
QueryType::Occlusion | QueryType::Timestamp => {
ash::vk::QueryPipelineStatisticFlags::empty()
}
};
let create_info = ash::vk::QueryPoolCreateInfo {
flags: ash::vk::QueryPoolCreateFlags::empty(),
query_type: query_type.into(),
query_count,
pipeline_statistics,
..Default::default()
};
let handle = unsafe {
let fns = device.fns();
let mut output = MaybeUninit::uninit();
check_errors(fns.v1_0.create_query_pool(
device.internal_object(),
&create_info,
ptr::null(),
output.as_mut_ptr(),
))?;
output.assume_init()
};
Ok(Arc::new(QueryPool {
handle,
device,
query_type,
query_count,
}))
}
#[inline]
pub fn query_type(&self) -> QueryType {
self.query_type
}
#[inline]
pub fn query_count(&self) -> u32 {
self.query_count
}
#[inline]
pub fn query(&self, index: u32) -> Option<Query> {
if index < self.query_count {
Some(Query { pool: self, index })
} else {
None
}
}
#[inline]
pub fn queries_range(&self, range: Range<u32>) -> Option<QueriesRange> {
assert!(!range.is_empty());
if range.end <= self.query_count {
Some(QueriesRange { pool: self, range })
} else {
None
}
}
}
impl Drop for QueryPool {
#[inline]
fn drop(&mut self) {
unsafe {
let fns = self.device.fns();
fns.v1_0
.destroy_query_pool(self.device.internal_object(), self.handle, ptr::null());
}
}
}
unsafe impl VulkanObject for QueryPool {
type Object = ash::vk::QueryPool;
#[inline]
fn internal_object(&self) -> ash::vk::QueryPool {
self.handle
}
}
unsafe impl DeviceOwned for QueryPool {
#[inline]
fn device(&self) -> &Arc<Device> {
&self.device
}
}
impl PartialEq for QueryPool {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.handle == other.handle && self.device() == other.device()
}
}
impl Eq for QueryPool {}
impl Hash for QueryPool {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.handle.hash(state);
self.device().hash(state);
}
}
#[derive(Clone, Debug)]
pub struct QueryPoolCreateInfo {
pub query_type: QueryType,
pub query_count: u32,
pub _ne: crate::NonExhaustive,
}
impl QueryPoolCreateInfo {
#[inline]
pub fn query_type(query_type: QueryType) -> Self {
Self {
query_type,
query_count: 0,
_ne: crate::NonExhaustive(()),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum QueryPoolCreationError {
OomError(OomError),
PipelineStatisticsQueryFeatureNotEnabled,
}
impl error::Error for QueryPoolCreationError {
#[inline]
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self {
QueryPoolCreationError::OomError(ref err) => Some(err),
_ => None,
}
}
}
impl fmt::Display for QueryPoolCreationError {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(
fmt,
"{}",
match *self {
QueryPoolCreationError::OomError(_) => "not enough memory available",
QueryPoolCreationError::PipelineStatisticsQueryFeatureNotEnabled => {
"a pipeline statistics pool was requested but the corresponding feature \
wasn't enabled"
}
}
)
}
}
impl From<OomError> for QueryPoolCreationError {
#[inline]
fn from(err: OomError) -> QueryPoolCreationError {
QueryPoolCreationError::OomError(err)
}
}
impl From<Error> for QueryPoolCreationError {
#[inline]
fn from(err: Error) -> QueryPoolCreationError {
match err {
err @ Error::OutOfHostMemory => QueryPoolCreationError::OomError(OomError::from(err)),
err @ Error::OutOfDeviceMemory => QueryPoolCreationError::OomError(OomError::from(err)),
_ => panic!("unexpected error: {:?}", err),
}
}
}
#[derive(Clone, Debug)]
pub struct Query<'a> {
pool: &'a QueryPool,
index: u32,
}
impl<'a> Query<'a> {
#[inline]
pub fn pool(&self) -> &'a QueryPool {
&self.pool
}
#[inline]
pub fn index(&self) -> u32 {
self.index
}
}
#[derive(Clone, Debug)]
pub struct QueriesRange<'a> {
pool: &'a QueryPool,
range: Range<u32>,
}
impl<'a> QueriesRange<'a> {
#[inline]
pub fn pool(&self) -> &'a QueryPool {
&self.pool
}
#[inline]
pub fn range(&self) -> Range<u32> {
self.range.clone()
}
pub fn get_results<T>(
&self,
destination: &mut [T],
flags: QueryResultFlags,
) -> Result<bool, GetResultsError>
where
T: QueryResultElement,
{
let stride = self.check_query_pool_results::<T>(
destination.as_ptr() as DeviceSize,
destination.len() as DeviceSize,
flags,
)?;
let result = unsafe {
let fns = self.pool.device.fns();
check_errors(fns.v1_0.get_query_pool_results(
self.pool.device.internal_object(),
self.pool.internal_object(),
self.range.start,
self.range.end - self.range.start,
size_of_val(destination),
destination.as_mut_ptr() as *mut c_void,
stride,
ash::vk::QueryResultFlags::from(flags) | T::FLAG,
))?
};
Ok(match result {
Success::Success => true,
Success::NotReady => false,
s => panic!("unexpected success value: {:?}", s),
})
}
pub(crate) fn check_query_pool_results<T>(
&self,
buffer_start: DeviceSize,
buffer_len: DeviceSize,
flags: QueryResultFlags,
) -> Result<DeviceSize, GetResultsError>
where
T: QueryResultElement,
{
assert!(buffer_len > 0);
debug_assert!(buffer_start % std::mem::size_of::<T>() as DeviceSize == 0);
let count = self.range.end - self.range.start;
let per_query_len =
self.pool.query_type.result_len() + flags.with_availability as DeviceSize;
let required_len = per_query_len * count as DeviceSize;
if buffer_len < required_len {
return Err(GetResultsError::BufferTooSmall {
required_len: required_len as DeviceSize,
actual_len: buffer_len as DeviceSize,
});
}
match self.pool.query_type {
QueryType::Occlusion => (),
QueryType::PipelineStatistics(_) => (),
QueryType::Timestamp => {
if flags.partial {
return Err(GetResultsError::InvalidFlags);
}
}
}
Ok(per_query_len * std::mem::size_of::<T>() as DeviceSize)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum GetResultsError {
BufferTooSmall {
required_len: DeviceSize,
actual_len: DeviceSize,
},
DeviceLost,
InvalidFlags,
OomError(OomError),
}
impl From<Error> for GetResultsError {
#[inline]
fn from(err: Error) -> Self {
match err {
Error::OutOfHostMemory | Error::OutOfDeviceMemory => {
Self::OomError(OomError::from(err))
}
Error::DeviceLost => Self::DeviceLost,
_ => panic!("unexpected error: {:?}", err),
}
}
}
impl From<OomError> for GetResultsError {
#[inline]
fn from(err: OomError) -> Self {
Self::OomError(err)
}
}
impl fmt::Display for GetResultsError {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(
fmt,
"{}",
match *self {
Self::BufferTooSmall { .. } => {
"the buffer is too small for the operation"
}
Self::DeviceLost => "the connection to the device has been lost",
Self::InvalidFlags => {
"the provided flags are not allowed for this type of query"
}
Self::OomError(_) => "not enough memory available",
}
)
}
}
impl error::Error for GetResultsError {
#[inline]
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self {
Self::OomError(ref err) => Some(err),
_ => None,
}
}
}
pub unsafe trait QueryResultElement {
const FLAG: ash::vk::QueryResultFlags;
}
unsafe impl QueryResultElement for u32 {
const FLAG: ash::vk::QueryResultFlags = ash::vk::QueryResultFlags::empty();
}
unsafe impl QueryResultElement for u64 {
const FLAG: ash::vk::QueryResultFlags = ash::vk::QueryResultFlags::TYPE_64;
}
#[derive(Debug, Copy, Clone)]
pub enum QueryType {
Occlusion,
PipelineStatistics(QueryPipelineStatisticFlags),
Timestamp,
}
impl QueryType {
#[inline]
pub const fn result_len(&self) -> DeviceSize {
match self {
Self::Occlusion | Self::Timestamp => 1,
Self::PipelineStatistics(flags) => flags.count(),
}
}
}
impl From<QueryType> for ash::vk::QueryType {
#[inline]
fn from(value: QueryType) -> Self {
match value {
QueryType::Occlusion => ash::vk::QueryType::OCCLUSION,
QueryType::PipelineStatistics(_) => ash::vk::QueryType::PIPELINE_STATISTICS,
QueryType::Timestamp => ash::vk::QueryType::TIMESTAMP,
}
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct QueryControlFlags {
pub precise: bool,
}
impl From<QueryControlFlags> for ash::vk::QueryControlFlags {
#[inline]
fn from(value: QueryControlFlags) -> Self {
let mut result = ash::vk::QueryControlFlags::empty();
if value.precise {
result |= ash::vk::QueryControlFlags::PRECISE;
}
result
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct QueryPipelineStatisticFlags {
pub input_assembly_vertices: bool,
pub input_assembly_primitives: bool,
pub vertex_shader_invocations: bool,
pub geometry_shader_invocations: bool,
pub geometry_shader_primitives: bool,
pub clipping_invocations: bool,
pub clipping_primitives: bool,
pub fragment_shader_invocations: bool,
pub tessellation_control_shader_patches: bool,
pub tessellation_evaluation_shader_invocations: bool,
pub compute_shader_invocations: bool,
}
impl QueryPipelineStatisticFlags {
#[inline]
pub fn none() -> QueryPipelineStatisticFlags {
QueryPipelineStatisticFlags {
input_assembly_vertices: false,
input_assembly_primitives: false,
vertex_shader_invocations: false,
geometry_shader_invocations: false,
geometry_shader_primitives: false,
clipping_invocations: false,
clipping_primitives: false,
fragment_shader_invocations: false,
tessellation_control_shader_patches: false,
tessellation_evaluation_shader_invocations: false,
compute_shader_invocations: false,
}
}
#[inline]
pub const fn count(&self) -> DeviceSize {
let &Self {
input_assembly_vertices,
input_assembly_primitives,
vertex_shader_invocations,
geometry_shader_invocations,
geometry_shader_primitives,
clipping_invocations,
clipping_primitives,
fragment_shader_invocations,
tessellation_control_shader_patches,
tessellation_evaluation_shader_invocations,
compute_shader_invocations,
} = self;
input_assembly_vertices as DeviceSize
+ input_assembly_primitives as DeviceSize
+ vertex_shader_invocations as DeviceSize
+ geometry_shader_invocations as DeviceSize
+ geometry_shader_primitives as DeviceSize
+ clipping_invocations as DeviceSize
+ clipping_primitives as DeviceSize
+ fragment_shader_invocations as DeviceSize
+ tessellation_control_shader_patches as DeviceSize
+ tessellation_evaluation_shader_invocations as DeviceSize
+ compute_shader_invocations as DeviceSize
}
#[inline]
pub const fn is_compute(&self) -> bool {
let &Self {
compute_shader_invocations,
..
} = self;
compute_shader_invocations
}
#[inline]
pub const fn is_graphics(&self) -> bool {
let &Self {
input_assembly_vertices,
input_assembly_primitives,
vertex_shader_invocations,
geometry_shader_invocations,
geometry_shader_primitives,
clipping_invocations,
clipping_primitives,
fragment_shader_invocations,
tessellation_control_shader_patches,
tessellation_evaluation_shader_invocations,
..
} = self;
input_assembly_vertices
|| input_assembly_primitives
|| vertex_shader_invocations
|| geometry_shader_invocations
|| geometry_shader_primitives
|| clipping_invocations
|| clipping_primitives
|| fragment_shader_invocations
|| tessellation_control_shader_patches
|| tessellation_evaluation_shader_invocations
}
}
impl From<QueryPipelineStatisticFlags> for ash::vk::QueryPipelineStatisticFlags {
fn from(value: QueryPipelineStatisticFlags) -> ash::vk::QueryPipelineStatisticFlags {
let mut result = ash::vk::QueryPipelineStatisticFlags::empty();
if value.input_assembly_vertices {
result |= ash::vk::QueryPipelineStatisticFlags::INPUT_ASSEMBLY_VERTICES;
}
if value.input_assembly_primitives {
result |= ash::vk::QueryPipelineStatisticFlags::INPUT_ASSEMBLY_PRIMITIVES;
}
if value.vertex_shader_invocations {
result |= ash::vk::QueryPipelineStatisticFlags::VERTEX_SHADER_INVOCATIONS;
}
if value.geometry_shader_invocations {
result |= ash::vk::QueryPipelineStatisticFlags::GEOMETRY_SHADER_INVOCATIONS;
}
if value.geometry_shader_primitives {
result |= ash::vk::QueryPipelineStatisticFlags::GEOMETRY_SHADER_PRIMITIVES;
}
if value.clipping_invocations {
result |= ash::vk::QueryPipelineStatisticFlags::CLIPPING_INVOCATIONS;
}
if value.clipping_primitives {
result |= ash::vk::QueryPipelineStatisticFlags::CLIPPING_PRIMITIVES;
}
if value.fragment_shader_invocations {
result |= ash::vk::QueryPipelineStatisticFlags::FRAGMENT_SHADER_INVOCATIONS;
}
if value.tessellation_control_shader_patches {
result |= ash::vk::QueryPipelineStatisticFlags::TESSELLATION_CONTROL_SHADER_PATCHES;
}
if value.tessellation_evaluation_shader_invocations {
result |=
ash::vk::QueryPipelineStatisticFlags::TESSELLATION_EVALUATION_SHADER_INVOCATIONS;
}
if value.compute_shader_invocations {
result |= ash::vk::QueryPipelineStatisticFlags::COMPUTE_SHADER_INVOCATIONS;
}
result
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct QueryResultFlags {
pub wait: bool,
pub with_availability: bool,
pub partial: bool,
}
impl From<QueryResultFlags> for ash::vk::QueryResultFlags {
#[inline]
fn from(value: QueryResultFlags) -> Self {
let mut result = ash::vk::QueryResultFlags::empty();
if value.wait {
result |= ash::vk::QueryResultFlags::WAIT;
}
if value.with_availability {
result |= ash::vk::QueryResultFlags::WITH_AVAILABILITY;
}
if value.partial {
result |= ash::vk::QueryResultFlags::PARTIAL;
}
result
}
}
#[cfg(test)]
mod tests {
use super::QueryPoolCreateInfo;
use crate::query::QueryPipelineStatisticFlags;
use crate::query::QueryPool;
use crate::query::QueryPoolCreationError;
use crate::query::QueryType;
#[test]
fn pipeline_statistics_feature() {
let (device, _) = gfx_dev_and_queue!();
let query_type = QueryType::PipelineStatistics(QueryPipelineStatisticFlags::none());
match QueryPool::new(
device,
QueryPoolCreateInfo {
query_count: 256,
..QueryPoolCreateInfo::query_type(query_type)
},
) {
Err(QueryPoolCreationError::PipelineStatisticsQueryFeatureNotEnabled) => (),
_ => panic!(),
};
}
}