Tobin Ehlis | cd9223b | 2014-11-19 16:19:28 -0700 | [diff] [blame] | 1 | /* |
Tobin Ehlis | cd9223b | 2014-11-19 16:19:28 -0700 | [diff] [blame] | 2 | * |
Courtney Goeltzenleuchter | fcbe16f | 2015-10-29 13:50:34 -0600 | [diff] [blame] | 3 | * Copyright (C) 2015 Valve Corporation |
Michael Lentine | 685f61c | 2015-10-29 10:41:47 -0700 | [diff] [blame] | 4 | * Copyright (C) 2015 Google, Inc. |
Tobin Ehlis | cd9223b | 2014-11-19 16:19:28 -0700 | [diff] [blame] | 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. |
Courtney Goeltzenleuchter | 0555952 | 2015-10-30 11:14:30 -0600 | [diff] [blame] | 23 | * |
| 24 | * Author: Tobin Ehlis <tobin@lunarg.com> |
Tobin Ehlis | cd9223b | 2014-11-19 16:19:28 -0700 | [diff] [blame] | 25 | */ |
Mark Lobodzinski | b6ddb46 | 2015-03-24 16:29:24 -0500 | [diff] [blame] | 26 | #pragma once |
Chia-I Wu | f869338 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 27 | #include <vector> |
Michael Lentine | b497949 | 2015-12-22 11:36:14 -0600 | [diff] [blame] | 28 | #include <unordered_map> |
David Pinedo | 9316d3b | 2015-11-06 12:54:48 -0700 | [diff] [blame] | 29 | #include "vulkan/vk_layer.h" |
Courtney Goeltzenleuchter | 7415d5a | 2015-12-09 15:48:16 -0700 | [diff] [blame] | 30 | #include "vulkan/vk_ext_debug_report.h" |
Mark Lobodzinski | b6ddb46 | 2015-03-24 16:29:24 -0500 | [diff] [blame] | 31 | |
| 32 | #ifdef __cplusplus |
| 33 | extern "C" { |
| 34 | #endif |
| 35 | |
Tobin Ehlis | cd9223b | 2014-11-19 16:19:28 -0700 | [diff] [blame] | 36 | // Mem Tracker ERROR codes |
| 37 | typedef enum _MEM_TRACK_ERROR |
| 38 | { |
Mark Lobodzinski | 944aab1 | 2015-06-05 13:59:04 -0500 | [diff] [blame] | 39 | MEMTRACK_NONE, // Used for INFO & other non-error messages |
| 40 | MEMTRACK_INVALID_CB, // Cmd Buffer invalid |
| 41 | MEMTRACK_INVALID_MEM_OBJ, // Invalid Memory Object |
Michael Lentine | 85bb2ef | 2015-12-22 17:30:09 -0600 | [diff] [blame] | 42 | MEMTRACK_INVALID_ALIASING, // Invalid Memory Aliasing |
Michael Lentine | a4c95d9 | 2015-12-28 10:17:32 -0600 | [diff] [blame] | 43 | MEMTRACK_INVALID_LAYOUT, // Invalid Layout |
Mark Lobodzinski | 944aab1 | 2015-06-05 13:59:04 -0500 | [diff] [blame] | 44 | MEMTRACK_INTERNAL_ERROR, // Bug in Mem Track Layer internal data structures |
| 45 | MEMTRACK_FREED_MEM_REF, // MEM Obj freed while it still has obj and/or CB refs |
| 46 | MEMTRACK_MEM_OBJ_CLEAR_EMPTY_BINDINGS, // Clearing bindings on mem obj that doesn't have any bindings |
| 47 | MEMTRACK_MISSING_MEM_BINDINGS, // Trying to retrieve mem bindings, but none found (may be internal error) |
| 48 | MEMTRACK_INVALID_OBJECT, // Attempting to reference generic VK Object that is invalid |
Mark Lobodzinski | 944aab1 | 2015-06-05 13:59:04 -0500 | [diff] [blame] | 49 | MEMTRACK_MEMORY_BINDING_ERROR, // Error during one of many calls that bind memory to object or CB |
| 50 | MEMTRACK_MEMORY_LEAK, // Failure to call vkFreeMemory on Mem Obj prior to DestroyDevice |
| 51 | MEMTRACK_INVALID_STATE, // Memory not in the correct state |
| 52 | MEMTRACK_RESET_CB_WHILE_IN_FLIGHT, // vkResetCommandBuffer() called on a CB that hasn't completed |
| 53 | MEMTRACK_INVALID_FENCE_STATE, // Invalid Fence State signaled or used |
| 54 | MEMTRACK_REBIND_OBJECT, // Non-sparse object bindings are immutable |
Tobin Ehlis | 41376e1 | 2015-07-03 08:45:14 -0600 | [diff] [blame] | 55 | MEMTRACK_INVALID_USAGE_FLAG, // Usage flags specified at image/buffer create conflict w/ use of object |
Michael Lentine | 48f5066 | 2015-10-28 16:26:14 -0700 | [diff] [blame] | 56 | MEMTRACK_INVALID_MAP, // Size flag specified at alloc is too small for mapping range |
Tobin Ehlis | cd9223b | 2014-11-19 16:19:28 -0700 | [diff] [blame] | 57 | } MEM_TRACK_ERROR; |
| 58 | |
Mark Lobodzinski | b9644ee | 2015-10-08 10:44:07 -0600 | [diff] [blame] | 59 | // MemTracker Semaphore states |
| 60 | typedef enum _MtSemaphoreState |
| 61 | { |
| 62 | MEMTRACK_SEMAPHORE_STATE_UNSET, // Semaphore is in an undefined state |
| 63 | MEMTRACK_SEMAPHORE_STATE_SIGNALLED, // Semaphore has is in signalled state |
| 64 | MEMTRACK_SEMAPHORE_STATE_WAIT, // Semaphore is in wait state |
| 65 | } MtSemaphoreState; |
| 66 | |
Michael Lentine | 685f61c | 2015-10-29 10:41:47 -0700 | [diff] [blame] | 67 | struct MemRange { |
| 68 | VkDeviceSize offset; |
| 69 | VkDeviceSize size; |
| 70 | }; |
| 71 | |
Tobin Ehlis | 8be20fd | 2015-01-07 17:49:29 -0700 | [diff] [blame] | 72 | /* |
| 73 | * Data Structure overview |
Courtney Goeltzenleuchter | f2ccd5a | 2015-04-15 00:14:36 -0600 | [diff] [blame] | 74 | * There are 4 global STL(' maps |
Mark Lobodzinski | 6434eff | 2015-03-31 16:05:35 -0500 | [diff] [blame] | 75 | * cbMap -- map of command Buffer (CB) objects to MT_CB_INFO structures |
| 76 | * Each MT_CB_INFO struct has an stl list container with |
Tobin Ehlis | 8be20fd | 2015-01-07 17:49:29 -0700 | [diff] [blame] | 77 | * memory objects that are referenced by this CB |
Mark Lobodzinski | 6434eff | 2015-03-31 16:05:35 -0500 | [diff] [blame] | 78 | * memObjMap -- map of Memory Objects to MT_MEM_OBJ_INFO structures |
| 79 | * Each MT_MEM_OBJ_INFO has two stl list containers with: |
Mark Lobodzinski | b6ddb46 | 2015-03-24 16:29:24 -0500 | [diff] [blame] | 80 | * -- all CBs referencing this mem obj |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 81 | * -- all VK Objects that are bound to this memory |
Mark Lobodzinski | 6434eff | 2015-03-31 16:05:35 -0500 | [diff] [blame] | 82 | * objectMap -- map of objects to MT_OBJ_INFO structures |
Tobin Ehlis | 8be20fd | 2015-01-07 17:49:29 -0700 | [diff] [blame] | 83 | * |
| 84 | * Algorithm overview |
| 85 | * These are the primary events that should happen related to different objects |
| 86 | * 1. Command buffers |
Mark Lobodzinski | b6ddb46 | 2015-03-24 16:29:24 -0500 | [diff] [blame] | 87 | * CREATION - Add object,structure to map |
| 88 | * CMD BIND - If mem associated, add mem reference to list container |
| 89 | * DESTROY - Remove from map, decrement (and report) mem references |
Tobin Ehlis | 8be20fd | 2015-01-07 17:49:29 -0700 | [diff] [blame] | 90 | * 2. Mem Objects |
Mark Lobodzinski | b6ddb46 | 2015-03-24 16:29:24 -0500 | [diff] [blame] | 91 | * CREATION - Add object,structure to map |
| 92 | * OBJ BIND - Add obj structure to list container for that mem node |
| 93 | * CMB BIND - If mem-related add CB structure to list container for that mem node |
| 94 | * DESTROY - Flag as errors any remaining refs and remove from map |
Tobin Ehlis | 8be20fd | 2015-01-07 17:49:29 -0700 | [diff] [blame] | 95 | * 3. Generic Objects |
Mark Lobodzinski | b6ddb46 | 2015-03-24 16:29:24 -0500 | [diff] [blame] | 96 | * MEM BIND - DESTROY any previous binding, Add obj node w/ ref to map, add obj ref to list container for that mem node |
Mark Lobodzinski | 6434eff | 2015-03-31 16:05:35 -0500 | [diff] [blame] | 97 | * DESTROY - If mem bound, remove reference list container for that memInfo, remove object ref from map |
Tobin Ehlis | 8be20fd | 2015-01-07 17:49:29 -0700 | [diff] [blame] | 98 | */ |
| 99 | // TODO : Is there a way to track when Cmd Buffer finishes & remove mem references at that point? |
| 100 | // TODO : Could potentially store a list of freed mem allocs to flag when they're incorrectly used |
| 101 | |
Tobin Ehlis | 257d974 | 2015-07-08 17:08:02 -0600 | [diff] [blame] | 102 | // Simple struct to hold handle and type of object so they can be uniquely identified and looked up in appropriate map |
| 103 | struct MT_OBJ_HANDLE_TYPE { |
| 104 | uint64_t handle; |
Courtney Goeltzenleuchter | 7415d5a | 2015-12-09 15:48:16 -0700 | [diff] [blame] | 105 | VkDebugReportObjectTypeEXT type; |
Tobin Ehlis | 257d974 | 2015-07-08 17:08:02 -0600 | [diff] [blame] | 106 | }; |
| 107 | |
Tobin Ehlis | 8be20fd | 2015-01-07 17:49:29 -0700 | [diff] [blame] | 108 | // Data struct for tracking memory object |
Mark Lobodzinski | 6434eff | 2015-03-31 16:05:35 -0500 | [diff] [blame] | 109 | struct MT_MEM_OBJ_INFO { |
Michael Lentine | 685f61c | 2015-10-29 10:41:47 -0700 | [diff] [blame] | 110 | void* object; // Dispatchable object used to create this memory (device of swapchain) |
| 111 | uint32_t refCount; // Count of references (obj bindings or CB use) |
Michael Lentine | 80d5187 | 2015-11-24 17:55:33 -0600 | [diff] [blame] | 112 | bool valid; // Stores if the memory has valid data or not |
Mark Lobodzinski | b1567a0 | 2015-04-21 15:33:04 -0600 | [diff] [blame] | 113 | VkDeviceMemory mem; |
Michael Lentine | 685f61c | 2015-10-29 10:41:47 -0700 | [diff] [blame] | 114 | VkMemoryAllocateInfo allocInfo; |
| 115 | list<MT_OBJ_HANDLE_TYPE> pObjBindings; // list container of objects bound to this memory |
| 116 | list<VkCommandBuffer> pCommandBufferBindings; // list container of cmd buffers that reference this mem object |
Michael Lentine | 685f61c | 2015-10-29 10:41:47 -0700 | [diff] [blame] | 117 | MemRange memRange; |
Mark Lobodzinski | f4945f8 | 2015-11-13 13:47:01 -0700 | [diff] [blame] | 118 | void *pData, *pDriverData; |
Mark Lobodzinski | b6ddb46 | 2015-03-24 16:29:24 -0500 | [diff] [blame] | 119 | }; |
Tobin Ehlis | 8be20fd | 2015-01-07 17:49:29 -0700 | [diff] [blame] | 120 | |
Tobin Ehlis | 257d974 | 2015-07-08 17:08:02 -0600 | [diff] [blame] | 121 | // This only applies to Buffers and Images, which can have memory bound to them |
| 122 | struct MT_OBJ_BINDING_INFO { |
| 123 | VkDeviceMemory mem; |
Michael Lentine | 80d5187 | 2015-11-24 17:55:33 -0600 | [diff] [blame] | 124 | bool valid; //If this is a swapchain image backing memory is not a MT_MEM_OBJ_INFO so store it here. |
Chris Forbes | b4740d9 | 2015-07-11 16:06:23 +1200 | [diff] [blame] | 125 | union create_info { |
Tobin Ehlis | 257d974 | 2015-07-08 17:08:02 -0600 | [diff] [blame] | 126 | VkImageCreateInfo image; |
| 127 | VkBufferCreateInfo buffer; |
Tobin Ehlis | 8be20fd | 2015-01-07 17:49:29 -0700 | [diff] [blame] | 128 | } create_info; |
Mark Lobodzinski | b6ddb46 | 2015-03-24 16:29:24 -0500 | [diff] [blame] | 129 | }; |
Tobin Ehlis | c145be8 | 2015-01-08 15:22:32 -0700 | [diff] [blame] | 130 | |
Mark Lobodzinski | b6ddb46 | 2015-03-24 16:29:24 -0500 | [diff] [blame] | 131 | // Track all command buffers |
Mark Lobodzinski | ba442c8 | 2015-10-29 15:45:27 -0600 | [diff] [blame] | 132 | typedef struct _MT_CB_INFO { |
Mark Lobodzinski | f4945f8 | 2015-11-13 13:47:01 -0700 | [diff] [blame] | 133 | VkCommandBufferAllocateInfo createInfo; |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 134 | VkPipeline pipelines[VK_PIPELINE_BIND_POINT_RANGE_SIZE]; |
Chia-I Wu | 08accc6 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 135 | uint32_t attachmentCount; |
Mark Lobodzinski | f4945f8 | 2015-11-13 13:47:01 -0700 | [diff] [blame] | 136 | VkCommandBuffer commandBuffer; |
Mark Lobodzinski | b1567a0 | 2015-04-21 15:33:04 -0600 | [diff] [blame] | 137 | uint64_t fenceId; |
Mike Stroyan | 950496e | 2015-05-19 15:16:08 -0600 | [diff] [blame] | 138 | VkFence lastSubmittedFence; |
| 139 | VkQueue lastSubmittedQueue; |
Michael Lentine | 80d5187 | 2015-11-24 17:55:33 -0600 | [diff] [blame] | 140 | VkRenderPass pass; |
Mark Lobodzinski | b6ddb46 | 2015-03-24 16:29:24 -0500 | [diff] [blame] | 141 | // Order dependent, stl containers must be at end of struct |
Mark Lobodzinski | b1567a0 | 2015-04-21 15:33:04 -0600 | [diff] [blame] | 142 | list<VkDeviceMemory> pMemObjList; // List container of Mem objs referenced by this CB |
Mark Lobodzinski | ba442c8 | 2015-10-29 15:45:27 -0600 | [diff] [blame] | 143 | // Constructor |
| 144 | _MT_CB_INFO():createInfo{},pipelines{},attachmentCount(0),fenceId(0),lastSubmittedFence{},lastSubmittedQueue{} {}; |
| 145 | } MT_CB_INFO; |
Mark Lobodzinski | e61ebe7 | 2015-03-17 10:53:12 -0500 | [diff] [blame] | 146 | |
Mark Lobodzinski | f4945f8 | 2015-11-13 13:47:01 -0700 | [diff] [blame] | 147 | // Track command pools and their command buffers |
| 148 | typedef struct _MT_CMD_POOL_INFO { |
| 149 | VkCommandPoolCreateFlags createFlags; |
| 150 | list<VkCommandBuffer> pCommandBuffers; // list container of cmd buffers allocated from this pool |
| 151 | } MT_CMD_POOL_INFO; |
| 152 | |
Michael Lentine | 80d5187 | 2015-11-24 17:55:33 -0600 | [diff] [blame] | 153 | struct MT_IMAGE_VIEW_INFO { |
| 154 | VkImage image; |
| 155 | }; |
| 156 | |
| 157 | struct MT_FB_ATTACHMENT_INFO { |
| 158 | VkImage image; |
| 159 | VkDeviceMemory mem; |
| 160 | }; |
| 161 | |
| 162 | struct MT_FB_INFO { |
| 163 | std::vector<MT_FB_ATTACHMENT_INFO> attachments; |
| 164 | }; |
| 165 | |
| 166 | struct MT_PASS_ATTACHMENT_INFO { |
Michael Lentine | b497949 | 2015-12-22 11:36:14 -0600 | [diff] [blame] | 167 | uint32_t attachment; |
Michael Lentine | 80d5187 | 2015-11-24 17:55:33 -0600 | [diff] [blame] | 168 | VkAttachmentLoadOp load_op; |
Michael Lentine | b497949 | 2015-12-22 11:36:14 -0600 | [diff] [blame] | 169 | VkAttachmentStoreOp store_op; |
Michael Lentine | 80d5187 | 2015-11-24 17:55:33 -0600 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | struct MT_PASS_INFO { |
| 173 | VkFramebuffer fb; |
| 174 | std::vector<MT_PASS_ATTACHMENT_INFO> attachments; |
Michael Lentine | b497949 | 2015-12-22 11:36:14 -0600 | [diff] [blame] | 175 | std::unordered_map<uint32_t, bool> attachment_first_read; |
Michael Lentine | a4c95d9 | 2015-12-28 10:17:32 -0600 | [diff] [blame] | 176 | std::unordered_map<uint32_t, VkImageLayout> attachment_first_layout; |
Michael Lentine | 80d5187 | 2015-11-24 17:55:33 -0600 | [diff] [blame] | 177 | }; |
| 178 | |
Mark Lobodzinski | 223ca20 | 2015-04-02 08:52:53 -0500 | [diff] [blame] | 179 | // Associate fenceId with a fence object |
Mark Lobodzinski | 6434eff | 2015-03-31 16:05:35 -0500 | [diff] [blame] | 180 | struct MT_FENCE_INFO { |
Mark Lobodzinski | f4945f8 | 2015-11-13 13:47:01 -0700 | [diff] [blame] | 181 | uint64_t fenceId; // Sequence number for fence at last submit |
| 182 | VkQueue queue; // Queue that this fence is submitted against or NULL |
Tobin Ehlis | 257d974 | 2015-07-08 17:08:02 -0600 | [diff] [blame] | 183 | VkFenceCreateInfo createInfo; |
Mark Lobodzinski | b6ddb46 | 2015-03-24 16:29:24 -0500 | [diff] [blame] | 184 | }; |
Mark Lobodzinski | e61ebe7 | 2015-03-17 10:53:12 -0500 | [diff] [blame] | 185 | |
Mark Lobodzinski | 223ca20 | 2015-04-02 08:52:53 -0500 | [diff] [blame] | 186 | // Track Queue information |
| 187 | struct MT_QUEUE_INFO { |
Mark Lobodzinski | b1567a0 | 2015-04-21 15:33:04 -0600 | [diff] [blame] | 188 | uint64_t lastRetiredId; |
| 189 | uint64_t lastSubmittedId; |
Mark Lobodzinski | f4945f8 | 2015-11-13 13:47:01 -0700 | [diff] [blame] | 190 | list<VkCommandBuffer> pQueueCommandBuffers; |
Mark Lobodzinski | b1567a0 | 2015-04-21 15:33:04 -0600 | [diff] [blame] | 191 | list<VkDeviceMemory> pMemRefList; |
Mark Lobodzinski | 223ca20 | 2015-04-02 08:52:53 -0500 | [diff] [blame] | 192 | }; |
| 193 | |
Mark Lobodzinski | b9644ee | 2015-10-08 10:44:07 -0600 | [diff] [blame] | 194 | // Track Swapchain Information |
Chia-I Wu | f869338 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 195 | struct MT_SWAP_CHAIN_INFO { |
Mark Lobodzinski | b9644ee | 2015-10-08 10:44:07 -0600 | [diff] [blame] | 196 | VkSwapchainCreateInfoKHR createInfo; |
| 197 | std::vector<VkImage> images; |
Chia-I Wu | f869338 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 198 | }; |
| 199 | |
Michael Lentine | 85bb2ef | 2015-12-22 17:30:09 -0600 | [diff] [blame] | 200 | struct MEMORY_RANGE { |
| 201 | uint64_t handle; |
| 202 | VkDeviceMemory memory; |
| 203 | VkDeviceSize start; |
| 204 | VkDeviceSize end; |
| 205 | }; |
| 206 | |
Mark Lobodzinski | b6ddb46 | 2015-03-24 16:29:24 -0500 | [diff] [blame] | 207 | #ifdef __cplusplus |
| 208 | } |
| 209 | #endif |