Skip to content

Commit

Permalink
Allocated base classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jherico committed Feb 18, 2024
1 parent f5e753c commit e757747
Show file tree
Hide file tree
Showing 56 changed files with 1,490 additions and 1,613 deletions.
13 changes: 6 additions & 7 deletions framework/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019-2023, Arm Limited and Contributors
# Copyright (c) 2019-2024, Arm Limited and Contributors
#
# SPDX-License-Identifier: Apache-2.0
#
Expand Down Expand Up @@ -78,7 +78,7 @@ set(COMMON_FILES
common/ktx_common.h
common/vk_common.h
common/vk_initializers.h
common/glm_common.h
common/glm_common.h
common/resource_caching.h
common/logging.h
common/helpers.h
Expand Down Expand Up @@ -242,6 +242,7 @@ set(CORE_FILES
core/command_pool.h
core/swapchain.h
core/command_buffer.h
core/allocated.h
core/buffer.h
core/image.h
core/image_view.h
Expand All @@ -251,9 +252,8 @@ set(CORE_FILES
core/framebuffer.h
core/render_pass.h
core/query_pool.h
core/scratch_buffer.h
core/acceleration_structure.h
core/shader_binding_table.h
core/hpp_allocated.h
core/hpp_buffer.h
core/hpp_command_buffer.h
core/hpp_command_pool.h
Expand Down Expand Up @@ -293,6 +293,7 @@ set(CORE_FILES
core/command_pool.cpp
core/swapchain.cpp
core/command_buffer.cpp
core/allocated.cpp
core/buffer.cpp
core/image.cpp
core/image_view.cpp
Expand All @@ -302,9 +303,7 @@ set(CORE_FILES
core/framebuffer.cpp
core/render_pass.cpp
core/query_pool.cpp
core/scratch_buffer.cpp
core/acceleration_structure.cpp
core/shader_binding_table.cpp
core/vulkan_resource.cpp
core/hpp_buffer.cpp
core/hpp_command_buffer.cpp
Expand Down Expand Up @@ -387,7 +386,7 @@ set(LINUX_D2D_FILES
platform/unix/direct_window.h
# Source Files
platform/unix/unix_d2d_platform.cpp
platform/unix/direct_window.cpp)
platform/unix/direct_window.cpp)

source_group("\\" FILES ${FRAMEWORK_FILES})
source_group("common\\" FILES ${COMMON_FILES})
Expand Down
23 changes: 4 additions & 19 deletions framework/api_vulkan_sample.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2019-2023, Sascha Willems
/* Copyright (c) 2019-2024, Sascha Willems
*
* SPDX-License-Identifier: Apache-2.0
*
Expand Down Expand Up @@ -1000,12 +1000,7 @@ Texture ApiVulkanSample::load_texture(const std::string &file, vkb::sg::Image::C

VkCommandBuffer command_buffer = device->create_command_buffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);

vkb::core::Buffer stage_buffer{*device,
texture.image->get_data().size(),
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VMA_MEMORY_USAGE_CPU_ONLY};

stage_buffer.update(texture.image->get_data());
vkb::core::Buffer stage_buffer = vkb::core::Buffer::create_staging_buffer(*device, texture.image->get_data());

// Setup buffer copy regions for each mip level
std::vector<VkBufferImageCopy> bufferCopyRegions;
Expand Down Expand Up @@ -1096,12 +1091,7 @@ Texture ApiVulkanSample::load_texture_array(const std::string &file, vkb::sg::Im

VkCommandBuffer command_buffer = device->create_command_buffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);

vkb::core::Buffer stage_buffer{*device,
texture.image->get_data().size(),
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VMA_MEMORY_USAGE_CPU_ONLY};

stage_buffer.update(texture.image->get_data());
vkb::core::Buffer stage_buffer = vkb::core::Buffer::create_staging_buffer(*device, texture.image->get_data());

// Setup buffer copy regions for each mip level
std::vector<VkBufferImageCopy> buffer_copy_regions;
Expand Down Expand Up @@ -1195,12 +1185,7 @@ Texture ApiVulkanSample::load_texture_cubemap(const std::string &file, vkb::sg::

VkCommandBuffer command_buffer = device->create_command_buffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);

vkb::core::Buffer stage_buffer{*device,
texture.image->get_data().size(),
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VMA_MEMORY_USAGE_CPU_ONLY};

stage_buffer.update(texture.image->get_data());
vkb::core::Buffer stage_buffer = vkb::core::Buffer::create_staging_buffer(*device, texture.image->get_data());

// Setup buffer copy regions for each mip level
std::vector<VkBufferImageCopy> buffer_copy_regions;
Expand Down
4 changes: 2 additions & 2 deletions framework/buffer_pool.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2019-2023, Arm Limited and Contributors
/* Copyright (c) 2019-2024, Arm Limited and Contributors
*
* SPDX-License-Identifier: Apache-2.0
*
Expand Down Expand Up @@ -127,7 +127,7 @@ class BufferBlock
class BufferPool
{
public:
BufferPool(Device &device, VkDeviceSize block_size, VkBufferUsageFlags usage, VmaMemoryUsage memory_usage = VMA_MEMORY_USAGE_CPU_TO_GPU);
BufferPool(Device &device, VkDeviceSize block_size, VkBufferUsageFlags usage, VmaMemoryUsage memory_usage = VMA_MEMORY_USAGE_AUTO);

BufferBlock &request_buffer_block(VkDeviceSize minimum_size, bool minimal = false);

Expand Down
6 changes: 3 additions & 3 deletions framework/core/acceleration_structure.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2021, Sascha Willems
/* Copyright (c) 2021-2024, Sascha Willems
*
* SPDX-License-Identifier: Apache-2.0
*
Expand All @@ -23,7 +23,7 @@ namespace vkb
{
namespace core
{
AccelerationStructure::AccelerationStructure(Device & device,
AccelerationStructure::AccelerationStructure(Device &device,
VkAccelerationStructureTypeKHR type) :
device{device},
type{type}
Expand Down Expand Up @@ -202,7 +202,7 @@ void AccelerationStructure::build(VkQueue queue, VkBuildAccelerationStructureFla
device_address = vkGetAccelerationStructureDeviceAddressKHR(device.get_handle(), &acceleration_device_address_info);

// Create a scratch buffer as a temporary storage for the acceleration structure build
scratch_buffer = std::make_unique<vkb::core::ScratchBuffer>(device, build_sizes_info.buildScratchSize);
scratch_buffer = std::make_unique<vkb::core::Buffer>(vkb::core::Buffer::create_scratch_buffer(device, build_sizes_info.buildScratchSize));

build_geometry_info.scratchData.deviceAddress = scratch_buffer->get_device_address();
build_geometry_info.dstAccelerationStructure = handle;
Expand Down
9 changes: 4 additions & 5 deletions framework/core/acceleration_structure.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2021, Sascha Willems
/* Copyright (c) 2021-2024, Sascha Willems
*
* SPDX-License-Identifier: Apache-2.0
*
Expand All @@ -20,7 +20,6 @@
#include "common/helpers.h"
#include "common/vk_common.h"
#include "core/buffer.h"
#include "core/scratch_buffer.h"

namespace vkb
{
Expand All @@ -39,7 +38,7 @@ class AccelerationStructure
* @param device A valid Vulkan device
* @param type The type of the acceleration structure (top- or bottom-level)
*/
AccelerationStructure(Device & device,
AccelerationStructure(Device &device,
VkAccelerationStructureTypeKHR type);

~AccelerationStructure();
Expand All @@ -54,7 +53,7 @@ class AccelerationStructure
* @param max_vertex Index of the last vertex in the geometry
* @param vertex_stride Stride of the vertex structure
* @param transform_offset Offset of this geometry in the transform data buffer
* @param vertex_format Format of the vertex structure
* @param vertex_format Format of the vertex structure
* @param flags Ray tracing geometry flags
* @param vertex_buffer_data_address set this if don't want the vertex_buffer data_address
* @param index_buffer_data_address set this if don't want the index_buffer data_address
Expand Down Expand Up @@ -149,7 +148,7 @@ class AccelerationStructure
bool updated = false;
};

std::unique_ptr<vkb::core::ScratchBuffer> scratch_buffer;
std::unique_ptr<vkb::core::Buffer> scratch_buffer;

std::map<uint64_t, Geometry> geometries{};

Expand Down
112 changes: 112 additions & 0 deletions framework/core/allocated.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/* Copyright (c) 2021-2024, NVIDIA CORPORATION. All rights reserved.
* Copyright (c) 2024, Bradley Austin Davis. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "allocated.h"

namespace vkb
{

namespace allocated
{

VmaAllocator &get_memory_allocator()
{
static VmaAllocator memory_allocator = VK_NULL_HANDLE;
return memory_allocator;
}

void init(const VmaAllocatorCreateInfo &create_info)
{
VkResult result = vmaCreateAllocator(&create_info, &get_memory_allocator());
if (result != VK_SUCCESS)
{
throw VulkanException{result, "Cannot create allocator"};
}
}

void shutdown()
{
auto &allocator = get_memory_allocator();
if (allocator != VK_NULL_HANDLE)
{
VmaTotalStatistics stats;
vmaCalculateStatistics(allocator, &stats);
LOGI("Total device memory leaked: {} bytes.", stats.total.statistics.allocationBytes);
vmaDestroyAllocator(allocator);
allocator = VK_NULL_HANDLE;
}
}

[[nodiscard]] std::pair<VkBuffer, VmaAllocation> vma_create_buffer(VmaAllocator alloctor, const VkBufferCreateInfo &create_info, VmaAllocationCreateInfo &alloc_create_info, VmaAllocationInfo *alloc_info)
{
VkBuffer handle = VK_NULL_HANDLE;
VmaAllocation allocation = VK_NULL_HANDLE;

auto result = vmaCreateBuffer(
alloctor,
&create_info,
&alloc_create_info,
&handle,
&allocation,
alloc_info);

if (result != VK_SUCCESS)
{
throw VulkanException{result, "Cannot create Buffer"};
}
return {handle, allocation};
}

[[nodiscard]] std::pair<VkImage, VmaAllocation> vma_create_image(VmaAllocator alloctor, const VkImageCreateInfo &create_info, VmaAllocationCreateInfo &alloc_create_info, VmaAllocationInfo *alloc_info)
{
VkImage handle = VK_NULL_HANDLE;
VmaAllocation allocation = VK_NULL_HANDLE;

#if 0
// If the image is an attachment, prefer dedicated memory
constexpr VkImageUsageFlags attachment_only_flags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT;
if (create_info.usage & attachment_only_flags)
{
alloc_create_info.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
}

if (create_info.usage & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT)
{
alloc_create_info.preferredFlags |= VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT;
}
#endif

auto result = vmaCreateImage(
alloctor,
&create_info,
&alloc_create_info,
&handle,
&allocation,
alloc_info);

if (result != VK_SUCCESS)
{
throw VulkanException{result, "Cannot create Image"};
}

return {handle, allocation};
}

} // namespace allocated

} // namespace vkb
Loading

0 comments on commit e757747

Please sign in to comment.