• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2015-2016 The Khronos Group Inc.
2  * Copyright (c) 2015-2016 Valve Corporation
3  * Copyright (c) 2015-2016 LunarG, Inc.
4  * Copyright (C) 2015-2016 Google Inc.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Author: Tobin Ehlis <tobine@google.com>
19  * Author: Mark Lobodzinski <mark@lunarg.com>
20  */
21 
22 #include "vulkan/vulkan.h"
23 
24 #include <unordered_map>
25 #include <unordered_set>
26 
27 #include "vk_layer_data.h"
28 #include "vk_safe_struct.h"
29 #include "vk_layer_utils.h"
30 #include "mutex"
31 
32 #pragma once
33 
34 namespace unique_objects {
35 
36 // All increments must be guarded by global_lock
37 static uint64_t global_unique_id = 1;
38 
39 struct TEMPLATE_STATE {
40     VkDescriptorUpdateTemplateKHR desc_update_template;
41     safe_VkDescriptorUpdateTemplateCreateInfoKHR create_info;
42 
TEMPLATE_STATETEMPLATE_STATE43     TEMPLATE_STATE(VkDescriptorUpdateTemplateKHR update_template, safe_VkDescriptorUpdateTemplateCreateInfoKHR *pCreateInfo)
44         : desc_update_template(update_template), create_info(*pCreateInfo) {}
45 };
46 
47 struct instance_layer_data {
48     VkInstance instance;
49 
50     debug_report_data *report_data;
51     std::vector<VkDebugReportCallbackEXT> logging_callback;
52     VkLayerInstanceDispatchTable dispatch_table = {};
53 
54     // The following are for keeping track of the temporary callbacks that can
55     // be used in vkCreateInstance and vkDestroyInstance:
56     uint32_t num_tmp_callbacks;
57     VkDebugReportCallbackCreateInfoEXT *tmp_dbg_create_infos;
58     VkDebugReportCallbackEXT *tmp_callbacks;
59 
60     std::unordered_map<uint64_t, uint64_t> unique_id_mapping;  // Map uniqueID to actual object handle
61 };
62 
63 struct layer_data {
64     instance_layer_data *instance_data;
65 
66     debug_report_data *report_data;
67     VkLayerDispatchTable dispatch_table = {};
68 
69     std::unordered_map<uint64_t, std::unique_ptr<TEMPLATE_STATE>> desc_template_map;
70 
71     bool wsi_enabled;
72     std::unordered_map<uint64_t, uint64_t> unique_id_mapping;  // Map uniqueID to actual object handle
73     VkPhysicalDevice gpu;
74 
75     struct SubpassesUsageStates {
76         std::unordered_set<uint32_t> subpasses_using_color_attachment;
77         std::unordered_set<uint32_t> subpasses_using_depthstencil_attachment;
78     };
79     // uses unwrapped handles
80     std::unordered_map<VkRenderPass, SubpassesUsageStates> renderpasses_states;
81 
layer_datalayer_data82     layer_data() : wsi_enabled(false), gpu(VK_NULL_HANDLE){};
83 };
84 
85 static std::unordered_map<void *, instance_layer_data *> instance_layer_data_map;
86 static std::unordered_map<void *, layer_data *> layer_data_map;
87 
88 static std::mutex global_lock;  // Protect map accesses and unique_id increments
89 
90 struct GenericHeader {
91     VkStructureType sType;
92     void *pNext;
93 };
94 
95 template <typename T>
ContainsExtStruct(const T * target,VkStructureType ext_type)96 bool ContainsExtStruct(const T *target, VkStructureType ext_type) {
97     assert(target != nullptr);
98 
99     const GenericHeader *ext_struct = reinterpret_cast<const GenericHeader *>(target->pNext);
100 
101     while (ext_struct != nullptr) {
102         if (ext_struct->sType == ext_type) {
103             return true;
104         }
105 
106         ext_struct = reinterpret_cast<const GenericHeader *>(ext_struct->pNext);
107     }
108 
109     return false;
110 }
111 
112 /* Unwrap a handle. */
113 // must hold lock!
114 template <typename HandleType, typename MapType>
Unwrap(MapType * layer_data,HandleType wrappedHandle)115 HandleType Unwrap(MapType *layer_data, HandleType wrappedHandle) {
116     // TODO: don't use operator[] here.
117     return (HandleType)layer_data->unique_id_mapping[reinterpret_cast<uint64_t const &>(wrappedHandle)];
118 }
119 
120 /* Wrap a newly created handle with a new unique ID, and return the new ID. */
121 // must hold lock!
122 template <typename HandleType, typename MapType>
WrapNew(MapType * layer_data,HandleType newlyCreatedHandle)123 HandleType WrapNew(MapType *layer_data, HandleType newlyCreatedHandle) {
124     auto unique_id = global_unique_id++;
125     layer_data->unique_id_mapping[unique_id] = reinterpret_cast<uint64_t const &>(newlyCreatedHandle);
126     return (HandleType)unique_id;
127 }
128 
129 }  // namespace unique_objects
130