Skip to content

Commit

Permalink
Unify all buffer types (#2127)
Browse files Browse the repository at this point in the history
* `CpuBufferPool` revamp

* Fix oopsie

* Fix docs

* Add `Subbuffer`

* Move `Buffer` to `buffer` module

* Replace all buffer types with `Subbuffer`

* Add constraints to `Index` and `QueryResultElement` traits

* Fix oopsies

* Fix examples

* Fix doc tests

* Rename `_unsized` to `_slice`

* Rename `CpuBufferAllocator`

* Implement `DeviceOwned` for `SubbufferAllocator`

* Use better generic type params

* Fix requirement message

* Fix docs

* Fix missing buffer alignment for texel buffer usage

* Add more docs to `Subbuffer`

* Add more utility functions to `Subbuffer`

* Remove superfluous `dst_offset`s

* Murder `FillBufferInfo`

* Fix example

* Add `Subbuffer::range`

* Fix wrong atom size alignment check

* Specify all fields in create infos derived from `BufferAllocateInfo`
  • Loading branch information
marc0246 authored Jan 12, 2023
1 parent fcb46e6 commit abfde84
Show file tree
Hide file tree
Showing 80 changed files with 2,904 additions and 4,051 deletions.
10 changes: 6 additions & 4 deletions examples/src/bin/basic-compute-shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// GPU", or *GPGPU*. This is what this example demonstrates.

use vulkano::{
buffer::{BufferUsage, CpuAccessibleBuffer},
buffer::{Buffer, BufferAllocateInfo, BufferUsage},
command_buffer::{
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, CommandBufferUsage,
},
Expand Down Expand Up @@ -156,10 +156,12 @@ fn main() {
// Iterator that produces the data.
let data_iter = 0..65536u32;
// Builds the buffer and fills it with this iterator.
CpuAccessibleBuffer::from_iter(
Buffer::from_iter(
&memory_allocator,
BufferUsage::STORAGE_BUFFER,
false,
BufferAllocateInfo {
buffer_usage: BufferUsage::STORAGE_BUFFER,
..Default::default()
},
data_iter,
)
.unwrap()
Expand Down
12 changes: 7 additions & 5 deletions examples/src/bin/buffer-allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// notice may not be copied, modified, or distributed except
// according to those terms.

// Modified triangle example to show `CpuBufferAllocator`.
// Modified triangle example to show `SubbufferAllocator`.

use bytemuck::{Pod, Zeroable};
use std::{
Expand All @@ -16,7 +16,7 @@ use std::{
};
use vulkano::{
buffer::{
allocator::{CpuBufferAllocator, CpuBufferAllocatorCreateInfo},
allocator::{SubbufferAllocator, SubbufferAllocatorCreateInfo},
BufferUsage,
},
command_buffer::{
Expand Down Expand Up @@ -165,9 +165,9 @@ fn main() {

// 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(
let buffer_allocator = SubbufferAllocator::new(
memory_allocator,
CpuBufferAllocatorCreateInfo {
SubbufferAllocatorCreateInfo {
// We want to use the allocated subbuffers as vertex buffers.
buffer_usage: BufferUsage::VERTEX_BUFFER,
..Default::default()
Expand Down Expand Up @@ -336,7 +336,9 @@ fn main() {
let num_vertices = data.len() as u32;

// Allocate a new subbuffer using the buffer allocator.
let buffer = buffer_allocator.from_iter(data.iter().copied()).unwrap();
let buffer = buffer_allocator.allocate_slice(data.len() as _).unwrap();
buffer.write().unwrap().copy_from_slice(&data);

let mut builder = AutoCommandBufferBuilder::primary(
&command_buffer_allocator,
queue.queue_family_index(),
Expand Down
22 changes: 11 additions & 11 deletions examples/src/bin/deferred/frame/ambient_lighting_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use std::sync::Arc;
use vulkano::{
buffer::{BufferUsage, CpuAccessibleBuffer, TypedBufferAccess},
buffer::{Buffer, BufferAllocateInfo, BufferUsage, Subbuffer},
command_buffer::{
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder,
CommandBufferInheritanceInfo, CommandBufferUsage, SecondaryAutoCommandBuffer,
Expand Down Expand Up @@ -37,7 +37,7 @@ use super::LightingVertex;
/// Allows applying an ambient lighting to a scene.
pub struct AmbientLightingSystem {
gfx_queue: Arc<Queue>,
vertex_buffer: Arc<CpuAccessibleBuffer<[LightingVertex]>>,
vertex_buffer: Subbuffer<[LightingVertex]>,
subpass: Subpass,
pipeline: Arc<GraphicsPipeline>,
command_buffer_allocator: Arc<StandardCommandBufferAllocator>,
Expand Down Expand Up @@ -66,15 +66,15 @@ impl AmbientLightingSystem {
position: [3.0, -1.0],
},
];
let vertex_buffer = {
CpuAccessibleBuffer::from_iter(
memory_allocator,
BufferUsage::VERTEX_BUFFER,
false,
vertices,
)
.expect("failed to create buffer")
};
let vertex_buffer = Buffer::from_iter(
memory_allocator,
BufferAllocateInfo {
buffer_usage: BufferUsage::VERTEX_BUFFER,
..Default::default()
},
vertices,
)
.expect("failed to create buffer");

let pipeline = {
let vs = vs::load(gfx_queue.device().clone()).expect("failed to create shader module");
Expand Down
12 changes: 7 additions & 5 deletions examples/src/bin/deferred/frame/directional_lighting_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use cgmath::Vector3;
use std::sync::Arc;
use vulkano::{
buffer::{BufferUsage, CpuAccessibleBuffer, TypedBufferAccess},
buffer::{Buffer, BufferAllocateInfo, BufferUsage, Subbuffer},
command_buffer::{
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder,
CommandBufferInheritanceInfo, CommandBufferUsage, SecondaryAutoCommandBuffer,
Expand Down Expand Up @@ -38,7 +38,7 @@ use super::LightingVertex;
/// Allows applying a directional light source to a scene.
pub struct DirectionalLightingSystem {
gfx_queue: Arc<Queue>,
vertex_buffer: Arc<CpuAccessibleBuffer<[LightingVertex]>>,
vertex_buffer: Subbuffer<[LightingVertex]>,
subpass: Subpass,
pipeline: Arc<GraphicsPipeline>,
command_buffer_allocator: Arc<StandardCommandBufferAllocator>,
Expand Down Expand Up @@ -68,10 +68,12 @@ impl DirectionalLightingSystem {
},
];
let vertex_buffer = {
CpuAccessibleBuffer::from_iter(
Buffer::from_iter(
memory_allocator,
BufferUsage::VERTEX_BUFFER,
false,
BufferAllocateInfo {
buffer_usage: BufferUsage::VERTEX_BUFFER,
..Default::default()
},
vertices,
)
.expect("failed to create buffer")
Expand Down
22 changes: 11 additions & 11 deletions examples/src/bin/deferred/frame/point_lighting_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use cgmath::{Matrix4, Vector3};
use std::sync::Arc;
use vulkano::{
buffer::{BufferUsage, CpuAccessibleBuffer, TypedBufferAccess},
buffer::{Buffer, BufferAllocateInfo, BufferUsage, Subbuffer},
command_buffer::{
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder,
CommandBufferInheritanceInfo, CommandBufferUsage, SecondaryAutoCommandBuffer,
Expand All @@ -37,7 +37,7 @@ use super::LightingVertex;

pub struct PointLightingSystem {
gfx_queue: Arc<Queue>,
vertex_buffer: Arc<CpuAccessibleBuffer<[LightingVertex]>>,
vertex_buffer: Subbuffer<[LightingVertex]>,
subpass: Subpass,
pipeline: Arc<GraphicsPipeline>,
command_buffer_allocator: Arc<StandardCommandBufferAllocator>,
Expand Down Expand Up @@ -66,15 +66,15 @@ impl PointLightingSystem {
position: [3.0, -1.0],
},
];
let vertex_buffer = {
CpuAccessibleBuffer::from_iter(
memory_allocator,
BufferUsage::VERTEX_BUFFER,
false,
vertices,
)
.expect("failed to create buffer")
};
let vertex_buffer = Buffer::from_iter(
memory_allocator,
BufferAllocateInfo {
buffer_usage: BufferUsage::VERTEX_BUFFER,
..Default::default()
},
vertices,
)
.expect("failed to create buffer");

let pipeline = {
let vs = vs::load(gfx_queue.device().clone()).expect("failed to create shader module");
Expand Down
22 changes: 11 additions & 11 deletions examples/src/bin/deferred/triangle_draw_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use bytemuck::{Pod, Zeroable};
use std::sync::Arc;
use vulkano::{
buffer::{BufferUsage, CpuAccessibleBuffer, TypedBufferAccess},
buffer::{Buffer, BufferAllocateInfo, BufferUsage, Subbuffer},
command_buffer::{
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder,
CommandBufferInheritanceInfo, CommandBufferUsage, SecondaryAutoCommandBuffer,
Expand All @@ -31,7 +31,7 @@ use vulkano::{

pub struct TriangleDrawSystem {
gfx_queue: Arc<Queue>,
vertex_buffer: Arc<CpuAccessibleBuffer<[TriangleVertex]>>,
vertex_buffer: Subbuffer<[TriangleVertex]>,
subpass: Subpass,
pipeline: Arc<GraphicsPipeline>,
command_buffer_allocator: Arc<StandardCommandBufferAllocator>,
Expand All @@ -56,15 +56,15 @@ impl TriangleDrawSystem {
position: [0.25, -0.1],
},
];
let vertex_buffer = {
CpuAccessibleBuffer::from_iter(
memory_allocator,
BufferUsage::VERTEX_BUFFER,
false,
vertices,
)
.expect("failed to create buffer")
};
let vertex_buffer = Buffer::from_iter(
memory_allocator,
BufferAllocateInfo {
buffer_usage: BufferUsage::VERTEX_BUFFER,
..Default::default()
},
vertices,
)
.expect("failed to create buffer");

let pipeline = {
let vs = vs::load(gfx_queue.device().clone()).expect("failed to create shader module");
Expand Down
20 changes: 12 additions & 8 deletions examples/src/bin/dynamic-buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use std::{iter::repeat, mem::size_of};
use vulkano::{
buffer::{BufferUsage, CpuAccessibleBuffer},
buffer::{Buffer, BufferAllocateInfo, BufferUsage},
command_buffer::{
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, CommandBufferUsage,
},
Expand Down Expand Up @@ -164,18 +164,22 @@ fn main() {
aligned_data
};

let input_buffer = CpuAccessibleBuffer::from_iter(
let input_buffer = Buffer::from_iter(
&memory_allocator,
BufferUsage::UNIFORM_BUFFER,
false,
aligned_data.into_iter(),
BufferAllocateInfo {
buffer_usage: BufferUsage::UNIFORM_BUFFER,
..Default::default()
},
aligned_data,
)
.unwrap();

let output_buffer = CpuAccessibleBuffer::from_iter(
let output_buffer = Buffer::from_iter(
&memory_allocator,
BufferUsage::STORAGE_BUFFER,
false,
BufferAllocateInfo {
buffer_usage: BufferUsage::STORAGE_BUFFER,
..Default::default()
},
(0..12).map(|_| 0u32),
)
.unwrap();
Expand Down
10 changes: 6 additions & 4 deletions examples/src/bin/dynamic-local-size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use std::{fs::File, io::BufWriter, path::Path};
use vulkano::{
buffer::{BufferUsage, CpuAccessibleBuffer},
buffer::{Buffer, BufferAllocateInfo, BufferUsage},
command_buffer::{
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, CommandBufferUsage,
CopyImageToBufferInfo,
Expand Down Expand Up @@ -223,10 +223,12 @@ fn main() {
)
.unwrap();

let buf = CpuAccessibleBuffer::from_iter(
let buf = Buffer::from_iter(
&memory_allocator,
BufferUsage::TRANSFER_DST,
false,
BufferAllocateInfo {
buffer_usage: BufferUsage::TRANSFER_DST,
..Default::default()
},
(0..1024 * 1024 * 4).map(|_| 0u8),
)
.unwrap();
Expand Down
12 changes: 7 additions & 5 deletions examples/src/bin/gl-interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod linux {
time::Instant,
};
use vulkano::{
buffer::{BufferUsage, CpuAccessibleBuffer, TypedBufferAccess},
buffer::{Buffer, BufferAllocateInfo, BufferUsage, Subbuffer},
command_buffer::{
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder,
CommandBufferUsage, RenderPassBeginInfo, SemaphoreSubmitInfo, SubmitInfo,
Expand Down Expand Up @@ -409,7 +409,7 @@ mod linux {
Arc<vulkano::sampler::Sampler>,
Arc<GraphicsPipeline>,
StandardMemoryAllocator,
Arc<CpuAccessibleBuffer<[MyVertex]>>,
Subbuffer<[MyVertex]>,
) {
let library = VulkanLibrary::new().unwrap();
let required_extensions = vulkano_win::required_extensions(&library);
Expand Down Expand Up @@ -567,10 +567,12 @@ mod linux {
position: [0.5, 0.5],
},
];
let vertex_buffer = CpuAccessibleBuffer::<[MyVertex]>::from_iter(
let vertex_buffer = Buffer::from_iter(
&memory_allocator,
BufferUsage::VERTEX_BUFFER,
false,
BufferAllocateInfo {
buffer_usage: BufferUsage::VERTEX_BUFFER,
..Default::default()
},
vertices,
)
.unwrap();
Expand Down
18 changes: 11 additions & 7 deletions examples/src/bin/image-self-copy-blit/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use bytemuck::{Pod, Zeroable};
use std::{io::Cursor, sync::Arc};
use vulkano::{
buffer::{BufferUsage, CpuAccessibleBuffer, TypedBufferAccess},
buffer::{Buffer, BufferAllocateInfo, BufferUsage},
command_buffer::{
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, BlitImageInfo,
BufferImageCopy, ClearColorImageInfo, CommandBufferUsage, CopyBufferToImageInfo,
Expand Down Expand Up @@ -182,10 +182,12 @@ fn main() {
position: [0.5, 0.5],
},
];
let vertex_buffer = CpuAccessibleBuffer::<[Vertex]>::from_iter(
let vertex_buffer = Buffer::from_iter(
&memory_allocator,
BufferUsage::VERTEX_BUFFER,
false,
BufferAllocateInfo {
buffer_usage: BufferUsage::VERTEX_BUFFER,
..Default::default()
},
vertices,
)
.unwrap();
Expand Down Expand Up @@ -243,10 +245,12 @@ fn main() {
)
.unwrap();

let buffer = CpuAccessibleBuffer::from_iter(
let buffer = Buffer::from_iter(
&memory_allocator,
BufferUsage::TRANSFER_SRC,
false,
BufferAllocateInfo {
buffer_usage: BufferUsage::TRANSFER_SRC,
..Default::default()
},
image_data,
)
.unwrap();
Expand Down
10 changes: 6 additions & 4 deletions examples/src/bin/image/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use bytemuck::{Pod, Zeroable};
use std::{io::Cursor, sync::Arc};
use vulkano::{
buffer::{BufferUsage, CpuAccessibleBuffer, TypedBufferAccess},
buffer::{Buffer, BufferAllocateInfo, BufferUsage},
command_buffer::{
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, CommandBufferUsage,
PrimaryCommandBufferAbstract, RenderPassBeginInfo, SubpassContents,
Expand Down Expand Up @@ -180,10 +180,12 @@ fn main() {
position: [0.5, 0.5],
},
];
let vertex_buffer = CpuAccessibleBuffer::<[Vertex]>::from_iter(
let vertex_buffer = Buffer::from_iter(
&memory_allocator,
BufferUsage::VERTEX_BUFFER,
false,
BufferAllocateInfo {
buffer_usage: BufferUsage::VERTEX_BUFFER,
..Default::default()
},
vertices,
)
.unwrap();
Expand Down
Loading

0 comments on commit abfde84

Please # to comment.