Tobin Ehlis | 6208641 | 2014-11-19 16:19:28 -0700 | [diff] [blame] | 1 | /* |
| 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 |
| 26 | typedef 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 Ehlis | 22d0323 | 2014-11-25 18:01:12 -0700 | [diff] [blame] | 42 | MEMTRACK_MEMORY_LEAK = 14, // Failure to call xglFreeMemory on Mem Obj prior to DestroyDevice |
Tobin Ehlis | 366fbd3 | 2015-01-14 12:47:30 -0700 | [diff] [blame] | 43 | MEMTRACK_INVALID_STATE = 15, // Memory not in the correct state |
Tobin Ehlis | 77b3abb | 2015-03-04 08:38:22 -0700 | [diff] [blame] | 44 | MEMTRACK_RESET_CB_WHILE_IN_FLIGHT = 16, // xglResetCommandBuffer() called on a CB that hasn't completed |
Tobin Ehlis | 6208641 | 2014-11-19 16:19:28 -0700 | [diff] [blame] | 45 | } MEM_TRACK_ERROR; |
| 46 | |
Tobin Ehlis | 6aa7742 | 2015-01-07 17:49:29 -0700 | [diff] [blame] | 47 | /* |
| 48 | * Data Structure overview |
| 49 | * There are 3 global Linked-Lists (LLs) |
| 50 | * pGlobalCBHead points to head of Command Buffer (CB) LL |
| 51 | * Off of each node in this LL there is a separate LL of |
| 52 | * memory objects that are referenced by this CB |
| 53 | * pGlobalMemObjHead points to head of Memory Object LL |
| 54 | * Off of each node in this LL there are 2 separate LL |
| 55 | * One is a list of all CBs referencing this mem obj |
| 56 | * Two is a list of all XGL Objects that are bound to this memory |
| 57 | * pGlobalObjHead point to head of XGL Objects w/ bound mem LL |
| 58 | * Each node of this LL contains a ptr to global Mem Obj node for bound mem |
| 59 | * |
| 60 | * The "Global" nodes are for the main LLs |
| 61 | * The "mini" nodes are for ancillary LLs that are pointed to from global nodes |
| 62 | * |
| 63 | * Algorithm overview |
| 64 | * These are the primary events that should happen related to different objects |
| 65 | * 1. Command buffers |
| 66 | * CREATION - Add node to global LL |
| 67 | * CMD BIND - If mem associated, add mem reference to mini LL |
| 68 | * DESTROY - Remove from global LL, decrement (and report) mem references |
| 69 | * 2. Mem Objects |
| 70 | * CREATION - Add node to global LL |
| 71 | * OBJ BIND - Add obj node to mini LL for that mem node |
| 72 | * CMB BIND - If mem-related add CB node to mini LL for that mem node |
| 73 | * DESTROY - Flag as errors any remaining refs and Remove from global LL |
| 74 | * 3. Generic Objects |
| 75 | * 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 |
| 76 | * DESTROY - If mem bound, remove reference from mini LL for that mem Node, remove global obj node |
| 77 | */ |
| 78 | // TODO : Is there a way to track when Cmd Buffer finishes & remove mem references at that point? |
| 79 | // TODO : Could potentially store a list of freed mem allocs to flag when they're incorrectly used |
| 80 | |
| 81 | // Generic data struct for various "mini" Linked-Lists |
| 82 | // This just wraps some type of XGL OBJ and a pNext ptr |
| 83 | // Used for xgl obj, cmd buffer, and mem obj wrapping |
| 84 | typedef struct _MINI_NODE { |
| 85 | struct _MINI_NODE* pNext; |
| 86 | union { // different objects that can be wrapped |
| 87 | XGL_OBJECT object; |
| 88 | XGL_GPU_MEMORY mem; |
| 89 | XGL_CMD_BUFFER cmdBuffer; |
| 90 | XGL_BASE_OBJECT data; // for generic handling of data |
| 91 | }; |
| 92 | } MINI_NODE; |
| 93 | |
| 94 | struct GLOBAL_MEM_OBJ_NODE; |
Tobin Ehlis | 6aa7742 | 2015-01-07 17:49:29 -0700 | [diff] [blame] | 95 | |
| 96 | // Data struct for tracking memory object |
| 97 | typedef struct _GLOBAL_MEM_OBJ_NODE { |
Mark Lobodzinski | 1542710 | 2015-02-18 16:38:17 -0600 | [diff] [blame] | 98 | struct _GLOBAL_MEM_OBJ_NODE *pNextGlobalNode; // Ptr to next mem obj in global list of all objs |
| 99 | MINI_NODE *pObjBindings; // Ptr to list of objects bound to this memory |
| 100 | MINI_NODE *pCmdBufferBindings; // Ptr to list of cmd buffers that reference this mem object |
| 101 | uint32_t refCount; // Count of references (obj bindings or CB use) |
| 102 | XGL_GPU_MEMORY mem; |
| 103 | XGL_MEMORY_ALLOC_INFO allocInfo; |
Tobin Ehlis | 6aa7742 | 2015-01-07 17:49:29 -0700 | [diff] [blame] | 104 | } GLOBAL_MEM_OBJ_NODE; |
| 105 | |
| 106 | typedef struct _GLOBAL_OBJECT_NODE { |
| 107 | struct _GLOBAL_OBJECT_NODE* pNext; |
| 108 | GLOBAL_MEM_OBJ_NODE* pMemNode; |
| 109 | XGL_OBJECT object; |
| 110 | XGL_STRUCTURE_TYPE sType; |
| 111 | int ref_count; |
| 112 | // Capture all object types that may have memory bound. From prog guide: |
| 113 | // The only objects that are guaranteed to have no external memory |
| 114 | // requirements are devices, queues, command buffers, shaders and memory objects. |
| 115 | union { |
| 116 | XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO color_attachment_view_create_info; |
| 117 | XGL_DEPTH_STENCIL_VIEW_CREATE_INFO ds_view_create_info; |
| 118 | XGL_IMAGE_VIEW_CREATE_INFO image_view_create_info; |
| 119 | XGL_IMAGE_CREATE_INFO image_create_info; |
| 120 | XGL_GRAPHICS_PIPELINE_CREATE_INFO graphics_pipeline_create_info; |
| 121 | XGL_COMPUTE_PIPELINE_CREATE_INFO compute_pipeline_create_info; |
| 122 | XGL_SAMPLER_CREATE_INFO sampler_create_info; |
Tobin Ehlis | 77b3abb | 2015-03-04 08:38:22 -0700 | [diff] [blame] | 123 | XGL_FENCE_CREATE_INFO fence_create_info; |
Mark Lobodzinski | 1542710 | 2015-02-18 16:38:17 -0600 | [diff] [blame] | 124 | #ifndef _WIN32 |
Tobin Ehlis | 6aa7742 | 2015-01-07 17:49:29 -0700 | [diff] [blame] | 125 | XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO wsi_x11_presentable_image_create_info; |
Mark Lobodzinski | 1542710 | 2015-02-18 16:38:17 -0600 | [diff] [blame] | 126 | #endif // _WIN32 |
Tobin Ehlis | 6aa7742 | 2015-01-07 17:49:29 -0700 | [diff] [blame] | 127 | } create_info; |
Mark Lobodzinski | 78a2b4b | 2015-02-20 16:38:40 -0600 | [diff] [blame] | 128 | char object_name[64]; |
Tobin Ehlis | 6aa7742 | 2015-01-07 17:49:29 -0700 | [diff] [blame] | 129 | } GLOBAL_OBJECT_NODE; |
Tobin Ehlis | 2836a7d | 2015-01-08 15:22:32 -0700 | [diff] [blame] | 130 | |
| 131 | /* |
| 132 | * Track a Vertex or Index buffer binding |
| 133 | */ |
| 134 | typedef struct _MEMORY_BINDING { |
| 135 | XGL_OBJECT mem; |
| 136 | XGL_GPU_SIZE offset; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 137 | uint32_t binding; |
Mark Lobodzinski | 1542710 | 2015-02-18 16:38:17 -0600 | [diff] [blame] | 138 | XGL_BUFFER buffer; |
Tobin Ehlis | 2836a7d | 2015-01-08 15:22:32 -0700 | [diff] [blame] | 139 | XGL_INDEX_TYPE indexType; |
| 140 | } MEMORY_BINDING; |
| 141 | |
Tobin Ehlis | 2836a7d | 2015-01-08 15:22:32 -0700 | [diff] [blame] | 142 | // Store a single LL of command buffers |
| 143 | typedef struct _GLOBAL_CB_NODE { |
| 144 | struct _GLOBAL_CB_NODE* pNextGlobalCBNode; |
| 145 | XGL_CMD_BUFFER_CREATE_INFO createInfo; |
| 146 | MINI_NODE* pMemObjList; // LL of Mem objs referenced by this CB |
| 147 | MINI_NODE* pVertexBufList; |
| 148 | MINI_NODE* pIndexBufList; |
| 149 | GLOBAL_OBJECT_NODE* pDynamicState[XGL_NUM_STATE_BIND_POINT]; |
| 150 | XGL_PIPELINE pipelines[XGL_NUM_PIPELINE_BIND_POINT]; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 151 | uint32_t colorAttachmentCount; |
Tobin Ehlis | 2836a7d | 2015-01-08 15:22:32 -0700 | [diff] [blame] | 152 | XGL_DEPTH_STENCIL_BIND_INFO dsBindInfo; |
| 153 | XGL_CMD_BUFFER cmdBuffer; |
Mark Lobodzinski | 821b57f | 2015-02-25 18:11:05 -0600 | [diff] [blame] | 154 | XGL_FENCE fence; // fence tracking this cmd buffer |
| 155 | bool32_t localFlag; // fence is internal to layer |
Tobin Ehlis | 2836a7d | 2015-01-08 15:22:32 -0700 | [diff] [blame] | 156 | } GLOBAL_CB_NODE; |