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 14, 2024
1 parent 2d611cd commit 2a6745b
Show file tree
Hide file tree
Showing 40 changed files with 1,204 additions and 838 deletions.
9 changes: 6 additions & 3 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 @@ -254,6 +255,7 @@ set(CORE_FILES
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 +295,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 Down Expand Up @@ -387,7 +390,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
90 changes: 90 additions & 0 deletions framework/core/allocated.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* 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 core
{

[[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;

// The existing code produces some weird combinations, like in gui.cpp where you see this...

if (alloc_create_info.usage < VMA_MEMORY_USAGE_AUTO)
{
alloc_create_info.usage = VMA_MEMORY_USAGE_AUTO;
}

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 core
} // namespace vkb
Loading

0 comments on commit 2a6745b

Please sign in to comment.