Skip to content

Commit

Permalink
Add VulkanDirectAllocator
Browse files Browse the repository at this point in the history
Simplify the `VulkanResourceAllocator` interface by extracting `*Direct`
functions into a separate class `VulkanDirectAllocator`.
  • Loading branch information
antonio-lunarg committed Oct 31, 2024
1 parent efc0a6c commit 924a885
Show file tree
Hide file tree
Showing 14 changed files with 346 additions and 294 deletions.
2 changes: 2 additions & 0 deletions android/framework/decode/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ target_sources(gfxrecon_decode
${GFXRECON_SOURCE_DIR}/framework/decode/vulkan_default_allocator.cpp
${GFXRECON_SOURCE_DIR}/framework/decode/vulkan_device_address_tracker.h
${GFXRECON_SOURCE_DIR}/framework/decode/vulkan_device_address_tracker.cpp
${GFXRECON_SOURCE_DIR}/framework/decode/vulkan_direct_allocator.h
${GFXRECON_SOURCE_DIR}/framework/decode/vulkan_direct_allocator.cpp
${GFXRECON_SOURCE_DIR}/framework/decode/vulkan_captured_swapchain.h
${GFXRECON_SOURCE_DIR}/framework/decode/vulkan_captured_swapchain.cpp
${GFXRECON_SOURCE_DIR}/framework/decode/vulkan_enum_util.h
Expand Down
2 changes: 2 additions & 0 deletions framework/decode/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ target_sources(gfxrecon_decode
${CMAKE_CURRENT_LIST_DIR}/vulkan_default_allocator.cpp
${CMAKE_CURRENT_LIST_DIR}/vulkan_device_address_tracker.h
${CMAKE_CURRENT_LIST_DIR}/vulkan_device_address_tracker.cpp
${CMAKE_CURRENT_LIST_DIR}/vulkan_direct_allocator.h
${CMAKE_CURRENT_LIST_DIR}/vulkan_direct_allocator.cpp
${CMAKE_CURRENT_LIST_DIR}/vulkan_captured_swapchain.h
${CMAKE_CURRENT_LIST_DIR}/vulkan_captured_swapchain.cpp
${CMAKE_CURRENT_LIST_DIR}/vulkan_json_consumer_base.h
Expand Down
56 changes: 29 additions & 27 deletions framework/decode/screenshot_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/

#include "decode/screenshot_handler.h"
#include "decode/vulkan_direct_allocator.h"
#include "util/image_writer.h"
#include "util/logging.h"
#include "util/platform.h"
Expand Down Expand Up @@ -366,7 +367,7 @@ void ScreenshotHandler::WriteImage(const std::string& filen
if (result == VK_SUCCESS)
{
void* data = nullptr;
result = allocator->MapResourceMemoryDirect(
result = allocator->GetDirectAllocator().MapResourceMemory(
copy_resource.buffer_size, 0, &data, copy_resource.buffer_data);

if (result == VK_SUCCESS)
Expand All @@ -380,7 +381,7 @@ void ScreenshotHandler::WriteImage(const std::string& filen
invalidate_range.offset = 0;
invalidate_range.size = copy_resource.buffer_size;

allocator->InvalidateMappedMemoryRangesDirect(
allocator->GetDirectAllocator().InvalidateMappedMemoryRanges(
1, &invalidate_range, &copy_resource.buffer_memory_data);
}

Expand All @@ -391,7 +392,7 @@ void ScreenshotHandler::WriteImage(const std::string& filen
copy_resource.buffer_size,
data);

allocator->UnmapResourceMemoryDirect(copy_resource.buffer_data);
allocator->GetDirectAllocator().UnmapResourceMemory(copy_resource.buffer_data);
}
}
else
Expand Down Expand Up @@ -563,8 +564,8 @@ VkResult ScreenshotHandler::CreateCopyResource(VkDevice
create_info.queueFamilyIndexCount = 0;
create_info.pQueueFamilyIndices = nullptr;

VkResult result =
allocator->CreateBufferDirect(&create_info, nullptr, &copy_resource->buffer, &copy_resource->buffer_data);
VkResult result = allocator->GetDirectAllocator().CreateBuffer(
&create_info, nullptr, &copy_resource->buffer, &copy_resource->buffer_data);

if (result == VK_SUCCESS)
{
Expand Down Expand Up @@ -592,18 +593,18 @@ VkResult ScreenshotHandler::CreateCopyResource(VkDevice
allocate_info.allocationSize = memory_requirements.size;
allocate_info.memoryTypeIndex = memory_type_index;

result = allocator->AllocateMemoryDirect(
result = allocator->GetDirectAllocator().AllocateMemory(
&allocate_info, nullptr, &copy_resource->buffer_memory, &copy_resource->buffer_memory_data);
}

if (result == VK_SUCCESS)
{
result = allocator->BindBufferMemoryDirect(copy_resource->buffer,
copy_resource->buffer_memory,
0,
copy_resource->buffer_data,
copy_resource->buffer_memory_data,
&copy_resource->memory_property_flags);
result = allocator->GetDirectAllocator().BindBufferMemory(copy_resource->buffer,
copy_resource->buffer_memory,
0,
copy_resource->buffer_data,
copy_resource->buffer_memory_data,
&copy_resource->memory_property_flags);
}

if ((result == VK_SUCCESS) &&
Expand All @@ -627,7 +628,7 @@ VkResult ScreenshotHandler::CreateCopyResource(VkDevice
image_create_info.pQueueFamilyIndices = nullptr;
image_create_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;

result = allocator->CreateImageDirect(
result = allocator->GetDirectAllocator().CreateImage(
&image_create_info, nullptr, &copy_resource->convert_image, &copy_resource->convert_image_data);

if (result == VK_SUCCESS)
Expand All @@ -645,22 +646,22 @@ VkResult ScreenshotHandler::CreateCopyResource(VkDevice
allocate_info.allocationSize = memory_requirements.size;
allocate_info.memoryTypeIndex = memory_type_index;

result = allocator->AllocateMemoryDirect(&allocate_info,
nullptr,
&copy_resource->convert_image_memory,
&copy_resource->convert_image_memory_data);
result = allocator->GetDirectAllocator().AllocateMemory(&allocate_info,
nullptr,
&copy_resource->convert_image_memory,
&copy_resource->convert_image_memory_data);
}

if (result == VK_SUCCESS)
{
VkMemoryPropertyFlags image_memory_property_flags = 0;

result = allocator->BindImageMemoryDirect(copy_resource->convert_image,
copy_resource->convert_image_memory,
0,
copy_resource->convert_image_data,
copy_resource->convert_image_memory_data,
&image_memory_property_flags);
result = allocator->GetDirectAllocator().BindImageMemory(copy_resource->convert_image,
copy_resource->convert_image_memory,
0,
copy_resource->convert_image_data,
copy_resource->convert_image_memory_data,
&image_memory_property_flags);
}
}

Expand All @@ -686,14 +687,15 @@ void ScreenshotHandler::DestroyCopyResource(VkDevice device, CopyResource* copy_
{
if (copy_resource->buffer != VK_NULL_HANDLE)
{
copy_resource->allocator->DestroyBufferDirect(copy_resource->buffer, nullptr, copy_resource->buffer_data);
copy_resource->allocator->GetDirectAllocator().DestroyBuffer(
copy_resource->buffer, nullptr, copy_resource->buffer_data);
copy_resource->buffer = VK_NULL_HANDLE;
copy_resource->buffer_data = 0;
}

if (copy_resource->buffer_memory != VK_NULL_HANDLE)
{
copy_resource->allocator->FreeMemoryDirect(
copy_resource->allocator->GetDirectAllocator().FreeMemory(
copy_resource->buffer_memory, nullptr, copy_resource->buffer_memory_data);
copy_resource->buffer_memory = VK_NULL_HANDLE;
copy_resource->buffer_memory_data = 0;
Expand All @@ -702,15 +704,15 @@ void ScreenshotHandler::DestroyCopyResource(VkDevice device, CopyResource* copy_

if (copy_resource->convert_image != VK_NULL_HANDLE)
{
copy_resource->allocator->DestroyImageDirect(
copy_resource->allocator->GetDirectAllocator().DestroyImage(
copy_resource->convert_image, nullptr, copy_resource->convert_image_data);
copy_resource->convert_image = VK_NULL_HANDLE;
copy_resource->convert_image_data = 0;
}

if (copy_resource->convert_image_memory != VK_NULL_HANDLE)
{
copy_resource->allocator->FreeMemoryDirect(
copy_resource->allocator->GetDirectAllocator().FreeMemory(
copy_resource->convert_image_memory, nullptr, copy_resource->convert_image_memory_data);
copy_resource->convert_image_memory = VK_NULL_HANDLE;
copy_resource->convert_image_memory_data = 0;
Expand Down
3 changes: 2 additions & 1 deletion framework/decode/vulkan_default_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ VkResult VulkanDefaultAllocator::Initialize(uint32_t
functions_ = functions;
memory_properties_ = replay_memory_properties;

return VK_SUCCESS;
return direct_allocator_.Initialize(*this);
}

void VulkanDefaultAllocator::Destroy()
{
device_ = VK_NULL_HANDLE;
direct_allocator_.Destroy();
}

VkResult VulkanDefaultAllocator::CreateBuffer(const VkBufferCreateInfo* create_info,
Expand Down
86 changes: 3 additions & 83 deletions framework/decode/vulkan_default_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#define GFXRECON_DECODE_VULKAN_DEFAULT_ALLOCATOR_H

#include "decode/vulkan_resource_allocator.h"
#include "decode/vulkan_direct_allocator.h"
#include "util/defines.h"

#include <limits>
Expand Down Expand Up @@ -181,75 +182,7 @@ class VulkanDefaultAllocator : public VulkanResourceAllocator
const ResourceData* allocator_resource_datas,
const MemoryData* allocator_memory_datas) override;

// Direct allocation methods that perform memory allocation and resource creation without performing memory
// translation. These methods allow the replay tool to allocate staging resources through the resource allocator so
// that the allocator is aware of all allocations performed at replay.
virtual VkResult CreateBufferDirect(const VkBufferCreateInfo* create_info,
const VkAllocationCallbacks* allocation_callbacks,
VkBuffer* buffer,
ResourceData* allocator_data) override
{
return CreateBuffer(create_info, allocation_callbacks, format::kNullHandleId, buffer, allocator_data);
}

virtual void DestroyBufferDirect(VkBuffer buffer,
const VkAllocationCallbacks* allocation_callbacks,
ResourceData allocator_data) override
{
DestroyBuffer(buffer, allocation_callbacks, allocator_data);
}

virtual VkResult CreateImageDirect(const VkImageCreateInfo* create_info,
const VkAllocationCallbacks* allocation_callbacks,
VkImage* image,
ResourceData* allocator_data) override
{
return CreateImage(create_info, allocation_callbacks, format::kNullHandleId, image, allocator_data);
}

virtual void DestroyImageDirect(VkImage image,
const VkAllocationCallbacks* allocation_callbacks,
ResourceData allocator_data) override
{
DestroyImage(image, allocation_callbacks, allocator_data);
}

virtual VkResult AllocateMemoryDirect(const VkMemoryAllocateInfo* allocate_info,
const VkAllocationCallbacks* allocation_callbacks,
VkDeviceMemory* memory,
MemoryData* allocator_data) override
{
return Allocate(allocate_info, allocation_callbacks, format::kNullHandleId, memory, allocator_data);
}

virtual void FreeMemoryDirect(VkDeviceMemory memory,
const VkAllocationCallbacks* allocation_callbacks,
MemoryData allocator_data) override
{
FreeMemory(memory, allocation_callbacks, allocator_data);
}

virtual VkResult BindBufferMemoryDirect(VkBuffer buffer,
VkDeviceMemory memory,
VkDeviceSize memory_offset,
ResourceData allocator_buffer_data,
MemoryData allocator_memory_data,
VkMemoryPropertyFlags* bind_memory_properties) override
{
return BindBufferMemory(
buffer, memory, memory_offset, allocator_buffer_data, allocator_memory_data, bind_memory_properties);
}

virtual VkResult BindImageMemoryDirect(VkImage image,
VkDeviceMemory memory,
VkDeviceSize memory_offset,
ResourceData allocator_image_data,
MemoryData allocator_memory_data,
VkMemoryPropertyFlags* bind_memory_properties) override
{
return BindImageMemory(
image, memory, memory_offset, allocator_image_data, allocator_memory_data, bind_memory_properties);
}
virtual VulkanDirectAllocator& GetDirectAllocator() override { return direct_allocator_; }

virtual VkResult MapResourceMemoryDirect(VkDeviceSize size,
VkMemoryMapFlags flags,
Expand All @@ -258,20 +191,6 @@ class VulkanDefaultAllocator : public VulkanResourceAllocator

virtual void UnmapResourceMemoryDirect(ResourceData allocator_data) override;

virtual VkResult FlushMappedMemoryRangesDirect(uint32_t memory_range_count,
const VkMappedMemoryRange* memory_ranges,
const MemoryData* allocator_datas) override
{
return FlushMappedMemoryRanges(memory_range_count, memory_ranges, allocator_datas);
}

virtual VkResult InvalidateMappedMemoryRangesDirect(uint32_t memory_range_count,
const VkMappedMemoryRange* memory_ranges,
const MemoryData* allocator_datas) override
{
return InvalidateMappedMemoryRanges(memory_range_count, memory_ranges, allocator_datas);
}

virtual bool SupportsOpaqueDeviceAddresses() override { return true; }

virtual bool SupportBindVideoSessionMemory() override { return false; }
Expand Down Expand Up @@ -319,6 +238,7 @@ class VulkanDefaultAllocator : public VulkanResourceAllocator
Functions functions_;
VkPhysicalDeviceMemoryProperties memory_properties_;
std::string custom_error_string_;
VulkanDirectAllocator direct_allocator_;
};

GFXRECON_END_NAMESPACE(decode)
Expand Down
Loading

0 comments on commit 924a885

Please sign in to comment.