diff --git a/android/framework/decode/CMakeLists.txt b/android/framework/decode/CMakeLists.txt index d7052f26b..2d6c2de63 100644 --- a/android/framework/decode/CMakeLists.txt +++ b/android/framework/decode/CMakeLists.txt @@ -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 diff --git a/framework/decode/CMakeLists.txt b/framework/decode/CMakeLists.txt index 29a910fc3..968d8acf8 100644 --- a/framework/decode/CMakeLists.txt +++ b/framework/decode/CMakeLists.txt @@ -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 diff --git a/framework/decode/screenshot_handler.cpp b/framework/decode/screenshot_handler.cpp index 3c4afba44..07a1d8131 100644 --- a/framework/decode/screenshot_handler.cpp +++ b/framework/decode/screenshot_handler.cpp @@ -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" @@ -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) @@ -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, ©_resource.buffer_memory_data); } @@ -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 @@ -563,8 +564,8 @@ VkResult ScreenshotHandler::CreateCopyResource(VkDevice create_info.queueFamilyIndexCount = 0; create_info.pQueueFamilyIndices = nullptr; - VkResult result = - allocator->CreateBufferDirect(&create_info, nullptr, ©_resource->buffer, ©_resource->buffer_data); + VkResult result = allocator->GetDirectAllocator().CreateBuffer( + &create_info, nullptr, ©_resource->buffer, ©_resource->buffer_data); if (result == VK_SUCCESS) { @@ -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, ©_resource->buffer_memory, ©_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, - ©_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, + ©_resource->memory_property_flags); } if ((result == VK_SUCCESS) && @@ -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, ©_resource->convert_image, ©_resource->convert_image_data); if (result == VK_SUCCESS) @@ -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, - ©_resource->convert_image_memory, - ©_resource->convert_image_memory_data); + result = allocator->GetDirectAllocator().AllocateMemory(&allocate_info, + nullptr, + ©_resource->convert_image_memory, + ©_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); } } @@ -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; @@ -702,7 +704,7 @@ 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; @@ -710,7 +712,7 @@ void ScreenshotHandler::DestroyCopyResource(VkDevice device, CopyResource* copy_ 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; diff --git a/framework/decode/vulkan_default_allocator.cpp b/framework/decode/vulkan_default_allocator.cpp index aaa407b2c..ecf8b2d6f 100644 --- a/framework/decode/vulkan_default_allocator.cpp +++ b/framework/decode/vulkan_default_allocator.cpp @@ -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, diff --git a/framework/decode/vulkan_default_allocator.h b/framework/decode/vulkan_default_allocator.h index 4fb13f6c5..bdeb07c27 100644 --- a/framework/decode/vulkan_default_allocator.h +++ b/framework/decode/vulkan_default_allocator.h @@ -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 @@ -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, @@ -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; } @@ -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) diff --git a/framework/decode/vulkan_direct_allocator.cpp b/framework/decode/vulkan_direct_allocator.cpp new file mode 100644 index 000000000..3ce83d69c --- /dev/null +++ b/framework/decode/vulkan_direct_allocator.cpp @@ -0,0 +1,135 @@ +/* +** Copyright (c) 2024 LunarG, Inc. +** +** Permission is hereby granted, free of charge, to any person obtaining a +** copy of this software and associated documentation files (the "Software"), +** to deal in the Software without restriction, including without limitation +** the rights to use, copy, modify, merge, publish, distribute, sublicense, +** and/or sell copies of the Software, and to permit persons to whom the +** Software is furnished to do so, subject to the following conditions: +** +** The above copyright notice and this permission notice shall be included in +** all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +** FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +** DEALINGS IN THE SOFTWARE. +*/ + +#include "decode/vulkan_direct_allocator.h" + +#include "decode/custom_vulkan_struct_decoders.h" +#include "decode/vulkan_object_info.h" +#include "generated/generated_vulkan_struct_decoders.h" +#include "util/platform.h" + +#include + +GFXRECON_BEGIN_NAMESPACE(gfxrecon) +GFXRECON_BEGIN_NAMESPACE(decode) + +VkResult VulkanDirectAllocator::Initialize(VulkanResourceAllocator& allocator) +{ + allocator_ = &allocator; + return VK_SUCCESS; +} + +void VulkanDirectAllocator::Destroy() +{ + allocator_ = nullptr; +} + +VkResult VulkanDirectAllocator::CreateBuffer(const VkBufferCreateInfo* create_info, + const VkAllocationCallbacks* allocation_callbacks, + VkBuffer* buffer, + ResourceData* allocator_data) +{ + return allocator_->CreateBuffer(create_info, allocation_callbacks, format::kNullHandleId, buffer, allocator_data); +} + +void VulkanDirectAllocator::DestroyBuffer(VkBuffer buffer, + const VkAllocationCallbacks* allocation_callbacks, + ResourceData allocator_data) +{ + allocator_->DestroyBuffer(buffer, allocation_callbacks, allocator_data); +} + +VkResult VulkanDirectAllocator::CreateImage(const VkImageCreateInfo* create_info, + const VkAllocationCallbacks* allocation_callbacks, + VkImage* image, + ResourceData* allocator_data) +{ + return allocator_->CreateImage(create_info, allocation_callbacks, format::kNullHandleId, image, allocator_data); +} + +void VulkanDirectAllocator::DestroyImage(VkImage image, + const VkAllocationCallbacks* allocation_callbacks, + ResourceData allocator_data) +{ + allocator_->DestroyImage(image, allocation_callbacks, allocator_data); +} + +VkResult VulkanDirectAllocator::AllocateMemory(const VkMemoryAllocateInfo* allocate_info, + const VkAllocationCallbacks* allocation_callbacks, + VkDeviceMemory* memory, + MemoryData* allocator_data) +{ + return allocator_->AllocateMemory( + allocate_info, allocation_callbacks, format::kNullHandleId, memory, allocator_data); +} + +void VulkanDirectAllocator::FreeMemory(VkDeviceMemory memory, + const VkAllocationCallbacks* allocation_callbacks, + MemoryData allocator_data) +{ + allocator_->FreeMemory(memory, allocation_callbacks, allocator_data); +} + +VkResult VulkanDirectAllocator::BindBufferMemory(VkBuffer buffer, + VkDeviceMemory memory, + VkDeviceSize memory_offset, + ResourceData allocator_buffer_data, + MemoryData allocator_memory_data, + VkMemoryPropertyFlags* bind_memory_properties) +{ + return allocator_->BindBufferMemory( + buffer, memory, memory_offset, allocator_buffer_data, allocator_memory_data, bind_memory_properties); +} + +VkResult VulkanDirectAllocator::BindImageMemory(VkImage image, + VkDeviceMemory memory, + VkDeviceSize memory_offset, + ResourceData allocator_image_data, + MemoryData allocator_memory_data, + VkMemoryPropertyFlags* bind_memory_properties) +{ + return allocator_->BindImageMemory( + image, memory, memory_offset, allocator_image_data, allocator_memory_data, bind_memory_properties); +} + +VkResult VulkanDirectAllocator::InvalidateMappedMemoryRanges(uint32_t memory_range_count, + const VkMappedMemoryRange* memory_ranges, + const MemoryData* allocator_datas) +{ + return allocator_->InvalidateMappedMemoryRanges(memory_range_count, memory_ranges, allocator_datas); +} + +VkResult VulkanDirectAllocator::MapResourceMemory(VkDeviceSize size, + VkMemoryMapFlags flags, + void** data, + ResourceData allocator_data) +{ + return allocator_->MapResourceMemoryDirect(size, flags, data, allocator_data); +} + +void VulkanDirectAllocator::UnmapResourceMemory(ResourceData allocator_data) +{ + allocator_->UnmapResourceMemoryDirect(allocator_data); +} + +GFXRECON_END_NAMESPACE(decode) +GFXRECON_END_NAMESPACE(gfxrecon) diff --git a/framework/decode/vulkan_direct_allocator.h b/framework/decode/vulkan_direct_allocator.h new file mode 100644 index 000000000..b546efbce --- /dev/null +++ b/framework/decode/vulkan_direct_allocator.h @@ -0,0 +1,111 @@ +/* +** Copyright (c) 2024 LunarG, Inc. +** +** Permission is hereby granted, free of charge, to any person obtaining a +** copy of this software and associated documentation files (the "Software"), +** to deal in the Software without restriction, including without limitation +** the rights to use, copy, modify, merge, publish, distribute, sublicense, +** and/or sell copies of the Software, and to permit persons to whom the +** Software is furnished to do so, subject to the following conditions: +** +** The above copyright notice and this permission notice shall be included in +** all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +** FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +** DEALINGS IN THE SOFTWARE. +*/ + +#ifndef GFXRECON_DECODE_VULKAN_DIRECT_ALLOCATOR_H +#define GFXRECON_DECODE_VULKAN_DIRECT_ALLOCATOR_H + +#include "decode/handle_pointer_decoder.h" +#include "decode/struct_pointer_decoder.h" +#include "decode/vulkan_resource_allocator.h" +#include "format/format.h" +#include "util/defines.h" + +#include "vulkan/vulkan.h" + +#include +#include + +GFXRECON_BEGIN_NAMESPACE(gfxrecon) +GFXRECON_BEGIN_NAMESPACE(decode) + +struct VulkanDeviceMemoryInfo; +struct VulkanBufferInfo; +struct VulkanImageInfo; + +class VulkanDirectAllocator +{ + public: + typedef uintptr_t ResourceData; + typedef uintptr_t MemoryData; + + public: + VkResult Initialize(VulkanResourceAllocator& allocator); + + void Destroy(); + + // 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. + VkResult CreateBuffer(const VkBufferCreateInfo* create_info, + const VkAllocationCallbacks* allocation_callbacks, + VkBuffer* buffer, + ResourceData* allocator_data); + + void DestroyBuffer(VkBuffer buffer, const VkAllocationCallbacks* allocation_callbacks, ResourceData allocator_data); + + VkResult CreateImage(const VkImageCreateInfo* create_info, + const VkAllocationCallbacks* allocation_callbacks, + VkImage* image, + ResourceData* allocator_data); + + void DestroyImage(VkImage image, const VkAllocationCallbacks* allocation_callbacks, ResourceData allocator_data); + + VkResult AllocateMemory(const VkMemoryAllocateInfo* allocate_info, + const VkAllocationCallbacks* allocation_callbacks, + VkDeviceMemory* memory, + MemoryData* allocator_data); + + void + FreeMemory(VkDeviceMemory memory, const VkAllocationCallbacks* allocation_callbacks, MemoryData allocator_data); + + VkResult BindBufferMemory(VkBuffer buffer, + VkDeviceMemory memory, + VkDeviceSize memory_offset, + ResourceData allocator_buffer_data, + MemoryData allocator_memory_data, + VkMemoryPropertyFlags* bind_memory_properties); + + VkResult BindImageMemory(VkImage image, + VkDeviceMemory memory, + VkDeviceSize memory_offset, + ResourceData allocator_image_data, + MemoryData allocator_memory_data, + VkMemoryPropertyFlags* bind_memory_properties); + + // Map the memory that the buffer was bound to. The returned pointer references the start of the buffer memory (it + // is the start of the memory the resource was bound to plus the resource bind offset). + VkResult MapResourceMemory(VkDeviceSize size, VkMemoryMapFlags flags, void** data, ResourceData allocator_data); + + void UnmapResourceMemory(ResourceData allocator_data); + + VkResult InvalidateMappedMemoryRanges(uint32_t memory_range_count, + const VkMappedMemoryRange* memory_ranges, + const MemoryData* allocator_datas); + + private: + VulkanResourceAllocator* allocator_; +}; + +GFXRECON_END_NAMESPACE(decode) +GFXRECON_END_NAMESPACE(gfxrecon) + +#endif // GFXRECON_DECODE_VULKAN_DIRECT_ALLOCATOR_H diff --git a/framework/decode/vulkan_object_cleanup_util.cpp b/framework/decode/vulkan_object_cleanup_util.cpp index 86f492efd..76c1d0aa1 100644 --- a/framework/decode/vulkan_object_cleanup_util.cpp +++ b/framework/decode/vulkan_object_cleanup_util.cpp @@ -22,6 +22,7 @@ #include "decode/vulkan_object_cleanup_util.h" +#include "decode/vulkan_direct_allocator.h" #include "decode/vulkan_resource_allocator.h" #include "format/format.h" #include "util/logging.h" @@ -644,8 +645,9 @@ void FreeAllLiveObjects(CommonObjectInfoTable* for (const VulkanImageInfo& image_info : object_info->image_infos) { - allocator->DestroyImageDirect(image_info.handle, nullptr, image_info.allocator_data); - allocator->FreeMemoryDirect(image_info.memory, nullptr, image_info.memory_allocator_data); + allocator->GetDirectAllocator().DestroyImage(image_info.handle, nullptr, image_info.allocator_data); + allocator->GetDirectAllocator().FreeMemory( + image_info.memory, nullptr, image_info.memory_allocator_data); } } }); diff --git a/framework/decode/vulkan_rebind_allocator.cpp b/framework/decode/vulkan_rebind_allocator.cpp index 38e64ac06..042d63d12 100644 --- a/framework/decode/vulkan_rebind_allocator.cpp +++ b/framework/decode/vulkan_rebind_allocator.cpp @@ -87,7 +87,13 @@ VkResult VulkanRebindAllocator::Initialize(uint32_t const VkPhysicalDeviceMemoryProperties& replay_memory_properties, const Functions& functions) { - VkResult result = VK_ERROR_INITIALIZATION_FAILED; + VkResult result = direct_allocator_.Initialize(*this); + if (result != VK_SUCCESS) + { + return result; + } + + result = VK_ERROR_INITIALIZATION_FAILED; if ((capture_memory_properties.memoryTypeCount == 0) || (replay_memory_properties.memoryTypeCount == 0)) { @@ -225,6 +231,8 @@ void VulkanRebindAllocator::Destroy() } device_ = VK_NULL_HANDLE; + + direct_allocator_.Destroy(); } VkResult VulkanRebindAllocator::CreateBuffer(const VkBufferCreateInfo* create_info, diff --git a/framework/decode/vulkan_rebind_allocator.h b/framework/decode/vulkan_rebind_allocator.h index 1c0549c36..7f2cbe0d8 100644 --- a/framework/decode/vulkan_rebind_allocator.h +++ b/framework/decode/vulkan_rebind_allocator.h @@ -23,6 +23,7 @@ #ifndef GFXRECON_DECODE_VULKAN_REBIND_ALLOCATOR_H #define GFXRECON_DECODE_VULKAN_REBIND_ALLOCATOR_H +#include "decode/vulkan_direct_allocator.h" #include "decode/vulkan_resource_allocator.h" #include "util/defines.h" @@ -203,85 +204,7 @@ class VulkanRebindAllocator : 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 AllocateMemory(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, - replay_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, - replay_memory_properties_); - } + virtual VulkanDirectAllocator& GetDirectAllocator() override { return direct_allocator_; } virtual VkResult MapResourceMemoryDirect(VkDeviceSize size, VkMemoryMapFlags flags, @@ -290,20 +213,6 @@ class VulkanRebindAllocator : public VulkanResourceAllocator virtual void UnmapResourceMemoryDirect(ResourceData) 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 false; } virtual bool SupportBindVideoSessionMemory() override { return true; } @@ -446,6 +355,7 @@ class VulkanRebindAllocator : public VulkanResourceAllocator VkCommandPool cmd_pool_; VkQueue staging_queue_; uint32_t staging_queue_family_; + VulkanDirectAllocator direct_allocator_; }; GFXRECON_END_NAMESPACE(decode) diff --git a/framework/decode/vulkan_replay_consumer_base.cpp b/framework/decode/vulkan_replay_consumer_base.cpp index c86fe5f5c..2c6078b2e 100644 --- a/framework/decode/vulkan_replay_consumer_base.cpp +++ b/framework/decode/vulkan_replay_consumer_base.cpp @@ -22,6 +22,7 @@ ** DEALINGS IN THE SOFTWARE. */ +#include "decode/vulkan_direct_allocator.h" #include "decode/vulkan_replay_consumer_base.h" #include "decode/custom_vulkan_struct_handle_mappers.h" #include "decode/descriptor_update_template_decoder.h" @@ -6272,8 +6273,8 @@ void VulkanReplayConsumerBase::OverrideDestroySwapchainKHR( for (const VulkanImageInfo& image_info : swapchain_info->image_infos) { - allocator->DestroyImageDirect(image_info.handle, nullptr, image_info.allocator_data); - allocator->FreeMemoryDirect(image_info.memory, nullptr, image_info.memory_allocator_data); + allocator->GetDirectAllocator().DestroyImage(image_info.handle, nullptr, image_info.allocator_data); + allocator->GetDirectAllocator().FreeMemory(image_info.memory, nullptr, image_info.memory_allocator_data); } } else @@ -9024,7 +9025,8 @@ VkResult VulkanReplayConsumerBase::CreateSwapchainImage(const VulkanDeviceInfo* assert(allocator != nullptr); VulkanResourceAllocator::ResourceData allocator_image_data; - VkResult result = allocator->CreateImageDirect(image_create_info, nullptr, image, &allocator_image_data); + VkResult result = + allocator->GetDirectAllocator().CreateImage(image_create_info, nullptr, image, &allocator_image_data); if (result == VK_SUCCESS) { @@ -9062,12 +9064,12 @@ VkResult VulkanReplayConsumerBase::CreateSwapchainImage(const VulkanDeviceInfo* alloc_info.memoryTypeIndex = memory_type_index; alloc_info.allocationSize = memory_reqs.size; - result = allocator->AllocateMemoryDirect(&alloc_info, nullptr, &memory, &allocator_memory_data); + result = allocator->GetDirectAllocator().AllocateMemory(&alloc_info, nullptr, &memory, &allocator_memory_data); if (result == VK_SUCCESS) { VkMemoryPropertyFlags flags; - result = allocator->BindImageMemoryDirect( + result = allocator->GetDirectAllocator().BindImageMemory( *image, memory, 0, allocator_image_data, allocator_memory_data, &flags); } @@ -9082,7 +9084,7 @@ VkResult VulkanReplayConsumerBase::CreateSwapchainImage(const VulkanDeviceInfo* } else { - allocator->DestroyImageDirect(*image, nullptr, allocator_image_data); + allocator->GetDirectAllocator().DestroyImage(*image, nullptr, allocator_image_data); } } return result; diff --git a/framework/decode/vulkan_resource_allocator.h b/framework/decode/vulkan_resource_allocator.h index 26d384025..9232cf9fb 100644 --- a/framework/decode/vulkan_resource_allocator.h +++ b/framework/decode/vulkan_resource_allocator.h @@ -39,6 +39,7 @@ GFXRECON_BEGIN_NAMESPACE(decode) struct VulkanDeviceMemoryInfo; struct VulkanBufferInfo; struct VulkanImageInfo; +class VulkanDirectAllocator; class VulkanResourceAllocator { @@ -235,65 +236,13 @@ class VulkanResourceAllocator const ResourceData* allocator_resource_datas, const MemoryData* allocator_memory_datas) = 0; - // 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) = 0; + virtual VulkanDirectAllocator& GetDirectAllocator() = 0; - virtual void DestroyBufferDirect(VkBuffer buffer, - const VkAllocationCallbacks* allocation_callbacks, - ResourceData allocator_data) = 0; - - virtual VkResult CreateImageDirect(const VkImageCreateInfo* create_info, - const VkAllocationCallbacks* allocation_callbacks, - VkImage* image, - ResourceData* allocator_data) = 0; - - virtual void DestroyImageDirect(VkImage image, - const VkAllocationCallbacks* allocation_callbacks, - ResourceData allocator_data) = 0; - - virtual VkResult AllocateMemoryDirect(const VkMemoryAllocateInfo* allocate_info, - const VkAllocationCallbacks* allocation_callbacks, - VkDeviceMemory* memory, - MemoryData* allocator_data) = 0; - - virtual void FreeMemoryDirect(VkDeviceMemory memory, - const VkAllocationCallbacks* allocation_callbacks, - MemoryData allocator_data) = 0; - - virtual VkResult BindBufferMemoryDirect(VkBuffer buffer, - VkDeviceMemory memory, - VkDeviceSize memory_offset, - ResourceData allocator_buffer_data, - MemoryData allocator_memory_data, - VkMemoryPropertyFlags* bind_memory_properties) = 0; - - virtual VkResult BindImageMemoryDirect(VkImage image, - VkDeviceMemory memory, - VkDeviceSize memory_offset, - ResourceData allocator_image_data, - MemoryData allocator_memory_data, - VkMemoryPropertyFlags* bind_memory_properties) = 0; - - // Map the memory that the buffer was bound to. The returned pointer references the start of the buffer memory (it - // is the start of the memory the resource was bound to plus the resource bind offset). virtual VkResult MapResourceMemoryDirect(VkDeviceSize size, VkMemoryMapFlags flags, void** data, ResourceData allocator_data) = 0; virtual void UnmapResourceMemoryDirect(ResourceData allocator_data) = 0; - virtual VkResult FlushMappedMemoryRangesDirect(uint32_t memory_range_count, - const VkMappedMemoryRange* memory_ranges, - const MemoryData* allocator_datas) = 0; - - virtual VkResult InvalidateMappedMemoryRangesDirect(uint32_t memory_range_count, - const VkMappedMemoryRange* memory_ranges, - const MemoryData* allocator_datas) = 0; - virtual bool SupportsOpaqueDeviceAddresses() = 0; virtual bool SupportBindVideoSessionMemory() = 0; }; diff --git a/framework/decode/vulkan_resource_initializer.cpp b/framework/decode/vulkan_resource_initializer.cpp index e554b42bb..a10433369 100644 --- a/framework/decode/vulkan_resource_initializer.cpp +++ b/framework/decode/vulkan_resource_initializer.cpp @@ -20,6 +20,7 @@ ** DEALINGS IN THE SOFTWARE. */ +#include "decode/vulkan_direct_allocator.h" #include "decode/vulkan_resource_initializer.h" #include "decode/vulkan_object_info.h" @@ -69,12 +70,12 @@ VulkanResourceInitializer::~VulkanResourceInitializer() if (staging_buffer_ != VK_NULL_HANDLE) { - resource_allocator_->DestroyBufferDirect(staging_buffer_, nullptr, staging_buffer_data_); + resource_allocator_->GetDirectAllocator().DestroyBuffer(staging_buffer_, nullptr, staging_buffer_data_); } if (staging_memory_ != VK_NULL_HANDLE) { - resource_allocator_->FreeMemoryDirect(staging_memory_, nullptr, staging_memory_data_); + resource_allocator_->GetDirectAllocator().FreeMemory(staging_memory_, nullptr, staging_memory_data_); } device_table_->DestroySampler(device_, draw_sampler_, nullptr); @@ -87,14 +88,15 @@ VkResult VulkanResourceInitializer::LoadData(VkDeviceSize VulkanResourceAllocator::ResourceData allocator_data) { void* mapped_memory = nullptr; - VkResult result = resource_allocator_->MapResourceMemoryDirect(size, 0, &mapped_memory, allocator_data); + VkResult result = + resource_allocator_->GetDirectAllocator().MapResourceMemory(size, 0, &mapped_memory, allocator_data); if (result == VK_SUCCESS) { GFXRECON_CHECK_CONVERSION_DATA_LOSS(size_t, size); size_t copy_size = static_cast(size); util::platform::MemoryCopy(mapped_memory, copy_size, data, copy_size); - resource_allocator_->UnmapResourceMemoryDirect(allocator_data); + resource_allocator_->GetDirectAllocator().UnmapResourceMemory(allocator_data); } return result; @@ -800,8 +802,8 @@ VkResult VulkanResourceInitializer::CreateStagingImage(const VkImageCreateInfo* VkImage staging_image = VK_NULL_HANDLE; VulkanResourceAllocator::ResourceData staging_image_data = 0; - VkResult result = - resource_allocator_->CreateImageDirect(image_create_info, nullptr, &staging_image, &staging_image_data); + VkResult result = resource_allocator_->GetDirectAllocator().CreateImage( + image_create_info, nullptr, &staging_image, &staging_image_data); if (result == VK_SUCCESS) { @@ -821,12 +823,13 @@ VkResult VulkanResourceInitializer::CreateStagingImage(const VkImageCreateInfo* alloc_info.memoryTypeIndex = memory_type_index; alloc_info.allocationSize = memory_reqs.size; - result = resource_allocator_->AllocateMemoryDirect(&alloc_info, nullptr, &staging_memory, &staging_memory_data); + result = resource_allocator_->GetDirectAllocator().AllocateMemory( + &alloc_info, nullptr, &staging_memory, &staging_memory_data); if (result == VK_SUCCESS) { VkMemoryPropertyFlags flags; - result = resource_allocator_->BindImageMemoryDirect( + result = resource_allocator_->GetDirectAllocator().BindImageMemory( staging_image, staging_memory, 0, staging_image_data, staging_memory_data, &flags); } @@ -853,12 +856,12 @@ void VulkanResourceInitializer::DestroyStagingImage(VkDeviceMemory { if (image != VK_NULL_HANDLE) { - resource_allocator_->DestroyImageDirect(image, nullptr, allocator_image_data); + resource_allocator_->GetDirectAllocator().DestroyImage(image, nullptr, allocator_image_data); } if (memory != VK_NULL_HANDLE) { - resource_allocator_->FreeMemoryDirect(memory, nullptr, allocator_memory_data); + resource_allocator_->GetDirectAllocator().FreeMemory(memory, nullptr, allocator_memory_data); } } @@ -935,7 +938,8 @@ VkResult VulkanResourceInitializer::AcquireStagingBuffer(VkDeviceMemory* create_info.queueFamilyIndexCount = 0; create_info.pQueueFamilyIndices = nullptr; - result = resource_allocator_->CreateBufferDirect(&create_info, nullptr, &staging_buffer, &staging_buffer_data); + result = resource_allocator_->GetDirectAllocator().CreateBuffer( + &create_info, nullptr, &staging_buffer, &staging_buffer_data); if (result == VK_SUCCESS) { @@ -956,13 +960,13 @@ VkResult VulkanResourceInitializer::AcquireStagingBuffer(VkDeviceMemory* alloc_info.allocationSize = memory_requirements.size; alloc_info.memoryTypeIndex = memory_type_index; - result = - resource_allocator_->AllocateMemoryDirect(&alloc_info, nullptr, &staging_memory, &staging_memory_data); + result = resource_allocator_->GetDirectAllocator().AllocateMemory( + &alloc_info, nullptr, &staging_memory, &staging_memory_data); if (result == VK_SUCCESS) { VkMemoryPropertyFlags flags; - result = resource_allocator_->BindBufferMemoryDirect( + result = resource_allocator_->GetDirectAllocator().BindBufferMemory( staging_buffer, staging_memory, 0, staging_buffer_data, staging_memory_data, &flags); } @@ -983,11 +987,11 @@ VkResult VulkanResourceInitializer::AcquireStagingBuffer(VkDeviceMemory* } else { - resource_allocator_->DestroyBufferDirect(staging_buffer, nullptr, staging_buffer_data); + resource_allocator_->GetDirectAllocator().DestroyBuffer(staging_buffer, nullptr, staging_buffer_data); if ((*memory) != VK_NULL_HANDLE) { - resource_allocator_->FreeMemoryDirect(staging_memory, nullptr, staging_memory_data); + resource_allocator_->GetDirectAllocator().FreeMemory(staging_memory, nullptr, staging_memory_data); } } } @@ -1033,12 +1037,12 @@ void VulkanResourceInitializer::ReleaseStagingBuffer(VkDeviceMemory { if (buffer != VK_NULL_HANDLE) { - resource_allocator_->DestroyBufferDirect(buffer, nullptr, staging_buffer_data); + resource_allocator_->GetDirectAllocator().DestroyBuffer(buffer, nullptr, staging_buffer_data); } if (memory != VK_NULL_HANDLE) { - resource_allocator_->FreeMemoryDirect(memory, nullptr, staging_memory_data); + resource_allocator_->GetDirectAllocator().FreeMemory(memory, nullptr, staging_memory_data); } } } diff --git a/framework/decode/vulkan_virtual_swapchain.cpp b/framework/decode/vulkan_virtual_swapchain.cpp index 19430cf78..96e226b26 100644 --- a/framework/decode/vulkan_virtual_swapchain.cpp +++ b/framework/decode/vulkan_virtual_swapchain.cpp @@ -22,6 +22,7 @@ #include "decode/vulkan_virtual_swapchain.h" +#include "decode/vulkan_direct_allocator.h" #include "decode/vulkan_resource_allocator.h" #include "decode/decoder_util.h" @@ -105,8 +106,8 @@ void VulkanVirtualSwapchain::CleanSwapchainResourceData(const VulkanDeviceInfo* for (const VulkanImageInfo& image_info : swapchain_info->image_infos) { - allocator->DestroyImageDirect(image_info.handle, nullptr, image_info.allocator_data); - allocator->FreeMemoryDirect(image_info.memory, nullptr, image_info.memory_allocator_data); + allocator->GetDirectAllocator().DestroyImage(image_info.handle, nullptr, image_info.allocator_data); + allocator->GetDirectAllocator().FreeMemory(image_info.memory, nullptr, image_info.memory_allocator_data); } // Delete the virtual swapchain-specific swapchain resource data @@ -115,8 +116,10 @@ void VulkanVirtualSwapchain::CleanSwapchainResourceData(const VulkanDeviceInfo* auto& swapchain_resources = swapchain_resources_[swapchain]; for (const VirtualImage& image_info : swapchain_resources->virtual_swapchain_images) { - allocator->DestroyImageDirect(image_info.image, nullptr, image_info.resource_allocator_data); - allocator->FreeMemoryDirect(image_info.memory, nullptr, image_info.memory_allocator_data); + allocator->GetDirectAllocator().DestroyImage( + image_info.image, nullptr, image_info.resource_allocator_data); + allocator->GetDirectAllocator().FreeMemory( + image_info.memory, nullptr, image_info.memory_allocator_data); } for (auto& copy_cmd_data : swapchain_resources->copy_cmd_data) @@ -1048,8 +1051,8 @@ VkResult VulkanVirtualSwapchain::CreateVirtualSwapchainImage(const VulkanDeviceI VulkanResourceAllocator* allocator = device_info->allocator.get(); assert(allocator != nullptr); - VkResult result = - allocator->CreateImageDirect(&image_create_info, nullptr, &image.image, &image.resource_allocator_data); + VkResult result = allocator->GetDirectAllocator().CreateImage( + &image_create_info, nullptr, &image.image, &image.resource_allocator_data); if (result == VK_SUCCESS) { @@ -1084,12 +1087,13 @@ VkResult VulkanVirtualSwapchain::CreateVirtualSwapchainImage(const VulkanDeviceI alloc_info.memoryTypeIndex = memory_type_index; alloc_info.allocationSize = memory_reqs.size; - result = allocator->AllocateMemoryDirect(&alloc_info, nullptr, &image.memory, &image.memory_allocator_data); + result = allocator->GetDirectAllocator().AllocateMemory( + &alloc_info, nullptr, &image.memory, &image.memory_allocator_data); if (result == VK_SUCCESS) { VkMemoryPropertyFlags flags; - result = allocator->BindImageMemoryDirect( + result = allocator->GetDirectAllocator().BindImageMemory( image.image, image.memory, 0, image.resource_allocator_data, image.memory_allocator_data, &flags); } @@ -1097,11 +1101,11 @@ VkResult VulkanVirtualSwapchain::CreateVirtualSwapchainImage(const VulkanDeviceI { if (image.memory != VK_NULL_HANDLE) { - allocator->FreeMemoryDirect(image.memory, nullptr, image.memory_allocator_data); + allocator->GetDirectAllocator().FreeMemory(image.memory, nullptr, image.memory_allocator_data); image.memory = VK_NULL_HANDLE; } - allocator->DestroyImageDirect(image.image, nullptr, image.resource_allocator_data); + allocator->GetDirectAllocator().DestroyImage(image.image, nullptr, image.resource_allocator_data); image.image = VK_NULL_HANDLE; } }