Skip to content

Commit

Permalink
Add configurable buffer alignment
Browse files Browse the repository at this point in the history
(cherry picked from commit cb7b623)
  • Loading branch information
attackgoat committed Dec 28, 2022
1 parent e2b7350 commit 6cdc3a0
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 88 deletions.
6 changes: 1 addition & 5 deletions contrib/screen-13-fx/src/bitmap_font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,7 @@ impl BitmapFont {
let vertex_buf_len = 120 * text.chars().count() as vk::DeviceSize;
let mut vertex_buf = self
.cache
.lease(BufferInfo {
size: vertex_buf_len,
usage: vk::BufferUsageFlags::VERTEX_BUFFER,
can_map: true,
})
.lease(BufferInfo::new_mappable(vertex_buf_len, vk::BufferUsageFlags::VERTEX_BUFFER))
.unwrap();

let mut vertex_count = 0;
Expand Down
9 changes: 4 additions & 5 deletions contrib/screen-13-fx/src/image_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,10 @@ impl ImageLoader {
//trace!("pixel_buf_len={pixel_buf_len} pixel_buf_stride={pixel_buf_stride}");

// Lease a temporary buffer from the cache pool
let mut pixel_buf = self.pool.lease(BufferInfo {
size: pixel_buf_len,
usage: vk::BufferUsageFlags::STORAGE_BUFFER,
can_map: true,
})?;
let mut pixel_buf = self.pool.lease(BufferInfo::new_mappable(
pixel_buf_len,
vk::BufferUsageFlags::STORAGE_BUFFER,
))?;

{
let pixel_buf =
Expand Down
18 changes: 3 additions & 15 deletions contrib/screen-13-imgui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,7 @@ impl ImGui {
let indices = cast_slice(draw_list.idx_buffer());
let mut index_buf = self
.pool
.lease(BufferInfo {
size: indices.len() as _,
usage: vk::BufferUsageFlags::INDEX_BUFFER,
can_map: true,
})
.lease(BufferInfo::new_mappable(indices.len() as _, vk::BufferUsageFlags::INDEX_BUFFER))
.unwrap();

{
Expand All @@ -136,11 +132,7 @@ impl ImGui {
let vertex_buf_len = vertices.len() * 20;
let mut vertex_buf = self
.pool
.lease(BufferInfo {
size: vertex_buf_len as _,
usage: vk::BufferUsageFlags::VERTEX_BUFFER,
can_map: true,
})
.lease(BufferInfo::new_mappable(vertex_buf_len as _, vk::BufferUsageFlags::VERTEX_BUFFER))
.unwrap();

{
Expand Down Expand Up @@ -268,11 +260,7 @@ impl ImGui {
let temp_buf_len = texture.data.len();
let mut temp_buf = self
.pool
.lease(BufferInfo {
size: temp_buf_len as _,
usage: vk::BufferUsageFlags::TRANSFER_SRC,
can_map: true,
})
.lease(BufferInfo::new_mappable(temp_buf_len as _, vk::BufferUsageFlags::TRANSFER_SRC))
.unwrap();

{
Expand Down
28 changes: 18 additions & 10 deletions examples/fuzzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,13 @@ fn record_accel_struct_builds(frame: &mut FrameContext, pool: &mut HashPool) {
)
.unwrap();

let scratch_buf_padding = frame
let accel_struct_scratch_offset_alignment = frame
.device
.accel_struct_properties
.as_ref()
.unwrap()
.min_accel_struct_scratch_offset_alignment as vk::DeviceSize;
.min_accel_struct_scratch_offset_alignment
as vk::DeviceSize;

// Lease and bind a bunch of bottom-level acceleration structures and add to instance buffer
let mut blas_nodes = Vec::with_capacity(BLAS_COUNT as _);
Expand Down Expand Up @@ -195,10 +196,14 @@ fn record_accel_struct_builds(frame: &mut FrameContext, pool: &mut HashPool) {

let blas_node = frame.render_graph.bind_node(blas);
let scratch_buf = frame.render_graph.bind_node(
pool.lease(BufferInfo::new(
blas_size.build_size + scratch_buf_padding,
vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS | vk::BufferUsageFlags::STORAGE_BUFFER,
))
pool.lease(
BufferInfo::new(
blas_size.build_size,
vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS
| vk::BufferUsageFlags::STORAGE_BUFFER,
)
.alignment(accel_struct_scratch_offset_alignment),
)
.unwrap(),
);

Expand Down Expand Up @@ -228,10 +233,13 @@ fn record_accel_struct_builds(frame: &mut FrameContext, pool: &mut HashPool) {
.unwrap();
let tlas_node = frame.render_graph.bind_node(tlas);
let tlas_scratch_buf = frame.render_graph.bind_node(
pool.lease(BufferInfo::new(
tlas_size.build_size + scratch_buf_padding,
vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS | vk::BufferUsageFlags::STORAGE_BUFFER,
))
pool.lease(
BufferInfo::new(
tlas_size.build_size,
vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS | vk::BufferUsageFlags::STORAGE_BUFFER,
)
.alignment(accel_struct_scratch_offset_alignment),
)
.unwrap(),
);

Expand Down
21 changes: 12 additions & 9 deletions examples/ray_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,17 +505,18 @@ fn main() -> anyhow::Result<()> {
// ------------------------------------------------------------------------------------------ //

let sbt_handle_size = align_up(shader_group_handle_size, shader_group_handle_alignment);
let sbt_rgen_size = align_up(sbt_handle_size, shader_group_base_alignment);
let sbt_hit_size = align_up(sbt_handle_size, shader_group_base_alignment);
let sbt_miss_size = align_up(2 * sbt_handle_size, shader_group_base_alignment);
let sbt_rgen_size = sbt_handle_size;
let sbt_hit_size = sbt_handle_size;
let sbt_miss_size = 2 * sbt_handle_size;
let sbt_buf = Arc::new({
let mut buf = Buffer::create(
&event_loop.device,
BufferInfo::new_mappable(
(sbt_rgen_size + sbt_hit_size + sbt_miss_size) as _,
vk::BufferUsageFlags::SHADER_BINDING_TABLE_KHR
| vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS,
),
)
.alignment(shader_group_base_alignment as _),
)
.unwrap();

Expand Down Expand Up @@ -659,7 +660,7 @@ fn main() -> anyhow::Result<()> {
// ------------------------------------------------------------------------------------------ //

{
let scratch_buf_padding = event_loop
let accel_struct_scratch_offset_alignment = event_loop
.device
.accel_struct_properties
.as_ref()
Expand All @@ -675,10 +676,11 @@ fn main() -> anyhow::Result<()> {
let scratch_buf = render_graph.bind_node(Buffer::create(
&event_loop.device,
BufferInfo::new(
blas_size.build_size + scratch_buf_padding,
blas_size.build_size,
vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS
| vk::BufferUsageFlags::STORAGE_BUFFER,
),
)
.alignment(accel_struct_scratch_offset_alignment),
)?);

render_graph
Expand Down Expand Up @@ -706,10 +708,11 @@ fn main() -> anyhow::Result<()> {
let scratch_buf = render_graph.bind_node(Buffer::create(
&event_loop.device,
BufferInfo::new(
tlas_size.build_size + scratch_buf_padding,
tlas_size.build_size,
vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS
| vk::BufferUsageFlags::STORAGE_BUFFER,
),
)
.alignment(accel_struct_scratch_offset_alignment),
)?);
let instance_node = render_graph.bind_node(&instance_buf);
let tlas_node = render_graph.bind_node(&tlas);
Expand Down
21 changes: 12 additions & 9 deletions examples/rt_triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,18 @@ fn main() -> anyhow::Result<()> {
// ------------------------------------------------------------------------------------------ //

let sbt_handle_size = align_up(shader_group_handle_size, shader_group_handle_alignment);
let sbt_rgen_size = align_up(sbt_handle_size, shader_group_base_alignment);
let sbt_hit_size = align_up(sbt_handle_size, shader_group_base_alignment);
let sbt_miss_size = align_up(2 * sbt_handle_size, shader_group_base_alignment);
let sbt_rgen_size = sbt_handle_size;
let sbt_hit_size = sbt_handle_size;
let sbt_miss_size = 2 * sbt_handle_size;
let sbt_buf = Arc::new({
let mut buf = Buffer::create(
&event_loop.device,
BufferInfo::new_mappable(
(sbt_rgen_size + sbt_hit_size + sbt_miss_size) as _,
vk::BufferUsageFlags::SHADER_BINDING_TABLE_KHR
| vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS,
),
)
.alignment(shader_group_base_alignment as _),
)
.unwrap();

Expand Down Expand Up @@ -324,7 +325,7 @@ fn main() -> anyhow::Result<()> {
// ------------------------------------------------------------------------------------------ //

{
let scratch_buf_padding = event_loop
let accel_struct_scratch_offset_alignment = event_loop
.device
.accel_struct_properties
.as_ref()
Expand All @@ -340,10 +341,11 @@ fn main() -> anyhow::Result<()> {
let scratch_buf = render_graph.bind_node(Buffer::create(
&event_loop.device,
BufferInfo::new(
blas_size.build_size + scratch_buf_padding,
blas_size.build_size,
vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS
| vk::BufferUsageFlags::STORAGE_BUFFER,
),
)
.alignment(accel_struct_scratch_offset_alignment),
)?);

render_graph
Expand Down Expand Up @@ -372,10 +374,11 @@ fn main() -> anyhow::Result<()> {
let scratch_buf = render_graph.bind_node(Buffer::create(
&event_loop.device,
BufferInfo::new(
tlas_size.build_size + scratch_buf_padding,
tlas_size.build_size,
vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS
| vk::BufferUsageFlags::STORAGE_BUFFER,
),
)
.alignment(accel_struct_scratch_offset_alignment),
)?);
let tlas_node = render_graph.bind_node(&tlas);

Expand Down
21 changes: 8 additions & 13 deletions src/driver/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,7 @@ impl Buffer {
})?
};
let mut requirements = unsafe { device.get_buffer_memory_requirements(buffer) };

if info
.usage
.contains(vk::BufferUsageFlags::SHADER_BINDING_TABLE_KHR)
{
// TODO: query device props
requirements.alignment = requirements.alignment.max(64);
}
requirements.alignment = requirements.alignment.max(info.alignment);

let memory_location = if info.can_map {
MemoryLocation::CpuToGpu
Expand Down Expand Up @@ -183,11 +176,7 @@ impl Buffer {
usage: vk::BufferUsageFlags,
slice: &[u8],
) -> Result<Self, DriverError> {
let info = BufferInfo {
can_map: true,
size: slice.len() as vk::DeviceSize,
usage,
};
let info = BufferInfo::new_mappable(slice.len() as _, usage);
let mut buffer = Self::create(device, info)?;

Self::copy_from_slice(&mut buffer, 0, slice);
Expand Down Expand Up @@ -419,6 +408,12 @@ impl Drop for Buffer {
pattern = "owned"
)]
pub struct BufferInfo {
/// Byte alignment of the base device address of the buffer.
///
/// Must be a power of two.
#[builder(default)]
pub alignment: vk::DeviceSize,

/// Size in bytes of the buffer to be created.
#[builder(default)]
pub size: vk::DeviceSize,
Expand Down
24 changes: 2 additions & 22 deletions src/graph/pass_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ pub type BindingOffset = u32;
/// Alias for the descriptor set index of a shader descriptor.
pub type DescriptorSetIndex = u32;

fn align_up_device_size(val: vk::DeviceSize, atom: vk::DeviceSize) -> vk::DeviceSize {
(val + atom - 1) & !(atom - 1)
}

/// Recording interface for acceleration structure commands.
///
/// This structure provides a strongly-typed set of methods which allow acceleration structures to
Expand Down Expand Up @@ -192,15 +188,7 @@ impl<'a> Acceleration<'a> {
.geometries(&tls.geometries)
.dst_acceleration_structure(*self.bindings[accel_struct_node])
.scratch_data(vk::DeviceOrHostAddressKHR {
device_address: align_up_device_size(
Buffer::device_address(&self.bindings[scratch_buf_node]),
self.device
.accel_struct_properties
.as_ref()
.expect("ray tracing feature must be enabled")
.min_accel_struct_scratch_offset_alignment
as _,
),
device_address: Buffer::device_address(&self.bindings[scratch_buf_node]),
});

self.device
Expand Down Expand Up @@ -267,15 +255,7 @@ impl<'a> Acceleration<'a> {
.dst_acceleration_structure(*self.bindings[dst_accel_node])
.src_acceleration_structure(*self.bindings[src_accel_node])
.scratch_data(vk::DeviceOrHostAddressKHR {
device_address: align_up_device_size(
Buffer::device_address(&self.bindings[scratch_buf_node]),
self.device
.accel_struct_properties
.as_ref()
.expect("ray tracing feature must be enabled")
.min_accel_struct_scratch_offset_alignment
as _,
),
device_address: Buffer::device_address(&self.bindings[scratch_buf_node]),
});

self.device
Expand Down

0 comments on commit 6cdc3a0

Please sign in to comment.