blob: 7196b9db548638e3ddd8de2de05f8925cb29c693 [file] [log] [blame]
Tobin Ehlis62086412014-11-19 16:19:28 -07001/*
2 * XGL
3 *
4 * Copyright (C) 2014 LunarG, Inc.
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.
23 */
24#include "xglLayer.h"
25// Mem Tracker ERROR codes
26typedef enum _MEM_TRACK_ERROR
27{
28 MEMTRACK_NONE = 0, // Used for INFO & other non-error messages
29 MEMTRACK_INVALID_CB = 1, // Cmd Buffer invalid
30 MEMTRACK_CB_MISSING_MEM_REF = 2, // pMemRefs for CB is missing a mem ref
31 MEMTRACK_INVALID_MEM_OBJ = 3, // Invalid Memory Object
32 MEMTRACK_INTERNAL_ERROR = 4, // Bug in Mem Track Layer internal data structures
33 MEMTRACK_CB_MISSING_FENCE = 5, // Cmd Buffer does not have fence
34 MEMTRACK_FREED_MEM_REF = 6, // MEM Obj freed while it still has obj and/or CB refs
35 MEMTRACK_MEM_OBJ_CLEAR_EMPTY_BINDINGS = 7, // Clearing bindings on mem obj that doesn't have any bindings
36 MEMTRACK_MISSING_MEM_BINDINGS = 8, // Trying to retrieve mem bindings, but none found (may be internal error)
37 MEMTRACK_INVALID_OBJECT = 9, // Attempting to reference generic XGL Object that is invalid
38 MEMTRACK_FREE_MEM_ERROR = 10, // Error while calling xglFreeMemory
39 MEMTRACK_DESTROY_OBJECT_ERROR = 11, // Destroying an object that has a memory reference
40 MEMTRACK_MEMORY_BINDING_ERROR = 12, // Error during one of many calls that bind memory to object or CB
41 MEMTRACK_OUT_OF_MEMORY_ERROR = 13, // malloc failed
Tobin Ehlis22d03232014-11-25 18:01:12 -070042 MEMTRACK_MEMORY_LEAK = 14, // Failure to call xglFreeMemory on Mem Obj prior to DestroyDevice
Tobin Ehlis6aa77422015-01-07 17:49:29 -070043 MEMTRACK_INVALID_STATE = 15, // Failure to call xglFreeMemory on Mem Obj prior to DestroyDevice
Tobin Ehlis62086412014-11-19 16:19:28 -070044} MEM_TRACK_ERROR;
45
Tobin Ehlis6aa77422015-01-07 17:49:29 -070046/*
47 * Data Structure overview
48 * There are 3 global Linked-Lists (LLs)
49 * pGlobalCBHead points to head of Command Buffer (CB) LL
50 * Off of each node in this LL there is a separate LL of
51 * memory objects that are referenced by this CB
52 * pGlobalMemObjHead points to head of Memory Object LL
53 * Off of each node in this LL there are 2 separate LL
54 * One is a list of all CBs referencing this mem obj
55 * Two is a list of all XGL Objects that are bound to this memory
56 * pGlobalObjHead point to head of XGL Objects w/ bound mem LL
57 * Each node of this LL contains a ptr to global Mem Obj node for bound mem
58 *
59 * The "Global" nodes are for the main LLs
60 * The "mini" nodes are for ancillary LLs that are pointed to from global nodes
61 *
62 * Algorithm overview
63 * These are the primary events that should happen related to different objects
64 * 1. Command buffers
65 * CREATION - Add node to global LL
66 * CMD BIND - If mem associated, add mem reference to mini LL
67 * DESTROY - Remove from global LL, decrement (and report) mem references
68 * 2. Mem Objects
69 * CREATION - Add node to global LL
70 * OBJ BIND - Add obj node to mini LL for that mem node
71 * CMB BIND - If mem-related add CB node to mini LL for that mem node
72 * DESTROY - Flag as errors any remaining refs and Remove from global LL
73 * 3. Generic Objects
74 * MEM BIND - DESTROY any previous binding, Add global obj node w/ ref to global mem obj node, Add obj node to mini LL for that mem node
75 * DESTROY - If mem bound, remove reference from mini LL for that mem Node, remove global obj node
76 */
77// TODO : Is there a way to track when Cmd Buffer finishes & remove mem references at that point?
78// TODO : Could potentially store a list of freed mem allocs to flag when they're incorrectly used
79
80// Generic data struct for various "mini" Linked-Lists
81// This just wraps some type of XGL OBJ and a pNext ptr
82// Used for xgl obj, cmd buffer, and mem obj wrapping
83typedef struct _MINI_NODE {
84 struct _MINI_NODE* pNext;
85 union { // different objects that can be wrapped
86 XGL_OBJECT object;
87 XGL_GPU_MEMORY mem;
88 XGL_CMD_BUFFER cmdBuffer;
89 XGL_BASE_OBJECT data; // for generic handling of data
90 };
91} MINI_NODE;
92
93struct GLOBAL_MEM_OBJ_NODE;
Tobin Ehlis2836a7d2015-01-08 15:22:32 -070094// Store a linked-list of transition nodes to account for different states across a single mem obj
95typedef struct _MEM_STATE_TRANSITION_NODE {
96 struct _MEM_STATE_TRANSITION_NODE* pNext;
97 uint32_t numRegions; // Allocation may be broken into various regions
98 uint32_t isMem; // 1 for memory, 0 for image
99 union {
100 XGL_MEMORY_STATE_TRANSITION memory;
101 XGL_IMAGE_STATE_TRANSITION image; // use when img attached to this mem obj
102 } transition;
103} MEM_STATE_TRANSITION_NODE;
Tobin Ehlis6aa77422015-01-07 17:49:29 -0700104
105// Data struct for tracking memory object
106typedef struct _GLOBAL_MEM_OBJ_NODE {
107 struct _GLOBAL_MEM_OBJ_NODE* pNextGlobalNode; // Ptr to next mem obj in global list of all objs
108 MINI_NODE* pObjBindings; // Ptr to list of objects bound to this memory
109 MINI_NODE* pCmdBufferBindings; // Ptr to list of cmd buffers that this mem object references
110 XGL_UINT refCount; // Count of references (obj bindings or CB use)
111 XGL_GPU_MEMORY mem;
112 XGL_MEMORY_ALLOC_INFO allocInfo;
Tobin Ehlis2836a7d2015-01-08 15:22:32 -0700113 MEM_STATE_TRANSITION_NODE* pTransitions; // LL of transitions for this Mem Obj
Tobin Ehlis6aa77422015-01-07 17:49:29 -0700114} GLOBAL_MEM_OBJ_NODE;
115
116typedef struct _GLOBAL_OBJECT_NODE {
117 struct _GLOBAL_OBJECT_NODE* pNext;
118 GLOBAL_MEM_OBJ_NODE* pMemNode;
119 XGL_OBJECT object;
120 XGL_STRUCTURE_TYPE sType;
121 int ref_count;
122 // Capture all object types that may have memory bound. From prog guide:
123 // The only objects that are guaranteed to have no external memory
124 // requirements are devices, queues, command buffers, shaders and memory objects.
125 union {
126 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO color_attachment_view_create_info;
127 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO ds_view_create_info;
128 XGL_IMAGE_VIEW_CREATE_INFO image_view_create_info;
129 XGL_IMAGE_CREATE_INFO image_create_info;
130 XGL_GRAPHICS_PIPELINE_CREATE_INFO graphics_pipeline_create_info;
131 XGL_COMPUTE_PIPELINE_CREATE_INFO compute_pipeline_create_info;
132 XGL_SAMPLER_CREATE_INFO sampler_create_info;
133 XGL_DESCRIPTOR_SET_CREATE_INFO descriptor_set_create_info;
134 XGL_VIEWPORT_STATE_CREATE_INFO viewport_create_info;
135 XGL_DEPTH_STENCIL_STATE_CREATE_INFO ds_state_create_info;
136 XGL_RASTER_STATE_CREATE_INFO raster_state_create_info;
137 XGL_COLOR_BLEND_STATE_CREATE_INFO cb_state_create_info;
138 XGL_MSAA_STATE_CREATE_INFO msaa_state_create_info;
139 XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO wsi_x11_presentable_image_create_info;
140 } create_info;
141 char object_name[32];
142} GLOBAL_OBJECT_NODE;
Tobin Ehlis2836a7d2015-01-08 15:22:32 -0700143
144/*
145 * Track a Vertex or Index buffer binding
146 */
147typedef struct _MEMORY_BINDING {
148 XGL_OBJECT mem;
149 XGL_GPU_SIZE offset;
150 XGL_UINT binding;
151 XGL_INDEX_TYPE indexType;
152} MEMORY_BINDING;
153
154/*
155 * Track a Descriptor Set binding
156 */
157typedef struct _DS_BINDING {
158 XGL_PIPELINE_BIND_POINT pipelineBindPoint;
159 XGL_DESCRIPTOR_SET descriptorSet;
160 XGL_UINT slotOffset;
161} DS_BINDING;
162
163// Store a single LL of command buffers
164typedef struct _GLOBAL_CB_NODE {
165 struct _GLOBAL_CB_NODE* pNextGlobalCBNode;
166 XGL_CMD_BUFFER_CREATE_INFO createInfo;
167 MINI_NODE* pMemObjList; // LL of Mem objs referenced by this CB
168 MINI_NODE* pVertexBufList;
169 MINI_NODE* pIndexBufList;
170 GLOBAL_OBJECT_NODE* pDynamicState[XGL_NUM_STATE_BIND_POINT];
171 XGL_PIPELINE pipelines[XGL_NUM_PIPELINE_BIND_POINT];
172 DS_BINDING descriptorSets[XGL_MAX_DESCRIPTOR_SETS];
173 XGL_UINT colorAttachmentCount;
174 XGL_COLOR_ATTACHMENT_BIND_INFO attachments[XGL_MAX_COLOR_ATTACHMENTS];
175 XGL_DEPTH_STENCIL_BIND_INFO dsBindInfo;
176 XGL_CMD_BUFFER cmdBuffer;
177 XGL_FENCE fence; // fence tracking this cmd buffer
178} GLOBAL_CB_NODE;