File: vulkan_remap_allocator.cpp

package info (click to toggle)
gfxreconstruct 0.9.18%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 24,636 kB
  • sloc: cpp: 328,961; ansic: 25,454; python: 18,156; xml: 255; sh: 128; makefile: 6
file content (119 lines) | stat: -rw-r--r-- 5,220 bytes parent folder | download
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
/*
** 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.
*/

#include "decode/vulkan_remap_allocator.h"

#include "decode/portability.h"
#include "util/logging.h"

#include <cassert>

GFXRECON_BEGIN_NAMESPACE(gfxrecon)
GFXRECON_BEGIN_NAMESPACE(decode)

VkResult VulkanRemapAllocator::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)
{
    VkResult result = VK_ERROR_INITIALIZATION_FAILED;

    VulkanDefaultAllocator::Initialize(api_version,
                                       instance,
                                       physical_device,
                                       device,
                                       enabled_device_extensions,
                                       capture_device_type,
                                       capture_memory_properties,
                                       replay_memory_properties,
                                       functions);

    if ((capture_memory_properties.memoryTypeCount == 0) || (replay_memory_properties.memoryTypeCount == 0))
    {
        GFXRECON_LOG_FATAL("Capture file does not contain physical device memory properties and cannot be used with "
                           "memory translation.");
    }
    else if (portability::CheckMemoryTypeCompatibility(capture_memory_properties, replay_memory_properties, false))
    {
        // One-to-one mapping when all memory types match.
        for (uint32_t i = 0; i < replay_memory_properties.memoryTypeCount; ++i)
        {
            index_map_.push_back(i);
        }

        result = VK_SUCCESS;
    }
    else
    {
        index_map_.resize(capture_memory_properties.memoryTypeCount);

        for (uint32_t i = 0; i < capture_memory_properties.memoryTypeCount; ++i)
        {
            index_map_[i] =
                portability::FindCompatibleMemoryType(i, capture_memory_properties, replay_memory_properties);
        }

        if (portability::CheckMemoryTypeIndexValidity(index_map_))
        {
            result = VK_SUCCESS;
        }
        else
        {
            GFXRECON_LOG_FATAL(
                "Failed to find valid memory type mappings for replay when using the \"-m remap\" option.");
            GFXRECON_LOG_FATAL(
                "Try replay with rebind memory translation enabled via the \"-m rebind\" option instead.");
        }
    }

    return result;
}

VkResult VulkanRemapAllocator::AllocateMemory(const VkMemoryAllocateInfo*  allocate_info,
                                              const VkAllocationCallbacks* allocation_callbacks,
                                              format::HandleId             capture_id,
                                              VkDeviceMemory*              memory,
                                              MemoryData*                  allocator_data)
{
    VkResult result = VK_ERROR_INITIALIZATION_FAILED;

    if ((allocate_info != nullptr) && (allocator_data != nullptr))
    {
        assert(allocate_info->memoryTypeIndex < index_map_.size());

        VkMemoryAllocateInfo replay_allocate_info = *allocate_info;

        replay_allocate_info.memoryTypeIndex = index_map_[allocate_info->memoryTypeIndex];

        result = Allocate(&replay_allocate_info, allocation_callbacks, capture_id, memory, allocator_data);
    }

    return result;
}

GFXRECON_END_NAMESPACE(decode)
GFXRECON_END_NAMESPACE(gfxrecon)