blob: fed66bf3a01d570c9ec31d67cc573d453da0fad2 [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.
Michael Lentine0a369f62016-02-03 16:51:46 -06004 * Copyright (c) 2015-2016 Google, Inc.
Jon Ashburn9eed2892015-06-01 10:02:09 -06005 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06006 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
Jon Ashburn9eed2892015-06-01 10:02:09 -06009 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060010 * http://www.apache.org/licenses/LICENSE-2.0
Jon Ashburn9eed2892015-06-01 10:02:09 -060011 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060012 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060017 *
18 * Author: Tobin Ehlis <tobin@lunarg.com>
Jon Ashburn9eed2892015-06-01 10:02:09 -060019 */
20#include <assert.h>
21#include <unordered_map>
22#include "vk_dispatch_table_helper.h"
David Pinedo9316d3b2015-11-06 12:54:48 -070023#include "vulkan/vk_layer.h"
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060024#include "vk_layer_table.h"
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060025static device_table_map tableMap;
26static instance_table_map tableInstanceMap;
Jon Ashburn9eed2892015-06-01 10:02:09 -060027
Jon Ashburn94f36d92015-06-15 09:30:12 -060028#define DISPATCH_MAP_DEBUG 0
Jon Ashburn9eed2892015-06-01 10:02:09 -060029
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060030// Map lookup must be thread safe
Jon Ashburn5484e0c2016-03-08 17:48:44 -070031VkLayerDispatchTable *device_dispatch_table(void *object) {
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060032 dispatch_key key = get_dispatch_key(object);
Jon Ashburn5484e0c2016-03-08 17:48:44 -070033 device_table_map::const_iterator it = tableMap.find((void *)key);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060034 assert(it != tableMap.end() && "Not able to find device dispatch entry");
35 return it->second;
36}
37
Jon Ashburn5484e0c2016-03-08 17:48:44 -070038VkLayerInstanceDispatchTable *instance_dispatch_table(void *object) {
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060039 dispatch_key key = get_dispatch_key(object);
Jon Ashburn5484e0c2016-03-08 17:48:44 -070040 instance_table_map::const_iterator it = tableInstanceMap.find((void *)key);
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -060041#if DISPATCH_MAP_DEBUG
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060042 if (it != tableInstanceMap.end()) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -070043 fprintf(stderr, "instance_dispatch_table: map: 0x%p, object: 0x%p, key: 0x%p, table: 0x%p\n", &tableInstanceMap, object,
44 key, it->second);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060045 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -070046 fprintf(stderr, "instance_dispatch_table: map: 0x%p, object: 0x%p, key: 0x%p, table: UNKNOWN\n", &tableInstanceMap,
47 object, key);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060048 }
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -060049#endif
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -070050 assert(it != tableInstanceMap.end() && "Not able to find instance dispatch entry");
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060051 return it->second;
52}
53
Jon Ashburn5484e0c2016-03-08 17:48:44 -070054void destroy_dispatch_table(device_table_map &map, dispatch_key key) {
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -060055#if DISPATCH_MAP_DEBUG
Michael Lentine0a369f62016-02-03 16:51:46 -060056 device_table_map::const_iterator it = map.find((void *)key);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060057 if (it != map.end()) {
Mark Muelleraab36502016-05-03 13:17:29 -060058 fprintf(stderr, "destroy device dispatch_table: map: 0x%p, key: 0x%p, table: 0x%p\n", &map, key, it->second);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060059 } else {
Mark Muelleraab36502016-05-03 13:17:29 -060060 fprintf(stderr, "destroy device dispatch table: map: 0x%p, key: 0x%p, table: UNKNOWN\n", &map, key);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060061 assert(it != map.end());
62 }
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -060063#endif
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060064 map.erase(key);
65}
66
Jon Ashburn5484e0c2016-03-08 17:48:44 -070067void destroy_dispatch_table(instance_table_map &map, dispatch_key key) {
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -060068#if DISPATCH_MAP_DEBUG
Michael Lentine0a369f62016-02-03 16:51:46 -060069 instance_table_map::const_iterator it = map.find((void *)key);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060070 if (it != map.end()) {
Mark Muelleraab36502016-05-03 13:17:29 -060071 fprintf(stderr, "destroy instance dispatch_table: map: 0x%p, key: 0x%p, table: 0x%p\n", &map, key, it->second);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060072 } else {
Mark Muelleraab36502016-05-03 13:17:29 -060073 fprintf(stderr, "destroy instance dispatch table: map: 0x%p, key: 0x%p, table: UNKNOWN\n", &map, key);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060074 assert(it != map.end());
75 }
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -060076#endif
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060077 map.erase(key);
78}
79
Jon Ashburn5484e0c2016-03-08 17:48:44 -070080void destroy_device_dispatch_table(dispatch_key key) { destroy_dispatch_table(tableMap, key); }
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060081
Jon Ashburn5484e0c2016-03-08 17:48:44 -070082void destroy_instance_dispatch_table(dispatch_key key) { destroy_dispatch_table(tableInstanceMap, key); }
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060083
Jon Ashburn5484e0c2016-03-08 17:48:44 -070084VkLayerDispatchTable *get_dispatch_table(device_table_map &map, void *object) {
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060085 dispatch_key key = get_dispatch_key(object);
Jon Ashburn5484e0c2016-03-08 17:48:44 -070086 device_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 Lobodzinski729a8d32017-01-26 12:16:30 -070089 fprintf(stderr, "device_dispatch_table: map: 0x%p, object: 0x%p, key: 0x%p, table: 0x%p\n", &tableInstanceMap, object,
90 key, it->second);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060091 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -070092 fprintf(stderr, "device_dispatch_table: map: 0x%p, object: 0x%p, key: 0x%p, table: UNKNOWN\n", &tableInstanceMap, object,
93 key);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060094 }
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -060095#endif
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060096 assert(it != map.end() && "Not able to find device dispatch entry");
97 return it->second;
98}
99
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700100VkLayerInstanceDispatchTable *get_dispatch_table(instance_table_map &map, void *object) {
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -0600101 dispatch_key key = get_dispatch_key(object);
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700102 instance_table_map::const_iterator it = map.find((void *)key);
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600103#if DISPATCH_MAP_DEBUG
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -0600104 if (it != map.end()) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700105 fprintf(stderr, "instance_dispatch_table: map: 0x%p, object: 0x%p, key: 0x%p, table: 0x%p\n", &tableInstanceMap, object,
106 key, it->second);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -0600107 } else {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700108 fprintf(stderr, "instance_dispatch_table: map: 0x%p, object: 0x%p, key: 0x%p, table: UNKNOWN\n", &tableInstanceMap,
109 object, key);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -0600110 }
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600111#endif
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -0600112 assert(it != map.end() && "Not able to find instance dispatch entry");
113 return it->second;
114}
115
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700116VkLayerInstanceCreateInfo *get_chain_info(const VkInstanceCreateInfo *pCreateInfo, VkLayerFunction func) {
117 VkLayerInstanceCreateInfo *chain_info = (VkLayerInstanceCreateInfo *)pCreateInfo->pNext;
118 while (chain_info && !(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO && chain_info->function == func)) {
119 chain_info = (VkLayerInstanceCreateInfo *)chain_info->pNext;
Courtney Goeltzenleuchter93a89cc2016-01-08 11:52:37 -0700120 }
121 assert(chain_info != NULL);
122 return chain_info;
123}
124
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700125VkLayerDeviceCreateInfo *get_chain_info(const VkDeviceCreateInfo *pCreateInfo, VkLayerFunction func) {
126 VkLayerDeviceCreateInfo *chain_info = (VkLayerDeviceCreateInfo *)pCreateInfo->pNext;
127 while (chain_info && !(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO && chain_info->function == func)) {
128 chain_info = (VkLayerDeviceCreateInfo *)chain_info->pNext;
Courtney Goeltzenleuchter93a89cc2016-01-08 11:52:37 -0700129 }
130 assert(chain_info != NULL);
131 return chain_info;
132}
133
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700134/* Various dispatchable objects will use the same underlying dispatch table if they
Jon Ashburn9eed2892015-06-01 10:02:09 -0600135 * are created from that "parent" object. Thus use pointer to dispatch table
136 * as the key to these table maps.
137 * Instance -> PhysicalDevice
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800138 * Device -> CommandBuffer or Queue
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700139 * If use the object themselves as key to map then implies Create entrypoints have to be intercepted
Jon Ashburn9eed2892015-06-01 10:02:09 -0600140 * and a new key inserted into map */
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700141VkLayerInstanceDispatchTable *initInstanceTable(VkInstance instance, const PFN_vkGetInstanceProcAddr gpa, instance_table_map &map) {
Jon Ashburn9eed2892015-06-01 10:02:09 -0600142 VkLayerInstanceDispatchTable *pTable;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700143 dispatch_key key = get_dispatch_key(instance);
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700144 instance_table_map::const_iterator it = map.find((void *)key);
Jon Ashburn9eed2892015-06-01 10:02:09 -0600145
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700146 if (it == map.end()) {
147 pTable = new VkLayerInstanceDispatchTable;
148 map[(void *)key] = pTable;
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600149#if DISPATCH_MAP_DEBUG
Mark Muelleraab36502016-05-03 13:17:29 -0600150 fprintf(stderr, "New, Instance: map: 0x%p, key: 0x%p, table: 0x%p\n", &map, key, pTable);
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600151#endif
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700152 } else {
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600153#if DISPATCH_MAP_DEBUG
Mark Muelleraab36502016-05-03 13:17:29 -0600154 fprintf(stderr, "Instance: map: 0x%p, key: 0x%p, table: 0x%p\n", &map, key, it->second);
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600155#endif
Jon Ashburn9eed2892015-06-01 10:02:09 -0600156 return it->second;
157 }
158
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700159 layer_init_instance_dispatch_table(instance, pTable, gpa);
Jon Ashburn9eed2892015-06-01 10:02:09 -0600160
Mark Young39389872017-01-19 21:10:49 -0700161 // Setup func pointers that are required but not externally exposed. These won't be added to the instance dispatch table by
162 // default.
163 pTable->GetPhysicalDeviceProcAddr = (PFN_GetPhysicalDeviceProcAddr)gpa(instance, "vk_layerGetPhysicalDeviceProcAddr");
164
Jon Ashburn9eed2892015-06-01 10:02:09 -0600165 return pTable;
166}
167
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700168VkLayerInstanceDispatchTable *initInstanceTable(VkInstance instance, const PFN_vkGetInstanceProcAddr gpa) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700169 return initInstanceTable(instance, gpa, tableInstanceMap);
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600170}
171
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700172VkLayerDispatchTable *initDeviceTable(VkDevice device, const PFN_vkGetDeviceProcAddr gpa, device_table_map &map) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700173 VkLayerDispatchTable *pTable;
174 dispatch_key key = get_dispatch_key(device);
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700175 device_table_map::const_iterator it = map.find((void *)key);
Jon Ashburn9eed2892015-06-01 10:02:09 -0600176
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700177 if (it == map.end()) {
178 pTable = new VkLayerDispatchTable;
179 map[(void *)key] = pTable;
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600180#if DISPATCH_MAP_DEBUG
Mark Muelleraab36502016-05-03 13:17:29 -0600181 fprintf(stderr, "New, Device: map: 0x%p, key: 0x%p, table: 0x%p\n", &map, key, pTable);
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600182#endif
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700183 } else {
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600184#if DISPATCH_MAP_DEBUG
Mark Muelleraab36502016-05-03 13:17:29 -0600185 fprintf(stderr, "Device: map: 0x%p, key: 0x%p, table: 0x%p\n", &map, key, it->second);
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600186#endif
Jon Ashburn9eed2892015-06-01 10:02:09 -0600187 return it->second;
188 }
189
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700190 layer_init_device_dispatch_table(device, pTable, gpa);
Jon Ashburn9eed2892015-06-01 10:02:09 -0600191
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700192 return pTable;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600193}
194
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700195VkLayerDispatchTable *initDeviceTable(VkDevice device, const PFN_vkGetDeviceProcAddr gpa) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700196 return initDeviceTable(device, gpa, tableMap);
Jon Ashburn9eed2892015-06-01 10:02:09 -0600197}