blob: 31e728a5d6e0a05b91cefebadbd9ecad015c816c [file] [log] [blame]
Tobin Ehlis62086412014-11-19 16:19:28 -07001/*
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002 * Vulkan
Tobin Ehlis62086412014-11-19 16:19:28 -07003 *
Mark Lobodzinski283a4c22015-03-24 16:29:24 -05004 * Copyright (C) 2015 LunarG, Inc.
Tobin Ehlis62086412014-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.
23 */
Mark Lobodzinski283a4c22015-03-24 16:29:24 -050024#pragma once
Chia-I Wu5b66aa52015-04-16 22:02:10 +080025#include <vector>
Tobin Ehlis2d1d9702015-07-03 09:42:57 -060026#include "vk_layer.h"
Tobin Ehlis92f12cd2015-07-08 17:08:02 -060027#include "layer_common.h"
Mark Lobodzinski283a4c22015-03-24 16:29:24 -050028
29#ifdef __cplusplus
30extern "C" {
31#endif
32
Tobin Ehlis62086412014-11-19 16:19:28 -070033// Mem Tracker ERROR codes
34typedef enum _MEM_TRACK_ERROR
35{
Mark Lobodzinskic66c6712015-06-05 13:59:04 -050036 MEMTRACK_NONE, // Used for INFO & other non-error messages
37 MEMTRACK_INVALID_CB, // Cmd Buffer invalid
38 MEMTRACK_INVALID_MEM_OBJ, // Invalid Memory Object
39 MEMTRACK_INTERNAL_ERROR, // Bug in Mem Track Layer internal data structures
40 MEMTRACK_FREED_MEM_REF, // MEM Obj freed while it still has obj and/or CB refs
41 MEMTRACK_MEM_OBJ_CLEAR_EMPTY_BINDINGS, // Clearing bindings on mem obj that doesn't have any bindings
42 MEMTRACK_MISSING_MEM_BINDINGS, // Trying to retrieve mem bindings, but none found (may be internal error)
43 MEMTRACK_INVALID_OBJECT, // Attempting to reference generic VK Object that is invalid
44 MEMTRACK_DESTROY_OBJECT_ERROR, // Destroying an object that has a memory reference
45 MEMTRACK_MEMORY_BINDING_ERROR, // Error during one of many calls that bind memory to object or CB
46 MEMTRACK_MEMORY_LEAK, // Failure to call vkFreeMemory on Mem Obj prior to DestroyDevice
47 MEMTRACK_INVALID_STATE, // Memory not in the correct state
48 MEMTRACK_RESET_CB_WHILE_IN_FLIGHT, // vkResetCommandBuffer() called on a CB that hasn't completed
49 MEMTRACK_INVALID_FENCE_STATE, // Invalid Fence State signaled or used
50 MEMTRACK_REBIND_OBJECT, // Non-sparse object bindings are immutable
Tobin Ehlisd94ba722015-07-03 08:45:14 -060051 MEMTRACK_INVALID_USAGE_FLAG, // Usage flags specified at image/buffer create conflict w/ use of object
Tobin Ehlis62086412014-11-19 16:19:28 -070052} MEM_TRACK_ERROR;
53
Tobin Ehlis6aa77422015-01-07 17:49:29 -070054/*
55 * Data Structure overview
Courtney Goeltzenleuchterdfd1b2a2015-04-15 00:14:36 -060056 * There are 4 global STL(' maps
Mark Lobodzinski7a428ce2015-03-31 16:05:35 -050057 * cbMap -- map of command Buffer (CB) objects to MT_CB_INFO structures
58 * Each MT_CB_INFO struct has an stl list container with
Tobin Ehlis6aa77422015-01-07 17:49:29 -070059 * memory objects that are referenced by this CB
Mark Lobodzinski7a428ce2015-03-31 16:05:35 -050060 * memObjMap -- map of Memory Objects to MT_MEM_OBJ_INFO structures
61 * Each MT_MEM_OBJ_INFO has two stl list containers with:
Mark Lobodzinski283a4c22015-03-24 16:29:24 -050062 * -- all CBs referencing this mem obj
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060063 * -- all VK Objects that are bound to this memory
Mark Lobodzinski7a428ce2015-03-31 16:05:35 -050064 * objectMap -- map of objects to MT_OBJ_INFO structures
Tobin Ehlis6aa77422015-01-07 17:49:29 -070065 *
66 * Algorithm overview
67 * These are the primary events that should happen related to different objects
68 * 1. Command buffers
Mark Lobodzinski283a4c22015-03-24 16:29:24 -050069 * CREATION - Add object,structure to map
70 * CMD BIND - If mem associated, add mem reference to list container
71 * DESTROY - Remove from map, decrement (and report) mem references
Tobin Ehlis6aa77422015-01-07 17:49:29 -070072 * 2. Mem Objects
Mark Lobodzinski283a4c22015-03-24 16:29:24 -050073 * CREATION - Add object,structure to map
74 * OBJ BIND - Add obj structure to list container for that mem node
75 * CMB BIND - If mem-related add CB structure to list container for that mem node
76 * DESTROY - Flag as errors any remaining refs and remove from map
Tobin Ehlis6aa77422015-01-07 17:49:29 -070077 * 3. Generic Objects
Mark Lobodzinski283a4c22015-03-24 16:29:24 -050078 * 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 -050079 * DESTROY - If mem bound, remove reference list container for that memInfo, remove object ref from map
Tobin Ehlis6aa77422015-01-07 17:49:29 -070080 */
81// TODO : Is there a way to track when Cmd Buffer finishes & remove mem references at that point?
82// TODO : Could potentially store a list of freed mem allocs to flag when they're incorrectly used
83
Tobin Ehlis92f12cd2015-07-08 17:08:02 -060084// Simple struct to hold handle and type of object so they can be uniquely identified and looked up in appropriate map
85struct MT_OBJ_HANDLE_TYPE {
86 uint64_t handle;
87 VkDbgObjectType type;
88};
89
Tobin Ehlis6aa77422015-01-07 17:49:29 -070090// Data struct for tracking memory object
Mark Lobodzinski7a428ce2015-03-31 16:05:35 -050091struct MT_MEM_OBJ_INFO {
Mark Lobodzinskid0273662015-08-10 14:37:52 -060092 void* object; // Dispatchable object used to create this memory (device of swapchain)
Mark Lobodzinskia908b162015-04-21 15:33:04 -060093 uint32_t refCount; // Count of references (obj bindings or CB use)
94 VkDeviceMemory mem;
95 VkMemoryAllocInfo allocInfo;
Tobin Ehlis92f12cd2015-07-08 17:08:02 -060096 list<MT_OBJ_HANDLE_TYPE> pObjBindings; // list container of objects bound to this memory
Mark Lobodzinskia908b162015-04-21 15:33:04 -060097 list<VkCmdBuffer> pCmdBufferBindings; // list container of cmd buffers that reference this mem object
Mark Lobodzinski283a4c22015-03-24 16:29:24 -050098};
Tobin Ehlis6aa77422015-01-07 17:49:29 -070099
Tobin Ehlis92f12cd2015-07-08 17:08:02 -0600100// This only applies to Buffers and Images, which can have memory bound to them
101struct MT_OBJ_BINDING_INFO {
102 VkDeviceMemory mem;
Chris Forbesa0a71b62015-07-11 16:06:23 +1200103 union create_info {
Tobin Ehlis92f12cd2015-07-08 17:08:02 -0600104 VkImageCreateInfo image;
105 VkBufferCreateInfo buffer;
Tony Barbour7910de72015-07-13 16:37:21 -0600106 // VkSwapChainCreateInfoWSI swapchain;
Chris Forbesa0a71b62015-07-11 16:06:23 +1200107
Tony Barbour7910de72015-07-13 16:37:21 -0600108 // create_info() : image() {}
Tobin Ehlis6aa77422015-01-07 17:49:29 -0700109 } create_info;
Mark Lobodzinski283a4c22015-03-24 16:29:24 -0500110};
Tobin Ehlis2836a7d2015-01-08 15:22:32 -0700111
Mark Lobodzinski283a4c22015-03-24 16:29:24 -0500112// Track all command buffers
Mark Lobodzinski7a428ce2015-03-31 16:05:35 -0500113struct MT_CB_INFO {
Mark Lobodzinskia908b162015-04-21 15:33:04 -0600114 VkCmdBufferCreateInfo createInfo;
Tobin Ehlis92f12cd2015-07-08 17:08:02 -0600115 uint64_t pLastBoundDynamicState[VK_NUM_STATE_BIND_POINT];
Courtney Goeltzenleuchtera7281c22015-07-12 15:42:02 -0600116 VkPipeline pipelines[VK_PIPELINE_BIND_POINT_NUM];
Chia-I Wuc278df82015-07-07 11:50:03 +0800117 uint32_t attachmentCount;
Mark Lobodzinskia908b162015-04-21 15:33:04 -0600118 VkCmdBuffer cmdBuffer;
119 uint64_t fenceId;
Mike Stroyan53430332015-05-19 15:16:08 -0600120 VkFence lastSubmittedFence;
121 VkQueue lastSubmittedQueue;
Mark Lobodzinski283a4c22015-03-24 16:29:24 -0500122 // Order dependent, stl containers must be at end of struct
Mark Lobodzinskia908b162015-04-21 15:33:04 -0600123 list<VkDeviceMemory> pMemObjList; // List container of Mem objs referenced by this CB
Mark Lobodzinski283a4c22015-03-24 16:29:24 -0500124};
Mark Lobodzinski4aad3642015-03-17 10:53:12 -0500125
Mark Lobodzinski85a83982015-04-02 08:52:53 -0500126// Associate fenceId with a fence object
Mark Lobodzinski7a428ce2015-03-31 16:05:35 -0500127struct MT_FENCE_INFO {
Mike Stroyan53430332015-05-19 15:16:08 -0600128 uint64_t fenceId; // Sequence number for fence at last submit
129 VkQueue queue; // Queue that this fence is submitted against or NULL
Tobin Ehlis92f12cd2015-07-08 17:08:02 -0600130 VkFenceCreateInfo createInfo;
Mark Lobodzinski283a4c22015-03-24 16:29:24 -0500131};
Mark Lobodzinski4aad3642015-03-17 10:53:12 -0500132
Mark Lobodzinski85a83982015-04-02 08:52:53 -0500133// Track Queue information
134struct MT_QUEUE_INFO {
Mark Lobodzinskia908b162015-04-21 15:33:04 -0600135 uint64_t lastRetiredId;
136 uint64_t lastSubmittedId;
137 list<VkCmdBuffer> pQueueCmdBuffers;
138 list<VkDeviceMemory> pMemRefList;
Mark Lobodzinski85a83982015-04-02 08:52:53 -0500139};
140
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800141struct MT_SWAP_CHAIN_INFO {
Mark Lobodzinskia908b162015-04-21 15:33:04 -0600142 VkSwapChainCreateInfoWSI createInfo;
Ian Elliottccac0232015-08-07 14:11:14 -0600143 std::vector<VkImage> images;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800144};
145
Mark Lobodzinski283a4c22015-03-24 16:29:24 -0500146#ifdef __cplusplus
147}
148#endif