forked from KhronosGroup/Vulkan-ValidationLayers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice_memory_state.h
154 lines (127 loc) · 6.13 KB
/
device_memory_state.h
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
/* Copyright (c) 2015-2021 The Khronos Group Inc.
* Copyright (c) 2015-2021 Valve Corporation
* Copyright (c) 2015-2021 LunarG, Inc.
* Copyright (C) 2015-2021 Google Inc.
* Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.
*
* 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.
*
* Author: Courtney Goeltzenleuchter <[email protected]>
* Author: Tobin Ehlis <[email protected]>
* Author: Chris Forbes <[email protected]>
* Author: Mark Lobodzinski <[email protected]>
* Author: Dave Houlton <[email protected]>
* Author: John Zulauf <[email protected]>
* Author: Tobias Hector <[email protected]>
* Author: Jeremy Gebben <[email protected]>
*/
#pragma once
#include "base_node.h"
class IMAGE_STATE;
struct MemRange {
VkDeviceSize offset = 0;
VkDeviceSize size = 0;
};
struct DedicatedBinding {
VulkanTypedHandle handle;
union CreateInfo {
CreateInfo(const VkBufferCreateInfo &b) : buffer(b) {}
CreateInfo(const VkImageCreateInfo &i) : image(i) {}
VkBufferCreateInfo buffer;
VkImageCreateInfo image;
} create_info;
DedicatedBinding(VkBuffer buffer, const VkBufferCreateInfo &buffer_create_info)
: handle(buffer, kVulkanObjectTypeBuffer), create_info(buffer_create_info) {}
DedicatedBinding(VkImage image, const VkImageCreateInfo &image_create_info)
: handle(image, kVulkanObjectTypeImage), create_info(image_create_info) {}
};
// Data struct for tracking memory object
class DEVICE_MEMORY_STATE : public BASE_NODE {
public:
const safe_VkMemoryAllocateInfo alloc_info;
const VkExternalMemoryHandleTypeFlags export_handle_type_flags;
const VkExternalMemoryHandleTypeFlags import_handle_type_flags;
const bool unprotected; // can't be used for protected memory
const bool multi_instance; // Allocated from MULTI_INSTANCE heap or having more than one deviceMask bit set
const layer_data::optional<DedicatedBinding> dedicated;
MemRange mapped_range;
void *p_driver_data; // Pointer to application's actual memory
const VkDeviceSize fake_base_address; // To allow a unified view of allocations, useful to Synchronization Validation
DEVICE_MEMORY_STATE(VkDeviceMemory mem, const VkMemoryAllocateInfo *p_alloc_info, uint64_t fake_address,
const VkMemoryType &memory_type, const VkMemoryHeap &memory_heap,
layer_data::optional<DedicatedBinding> &&dedicated_binding, uint32_t physical_device_count);
bool IsImport() const { return import_handle_type_flags != 0; }
bool IsImportAHB() const {
return (import_handle_type_flags & VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID) != 0;
}
bool IsExport() const { return export_handle_type_flags != 0; }
bool IsDedicatedBuffer() const { return dedicated && dedicated->handle.type == kVulkanObjectTypeBuffer; }
bool IsDedicatedImage() const { return dedicated && dedicated->handle.type == kVulkanObjectTypeImage; }
VkDeviceMemory mem() const { return handle_.Cast<VkDeviceMemory>(); }
const NodeSet &ObjectBindings() const { return parent_nodes_; }
};
// Generic memory binding struct to track objects bound to objects
struct MEM_BINDING {
std::shared_ptr<DEVICE_MEMORY_STATE> mem_state;
VkDeviceSize offset;
VkDeviceSize size;
};
inline bool operator==(MEM_BINDING a, MEM_BINDING b) NOEXCEPT {
return a.mem_state == b.mem_state && a.offset == b.offset && a.size == b.size;
}
namespace std {
template <>
struct hash<MEM_BINDING> {
size_t operator()(MEM_BINDING mb) const NOEXCEPT {
auto intermediate = hash<uint64_t>()(reinterpret_cast<uint64_t &>(mb.mem_state)) ^ hash<uint64_t>()(mb.offset);
return intermediate ^ hash<uint64_t>()(mb.size);
}
};
} // namespace std
// Superclass for bindable object state (currently images, buffers and acceleration structures)
class BINDABLE : public BASE_NODE {
protected:
using BoundMemoryMap = small_unordered_map<VkDeviceMemory, MEM_BINDING, 1>;
BoundMemoryMap bound_memory_;
public:
// Tracks external memory types creating resource
const VkExternalMemoryHandleTypeFlags external_memory_handle;
const bool sparse; // Is this object being bound with sparse memory or not?
const bool unprotected; // can't be used for protected memory
template <typename Handle>
BINDABLE(Handle h, VulkanObjectType t, bool is_sparse, bool is_unprotected, VkExternalMemoryHandleTypeFlags handle_type)
: BASE_NODE(h, t),
bound_memory_{},
external_memory_handle(handle_type),
sparse(is_sparse),
unprotected(is_unprotected) {}
virtual ~BINDABLE() {
if (!Destroyed()) {
Destroy();
}
}
void Destroy() override;
// Return unordered set of memory objects that are bound
// Instead of creating a set from scratch each query, return the cached one
const BoundMemoryMap &GetBoundMemory() const { return bound_memory_; }
const MEM_BINDING *Binding() const {
return (!sparse && bound_memory_.size() == 1) ? &(bound_memory_.begin()->second) : nullptr;
}
const DEVICE_MEMORY_STATE *MemState() const { return Binding() ? Binding()->mem_state.get() : nullptr; }
virtual void SetMemBinding(std::shared_ptr<DEVICE_MEMORY_STATE> &mem, VkDeviceSize memory_offset);
void SetSparseMemBinding(std::shared_ptr<DEVICE_MEMORY_STATE> &mem, const VkDeviceSize mem_offset, const VkDeviceSize mem_size);
bool IsExternalAHB() const {
return (external_memory_handle & VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID) != 0;
}
virtual VkDeviceSize GetFakeBaseAddress() const;
};