Skip to content

Commit

Permalink
CpuBufferPool revamp (#2076)
Browse files Browse the repository at this point in the history
* `CpuBufferPool` revamp

* Fix oopsie

* Fix docs
  • Loading branch information
marc0246 authored Nov 5, 2022
1 parent c5c6bf0 commit fe01ddd
Show file tree
Hide file tree
Showing 8 changed files with 672 additions and 1,017 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,18 @@
// notice may not be copied, modified, or distributed except
// according to those terms.

// BufferPool Example
//
// Modified triangle example to show BufferPool
// Using a pool allows multiple buffers to be "in-flight" simultaneously
// and is suited to highly dynamic, similar sized chunks of data
//
// NOTE:(jdnewman85) ATM (5/4/2020) CpuBufferPool.next() and .chunk() have identical documentation
// I was unable to get next() to work. The compiler complained that the resulting buffer
// didn't implement VertexSource. Similar issues have been reported.
// See: https://github.com/vulkano-rs/vulkano/issues/1221
// Finally, I have not profiled CpuBufferPool against CpuAccessibleBuffer
// Modified triangle example to show `CpuBufferAllocator`.

use bytemuck::{Pod, Zeroable};
use std::{
sync::Arc,
time::{SystemTime, UNIX_EPOCH},
};
use vulkano::{
buffer::CpuBufferPool,
buffer::{
allocator::{CpuBufferAllocator, CpuBufferAllocatorCreateInfo},
BufferUsage,
},
command_buffer::{
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, CommandBufferUsage,
RenderPassBeginInfo, SubpassContents,
Expand Down Expand Up @@ -171,8 +164,16 @@ fn main() {

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

// Vertex Buffer Pool
let buffer_pool: CpuBufferPool<Vertex> = CpuBufferPool::vertex_buffer(memory_allocator);
// Using a buffer allocator allows multiple buffers to be "in-flight" simultaneously and is
// suited to highly dynamic data like vertex, index and uniform buffers.
let buffer_allocator = CpuBufferAllocator::new(
memory_allocator,
CpuBufferAllocatorCreateInfo {
// We want to use the allocated subbuffers as vertex buffers.
buffer_usage: BufferUsage::VERTEX_BUFFER,
..Default::default()
},
);

mod vs {
vulkano_shaders::shader! {
Expand Down Expand Up @@ -335,8 +336,8 @@ fn main() {
];
let num_vertices = data.len() as u32;

// Allocate a new chunk from buffer_pool
let buffer = buffer_pool.from_iter(data.to_vec()).unwrap();
// Allocate a new subbuffer using the buffer allocator.
let buffer = buffer_allocator.from_iter(data.iter().copied()).unwrap();
let mut builder = AutoCommandBufferBuilder::primary(
&command_buffer_allocator,
queue.queue_family_index(),
Expand Down
27 changes: 17 additions & 10 deletions examples/src/bin/indirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
use bytemuck::{Pod, Zeroable};
use std::sync::Arc;
use vulkano::{
buffer::{BufferUsage, CpuBufferPool},
buffer::{
allocator::{CpuBufferAllocator, CpuBufferAllocatorCreateInfo},
BufferUsage,
},
command_buffer::{
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, CommandBufferUsage,
DrawIndirectCommand, RenderPassBeginInfo, SubpassContents,
Expand All @@ -42,7 +45,7 @@ use vulkano::{
image::{view::ImageView, ImageAccess, ImageUsage, SwapchainImage},
impl_vertex,
instance::{Instance, InstanceCreateInfo},
memory::allocator::{MemoryUsage, StandardMemoryAllocator},
memory::allocator::StandardMemoryAllocator,
pipeline::{
graphics::{
input_assembly::InputAssemblyState,
Expand Down Expand Up @@ -256,17 +259,21 @@ fn main() {

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

// Each frame we generate a new set of vertices and each frame we need a new DrawIndirectCommand struct to
// set the number of vertices to draw
let indirect_args_pool: CpuBufferPool<DrawIndirectCommand> = CpuBufferPool::new(
// Each frame we generate a new set of vertices and each frame we need a new
// DrawIndirectCommand struct to set the number of vertices to draw.
let indirect_args_pool = CpuBufferAllocator::new(
memory_allocator.clone(),
BufferUsage::INDIRECT_BUFFER | BufferUsage::STORAGE_BUFFER,
MemoryUsage::Upload,
CpuBufferAllocatorCreateInfo {
buffer_usage: BufferUsage::INDIRECT_BUFFER | BufferUsage::STORAGE_BUFFER,
..Default::default()
},
);
let vertex_pool: CpuBufferPool<Vertex> = CpuBufferPool::new(
let vertex_pool = CpuBufferAllocator::new(
memory_allocator,
BufferUsage::STORAGE_BUFFER | BufferUsage::VERTEX_BUFFER,
MemoryUsage::Upload,
CpuBufferAllocatorCreateInfo {
buffer_usage: BufferUsage::STORAGE_BUFFER | BufferUsage::VERTEX_BUFFER,
..Default::default()
},
);

let compute_pipeline = ComputePipeline::new(
Expand Down
15 changes: 10 additions & 5 deletions examples/src/bin/teapot/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use cgmath::{Matrix3, Matrix4, Point3, Rad, Vector3};
use examples::{Normal, Vertex, INDICES, NORMALS, VERTICES};
use std::{sync::Arc, time::Instant};
use vulkano::{
buffer::{BufferUsage, CpuAccessibleBuffer, CpuBufferPool, TypedBufferAccess},
buffer::{
allocator::{CpuBufferAllocator, CpuBufferAllocatorCreateInfo},
BufferUsage, CpuAccessibleBuffer, TypedBufferAccess,
},
command_buffer::{
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, CommandBufferUsage,
RenderPassBeginInfo, SubpassContents,
Expand All @@ -26,7 +29,7 @@ use vulkano::{
format::Format,
image::{view::ImageView, AttachmentImage, ImageAccess, ImageUsage, SwapchainImage},
instance::{Instance, InstanceCreateInfo},
memory::allocator::{MemoryUsage, StandardMemoryAllocator},
memory::allocator::StandardMemoryAllocator,
pipeline::{
graphics::{
depth_stencil::DepthStencilState,
Expand Down Expand Up @@ -180,10 +183,12 @@ fn main() {
)
.unwrap();

let uniform_buffer = CpuBufferPool::<vs::ty::Data>::new(
let uniform_buffer = CpuBufferAllocator::new(
memory_allocator.clone(),
BufferUsage::UNIFORM_BUFFER,
MemoryUsage::Upload,
CpuBufferAllocatorCreateInfo {
buffer_usage: BufferUsage::UNIFORM_BUFFER,
..Default::default()
},
);

let vs = vs::load(device.clone()).unwrap();
Expand Down
Loading

0 comments on commit fe01ddd

Please # to comment.