From 63a53f1b93687ad927cd23ac03033e319f40241b Mon Sep 17 00:00:00 2001 From: marc0246 <40955683+marc0246@users.noreply.github.com> Date: Sun, 12 Nov 2023 17:17:37 +0100 Subject: [PATCH] Descriptor set revamp (#2404) * Descriptor set revamp * Fix tests * Fix examples * Add `DescriptorSet::update_unchecked` * Fix docs * Add `Debug` impls * Add `DescriptorSet::update_by_ref[_unchecked]` * Rename `UnsafeDescriptorSet` to `RawDescriptorSet` * Fix outdated docs for `DescriptorPool` --- examples/async-update/main.rs | 6 +- examples/basic-compute-shader/main.rs | 4 +- .../deferred/frame/ambient_lighting_system.rs | 4 +- .../frame/directional_lighting_system.rs | 4 +- .../deferred/frame/point_lighting_system.rs | 4 +- examples/dynamic-buffers/main.rs | 4 +- examples/dynamic-local-size/main.rs | 4 +- examples/gl-interop/main.rs | 4 +- examples/image-self-copy-blit/main.rs | 4 +- examples/image/main.rs | 4 +- examples/immutable-sampler/main.rs | 4 +- examples/indirect/main.rs | 4 +- .../fractal_compute_pipeline.rs | 4 +- .../pixels_draw_pipeline.rs | 6 +- .../multi-window-game-of-life/game_of_life.rs | 4 +- .../multi-window-game-of-life/pixels_draw.rs | 6 +- examples/push-constants/main.rs | 4 +- examples/runtime-array/main.rs | 6 +- examples/self-copy-buffer/main.rs | 4 +- examples/shader-include/main.rs | 4 +- examples/shader-types-sharing/main.rs | 4 +- examples/simple-particles/main.rs | 4 +- examples/specialization-constants/main.rs | 4 +- examples/teapot/main.rs | 4 +- examples/texture-array/main.rs | 4 +- vulkano/src/command_buffer/auto/builder.rs | 24 +- vulkano/src/command_buffer/auto/mod.rs | 8 +- .../src/command_buffer/commands/bind_push.rs | 3 +- .../src/command_buffer/commands/pipeline.rs | 35 +-- vulkano/src/descriptor_set/mod.rs | 275 ++++++++++++++---- vulkano/src/descriptor_set/persistent.rs | 151 ---------- vulkano/src/descriptor_set/pool.rs | 10 +- vulkano/src/descriptor_set/sys.rs | 37 +-- vulkano/src/descriptor_set/update.rs | 11 +- vulkano/src/image/sampler/ycbcr.rs | 4 +- vulkano/src/pipeline/compute.rs | 6 +- 36 files changed, 346 insertions(+), 326 deletions(-) delete mode 100644 vulkano/src/descriptor_set/persistent.rs diff --git a/examples/async-update/main.rs b/examples/async-update/main.rs index 3c529bb6..4276342e 100644 --- a/examples/async-update/main.rs +++ b/examples/async-update/main.rs @@ -47,7 +47,7 @@ use vulkano::{ PrimaryCommandBufferAbstract, RenderPassBeginInfo, }, descriptor_set::{ - allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet, + allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet, }, device::{ physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, Queue, @@ -486,7 +486,7 @@ fn main() -> Result<(), impl Error> { let uniform_buffer_sets = uniform_buffers .iter() .map(|buffer| { - PersistentDescriptorSet::new( + DescriptorSet::new( descriptor_set_allocator.clone(), pipeline.layout().set_layouts()[0].clone(), [WriteDescriptorSet::buffer(0, buffer.clone())], @@ -499,7 +499,7 @@ fn main() -> Result<(), impl Error> { // Create the descriptor sets for sampling the textures. let sampler = Sampler::new(device.clone(), SamplerCreateInfo::simple_repeat_linear()).unwrap(); let sampler_sets = textures.map(|texture| { - PersistentDescriptorSet::new( + DescriptorSet::new( descriptor_set_allocator.clone(), pipeline.layout().set_layouts()[1].clone(), [ diff --git a/examples/basic-compute-shader/main.rs b/examples/basic-compute-shader/main.rs index 6df2c4f6..2259423f 100644 --- a/examples/basic-compute-shader/main.rs +++ b/examples/basic-compute-shader/main.rs @@ -11,7 +11,7 @@ use vulkano::{ allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, CommandBufferUsage, }, descriptor_set::{ - allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet, + allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet, }, device::{ physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo, @@ -184,7 +184,7 @@ fn main() { // If you want to run the pipeline on multiple different buffers, you need to create multiple // descriptor sets that each contain the buffer you want to run the shader on. let layout = pipeline.layout().set_layouts().get(0).unwrap(); - let set = PersistentDescriptorSet::new( + let set = DescriptorSet::new( descriptor_set_allocator, layout.clone(), [WriteDescriptorSet::buffer(0, data_buffer.clone())], diff --git a/examples/deferred/frame/ambient_lighting_system.rs b/examples/deferred/frame/ambient_lighting_system.rs index f744e10f..9264d974 100644 --- a/examples/deferred/frame/ambient_lighting_system.rs +++ b/examples/deferred/frame/ambient_lighting_system.rs @@ -6,7 +6,7 @@ use vulkano::{ CommandBufferInheritanceInfo, CommandBufferUsage, SecondaryAutoCommandBuffer, }, descriptor_set::{ - allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet, + allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet, }, device::Queue, image::view::ImageView, @@ -168,7 +168,7 @@ impl AmbientLightingSystem { }; let layout = self.pipeline.layout().set_layouts().get(0).unwrap(); - let descriptor_set = PersistentDescriptorSet::new( + let descriptor_set = DescriptorSet::new( self.descriptor_set_allocator.clone(), layout.clone(), [WriteDescriptorSet::image_view(0, color_input)], diff --git a/examples/deferred/frame/directional_lighting_system.rs b/examples/deferred/frame/directional_lighting_system.rs index 5c9176d1..df6ea66d 100644 --- a/examples/deferred/frame/directional_lighting_system.rs +++ b/examples/deferred/frame/directional_lighting_system.rs @@ -7,7 +7,7 @@ use vulkano::{ CommandBufferInheritanceInfo, CommandBufferUsage, SecondaryAutoCommandBuffer, }, descriptor_set::{ - allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet, + allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet, }, device::Queue, image::view::ImageView, @@ -179,7 +179,7 @@ impl DirectionalLightingSystem { }; let layout = self.pipeline.layout().set_layouts().get(0).unwrap(); - let descriptor_set = PersistentDescriptorSet::new( + let descriptor_set = DescriptorSet::new( self.descriptor_set_allocator.clone(), layout.clone(), [ diff --git a/examples/deferred/frame/point_lighting_system.rs b/examples/deferred/frame/point_lighting_system.rs index 27752d14..e21f8eda 100644 --- a/examples/deferred/frame/point_lighting_system.rs +++ b/examples/deferred/frame/point_lighting_system.rs @@ -7,7 +7,7 @@ use vulkano::{ CommandBufferInheritanceInfo, CommandBufferUsage, SecondaryAutoCommandBuffer, }, descriptor_set::{ - allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet, + allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet, }, device::Queue, image::view::ImageView, @@ -191,7 +191,7 @@ impl PointLightingSystem { }; let layout = self.pipeline.layout().set_layouts().get(0).unwrap(); - let descriptor_set = PersistentDescriptorSet::new( + let descriptor_set = DescriptorSet::new( self.descriptor_set_allocator.clone(), layout.clone(), [ diff --git a/examples/dynamic-buffers/main.rs b/examples/dynamic-buffers/main.rs index 34d5a910..4786a030 100644 --- a/examples/dynamic-buffers/main.rs +++ b/examples/dynamic-buffers/main.rs @@ -12,7 +12,7 @@ use vulkano::{ }, descriptor_set::{ allocator::StandardDescriptorSetAllocator, layout::DescriptorType, DescriptorBufferInfo, - DescriptorSet, PersistentDescriptorSet, WriteDescriptorSet, + DescriptorSet, WriteDescriptorSet, }, device::{ physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo, @@ -212,7 +212,7 @@ fn main() { .unwrap(); let layout = pipeline.layout().set_layouts().get(0).unwrap(); - let set = PersistentDescriptorSet::new( + let set = DescriptorSet::new( descriptor_set_allocator, layout.clone(), [ diff --git a/examples/dynamic-local-size/main.rs b/examples/dynamic-local-size/main.rs index 07c055b6..8f448e52 100644 --- a/examples/dynamic-local-size/main.rs +++ b/examples/dynamic-local-size/main.rs @@ -12,7 +12,7 @@ use vulkano::{ CopyImageToBufferInfo, }, descriptor_set::{ - allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet, + allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet, }, device::{ physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo, @@ -224,7 +224,7 @@ fn main() { let view = ImageView::new_default(image.clone()).unwrap(); let layout = pipeline.layout().set_layouts().get(0).unwrap(); - let set = PersistentDescriptorSet::new( + let set = DescriptorSet::new( descriptor_set_allocator, layout.clone(), [WriteDescriptorSet::image_view(0, view)], diff --git a/examples/gl-interop/main.rs b/examples/gl-interop/main.rs index ad415eb6..3efcbfb1 100644 --- a/examples/gl-interop/main.rs +++ b/examples/gl-interop/main.rs @@ -24,7 +24,7 @@ mod linux { CommandBufferUsage, RenderPassBeginInfo, SemaphoreSubmitInfo, SubmitInfo, }, descriptor_set::{ - allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet, + allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet, }, device::{ physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, Queue, @@ -278,7 +278,7 @@ mod linux { let layout = pipeline.layout().set_layouts().get(0).unwrap(); - let set = PersistentDescriptorSet::new( + let set = DescriptorSet::new( descriptor_set_allocator, layout.clone(), [ diff --git a/examples/image-self-copy-blit/main.rs b/examples/image-self-copy-blit/main.rs index 112f3bae..bd59fd68 100644 --- a/examples/image-self-copy-blit/main.rs +++ b/examples/image-self-copy-blit/main.rs @@ -7,7 +7,7 @@ use vulkano::{ CopyImageInfo, ImageBlit, ImageCopy, PrimaryCommandBufferAbstract, RenderPassBeginInfo, }, descriptor_set::{ - allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet, + allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet, }, device::{ physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo, @@ -380,7 +380,7 @@ fn main() -> Result<(), impl Error> { }; let layout = pipeline.layout().set_layouts().get(0).unwrap(); - let set = PersistentDescriptorSet::new( + let set = DescriptorSet::new( descriptor_set_allocator, layout.clone(), [ diff --git a/examples/image/main.rs b/examples/image/main.rs index 08c2f5f7..276d94d8 100644 --- a/examples/image/main.rs +++ b/examples/image/main.rs @@ -6,7 +6,7 @@ use vulkano::{ CopyBufferToImageInfo, PrimaryCommandBufferAbstract, RenderPassBeginInfo, }, descriptor_set::{ - allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet, + allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet, }, device::{ physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo, @@ -328,7 +328,7 @@ fn main() -> Result<(), impl Error> { }; let layout = pipeline.layout().set_layouts().get(0).unwrap(); - let set = PersistentDescriptorSet::new( + let set = DescriptorSet::new( descriptor_set_allocator, layout.clone(), [ diff --git a/examples/immutable-sampler/main.rs b/examples/immutable-sampler/main.rs index 3cbc34e0..c00ac73c 100644 --- a/examples/immutable-sampler/main.rs +++ b/examples/immutable-sampler/main.rs @@ -15,7 +15,7 @@ use vulkano::{ CopyBufferToImageInfo, PrimaryCommandBufferAbstract, RenderPassBeginInfo, }, descriptor_set::{ - allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet, + allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet, }, device::{ physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo, @@ -349,7 +349,7 @@ fn main() -> Result<(), impl Error> { // Use `image_view` instead of `image_view_sampler`, since the sampler is already in the // layout. - let set = PersistentDescriptorSet::new( + let set = DescriptorSet::new( descriptor_set_allocator, layout.clone(), [WriteDescriptorSet::image_view(1, texture)], diff --git a/examples/indirect/main.rs b/examples/indirect/main.rs index 14851f3a..1ffd7b7b 100644 --- a/examples/indirect/main.rs +++ b/examples/indirect/main.rs @@ -25,7 +25,7 @@ use vulkano::{ DrawIndirectCommand, RenderPassBeginInfo, }, descriptor_set::{ - allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet, + allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet, }, device::{ physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo, @@ -454,7 +454,7 @@ fn main() -> Result<(), impl Error> { // Pass the two buffers to the compute shader. let layout = compute_pipeline.layout().set_layouts().get(0).unwrap(); - let cs_desciptor_set = PersistentDescriptorSet::new( + let cs_desciptor_set = DescriptorSet::new( descriptor_set_allocator.clone(), layout.clone(), [ diff --git a/examples/interactive-fractal/fractal_compute_pipeline.rs b/examples/interactive-fractal/fractal_compute_pipeline.rs index e076c828..1423ca22 100644 --- a/examples/interactive-fractal/fractal_compute_pipeline.rs +++ b/examples/interactive-fractal/fractal_compute_pipeline.rs @@ -8,7 +8,7 @@ use vulkano::{ PrimaryCommandBufferAbstract, }, descriptor_set::{ - allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet, + allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet, }, device::Queue, image::view::ImageView, @@ -138,7 +138,7 @@ impl FractalComputePipeline { let image_extent = image_view.image().extent(); let pipeline_layout = self.pipeline.layout(); let desc_layout = pipeline_layout.set_layouts().get(0).unwrap(); - let set = PersistentDescriptorSet::new( + let set = DescriptorSet::new( self.descriptor_set_allocator.clone(), desc_layout.clone(), [ diff --git a/examples/interactive-fractal/pixels_draw_pipeline.rs b/examples/interactive-fractal/pixels_draw_pipeline.rs index a034375c..9f64188f 100644 --- a/examples/interactive-fractal/pixels_draw_pipeline.rs +++ b/examples/interactive-fractal/pixels_draw_pipeline.rs @@ -6,7 +6,7 @@ use vulkano::{ CommandBufferInheritanceInfo, CommandBufferUsage, SecondaryAutoCommandBuffer, }, descriptor_set::{ - allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet, + allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet, }, device::Queue, image::{ @@ -172,7 +172,7 @@ impl PixelsDrawPipeline { } } - fn create_descriptor_set(&self, image: Arc) -> Arc { + fn create_descriptor_set(&self, image: Arc) -> Arc { let layout = self.pipeline.layout().set_layouts().get(0).unwrap(); let sampler = Sampler::new( self.gfx_queue.device().clone(), @@ -186,7 +186,7 @@ impl PixelsDrawPipeline { ) .unwrap(); - PersistentDescriptorSet::new( + DescriptorSet::new( self.descriptor_set_allocator.clone(), layout.clone(), [ diff --git a/examples/multi-window-game-of-life/game_of_life.rs b/examples/multi-window-game-of-life/game_of_life.rs index 6978c9c4..484cee5d 100644 --- a/examples/multi-window-game-of-life/game_of_life.rs +++ b/examples/multi-window-game-of-life/game_of_life.rs @@ -9,7 +9,7 @@ use vulkano::{ PrimaryAutoCommandBuffer, }, descriptor_set::{ - allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet, + allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet, }, device::Queue, format::Format, @@ -173,7 +173,7 @@ impl GameOfLifeComputePipeline { let image_extent = self.image.image().extent(); let pipeline_layout = self.compute_life_pipeline.layout(); let desc_layout = pipeline_layout.set_layouts().get(0).unwrap(); - let set = PersistentDescriptorSet::new( + let set = DescriptorSet::new( self.descriptor_set_allocator.clone(), desc_layout.clone(), [ diff --git a/examples/multi-window-game-of-life/pixels_draw.rs b/examples/multi-window-game-of-life/pixels_draw.rs index 7e9628b5..14b8b497 100644 --- a/examples/multi-window-game-of-life/pixels_draw.rs +++ b/examples/multi-window-game-of-life/pixels_draw.rs @@ -7,7 +7,7 @@ use vulkano::{ CommandBufferInheritanceInfo, CommandBufferUsage, SecondaryAutoCommandBuffer, }, descriptor_set::{ - allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet, + allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet, }, device::Queue, image::{ @@ -168,7 +168,7 @@ impl PixelsDrawPipeline { } } - fn create_image_sampler_nearest(&self, image: Arc) -> Arc { + fn create_image_sampler_nearest(&self, image: Arc) -> Arc { let layout = self.pipeline.layout().set_layouts().get(0).unwrap(); let sampler = Sampler::new( self.gfx_queue.device().clone(), @@ -182,7 +182,7 @@ impl PixelsDrawPipeline { ) .unwrap(); - PersistentDescriptorSet::new( + DescriptorSet::new( self.descriptor_set_allocator.clone(), layout.clone(), [ diff --git a/examples/push-constants/main.rs b/examples/push-constants/main.rs index 36b18150..9b6ec973 100644 --- a/examples/push-constants/main.rs +++ b/examples/push-constants/main.rs @@ -10,7 +10,7 @@ use vulkano::{ allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, CommandBufferUsage, }, descriptor_set::{ - allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet, + allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet, }, device::{ physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo, @@ -156,7 +156,7 @@ fn main() { .unwrap(); let layout = pipeline.layout().set_layouts().get(0).unwrap(); - let set = PersistentDescriptorSet::new( + let set = DescriptorSet::new( descriptor_set_allocator, layout.clone(), [WriteDescriptorSet::buffer(0, data_buffer.clone())], diff --git a/examples/runtime-array/main.rs b/examples/runtime-array/main.rs index fabfcb79..4631d845 100644 --- a/examples/runtime-array/main.rs +++ b/examples/runtime-array/main.rs @@ -6,8 +6,8 @@ use vulkano::{ CopyBufferToImageInfo, PrimaryCommandBufferAbstract, RenderPassBeginInfo, }, descriptor_set::{ - allocator::StandardDescriptorSetAllocator, layout::DescriptorBindingFlags, - PersistentDescriptorSet, WriteDescriptorSet, + allocator::StandardDescriptorSetAllocator, layout::DescriptorBindingFlags, DescriptorSet, + WriteDescriptorSet, }, device::{ physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, Features, @@ -448,7 +448,7 @@ fn main() -> Result<(), impl Error> { }; let layout = pipeline.layout().set_layouts().get(0).unwrap(); - let set = PersistentDescriptorSet::new_variable( + let set = DescriptorSet::new_variable( descriptor_set_allocator, layout.clone(), 2, diff --git a/examples/self-copy-buffer/main.rs b/examples/self-copy-buffer/main.rs index c29e4439..973caba2 100644 --- a/examples/self-copy-buffer/main.rs +++ b/examples/self-copy-buffer/main.rs @@ -10,7 +10,7 @@ use vulkano::{ CommandBufferUsage, CopyBufferInfoTyped, }, descriptor_set::{ - allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet, + allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet, }, device::{ physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo, @@ -152,7 +152,7 @@ fn main() { .unwrap(); let layout = pipeline.layout().set_layouts().get(0).unwrap(); - let set = PersistentDescriptorSet::new( + let set = DescriptorSet::new( descriptor_set_allocator, layout.clone(), [WriteDescriptorSet::buffer(0, data_buffer.clone())], diff --git a/examples/shader-include/main.rs b/examples/shader-include/main.rs index a1b1f4fc..f34bc91e 100644 --- a/examples/shader-include/main.rs +++ b/examples/shader-include/main.rs @@ -9,7 +9,7 @@ use vulkano::{ allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, CommandBufferUsage, }, descriptor_set::{ - allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet, + allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet, }, device::{ physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo, @@ -156,7 +156,7 @@ fn main() { .unwrap(); let layout = pipeline.layout().set_layouts().get(0).unwrap(); - let set = PersistentDescriptorSet::new( + let set = DescriptorSet::new( descriptor_set_allocator, layout.clone(), [WriteDescriptorSet::buffer(0, data_buffer.clone())], diff --git a/examples/shader-types-sharing/main.rs b/examples/shader-types-sharing/main.rs index 0f131e06..e14470ed 100644 --- a/examples/shader-types-sharing/main.rs +++ b/examples/shader-types-sharing/main.rs @@ -23,7 +23,7 @@ use vulkano::{ allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, CommandBufferUsage, }, descriptor_set::{ - allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet, + allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet, }, device::{ physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, Queue, @@ -184,7 +184,7 @@ fn main() { descriptor_set_allocator: Arc, ) { let layout = pipeline.layout().set_layouts().get(0).unwrap(); - let set = PersistentDescriptorSet::new( + let set = DescriptorSet::new( descriptor_set_allocator, layout.clone(), [WriteDescriptorSet::buffer(0, data_buffer)], diff --git a/examples/simple-particles/main.rs b/examples/simple-particles/main.rs index 4be9f1dc..945ea3aa 100644 --- a/examples/simple-particles/main.rs +++ b/examples/simple-particles/main.rs @@ -11,7 +11,7 @@ use vulkano::{ CopyBufferInfo, PrimaryCommandBufferAbstract, RenderPassBeginInfo, }, descriptor_set::{ - allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet, + allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet, }, device::{ physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo, @@ -432,7 +432,7 @@ fn main() -> Result<(), impl Error> { // Create a new descriptor set for binding vertices as a storage buffer. use vulkano::pipeline::Pipeline; // Required to access the `layout` method of pipeline. - let descriptor_set = PersistentDescriptorSet::new( + let descriptor_set = DescriptorSet::new( descriptor_set_allocator.clone(), compute_pipeline .layout() diff --git a/examples/specialization-constants/main.rs b/examples/specialization-constants/main.rs index dde61a8d..53eeb529 100644 --- a/examples/specialization-constants/main.rs +++ b/examples/specialization-constants/main.rs @@ -7,7 +7,7 @@ use vulkano::{ allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, CommandBufferUsage, }, descriptor_set::{ - allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet, + allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet, }, device::{ physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo, @@ -157,7 +157,7 @@ fn main() { .unwrap(); let layout = pipeline.layout().set_layouts().get(0).unwrap(); - let set = PersistentDescriptorSet::new( + let set = DescriptorSet::new( descriptor_set_allocator, layout.clone(), [WriteDescriptorSet::buffer(0, data_buffer.clone())], diff --git a/examples/teapot/main.rs b/examples/teapot/main.rs index 37282026..d23447d8 100644 --- a/examples/teapot/main.rs +++ b/examples/teapot/main.rs @@ -11,7 +11,7 @@ use vulkano::{ RenderPassBeginInfo, }, descriptor_set::{ - allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet, + allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet, }, device::{ physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, DeviceOwned, @@ -341,7 +341,7 @@ fn main() -> Result<(), impl Error> { }; let layout = pipeline.layout().set_layouts().get(0).unwrap(); - let set = PersistentDescriptorSet::new( + let set = DescriptorSet::new( descriptor_set_allocator.clone(), layout.clone(), [WriteDescriptorSet::buffer(0, uniform_buffer_subbuffer)], diff --git a/examples/texture-array/main.rs b/examples/texture-array/main.rs index 9cfa983a..46c0e1b8 100644 --- a/examples/texture-array/main.rs +++ b/examples/texture-array/main.rs @@ -6,7 +6,7 @@ use vulkano::{ CopyBufferToImageInfo, PrimaryCommandBufferAbstract, RenderPassBeginInfo, }, descriptor_set::{ - allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet, + allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet, }, device::{ physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo, @@ -339,7 +339,7 @@ fn main() -> Result<(), impl Error> { }; let layout = pipeline.layout().set_layouts().get(0).unwrap(); - let set = PersistentDescriptorSet::new( + let set = DescriptorSet::new( descriptor_set_allocator, layout.clone(), [ diff --git a/vulkano/src/command_buffer/auto/builder.rs b/vulkano/src/command_buffer/auto/builder.rs index f3d76689..8625910e 100644 --- a/vulkano/src/command_buffer/auto/builder.rs +++ b/vulkano/src/command_buffer/auto/builder.rs @@ -39,7 +39,7 @@ use crate::{ DeviceSize, Validated, ValidationError, VulkanError, }; use ahash::HashMap; -use parking_lot::Mutex; +use parking_lot::{Mutex, RwLockReadGuard}; use smallvec::SmallVec; use std::{ collections::hash_map::Entry, @@ -1730,10 +1730,10 @@ pub(in crate::command_buffer) enum SetOrPush { } impl SetOrPush { - pub(in crate::command_buffer) fn resources(&self) -> &DescriptorSetResources { + pub(in crate::command_buffer) fn resources(&self) -> SetOrPushResources<'_> { match self { - Self::Set(set) => set.as_ref().0.resources(), - Self::Push(resources) => resources, + Self::Set(set) => SetOrPushResources::Set(set.as_ref().0.resources()), + Self::Push(resources) => SetOrPushResources::Push(resources), } } @@ -1746,6 +1746,22 @@ impl SetOrPush { } } +pub(in crate::command_buffer) enum SetOrPushResources<'a> { + Set(RwLockReadGuard<'a, DescriptorSetResources>), + Push(&'a DescriptorSetResources), +} + +impl std::ops::Deref for SetOrPushResources<'_> { + type Target = DescriptorSetResources; + + fn deref(&self) -> &Self::Target { + match self { + Self::Set(guard) => guard, + Self::Push(r) => r, + } + } +} + #[derive(Clone, Copy, Debug, Default)] pub(in crate::command_buffer) struct StencilStateDynamic { pub(in crate::command_buffer) front: Option, diff --git a/vulkano/src/command_buffer/auto/mod.rs b/vulkano/src/command_buffer/auto/mod.rs index 0205fb65..4d81be86 100644 --- a/vulkano/src/command_buffer/auto/mod.rs +++ b/vulkano/src/command_buffer/auto/mod.rs @@ -326,7 +326,7 @@ mod tests { DescriptorSetLayout, DescriptorSetLayoutBinding, DescriptorSetLayoutCreateInfo, DescriptorType, }, - PersistentDescriptorSet, WriteDescriptorSet, + DescriptorSet, WriteDescriptorSet, }, device::{Device, DeviceCreateInfo, QueueCreateInfo}, image::sampler::{Sampler, SamplerCreateInfo}, @@ -785,7 +785,7 @@ mod tests { Default::default(), )); - let set = PersistentDescriptorSet::new( + let set = DescriptorSet::new( ds_allocator.clone(), set_layout.clone(), [WriteDescriptorSet::sampler( @@ -856,8 +856,8 @@ mod tests { ) .unwrap(); - let set = PersistentDescriptorSet::new( - ds_allocator.clone(), + let set = DescriptorSet::new( + ds_allocator, set_layout, [WriteDescriptorSet::sampler( 0, diff --git a/vulkano/src/command_buffer/commands/bind_push.rs b/vulkano/src/command_buffer/commands/bind_push.rs index 62865c6c..807aee4f 100644 --- a/vulkano/src/command_buffer/commands/bind_push.rs +++ b/vulkano/src/command_buffer/commands/bind_push.rs @@ -582,7 +582,8 @@ where let dynamic_offsets = &dynamic_offsets_remaining[..split_index]; dynamic_offsets_remaining = &dynamic_offsets_remaining[split_index..]; - let elements = match set.resources().binding(binding_num) { + let resources = set.resources(); + let elements = match resources.binding(binding_num) { Some(DescriptorBindingResources::Buffer(elements)) => elements.as_slice(), _ => unreachable!(), }; diff --git a/vulkano/src/command_buffer/commands/pipeline.rs b/vulkano/src/command_buffer/commands/pipeline.rs index c6b4356b..f98fa2d2 100644 --- a/vulkano/src/command_buffer/commands/pipeline.rs +++ b/vulkano/src/command_buffer/commands/pipeline.rs @@ -1279,28 +1279,29 @@ where { // Check sampler-image compatibility. Only done for separate samplers; // combined image samplers are checked when updating the descriptor set. - - // If the image view isn't actually present in the resources, then just skip it. - // It will be caught later by check_resources. - let iter = desc_reqs.sampler_with_images.iter().filter_map(|id| { - descriptor_set_state - .descriptor_sets - .get(&id.set) - .and_then(|set| set.resources().binding(id.binding)) - .and_then(|res| match res { - DescriptorBindingResources::ImageView(elements) => elements - .get(id.index as usize) - .and_then(|opt| opt.as_ref().map(|opt| (id, opt))), - _ => None, - }) - }); - - for (id, image_view_info) in iter { + for id in &desc_reqs.sampler_with_images { let DescriptorIdentifier { set: iset_num, binding: ibinding_num, index: iindex, } = id; + + // If the image view isn't actually present in the resources, then just + // skip it. It will be caught later by check_resources. + let Some(set) = descriptor_set_state.descriptor_sets.get(iset_num) + else { + continue; + }; + let resources = set.resources(); + let Some(DescriptorBindingResources::ImageView(elements)) = + resources.binding(*ibinding_num) + else { + continue; + }; + let Some(Some(image_view_info)) = elements.get(*iindex as usize) else { + continue; + }; + let DescriptorImageViewInfo { image_view, image_layout: _, diff --git a/vulkano/src/descriptor_set/mod.rs b/vulkano/src/descriptor_set/mod.rs index 44d4ab00..cd5dedc4 100644 --- a/vulkano/src/descriptor_set/mod.rs +++ b/vulkano/src/descriptor_set/mod.rs @@ -23,7 +23,7 @@ //! //! ## Creating a descriptor set //! -//! TODO: write example for: PersistentDescriptorSet::start(layout.clone()).add_buffer(data_buffer.clone()) +//! TODO: write //! //! ## Passing the descriptor set when drawing //! @@ -31,9 +31,8 @@ //! //! # When drawing //! -//! When you call a function that adds a draw command to a command buffer, one of the parameters -//! corresponds to the list of descriptor sets to use. Vulkano will check that what you passed is -//! compatible with the layout of the pipeline. +//! When you call a function that adds a draw command to a command buffer, vulkano will check that +//! the descriptor sets you bound are compatible with the layout of the pipeline. //! //! TODO: talk about perfs of changing sets //! @@ -41,59 +40,60 @@ //! //! There are three concepts in Vulkan related to descriptor sets: //! -//! - A `DescriptorSetLayout` is a Vulkan object that describes to the Vulkan implementation the +//! - A `VkDescriptorSetLayout` is a Vulkan object that describes to the Vulkan implementation the //! layout of a future descriptor set. When you allocate a descriptor set, you have to pass an //! instance of this object. This is represented with the [`DescriptorSetLayout`] type in //! vulkano. -//! - A `DescriptorPool` is a Vulkan object that holds the memory of descriptor sets and that can +//! - A `VkDescriptorPool` is a Vulkan object that holds the memory of descriptor sets and that can //! be used to allocate and free individual descriptor sets. This is represented with the //! [`DescriptorPool`] type in vulkano. -//! - A `DescriptorSet` contains the bindings to resources and is allocated from a pool. This is -//! represented with the [`UnsafeDescriptorSet`] type in vulkano. +//! - A `VkDescriptorSet` contains the bindings to resources and is allocated from a pool. This is +//! represented with the [`RawDescriptorSet`] type in vulkano. //! //! In addition to this, vulkano defines the following: //! //! - The [`DescriptorSetAllocator`] trait can be implemented on types from which you can allocate //! and free descriptor sets. However it is different from Vulkan descriptor pools in the sense -//! that an implementation of the [`DescriptorSetAllocator`] trait can manage multiple Vulkan +//! that an implementation of the `DescriptorSetAllocator` trait can manage multiple Vulkan //! descriptor pools. //! - The [`StandardDescriptorSetAllocator`] type is a default implementation of the //! [`DescriptorSetAllocator`] trait. -//! - The [`DescriptorSet`] trait is implemented on types that wrap around Vulkan descriptor sets in -//! a safe way. A Vulkan descriptor set is inherently unsafe, so we need safe wrappers around -//! them. -//! - The [`DescriptorSetsCollection`] trait is implemented on collections of types that implement -//! [`DescriptorSet`]. It is what you pass to the draw functions. +//! - The [`DescriptorSet`] type wraps around an `RawDescriptorSet` a safe way. A Vulkan descriptor +//! set is inherently unsafe, so we need safe wrappers around them. +//! - The [`DescriptorSetsCollection`] trait is implemented on collections of descriptor sets. It +//! is what you pass to the bind function. //! //! [`DescriptorPool`]: pool::DescriptorPool -//! [`UnsafeDescriptorSet`]: sys::UnsafeDescriptorSet +//! [`RawDescriptorSet`]: sys::RawDescriptorSet //! [`DescriptorSetAllocator`]: allocator::DescriptorSetAllocator //! [`StandardDescriptorSetAllocator`]: allocator::StandardDescriptorSetAllocator pub(crate) use self::update::DescriptorWriteInfo; +use self::{ + allocator::DescriptorSetAllocator, + layout::DescriptorSetLayout, + pool::{DescriptorPool, DescriptorPoolAlloc}, + sys::RawDescriptorSet, +}; pub use self::{ collection::DescriptorSetsCollection, - persistent::PersistentDescriptorSet, update::{ CopyDescriptorSet, DescriptorBufferInfo, DescriptorImageViewInfo, WriteDescriptorSet, WriteDescriptorSetElements, }, }; -use self::{ - layout::DescriptorSetLayout, - pool::{DescriptorPool, DescriptorPoolAlloc}, -}; use crate::{ acceleration_structure::AccelerationStructure, buffer::view::BufferView, descriptor_set::layout::{ DescriptorBindingFlags, DescriptorSetLayoutCreateFlags, DescriptorType, }, - device::DeviceOwned, + device::{Device, DeviceOwned}, image::{sampler::Sampler, ImageLayout}, - VulkanObject, + Validated, ValidationError, VulkanError, VulkanObject, }; use ahash::HashMap; +use parking_lot::{RwLock, RwLockReadGuard}; use smallvec::{smallvec, SmallVec}; use std::{ hash::{Hash, Hasher}, @@ -103,67 +103,225 @@ use std::{ pub mod allocator; mod collection; pub mod layout; -pub mod persistent; pub mod pool; pub mod sys; mod update; -/// Trait for objects that contain a collection of resources that will be accessible by shaders. +/// An object that contains a collection of resources that will be accessible by shaders. /// -/// Objects of this type can be passed when submitting a draw command. -pub unsafe trait DescriptorSet: - VulkanObject + DeviceOwned + Send + Sync -{ +/// Descriptor sets can be bound when recording a command buffer. +#[derive(Debug)] +pub struct DescriptorSet { + inner: RawDescriptorSet, + resources: RwLock, +} + +impl DescriptorSet { + /// Creates and returns a new descriptor set with a variable descriptor count of 0. + pub fn new( + allocator: Arc, + layout: Arc, + descriptor_writes: impl IntoIterator, + descriptor_copies: impl IntoIterator, + ) -> Result, Validated> { + Self::new_variable(allocator, layout, 0, descriptor_writes, descriptor_copies) + } + + /// Creates and returns a new descriptor set with the requested variable descriptor count. + pub fn new_variable( + allocator: Arc, + layout: Arc, + variable_descriptor_count: u32, + descriptor_writes: impl IntoIterator, + descriptor_copies: impl IntoIterator, + ) -> Result, Validated> { + let mut set = DescriptorSet { + inner: RawDescriptorSet::new(allocator, &layout, variable_descriptor_count)?, + resources: RwLock::new(DescriptorSetResources::new( + &layout, + variable_descriptor_count, + )), + }; + + set.update(descriptor_writes, descriptor_copies)?; + + Ok(Arc::new(set)) + } + /// Returns the allocation of the descriptor set. - fn alloc(&self) -> &DescriptorPoolAlloc; + #[inline] + pub fn alloc(&self) -> &DescriptorPoolAlloc { + &self.inner.alloc().inner + } /// Returns the descriptor pool that the descriptor set was allocated from. - fn pool(&self) -> &DescriptorPool; + #[inline] + pub fn pool(&self) -> &DescriptorPool { + self.inner.pool() + } /// Returns the layout of this descriptor set. #[inline] - fn layout(&self) -> &Arc { + pub fn layout(&self) -> &Arc { self.alloc().layout() } /// Returns the variable descriptor count that this descriptor set was allocated with. #[inline] - fn variable_descriptor_count(&self) -> u32 { + pub fn variable_descriptor_count(&self) -> u32 { self.alloc().variable_descriptor_count() } /// Creates a [`DescriptorSetWithOffsets`] with the given dynamic offsets. - fn offsets( + pub fn offsets( self: Arc, dynamic_offsets: impl IntoIterator, - ) -> DescriptorSetWithOffsets - where - Self: Sized + 'static, - { + ) -> DescriptorSetWithOffsets { DescriptorSetWithOffsets::new(self, dynamic_offsets) } /// Returns the resources bound to this descriptor set. - fn resources(&self) -> &DescriptorSetResources; -} - -impl PartialEq for dyn DescriptorSet { #[inline] - fn eq(&self, other: &Self) -> bool { - self.alloc() == other.alloc() + pub fn resources(&self) -> RwLockReadGuard<'_, DescriptorSetResources> { + self.resources.read() + } + + /// Updates the descriptor set with new values. + pub fn update( + &mut self, + descriptor_writes: impl IntoIterator, + descriptor_copies: impl IntoIterator, + ) -> Result<(), Box> { + let descriptor_writes: SmallVec<[_; 8]> = descriptor_writes.into_iter().collect(); + let descriptor_copies: SmallVec<[_; 8]> = descriptor_copies.into_iter().collect(); + + self.inner + .validate_update(&descriptor_writes, &descriptor_copies)?; + + unsafe { + Self::update_inner( + &self.inner, + self.resources.get_mut(), + &descriptor_writes, + &descriptor_copies, + ); + } + + Ok(()) + } + + #[cfg_attr(not(feature = "document_unchecked"), doc(hidden))] + pub unsafe fn update_unchecked( + &mut self, + descriptor_writes: impl IntoIterator, + descriptor_copies: impl IntoIterator, + ) { + let descriptor_writes: SmallVec<[_; 8]> = descriptor_writes.into_iter().collect(); + let descriptor_copies: SmallVec<[_; 8]> = descriptor_copies.into_iter().collect(); + + Self::update_inner( + &self.inner, + self.resources.get_mut(), + &descriptor_writes, + &descriptor_copies, + ); + } + + /// Updates the descriptor set with new values. + /// + /// # Safety + /// + /// - Host access to the descriptor set must be externally synchronized. + pub unsafe fn update_by_ref( + &self, + descriptor_writes: impl IntoIterator, + descriptor_copies: impl IntoIterator, + ) -> Result<(), Box> { + let descriptor_writes: SmallVec<[_; 8]> = descriptor_writes.into_iter().collect(); + let descriptor_copies: SmallVec<[_; 8]> = descriptor_copies.into_iter().collect(); + + self.inner + .validate_update(&descriptor_writes, &descriptor_copies)?; + + Self::update_inner( + &self.inner, + &mut self.resources.write(), + &descriptor_writes, + &descriptor_copies, + ); + + Ok(()) + } + + #[cfg_attr(not(feature = "document_unchecked"), doc(hidden))] + pub unsafe fn update_by_ref_unchecked( + &self, + descriptor_writes: impl IntoIterator, + descriptor_copies: impl IntoIterator, + ) { + let descriptor_writes: SmallVec<[_; 8]> = descriptor_writes.into_iter().collect(); + let descriptor_copies: SmallVec<[_; 8]> = descriptor_copies.into_iter().collect(); + + Self::update_inner( + &self.inner, + &mut self.resources.write(), + &descriptor_writes, + &descriptor_copies, + ); + } + + unsafe fn update_inner( + inner: &RawDescriptorSet, + resources: &mut DescriptorSetResources, + descriptor_writes: &[WriteDescriptorSet], + descriptor_copies: &[CopyDescriptorSet], + ) { + inner.update_unchecked(descriptor_writes, descriptor_copies); + + for write in descriptor_writes { + resources.write(write, inner.layout()); + } + + for copy in descriptor_copies { + resources.copy(copy); + } } } -impl Eq for dyn DescriptorSet {} +unsafe impl VulkanObject for DescriptorSet { + type Handle = ash::vk::DescriptorSet; -impl Hash for dyn DescriptorSet { + #[inline] + fn handle(&self) -> Self::Handle { + self.inner.handle() + } +} + +unsafe impl DeviceOwned for DescriptorSet { + #[inline] + fn device(&self) -> &Arc { + self.inner.device() + } +} + +impl PartialEq for DescriptorSet { + #[inline] + fn eq(&self, other: &Self) -> bool { + self.inner == other.inner + } +} + +impl Eq for DescriptorSet {} + +impl Hash for DescriptorSet { + #[inline] fn hash(&self, state: &mut H) { - self.alloc().hash(state); + self.inner.hash(state); } } /// The resources that are bound to a descriptor set. -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct DescriptorSetResources { binding_resources: HashMap, } @@ -262,9 +420,8 @@ impl DescriptorSetResources { #[inline] pub(crate) fn copy(&mut self, copy: &CopyDescriptorSet) { - let src = copy - .src_set - .resources() + let resources = copy.src_set.resources(); + let src = resources .binding_resources .get(©.src_binding) .expect("descriptor copy has invalid src_binding number"); @@ -281,7 +438,7 @@ impl DescriptorSetResources { } /// The resources that are bound to a single descriptor set binding. -#[derive(Clone)] +#[derive(Clone, Debug)] pub enum DescriptorBindingResources { None(Elements<()>), Buffer(Elements), @@ -471,13 +628,13 @@ impl DescriptorBindingResources { #[derive(Clone)] pub struct DescriptorSetWithOffsets { - descriptor_set: Arc, + descriptor_set: Arc, dynamic_offsets: SmallVec<[u32; 4]>, } impl DescriptorSetWithOffsets { pub fn new( - descriptor_set: Arc, + descriptor_set: Arc, dynamic_offsets: impl IntoIterator, ) -> Self { Self { @@ -487,21 +644,19 @@ impl DescriptorSetWithOffsets { } #[inline] - pub fn as_ref(&self) -> (&Arc, &[u32]) { + pub fn as_ref(&self) -> (&Arc, &[u32]) { (&self.descriptor_set, &self.dynamic_offsets) } #[inline] - pub fn into_tuple(self) -> (Arc, impl ExactSizeIterator) { + pub fn into_tuple(self) -> (Arc, impl ExactSizeIterator) { (self.descriptor_set, self.dynamic_offsets.into_iter()) } } -impl From> for DescriptorSetWithOffsets -where - S: DescriptorSet + 'static, -{ - fn from(descriptor_set: Arc) -> Self { +impl From> for DescriptorSetWithOffsets { + #[inline] + fn from(descriptor_set: Arc) -> Self { DescriptorSetWithOffsets::new(descriptor_set, std::iter::empty()) } } diff --git a/vulkano/src/descriptor_set/persistent.rs b/vulkano/src/descriptor_set/persistent.rs deleted file mode 100644 index 4ec0dfa4..00000000 --- a/vulkano/src/descriptor_set/persistent.rs +++ /dev/null @@ -1,151 +0,0 @@ -//! A simple, immutable descriptor set that is expected to be long-lived. -//! -//! Creating a persistent descriptor set allocates from a pool, and can't be modified once created. -//! You are therefore encouraged to create them at initialization and not the during -//! performance-critical paths. -//! -//! > **Note**: You can control of the pool that is used to create the descriptor set, if you wish -//! > so. By creating a implementation of the `DescriptorPool` trait that doesn't perform any -//! > actual allocation, you can skip this allocation and make it acceptable to use a persistent -//! > descriptor set in performance-critical paths.. -//! -//! # Examples -//! TODO: - -use super::{ - pool::{DescriptorPool, DescriptorPoolAlloc}, - sys::UnsafeDescriptorSet, - CopyDescriptorSet, -}; -use crate::{ - descriptor_set::{ - allocator::DescriptorSetAllocator, update::WriteDescriptorSet, DescriptorSet, - DescriptorSetLayout, DescriptorSetResources, - }, - device::{Device, DeviceOwned}, - Validated, ValidationError, VulkanError, VulkanObject, -}; -use smallvec::SmallVec; -use std::{ - hash::{Hash, Hasher}, - sync::Arc, -}; - -/// A simple, immutable descriptor set that is expected to be long-lived. -pub struct PersistentDescriptorSet { - inner: UnsafeDescriptorSet, - resources: DescriptorSetResources, -} - -impl PersistentDescriptorSet { - /// Creates and returns a new descriptor set with a variable descriptor count of 0. - /// - /// See `new_with_pool` for more. - #[inline] - pub fn new( - allocator: Arc, - layout: Arc, - descriptor_writes: impl IntoIterator, - descriptor_copies: impl IntoIterator, - ) -> Result, Validated> { - Self::new_variable(allocator, layout, 0, descriptor_writes, descriptor_copies) - } - - /// Creates and returns a new descriptor set with the requested variable descriptor count, - /// allocating it from the provided pool. - /// - /// # Panics - /// - /// - Panics if `layout` was created for push descriptors rather than descriptor sets. - /// - Panics if `variable_descriptor_count` is too large for the given `layout`. - pub fn new_variable( - allocator: Arc, - layout: Arc, - variable_descriptor_count: u32, - descriptor_writes: impl IntoIterator, - descriptor_copies: impl IntoIterator, - ) -> Result, Validated> { - let mut set = PersistentDescriptorSet { - inner: UnsafeDescriptorSet::new(allocator, &layout, variable_descriptor_count)?, - resources: DescriptorSetResources::new(&layout, variable_descriptor_count), - }; - - unsafe { - set.update(descriptor_writes, descriptor_copies)?; - } - - Ok(Arc::new(set)) - } - - unsafe fn update( - &mut self, - descriptor_writes: impl IntoIterator, - descriptor_copies: impl IntoIterator, - ) -> Result<(), Box> { - let descriptor_writes: SmallVec<[_; 8]> = descriptor_writes.into_iter().collect(); - let descriptor_copies: SmallVec<[_; 8]> = descriptor_copies.into_iter().collect(); - - unsafe { - self.inner.update(&descriptor_writes, &descriptor_copies)?; - } - - for write in descriptor_writes { - self.resources.write(&write, self.inner.layout()); - } - - for copy in descriptor_copies { - self.resources.copy(©); - } - - Ok(()) - } -} - -unsafe impl DescriptorSet for PersistentDescriptorSet { - #[inline] - fn alloc(&self) -> &DescriptorPoolAlloc { - &self.inner.alloc().inner - } - - #[inline] - fn pool(&self) -> &DescriptorPool { - &self.inner.alloc().pool - } - - #[inline] - fn resources(&self) -> &DescriptorSetResources { - &self.resources - } -} - -unsafe impl VulkanObject for PersistentDescriptorSet { - type Handle = ash::vk::DescriptorSet; - - #[inline] - fn handle(&self) -> Self::Handle { - self.inner.handle() - } -} - -unsafe impl DeviceOwned for PersistentDescriptorSet { - #[inline] - fn device(&self) -> &Arc { - self.layout().device() - } -} - -impl PartialEq for PersistentDescriptorSet { - #[inline] - fn eq(&self, other: &Self) -> bool { - self.inner == other.inner - } -} - -impl Eq for PersistentDescriptorSet {} - -impl Hash for PersistentDescriptorSet { - #[inline] - fn hash(&self, state: &mut H) { - self.inner.hash(state); - } -} diff --git a/vulkano/src/descriptor_set/pool.rs b/vulkano/src/descriptor_set/pool.rs index ea6cc64d..f305ded8 100644 --- a/vulkano/src/descriptor_set/pool.rs +++ b/vulkano/src/descriptor_set/pool.rs @@ -1,7 +1,7 @@ use crate::{ descriptor_set::{ layout::{DescriptorSetLayout, DescriptorSetLayoutCreateFlags, DescriptorType}, - sys::UnsafeDescriptorSet, + sys::RawDescriptorSet, }, device::{Device, DeviceOwned, DeviceOwnedDebugWrapper}, instance::InstanceOwnedDebugWrapper, @@ -345,7 +345,7 @@ impl DescriptorPool { #[inline] pub unsafe fn free_descriptor_sets( &self, - descriptor_sets: impl IntoIterator, + descriptor_sets: impl IntoIterator, ) -> Result<(), Validated> { self.validate_free_descriptor_sets()?; @@ -371,7 +371,7 @@ impl DescriptorPool { #[cfg_attr(not(feature = "document_unchecked"), doc(hidden))] pub unsafe fn free_descriptor_sets_unchecked( &self, - descriptor_sets: impl IntoIterator, + descriptor_sets: impl IntoIterator, ) -> Result<(), VulkanError> { let sets: SmallVec<[_; 8]> = descriptor_sets.into_iter().map(|s| s.handle()).collect(); if !sets.is_empty() { @@ -440,7 +440,7 @@ unsafe impl DeviceOwned for DescriptorPool { impl_id_counter!(DescriptorPool); -/// Parameters to create a new `UnsafeDescriptorPool`. +/// Parameters to create a new `DescriptorPool`. #[derive(Clone, Debug)] pub struct DescriptorPoolCreateInfo { /// Additional properties of the descriptor pool. @@ -598,7 +598,7 @@ vulkan_bitflags! { ]), */ } -/// Parameters to allocate a new `UnsafeDescriptorSet` from an `UnsafeDescriptorPool`. +/// Parameters to allocate a new `DescriptorPoolAlloc` from a `DescriptorPool`. #[derive(Clone, Debug)] pub struct DescriptorSetAllocateInfo { /// The descriptor set layout to create the set for. diff --git a/vulkano/src/descriptor_set/sys.rs b/vulkano/src/descriptor_set/sys.rs index 390d4a21..3e61e14c 100644 --- a/vulkano/src/descriptor_set/sys.rs +++ b/vulkano/src/descriptor_set/sys.rs @@ -21,27 +21,27 @@ use std::{ sync::Arc, }; -/// Low-level descriptor set. +/// A raw descriptor set corresponding directly to a `VkDescriptorSet`. /// -/// This descriptor set does not keep track of synchronization, -/// nor does it store any information on what resources have been written to each descriptor. +/// This descriptor set does not keep track of synchronization, nor does it store any information +/// on what resources have been written to each descriptor. #[derive(Debug)] -pub struct UnsafeDescriptorSet { +pub struct RawDescriptorSet { allocation: ManuallyDrop, allocator: Arc, } -impl UnsafeDescriptorSet { +impl RawDescriptorSet { /// Allocates a new descriptor set and returns it. #[inline] pub fn new( allocator: Arc, layout: &Arc, variable_descriptor_count: u32, - ) -> Result> { + ) -> Result> { let allocation = allocator.allocate(layout, variable_descriptor_count)?; - Ok(UnsafeDescriptorSet { + Ok(RawDescriptorSet { allocation: ManuallyDrop::new(allocation), allocator, }) @@ -77,11 +77,12 @@ impl UnsafeDescriptorSet { /// /// - The resources in `descriptor_writes` and `descriptor_copies` must be kept alive for as /// long as `self` is in use. - /// - The descriptor set must not be in use by the device, - /// or be recorded to a command buffer as part of a bind command. + /// - The descriptor set must not be in use by the device, or be recorded to a command buffer + /// as part of a bind command. + /// - Host access to the descriptor set must be externally synchronized. #[inline] pub unsafe fn update( - &mut self, + &self, descriptor_writes: &[WriteDescriptorSet], descriptor_copies: &[CopyDescriptorSet], ) -> Result<(), Box> { @@ -91,7 +92,7 @@ impl UnsafeDescriptorSet { Ok(()) } - fn validate_update( + pub(super) fn validate_update( &self, descriptor_writes: &[WriteDescriptorSet], descriptor_copies: &[CopyDescriptorSet], @@ -112,7 +113,7 @@ impl UnsafeDescriptorSet { #[cfg_attr(not(feature = "document_unchecked"), doc(hidden))] pub unsafe fn update_unchecked( - &mut self, + &self, descriptor_writes: &[WriteDescriptorSet], descriptor_copies: &[CopyDescriptorSet], ) { @@ -206,7 +207,7 @@ impl UnsafeDescriptorSet { } } -impl Drop for UnsafeDescriptorSet { +impl Drop for RawDescriptorSet { #[inline] fn drop(&mut self) { let allocation = unsafe { ManuallyDrop::take(&mut self.allocation) }; @@ -214,7 +215,7 @@ impl Drop for UnsafeDescriptorSet { } } -unsafe impl VulkanObject for UnsafeDescriptorSet { +unsafe impl VulkanObject for RawDescriptorSet { type Handle = ash::vk::DescriptorSet; #[inline] @@ -223,23 +224,23 @@ unsafe impl VulkanObject for UnsafeDescriptorSet { } } -unsafe impl DeviceOwned for UnsafeDescriptorSet { +unsafe impl DeviceOwned for RawDescriptorSet { #[inline] fn device(&self) -> &Arc { self.allocation.inner.device() } } -impl PartialEq for UnsafeDescriptorSet { +impl PartialEq for RawDescriptorSet { #[inline] fn eq(&self, other: &Self) -> bool { self.allocation.inner == other.allocation.inner } } -impl Eq for UnsafeDescriptorSet {} +impl Eq for RawDescriptorSet {} -impl Hash for UnsafeDescriptorSet { +impl Hash for RawDescriptorSet { #[inline] fn hash(&self, state: &mut H) { self.allocation.inner.hash(state); diff --git a/vulkano/src/descriptor_set/update.rs b/vulkano/src/descriptor_set/update.rs index 09ca6825..c7e12911 100644 --- a/vulkano/src/descriptor_set/update.rs +++ b/vulkano/src/descriptor_set/update.rs @@ -1,6 +1,6 @@ use super::{ layout::{DescriptorSetLayout, DescriptorType}, - sys::UnsafeDescriptorSet, + sys::RawDescriptorSet, DescriptorSet, }; use crate::{ @@ -1581,7 +1581,7 @@ pub struct CopyDescriptorSet { /// The source descriptor set to copy from. /// /// There is no default value. - pub src_set: Arc, + pub src_set: Arc, /// The binding number in the source descriptor set to copy from. /// @@ -1612,7 +1612,7 @@ pub struct CopyDescriptorSet { impl CopyDescriptorSet { /// Returns a `CopyDescriptorSet` with the specified `src_set`. #[inline] - pub fn new(src_set: Arc) -> Self { + pub fn new(src_set: Arc) -> Self { Self { src_set, src_binding: 0, @@ -1624,10 +1624,7 @@ impl CopyDescriptorSet { } } - pub(crate) fn validate( - &self, - dst_set: &UnsafeDescriptorSet, - ) -> Result<(), Box> { + pub(crate) fn validate(&self, dst_set: &RawDescriptorSet) -> Result<(), Box> { let &Self { ref src_set, src_binding, diff --git a/vulkano/src/image/sampler/ycbcr.rs b/vulkano/src/image/sampler/ycbcr.rs index 3fc504b8..2ecfef51 100644 --- a/vulkano/src/image/sampler/ycbcr.rs +++ b/vulkano/src/image/sampler/ycbcr.rs @@ -20,7 +20,7 @@ //! DescriptorSetLayout, DescriptorSetLayoutBinding, DescriptorSetLayoutCreateInfo, //! DescriptorType, //! }, -//! PersistentDescriptorSet, WriteDescriptorSet, +//! DescriptorSet, WriteDescriptorSet, //! }, //! format::Format, //! image::{ @@ -99,7 +99,7 @@ //! }; //! let image_view = ImageView::new(image, create_info).unwrap(); //! -//! let descriptor_set = PersistentDescriptorSet::new( +//! let descriptor_set = DescriptorSet::new( //! descriptor_set_allocator.clone(), //! descriptor_set_layout.clone(), //! [WriteDescriptorSet::image_view(0, image_view)], diff --git a/vulkano/src/pipeline/compute.rs b/vulkano/src/pipeline/compute.rs index 422bbb18..2fb03572 100644 --- a/vulkano/src/pipeline/compute.rs +++ b/vulkano/src/pipeline/compute.rs @@ -448,7 +448,7 @@ mod tests { allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, CommandBufferUsage, }, descriptor_set::{ - allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet, + allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet, }, memory::allocator::{AllocationCreateInfo, MemoryTypeFilter, StandardMemoryAllocator}, pipeline::{ @@ -544,7 +544,7 @@ mod tests { device.clone(), Default::default(), )); - let set = PersistentDescriptorSet::new( + let set = DescriptorSet::new( ds_allocator, pipeline.layout().set_layouts().get(0).unwrap().clone(), [WriteDescriptorSet::buffer(0, data_buffer.clone())], @@ -688,7 +688,7 @@ mod tests { device.clone(), Default::default(), )); - let set = PersistentDescriptorSet::new( + let set = DescriptorSet::new( ds_allocator, pipeline.layout().set_layouts().get(0).unwrap().clone(), [WriteDescriptorSet::buffer(0, data_buffer.clone())],