Skip to content

Commit

Permalink
Memory allocation revamp (vulkano-rs#1997)
Browse files Browse the repository at this point in the history
* Add suballocators

* Add tests

* Retroactively abort `PoolAllocatorCreateInfo`

* Use const generic for `PoolAllocator`'s block size

* Move `new` and `try_into_region` to `Suballocator`

* Move `allocate_unchecked` to `Suballocator`

* Fix constructor visibility

* Move `free_size` to `Suballocator`

* Small fixes

* Merge `BumpAllocator` and `SyncBumpAllocator`

* Restrict `AllocParent::None` to tests

* Rewording

* Add dedicated allocations

* Add `Suballocator::cleanup`

* Make `free_size`s lock-free

* Add `Suballocator::largest_free_chunk`

* Add `ArrayVec`

* Remove useless `unsafe`

* Add `MemoryAllocator`

* Add `GenericMemoryAllocator`

* Small fixes

* Retroactively abort `largest_free_chunk`

* Small docs adjustments

* Rearrange

* Add `MemoryAlloc::mapped_ptr`

* Fix oopsie

* Add support for non-coherent mapped memory

* Add `DeviceOwned` subtrait to `Suballocator`

* Move granularities to suballocators, fix tests

* Add cache control

* Fix oopsie where alignment of 0 is possible

* Store `Arc<DeviceMemory>` in suballocators

* Add `MemoryAllocator::create_{buffer, image}`

* Remove `MemoryPool`

* Fix examples

* Remove `MemoryAlloc::{memory, memory_type_index}`

* Minor improvement to `AllocationCreationError`

* Add some example docs

* Add support for external memory

* Swicheroo

* Small fix

* Shorten sm names, cache atom size in suballocators

* Add config for allocation type to generic allocatr

* Engrish

* Fix a big oopsie

* Spliteroo

* Inglisch
  • Loading branch information
marc0246 authored Oct 26, 2022
1 parent f079c2b commit 34b7095
Show file tree
Hide file tree
Showing 69 changed files with 5,845 additions and 1,903 deletions.
4 changes: 3 additions & 1 deletion examples/src/bin/basic-compute-shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use vulkano::{
physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo,
},
instance::{Instance, InstanceCreateInfo},
memory::allocator::StandardMemoryAllocator,
pipeline::{ComputePipeline, Pipeline, PipelineBindPoint},
sync::{self, GpuFuture},
VulkanLibrary,
Expand Down Expand Up @@ -144,6 +145,7 @@ fn main() {
.unwrap()
};

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

Expand All @@ -153,7 +155,7 @@ fn main() {
let data_iter = 0..65536u32;
// Builds the buffer and fills it with this iterator.
CpuAccessibleBuffer::from_iter(
device.clone(),
&memory_allocator,
BufferUsage {
storage_buffer: true,
..BufferUsage::empty()
Expand Down
5 changes: 4 additions & 1 deletion examples/src/bin/buffer-pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use vulkano::{
image::{view::ImageView, ImageAccess, ImageUsage, SwapchainImage},
impl_vertex,
instance::{Instance, InstanceCreateInfo},
memory::allocator::StandardMemoryAllocator,
pipeline::{
graphics::{
input_assembly::InputAssemblyState,
Expand Down Expand Up @@ -169,8 +170,10 @@ fn main() {
.unwrap()
};

let memory_allocator = Arc::new(StandardMemoryAllocator::new_default(device.clone()));

// Vertex Buffer Pool
let buffer_pool: CpuBufferPool<Vertex> = CpuBufferPool::vertex_buffer(device.clone());
let buffer_pool: CpuBufferPool<Vertex> = CpuBufferPool::vertex_buffer(memory_allocator);

mod vs {
vulkano_shaders::shader! {
Expand Down
5 changes: 4 additions & 1 deletion examples/src/bin/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use vulkano::{
},
Instance, InstanceCreateInfo, InstanceExtensions,
},
memory::allocator::StandardMemoryAllocator,
VulkanLibrary,
};

Expand Down Expand Up @@ -175,7 +176,7 @@ fn main() {
.expect("failed to create device");
let queue = queues.next().unwrap();

let command_buffer_allocator = StandardCommandBufferAllocator::new(device);
let command_buffer_allocator = StandardCommandBufferAllocator::new(device.clone());
let mut command_buffer_builder = AutoCommandBufferBuilder::primary(
&command_buffer_allocator,
queue.queue_family_index(),
Expand All @@ -191,7 +192,9 @@ fn main() {
array_layers: 1,
};
static DATA: [[u8; 4]; 4096 * 4096] = [[0; 4]; 4096 * 4096];
let memory_allocator = StandardMemoryAllocator::new_default(device);
let _ = ImmutableImage::from_iter(
&memory_allocator,
DATA.iter().copied(),
dimensions,
MipmapsCount::One,
Expand Down
4 changes: 3 additions & 1 deletion examples/src/bin/deferred/frame/ambient_lighting_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use vulkano::{
device::Queue,
image::ImageViewAbstract,
impl_vertex,
memory::allocator::MemoryAllocator,
pipeline::{
graphics::{
color_blend::{AttachmentBlend, BlendFactor, BlendOp, ColorBlendState},
Expand Down Expand Up @@ -48,6 +49,7 @@ impl AmbientLightingSystem {
pub fn new(
gfx_queue: Arc<Queue>,
subpass: Subpass,
memory_allocator: &impl MemoryAllocator,
command_buffer_allocator: Rc<StandardCommandBufferAllocator>,
descriptor_set_allocator: Rc<StandardDescriptorSetAllocator>,
) -> AmbientLightingSystem {
Expand All @@ -66,7 +68,7 @@ impl AmbientLightingSystem {
];
let vertex_buffer = {
CpuAccessibleBuffer::from_iter(
gfx_queue.device().clone(),
memory_allocator,
BufferUsage {
vertex_buffer: true,
..BufferUsage::empty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use vulkano::{
device::Queue,
image::ImageViewAbstract,
impl_vertex,
memory::allocator::MemoryAllocator,
pipeline::{
graphics::{
color_blend::{AttachmentBlend, BlendFactor, BlendOp, ColorBlendState},
Expand Down Expand Up @@ -49,6 +50,7 @@ impl DirectionalLightingSystem {
pub fn new(
gfx_queue: Arc<Queue>,
subpass: Subpass,
memory_allocator: &impl MemoryAllocator,
command_buffer_allocator: Rc<StandardCommandBufferAllocator>,
descriptor_set_allocator: Rc<StandardDescriptorSetAllocator>,
) -> DirectionalLightingSystem {
Expand All @@ -67,7 +69,7 @@ impl DirectionalLightingSystem {
];
let vertex_buffer = {
CpuAccessibleBuffer::from_iter(
gfx_queue.device().clone(),
memory_allocator,
BufferUsage {
vertex_buffer: true,
..BufferUsage::empty()
Expand Down
4 changes: 3 additions & 1 deletion examples/src/bin/deferred/frame/point_lighting_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use vulkano::{
device::Queue,
image::ImageViewAbstract,
impl_vertex,
memory::allocator::MemoryAllocator,
pipeline::{
graphics::{
color_blend::{AttachmentBlend, BlendFactor, BlendOp, ColorBlendState},
Expand All @@ -48,6 +49,7 @@ impl PointLightingSystem {
pub fn new(
gfx_queue: Arc<Queue>,
subpass: Subpass,
memory_allocator: &impl MemoryAllocator,
command_buffer_allocator: Rc<StandardCommandBufferAllocator>,
descriptor_set_allocator: Rc<StandardDescriptorSetAllocator>,
) -> PointLightingSystem {
Expand All @@ -66,7 +68,7 @@ impl PointLightingSystem {
];
let vertex_buffer = {
CpuAccessibleBuffer::from_iter(
gfx_queue.device().clone(),
memory_allocator,
BufferUsage {
vertex_buffer: true,
..BufferUsage::empty()
Expand Down
19 changes: 13 additions & 6 deletions examples/src/bin/deferred/frame/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use vulkano::{
device::Queue,
format::Format,
image::{view::ImageView, AttachmentImage, ImageAccess, ImageUsage, ImageViewAbstract},
memory::allocator::StandardMemoryAllocator,
render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass},
sync::GpuFuture,
};
Expand All @@ -38,6 +39,7 @@ pub struct FrameSystem {
// in of a change in the dimensions.
render_pass: Arc<RenderPass>,

memory_allocator: Arc<StandardMemoryAllocator>,
command_buffer_allocator: Rc<StandardCommandBufferAllocator>,

// Intermediate render target that will contain the albedo of each pixel of the scene.
Expand Down Expand Up @@ -71,6 +73,7 @@ impl FrameSystem {
pub fn new(
gfx_queue: Arc<Queue>,
final_output_format: Format,
memory_allocator: Arc<StandardMemoryAllocator>,
command_buffer_allocator: Rc<StandardCommandBufferAllocator>,
) -> FrameSystem {
// Creating the render pass.
Expand Down Expand Up @@ -152,7 +155,7 @@ impl FrameSystem {
// These images will be replaced the first time we call `frame()`.
let diffuse_buffer = ImageView::new_default(
AttachmentImage::with_usage(
gfx_queue.device().clone(),
&*memory_allocator,
[1, 1],
Format::A2B10G10R10_UNORM_PACK32,
ImageUsage {
Expand All @@ -166,7 +169,7 @@ impl FrameSystem {
.unwrap();
let normals_buffer = ImageView::new_default(
AttachmentImage::with_usage(
gfx_queue.device().clone(),
&*memory_allocator,
[1, 1],
Format::R16G16B16A16_SFLOAT,
ImageUsage {
Expand All @@ -180,7 +183,7 @@ impl FrameSystem {
.unwrap();
let depth_buffer = ImageView::new_default(
AttachmentImage::with_usage(
gfx_queue.device().clone(),
&*memory_allocator,
[1, 1],
Format::D16_UNORM,
ImageUsage {
Expand All @@ -203,25 +206,29 @@ impl FrameSystem {
let ambient_lighting_system = AmbientLightingSystem::new(
gfx_queue.clone(),
lighting_subpass.clone(),
&*memory_allocator,
command_buffer_allocator.clone(),
descriptor_set_allocator.clone(),
);
let directional_lighting_system = DirectionalLightingSystem::new(
gfx_queue.clone(),
lighting_subpass.clone(),
&*memory_allocator,
command_buffer_allocator.clone(),
descriptor_set_allocator.clone(),
);
let point_lighting_system = PointLightingSystem::new(
gfx_queue.clone(),
lighting_subpass,
&*memory_allocator,
command_buffer_allocator.clone(),
descriptor_set_allocator,
);

FrameSystem {
gfx_queue,
render_pass,
memory_allocator,
command_buffer_allocator,
diffuse_buffer,
normals_buffer,
Expand Down Expand Up @@ -270,7 +277,7 @@ impl FrameSystem {
// render pass their content becomes undefined.
self.diffuse_buffer = ImageView::new_default(
AttachmentImage::with_usage(
self.gfx_queue.device().clone(),
&*self.memory_allocator,
img_dims,
Format::A2B10G10R10_UNORM_PACK32,
ImageUsage {
Expand All @@ -284,7 +291,7 @@ impl FrameSystem {
.unwrap();
self.normals_buffer = ImageView::new_default(
AttachmentImage::with_usage(
self.gfx_queue.device().clone(),
&*self.memory_allocator,
img_dims,
Format::R16G16B16A16_SFLOAT,
ImageUsage {
Expand All @@ -298,7 +305,7 @@ impl FrameSystem {
.unwrap();
self.depth_buffer = ImageView::new_default(
AttachmentImage::with_usage(
self.gfx_queue.device().clone(),
&*self.memory_allocator,
img_dims,
Format::D16_UNORM,
ImageUsage {
Expand Down
6 changes: 5 additions & 1 deletion examples/src/bin/deferred/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ use crate::{
triangle_draw_system::TriangleDrawSystem,
};
use cgmath::{Matrix4, SquareMatrix, Vector3};
use std::rc::Rc;
use std::{rc::Rc, sync::Arc};
use vulkano::{
command_buffer::allocator::StandardCommandBufferAllocator,
device::{
physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo,
},
image::{view::ImageView, ImageUsage},
instance::{Instance, InstanceCreateInfo},
memory::allocator::StandardMemoryAllocator,
swapchain::{
acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError,
SwapchainPresentInfo,
Expand Down Expand Up @@ -164,17 +165,20 @@ fn main() {
(swapchain, images)
};

let memory_allocator = Arc::new(StandardMemoryAllocator::new_default(device.clone()));
let command_buffer_allocator = Rc::new(StandardCommandBufferAllocator::new(device.clone()));

// Here is the basic initialization for the deferred system.
let mut frame_system = FrameSystem::new(
queue.clone(),
swapchain.image_format(),
memory_allocator.clone(),
command_buffer_allocator.clone(),
);
let triangle_draw_system = TriangleDrawSystem::new(
queue.clone(),
frame_system.deferred_subpass(),
&memory_allocator,
command_buffer_allocator,
);

Expand Down
4 changes: 3 additions & 1 deletion examples/src/bin/deferred/triangle_draw_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use vulkano::{
},
device::Queue,
impl_vertex,
memory::allocator::StandardMemoryAllocator,
pipeline::{
graphics::{
depth_stencil::DepthStencilState,
Expand All @@ -42,6 +43,7 @@ impl TriangleDrawSystem {
pub fn new(
gfx_queue: Arc<Queue>,
subpass: Subpass,
memory_allocator: &StandardMemoryAllocator,
command_buffer_allocator: Rc<StandardCommandBufferAllocator>,
) -> TriangleDrawSystem {
let vertices = [
Expand All @@ -57,7 +59,7 @@ impl TriangleDrawSystem {
];
let vertex_buffer = {
CpuAccessibleBuffer::from_iter(
gfx_queue.device().clone(),
memory_allocator,
BufferUsage {
vertex_buffer: true,
..BufferUsage::empty()
Expand Down
6 changes: 4 additions & 2 deletions examples/src/bin/dynamic-buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use vulkano::{
physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo,
},
instance::{Instance, InstanceCreateInfo},
memory::allocator::StandardMemoryAllocator,
pipeline::{ComputePipeline, Pipeline, PipelineBindPoint},
sync::{self, GpuFuture},
VulkanLibrary,
Expand Down Expand Up @@ -131,6 +132,7 @@ fn main() {
)
.unwrap();

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

Expand Down Expand Up @@ -164,7 +166,7 @@ fn main() {
};

let input_buffer = CpuAccessibleBuffer::from_iter(
device.clone(),
&memory_allocator,
BufferUsage {
uniform_buffer: true,
..BufferUsage::empty()
Expand All @@ -175,7 +177,7 @@ fn main() {
.unwrap();

let output_buffer = CpuAccessibleBuffer::from_iter(
device.clone(),
&memory_allocator,
BufferUsage {
storage_buffer: true,
..BufferUsage::empty()
Expand Down
6 changes: 4 additions & 2 deletions examples/src/bin/dynamic-local-size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use vulkano::{
format::Format,
image::{view::ImageView, ImageDimensions, StorageImage},
instance::{Instance, InstanceCreateInfo, InstanceExtensions},
memory::allocator::StandardMemoryAllocator,
pipeline::{ComputePipeline, Pipeline, PipelineBindPoint},
sync::{self, GpuFuture},
VulkanLibrary,
Expand Down Expand Up @@ -198,11 +199,12 @@ fn main() {
)
.unwrap();

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

let image = StorageImage::new(
device.clone(),
&memory_allocator,
ImageDimensions::Dim2d {
width: 1024,
height: 1024,
Expand All @@ -223,7 +225,7 @@ fn main() {
.unwrap();

let buf = CpuAccessibleBuffer::from_iter(
device.clone(),
&memory_allocator,
BufferUsage {
transfer_dst: true,
..BufferUsage::empty()
Expand Down
Loading

0 comments on commit 34b7095

Please # to comment.