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 |
Mark Lobodzinski | 1481755 | 2016-05-19 17:08:10 -0600 | [diff] [blame] | 37 | enum THREADING_CHECKER_ERROR { |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 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 |
Mark Lobodzinski | 1481755 | 2016-05-19 17:08:10 -0600 | [diff] [blame] | 41 | }; |
Mike Stroyan | 3712d5c | 2015-04-02 11:59:05 -0600 | [diff] [blame] | 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 | |
Mike Stroyan | 0b64aee | 2016-07-13 10:10:25 -0600 | [diff] [blame] | 51 | namespace threading { |
| 52 | volatile bool vulkan_in_use = false; |
| 53 | volatile bool vulkan_multi_threaded = false; |
| 54 | // starting check if an application is using vulkan from multiple threads. |
| 55 | inline bool startMultiThread() { |
| 56 | if (vulkan_multi_threaded) { |
| 57 | return true; |
| 58 | } |
| 59 | if (vulkan_in_use) { |
| 60 | vulkan_multi_threaded = true; |
| 61 | return true; |
| 62 | } |
| 63 | vulkan_in_use = true; |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | // finishing check if an application is using vulkan from multiple threads. |
| 68 | inline void finishMultiThread() { vulkan_in_use = false; } |
| 69 | } // namespace threading |
| 70 | |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 71 | template <typename T> class counter { |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 72 | public: |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 73 | const char *typeName; |
| 74 | VkDebugReportObjectTypeEXT objectType; |
Mike Stroyan | 1a08001 | 2016-01-29 15:33:21 -0700 | [diff] [blame] | 75 | std::unordered_map<T, object_use_data> uses; |
Mike Stroyan | b3dd790 | 2016-06-30 13:21:37 -0600 | [diff] [blame] | 76 | std::mutex counter_lock; |
| 77 | std::condition_variable counter_condition; |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 78 | void startWrite(debug_report_data *report_data, T object) { |
Dustin Graves | 080069b | 2016-04-05 13:48:15 -0600 | [diff] [blame] | 79 | bool skipCall = false; |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 80 | loader_platform_thread_id tid = loader_platform_get_thread_id(); |
Mike Stroyan | b3dd790 | 2016-06-30 13:21:37 -0600 | [diff] [blame] | 81 | std::unique_lock<std::mutex> lock(counter_lock); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 82 | if (uses.find(object) == uses.end()) { |
| 83 | // There is no current use of the object. Record writer thread. |
| 84 | struct object_use_data *use_data = &uses[object]; |
| 85 | use_data->reader_count = 0; |
| 86 | use_data->writer_count = 1; |
| 87 | use_data->thread = tid; |
| 88 | } else { |
| 89 | struct object_use_data *use_data = &uses[object]; |
| 90 | if (use_data->reader_count == 0) { |
| 91 | // There are no readers. Two writers just collided. |
| 92 | if (use_data->thread != tid) { |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame^] | 93 | skipCall |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, objectType, (uint64_t)(object), 0, |
| 94 | THREADING_CHECKER_MULTIPLE_THREADS, "THREADING", |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 95 | "THREADING ERROR : object of type %s is simultaneously used in thread %ld and thread %ld", |
| 96 | typeName, use_data->thread, tid); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 97 | if (skipCall) { |
| 98 | // Wait for thread-safe access to object instead of skipping call. |
| 99 | while (uses.find(object) != uses.end()) { |
Mike Stroyan | b3dd790 | 2016-06-30 13:21:37 -0600 | [diff] [blame] | 100 | counter_condition.wait(lock); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 101 | } |
| 102 | // There is now no current use of the object. Record writer thread. |
| 103 | struct object_use_data *use_data = &uses[object]; |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 104 | use_data->thread = tid; |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 105 | use_data->reader_count = 0; |
| 106 | use_data->writer_count = 1; |
| 107 | } else { |
| 108 | // Continue with an unsafe use of the object. |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 109 | use_data->thread = tid; |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 110 | use_data->writer_count += 1; |
| 111 | } |
| 112 | } else { |
Mike Stroyan | c877450 | 2016-02-05 09:11:32 -0700 | [diff] [blame] | 113 | // This is either safe multiple use in one call, or recursive use. |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 114 | // There is no way to make recursion safe. Just forge ahead. |
| 115 | use_data->writer_count += 1; |
| 116 | } |
| 117 | } else { |
| 118 | // There are readers. This writer collided with them. |
| 119 | if (use_data->thread != tid) { |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame^] | 120 | skipCall |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, objectType, (uint64_t)(object), 0, |
| 121 | THREADING_CHECKER_MULTIPLE_THREADS, "THREADING", |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 122 | "THREADING ERROR : object of type %s is simultaneously used in thread %ld and thread %ld", |
| 123 | typeName, use_data->thread, tid); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 124 | if (skipCall) { |
| 125 | // Wait for thread-safe access to object instead of skipping call. |
| 126 | while (uses.find(object) != uses.end()) { |
Mike Stroyan | b3dd790 | 2016-06-30 13:21:37 -0600 | [diff] [blame] | 127 | counter_condition.wait(lock); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 128 | } |
| 129 | // There is now no current use of the object. Record writer thread. |
| 130 | struct object_use_data *use_data = &uses[object]; |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 131 | use_data->thread = tid; |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 132 | use_data->reader_count = 0; |
| 133 | use_data->writer_count = 1; |
| 134 | } else { |
| 135 | // Continue with an unsafe use of the object. |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 136 | use_data->thread = tid; |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 137 | use_data->writer_count += 1; |
| 138 | } |
| 139 | } else { |
Mike Stroyan | c877450 | 2016-02-05 09:11:32 -0700 | [diff] [blame] | 140 | // This is either safe multiple use in one call, or recursive use. |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 141 | // There is no way to make recursion safe. Just forge ahead. |
| 142 | use_data->writer_count += 1; |
| 143 | } |
| 144 | } |
| 145 | } |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 148 | void finishWrite(T object) { |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 149 | // Object is no longer in use |
Mike Stroyan | b3dd790 | 2016-06-30 13:21:37 -0600 | [diff] [blame] | 150 | std::unique_lock<std::mutex> lock(counter_lock); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 151 | uses[object].writer_count -= 1; |
| 152 | if ((uses[object].reader_count == 0) && (uses[object].writer_count == 0)) { |
| 153 | uses.erase(object); |
| 154 | } |
| 155 | // Notify any waiting threads that this object may be safe to use |
Jeremy Hayes | b350beb | 2016-04-12 13:48:52 -0600 | [diff] [blame] | 156 | lock.unlock(); |
Mike Stroyan | b3dd790 | 2016-06-30 13:21:37 -0600 | [diff] [blame] | 157 | counter_condition.notify_all(); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void startRead(debug_report_data *report_data, T object) { |
Dustin Graves | 080069b | 2016-04-05 13:48:15 -0600 | [diff] [blame] | 161 | bool skipCall = false; |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 162 | loader_platform_thread_id tid = loader_platform_get_thread_id(); |
Mike Stroyan | b3dd790 | 2016-06-30 13:21:37 -0600 | [diff] [blame] | 163 | std::unique_lock<std::mutex> lock(counter_lock); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 164 | if (uses.find(object) == uses.end()) { |
| 165 | // There is no current use of the object. Record reader count |
| 166 | struct object_use_data *use_data = &uses[object]; |
| 167 | use_data->reader_count = 1; |
| 168 | use_data->writer_count = 0; |
| 169 | use_data->thread = tid; |
Mike Stroyan | c877450 | 2016-02-05 09:11:32 -0700 | [diff] [blame] | 170 | } else if (uses[object].writer_count > 0 && uses[object].thread != tid) { |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 171 | // There is a writer of the object. |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame^] | 172 | skipCall |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, objectType, (uint64_t)(object), 0, |
| 173 | THREADING_CHECKER_MULTIPLE_THREADS, "THREADING", |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 174 | "THREADING ERROR : object of type %s is simultaneously used in thread %ld and thread %ld", typeName, |
| 175 | uses[object].thread, tid); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 176 | if (skipCall) { |
| 177 | // Wait for thread-safe access to object instead of skipping call. |
| 178 | while (uses.find(object) != uses.end()) { |
Mike Stroyan | b3dd790 | 2016-06-30 13:21:37 -0600 | [diff] [blame] | 179 | counter_condition.wait(lock); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 180 | } |
| 181 | // There is no current use of the object. Record reader count |
| 182 | struct object_use_data *use_data = &uses[object]; |
| 183 | use_data->reader_count = 1; |
| 184 | use_data->writer_count = 0; |
| 185 | use_data->thread = tid; |
| 186 | } else { |
| 187 | uses[object].reader_count += 1; |
| 188 | } |
| 189 | } else { |
| 190 | // There are other readers of the object. Increase reader count |
| 191 | uses[object].reader_count += 1; |
| 192 | } |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 193 | } |
| 194 | void finishRead(T object) { |
Mike Stroyan | b3dd790 | 2016-06-30 13:21:37 -0600 | [diff] [blame] | 195 | std::unique_lock<std::mutex> lock(counter_lock); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 196 | uses[object].reader_count -= 1; |
| 197 | if ((uses[object].reader_count == 0) && (uses[object].writer_count == 0)) { |
| 198 | uses.erase(object); |
| 199 | } |
Mike Stroyan | b3dd790 | 2016-06-30 13:21:37 -0600 | [diff] [blame] | 200 | // Notify any waiting threads that this object may be safe to use |
Jeremy Hayes | b350beb | 2016-04-12 13:48:52 -0600 | [diff] [blame] | 201 | lock.unlock(); |
Mike Stroyan | b3dd790 | 2016-06-30 13:21:37 -0600 | [diff] [blame] | 202 | counter_condition.notify_all(); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 203 | } |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 204 | 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] | 205 | typeName = name; |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 206 | objectType = type; |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 207 | } |
| 208 | }; |
| 209 | |
Cody Northrop | 55443ef | 2015-09-28 15:09:32 -0600 | [diff] [blame] | 210 | struct layer_data { |
Chia-I Wu | 59d0a33 | 2016-05-16 11:21:03 +0800 | [diff] [blame] | 211 | VkInstance instance; |
| 212 | |
Mike Stroyan | 313f7e6 | 2015-08-10 16:42:53 -0600 | [diff] [blame] | 213 | debug_report_data *report_data; |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 214 | std::vector<VkDebugReportCallbackEXT> logging_callback; |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 215 | VkLayerDispatchTable *device_dispatch_table; |
| 216 | VkLayerInstanceDispatchTable *instance_dispatch_table; |
Ian Elliott | ed6b5ac | 2016-04-28 09:08:13 -0600 | [diff] [blame] | 217 | // The following are for keeping track of the temporary callbacks that can |
| 218 | // be used in vkCreateInstance and vkDestroyInstance: |
| 219 | uint32_t num_tmp_callbacks; |
| 220 | VkDebugReportCallbackCreateInfoEXT *tmp_dbg_create_infos; |
| 221 | VkDebugReportCallbackEXT *tmp_callbacks; |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 222 | counter<VkCommandBuffer> c_VkCommandBuffer; |
| 223 | counter<VkDevice> c_VkDevice; |
| 224 | counter<VkInstance> c_VkInstance; |
| 225 | counter<VkQueue> c_VkQueue; |
Mike Stroyan | 31c50c8 | 2016-01-29 15:09:04 -0700 | [diff] [blame] | 226 | #ifdef DISTINCT_NONDISPATCHABLE_HANDLES |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 227 | counter<VkBuffer> c_VkBuffer; |
| 228 | counter<VkBufferView> c_VkBufferView; |
| 229 | counter<VkCommandPool> c_VkCommandPool; |
| 230 | counter<VkDescriptorPool> c_VkDescriptorPool; |
| 231 | counter<VkDescriptorSet> c_VkDescriptorSet; |
| 232 | counter<VkDescriptorSetLayout> c_VkDescriptorSetLayout; |
| 233 | counter<VkDeviceMemory> c_VkDeviceMemory; |
| 234 | counter<VkEvent> c_VkEvent; |
| 235 | counter<VkFence> c_VkFence; |
| 236 | counter<VkFramebuffer> c_VkFramebuffer; |
| 237 | counter<VkImage> c_VkImage; |
| 238 | counter<VkImageView> c_VkImageView; |
| 239 | counter<VkPipeline> c_VkPipeline; |
| 240 | counter<VkPipelineCache> c_VkPipelineCache; |
| 241 | counter<VkPipelineLayout> c_VkPipelineLayout; |
| 242 | counter<VkQueryPool> c_VkQueryPool; |
| 243 | counter<VkRenderPass> c_VkRenderPass; |
| 244 | counter<VkSampler> c_VkSampler; |
| 245 | counter<VkSemaphore> c_VkSemaphore; |
| 246 | counter<VkShaderModule> c_VkShaderModule; |
| 247 | counter<VkDebugReportCallbackEXT> c_VkDebugReportCallbackEXT; |
Mark Lobodzinski | 2d58982 | 2016-12-12 09:44:34 -0700 | [diff] [blame] | 248 | counter<VkObjectTableNVX> c_VkObjectTableNVX; |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame^] | 249 | counter<VkIndirectCommandsLayoutNVX> c_VkIndirectCommandsLayoutNVX; |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 250 | #else // DISTINCT_NONDISPATCHABLE_HANDLES |
Mike Stroyan | 31c50c8 | 2016-01-29 15:09:04 -0700 | [diff] [blame] | 251 | counter<uint64_t> c_uint64_t; |
| 252 | #endif // DISTINCT_NONDISPATCHABLE_HANDLES |
Mark Lobodzinski | 2d58982 | 2016-12-12 09:44:34 -0700 | [diff] [blame] | 253 | |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 254 | layer_data() |
Ian Elliott | ed6b5ac | 2016-04-28 09:08:13 -0600 | [diff] [blame] | 255 | : report_data(nullptr), num_tmp_callbacks(0), tmp_dbg_create_infos(nullptr), tmp_callbacks(nullptr), |
| 256 | c_VkCommandBuffer("VkCommandBuffer", VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT), |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 257 | c_VkDevice("VkDevice", VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT), |
| 258 | c_VkInstance("VkInstance", VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT), |
| 259 | c_VkQueue("VkQueue", VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT), |
Mike Stroyan | 31c50c8 | 2016-01-29 15:09:04 -0700 | [diff] [blame] | 260 | #ifdef DISTINCT_NONDISPATCHABLE_HANDLES |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 261 | c_VkBuffer("VkBuffer", VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT), |
| 262 | c_VkBufferView("VkBufferView", VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_VIEW_EXT), |
| 263 | c_VkCommandPool("VkCommandPool", VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_POOL_EXT), |
| 264 | c_VkDescriptorPool("VkDescriptorPool", VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT), |
| 265 | c_VkDescriptorSet("VkDescriptorSet", VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT), |
| 266 | c_VkDescriptorSetLayout("VkDescriptorSetLayout", VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT), |
| 267 | c_VkDeviceMemory("VkDeviceMemory", VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT), |
| 268 | c_VkEvent("VkEvent", VK_DEBUG_REPORT_OBJECT_TYPE_EVENT_EXT), c_VkFence("VkFence", VK_DEBUG_REPORT_OBJECT_TYPE_FENCE_EXT), |
| 269 | c_VkFramebuffer("VkFramebuffer", VK_DEBUG_REPORT_OBJECT_TYPE_FRAMEBUFFER_EXT), |
| 270 | c_VkImage("VkImage", VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT), |
| 271 | c_VkImageView("VkImageView", VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT), |
| 272 | c_VkPipeline("VkPipeline", VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT), |
| 273 | c_VkPipelineCache("VkPipelineCache", VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_CACHE_EXT), |
| 274 | c_VkPipelineLayout("VkPipelineLayout", VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_LAYOUT_EXT), |
| 275 | c_VkQueryPool("VkQueryPool", VK_DEBUG_REPORT_OBJECT_TYPE_QUERY_POOL_EXT), |
| 276 | c_VkRenderPass("VkRenderPass", VK_DEBUG_REPORT_OBJECT_TYPE_RENDER_PASS_EXT), |
| 277 | c_VkSampler("VkSampler", VK_DEBUG_REPORT_OBJECT_TYPE_SAMPLER_EXT), |
| 278 | c_VkSemaphore("VkSemaphore", VK_DEBUG_REPORT_OBJECT_TYPE_SEMAPHORE_EXT), |
| 279 | c_VkShaderModule("VkShaderModule", VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT), |
Mark Lobodzinski | 2d58982 | 2016-12-12 09:44:34 -0700 | [diff] [blame] | 280 | c_VkDebugReportCallbackEXT("VkDebugReportCallbackEXT", VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT), |
| 281 | c_VkObjectTableNVX("VkObjectTableNVX", VK_DEBUG_REPORT_OBJECT_TYPE_OBJECT_TABLE_NVX_EXT), |
| 282 | c_VkIndirectCommandsLayoutNVX("VkIndirectCommandsLayoutNVX", VK_DEBUG_REPORT_OBJECT_TYPE_INDIRECT_COMMANDS_LAYOUT_NVX_EXT) |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 283 | #else // DISTINCT_NONDISPATCHABLE_HANDLES |
| 284 | 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] | 285 | #endif // DISTINCT_NONDISPATCHABLE_HANDLES |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 286 | {}; |
Cody Northrop | 55443ef | 2015-09-28 15:09:32 -0600 | [diff] [blame] | 287 | }; |
Mike Stroyan | 313f7e6 | 2015-08-10 16:42:53 -0600 | [diff] [blame] | 288 | |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 289 | #define WRAPPER(type) \ |
| 290 | static void startWriteObject(struct layer_data *my_data, type object) { \ |
| 291 | my_data->c_##type.startWrite(my_data->report_data, object); \ |
| 292 | } \ |
| 293 | static void finishWriteObject(struct layer_data *my_data, type object) { my_data->c_##type.finishWrite(object); } \ |
| 294 | static void startReadObject(struct layer_data *my_data, type object) { \ |
| 295 | my_data->c_##type.startRead(my_data->report_data, object); \ |
| 296 | } \ |
| 297 | 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] | 298 | |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 299 | WRAPPER(VkDevice) |
| 300 | WRAPPER(VkInstance) |
| 301 | WRAPPER(VkQueue) |
Mike Stroyan | 31c50c8 | 2016-01-29 15:09:04 -0700 | [diff] [blame] | 302 | #ifdef DISTINCT_NONDISPATCHABLE_HANDLES |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 303 | WRAPPER(VkBuffer) |
| 304 | WRAPPER(VkBufferView) |
| 305 | WRAPPER(VkCommandPool) |
| 306 | WRAPPER(VkDescriptorPool) |
| 307 | WRAPPER(VkDescriptorSet) |
| 308 | WRAPPER(VkDescriptorSetLayout) |
| 309 | WRAPPER(VkDeviceMemory) |
| 310 | WRAPPER(VkEvent) |
| 311 | WRAPPER(VkFence) |
| 312 | WRAPPER(VkFramebuffer) |
| 313 | WRAPPER(VkImage) |
| 314 | WRAPPER(VkImageView) |
| 315 | WRAPPER(VkPipeline) |
| 316 | WRAPPER(VkPipelineCache) |
| 317 | WRAPPER(VkPipelineLayout) |
| 318 | WRAPPER(VkQueryPool) |
| 319 | WRAPPER(VkRenderPass) |
| 320 | WRAPPER(VkSampler) |
| 321 | WRAPPER(VkSemaphore) |
| 322 | WRAPPER(VkShaderModule) |
| 323 | WRAPPER(VkDebugReportCallbackEXT) |
Mark Lobodzinski | 2d58982 | 2016-12-12 09:44:34 -0700 | [diff] [blame] | 324 | WRAPPER(VkObjectTableNVX) |
| 325 | WRAPPER(VkIndirectCommandsLayoutNVX) |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 326 | #else // DISTINCT_NONDISPATCHABLE_HANDLES |
Mike Stroyan | 31c50c8 | 2016-01-29 15:09:04 -0700 | [diff] [blame] | 327 | WRAPPER(uint64_t) |
| 328 | #endif // DISTINCT_NONDISPATCHABLE_HANDLES |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 329 | |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 330 | static std::unordered_map<void *, layer_data *> layer_data_map; |
Mike Stroyan | b3dd790 | 2016-06-30 13:21:37 -0600 | [diff] [blame] | 331 | static std::mutex command_pool_lock; |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 332 | static std::unordered_map<VkCommandBuffer, VkCommandPool> command_pool_map; |
| 333 | |
| 334 | // VkCommandBuffer needs check for implicit use of command pool |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 335 | 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] | 336 | if (lockPool) { |
Mike Stroyan | b3dd790 | 2016-06-30 13:21:37 -0600 | [diff] [blame] | 337 | std::unique_lock<std::mutex> lock(command_pool_lock); |
Mike Stroyan | ae8e8a7 | 2016-02-08 10:27:55 -0700 | [diff] [blame] | 338 | VkCommandPool pool = command_pool_map[object]; |
Jeremy Hayes | b350beb | 2016-04-12 13:48:52 -0600 | [diff] [blame] | 339 | lock.unlock(); |
Mike Stroyan | ae8e8a7 | 2016-02-08 10:27:55 -0700 | [diff] [blame] | 340 | startWriteObject(my_data, pool); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 341 | } |
| 342 | my_data->c_VkCommandBuffer.startWrite(my_data->report_data, object); |
| 343 | } |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 344 | 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] | 345 | my_data->c_VkCommandBuffer.finishWrite(object); |
| 346 | if (lockPool) { |
Mike Stroyan | b3dd790 | 2016-06-30 13:21:37 -0600 | [diff] [blame] | 347 | std::unique_lock<std::mutex> lock(command_pool_lock); |
Mike Stroyan | ae8e8a7 | 2016-02-08 10:27:55 -0700 | [diff] [blame] | 348 | VkCommandPool pool = command_pool_map[object]; |
Jeremy Hayes | b350beb | 2016-04-12 13:48:52 -0600 | [diff] [blame] | 349 | lock.unlock(); |
Mike Stroyan | ae8e8a7 | 2016-02-08 10:27:55 -0700 | [diff] [blame] | 350 | finishWriteObject(my_data, pool); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 351 | } |
| 352 | } |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 353 | static void startReadObject(struct layer_data *my_data, VkCommandBuffer object) { |
Mike Stroyan | b3dd790 | 2016-06-30 13:21:37 -0600 | [diff] [blame] | 354 | std::unique_lock<std::mutex> lock(command_pool_lock); |
Mike Stroyan | ae8e8a7 | 2016-02-08 10:27:55 -0700 | [diff] [blame] | 355 | VkCommandPool pool = command_pool_map[object]; |
Jeremy Hayes | b350beb | 2016-04-12 13:48:52 -0600 | [diff] [blame] | 356 | lock.unlock(); |
Mike Stroyan | ae8e8a7 | 2016-02-08 10:27:55 -0700 | [diff] [blame] | 357 | startReadObject(my_data, pool); |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 358 | my_data->c_VkCommandBuffer.startRead(my_data->report_data, object); |
| 359 | } |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 360 | static void finishReadObject(struct layer_data *my_data, VkCommandBuffer object) { |
Mike Stroyan | 845bdc4 | 2015-11-02 15:30:20 -0700 | [diff] [blame] | 361 | my_data->c_VkCommandBuffer.finishRead(object); |
Mike Stroyan | b3dd790 | 2016-06-30 13:21:37 -0600 | [diff] [blame] | 362 | std::unique_lock<std::mutex> lock(command_pool_lock); |
Mike Stroyan | ae8e8a7 | 2016-02-08 10:27:55 -0700 | [diff] [blame] | 363 | VkCommandPool pool = command_pool_map[object]; |
Jeremy Hayes | b350beb | 2016-04-12 13:48:52 -0600 | [diff] [blame] | 364 | lock.unlock(); |
Mike Stroyan | ae8e8a7 | 2016-02-08 10:27:55 -0700 | [diff] [blame] | 365 | finishReadObject(my_data, pool); |
Mike Stroyan | 313f7e6 | 2015-08-10 16:42:53 -0600 | [diff] [blame] | 366 | } |
| 367 | #endif // THREADING_H |