Mark Lobodzinski | 6eda00a | 2016-02-02 15:55:36 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2015-2016 The Khronos Group Inc. |
| 2 | * Copyright (c) 2015-2016 Valve Corporation |
| 3 | * Copyright (c) 2015-2016 LunarG, Inc. |
Mike Stroyan | 3712d5c | 2015-04-02 11:59:05 -0600 | [diff] [blame] | 4 | * |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
Mike Stroyan | 3712d5c | 2015-04-02 11:59:05 -0600 | [diff] [blame] | 8 | * |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Mike Stroyan | 3712d5c | 2015-04-02 11:59:05 -0600 | [diff] [blame] | 10 | * |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
Courtney Goeltzenleuchter | 0555952 | 2015-10-30 11:14:30 -0600 | [diff] [blame] | 16 | * |
| 17 | * Author: Cody Northrop <cody@lunarg.com> |
| 18 | * Author: Mike Stroyan <mike@LunarG.com> |
Mike Stroyan | 3712d5c | 2015-04-02 11:59:05 -0600 | [diff] [blame] | 19 | */ |
Mark Lobodzinski | 6eda00a | 2016-02-02 15:55:36 -0700 | [diff] [blame] | 20 | |
Mike Stroyan | 313f7e6 | 2015-08-10 16:42:53 -0600 | [diff] [blame] | 21 | #ifndef THREADING_H |
| 22 | #define THREADING_H |
Jeremy Hayes | b350beb | 2016-04-12 13:48:52 -0600 | [diff] [blame] | 23 | #include <condition_variable> |
| 24 | #include <mutex> |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 25 | #include <vector> |
Mike Stroyan | 313f7e6 | 2015-08-10 16:42:53 -0600 | [diff] [blame] | 26 | #include "vk_layer_config.h" |
| 27 | #include "vk_layer_logging.h" |
Mike Stroyan | 3712d5c | 2015-04-02 11:59:05 -0600 | [diff] [blame] | 28 | |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 29 | #if defined(__LP64__) || defined(_WIN64) || defined(__x86_64__) || defined(_M_X64) || defined(__ia64) || defined(_M_IA64) || \ |
| 30 | defined(__aarch64__) || defined(__powerpc64__) |
Mike Stroyan | 31c50c8 | 2016-01-29 15:09:04 -0700 | [diff] [blame] | 31 | // If pointers are 64-bit, then there can be separate counters for each |
| 32 | // NONDISPATCHABLE_HANDLE type. Otherwise they are all typedef uint64_t. |
| 33 | #define DISTINCT_NONDISPATCHABLE_HANDLES |
| 34 | #endif |
| 35 | |
Mike Stroyan | 3712d5c | 2015-04-02 11:59:05 -0600 | [diff] [blame] | 36 | // Draw State ERROR codes |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 37 | typedef enum _THREADING_CHECKER_ERROR { |
| 38 | THREADING_CHECKER_NONE, // Used for INFO & other non-error messages |
| 39 | THREADING_CHECKER_MULTIPLE_THREADS, // Object used simultaneously by multiple threads |
| 40 | THREADING_CHECKER_SINGLE_THREAD_REUSE, // Object used simultaneously by recursion in single thread |
Mike Stroyan | 3712d5c | 2015-04-02 11:59:05 -0600 | [diff] [blame] | 41 | } THREADING_CHECKER_ERROR; |
| 42 | |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 43 | struct object_use_data { |
| 44 | loader_platform_thread_id thread; |
| 45 | int reader_count; |
| 46 | int writer_count; |
| 47 | }; |
| 48 | |
| 49 | struct layer_data; |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 50 | |
Jeremy Hayes | b350beb | 2016-04-12 13:48:52 -0600 | [diff] [blame] | 51 | static std::mutex global_lock; |
| 52 | static std::condition_variable global_condition; |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 53 | |
| 54 | template <typename T> class counter { |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 55 | public: |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 56 | const char *typeName; |
| 57 | VkDebugReportObjectTypeEXT objectType; |
Mike Stroyan | 1a08001 | 2016-01-29 15:33:21 -0700 | [diff] [blame] | 58 | std::unordered_map<T, object_use_data> uses; |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 59 | void startWrite(debug_report_data *report_data, T object) { |
Dustin Graves | 080069b | 2016-04-05 13:48:15 -0600 | [diff] [blame] | 60 | bool skipCall = false; |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 61 | loader_platform_thread_id tid = loader_platform_get_thread_id(); |
Jeremy Hayes | b350beb | 2016-04-12 13:48:52 -0600 | [diff] [blame] | 62 | std::unique_lock<std::mutex> lock(global_lock); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 63 | if (uses.find(object) == uses.end()) { |
| 64 | // There is no current use of the object. Record writer thread. |
| 65 | struct object_use_data *use_data = &uses[object]; |
| 66 | use_data->reader_count = 0; |
| 67 | use_data->writer_count = 1; |
| 68 | use_data->thread = tid; |
| 69 | } else { |
| 70 | struct object_use_data *use_data = &uses[object]; |
| 71 | if (use_data->reader_count == 0) { |
| 72 | // There are no readers. Two writers just collided. |
| 73 | if (use_data->thread != tid) { |
Dustin Graves | 37bef95 | 2016-02-05 16:06:21 -0700 | [diff] [blame] | 74 | skipCall |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, objectType, (uint64_t)(object), |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 75 | /*location*/ 0, THREADING_CHECKER_MULTIPLE_THREADS, "THREADING", |
| 76 | "THREADING ERROR : object of type %s is simultaneously used in thread %ld and thread %ld", |
| 77 | typeName, use_data->thread, tid); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 78 | if (skipCall) { |
| 79 | // Wait for thread-safe access to object instead of skipping call. |
| 80 | while (uses.find(object) != uses.end()) { |
Jeremy Hayes | b350beb | 2016-04-12 13:48:52 -0600 | [diff] [blame] | 81 | global_condition.wait(lock); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 82 | } |
| 83 | // There is now no current use of the object. Record writer thread. |
| 84 | struct object_use_data *use_data = &uses[object]; |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 85 | use_data->thread = tid; |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 86 | use_data->reader_count = 0; |
| 87 | use_data->writer_count = 1; |
| 88 | } else { |
| 89 | // Continue with an unsafe use of the object. |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 90 | use_data->thread = tid; |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 91 | use_data->writer_count += 1; |
| 92 | } |
| 93 | } else { |
Mike Stroyan | c877450 | 2016-02-05 09:11:32 -0700 | [diff] [blame] | 94 | // This is either safe multiple use in one call, or recursive use. |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 95 | // There is no way to make recursion safe. Just forge ahead. |
| 96 | use_data->writer_count += 1; |
| 97 | } |
| 98 | } else { |
| 99 | // There are readers. This writer collided with them. |
| 100 | if (use_data->thread != tid) { |
Dustin Graves | 37bef95 | 2016-02-05 16:06:21 -0700 | [diff] [blame] | 101 | skipCall |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, objectType, (uint64_t)(object), |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 102 | /*location*/ 0, THREADING_CHECKER_MULTIPLE_THREADS, "THREADING", |
| 103 | "THREADING ERROR : object of type %s is simultaneously used in thread %ld and thread %ld", |
| 104 | typeName, use_data->thread, tid); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 105 | if (skipCall) { |
| 106 | // Wait for thread-safe access to object instead of skipping call. |
| 107 | while (uses.find(object) != uses.end()) { |
Jeremy Hayes | b350beb | 2016-04-12 13:48:52 -0600 | [diff] [blame] | 108 | global_condition.wait(lock); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 109 | } |
| 110 | // There is now no current use of the object. Record writer thread. |
| 111 | struct object_use_data *use_data = &uses[object]; |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 112 | use_data->thread = tid; |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 113 | use_data->reader_count = 0; |
| 114 | use_data->writer_count = 1; |
| 115 | } else { |
| 116 | // Continue with an unsafe use of the object. |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 117 | use_data->thread = tid; |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 118 | use_data->writer_count += 1; |
| 119 | } |
| 120 | } else { |
Mike Stroyan | c877450 | 2016-02-05 09:11:32 -0700 | [diff] [blame] | 121 | // This is either safe multiple use in one call, or recursive use. |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 122 | // There is no way to make recursion safe. Just forge ahead. |
| 123 | use_data->writer_count += 1; |
| 124 | } |
| 125 | } |
| 126 | } |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 129 | void finishWrite(T object) { |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 130 | // Object is no longer in use |
Jeremy Hayes | b350beb | 2016-04-12 13:48:52 -0600 | [diff] [blame] | 131 | std::unique_lock<std::mutex> lock(global_lock); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 132 | uses[object].writer_count -= 1; |
| 133 | if ((uses[object].reader_count == 0) && (uses[object].writer_count == 0)) { |
| 134 | uses.erase(object); |
| 135 | } |
| 136 | // Notify any waiting threads that this object may be safe to use |
Jeremy Hayes | b350beb | 2016-04-12 13:48:52 -0600 | [diff] [blame] | 137 | lock.unlock(); |
| 138 | global_condition.notify_all(); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void startRead(debug_report_data *report_data, T object) { |
Dustin Graves | 080069b | 2016-04-05 13:48:15 -0600 | [diff] [blame] | 142 | bool skipCall = false; |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 143 | loader_platform_thread_id tid = loader_platform_get_thread_id(); |
Jeremy Hayes | b350beb | 2016-04-12 13:48:52 -0600 | [diff] [blame] | 144 | std::unique_lock<std::mutex> lock(global_lock); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 145 | if (uses.find(object) == uses.end()) { |
| 146 | // There is no current use of the object. Record reader count |
| 147 | struct object_use_data *use_data = &uses[object]; |
| 148 | use_data->reader_count = 1; |
| 149 | use_data->writer_count = 0; |
| 150 | use_data->thread = tid; |
Mike Stroyan | c877450 | 2016-02-05 09:11:32 -0700 | [diff] [blame] | 151 | } else if (uses[object].writer_count > 0 && uses[object].thread != tid) { |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 152 | // There is a writer of the object. |
Dustin Graves | 37bef95 | 2016-02-05 16:06:21 -0700 | [diff] [blame] | 153 | skipCall |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, objectType, (uint64_t)(object), |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 154 | /*location*/ 0, THREADING_CHECKER_MULTIPLE_THREADS, "THREADING", |
| 155 | "THREADING ERROR : object of type %s is simultaneously used in thread %ld and thread %ld", typeName, |
| 156 | uses[object].thread, tid); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 157 | if (skipCall) { |
| 158 | // Wait for thread-safe access to object instead of skipping call. |
| 159 | while (uses.find(object) != uses.end()) { |
Jeremy Hayes | b350beb | 2016-04-12 13:48:52 -0600 | [diff] [blame] | 160 | global_condition.wait(lock); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 161 | } |
| 162 | // There is no current use of the object. Record reader count |
| 163 | struct object_use_data *use_data = &uses[object]; |
| 164 | use_data->reader_count = 1; |
| 165 | use_data->writer_count = 0; |
| 166 | use_data->thread = tid; |
| 167 | } else { |
| 168 | uses[object].reader_count += 1; |
| 169 | } |
| 170 | } else { |
| 171 | // There are other readers of the object. Increase reader count |
| 172 | uses[object].reader_count += 1; |
| 173 | } |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 174 | } |
| 175 | void finishRead(T object) { |
Jeremy Hayes | b350beb | 2016-04-12 13:48:52 -0600 | [diff] [blame] | 176 | std::unique_lock<std::mutex> lock(global_lock); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 177 | uses[object].reader_count -= 1; |
| 178 | if ((uses[object].reader_count == 0) && (uses[object].writer_count == 0)) { |
| 179 | uses.erase(object); |
| 180 | } |
| 181 | // Notify and waiting threads that this object may be safe to use |
Jeremy Hayes | b350beb | 2016-04-12 13:48:52 -0600 | [diff] [blame] | 182 | lock.unlock(); |
| 183 | global_condition.notify_all(); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 184 | } |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 185 | counter(const char *name = "", VkDebugReportObjectTypeEXT type = VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT) { |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 186 | typeName = name; |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 187 | objectType = type; |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 188 | } |
| 189 | }; |
| 190 | |
Cody Northrop | 55443ef | 2015-09-28 15:09:32 -0600 | [diff] [blame] | 191 | struct layer_data { |
Mike Stroyan | 313f7e6 | 2015-08-10 16:42:53 -0600 | [diff] [blame] | 192 | debug_report_data *report_data; |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 193 | std::vector<VkDebugReportCallbackEXT> logging_callback; |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 194 | VkLayerDispatchTable *device_dispatch_table; |
| 195 | VkLayerInstanceDispatchTable *instance_dispatch_table; |
Ian Elliott | ed6b5ac | 2016-04-28 09:08:13 -0600 | [diff] [blame^] | 196 | // The following are for keeping track of the temporary callbacks that can |
| 197 | // be used in vkCreateInstance and vkDestroyInstance: |
| 198 | uint32_t num_tmp_callbacks; |
| 199 | VkDebugReportCallbackCreateInfoEXT *tmp_dbg_create_infos; |
| 200 | VkDebugReportCallbackEXT *tmp_callbacks; |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 201 | counter<VkCommandBuffer> c_VkCommandBuffer; |
| 202 | counter<VkDevice> c_VkDevice; |
| 203 | counter<VkInstance> c_VkInstance; |
| 204 | counter<VkQueue> c_VkQueue; |
Mike Stroyan | 31c50c8 | 2016-01-29 15:09:04 -0700 | [diff] [blame] | 205 | #ifdef DISTINCT_NONDISPATCHABLE_HANDLES |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 206 | counter<VkBuffer> c_VkBuffer; |
| 207 | counter<VkBufferView> c_VkBufferView; |
| 208 | counter<VkCommandPool> c_VkCommandPool; |
| 209 | counter<VkDescriptorPool> c_VkDescriptorPool; |
| 210 | counter<VkDescriptorSet> c_VkDescriptorSet; |
| 211 | counter<VkDescriptorSetLayout> c_VkDescriptorSetLayout; |
| 212 | counter<VkDeviceMemory> c_VkDeviceMemory; |
| 213 | counter<VkEvent> c_VkEvent; |
| 214 | counter<VkFence> c_VkFence; |
| 215 | counter<VkFramebuffer> c_VkFramebuffer; |
| 216 | counter<VkImage> c_VkImage; |
| 217 | counter<VkImageView> c_VkImageView; |
| 218 | counter<VkPipeline> c_VkPipeline; |
| 219 | counter<VkPipelineCache> c_VkPipelineCache; |
| 220 | counter<VkPipelineLayout> c_VkPipelineLayout; |
| 221 | counter<VkQueryPool> c_VkQueryPool; |
| 222 | counter<VkRenderPass> c_VkRenderPass; |
| 223 | counter<VkSampler> c_VkSampler; |
| 224 | counter<VkSemaphore> c_VkSemaphore; |
| 225 | counter<VkShaderModule> c_VkShaderModule; |
| 226 | counter<VkDebugReportCallbackEXT> c_VkDebugReportCallbackEXT; |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 227 | #else // DISTINCT_NONDISPATCHABLE_HANDLES |
Mike Stroyan | 31c50c8 | 2016-01-29 15:09:04 -0700 | [diff] [blame] | 228 | counter<uint64_t> c_uint64_t; |
| 229 | #endif // DISTINCT_NONDISPATCHABLE_HANDLES |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 230 | layer_data() |
Ian Elliott | ed6b5ac | 2016-04-28 09:08:13 -0600 | [diff] [blame^] | 231 | : report_data(nullptr), num_tmp_callbacks(0), tmp_dbg_create_infos(nullptr), tmp_callbacks(nullptr), |
| 232 | c_VkCommandBuffer("VkCommandBuffer", VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT), |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 233 | c_VkDevice("VkDevice", VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT), |
| 234 | c_VkInstance("VkInstance", VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT), |
| 235 | c_VkQueue("VkQueue", VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT), |
Mike Stroyan | 31c50c8 | 2016-01-29 15:09:04 -0700 | [diff] [blame] | 236 | #ifdef DISTINCT_NONDISPATCHABLE_HANDLES |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 237 | c_VkBuffer("VkBuffer", VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT), |
| 238 | c_VkBufferView("VkBufferView", VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_VIEW_EXT), |
| 239 | c_VkCommandPool("VkCommandPool", VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_POOL_EXT), |
| 240 | c_VkDescriptorPool("VkDescriptorPool", VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT), |
| 241 | c_VkDescriptorSet("VkDescriptorSet", VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT), |
| 242 | c_VkDescriptorSetLayout("VkDescriptorSetLayout", VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT), |
| 243 | c_VkDeviceMemory("VkDeviceMemory", VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT), |
| 244 | c_VkEvent("VkEvent", VK_DEBUG_REPORT_OBJECT_TYPE_EVENT_EXT), c_VkFence("VkFence", VK_DEBUG_REPORT_OBJECT_TYPE_FENCE_EXT), |
| 245 | c_VkFramebuffer("VkFramebuffer", VK_DEBUG_REPORT_OBJECT_TYPE_FRAMEBUFFER_EXT), |
| 246 | c_VkImage("VkImage", VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT), |
| 247 | c_VkImageView("VkImageView", VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT), |
| 248 | c_VkPipeline("VkPipeline", VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT), |
| 249 | c_VkPipelineCache("VkPipelineCache", VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_CACHE_EXT), |
| 250 | c_VkPipelineLayout("VkPipelineLayout", VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_LAYOUT_EXT), |
| 251 | c_VkQueryPool("VkQueryPool", VK_DEBUG_REPORT_OBJECT_TYPE_QUERY_POOL_EXT), |
| 252 | c_VkRenderPass("VkRenderPass", VK_DEBUG_REPORT_OBJECT_TYPE_RENDER_PASS_EXT), |
| 253 | c_VkSampler("VkSampler", VK_DEBUG_REPORT_OBJECT_TYPE_SAMPLER_EXT), |
| 254 | c_VkSemaphore("VkSemaphore", VK_DEBUG_REPORT_OBJECT_TYPE_SEMAPHORE_EXT), |
| 255 | c_VkShaderModule("VkShaderModule", VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT), |
| 256 | c_VkDebugReportCallbackEXT("VkDebugReportCallbackEXT", VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT) |
| 257 | #else // DISTINCT_NONDISPATCHABLE_HANDLES |
| 258 | c_uint64_t("NON_DISPATCHABLE_HANDLE", VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT) |
Mike Stroyan | 31c50c8 | 2016-01-29 15:09:04 -0700 | [diff] [blame] | 259 | #endif // DISTINCT_NONDISPATCHABLE_HANDLES |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 260 | {}; |
Cody Northrop | 55443ef | 2015-09-28 15:09:32 -0600 | [diff] [blame] | 261 | }; |
Mike Stroyan | 313f7e6 | 2015-08-10 16:42:53 -0600 | [diff] [blame] | 262 | |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 263 | #define WRAPPER(type) \ |
| 264 | static void startWriteObject(struct layer_data *my_data, type object) { \ |
| 265 | my_data->c_##type.startWrite(my_data->report_data, object); \ |
| 266 | } \ |
| 267 | static void finishWriteObject(struct layer_data *my_data, type object) { my_data->c_##type.finishWrite(object); } \ |
| 268 | static void startReadObject(struct layer_data *my_data, type object) { \ |
| 269 | my_data->c_##type.startRead(my_data->report_data, object); \ |
| 270 | } \ |
| 271 | static void finishReadObject(struct layer_data *my_data, type object) { my_data->c_##type.finishRead(object); } |
Mike Stroyan | 313f7e6 | 2015-08-10 16:42:53 -0600 | [diff] [blame] | 272 | |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 273 | WRAPPER(VkDevice) |
| 274 | WRAPPER(VkInstance) |
| 275 | WRAPPER(VkQueue) |
Mike Stroyan | 31c50c8 | 2016-01-29 15:09:04 -0700 | [diff] [blame] | 276 | #ifdef DISTINCT_NONDISPATCHABLE_HANDLES |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 277 | WRAPPER(VkBuffer) |
| 278 | WRAPPER(VkBufferView) |
| 279 | WRAPPER(VkCommandPool) |
| 280 | WRAPPER(VkDescriptorPool) |
| 281 | WRAPPER(VkDescriptorSet) |
| 282 | WRAPPER(VkDescriptorSetLayout) |
| 283 | WRAPPER(VkDeviceMemory) |
| 284 | WRAPPER(VkEvent) |
| 285 | WRAPPER(VkFence) |
| 286 | WRAPPER(VkFramebuffer) |
| 287 | WRAPPER(VkImage) |
| 288 | WRAPPER(VkImageView) |
| 289 | WRAPPER(VkPipeline) |
| 290 | WRAPPER(VkPipelineCache) |
| 291 | WRAPPER(VkPipelineLayout) |
| 292 | WRAPPER(VkQueryPool) |
| 293 | WRAPPER(VkRenderPass) |
| 294 | WRAPPER(VkSampler) |
| 295 | WRAPPER(VkSemaphore) |
| 296 | WRAPPER(VkShaderModule) |
| 297 | WRAPPER(VkDebugReportCallbackEXT) |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 298 | #else // DISTINCT_NONDISPATCHABLE_HANDLES |
Mike Stroyan | 31c50c8 | 2016-01-29 15:09:04 -0700 | [diff] [blame] | 299 | WRAPPER(uint64_t) |
| 300 | #endif // DISTINCT_NONDISPATCHABLE_HANDLES |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 301 | |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 302 | static std::unordered_map<void *, layer_data *> layer_data_map; |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 303 | static std::unordered_map<VkCommandBuffer, VkCommandPool> command_pool_map; |
| 304 | |
| 305 | // VkCommandBuffer needs check for implicit use of command pool |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 306 | static void startWriteObject(struct layer_data *my_data, VkCommandBuffer object, bool lockPool = true) { |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 307 | if (lockPool) { |
Jeremy Hayes | b350beb | 2016-04-12 13:48:52 -0600 | [diff] [blame] | 308 | std::unique_lock<std::mutex> lock(global_lock); |
Mike Stroyan | ae8e8a7 | 2016-02-08 10:27:55 -0700 | [diff] [blame] | 309 | VkCommandPool pool = command_pool_map[object]; |
Jeremy Hayes | b350beb | 2016-04-12 13:48:52 -0600 | [diff] [blame] | 310 | lock.unlock(); |
Mike Stroyan | ae8e8a7 | 2016-02-08 10:27:55 -0700 | [diff] [blame] | 311 | startWriteObject(my_data, pool); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 312 | } |
| 313 | my_data->c_VkCommandBuffer.startWrite(my_data->report_data, object); |
| 314 | } |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 315 | static void finishWriteObject(struct layer_data *my_data, VkCommandBuffer object, bool lockPool = true) { |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 316 | my_data->c_VkCommandBuffer.finishWrite(object); |
| 317 | if (lockPool) { |
Jeremy Hayes | b350beb | 2016-04-12 13:48:52 -0600 | [diff] [blame] | 318 | std::unique_lock<std::mutex> lock(global_lock); |
Mike Stroyan | ae8e8a7 | 2016-02-08 10:27:55 -0700 | [diff] [blame] | 319 | VkCommandPool pool = command_pool_map[object]; |
Jeremy Hayes | b350beb | 2016-04-12 13:48:52 -0600 | [diff] [blame] | 320 | lock.unlock(); |
Mike Stroyan | ae8e8a7 | 2016-02-08 10:27:55 -0700 | [diff] [blame] | 321 | finishWriteObject(my_data, pool); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 322 | } |
| 323 | } |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 324 | static void startReadObject(struct layer_data *my_data, VkCommandBuffer object) { |
Jeremy Hayes | b350beb | 2016-04-12 13:48:52 -0600 | [diff] [blame] | 325 | std::unique_lock<std::mutex> lock(global_lock); |
Mike Stroyan | ae8e8a7 | 2016-02-08 10:27:55 -0700 | [diff] [blame] | 326 | VkCommandPool pool = command_pool_map[object]; |
Jeremy Hayes | b350beb | 2016-04-12 13:48:52 -0600 | [diff] [blame] | 327 | lock.unlock(); |
Mike Stroyan | ae8e8a7 | 2016-02-08 10:27:55 -0700 | [diff] [blame] | 328 | startReadObject(my_data, pool); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 329 | my_data->c_VkCommandBuffer.startRead(my_data->report_data, object); |
| 330 | } |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 331 | static void finishReadObject(struct layer_data *my_data, VkCommandBuffer object) { |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 332 | my_data->c_VkCommandBuffer.finishRead(object); |
Jeremy Hayes | b350beb | 2016-04-12 13:48:52 -0600 | [diff] [blame] | 333 | std::unique_lock<std::mutex> lock(global_lock); |
Mike Stroyan | ae8e8a7 | 2016-02-08 10:27:55 -0700 | [diff] [blame] | 334 | VkCommandPool pool = command_pool_map[object]; |
Jeremy Hayes | b350beb | 2016-04-12 13:48:52 -0600 | [diff] [blame] | 335 | lock.unlock(); |
Mike Stroyan | ae8e8a7 | 2016-02-08 10:27:55 -0700 | [diff] [blame] | 336 | finishReadObject(my_data, pool); |
Mike Stroyan | 313f7e6 | 2015-08-10 16:42:53 -0600 | [diff] [blame] | 337 | } |
| 338 | #endif // THREADING_H |