Remove duplicate validation in GenericMemoryAllocator (#2336)

* Remove duplicate validation

* Merge `GenericMemoryAllocator::new[_unchecked]`

* Fix oopsie from earlier PR
This commit is contained in:
marc0246 2023-09-28 11:06:08 +02:00 committed by GitHub
parent 44732afefe
commit b49476036c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -235,8 +235,7 @@ use super::{
use crate::{
device::{Device, DeviceOwned},
instance::InstanceOwnedDebugWrapper,
DeviceSize, Requires, RequiresAllOf, RequiresOneOf, Validated, ValidationError, Version,
VulkanError,
DeviceSize, Validated, Version, VulkanError,
};
use ash::vk::MAX_MEMORY_TYPES;
use parking_lot::Mutex;
@ -855,7 +854,7 @@ impl StandardMemoryAllocator {
memory_heaps,
} = device.physical_device().memory_properties();
let mut block_sizes = [0; MAX_MEMORY_TYPES];
let mut block_sizes = vec![0; memory_types.len()];
let mut memory_type_bits = u32::MAX;
for (index, memory_type) in memory_types.iter().enumerate() {
@ -888,7 +887,7 @@ impl StandardMemoryAllocator {
..Default::default()
};
unsafe { Self::new_unchecked(device, create_info) }
Self::new(device, create_info)
}
}
@ -969,31 +968,7 @@ impl<S> GenericMemoryAllocator<S> {
/// memory types.
/// - Panics if `create_info.export_handle_types` is non-empty and doesn't contain as many
/// elements as the number of memory types.
pub fn new(
device: Arc<Device>,
create_info: GenericMemoryAllocatorCreateInfo<'_>,
) -> Result<Self, Box<ValidationError>> {
Self::validate_new(&device, &create_info)?;
Ok(unsafe { Self::new_unchecked(device, create_info) })
}
fn validate_new(
device: &Device,
create_info: &GenericMemoryAllocatorCreateInfo<'_>,
) -> Result<(), Box<ValidationError>> {
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: GenericMemoryAllocatorCreateInfo<'_>,
) -> Self {
pub fn new(device: Arc<Device>, create_info: GenericMemoryAllocatorCreateInfo<'_>) -> Self {
let GenericMemoryAllocatorCreateInfo {
block_sizes,
memory_type_bits,
@ -1003,6 +978,23 @@ impl<S> GenericMemoryAllocator<S> {
_ne: _,
} = create_info;
let memory_types = &device.physical_device().memory_properties().memory_types;
assert_eq!(
block_sizes.len(),
memory_types.len(),
"`create_info.block_sizes` must contain as many elements as the number of memory types",
);
if !export_handle_types.is_empty() {
assert_eq!(
export_handle_types.len(),
memory_types.len(),
"`create_info.export_handle_types` must contain as many elements as the number of \
memory types if not empty",
);
}
let buffer_image_granularity = device
.physical_device()
.properties()
@ -1685,56 +1677,6 @@ pub struct GenericMemoryAllocatorCreateInfo<'a> {
pub _ne: crate::NonExhaustive,
}
impl GenericMemoryAllocatorCreateInfo<'_> {
pub(crate) fn validate(&self, device: &Device) -> Result<(), Box<ValidationError>> {
let &Self {
block_sizes,
memory_type_bits: _,
dedicated_allocation: _,
export_handle_types,
device_address: _,
_ne: _,
} = self;
let memory_types = &device.physical_device().memory_properties().memory_types;
assert!(
block_sizes.len() == memory_types.len(),
"`create_info.block_sizes` must contain as many elements as the number of memory types",
);
if !export_handle_types.is_empty() {
if !(device.api_version() >= Version::V1_1
&& device.enabled_extensions().khr_external_memory)
{
return Err(Box::new(ValidationError {
context: "export_handle_types".into(),
problem: "is not empty".into(),
requires_one_of: RequiresOneOf(&[
RequiresAllOf(&[Requires::APIVersion(Version::V1_1)]),
RequiresAllOf(&[Requires::DeviceExtension("khr_external_memory")]),
]),
..Default::default()
}));
}
assert!(
export_handle_types.len() == memory_types.len(),
"`create_info.export_handle_types` must contain as many elements as the number of \
memory types if not empty",
);
for (index, export_handle_types) in export_handle_types.iter().enumerate() {
export_handle_types
.validate_device(device)
.map_err(|err| err.add_context(format!("export_handle_types[{}]", index)))?;
}
}
Ok(())
}
}
impl Default for GenericMemoryAllocatorCreateInfo<'_> {
#[inline]
fn default() -> Self {