blob: 9bb76cdbf31eb4fe32f240250ab1374d33f1452e [file] [log] [blame]
Tobin Ehlis62086412014-11-19 16:19:28 -07001/*
Tobin Ehlis62086412014-11-19 16:19:28 -07002 *
Courtney Goeltzenleuchter8a17da52015-10-29 13:50:34 -06003 * Copyright (C) 2015 Valve Corporation
Tobin Ehlis62086412014-11-19 16:19:28 -07004 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
Mark Lobodzinski283a4c22015-03-24 16:29:24 -050023#pragma once
Chia-I Wu5b66aa52015-04-16 22:02:10 +080024#include <vector>
Tobin Ehlis2d1d9702015-07-03 09:42:57 -060025#include "vk_layer.h"
Courtney Goeltzenleuchterd4d26dd2015-09-23 12:30:48 -060026#include "vk_debug_report_lunarg.h"
Mark Lobodzinski283a4c22015-03-24 16:29:24 -050027
28#ifdef __cplusplus
29extern "C" {
30#endif
31
Tobin Ehlis62086412014-11-19 16:19:28 -070032// Mem Tracker ERROR codes
33typedef enum _MEM_TRACK_ERROR
34{
Mark Lobodzinskic66c6712015-06-05 13:59:04 -050035 MEMTRACK_NONE, // Used for INFO & other non-error messages
36 MEMTRACK_INVALID_CB, // Cmd Buffer invalid
37 MEMTRACK_INVALID_MEM_OBJ, // Invalid Memory Object
38 MEMTRACK_INTERNAL_ERROR, // Bug in Mem Track Layer internal data structures
39 MEMTRACK_FREED_MEM_REF, // MEM Obj freed while it still has obj and/or CB refs
40 MEMTRACK_MEM_OBJ_CLEAR_EMPTY_BINDINGS, // Clearing bindings on mem obj that doesn't have any bindings
41 MEMTRACK_MISSING_MEM_BINDINGS, // Trying to retrieve mem bindings, but none found (may be internal error)
42 MEMTRACK_INVALID_OBJECT, // Attempting to reference generic VK Object that is invalid
Mark Lobodzinskic66c6712015-06-05 13:59:04 -050043 MEMTRACK_MEMORY_BINDING_ERROR, // Error during one of many calls that bind memory to object or CB
44 MEMTRACK_MEMORY_LEAK, // Failure to call vkFreeMemory on Mem Obj prior to DestroyDevice
45 MEMTRACK_INVALID_STATE, // Memory not in the correct state
46 MEMTRACK_RESET_CB_WHILE_IN_FLIGHT, // vkResetCommandBuffer() called on a CB that hasn't completed
47 MEMTRACK_INVALID_FENCE_STATE, // Invalid Fence State signaled or used
48 MEMTRACK_REBIND_OBJECT, // Non-sparse object bindings are immutable
Tobin Ehlisd94ba722015-07-03 08:45:14 -060049 MEMTRACK_INVALID_USAGE_FLAG, // Usage flags specified at image/buffer create conflict w/ use of object
Michael Lentine4fbbaa32015-10-28 16:26:14 -070050 MEMTRACK_INVALID_MAP, // Size flag specified at alloc is too small for mapping range
Tobin Ehlis62086412014-11-19 16:19:28 -070051} MEM_TRACK_ERROR;
52
Mark Lobodzinski2a253072015-10-08 10:44:07 -060053// MemTracker Semaphore states
54typedef enum _MtSemaphoreState
55{
56 MEMTRACK_SEMAPHORE_STATE_UNSET, // Semaphore is in an undefined state
57 MEMTRACK_SEMAPHORE_STATE_SIGNALLED, // Semaphore has is in signalled state
58 MEMTRACK_SEMAPHORE_STATE_WAIT, // Semaphore is in wait state
59} MtSemaphoreState;
60
Tobin Ehlis6aa77422015-01-07 17:49:29 -070061/*
62 * Data Structure overview
Courtney Goeltzenleuchterdfd1b2a2015-04-15 00:14:36 -060063 * There are 4 global STL(' maps
Mark Lobodzinski7a428ce2015-03-31 16:05:35 -050064 * cbMap -- map of command Buffer (CB) objects to MT_CB_INFO structures
65 * Each MT_CB_INFO struct has an stl list container with
Tobin Ehlis6aa77422015-01-07 17:49:29 -070066 * memory objects that are referenced by this CB
Mark Lobodzinski7a428ce2015-03-31 16:05:35 -050067 * memObjMap -- map of Memory Objects to MT_MEM_OBJ_INFO structures
68 * Each MT_MEM_OBJ_INFO has two stl list containers with:
Mark Lobodzinski283a4c22015-03-24 16:29:24 -050069 * -- all CBs referencing this mem obj
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060070 * -- all VK Objects that are bound to this memory
Mark Lobodzinski7a428ce2015-03-31 16:05:35 -050071 * objectMap -- map of objects to MT_OBJ_INFO structures
Tobin Ehlis6aa77422015-01-07 17:49:29 -070072 *
73 * Algorithm overview
74 * These are the primary events that should happen related to different objects
75 * 1. Command buffers
Mark Lobodzinski283a4c22015-03-24 16:29:24 -050076 * CREATION - Add object,structure to map
77 * CMD BIND - If mem associated, add mem reference to list container
78 * DESTROY - Remove from map, decrement (and report) mem references
Tobin Ehlis6aa77422015-01-07 17:49:29 -070079 * 2. Mem Objects
Mark Lobodzinski283a4c22015-03-24 16:29:24 -050080 * CREATION - Add object,structure to map
81 * OBJ BIND - Add obj structure to list container for that mem node
82 * CMB BIND - If mem-related add CB structure to list container for that mem node
83 * DESTROY - Flag as errors any remaining refs and remove from map
Tobin Ehlis6aa77422015-01-07 17:49:29 -070084 * 3. Generic Objects
Mark Lobodzinski283a4c22015-03-24 16:29:24 -050085 * MEM BIND - DESTROY any previous binding, Add obj node w/ ref to map, add obj ref to list container for that mem node
Mark Lobodzinski7a428ce2015-03-31 16:05:35 -050086 * DESTROY - If mem bound, remove reference list container for that memInfo, remove object ref from map
Tobin Ehlis6aa77422015-01-07 17:49:29 -070087 */
88// TODO : Is there a way to track when Cmd Buffer finishes & remove mem references at that point?
89// TODO : Could potentially store a list of freed mem allocs to flag when they're incorrectly used
90
Tobin Ehlis92f12cd2015-07-08 17:08:02 -060091// Simple struct to hold handle and type of object so they can be uniquely identified and looked up in appropriate map
92struct MT_OBJ_HANDLE_TYPE {
93 uint64_t handle;
94 VkDbgObjectType type;
95};
96
Tobin Ehlis6aa77422015-01-07 17:49:29 -070097// Data struct for tracking memory object
Mark Lobodzinski7a428ce2015-03-31 16:05:35 -050098struct MT_MEM_OBJ_INFO {
Mark Lobodzinskid0273662015-08-10 14:37:52 -060099 void* object; // Dispatchable object used to create this memory (device of swapchain)
Mark Lobodzinskia908b162015-04-21 15:33:04 -0600100 uint32_t refCount; // Count of references (obj bindings or CB use)
101 VkDeviceMemory mem;
Chia-I Wu1f851912015-10-27 18:04:07 +0800102 VkMemoryAllocateInfo allocInfo;
Tobin Ehlis92f12cd2015-07-08 17:08:02 -0600103 list<MT_OBJ_HANDLE_TYPE> pObjBindings; // list container of objects bound to this memory
Chia-I Wu1f851912015-10-27 18:04:07 +0800104 list<VkCommandBuffer> pCommandBufferBindings; // list container of cmd buffers that reference this mem object
Mark Lobodzinski283a4c22015-03-24 16:29:24 -0500105};
Tobin Ehlis6aa77422015-01-07 17:49:29 -0700106
Tobin Ehlis92f12cd2015-07-08 17:08:02 -0600107// This only applies to Buffers and Images, which can have memory bound to them
108struct MT_OBJ_BINDING_INFO {
109 VkDeviceMemory mem;
Chris Forbesa0a71b62015-07-11 16:06:23 +1200110 union create_info {
Tobin Ehlis92f12cd2015-07-08 17:08:02 -0600111 VkImageCreateInfo image;
112 VkBufferCreateInfo buffer;
Tobin Ehlis6aa77422015-01-07 17:49:29 -0700113 } create_info;
Mark Lobodzinski283a4c22015-03-24 16:29:24 -0500114};
Tobin Ehlis2836a7d2015-01-08 15:22:32 -0700115
Mark Lobodzinski283a4c22015-03-24 16:29:24 -0500116// Track all command buffers
Mark Lobodzinski820d8622015-10-29 15:45:27 -0600117typedef struct _MT_CB_INFO {
Chia-I Wu1f851912015-10-27 18:04:07 +0800118 VkCommandBufferAllocateInfo createInfo;
119 VkPipeline pipelines[VK_PIPELINE_BIND_POINT_RANGE_SIZE];
Chia-I Wuc278df82015-07-07 11:50:03 +0800120 uint32_t attachmentCount;
Chia-I Wu1f851912015-10-27 18:04:07 +0800121 VkCommandBuffer commandBuffer;
Mark Lobodzinskia908b162015-04-21 15:33:04 -0600122 uint64_t fenceId;
Mike Stroyan53430332015-05-19 15:16:08 -0600123 VkFence lastSubmittedFence;
124 VkQueue lastSubmittedQueue;
Mark Lobodzinski283a4c22015-03-24 16:29:24 -0500125 // Order dependent, stl containers must be at end of struct
Mark Lobodzinskia908b162015-04-21 15:33:04 -0600126 list<VkDeviceMemory> pMemObjList; // List container of Mem objs referenced by this CB
Mark Lobodzinski820d8622015-10-29 15:45:27 -0600127 // Constructor
128 _MT_CB_INFO():createInfo{},pipelines{},attachmentCount(0),fenceId(0),lastSubmittedFence{},lastSubmittedQueue{} {};
129} MT_CB_INFO;
Mark Lobodzinski4aad3642015-03-17 10:53:12 -0500130
Mark Lobodzinski85a83982015-04-02 08:52:53 -0500131// Associate fenceId with a fence object
Mark Lobodzinski7a428ce2015-03-31 16:05:35 -0500132struct MT_FENCE_INFO {
Mike Stroyan53430332015-05-19 15:16:08 -0600133 uint64_t fenceId; // Sequence number for fence at last submit
134 VkQueue queue; // Queue that this fence is submitted against or NULL
Tobin Ehlis92f12cd2015-07-08 17:08:02 -0600135 VkFenceCreateInfo createInfo;
Mark Lobodzinski283a4c22015-03-24 16:29:24 -0500136};
Mark Lobodzinski4aad3642015-03-17 10:53:12 -0500137
Mark Lobodzinski85a83982015-04-02 08:52:53 -0500138// Track Queue information
139struct MT_QUEUE_INFO {
Mark Lobodzinskia908b162015-04-21 15:33:04 -0600140 uint64_t lastRetiredId;
141 uint64_t lastSubmittedId;
Chia-I Wu1f851912015-10-27 18:04:07 +0800142 list<VkCommandBuffer> pQueueCommandBuffers;
Mark Lobodzinskia908b162015-04-21 15:33:04 -0600143 list<VkDeviceMemory> pMemRefList;
Mark Lobodzinski85a83982015-04-02 08:52:53 -0500144};
145
Mark Lobodzinski2a253072015-10-08 10:44:07 -0600146// Track Swapchain Information
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800147struct MT_SWAP_CHAIN_INFO {
Mark Lobodzinski2a253072015-10-08 10:44:07 -0600148 VkSwapchainCreateInfoKHR createInfo;
149 std::vector<VkImage> images;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800150};
151
Mark Lobodzinski283a4c22015-03-24 16:29:24 -0500152#ifdef __cplusplus
153}
154#endif