Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Vulkan |
| 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 <assert.h> |
| 25 | #include <unordered_map> |
| 26 | #include "vk_dispatch_table_helper.h" |
| 27 | #include "vkLayer.h" |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 28 | #include "layers_table.h" |
Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 29 | static device_table_map tableMap; |
| 30 | static instance_table_map tableInstanceMap; |
Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 31 | |
Jon Ashburn | 4ec582f | 2015-06-15 09:30:12 -0600 | [diff] [blame] | 32 | #define DISPATCH_MAP_DEBUG 0 |
Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 33 | |
Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 34 | // Map lookup must be thread safe |
| 35 | VkLayerDispatchTable *device_dispatch_table(VkObject object) |
| 36 | { |
| 37 | // VkLayerDispatchTable *pDisp = *(VkLayerDispatchTable **) object; |
| 38 | dispatch_key key = get_dispatch_key(object); |
| 39 | device_table_map::const_iterator it = tableMap.find((void *) key); |
| 40 | assert(it != tableMap.end() && "Not able to find device dispatch entry"); |
| 41 | return it->second; |
| 42 | } |
| 43 | |
| 44 | VkLayerInstanceDispatchTable *instance_dispatch_table(VkObject object) |
| 45 | { |
| 46 | // VkLayerInstanceDispatchTable *pDisp = *(VkLayerInstanceDispatchTable **) object; |
| 47 | dispatch_key key = get_dispatch_key(object); |
| 48 | instance_table_map::const_iterator it = tableInstanceMap.find((void *) key); |
Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 49 | #if DISPATCH_MAP_DEBUG |
Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 50 | if (it != tableInstanceMap.end()) { |
| 51 | fprintf(stderr, "instance_dispatch_table: map: %p, object: %p, key: %p, table: %p\n", &tableInstanceMap, object, key, it->second); |
| 52 | } else { |
| 53 | fprintf(stderr, "instance_dispatch_table: map: %p, object: %p, key: %p, table: UNKNOWN\n", &tableInstanceMap, object, key); |
| 54 | } |
Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 55 | #endif |
Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 56 | assert(it != tableInstanceMap.end() && "Not able to find instance dispatch entry"); |
| 57 | return it->second; |
| 58 | } |
| 59 | |
| 60 | void destroy_dispatch_table(device_table_map &map, dispatch_key key) |
| 61 | { |
| 62 | device_table_map::const_iterator it = map.find((void *) key); |
Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 63 | #if DISPATCH_MAP_DEBUG |
Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 64 | if (it != map.end()) { |
| 65 | fprintf(stderr, "destroy device dispatch_table: map: %p, key: %p, table: %p\n", &map, key, it->second); |
| 66 | } else { |
| 67 | fprintf(stderr, "destroy device dispatch table: map: %p, key: %p, table: UNKNOWN\n", &map, key); |
| 68 | assert(it != map.end()); |
| 69 | } |
Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 70 | #endif |
Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 71 | map.erase(key); |
| 72 | } |
| 73 | |
| 74 | void destroy_dispatch_table(instance_table_map &map, dispatch_key key) |
| 75 | { |
| 76 | instance_table_map::const_iterator it = map.find((void *) key); |
Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 77 | #if DISPATCH_MAP_DEBUG |
Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 78 | if (it != map.end()) { |
| 79 | fprintf(stderr, "destroy instance dispatch_table: map: %p, key: %p, table: %p\n", &map, key, it->second); |
| 80 | } else { |
| 81 | fprintf(stderr, "destroy instance dispatch table: map: %p, key: %p, table: UNKNOWN\n", &map, key); |
| 82 | assert(it != map.end()); |
| 83 | } |
Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 84 | #endif |
Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 85 | map.erase(key); |
| 86 | } |
| 87 | |
| 88 | void destroy_device_dispatch_table(dispatch_key key) |
| 89 | { |
| 90 | destroy_dispatch_table(tableMap, key); |
| 91 | } |
| 92 | |
| 93 | void destroy_instance_dispatch_table(dispatch_key key) |
| 94 | { |
| 95 | destroy_dispatch_table(tableInstanceMap, key); |
| 96 | } |
| 97 | |
| 98 | VkLayerDispatchTable *get_dispatch_table(device_table_map &map, VkObject object) |
| 99 | { |
| 100 | // VkLayerDispatchTable *pDisp = *(VkLayerDispatchTable **) object; |
| 101 | dispatch_key key = get_dispatch_key(object); |
| 102 | device_table_map::const_iterator it = map.find((void *) key); |
Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 103 | #if DISPATCH_MAP_DEBUG |
Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 104 | if (it != map.end()) { |
| 105 | fprintf(stderr, "instance_dispatch_table: map: %p, object: %p, key: %p, table: %p\n", &tableInstanceMap, object, key, it->second); |
| 106 | } else { |
| 107 | fprintf(stderr, "instance_dispatch_table: map: %p, object: %p, key: %p, table: UNKNOWN\n", &tableInstanceMap, object, key); |
| 108 | } |
Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 109 | #endif |
Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 110 | assert(it != map.end() && "Not able to find device dispatch entry"); |
| 111 | return it->second; |
| 112 | } |
| 113 | |
| 114 | VkLayerInstanceDispatchTable *get_dispatch_table(instance_table_map &map, VkObject object) |
| 115 | { |
| 116 | // VkLayerInstanceDispatchTable *pDisp = *(VkLayerInstanceDispatchTable **) object; |
| 117 | dispatch_key key = get_dispatch_key(object); |
| 118 | instance_table_map::const_iterator it = map.find((void *) key); |
Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 119 | #if DISPATCH_MAP_DEBUG |
Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 120 | if (it != map.end()) { |
| 121 | fprintf(stderr, "instance_dispatch_table: map: %p, object: %p, key: %p, table: %p\n", &tableInstanceMap, object, key, it->second); |
| 122 | } else { |
| 123 | fprintf(stderr, "instance_dispatch_table: map: %p, object: %p, key: %p, table: UNKNOWN\n", &tableInstanceMap, object, key); |
| 124 | } |
Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 125 | #endif |
Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 126 | assert(it != map.end() && "Not able to find instance dispatch entry"); |
| 127 | return it->second; |
| 128 | } |
| 129 | |
Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 130 | /* Various dispatchable objects will use the same underlying dispatch table if they |
| 131 | * are created from that "parent" object. Thus use pointer to dispatch table |
| 132 | * as the key to these table maps. |
| 133 | * Instance -> PhysicalDevice |
| 134 | * Device -> CmdBuffer or Queue |
| 135 | * If use the object themselves as key to map then implies Create entrypoints have to be intercepted |
| 136 | * and a new key inserted into map */ |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 137 | VkLayerInstanceDispatchTable * initInstanceTable(instance_table_map &map, const VkBaseLayerObject *instancew) |
Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 138 | { |
| 139 | VkLayerInstanceDispatchTable *pTable; |
| 140 | assert(instancew); |
| 141 | VkLayerInstanceDispatchTable **ppDisp = (VkLayerInstanceDispatchTable **) instancew->baseObject; |
| 142 | |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 143 | std::unordered_map<void *, VkLayerInstanceDispatchTable *>::const_iterator it = map.find((void *) *ppDisp); |
Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 144 | if (it == tableInstanceMap.end()) |
| 145 | { |
| 146 | pTable = new VkLayerInstanceDispatchTable; |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 147 | map[(void *) *ppDisp] = pTable; |
Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 148 | #if DISPATCH_MAP_DEBUG |
Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 149 | fprintf(stderr, "New, Instance: map: %p, base object: %p, key: %p, table: %p\n", &map, instancew, *ppDisp, pTable); |
Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 150 | #endif |
Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 151 | assert(map.size() <= 1 && "Instance dispatch table map has more than one entry"); |
Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 152 | } else |
| 153 | { |
Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 154 | #if DISPATCH_MAP_DEBUG |
Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 155 | fprintf(stderr, "Instance: map: %p, base object: %p, key: %p, table: %p\n", &map, instancew, *ppDisp, it->second); |
Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 156 | #endif |
Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 157 | return it->second; |
| 158 | } |
| 159 | |
| 160 | layer_init_instance_dispatch_table(pTable, instancew); |
| 161 | |
| 162 | return pTable; |
| 163 | } |
| 164 | |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 165 | VkLayerInstanceDispatchTable * initInstanceTable(const VkBaseLayerObject *instancew) |
Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 166 | { |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 167 | return initInstanceTable(tableInstanceMap, instancew); |
| 168 | } |
| 169 | |
| 170 | VkLayerDispatchTable * initDeviceTable(device_table_map &map, const VkBaseLayerObject *devw) |
| 171 | { |
| 172 | VkLayerDispatchTable *layer_device_table = NULL; |
Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 173 | assert(devw); |
| 174 | VkLayerDispatchTable **ppDisp = (VkLayerDispatchTable **) (devw->baseObject); |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 175 | VkLayerDispatchTable *base_device_table = *ppDisp; |
Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 176 | |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 177 | std::unordered_map<void *, VkLayerDispatchTable *>::const_iterator it = map.find((void *) base_device_table); |
Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 178 | if (it == tableMap.end()) |
| 179 | { |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 180 | layer_device_table = new VkLayerDispatchTable; |
| 181 | map[(void *) base_device_table] = layer_device_table; |
Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 182 | #if DISPATCH_MAP_DEBUG |
Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 183 | fprintf(stderr, "New, Device: map: %p, base object: %p, key: %p, table: %p\n", &map, devw, *ppDisp, layer_device_table); |
Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 184 | #endif |
Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 185 | } else |
| 186 | { |
Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 187 | #if DISPATCH_MAP_DEBUG |
Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 188 | fprintf(stderr, "Device: map: %p, base object: %p, key: %p, table: %p\n", &map, devw, *ppDisp, it->second); |
Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 189 | #endif |
Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 190 | return it->second; |
| 191 | } |
| 192 | |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 193 | layer_initialize_dispatch_table(layer_device_table, devw); |
Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 194 | |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 195 | return layer_device_table; |
| 196 | } |
| 197 | |
| 198 | VkLayerDispatchTable * initDeviceTable(const VkBaseLayerObject *devw) |
| 199 | { |
| 200 | return initDeviceTable(tableMap, devw); |
Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 201 | } |