blob: 5e2614771530a8306d3975e2ee0d190292b7b174 [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 Ehlis366fbd32015-01-14 12:47:30 -070043 MEMTRACK_INVALID_STATE = 15, // Memory not in the correct state
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 Ehlis6aa77422015-01-07 17:49:29 -070094
95// Data struct for tracking memory object
96typedef struct _GLOBAL_MEM_OBJ_NODE {
Mark Lobodzinski15427102015-02-18 16:38:17 -060097 struct _GLOBAL_MEM_OBJ_NODE *pNextGlobalNode; // Ptr to next mem obj in global list of all objs
98 MINI_NODE *pObjBindings; // Ptr to list of objects bound to this memory
99 MINI_NODE *pCmdBufferBindings; // Ptr to list of cmd buffers that reference this mem object
100 uint32_t refCount; // Count of references (obj bindings or CB use)
101 XGL_GPU_MEMORY mem;
102 XGL_MEMORY_ALLOC_INFO allocInfo;
Tobin Ehlis6aa77422015-01-07 17:49:29 -0700103} GLOBAL_MEM_OBJ_NODE;
104
105typedef struct _GLOBAL_OBJECT_NODE {
106 struct _GLOBAL_OBJECT_NODE* pNext;
107 GLOBAL_MEM_OBJ_NODE* pMemNode;
108 XGL_OBJECT object;
109 XGL_STRUCTURE_TYPE sType;
110 int ref_count;
111 // Capture all object types that may have memory bound. From prog guide:
112 // The only objects that are guaranteed to have no external memory
113 // requirements are devices, queues, command buffers, shaders and memory objects.
114 union {
115 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO color_attachment_view_create_info;
116 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO ds_view_create_info;
117 XGL_IMAGE_VIEW_CREATE_INFO image_view_create_info;
118 XGL_IMAGE_CREATE_INFO image_create_info;
119 XGL_GRAPHICS_PIPELINE_CREATE_INFO graphics_pipeline_create_info;
120 XGL_COMPUTE_PIPELINE_CREATE_INFO compute_pipeline_create_info;
121 XGL_SAMPLER_CREATE_INFO sampler_create_info;
Mark Lobodzinski15427102015-02-18 16:38:17 -0600122#ifndef _WIN32
Tobin Ehlis6aa77422015-01-07 17:49:29 -0700123 XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO wsi_x11_presentable_image_create_info;
Mark Lobodzinski15427102015-02-18 16:38:17 -0600124#endif // _WIN32
Tobin Ehlis6aa77422015-01-07 17:49:29 -0700125 } create_info;
Mark Lobodzinski78a2b4b2015-02-20 16:38:40 -0600126 char object_name[64];
Tobin Ehlis6aa77422015-01-07 17:49:29 -0700127} GLOBAL_OBJECT_NODE;
Tobin Ehlis2836a7d2015-01-08 15:22:32 -0700128
129/*
130 * Track a Vertex or Index buffer binding
131 */
132typedef struct _MEMORY_BINDING {
133 XGL_OBJECT mem;
134 XGL_GPU_SIZE offset;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600135 uint32_t binding;
Mark Lobodzinski15427102015-02-18 16:38:17 -0600136 XGL_BUFFER buffer;
Tobin Ehlis2836a7d2015-01-08 15:22:32 -0700137 XGL_INDEX_TYPE indexType;
138} MEMORY_BINDING;
139
Tobin Ehlis2836a7d2015-01-08 15:22:32 -0700140// Store a single LL of command buffers
141typedef struct _GLOBAL_CB_NODE {
142 struct _GLOBAL_CB_NODE* pNextGlobalCBNode;
143 XGL_CMD_BUFFER_CREATE_INFO createInfo;
144 MINI_NODE* pMemObjList; // LL of Mem objs referenced by this CB
145 MINI_NODE* pVertexBufList;
146 MINI_NODE* pIndexBufList;
147 GLOBAL_OBJECT_NODE* pDynamicState[XGL_NUM_STATE_BIND_POINT];
148 XGL_PIPELINE pipelines[XGL_NUM_PIPELINE_BIND_POINT];
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600149 uint32_t colorAttachmentCount;
Tobin Ehlis2836a7d2015-01-08 15:22:32 -0700150 XGL_DEPTH_STENCIL_BIND_INFO dsBindInfo;
151 XGL_CMD_BUFFER cmdBuffer;
152 XGL_FENCE fence; // fence tracking this cmd buffer
153} GLOBAL_CB_NODE;