1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
// Copyright (c) 2023 The vulkano developers
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.
//! Store user-defined data associated with Vulkan objects.
//!
//! A private data slot allows you to associate an arbitrary `u64` value with any device-owned
//! Vulkan object. Each private data slot can store one value per object, but you can use the
//! value to look up a larger amount of data in a collection such as a `HashMap`.
//!
//! The intended usage is to create one private data slot for every subsystem in your program
//! that needs to assign data to objects independently of the others. That way, different parts
//! of a program manage their own private data and don't interfere with each other's data.
//!
//! When creating a device, it is possible to reserve private data slots ahead of time using
//! [`DeviceCreateInfo::private_data_slot_request_count`]. This is not necessary, but it can
//! speed up the use of data slots later.
//!
//! [`DeviceCreateInfo::private_data_slot_request_count`]: super::DeviceCreateInfo::private_data_slot_request_count
use super::{Device, DeviceOwned};
use crate::{
instance::InstanceOwnedDebugWrapper, Requires, RequiresAllOf, RequiresOneOf, Validated,
ValidationError, Version, VulkanError, VulkanObject,
};
use ash::vk::Handle;
use std::{mem::MaybeUninit, ptr, sync::Arc};
/// An object that stores one `u64` value per Vulkan object.
#[derive(Debug)]
pub struct PrivateDataSlot {
device: InstanceOwnedDebugWrapper<Arc<Device>>,
handle: ash::vk::PrivateDataSlot,
}
impl PrivateDataSlot {
/// Creates a new `PrivateDataSlot`.
///
/// The `private_data` feature must be enabled on the device.
#[inline]
pub fn new(
device: Arc<Device>,
create_info: PrivateDataSlotCreateInfo,
) -> Result<Self, Validated<VulkanError>> {
Self::validate_new(&device, &create_info)?;
unsafe { Ok(Self::new_unchecked(device, create_info)?) }
}
fn validate_new(
device: &Device,
create_info: &PrivateDataSlotCreateInfo,
) -> Result<(), Box<ValidationError>> {
if !device.enabled_features().private_data {
return Err(Box::new(ValidationError {
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::Feature(
"private_data",
)])]),
vuids: &["VUID-vkCreatePrivateDataSlot-privateData-04564"],
..Default::default()
}));
}
create_info
.validate(device)
.map_err(|err| err.add_context("create_info"))?;
Ok(())
}
#[cfg_attr(not(feature = "document_unchecked"), doc(hidden))]
pub unsafe fn new_unchecked(
device: Arc<Device>,
create_info: PrivateDataSlotCreateInfo,
) -> Result<Self, VulkanError> {
let &PrivateDataSlotCreateInfo { _ne: _ } = &create_info;
let create_info_vk = ash::vk::PrivateDataSlotCreateInfo {
flags: ash::vk::PrivateDataSlotCreateFlags::empty(),
..Default::default()
};
let handle = {
let fns = device.fns();
let mut output = MaybeUninit::uninit();
if device.api_version() >= Version::V1_3 {
(fns.v1_3.create_private_data_slot)(
device.handle(),
&create_info_vk,
ptr::null(),
output.as_mut_ptr(),
)
} else {
(fns.ext_private_data.create_private_data_slot_ext)(
device.handle(),
&create_info_vk,
ptr::null(),
output.as_mut_ptr(),
)
}
.result()
.map_err(VulkanError::from)?;
output.assume_init()
};
Ok(Self::from_handle(device, handle, create_info))
}
/// Creates a new `PrivateDataSlot` from a raw object handle.
///
/// # Safety
///
/// - `handle` must be a valid Vulkan object handle created from `device`.
/// - `create_info` must match the info used to create the object.
#[inline]
pub unsafe fn from_handle(
device: Arc<Device>,
handle: ash::vk::PrivateDataSlot,
_create_info: PrivateDataSlotCreateInfo,
) -> Self {
Self {
device: InstanceOwnedDebugWrapper(device),
handle,
}
}
/// Sets the private data that is associated with `object` to `data`.
///
/// If `self` already has data for `object`, that data is replaced with the new value.
#[inline]
pub fn set_private_data<T: VulkanObject + DeviceOwned>(
&self,
object: &T,
data: u64,
) -> Result<(), Validated<VulkanError>> {
self.validate_set_private_data(object)?;
unsafe { Ok(self.set_private_data_unchecked(object, data)?) }
}
fn validate_set_private_data<T: VulkanObject + DeviceOwned>(
&self,
object: &T,
) -> Result<(), Box<ValidationError>> {
assert_eq!(self.device(), object.device());
Ok(())
}
#[cfg_attr(not(feature = "document_unchecked"), doc(hidden))]
pub unsafe fn set_private_data_unchecked<T: VulkanObject + DeviceOwned>(
&self,
object: &T,
data: u64,
) -> Result<(), VulkanError> {
let fns = self.device.fns();
if self.device.api_version() >= Version::V1_3 {
(fns.v1_3.set_private_data)(
self.device.handle(),
T::Handle::TYPE,
object.handle().as_raw(),
self.handle,
data,
)
} else {
(fns.ext_private_data.set_private_data_ext)(
self.device.handle(),
T::Handle::TYPE,
object.handle().as_raw(),
self.handle,
data,
)
}
.result()
.map_err(VulkanError::from)
}
/// Returns the private data in `self` that is associated with `object`.
///
/// If no private data was previously set, 0 is returned.
pub fn get_private_data<T: VulkanObject + DeviceOwned>(&self, object: &T) -> u64 {
let fns = self.device.fns();
unsafe {
let mut output = MaybeUninit::uninit();
if self.device.api_version() >= Version::V1_3 {
(fns.v1_3.get_private_data)(
self.device.handle(),
T::Handle::TYPE,
object.handle().as_raw(),
self.handle,
output.as_mut_ptr(),
)
} else {
(fns.ext_private_data.get_private_data_ext)(
self.device.handle(),
T::Handle::TYPE,
object.handle().as_raw(),
self.handle,
output.as_mut_ptr(),
)
}
output.assume_init()
}
}
}
impl Drop for PrivateDataSlot {
#[inline]
fn drop(&mut self) {
unsafe {
let fns = self.device.fns();
if self.device.api_version() >= Version::V1_3 {
(fns.v1_3.destroy_private_data_slot)(
self.device.handle(),
self.handle,
ptr::null(),
);
} else {
(fns.ext_private_data.destroy_private_data_slot_ext)(
self.device.handle(),
self.handle,
ptr::null(),
);
}
}
}
}
unsafe impl VulkanObject for PrivateDataSlot {
type Handle = ash::vk::PrivateDataSlot;
#[inline]
fn handle(&self) -> Self::Handle {
self.handle
}
}
unsafe impl DeviceOwned for PrivateDataSlot {
#[inline]
fn device(&self) -> &Arc<Device> {
&self.device
}
}
/// Parameters to create a new `PrivateDataSlot`.
#[derive(Clone, Debug)]
pub struct PrivateDataSlotCreateInfo {
pub _ne: crate::NonExhaustive,
}
impl Default for PrivateDataSlotCreateInfo {
#[inline]
fn default() -> Self {
Self {
_ne: crate::NonExhaustive(()),
}
}
}
impl PrivateDataSlotCreateInfo {
pub(crate) fn validate(&self, _device: &Device) -> Result<(), Box<ValidationError>> {
Ok(())
}
}