blob: a43c4002dcb7d0b69bb97e6c045a68fa4128957f [file] [log] [blame]
Tobin Ehliscd9223b2014-11-19 16:19:28 -07001/*
Tobin Ehliscd9223b2014-11-19 16:19:28 -07002 *
Courtney Goeltzenleuchterfcbe16f2015-10-29 13:50:34 -06003 * Copyright (C) 2015 Valve Corporation
Michael Lentine685f61c2015-10-29 10:41:47 -07004 * Copyright (C) 2015 Google, Inc.
Tobin Ehliscd9223b2014-11-19 16:19:28 -07005 *
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 Goeltzenleuchter05559522015-10-30 11:14:30 -060023 *
24 * Author: Tobin Ehlis <tobin@lunarg.com>
Tobin Ehliscd9223b2014-11-19 16:19:28 -070025 */
Mark Lobodzinskib6ddb462015-03-24 16:29:24 -050026#pragma once
Chia-I Wuf8693382015-04-16 22:02:10 +080027#include <vector>
Michael Lentineb4979492015-12-22 11:36:14 -060028#include <unordered_map>
David Pinedo9316d3b2015-11-06 12:54:48 -070029#include "vulkan/vk_layer.h"
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -070030#include "vulkan/vk_ext_debug_report.h"
Mark Lobodzinskib6ddb462015-03-24 16:29:24 -050031
32#ifdef __cplusplus
33extern "C" {
34#endif
35
Tobin Ehliscd9223b2014-11-19 16:19:28 -070036// Mem Tracker ERROR codes
37typedef enum _MEM_TRACK_ERROR
38{
Mark Lobodzinski944aab12015-06-05 13:59:04 -050039 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 Lentine85bb2ef2015-12-22 17:30:09 -060042 MEMTRACK_INVALID_ALIASING, // Invalid Memory Aliasing
Mark Lobodzinski944aab12015-06-05 13:59:04 -050043 MEMTRACK_INTERNAL_ERROR, // Bug in Mem Track Layer internal data structures
44 MEMTRACK_FREED_MEM_REF, // MEM Obj freed while it still has obj and/or CB refs
45 MEMTRACK_MEM_OBJ_CLEAR_EMPTY_BINDINGS, // Clearing bindings on mem obj that doesn't have any bindings
46 MEMTRACK_MISSING_MEM_BINDINGS, // Trying to retrieve mem bindings, but none found (may be internal error)
47 MEMTRACK_INVALID_OBJECT, // Attempting to reference generic VK Object that is invalid
Mark Lobodzinski944aab12015-06-05 13:59:04 -050048 MEMTRACK_MEMORY_BINDING_ERROR, // Error during one of many calls that bind memory to object or CB
49 MEMTRACK_MEMORY_LEAK, // Failure to call vkFreeMemory on Mem Obj prior to DestroyDevice
50 MEMTRACK_INVALID_STATE, // Memory not in the correct state
51 MEMTRACK_RESET_CB_WHILE_IN_FLIGHT, // vkResetCommandBuffer() called on a CB that hasn't completed
52 MEMTRACK_INVALID_FENCE_STATE, // Invalid Fence State signaled or used
53 MEMTRACK_REBIND_OBJECT, // Non-sparse object bindings are immutable
Tobin Ehlis41376e12015-07-03 08:45:14 -060054 MEMTRACK_INVALID_USAGE_FLAG, // Usage flags specified at image/buffer create conflict w/ use of object
Michael Lentine48f50662015-10-28 16:26:14 -070055 MEMTRACK_INVALID_MAP, // Size flag specified at alloc is too small for mapping range
Tobin Ehliscd9223b2014-11-19 16:19:28 -070056} MEM_TRACK_ERROR;
57
Mark Lobodzinskib9644ee2015-10-08 10:44:07 -060058// MemTracker Semaphore states
59typedef enum _MtSemaphoreState
60{
61 MEMTRACK_SEMAPHORE_STATE_UNSET, // Semaphore is in an undefined state
62 MEMTRACK_SEMAPHORE_STATE_SIGNALLED, // Semaphore has is in signalled state
63 MEMTRACK_SEMAPHORE_STATE_WAIT, // Semaphore is in wait state
64} MtSemaphoreState;
65
Michael Lentine685f61c2015-10-29 10:41:47 -070066struct MemRange {
67 VkDeviceSize offset;
68 VkDeviceSize size;
69};
70
Tobin Ehlis8be20fd2015-01-07 17:49:29 -070071/*
72 * Data Structure overview
Courtney Goeltzenleuchterf2ccd5a2015-04-15 00:14:36 -060073 * There are 4 global STL(' maps
Mark Lobodzinski6434eff2015-03-31 16:05:35 -050074 * cbMap -- map of command Buffer (CB) objects to MT_CB_INFO structures
75 * Each MT_CB_INFO struct has an stl list container with
Tobin Ehlis8be20fd2015-01-07 17:49:29 -070076 * memory objects that are referenced by this CB
Mark Lobodzinski6434eff2015-03-31 16:05:35 -050077 * memObjMap -- map of Memory Objects to MT_MEM_OBJ_INFO structures
78 * Each MT_MEM_OBJ_INFO has two stl list containers with:
Mark Lobodzinskib6ddb462015-03-24 16:29:24 -050079 * -- all CBs referencing this mem obj
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060080 * -- all VK Objects that are bound to this memory
Mark Lobodzinski6434eff2015-03-31 16:05:35 -050081 * objectMap -- map of objects to MT_OBJ_INFO structures
Tobin Ehlis8be20fd2015-01-07 17:49:29 -070082 *
83 * Algorithm overview
84 * These are the primary events that should happen related to different objects
85 * 1. Command buffers
Mark Lobodzinskib6ddb462015-03-24 16:29:24 -050086 * CREATION - Add object,structure to map
87 * CMD BIND - If mem associated, add mem reference to list container
88 * DESTROY - Remove from map, decrement (and report) mem references
Tobin Ehlis8be20fd2015-01-07 17:49:29 -070089 * 2. Mem Objects
Mark Lobodzinskib6ddb462015-03-24 16:29:24 -050090 * CREATION - Add object,structure to map
91 * OBJ BIND - Add obj structure to list container for that mem node
92 * CMB BIND - If mem-related add CB structure to list container for that mem node
93 * DESTROY - Flag as errors any remaining refs and remove from map
Tobin Ehlis8be20fd2015-01-07 17:49:29 -070094 * 3. Generic Objects
Mark Lobodzinskib6ddb462015-03-24 16:29:24 -050095 * MEM BIND - DESTROY any previous binding, Add obj node w/ ref to map, add obj ref to list container for that mem node
Mark Lobodzinski6434eff2015-03-31 16:05:35 -050096 * DESTROY - If mem bound, remove reference list container for that memInfo, remove object ref from map
Tobin Ehlis8be20fd2015-01-07 17:49:29 -070097 */
98// TODO : Is there a way to track when Cmd Buffer finishes & remove mem references at that point?
99// TODO : Could potentially store a list of freed mem allocs to flag when they're incorrectly used
100
Tobin Ehlis257d9742015-07-08 17:08:02 -0600101// Simple struct to hold handle and type of object so they can be uniquely identified and looked up in appropriate map
102struct MT_OBJ_HANDLE_TYPE {
103 uint64_t handle;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700104 VkDebugReportObjectTypeEXT type;
Tobin Ehlis257d9742015-07-08 17:08:02 -0600105};
106
Tobin Ehlis8be20fd2015-01-07 17:49:29 -0700107// Data struct for tracking memory object
Mark Lobodzinski6434eff2015-03-31 16:05:35 -0500108struct MT_MEM_OBJ_INFO {
Michael Lentine685f61c2015-10-29 10:41:47 -0700109 void* object; // Dispatchable object used to create this memory (device of swapchain)
110 uint32_t refCount; // Count of references (obj bindings or CB use)
Michael Lentine80d51872015-11-24 17:55:33 -0600111 bool valid; // Stores if the memory has valid data or not
Mark Lobodzinskib1567a02015-04-21 15:33:04 -0600112 VkDeviceMemory mem;
Michael Lentine685f61c2015-10-29 10:41:47 -0700113 VkMemoryAllocateInfo allocInfo;
114 list<MT_OBJ_HANDLE_TYPE> pObjBindings; // list container of objects bound to this memory
115 list<VkCommandBuffer> pCommandBufferBindings; // list container of cmd buffers that reference this mem object
Michael Lentine685f61c2015-10-29 10:41:47 -0700116 MemRange memRange;
Mark Lobodzinskif4945f82015-11-13 13:47:01 -0700117 void *pData, *pDriverData;
Mark Lobodzinskib6ddb462015-03-24 16:29:24 -0500118};
Tobin Ehlis8be20fd2015-01-07 17:49:29 -0700119
Tobin Ehlis257d9742015-07-08 17:08:02 -0600120// This only applies to Buffers and Images, which can have memory bound to them
121struct MT_OBJ_BINDING_INFO {
122 VkDeviceMemory mem;
Michael Lentine80d51872015-11-24 17:55:33 -0600123 bool valid; //If this is a swapchain image backing memory is not a MT_MEM_OBJ_INFO so store it here.
Chris Forbesb4740d92015-07-11 16:06:23 +1200124 union create_info {
Tobin Ehlis257d9742015-07-08 17:08:02 -0600125 VkImageCreateInfo image;
126 VkBufferCreateInfo buffer;
Tobin Ehlis8be20fd2015-01-07 17:49:29 -0700127 } create_info;
Mark Lobodzinskib6ddb462015-03-24 16:29:24 -0500128};
Tobin Ehlisc145be82015-01-08 15:22:32 -0700129
Mark Lobodzinskib6ddb462015-03-24 16:29:24 -0500130// Track all command buffers
Mark Lobodzinskiba442c82015-10-29 15:45:27 -0600131typedef struct _MT_CB_INFO {
Mark Lobodzinskif4945f82015-11-13 13:47:01 -0700132 VkCommandBufferAllocateInfo createInfo;
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800133 VkPipeline pipelines[VK_PIPELINE_BIND_POINT_RANGE_SIZE];
Chia-I Wu08accc62015-07-07 11:50:03 +0800134 uint32_t attachmentCount;
Mark Lobodzinskif4945f82015-11-13 13:47:01 -0700135 VkCommandBuffer commandBuffer;
Mark Lobodzinskib1567a02015-04-21 15:33:04 -0600136 uint64_t fenceId;
Mike Stroyan950496e2015-05-19 15:16:08 -0600137 VkFence lastSubmittedFence;
138 VkQueue lastSubmittedQueue;
Michael Lentine80d51872015-11-24 17:55:33 -0600139 VkRenderPass pass;
Mark Lobodzinskib6ddb462015-03-24 16:29:24 -0500140 // Order dependent, stl containers must be at end of struct
Mark Lobodzinskib1567a02015-04-21 15:33:04 -0600141 list<VkDeviceMemory> pMemObjList; // List container of Mem objs referenced by this CB
Mark Lobodzinskiba442c82015-10-29 15:45:27 -0600142 // Constructor
143 _MT_CB_INFO():createInfo{},pipelines{},attachmentCount(0),fenceId(0),lastSubmittedFence{},lastSubmittedQueue{} {};
144} MT_CB_INFO;
Mark Lobodzinskie61ebe72015-03-17 10:53:12 -0500145
Mark Lobodzinskif4945f82015-11-13 13:47:01 -0700146// Track command pools and their command buffers
147typedef struct _MT_CMD_POOL_INFO {
148 VkCommandPoolCreateFlags createFlags;
149 list<VkCommandBuffer> pCommandBuffers; // list container of cmd buffers allocated from this pool
150} MT_CMD_POOL_INFO;
151
Michael Lentine80d51872015-11-24 17:55:33 -0600152struct MT_IMAGE_VIEW_INFO {
153 VkImage image;
154};
155
156struct MT_FB_ATTACHMENT_INFO {
157 VkImage image;
158 VkDeviceMemory mem;
159};
160
161struct MT_FB_INFO {
162 std::vector<MT_FB_ATTACHMENT_INFO> attachments;
163};
164
165struct MT_PASS_ATTACHMENT_INFO {
Michael Lentineb4979492015-12-22 11:36:14 -0600166 uint32_t attachment;
Michael Lentine80d51872015-11-24 17:55:33 -0600167 VkAttachmentLoadOp load_op;
Michael Lentineb4979492015-12-22 11:36:14 -0600168 VkAttachmentStoreOp store_op;
Michael Lentine80d51872015-11-24 17:55:33 -0600169};
170
171struct MT_PASS_INFO {
172 VkFramebuffer fb;
173 std::vector<MT_PASS_ATTACHMENT_INFO> attachments;
Michael Lentineb4979492015-12-22 11:36:14 -0600174 std::unordered_map<uint32_t, bool> attachment_first_read;
Michael Lentine80d51872015-11-24 17:55:33 -0600175};
176
Mark Lobodzinski223ca202015-04-02 08:52:53 -0500177// Associate fenceId with a fence object
Mark Lobodzinski6434eff2015-03-31 16:05:35 -0500178struct MT_FENCE_INFO {
Mark Lobodzinskif4945f82015-11-13 13:47:01 -0700179 uint64_t fenceId; // Sequence number for fence at last submit
180 VkQueue queue; // Queue that this fence is submitted against or NULL
Tobin Ehlis257d9742015-07-08 17:08:02 -0600181 VkFenceCreateInfo createInfo;
Mark Lobodzinskib6ddb462015-03-24 16:29:24 -0500182};
Mark Lobodzinskie61ebe72015-03-17 10:53:12 -0500183
Mark Lobodzinski223ca202015-04-02 08:52:53 -0500184// Track Queue information
185struct MT_QUEUE_INFO {
Mark Lobodzinskib1567a02015-04-21 15:33:04 -0600186 uint64_t lastRetiredId;
187 uint64_t lastSubmittedId;
Mark Lobodzinskif4945f82015-11-13 13:47:01 -0700188 list<VkCommandBuffer> pQueueCommandBuffers;
Mark Lobodzinskib1567a02015-04-21 15:33:04 -0600189 list<VkDeviceMemory> pMemRefList;
Mark Lobodzinski223ca202015-04-02 08:52:53 -0500190};
191
Mark Lobodzinskib9644ee2015-10-08 10:44:07 -0600192// Track Swapchain Information
Chia-I Wuf8693382015-04-16 22:02:10 +0800193struct MT_SWAP_CHAIN_INFO {
Mark Lobodzinskib9644ee2015-10-08 10:44:07 -0600194 VkSwapchainCreateInfoKHR createInfo;
195 std::vector<VkImage> images;
Chia-I Wuf8693382015-04-16 22:02:10 +0800196};
197
Michael Lentine85bb2ef2015-12-22 17:30:09 -0600198struct MEMORY_RANGE {
199 uint64_t handle;
200 VkDeviceMemory memory;
201 VkDeviceSize start;
202 VkDeviceSize end;
203};
204
Mark Lobodzinskib6ddb462015-03-24 16:29:24 -0500205#ifdef __cplusplus
206}
207#endif