Skip to content

Commit

Permalink
Remove duplicate validation in GenericMemoryAllocator (#2336)
Browse files Browse the repository at this point in the history
* Remove duplicate validation

* Merge `GenericMemoryAllocator::new[_unchecked]`

* Fix oopsie from earlier PR
  • Loading branch information
marc0246 authored Sep 28, 2023
1 parent 44732af commit b494760
Showing 1 changed file with 21 additions and 79 deletions.
100 changes: 21 additions & 79 deletions vulkano/src/memory/allocator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -888,7 +887,7 @@ impl StandardMemoryAllocator {
..Default::default()
};

unsafe { Self::new_unchecked(device, create_info) }
Self::new(device, create_info)
}
}

Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b494760

Please sign in to comment.