| Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 1 | /* |
| Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 2 | * |
| 3 | * Copyright (C) 2014 LunarG, Inc. |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | * copy of this software and associated documentation files (the "Software"), |
| 7 | * to deal in the Software without restriction, including without limitation |
| 8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 | * and/or sell copies of the Software, and to permit persons to whom the |
| 10 | * Software is furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be included |
| 13 | * in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | * DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | #include <assert.h> |
| 24 | #include <unordered_map> |
| 25 | #include "vk_dispatch_table_helper.h" |
| Tobin Ehlis | 2d1d970 | 2015-07-03 09:42:57 -0600 | [diff] [blame] | 26 | #include "vk_layer.h" |
| Tobin Ehlis | 56d204a | 2015-07-03 10:15:26 -0600 | [diff] [blame] | 27 | #include "vk_layer_table.h" |
| Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 28 | static device_table_map tableMap; |
| 29 | static instance_table_map tableInstanceMap; |
| Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 30 | |
| Jon Ashburn | 4ec582f | 2015-06-15 09:30:12 -0600 | [diff] [blame] | 31 | #define DISPATCH_MAP_DEBUG 0 |
| Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 32 | |
| Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 33 | // Map lookup must be thread safe |
| Tobin Ehlis | 0b9c195 | 2015-07-06 14:02:36 -0600 | [diff] [blame] | 34 | VkLayerDispatchTable *device_dispatch_table(void* object) |
| Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 35 | { |
| Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 36 | dispatch_key key = get_dispatch_key(object); |
| 37 | device_table_map::const_iterator it = tableMap.find((void *) key); |
| 38 | assert(it != tableMap.end() && "Not able to find device dispatch entry"); |
| 39 | return it->second; |
| 40 | } |
| 41 | |
| Tobin Ehlis | 0b9c195 | 2015-07-06 14:02:36 -0600 | [diff] [blame] | 42 | VkLayerInstanceDispatchTable *instance_dispatch_table(void* object) |
| Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 43 | { |
| Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 44 | dispatch_key key = get_dispatch_key(object); |
| 45 | instance_table_map::const_iterator it = tableInstanceMap.find((void *) key); |
| Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 46 | #if DISPATCH_MAP_DEBUG |
| Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 47 | if (it != tableInstanceMap.end()) { |
| 48 | fprintf(stderr, "instance_dispatch_table: map: %p, object: %p, key: %p, table: %p\n", &tableInstanceMap, object, key, it->second); |
| 49 | } else { |
| 50 | fprintf(stderr, "instance_dispatch_table: map: %p, object: %p, key: %p, table: UNKNOWN\n", &tableInstanceMap, object, key); |
| 51 | } |
| Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 52 | #endif |
| Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 53 | assert(it != tableInstanceMap.end() && "Not able to find instance dispatch entry"); |
| 54 | return it->second; |
| 55 | } |
| 56 | |
| 57 | void destroy_dispatch_table(device_table_map &map, dispatch_key key) |
| 58 | { |
| 59 | device_table_map::const_iterator it = map.find((void *) key); |
| Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 60 | #if DISPATCH_MAP_DEBUG |
| Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 61 | if (it != map.end()) { |
| 62 | fprintf(stderr, "destroy device dispatch_table: map: %p, key: %p, table: %p\n", &map, key, it->second); |
| 63 | } else { |
| 64 | fprintf(stderr, "destroy device dispatch table: map: %p, key: %p, table: UNKNOWN\n", &map, key); |
| 65 | assert(it != map.end()); |
| 66 | } |
| Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 67 | #endif |
| Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 68 | map.erase(key); |
| 69 | } |
| 70 | |
| 71 | void destroy_dispatch_table(instance_table_map &map, dispatch_key key) |
| 72 | { |
| 73 | instance_table_map::const_iterator it = map.find((void *) key); |
| Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 74 | #if DISPATCH_MAP_DEBUG |
| Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 75 | if (it != map.end()) { |
| 76 | fprintf(stderr, "destroy instance dispatch_table: map: %p, key: %p, table: %p\n", &map, key, it->second); |
| 77 | } else { |
| 78 | fprintf(stderr, "destroy instance dispatch table: map: %p, key: %p, table: UNKNOWN\n", &map, key); |
| 79 | assert(it != map.end()); |
| 80 | } |
| Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 81 | #endif |
| Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 82 | map.erase(key); |
| 83 | } |
| 84 | |
| 85 | void destroy_device_dispatch_table(dispatch_key key) |
| 86 | { |
| 87 | destroy_dispatch_table(tableMap, key); |
| 88 | } |
| 89 | |
| 90 | void destroy_instance_dispatch_table(dispatch_key key) |
| 91 | { |
| 92 | destroy_dispatch_table(tableInstanceMap, key); |
| 93 | } |
| 94 | |
| Tobin Ehlis | 0b9c195 | 2015-07-06 14:02:36 -0600 | [diff] [blame] | 95 | VkLayerDispatchTable *get_dispatch_table(device_table_map &map, void* object) |
| Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 96 | { |
| Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 97 | dispatch_key key = get_dispatch_key(object); |
| 98 | device_table_map::const_iterator it = map.find((void *) key); |
| Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 99 | #if DISPATCH_MAP_DEBUG |
| Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 100 | if (it != map.end()) { |
| 101 | fprintf(stderr, "instance_dispatch_table: map: %p, object: %p, key: %p, table: %p\n", &tableInstanceMap, object, key, it->second); |
| 102 | } else { |
| 103 | fprintf(stderr, "instance_dispatch_table: map: %p, object: %p, key: %p, table: UNKNOWN\n", &tableInstanceMap, object, key); |
| 104 | } |
| Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 105 | #endif |
| Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 106 | assert(it != map.end() && "Not able to find device dispatch entry"); |
| 107 | return it->second; |
| 108 | } |
| 109 | |
| Tobin Ehlis | 0b9c195 | 2015-07-06 14:02:36 -0600 | [diff] [blame] | 110 | VkLayerInstanceDispatchTable *get_dispatch_table(instance_table_map &map, void* object) |
| Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 111 | { |
| 112 | // VkLayerInstanceDispatchTable *pDisp = *(VkLayerInstanceDispatchTable **) object; |
| 113 | dispatch_key key = get_dispatch_key(object); |
| 114 | instance_table_map::const_iterator it = map.find((void *) key); |
| Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 115 | #if DISPATCH_MAP_DEBUG |
| Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 116 | if (it != map.end()) { |
| 117 | fprintf(stderr, "instance_dispatch_table: map: %p, object: %p, key: %p, table: %p\n", &tableInstanceMap, object, key, it->second); |
| 118 | } else { |
| 119 | fprintf(stderr, "instance_dispatch_table: map: %p, object: %p, key: %p, table: UNKNOWN\n", &tableInstanceMap, object, key); |
| 120 | } |
| Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 121 | #endif |
| Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 122 | assert(it != map.end() && "Not able to find instance dispatch entry"); |
| 123 | return it->second; |
| 124 | } |
| 125 | |
| Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 126 | /* Various dispatchable objects will use the same underlying dispatch table if they |
| 127 | * are created from that "parent" object. Thus use pointer to dispatch table |
| 128 | * as the key to these table maps. |
| 129 | * Instance -> PhysicalDevice |
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 130 | * Device -> CommandBuffer or Queue |
| Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 131 | * If use the object themselves as key to map then implies Create entrypoints have to be intercepted |
| 132 | * and a new key inserted into map */ |
| Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 133 | VkLayerInstanceDispatchTable * initInstanceTable(instance_table_map &map, const VkBaseLayerObject *instancew) |
| Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 134 | { |
| 135 | VkLayerInstanceDispatchTable *pTable; |
| 136 | assert(instancew); |
| 137 | VkLayerInstanceDispatchTable **ppDisp = (VkLayerInstanceDispatchTable **) instancew->baseObject; |
| 138 | |
| Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 139 | std::unordered_map<void *, VkLayerInstanceDispatchTable *>::const_iterator it = map.find((void *) *ppDisp); |
| Jeremy Hayes | 70af09d | 2015-06-19 10:57:11 -0600 | [diff] [blame] | 140 | if (it == map.end()) |
| Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 141 | { |
| 142 | pTable = new VkLayerInstanceDispatchTable; |
| Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 143 | map[(void *) *ppDisp] = pTable; |
| Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 144 | #if DISPATCH_MAP_DEBUG |
| Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 145 | 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] | 146 | #endif |
| Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 147 | } else |
| 148 | { |
| Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 149 | #if DISPATCH_MAP_DEBUG |
| Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 150 | 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] | 151 | #endif |
| Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 152 | return it->second; |
| 153 | } |
| 154 | |
| 155 | layer_init_instance_dispatch_table(pTable, instancew); |
| 156 | |
| 157 | return pTable; |
| 158 | } |
| 159 | |
| Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 160 | VkLayerInstanceDispatchTable * initInstanceTable(const VkBaseLayerObject *instancew) |
| Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 161 | { |
| Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 162 | return initInstanceTable(tableInstanceMap, instancew); |
| 163 | } |
| 164 | |
| 165 | VkLayerDispatchTable * initDeviceTable(device_table_map &map, const VkBaseLayerObject *devw) |
| 166 | { |
| 167 | VkLayerDispatchTable *layer_device_table = NULL; |
| Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 168 | assert(devw); |
| 169 | VkLayerDispatchTable **ppDisp = (VkLayerDispatchTable **) (devw->baseObject); |
| Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 170 | VkLayerDispatchTable *base_device_table = *ppDisp; |
| Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 171 | |
| Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 172 | std::unordered_map<void *, VkLayerDispatchTable *>::const_iterator it = map.find((void *) base_device_table); |
| Jeremy Hayes | 70af09d | 2015-06-19 10:57:11 -0600 | [diff] [blame] | 173 | if (it == map.end()) |
| Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 174 | { |
| Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 175 | layer_device_table = new VkLayerDispatchTable; |
| 176 | map[(void *) base_device_table] = layer_device_table; |
| Courtney Goeltzenleuchter | 2906bb5 | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 177 | #if DISPATCH_MAP_DEBUG |
| Courtney Goeltzenleuchter | fde3a53 | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 178 | 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] | 179 | #endif |
| Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 180 | } else |
| 181 | { |
| 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, "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] | 184 | #endif |
| Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 185 | return it->second; |
| 186 | } |
| 187 | |
| Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 188 | layer_initialize_dispatch_table(layer_device_table, devw); |
| Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 189 | |
| Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 190 | return layer_device_table; |
| 191 | } |
| 192 | |
| 193 | VkLayerDispatchTable * initDeviceTable(const VkBaseLayerObject *devw) |
| 194 | { |
| 195 | return initDeviceTable(tableMap, devw); |
| Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 196 | } |