Skip to content

Commit 6cac4ee

Browse files
committed
change: remove buffer usage flags
1 parent 09e19d0 commit 6cac4ee

10 files changed

Lines changed: 74 additions & 149 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "phobos"
3-
version = "0.9.2"
3+
version = "0.10.0"
44
edition = "2021"
55
license = "Apache-2.0"
66
description = "Fast, powerful Vulkan abstraction library"

examples/01_basic/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ impl ExampleApp for Basic {
9595
vertex_buffer: staged_buffer_upload(
9696
ctx.clone(),
9797
data.as_slice(),
98-
vk::BufferUsageFlags::VERTEX_BUFFER,
9998
)?,
10099
};
101100
ctx.device.set_name(&resources.vertex_buffer, "Vertex Buffer")?;
@@ -124,9 +123,9 @@ impl ExampleApp for Basic {
124123
let offscreen_pass = PassBuilder::render("offscreen")
125124
.color([1.0, 0.0, 0.0, 1.0])
126125
.clear_color_attachment(&offscreen, ClearColor::Float([0.0, 0.0, 0.0, 0.0]))?
127-
.execute_fn(|mut cmd, ifc, _bindings, _| {
126+
.execute_fn(|mut cmd, pool, _bindings, _| {
128127
// Our pass will render a fullscreen quad that 'clears' the screen, just so we can test pipeline creation
129-
let mut buffer = ifc.allocate_scratch_vbo(
128+
let mut buffer = pool.allocate_scratch_buffer(
130129
(vertices.len() * std::mem::size_of::<f32>()) as vk::DeviceSize,
131130
)?;
132131
let slice = buffer.mapped_slice::<f32>()?;
@@ -148,7 +147,7 @@ impl ExampleApp for Basic {
148147
offscreen_pass.output(&offscreen).unwrap(),
149148
PipelineStage::FRAGMENT_SHADER,
150149
)
151-
.execute_fn(|cmd, _ifc, bindings, _| {
150+
.execute_fn(|cmd, _pool, bindings, _| {
152151
cmd.full_viewport_scissor()
153152
.bind_graphics_pipeline("sample")?
154153
.resolve_and_bind_sampled_image(

examples/02_headless_compute/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ impl ExampleApp for Compute {
2626
ctx.device,
2727
&mut ctx.allocator,
2828
(1024 * 4 * std::mem::size_of::<f32>()) as u64,
29-
vk::BufferUsageFlags::STORAGE_BUFFER | vk::BufferUsageFlags::TRANSFER_DST,
3029
MemoryType::CpuToGpu,
3130
)?;
3231

examples/03_raytracing/main.rs

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use anyhow::Result;
44
use ash::vk;
55
use glam::{Mat4, Vec3};
66
use log::{info, trace};
7-
87
use phobos::graph::pass::ClearColor;
98
use phobos::image;
109
use phobos::pipeline::raytracing::RayTracingPipelineBuilder;
@@ -15,7 +14,7 @@ use phobos::sync::submit_batch::SubmitBatch;
1514
use phobos::util::align::align;
1615

1716
use crate::example_runner::{
18-
Context, create_shader, ExampleApp, ExampleRunner, load_spirv_file, WindowContext,
17+
create_shader, load_spirv_file, Context, ExampleApp, ExampleRunner, WindowContext,
1918
};
2019

2120
#[path = "../example_runner/lib.rs"]
@@ -42,7 +41,6 @@ struct RaytracingSample {
4241
fn make_input_buffer<T: Copy>(
4342
ctx: &mut Context,
4443
data: &[T],
45-
usage: vk::BufferUsageFlags,
4644
alignment: Option<u64>,
4745
name: &str,
4846
) -> Result<Buffer> {
@@ -51,19 +49,13 @@ fn make_input_buffer<T: Copy>(
5149
ctx.device.clone(),
5250
&mut ctx.allocator,
5351
(data.len() * std::mem::size_of::<T>()) as u64,
54-
vk::BufferUsageFlags::ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_KHR
55-
| vk::BufferUsageFlags::TRANSFER_DST
56-
| usage,
5752
MemoryType::CpuToGpu,
5853
)?,
5954
Some(alignment) => Buffer::new_aligned(
6055
ctx.device.clone(),
6156
&mut ctx.allocator,
6257
(data.len() * std::mem::size_of::<T>()) as u64,
6358
alignment,
64-
vk::BufferUsageFlags::ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_KHR
65-
| vk::BufferUsageFlags::TRANSFER_DST
66-
| usage,
6759
MemoryType::CpuToGpu,
6860
)?,
6961
};
@@ -80,12 +72,12 @@ fn make_vertex_buffer(ctx: &mut Context) -> Result<Buffer> {
8072
-1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0,
8173
1.0,
8274
];
83-
make_input_buffer(ctx, &vertices, vk::BufferUsageFlags::VERTEX_BUFFER, None, "Vertex Buffer")
75+
make_input_buffer(ctx, &vertices, None, "Vertex Buffer")
8476
}
8577

8678
fn make_index_buffer(ctx: &mut Context) -> Result<Buffer> {
8779
let indices = (0..=5).collect::<Vec<u32>>();
88-
make_input_buffer(ctx, indices.as_slice(), vk::BufferUsageFlags::INDEX_BUFFER, None, "Index Buffer")
80+
make_input_buffer(ctx, indices.as_slice(), None, "Index Buffer")
8981
}
9082

9183
fn make_instance_buffer(ctx: &mut Context, blas: &AccelerationStructure) -> Result<Buffer> {
@@ -99,7 +91,7 @@ fn make_instance_buffer(ctx: &mut Context, blas: &AccelerationStructure) -> Resu
9991
.acceleration_structure(&blas, AccelerationStructureBuildType::Device)?;
10092
// The Vulkan spec states: For any element of pInfos[i].pGeometries or pInfos[i].ppGeometries with a geometryType of VK_GEOMETRY_TYPE_INSTANCES_KHR,
10193
// if geometry.arrayOfPointers is VK_FALSE, geometry.instances.data.deviceAddress must be aligned to 16 bytes
102-
make_input_buffer(ctx, std::slice::from_ref(&instance), Default::default(), Some(16), "Instance Buffer")
94+
make_input_buffer(ctx, std::slice::from_ref(&instance), Some(16), "Instance Buffer")
10395
}
10496

10597
fn blas_build_info<'a>(vertices: &Buffer, indices: &Buffer) -> AccelerationStructureBuildInfo<'a> {
@@ -148,19 +140,10 @@ fn make_acceleration_structure(
148140
build_info,
149141
prim_counts,
150142
)?;
151-
let buffer = Buffer::new_device_local(
152-
ctx.device.clone(),
153-
&mut ctx.allocator,
154-
sizes.size,
155-
vk::BufferUsageFlags::ACCELERATION_STRUCTURE_STORAGE_KHR,
156-
)?;
143+
let buffer = Buffer::new_device_local(ctx.device.clone(), &mut ctx.allocator, sizes.size)?;
157144
// Allocate scratch buffer for building the acceleration structure
158-
let scratch_buffer = Buffer::new_device_local(
159-
ctx.device.clone(),
160-
&mut ctx.allocator,
161-
sizes.build_scratch_size,
162-
vk::BufferUsageFlags::STORAGE_BUFFER,
163-
)?;
145+
let scratch_buffer =
146+
Buffer::new_device_local(ctx.device.clone(), &mut ctx.allocator, sizes.build_scratch_size)?;
164147
let acceleration_structure = AccelerationStructure::new(
165148
ctx.device.clone(),
166149
build_info.ty(),
@@ -182,12 +165,7 @@ fn make_compacted(
182165
size: u64,
183166
) -> Result<(AccelerationStructure, Buffer)> {
184167
// Create final compacted acceleration structures
185-
let compact_buffer = Buffer::new_device_local(
186-
ctx.device.clone(),
187-
&mut ctx.allocator,
188-
size,
189-
vk::BufferUsageFlags::ACCELERATION_STRUCTURE_STORAGE_KHR,
190-
)?;
168+
let compact_buffer = Buffer::new_device_local(ctx.device.clone(), &mut ctx.allocator, size)?;
191169
let compact_as = AccelerationStructure::new(
192170
ctx.device.clone(),
193171
accel.ty(),
@@ -361,12 +339,12 @@ impl ExampleApp for RaytracingSample {
361339
let render_pass = PassBuilder::render("copy")
362340
.clear_color_attachment(&swap, ClearColor::Float([0.0, 0.0, 0.0, 0.0]))?
363341
.sample_image(rt_pass.output(&rt_image).unwrap(), PipelineStage::FRAGMENT_SHADER)
364-
.execute_fn(|cmd, ifc, bindings, _| {
342+
.execute_fn(|cmd, pool, bindings, _| {
365343
let vertices: Vec<f32> = vec![
366344
-1.0, 1.0, 0.0, 1.0, -1.0, -1.0, 0.0, 0.0, 1.0, -1.0, 1.0, 0.0, -1.0, 1.0, 0.0,
367345
1.0, 1.0, -1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0,
368346
];
369-
let mut vtx_buffer = ifc.allocate_scratch_vbo(
347+
let mut vtx_buffer = pool.allocate_scratch_buffer(
370348
(vertices.len() * std::mem::size_of::<f32>()) as vk::DeviceSize,
371349
)?;
372350
let slice = vtx_buffer.mapped_slice::<f32>()?;

examples/04_fsr2/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ impl ExampleApp for Fsr2Sample {
376376
-1.0, 1.0, 0.0, 1.0, -1.0, -1.0, 0.0, 0.0, 1.0, -1.0, 1.0, 0.0, -1.0, 1.0, 0.0,
377377
1.0, 1.0, -1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0,
378378
];
379-
let mut vtx_buffer = local_pool.allocate_scratch_vbo(
379+
let mut vtx_buffer = local_pool.allocate_scratch_buffer(
380380
(vertices.len() * std::mem::size_of::<f32>()) as vk::DeviceSize,
381381
)?;
382382
let slice = vtx_buffer.mapped_slice::<f32>()?;

examples/example_runner/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use std::fs;
44
use std::fs::File;
5-
use std::io::{Read, Write};
5+
use std::io::{Read};
66
use std::path::Path;
77

88
use anyhow::{bail, Result};
@@ -175,7 +175,7 @@ macro_rules! ubo_struct_assign {
175175
$($fname:$ftype,)*
176176
}
177177

178-
let mut buffer_name = $pool.allocate_scratch_ubo(std::mem::size_of::<$name>() as vk::DeviceSize)?;
178+
let mut buffer_name = $pool.allocate_scratch_buffer(std::mem::size_of::<$name>() as vk::DeviceSize)?;
179179
let $var = buffer_name.mapped_slice::<$name>()?;
180180
let mut $var = $var.get_mut(0).unwrap();
181181

@@ -206,13 +206,11 @@ pub fn create_shader(path: &str, stage: vk::ShaderStageFlags) -> ShaderCreateInf
206206
pub fn staged_buffer_upload<T: Copy>(
207207
mut ctx: Context,
208208
data: &[T],
209-
usage: vk::BufferUsageFlags,
210209
) -> Result<Buffer> {
211210
let staging = Buffer::new(
212211
ctx.device.clone(),
213212
&mut ctx.allocator,
214213
data.len() as u64 * std::mem::size_of::<T>() as u64,
215-
vk::BufferUsageFlags::TRANSFER_SRC,
216214
MemoryType::CpuToGpu,
217215
)?;
218216

@@ -223,7 +221,6 @@ pub fn staged_buffer_upload<T: Copy>(
223221
ctx.device,
224222
&mut ctx.allocator,
225223
staging.size(),
226-
vk::BufferUsageFlags::TRANSFER_DST | usage,
227224
)?;
228225
let view = buffer.view_full();
229226

src/allocator/scratch_allocator.rs

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,9 @@ use anyhow::Result;
3232
use ash::vk;
3333
use gpu_allocator::AllocationError::OutOfMemory;
3434

35-
use crate::{Allocator, Buffer, BufferView, DefaultAllocator, Device, Error, MemoryType};
36-
use crate::Error::AllocationError;
3735
use crate::pool::Poolable;
38-
39-
/// Info needed to create a scratch allocator in a resource pool
40-
#[derive(Copy, Clone, Hash, Eq, PartialEq)]
41-
pub struct ScratchAllocatorCreateInfo {
42-
/// Usage flags for the internally created buffer
43-
pub usage: vk::BufferUsageFlags,
44-
}
36+
use crate::Error::AllocationError;
37+
use crate::{Allocator, Buffer, BufferView, DefaultAllocator, Device, Error, MemoryType};
4538

4639
/// A linear allocator used for short-lived resources. A good example of such a resource is a buffer
4740
/// that needs to be updated every frame, like a uniform buffer for transform data.
@@ -82,8 +75,10 @@ pub struct ScratchAllocator<A: Allocator = DefaultAllocator> {
8275
}
8376

8477
impl<A: Allocator> ScratchAllocator<A> {
85-
/// Create a new scratch allocator with a specified maximum capacity. All possible usages for buffers allocated from this should be
86-
/// given in the usage flags. The actual allocated size may be slightly larger to satisfy alignment requirements.
78+
/// Create a new scratch allocator with a specified maximum capacity.
79+
/// The actual allocated size may be slightly larger to satisfy alignment requirements.
80+
/// Alignment requirement is the maximum alignment needed for any buffer type. For more granular control, use
81+
/// [`Self::new_with_alignment()`]
8782
/// # Errors
8883
/// * Fails if the internal allocation fails. This is possible when VRAM runs out.
8984
/// * Fails if the memory heap used for the allocation is not mappable.
@@ -92,39 +87,21 @@ impl<A: Allocator> ScratchAllocator<A> {
9287
/// # use phobos::*;
9388
/// # use anyhow::Result;
9489
/// fn make_scratch_allocator<A: Allocator>(device: Device, alloc: &mut A) -> Result<ScratchAllocator<A>> {
95-
/// ScratchAllocator::new(device, alloc, 1024 as usize, vk::BufferUsageFlags::UNIFORM_BUFFER)
90+
/// ScratchAllocator::new(device, alloc, 1024 as usize)
9691
/// }
9792
/// ```
9893
pub fn new(
9994
device: Device,
10095
allocator: &mut A,
10196
max_size: impl Into<vk::DeviceSize>,
102-
usage: vk::BufferUsageFlags,
10397
) -> Result<Self> {
104-
let buffer = Buffer::new(device.clone(), allocator, max_size, usage, MemoryType::CpuToGpu)?;
105-
let mut alignment = 0;
106-
if usage
107-
.intersects(vk::BufferUsageFlags::VERTEX_BUFFER | vk::BufferUsageFlags::INDEX_BUFFER)
108-
{
109-
alignment = alignment.max(16);
110-
} else if usage.contains(vk::BufferUsageFlags::UNIFORM_BUFFER) {
111-
alignment = alignment.max(
112-
device
113-
.properties()
114-
.limits
115-
.min_uniform_buffer_offset_alignment,
116-
);
117-
} else if usage.contains(vk::BufferUsageFlags::STORAGE_BUFFER) {
118-
alignment = alignment.max(
119-
device
120-
.properties()
121-
.limits
122-
.min_storage_buffer_offset_alignment,
123-
);
124-
} else {
125-
unimplemented!()
126-
};
98+
Self::new_with_alignment(device, allocator, max_size, 256)
99+
}
127100

101+
/// Create a new scratch allocator with given alignment. The alignment used must be large enough to satisfy the alignment requirements
102+
/// of all buffer usage flags buffers from this allocator will be used with.
103+
pub fn new_with_alignment(device: Device, allocator: &mut A, max_size: impl Into<vk::DeviceSize>, alignment: u64) -> Result<Self> {
104+
let buffer = Buffer::new(device, allocator, max_size, MemoryType::CpuToGpu)?;
128105
if buffer.is_mapped() {
129106
Ok(Self {
130107
buffer,
@@ -182,7 +159,7 @@ impl<A: Allocator> ScratchAllocator<A> {
182159
/// }
183160
///
184161
/// fn use_scratch_allocator<A: Allocator>(device: Device, alloc: &mut A) -> Result<()> {
185-
/// let mut allocator = ScratchAllocator::new(device.clone(), alloc, 128 as u64, vk::BufferUsageFlags::UNIFORM_BUFFER)?;
162+
/// let mut allocator = ScratchAllocator::new(device.clone(), alloc, 128 as u64)?;
186163
/// let buffer: BufferView = allocator.allocate(128 as u64)?;
187164
/// let mut fence = use_the_buffer(buffer);
188165
/// fence.wait()?;
@@ -199,7 +176,7 @@ impl<A: Allocator> ScratchAllocator<A> {
199176
}
200177

201178
impl<A: Allocator> Poolable for ScratchAllocator<A> {
202-
type Key = ScratchAllocatorCreateInfo;
179+
type Key = ();
203180

204181
fn on_release(&mut self) {
205182
unsafe { self.reset() }

src/pipeline/raytracing.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,6 @@ impl<A: Allocator> ShaderBindingTable<A> {
9999
device.clone(),
100100
&mut allocator,
101101
sbt_size as u64,
102-
vk::BufferUsageFlags::SHADER_BINDING_TABLE_KHR
103-
| vk::BufferUsageFlags::TRANSFER_DST
104-
| vk::BufferUsageFlags::TRANSFER_SRC,
105102
MemoryType::CpuToGpu,
106103
)?;
107104
let handles = unsafe {

0 commit comments

Comments
 (0)