Jon Ashburn | 9eed289 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 1 | /* |
Jon Ashburn | 9eed289 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 2 | * |
Courtney Goeltzenleuchter | fcbe16f | 2015-10-29 13:50:34 -0600 | [diff] [blame] | 3 | * Copyright (C) 2015 Valve Corporation |
Jon Ashburn | 9eed289 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 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. |
Courtney Goeltzenleuchter | 0555952 | 2015-10-30 11:14:30 -0600 | [diff] [blame] | 22 | * |
| 23 | * Author: Tobin Ehlis <tobin@lunarg.com> |
Jon Ashburn | 9eed289 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 24 | */ |
| 25 | #include <assert.h> |
| 26 | #include <unordered_map> |
| 27 | #include "vk_dispatch_table_helper.h" |
David Pinedo | 9316d3b | 2015-11-06 12:54:48 -0700 | [diff] [blame] | 28 | #include "vulkan/vk_layer.h" |
Tobin Ehlis | a0cb02e | 2015-07-03 10:15:26 -0600 | [diff] [blame] | 29 | #include "vk_layer_table.h" |
Courtney Goeltzenleuchter | c08e72d | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 30 | static device_table_map tableMap; |
| 31 | static instance_table_map tableInstanceMap; |
Jon Ashburn | 9eed289 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 32 | |
Jon Ashburn | 94f36d9 | 2015-06-15 09:30:12 -0600 | [diff] [blame] | 33 | #define DISPATCH_MAP_DEBUG 0 |
Jon Ashburn | 9eed289 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 34 | |
Courtney Goeltzenleuchter | c08e72d | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 35 | // Map lookup must be thread safe |
Tobin Ehlis | a30e7e5 | 2015-07-06 14:02:36 -0600 | [diff] [blame] | 36 | VkLayerDispatchTable *device_dispatch_table(void* object) |
Courtney Goeltzenleuchter | c08e72d | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 37 | { |
Courtney Goeltzenleuchter | c08e72d | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 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 | |
Tobin Ehlis | a30e7e5 | 2015-07-06 14:02:36 -0600 | [diff] [blame] | 44 | VkLayerInstanceDispatchTable *instance_dispatch_table(void* object) |
Courtney Goeltzenleuchter | c08e72d | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 45 | { |
Courtney Goeltzenleuchter | c08e72d | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 46 | dispatch_key key = get_dispatch_key(object); |
| 47 | instance_table_map::const_iterator it = tableInstanceMap.find((void *) key); |
Courtney Goeltzenleuchter | e3d3f5d | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 48 | #if DISPATCH_MAP_DEBUG |
Courtney Goeltzenleuchter | c08e72d | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 49 | if (it != tableInstanceMap.end()) { |
| 50 | fprintf(stderr, "instance_dispatch_table: map: %p, object: %p, key: %p, table: %p\n", &tableInstanceMap, object, key, it->second); |
| 51 | } else { |
| 52 | fprintf(stderr, "instance_dispatch_table: map: %p, object: %p, key: %p, table: UNKNOWN\n", &tableInstanceMap, object, key); |
| 53 | } |
Courtney Goeltzenleuchter | e3d3f5d | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 54 | #endif |
Courtney Goeltzenleuchter | c08e72d | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 55 | assert(it != tableInstanceMap.end() && "Not able to find instance dispatch entry"); |
| 56 | return it->second; |
| 57 | } |
| 58 | |
| 59 | void destroy_dispatch_table(device_table_map &map, dispatch_key key) |
| 60 | { |
| 61 | device_table_map::const_iterator it = map.find((void *) key); |
Courtney Goeltzenleuchter | e3d3f5d | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 62 | #if DISPATCH_MAP_DEBUG |
Courtney Goeltzenleuchter | c08e72d | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 63 | if (it != map.end()) { |
| 64 | fprintf(stderr, "destroy device dispatch_table: map: %p, key: %p, table: %p\n", &map, key, it->second); |
| 65 | } else { |
| 66 | fprintf(stderr, "destroy device dispatch table: map: %p, key: %p, table: UNKNOWN\n", &map, key); |
| 67 | assert(it != map.end()); |
| 68 | } |
Courtney Goeltzenleuchter | e3d3f5d | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 69 | #endif |
Courtney Goeltzenleuchter | c08e72d | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 70 | map.erase(key); |
| 71 | } |
| 72 | |
| 73 | void destroy_dispatch_table(instance_table_map &map, dispatch_key key) |
| 74 | { |
| 75 | instance_table_map::const_iterator it = map.find((void *) key); |
Courtney Goeltzenleuchter | e3d3f5d | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 76 | #if DISPATCH_MAP_DEBUG |
Courtney Goeltzenleuchter | c08e72d | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 77 | if (it != map.end()) { |
| 78 | fprintf(stderr, "destroy instance dispatch_table: map: %p, key: %p, table: %p\n", &map, key, it->second); |
| 79 | } else { |
| 80 | fprintf(stderr, "destroy instance dispatch table: map: %p, key: %p, table: UNKNOWN\n", &map, key); |
| 81 | assert(it != map.end()); |
| 82 | } |
Courtney Goeltzenleuchter | e3d3f5d | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 83 | #endif |
Courtney Goeltzenleuchter | c08e72d | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 84 | map.erase(key); |
| 85 | } |
| 86 | |
| 87 | void destroy_device_dispatch_table(dispatch_key key) |
| 88 | { |
| 89 | destroy_dispatch_table(tableMap, key); |
| 90 | } |
| 91 | |
| 92 | void destroy_instance_dispatch_table(dispatch_key key) |
| 93 | { |
| 94 | destroy_dispatch_table(tableInstanceMap, key); |
| 95 | } |
| 96 | |
Tobin Ehlis | a30e7e5 | 2015-07-06 14:02:36 -0600 | [diff] [blame] | 97 | VkLayerDispatchTable *get_dispatch_table(device_table_map &map, void* object) |
Courtney Goeltzenleuchter | c08e72d | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 98 | { |
Courtney Goeltzenleuchter | c08e72d | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 99 | dispatch_key key = get_dispatch_key(object); |
| 100 | device_table_map::const_iterator it = map.find((void *) key); |
Courtney Goeltzenleuchter | e3d3f5d | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 101 | #if DISPATCH_MAP_DEBUG |
Courtney Goeltzenleuchter | c08e72d | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 102 | if (it != map.end()) { |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 103 | fprintf(stderr, "device_dispatch_table: map: %p, object: %p, key: %p, table: %p\n", &tableInstanceMap, object, key, it->second); |
Courtney Goeltzenleuchter | c08e72d | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 104 | } else { |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 105 | fprintf(stderr, "device_dispatch_table: map: %p, object: %p, key: %p, table: UNKNOWN\n", &tableInstanceMap, object, key); |
Courtney Goeltzenleuchter | c08e72d | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 106 | } |
Courtney Goeltzenleuchter | e3d3f5d | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 107 | #endif |
Courtney Goeltzenleuchter | c08e72d | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 108 | assert(it != map.end() && "Not able to find device dispatch entry"); |
| 109 | return it->second; |
| 110 | } |
| 111 | |
Tobin Ehlis | a30e7e5 | 2015-07-06 14:02:36 -0600 | [diff] [blame] | 112 | VkLayerInstanceDispatchTable *get_dispatch_table(instance_table_map &map, void* object) |
Courtney Goeltzenleuchter | c08e72d | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 113 | { |
| 114 | // VkLayerInstanceDispatchTable *pDisp = *(VkLayerInstanceDispatchTable **) object; |
| 115 | dispatch_key key = get_dispatch_key(object); |
| 116 | instance_table_map::const_iterator it = map.find((void *) key); |
Courtney Goeltzenleuchter | e3d3f5d | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 117 | #if DISPATCH_MAP_DEBUG |
Courtney Goeltzenleuchter | c08e72d | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 118 | if (it != map.end()) { |
| 119 | fprintf(stderr, "instance_dispatch_table: map: %p, object: %p, key: %p, table: %p\n", &tableInstanceMap, object, key, it->second); |
| 120 | } else { |
| 121 | fprintf(stderr, "instance_dispatch_table: map: %p, object: %p, key: %p, table: UNKNOWN\n", &tableInstanceMap, object, key); |
| 122 | } |
Courtney Goeltzenleuchter | e3d3f5d | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 123 | #endif |
Courtney Goeltzenleuchter | c08e72d | 2015-06-13 21:18:30 -0600 | [diff] [blame] | 124 | assert(it != map.end() && "Not able to find instance dispatch entry"); |
| 125 | return it->second; |
| 126 | } |
| 127 | |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 128 | VkLayerInstanceCreateInfo *get_chain_info(const VkInstanceCreateInfo *pCreateInfo, VkLayerFunction func) |
Courtney Goeltzenleuchter | 93a89cc | 2016-01-08 11:52:37 -0700 | [diff] [blame] | 129 | { |
| 130 | VkLayerInstanceCreateInfo *chain_info = (VkLayerInstanceCreateInfo *) pCreateInfo->pNext; |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 131 | while (chain_info && !(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO |
| 132 | && chain_info->function == func)) { |
Courtney Goeltzenleuchter | 93a89cc | 2016-01-08 11:52:37 -0700 | [diff] [blame] | 133 | chain_info = (VkLayerInstanceCreateInfo *) chain_info->pNext; |
| 134 | } |
| 135 | assert(chain_info != NULL); |
| 136 | return chain_info; |
| 137 | } |
| 138 | |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 139 | VkLayerDeviceCreateInfo *get_chain_info(const VkDeviceCreateInfo *pCreateInfo, VkLayerFunction func) |
Courtney Goeltzenleuchter | 93a89cc | 2016-01-08 11:52:37 -0700 | [diff] [blame] | 140 | { |
| 141 | VkLayerDeviceCreateInfo *chain_info = (VkLayerDeviceCreateInfo *) pCreateInfo->pNext; |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 142 | while (chain_info && !(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO |
| 143 | && chain_info->function == func)) { |
Courtney Goeltzenleuchter | 93a89cc | 2016-01-08 11:52:37 -0700 | [diff] [blame] | 144 | chain_info = (VkLayerDeviceCreateInfo *) chain_info->pNext; |
| 145 | } |
| 146 | assert(chain_info != NULL); |
| 147 | return chain_info; |
| 148 | } |
| 149 | |
Jon Ashburn | 9eed289 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 150 | /* Various dispatchable objects will use the same underlying dispatch table if they |
| 151 | * are created from that "parent" object. Thus use pointer to dispatch table |
| 152 | * as the key to these table maps. |
| 153 | * Instance -> PhysicalDevice |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 154 | * Device -> CommandBuffer or Queue |
Jon Ashburn | 9eed289 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 155 | * If use the object themselves as key to map then implies Create entrypoints have to be intercepted |
| 156 | * and a new key inserted into map */ |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 157 | VkLayerInstanceDispatchTable * initInstanceTable(VkInstance instance, const PFN_vkGetInstanceProcAddr gpa, instance_table_map &map) |
Jon Ashburn | 9eed289 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 158 | { |
| 159 | VkLayerInstanceDispatchTable *pTable; |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 160 | dispatch_key key = get_dispatch_key(instance); |
| 161 | instance_table_map::const_iterator it = map.find((void *) key); |
Jon Ashburn | 9eed289 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 162 | |
Jeremy Hayes | d427d72 | 2015-06-19 10:57:11 -0600 | [diff] [blame] | 163 | if (it == map.end()) |
Jon Ashburn | 9eed289 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 164 | { |
| 165 | pTable = new VkLayerInstanceDispatchTable; |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 166 | map[(void *) key] = pTable; |
Courtney Goeltzenleuchter | e3d3f5d | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 167 | #if DISPATCH_MAP_DEBUG |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 168 | fprintf(stderr, "New, Instance: map: %p, key: %p, table: %p\n", &map, key, pTable); |
Courtney Goeltzenleuchter | e3d3f5d | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 169 | #endif |
Jon Ashburn | 9eed289 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 170 | } else |
| 171 | { |
Courtney Goeltzenleuchter | e3d3f5d | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 172 | #if DISPATCH_MAP_DEBUG |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 173 | fprintf(stderr, "Instance: map: %p, key: %p, table: %p\n", &map, key, it->second); |
Courtney Goeltzenleuchter | e3d3f5d | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 174 | #endif |
Jon Ashburn | 9eed289 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 175 | return it->second; |
| 176 | } |
| 177 | |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 178 | layer_init_instance_dispatch_table(instance, pTable, gpa); |
Jon Ashburn | 9eed289 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 179 | |
| 180 | return pTable; |
| 181 | } |
| 182 | |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 183 | VkLayerInstanceDispatchTable * initInstanceTable(VkInstance instance, const PFN_vkGetInstanceProcAddr gpa) |
Jon Ashburn | 9eed289 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 184 | { |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 185 | return initInstanceTable(instance, gpa, tableInstanceMap); |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 186 | } |
| 187 | |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 188 | VkLayerDispatchTable * initDeviceTable(VkDevice device, const PFN_vkGetDeviceProcAddr gpa, device_table_map &map) |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 189 | { |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 190 | VkLayerDispatchTable *pTable; |
| 191 | dispatch_key key = get_dispatch_key(device); |
| 192 | device_table_map::const_iterator it = map.find((void *) key); |
Jon Ashburn | 9eed289 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 193 | |
Jeremy Hayes | d427d72 | 2015-06-19 10:57:11 -0600 | [diff] [blame] | 194 | if (it == map.end()) |
Jon Ashburn | 9eed289 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 195 | { |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 196 | pTable = new VkLayerDispatchTable; |
| 197 | map[(void *) key] = pTable; |
Courtney Goeltzenleuchter | e3d3f5d | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 198 | #if DISPATCH_MAP_DEBUG |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 199 | fprintf(stderr, "New, Device: map: %p, key: %p, table: %p\n", &map, key, pTable); |
Courtney Goeltzenleuchter | e3d3f5d | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 200 | #endif |
Jon Ashburn | 9eed289 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 201 | } else |
| 202 | { |
Courtney Goeltzenleuchter | e3d3f5d | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 203 | #if DISPATCH_MAP_DEBUG |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 204 | fprintf(stderr, "Device: map: %p, key: %p, table: %p\n", &map, key, it->second); |
Courtney Goeltzenleuchter | e3d3f5d | 2015-06-13 21:48:26 -0600 | [diff] [blame] | 205 | #endif |
Jon Ashburn | 9eed289 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 206 | return it->second; |
| 207 | } |
| 208 | |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 209 | layer_init_device_dispatch_table(device, pTable, gpa); |
Jon Ashburn | 9eed289 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 210 | |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 211 | return pTable; |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 212 | } |
| 213 | |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 214 | VkLayerDispatchTable * initDeviceTable(VkDevice device, const PFN_vkGetDeviceProcAddr gpa) |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 215 | { |
Courtney Goeltzenleuchter | 00150eb | 2016-01-08 12:18:43 -0700 | [diff] [blame] | 216 | return initDeviceTable(device, gpa, tableMap); |
Jon Ashburn | 9eed289 | 2015-06-01 10:02:09 -0600 | [diff] [blame] | 217 | } |