Skip to content

Commit

Permalink
Generate device properties that are alignment as DeviceAlignment (#…
Browse files Browse the repository at this point in the history
…2155)

* Move `DeviceAlignment`

* Generate alignment-properties with the type `DeviceAlignment`

* Small fix

* Another small fix

* Switch to using `is_aligned` where possible

* Oopsie
  • Loading branch information
marc0246 authored Mar 15, 2023
1 parent 19acc71 commit 127b3ea
Show file tree
Hide file tree
Showing 20 changed files with 384 additions and 363 deletions.
3 changes: 2 additions & 1 deletion examples/src/bin/dynamic-buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ fn main() {
let min_dynamic_align = device
.physical_device()
.properties()
.min_uniform_buffer_offset_alignment as usize;
.min_uniform_buffer_offset_alignment
.as_devicesize() as usize;

println!("Minimum uniform buffer offset alignment: {min_dynamic_align}");
println!("Input: {data:?}");
Expand Down
13 changes: 13 additions & 0 deletions vulkano/autogen/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,19 @@ fn properties_members(types: &HashMap<&str, (&Type, Vec<&str>)>) -> Vec<Properti
let vulkano_member = name.to_snake_case();
let vulkano_ty = match name {
"apiVersion" => quote! { Version },
"bufferImageGranularity"
| "minStorageBufferOffsetAlignment"
| "minTexelBufferOffsetAlignment"
| "minUniformBufferOffsetAlignment"
| "nonCoherentAtomSize"
| "optimalBufferCopyOffsetAlignment"
| "optimalBufferCopyRowPitchAlignment"
| "robustStorageBufferAccessSizeAlignment"
| "robustUniformBufferAccessSizeAlignment"
| "storageTexelBufferOffsetAlignmentBytes"
| "uniformTexelBufferOffsetAlignmentBytes" => {
quote! { DeviceAlignment }
}
_ => vulkano_type(ty, len),
};
match properties.entry(vulkano_member.clone()) {
Expand Down
10 changes: 6 additions & 4 deletions vulkano/src/buffer/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ use super::{Buffer, BufferContents, BufferError, BufferMemory, BufferUsage, Subb
use crate::{
buffer::BufferAllocateInfo,
device::{Device, DeviceOwned},
memory::allocator::{
align_up, AllocationCreationError, DeviceAlignment, DeviceLayout, MemoryAllocator,
MemoryUsage, StandardMemoryAllocator,
memory::{
allocator::{
align_up, AllocationCreationError, DeviceLayout, MemoryAllocator, MemoryUsage,
StandardMemoryAllocator,
},
DeviceAlignment,
},
DeviceSize, NonZeroDeviceSize,
};
Expand Down Expand Up @@ -145,7 +148,6 @@ where
.into_iter()
.flatten()
.max()
.map(|alignment| DeviceAlignment::new(alignment).unwrap())
.unwrap_or(DeviceAlignment::MIN);

SubbufferAllocator {
Expand Down
13 changes: 8 additions & 5 deletions vulkano/src/buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ use crate::{
macros::vulkan_bitflags,
memory::{
allocator::{
AllocationCreateInfo, AllocationCreationError, AllocationType, DeviceAlignment,
DeviceLayout, MemoryAlloc, MemoryAllocatePreference, MemoryAllocator, MemoryUsage,
AllocationCreateInfo, AllocationCreationError, AllocationType, DeviceLayout,
MemoryAlloc, MemoryAllocatePreference, MemoryAllocator, MemoryUsage,
},
DedicatedAllocation, ExternalMemoryHandleType, ExternalMemoryHandleTypes,
ExternalMemoryProperties, MemoryRequirements,
is_aligned, DedicatedAllocation, DeviceAlignment, ExternalMemoryHandleType,
ExternalMemoryHandleTypes, ExternalMemoryProperties, MemoryRequirements,
},
range_map::RangeMap,
sync::{future::AccessError, CurrentAccess, Sharing},
Expand Down Expand Up @@ -394,7 +394,10 @@ impl Buffer {
};

let mut allocation = unsafe { allocator.allocate_unchecked(create_info) }?;
debug_assert!(allocation.offset() % requirements.layout.alignment().as_nonzero() == 0);
debug_assert!(is_aligned(
allocation.offset(),
requirements.layout.alignment(),
));
debug_assert!(allocation.size() == requirements.layout.size());

// The implementation might require a larger size than we wanted. With this it is easier to
Expand Down
4 changes: 2 additions & 2 deletions vulkano/src/buffer/subbuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use crate::{
macros::try_opt,
memory::{
self,
allocator::{align_down, align_up, DeviceAlignment, DeviceLayout},
is_aligned,
allocator::{align_down, align_up, DeviceLayout},
is_aligned, DeviceAlignment,
},
DeviceSize, NonZeroDeviceSize,
};
Expand Down
20 changes: 7 additions & 13 deletions vulkano/src/buffer/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use super::{Buffer, BufferCreateFlags, BufferError, BufferMemory, BufferUsage};
use crate::{
device::{Device, DeviceOwned},
memory::{
allocator::{AllocationType, DeviceAlignment, DeviceLayout, MemoryAlloc},
DedicatedTo, ExternalMemoryHandleTypes, MemoryAllocateFlags, MemoryPropertyFlags,
MemoryRequirements,
allocator::{AllocationType, DeviceLayout, MemoryAlloc},
is_aligned, DedicatedTo, ExternalMemoryHandleTypes, MemoryAllocateFlags,
MemoryPropertyFlags, MemoryRequirements,
},
sync::Sharing,
DeviceSize, RequiresOneOf, Version, VulkanError, VulkanObject,
Expand Down Expand Up @@ -290,27 +290,21 @@ impl RawBuffer {
if usage.intersects(BufferUsage::UNIFORM_TEXEL_BUFFER | BufferUsage::STORAGE_TEXEL_BUFFER) {
memory_requirements.layout = memory_requirements
.layout
.align_to(
DeviceAlignment::new(properties.min_texel_buffer_offset_alignment).unwrap(),
)
.align_to(properties.min_texel_buffer_offset_alignment)
.unwrap();
}

if usage.intersects(BufferUsage::STORAGE_BUFFER) {
memory_requirements.layout = memory_requirements
.layout
.align_to(
DeviceAlignment::new(properties.min_storage_buffer_offset_alignment).unwrap(),
)
.align_to(properties.min_storage_buffer_offset_alignment)
.unwrap();
}

if usage.intersects(BufferUsage::UNIFORM_BUFFER) {
memory_requirements.layout = memory_requirements
.layout
.align_to(
DeviceAlignment::new(properties.min_uniform_buffer_offset_alignment).unwrap(),
)
.align_to(properties.min_uniform_buffer_offset_alignment)
.unwrap();
}

Expand Down Expand Up @@ -445,7 +439,7 @@ impl RawBuffer {
}

// VUID-VkBindBufferMemoryInfo-memoryOffset-01036
if memory_offset % memory_requirements.layout.alignment().as_nonzero() != 0 {
if !is_aligned(memory_offset, memory_requirements.layout.alignment()) {
return Err(BufferError::MemoryAllocationNotAligned {
allocation_offset: memory_offset,
required_alignment: memory_requirements.layout.alignment(),
Expand Down
14 changes: 8 additions & 6 deletions vulkano/src/buffer/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use super::{BufferUsage, Subbuffer};
use crate::{
device::{Device, DeviceOwned},
format::{Format, FormatFeatures},
memory::{is_aligned, DeviceAlignment},
DeviceSize, OomError, RequirementNotMet, RequiresOneOf, Version, VulkanError, VulkanObject,
};
use std::{
Expand Down Expand Up @@ -152,11 +153,12 @@ impl BufferView {

if device.api_version() >= Version::V1_3 || device.enabled_features().texel_buffer_alignment
{
let element_size = if block_size % 3 == 0 {
let element_size = DeviceAlignment::new(if block_size % 3 == 0 {
block_size / 3
} else {
block_size
};
})
.unwrap();

if buffer.usage().intersects(BufferUsage::STORAGE_TEXEL_BUFFER) {
let mut required_alignment = properties
Expand All @@ -171,7 +173,7 @@ impl BufferView {
}

// VUID-VkBufferViewCreateInfo-buffer-02750
if offset % required_alignment != 0 {
if !is_aligned(offset, required_alignment) {
return Err(BufferViewCreationError::OffsetNotAligned {
offset,
required_alignment,
Expand All @@ -192,7 +194,7 @@ impl BufferView {
}

// VUID-VkBufferViewCreateInfo-buffer-02751
if offset % required_alignment != 0 {
if !is_aligned(offset, required_alignment) {
return Err(BufferViewCreationError::OffsetNotAligned {
offset,
required_alignment,
Expand All @@ -203,7 +205,7 @@ impl BufferView {
let required_alignment = properties.min_texel_buffer_offset_alignment;

// VUID-VkBufferViewCreateInfo-offset-02749
if offset % required_alignment != 0 {
if !is_aligned(offset, required_alignment) {
return Err(BufferViewCreationError::OffsetNotAligned {
offset,
required_alignment,
Expand Down Expand Up @@ -340,7 +342,7 @@ pub enum BufferViewCreationError {
/// The offset within the buffer is not a multiple of the required alignment.
OffsetNotAligned {
offset: DeviceSize,
required_alignment: DeviceSize,
required_alignment: DeviceAlignment,
},

/// The range within the buffer is not a multiple of the required alignment.
Expand Down
11 changes: 6 additions & 5 deletions vulkano/src/command_buffer/commands/bind_push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::{
WriteDescriptorSet,
},
device::{DeviceOwned, QueueFlags},
memory::{is_aligned, DeviceAlignment},
pipeline::{
graphics::{
input_assembly::{Index, IndexType},
Expand Down Expand Up @@ -132,8 +133,8 @@ where
}

let properties = self.device().physical_device().properties();
let uniform_alignment = properties.min_uniform_buffer_offset_alignment as u32;
let storage_alignment = properties.min_storage_buffer_offset_alignment as u32;
let uniform_alignment = properties.min_uniform_buffer_offset_alignment;
let storage_alignment = properties.min_storage_buffer_offset_alignment;

for (i, set) in descriptor_sets.iter().enumerate() {
let set_num = first_set + i as u32;
Expand Down Expand Up @@ -183,7 +184,7 @@ where
{
// VUID-vkCmdBindDescriptorSets-pDynamicOffsets-01971
// VUID-vkCmdBindDescriptorSets-pDynamicOffsets-01972
if offset % required_alignment != 0 {
if !is_aligned(offset as DeviceSize, required_alignment) {
return Err(BindPushError::DynamicOffsetNotAligned {
set_num,
binding_num,
Expand Down Expand Up @@ -1341,7 +1342,7 @@ pub(in super::super) enum BindPushError {
binding_num: u32,
index: u32,
offset: u32,
required_alignment: u32,
required_alignment: DeviceAlignment,
},

/// In an element of `descriptor_sets`, a provided dynamic offset, when added to the end of the
Expand Down Expand Up @@ -1459,7 +1460,7 @@ impl Display for BindPushError {
"in the element of `descriptor_sets` being bound to slot {}, the dynamic offset \
provided for binding {} index {} ({}) is not a multiple of the value of the \
`min_uniform_buffer_offset_alignment` or `min_storage_buffer_offset_alignment` \
property ({})",
property ({:?})",
set_num, binding_num, index, offset, required_alignment,
),
Self::DynamicOffsetOutOfBufferBounds {
Expand Down
7 changes: 4 additions & 3 deletions vulkano/src/command_buffer/standard/builder/bind_push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::{
DescriptorWriteInfo, WriteDescriptorSet,
},
device::{DeviceOwned, QueueFlags},
memory::is_aligned,
pipeline::{
graphics::{
input_assembly::{Index, IndexType},
Expand Down Expand Up @@ -110,8 +111,8 @@ where
}

let properties = self.device().physical_device().properties();
let uniform_alignment = properties.min_uniform_buffer_offset_alignment as u32;
let storage_alignment = properties.min_storage_buffer_offset_alignment as u32;
let uniform_alignment = properties.min_uniform_buffer_offset_alignment;
let storage_alignment = properties.min_storage_buffer_offset_alignment;

for (i, set) in descriptor_sets.iter().enumerate() {
let set_num = first_set + i as u32;
Expand Down Expand Up @@ -161,7 +162,7 @@ where
{
// VUID-vkCmdBindDescriptorSets-pDynamicOffsets-01971
// VUID-vkCmdBindDescriptorSets-pDynamicOffsets-01972
if offset % required_alignment != 0 {
if !is_aligned(offset as DeviceSize, required_alignment) {
return Err(BindPushError::DynamicOffsetNotAligned {
set_num,
binding_num,
Expand Down
22 changes: 13 additions & 9 deletions vulkano/src/device/properties.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
use super::physical::{
MemoryDecompressionMethods, OpticalFlowGridSizes, PipelineRobustnessBufferBehavior,
PipelineRobustnessImageBehavior, RayTracingInvocationReorderMode,
ConformanceVersion, DriverId, MemoryDecompressionMethods, OpticalFlowGridSizes,
PhysicalDeviceType, PipelineRobustnessBufferBehavior, PipelineRobustnessImageBehavior,
PointClippingBehavior, RayTracingInvocationReorderMode, ShaderCoreProperties,
ShaderFloatControlsIndependence, SubgroupFeatures,
};
use crate::{
device::{
physical::{
ConformanceVersion, DriverId, PhysicalDeviceType, PointClippingBehavior,
ShaderCoreProperties, ShaderFloatControlsIndependence, SubgroupFeatures,
},
DeviceExtensions, QueueFlags,
},
device::{DeviceExtensions, QueueFlags},
image::{SampleCount, SampleCounts},
instance::InstanceExtensions,
memory::DeviceAlignment,
render_pass::ResolveModes,
shader::ShaderStages,
DeviceSize, Version,
Expand Down Expand Up @@ -65,6 +62,13 @@ impl FromVulkan<u64> for u64 {
}
}

impl FromVulkan<u64> for DeviceAlignment {
#[inline]
fn from_vulkan(val: u64) -> Option<Self> {
DeviceAlignment::new(val)
}
}

impl FromVulkan<usize> for usize {
#[inline]
fn from_vulkan(val: usize) -> Option<Self> {
Expand Down
6 changes: 3 additions & 3 deletions vulkano/src/image/attachment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
AllocationCreateInfo, AllocationType, MemoryAllocatePreference, MemoryAllocator,
MemoryUsage,
},
DedicatedAllocation, DeviceMemoryError, ExternalMemoryHandleType,
is_aligned, DedicatedAllocation, DeviceMemoryError, ExternalMemoryHandleType,
ExternalMemoryHandleTypes,
},
DeviceSize,
Expand Down Expand Up @@ -427,7 +427,7 @@ impl AttachmentImage {

match unsafe { allocator.allocate_unchecked(create_info) } {
Ok(alloc) => {
debug_assert!(alloc.offset() % requirements.layout.alignment().as_nonzero() == 0);
debug_assert!(is_aligned(alloc.offset(), requirements.layout.alignment()));
debug_assert!(alloc.size() == requirements.layout.size());

let inner = Arc::new(unsafe {
Expand Down Expand Up @@ -532,7 +532,7 @@ impl AttachmentImage {
)
} {
Ok(alloc) => {
debug_assert!(alloc.offset() % requirements.layout.alignment().as_nonzero() == 0);
debug_assert!(is_aligned(alloc.offset(), requirements.layout.alignment()));
debug_assert!(alloc.size() == requirements.layout.size());

let inner = Arc::new(unsafe {
Expand Down
4 changes: 2 additions & 2 deletions vulkano/src/image/immutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{
AllocationCreateInfo, AllocationCreationError, AllocationType,
MemoryAllocatePreference, MemoryAllocator, MemoryUsage,
},
DedicatedAllocation,
is_aligned, DedicatedAllocation,
},
sampler::Filter,
sync::Sharing,
Expand Down Expand Up @@ -149,7 +149,7 @@ impl ImmutableImage {

match unsafe { allocator.allocate_unchecked(create_info) } {
Ok(alloc) => {
debug_assert!(alloc.offset() % requirements.layout.alignment().as_nonzero() == 0);
debug_assert!(is_aligned(alloc.offset(), requirements.layout.alignment()));
debug_assert!(alloc.size() == requirements.layout.size());

let inner = Arc::new(unsafe {
Expand Down
6 changes: 3 additions & 3 deletions vulkano/src/image/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
AllocationCreateInfo, AllocationType, MemoryAllocatePreference, MemoryAllocator,
MemoryUsage,
},
DedicatedAllocation, DeviceMemoryError, ExternalMemoryHandleType,
is_aligned, DedicatedAllocation, DeviceMemoryError, ExternalMemoryHandleType,
ExternalMemoryHandleTypes,
},
sync::Sharing,
Expand Down Expand Up @@ -125,7 +125,7 @@ impl StorageImage {

match unsafe { allocator.allocate_unchecked(create_info) } {
Ok(alloc) => {
debug_assert!(alloc.offset() % requirements.layout.alignment().as_nonzero() == 0);
debug_assert!(is_aligned(alloc.offset(), requirements.layout.alignment()));
debug_assert!(alloc.size() == requirements.layout.size());

let inner = Arc::new(unsafe {
Expand Down Expand Up @@ -205,7 +205,7 @@ impl StorageImage {
)
} {
Ok(alloc) => {
debug_assert!(alloc.offset() % requirements.layout.alignment().as_nonzero() == 0);
debug_assert!(is_aligned(alloc.offset(), requirements.layout.alignment()));
debug_assert!(alloc.size() == requirements.layout.size());

let inner = Arc::new(unsafe {
Expand Down
Loading

0 comments on commit 127b3ea

Please # to comment.