Coverage Report

Created: 2025-08-25 06:14

/src/vulkan-loader/loader/debug_utils.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2015-2021 The Khronos Group Inc.
3
 * Copyright (c) 2015-2021 Valve Corporation
4
 * Copyright (c) 2015-2021 LunarG, Inc.
5
 * Copyright (C) 2015-2016 Google Inc.
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 *
19
 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
20
 * Author: Jon Ashburn <jon@LunarG.com>
21
 * Author: Mark Young <marky@lunarg.com>
22
 * Author: Charles Giessen <charles@lunarg.com>
23
 *
24
 */
25
26
#include <inttypes.h>
27
#include <stdio.h>
28
#include <stdlib.h>
29
#include <string.h>
30
31
#include "vk_object_types.h"
32
33
#include "allocation.h"
34
#include "debug_utils.h"
35
#include "log.h"
36
#include "loader.h"
37
#include "vk_loader_platform.h"
38
39
// VK_EXT_debug_report related items
40
41
VkResult util_CreateDebugUtilsMessenger(struct loader_instance *inst, const VkDebugUtilsMessengerCreateInfoEXT *pCreateInfo,
42
0
                                        const VkAllocationCallbacks *pAllocator, VkDebugUtilsMessengerEXT messenger) {
43
0
    VkLayerDbgFunctionNode *new_dbg_function_node = NULL;
44
45
0
    new_dbg_function_node = (VkLayerDbgFunctionNode *)loader_calloc_with_instance_fallback(
46
0
        pAllocator, inst, sizeof(VkLayerDbgFunctionNode), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
47
48
0
    if (!new_dbg_function_node) {
49
0
        return VK_ERROR_OUT_OF_HOST_MEMORY;
50
0
    }
51
52
0
    new_dbg_function_node->is_messenger = true;
53
0
    new_dbg_function_node->messenger.messenger = messenger;
54
0
    new_dbg_function_node->messenger.pfnUserCallback = pCreateInfo->pfnUserCallback;
55
0
    new_dbg_function_node->messenger.messageSeverity = pCreateInfo->messageSeverity;
56
0
    new_dbg_function_node->messenger.messageType = pCreateInfo->messageType;
57
0
    new_dbg_function_node->pUserData = pCreateInfo->pUserData;
58
0
    new_dbg_function_node->pNext = inst->instance_only_dbg_function_head;
59
0
    inst->instance_only_dbg_function_head = new_dbg_function_node;
60
0
    inst->current_dbg_function_head = inst->instance_only_dbg_function_head;
61
62
0
    return VK_SUCCESS;
63
0
}
64
65
VKAPI_ATTR VkResult VKAPI_CALL debug_utils_CreateDebugUtilsMessengerEXT(VkInstance instance,
66
                                                                        const VkDebugUtilsMessengerCreateInfoEXT *pCreateInfo,
67
                                                                        const VkAllocationCallbacks *pAllocator,
68
0
                                                                        VkDebugUtilsMessengerEXT *pMessenger) {
69
0
    struct loader_instance *inst = loader_get_instance(instance);
70
0
    loader_platform_thread_lock_mutex(&loader_lock);
71
0
    VkResult result = inst->disp->layer_inst_disp.CreateDebugUtilsMessengerEXT(inst->instance, pCreateInfo, pAllocator, pMessenger);
72
0
    loader_platform_thread_unlock_mutex(&loader_lock);
73
0
    return result;
74
0
}
75
76
VkBool32 util_SubmitDebugUtilsMessageEXT(const struct loader_instance *inst, VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
77
                                         VkDebugUtilsMessageTypeFlagsEXT messageTypes,
78
0
                                         const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData) {
79
0
    VkBool32 bail = false;
80
81
0
    if (NULL != pCallbackData) {
82
0
        VkLayerDbgFunctionNode *pTrav = inst->current_dbg_function_head;
83
0
        VkDebugReportObjectTypeEXT object_type = VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT;
84
0
        VkDebugReportFlagsEXT object_flags = 0;
85
0
        uint64_t object_handle = 0;
86
87
0
        debug_utils_AnnotFlagsToReportFlags(messageSeverity, messageTypes, &object_flags);
88
0
        if (0 < pCallbackData->objectCount) {
89
0
            debug_utils_AnnotObjectToDebugReportObject(pCallbackData->pObjects, &object_type, &object_handle);
90
0
        }
91
0
        while (pTrav) {
92
0
            if (pTrav->is_messenger && (pTrav->messenger.messageSeverity & messageSeverity) &&
93
0
                (pTrav->messenger.messageType & messageTypes)) {
94
0
                if (pTrav->messenger.pfnUserCallback(messageSeverity, messageTypes, pCallbackData, pTrav->pUserData)) {
95
0
                    bail = true;
96
0
                }
97
0
            }
98
0
            if (!pTrav->is_messenger && pTrav->report.msgFlags & object_flags) {
99
0
                if (pTrav->report.pfnMsgCallback(object_flags, object_type, object_handle, 0, pCallbackData->messageIdNumber,
100
0
                                                 pCallbackData->pMessageIdName, pCallbackData->pMessage, pTrav->pUserData)) {
101
0
                    bail = true;
102
0
                }
103
0
            }
104
0
            pTrav = pTrav->pNext;
105
0
        }
106
0
    }
107
108
0
    return bail;
109
0
}
110
111
void util_DestroyDebugUtilsMessenger(struct loader_instance *inst, VkDebugUtilsMessengerEXT messenger,
112
0
                                     const VkAllocationCallbacks *pAllocator) {
113
0
    VkLayerDbgFunctionNode *pTrav = inst->current_dbg_function_head;
114
0
    VkLayerDbgFunctionNode *pPrev = pTrav;
115
116
0
    while (pTrav) {
117
0
        if (pTrav->is_messenger && pTrav->messenger.messenger == messenger) {
118
0
            pPrev->pNext = pTrav->pNext;
119
0
            if (inst->current_dbg_function_head == pTrav) inst->current_dbg_function_head = pTrav->pNext;
120
0
            if (inst->instance_only_dbg_function_head == pTrav) inst->instance_only_dbg_function_head = pTrav->pNext;
121
0
            loader_free_with_instance_fallback(pAllocator, inst, pTrav);
122
0
            break;
123
0
        }
124
0
        pPrev = pTrav;
125
0
        pTrav = pTrav->pNext;
126
0
    }
127
0
}
128
129
VkResult util_CreateDebugUtilsMessengers(struct loader_instance *inst, const void *pChain,
130
0
                                         const VkAllocationCallbacks *pAllocator) {
131
0
    const void *pNext = pChain;
132
0
    while (pNext) {
133
0
        VkBaseInStructure in_structure = {0};
134
0
        memcpy(&in_structure, pNext, sizeof(VkBaseInStructure));
135
0
        if (in_structure.sType == VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT) {
136
            // Assign a unique handle to each messenger (just use the address of the VkDebugUtilsMessengerCreateInfoEXT)
137
            // This is only being used this way due to it being for an 'anonymous' callback during instance creation
138
0
            VkDebugUtilsMessengerEXT messenger_handle = (VkDebugUtilsMessengerEXT)(uintptr_t)pNext;
139
0
            VkResult ret = util_CreateDebugUtilsMessenger(inst, (const VkDebugUtilsMessengerCreateInfoEXT *)pNext, pAllocator,
140
0
                                                          messenger_handle);
141
0
            if (ret != VK_SUCCESS) {
142
0
                return ret;
143
0
            }
144
0
        }
145
0
        pNext = in_structure.pNext;
146
0
    }
147
0
    return VK_SUCCESS;
148
0
}
149
150
VKAPI_ATTR void VKAPI_CALL debug_utils_SubmitDebugUtilsMessageEXT(VkInstance instance,
151
                                                                  VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
152
                                                                  VkDebugUtilsMessageTypeFlagsEXT messageTypes,
153
0
                                                                  const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData) {
154
0
    struct loader_instance *inst = loader_get_instance(instance);
155
156
0
    inst->disp->layer_inst_disp.SubmitDebugUtilsMessageEXT(inst->instance, messageSeverity, messageTypes, pCallbackData);
157
0
}
158
159
VKAPI_ATTR void VKAPI_CALL debug_utils_DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT messenger,
160
0
                                                                     const VkAllocationCallbacks *pAllocator) {
161
0
    struct loader_instance *inst = loader_get_instance(instance);
162
0
    loader_platform_thread_lock_mutex(&loader_lock);
163
164
0
    inst->disp->layer_inst_disp.DestroyDebugUtilsMessengerEXT(inst->instance, messenger, pAllocator);
165
166
0
    loader_platform_thread_unlock_mutex(&loader_lock);
167
0
}
168
169
// This is the instance chain terminator function for CreateDebugUtilsMessenger
170
VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateDebugUtilsMessengerEXT(VkInstance instance,
171
                                                                       const VkDebugUtilsMessengerCreateInfoEXT *pCreateInfo,
172
                                                                       const VkAllocationCallbacks *pAllocator,
173
0
                                                                       VkDebugUtilsMessengerEXT *pMessenger) {
174
0
    struct loader_instance *inst = (struct loader_instance *)instance;
175
0
    VkResult res = VK_SUCCESS;
176
0
    VkLayerDbgFunctionNode *new_dbg_func_node = NULL;
177
0
    uint32_t next_index = 0;
178
179
0
    uint32_t *pNextIndex = loader_instance_heap_alloc(inst, sizeof(uint32_t), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
180
0
    if (NULL == pNextIndex) {
181
0
        res = VK_ERROR_OUT_OF_HOST_MEMORY;
182
0
        goto out;
183
0
    }
184
185
0
    res = loader_get_next_available_entry(inst, &inst->debug_utils_messengers_list, &next_index, pAllocator);
186
0
    if (res != VK_SUCCESS) {
187
0
        goto out;
188
0
    }
189
190
0
    for (struct loader_icd_term *icd_term = inst->icd_terms; icd_term != NULL; icd_term = icd_term->next) {
191
0
        if (icd_term->debug_utils_messenger_list.list == NULL) {
192
0
            res = loader_init_generic_list(inst, (struct loader_generic_list *)&icd_term->debug_utils_messenger_list,
193
0
                                           sizeof(VkDebugUtilsMessengerEXT));
194
0
            if (res != VK_SUCCESS) {
195
0
                goto out;
196
0
            }
197
0
        } else if (icd_term->debug_utils_messenger_list.capacity <= next_index * sizeof(VkDebugUtilsMessengerEXT)) {
198
0
            res = loader_resize_generic_list(inst, (struct loader_generic_list *)&icd_term->debug_utils_messenger_list);
199
0
            if (res != VK_SUCCESS) {
200
0
                goto out;
201
0
            }
202
0
        }
203
204
0
        if (icd_term->dispatch.CreateDebugUtilsMessengerEXT) {
205
0
            res = icd_term->dispatch.CreateDebugUtilsMessengerEXT(icd_term->instance, pCreateInfo, pAllocator,
206
0
                                                                  &icd_term->debug_utils_messenger_list.list[next_index]);
207
208
0
            if (res != VK_SUCCESS) {
209
0
                goto out;
210
0
            }
211
0
        }
212
0
    }
213
214
    // Setup the debug report callback in the terminator since a layer may want
215
    // to grab the information itself (RenderDoc) and then return back to the
216
    // user callback a sub-set of the messages.
217
0
    new_dbg_func_node = (VkLayerDbgFunctionNode *)loader_calloc_with_instance_fallback(
218
0
        pAllocator, inst, sizeof(VkLayerDbgFunctionNode), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
219
0
    if (!new_dbg_func_node) {
220
0
        res = VK_ERROR_OUT_OF_HOST_MEMORY;
221
0
        goto out;
222
0
    }
223
224
0
    new_dbg_func_node->is_messenger = true;
225
0
    new_dbg_func_node->messenger.pfnUserCallback = pCreateInfo->pfnUserCallback;
226
0
    new_dbg_func_node->messenger.messageSeverity = pCreateInfo->messageSeverity;
227
0
    new_dbg_func_node->messenger.messageType = pCreateInfo->messageType;
228
0
    new_dbg_func_node->pUserData = pCreateInfo->pUserData;
229
0
    new_dbg_func_node->pNext = inst->current_dbg_function_head;
230
0
    inst->current_dbg_function_head = new_dbg_func_node;
231
0
    *pNextIndex = next_index;
232
0
    *pMessenger = (VkDebugUtilsMessengerEXT)(uintptr_t)pNextIndex;
233
0
    new_dbg_func_node->messenger.messenger = *pMessenger;
234
235
0
out:
236
237
    // Roll back on errors
238
0
    if (VK_SUCCESS != res) {
239
0
        if (pNextIndex) {
240
0
            for (struct loader_icd_term *icd_term = inst->icd_terms; icd_term; icd_term = icd_term->next) {
241
0
                if (icd_term->debug_utils_messenger_list.list && icd_term->debug_utils_messenger_list.list[next_index] &&
242
0
                    NULL != icd_term->dispatch.DestroyDebugUtilsMessengerEXT) {
243
0
                    icd_term->dispatch.DestroyDebugUtilsMessengerEXT(
244
0
                        icd_term->instance, icd_term->debug_utils_messenger_list.list[next_index], pAllocator);
245
0
                }
246
0
            }
247
0
        }
248
0
        if (inst->debug_utils_messengers_list.list &&
249
0
            inst->debug_utils_messengers_list.capacity > (*pNextIndex) * sizeof(struct loader_used_object_status)) {
250
0
            inst->debug_utils_messengers_list.list[*pNextIndex].status = VK_FALSE;
251
0
            if (NULL != pAllocator) {
252
0
                inst->debug_utils_messengers_list.list[*pNextIndex].allocation_callbacks = *pAllocator;
253
0
            }
254
0
        }
255
0
        loader_free_with_instance_fallback(pAllocator, inst, new_dbg_func_node);
256
0
        loader_free_with_instance_fallback(pAllocator, inst, pNextIndex);
257
0
    }
258
259
0
    return res;
260
0
}
261
262
// This is the instance chain terminator function for DestroyDebugUtilsMessenger
263
VKAPI_ATTR void VKAPI_CALL terminator_DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT messenger,
264
0
                                                                    const VkAllocationCallbacks *pAllocator) {
265
0
    struct loader_instance *inst = (struct loader_instance *)instance;
266
0
    uint32_t *debug_messenger_index = (uint32_t *)(uintptr_t)messenger;
267
    // Make sure that messenger actually points to anything
268
0
    if (NULL == debug_messenger_index) {
269
0
        return;
270
0
    }
271
272
0
    for (struct loader_icd_term *icd_term = inst->icd_terms; icd_term; icd_term = icd_term->next) {
273
0
        if (icd_term->debug_utils_messenger_list.list && icd_term->debug_utils_messenger_list.list[*debug_messenger_index] &&
274
0
            NULL != icd_term->dispatch.DestroyDebugUtilsMessengerEXT) {
275
0
            icd_term->dispatch.DestroyDebugUtilsMessengerEXT(
276
0
                icd_term->instance, icd_term->debug_utils_messenger_list.list[*debug_messenger_index], pAllocator);
277
0
        }
278
0
    }
279
280
0
    util_DestroyDebugUtilsMessenger(inst, messenger, pAllocator);
281
0
    if (inst->debug_utils_messengers_list.list &&
282
0
        inst->debug_utils_messengers_list.capacity > (*debug_messenger_index) * sizeof(struct loader_used_object_status)) {
283
0
        inst->debug_utils_messengers_list.list[*debug_messenger_index].status = VK_FALSE;
284
0
        if (NULL != pAllocator) {
285
0
            inst->debug_utils_messengers_list.list[*debug_messenger_index].allocation_callbacks = *pAllocator;
286
0
        }
287
0
    }
288
289
0
    loader_free_with_instance_fallback(pAllocator, inst, debug_messenger_index);
290
0
}
291
292
// This is the instance chain terminator function for SubmitDebugUtilsMessageEXT
293
VKAPI_ATTR void VKAPI_CALL terminator_SubmitDebugUtilsMessageEXT(VkInstance instance,
294
                                                                 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
295
                                                                 VkDebugUtilsMessageTypeFlagsEXT messageTypes,
296
0
                                                                 const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData) {
297
0
    loader_platform_thread_lock_mutex(&loader_lock);
298
    // NOTE: Just make the callback ourselves because there could be one or more ICDs that support this extension
299
    //       and each one will trigger the callback to the user.  This would result in multiple callback triggers
300
    //       per message.  Instead, if we get a messaged up to here, then just trigger the message ourselves and
301
    //       return.  This would still allow the ICDs to trigger their own messages, but won't get any external ones.
302
0
    struct loader_instance *inst = (struct loader_instance *)instance;
303
0
    util_SubmitDebugUtilsMessageEXT(inst, messageSeverity, messageTypes, pCallbackData);
304
0
    loader_platform_thread_unlock_mutex(&loader_lock);
305
0
}
306
307
// VK_EXT_debug_report related items
308
309
VkResult util_CreateDebugReportCallback(struct loader_instance *inst, const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
310
0
                                        const VkAllocationCallbacks *pAllocator, VkDebugReportCallbackEXT callback) {
311
0
    VkLayerDbgFunctionNode *new_dbg_func_node = NULL;
312
313
0
    new_dbg_func_node = (VkLayerDbgFunctionNode *)loader_calloc_with_instance_fallback(
314
0
        pAllocator, inst, sizeof(VkLayerDbgFunctionNode), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
315
0
    if (!new_dbg_func_node) {
316
0
        return VK_ERROR_OUT_OF_HOST_MEMORY;
317
0
    }
318
319
0
    new_dbg_func_node->is_messenger = false;
320
0
    new_dbg_func_node->report.msgCallback = callback;
321
0
    new_dbg_func_node->report.pfnMsgCallback = pCreateInfo->pfnCallback;
322
0
    new_dbg_func_node->report.msgFlags = pCreateInfo->flags;
323
0
    new_dbg_func_node->pUserData = pCreateInfo->pUserData;
324
0
    new_dbg_func_node->pNext = inst->instance_only_dbg_function_head;
325
0
    inst->instance_only_dbg_function_head = new_dbg_func_node;
326
0
    inst->current_dbg_function_head = inst->instance_only_dbg_function_head;
327
328
0
    return VK_SUCCESS;
329
0
}
330
331
VKAPI_ATTR VkResult VKAPI_CALL debug_utils_CreateDebugReportCallbackEXT(VkInstance instance,
332
                                                                        const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
333
                                                                        const VkAllocationCallbacks *pAllocator,
334
0
                                                                        VkDebugReportCallbackEXT *pCallback) {
335
0
    struct loader_instance *inst = loader_get_instance(instance);
336
0
    loader_platform_thread_lock_mutex(&loader_lock);
337
0
    VkResult result = inst->disp->layer_inst_disp.CreateDebugReportCallbackEXT(inst->instance, pCreateInfo, pAllocator, pCallback);
338
0
    loader_platform_thread_unlock_mutex(&loader_lock);
339
0
    return result;
340
0
}
341
342
// Utility function to handle reporting
343
VkBool32 util_DebugReportMessage(const struct loader_instance *inst, VkFlags msgFlags, VkDebugReportObjectTypeEXT objectType,
344
0
                                 uint64_t srcObject, size_t location, int32_t msgCode, const char *pLayerPrefix, const char *pMsg) {
345
0
    VkBool32 bail = false;
346
0
    VkLayerDbgFunctionNode *pTrav = inst->current_dbg_function_head;
347
0
    VkDebugUtilsMessageSeverityFlagBitsEXT severity;
348
0
    VkDebugUtilsMessageTypeFlagsEXT types;
349
0
    VkDebugUtilsMessengerCallbackDataEXT callback_data;
350
0
    VkDebugUtilsObjectNameInfoEXT object_name;
351
352
0
    debug_utils_ReportFlagsToAnnotFlags(msgFlags, false, &severity, &types);
353
0
    debug_utils_ReportObjectToAnnotObject(objectType, srcObject, &object_name);
354
355
0
    callback_data.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CALLBACK_DATA_EXT;
356
0
    callback_data.pNext = NULL;
357
0
    callback_data.flags = 0;
358
0
    callback_data.pMessageIdName = pLayerPrefix;
359
0
    callback_data.messageIdNumber = msgCode;
360
0
    callback_data.pMessage = pMsg;
361
0
    callback_data.cmdBufLabelCount = 0;
362
0
    callback_data.pCmdBufLabels = NULL;
363
0
    callback_data.queueLabelCount = 0;
364
0
    callback_data.pQueueLabels = NULL;
365
0
    callback_data.objectCount = 1;
366
0
    callback_data.pObjects = &object_name;
367
368
0
    while (pTrav) {
369
0
        if (!pTrav->is_messenger && pTrav->report.msgFlags & msgFlags) {
370
0
            if (pTrav->report.pfnMsgCallback(msgFlags, objectType, srcObject, location, msgCode, pLayerPrefix, pMsg,
371
0
                                             pTrav->pUserData)) {
372
0
                bail = true;
373
0
            }
374
0
        }
375
0
        if (pTrav->is_messenger && (pTrav->messenger.messageSeverity & severity) && (pTrav->messenger.messageType & types)) {
376
0
            if (pTrav->messenger.pfnUserCallback(severity, types, &callback_data, pTrav->pUserData)) {
377
0
                bail = true;
378
0
            }
379
0
        }
380
381
0
        pTrav = pTrav->pNext;
382
0
    }
383
384
0
    return bail;
385
0
}
386
387
void util_DestroyDebugReportCallback(struct loader_instance *inst, VkDebugReportCallbackEXT callback,
388
0
                                     const VkAllocationCallbacks *pAllocator) {
389
0
    VkLayerDbgFunctionNode *pTrav = inst->current_dbg_function_head;
390
0
    VkLayerDbgFunctionNode *pPrev = pTrav;
391
392
0
    while (pTrav) {
393
0
        if (!pTrav->is_messenger && pTrav->report.msgCallback == callback) {
394
0
            pPrev->pNext = pTrav->pNext;
395
0
            if (inst->current_dbg_function_head == pTrav) inst->current_dbg_function_head = pTrav->pNext;
396
0
            if (inst->instance_only_dbg_function_head == pTrav) inst->instance_only_dbg_function_head = pTrav->pNext;
397
0
            if (inst->current_dbg_function_head == pTrav) inst->current_dbg_function_head = pTrav->pNext;
398
0
            loader_free_with_instance_fallback(pAllocator, inst, pTrav);
399
0
            break;
400
0
        }
401
0
        pPrev = pTrav;
402
0
        pTrav = pTrav->pNext;
403
0
    }
404
0
}
405
406
VkResult util_CreateDebugReportCallbacks(struct loader_instance *inst, const void *pChain,
407
0
                                         const VkAllocationCallbacks *pAllocator) {
408
0
    const void *pNext = pChain;
409
0
    while (pNext) {
410
0
        VkBaseInStructure in_structure = {0};
411
0
        memcpy(&in_structure, pNext, sizeof(VkBaseInStructure));
412
0
        if (in_structure.sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
413
            // Assign a unique handle to each callback (just use the address of the VkDebugReportCallbackCreateInfoEXT):
414
            // This is only being used this way due to it being for an 'anonymous' callback during instance creation
415
0
            VkDebugReportCallbackEXT report_handle = (VkDebugReportCallbackEXT)(uintptr_t)pNext;
416
0
            VkResult ret =
417
0
                util_CreateDebugReportCallback(inst, (const VkDebugReportCallbackCreateInfoEXT *)pNext, pAllocator, report_handle);
418
0
            if (ret != VK_SUCCESS) {
419
0
                return ret;
420
0
            }
421
0
        }
422
0
        pNext = in_structure.pNext;
423
0
    }
424
0
    return VK_SUCCESS;
425
0
}
426
427
VKAPI_ATTR void VKAPI_CALL debug_utils_DestroyDebugReportCallbackEXT(VkInstance instance, VkDebugReportCallbackEXT callback,
428
0
                                                                     const VkAllocationCallbacks *pAllocator) {
429
0
    struct loader_instance *inst = loader_get_instance(instance);
430
0
    loader_platform_thread_lock_mutex(&loader_lock);
431
432
0
    inst->disp->layer_inst_disp.DestroyDebugReportCallbackEXT(inst->instance, callback, pAllocator);
433
434
0
    loader_platform_thread_unlock_mutex(&loader_lock);
435
0
}
436
437
VKAPI_ATTR void VKAPI_CALL debug_utils_DebugReportMessageEXT(VkInstance instance, VkDebugReportFlagsEXT flags,
438
                                                             VkDebugReportObjectTypeEXT objType, uint64_t object, size_t location,
439
0
                                                             int32_t msgCode, const char *pLayerPrefix, const char *pMsg) {
440
0
    struct loader_instance *inst = loader_get_instance(instance);
441
442
0
    inst->disp->layer_inst_disp.DebugReportMessageEXT(inst->instance, flags, objType, object, location, msgCode, pLayerPrefix,
443
0
                                                      pMsg);
444
0
}
445
446
// This is the instance chain terminator function
447
// for CreateDebugReportCallback
448
VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateDebugReportCallbackEXT(VkInstance instance,
449
                                                                       const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
450
                                                                       const VkAllocationCallbacks *pAllocator,
451
0
                                                                       VkDebugReportCallbackEXT *pCallback) {
452
0
    struct loader_instance *inst = (struct loader_instance *)instance;
453
0
    VkResult res = VK_SUCCESS;
454
0
    VkLayerDbgFunctionNode *new_dbg_func_node = NULL;
455
0
    uint32_t next_index = 0;
456
457
0
    uint32_t *pNextIndex = loader_instance_heap_alloc(inst, sizeof(uint32_t), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
458
0
    if (NULL == pNextIndex) {
459
0
        res = VK_ERROR_OUT_OF_HOST_MEMORY;
460
0
        goto out;
461
0
    }
462
463
0
    res = loader_get_next_available_entry(inst, &inst->debug_report_callbacks_list, &next_index, pAllocator);
464
0
    if (res != VK_SUCCESS) {
465
0
        goto out;
466
0
    }
467
468
0
    for (struct loader_icd_term *icd_term = inst->icd_terms; icd_term != NULL; icd_term = icd_term->next) {
469
0
        if (icd_term->debug_report_callback_list.list == NULL) {
470
0
            res = loader_init_generic_list(inst, (struct loader_generic_list *)&icd_term->debug_report_callback_list,
471
0
                                           sizeof(VkDebugUtilsMessengerEXT));
472
0
            if (res != VK_SUCCESS) {
473
0
                goto out;
474
0
            }
475
0
        } else if (icd_term->debug_report_callback_list.capacity <= next_index * sizeof(VkDebugReportCallbackEXT)) {
476
0
            res = loader_resize_generic_list(inst, (struct loader_generic_list *)&icd_term->debug_report_callback_list);
477
0
            if (res != VK_SUCCESS) {
478
0
                goto out;
479
0
            }
480
0
        }
481
482
0
        if (icd_term->dispatch.CreateDebugReportCallbackEXT) {
483
0
            res = icd_term->dispatch.CreateDebugReportCallbackEXT(icd_term->instance, pCreateInfo, pAllocator,
484
0
                                                                  &icd_term->debug_report_callback_list.list[next_index]);
485
486
0
            if (res != VK_SUCCESS) {
487
0
                goto out;
488
0
            }
489
0
        }
490
0
    }
491
492
    // Setup the debug report callback in the terminator since a layer may want
493
    // to grab the information itself (RenderDoc) and then return back to the
494
    // user callback a sub-set of the messages.
495
0
    new_dbg_func_node = (VkLayerDbgFunctionNode *)loader_calloc_with_instance_fallback(
496
0
        pAllocator, inst, sizeof(VkLayerDbgFunctionNode), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
497
498
0
    if (!new_dbg_func_node) {
499
0
        res = VK_ERROR_OUT_OF_HOST_MEMORY;
500
0
        goto out;
501
0
    }
502
503
0
    new_dbg_func_node->is_messenger = false;
504
0
    new_dbg_func_node->report.pfnMsgCallback = pCreateInfo->pfnCallback;
505
0
    new_dbg_func_node->report.msgFlags = pCreateInfo->flags;
506
0
    new_dbg_func_node->pUserData = pCreateInfo->pUserData;
507
0
    new_dbg_func_node->pNext = inst->current_dbg_function_head;
508
0
    inst->current_dbg_function_head = new_dbg_func_node;
509
0
    *pNextIndex = next_index;
510
0
    *pCallback = (VkDebugReportCallbackEXT)(uintptr_t)pNextIndex;
511
0
    new_dbg_func_node->report.msgCallback = *pCallback;
512
513
0
out:
514
515
    // Roll back on errors
516
0
    if (VK_SUCCESS != res) {
517
0
        if (pNextIndex) {
518
0
            for (struct loader_icd_term *icd_term = inst->icd_terms; icd_term; icd_term = icd_term->next) {
519
0
                if (icd_term->debug_report_callback_list.list && icd_term->debug_report_callback_list.list[next_index] &&
520
0
                    NULL != icd_term->dispatch.DestroyDebugReportCallbackEXT) {
521
0
                    icd_term->dispatch.DestroyDebugReportCallbackEXT(
522
0
                        icd_term->instance, icd_term->debug_report_callback_list.list[next_index], pAllocator);
523
0
                }
524
0
            }
525
0
        }
526
0
        if (inst->debug_report_callbacks_list.list &&
527
0
            inst->debug_report_callbacks_list.capacity > (*pNextIndex) * sizeof(struct loader_used_object_status)) {
528
0
            inst->debug_report_callbacks_list.list[*pNextIndex].status = VK_FALSE;
529
0
            if (NULL != pAllocator) {
530
0
                inst->debug_report_callbacks_list.list[*pNextIndex].allocation_callbacks = *pAllocator;
531
0
            }
532
0
        }
533
0
        loader_free_with_instance_fallback(pAllocator, inst, new_dbg_func_node);
534
0
        loader_free_with_instance_fallback(pAllocator, inst, pNextIndex);
535
0
    }
536
537
0
    return res;
538
0
}
539
540
// This is the instance chain terminator function for DestroyDebugReportCallback
541
VKAPI_ATTR void VKAPI_CALL terminator_DestroyDebugReportCallbackEXT(VkInstance instance, VkDebugReportCallbackEXT callback,
542
0
                                                                    const VkAllocationCallbacks *pAllocator) {
543
0
    struct loader_instance *inst = (struct loader_instance *)instance;
544
0
    uint32_t *debug_report_index = (uint32_t *)(uintptr_t)callback;
545
    // Make sure that callback actually points to anything
546
0
    if (NULL == debug_report_index) {
547
0
        return;
548
0
    }
549
0
    for (struct loader_icd_term *icd_term = inst->icd_terms; icd_term; icd_term = icd_term->next) {
550
0
        if (icd_term->debug_report_callback_list.list && icd_term->debug_report_callback_list.list[*debug_report_index] &&
551
0
            NULL != icd_term->dispatch.DestroyDebugReportCallbackEXT) {
552
0
            icd_term->dispatch.DestroyDebugReportCallbackEXT(
553
0
                icd_term->instance, icd_term->debug_report_callback_list.list[*debug_report_index], pAllocator);
554
0
        }
555
0
    }
556
557
0
    util_DestroyDebugReportCallback(inst, callback, pAllocator);
558
0
    if (inst->debug_report_callbacks_list.list &&
559
0
        inst->debug_report_callbacks_list.capacity > (*debug_report_index) * sizeof(struct loader_used_object_status)) {
560
0
        inst->debug_report_callbacks_list.list[*debug_report_index].status = VK_FALSE;
561
0
        if (NULL != pAllocator) {
562
0
            inst->debug_report_callbacks_list.list[*debug_report_index].allocation_callbacks = *pAllocator;
563
0
        }
564
0
    }
565
0
    loader_free_with_instance_fallback(pAllocator, inst, debug_report_index);
566
0
}
567
568
// This is the instance chain terminator function for DebugReportMessage
569
VKAPI_ATTR void VKAPI_CALL terminator_DebugReportMessageEXT(VkInstance instance, VkDebugReportFlagsEXT flags,
570
                                                            VkDebugReportObjectTypeEXT objType, uint64_t object, size_t location,
571
0
                                                            int32_t msgCode, const char *pLayerPrefix, const char *pMsg) {
572
0
    const struct loader_icd_term *icd_term;
573
574
0
    struct loader_instance *inst = (struct loader_instance *)instance;
575
576
0
    loader_platform_thread_lock_mutex(&loader_lock);
577
0
    for (icd_term = inst->icd_terms; icd_term; icd_term = icd_term->next) {
578
0
        if (icd_term->dispatch.DebugReportMessageEXT != NULL) {
579
0
            icd_term->dispatch.DebugReportMessageEXT(icd_term->instance, flags, objType, object, location, msgCode, pLayerPrefix,
580
0
                                                     pMsg);
581
0
        }
582
0
    }
583
584
    // Now that all ICDs have seen the message, call the necessary callbacks.  Ignoring "bail" return value
585
    // as there is nothing to bail from at this point.
586
587
0
    util_DebugReportMessage(inst, flags, objType, object, location, msgCode, pLayerPrefix, pMsg);
588
589
0
    loader_platform_thread_unlock_mutex(&loader_lock);
590
0
}
591
592
// General utilities
593
594
const VkExtensionProperties debug_utils_extension_info[] = {
595
    {VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION},
596
    {VK_EXT_DEBUG_UTILS_EXTENSION_NAME, VK_EXT_DEBUG_UTILS_SPEC_VERSION},
597
};
598
599
0
void destroy_debug_callbacks_chain(struct loader_instance *inst, const VkAllocationCallbacks *pAllocator) {
600
0
    VkLayerDbgFunctionNode *pTrav = inst->current_dbg_function_head;
601
0
    VkLayerDbgFunctionNode *pNext = NULL;
602
0
    while (pTrav) {
603
0
        pNext = pTrav->pNext;
604
0
        loader_free_with_instance_fallback(pAllocator, inst, pTrav);
605
0
        pTrav = pNext;
606
0
    }
607
0
    inst->current_dbg_function_head = NULL;
608
0
}
609
610
0
VkResult add_debug_extensions_to_ext_list(const struct loader_instance *inst, struct loader_extension_list *ext_list) {
611
0
    return loader_add_to_ext_list(inst, ext_list, sizeof(debug_utils_extension_info) / sizeof(VkExtensionProperties),
612
0
                                  debug_utils_extension_info);
613
0
}
614
615
0
bool debug_extensions_InstanceGpa(struct loader_instance *ptr_instance, const char *name, void **addr) {
616
0
    bool ret_type = false;
617
618
0
    *addr = NULL;
619
620
0
    if (!strcmp("vkCreateDebugReportCallbackEXT", name)) {
621
0
        *addr = ptr_instance->enabled_extensions.ext_debug_report == 1 ? (void *)debug_utils_CreateDebugReportCallbackEXT : NULL;
622
0
        ret_type = true;
623
0
    } else if (!strcmp("vkDestroyDebugReportCallbackEXT", name)) {
624
0
        *addr = ptr_instance->enabled_extensions.ext_debug_report == 1 ? (void *)debug_utils_DestroyDebugReportCallbackEXT : NULL;
625
0
        ret_type = true;
626
0
    } else if (!strcmp("vkDebugReportMessageEXT", name)) {
627
0
        *addr = ptr_instance->enabled_extensions.ext_debug_report == 1 ? (void *)debug_utils_DebugReportMessageEXT : NULL;
628
0
        return true;
629
0
    }
630
0
    if (!strcmp("vkCreateDebugUtilsMessengerEXT", name)) {
631
0
        *addr = ptr_instance->enabled_extensions.ext_debug_utils == 1 ? (void *)debug_utils_CreateDebugUtilsMessengerEXT : NULL;
632
0
        ret_type = true;
633
0
    } else if (!strcmp("vkDestroyDebugUtilsMessengerEXT", name)) {
634
0
        *addr = ptr_instance->enabled_extensions.ext_debug_utils == 1 ? (void *)debug_utils_DestroyDebugUtilsMessengerEXT : NULL;
635
0
        ret_type = true;
636
0
    } else if (!strcmp("vkSubmitDebugUtilsMessageEXT", name)) {
637
0
        *addr = ptr_instance->enabled_extensions.ext_debug_utils == 1 ? (void *)debug_utils_SubmitDebugUtilsMessageEXT : NULL;
638
0
        ret_type = true;
639
0
    }
640
641
0
    return ret_type;
642
0
}
643
644
bool debug_utils_ReportFlagsToAnnotFlags(VkDebugReportFlagsEXT dr_flags, bool default_flag_is_spec,
645
                                         VkDebugUtilsMessageSeverityFlagBitsEXT *da_severity,
646
0
                                         VkDebugUtilsMessageTypeFlagsEXT *da_type) {
647
0
    bool type_set = false;
648
0
    if (NULL == da_severity || NULL == da_type) {
649
0
        return false;
650
0
    }
651
0
    *da_type = 0;
652
0
    *da_severity = 0;
653
654
0
    if ((dr_flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) != 0) {
655
0
        *da_severity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT;
656
0
        *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT;
657
0
        type_set = true;
658
0
    } else if ((dr_flags & (VK_DEBUG_REPORT_WARNING_BIT_EXT | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT)) != 0) {
659
0
        *da_severity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
660
0
    } else if ((dr_flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0) {
661
0
        *da_severity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
662
0
    } else if ((dr_flags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) != 0) {
663
0
        *da_severity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT;
664
0
        *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT;
665
0
        type_set = true;
666
0
    }
667
668
0
    if ((dr_flags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) != 0) {
669
0
        *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
670
0
    } else if (!type_set) {
671
0
        if (default_flag_is_spec) {
672
0
            *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT;
673
0
        } else {
674
0
            *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT;
675
0
        }
676
0
    }
677
678
0
    return true;
679
0
}
680
681
bool debug_utils_AnnotFlagsToReportFlags(VkDebugUtilsMessageSeverityFlagBitsEXT da_severity,
682
0
                                         VkDebugUtilsMessageTypeFlagsEXT da_type, VkDebugReportFlagsEXT *dr_flags) {
683
0
    if (NULL == dr_flags) {
684
0
        return false;
685
0
    }
686
687
0
    *dr_flags = 0;
688
689
0
    if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) != 0) {
690
0
        *dr_flags |= VK_DEBUG_REPORT_ERROR_BIT_EXT;
691
0
    } else if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) != 0) {
692
0
        if ((da_type & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) != 0) {
693
0
            *dr_flags |= VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
694
0
        } else {
695
0
            *dr_flags |= VK_DEBUG_REPORT_WARNING_BIT_EXT;
696
0
        }
697
0
    } else if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) != 0) {
698
0
        *dr_flags |= VK_DEBUG_REPORT_INFORMATION_BIT_EXT;
699
0
    } else if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) != 0) {
700
0
        *dr_flags |= VK_DEBUG_REPORT_DEBUG_BIT_EXT;
701
0
    }
702
703
0
    return true;
704
0
}
705
706
bool debug_utils_ReportObjectToAnnotObject(VkDebugReportObjectTypeEXT dr_object_type, uint64_t object_handle,
707
0
                                           VkDebugUtilsObjectNameInfoEXT *da_object_name_info) {
708
0
    if (NULL == da_object_name_info) {
709
0
        return false;
710
0
    }
711
0
    da_object_name_info->sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
712
0
    da_object_name_info->pNext = NULL;
713
0
    da_object_name_info->objectHandle = (uint64_t)(uintptr_t)object_handle;
714
0
    da_object_name_info->pObjectName = NULL;
715
0
    da_object_name_info->objectType = convertDebugReportObjectToCoreObject(dr_object_type);
716
0
    return true;
717
0
}
718
719
bool debug_utils_AnnotObjectToDebugReportObject(const VkDebugUtilsObjectNameInfoEXT *da_object_name_info,
720
0
                                                VkDebugReportObjectTypeEXT *dr_object_type, uint64_t *dr_object_handle) {
721
0
    if (NULL == da_object_name_info || NULL == dr_object_type || NULL == dr_object_handle) {
722
0
        return false;
723
0
    }
724
0
    *dr_object_type = convertCoreObjectToDebugReportObject(da_object_name_info->objectType);
725
0
    *dr_object_handle = da_object_name_info->objectHandle;
726
0
    return true;
727
0
}