1/* Copyright (c) 2015-2016 The Khronos Group Inc.
2 * Copyright (c) 2015-2016 Valve Corporation
3 * Copyright (c) 2015-2016 LunarG, Inc.
4 * Copyright (C) 2015-2016 Google Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Mark Lobodzinski <mark@lunarg.com>
19 * Author: Jon Ashburn <jon@lunarg.com>
20 * Author: Tobin Ehlis <tobin@lunarg.com>
21 */
22
23#include <mutex>
24
25#include "vk_enum_string_helper.h"
26#include "vk_layer_extension_utils.h"
27#include "vk_layer_table.h"
28#include "vk_layer_utils.h"
29#include "vulkan/vk_layer.h"
30
31namespace object_tracker {
32
33// Object Tracker ERROR codes
34enum OBJECT_TRACK_ERROR {
35    OBJTRACK_NONE,                     // Used for INFO & other non-error messages
36    OBJTRACK_UNKNOWN_OBJECT,           // Updating uses of object that's not in global object list
37    OBJTRACK_INTERNAL_ERROR,           // Bug with data tracking within the layer
38    OBJTRACK_OBJECT_LEAK,              // OBJECT was not correctly freed/destroyed
39    OBJTRACK_INVALID_OBJECT,           // Object used that has never been created
40    OBJTRACK_DESCRIPTOR_POOL_MISMATCH, // Descriptor Pools specified incorrectly
41    OBJTRACK_COMMAND_POOL_MISMATCH,    // Command Pools specified incorrectly
42    OBJTRACK_ALLOCATOR_MISMATCH,       // Created with custom allocator but destroyed without
43};
44
45// Object Status -- used to track state of individual objects
46typedef VkFlags ObjectStatusFlags;
47enum ObjectStatusFlagBits {
48    OBJSTATUS_NONE = 0x00000000,                     // No status is set
49    OBJSTATUS_FENCE_IS_SUBMITTED = 0x00000001,       // Fence has been submitted
50    OBJSTATUS_VIEWPORT_BOUND = 0x00000002,           // Viewport state object has been bound
51    OBJSTATUS_RASTER_BOUND = 0x00000004,             // Viewport state object has been bound
52    OBJSTATUS_COLOR_BLEND_BOUND = 0x00000008,        // Viewport state object has been bound
53    OBJSTATUS_DEPTH_STENCIL_BOUND = 0x00000010,      // Viewport state object has been bound
54    OBJSTATUS_GPU_MEM_MAPPED = 0x00000020,           // Memory object is currently mapped
55    OBJSTATUS_COMMAND_BUFFER_SECONDARY = 0x00000040, // Command Buffer is of type SECONDARY
56    OBJSTATUS_CUSTOM_ALLOCATOR = 0x00000080,         // Allocated with custom allocator
57};
58
59// Object and state information structure
60struct OBJTRACK_NODE {
61    uint64_t handle;                        // Object handle (new)
62    VkDebugReportObjectTypeEXT object_type; // Object type identifier
63    ObjectStatusFlags status;               // Object state
64    uint64_t parent_object;                 // Parent object
65};
66
67// Track Queue information
68struct OT_QUEUE_INFO {
69    uint32_t queue_node_index;
70    VkQueue queue;
71};
72
73// Layer name string to be logged with validation messages.
74const char LayerName[] = "ObjectTracker";
75
76struct instance_extension_enables {
77    bool wsi_enabled;
78    bool xlib_enabled;
79    bool xcb_enabled;
80    bool wayland_enabled;
81    bool mir_enabled;
82    bool android_enabled;
83    bool win32_enabled;
84};
85
86typedef std::unordered_map<uint64_t, OBJTRACK_NODE *> object_map_type;
87struct layer_data {
88    VkInstance instance;
89    VkPhysicalDevice physical_device;
90
91    uint64_t num_objects[VK_DEBUG_REPORT_OBJECT_TYPE_RANGE_SIZE_EXT + 1];
92    uint64_t num_total_objects;
93
94    debug_report_data *report_data;
95    std::vector<VkDebugReportCallbackEXT> logging_callback;
96    bool wsi_enabled;
97    bool wsi_display_swapchain_enabled;
98    bool objtrack_extensions_enabled;
99
100    // The following are for keeping track of the temporary callbacks that can
101    // be used in vkCreateInstance and vkDestroyInstance:
102    uint32_t num_tmp_callbacks;
103    VkDebugReportCallbackCreateInfoEXT *tmp_dbg_create_infos;
104    VkDebugReportCallbackEXT *tmp_callbacks;
105
106    std::vector<VkQueueFamilyProperties> queue_family_properties;
107
108    // Vector of unordered_maps per object type to hold OBJTRACK_NODE info
109    std::vector<object_map_type> object_map;
110    // Special-case map for swapchain images
111    std::unordered_map<uint64_t, OBJTRACK_NODE *> swapchainImageMap;
112    // Map of queue information structures, one per queue
113    std::unordered_map<VkQueue, OT_QUEUE_INFO *> queue_info_map;
114
115    // Default constructor
116    layer_data()
117        : instance(nullptr), physical_device(nullptr), num_objects{}, num_total_objects(0), report_data(nullptr),
118          wsi_enabled(false), wsi_display_swapchain_enabled(false), objtrack_extensions_enabled(false), num_tmp_callbacks(0),
119          tmp_dbg_create_infos(nullptr), tmp_callbacks(nullptr), object_map{} {
120        object_map.resize(VK_DEBUG_REPORT_OBJECT_TYPE_RANGE_SIZE_EXT + 1);
121    }
122};
123
124
125static std::unordered_map<void *, struct instance_extension_enables> instanceExtMap;
126static std::unordered_map<void *, layer_data *> layer_data_map;
127static device_table_map ot_device_table_map;
128static instance_table_map ot_instance_table_map;
129static std::mutex global_lock;
130static uint64_t object_track_index = 0;
131
132// Array of object name strings for OBJECT_TYPE enum conversion
133static const char *object_name[VK_DEBUG_REPORT_OBJECT_TYPE_RANGE_SIZE_EXT] = {
134    "Unknown",               // VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN
135    "Instance",              // VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT
136    "Physical Device",       // VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT
137    "Device",                // VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT
138    "Queue",                 // VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT
139    "Semaphore",             // VK_DEBUG_REPORT_OBJECT_TYPE_SEMAPHORE_EXT
140    "Command Buffer",        // VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT
141    "Fence",                 // VK_DEBUG_REPORT_OBJECT_TYPE_FENCE_EXT
142    "Device Memory",         // VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT
143    "Buffer",                // VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT
144    "Image",                 // VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT
145    "Event",                 // VK_DEBUG_REPORT_OBJECT_TYPE_EVENT_EXT
146    "Query Pool",            // VK_DEBUG_REPORT_OBJECT_TYPE_QUERY_POOL_EXT
147    "Buffer View",           // VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_VIEW_EXT
148    "Image View",            // VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT
149    "Shader Module",         // VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT
150    "Pipeline Cache",        // VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_CACHE_EXT
151    "Pipeline Layout",       // VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_LAYOUT_EXT
152    "Render Pass",           // VK_DEBUG_REPORT_OBJECT_TYPE_RENDER_PASS_EXT
153    "Pipeline",              // VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT
154    "Descriptor Set Layout", // VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT
155    "Sampler",               // VK_DEBUG_REPORT_OBJECT_TYPE_SAMPLER_EXT
156    "Descriptor Pool",       // VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT
157    "Descriptor Set",        // VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT
158    "Framebuffer",           // VK_DEBUG_REPORT_OBJECT_TYPE_FRAMEBUFFER_EXT
159    "Command Pool",          // VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_POOL_EXT
160    "SurfaceKHR",            // VK_DEBUG_REPORT_OBJECT_TYPE_SURFACE_KHR_EXT
161    "SwapchainKHR",          // VK_DEBUG_REPORT_OBJECT_TYPE_SWAPCHAIN_KHR_EXT
162    "Debug Report" };        // VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT
163
164#include "vk_dispatch_table_helper.h"
165
166} // namespace object_tracker
167