Skip to content

Commit

Permalink
Move to manual management of descriptor set and command buffer alloca…
Browse files Browse the repository at this point in the history
…tors (#1957)

* Rename `Pool` -> `Allocator`

* Fix module names

* Rename actual pool types

* Remove internally managed allocators

* Fix doc tests

* Update docs

* Update examples

* Fix merging oopsie

* Fix cb allocators only working for one q family

* Make `DescriptorPool` `!Sync`, remove `&mut`

* Combine lean files

* Consistency

* Rename single layout pools

* Re-export variable descriptor set pool

Co-authored-by: comrademarc <40955683+comrademarc@users.noreply.github.com>
  • Loading branch information
marc0246 and marc0246 authored Oct 5, 2022
1 parent f70f433 commit ef65c98
Show file tree
Hide file tree
Showing 78 changed files with 1,669 additions and 1,300 deletions.
14 changes: 11 additions & 3 deletions examples/src/bin/basic-compute-shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@

use vulkano::{
buffer::{BufferUsage, CpuAccessibleBuffer},
command_buffer::{AutoCommandBufferBuilder, CommandBufferUsage},
descriptor_set::{PersistentDescriptorSet, WriteDescriptorSet},
command_buffer::{
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, CommandBufferUsage,
},
descriptor_set::{
allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet,
},
device::{
physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo,
},
Expand Down Expand Up @@ -140,6 +144,9 @@ fn main() {
.unwrap()
};

let descriptor_set_allocator = StandardDescriptorSetAllocator::new(device.clone());
let command_buffer_allocator = StandardCommandBufferAllocator::new(device.clone());

// We start by creating the buffer that will store the data.
let data_buffer = {
// Iterator that produces the data.
Expand Down Expand Up @@ -167,14 +174,15 @@ fn main() {
// 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(
&descriptor_set_allocator,
layout.clone(),
[WriteDescriptorSet::buffer(0, data_buffer.clone())],
)
.unwrap();

// In order to execute our operation, we have to build a command buffer.
let mut builder = AutoCommandBufferBuilder::primary(
device.clone(),
&command_buffer_allocator,
queue.queue_family_index(),
CommandBufferUsage::OneTimeSubmit,
)
Expand Down
7 changes: 5 additions & 2 deletions examples/src/bin/buffer-pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ use std::{
use vulkano::{
buffer::CpuBufferPool,
command_buffer::{
AutoCommandBufferBuilder, CommandBufferUsage, RenderPassBeginInfo, SubpassContents,
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, CommandBufferUsage,
RenderPassBeginInfo, SubpassContents,
},
device::{
physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo,
Expand Down Expand Up @@ -239,6 +240,8 @@ fn main() {
let mut recreate_swapchain = false;
let mut previous_frame_end = Some(sync::now(device.clone()).boxed());

let command_buffer_allocator = StandardCommandBufferAllocator::new(device.clone());

event_loop.run(move |event, _, control_flow| {
match event {
Event::WindowEvent {
Expand Down Expand Up @@ -330,7 +333,7 @@ fn main() {
// Allocate a new chunk from buffer_pool
let buffer = buffer_pool.from_iter(data.to_vec()).unwrap();
let mut builder = AutoCommandBufferBuilder::primary(
device.clone(),
&command_buffer_allocator,
queue.queue_family_index(),
CommandBufferUsage::OneTimeSubmit,
)
Expand Down
8 changes: 5 additions & 3 deletions examples/src/bin/clear_attachments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use std::sync::Arc;
use vulkano::{
command_buffer::{
AutoCommandBufferBuilder, ClearAttachment, ClearRect, CommandBufferUsage,
RenderPassBeginInfo, SubpassContents,
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, ClearAttachment,
ClearRect, CommandBufferUsage, RenderPassBeginInfo, SubpassContents,
},
device::{
physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo,
Expand Down Expand Up @@ -153,6 +153,8 @@ fn main() {
)
.unwrap();

let command_buffer_allocator = StandardCommandBufferAllocator::new(device.clone());

let mut width = swapchain.image_extent()[0];
let mut height = swapchain.image_extent()[1];
let mut framebuffers = window_size_dependent_setup(&images, render_pass.clone());
Expand Down Expand Up @@ -213,7 +215,7 @@ fn main() {
}

let mut builder = AutoCommandBufferBuilder::primary(
device.clone(),
&command_buffer_allocator,
queue.queue_family_index(),
CommandBufferUsage::OneTimeSubmit,
)
Expand Down
6 changes: 5 additions & 1 deletion examples/src/bin/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use std::sync::Arc;
use vulkano::{
command_buffer::allocator::StandardCommandBufferAllocator,
device::{
physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo,
},
Expand Down Expand Up @@ -158,7 +159,7 @@ fn main() {
})
.expect("no device available");

let (_, mut queues) = Device::new(
let (device, mut queues) = Device::new(
physical_device,
DeviceCreateInfo {
enabled_extensions: device_extensions,
Expand All @@ -172,6 +173,8 @@ fn main() {
.expect("failed to create device");
let queue = queues.next().unwrap();

let command_buffer_allocator = StandardCommandBufferAllocator::new(device);

// Create an image in order to generate some additional logging:
let pixel_format = Format::R8G8B8A8_UINT;
let dimensions = ImageDimensions::Dim2d {
Expand All @@ -185,6 +188,7 @@ fn main() {
dimensions,
MipmapsCount::One,
pixel_format,
&command_buffer_allocator,
queue,
)
.unwrap();
Expand Down
24 changes: 18 additions & 6 deletions examples/src/bin/deferred/frame/ambient_lighting_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
// according to those terms.

use bytemuck::{Pod, Zeroable};
use std::sync::Arc;
use std::{rc::Rc, sync::Arc};
use vulkano::{
buffer::{BufferUsage, CpuAccessibleBuffer, TypedBufferAccess},
command_buffer::{
AutoCommandBufferBuilder, CommandBufferInheritanceInfo, CommandBufferUsage,
SecondaryAutoCommandBuffer,
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder,
CommandBufferInheritanceInfo, CommandBufferUsage, SecondaryAutoCommandBuffer,
},
descriptor_set::{
allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet,
},
descriptor_set::{PersistentDescriptorSet, WriteDescriptorSet},
device::Queue,
image::ImageViewAbstract,
impl_vertex,
Expand All @@ -37,11 +39,18 @@ pub struct AmbientLightingSystem {
vertex_buffer: Arc<CpuAccessibleBuffer<[Vertex]>>,
subpass: Subpass,
pipeline: Arc<GraphicsPipeline>,
command_buffer_allocator: Rc<StandardCommandBufferAllocator>,
descriptor_set_allocator: Rc<StandardDescriptorSetAllocator>,
}

impl AmbientLightingSystem {
/// Initializes the ambient lighting system.
pub fn new(gfx_queue: Arc<Queue>, subpass: Subpass) -> AmbientLightingSystem {
pub fn new(
gfx_queue: Arc<Queue>,
subpass: Subpass,
command_buffer_allocator: Rc<StandardCommandBufferAllocator>,
descriptor_set_allocator: Rc<StandardDescriptorSetAllocator>,
) -> AmbientLightingSystem {
// TODO: vulkano doesn't allow us to draw without a vertex buffer, otherwise we could
// hard-code these values in the shader
let vertices = [
Expand Down Expand Up @@ -98,6 +107,8 @@ impl AmbientLightingSystem {
vertex_buffer,
subpass,
pipeline,
command_buffer_allocator,
descriptor_set_allocator,
}
}

Expand Down Expand Up @@ -125,6 +136,7 @@ impl AmbientLightingSystem {

let layout = self.pipeline.layout().set_layouts().get(0).unwrap();
let descriptor_set = PersistentDescriptorSet::new(
&*self.descriptor_set_allocator,
layout.clone(),
[WriteDescriptorSet::image_view(0, color_input)],
)
Expand All @@ -137,7 +149,7 @@ impl AmbientLightingSystem {
};

let mut builder = AutoCommandBufferBuilder::secondary(
self.gfx_queue.device().clone(),
&*self.command_buffer_allocator,
self.gfx_queue.queue_family_index(),
CommandBufferUsage::MultipleSubmit,
CommandBufferInheritanceInfo {
Expand Down
24 changes: 18 additions & 6 deletions examples/src/bin/deferred/frame/directional_lighting_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@

use bytemuck::{Pod, Zeroable};
use cgmath::Vector3;
use std::sync::Arc;
use std::{rc::Rc, sync::Arc};
use vulkano::{
buffer::{BufferUsage, CpuAccessibleBuffer, TypedBufferAccess},
command_buffer::{
AutoCommandBufferBuilder, CommandBufferInheritanceInfo, CommandBufferUsage,
SecondaryAutoCommandBuffer,
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder,
CommandBufferInheritanceInfo, CommandBufferUsage, SecondaryAutoCommandBuffer,
},
descriptor_set::{
allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet,
},
descriptor_set::{PersistentDescriptorSet, WriteDescriptorSet},
device::Queue,
image::ImageViewAbstract,
impl_vertex,
Expand All @@ -38,11 +40,18 @@ pub struct DirectionalLightingSystem {
vertex_buffer: Arc<CpuAccessibleBuffer<[Vertex]>>,
subpass: Subpass,
pipeline: Arc<GraphicsPipeline>,
command_buffer_allocator: Rc<StandardCommandBufferAllocator>,
descriptor_set_allocator: Rc<StandardDescriptorSetAllocator>,
}

impl DirectionalLightingSystem {
/// Initializes the directional lighting system.
pub fn new(gfx_queue: Arc<Queue>, subpass: Subpass) -> DirectionalLightingSystem {
pub fn new(
gfx_queue: Arc<Queue>,
subpass: Subpass,
command_buffer_allocator: Rc<StandardCommandBufferAllocator>,
descriptor_set_allocator: Rc<StandardDescriptorSetAllocator>,
) -> DirectionalLightingSystem {
// TODO: vulkano doesn't allow us to draw without a vertex buffer, otherwise we could
// hard-code these values in the shader
let vertices = [
Expand Down Expand Up @@ -99,6 +108,8 @@ impl DirectionalLightingSystem {
vertex_buffer,
subpass,
pipeline,
command_buffer_allocator,
descriptor_set_allocator,
}
}

Expand Down Expand Up @@ -136,6 +147,7 @@ impl DirectionalLightingSystem {

let layout = self.pipeline.layout().set_layouts().get(0).unwrap();
let descriptor_set = PersistentDescriptorSet::new(
&*self.descriptor_set_allocator,
layout.clone(),
[
WriteDescriptorSet::image_view(0, color_input),
Expand All @@ -151,7 +163,7 @@ impl DirectionalLightingSystem {
};

let mut builder = AutoCommandBufferBuilder::secondary(
self.gfx_queue.device().clone(),
&*self.command_buffer_allocator,
self.gfx_queue.queue_family_index(),
CommandBufferUsage::MultipleSubmit,
CommandBufferInheritanceInfo {
Expand Down
24 changes: 18 additions & 6 deletions examples/src/bin/deferred/frame/point_lighting_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@

use bytemuck::{Pod, Zeroable};
use cgmath::{Matrix4, Vector3};
use std::sync::Arc;
use std::{rc::Rc, sync::Arc};
use vulkano::{
buffer::{BufferUsage, CpuAccessibleBuffer, TypedBufferAccess},
command_buffer::{
AutoCommandBufferBuilder, CommandBufferInheritanceInfo, CommandBufferUsage,
SecondaryAutoCommandBuffer,
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder,
CommandBufferInheritanceInfo, CommandBufferUsage, SecondaryAutoCommandBuffer,
},
descriptor_set::{
allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet,
},
descriptor_set::{PersistentDescriptorSet, WriteDescriptorSet},
device::Queue,
image::ImageViewAbstract,
impl_vertex,
Expand All @@ -37,11 +39,18 @@ pub struct PointLightingSystem {
vertex_buffer: Arc<CpuAccessibleBuffer<[Vertex]>>,
subpass: Subpass,
pipeline: Arc<GraphicsPipeline>,
command_buffer_allocator: Rc<StandardCommandBufferAllocator>,
descriptor_set_allocator: Rc<StandardDescriptorSetAllocator>,
}

impl PointLightingSystem {
/// Initializes the point lighting system.
pub fn new(gfx_queue: Arc<Queue>, subpass: Subpass) -> PointLightingSystem {
pub fn new(
gfx_queue: Arc<Queue>,
subpass: Subpass,
command_buffer_allocator: Rc<StandardCommandBufferAllocator>,
descriptor_set_allocator: Rc<StandardDescriptorSetAllocator>,
) -> PointLightingSystem {
// TODO: vulkano doesn't allow us to draw without a vertex buffer, otherwise we could
// hard-code these values in the shader
let vertices = [
Expand Down Expand Up @@ -98,6 +107,8 @@ impl PointLightingSystem {
vertex_buffer,
subpass,
pipeline,
command_buffer_allocator,
descriptor_set_allocator,
}
}

Expand Down Expand Up @@ -148,6 +159,7 @@ impl PointLightingSystem {

let layout = self.pipeline.layout().set_layouts().get(0).unwrap();
let descriptor_set = PersistentDescriptorSet::new(
&*self.descriptor_set_allocator,
layout.clone(),
[
WriteDescriptorSet::image_view(0, color_input),
Expand All @@ -164,7 +176,7 @@ impl PointLightingSystem {
};

let mut builder = AutoCommandBufferBuilder::secondary(
self.gfx_queue.device().clone(),
&*self.command_buffer_allocator,
self.gfx_queue.queue_family_index(),
CommandBufferUsage::MultipleSubmit,
CommandBufferInheritanceInfo {
Expand Down
Loading

0 comments on commit ef65c98

Please sign in to comment.