blob: 7430fa3995413017ec7f9087b2ecd59bd04d8ddd [file] [log] [blame]
Tobin Ehlisacab8882014-11-14 13:01:02 -07001/*
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002 * Vulkan
Tobin Ehlisacab8882014-11-14 13:01:02 -07003 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
Tobin Ehlis2d1d9702015-07-03 09:42:57 -060025#include "vk_layer.h"
Courtney Goeltzenleuchter8b0e68d2015-07-05 22:13:43 -060026#include "vk_layer_extension_utils.h"
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060027#include "vk_enum_string_helper.h"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -060028
Tobin Ehlis3c26a542014-11-18 11:28:33 -070029// Object Tracker ERROR codes
30typedef enum _OBJECT_TRACK_ERROR
31{
Chia-I Wub1466182015-01-05 14:33:42 +080032 OBJTRACK_NONE, // Used for INFO & other non-error messages
33 OBJTRACK_UNKNOWN_OBJECT, // Updating uses of object that's not in global object list
34 OBJTRACK_INTERNAL_ERROR, // Bug with data tracking within the layer
35 OBJTRACK_DESTROY_OBJECT_FAILED, // Couldn't find object to be destroyed
Mark Lobodzinski7a3d5ff2015-05-05 18:24:45 -050036 OBJTRACK_OBJECT_TYPE_MISMATCH, // Object did not match corresponding Object Type
Chia-I Wub1466182015-01-05 14:33:42 +080037 OBJTRACK_OBJECT_LEAK, // OBJECT was not correctly freed/destroyed
38 OBJTRACK_OBJCOUNT_MAX_EXCEEDED, // Request for Object data in excess of max obj count
Tobin Ehlis235c20e2015-01-16 08:56:30 -070039 OBJTRACK_INVALID_FENCE, // Requested status of unsubmitted fence object
Tobin Ehlis586aa012015-06-08 17:36:28 -060040 OBJTRACK_INVALID_OBJECT, // Object used that has never been created
Tobin Ehlis3c26a542014-11-18 11:28:33 -070041} OBJECT_TRACK_ERROR;
42
Tobin Ehlis235c20e2015-01-16 08:56:30 -070043// Object Status -- used to track state of individual objects
Mark Lobodzinski7d2d5ac2015-05-20 17:33:47 -050044typedef VkFlags ObjectStatusFlags;
45typedef enum _ObjectStatusFlagBits
Tobin Ehlis235c20e2015-01-16 08:56:30 -070046{
Mark Lobodzinski01552702015-02-03 10:06:31 -060047 OBJSTATUS_NONE = 0x00000000, // No status is set
48 OBJSTATUS_FENCE_IS_SUBMITTED = 0x00000001, // Fence has been submitted
49 OBJSTATUS_VIEWPORT_BOUND = 0x00000002, // Viewport state object has been bound
50 OBJSTATUS_RASTER_BOUND = 0x00000004, // Viewport state object has been bound
51 OBJSTATUS_COLOR_BLEND_BOUND = 0x00000008, // Viewport state object has been bound
52 OBJSTATUS_DEPTH_STENCIL_BOUND = 0x00000010, // Viewport state object has been bound
Mark Lobodzinski4186e712015-02-03 11:52:26 -060053 OBJSTATUS_GPU_MEM_MAPPED = 0x00000020, // Memory object is currently mapped
Mark Lobodzinski7d2d5ac2015-05-20 17:33:47 -050054} ObjectStatusFlagBits;
Chia-I Wu5b66aa52015-04-16 22:02:10 +080055
Tobin Ehlisacab8882014-11-14 13:01:02 -070056typedef struct _OBJTRACK_NODE {
Tony Barbour2a199c12015-07-09 17:31:46 -060057 uint64_t vkObj;
58 VkDbgObjectType objType;
59 ObjectStatusFlags status;
Tobin Ehlisacab8882014-11-14 13:01:02 -070060} OBJTRACK_NODE;
Mark Lobodzinskie1d3f0c2015-02-09 10:20:53 -060061
Tobin Ehlisacab8882014-11-14 13:01:02 -070062// prototype for extension functions
Mark Lobodzinski14305ad2015-06-23 11:35:12 -060063uint64_t objTrackGetObjectCount(VkDevice device);
Tony Barbour2a199c12015-07-09 17:31:46 -060064uint64_t objTrackGetObjectsOfTypeCount(VkDevice, VkDbgObjectType type);
Mark Lobodzinskie1d3f0c2015-02-09 10:20:53 -060065
Tobin Ehlis3c26a542014-11-18 11:28:33 -070066// Func ptr typedefs
Mark Lobodzinski14305ad2015-06-23 11:35:12 -060067typedef uint64_t (*OBJ_TRACK_GET_OBJECT_COUNT)(VkDevice);
Tony Barbour2a199c12015-07-09 17:31:46 -060068typedef uint64_t (*OBJ_TRACK_GET_OBJECTS_OF_TYPE_COUNT)(VkDevice, VkDbgObjectType);
Mark Lobodzinski14305ad2015-06-23 11:35:12 -060069
70typedef struct _layer_data {
71 debug_report_data *report_data;
72 //TODO: put instance data here
73 VkDbgMsgCallback logging_callback;
Courtney Goeltzenleuchter8b0e68d2015-07-05 22:13:43 -060074 bool wsi_lunarg_enabled;
75 bool objtrack_extensions_enabled;
Mark Lobodzinski14305ad2015-06-23 11:35:12 -060076} layer_data;
77
78static std::unordered_map<void*, layer_data *> layer_data_map;
79static device_table_map ObjectTracker_device_table_map;
80static instance_table_map ObjectTracker_instance_table_map;
81
82static long long unsigned int object_track_index = 0;
83static int objLockInitialized = 0;
84static loader_platform_thread_mutex objLock;
85
86// Objects stored in a global map w/ struct containing basic info
Tony Barbour2a199c12015-07-09 17:31:46 -060087// unordered_map<const void*, OBJTRACK_NODE*> objMap;
Mark Lobodzinski14305ad2015-06-23 11:35:12 -060088
Tony Barbour2a199c12015-07-09 17:31:46 -060089#define NUM_OBJECT_TYPES VK_OBJECT_TYPE_NUM
Mark Lobodzinski14305ad2015-06-23 11:35:12 -060090
91static uint64_t numObjs[NUM_OBJECT_TYPES] = {0};
92static uint64_t numTotalObjs = 0;
93static VkPhysicalDeviceQueueProperties *queueInfo = NULL;
94static uint32_t queueCount = 0;
95
96template layer_data *get_my_data_ptr<layer_data>(
97 void *data_key, std::unordered_map<void *, layer_data *> &data_map);
98
99//
100// Internal Object Tracker Functions
101//
102
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600103static void createDeviceRegisterExtensions(const VkDeviceCreateInfo* pCreateInfo, VkDevice device)
104{
Courtney Goeltzenleuchter8b0e68d2015-07-05 22:13:43 -0600105 layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
106 my_device_data->wsi_lunarg_enabled = false;
107 for (uint32_t i = 0; i < pCreateInfo->extensionCount; i++) {
108 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_WSI_LUNARG_EXTENSION_NAME) == 0)
109 my_device_data->wsi_lunarg_enabled = true;
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600110
Courtney Goeltzenleuchter8b0e68d2015-07-05 22:13:43 -0600111 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], "OBJTRACK_EXTENSIONS") == 0)
112 my_device_data->objtrack_extensions_enabled = true;
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600113 }
114}
115
116// Indicate device or instance dispatch table type
117typedef enum _DispTableType
118{
119 DISP_TBL_TYPE_INSTANCE,
120 DISP_TBL_TYPE_DEVICE,
121} DispTableType;
122
Tony Barbour2a199c12015-07-09 17:31:46 -0600123debug_report_data *mdd(const void* object)
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600124{
125 dispatch_key key = get_dispatch_key(object);
126 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600127 return my_data->report_data;
128}
129
130debug_report_data *mid(VkInstance object)
131{
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600132 dispatch_key key = get_dispatch_key(object);
133 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600134 return my_data->report_data;
135}
136
137// For each Queue's doubly linked-list of mem refs
138typedef struct _OT_MEM_INFO {
139 VkDeviceMemory mem;
140 struct _OT_MEM_INFO *pNextMI;
141 struct _OT_MEM_INFO *pPrevMI;
142
143} OT_MEM_INFO;
144
145// Track Queue information
146typedef struct _OT_QUEUE_INFO {
147 OT_MEM_INFO *pMemRefList;
148 struct _OT_QUEUE_INFO *pNextQI;
149 uint32_t queueNodeIndex;
150 VkQueue queue;
151 uint32_t refCount;
152} OT_QUEUE_INFO;
153
154// Global list of QueueInfo structures, one per queue
155static OT_QUEUE_INFO *g_pQueueInfo = NULL;
156
157// Convert an object type enum to an object type array index
158static uint32_t
159objTypeToIndex(
160 uint32_t objType)
161{
162 uint32_t index = objType;
163 if (objType > VK_OBJECT_TYPE_END_RANGE) {
164 // These come from vk_wsi_lunarg.h, rebase
165 index = (index -(VK_WSI_LUNARG_EXTENSION_NUMBER * -1000)) + VK_OBJECT_TYPE_END_RANGE;
166 }
167 return index;
168}
169
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600170// Add new queue to head of global queue list
171static void
172addQueueInfo(
173 uint32_t queueNodeIndex,
174 VkQueue queue)
175{
176 OT_QUEUE_INFO *pQueueInfo = new OT_QUEUE_INFO;
177
178 if (pQueueInfo != NULL) {
179 memset(pQueueInfo, 0, sizeof(OT_QUEUE_INFO));
180 pQueueInfo->queue = queue;
181 pQueueInfo->queueNodeIndex = queueNodeIndex;
182 pQueueInfo->pNextQI = g_pQueueInfo;
183 g_pQueueInfo = pQueueInfo;
184 }
185 else {
Tony Barbour2a199c12015-07-09 17:31:46 -0600186 log_msg(mdd(queue), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_QUEUE, reinterpret_cast<VkUintPtrLeast64>(queue), 0, OBJTRACK_INTERNAL_ERROR, "OBJTRACK",
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600187 "ERROR: VK_ERROR_OUT_OF_HOST_MEMORY -- could not allocate memory for Queue Information");
188 }
189}
190
191// Destroy memRef lists and free all memory
192static void
193destroyQueueMemRefLists(void)
194{
195 OT_QUEUE_INFO *pQueueInfo = g_pQueueInfo;
196 OT_QUEUE_INFO *pDelQueueInfo = NULL;
197 while (pQueueInfo != NULL) {
198 OT_MEM_INFO *pMemInfo = pQueueInfo->pMemRefList;
199 while (pMemInfo != NULL) {
200 OT_MEM_INFO *pDelMemInfo = pMemInfo;
201 pMemInfo = pMemInfo->pNextMI;
202 delete pDelMemInfo;
203 }
204 pDelQueueInfo = pQueueInfo;
205 pQueueInfo = pQueueInfo->pNextQI;
206 delete pDelQueueInfo;
207 }
208 g_pQueueInfo = pQueueInfo;
209}
210
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600211static void
212setGpuQueueInfoState(
Tony Barbour426b9052015-06-24 16:06:58 -0600213 uint32_t count,
214 void *pData)
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600215{
Tony Barbour426b9052015-06-24 16:06:58 -0600216 queueCount = count;
217 queueInfo = (VkPhysicalDeviceQueueProperties*)realloc((void*)queueInfo, count * sizeof(VkPhysicalDeviceQueueProperties));
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600218 if (queueInfo != NULL) {
Tony Barbour426b9052015-06-24 16:06:58 -0600219 memcpy(queueInfo, pData, count * sizeof(VkPhysicalDeviceQueueProperties));
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600220 }
221}
222
223// Check Queue type flags for selected queue operations
224static void
225validateQueueFlags(
226 VkQueue queue,
227 const char *function)
228{
229 OT_QUEUE_INFO *pQueueInfo = g_pQueueInfo;
230 while ((pQueueInfo != NULL) && (pQueueInfo->queue != queue)) {
231 pQueueInfo = pQueueInfo->pNextQI;
232 }
233 if (pQueueInfo != NULL) {
234 if ((queueInfo != NULL) && (queueInfo[pQueueInfo->queueNodeIndex].queueFlags & VK_QUEUE_SPARSE_MEMMGR_BIT) == 0) {
Tony Barbour2a199c12015-07-09 17:31:46 -0600235 log_msg(mdd(queue), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_QUEUE, reinterpret_cast<VkUintPtrLeast64>(queue), 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK",
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600236 "Attempting %s on a non-memory-management capable queue -- VK_QUEUE_SPARSE_MEMMGR_BIT not set", function);
237 } else {
Tony Barbour2a199c12015-07-09 17:31:46 -0600238 log_msg(mdd(queue), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_QUEUE, reinterpret_cast<VkUintPtrLeast64>(queue), 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK",
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600239 "Attempting %s on a possibly non-memory-management capable queue -- VK_QUEUE_SPARSE_MEMMGR_BIT not known", function);
240 }
241 }
242}
243
Tony Barbour2a199c12015-07-09 17:31:46 -0600244/* TODO: Port to new type safety */
245#if 0
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600246// Check object status for selected flag state
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -0600247static VkBool32
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600248validate_status(
249 VkObject dispatchable_object,
250 VkObject vkObj,
251 VkObjectType objType,
252 ObjectStatusFlags status_mask,
253 ObjectStatusFlags status_flag,
254 VkFlags msg_flags,
255 OBJECT_TRACK_ERROR error_code,
256 const char *fail_msg)
257{
258 if (objMap.find(vkObj) != objMap.end()) {
259 OBJTRACK_NODE* pNode = objMap[vkObj];
260 if ((pNode->status & status_mask) != status_flag) {
261 char str[1024];
262 log_msg(mdd(dispatchable_object), msg_flags, pNode->objType, vkObj, 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK",
263 "OBJECT VALIDATION WARNING: %s object 0x%" PRIxLEAST64 ": %s", string_VkObjectType(objType),
264 reinterpret_cast<VkUintPtrLeast64>(vkObj), fail_msg);
265 return VK_FALSE;
266 }
267 return VK_TRUE;
268 }
269 else {
270 // If we do not find it print an error
271 log_msg(mdd(dispatchable_object), msg_flags, (VkObjectType) 0, vkObj, 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK",
272 "Unable to obtain status for non-existent object 0x%" PRIxLEAST64 " of %s type",
273 reinterpret_cast<VkUintPtrLeast64>(vkObj), string_VkObjectType(objType));
274 return VK_FALSE;
275 }
276}
Tony Barbour2a199c12015-07-09 17:31:46 -0600277#endif
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600278
279#include "vk_dispatch_table_helper.h"
280static void
281initObjectTracker(
282 layer_data *my_data)
283{
284 uint32_t report_flags = 0;
285 uint32_t debug_action = 0;
286 FILE *log_output = NULL;
287 const char *option_str;
288 // initialize ObjectTracker options
289 report_flags = getLayerOptionFlags("ObjectTrackerReportFlags", 0);
290 getLayerOptionEnum("ObjectTrackerDebugAction", (uint32_t *) &debug_action);
291
292 if (debug_action & VK_DBG_LAYER_ACTION_LOG_MSG)
293 {
294 option_str = getLayerOption("ObjectTrackerLogFilename");
295 if (option_str) {
296 log_output = fopen(option_str, "w");
297 }
298 if (log_output == NULL) {
299 log_output = stdout;
300 }
301
302 layer_create_msg_callback(my_data->report_data, report_flags, log_callback, (void *) log_output, &my_data->logging_callback);
303 }
304
305 if (!objLockInitialized)
306 {
307 // TODO/TBD: Need to delete this mutex sometime. How??? One
308 // suggestion is to call this during vkCreateInstance(), and then we
309 // can clean it up during vkDestroyInstance(). However, that requires
310 // that the layer have per-instance locks. We need to come back and
311 // address this soon.
312 loader_platform_thread_create_mutex(&objLock);
313 objLockInitialized = 1;
314 }
315}
316
Tony Barbour2a199c12015-07-09 17:31:46 -0600317//
318// Forward declares of generated routines
319//
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600320
Tony Barbour2a199c12015-07-09 17:31:46 -0600321static void create_obj(VkInstance dispatchable_object, VkInstance object, VkDbgObjectType objType);
322static void create_obj(VkDevice dispatchable_object, VkDevice object, VkDbgObjectType objType);
323static void create_obj(VkDevice dispatchable_object, VkDescriptorSet object, VkDbgObjectType objType);
324static void validate_object(VkInstance dispatchable_object, VkInstance object);
325static void validate_object(VkDevice dispatchable_object, VkDevice object);
326static void validate_object(VkDevice dispatchable_object, VkDescriptorPool object);
327static void destroy_obj(VkInstance dispatchable_object, VkInstance object);
328static void destroy_obj(VkDevice dispatchable_object, VkDeviceMemory object);
329static void set_status(VkDevice dispatchable_object, VkDeviceMemory object, VkDbgObjectType objType, ObjectStatusFlags status_flag);
330static void reset_status(VkDevice dispatchable_object, VkDeviceMemory object, VkDbgObjectType objType, ObjectStatusFlags status_flag);
331#if 0
332static VkBool32 validate_status(VkDevice dispatchable_object, VkFence object, VkDbgObjectType objType,
333 ObjectStatusFlags status_mask, ObjectStatusFlags status_flag, VkFlags msg_flags, OBJECT_TRACK_ERROR error_code,
334 const char *fail_msg);
335#endif
336extern unordered_map<const void*, OBJTRACK_NODE*> VkBufferMap;
337extern unordered_map<const void*, OBJTRACK_NODE*> VkFenceMap;
338extern unordered_map<const void*, OBJTRACK_NODE*> VkSemaphoreMap;
339extern unordered_map<const void*, OBJTRACK_NODE*> VkCmdBufferMap;
340extern unordered_map<const void*, OBJTRACK_NODE*> VkSwapChainWSIMap;
341
342static void validate_object(VkQueue dispatchable_object, VkBuffer object)
343{
344 if (VkBufferMap.find((void*)object.handle) != VkBufferMap.end()) {
345 log_msg(mdd(dispatchable_object), VK_DBG_REPORT_ERROR_BIT, (VkDbgObjectType) 0, object.handle, 0, OBJTRACK_INVALID_OBJECT, "OBJTRACK",
Tobin Ehlis33ce8fd2015-07-10 18:25:07 -0600346 "Invalid VkBuffer Object %p",reinterpret_cast<VkUintPtrLeast64>(object.handle));
Tony Barbour2a199c12015-07-09 17:31:46 -0600347 }
348}
349
350static void set_status(VkQueue dispatchable_object, VkFence object, VkDbgObjectType objType, ObjectStatusFlags status_flag)
351{
352 if (object != VK_NULL_HANDLE) {
353 if (VkFenceMap.find((void*)object.handle) != VkFenceMap.end()) {
354 OBJTRACK_NODE* pNode = VkFenceMap[(void*)object.handle];
355 pNode->status |= status_flag;
356 return;
357 }
358 else {
359 // If we do not find it print an error
360 log_msg(mdd(dispatchable_object), VK_DBG_REPORT_ERROR_BIT, (VkDbgObjectType) 0, reinterpret_cast<VkUintPtrLeast64>(object.handle), 0, OBJTRACK_NONE, "OBJTRACK",
361 "Unable to set status for non-existent object 0x%" PRIxLEAST64 " of %s type",
362 reinterpret_cast<VkUintPtrLeast64>(object.handle), string_VkDbgObjectType(objType));
363 }
364 }
365}
366
367static void validate_object(VkQueue dispatchable_object, VkSemaphore object)
368{
369 if (VkSemaphoreMap.find((void*)object.handle) == VkSemaphoreMap.end()) {
370 log_msg(mdd(dispatchable_object), VK_DBG_REPORT_ERROR_BIT, (VkDbgObjectType) 0, object.handle, 0, OBJTRACK_INVALID_OBJECT, "OBJTRACK",
Tobin Ehlis33ce8fd2015-07-10 18:25:07 -0600371 "Invalid VkSemaphore Object %p",reinterpret_cast<VkUintPtrLeast64>(object.handle));
Tony Barbour2a199c12015-07-09 17:31:46 -0600372 }
373}
374
375static void validate_object(VkDevice dispatchable_object, VkCmdBuffer object)
376{
Tobin Ehlis33ce8fd2015-07-10 18:25:07 -0600377 if (VkCmdBufferMap.find(object) == VkCmdBufferMap.end()) {
Tony Barbour2a199c12015-07-09 17:31:46 -0600378 log_msg(mdd(dispatchable_object), VK_DBG_REPORT_ERROR_BIT, (VkDbgObjectType) 0, reinterpret_cast<VkUintPtrLeast64>(object), 0, OBJTRACK_INVALID_OBJECT, "OBJTRACK",
Tobin Ehlis33ce8fd2015-07-10 18:25:07 -0600379 "Invalid VkCmdBuffer Object %p",reinterpret_cast<VkUintPtrLeast64>(object));
Tony Barbour2a199c12015-07-09 17:31:46 -0600380 }
381}
382
383static void create_obj(VkDevice dispatchable_object, VkCmdBuffer vkObj, VkDbgObjectType objType)
384{
385 log_msg(mdd(dispatchable_object), VK_DBG_REPORT_INFO_BIT, objType, reinterpret_cast<VkUintPtrLeast64>(vkObj), 0, OBJTRACK_NONE, "OBJTRACK",
386 "OBJ[%llu] : CREATE %s object 0x%" PRIxLEAST64 , object_track_index++, string_VkDbgObjectType(objType),
387 reinterpret_cast<VkUintPtrLeast64>(vkObj));
388
389 OBJTRACK_NODE* pNewObjNode = new OBJTRACK_NODE;
390 pNewObjNode->objType = objType;
391 pNewObjNode->status = OBJSTATUS_NONE;
392 pNewObjNode->vkObj = reinterpret_cast<VkUintPtrLeast64>(vkObj);
393 VkCmdBufferMap[vkObj] = pNewObjNode;
394 uint32_t objIndex = objTypeToIndex(objType);
395 numObjs[objIndex]++;
396 numTotalObjs++;
397}
398static void create_obj(VkDevice dispatchable_object, VkSwapChainWSI vkObj, VkDbgObjectType objType)
399{
400 log_msg(mdd(dispatchable_object), VK_DBG_REPORT_INFO_BIT, objType, reinterpret_cast<VkUintPtrLeast64>(vkObj), 0, OBJTRACK_NONE, "OBJTRACK",
401 "OBJ[%llu] : CREATE %s object 0x%" PRIxLEAST64 , object_track_index++, string_VkDbgObjectType(objType),
402 reinterpret_cast<VkUintPtrLeast64>(vkObj));
403
404 OBJTRACK_NODE* pNewObjNode = new OBJTRACK_NODE;
405 pNewObjNode->objType = objType;
406 pNewObjNode->status = OBJSTATUS_NONE;
407 pNewObjNode->vkObj = reinterpret_cast<VkUintPtrLeast64>(vkObj);
408 VkSwapChainWSIMap[vkObj] = pNewObjNode;
409 uint32_t objIndex = objTypeToIndex(objType);
410 numObjs[objIndex]++;
411 numTotalObjs++;
412}
413static void destroy_obj(VkSwapChainWSI dispatchable_object, VkSwapChainWSI object)
414{
415 if (VkSwapChainWSIMap.find(object) != VkSwapChainWSIMap.end()) {
416 OBJTRACK_NODE* pNode = VkSwapChainWSIMap[object];
417 uint32_t objIndex = objTypeToIndex(pNode->objType);
418 assert(numTotalObjs > 0);
419 numTotalObjs--;
420 assert(numObjs[objIndex] > 0);
421 numObjs[objIndex]--;
422 log_msg(mdd(dispatchable_object), VK_DBG_REPORT_INFO_BIT, pNode->objType, reinterpret_cast<VkUintPtrLeast64>(object), 0, OBJTRACK_NONE, "OBJTRACK",
423 "OBJ_STAT Destroy %s obj 0x%" PRIxLEAST64 " (%lu total objs remain & %lu %s objs).",
424 string_VkDbgObjectType(pNode->objType), reinterpret_cast<VkUintPtrLeast64>(object), numTotalObjs, numObjs[objIndex],
425 string_VkDbgObjectType(pNode->objType));
426 delete pNode;
427 VkSwapChainWSIMap.erase(object);
428 } else {
429 log_msg(mdd(dispatchable_object), VK_DBG_REPORT_ERROR_BIT, (VkDbgObjectType) 0, reinterpret_cast<VkUintPtrLeast64>(object), 0, OBJTRACK_NONE, "OBJTRACK",
430 "Unable to remove obj 0x%" PRIxLEAST64 ". Was it created? Has it already been destroyed?",
431 reinterpret_cast<VkUintPtrLeast64>(object));
432 }
433}
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600434//
435// Non-auto-generated API functions called by generated code
436//
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600437VkResult
438explicit_CreateInstance(
439 const VkInstanceCreateInfo *pCreateInfo,
440 VkInstance * pInstance)
441{
442 loader_platform_thread_lock_mutex(&objLock);
443 VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ObjectTracker_instance_table_map, *pInstance);
444 VkResult result = pInstanceTable->CreateInstance(pCreateInfo, pInstance);
445
446 if (result == VK_SUCCESS) {
447 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
448 my_data->report_data = debug_report_create_instance(
Courtney Goeltzenleuchter8b0e68d2015-07-05 22:13:43 -0600449 pInstanceTable,
450 *pInstance,
451 pCreateInfo->extensionCount,
452 pCreateInfo->ppEnabledExtensionNames);
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600453
454 initObjectTracker(my_data);
Tobin Ehlis86b52ec2015-06-30 14:31:50 -0600455 create_obj(*pInstance, *pInstance, VK_OBJECT_TYPE_INSTANCE);
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600456 }
457 loader_platform_thread_unlock_mutex(&objLock);
458 return result;
459}
460
461VkResult
Tony Barbour426b9052015-06-24 16:06:58 -0600462explicit_GetPhysicalDeviceQueueProperties(
463 VkPhysicalDevice gpu,
464 uint32_t count,
465 VkPhysicalDeviceQueueProperties* pProperties)
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600466{
Tony Barbour426b9052015-06-24 16:06:58 -0600467 VkResult result = get_dispatch_table(ObjectTracker_instance_table_map, gpu)->GetPhysicalDeviceQueueProperties(gpu, count, pProperties);
468
469 loader_platform_thread_lock_mutex(&objLock);
470 setGpuQueueInfoState(count, pProperties);
471 loader_platform_thread_unlock_mutex(&objLock);
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600472 return result;
473}
474
475VkResult
476explicit_CreateDevice(
477 VkPhysicalDevice gpu,
478 const VkDeviceCreateInfo *pCreateInfo,
479 VkDevice *pDevice)
480{
481 loader_platform_thread_lock_mutex(&objLock);
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600482// VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ObjectTracker_instance_table_map, gpu);
483 VkLayerDispatchTable *pDeviceTable = get_dispatch_table(ObjectTracker_device_table_map, *pDevice);
484 VkResult result = pDeviceTable->CreateDevice(gpu, pCreateInfo, pDevice);
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600485 if (result == VK_SUCCESS) {
486 layer_data *my_instance_data = get_my_data_ptr(get_dispatch_key(gpu), layer_data_map);
487 //// VkLayerDispatchTable *pTable = get_dispatch_table(ObjectTracker_device_table_map, *pDevice);
488 layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(*pDevice), layer_data_map);
489 my_device_data->report_data = layer_debug_report_create_device(my_instance_data->report_data, *pDevice);
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600490 create_obj(*pDevice, *pDevice, VK_OBJECT_TYPE_DEVICE);
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600491 }
492
493 loader_platform_thread_unlock_mutex(&objLock);
494 return result;
495}
496
497VkResult
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600498explicit_GetDeviceQueue(
499 VkDevice device,
500 uint32_t queueNodeIndex,
501 uint32_t queueIndex,
502 VkQueue *pQueue)
503{
504 loader_platform_thread_lock_mutex(&objLock);
505 validate_object(device, device);
506 loader_platform_thread_unlock_mutex(&objLock);
507
508 VkResult result = get_dispatch_table(ObjectTracker_device_table_map, device)->GetDeviceQueue(device, queueNodeIndex, queueIndex, pQueue);
509
510 loader_platform_thread_lock_mutex(&objLock);
511 addQueueInfo(queueNodeIndex, *pQueue);
512 loader_platform_thread_unlock_mutex(&objLock);
513
514 return result;
515}
516
517VkResult
518explicit_QueueSubmit(
519 VkQueue queue,
520 uint32_t cmdBufferCount,
521 const VkCmdBuffer *pCmdBuffers,
522 VkFence fence)
523{
524 loader_platform_thread_lock_mutex(&objLock);
525 set_status(queue, fence, VK_OBJECT_TYPE_FENCE, OBJSTATUS_FENCE_IS_SUBMITTED);
526 // TODO: Fix for updated memory reference mechanism
527 // validate_memory_mapping_status(pMemRefs, memRefCount);
528 // validate_mem_ref_count(memRefCount);
529 loader_platform_thread_unlock_mutex(&objLock);
530
531 VkResult result = get_dispatch_table(ObjectTracker_device_table_map, queue)->QueueSubmit(queue, cmdBufferCount, pCmdBuffers, fence);
532
533 return result;
534}
535
536VkResult
537explicit_MapMemory(
538 VkDevice device,
539 VkDeviceMemory mem,
540 VkDeviceSize offset,
541 VkDeviceSize size,
542 VkFlags flags,
543 void **ppData)
544{
545 loader_platform_thread_lock_mutex(&objLock);
546 set_status(device, mem, VK_OBJECT_TYPE_DEVICE_MEMORY, OBJSTATUS_GPU_MEM_MAPPED);
547 validate_object(device, device);
548 loader_platform_thread_unlock_mutex(&objLock);
549
550 VkResult result = get_dispatch_table(ObjectTracker_device_table_map, device)->MapMemory(device, mem, offset, size, flags, ppData);
551
552 return result;
553}
554
555VkResult
556explicit_UnmapMemory(
557 VkDevice device,
558 VkDeviceMemory mem)
559{
560 loader_platform_thread_lock_mutex(&objLock);
561 reset_status(device, mem, VK_OBJECT_TYPE_DEVICE_MEMORY, OBJSTATUS_GPU_MEM_MAPPED);
562 validate_object(device, device);
563 loader_platform_thread_unlock_mutex(&objLock);
564
565 VkResult result = get_dispatch_table(ObjectTracker_device_table_map, device)->UnmapMemory(device, mem);
566
567 return result;
568}
569
570VkResult
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600571explicit_QueueBindSparseBufferMemory(
Mark Lobodzinski83d4e6a2015-07-03 15:58:09 -0600572 VkQueue queue,
573 VkBuffer buffer,
574 uint32_t numBindings,
575 const VkSparseMemoryBindInfo* pBindInfo)
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600576{
577 loader_platform_thread_lock_mutex(&objLock);
578 validateQueueFlags(queue, "QueueBindSparseBufferMemory");
579 validate_object(queue, buffer);
580 loader_platform_thread_unlock_mutex(&objLock);
581
Mark Lobodzinski83d4e6a2015-07-03 15:58:09 -0600582 VkResult result = get_dispatch_table(ObjectTracker_device_table_map, queue)->QueueBindSparseBufferMemory(queue, buffer, numBindings, pBindInfo);
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600583 return result;
584}
585
586VkResult
587explicit_QueueBindSparseImageMemory(
Mark Lobodzinski83d4e6a2015-07-03 15:58:09 -0600588 VkQueue queue,
589 VkImage image,
590 uint32_t numBindings,
591 const VkSparseImageMemoryBindInfo* pBindInfo)
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600592{
593 loader_platform_thread_lock_mutex(&objLock);
594 validateQueueFlags(queue, "QueueBindSparseImageMemory");
595 loader_platform_thread_unlock_mutex(&objLock);
596
Mark Lobodzinski83d4e6a2015-07-03 15:58:09 -0600597 VkResult result = get_dispatch_table(ObjectTracker_device_table_map, queue)->QueueBindSparseImageMemory(queue, image, numBindings, pBindInfo);
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600598 return result;
599}
600
Mark Lobodzinski83d4e6a2015-07-03 15:58:09 -0600601VkResult
602explicit_QueueBindSparseImageOpaqueMemory(
603 VkQueue queue,
604 VkImage image,
605 uint32_t numBindings,
606 const VkSparseMemoryBindInfo* pBindInfo)
607{
608 loader_platform_thread_lock_mutex(&objLock);
609 validateQueueFlags(queue, "QueueBindSparseImageOpaqueMemory");
610 loader_platform_thread_unlock_mutex(&objLock);
611
612 VkResult result = get_dispatch_table(ObjectTracker_device_table_map, queue)->QueueBindSparseImageOpaqueMemory(queue, image, numBindings, pBindInfo);
613 return result;
614}
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600615
616VkResult
617explicit_GetFenceStatus(
618 VkDevice device,
619 VkFence fence)
620{
621 loader_platform_thread_lock_mutex(&objLock);
622 // Warn if submitted_flag is not set
Tony Barbour2a199c12015-07-09 17:31:46 -0600623#if 0
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600624 validate_status(device, fence, VK_OBJECT_TYPE_FENCE, OBJSTATUS_FENCE_IS_SUBMITTED, OBJSTATUS_FENCE_IS_SUBMITTED,
625 VK_DBG_REPORT_ERROR_BIT, OBJTRACK_INVALID_FENCE, "Status Requested for Unsubmitted Fence");
Tony Barbour2a199c12015-07-09 17:31:46 -0600626#endif
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600627 validate_object(device, device);
628 loader_platform_thread_unlock_mutex(&objLock);
629
630 VkResult result = get_dispatch_table(ObjectTracker_device_table_map, device)->GetFenceStatus(device, fence);
631
632 return result;
633}
634
635VkResult
636explicit_WaitForFences(
637 VkDevice device,
638 uint32_t fenceCount,
639 const VkFence *pFences,
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -0600640 VkBool32 waitAll,
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600641 uint64_t timeout)
642{
643 loader_platform_thread_lock_mutex(&objLock);
Tony Barbour2a199c12015-07-09 17:31:46 -0600644#if 0
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600645 // Warn if waiting on unsubmitted fence
646 for (uint32_t i = 0; i < fenceCount; i++) {
647 validate_status(device, pFences[i], VK_OBJECT_TYPE_FENCE, OBJSTATUS_FENCE_IS_SUBMITTED, OBJSTATUS_FENCE_IS_SUBMITTED,
648 VK_DBG_REPORT_ERROR_BIT, OBJTRACK_INVALID_FENCE, "Waiting for Unsubmitted Fence");
649 }
Tony Barbour2a199c12015-07-09 17:31:46 -0600650#endif
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600651 validate_object(device, device);
652 loader_platform_thread_unlock_mutex(&objLock);
653
654 VkResult result = get_dispatch_table(ObjectTracker_device_table_map, device)->WaitForFences(device, fenceCount, pFences, waitAll, timeout);
655
656 return result;
657}
658
659VkResult
660explicit_AllocDescriptorSets(
661 VkDevice device,
662 VkDescriptorPool descriptorPool,
663 VkDescriptorSetUsage setUsage,
664 uint32_t count,
665 const VkDescriptorSetLayout *pSetLayouts,
666 VkDescriptorSet *pDescriptorSets,
667 uint32_t *pCount)
668{
669 loader_platform_thread_lock_mutex(&objLock);
670 validate_object(device, device);
671 validate_object(device, descriptorPool);
672 loader_platform_thread_unlock_mutex(&objLock);
673
674 VkResult result = get_dispatch_table(ObjectTracker_device_table_map, device)->AllocDescriptorSets(
675 device, descriptorPool, setUsage, count, pSetLayouts, pDescriptorSets, pCount);
676
677 loader_platform_thread_lock_mutex(&objLock);
678 for (uint32_t i = 0; i < *pCount; i++) {
679 create_obj(device, pDescriptorSets[i], VK_OBJECT_TYPE_DESCRIPTOR_SET);
680 }
681 loader_platform_thread_unlock_mutex(&objLock);
682
683 return result;
684}
685
686VkResult
687explicit_DestroySwapChainWSI(
688 VkSwapChainWSI swapChain)
689{
690
691 loader_platform_thread_lock_mutex(&objLock);
692 destroy_obj(swapChain, swapChain);
693 loader_platform_thread_unlock_mutex(&objLock);
694
695 VkResult result = get_dispatch_table(ObjectTracker_device_table_map, swapChain)->DestroySwapChainWSI(swapChain);
696
697 return result;
698}
699
700VkResult
701explicit_FreeMemory(
702 VkDevice device,
703 VkDeviceMemory mem)
704{
705 loader_platform_thread_lock_mutex(&objLock);
706 validate_object(device, device);
707 loader_platform_thread_unlock_mutex(&objLock);
708
709 VkResult result = get_dispatch_table(ObjectTracker_device_table_map, device)->FreeMemory(device, mem);
710
711 loader_platform_thread_lock_mutex(&objLock);
712 destroy_obj(device, mem);
713 loader_platform_thread_unlock_mutex(&objLock);
714
715 return result;
716}
Tony Barbour2a199c12015-07-09 17:31:46 -0600717