blob: 57f2989de151b643589b169f65a0be7da903d37b [file] [log] [blame]
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -07001/* Copyright (c) 2015-2016 The Khronos Group Inc.
2 * Copyright (c) 2015-2016 Valve Corporation
3 * Copyright (c) 2015-2016 LunarG, Inc.
Jon Ashburn9eed2892015-06-01 10:02:09 -06004 *
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -07005 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and/or associated documentation files (the "Materials"), to
7 * deal in the Materials without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Materials, and to permit persons to whom the Materials
10 * are furnished to do so, subject to the following conditions:
Jon Ashburn9eed2892015-06-01 10:02:09 -060011 *
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -070012 * The above copyright notice(s) and this permission notice shall be included
13 * in all copies or substantial portions of the Materials.
Jon Ashburn9eed2892015-06-01 10:02:09 -060014 *
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -070015 * The Materials are Confidential Information as defined by the Khronos
16 * Membership Agreement until designated non-confidential by Khronos, at which
17 * point this condition clause shall be removed.
Jon Ashburn9eed2892015-06-01 10:02:09 -060018 *
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -070019 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Jon Ashburn9eed2892015-06-01 10:02:09 -060020 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -070021 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 *
23 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
24 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
26 * USE OR OTHER DEALINGS IN THE MATERIALS
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060027 *
28 * Author: Tobin Ehlis <tobin@lunarg.com>
Jon Ashburn9eed2892015-06-01 10:02:09 -060029 */
30#include <assert.h>
31#include <unordered_map>
32#include "vk_dispatch_table_helper.h"
David Pinedo9316d3b2015-11-06 12:54:48 -070033#include "vulkan/vk_layer.h"
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060034#include "vk_layer_table.h"
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060035static device_table_map tableMap;
36static instance_table_map tableInstanceMap;
Jon Ashburn9eed2892015-06-01 10:02:09 -060037
Jon Ashburn94f36d92015-06-15 09:30:12 -060038#define DISPATCH_MAP_DEBUG 0
Jon Ashburn9eed2892015-06-01 10:02:09 -060039
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060040// Map lookup must be thread safe
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -070041VkLayerDispatchTable *device_dispatch_table(void *object) {
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060042 dispatch_key key = get_dispatch_key(object);
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -070043 device_table_map::const_iterator it = tableMap.find((void *)key);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060044 assert(it != tableMap.end() && "Not able to find device dispatch entry");
45 return it->second;
46}
47
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -070048VkLayerInstanceDispatchTable *instance_dispatch_table(void *object) {
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060049 dispatch_key key = get_dispatch_key(object);
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -070050 instance_table_map::const_iterator it = tableInstanceMap.find((void *)key);
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -060051#if DISPATCH_MAP_DEBUG
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060052 if (it != tableInstanceMap.end()) {
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -070053 fprintf(stderr, "instance_dispatch_table: map: %p, object: %p, key: "
54 "%p, table: %p\n",
55 &tableInstanceMap, object, key, it->second);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060056 } else {
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -070057 fprintf(stderr, "instance_dispatch_table: map: %p, object: %p, key: "
58 "%p, table: UNKNOWN\n",
59 &tableInstanceMap, object, key);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060060 }
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -060061#endif
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -070062 assert(it != tableInstanceMap.end() &&
63 "Not able to find instance dispatch entry");
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060064 return it->second;
65}
66
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -070067void destroy_dispatch_table(device_table_map &map, dispatch_key key) {
68 device_table_map::const_iterator it = map.find((void *)key);
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -060069#if DISPATCH_MAP_DEBUG
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060070 if (it != map.end()) {
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -070071 fprintf(stderr,
72 "destroy device dispatch_table: map: %p, key: %p, table: %p\n",
73 &map, key, it->second);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060074 } else {
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -070075 fprintf(
76 stderr,
77 "destroy device dispatch table: map: %p, key: %p, table: UNKNOWN\n",
78 &map, key);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060079 assert(it != map.end());
80 }
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -060081#endif
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060082 map.erase(key);
83}
84
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -070085void destroy_dispatch_table(instance_table_map &map, dispatch_key key) {
86 instance_table_map::const_iterator it = map.find((void *)key);
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -060087#if DISPATCH_MAP_DEBUG
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060088 if (it != map.end()) {
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -070089 fprintf(
90 stderr,
91 "destroy instance dispatch_table: map: %p, key: %p, table: %p\n",
92 &map, key, it->second);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060093 } else {
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -070094 fprintf(stderr, "destroy instance dispatch table: map: %p, key: %p, "
95 "table: UNKNOWN\n",
96 &map, key);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060097 assert(it != map.end());
98 }
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -060099#endif
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -0600100 map.erase(key);
101}
102
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700103void destroy_device_dispatch_table(dispatch_key key) {
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -0600104 destroy_dispatch_table(tableMap, key);
105}
106
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700107void destroy_instance_dispatch_table(dispatch_key key) {
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -0600108 destroy_dispatch_table(tableInstanceMap, key);
109}
110
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700111VkLayerDispatchTable *get_dispatch_table(device_table_map &map, void *object) {
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -0600112 dispatch_key key = get_dispatch_key(object);
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700113 device_table_map::const_iterator it = map.find((void *)key);
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600114#if DISPATCH_MAP_DEBUG
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -0600115 if (it != map.end()) {
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700116 fprintf(
117 stderr,
118 "device_dispatch_table: map: %p, object: %p, key: %p, table: %p\n",
119 &tableInstanceMap, object, key, it->second);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -0600120 } else {
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700121 fprintf(stderr, "device_dispatch_table: map: %p, object: %p, key: %p, "
122 "table: UNKNOWN\n",
123 &tableInstanceMap, object, key);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -0600124 }
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600125#endif
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -0600126 assert(it != map.end() && "Not able to find device dispatch entry");
127 return it->second;
128}
129
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700130VkLayerInstanceDispatchTable *get_dispatch_table(instance_table_map &map,
131 void *object) {
132 // VkLayerInstanceDispatchTable *pDisp = *(VkLayerInstanceDispatchTable
133 // **) object;
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -0600134 dispatch_key key = get_dispatch_key(object);
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700135 instance_table_map::const_iterator it = map.find((void *)key);
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600136#if DISPATCH_MAP_DEBUG
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -0600137 if (it != map.end()) {
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700138 fprintf(stderr, "instance_dispatch_table: map: %p, object: %p, key: "
139 "%p, table: %p\n",
140 &tableInstanceMap, object, key, it->second);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -0600141 } else {
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700142 fprintf(stderr, "instance_dispatch_table: map: %p, object: %p, key: "
143 "%p, table: UNKNOWN\n",
144 &tableInstanceMap, object, key);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -0600145 }
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600146#endif
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -0600147 assert(it != map.end() && "Not able to find instance dispatch entry");
148 return it->second;
149}
150
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700151VkLayerInstanceCreateInfo *
152get_chain_info(const VkInstanceCreateInfo *pCreateInfo, VkLayerFunction func) {
153 VkLayerInstanceCreateInfo *chain_info =
154 (VkLayerInstanceCreateInfo *)pCreateInfo->pNext;
155 while (
156 chain_info &&
157 !(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO &&
158 chain_info->function == func)) {
159 chain_info = (VkLayerInstanceCreateInfo *)chain_info->pNext;
Courtney Goeltzenleuchter93a89cc2016-01-08 11:52:37 -0700160 }
161 assert(chain_info != NULL);
162 return chain_info;
163}
164
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700165VkLayerDeviceCreateInfo *get_chain_info(const VkDeviceCreateInfo *pCreateInfo,
166 VkLayerFunction func) {
167 VkLayerDeviceCreateInfo *chain_info =
168 (VkLayerDeviceCreateInfo *)pCreateInfo->pNext;
169 while (chain_info &&
170 !(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO &&
171 chain_info->function == func)) {
172 chain_info = (VkLayerDeviceCreateInfo *)chain_info->pNext;
Courtney Goeltzenleuchter93a89cc2016-01-08 11:52:37 -0700173 }
174 assert(chain_info != NULL);
175 return chain_info;
176}
177
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700178/* Various dispatchable objects will use the same underlying dispatch table if
179 * they
Jon Ashburn9eed2892015-06-01 10:02:09 -0600180 * are created from that "parent" object. Thus use pointer to dispatch table
181 * as the key to these table maps.
182 * Instance -> PhysicalDevice
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800183 * Device -> CommandBuffer or Queue
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700184 * If use the object themselves as key to map then implies Create entrypoints
185 * have to be intercepted
Jon Ashburn9eed2892015-06-01 10:02:09 -0600186 * and a new key inserted into map */
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700187VkLayerInstanceDispatchTable *
188initInstanceTable(VkInstance instance, const PFN_vkGetInstanceProcAddr gpa,
189 instance_table_map &map) {
Jon Ashburn9eed2892015-06-01 10:02:09 -0600190 VkLayerInstanceDispatchTable *pTable;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700191 dispatch_key key = get_dispatch_key(instance);
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700192 instance_table_map::const_iterator it = map.find((void *)key);
Jon Ashburn9eed2892015-06-01 10:02:09 -0600193
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700194 if (it == map.end()) {
195 pTable = new VkLayerInstanceDispatchTable;
196 map[(void *)key] = pTable;
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600197#if DISPATCH_MAP_DEBUG
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700198 fprintf(stderr, "New, Instance: map: %p, key: %p, table: %p\n", &map,
199 key, pTable);
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600200#endif
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700201 } else {
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600202#if DISPATCH_MAP_DEBUG
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700203 fprintf(stderr, "Instance: map: %p, key: %p, table: %p\n", &map, key,
204 it->second);
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600205#endif
Jon Ashburn9eed2892015-06-01 10:02:09 -0600206 return it->second;
207 }
208
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700209 layer_init_instance_dispatch_table(instance, pTable, gpa);
Jon Ashburn9eed2892015-06-01 10:02:09 -0600210
211 return pTable;
212}
213
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700214VkLayerInstanceDispatchTable *
215initInstanceTable(VkInstance instance, const PFN_vkGetInstanceProcAddr gpa) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700216 return initInstanceTable(instance, gpa, tableInstanceMap);
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600217}
218
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700219VkLayerDispatchTable *initDeviceTable(VkDevice device,
220 const PFN_vkGetDeviceProcAddr gpa,
221 device_table_map &map) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700222 VkLayerDispatchTable *pTable;
223 dispatch_key key = get_dispatch_key(device);
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700224 device_table_map::const_iterator it = map.find((void *)key);
Jon Ashburn9eed2892015-06-01 10:02:09 -0600225
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700226 if (it == map.end()) {
227 pTable = new VkLayerDispatchTable;
228 map[(void *)key] = pTable;
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600229#if DISPATCH_MAP_DEBUG
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700230 fprintf(stderr, "New, Device: map: %p, key: %p, table: %p\n", &map, key,
231 pTable);
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600232#endif
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700233 } else {
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600234#if DISPATCH_MAP_DEBUG
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700235 fprintf(stderr, "Device: map: %p, key: %p, table: %p\n", &map, key,
236 it->second);
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600237#endif
Jon Ashburn9eed2892015-06-01 10:02:09 -0600238 return it->second;
239 }
240
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700241 layer_init_device_dispatch_table(device, pTable, gpa);
Jon Ashburn9eed2892015-06-01 10:02:09 -0600242
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700243 return pTable;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600244}
245
Mark Lobodzinskib39d9e62016-02-02 17:06:29 -0700246VkLayerDispatchTable *initDeviceTable(VkDevice device,
247 const PFN_vkGetDeviceProcAddr gpa) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700248 return initDeviceTable(device, gpa, tableMap);
Jon Ashburn9eed2892015-06-01 10:02:09 -0600249}