diff --git a/include/vierkant/Buffer.hpp b/include/vierkant/Buffer.hpp index 659aa596..773339cf 100644 --- a/include/vierkant/Buffer.hpp +++ b/include/vierkant/Buffer.hpp @@ -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; @@ -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; @@ -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 \ No newline at end of file diff --git a/src/Buffer.cpp b/src/Buffer.cpp index fbc7084a..e41f50a7 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -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() @@ -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); } diff --git a/src/RayBuilder.cpp b/src/RayBuilder.cpp index 20036ae7..6cd7d724 100644 --- a/src/RayBuilder.cpp +++ b/src/RayBuilder.cpp @@ -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(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(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(); @@ -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; } diff --git a/src/RayTracer.cpp b/src/RayTracer.cpp index 72c5c545..324f80af 100644 --- a/src/RayTracer.cpp +++ b/src/RayTracer.cpp @@ -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);