blob: a52e2e3d1c851bd791660ab09607091b5d7c0e8a [file] [log] [blame]
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -07001/* 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 Ehliscd9223b2014-11-19 16:19:28 -07005 *
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -07006 * 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 Ehliscd9223b2014-11-19 16:19:28 -070012 *
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -070013 * The above copyright notice(s) and this permission notice shall be included
14 * in all copies or substantial portions of the Materials.
Tobin Ehliscd9223b2014-11-19 16:19:28 -070015 *
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -070016 * 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 Ehliscd9223b2014-11-19 16:19:28 -070019 *
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -070020 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Tobin Ehliscd9223b2014-11-19 16:19:28 -070021 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -070022 * 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 Goeltzenleuchter05559522015-10-30 11:14:30 -060028 *
29 * Author: Tobin Ehlis <tobin@lunarg.com>
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -070030 * Author: Mark Lobodzinski <mark@lunarg.com>
Tobin Ehliscd9223b2014-11-19 16:19:28 -070031 */
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -070032
Mark Lobodzinskib6ddb462015-03-24 16:29:24 -050033#pragma once
Chia-I Wuf8693382015-04-16 22:02:10 +080034#include <vector>
Michael Lentineb4979492015-12-22 11:36:14 -060035#include <unordered_map>
David Pinedo9316d3b2015-11-06 12:54:48 -070036#include "vulkan/vk_layer.h"
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -070037#include "vulkan/vk_ext_debug_report.h"
Mark Lobodzinskib6ddb462015-03-24 16:29:24 -050038
39#ifdef __cplusplus
40extern "C" {
41#endif
42
Tobin Ehliscd9223b2014-11-19 16:19:28 -070043// Mem Tracker ERROR codes
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -070044typedef 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 Ehliscd9223b2014-11-19 16:19:28 -070072} MEM_TRACK_ERROR;
73
Mark Lobodzinskib9644ee2015-10-08 10:44:07 -060074// MemTracker Semaphore states
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -070075typedef 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 Lobodzinskib9644ee2015-10-08 10:44:07 -060079} MtSemaphoreState;
80
Michael Lentine685f61c2015-10-29 10:41:47 -070081struct MemRange {
82 VkDeviceSize offset;
83 VkDeviceSize size;
84};
85
Tobin Ehlis8be20fd2015-01-07 17:49:29 -070086/*
87 * Data Structure overview
Courtney Goeltzenleuchterf2ccd5a2015-04-15 00:14:36 -060088 * There are 4 global STL(' maps
Mark Lobodzinski6434eff2015-03-31 16:05:35 -050089 * 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 Ehlis8be20fd2015-01-07 17:49:29 -070091 * memory objects that are referenced by this CB
Mark Lobodzinski6434eff2015-03-31 16:05:35 -050092 * memObjMap -- map of Memory Objects to MT_MEM_OBJ_INFO structures
93 * Each MT_MEM_OBJ_INFO has two stl list containers with:
Mark Lobodzinskib6ddb462015-03-24 16:29:24 -050094 * -- all CBs referencing this mem obj
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060095 * -- all VK Objects that are bound to this memory
Mark Lobodzinski6434eff2015-03-31 16:05:35 -050096 * objectMap -- map of objects to MT_OBJ_INFO structures
Tobin Ehlis8be20fd2015-01-07 17:49:29 -070097 *
98 * Algorithm overview
99 * These are the primary events that should happen related to different objects
100 * 1. Command buffers
Mark Lobodzinskib6ddb462015-03-24 16:29:24 -0500101 * 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 Ehlis8be20fd2015-01-07 17:49:29 -0700104 * 2. Mem Objects
Mark Lobodzinskib6ddb462015-03-24 16:29:24 -0500105 * CREATION - Add object,structure to map
106 * OBJ BIND - Add obj structure to list container for that mem node
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700107 * CMB BIND - If mem-related add CB structure to list container for that mem
108 *node
Mark Lobodzinskib6ddb462015-03-24 16:29:24 -0500109 * DESTROY - Flag as errors any remaining refs and remove from map
Tobin Ehlis8be20fd2015-01-07 17:49:29 -0700110 * 3. Generic Objects
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700111 * 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 Ehlis8be20fd2015-01-07 17:49:29 -0700115 */
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700116// 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 Ehlis8be20fd2015-01-07 17:49:29 -0700120
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700121// Simple struct to hold handle and type of object so they can be uniquely
122// identified and looked up in appropriate map
Tobin Ehlis257d9742015-07-08 17:08:02 -0600123struct MT_OBJ_HANDLE_TYPE {
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700124 uint64_t handle;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700125 VkDebugReportObjectTypeEXT type;
Tobin Ehlis257d9742015-07-08 17:08:02 -0600126};
127
Tobin Ehlis8be20fd2015-01-07 17:49:29 -0700128// Data struct for tracking memory object
Mark Lobodzinski6434eff2015-03-31 16:05:35 -0500129struct MT_MEM_OBJ_INFO {
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700130 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 Lobodzinskib6ddb462015-03-24 16:29:24 -0500143};
Tobin Ehlis8be20fd2015-01-07 17:49:29 -0700144
Tobin Ehlis257d9742015-07-08 17:08:02 -0600145// This only applies to Buffers and Images, which can have memory bound to them
146struct MT_OBJ_BINDING_INFO {
147 VkDeviceMemory mem;
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700148 bool valid; // If this is a swapchain image backing memory is not a
149 // MT_MEM_OBJ_INFO so store it here.
Chris Forbesb4740d92015-07-11 16:06:23 +1200150 union create_info {
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700151 VkImageCreateInfo image;
Tobin Ehlis257d9742015-07-08 17:08:02 -0600152 VkBufferCreateInfo buffer;
Tobin Ehlis8be20fd2015-01-07 17:49:29 -0700153 } create_info;
Mark Lobodzinskib6ddb462015-03-24 16:29:24 -0500154};
Tobin Ehlisc145be82015-01-08 15:22:32 -0700155
Mark Lobodzinskib6ddb462015-03-24 16:29:24 -0500156// Track all command buffers
Mark Lobodzinskiba442c82015-10-29 15:45:27 -0600157typedef struct _MT_CB_INFO {
Mark Lobodzinskif4945f82015-11-13 13:47:01 -0700158 VkCommandBufferAllocateInfo createInfo;
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700159 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 Lobodzinskib6ddb462015-03-24 16:29:24 -0500167 // Order dependent, stl containers must be at end of struct
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700168 list<VkDeviceMemory>
169 pMemObjList; // List container of Mem objs referenced by this CB
Mark Lobodzinskiba442c82015-10-29 15:45:27 -0600170 // Constructor
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700171 _MT_CB_INFO()
172 : createInfo{}, pipelines{}, attachmentCount(0), fenceId(0),
173 lastSubmittedFence{}, lastSubmittedQueue{} {};
Mark Lobodzinskiba442c82015-10-29 15:45:27 -0600174} MT_CB_INFO;
Mark Lobodzinskie61ebe72015-03-17 10:53:12 -0500175
Mark Lobodzinskif4945f82015-11-13 13:47:01 -0700176// Track command pools and their command buffers
177typedef struct _MT_CMD_POOL_INFO {
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700178 VkCommandPoolCreateFlags createFlags;
179 list<VkCommandBuffer> pCommandBuffers; // list container of cmd buffers
180 // allocated from this pool
Mark Lobodzinskif4945f82015-11-13 13:47:01 -0700181} MT_CMD_POOL_INFO;
182
Michael Lentine80d51872015-11-24 17:55:33 -0600183struct MT_IMAGE_VIEW_INFO {
184 VkImage image;
185};
186
187struct MT_FB_ATTACHMENT_INFO {
188 VkImage image;
189 VkDeviceMemory mem;
190};
191
192struct MT_FB_INFO {
193 std::vector<MT_FB_ATTACHMENT_INFO> attachments;
194};
195
196struct MT_PASS_ATTACHMENT_INFO {
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700197 uint32_t attachment;
198 VkAttachmentLoadOp load_op;
199 VkAttachmentStoreOp store_op;
Michael Lentine80d51872015-11-24 17:55:33 -0600200};
201
202struct MT_PASS_INFO {
203 VkFramebuffer fb;
204 std::vector<MT_PASS_ATTACHMENT_INFO> attachments;
Michael Lentineb4979492015-12-22 11:36:14 -0600205 std::unordered_map<uint32_t, bool> attachment_first_read;
Michael Lentinea4c95d92015-12-28 10:17:32 -0600206 std::unordered_map<uint32_t, VkImageLayout> attachment_first_layout;
Michael Lentine80d51872015-11-24 17:55:33 -0600207};
208
Mark Lobodzinski223ca202015-04-02 08:52:53 -0500209// Associate fenceId with a fence object
Mark Lobodzinski6434eff2015-03-31 16:05:35 -0500210struct MT_FENCE_INFO {
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700211 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 Ehlis257d9742015-07-08 17:08:02 -0600215 VkFenceCreateInfo createInfo;
Mark Lobodzinskib6ddb462015-03-24 16:29:24 -0500216};
Mark Lobodzinskie61ebe72015-03-17 10:53:12 -0500217
Mark Lobodzinski223ca202015-04-02 08:52:53 -0500218// Track Queue information
219struct MT_QUEUE_INFO {
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700220 uint64_t lastRetiredId;
221 uint64_t lastSubmittedId;
222 list<VkCommandBuffer> pQueueCommandBuffers;
223 list<VkDeviceMemory> pMemRefList;
Mark Lobodzinski223ca202015-04-02 08:52:53 -0500224};
225
Mark Lobodzinskib9644ee2015-10-08 10:44:07 -0600226// Track Swapchain Information
Chia-I Wuf8693382015-04-16 22:02:10 +0800227struct MT_SWAP_CHAIN_INFO {
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700228 VkSwapchainCreateInfoKHR createInfo;
229 std::vector<VkImage> images;
Chia-I Wuf8693382015-04-16 22:02:10 +0800230};
231
Michael Lentine85bb2ef2015-12-22 17:30:09 -0600232struct MEMORY_RANGE {
233 uint64_t handle;
234 VkDeviceMemory memory;
235 VkDeviceSize start;
236 VkDeviceSize end;
237};
238
Mark Lobodzinskib6ddb462015-03-24 16:29:24 -0500239#ifdef __cplusplus
240}
241#endif