Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Replace ImageAccess::descriptor_layouts() #2197

Merged
merged 6 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions examples/src/bin/dynamic-buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use vulkano::{
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, CommandBufferUsage,
},
descriptor_set::{
allocator::StandardDescriptorSetAllocator, layout::DescriptorType, DescriptorSet,
PersistentDescriptorSet, WriteDescriptorSet,
allocator::StandardDescriptorSetAllocator, layout::DescriptorType, DescriptorBufferInfo,
DescriptorSet, PersistentDescriptorSet, WriteDescriptorSet,
},
device::{
physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo,
Expand Down Expand Up @@ -226,8 +226,10 @@ fn main() {
// this range.
WriteDescriptorSet::buffer_with_range(
0,
input_buffer,
0..size_of::<cs::InData>() as DeviceSize,
DescriptorBufferInfo {
buffer: input_buffer,
range: 0..size_of::<cs::InData>() as DeviceSize,
},
),
WriteDescriptorSet::buffer(1, output_buffer.clone()),
],
Expand Down
66 changes: 43 additions & 23 deletions vulkano/src/command_buffer/commands/bind_push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ use crate::{
AutoCommandBufferBuilder,
},
descriptor_set::{
check_descriptor_write, layout::DescriptorType, sys::UnsafeDescriptorSet,
DescriptorBindingResources, DescriptorSetResources, DescriptorSetUpdateError,
DescriptorSetWithOffsets, DescriptorSetsCollection, DescriptorWriteInfo,
WriteDescriptorSet,
layout::DescriptorType, set_descriptor_write_image_layouts, sys::UnsafeDescriptorSet,
validate_descriptor_write, DescriptorBindingResources, DescriptorBufferInfo,
DescriptorSetResources, DescriptorSetUpdateError, DescriptorSetWithOffsets,
DescriptorSetsCollection, DescriptorWriteInfo, WriteDescriptorSet,
},
device::{DeviceOwned, QueueFlags},
memory::{is_aligned, DeviceAlignment},
Expand Down Expand Up @@ -194,7 +194,9 @@ where
});
}

if let Some((buffer, range)) = element {
if let Some(buffer_info) = element {
let DescriptorBufferInfo { buffer, range } = buffer_info;

// VUID-vkCmdBindDescriptorSets-pDescriptorSets-01979
if offset as DeviceSize + range.end > buffer.size() {
return Err(BindPushError::DynamicOffsetOutOfBufferBounds {
Expand Down Expand Up @@ -623,7 +625,15 @@ where
set_num: u32,
descriptor_writes: impl IntoIterator<Item = WriteDescriptorSet>,
) -> &mut Self {
let descriptor_writes: SmallVec<[_; 8]> = descriptor_writes.into_iter().collect();
let mut descriptor_writes: SmallVec<[_; 8]> = descriptor_writes.into_iter().collect();

// Set the image layouts
if let Some(set_layout) = pipeline_layout.set_layouts().get(set_num as usize) {
for write in &mut descriptor_writes {
set_descriptor_write_image_layouts(write, set_layout);
}
}

self.validate_push_descriptor_set(
pipeline_bind_point,
&pipeline_layout,
Expand Down Expand Up @@ -706,7 +716,7 @@ where
}

for write in descriptor_writes {
check_descriptor_write(write, descriptor_set_layout, 0)?;
validate_descriptor_write(write, descriptor_set_layout, 0)?;
}

Ok(())
Expand Down Expand Up @@ -901,9 +911,16 @@ impl SyncCommandBufferBuilder {
}
}

let descriptor_writes: SmallVec<[WriteDescriptorSet; 8]> =
let mut descriptor_writes: SmallVec<[WriteDescriptorSet; 8]> =
descriptor_writes.into_iter().collect();

// Set the image layouts
if let Some(set_layout) = pipeline_layout.set_layouts().get(set_num as usize) {
for write in &mut descriptor_writes {
set_descriptor_write_image_layouts(write, set_layout);
}
}

let state = self.current_state.invalidate_descriptor_sets(
pipeline_bind_point,
pipeline_layout.clone(),
Expand Down Expand Up @@ -1213,12 +1230,15 @@ impl UnsafeCommandBufferBuilder {
descriptor_writes: impl IntoIterator<Item = &'a WriteDescriptorSet>,
) {
debug_assert!(self.device.enabled_extensions().khr_push_descriptor);
let set_layout = &pipeline_layout.set_layouts()[set_num as usize];

let (infos, mut writes): (SmallVec<[_; 8]>, SmallVec<[_; 8]>) = descriptor_writes
let (infos_vk, mut writes_vk): (SmallVec<[_; 8]>, SmallVec<[_; 8]>) = descriptor_writes
.into_iter()
.map(|write| {
let binding =
&pipeline_layout.set_layouts()[set_num as usize].bindings()[&write.binding()];
let mut write = write.clone(); // Ew!
set_descriptor_write_image_layouts(&mut write, set_layout);

let binding = &set_layout.bindings()[&write.binding()];

(
write.to_vulkan_info(binding.descriptor_type),
Expand All @@ -1227,28 +1247,28 @@ impl UnsafeCommandBufferBuilder {
})
.unzip();

if writes.is_empty() {
if writes_vk.is_empty() {
return;
}

// Set the info pointers separately.
for (info, write) in infos.iter().zip(writes.iter_mut()) {
match info {
for (info_vk, write_vk) in infos_vk.iter().zip(writes_vk.iter_mut()) {
match info_vk {
DescriptorWriteInfo::Image(info) => {
write.descriptor_count = info.len() as u32;
write.p_image_info = info.as_ptr();
write_vk.descriptor_count = info.len() as u32;
write_vk.p_image_info = info.as_ptr();
}
DescriptorWriteInfo::Buffer(info) => {
write.descriptor_count = info.len() as u32;
write.p_buffer_info = info.as_ptr();
write_vk.descriptor_count = info.len() as u32;
write_vk.p_buffer_info = info.as_ptr();
}
DescriptorWriteInfo::BufferView(info) => {
write.descriptor_count = info.len() as u32;
write.p_texel_buffer_view = info.as_ptr();
write_vk.descriptor_count = info.len() as u32;
write_vk.p_texel_buffer_view = info.as_ptr();
}
}

debug_assert!(write.descriptor_count != 0);
debug_assert!(write_vk.descriptor_count != 0);
}

let fns = self.device.fns();
Expand All @@ -1258,8 +1278,8 @@ impl UnsafeCommandBufferBuilder {
pipeline_bind_point.into(),
pipeline_layout.handle(),
set_num,
writes.len() as u32,
writes.as_ptr(),
writes_vk.len() as u32,
writes_vk.as_ptr(),
);
}
}
Expand Down
77 changes: 54 additions & 23 deletions vulkano/src/command_buffer/commands/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ use crate::{
AutoCommandBufferBuilder, DispatchIndirectCommand, DrawIndexedIndirectCommand,
DrawIndirectCommand, ResourceInCommand, ResourceUseRef, SubpassContents,
},
descriptor_set::{layout::DescriptorType, DescriptorBindingResources},
descriptor_set::{
layout::DescriptorType, DescriptorBindingResources, DescriptorBufferInfo,
DescriptorImageViewInfo,
},
device::{DeviceOwned, QueueFlags},
format::{Format, FormatFeatures},
image::{
view::ImageViewType, ImageAccess, ImageAspects, ImageSubresourceRange, ImageViewAbstract,
SampleCount,
view::ImageViewType, ImageAccess, ImageAspects, ImageLayout, ImageSubresourceRange,
ImageViewAbstract, SampleCount,
},
pipeline::{
graphics::{
Expand Down Expand Up @@ -642,8 +645,7 @@ where
let layout_binding =
&pipeline.layout().set_layouts()[set_num as usize].bindings()[&binding_num];

let check_buffer =
|_index: u32, (_buffer, _range): &(Subbuffer<[u8]>, Range<DeviceSize>)| Ok(());
let check_buffer = |_index: u32, _buffer_info: &DescriptorBufferInfo| Ok(());

let check_buffer_view = |index: u32, buffer_view: &Arc<BufferView>| {
for desc_reqs in (binding_reqs.descriptors.get(&Some(index)).into_iter())
Expand Down Expand Up @@ -840,7 +842,12 @@ where
Ok(())
};

let check_image_view = |index: u32, image_view: &Arc<dyn ImageViewAbstract>| {
let check_image_view = |index: u32, image_view_info: &DescriptorImageViewInfo| {
let DescriptorImageViewInfo {
image_view,
image_layout: _,
} = image_view_info;

check_image_view_common(index, image_view)?;

if let Some(sampler) = layout_binding.immutable_samplers.get(index as usize) {
Expand All @@ -850,13 +857,21 @@ where
Ok(())
};

let check_image_view_sampler =
|index: u32, (image_view, sampler): &(Arc<dyn ImageViewAbstract>, Arc<Sampler>)| {
check_image_view_common(index, image_view)?;
check_sampler_common(index, sampler)?;
let check_image_view_sampler = |index: u32,
(image_view_info, sampler): &(
DescriptorImageViewInfo,
Arc<Sampler>,
)| {
let DescriptorImageViewInfo {
image_view,
image_layout: _,
} = image_view_info;

Ok(())
};
check_image_view_common(index, image_view)?;
check_sampler_common(index, sampler)?;

Ok(())
};

let check_sampler = |index: u32, sampler: &Arc<Sampler>| {
check_sampler_common(index, sampler)?;
Expand All @@ -881,7 +896,12 @@ where
})
});

for (id, image_view) in iter {
for (id, image_view_info) in iter {
let DescriptorImageViewInfo {
image_view,
image_layout: _,
} = image_view_info;

if let Err(error) = sampler.check_can_sample(image_view.as_ref()) {
return Err(
DescriptorResourceInvalidError::SamplerImageViewIncompatible {
Expand Down Expand Up @@ -2101,16 +2121,12 @@ impl SyncCommandBufferBuilder {
)
})
};
let image_resource = |(index, image, subresource_range): (
let image_resource = |(index, image, layout, subresource_range): (
u32,
Arc<dyn ImageAccess>,
ImageLayout,
ImageSubresourceRange,
)| {
let layout = image
.descriptor_layouts()
.expect("descriptor_layouts must return Some when used in an image view")
.layout_for(descriptor_type);

memory_iter(index).map(move |memory| {
(
ResourceUseRef {
Expand Down Expand Up @@ -2147,7 +2163,9 @@ impl SyncCommandBufferBuilder {
resources.extend(
(elements.iter().enumerate())
.filter_map(|(index, element)| {
element.as_ref().map(|(buffer, range)| {
element.as_ref().map(|buffer_info| {
let DescriptorBufferInfo { buffer, range } = buffer_info;

let dynamic_offset = dynamic_offsets[index] as DeviceSize;

(
Expand All @@ -2164,7 +2182,8 @@ impl SyncCommandBufferBuilder {
resources.extend(
(elements.iter().enumerate())
.filter_map(|(index, element)| {
element.as_ref().map(|(buffer, range)| {
element.as_ref().map(|buffer_info| {
let DescriptorBufferInfo { buffer, range } = buffer_info;
(index as u32, buffer.clone(), range.clone())
})
})
Expand All @@ -2191,10 +2210,16 @@ impl SyncCommandBufferBuilder {
resources.extend(
(elements.iter().enumerate())
.filter_map(|(index, element)| {
element.as_ref().map(|image_view| {
element.as_ref().map(|image_view_info| {
let &DescriptorImageViewInfo {
ref image_view,
image_layout,
} = image_view_info;

(
index as u32,
image_view.image(),
image_layout,
image_view.subresource_range().clone(),
)
})
Expand All @@ -2206,10 +2231,16 @@ impl SyncCommandBufferBuilder {
resources.extend(
(elements.iter().enumerate())
.filter_map(|(index, element)| {
element.as_ref().map(|(image_view, _)| {
element.as_ref().map(|(image_view_info, _sampler)| {
let &DescriptorImageViewInfo {
ref image_view,
image_layout,
} = image_view_info;

(
index as u32,
image_view.image(),
image_layout,
image_view.subresource_range().clone(),
)
})
Expand Down
Loading