Skip to content

Commit

Permalink
Add option to align buffer-memory
Browse files Browse the repository at this point in the history
- use for sbt- and acceleration-structure scratch
  • Loading branch information
crocdialer committed Sep 25, 2024
1 parent 470a748 commit ac00365
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
9 changes: 6 additions & 3 deletions include/vierkant/Buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class Buffer
{
DevicePtr device;
const void *data = nullptr;
size_t num_bytes = 0;
uint64_t num_bytes = 0;
uint32_t alignment = 0;
VkBufferUsageFlags usage = 0;
VmaMemoryUsage mem_usage = VMA_MEMORY_USAGE_UNKNOWN;
VmaPoolPtr pool;
Expand Down Expand Up @@ -157,6 +158,8 @@ class Buffer
[[nodiscard]] vierkant::DevicePtr device() const { return m_device; }

private:
explicit Buffer(const create_info_t &create_info);

DevicePtr m_device;

VkBuffer m_buffer = VK_NULL_HANDLE;
Expand All @@ -175,11 +178,11 @@ class Buffer

VmaMemoryUsage m_mem_usage = VMA_MEMORY_USAGE_UNKNOWN;

uint32_t m_min_alignment = 0;

VmaPoolPtr m_pool = nullptr;

std::string m_name;

Buffer(const create_info_t &create_info);
};

}// namespace vierkant
6 changes: 3 additions & 3 deletions src/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ BufferPtr Buffer::create(DevicePtr device, const void *data, size_t num_bytes, V

Buffer::Buffer(const create_info_t &create_info)
: m_device(create_info.device), m_usage(create_info.usage), m_mem_usage(create_info.mem_usage),
m_pool(create_info.pool), m_name(create_info.name)
m_min_alignment(create_info.alignment), m_pool(create_info.pool), m_name(create_info.name)
{}

Buffer::~Buffer()
Expand Down Expand Up @@ -210,8 +210,8 @@ void Buffer::set_data(const void *data, size_t num_bytes)
alloc_info.usage = m_mem_usage;
alloc_info.pool = m_pool.get();

vmaCreateBuffer(m_device->vk_mem_allocator(), &buffer_info, &alloc_info, &m_buffer, &m_allocation,
&m_allocation_info);
vmaCreateBufferWithAlignment(m_device->vk_mem_allocator(), &buffer_info, &alloc_info, m_min_alignment,
&m_buffer, &m_allocation, &m_allocation_info);

//! set optional name for debugging
if(!m_name.empty()) { m_device->set_object_name(uint64_t(m_buffer), VK_OBJECT_TYPE_BUFFER, m_name); }
Expand Down
29 changes: 19 additions & 10 deletions src/RayBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,16 @@ RayBuilder::build_result_t RayBuilder::create_mesh_structures(const create_mesh_
acceleration_asset.vertex_buffer = vertex_buffer;
acceleration_asset.vertex_buffer_offset = vertex_buffer_offset;

// Allocate the scratch buffers holding the temporary data of the
// acceleration structure builder
acceleration_asset.scratch_buffer =
vierkant::Buffer::create(m_device, nullptr, std::max<uint64_t>(size_info.buildScratchSize, 1 << 12U),
VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
VMA_MEMORY_USAGE_GPU_ONLY, m_memory_pool);
// allocate a scratch buffer for temporary data during acceleration-structure building
vierkant::Buffer::create_info_t scratch_buffer_info = {};
scratch_buffer_info.device = m_device;
scratch_buffer_info.pool = m_memory_pool;
scratch_buffer_info.num_bytes = std::max<uint64_t>(size_info.buildScratchSize, 1 << 12U);
scratch_buffer_info.alignment =
m_device->properties().acceleration_structure.minAccelerationStructureScratchOffsetAlignment;
scratch_buffer_info.usage = VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
scratch_buffer_info.mem_usage = VMA_MEMORY_USAGE_GPU_ONLY;
acceleration_asset.scratch_buffer = vierkant::Buffer::create(scratch_buffer_info);

// assign acceleration structure and scratch_buffer
build_info.dstAccelerationStructure = acceleration_asset.structure.get();
Expand Down Expand Up @@ -902,10 +906,15 @@ RayBuilder::scene_acceleration_context_ptr RayBuilder::create_scene_acceleration
vierkant::create_query_pool(m_device, 2 * UpdateSemaphoreValue::MAX_VALUE, VK_QUERY_TYPE_TIMESTAMP);

// Create a small scratch buffer used during build of the top level acceleration structure
ret->scratch_buffer_top =
vierkant::Buffer::create(m_device, nullptr, 1U << 12U,
VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
VMA_MEMORY_USAGE_GPU_ONLY, m_memory_pool);
vierkant::Buffer::create_info_t scratch_buffer_info = {};
scratch_buffer_info.device = m_device;
scratch_buffer_info.pool = m_memory_pool;
scratch_buffer_info.num_bytes = 1U << 12U;
scratch_buffer_info.alignment =
m_device->properties().acceleration_structure.minAccelerationStructureScratchOffsetAlignment;
scratch_buffer_info.usage = VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
scratch_buffer_info.mem_usage = VMA_MEMORY_USAGE_GPU_ONLY;
ret->scratch_buffer_top = vierkant::Buffer::create(scratch_buffer_info);
return ret;
}

Expand Down
14 changes: 10 additions & 4 deletions src/RayTracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,16 @@ RayTracer::create_shader_binding_table(VkPipeline pipeline, const vierkant::rayt
aligned_size(group_elements[Group(g)] * handle_size_aligned, ray_props.shaderGroupBaseAlignment);
binding_table_size += binding_table.strided_address_region[g].size;
}
binding_table.buffer = vierkant::Buffer::create(m_device, nullptr, binding_table_size,
VK_BUFFER_USAGE_SHADER_BINDING_TABLE_BIT_KHR |
VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT,
VMA_MEMORY_USAGE_CPU_TO_GPU);

vierkant::Buffer::create_info_t binding_table_buffer_info = {};
binding_table_buffer_info.device = m_device;
binding_table_buffer_info.num_bytes = binding_table_size;
binding_table_buffer_info.alignment = ray_props.shaderGroupBaseAlignment;
binding_table_buffer_info.usage =
VK_BUFFER_USAGE_SHADER_BINDING_TABLE_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
binding_table_buffer_info.mem_usage = VMA_MEMORY_USAGE_CPU_TO_GPU;
binding_table.buffer = vierkant::Buffer::create(binding_table_buffer_info);

// shader groups
auto group_create_infos = vierkant::raytracing_shader_groups(shader_stages);

Expand Down

0 comments on commit ac00365

Please sign in to comment.