Mark Lobodzinski | 6eda00a | 2016-02-02 15:55:36 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2015-2016 The Khronos Group Inc. |
| 2 | * Copyright (c) 2015-2016 Valve Corporation |
| 3 | * Copyright (c) 2015-2016 LunarG, Inc. |
| 4 | * Copyright (C) 2015-2016 Google Inc. |
Tobin Ehlis | cd9223b | 2014-11-19 16:19:28 -0700 | [diff] [blame] | 5 | * |
Mark Lobodzinski | 6eda00a | 2016-02-02 15:55:36 -0700 | [diff] [blame] | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and/or associated documentation files (the "Materials"), to |
| 8 | * deal in the Materials without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Materials, and to permit persons to whom the Materials |
| 11 | * are furnished to do so, subject to the following conditions: |
Tobin Ehlis | cd9223b | 2014-11-19 16:19:28 -0700 | [diff] [blame] | 12 | * |
Mark Lobodzinski | 6eda00a | 2016-02-02 15:55:36 -0700 | [diff] [blame] | 13 | * The above copyright notice(s) and this permission notice shall be included |
| 14 | * in all copies or substantial portions of the Materials. |
Tobin Ehlis | cd9223b | 2014-11-19 16:19:28 -0700 | [diff] [blame] | 15 | * |
Mark Lobodzinski | 6eda00a | 2016-02-02 15:55:36 -0700 | [diff] [blame] | 16 | * The Materials are Confidential Information as defined by the Khronos |
| 17 | * Membership Agreement until designated non-confidential by Khronos, at which |
| 18 | * point this condition clause shall be removed. |
Tobin Ehlis | cd9223b | 2014-11-19 16:19:28 -0700 | [diff] [blame] | 19 | * |
Mark Lobodzinski | 6eda00a | 2016-02-02 15:55:36 -0700 | [diff] [blame] | 20 | * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Tobin Ehlis | cd9223b | 2014-11-19 16:19:28 -0700 | [diff] [blame] | 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Mark Lobodzinski | 6eda00a | 2016-02-02 15:55:36 -0700 | [diff] [blame] | 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 23 | * |
| 24 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 25 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 26 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE |
| 27 | * USE OR OTHER DEALINGS IN THE MATERIALS |
Courtney Goeltzenleuchter | 0555952 | 2015-10-30 11:14:30 -0600 | [diff] [blame] | 28 | * |
| 29 | * Author: Tobin Ehlis <tobin@lunarg.com> |
Mark Lobodzinski | 6eda00a | 2016-02-02 15:55:36 -0700 | [diff] [blame] | 30 | * Author: Mark Lobodzinski <mark@lunarg.com> |
Tobin Ehlis | cd9223b | 2014-11-19 16:19:28 -0700 | [diff] [blame] | 31 | */ |
Mark Lobodzinski | 6eda00a | 2016-02-02 15:55:36 -0700 | [diff] [blame] | 32 | |
Mark Lobodzinski | b6ddb46 | 2015-03-24 16:29:24 -0500 | [diff] [blame] | 33 | #pragma once |
Chia-I Wu | f869338 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 34 | #include <vector> |
Michael Lentine | b497949 | 2015-12-22 11:36:14 -0600 | [diff] [blame] | 35 | #include <unordered_map> |
David Pinedo | 9316d3b | 2015-11-06 12:54:48 -0700 | [diff] [blame] | 36 | #include "vulkan/vk_layer.h" |
Courtney Goeltzenleuchter | 7415d5a | 2015-12-09 15:48:16 -0700 | [diff] [blame] | 37 | #include "vulkan/vk_ext_debug_report.h" |
Mark Lobodzinski | b6ddb46 | 2015-03-24 16:29:24 -0500 | [diff] [blame] | 38 | |
| 39 | #ifdef __cplusplus |
| 40 | extern "C" { |
| 41 | #endif |
| 42 | |
Tobin Ehlis | cd9223b | 2014-11-19 16:19:28 -0700 | [diff] [blame] | 43 | // Mem Tracker ERROR codes |
Mark Lobodzinski | b39d9e6 | 2016-02-02 17:06:29 -0700 | [diff] [blame^] | 44 | typedef enum _MEM_TRACK_ERROR { |
| 45 | MEMTRACK_NONE, // Used for INFO & other non-error messages |
| 46 | MEMTRACK_INVALID_CB, // Cmd Buffer invalid |
| 47 | MEMTRACK_INVALID_MEM_OBJ, // Invalid Memory Object |
| 48 | MEMTRACK_INVALID_ALIASING, // Invalid Memory Aliasing |
| 49 | MEMTRACK_INVALID_LAYOUT, // Invalid Layout |
| 50 | MEMTRACK_INTERNAL_ERROR, // Bug in Mem Track Layer internal data structures |
| 51 | MEMTRACK_FREED_MEM_REF, // MEM Obj freed while it still has obj and/or CB |
| 52 | // refs |
| 53 | MEMTRACK_MEM_OBJ_CLEAR_EMPTY_BINDINGS, // Clearing bindings on mem obj that |
| 54 | // doesn't have any bindings |
| 55 | MEMTRACK_MISSING_MEM_BINDINGS, // Trying to retrieve mem bindings, but none |
| 56 | // found (may be internal error) |
| 57 | MEMTRACK_INVALID_OBJECT, // Attempting to reference generic VK Object that |
| 58 | // is invalid |
| 59 | MEMTRACK_MEMORY_BINDING_ERROR, // Error during one of many calls that bind |
| 60 | // memory to object or CB |
| 61 | MEMTRACK_MEMORY_LEAK, // Failure to call vkFreeMemory on Mem Obj prior to |
| 62 | // DestroyDevice |
| 63 | MEMTRACK_INVALID_STATE, // Memory not in the correct state |
| 64 | MEMTRACK_RESET_CB_WHILE_IN_FLIGHT, // vkResetCommandBuffer() called on a CB |
| 65 | // that hasn't completed |
| 66 | MEMTRACK_INVALID_FENCE_STATE, // Invalid Fence State signaled or used |
| 67 | MEMTRACK_REBIND_OBJECT, // Non-sparse object bindings are immutable |
| 68 | MEMTRACK_INVALID_USAGE_FLAG, // Usage flags specified at image/buffer create |
| 69 | // conflict w/ use of object |
| 70 | MEMTRACK_INVALID_MAP, // Size flag specified at alloc is too small for |
| 71 | // mapping range |
Tobin Ehlis | cd9223b | 2014-11-19 16:19:28 -0700 | [diff] [blame] | 72 | } MEM_TRACK_ERROR; |
| 73 | |
Mark Lobodzinski | b9644ee | 2015-10-08 10:44:07 -0600 | [diff] [blame] | 74 | // MemTracker Semaphore states |
Mark Lobodzinski | b39d9e6 | 2016-02-02 17:06:29 -0700 | [diff] [blame^] | 75 | typedef enum _MtSemaphoreState { |
| 76 | MEMTRACK_SEMAPHORE_STATE_UNSET, // Semaphore is in an undefined state |
| 77 | MEMTRACK_SEMAPHORE_STATE_SIGNALLED, // Semaphore has is in signalled state |
| 78 | MEMTRACK_SEMAPHORE_STATE_WAIT, // Semaphore is in wait state |
Mark Lobodzinski | b9644ee | 2015-10-08 10:44:07 -0600 | [diff] [blame] | 79 | } MtSemaphoreState; |
| 80 | |
Michael Lentine | 685f61c | 2015-10-29 10:41:47 -0700 | [diff] [blame] | 81 | struct MemRange { |
| 82 | VkDeviceSize offset; |
| 83 | VkDeviceSize size; |
| 84 | }; |
| 85 | |
Tobin Ehlis | 8be20fd | 2015-01-07 17:49:29 -0700 | [diff] [blame] | 86 | /* |
| 87 | * Data Structure overview |
Courtney Goeltzenleuchter | f2ccd5a | 2015-04-15 00:14:36 -0600 | [diff] [blame] | 88 | * There are 4 global STL(' maps |
Mark Lobodzinski | 6434eff | 2015-03-31 16:05:35 -0500 | [diff] [blame] | 89 | * cbMap -- map of command Buffer (CB) objects to MT_CB_INFO structures |
| 90 | * Each MT_CB_INFO struct has an stl list container with |
Tobin Ehlis | 8be20fd | 2015-01-07 17:49:29 -0700 | [diff] [blame] | 91 | * memory objects that are referenced by this CB |
Mark Lobodzinski | 6434eff | 2015-03-31 16:05:35 -0500 | [diff] [blame] | 92 | * memObjMap -- map of Memory Objects to MT_MEM_OBJ_INFO structures |
| 93 | * Each MT_MEM_OBJ_INFO has two stl list containers with: |
Mark Lobodzinski | b6ddb46 | 2015-03-24 16:29:24 -0500 | [diff] [blame] | 94 | * -- all CBs referencing this mem obj |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 95 | * -- all VK Objects that are bound to this memory |
Mark Lobodzinski | 6434eff | 2015-03-31 16:05:35 -0500 | [diff] [blame] | 96 | * objectMap -- map of objects to MT_OBJ_INFO structures |
Tobin Ehlis | 8be20fd | 2015-01-07 17:49:29 -0700 | [diff] [blame] | 97 | * |
| 98 | * Algorithm overview |
| 99 | * These are the primary events that should happen related to different objects |
| 100 | * 1. Command buffers |
Mark Lobodzinski | b6ddb46 | 2015-03-24 16:29:24 -0500 | [diff] [blame] | 101 | * CREATION - Add object,structure to map |
| 102 | * CMD BIND - If mem associated, add mem reference to list container |
| 103 | * DESTROY - Remove from map, decrement (and report) mem references |
Tobin Ehlis | 8be20fd | 2015-01-07 17:49:29 -0700 | [diff] [blame] | 104 | * 2. Mem Objects |
Mark Lobodzinski | b6ddb46 | 2015-03-24 16:29:24 -0500 | [diff] [blame] | 105 | * CREATION - Add object,structure to map |
| 106 | * OBJ BIND - Add obj structure to list container for that mem node |
Mark Lobodzinski | b39d9e6 | 2016-02-02 17:06:29 -0700 | [diff] [blame^] | 107 | * CMB BIND - If mem-related add CB structure to list container for that mem |
| 108 | *node |
Mark Lobodzinski | b6ddb46 | 2015-03-24 16:29:24 -0500 | [diff] [blame] | 109 | * DESTROY - Flag as errors any remaining refs and remove from map |
Tobin Ehlis | 8be20fd | 2015-01-07 17:49:29 -0700 | [diff] [blame] | 110 | * 3. Generic Objects |
Mark Lobodzinski | b39d9e6 | 2016-02-02 17:06:29 -0700 | [diff] [blame^] | 111 | * MEM BIND - DESTROY any previous binding, Add obj node w/ ref to map, add |
| 112 | *obj ref to list container for that mem node |
| 113 | * DESTROY - If mem bound, remove reference list container for that memInfo, |
| 114 | *remove object ref from map |
Tobin Ehlis | 8be20fd | 2015-01-07 17:49:29 -0700 | [diff] [blame] | 115 | */ |
Mark Lobodzinski | b39d9e6 | 2016-02-02 17:06:29 -0700 | [diff] [blame^] | 116 | // TODO : Is there a way to track when Cmd Buffer finishes & remove mem |
| 117 | // references at that point? |
| 118 | // TODO : Could potentially store a list of freed mem allocs to flag when |
| 119 | // they're incorrectly used |
Tobin Ehlis | 8be20fd | 2015-01-07 17:49:29 -0700 | [diff] [blame] | 120 | |
Mark Lobodzinski | b39d9e6 | 2016-02-02 17:06:29 -0700 | [diff] [blame^] | 121 | // Simple struct to hold handle and type of object so they can be uniquely |
| 122 | // identified and looked up in appropriate map |
Tobin Ehlis | 257d974 | 2015-07-08 17:08:02 -0600 | [diff] [blame] | 123 | struct MT_OBJ_HANDLE_TYPE { |
Mark Lobodzinski | b39d9e6 | 2016-02-02 17:06:29 -0700 | [diff] [blame^] | 124 | uint64_t handle; |
Courtney Goeltzenleuchter | 7415d5a | 2015-12-09 15:48:16 -0700 | [diff] [blame] | 125 | VkDebugReportObjectTypeEXT type; |
Tobin Ehlis | 257d974 | 2015-07-08 17:08:02 -0600 | [diff] [blame] | 126 | }; |
| 127 | |
Tobin Ehlis | 8be20fd | 2015-01-07 17:49:29 -0700 | [diff] [blame] | 128 | // Data struct for tracking memory object |
Mark Lobodzinski | 6434eff | 2015-03-31 16:05:35 -0500 | [diff] [blame] | 129 | struct MT_MEM_OBJ_INFO { |
Mark Lobodzinski | b39d9e6 | 2016-02-02 17:06:29 -0700 | [diff] [blame^] | 130 | void *object; // Dispatchable object used to create this memory (device of |
| 131 | // swapchain) |
| 132 | uint32_t refCount; // Count of references (obj bindings or CB use) |
| 133 | bool valid; // Stores if the memory has valid data or not |
| 134 | VkDeviceMemory mem; |
| 135 | VkMemoryAllocateInfo allocInfo; |
| 136 | list<MT_OBJ_HANDLE_TYPE> |
| 137 | pObjBindings; // list container of objects bound to this memory |
| 138 | list<VkCommandBuffer> pCommandBufferBindings; // list container of cmd |
| 139 | // buffers that reference this |
| 140 | // mem object |
| 141 | MemRange memRange; |
| 142 | void *pData, *pDriverData; |
Mark Lobodzinski | b6ddb46 | 2015-03-24 16:29:24 -0500 | [diff] [blame] | 143 | }; |
Tobin Ehlis | 8be20fd | 2015-01-07 17:49:29 -0700 | [diff] [blame] | 144 | |
Tobin Ehlis | 257d974 | 2015-07-08 17:08:02 -0600 | [diff] [blame] | 145 | // This only applies to Buffers and Images, which can have memory bound to them |
| 146 | struct MT_OBJ_BINDING_INFO { |
| 147 | VkDeviceMemory mem; |
Mark Lobodzinski | b39d9e6 | 2016-02-02 17:06:29 -0700 | [diff] [blame^] | 148 | bool valid; // If this is a swapchain image backing memory is not a |
| 149 | // MT_MEM_OBJ_INFO so store it here. |
Chris Forbes | b4740d9 | 2015-07-11 16:06:23 +1200 | [diff] [blame] | 150 | union create_info { |
Mark Lobodzinski | b39d9e6 | 2016-02-02 17:06:29 -0700 | [diff] [blame^] | 151 | VkImageCreateInfo image; |
Tobin Ehlis | 257d974 | 2015-07-08 17:08:02 -0600 | [diff] [blame] | 152 | VkBufferCreateInfo buffer; |
Tobin Ehlis | 8be20fd | 2015-01-07 17:49:29 -0700 | [diff] [blame] | 153 | } create_info; |
Mark Lobodzinski | b6ddb46 | 2015-03-24 16:29:24 -0500 | [diff] [blame] | 154 | }; |
Tobin Ehlis | c145be8 | 2015-01-08 15:22:32 -0700 | [diff] [blame] | 155 | |
Mark Lobodzinski | b6ddb46 | 2015-03-24 16:29:24 -0500 | [diff] [blame] | 156 | // Track all command buffers |
Mark Lobodzinski | ba442c8 | 2015-10-29 15:45:27 -0600 | [diff] [blame] | 157 | typedef struct _MT_CB_INFO { |
Mark Lobodzinski | f4945f8 | 2015-11-13 13:47:01 -0700 | [diff] [blame] | 158 | VkCommandBufferAllocateInfo createInfo; |
Mark Lobodzinski | b39d9e6 | 2016-02-02 17:06:29 -0700 | [diff] [blame^] | 159 | VkPipeline pipelines[VK_PIPELINE_BIND_POINT_RANGE_SIZE]; |
| 160 | uint32_t attachmentCount; |
| 161 | VkCommandBuffer commandBuffer; |
| 162 | uint64_t fenceId; |
| 163 | VkFence lastSubmittedFence; |
| 164 | VkQueue lastSubmittedQueue; |
| 165 | VkRenderPass pass; |
| 166 | vector<std::function<VkBool32()>> validate_functions; |
Mark Lobodzinski | b6ddb46 | 2015-03-24 16:29:24 -0500 | [diff] [blame] | 167 | // Order dependent, stl containers must be at end of struct |
Mark Lobodzinski | b39d9e6 | 2016-02-02 17:06:29 -0700 | [diff] [blame^] | 168 | list<VkDeviceMemory> |
| 169 | pMemObjList; // List container of Mem objs referenced by this CB |
Mark Lobodzinski | ba442c8 | 2015-10-29 15:45:27 -0600 | [diff] [blame] | 170 | // Constructor |
Mark Lobodzinski | b39d9e6 | 2016-02-02 17:06:29 -0700 | [diff] [blame^] | 171 | _MT_CB_INFO() |
| 172 | : createInfo{}, pipelines{}, attachmentCount(0), fenceId(0), |
| 173 | lastSubmittedFence{}, lastSubmittedQueue{} {}; |
Mark Lobodzinski | ba442c8 | 2015-10-29 15:45:27 -0600 | [diff] [blame] | 174 | } MT_CB_INFO; |
Mark Lobodzinski | e61ebe7 | 2015-03-17 10:53:12 -0500 | [diff] [blame] | 175 | |
Mark Lobodzinski | f4945f8 | 2015-11-13 13:47:01 -0700 | [diff] [blame] | 176 | // Track command pools and their command buffers |
| 177 | typedef struct _MT_CMD_POOL_INFO { |
Mark Lobodzinski | b39d9e6 | 2016-02-02 17:06:29 -0700 | [diff] [blame^] | 178 | VkCommandPoolCreateFlags createFlags; |
| 179 | list<VkCommandBuffer> pCommandBuffers; // list container of cmd buffers |
| 180 | // allocated from this pool |
Mark Lobodzinski | f4945f8 | 2015-11-13 13:47:01 -0700 | [diff] [blame] | 181 | } MT_CMD_POOL_INFO; |
| 182 | |
Michael Lentine | 80d5187 | 2015-11-24 17:55:33 -0600 | [diff] [blame] | 183 | struct MT_IMAGE_VIEW_INFO { |
| 184 | VkImage image; |
| 185 | }; |
| 186 | |
| 187 | struct MT_FB_ATTACHMENT_INFO { |
| 188 | VkImage image; |
| 189 | VkDeviceMemory mem; |
| 190 | }; |
| 191 | |
| 192 | struct MT_FB_INFO { |
| 193 | std::vector<MT_FB_ATTACHMENT_INFO> attachments; |
| 194 | }; |
| 195 | |
| 196 | struct MT_PASS_ATTACHMENT_INFO { |
Mark Lobodzinski | b39d9e6 | 2016-02-02 17:06:29 -0700 | [diff] [blame^] | 197 | uint32_t attachment; |
| 198 | VkAttachmentLoadOp load_op; |
| 199 | VkAttachmentStoreOp store_op; |
Michael Lentine | 80d5187 | 2015-11-24 17:55:33 -0600 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | struct MT_PASS_INFO { |
| 203 | VkFramebuffer fb; |
| 204 | std::vector<MT_PASS_ATTACHMENT_INFO> attachments; |
Michael Lentine | b497949 | 2015-12-22 11:36:14 -0600 | [diff] [blame] | 205 | std::unordered_map<uint32_t, bool> attachment_first_read; |
Michael Lentine | a4c95d9 | 2015-12-28 10:17:32 -0600 | [diff] [blame] | 206 | std::unordered_map<uint32_t, VkImageLayout> attachment_first_layout; |
Michael Lentine | 80d5187 | 2015-11-24 17:55:33 -0600 | [diff] [blame] | 207 | }; |
| 208 | |
Mark Lobodzinski | 223ca20 | 2015-04-02 08:52:53 -0500 | [diff] [blame] | 209 | // Associate fenceId with a fence object |
Mark Lobodzinski | 6434eff | 2015-03-31 16:05:35 -0500 | [diff] [blame] | 210 | struct MT_FENCE_INFO { |
Mark Lobodzinski | b39d9e6 | 2016-02-02 17:06:29 -0700 | [diff] [blame^] | 211 | uint64_t fenceId; // Sequence number for fence at last submit |
| 212 | VkQueue queue; // Queue that this fence is submitted against or NULL |
| 213 | VkBool32 firstTimeFlag; // Fence was created in signaled state, avoid |
| 214 | // warnings for first use |
Tobin Ehlis | 257d974 | 2015-07-08 17:08:02 -0600 | [diff] [blame] | 215 | VkFenceCreateInfo createInfo; |
Mark Lobodzinski | b6ddb46 | 2015-03-24 16:29:24 -0500 | [diff] [blame] | 216 | }; |
Mark Lobodzinski | e61ebe7 | 2015-03-17 10:53:12 -0500 | [diff] [blame] | 217 | |
Mark Lobodzinski | 223ca20 | 2015-04-02 08:52:53 -0500 | [diff] [blame] | 218 | // Track Queue information |
| 219 | struct MT_QUEUE_INFO { |
Mark Lobodzinski | b39d9e6 | 2016-02-02 17:06:29 -0700 | [diff] [blame^] | 220 | uint64_t lastRetiredId; |
| 221 | uint64_t lastSubmittedId; |
| 222 | list<VkCommandBuffer> pQueueCommandBuffers; |
| 223 | list<VkDeviceMemory> pMemRefList; |
Mark Lobodzinski | 223ca20 | 2015-04-02 08:52:53 -0500 | [diff] [blame] | 224 | }; |
| 225 | |
Mark Lobodzinski | b9644ee | 2015-10-08 10:44:07 -0600 | [diff] [blame] | 226 | // Track Swapchain Information |
Chia-I Wu | f869338 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 227 | struct MT_SWAP_CHAIN_INFO { |
Mark Lobodzinski | b39d9e6 | 2016-02-02 17:06:29 -0700 | [diff] [blame^] | 228 | VkSwapchainCreateInfoKHR createInfo; |
| 229 | std::vector<VkImage> images; |
Chia-I Wu | f869338 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 230 | }; |
| 231 | |
Michael Lentine | 85bb2ef | 2015-12-22 17:30:09 -0600 | [diff] [blame] | 232 | struct MEMORY_RANGE { |
| 233 | uint64_t handle; |
| 234 | VkDeviceMemory memory; |
| 235 | VkDeviceSize start; |
| 236 | VkDeviceSize end; |
| 237 | }; |
| 238 | |
Mark Lobodzinski | b6ddb46 | 2015-03-24 16:29:24 -0500 | [diff] [blame] | 239 | #ifdef __cplusplus |
| 240 | } |
| 241 | #endif |