1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
/*
** Copyright (c) 2020 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_REBIND_ALLOCATOR_H
#define GFXRECON_DECODE_VULKAN_REBIND_ALLOCATOR_H
#include "decode/vulkan_resource_allocator.h"
#include "util/defines.h"
#include "vk_mem_alloc.h"
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include <unordered_map>
GFXRECON_BEGIN_NAMESPACE(gfxrecon)
GFXRECON_BEGIN_NAMESPACE(decode)
class VulkanRebindAllocator : public VulkanResourceAllocator
{
public:
VulkanRebindAllocator();
virtual ~VulkanRebindAllocator() override;
virtual VkResult Initialize(uint32_t api_version,
VkInstance instance,
VkPhysicalDevice physical_device,
VkDevice device,
const std::vector<std::string>& enabled_device_extensions,
VkPhysicalDeviceType capture_device_type,
const VkPhysicalDeviceMemoryProperties& capture_memory_properties,
const VkPhysicalDeviceMemoryProperties& replay_memory_properties,
const Functions& functions) override;
virtual void Destroy() override;
virtual VkResult CreateBuffer(const VkBufferCreateInfo* create_info,
const VkAllocationCallbacks* allocation_callbacks,
format::HandleId capture_id,
VkBuffer* buffer,
ResourceData* allocator_data) override;
virtual void DestroyBuffer(VkBuffer buffer,
const VkAllocationCallbacks* allocation_callbacks,
ResourceData allocator_data) override;
virtual VkResult CreateImage(const VkImageCreateInfo* create_info,
const VkAllocationCallbacks* allocation_callbacks,
format::HandleId capture_id,
VkImage* image,
ResourceData* allocator_data) override;
virtual void DestroyImage(VkImage image,
const VkAllocationCallbacks* allocation_callbacks,
ResourceData allocator_data) override;
virtual void GetImageSubresourceLayout(VkImage image,
const VkImageSubresource* subresource,
VkSubresourceLayout* layout,
const VkSubresourceLayout* original_layout,
ResourceData allocator_data) override;
virtual VkResult AllocateMemory(const VkMemoryAllocateInfo* allocate_info,
const VkAllocationCallbacks* allocation_callbacks,
format::HandleId capture_id,
VkDeviceMemory* memory,
MemoryData* allocator_data) override;
virtual void FreeMemory(VkDeviceMemory memory,
const VkAllocationCallbacks* allocation_callbacks,
MemoryData allocator_data) override;
virtual void GetDeviceMemoryCommitment(VkDeviceMemory memory,
VkDeviceSize* committed_memory_in_bytes,
MemoryData allocator_data) override;
virtual VkResult BindBufferMemory(VkBuffer buffer,
VkDeviceMemory memory,
VkDeviceSize memory_offset,
ResourceData allocator_buffer_data,
MemoryData allocator_memory_data,
VkMemoryPropertyFlags* bind_memory_properties) override;
virtual VkResult BindBufferMemory2(uint32_t bind_info_count,
const VkBindBufferMemoryInfo* bind_infos,
const ResourceData* allocator_buffer_datas,
const MemoryData* allocator_memory_datas,
VkMemoryPropertyFlags* bind_memory_properties) override;
virtual VkResult BindImageMemory(VkImage image,
VkDeviceMemory memory,
VkDeviceSize memory_offset,
ResourceData allocator_image_data,
MemoryData allocator_memory_data,
VkMemoryPropertyFlags* bind_memory_properties) override;
virtual VkResult BindImageMemory2(uint32_t bind_info_count,
const VkBindImageMemoryInfo* bind_infos,
const ResourceData* allocator_image_datas,
const MemoryData* allocator_memory_datas,
VkMemoryPropertyFlags* bind_memory_properties) override;
virtual VkResult MapMemory(VkDeviceMemory memory,
VkDeviceSize offset,
VkDeviceSize size,
VkMemoryMapFlags flags,
void** data,
MemoryData allocator_data) override;
virtual void UnmapMemory(VkDeviceMemory memory, MemoryData allocator_data) override;
virtual VkResult FlushMappedMemoryRanges(uint32_t memory_range_count,
const VkMappedMemoryRange* memory_ranges,
const MemoryData* allocator_datas) override;
virtual VkResult InvalidateMappedMemoryRanges(uint32_t memory_range_count,
const VkMappedMemoryRange* memory_ranges,
const MemoryData* allocator_datas) override;
virtual VkResult
WriteMappedMemoryRange(MemoryData allocator_data, uint64_t offset, uint64_t size, const uint8_t* data) override;
virtual void ReportAllocateMemoryIncompatibility(const VkMemoryAllocateInfo* allocate_info) override;
virtual void ReportBindBufferIncompatibility(VkBuffer buffer,
ResourceData allocator_resource_data,
MemoryData allocator_memory_data) override;
virtual void ReportBindBuffer2Incompatibility(uint32_t bind_info_count,
const VkBindBufferMemoryInfo* bind_infos,
const ResourceData* allocator_resource_datas,
const MemoryData* allocator_memory_datas) override;
virtual void ReportBindImageIncompatibility(VkImage image,
ResourceData allocator_resource_data,
MemoryData allocator_memory_data) override;
virtual void ReportBindImage2Incompatibility(uint32_t bind_info_count,
const VkBindImageMemoryInfo* bind_infos,
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);
}
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 VkResult MapResourceMemoryDirect(VkDeviceSize size,
VkMemoryMapFlags flags,
void** data,
ResourceData allocator_data) override;
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; }
private:
struct MemoryAllocInfo;
struct SubresourceLayouts
{
VkImageSubresource subresource{};
VkSubresourceLayout original{};
VkSubresourceLayout rebind{};
};
struct ResourceAllocInfo
{
MemoryAllocInfo* memory_info{ nullptr };
VmaAllocation allocation{ VK_NULL_HANDLE };
bool is_host_visible{ false };
void* mapped_pointer{ nullptr };
VkDeviceSize original_offset{ 0 };
VkDeviceSize rebind_offset{ 0 };
VkDeviceSize size{ 0 };
bool is_image{ false };
VkFlags usage{ 0 };
VkImageTiling tiling{};
uint32_t height{ 0 };
bool uses_extensions{ false };
// Image layouts for performing mapped memory writes to linear images with different capture/replay memory
// alignments.
std::vector<SubresourceLayouts> layouts;
};
struct MemoryAllocInfo
{
VkDeviceSize allocation_size{ 0 };
uint32_t original_index{ std::numeric_limits<uint32_t>::max() };
bool is_mapped{ false };
VkDeviceSize mapped_offset{ 0 };
std::unique_ptr<uint8_t[]> original_content;
std::unordered_map<VkBuffer, ResourceAllocInfo*> original_buffers;
std::unordered_map<VkImage, ResourceAllocInfo*> original_images;
};
private:
void WriteBoundResource(ResourceAllocInfo* resource_alloc_info,
VkDeviceSize src_offset,
VkDeviceSize dst_offset,
VkDeviceSize data_size,
const uint8_t* data);
// Identify sub-ranges of resources that overlap with a memory region from their original memory binding. If the
// resource overlapped with the original range, the src_offset is the offset from the start of the original
// resource, the dst_offset is the offset from the start of the new resource allocation, and the data_size is the
// region of the resource that overlaps with the original memory region.
bool TranslateMemoryRange(const ResourceAllocInfo* resource_alloc_info,
VkDeviceSize oiriginal_start,
VkDeviceSize original_end,
VkDeviceSize* src_offset,
VkDeviceSize* dst_offset,
VkDeviceSize* data_size);
void UpdateBoundResource(ResourceAllocInfo* resource_alloc_info,
VkDeviceSize write_start,
VkDeviceSize write_end,
const uint8_t* data);
VkResult UpdateMappedMemoryRange(ResourceAllocInfo* resource_alloc_info,
VkDeviceSize oiriginal_start,
VkDeviceSize original_end,
VkResult (*update_func)(VmaAllocator, VmaAllocation, VkDeviceSize, VkDeviceSize));
VkResult UpdateMappedMemoryRanges(uint32_t memory_range_count,
const VkMappedMemoryRange* memory_ranges,
const MemoryData* allocator_datas,
VkResult (*update_func)(VmaAllocator, VmaAllocation, VkDeviceSize, VkDeviceSize));
VmaMemoryUsage GetBufferMemoryUsage(VkBufferUsageFlags buffer_usage,
VkMemoryPropertyFlags capture_properties,
const VkMemoryRequirements& replay_requirements);
VmaMemoryUsage GetImageMemoryUsage(VkImageUsageFlags image_usage,
VkImageTiling tiling,
VkMemoryPropertyFlags capture_properties,
const VkMemoryRequirements& replay_requirements);
VmaMemoryUsage AdjustMemoryUsage(VmaMemoryUsage desired_usage, const VkMemoryRequirements& replay_requirements);
void ReportBindIncompatibility(const ResourceData* allocator_resource_datas, uint32_t resource_count);
private:
VkDevice device_;
VmaAllocator allocator_;
Functions functions_;
VmaVulkanFunctions vma_functions_;
VkPhysicalDeviceType capture_device_type_;
VkPhysicalDeviceMemoryProperties capture_memory_properties_;
VkPhysicalDeviceMemoryProperties replay_memory_properties_;
};
GFXRECON_END_NAMESPACE(decode)
GFXRECON_END_NAMESPACE(gfxrecon)
#endif // GFXRECON_DECODE_VULKAN_REBIND_ALLOCATOR_H
|