blob: 481acd4b620781fbe710d07b04514abc059a7775 [file] [log] [blame]
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -07001/* Copyright (c) 2015-2016 The Khronos Group Inc.
2 * Copyright (c) 2015-2016 Valve Corporation
3 * Copyright (c) 2015-2016 LunarG, Inc.
Mike Stroyan3712d5c2015-04-02 11:59:05 -06004 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06005 * 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 Stroyan3712d5c2015-04-02 11:59:05 -06008 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06009 * http://www.apache.org/licenses/LICENSE-2.0
Mike Stroyan3712d5c2015-04-02 11:59:05 -060010 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060011 * 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 Goeltzenleuchter05559522015-10-30 11:14:30 -060016 *
17 * Author: Cody Northrop <cody@lunarg.com>
18 * Author: Mike Stroyan <mike@LunarG.com>
Mike Stroyan3712d5c2015-04-02 11:59:05 -060019 */
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -070020
Mike Stroyan313f7e62015-08-10 16:42:53 -060021#ifndef THREADING_H
22#define THREADING_H
Jeremy Hayesb350beb2016-04-12 13:48:52 -060023#include <condition_variable>
24#include <mutex>
Mike Stroyan845bdc42015-11-02 15:30:20 -070025#include <vector>
Mike Stroyan313f7e62015-08-10 16:42:53 -060026#include "vk_layer_config.h"
27#include "vk_layer_logging.h"
Mike Stroyan3712d5c2015-04-02 11:59:05 -060028
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070029#if defined(__LP64__) || defined(_WIN64) || defined(__x86_64__) || defined(_M_X64) || defined(__ia64) || defined(_M_IA64) || \
Jon Ashburn5484e0c2016-03-08 17:48:44 -070030 defined(__aarch64__) || defined(__powerpc64__)
Mike Stroyan31c50c82016-01-29 15:09:04 -070031// 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 Stroyan3712d5c2015-04-02 11:59:05 -060036// Draw State ERROR codes
Mark Lobodzinski14817552016-05-19 17:08:10 -060037enum THREADING_CHECKER_ERROR {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070038 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 Lobodzinski14817552016-05-19 17:08:10 -060041};
Mike Stroyan3712d5c2015-04-02 11:59:05 -060042
Mike Stroyan845bdc42015-11-02 15:30:20 -070043struct object_use_data {
44 loader_platform_thread_id thread;
45 int reader_count;
46 int writer_count;
47};
48
49struct layer_data;
Mike Stroyan845bdc42015-11-02 15:30:20 -070050
Mike Stroyan0b64aee2016-07-13 10:10:25 -060051namespace threading {
52volatile bool vulkan_in_use = false;
53volatile bool vulkan_multi_threaded = false;
54// starting check if an application is using vulkan from multiple threads.
55inline 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.
68inline void finishMultiThread() { vulkan_in_use = false; }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070069} // namespace threading
Mike Stroyan0b64aee2016-07-13 10:10:25 -060070
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070071template <typename T>
72class counter {
73 public:
Mike Stroyan845bdc42015-11-02 15:30:20 -070074 const char *typeName;
75 VkDebugReportObjectTypeEXT objectType;
Mike Stroyan1a080012016-01-29 15:33:21 -070076 std::unordered_map<T, object_use_data> uses;
Mike Stroyanb3dd7902016-06-30 13:21:37 -060077 std::mutex counter_lock;
78 std::condition_variable counter_condition;
Jon Ashburn5484e0c2016-03-08 17:48:44 -070079 void startWrite(debug_report_data *report_data, T object) {
Dustin Graves080069b2016-04-05 13:48:15 -060080 bool skipCall = false;
Mike Stroyan845bdc42015-11-02 15:30:20 -070081 loader_platform_thread_id tid = loader_platform_get_thread_id();
Mike Stroyanb3dd7902016-06-30 13:21:37 -060082 std::unique_lock<std::mutex> lock(counter_lock);
Mike Stroyan845bdc42015-11-02 15:30:20 -070083 if (uses.find(object) == uses.end()) {
84 // There is no current use of the object. Record writer thread.
85 struct object_use_data *use_data = &uses[object];
86 use_data->reader_count = 0;
87 use_data->writer_count = 1;
88 use_data->thread = tid;
89 } else {
90 struct object_use_data *use_data = &uses[object];
91 if (use_data->reader_count == 0) {
92 // There are no readers. Two writers just collided.
93 if (use_data->thread != tid) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -070094 skipCall |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, objectType, (uint64_t)(object), 0,
95 THREADING_CHECKER_MULTIPLE_THREADS, "THREADING",
Jon Ashburn5484e0c2016-03-08 17:48:44 -070096 "THREADING ERROR : object of type %s is simultaneously used in thread %ld and thread %ld",
97 typeName, use_data->thread, tid);
Mike Stroyan845bdc42015-11-02 15:30:20 -070098 if (skipCall) {
99 // Wait for thread-safe access to object instead of skipping call.
100 while (uses.find(object) != uses.end()) {
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600101 counter_condition.wait(lock);
Mike Stroyan845bdc42015-11-02 15:30:20 -0700102 }
103 // There is now no current use of the object. Record writer thread.
104 struct object_use_data *use_data = &uses[object];
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700105 use_data->thread = tid;
Mike Stroyan845bdc42015-11-02 15:30:20 -0700106 use_data->reader_count = 0;
107 use_data->writer_count = 1;
108 } else {
109 // Continue with an unsafe use of the object.
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700110 use_data->thread = tid;
Mike Stroyan845bdc42015-11-02 15:30:20 -0700111 use_data->writer_count += 1;
112 }
113 } else {
Mike Stroyanc8774502016-02-05 09:11:32 -0700114 // This is either safe multiple use in one call, or recursive use.
Mike Stroyan845bdc42015-11-02 15:30:20 -0700115 // There is no way to make recursion safe. Just forge ahead.
116 use_data->writer_count += 1;
117 }
118 } else {
119 // There are readers. This writer collided with them.
120 if (use_data->thread != tid) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700121 skipCall |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, objectType, (uint64_t)(object), 0,
122 THREADING_CHECKER_MULTIPLE_THREADS, "THREADING",
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700123 "THREADING ERROR : object of type %s is simultaneously used in thread %ld and thread %ld",
124 typeName, use_data->thread, tid);
Mike Stroyan845bdc42015-11-02 15:30:20 -0700125 if (skipCall) {
126 // Wait for thread-safe access to object instead of skipping call.
127 while (uses.find(object) != uses.end()) {
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600128 counter_condition.wait(lock);
Mike Stroyan845bdc42015-11-02 15:30:20 -0700129 }
130 // There is now no current use of the object. Record writer thread.
131 struct object_use_data *use_data = &uses[object];
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700132 use_data->thread = tid;
Mike Stroyan845bdc42015-11-02 15:30:20 -0700133 use_data->reader_count = 0;
134 use_data->writer_count = 1;
135 } else {
136 // Continue with an unsafe use of the object.
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700137 use_data->thread = tid;
Mike Stroyan845bdc42015-11-02 15:30:20 -0700138 use_data->writer_count += 1;
139 }
140 } else {
Mike Stroyanc8774502016-02-05 09:11:32 -0700141 // This is either safe multiple use in one call, or recursive use.
Mike Stroyan845bdc42015-11-02 15:30:20 -0700142 // There is no way to make recursion safe. Just forge ahead.
143 use_data->writer_count += 1;
144 }
145 }
146 }
Mike Stroyan845bdc42015-11-02 15:30:20 -0700147 }
148
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700149 void finishWrite(T object) {
Mike Stroyan845bdc42015-11-02 15:30:20 -0700150 // Object is no longer in use
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600151 std::unique_lock<std::mutex> lock(counter_lock);
Mike Stroyan845bdc42015-11-02 15:30:20 -0700152 uses[object].writer_count -= 1;
153 if ((uses[object].reader_count == 0) && (uses[object].writer_count == 0)) {
154 uses.erase(object);
155 }
156 // Notify any waiting threads that this object may be safe to use
Jeremy Hayesb350beb2016-04-12 13:48:52 -0600157 lock.unlock();
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600158 counter_condition.notify_all();
Mike Stroyan845bdc42015-11-02 15:30:20 -0700159 }
160
161 void startRead(debug_report_data *report_data, T object) {
Dustin Graves080069b2016-04-05 13:48:15 -0600162 bool skipCall = false;
Mike Stroyan845bdc42015-11-02 15:30:20 -0700163 loader_platform_thread_id tid = loader_platform_get_thread_id();
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600164 std::unique_lock<std::mutex> lock(counter_lock);
Mike Stroyan845bdc42015-11-02 15:30:20 -0700165 if (uses.find(object) == uses.end()) {
166 // There is no current use of the object. Record reader count
167 struct object_use_data *use_data = &uses[object];
168 use_data->reader_count = 1;
169 use_data->writer_count = 0;
170 use_data->thread = tid;
Mike Stroyanc8774502016-02-05 09:11:32 -0700171 } else if (uses[object].writer_count > 0 && uses[object].thread != tid) {
Mike Stroyan845bdc42015-11-02 15:30:20 -0700172 // There is a writer of the object.
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700173 skipCall |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, objectType, (uint64_t)(object), 0,
174 THREADING_CHECKER_MULTIPLE_THREADS, "THREADING",
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700175 "THREADING ERROR : object of type %s is simultaneously used in thread %ld and thread %ld", typeName,
176 uses[object].thread, tid);
Mike Stroyan845bdc42015-11-02 15:30:20 -0700177 if (skipCall) {
178 // Wait for thread-safe access to object instead of skipping call.
179 while (uses.find(object) != uses.end()) {
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600180 counter_condition.wait(lock);
Mike Stroyan845bdc42015-11-02 15:30:20 -0700181 }
182 // There is no current use of the object. Record reader count
183 struct object_use_data *use_data = &uses[object];
184 use_data->reader_count = 1;
185 use_data->writer_count = 0;
186 use_data->thread = tid;
187 } else {
188 uses[object].reader_count += 1;
189 }
190 } else {
191 // There are other readers of the object. Increase reader count
192 uses[object].reader_count += 1;
193 }
Mike Stroyan845bdc42015-11-02 15:30:20 -0700194 }
195 void finishRead(T object) {
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600196 std::unique_lock<std::mutex> lock(counter_lock);
Mike Stroyan845bdc42015-11-02 15:30:20 -0700197 uses[object].reader_count -= 1;
198 if ((uses[object].reader_count == 0) && (uses[object].writer_count == 0)) {
199 uses.erase(object);
200 }
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600201 // Notify any waiting threads that this object may be safe to use
Jeremy Hayesb350beb2016-04-12 13:48:52 -0600202 lock.unlock();
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600203 counter_condition.notify_all();
Mike Stroyan845bdc42015-11-02 15:30:20 -0700204 }
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700205 counter(const char *name = "", VkDebugReportObjectTypeEXT type = VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT) {
Mike Stroyan845bdc42015-11-02 15:30:20 -0700206 typeName = name;
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700207 objectType = type;
Mike Stroyan845bdc42015-11-02 15:30:20 -0700208 }
209};
210
Cody Northrop55443ef2015-09-28 15:09:32 -0600211struct layer_data {
Chia-I Wu59d0a332016-05-16 11:21:03 +0800212 VkInstance instance;
213
Mike Stroyan313f7e62015-08-10 16:42:53 -0600214 debug_report_data *report_data;
Mike Stroyan845bdc42015-11-02 15:30:20 -0700215 std::vector<VkDebugReportCallbackEXT> logging_callback;
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700216 VkLayerDispatchTable *device_dispatch_table;
217 VkLayerInstanceDispatchTable *instance_dispatch_table;
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600218 // The following are for keeping track of the temporary callbacks that can
219 // be used in vkCreateInstance and vkDestroyInstance:
220 uint32_t num_tmp_callbacks;
221 VkDebugReportCallbackCreateInfoEXT *tmp_dbg_create_infos;
222 VkDebugReportCallbackEXT *tmp_callbacks;
Mike Stroyan845bdc42015-11-02 15:30:20 -0700223 counter<VkCommandBuffer> c_VkCommandBuffer;
224 counter<VkDevice> c_VkDevice;
225 counter<VkInstance> c_VkInstance;
226 counter<VkQueue> c_VkQueue;
Mike Stroyan31c50c82016-01-29 15:09:04 -0700227#ifdef DISTINCT_NONDISPATCHABLE_HANDLES
Mike Stroyan845bdc42015-11-02 15:30:20 -0700228 counter<VkBuffer> c_VkBuffer;
229 counter<VkBufferView> c_VkBufferView;
230 counter<VkCommandPool> c_VkCommandPool;
231 counter<VkDescriptorPool> c_VkDescriptorPool;
232 counter<VkDescriptorSet> c_VkDescriptorSet;
233 counter<VkDescriptorSetLayout> c_VkDescriptorSetLayout;
234 counter<VkDeviceMemory> c_VkDeviceMemory;
235 counter<VkEvent> c_VkEvent;
236 counter<VkFence> c_VkFence;
237 counter<VkFramebuffer> c_VkFramebuffer;
238 counter<VkImage> c_VkImage;
239 counter<VkImageView> c_VkImageView;
240 counter<VkPipeline> c_VkPipeline;
241 counter<VkPipelineCache> c_VkPipelineCache;
242 counter<VkPipelineLayout> c_VkPipelineLayout;
243 counter<VkQueryPool> c_VkQueryPool;
244 counter<VkRenderPass> c_VkRenderPass;
245 counter<VkSampler> c_VkSampler;
246 counter<VkSemaphore> c_VkSemaphore;
247 counter<VkShaderModule> c_VkShaderModule;
248 counter<VkDebugReportCallbackEXT> c_VkDebugReportCallbackEXT;
Mark Lobodzinski2d589822016-12-12 09:44:34 -0700249 counter<VkObjectTableNVX> c_VkObjectTableNVX;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700250 counter<VkIndirectCommandsLayoutNVX> c_VkIndirectCommandsLayoutNVX;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700251#else // DISTINCT_NONDISPATCHABLE_HANDLES
Mike Stroyan31c50c82016-01-29 15:09:04 -0700252 counter<uint64_t> c_uint64_t;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700253#endif // DISTINCT_NONDISPATCHABLE_HANDLES
Mark Lobodzinski2d589822016-12-12 09:44:34 -0700254
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700255 layer_data()
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700256 : report_data(nullptr),
257 num_tmp_callbacks(0),
258 tmp_dbg_create_infos(nullptr),
259 tmp_callbacks(nullptr),
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600260 c_VkCommandBuffer("VkCommandBuffer", VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT),
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700261 c_VkDevice("VkDevice", VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT),
262 c_VkInstance("VkInstance", VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT),
263 c_VkQueue("VkQueue", VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT),
Mike Stroyan31c50c82016-01-29 15:09:04 -0700264#ifdef DISTINCT_NONDISPATCHABLE_HANDLES
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700265 c_VkBuffer("VkBuffer", VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT),
266 c_VkBufferView("VkBufferView", VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_VIEW_EXT),
267 c_VkCommandPool("VkCommandPool", VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_POOL_EXT),
268 c_VkDescriptorPool("VkDescriptorPool", VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT),
269 c_VkDescriptorSet("VkDescriptorSet", VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT),
270 c_VkDescriptorSetLayout("VkDescriptorSetLayout", VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT),
271 c_VkDeviceMemory("VkDeviceMemory", VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT),
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700272 c_VkEvent("VkEvent", VK_DEBUG_REPORT_OBJECT_TYPE_EVENT_EXT),
273 c_VkFence("VkFence", VK_DEBUG_REPORT_OBJECT_TYPE_FENCE_EXT),
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700274 c_VkFramebuffer("VkFramebuffer", VK_DEBUG_REPORT_OBJECT_TYPE_FRAMEBUFFER_EXT),
275 c_VkImage("VkImage", VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT),
276 c_VkImageView("VkImageView", VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT),
277 c_VkPipeline("VkPipeline", VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT),
278 c_VkPipelineCache("VkPipelineCache", VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_CACHE_EXT),
279 c_VkPipelineLayout("VkPipelineLayout", VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_LAYOUT_EXT),
280 c_VkQueryPool("VkQueryPool", VK_DEBUG_REPORT_OBJECT_TYPE_QUERY_POOL_EXT),
281 c_VkRenderPass("VkRenderPass", VK_DEBUG_REPORT_OBJECT_TYPE_RENDER_PASS_EXT),
282 c_VkSampler("VkSampler", VK_DEBUG_REPORT_OBJECT_TYPE_SAMPLER_EXT),
283 c_VkSemaphore("VkSemaphore", VK_DEBUG_REPORT_OBJECT_TYPE_SEMAPHORE_EXT),
284 c_VkShaderModule("VkShaderModule", VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT),
Mark Lobodzinski2d589822016-12-12 09:44:34 -0700285 c_VkDebugReportCallbackEXT("VkDebugReportCallbackEXT", VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT),
286 c_VkObjectTableNVX("VkObjectTableNVX", VK_DEBUG_REPORT_OBJECT_TYPE_OBJECT_TABLE_NVX_EXT),
287 c_VkIndirectCommandsLayoutNVX("VkIndirectCommandsLayoutNVX", VK_DEBUG_REPORT_OBJECT_TYPE_INDIRECT_COMMANDS_LAYOUT_NVX_EXT)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700288#else // DISTINCT_NONDISPATCHABLE_HANDLES
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700289 c_uint64_t("NON_DISPATCHABLE_HANDLE", VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700290#endif // DISTINCT_NONDISPATCHABLE_HANDLES
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700291 {};
Cody Northrop55443ef2015-09-28 15:09:32 -0600292};
Mike Stroyan313f7e62015-08-10 16:42:53 -0600293
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700294#define WRAPPER(type) \
295 static void startWriteObject(struct layer_data *my_data, type object) { \
296 my_data->c_##type.startWrite(my_data->report_data, object); \
297 } \
298 static void finishWriteObject(struct layer_data *my_data, type object) { my_data->c_##type.finishWrite(object); } \
299 static void startReadObject(struct layer_data *my_data, type object) { \
300 my_data->c_##type.startRead(my_data->report_data, object); \
301 } \
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700302 static void finishReadObject(struct layer_data *my_data, type object) { my_data->c_##type.finishRead(object); }
Mike Stroyan313f7e62015-08-10 16:42:53 -0600303
Mike Stroyan845bdc42015-11-02 15:30:20 -0700304WRAPPER(VkDevice)
305WRAPPER(VkInstance)
306WRAPPER(VkQueue)
Mike Stroyan31c50c82016-01-29 15:09:04 -0700307#ifdef DISTINCT_NONDISPATCHABLE_HANDLES
Mike Stroyan845bdc42015-11-02 15:30:20 -0700308WRAPPER(VkBuffer)
309WRAPPER(VkBufferView)
310WRAPPER(VkCommandPool)
311WRAPPER(VkDescriptorPool)
312WRAPPER(VkDescriptorSet)
313WRAPPER(VkDescriptorSetLayout)
314WRAPPER(VkDeviceMemory)
315WRAPPER(VkEvent)
316WRAPPER(VkFence)
317WRAPPER(VkFramebuffer)
318WRAPPER(VkImage)
319WRAPPER(VkImageView)
320WRAPPER(VkPipeline)
321WRAPPER(VkPipelineCache)
322WRAPPER(VkPipelineLayout)
323WRAPPER(VkQueryPool)
324WRAPPER(VkRenderPass)
325WRAPPER(VkSampler)
326WRAPPER(VkSemaphore)
327WRAPPER(VkShaderModule)
328WRAPPER(VkDebugReportCallbackEXT)
Mark Lobodzinski2d589822016-12-12 09:44:34 -0700329WRAPPER(VkObjectTableNVX)
330WRAPPER(VkIndirectCommandsLayoutNVX)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700331#else // DISTINCT_NONDISPATCHABLE_HANDLES
Mike Stroyan31c50c82016-01-29 15:09:04 -0700332WRAPPER(uint64_t)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700333#endif // DISTINCT_NONDISPATCHABLE_HANDLES
Mike Stroyan845bdc42015-11-02 15:30:20 -0700334
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700335static std::unordered_map<void *, layer_data *> layer_data_map;
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600336static std::mutex command_pool_lock;
Mike Stroyan845bdc42015-11-02 15:30:20 -0700337static std::unordered_map<VkCommandBuffer, VkCommandPool> command_pool_map;
338
339// VkCommandBuffer needs check for implicit use of command pool
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700340static void startWriteObject(struct layer_data *my_data, VkCommandBuffer object, bool lockPool = true) {
Mike Stroyan845bdc42015-11-02 15:30:20 -0700341 if (lockPool) {
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600342 std::unique_lock<std::mutex> lock(command_pool_lock);
Mike Stroyanae8e8a72016-02-08 10:27:55 -0700343 VkCommandPool pool = command_pool_map[object];
Jeremy Hayesb350beb2016-04-12 13:48:52 -0600344 lock.unlock();
Mike Stroyanae8e8a72016-02-08 10:27:55 -0700345 startWriteObject(my_data, pool);
Mike Stroyan845bdc42015-11-02 15:30:20 -0700346 }
347 my_data->c_VkCommandBuffer.startWrite(my_data->report_data, object);
348}
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700349static void finishWriteObject(struct layer_data *my_data, VkCommandBuffer object, bool lockPool = true) {
Mike Stroyan845bdc42015-11-02 15:30:20 -0700350 my_data->c_VkCommandBuffer.finishWrite(object);
351 if (lockPool) {
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600352 std::unique_lock<std::mutex> lock(command_pool_lock);
Mike Stroyanae8e8a72016-02-08 10:27:55 -0700353 VkCommandPool pool = command_pool_map[object];
Jeremy Hayesb350beb2016-04-12 13:48:52 -0600354 lock.unlock();
Mike Stroyanae8e8a72016-02-08 10:27:55 -0700355 finishWriteObject(my_data, pool);
Mike Stroyan845bdc42015-11-02 15:30:20 -0700356 }
357}
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700358static void startReadObject(struct layer_data *my_data, VkCommandBuffer object) {
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600359 std::unique_lock<std::mutex> lock(command_pool_lock);
Mike Stroyanae8e8a72016-02-08 10:27:55 -0700360 VkCommandPool pool = command_pool_map[object];
Jeremy Hayesb350beb2016-04-12 13:48:52 -0600361 lock.unlock();
Mike Stroyanae8e8a72016-02-08 10:27:55 -0700362 startReadObject(my_data, pool);
Mike Stroyan845bdc42015-11-02 15:30:20 -0700363 my_data->c_VkCommandBuffer.startRead(my_data->report_data, object);
364}
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700365static void finishReadObject(struct layer_data *my_data, VkCommandBuffer object) {
Mike Stroyan845bdc42015-11-02 15:30:20 -0700366 my_data->c_VkCommandBuffer.finishRead(object);
Mike Stroyanb3dd7902016-06-30 13:21:37 -0600367 std::unique_lock<std::mutex> lock(command_pool_lock);
Mike Stroyanae8e8a72016-02-08 10:27:55 -0700368 VkCommandPool pool = command_pool_map[object];
Jeremy Hayesb350beb2016-04-12 13:48:52 -0600369 lock.unlock();
Mike Stroyanae8e8a72016-02-08 10:27:55 -0700370 finishReadObject(my_data, pool);
Mike Stroyan313f7e62015-08-10 16:42:53 -0600371}
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700372#endif // THREADING_H