• 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 "vk_layer_data.h"
25 #include "vk_safe_struct.h"
26 #include "vk_layer_utils.h"
27 #include "mutex"
28 
29 #pragma once
30 
31 namespace unique_objects {
32 
33 // The display-server-specific WSI extensions are handled explicitly
34 static const char *kUniqueObjectsSupportedInstanceExtensions =
35 #ifdef VK_USE_PLATFORM_XLIB_KHR
36     VK_KHR_XLIB_SURFACE_EXTENSION_NAME
37 #endif
38 #ifdef VK_USE_PLATFORM_XCB_KHR
39     VK_KHR_XCB_SURFACE_EXTENSION_NAME
40 #endif
41 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
42     VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
43 #endif
44 #ifdef VK_USE_PLATFORM_MIR_KHR
45     VK_KHR_MIR_SURFACE_EXTENSION_NAME
46 #endif
47 #ifdef VK_USE_PLATFORM_ANDROID_KHR
48     VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
49 #endif
50 #ifdef VK_USE_PLATFORM_WIN32_KHR
51     VK_KHR_WIN32_SURFACE_EXTENSION_NAME
52 #endif
53     VK_EXT_DEBUG_MARKER_EXTENSION_NAME
54     VK_EXT_DEBUG_REPORT_EXTENSION_NAME
55     VK_KHR_DISPLAY_EXTENSION_NAME
56     VK_KHR_SURFACE_EXTENSION_NAME
57     VK_NV_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME
58     VK_EXT_VALIDATION_FLAGS_EXTENSION_NAME;
59 
60 static const char *kUniqueObjectsSupportedDeviceExtensions =
61     VK_AMD_RASTERIZATION_ORDER_EXTENSION_NAME
62     VK_AMD_SHADER_TRINARY_MINMAX_EXTENSION_NAME
63     VK_AMD_SHADER_EXPLICIT_VERTEX_PARAMETER_EXTENSION_NAME
64     VK_AMD_GCN_SHADER_EXTENSION_NAME
65     VK_IMG_FILTER_CUBIC_EXTENSION_NAME
66     VK_IMG_FORMAT_PVRTC_EXTENSION_NAME
67     VK_KHR_SAMPLER_MIRROR_CLAMP_TO_EDGE_EXTENSION_NAME
68     VK_KHR_SWAPCHAIN_EXTENSION_NAME
69     VK_KHR_DISPLAY_SWAPCHAIN_EXTENSION_NAME
70     VK_NV_DEDICATED_ALLOCATION_EXTENSION_NAME
71     VK_NV_GLSL_SHADER_EXTENSION_NAME
72     VK_AMD_DRAW_INDIRECT_COUNT_EXTENSION_NAME
73     VK_AMD_NEGATIVE_VIEWPORT_HEIGHT_EXTENSION_NAME
74     VK_AMD_GPU_SHADER_HALF_FLOAT_EXTENSION_NAME
75     VK_AMD_SHADER_BALLOT_EXTENSION_NAME
76     VK_NV_EXTERNAL_MEMORY_EXTENSION_NAME
77 #ifdef VK_USE_PLATFORM_WIN32_KHR
78     VK_NV_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME
79     VK_NV_WIN32_KEYED_MUTEX_EXTENSION_NAME
80 #endif
81     VK_NV_EXTERNAL_MEMORY_EXTENSION_NAME;
82 
83 // All increments must be guarded by global_lock
84 static uint64_t global_unique_id = 1;
85 
86 struct layer_data {
87     VkInstance instance;
88 
89     debug_report_data *report_data;
90     std::vector<VkDebugReportCallbackEXT> logging_callback;
91     VkLayerDispatchTable *device_dispatch_table;
92     VkLayerInstanceDispatchTable *instance_dispatch_table;
93 
94     // The following are for keeping track of the temporary callbacks that can
95     // be used in vkCreateInstance and vkDestroyInstance:
96     uint32_t num_tmp_callbacks;
97     VkDebugReportCallbackCreateInfoEXT *tmp_dbg_create_infos;
98     VkDebugReportCallbackEXT *tmp_callbacks;
99 
100     bool wsi_enabled;
101     std::unordered_map<uint64_t, uint64_t> unique_id_mapping; // Map uniqueID to actual object handle
102     VkPhysicalDevice gpu;
103 
layer_datalayer_data104     layer_data() : wsi_enabled(false), gpu(VK_NULL_HANDLE){};
105 };
106 
107 struct instance_extension_enables {
108     bool wsi_enabled;
109     bool xlib_enabled;
110     bool xcb_enabled;
111     bool wayland_enabled;
112     bool mir_enabled;
113     bool android_enabled;
114     bool win32_enabled;
115     bool display_enabled;
116 };
117 
118 static std::unordered_map<void *, struct instance_extension_enables> instance_ext_map;
119 static std::unordered_map<void *, layer_data *> layer_data_map;
120 
121 static std::mutex global_lock; // Protect map accesses and unique_id increments
122 
123 struct GenericHeader {
124     VkStructureType sType;
125     void *pNext;
126 };
127 
ContainsExtStruct(const T * target,VkStructureType ext_type)128 template <typename T> bool ContainsExtStruct(const T *target, VkStructureType ext_type) {
129     assert(target != nullptr);
130 
131     const GenericHeader *ext_struct = reinterpret_cast<const GenericHeader *>(target->pNext);
132 
133     while (ext_struct != nullptr) {
134         if (ext_struct->sType == ext_type) {
135             return true;
136         }
137 
138         ext_struct = reinterpret_cast<const GenericHeader *>(ext_struct->pNext);
139     }
140 
141     return false;
142 }
143 
144 } // namespace unique_objects
145