blob: 8664a891e41f105ff4820041176f0c5366fecf9c [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()) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -070043 fprintf(stderr, "instance_dispatch_table: map: %p, object: %p, key: %p, table: %p\n", &tableInstanceMap, object, key,
44 it->second);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060045 } else {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -070046 fprintf(stderr, "instance_dispatch_table: map: %p, object: %p, key: %p, table: UNKNOWN\n", &tableInstanceMap, object, key);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060047 }
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -060048#endif
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -070049 assert(it != tableInstanceMap.end() && "Not able to find instance dispatch entry");
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060050 return it->second;
51}
52
Jon Ashburn5484e0c2016-03-08 17:48:44 -070053void destroy_dispatch_table(device_table_map &map, dispatch_key key) {
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -060054#if DISPATCH_MAP_DEBUG
Michael Lentine0a369f62016-02-03 16:51:46 -060055 device_table_map::const_iterator it = map.find((void *)key);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060056 if (it != map.end()) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -070057 fprintf(stderr, "destroy device dispatch_table: map: %p, key: %p, table: %p\n", &map, key, it->second);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060058 } else {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -070059 fprintf(stderr, "destroy device dispatch table: map: %p, key: %p, table: UNKNOWN\n", &map, key);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060060 assert(it != map.end());
61 }
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -060062#endif
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060063 map.erase(key);
64}
65
Jon Ashburn5484e0c2016-03-08 17:48:44 -070066void destroy_dispatch_table(instance_table_map &map, dispatch_key key) {
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -060067#if DISPATCH_MAP_DEBUG
Michael Lentine0a369f62016-02-03 16:51:46 -060068 instance_table_map::const_iterator it = map.find((void *)key);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060069 if (it != map.end()) {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -070070 fprintf(stderr, "destroy instance dispatch_table: map: %p, key: %p, table: %p\n", &map, key, it->second);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060071 } else {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -070072 fprintf(stderr, "destroy instance dispatch table: map: %p, key: %p, table: UNKNOWN\n", &map, key);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060073 assert(it != map.end());
74 }
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -060075#endif
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060076 map.erase(key);
77}
78
Jon Ashburn5484e0c2016-03-08 17:48:44 -070079void destroy_device_dispatch_table(dispatch_key key) { destroy_dispatch_table(tableMap, key); }
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060080
Jon Ashburn5484e0c2016-03-08 17:48:44 -070081void destroy_instance_dispatch_table(dispatch_key key) { destroy_dispatch_table(tableInstanceMap, key); }
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060082
Jon Ashburn5484e0c2016-03-08 17:48:44 -070083VkLayerDispatchTable *get_dispatch_table(device_table_map &map, void *object) {
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060084 dispatch_key key = get_dispatch_key(object);
Jon Ashburn5484e0c2016-03-08 17:48:44 -070085 device_table_map::const_iterator it = map.find((void *)key);
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -060086#if DISPATCH_MAP_DEBUG
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060087 if (it != map.end()) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -070088 fprintf(stderr, "device_dispatch_table: map: %p, object: %p, key: %p, table: %p\n", &tableInstanceMap, object, key,
89 it->second);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060090 } else {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -070091 fprintf(stderr, "device_dispatch_table: map: %p, object: %p, key: %p, table: UNKNOWN\n", &tableInstanceMap, object, key);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060092 }
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -060093#endif
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -060094 assert(it != map.end() && "Not able to find device dispatch entry");
95 return it->second;
96}
97
Jon Ashburn5484e0c2016-03-08 17:48:44 -070098VkLayerInstanceDispatchTable *get_dispatch_table(instance_table_map &map, void *object) {
99 // VkLayerInstanceDispatchTable *pDisp = *(VkLayerInstanceDispatchTable **) object;
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -0600100 dispatch_key key = get_dispatch_key(object);
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700101 instance_table_map::const_iterator it = map.find((void *)key);
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600102#if DISPATCH_MAP_DEBUG
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -0600103 if (it != map.end()) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700104 fprintf(stderr, "instance_dispatch_table: map: %p, object: %p, key: %p, table: %p\n", &tableInstanceMap, object, key,
105 it->second);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -0600106 } else {
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700107 fprintf(stderr, "instance_dispatch_table: map: %p, object: %p, key: %p, table: UNKNOWN\n", &tableInstanceMap, object, key);
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -0600108 }
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600109#endif
Courtney Goeltzenleuchterc08e72d2015-06-13 21:18:30 -0600110 assert(it != map.end() && "Not able to find instance dispatch entry");
111 return it->second;
112}
113
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700114VkLayerInstanceCreateInfo *get_chain_info(const VkInstanceCreateInfo *pCreateInfo, VkLayerFunction func) {
115 VkLayerInstanceCreateInfo *chain_info = (VkLayerInstanceCreateInfo *)pCreateInfo->pNext;
116 while (chain_info && !(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO && chain_info->function == func)) {
117 chain_info = (VkLayerInstanceCreateInfo *)chain_info->pNext;
Courtney Goeltzenleuchter93a89cc2016-01-08 11:52:37 -0700118 }
119 assert(chain_info != NULL);
120 return chain_info;
121}
122
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700123VkLayerDeviceCreateInfo *get_chain_info(const VkDeviceCreateInfo *pCreateInfo, VkLayerFunction func) {
124 VkLayerDeviceCreateInfo *chain_info = (VkLayerDeviceCreateInfo *)pCreateInfo->pNext;
125 while (chain_info && !(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO && chain_info->function == func)) {
126 chain_info = (VkLayerDeviceCreateInfo *)chain_info->pNext;
Courtney Goeltzenleuchter93a89cc2016-01-08 11:52:37 -0700127 }
128 assert(chain_info != NULL);
129 return chain_info;
130}
131
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700132/* Various dispatchable objects will use the same underlying dispatch table if they
Jon Ashburn9eed2892015-06-01 10:02:09 -0600133 * are created from that "parent" object. Thus use pointer to dispatch table
134 * as the key to these table maps.
135 * Instance -> PhysicalDevice
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800136 * Device -> CommandBuffer or Queue
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700137 * If use the object themselves as key to map then implies Create entrypoints have to be intercepted
Jon Ashburn9eed2892015-06-01 10:02:09 -0600138 * and a new key inserted into map */
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700139VkLayerInstanceDispatchTable *initInstanceTable(VkInstance instance, const PFN_vkGetInstanceProcAddr gpa, instance_table_map &map) {
Jon Ashburn9eed2892015-06-01 10:02:09 -0600140 VkLayerInstanceDispatchTable *pTable;
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700141 dispatch_key key = get_dispatch_key(instance);
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700142 instance_table_map::const_iterator it = map.find((void *)key);
Jon Ashburn9eed2892015-06-01 10:02:09 -0600143
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700144 if (it == map.end()) {
145 pTable = new VkLayerInstanceDispatchTable;
146 map[(void *)key] = pTable;
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600147#if DISPATCH_MAP_DEBUG
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700148 fprintf(stderr, "New, Instance: map: %p, key: %p, table: %p\n", &map, key, pTable);
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600149#endif
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700150 } else {
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600151#if DISPATCH_MAP_DEBUG
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700152 fprintf(stderr, "Instance: map: %p, key: %p, table: %p\n", &map, key, it->second);
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600153#endif
Jon Ashburn9eed2892015-06-01 10:02:09 -0600154 return it->second;
155 }
156
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700157 layer_init_instance_dispatch_table(instance, pTable, gpa);
Jon Ashburn9eed2892015-06-01 10:02:09 -0600158
159 return pTable;
160}
161
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700162VkLayerInstanceDispatchTable *initInstanceTable(VkInstance instance, const PFN_vkGetInstanceProcAddr gpa) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700163 return initInstanceTable(instance, gpa, tableInstanceMap);
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600164}
165
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700166VkLayerDispatchTable *initDeviceTable(VkDevice device, const PFN_vkGetDeviceProcAddr gpa, device_table_map &map) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700167 VkLayerDispatchTable *pTable;
168 dispatch_key key = get_dispatch_key(device);
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700169 device_table_map::const_iterator it = map.find((void *)key);
Jon Ashburn9eed2892015-06-01 10:02:09 -0600170
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700171 if (it == map.end()) {
172 pTable = new VkLayerDispatchTable;
173 map[(void *)key] = pTable;
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600174#if DISPATCH_MAP_DEBUG
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700175 fprintf(stderr, "New, Device: map: %p, key: %p, table: %p\n", &map, key, pTable);
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600176#endif
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700177 } else {
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600178#if DISPATCH_MAP_DEBUG
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700179 fprintf(stderr, "Device: map: %p, key: %p, table: %p\n", &map, key, it->second);
Courtney Goeltzenleuchtere3d3f5d2015-06-13 21:48:26 -0600180#endif
Jon Ashburn9eed2892015-06-01 10:02:09 -0600181 return it->second;
182 }
183
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700184 layer_init_device_dispatch_table(device, pTable, gpa);
Jon Ashburn9eed2892015-06-01 10:02:09 -0600185
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700186 return pTable;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600187}
188
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700189VkLayerDispatchTable *initDeviceTable(VkDevice device, const PFN_vkGetDeviceProcAddr gpa) {
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700190 return initDeviceTable(device, gpa, tableMap);
Jon Ashburn9eed2892015-06-01 10:02:09 -0600191}