blob: e6c67ed438e757ede74d727ccfd67abe1fa0b92c [file] [log] [blame]
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001#!/usr/bin/env python3
2#
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06003# VK
Tobin Ehlis92dbf802014-10-22 09:06:33 -06004#
5# Copyright (C) 2014 LunarG, Inc.
6#
7# Permission is hereby granted, free of charge, to any person obtaining a
8# copy of this software and associated documentation files (the "Software"),
9# to deal in the Software without restriction, including without limitation
10# the rights to use, copy, modify, merge, publish, distribute, sublicense,
11# and/or sell copies of the Software, and to permit persons to whom the
12# Software is furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included
15# in all copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23# DEALINGS IN THE SOFTWARE.
24#
25# Authors:
26# Chia-I Wu <olv@lunarg.com>
27
28import sys
Tobin Ehlis6cd06372014-12-17 17:44:50 -070029import os
Mark Lobodzinski4e5016f2015-05-05 15:01:37 -050030import re
Tobin Ehlis92dbf802014-10-22 09:06:33 -060031
Courtney Goeltzenleuchtera8c06282015-04-14 14:55:44 -060032import vulkan
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060033import vk_helper
Tobin Ehlisfde2fc32015-06-12 12:49:01 -060034from source_line_info import sourcelineinfo
Tobin Ehlis18cf7262015-08-26 11:22:09 -060035from collections import defaultdict
Tobin Ehlis92dbf802014-10-22 09:06:33 -060036
Jon Ashburn2666e2f2015-05-15 15:09:35 -060037def proto_is_global(proto):
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -060038 if proto.params[0].ty == "VkInstance" or proto.params[0].ty == "VkPhysicalDevice" or proto.name == "CreateInstance" or proto.name == "EnumerateInstanceLayerProperties" or proto.name == "EnumerateInstanceExtensionProperties" or proto.name == "EnumerateDeviceLayerProperties" or proto.name == "EnumerateDeviceExtensionProperties":
Jon Ashburn2666e2f2015-05-15 15:09:35 -060039 return True
40 else:
41 return False
42
Mike Stroyan7c2efaa2015-04-03 13:58:35 -060043def generate_get_proc_addr_check(name):
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060044 return " if (!%s || %s[0] != 'v' || %s[1] != 'k')\n" \
45 " return NULL;" % ((name,) * 3)
Mike Stroyan7c2efaa2015-04-03 13:58:35 -060046
Mark Lobodzinski4e5016f2015-05-05 15:01:37 -050047def ucc_to_U_C_C(CamelCase):
48 temp = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', CamelCase)
49 return re.sub('([a-z0-9])([A-Z])', r'\1_\2', temp).upper()
50
Tobin Ehlis92dbf802014-10-22 09:06:33 -060051class Subcommand(object):
52 def __init__(self, argv):
53 self.argv = argv
Courtney Goeltzenleuchtera8c06282015-04-14 14:55:44 -060054 self.headers = vulkan.headers
55 self.protos = vulkan.protos
Mike Stroyan3aecdb42015-04-03 17:13:23 -060056 self.no_addr = False
57 self.layer_name = ""
Tobin Ehlisfde2fc32015-06-12 12:49:01 -060058 self.lineinfo = sourcelineinfo()
Tobin Ehlis92dbf802014-10-22 09:06:33 -060059
60 def run(self):
Tobin Ehlis92dbf802014-10-22 09:06:33 -060061 print(self.generate())
62
63 def generate(self):
64 copyright = self.generate_copyright()
65 header = self.generate_header()
66 body = self.generate_body()
67 footer = self.generate_footer()
68
69 contents = []
70 if copyright:
71 contents.append(copyright)
72 if header:
73 contents.append(header)
74 if body:
75 contents.append(body)
76 if footer:
77 contents.append(footer)
78
79 return "\n\n".join(contents)
80
81 def generate_copyright(self):
82 return """/* THIS FILE IS GENERATED. DO NOT EDIT. */
83
84/*
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060085 * Vulkan
Tobin Ehlis92dbf802014-10-22 09:06:33 -060086 *
87 * Copyright (C) 2014 LunarG, Inc.
88 *
89 * Permission is hereby granted, free of charge, to any person obtaining a
90 * copy of this software and associated documentation files (the "Software"),
91 * to deal in the Software without restriction, including without limitation
92 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
93 * and/or sell copies of the Software, and to permit persons to whom the
94 * Software is furnished to do so, subject to the following conditions:
95 *
96 * The above copyright notice and this permission notice shall be included
97 * in all copies or substantial portions of the Software.
98 *
99 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
100 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
101 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
102 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
103 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
104 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
105 * DEALINGS IN THE SOFTWARE.
106 */"""
107
108 def generate_header(self):
109 return "\n".join(["#include <" + h + ">" for h in self.headers])
110
111 def generate_body(self):
112 pass
113
114 def generate_footer(self):
115 pass
116
117 # Return set of printf '%' qualifier and input to that qualifier
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600118 def _get_printf_params(self, vk_type, name, output_param, cpp=False):
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600119 # TODO : Need ENUM and STRUCT checks here
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600120 if vk_helper.is_type(vk_type, 'enum'):#"_TYPE" in vk_type: # TODO : This should be generic ENUM check
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600121 return ("%s", "string_%s(%s)" % (vk_type.replace('const ', '').strip('*'), name))
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600122 if "char*" == vk_type:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600123 return ("%s", name)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600124 if "uint64" in vk_type:
125 if '*' in vk_type:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600126 return ("%lu", "*%s" % name)
127 return ("%lu", name)
Tobin Ehlis0b9c1952015-07-06 14:02:36 -0600128 if vk_type.strip('*') in vulkan.object_non_dispatch_list:
129 if '*' in vk_type:
130 return ("%lu", "%s->handle" % name)
131 return ("%lu", "%s.handle" % name)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600132 if "size" in vk_type:
133 if '*' in vk_type:
Mark Lobodzinskib7c5b232015-10-06 09:57:52 -0600134 return ("%lu", "(unsigned long)*%s" % name)
135 return ("%lu", "(unsigned long)%s" % name)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600136 if "float" in vk_type:
137 if '[' in vk_type: # handle array, current hard-coded to 4 (TODO: Make this dynamic)
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700138 if cpp:
139 return ("[%i, %i, %i, %i]", '"[" << %s[0] << "," << %s[1] << "," << %s[2] << "," << %s[3] << "]"' % (name, name, name, name))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600140 return ("[%f, %f, %f, %f]", "%s[0], %s[1], %s[2], %s[3]" % (name, name, name, name))
141 return ("%f", name)
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -0600142 if "bool" in vk_type.lower() or 'xcb_randr_crtc_t' in vk_type:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600143 return ("%u", name)
Tobin Ehlisf29da382015-04-15 07:46:12 -0600144 if True in [t in vk_type.lower() for t in ["int", "flags", "mask", "xcb_window_t"]]:
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600145 if '[' in vk_type: # handle array, current hard-coded to 4 (TODO: Make this dynamic)
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700146 if cpp:
147 return ("[%i, %i, %i, %i]", "%s[0] << %s[1] << %s[2] << %s[3]" % (name, name, name, name))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600148 return ("[%i, %i, %i, %i]", "%s[0], %s[1], %s[2], %s[3]" % (name, name, name, name))
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600149 if '*' in vk_type:
Tobin Ehlis1336c8d2015-02-04 15:15:11 -0700150 if 'pUserData' == name:
151 return ("%i", "((pUserData == 0) ? 0 : *(pUserData))")
Tobin Ehlisa74d53a2015-04-17 13:26:33 -0600152 if 'const' in vk_type.lower():
153 return ("%p", "(void*)(%s)" % name)
Jon Ashburn1f7e2d72014-12-12 16:10:45 -0700154 return ("%i", "*(%s)" % name)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600155 return ("%i", name)
Tobin Ehlis0a1e06d2014-11-11 17:28:22 -0700156 # TODO : This is special-cased as there's only one "format" param currently and it's nice to expand it
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600157 if "VkFormat" == vk_type:
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700158 if cpp:
159 return ("%p", "&%s" % name)
Courtney Goeltzenleuchterab36aa62015-07-12 12:40:29 -0600160 return ("{%s.channelFormat = %%s, %s.numericFormat = %%s}" % (name, name), "string_VK_CHANNEL_FORMAT(%s.channelFormat), string_VK_FORMAT_NUM(%s.numericFormat)" % (name, name))
Tobin Ehlisa554dc32014-11-19 15:52:46 -0700161 if output_param:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600162 return ("%p", "(void*)*%s" % name)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600163 if vk_helper.is_type(vk_type, 'struct') and '*' not in vk_type:
Courtney Goeltzenleuchter9a1ded82015-04-03 16:35:32 -0600164 return ("%p", "(void*)(&%s)" % name)
Jon Ashburn1f7e2d72014-12-12 16:10:45 -0700165 return ("%p", "(void*)(%s)" % name)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600166
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600167 def _gen_create_msg_callback(self):
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700168 r_body = []
Tobin Ehlisfde2fc32015-06-12 12:49:01 -0600169 r_body.append('%s' % self.lineinfo.get())
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600170 r_body.append('VK_LAYER_EXPORT VkResult VKAPI vkDbgCreateMsgCallback(VkInstance instance, VkFlags msgFlags, const PFN_vkDbgMsgCallback pfnMsgCallback, void* pUserData, VkDbgMsgCallback* pMsgCallback)')
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700171 r_body.append('{')
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600172 # Switch to this code section for the new per-instance storage and debug callbacks
Mike Stroyan90a166e2015-08-10 16:42:53 -0600173 if self.layer_name == 'ObjectTracker' or self.layer_name == 'Threading':
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600174 r_body.append(' VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(%s_instance_table_map, instance);' % self.layer_name )
175 r_body.append(' VkResult result = pInstanceTable->DbgCreateMsgCallback(instance, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);')
176 r_body.append(' if (VK_SUCCESS == result) {')
177 r_body.append(' layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);')
178 r_body.append(' result = layer_create_msg_callback(my_data->report_data, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);')
179 r_body.append(' }')
180 r_body.append(' return result;')
181 else:
Jon Ashburn649d0ca2015-10-06 17:05:21 -0600182 r_body.append(' VkResult result = instance_dispatch_table(instance)->DbgCreateMsgCallback(instance, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);')
183 r_body.append(' if (VK_SUCCESS == result) {')
184 r_body.append(' layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);')
185 r_body.append(' result = layer_create_msg_callback(my_data->report_data, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);')
186 r_body.append(' }')
187 r_body.append(' return result;')
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700188 r_body.append('}')
189 return "\n".join(r_body)
190
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600191 def _gen_destroy_msg_callback(self):
192 r_body = []
Tobin Ehlisfde2fc32015-06-12 12:49:01 -0600193 r_body.append('%s' % self.lineinfo.get())
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600194 r_body.append('VK_LAYER_EXPORT VkResult VKAPI vkDbgDestroyMsgCallback(VkInstance instance, VkDbgMsgCallback msgCallback)')
195 r_body.append('{')
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600196 # Switch to this code section for the new per-instance storage and debug callbacks
Mike Stroyan90a166e2015-08-10 16:42:53 -0600197 if self.layer_name == 'ObjectTracker' or self.layer_name == 'Threading':
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600198 r_body.append(' VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(%s_instance_table_map, instance);' % self.layer_name )
199 r_body.append(' VkResult result = pInstanceTable->DbgDestroyMsgCallback(instance, msgCallback);')
200 r_body.append(' layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);')
201 r_body.append(' layer_destroy_msg_callback(my_data->report_data, msgCallback);')
202 r_body.append(' return result;')
203 else:
Jon Ashburn649d0ca2015-10-06 17:05:21 -0600204 r_body.append(' VkResult result = instance_dispatch_table(instance)->DbgDestroyMsgCallback(instance, msgCallback);')
205 r_body.append(' layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);')
206 r_body.append(' layer_destroy_msg_callback(my_data->report_data, msgCallback);')
207 r_body.append(' return result;')
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600208 r_body.append('}')
209 return "\n".join(r_body)
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700210
Tony Barbour426b9052015-06-24 16:06:58 -0600211 def _gen_layer_get_global_extension_props(self, layer="Generic"):
212 ggep_body = []
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600213 # generated layers do not provide any global extensions
214 ggep_body.append('%s' % self.lineinfo.get())
215
216 ggep_body.append('')
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600217 ggep_body.append('VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount, VkExtensionProperties* pProperties)')
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600218 ggep_body.append('{')
219 ggep_body.append(' return util_GetExtensionProperties(0, NULL, pCount, pProperties);')
220 ggep_body.append('}')
221 return "\n".join(ggep_body)
222
223 def _gen_layer_get_global_layer_props(self, layer="Generic"):
224 ggep_body = []
225 if layer == 'Generic':
226 # Do nothing, extension definition part of generic.h
Tony Barbour426b9052015-06-24 16:06:58 -0600227 ggep_body.append('%s' % self.lineinfo.get())
Courtney Goeltzenleuchter23b5f8d2015-06-17 20:51:59 -0600228 else:
Tony Barbour426b9052015-06-24 16:06:58 -0600229 ggep_body.append('%s' % self.lineinfo.get())
Courtney Goeltzenleuchter79d8d2c2015-07-07 17:51:45 -0600230 ggep_body.append('static const VkLayerProperties globalLayerProps[] = {')
Tony Barbour426b9052015-06-24 16:06:58 -0600231 ggep_body.append(' {')
Tony Barbour426b9052015-06-24 16:06:58 -0600232 ggep_body.append(' "%s",' % layer)
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600233 ggep_body.append(' VK_API_VERSION, // specVersion')
234 ggep_body.append(' VK_MAKE_VERSION(0, 1, 0), // implVersion')
Tony Barbour426b9052015-06-24 16:06:58 -0600235 ggep_body.append(' "layer: %s",' % layer)
236 ggep_body.append(' }')
237 ggep_body.append('};')
238 ggep_body.append('')
239 ggep_body.append('%s' % self.lineinfo.get())
Tony Barbour426b9052015-06-24 16:06:58 -0600240 ggep_body.append('')
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600241 ggep_body.append('VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceLayerProperties(uint32_t *pCount, VkLayerProperties* pProperties)')
Tony Barbour426b9052015-06-24 16:06:58 -0600242 ggep_body.append('{')
Courtney Goeltzenleuchter79d8d2c2015-07-07 17:51:45 -0600243 ggep_body.append(' return util_GetLayerProperties(ARRAY_SIZE(globalLayerProps), globalLayerProps, pCount, pProperties);')
Tony Barbour426b9052015-06-24 16:06:58 -0600244 ggep_body.append('}')
245 return "\n".join(ggep_body)
246
Courtney Goeltzenleuchter03f1c8c2015-07-06 09:11:12 -0600247 def _gen_layer_get_physical_device_layer_props(self, layer="Generic"):
248 gpdlp_body = []
249 if layer == 'Generic':
250 # Do nothing, extension definition part of generic.h
251 gpdlp_body.append('%s' % self.lineinfo.get())
252 else:
253 gpdlp_body.append('%s' % self.lineinfo.get())
Courtney Goeltzenleuchter79d8d2c2015-07-07 17:51:45 -0600254 gpdlp_body.append('static const VkLayerProperties deviceLayerProps[] = {')
Courtney Goeltzenleuchter03f1c8c2015-07-06 09:11:12 -0600255 gpdlp_body.append(' {')
256 gpdlp_body.append(' "%s",' % layer)
257 gpdlp_body.append(' VK_API_VERSION,')
258 gpdlp_body.append(' VK_MAKE_VERSION(0, 1, 0),')
259 gpdlp_body.append(' "layer: %s",' % layer)
260 gpdlp_body.append(' }')
261 gpdlp_body.append('};')
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600262 gpdlp_body.append('VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount, VkLayerProperties* pProperties)')
Courtney Goeltzenleuchter03f1c8c2015-07-06 09:11:12 -0600263 gpdlp_body.append('{')
Courtney Goeltzenleuchter79d8d2c2015-07-07 17:51:45 -0600264 gpdlp_body.append(' return util_GetLayerProperties(ARRAY_SIZE(deviceLayerProps), deviceLayerProps, pCount, pProperties);')
Courtney Goeltzenleuchter03f1c8c2015-07-06 09:11:12 -0600265 gpdlp_body.append('}')
266 gpdlp_body.append('')
267 return "\n".join(gpdlp_body)
268
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600269 def _generate_dispatch_entrypoints(self, qual=""):
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600270 if qual:
271 qual += " "
272
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600273 funcs = []
274 intercepted = []
275 for proto in self.protos:
Jon Ashburn1245cec2015-05-18 13:20:15 -0600276 if proto.name == "GetDeviceProcAddr" or proto.name == "GetInstanceProcAddr":
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600277 continue
Mike Stroyan88f0ecf2015-04-08 10:27:43 -0600278 else:
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600279 intercept = self.generate_intercept(proto, qual)
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600280 if intercept is None:
281 # fill in default intercept for certain entrypoints
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600282 if 'DbgCreateMsgCallback' == proto.name:
283 intercept = self._gen_layer_dbg_create_msg_callback()
284 elif 'DbgDestroyMsgCallback' == proto.name:
285 intercept = self._gen_layer_dbg_destroy_msg_callback()
286 elif 'CreateDevice' == proto.name:
287 funcs.append('/* CreateDevice HERE */')
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600288 elif 'EnumerateInstanceExtensionProperties' == proto.name:
Tony Barbour426b9052015-06-24 16:06:58 -0600289 intercept = self._gen_layer_get_global_extension_props(self.layer_name)
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600290 elif 'EnumerateInstanceLayerProperties' == proto.name:
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600291 intercept = self._gen_layer_get_global_layer_props(self.layer_name)
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600292 elif 'EnumerateDeviceLayerProperties' == proto.name:
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600293 intercept = self._gen_layer_get_physical_device_layer_props(self.layer_name)
Tony Barbour426b9052015-06-24 16:06:58 -0600294
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600295 if intercept is not None:
296 funcs.append(intercept)
Ian Elliott338dedb2015-08-21 15:09:33 -0600297 if not "KHR" in proto.name:
Jon Ashburn7e07faf2015-06-18 15:02:58 -0600298 intercepted.append(proto)
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600299
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600300 prefix="vk"
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600301 lookups = []
302 for proto in intercepted:
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600303 lookups.append("if (!strcmp(name, \"%s\"))" % proto.name)
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600304 lookups.append(" return (PFN_vkVoidFunction) %s%s;" %
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600305 (prefix, proto.name))
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600306
307 # add customized layer_intercept_proc
308 body = []
Tobin Ehlisfde2fc32015-06-12 12:49:01 -0600309 body.append('%s' % self.lineinfo.get())
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600310 body.append("static inline PFN_vkVoidFunction layer_intercept_proc(const char *name)")
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600311 body.append("{")
312 body.append(generate_get_proc_addr_check("name"))
313 body.append("")
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600314 body.append(" name += 2;")
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600315 body.append(" %s" % "\n ".join(lookups))
316 body.append("")
317 body.append(" return NULL;")
318 body.append("}")
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600319 # add layer_intercept_instance_proc
320 lookups = []
321 for proto in self.protos:
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600322 if not proto_is_global(proto):
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600323 continue
324
325 if not proto in intercepted:
326 continue
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600327 if proto.name == "CreateDevice":
328 continue
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600329 lookups.append("if (!strcmp(name, \"%s\"))" % proto.name)
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600330 lookups.append(" return (PFN_vkVoidFunction) %s%s;" % (prefix, proto.name))
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600331
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600332 body.append("static inline PFN_vkVoidFunction layer_intercept_instance_proc(const char *name)")
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600333 body.append("{")
334 body.append(generate_get_proc_addr_check("name"))
335 body.append("")
336 body.append(" name += 2;")
337 body.append(" %s" % "\n ".join(lookups))
338 body.append("")
339 body.append(" return NULL;")
340 body.append("}")
341
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600342 funcs.append("\n".join(body))
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600343 return "\n\n".join(funcs)
344
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700345 def _generate_extensions(self):
346 exts = []
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600347 exts.append('%s' % self.lineinfo.get())
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600348 exts.append(self._gen_create_msg_callback())
349 exts.append(self._gen_destroy_msg_callback())
Tobin Ehlisf29da382015-04-15 07:46:12 -0600350 return "\n".join(exts)
351
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600352 def _generate_layer_gpa_function(self, extensions=[], instance_extensions=[]):
Jon Ashburn301c5f02015-04-06 10:58:22 -0600353 func_body = []
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600354#
Courtney Goeltzenleuchter03f1c8c2015-07-06 09:11:12 -0600355# New style of GPA Functions for the new layer_data/layer_logging changes
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600356#
Mike Stroyan90a166e2015-08-10 16:42:53 -0600357 if self.layer_name == 'ObjectTracker' or self.layer_name == 'Threading':
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600358 func_body.append("VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr(VkDevice device, const char* funcName)\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600359 "{\n"
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600360 " PFN_vkVoidFunction addr;\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600361 " if (device == VK_NULL_HANDLE) {\n"
362 " return NULL;\n"
363 " }\n"
364 " /* loader uses this to force layer initialization; device object is wrapped */\n"
365 " if (!strcmp(\"vkGetDeviceProcAddr\", funcName)) {\n"
366 " initDeviceTable(%s_device_table_map, (const VkBaseLayerObject *) device);\n"
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600367 " return (PFN_vkVoidFunction) vkGetDeviceProcAddr;\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600368 " }\n\n"
369 " addr = layer_intercept_proc(funcName);\n"
370 " if (addr)\n"
371 " return addr;" % self.layer_name)
372 if 0 != len(extensions):
Courtney Goeltzenleuchter03f1c8c2015-07-06 09:11:12 -0600373 func_body.append('%s' % self.lineinfo.get())
374 func_body.append(' layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);')
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600375 for (ext_enable, ext_list) in extensions:
376 extra_space = ""
377 if 0 != len(ext_enable):
Courtney Goeltzenleuchter03f1c8c2015-07-06 09:11:12 -0600378 func_body.append(' if (my_device_data->%s) {' % ext_enable)
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600379 extra_space = " "
380 for ext_name in ext_list:
381 func_body.append(' %sif (!strcmp("%s", funcName))\n'
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600382 ' %sreturn reinterpret_cast<PFN_vkVoidFunction>(%s);' % (extra_space, ext_name, extra_space, ext_name))
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600383 if 0 != len(ext_enable):
384 func_body.append(' }\n')
385 func_body.append("\n if (get_dispatch_table(%s_device_table_map, device)->GetDeviceProcAddr == NULL)\n"
386 " return NULL;\n"
387 " return get_dispatch_table(%s_device_table_map, device)->GetDeviceProcAddr(device, funcName);\n"
388 "}\n" % (self.layer_name, self.layer_name))
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600389 func_body.append("VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName)\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600390 "{\n"
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600391 " PFN_vkVoidFunction addr;\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600392 " if (instance == VK_NULL_HANDLE) {\n"
393 " return NULL;\n"
394 " }\n"
395 " /* loader uses this to force layer initialization; instance object is wrapped */\n"
396 " if (!strcmp(\"vkGetInstanceProcAddr\", funcName)) {\n"
397 " initInstanceTable(%s_instance_table_map, (const VkBaseLayerObject *) instance);\n"
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600398 " return (PFN_vkVoidFunction) vkGetInstanceProcAddr;\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600399 " }\n\n"
400 " addr = layer_intercept_instance_proc(funcName);\n"
401 " if (addr) {\n"
402 " return addr;"
403 " }\n" % self.layer_name)
Jon Ashburn301c5f02015-04-06 10:58:22 -0600404
Jon Ashburnde4f1102015-09-17 10:00:32 -0600405 table_declared = False
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600406 if 0 != len(instance_extensions):
Jon Ashburnde4f1102015-09-17 10:00:32 -0600407 for (ext_enable, ext_list) in instance_extensions:
408 extra_space = ""
409 if 0 != len(ext_enable):
410 if ext_enable == 'msg_callback_get_proc_addr':
411 func_body.append(" layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600412 " addr = debug_report_get_instance_proc_addr(my_data->report_data, funcName);\n"
413 " if (addr) {\n"
414 " return addr;\n"
Jon Ashburnde4f1102015-09-17 10:00:32 -0600415 " }\n")
416 else:
417 if table_declared == False:
418 func_body.append(" VkLayerInstanceDispatchTable* pTable = get_dispatch_table(%s_instance_table_map, instance);" % self.layer_name)
419 table_declared = True
420 func_body.append(' if (instanceExtMap.size() != 0 && instanceExtMap[pTable].%s)' % ext_enable)
421 func_body.append(' {')
422 extra_space = " "
423 for ext_name in ext_list:
424 func_body.append(' %sif (!strcmp("%s", funcName))\n'
425 ' return reinterpret_cast<PFN_vkVoidFunction>(%s);' % (extra_space, ext_name, ext_name))
426 if 0 != len(ext_enable):
427 func_body.append(' }\n')
428
429 func_body.append(" if (get_dispatch_table(%s_instance_table_map, instance)->GetInstanceProcAddr == NULL) {\n"
430 " return NULL;\n"
431 " }\n"
432 " return get_dispatch_table(%s_instance_table_map, instance)->GetInstanceProcAddr(instance, funcName);\n"
433 "}\n" % (self.layer_name, self.layer_name))
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600434 return "\n".join(func_body)
435 else:
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600436 func_body.append('%s' % self.lineinfo.get())
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600437 func_body.append("VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr(VkDevice device, const char* funcName)\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600438 "{\n"
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600439 " PFN_vkVoidFunction addr;\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600440 " if (device == VK_NULL_HANDLE) {\n"
441 " return NULL;\n"
Jon Ashburn649d0ca2015-10-06 17:05:21 -0600442 " }\n")
443 if self.layer_name == 'Generic':
444 func_body.append("\n"
445 " /* loader uses this to force layer initialization; device object is wrapped */\n"
446 " if (!strcmp(\"vkGetDeviceProcAddr\", funcName)) {\n"
447 " initDeviceTable((const VkBaseLayerObject *) device);\n"
448 " return (PFN_vkVoidFunction) vkGetDeviceProcAddr;\n"
449 " }\n\n"
450 " addr = layer_intercept_proc(funcName);\n"
451 " if (addr)\n"
452 " return addr;")
453 else:
454 func_body.append("\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600455 " loader_platform_thread_once(&initOnce, init%s);\n\n"
456 " /* loader uses this to force layer initialization; device object is wrapped */\n"
457 " if (!strcmp(\"vkGetDeviceProcAddr\", funcName)) {\n"
458 " initDeviceTable((const VkBaseLayerObject *) device);\n"
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600459 " return (PFN_vkVoidFunction) vkGetDeviceProcAddr;\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600460 " }\n\n"
461 " addr = layer_intercept_proc(funcName);\n"
462 " if (addr)\n"
463 " return addr;" % self.layer_name)
464 func_body.append('')
465 func_body.append(' VkLayerDispatchTable *pDisp = device_dispatch_table(device);')
466 if 0 != len(extensions):
467 extra_space = ""
468 for (ext_enable, ext_list) in extensions:
469 if 0 != len(ext_enable):
Jon Ashburn83334db2015-09-16 18:08:32 -0600470 func_body.append(' if (deviceExtMap.size() != 0 && deviceExtMap[pDisp].%s)' % ext_enable)
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600471 func_body.append(' {')
472 extra_space = " "
473 for ext_name in ext_list:
474 func_body.append(' %sif (!strcmp("%s", funcName))\n'
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600475 ' return reinterpret_cast<PFN_vkVoidFunction>(%s);' % (extra_space, ext_name, ext_name))
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600476 if 0 != len(ext_enable):
477 func_body.append(' }')
478 func_body.append('%s' % self.lineinfo.get())
479 func_body.append(" {\n"
480 " if (pDisp->GetDeviceProcAddr == NULL)\n"
481 " return NULL;\n"
482 " return pDisp->GetDeviceProcAddr(device, funcName);\n"
483 " }\n"
484 "}\n")
Jon Ashburnde4f1102015-09-17 10:00:32 -0600485 func_body.append('%s' % self.lineinfo.get())
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600486 func_body.append("VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName)\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600487 "{\n"
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600488 " PFN_vkVoidFunction addr;\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600489 " if (instance == VK_NULL_HANDLE) {\n"
490 " return NULL;\n"
Jon Ashburn649d0ca2015-10-06 17:05:21 -0600491 " }\n")
492 if self.layer_name == 'Generic':
493 func_body.append("\n"
494 " /* loader uses this to force layer initialization; instance object is wrapped */\n"
495 " if (!strcmp(\"vkGetInstanceProcAddr\", funcName)) {\n"
496 " initInstanceTable((const VkBaseLayerObject *) instance);\n"
497 " return (PFN_vkVoidFunction) vkGetInstanceProcAddr;\n"
498 " }\n\n"
499 " addr = layer_intercept_instance_proc(funcName);\n"
500 " if (addr)\n"
501 " return addr;")
502 else:
503 func_body.append(
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600504 " loader_platform_thread_once(&initOnce, init%s);\n\n"
505 " /* loader uses this to force layer initialization; instance object is wrapped */\n"
506 " if (!strcmp(\"vkGetInstanceProcAddr\", funcName)) {\n"
507 " initInstanceTable((const VkBaseLayerObject *) instance);\n"
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600508 " return (PFN_vkVoidFunction) vkGetInstanceProcAddr;\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600509 " }\n\n"
510 " addr = layer_intercept_instance_proc(funcName);\n"
511 " if (addr)\n"
512 " return addr;" % self.layer_name)
Jon Ashburnde4f1102015-09-17 10:00:32 -0600513 func_body.append("")
514 func_body.append(" VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance);")
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600515 if 0 != len(instance_extensions):
Jon Ashburnde4f1102015-09-17 10:00:32 -0600516 extra_space = ""
517 for (ext_enable, ext_list) in instance_extensions:
518 if 0 != len(ext_enable):
Jon Ashburn649d0ca2015-10-06 17:05:21 -0600519 if ext_enable == 'msg_callback_get_proc_addr':
520 func_body.append(" layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);\n"
521 " addr = debug_report_get_instance_proc_addr(my_data->report_data, funcName);\n"
522 " if (addr) {\n"
523 " return addr;\n"
524 " }\n")
525 else:
526 func_body.append(' if (instanceExtMap.size() != 0 && instanceExtMap[pTable].%s)' % ext_enable)
527 func_body.append(' {')
528 extra_space = " "
529 for ext_name in ext_list:
530 func_body.append(' %sif (!strcmp("%s", funcName))\n'
Jon Ashburnde4f1102015-09-17 10:00:32 -0600531 ' return reinterpret_cast<PFN_vkVoidFunction>(%s);' % (extra_space, ext_name, ext_name))
Jon Ashburn649d0ca2015-10-06 17:05:21 -0600532 if 0 != len(ext_enable):
533 func_body.append(' }\n')
Jon Ashburnde4f1102015-09-17 10:00:32 -0600534
535 func_body.append(" if (pTable->GetInstanceProcAddr == NULL)\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600536 " return NULL;\n"
537 " return pTable->GetInstanceProcAddr(instance, funcName);\n"
538 "}\n")
539 return "\n".join(func_body)
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600540
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600541
Mike Stroyan354ed672015-05-15 08:50:57 -0600542 def _generate_layer_initialization(self, init_opts=False, prefix='vk', lockname=None, condname=None):
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600543 func_body = ["#include \"vk_dispatch_table_helper.h\""]
Tobin Ehlisfde2fc32015-06-12 12:49:01 -0600544 func_body.append('%s' % self.lineinfo.get())
Jon Ashburn649d0ca2015-10-06 17:05:21 -0600545 func_body.append('static void init%s(layer_data *my_data)\n'
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600546 '{\n' % self.layer_name)
Jon Ashburn21001f62015-02-16 08:26:50 -0700547 if init_opts:
Tobin Ehlisfde2fc32015-06-12 12:49:01 -0600548 func_body.append('%s' % self.lineinfo.get())
Jon Ashburn649d0ca2015-10-06 17:05:21 -0600549 func_body.append(' uint32_t report_flags = 0;')
550 func_body.append(' uint32_t debug_action = 0;')
551 func_body.append(' FILE *log_output = NULL;')
552 func_body.append(' const char *option_str;\n')
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600553 func_body.append(' // initialize %s options' % self.layer_name)
Jon Ashburn649d0ca2015-10-06 17:05:21 -0600554 func_body.append(' report_flags = getLayerOptionFlags("%sReportFlags", 0);' % self.layer_name)
555 func_body.append(' getLayerOptionEnum("%sDebugAction", (uint32_t *) &debug_action);' % self.layer_name)
Jon Ashburn21001f62015-02-16 08:26:50 -0700556 func_body.append('')
Jon Ashburn649d0ca2015-10-06 17:05:21 -0600557 func_body.append(' if (debug_action & VK_DBG_LAYER_ACTION_LOG_MSG)')
Jon Ashburn21001f62015-02-16 08:26:50 -0700558 func_body.append(' {')
Jon Ashburn649d0ca2015-10-06 17:05:21 -0600559 func_body.append(' option_str = getLayerOption("%sLogFilename");' % self.layer_name)
560 func_body.append(' log_output = getLayerLogOutput(option_str,"%s");' % self.layer_name)
561 func_body.append(' layer_create_msg_callback(my_data->report_data, report_flags,')
562 func_body.append(' log_callback, (void *) log_output,')
563 func_body.append(' &my_data->logging_callback);')
Jon Ashburn21001f62015-02-16 08:26:50 -0700564 func_body.append(' }')
565 func_body.append('')
Tobin Ehlis84a8a9b2015-02-23 14:09:16 -0700566 if lockname is not None:
Tobin Ehlisfde2fc32015-06-12 12:49:01 -0600567 func_body.append('%s' % self.lineinfo.get())
Tobin Ehlis84a8a9b2015-02-23 14:09:16 -0700568 func_body.append(" if (!%sLockInitialized)" % lockname)
569 func_body.append(" {")
570 func_body.append(" // TODO/TBD: Need to delete this mutex sometime. How???")
571 func_body.append(" loader_platform_thread_create_mutex(&%sLock);" % lockname)
Mike Stroyan354ed672015-05-15 08:50:57 -0600572 if condname is not None:
573 func_body.append(" loader_platform_thread_init_cond(&%sCond);" % condname)
Tobin Ehlis84a8a9b2015-02-23 14:09:16 -0700574 func_body.append(" %sLockInitialized = 1;" % lockname)
575 func_body.append(" }")
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600576 func_body.append("}\n")
Jon Ashburnd9564002015-05-07 10:27:37 -0600577 func_body.append('')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600578 return "\n".join(func_body)
579
Mike Stroyan90a166e2015-08-10 16:42:53 -0600580 def _generate_new_layer_initialization(self, init_opts=False, prefix='vk', lockname=None, condname=None):
581 func_body = ["#include \"vk_dispatch_table_helper.h\""]
582 func_body.append('%s' % self.lineinfo.get())
583 func_body.append('static void init%s(layer_data *my_data)\n'
584 '{\n' % self.layer_name)
585 if init_opts:
586 func_body.append('%s' % self.lineinfo.get())
587 func_body.append(' uint32_t report_flags = 0;')
588 func_body.append(' uint32_t debug_action = 0;')
589 func_body.append(' FILE *log_output = NULL;')
590 func_body.append(' const char *strOpt;')
591 func_body.append(' // initialize %s options' % self.layer_name)
592 func_body.append(' report_flags = getLayerOptionFlags("%sReportFlags", 0);' % self.layer_name)
Jon Ashburnf73135a2015-10-06 17:20:01 -0600593 func_body.append(' getLayerOptionEnum("%sDebugAction", (uint32_t *) &debug_action);' % self.layer_name)
Mike Stroyan90a166e2015-08-10 16:42:53 -0600594 func_body.append('')
595 func_body.append(' if (debug_action & VK_DBG_LAYER_ACTION_LOG_MSG)')
596 func_body.append(' {')
597 func_body.append(' strOpt = getLayerOption("%sLogFilename");' % self.layer_name)
Tobin Ehlisb4b6e7c2015-09-15 09:55:54 -0600598 func_body.append(' log_output = getLayerLogOutput(strOpt, "%s");' % self.layer_name)
Mike Stroyan90a166e2015-08-10 16:42:53 -0600599 func_body.append(' layer_create_msg_callback(my_data->report_data, report_flags, log_callback, (void *) log_output, &my_data->logging_callback);')
600 func_body.append(' }')
601 func_body.append('')
602 if lockname is not None:
603 func_body.append('%s' % self.lineinfo.get())
604 func_body.append(" if (!%sLockInitialized)" % lockname)
605 func_body.append(" {")
606 func_body.append(" // TODO/TBD: Need to delete this mutex sometime. How???")
607 func_body.append(" loader_platform_thread_create_mutex(&%sLock);" % lockname)
608 if condname is not None:
609 func_body.append(" loader_platform_thread_init_cond(&%sCond);" % condname)
610 func_body.append(" %sLockInitialized = 1;" % lockname)
611 func_body.append(" }")
612 func_body.append("}\n")
613 func_body.append('')
614 return "\n".join(func_body)
615
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600616class LayerFuncsSubcommand(Subcommand):
617 def generate_header(self):
Tobin Ehlis2d1d9702015-07-03 09:42:57 -0600618 return '#include <vk_layer.h>\n#include "loader.h"'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600619
620 def generate_body(self):
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600621 return self._generate_dispatch_entrypoints("static")
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600622
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600623class GenericLayerSubcommand(Subcommand):
624 def generate_header(self):
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -0500625 gen_header = []
Courtney Goeltzenleuchter79d8d2c2015-07-07 17:51:45 -0600626 gen_header.append('%s' % self.lineinfo.get())
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -0500627 gen_header.append('#include <stdio.h>')
628 gen_header.append('#include <stdlib.h>')
629 gen_header.append('#include <string.h>')
630 gen_header.append('#include <unordered_map>')
Tobin Ehlis7a51d902015-07-03 10:34:49 -0600631 gen_header.append('#include "vk_loader_platform.h"')
Tobin Ehlis2d1d9702015-07-03 09:42:57 -0600632 gen_header.append('#include "vk_layer.h"')
Tobin Ehlis56d204a2015-07-03 10:15:26 -0600633 gen_header.append('#include "vk_layer_config.h"')
Jon Ashburn649d0ca2015-10-06 17:05:21 -0600634 gen_header.append('#include "vk_layer_logging.h"')
Tobin Ehlis56d204a2015-07-03 10:15:26 -0600635 gen_header.append('#include "vk_layer_table.h"')
Courtney Goeltzenleuchter03f1c8c2015-07-06 09:11:12 -0600636 gen_header.append('#include "vk_layer_extension_utils.h"')
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -0500637 gen_header.append('')
Courtney Goeltzenleuchter79d8d2c2015-07-07 17:51:45 -0600638 gen_header.append('#include "generic.h"')
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600639 gen_header.append('')
Tobin Ehlis0b9c1952015-07-06 14:02:36 -0600640 gen_header.append('%s' % self.lineinfo.get())
641 gen_header.append('#define LAYER_EXT_ARRAY_SIZE 1')
642 gen_header.append('#define LAYER_DEV_EXT_ARRAY_SIZE 1')
Jon Ashburn649d0ca2015-10-06 17:05:21 -0600643 gen_header.append('//static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(initOnce);')
644 gen_header.append('static std::unordered_map<void *, layer_data *> layer_data_map;\n')
645 gen_header.append('template layer_data *get_my_data_ptr<layer_data>(')
646 gen_header.append(' void *data_key,')
647 gen_header.append(' std::unordered_map<void *, layer_data *> &data_map);\n')
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -0500648 gen_header.append('')
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -0500649 return "\n".join(gen_header)
Tobin Ehlis65d94ad2015-07-07 11:02:44 -0600650
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600651 def generate_intercept(self, proto, qual):
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600652 if proto.name in [ 'EnumerateInstanceLayerProperties', 'EnumerateInstanceExtensionProperties', 'EnumerateDeviceLayerProperties', 'EnumerateDeviceExtensionProperties' ]:
Mike Stroyan723913e2015-04-03 14:39:16 -0600653 # use default version
654 return None
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600655 decl = proto.c_func(prefix="vk", attr="VKAPI")
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600656 ret_val = ''
657 stmt = ''
658 funcs = []
Jon Ashburn52c1d732015-05-12 17:23:55 -0600659 table = ''
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600660 if proto_is_global(proto):
Jon Ashburn52c1d732015-05-12 17:23:55 -0600661 table = 'Instance'
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600662 if proto.ret != "void":
Courtney Goeltzenleuchter4843dda2015-06-26 15:07:53 -0600663 funcs.append('%s' % self.lineinfo.get())
Jon Ashburn53c16772015-05-06 10:15:07 -0600664 ret_val = "%s result = " % proto.ret
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600665 stmt = " return result;\n"
Jon Ashburn7cdb8c32015-05-22 12:01:50 -0600666 if proto.name == "CreateDevice":
Courtney Goeltzenleuchter4843dda2015-06-26 15:07:53 -0600667 funcs.append('%s' % self.lineinfo.get())
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600668 funcs.append('%s%s\n'
669 '{\n'
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -0500670 ' char str[1024];\n'
Jon Ashburn649d0ca2015-10-06 17:05:21 -0600671 ' layer_data *my_data = get_my_data_ptr(get_dispatch_key(physicalDevice), layer_data_map);\n'
672 ' sprintf(str, "At start of Generic layered %s\\n");\n'
673 ' log_msg(my_data->report_data, VK_DBG_REPORT_INFO_BIT, VK_OBJECT_TYPE_PHYSICAL_DEVICE,'
674 ' (uint64_t)physicalDevice, 0, 0, (char *) "Generic", (char *) str);\n'
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600675 ' %sdevice_dispatch_table(*pDevice)->%s;\n'
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600676 ' if (result == VK_SUCCESS) {\n'
Jon Ashburn649d0ca2015-10-06 17:05:21 -0600677 ' my_data->report_data = layer_debug_report_create_device(my_data->report_data, *pDevice);\n'
Jon Ashburn7e07faf2015-06-18 15:02:58 -0600678 ' createDeviceRegisterExtensions(pCreateInfo, *pDevice);\n'
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600679 ' }\n'
Jon Ashburn649d0ca2015-10-06 17:05:21 -0600680 ' sprintf(str, "Completed Generic layered %s\\n");\n'
681 ' log_msg(my_data->report_data, VK_DBG_REPORT_INFO_BIT, VK_OBJECT_TYPE_PHYSICAL_DEVICE, (uint64_t)physicalDevice, 0, 0, (char *) "Generic", (char *) str);\n'
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -0500682 ' %s'
683 '}' % (qual, decl, proto.name, ret_val, proto.c_call(), proto.name, stmt))
684 elif proto.name == "DestroyDevice":
Courtney Goeltzenleuchter4843dda2015-06-26 15:07:53 -0600685 funcs.append('%s' % self.lineinfo.get())
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -0500686 funcs.append('%s%s\n'
687 '{\n'
Courtney Goeltzenleuchter9f171942015-06-13 21:22:12 -0600688 ' dispatch_key key = get_dispatch_key(device);\n'
Jon Ashburn7e07faf2015-06-18 15:02:58 -0600689 ' VkLayerDispatchTable *pDisp = device_dispatch_table(device);\n'
Mark Lobodzinski67b42b72015-09-07 13:59:43 -0600690 ' pDisp->DestroyDevice(device);\n'
Jon Ashburn7e07faf2015-06-18 15:02:58 -0600691 ' deviceExtMap.erase(pDisp);\n'
Courtney Goeltzenleuchter9f171942015-06-13 21:22:12 -0600692 ' destroy_device_dispatch_table(key);\n'
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -0500693 '}\n' % (qual, decl))
694 elif proto.name == "DestroyInstance":
Courtney Goeltzenleuchter4843dda2015-06-26 15:07:53 -0600695 funcs.append('%s' % self.lineinfo.get())
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -0500696 funcs.append('%s%s\n'
697 '{\n'
Courtney Goeltzenleuchter9f171942015-06-13 21:22:12 -0600698 ' dispatch_key key = get_dispatch_key(instance);\n'
Jon Ashburnde4f1102015-09-17 10:00:32 -0600699 ' VkLayerInstanceDispatchTable *pDisp = instance_dispatch_table(instance);\n'
700 ' pDisp->DestroyInstance(instance);\n'
Jon Ashburn649d0ca2015-10-06 17:05:21 -0600701 ' // Clean up logging callback, if any\n'
702 ' layer_data *my_data = get_my_data_ptr(key, layer_data_map);\n'
703 ' if (my_data->logging_callback) {\n'
704 ' layer_destroy_msg_callback(my_data->report_data, my_data->logging_callback);\n'
705 ' }\n\n'
706 ' layer_debug_report_destroy_instance(my_data->report_data);\n'
707 ' layer_data_map.erase(key);\n'
Jon Ashburnde4f1102015-09-17 10:00:32 -0600708 ' instanceExtMap.erase(pDisp);\n'
Courtney Goeltzenleuchter9f171942015-06-13 21:22:12 -0600709 ' destroy_instance_dispatch_table(key);\n'
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -0500710 '}\n' % (qual, decl))
Jon Ashburnde4f1102015-09-17 10:00:32 -0600711 elif proto.name == "CreateInstance":
Courtney Goeltzenleuchter4843dda2015-06-26 15:07:53 -0600712 funcs.append('%s' % self.lineinfo.get())
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -0500713 # CreateInstance needs to use the second parm instead of the first to set the correct dispatch table
Jon Ashburnde4f1102015-09-17 10:00:32 -0600714 funcs.append('%s%s\n'
715 '{\n'
Jon Ashburn649d0ca2015-10-06 17:05:21 -0600716 ' char str[1024];\n'
Jon Ashburnde4f1102015-09-17 10:00:32 -0600717 ' %sinstance_dispatch_table(*pInstance)->%s;\n'
718 ' if (result == VK_SUCCESS) {\n'
719 ' createInstanceRegisterExtensions(pCreateInfo, *pInstance);\n'
Jon Ashburn649d0ca2015-10-06 17:05:21 -0600720 ' layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);\n'
721 ' my_data->report_data = debug_report_create_instance(\n'
722 ' instance_dispatch_table(*pInstance),\n'
723 ' *pInstance,\n'
724 ' pCreateInfo->extensionCount,\n'
725 ' pCreateInfo->ppEnabledExtensionNames);\n'
726 ' initGeneric(my_data);\n'
727 ' sprintf(str, "Completed Generic layered %s\\n");\n'
728 ' log_msg(my_data->report_data, VK_DBG_REPORT_INFO_BIT, VK_OBJECT_TYPE_INSTANCE, (uint64_t)*pInstance, 0, 0, (char *) "Generic", (char *) str);\n'
Jon Ashburnde4f1102015-09-17 10:00:32 -0600729 ' }\n'
Tobin Ehlis488e9202015-09-17 16:27:04 -0600730 ' return result;\n'
Jon Ashburn649d0ca2015-10-06 17:05:21 -0600731 '}\n' % (qual, decl, ret_val, proto.c_call(), proto.name))
Jon Ashburnde4f1102015-09-17 10:00:32 -0600732 else:
733 funcs.append('%s' % self.lineinfo.get())
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -0500734 dispatch_param = proto.params[0].name
735 # Must use 'instance' table for these APIs, 'device' table otherwise
736 table_type = ""
737 if proto_is_global(proto):
738 table_type = "instance"
739 else:
740 table_type = "device"
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600741 funcs.append('%s%s\n'
742 '{\n'
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -0500743 ' %s%s_dispatch_table(%s)->%s;\n'
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600744 '%s'
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -0500745 '}' % (qual, decl, ret_val, table_type, dispatch_param, proto.c_call(), stmt))
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600746 return "\n\n".join(funcs)
747
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600748 def generate_body(self):
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600749 self.layer_name = "Generic"
Jon Ashburn649d0ca2015-10-06 17:05:21 -0600750 instance_extensions=[('msg_callback_get_proc_addr', []),
751 ('wsi_enabled',
Jon Ashburnde4f1102015-09-17 10:00:32 -0600752 ['vkGetPhysicalDeviceSurfaceSupportKHR'])]
Ian Elliottb134e842015-07-06 14:31:32 -0600753 extensions=[('wsi_enabled',
Jon Ashburn83334db2015-09-16 18:08:32 -0600754 ['vkGetSurfacePropertiesKHR', 'vkGetSurfaceFormatsKHR',
755 'vkGetSurfacePresentModesKHR', 'vkCreateSwapchainKHR',
756 'vkDestroySwapchainKHR', 'vkGetSwapchainImagesKHR',
757 'vkAcquireNextImageKHR', 'vkQueuePresentKHR'])]
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600758 body = [self._generate_layer_initialization(True),
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600759 self._generate_dispatch_entrypoints("VK_LAYER_EXPORT"),
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600760 self._gen_create_msg_callback(),
761 self._gen_destroy_msg_callback(),
Jon Ashburnde4f1102015-09-17 10:00:32 -0600762 self._generate_layer_gpa_function(extensions, instance_extensions)]
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600763
764 return "\n\n".join(body)
765
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600766class APIDumpSubcommand(Subcommand):
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600767 def generate_header(self):
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700768 header_txt = []
Tobin Ehlisfde2fc32015-06-12 12:49:01 -0600769 header_txt.append('%s' % self.lineinfo.get())
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600770 header_txt.append('#include <fstream>')
771 header_txt.append('#include <iostream>')
772 header_txt.append('#include <string>')
Jon Ashburn7e07faf2015-06-18 15:02:58 -0600773 header_txt.append('#include <string.h>')
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600774 header_txt.append('')
Tobin Ehlisdc749502015-08-28 09:57:53 -0600775 header_txt.append('#include "vk_loader_platform.h"')
776 header_txt.append('#include "vk_layer.h"')
777 header_txt.append('#include "vk_struct_string_helper_cpp.h"')
778 header_txt.append('#include "vk_layer_table.h"')
779 header_txt.append('#include "vk_layer_extension_utils.h"')
780 header_txt.append('#include <unordered_map>')
Jon Ashburnd3e4a762015-09-17 15:28:12 -0600781 header_txt.append('#include "apidump.h"')
Tobin Ehlisdc749502015-08-28 09:57:53 -0600782 header_txt.append('')
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600783 header_txt.append('static std::ofstream fileStream;')
784 header_txt.append('static std::string fileName = "vk_apidump.txt";')
785 header_txt.append('std::ostream* outputStream = NULL;')
786 header_txt.append('void ConfigureOutputStream(bool writeToFile, bool flushAfterWrite)')
787 header_txt.append('{')
788 header_txt.append(' if(writeToFile)')
789 header_txt.append(' {')
Tobin Ehlisebd34682015-09-18 14:32:12 -0600790 header_txt.append(' if (fileName == "stdout")')
791 header_txt.append(' {')
Tobin Ehlisdc749502015-08-28 09:57:53 -0600792 header_txt.append(' outputStream = &std::cout;')
Tobin Ehlisebd34682015-09-18 14:32:12 -0600793 header_txt.append(' (*outputStream) << endl << "APIDump output filename \'stdout\' specified. Writing to STDOUT instead of a file." << endl << endl;')
794 header_txt.append(' } else {')
795 header_txt.append(' fileStream.open(fileName);')
796 header_txt.append(' if ((fileStream.rdstate() & fileStream.failbit) != 0) {')
797 header_txt.append(' outputStream = &std::cout;')
798 header_txt.append(' (*outputStream) << endl << "APIDump ERROR: Bad output filename specified: " << fileName << ". Writing to STDOUT instead" << endl << endl;')
799 header_txt.append(' }')
800 header_txt.append(' else')
801 header_txt.append(' outputStream = &fileStream;')
Tobin Ehlisdc749502015-08-28 09:57:53 -0600802 header_txt.append(' }')
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600803 header_txt.append(' }')
804 header_txt.append(' else')
805 header_txt.append(' {')
806 header_txt.append(' outputStream = &std::cout;')
807 header_txt.append(' }')
808 header_txt.append('')
809 header_txt.append(' if(flushAfterWrite)')
810 header_txt.append(' {')
811 header_txt.append(' outputStream->sync_with_stdio(true);')
812 header_txt.append(' }')
813 header_txt.append(' else')
814 header_txt.append(' {')
815 header_txt.append(' outputStream->sync_with_stdio(false);')
816 header_txt.append(' }')
817 header_txt.append('}')
818 header_txt.append('')
Tobin Ehlisfde2fc32015-06-12 12:49:01 -0600819 header_txt.append('%s' % self.lineinfo.get())
Jon Ashburn301c5f02015-04-06 10:58:22 -0600820 header_txt.append('static VkBaseLayerObject *pCurObj;')
Tobin Ehlis55c40412015-04-27 17:30:04 -0600821 header_txt.append('static bool g_APIDumpDetailed = true;')
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600822 header_txt.append('')
Jon Ashburnd9564002015-05-07 10:27:37 -0600823 header_txt.append('static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(initOnce);')
Mark Lobodzinskibf7c7002015-05-26 09:29:09 -0500824 header_txt.append('')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700825 header_txt.append('static int printLockInitialized = 0;')
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600826 header_txt.append('static loader_platform_thread_mutex printLock;')
827 header_txt.append('')
Tobin Ehlisfde2fc32015-06-12 12:49:01 -0600828 header_txt.append('%s' % self.lineinfo.get())
Tobin Ehlis0b9c1952015-07-06 14:02:36 -0600829 header_txt.append('#define LAYER_EXT_ARRAY_SIZE 1')
830 header_txt.append('#define LAYER_DEV_EXT_ARRAY_SIZE 1')
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700831 header_txt.append('#define MAX_TID 513')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700832 header_txt.append('static loader_platform_thread_id tidMapping[MAX_TID] = {0};')
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700833 header_txt.append('static uint32_t maxTID = 0;')
834 header_txt.append('// Map actual TID to an index value and return that index')
835 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
836 header_txt.append('static uint32_t getTIDIndex() {')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700837 header_txt.append(' loader_platform_thread_id tid = loader_platform_get_thread_id();')
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700838 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
839 header_txt.append(' if (tid == tidMapping[i])')
840 header_txt.append(' return i;')
841 header_txt.append(' }')
842 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
Ian Elliott81ac44c2015-01-13 17:52:38 -0700843 header_txt.append(' uint32_t retVal = (uint32_t) maxTID;')
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700844 header_txt.append(' tidMapping[maxTID++] = tid;')
845 header_txt.append(' assert(maxTID < MAX_TID);')
846 header_txt.append(' return retVal;')
847 header_txt.append('}')
Mark Lobodzinskibf7c7002015-05-26 09:29:09 -0500848 header_txt.append('')
Mark Lobodzinski98586a92015-07-30 10:11:32 -0600849 header_txt.append('void interpret_memBarriers(const void* const* ppMemBarriers, uint32_t memBarrierCount)')
850 header_txt.append('{')
851 header_txt.append(' if (ppMemBarriers) {')
852 header_txt.append(' string tmp_str;')
Mark Lobodzinski91ea0952015-07-31 13:47:42 -0600853 header_txt.append(' for (uint32_t i = 0; i < memBarrierCount; i++) {')
854 header_txt.append(' switch(*(VkStructureType*)ppMemBarriers[i])')
Mark Lobodzinski98586a92015-07-30 10:11:32 -0600855 header_txt.append(' {')
856 header_txt.append(' case VK_STRUCTURE_TYPE_MEMORY_BARRIER:')
Mark Lobodzinski91ea0952015-07-31 13:47:42 -0600857 header_txt.append(' tmp_str = vk_print_vkmemorybarrier((VkMemoryBarrier*)ppMemBarriers[i], " ");')
Mark Lobodzinski98586a92015-07-30 10:11:32 -0600858 header_txt.append(' break;')
859 header_txt.append(' case VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER:')
Mark Lobodzinski91ea0952015-07-31 13:47:42 -0600860 header_txt.append(' tmp_str = vk_print_vkbuffermemorybarrier((VkBufferMemoryBarrier*)ppMemBarriers[i], " ");')
Mark Lobodzinski98586a92015-07-30 10:11:32 -0600861 header_txt.append(' break;')
862 header_txt.append(' case VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER:')
Mark Lobodzinski91ea0952015-07-31 13:47:42 -0600863 header_txt.append(' tmp_str = vk_print_vkimagememorybarrier((VkImageMemoryBarrier*)ppMemBarriers[i], " ");')
Mark Lobodzinski98586a92015-07-30 10:11:32 -0600864 header_txt.append(' break;')
865 header_txt.append(' default:')
866 header_txt.append(' break;')
867 header_txt.append(' }')
868 header_txt.append('')
869 header_txt.append(' if (StreamControl::writeAddress == true) {')
870 header_txt.append(' (*outputStream) << " ppMemBarriers[" << i << "] (" << &ppMemBarriers[i] << ")" << endl << tmp_str << endl;')
871 header_txt.append(' } else {')
872 header_txt.append(' (*outputStream) << " ppMemBarriers[" << i << "] (address)" << endl << " address" << endl;')
873 header_txt.append(' }')
874 header_txt.append(' }')
875 header_txt.append(' }')
876 header_txt.append('}')
877 header_txt.append('')
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700878 return "\n".join(header_txt)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600879
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600880 def generate_init(self):
881 func_body = []
Tobin Ehlisfde2fc32015-06-12 12:49:01 -0600882 func_body.append('%s' % self.lineinfo.get())
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600883 func_body.append('#include "vk_dispatch_table_helper.h"')
Tobin Ehlis56d204a2015-07-03 10:15:26 -0600884 func_body.append('#include "vk_layer_config.h"')
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600885 func_body.append('')
886 func_body.append('static void init%s(void)' % self.layer_name)
887 func_body.append('{')
888 func_body.append(' using namespace StreamControl;')
889 func_body.append('')
Tobin Ehlis3234aba2015-08-26 16:18:52 -0600890 func_body.append(' char const*const logName = getLayerOption("APIDumpLogFilename");')
891 func_body.append(' if(logName != NULL)')
892 func_body.append(' {')
893 func_body.append(' fileName = logName;')
894 func_body.append(' }')
895 func_body.append('')
Tobin Ehlis55c40412015-04-27 17:30:04 -0600896 func_body.append(' char const*const detailedStr = getLayerOption("APIDumpDetailed");')
897 func_body.append(' if(detailedStr != NULL)')
898 func_body.append(' {')
899 func_body.append(' if(strcmp(detailedStr, "TRUE") == 0)')
900 func_body.append(' {')
901 func_body.append(' g_APIDumpDetailed = true;')
902 func_body.append(' }')
903 func_body.append(' else if(strcmp(detailedStr, "FALSE") == 0)')
904 func_body.append(' {')
905 func_body.append(' g_APIDumpDetailed = false;')
906 func_body.append(' }')
907 func_body.append(' }')
908 func_body.append('')
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600909 func_body.append(' char const*const writeToFileStr = getLayerOption("APIDumpFile");')
910 func_body.append(' bool writeToFile = false;')
911 func_body.append(' if(writeToFileStr != NULL)')
912 func_body.append(' {')
913 func_body.append(' if(strcmp(writeToFileStr, "TRUE") == 0)')
914 func_body.append(' {')
915 func_body.append(' writeToFile = true;')
916 func_body.append(' }')
917 func_body.append(' else if(strcmp(writeToFileStr, "FALSE") == 0)')
918 func_body.append(' {')
919 func_body.append(' writeToFile = false;')
920 func_body.append(' }')
921 func_body.append(' }')
922 func_body.append('')
Tobin Ehlisfde2fc32015-06-12 12:49:01 -0600923 func_body.append('%s' % self.lineinfo.get())
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600924 func_body.append(' char const*const noAddrStr = getLayerOption("APIDumpNoAddr");')
925 func_body.append(' if(noAddrStr != NULL)')
926 func_body.append(' {')
927 func_body.append(' if(strcmp(noAddrStr, "FALSE") == 0)')
928 func_body.append(' {')
929 func_body.append(' StreamControl::writeAddress = true;')
930 func_body.append(' }')
931 func_body.append(' else if(strcmp(noAddrStr, "TRUE") == 0)')
932 func_body.append(' {')
933 func_body.append(' StreamControl::writeAddress = false;')
934 func_body.append(' }')
935 func_body.append(' }')
936 func_body.append('')
937 func_body.append(' char const*const flushAfterWriteStr = getLayerOption("APIDumpFlush");')
938 func_body.append(' bool flushAfterWrite = false;')
939 func_body.append(' if(flushAfterWriteStr != NULL)')
940 func_body.append(' {')
941 func_body.append(' if(strcmp(flushAfterWriteStr, "TRUE") == 0)')
942 func_body.append(' {')
943 func_body.append(' flushAfterWrite = true;')
944 func_body.append(' }')
945 func_body.append(' else if(strcmp(flushAfterWriteStr, "FALSE") == 0)')
946 func_body.append(' {')
947 func_body.append(' flushAfterWrite = false;')
948 func_body.append(' }')
949 func_body.append(' }')
950 func_body.append('')
Tobin Ehlisfde2fc32015-06-12 12:49:01 -0600951 func_body.append('%s' % self.lineinfo.get())
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600952 func_body.append(' ConfigureOutputStream(writeToFile, flushAfterWrite);')
953 func_body.append('')
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600954 func_body.append(' if (!printLockInitialized)')
955 func_body.append(' {')
956 func_body.append(' // TODO/TBD: Need to delete this mutex sometime. How???')
957 func_body.append(' loader_platform_thread_create_mutex(&printLock);')
958 func_body.append(' printLockInitialized = 1;')
959 func_body.append(' }')
960 func_body.append('}')
961 func_body.append('')
962 return "\n".join(func_body)
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700963
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600964 def generate_intercept(self, proto, qual):
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600965 if proto.name in [ 'EnumerateInstanceLayerProperties','EnumerateInstanceExtensionProperties','EnumerateDeviceLayerProperties','EnumerateDeviceExtensionProperties']:
Jon Ashburneb2728b2015-04-10 14:33:07 -0600966 return None
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600967 decl = proto.c_func(prefix="vk", attr="VKAPI")
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600968 ret_val = ''
969 stmt = ''
970 funcs = []
971 sp_param_dict = {} # Store 'index' for struct param to print, or an name of binding "Count" param for array to print
972 create_params = 0 # Num of params at end of function that are created and returned as output values
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800973 if 'AllocDescriptorSets' in proto.name:
Cody Northropc8aa4a52015-08-03 12:47:29 -0600974 create_params = -1
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600975 elif 'Create' in proto.name or 'Alloc' in proto.name or 'MapMemory' in proto.name:
976 create_params = -1
977 if proto.ret != "void":
Jon Ashburn53c16772015-05-06 10:15:07 -0600978 ret_val = "%s result = " % proto.ret
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600979 stmt = " return result;\n"
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600980 f_open = 'loader_platform_thread_lock_mutex(&printLock);\n '
Tobin Ehlisfde2fc32015-06-12 12:49:01 -0600981 log_func = '%s\n' % self.lineinfo.get()
982 log_func += ' if (StreamControl::writeAddress == true) {'
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600983 log_func += '\n (*outputStream) << "t{" << getTIDIndex() << "} vk%s(' % proto.name
984 log_func_no_addr = '\n (*outputStream) << "t{" << getTIDIndex() << "} vk%s(' % proto.name
985 f_close = '\n loader_platform_thread_unlock_mutex(&printLock);'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600986 pindex = 0
987 prev_count_name = ''
988 for p in proto.params:
989 cp = False
990 if 0 != create_params:
991 # If this is any of the N last params of the func, treat as output
992 for y in range(-1, create_params-1, -1):
993 if p.name == proto.params[y].name:
994 cp = True
995 (pft, pfi) = self._get_printf_params(p.ty, p.name, cp, cpp=True)
Jon Ashburn83334db2015-09-16 18:08:32 -0600996 if p.name == "pSwapchain" or p.name == "pSwapchainImages":
997 log_func += '%s = " << ((%s == NULL) ? 0 : %s->handle) << ", ' % (p.name, p.name, p.name)
Ian Elliott338dedb2015-08-21 15:09:33 -0600998 elif p.name == "swapchain":
Ian Elliottb134e842015-07-06 14:31:32 -0600999 log_func += '%s = " << %s.handle << ", ' % (p.name, p.name)
1000 else:
1001 log_func += '%s = " << %s << ", ' % (p.name, pfi)
Tobin Ehlis4a636a12015-04-09 09:19:36 -06001002 if "%p" == pft:
1003 log_func_no_addr += '%s = address, ' % (p.name)
1004 else:
1005 log_func_no_addr += '%s = " << %s << ", ' % (p.name, pfi)
Tobin Ehlisc99cb0d2015-04-16 15:56:11 -06001006 if prev_count_name != '' and (prev_count_name.replace('Count', '')[1:] in p.name):
Mike Stroyan2ad66f12015-04-03 17:45:53 -06001007 sp_param_dict[pindex] = prev_count_name
Tobin Ehlisc99cb0d2015-04-16 15:56:11 -06001008 prev_count_name = ''
Chia-I Wu5b66aa52015-04-16 22:02:10 +08001009 elif vk_helper.is_type(p.ty.strip('*').replace('const ', ''), 'struct'):
Mike Stroyan2ad66f12015-04-03 17:45:53 -06001010 sp_param_dict[pindex] = 'index'
Mike Stroyan2ad66f12015-04-03 17:45:53 -06001011 if p.name.endswith('Count'):
1012 if '*' in p.ty:
1013 prev_count_name = "*%s" % p.name
1014 else:
1015 prev_count_name = p.name
Courtney Goeltzenleuchter70a0c752015-09-23 12:28:10 -06001016 else:
1017 prev_count_name = ''
Jon Ashburneb2728b2015-04-10 14:33:07 -06001018 pindex += 1
Mike Stroyan2ad66f12015-04-03 17:45:53 -06001019 log_func = log_func.strip(', ')
Tobin Ehlis4a636a12015-04-09 09:19:36 -06001020 log_func_no_addr = log_func_no_addr.strip(', ')
Jon Ashburn53c16772015-05-06 10:15:07 -06001021 if proto.ret == "VkResult":
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001022 log_func += ') = " << string_VkResult((VkResult)result) << endl'
1023 log_func_no_addr += ') = " << string_VkResult((VkResult)result) << endl'
Jon Ashburn53c16772015-05-06 10:15:07 -06001024 elif proto.ret == "void*":
1025 log_func += ') = " << result << endl'
1026 log_func_no_addr += ') = " << result << endl'
Mike Stroyan2ad66f12015-04-03 17:45:53 -06001027 else:
1028 log_func += ')\\n"'
Tobin Ehlis4a636a12015-04-09 09:19:36 -06001029 log_func_no_addr += ')\\n"'
Mike Stroyan2ad66f12015-04-03 17:45:53 -06001030 log_func += ';'
Tobin Ehlis4a636a12015-04-09 09:19:36 -06001031 log_func_no_addr += ';'
Tobin Ehlis0b9c1952015-07-06 14:02:36 -06001032 log_func += '\n }\n else {%s\n }' % log_func_no_addr;
Tobin Ehlisfde2fc32015-06-12 12:49:01 -06001033 log_func += '\n%s' % self.lineinfo.get()
Courtney Goeltzenleuchter70a0c752015-09-23 12:28:10 -06001034 # log_func += '\n// Proto %s has param_dict: %s' % (proto.name, sp_param_dict)
Mike Stroyan2ad66f12015-04-03 17:45:53 -06001035 if len(sp_param_dict) > 0:
Tobin Ehlis55c40412015-04-27 17:30:04 -06001036 indent = ' '
1037 log_func += '\n%sif (g_APIDumpDetailed) {' % indent
1038 indent += ' '
Mike Stroyan2ad66f12015-04-03 17:45:53 -06001039 i_decl = False
Tobin Ehlisfde2fc32015-06-12 12:49:01 -06001040 log_func += '\n%s' % self.lineinfo.get()
Tobin Ehlis55c40412015-04-27 17:30:04 -06001041 log_func += '\n%sstring tmp_str;' % indent
Mike Stroyan2ad66f12015-04-03 17:45:53 -06001042 for sp_index in sp_param_dict:
Courtney Goeltzenleuchter70a0c752015-09-23 12:28:10 -06001043 # log_func += '\n// sp_index: %s' % str(sp_index)
Mike Stroyan2ad66f12015-04-03 17:45:53 -06001044 if 'index' == sp_param_dict[sp_index]:
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001045 cis_print_func = 'vk_print_%s' % (proto.params[sp_index].ty.replace('const ', '').strip('*').lower())
Tobin Ehlis4a636a12015-04-09 09:19:36 -06001046 local_name = proto.params[sp_index].name
1047 if '*' not in proto.params[sp_index].ty:
1048 local_name = '&%s' % proto.params[sp_index].name
Tobin Ehlisfde2fc32015-06-12 12:49:01 -06001049 log_func += '\n%s' % self.lineinfo.get()
Tobin Ehlis55c40412015-04-27 17:30:04 -06001050 log_func += '\n%sif (%s) {' % (indent, local_name)
1051 indent += ' '
1052 log_func += '\n%stmp_str = %s(%s, " ");' % (indent, cis_print_func, local_name)
1053 log_func += '\n%s(*outputStream) << " %s (" << %s << ")" << endl << tmp_str << endl;' % (indent, local_name, local_name)
1054 indent = indent[4:]
1055 log_func += '\n%s}' % (indent)
Mark Lobodzinski98586a92015-07-30 10:11:32 -06001056 elif 'memBarrierCount' == sp_param_dict[sp_index]: # call helper function
1057 log_func += '\n%sif (ppMemBarriers) {' % (indent)
1058 log_func += '\n%s interpret_memBarriers(ppMemBarriers, memBarrierCount);' % (indent)
1059 log_func += '\n%s}' % (indent)
Mike Stroyan2ad66f12015-04-03 17:45:53 -06001060 else: # We have a count value stored to iterate over an array
1061 print_cast = ''
1062 print_func = ''
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001063 if vk_helper.is_type(proto.params[sp_index].ty.strip('*').replace('const ', ''), 'struct'):
Mike Stroyan2ad66f12015-04-03 17:45:53 -06001064 print_cast = '&'
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001065 print_func = 'vk_print_%s' % proto.params[sp_index].ty.replace('const ', '').strip('*').lower()
Mike Stroyan2ad66f12015-04-03 17:45:53 -06001066 else:
Tobin Ehlis4a636a12015-04-09 09:19:36 -06001067 print_cast = ''
Mike Stroyan2ad66f12015-04-03 17:45:53 -06001068 print_func = 'string_convert_helper'
1069 #cis_print_func = 'tmp_str = string_convert_helper((void*)%s[i], " ");' % proto.params[sp_index].name
Tobin Ehlis0b9c1952015-07-06 14:02:36 -06001070 if proto.params[sp_index].ty.strip('*').replace('const ', '') in vulkan.object_non_dispatch_list:
1071 cis_print_func = 'tmp_str = %s(%s%s[i].handle, " ");' % (print_func, print_cast, proto.params[sp_index].name)
1072 else:
1073 cis_print_func = 'tmp_str = %s(%s%s[i], " ");' % (print_func, print_cast, proto.params[sp_index].name)
Mike Stroyan2ad66f12015-04-03 17:45:53 -06001074 if not i_decl:
Tobin Ehlis55c40412015-04-27 17:30:04 -06001075 log_func += '\n%suint32_t i;' % (indent)
Mike Stroyan2ad66f12015-04-03 17:45:53 -06001076 i_decl = True
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001077 log_func += '\n%sif (%s) {' % (indent, proto.params[sp_index].name)
1078 indent += ' '
Tobin Ehlisfde2fc32015-06-12 12:49:01 -06001079 log_func += '\n%s' % self.lineinfo.get()
Tobin Ehlis55c40412015-04-27 17:30:04 -06001080 log_func += '\n%sfor (i = 0; i < %s; i++) {' % (indent, sp_param_dict[sp_index])
1081 indent += ' '
1082 log_func += '\n%s%s' % (indent, cis_print_func)
Tobin Ehlis8ad15742015-04-28 10:58:20 -06001083 log_func += '\n%sif (StreamControl::writeAddress == true) {' % (indent)
1084 indent += ' '
Tobin Ehlis55c40412015-04-27 17:30:04 -06001085 log_func += '\n%s(*outputStream) << " %s[" << i << "] (" << %s%s[i] << ")" << endl << tmp_str << endl;' % (indent, proto.params[sp_index].name, '&', proto.params[sp_index].name)
1086 indent = indent[4:]
Tobin Ehlis8ad15742015-04-28 10:58:20 -06001087 log_func += '\n%s} else {' % (indent)
1088 indent += ' '
1089 log_func += '\n%s(*outputStream) << " %s[" << i << "] (address)" << endl << " address" << endl;' % (indent, proto.params[sp_index].name)
1090 indent = indent[4:]
1091 log_func += '\n%s}' % (indent)
1092 indent = indent[4:]
Tobin Ehlis55c40412015-04-27 17:30:04 -06001093 log_func += '\n%s}' % (indent)
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001094 indent = indent[4:]
1095 log_func += '\n%s}' % (indent)
Tobin Ehlis55c40412015-04-27 17:30:04 -06001096 indent = indent[4:]
1097 log_func += '\n%s}' % (indent)
Mark Lobodzinskibf7c7002015-05-26 09:29:09 -05001098 table_type = ''
Jon Ashburn2666e2f2015-05-15 15:09:35 -06001099 if proto_is_global(proto):
Mark Lobodzinskibf7c7002015-05-26 09:29:09 -05001100 table_type = 'instance'
1101 else:
1102 table_type = 'device'
Jon Ashburnde4f1102015-09-17 10:00:32 -06001103 dispatch_param = proto.params[0].name
Jon Ashburn52c1d732015-05-12 17:23:55 -06001104
Mark Lobodzinskibf7c7002015-05-26 09:29:09 -05001105 if proto.name == "CreateInstance":
1106 dispatch_param = '*' + proto.params[1].name
Jon Ashburnde4f1102015-09-17 10:00:32 -06001107 funcs.append('%s%s\n'
1108 '{\n'
1109 ' using namespace StreamControl;\n'
1110 ' %sinstance_dispatch_table(*pInstance)->%s;\n'
1111 ' if (result == VK_SUCCESS) {\n'
1112 ' createInstanceRegisterExtensions(pCreateInfo, *pInstance);\n'
1113 ' }\n'
1114 ' %s%s%s\n'
1115 '%s'
1116 '}\n' % (qual, decl, ret_val, proto.c_call(), f_open, log_func, f_close, stmt))
Mark Lobodzinskibf7c7002015-05-26 09:29:09 -05001117
Jon Ashburnde4f1102015-09-17 10:00:32 -06001118 elif proto.name == "CreateDevice":
Courtney Goeltzenleuchter4843dda2015-06-26 15:07:53 -06001119 funcs.append('%s\n' % self.lineinfo.get())
Jon Ashburn7e07faf2015-06-18 15:02:58 -06001120 funcs.append('%s%s\n'
1121 '{\n'
1122 ' using namespace StreamControl;\n'
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -06001123 ' %sdevice_dispatch_table(*pDevice)->%s;\n'
Jon Ashburn7e07faf2015-06-18 15:02:58 -06001124 ' if (result == VK_SUCCESS)\n'
1125 ' createDeviceRegisterExtensions(pCreateInfo, *pDevice);\n'
1126 ' %s%s%s\n'
1127 '%s'
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -06001128 '}' % (qual, decl, ret_val, proto.c_call(), f_open, log_func, f_close, stmt))
Jon Ashburn7e07faf2015-06-18 15:02:58 -06001129 elif proto.name == "DestroyDevice":
Mark Lobodzinskibf7c7002015-05-26 09:29:09 -05001130 funcs.append('%s%s\n'
1131 '{\n'
1132 ' using namespace StreamControl;\n'
Courtney Goeltzenleuchter9f171942015-06-13 21:22:12 -06001133 ' dispatch_key key = get_dispatch_key(device);\n'
Jon Ashburnde4f1102015-09-17 10:00:32 -06001134 ' VkLayerDispatchTable *pDisp = %s_dispatch_table(%s);\n'
Jon Ashburn7e07faf2015-06-18 15:02:58 -06001135 ' %spDisp->%s;\n'
1136 ' deviceExtMap.erase(pDisp);\n'
Courtney Goeltzenleuchter9f171942015-06-13 21:22:12 -06001137 ' destroy_device_dispatch_table(key);\n'
Mark Lobodzinskibf7c7002015-05-26 09:29:09 -05001138 ' %s%s%s\n'
1139 '%s'
Jon Ashburn7e07faf2015-06-18 15:02:58 -06001140 '}' % (qual, decl, table_type, dispatch_param, ret_val, proto.c_call(), f_open, log_func, f_close, stmt))
Mark Lobodzinskibf7c7002015-05-26 09:29:09 -05001141 elif proto.name == "DestroyInstance":
1142 funcs.append('%s%s\n'
1143 '{\n'
1144 ' using namespace StreamControl;\n'
Courtney Goeltzenleuchter9f171942015-06-13 21:22:12 -06001145 ' dispatch_key key = get_dispatch_key(instance);\n'
Jon Ashburnde4f1102015-09-17 10:00:32 -06001146 ' VkLayerInstanceDispatchTable *pDisp = %s_dispatch_table(%s);\n'
1147 ' %spDisp->%s;\n'
1148 ' instanceExtMap.erase(pDisp);\n'
Courtney Goeltzenleuchter9f171942015-06-13 21:22:12 -06001149 ' destroy_instance_dispatch_table(key);\n'
Mark Lobodzinskibf7c7002015-05-26 09:29:09 -05001150 ' %s%s%s\n'
1151 '%s'
Jon Ashburnde4f1102015-09-17 10:00:32 -06001152 '}' % (qual, decl, table_type, dispatch_param, ret_val, proto.c_call(), f_open, log_func, f_close, stmt))
Mark Lobodzinskibf7c7002015-05-26 09:29:09 -05001153 else:
1154 funcs.append('%s%s\n'
1155 '{\n'
1156 ' using namespace StreamControl;\n'
1157 ' %s%s_dispatch_table(%s)->%s;\n'
Jon Ashburn301c5f02015-04-06 10:58:22 -06001158 ' %s%s%s\n'
1159 '%s'
Mark Lobodzinskibf7c7002015-05-26 09:29:09 -05001160 '}' % (qual, decl, ret_val, table_type, dispatch_param, proto.c_call(), f_open, log_func, f_close, stmt))
Mike Stroyan2ad66f12015-04-03 17:45:53 -06001161 return "\n\n".join(funcs)
1162
Tobin Ehlis434db7c2015-01-10 12:42:41 -07001163 def generate_body(self):
Tobin Ehlis4a636a12015-04-09 09:19:36 -06001164 self.layer_name = "APIDump"
Jon Ashburnde4f1102015-09-17 10:00:32 -06001165 instance_extensions=[('wsi_enabled',
1166 ['vkGetPhysicalDeviceSurfaceSupportKHR'])]
Ian Elliottb134e842015-07-06 14:31:32 -06001167 extensions=[('wsi_enabled',
Jon Ashburn83334db2015-09-16 18:08:32 -06001168 ['vkGetSurfacePropertiesKHR', 'vkGetSurfaceFormatsKHR',
1169 'vkGetSurfacePresentModesKHR', 'vkCreateSwapchainKHR',
1170 'vkDestroySwapchainKHR', 'vkGetSwapchainImagesKHR',
1171 'vkAcquireNextImageKHR', 'vkQueuePresentKHR'])]
Tobin Ehlis4a636a12015-04-09 09:19:36 -06001172 body = [self.generate_init(),
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001173 self._generate_dispatch_entrypoints("VK_LAYER_EXPORT"),
Jon Ashburnde4f1102015-09-17 10:00:32 -06001174 self._generate_layer_gpa_function(extensions, instance_extensions)]
Tobin Ehlis434db7c2015-01-10 12:42:41 -07001175 return "\n\n".join(body)
1176
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001177class ObjectTrackerSubcommand(Subcommand):
1178 def generate_header(self):
1179 header_txt = []
Tobin Ehlisfde2fc32015-06-12 12:49:01 -06001180 header_txt.append('%s' % self.lineinfo.get())
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001181 header_txt.append('#include <stdio.h>')
1182 header_txt.append('#include <stdlib.h>')
1183 header_txt.append('#include <string.h>')
1184 header_txt.append('#include <inttypes.h>')
1185 header_txt.append('')
1186 header_txt.append('#include "vulkan.h"')
Tobin Ehlis7a51d902015-07-03 10:34:49 -06001187 header_txt.append('#include "vk_loader_platform.h"')
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001188 header_txt.append('')
Tobin Ehlis586aa012015-06-08 17:36:28 -06001189 header_txt.append('#include <unordered_map>')
1190 header_txt.append('using namespace std;')
Jon Ashburnf73135a2015-10-06 17:20:01 -06001191 header_txt.append('#include "vk_layer.h"')
Tobin Ehlis56d204a2015-07-03 10:15:26 -06001192 header_txt.append('#include "vk_layer_config.h"')
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001193 header_txt.append('#include "vk_debug_report_lunarg.h"')
Tobin Ehlis56d204a2015-07-03 10:15:26 -06001194 header_txt.append('#include "vk_layer_table.h"')
1195 header_txt.append('#include "vk_layer_data.h"')
1196 header_txt.append('#include "vk_layer_logging.h"')
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001197 header_txt.append('')
1198# NOTE: The non-autoGenerated code is in the object_track.h header file
1199 header_txt.append('#include "object_track.h"')
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -05001200 header_txt.append('')
Jon Ashburnd9564002015-05-07 10:27:37 -06001201 header_txt.append('static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(initOnce);')
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -05001202 header_txt.append('')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001203 return "\n".join(header_txt)
1204
Tony Barbour2a199c12015-07-09 17:31:46 -06001205 def generate_maps(self):
1206 maps_txt = []
1207 for o in vulkan.core.objects:
1208 maps_txt.append('unordered_map<const void*, OBJTRACK_NODE*> %sMap;' % (o))
Ian Elliott338dedb2015-08-21 15:09:33 -06001209 maps_txt.append('unordered_map<const void*, OBJTRACK_NODE*> VkSwapchainKHRMap;')
Tony Barbour2a199c12015-07-09 17:31:46 -06001210 return "\n".join(maps_txt)
1211
1212 def generate_procs(self):
1213 procs_txt = []
1214 for o in vulkan.core.objects:
1215 procs_txt.append('%s' % self.lineinfo.get())
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001216 if o in vulkan.object_dispatch_list:
Tony Barbour2a199c12015-07-09 17:31:46 -06001217 procs_txt.append('static void create_obj(%s dispatchable_object, %s vkObj, VkDbgObjectType objType)' % (o, o))
1218 procs_txt.append('{')
Courtney Goeltzenleuchter0f2b9e22015-08-26 15:09:25 -06001219 procs_txt.append(' log_msg(mdd(dispatchable_object), VK_DBG_REPORT_INFO_BIT, objType, reinterpret_cast<uint64_t>(vkObj), 0, OBJTRACK_NONE, "OBJTRACK",')
Tony Barbour2a199c12015-07-09 17:31:46 -06001220 procs_txt.append(' "OBJ[%llu] : CREATE %s object 0x%" PRIxLEAST64 , object_track_index++, string_VkDbgObjectType(objType),')
Courtney Goeltzenleuchter0f2b9e22015-08-26 15:09:25 -06001221 procs_txt.append(' reinterpret_cast<uint64_t>(vkObj));')
Tony Barbour2a199c12015-07-09 17:31:46 -06001222 else:
1223 procs_txt.append('static void create_obj(VkDevice dispatchable_object, %s vkObj, VkDbgObjectType objType)' % (o))
1224 procs_txt.append('{')
1225 procs_txt.append(' log_msg(mdd(dispatchable_object), VK_DBG_REPORT_INFO_BIT, objType, vkObj.handle, 0, OBJTRACK_NONE, "OBJTRACK",')
1226 procs_txt.append(' "OBJ[%llu] : CREATE %s object 0x%" PRIxLEAST64 , object_track_index++, string_VkDbgObjectType(objType),')
Tony Barbour8db65372015-07-10 18:32:33 -06001227 procs_txt.append(' vkObj.handle);')
Tony Barbour2a199c12015-07-09 17:31:46 -06001228 procs_txt.append('')
1229 procs_txt.append(' OBJTRACK_NODE* pNewObjNode = new OBJTRACK_NODE;')
1230 procs_txt.append(' pNewObjNode->objType = objType;')
1231 procs_txt.append(' pNewObjNode->status = OBJSTATUS_NONE;')
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001232 if o in vulkan.object_dispatch_list:
Courtney Goeltzenleuchter0f2b9e22015-08-26 15:09:25 -06001233 procs_txt.append(' pNewObjNode->vkObj = reinterpret_cast<uint64_t>(vkObj);')
Tony Barbour2a199c12015-07-09 17:31:46 -06001234 procs_txt.append(' %sMap[vkObj] = pNewObjNode;' % (o))
1235 else:
Tony Barbour8db65372015-07-10 18:32:33 -06001236 procs_txt.append(' pNewObjNode->vkObj = vkObj.handle;')
Tony Barbour2a199c12015-07-09 17:31:46 -06001237 procs_txt.append(' %sMap[(void*)vkObj.handle] = pNewObjNode;' % (o))
1238 procs_txt.append(' uint32_t objIndex = objTypeToIndex(objType);')
1239 procs_txt.append(' numObjs[objIndex]++;')
1240 procs_txt.append(' numTotalObjs++;')
1241 procs_txt.append('}')
1242 procs_txt.append('')
1243 procs_txt.append('%s' % self.lineinfo.get())
Tobin Ehlis87f115c2015-09-15 15:02:17 -06001244 # TODO : This is not complete and currently requires some hand-coded function in the header
1245 # Really we want to capture the set of all objects and their associated dispatchable objects
1246 # that are bound by the API calls:
1247 # foreach API Call
1248 # foreach object type seen by call
1249 # create validate_object(disp_obj, object)
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001250 if o in vulkan.object_dispatch_list:
Tobin Ehlisf2f97402015-09-11 12:57:55 -06001251 procs_txt.append('static VkBool32 validate_object(%s dispatchable_object, %s object)' % (o, o))
Tony Barbour2a199c12015-07-09 17:31:46 -06001252 else:
Tobin Ehlisf2f97402015-09-11 12:57:55 -06001253 procs_txt.append('static VkBool32 validate_object(VkDevice dispatchable_object, %s object)' % (o))
Tony Barbour2a199c12015-07-09 17:31:46 -06001254 procs_txt.append('{')
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001255 if o in vulkan.object_dispatch_list:
Tony Barbour2a199c12015-07-09 17:31:46 -06001256 procs_txt.append(' if (%sMap.find(object) == %sMap.end()) {' % (o, o))
Tobin Ehlisf2f97402015-09-11 12:57:55 -06001257 procs_txt.append(' return log_msg(mdd(dispatchable_object), VK_DBG_REPORT_ERROR_BIT, (VkDbgObjectType) 0, reinterpret_cast<uint64_t>(object), 0, OBJTRACK_INVALID_OBJECT, "OBJTRACK",')
Courtney Goeltzenleuchter0f2b9e22015-08-26 15:09:25 -06001258 procs_txt.append(' "Invalid %s Object %%p",reinterpret_cast<uint64_t>(object));' % o)
Tony Barbour2a199c12015-07-09 17:31:46 -06001259 else:
Mark Lobodzinski61350002015-10-13 11:45:33 -06001260 if o == "VkPipelineCache":
1261 procs_txt.append(' // VkPipelineCache object can be NULL if not caching')
1262 procs_txt.append(' if (object == VK_NULL_HANDLE) return VK_TRUE;')
1263 procs_txt.append('')
Mark Lobodzinski9c483302015-10-14 13:16:33 -06001264 if o == "VkImage":
1265 procs_txt.append(' // We need to validate normal image objects and those from the swapchain')
1266 procs_txt.append(' if ((%sMap.find((void*)object.handle) == %sMap.end()) &&' % (o, o))
1267 procs_txt.append(' (swapchainImageMap.find((void*)object.handle) == swapchainImageMap.end())) {')
1268 else:
1269 procs_txt.append(' if (%sMap.find((void*)object.handle) == %sMap.end()) {' % (o, o))
Tobin Ehlisf2f97402015-09-11 12:57:55 -06001270 procs_txt.append(' return log_msg(mdd(dispatchable_object), VK_DBG_REPORT_ERROR_BIT, (VkDbgObjectType) 0, object.handle, 0, OBJTRACK_INVALID_OBJECT, "OBJTRACK",')
Tony Barbour8db65372015-07-10 18:32:33 -06001271 procs_txt.append(' "Invalid %s Object %%p", object.handle);' % o)
Tony Barbour2a199c12015-07-09 17:31:46 -06001272 procs_txt.append(' }')
Tobin Ehlisf2f97402015-09-11 12:57:55 -06001273 procs_txt.append(' return VK_FALSE;')
Tony Barbour2a199c12015-07-09 17:31:46 -06001274 procs_txt.append('}')
1275 procs_txt.append('')
1276 procs_txt.append('')
1277 procs_txt.append('%s' % self.lineinfo.get())
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001278 if o in vulkan.object_dispatch_list:
Tony Barbour2a199c12015-07-09 17:31:46 -06001279 procs_txt.append('static void destroy_obj(%s dispatchable_object, %s object)' % (o, o))
1280 else:
1281 procs_txt.append('static void destroy_obj(VkDevice dispatchable_object, %s object)' % (o))
1282 procs_txt.append('{')
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001283 if o in vulkan.object_dispatch_list:
Tony Barbour2a199c12015-07-09 17:31:46 -06001284 procs_txt.append(' if (%sMap.find(object) != %sMap.end()) {' % (o, o))
1285 procs_txt.append(' OBJTRACK_NODE* pNode = %sMap[object];' % (o))
1286 else:
1287 procs_txt.append(' if (%sMap.find((void*)object.handle) != %sMap.end()) {' % (o, o))
1288 procs_txt.append(' OBJTRACK_NODE* pNode = %sMap[(void*)object.handle];' % (o))
1289 procs_txt.append(' uint32_t objIndex = objTypeToIndex(pNode->objType);')
1290 procs_txt.append(' assert(numTotalObjs > 0);')
1291 procs_txt.append(' numTotalObjs--;')
1292 procs_txt.append(' assert(numObjs[objIndex] > 0);')
1293 procs_txt.append(' numObjs[objIndex]--;')
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001294 if o in vulkan.object_dispatch_list:
Courtney Goeltzenleuchter0f2b9e22015-08-26 15:09:25 -06001295 procs_txt.append(' log_msg(mdd(dispatchable_object), VK_DBG_REPORT_INFO_BIT, pNode->objType, reinterpret_cast<uint64_t>(object), 0, OBJTRACK_NONE, "OBJTRACK",')
Tony Barbour2a199c12015-07-09 17:31:46 -06001296 procs_txt.append(' "OBJ_STAT Destroy %s obj 0x%" PRIxLEAST64 " (%lu total objs remain & %lu %s objs).",')
Courtney Goeltzenleuchter0f2b9e22015-08-26 15:09:25 -06001297 procs_txt.append(' string_VkDbgObjectType(pNode->objType), reinterpret_cast<uint64_t>(object), numTotalObjs, numObjs[objIndex],')
Tony Barbour2a199c12015-07-09 17:31:46 -06001298 else:
1299 procs_txt.append(' log_msg(mdd(dispatchable_object), VK_DBG_REPORT_INFO_BIT, pNode->objType, object.handle, 0, OBJTRACK_NONE, "OBJTRACK",')
1300 procs_txt.append(' "OBJ_STAT Destroy %s obj 0x%" PRIxLEAST64 " (%lu total objs remain & %lu %s objs).",')
Tony Barbour8db65372015-07-10 18:32:33 -06001301 procs_txt.append(' string_VkDbgObjectType(pNode->objType), object.handle, numTotalObjs, numObjs[objIndex],')
Tony Barbour2a199c12015-07-09 17:31:46 -06001302 procs_txt.append(' string_VkDbgObjectType(pNode->objType));')
1303 procs_txt.append(' delete pNode;')
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001304 if o in vulkan.object_dispatch_list:
Tony Barbour2a199c12015-07-09 17:31:46 -06001305 procs_txt.append(' %sMap.erase(object);' % (o))
1306 procs_txt.append(' } else {')
Courtney Goeltzenleuchter0f2b9e22015-08-26 15:09:25 -06001307 procs_txt.append(' log_msg(mdd(dispatchable_object), VK_DBG_REPORT_ERROR_BIT, (VkDbgObjectType) 0, reinterpret_cast<uint64_t>(object), 0, OBJTRACK_NONE, "OBJTRACK",')
Tony Barbour2a199c12015-07-09 17:31:46 -06001308 procs_txt.append(' "Unable to remove obj 0x%" PRIxLEAST64 ". Was it created? Has it already been destroyed?",')
Courtney Goeltzenleuchter0f2b9e22015-08-26 15:09:25 -06001309 procs_txt.append(' reinterpret_cast<uint64_t>(object));')
Tony Barbour2a199c12015-07-09 17:31:46 -06001310 procs_txt.append(' }')
1311 else:
1312 procs_txt.append(' %sMap.erase((void*)object.handle);' % (o))
1313 procs_txt.append(' } else {')
1314 procs_txt.append(' log_msg(mdd(dispatchable_object), VK_DBG_REPORT_ERROR_BIT, (VkDbgObjectType) 0, object.handle, 0, OBJTRACK_NONE, "OBJTRACK",')
1315 procs_txt.append(' "Unable to remove obj 0x%" PRIxLEAST64 ". Was it created? Has it already been destroyed?",')
Tony Barbour8db65372015-07-10 18:32:33 -06001316 procs_txt.append(' object.handle);')
Tony Barbour2a199c12015-07-09 17:31:46 -06001317 procs_txt.append(' }')
1318 procs_txt.append('}')
1319 procs_txt.append('')
1320 procs_txt.append('%s' % self.lineinfo.get())
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001321 if o in vulkan.object_dispatch_list:
Tobin Ehlisf2f97402015-09-11 12:57:55 -06001322 procs_txt.append('static VkBool32 set_status(%s dispatchable_object, %s object, VkDbgObjectType objType, ObjectStatusFlags status_flag)' % (o, o))
Tony Barbour2a199c12015-07-09 17:31:46 -06001323 procs_txt.append('{')
1324 procs_txt.append(' if (object != VK_NULL_HANDLE) {')
1325 procs_txt.append(' if (%sMap.find(object) != %sMap.end()) {' % (o, o))
1326 procs_txt.append(' OBJTRACK_NODE* pNode = %sMap[object];' % (o))
1327 else:
Tobin Ehlisf2f97402015-09-11 12:57:55 -06001328 procs_txt.append('static VkBool32 set_status(VkDevice dispatchable_object, %s object, VkDbgObjectType objType, ObjectStatusFlags status_flag)' % (o))
Tony Barbour2a199c12015-07-09 17:31:46 -06001329 procs_txt.append('{')
Dana Jansens4ff0df72015-07-30 13:21:41 -07001330 procs_txt.append(' if (object != VK_NULL_HANDLE) {')
Tony Barbour2a199c12015-07-09 17:31:46 -06001331 procs_txt.append(' if (%sMap.find((void*)object.handle) != %sMap.end()) {' % (o, o))
1332 procs_txt.append(' OBJTRACK_NODE* pNode = %sMap[(void*)object.handle];' % (o))
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001333 procs_txt.append(' pNode->status |= status_flag;')
Tony Barbour2a199c12015-07-09 17:31:46 -06001334 procs_txt.append(' }')
1335 procs_txt.append(' else {')
1336 procs_txt.append(' // If we do not find it print an error')
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001337 if o in vulkan.object_dispatch_list:
Tobin Ehlisf2f97402015-09-11 12:57:55 -06001338 procs_txt.append(' return log_msg(mdd(dispatchable_object), VK_DBG_REPORT_ERROR_BIT, (VkDbgObjectType) 0, reinterpret_cast<uint64_t>(object), 0, OBJTRACK_NONE, "OBJTRACK",')
Tony Barbour2a199c12015-07-09 17:31:46 -06001339 procs_txt.append(' "Unable to set status for non-existent object 0x%" PRIxLEAST64 " of %s type",')
Courtney Goeltzenleuchter0f2b9e22015-08-26 15:09:25 -06001340 procs_txt.append(' reinterpret_cast<uint64_t>(object), string_VkDbgObjectType(objType));')
Tony Barbour2a199c12015-07-09 17:31:46 -06001341 else:
Tobin Ehlisf2f97402015-09-11 12:57:55 -06001342 procs_txt.append(' return log_msg(mdd(dispatchable_object), VK_DBG_REPORT_ERROR_BIT, (VkDbgObjectType) 0, object.handle, 0, OBJTRACK_NONE, "OBJTRACK",')
Tony Barbour2a199c12015-07-09 17:31:46 -06001343 procs_txt.append(' "Unable to set status for non-existent object 0x%" PRIxLEAST64 " of %s type",')
Tony Barbour8db65372015-07-10 18:32:33 -06001344 procs_txt.append(' object.handle, string_VkDbgObjectType(objType));')
Tony Barbour2a199c12015-07-09 17:31:46 -06001345 procs_txt.append(' }')
1346 procs_txt.append(' }')
Tobin Ehlisf2f97402015-09-11 12:57:55 -06001347 procs_txt.append(' return VK_FALSE;')
Tony Barbour2a199c12015-07-09 17:31:46 -06001348 procs_txt.append('}')
1349 procs_txt.append('')
1350 procs_txt.append('%s' % self.lineinfo.get())
1351 procs_txt.append('static VkBool32 validate_status(')
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001352 if o in vulkan.object_dispatch_list:
Tony Barbour2a199c12015-07-09 17:31:46 -06001353 procs_txt.append('%s dispatchable_object, %s object,' % (o, o))
1354 else:
1355 procs_txt.append('VkDevice dispatchable_object, %s object,' % (o))
1356 procs_txt.append(' VkDbgObjectType objType,')
1357 procs_txt.append(' ObjectStatusFlags status_mask,')
1358 procs_txt.append(' ObjectStatusFlags status_flag,')
1359 procs_txt.append(' VkFlags msg_flags,')
1360 procs_txt.append(' OBJECT_TRACK_ERROR error_code,')
1361 procs_txt.append(' const char *fail_msg)')
1362 procs_txt.append('{')
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001363 if o in vulkan.object_dispatch_list:
Tony Barbour2a199c12015-07-09 17:31:46 -06001364 procs_txt.append(' if (%sMap.find(object) != %sMap.end()) {' % (o, o))
1365 procs_txt.append(' OBJTRACK_NODE* pNode = %sMap[object];' % (o))
1366 else:
1367 procs_txt.append(' if (%sMap.find((void*)object.handle) != %sMap.end()) {' % (o, o))
1368 procs_txt.append(' OBJTRACK_NODE* pNode = %sMap[(void*)object.handle];' % (o))
1369 procs_txt.append(' if ((pNode->status & status_mask) != status_flag) {')
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001370 if o in vulkan.object_dispatch_list:
Courtney Goeltzenleuchter0f2b9e22015-08-26 15:09:25 -06001371 procs_txt.append(' log_msg(mdd(dispatchable_object), msg_flags, pNode->objType, reinterpret_cast<uint64_t>(object), 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK",')
Tony Barbour2a199c12015-07-09 17:31:46 -06001372 procs_txt.append(' "OBJECT VALIDATION WARNING: %s object 0x%" PRIxLEAST64 ": %s", string_VkDbgObjectType(objType),')
Courtney Goeltzenleuchter0f2b9e22015-08-26 15:09:25 -06001373 procs_txt.append(' reinterpret_cast<uint64_t>(object), fail_msg);')
Tony Barbour2a199c12015-07-09 17:31:46 -06001374 else:
1375 procs_txt.append(' log_msg(mdd(dispatchable_object), msg_flags, pNode->objType, object.handle, 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK",')
1376 procs_txt.append(' "OBJECT VALIDATION WARNING: %s object 0x%" PRIxLEAST64 ": %s", string_VkDbgObjectType(objType),')
Tony Barbour8db65372015-07-10 18:32:33 -06001377 procs_txt.append(' object.handle, fail_msg);')
Tony Barbour2a199c12015-07-09 17:31:46 -06001378 procs_txt.append(' return VK_FALSE;')
1379 procs_txt.append(' }')
1380 procs_txt.append(' return VK_TRUE;')
1381 procs_txt.append(' }')
1382 procs_txt.append(' else {')
1383 procs_txt.append(' // If we do not find it print an error')
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001384 if o in vulkan.object_dispatch_list:
Courtney Goeltzenleuchter0f2b9e22015-08-26 15:09:25 -06001385 procs_txt.append(' log_msg(mdd(dispatchable_object), msg_flags, (VkDbgObjectType) 0, reinterpret_cast<uint64_t>(object), 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK",')
Tony Barbour2a199c12015-07-09 17:31:46 -06001386 procs_txt.append(' "Unable to obtain status for non-existent object 0x%" PRIxLEAST64 " of %s type",')
Courtney Goeltzenleuchter0f2b9e22015-08-26 15:09:25 -06001387 procs_txt.append(' reinterpret_cast<uint64_t>(object), string_VkDbgObjectType(objType));')
Tony Barbour2a199c12015-07-09 17:31:46 -06001388 else:
1389 procs_txt.append(' log_msg(mdd(dispatchable_object), msg_flags, (VkDbgObjectType) 0, object.handle, 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK",')
1390 procs_txt.append(' "Unable to obtain status for non-existent object 0x%" PRIxLEAST64 " of %s type",')
Tony Barbour8db65372015-07-10 18:32:33 -06001391 procs_txt.append(' object.handle, string_VkDbgObjectType(objType));')
Tony Barbour2a199c12015-07-09 17:31:46 -06001392 procs_txt.append(' return VK_FALSE;')
1393 procs_txt.append(' }')
1394 procs_txt.append('}')
1395 procs_txt.append('')
1396 procs_txt.append('%s' % self.lineinfo.get())
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001397 if o in vulkan.object_dispatch_list:
Tobin Ehlisf2f97402015-09-11 12:57:55 -06001398 procs_txt.append('static VkBool32 reset_status(%s dispatchable_object, %s object, VkDbgObjectType objType, ObjectStatusFlags status_flag)' % (o, o))
Tony Barbour2a199c12015-07-09 17:31:46 -06001399 procs_txt.append('{')
1400 procs_txt.append(' if (%sMap.find(object) != %sMap.end()) {' % (o, o))
1401 procs_txt.append(' OBJTRACK_NODE* pNode = %sMap[object];' % (o))
1402 else:
Tobin Ehlisf2f97402015-09-11 12:57:55 -06001403 procs_txt.append('static VkBool32 reset_status(VkDevice dispatchable_object, %s object, VkDbgObjectType objType, ObjectStatusFlags status_flag)' % (o))
Tony Barbour2a199c12015-07-09 17:31:46 -06001404 procs_txt.append('{')
1405 procs_txt.append(' if (%sMap.find((void*)object.handle) != %sMap.end()) {' % (o, o))
1406 procs_txt.append(' OBJTRACK_NODE* pNode = %sMap[(void*)object.handle];' % (o))
1407 procs_txt.append(' pNode->status &= ~status_flag;')
Tony Barbour2a199c12015-07-09 17:31:46 -06001408 procs_txt.append(' }')
1409 procs_txt.append(' else {')
1410 procs_txt.append(' // If we do not find it print an error')
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001411 if o in vulkan.object_dispatch_list:
Tobin Ehlisf2f97402015-09-11 12:57:55 -06001412 procs_txt.append(' return log_msg(mdd(dispatchable_object), VK_DBG_REPORT_ERROR_BIT, objType, reinterpret_cast<uint64_t>(object), 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK",')
Tony Barbour2a199c12015-07-09 17:31:46 -06001413 procs_txt.append(' "Unable to reset status for non-existent object 0x%" PRIxLEAST64 " of %s type",')
Courtney Goeltzenleuchter0f2b9e22015-08-26 15:09:25 -06001414 procs_txt.append(' reinterpret_cast<uint64_t>(object), string_VkDbgObjectType(objType));')
Tony Barbour2a199c12015-07-09 17:31:46 -06001415 else:
Tobin Ehlisf2f97402015-09-11 12:57:55 -06001416 procs_txt.append(' return log_msg(mdd(dispatchable_object), VK_DBG_REPORT_ERROR_BIT, objType, object.handle, 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK",')
Tony Barbour2a199c12015-07-09 17:31:46 -06001417 procs_txt.append(' "Unable to reset status for non-existent object 0x%" PRIxLEAST64 " of %s type",')
Tony Barbour8db65372015-07-10 18:32:33 -06001418 procs_txt.append(' object.handle, string_VkDbgObjectType(objType));')
Tony Barbour2a199c12015-07-09 17:31:46 -06001419 procs_txt.append(' }')
Tobin Ehlisf2f97402015-09-11 12:57:55 -06001420 procs_txt.append(' return VK_FALSE;')
Tony Barbour2a199c12015-07-09 17:31:46 -06001421 procs_txt.append('}')
1422 procs_txt.append('')
1423 return "\n".join(procs_txt)
1424
Mark Lobodzinski4a611132015-07-17 11:51:24 -06001425 def generate_destroy_instance(self):
Tony Barbour2a199c12015-07-09 17:31:46 -06001426 gedi_txt = []
1427 gedi_txt.append('%s' % self.lineinfo.get())
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001428 gedi_txt.append('void vkDestroyInstance(')
Tony Barbour2a199c12015-07-09 17:31:46 -06001429 gedi_txt.append('VkInstance instance)')
1430 gedi_txt.append('{')
1431 gedi_txt.append(' loader_platform_thread_lock_mutex(&objLock);')
1432 gedi_txt.append(' validate_object(instance, instance);')
1433 gedi_txt.append('')
1434 gedi_txt.append(' destroy_obj(instance, instance);')
1435 gedi_txt.append(' // Report any remaining objects in LL')
1436 for o in vulkan.core.objects:
Mike Stroyan09b75852015-08-18 14:48:34 -06001437 if o in ['VkInstance', 'VkPhysicalDevice', 'VkQueue']:
Tony Barbour2a199c12015-07-09 17:31:46 -06001438 continue
1439 gedi_txt.append(' for (auto it = %sMap.begin(); it != %sMap.end(); ++it) {' % (o, o))
1440 gedi_txt.append(' OBJTRACK_NODE* pNode = it->second;')
1441 gedi_txt.append(' log_msg(mid(instance), VK_DBG_REPORT_ERROR_BIT, pNode->objType, pNode->vkObj, 0, OBJTRACK_OBJECT_LEAK, "OBJTRACK",')
1442 gedi_txt.append(' "OBJ ERROR : %s object 0x%" PRIxLEAST64 " has not been destroyed.", string_VkDbgObjectType(pNode->objType),')
Tony Barbour8db65372015-07-10 18:32:33 -06001443 gedi_txt.append(' pNode->vkObj);')
Tony Barbour2a199c12015-07-09 17:31:46 -06001444 gedi_txt.append(' }')
Mike Stroyan09b75852015-08-18 14:48:34 -06001445 gedi_txt.append(' %sMap.clear();' % (o))
Tony Barbour2a199c12015-07-09 17:31:46 -06001446 gedi_txt.append('')
1447 gedi_txt.append(' dispatch_key key = get_dispatch_key(instance);')
1448 gedi_txt.append(' VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(ObjectTracker_instance_table_map, instance);')
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001449 gedi_txt.append(' pInstanceTable->DestroyInstance(instance);')
Tony Barbour2a199c12015-07-09 17:31:46 -06001450 gedi_txt.append('')
1451 gedi_txt.append(' // Clean up logging callback, if any')
1452 gedi_txt.append(' layer_data *my_data = get_my_data_ptr(key, layer_data_map);')
1453 gedi_txt.append(' if (my_data->logging_callback) {')
1454 gedi_txt.append(' layer_destroy_msg_callback(my_data->report_data, my_data->logging_callback);')
1455 gedi_txt.append(' }')
1456 gedi_txt.append('')
1457 gedi_txt.append(' layer_debug_report_destroy_instance(mid(instance));')
1458 gedi_txt.append(' layer_data_map.erase(pInstanceTable);')
1459 gedi_txt.append('')
Jon Ashburnde4f1102015-09-17 10:00:32 -06001460 gedi_txt.append(' instanceExtMap.erase(pInstanceTable);')
Tony Barbour2a199c12015-07-09 17:31:46 -06001461 gedi_txt.append(' loader_platform_thread_unlock_mutex(&objLock);')
Mike Stroyan09b75852015-08-18 14:48:34 -06001462 # The loader holds a mutex that protects this from other threads
1463 gedi_txt.append(' ObjectTracker_instance_table_map.erase(key);')
1464 gedi_txt.append(' if (ObjectTracker_instance_table_map.empty()) {')
1465 gedi_txt.append(' // Release mutex when destroying last instance.')
1466 gedi_txt.append(' loader_platform_thread_delete_mutex(&objLock);')
1467 gedi_txt.append(' objLockInitialized = 0;')
1468 gedi_txt.append(' }')
Tony Barbour2a199c12015-07-09 17:31:46 -06001469 gedi_txt.append('}')
1470 gedi_txt.append('')
1471 return "\n".join(gedi_txt)
1472
Mark Lobodzinski4a611132015-07-17 11:51:24 -06001473 def generate_destroy_device(self):
Tony Barbour2a199c12015-07-09 17:31:46 -06001474 gedd_txt = []
1475 gedd_txt.append('%s' % self.lineinfo.get())
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001476 gedd_txt.append('void vkDestroyDevice(')
Tony Barbour2a199c12015-07-09 17:31:46 -06001477 gedd_txt.append('VkDevice device)')
1478 gedd_txt.append('{')
1479 gedd_txt.append(' loader_platform_thread_lock_mutex(&objLock);')
1480 gedd_txt.append(' validate_object(device, device);')
1481 gedd_txt.append('')
1482 gedd_txt.append(' destroy_obj(device, device);')
1483 gedd_txt.append(' // Report any remaining objects in LL')
1484 for o in vulkan.core.objects:
Mike Stroyan09b75852015-08-18 14:48:34 -06001485 if o in ['VkInstance', 'VkPhysicalDevice', 'VkQueue', 'VkDevice']:
Tony Barbour2a199c12015-07-09 17:31:46 -06001486 continue
Mark Lobodzinskib7af2032015-10-08 16:12:49 -06001487 if o != 'VkDescriptorSet':
1488 gedd_txt.append(' for (auto it = %sMap.begin(); it != %sMap.end(); ++it) {' % (o, o))
1489 gedd_txt.append(' OBJTRACK_NODE* pNode = it->second;')
1490 gedd_txt.append(' log_msg(mdd(device), VK_DBG_REPORT_ERROR_BIT, pNode->objType, pNode->vkObj, 0, OBJTRACK_OBJECT_LEAK, "OBJTRACK",')
1491 gedd_txt.append(' "OBJ ERROR : %s object 0x%" PRIxLEAST64 " has not been destroyed.", string_VkDbgObjectType(pNode->objType),')
1492 gedd_txt.append(' pNode->vkObj);')
1493 gedd_txt.append(' }')
1494 else:
1495 gedd_txt.append(' // DescriptorSets are implicitly cleared via DestroyDescriptorPool, ignore remaining objects')
Mike Stroyan09b75852015-08-18 14:48:34 -06001496 gedd_txt.append(' %sMap.clear();' % (o))
Tony Barbour2a199c12015-07-09 17:31:46 -06001497 gedd_txt.append('')
1498 gedd_txt.append(" // Clean up Queue's MemRef Linked Lists")
1499 gedd_txt.append(' destroyQueueMemRefLists();')
1500 gedd_txt.append('')
1501 gedd_txt.append(' loader_platform_thread_unlock_mutex(&objLock);')
1502 gedd_txt.append('')
1503 gedd_txt.append(' dispatch_key key = get_dispatch_key(device);')
1504 gedd_txt.append(' VkLayerDispatchTable *pDisp = get_dispatch_table(ObjectTracker_device_table_map, device);')
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001505 gedd_txt.append(' pDisp->DestroyDevice(device);')
Tony Barbour2a199c12015-07-09 17:31:46 -06001506 gedd_txt.append(' ObjectTracker_device_table_map.erase(key);')
1507 gedd_txt.append(' assert(ObjectTracker_device_table_map.size() == 0 && "Should not have any instance mappings hanging around");')
1508 gedd_txt.append('')
Tony Barbour2a199c12015-07-09 17:31:46 -06001509 gedd_txt.append('}')
1510 gedd_txt.append('')
1511 return "\n".join(gedd_txt)
1512
1513 def generate_command_buffer_validates(self):
1514 cbv_txt = []
1515 cbv_txt.append('%s' % self.lineinfo.get())
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -06001516 for o in ['VkPipeline',
Tony Barbour2a199c12015-07-09 17:31:46 -06001517 'VkPipelineLayout', 'VkBuffer', 'VkEvent', 'VkQueryPool', 'VkRenderPass', 'VkFramebuffer']:
Tobin Ehlisf2f97402015-09-11 12:57:55 -06001518 cbv_txt.append('static VkBool32 validate_object(VkCmdBuffer dispatchable_object, %s object)' % (o))
Tony Barbour2a199c12015-07-09 17:31:46 -06001519 cbv_txt.append('{')
1520 cbv_txt.append(' if (%sMap.find((void*)object.handle) == %sMap.end()) {' % (o, o))
Tobin Ehlisf2f97402015-09-11 12:57:55 -06001521 cbv_txt.append(' return log_msg(mdd(dispatchable_object), VK_DBG_REPORT_ERROR_BIT, (VkDbgObjectType) 0, object.handle, 0, OBJTRACK_INVALID_OBJECT, "OBJTRACK",')
Tony Barbour8db65372015-07-10 18:32:33 -06001522 cbv_txt.append(' "Invalid %s Object %%p", object.handle);' % (o))
Tony Barbour2a199c12015-07-09 17:31:46 -06001523 cbv_txt.append(' }')
Tobin Ehlisf2f97402015-09-11 12:57:55 -06001524 cbv_txt.append(' return VK_FALSE;')
Tony Barbour2a199c12015-07-09 17:31:46 -06001525 cbv_txt.append('}')
1526 cbv_txt.append('')
1527 return "\n".join(cbv_txt)
1528
Mike Stroyan3aecdb42015-04-03 17:13:23 -06001529 def generate_intercept(self, proto, qual):
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001530 if proto.name in [ 'DbgCreateMsgCallback', 'EnumerateInstanceLayerProperties', 'EnumerateInstanceExtensionProperties','EnumerateDeviceLayerProperties', 'EnumerateDeviceExtensionProperties' ]:
Mike Stroyan723913e2015-04-03 14:39:16 -06001531 # use default version
1532 return None
Mark Lobodzinski4e5016f2015-05-05 15:01:37 -05001533
Tony Barbour2a199c12015-07-09 17:31:46 -06001534 # Create map of object names to object type enums of the form VkName : VkObjectTypeName
1535 obj_type_mapping = {base_t : base_t.replace("Vk", "VkObjectType") for base_t in vulkan.object_type_list}
Mark Lobodzinski4e5016f2015-05-05 15:01:37 -05001536 # Convert object type enum names from UpperCamelCase to UPPER_CASE_WITH_UNDERSCORES
1537 for objectName, objectTypeEnum in obj_type_mapping.items():
1538 obj_type_mapping[objectName] = ucc_to_U_C_C(objectTypeEnum);
1539 # Command Buffer Object doesn't follow the rule.
1540 obj_type_mapping['VkCmdBuffer'] = "VK_OBJECT_TYPE_COMMAND_BUFFER"
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001541 obj_type_mapping['VkShaderModule'] = "VK_OBJECT_TYPE_SHADER_MODULE"
Mike Stroyan723913e2015-04-03 14:39:16 -06001542
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001543 explicit_object_tracker_functions = [
1544 "CreateInstance",
Tobin Ehlis87f115c2015-09-15 15:02:17 -06001545 "EnumeratePhysicalDevices",
Cody Northropef72e2a2015-08-03 17:04:53 -06001546 "GetPhysicalDeviceQueueFamilyProperties",
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001547 "CreateDevice",
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001548 "GetDeviceQueue",
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001549 "QueueBindSparseImageMemory",
1550 "QueueBindSparseBufferMemory",
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001551 "AllocDescriptorSets",
Tony Barbour912e8152015-07-20 10:52:13 -06001552 "FreeDescriptorSets",
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001553 "MapMemory",
1554 "UnmapMemory",
1555 "FreeMemory",
Mark Lobodzinski9c483302015-10-14 13:16:33 -06001556 "DestroySwapchainKHR",
1557 "GetSwapchainImagesKHR"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001558 ]
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001559 decl = proto.c_func(prefix="vk", attr="VKAPI")
Mike Stroyan723913e2015-04-03 14:39:16 -06001560 param0_name = proto.params[0].name
Mark Lobodzinskic44a5a12015-05-08 09:12:28 -05001561 using_line = ''
Mike Stroyan723913e2015-04-03 14:39:16 -06001562 create_line = ''
Mark Lobodzinski4a611132015-07-17 11:51:24 -06001563 destroy_line = ''
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001564 # Dict below tracks params that are vk objects. Dict is "loop count"->["params w/ that loop count"] where '0' is params that aren't in an array
1565 loop_params = defaultdict(list) # Dict uses loop count as key to make final code generation cleaner so params shared in single loop where needed
Tobin Ehlis33ce8fd2015-07-10 18:25:07 -06001566 # TODO : For now skipping objs that can be NULL. Really should check these and have special case that allows them to be NULL
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001567 # or better yet, these should be encoded into an API json definition and we generate checks from there
1568 # Until then, this is a dict where each func name is a list of object params that can be null (so don't need to be validated)
1569 # param names may be directly passed to the function, or may be a field in a struct param
1570 valid_null_object_names = {'CreateGraphicsPipelines' : ['basePipelineHandle'],
1571 'CreateComputePipelines' : ['basePipelineHandle'],
1572 'BeginCommandBuffer' : ['renderPass', 'framebuffer'],
Tobin Ehlis87f115c2015-09-15 15:02:17 -06001573 'QueueSubmit' : ['fence'],
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001574 }
Tobin Ehlis586aa012015-06-08 17:36:28 -06001575 # TODO : A few of the skipped types are just "hard" cases that need some more work to support
1576 # Need to handle NULL fences on queue submit, binding null memory, and WSI Image objects
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001577 param_count = 'NONE' # keep track of arrays passed directly into API functions
Tobin Ehlis586aa012015-06-08 17:36:28 -06001578 for p in proto.params:
Tobin Ehlis87f115c2015-09-15 15:02:17 -06001579 base_type = p.ty.replace('const ', '').strip('*')
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001580 if 'count' in p.name.lower():
1581 param_count = p.name
Tobin Ehlis87f115c2015-09-15 15:02:17 -06001582 if base_type in vulkan.core.objects:
1583 # This is an object to potentially check for validity. First see if it's an array
1584 if '*' in p.ty and 'const' in p.ty and param_count != 'NONE':
1585 loop_params[param_count].append(p.name)
1586 # Not an array, check for just a base Object that's not in exceptions
1587 elif '*' not in p.ty and (proto.name not in valid_null_object_names or p.name not in valid_null_object_names[proto.name]):
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001588 loop_params[0].append(p.name)
Tobin Ehlis87f115c2015-09-15 15:02:17 -06001589 elif vk_helper.is_type(base_type, 'struct'):
1590 struct_type = base_type
Tobin Ehlis25756af2015-06-30 14:32:16 -06001591 if vk_helper.typedef_rev_dict[struct_type] in vk_helper.struct_dict:
1592 struct_type = vk_helper.typedef_rev_dict[struct_type]
1593 for m in sorted(vk_helper.struct_dict[struct_type]):
1594 if vk_helper.struct_dict[struct_type][m]['type'] in vulkan.core.objects and vk_helper.struct_dict[struct_type][m]['type'] not in ['VkPhysicalDevice', 'VkQueue', 'VkFence', 'VkImage', 'VkDeviceMemory']:
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001595 if proto.name not in valid_null_object_names or vk_helper.struct_dict[struct_type][m]['name'] not in valid_null_object_names[proto.name]:
1596 # If we have a count and this param is a ptr w/ last letter 's', then this is a dynamically sized array param
1597 # This is not great, but gets the job done for now
1598 if param_count != 'NONE' and '*' in p.ty and 's' == p.name[-1]:
1599 param_name = '%s[i].%s' % (p.name, vk_helper.struct_dict[struct_type][m]['name'])
Tobin Ehlis157e28d2015-07-10 11:10:21 -06001600 else:
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001601 param_name = '%s->%s' % (p.name, vk_helper.struct_dict[struct_type][m]['name'])
1602 if vk_helper.struct_dict[struct_type][m]['dyn_array']:
1603 loop_count = '%s->%s' % (p.name, vk_helper.struct_dict[struct_type][m]['array_size'])
1604 loop_params[loop_count].append(param_name)
1605 else:
1606 if '[' in param_name: # dynamic array param, set size
1607 loop_params[param_count].append(param_name)
1608 else:
1609 loop_params[0].append(param_name)
Mike Stroyan723913e2015-04-03 14:39:16 -06001610 funcs = []
Tobin Ehlis586aa012015-06-08 17:36:28 -06001611 mutex_unlock = False
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001612 funcs.append('%s\n' % self.lineinfo.get())
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001613 if proto.name in explicit_object_tracker_functions:
Jon Ashburn630e44f2015-04-08 21:33:34 -06001614 funcs.append('%s%s\n'
1615 '{\n'
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001616 ' return explicit_%s;\n'
1617 '}' % (qual, decl, proto.c_call()))
1618 return "".join(funcs)
Mark Lobodzinski4a611132015-07-17 11:51:24 -06001619 elif 'DestroyInstance' in proto.name or 'DestroyDevice' in proto.name:
1620 return ""
Jon Ashburn630e44f2015-04-08 21:33:34 -06001621 else:
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001622 if 'Create' in proto.name or 'Alloc' in proto.name:
1623 create_line = ' loader_platform_thread_lock_mutex(&objLock);\n'
1624 create_line += ' if (result == VK_SUCCESS) {\n'
1625 create_line += ' create_obj(%s, *%s, %s);\n' % (param0_name, proto.params[-1].name, obj_type_mapping[proto.params[-1].ty.strip('*').replace('const ', '')])
1626 create_line += ' }\n'
1627 create_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06001628 if 'FreeCommandBuffers' in proto.name:
1629 funcs.append('%s\n' % self.lineinfo.get())
1630 destroy_line = ' loader_platform_thread_lock_mutex(&objLock);\n'
1631 destroy_line += ' for (uint32_t i = 0; i < count; i++) {\n'
1632 destroy_line += ' destroy_obj(%s[i], %s[i]);\n' % (proto.params[-1].name, proto.params[-1].name)
1633 destroy_line += ' }\n'
1634 destroy_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
Mark Lobodzinski4a611132015-07-17 11:51:24 -06001635 if 'Destroy' in proto.name:
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06001636 funcs.append('%s\n' % self.lineinfo.get())
Mark Lobodzinski4a611132015-07-17 11:51:24 -06001637 destroy_line = ' loader_platform_thread_lock_mutex(&objLock);\n'
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001638# destroy_line += ' if (result == VK_SUCCESS) {\n'
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06001639 destroy_line += ' destroy_obj(%s, %s);\n' % (param0_name, proto.params[-1].name)
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001640# destroy_line += ' }\n'
Mark Lobodzinski4a611132015-07-17 11:51:24 -06001641 destroy_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001642 if len(loop_params) > 0:
Tobin Ehlisf2f97402015-09-11 12:57:55 -06001643 using_line += ' VkBool32 skipCall = VK_FALSE;\n'
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001644 if not mutex_unlock:
1645 using_line += ' loader_platform_thread_lock_mutex(&objLock);\n'
1646 mutex_unlock = True
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001647 for lc in loop_params:
1648 if 0 == lc: # No looping required for these params
1649 for opn in loop_params[lc]:
1650 if '->' in opn:
1651 using_line += ' if (%s)\n' % (opn.split('-')[0])
Tobin Ehlisf2f97402015-09-11 12:57:55 -06001652 using_line += ' skipCall |= validate_object(%s, %s);\n' % (param0_name, opn)
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001653 else:
Tobin Ehlisf2f97402015-09-11 12:57:55 -06001654 using_line += ' skipCall |= validate_object(%s, %s);\n' % (param0_name, opn)
Tobin Ehlis25756af2015-06-30 14:32:16 -06001655 else:
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001656 base_param = loop_params[lc][0].split('-')[0].split('[')[0]
1657 using_line += ' if (%s) {\n' % base_param
Tobin Ehlis41b35502015-10-23 16:57:03 -06001658 using_line += ' for (uint32_t i=0; i<%s; i++) {\n' % lc
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001659 for opn in loop_params[lc]:
1660 if '[' in opn: # API func param is array
Tobin Ehlisf2f97402015-09-11 12:57:55 -06001661 using_line += ' skipCall |= validate_object(%s, %s);\n' % (param0_name, opn)
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001662 else: # struct element is array
Tobin Ehlisf2f97402015-09-11 12:57:55 -06001663 using_line += ' skipCall |= validate_object(%s, %s[i]);\n' % (param0_name, opn)
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001664 using_line += ' }\n'
1665 using_line += ' }\n'
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001666 if mutex_unlock:
1667 using_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
Tobin Ehlisf2f97402015-09-11 12:57:55 -06001668 if len(loop_params) > 0:
1669 using_line += ' if (skipCall)\n'
1670 if proto.ret != "void":
1671 using_line += ' return VK_ERROR_VALIDATION_FAILED;\n'
1672 else:
1673 using_line += ' return;\n'
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001674 ret_val = ''
1675 stmt = ''
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001676 if proto.ret != "void":
1677 ret_val = "%s result = " % proto.ret
1678 stmt = " return result;\n"
1679
1680 dispatch_param = proto.params[0].name
1681 if 'CreateInstance' in proto.name:
1682 dispatch_param = '*' + proto.params[1].name
1683
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -05001684 # Must use 'instance' table for these APIs, 'device' table otherwise
1685 table_type = ""
1686 if proto_is_global(proto):
1687 table_type = "instance"
1688 else:
1689 table_type = "device"
Mike Stroyan723913e2015-04-03 14:39:16 -06001690 funcs.append('%s%s\n'
1691 '{\n'
1692 '%s'
Mike Stroyan723913e2015-04-03 14:39:16 -06001693 '%s'
Mike Stroyane78816e2015-09-28 13:47:29 -06001694 ' %sget_dispatch_table(ObjectTracker_%s_table_map, %s)->%s;\n'
1695 '%s'
1696 '%s'
1697 '}' % (qual, decl, using_line, destroy_line, ret_val, table_type, dispatch_param, proto.c_call(), create_line, stmt))
Mike Stroyan723913e2015-04-03 14:39:16 -06001698 return "\n\n".join(funcs)
1699
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001700 def generate_body(self):
Mike Stroyan3aecdb42015-04-03 17:13:23 -06001701 self.layer_name = "ObjectTracker"
Ian Elliottb134e842015-07-06 14:31:32 -06001702 extensions=[('wsi_enabled',
Jon Ashburn83334db2015-09-16 18:08:32 -06001703 ['vkGetSurfacePropertiesKHR', 'vkGetSurfaceFormatsKHR',
1704 'vkGetSurfacePresentModesKHR', 'vkCreateSwapchainKHR',
1705 'vkDestroySwapchainKHR', 'vkGetSwapchainImagesKHR',
1706 'vkAcquireNextImageKHR', 'vkQueuePresentKHR'])]
Jon Ashburnde4f1102015-09-17 10:00:32 -06001707 instance_extensions=[('msg_callback_get_proc_addr', []),
1708 ('wsi_enabled',
1709 ['vkGetPhysicalDeviceSurfaceSupportKHR'])]
Tony Barbour2a199c12015-07-09 17:31:46 -06001710 body = [self.generate_maps(),
1711 self.generate_procs(),
Mark Lobodzinski4a611132015-07-17 11:51:24 -06001712 self.generate_destroy_instance(),
1713 self.generate_destroy_device(),
Tony Barbour2a199c12015-07-09 17:31:46 -06001714 self.generate_command_buffer_validates(),
1715 self._generate_dispatch_entrypoints("VK_LAYER_EXPORT"),
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001716 self._generate_extensions(),
Jon Ashburn7e07faf2015-06-18 15:02:58 -06001717 self._generate_layer_gpa_function(extensions,
Jon Ashburnde4f1102015-09-17 10:00:32 -06001718 instance_extensions)]
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001719 return "\n\n".join(body)
Courtney Goeltzenleuchtere6094fc2014-11-18 10:40:29 -07001720
Mike Stroyanb326d2c2015-04-02 11:59:05 -06001721class ThreadingSubcommand(Subcommand):
Mike Stroyanc0e49b22015-07-09 11:01:07 -06001722 thread_check_dispatchable_objects = [
1723 "VkQueue",
1724 "VkCmdBuffer",
1725 ]
1726 thread_check_nondispatchable_objects = [
1727 "VkDeviceMemory",
1728 "VkBuffer",
1729 "VkImage",
1730 "VkDescriptorSet",
1731 "VkDescriptorPool",
1732 "VkSemaphore"
1733 ]
Mike Stroyan90a166e2015-08-10 16:42:53 -06001734 thread_check_object_types = {
1735 'VkInstance' : 'VK_OBJECT_TYPE_INSTANCE',
1736 'VkPhysicalDevice' : 'VK_OBJECT_TYPE_PHYSICAL_DEVICE',
1737 'VkDevice' : 'VK_OBJECT_TYPE_DEVICE',
1738 'VkQueue' : 'VK_OBJECT_TYPE_QUEUE',
1739 'VkCmdBuffer' : 'VK_OBJECT_TYPE_COMMAND_BUFFER',
1740 'VkFence' : 'VK_OBJECT_TYPE_FENCE',
1741 'VkDeviceMemory' : 'VK_OBJECT_TYPE_DEVICE_MEMORY',
1742 'VkBuffer' : 'VK_OBJECT_TYPE_BUFFER',
1743 'VkImage' : 'VK_OBJECT_TYPE_IMAGE',
1744 'VkSemaphore' : 'VK_OBJECT_TYPE_SEMAPHORE',
1745 'VkEvent' : 'VK_OBJECT_TYPE_EVENT',
1746 'VkQueryPool' : 'VK_OBJECT_TYPE_QUERY_POOL',
1747 'VkBufferView' : 'VK_OBJECT_TYPE_BUFFER_VIEW',
1748 'VkImageView' : 'VK_OBJECT_TYPE_IMAGE_VIEW',
Mike Stroyan90a166e2015-08-10 16:42:53 -06001749 'VkShaderModule' : 'VK_OBJECT_TYPE_SHADER_MODULE',
1750 'VkShader' : 'VK_OBJECT_TYPE_SHADER',
1751 'VkPipelineCache' : 'VK_OBJECT_TYPE_PIPELINE_CACHE',
1752 'VkPipelineLayout' : 'VK_OBJECT_TYPE_PIPELINE_LAYOUT',
1753 'VkRenderPass' : 'VK_OBJECT_TYPE_RENDER_PASS',
1754 'VkPipeline' : 'VK_OBJECT_TYPE_PIPELINE',
1755 'VkDescriptorSetLayout' : 'VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT',
1756 'VkSampler' : 'VK_OBJECT_TYPE_SAMPLER',
1757 'VkDescriptorPool' : 'VK_OBJECT_TYPE_DESCRIPTOR_POOL',
1758 'VkDescriptorSet' : 'VK_OBJECT_TYPE_DESCRIPTOR_SET',
Mike Stroyan90a166e2015-08-10 16:42:53 -06001759 'VkFramebuffer' : 'VK_OBJECT_TYPE_FRAMEBUFFER',
1760 'VkCmdPool' : 'VK_OBJECT_TYPE_CMD_POOL',
1761 }
1762 def generate_useObject(self, ty):
1763 obj_type = self.thread_check_object_types[ty]
1764 if ty in self.thread_check_dispatchable_objects:
1765 key = "object"
Courtney Goeltzenleuchter0f2b9e22015-08-26 15:09:25 -06001766 msg_object = "reinterpret_cast<uint64_t>(object)"
Mike Stroyan90a166e2015-08-10 16:42:53 -06001767 else:
1768 key = "object.handle"
1769 msg_object = "object.handle"
Mike Stroyanc0e49b22015-07-09 11:01:07 -06001770 header_txt = []
1771 header_txt.append('%s' % self.lineinfo.get())
Mike Stroyan90a166e2015-08-10 16:42:53 -06001772 header_txt.append('static void useObject(const void* dispatchable_object, %s object)' % ty)
Mike Stroyanc0e49b22015-07-09 11:01:07 -06001773 header_txt.append('{')
1774 header_txt.append(' loader_platform_thread_id tid = loader_platform_get_thread_id();')
1775 header_txt.append(' loader_platform_thread_lock_mutex(&threadingLock);')
1776 header_txt.append(' if (%sObjectsInUse.find(%s) == %sObjectsInUse.end()) {' % (ty, key, ty))
1777 header_txt.append(' %sObjectsInUse[%s] = tid;' % (ty, key))
1778 header_txt.append(' } else {')
1779 header_txt.append(' if (%sObjectsInUse[%s] != tid) {' % (ty, key))
Mike Stroyan90a166e2015-08-10 16:42:53 -06001780 header_txt.append(' log_msg(mdd(dispatchable_object), VK_DBG_REPORT_ERROR_BIT, %s, %s,' % (obj_type, msg_object))
1781 header_txt.append(' /*location*/ 0, THREADING_CHECKER_MULTIPLE_THREADS, "THREADING",')
1782 header_txt.append(' "THREADING ERROR : object of type %s is simultaneously used in thread %%ld and thread %%ld",' % (ty))
1783 header_txt.append(' %sObjectsInUse[%s], tid);' % (ty, key))
Mike Stroyanc0e49b22015-07-09 11:01:07 -06001784 header_txt.append(' // Wait for thread-safe access to object')
1785 header_txt.append(' while (%sObjectsInUse.find(%s) != %sObjectsInUse.end()) {' % (ty, key, ty))
1786 header_txt.append(' loader_platform_thread_cond_wait(&threadingCond, &threadingLock);')
1787 header_txt.append(' }')
1788 header_txt.append(' %sObjectsInUse[%s] = tid;' % (ty, key))
1789 header_txt.append(' } else {')
Mike Stroyan90a166e2015-08-10 16:42:53 -06001790 header_txt.append(' log_msg(mdd(dispatchable_object), VK_DBG_REPORT_ERROR_BIT, %s, %s,' % (obj_type, msg_object))
1791 header_txt.append(' /*location*/ 0, THREADING_CHECKER_MULTIPLE_THREADS, "THREADING",')
1792 header_txt.append(' "THREADING ERROR : object of type %s is recursively used in thread %%ld",' % (ty))
1793 header_txt.append(' tid);')
Mike Stroyanc0e49b22015-07-09 11:01:07 -06001794 header_txt.append(' }')
1795 header_txt.append(' }')
1796 header_txt.append(' loader_platform_thread_unlock_mutex(&threadingLock);')
1797 header_txt.append('}')
1798 return "\n".join(header_txt)
Mike Stroyan90a166e2015-08-10 16:42:53 -06001799 def generate_finishUsingObject(self, ty):
1800 if ty in self.thread_check_dispatchable_objects:
1801 key = "object"
1802 else:
1803 key = "object.handle"
Mike Stroyanc0e49b22015-07-09 11:01:07 -06001804 header_txt = []
1805 header_txt.append('%s' % self.lineinfo.get())
1806 header_txt.append('static void finishUsingObject(%s object)' % ty)
1807 header_txt.append('{')
1808 header_txt.append(' // Object is no longer in use')
1809 header_txt.append(' loader_platform_thread_lock_mutex(&threadingLock);')
1810 header_txt.append(' %sObjectsInUse.erase(%s);' % (ty, key))
1811 header_txt.append(' loader_platform_thread_cond_broadcast(&threadingCond);')
1812 header_txt.append(' loader_platform_thread_unlock_mutex(&threadingLock);')
1813 header_txt.append('}')
1814 return "\n".join(header_txt)
Mike Stroyanb326d2c2015-04-02 11:59:05 -06001815 def generate_header(self):
1816 header_txt = []
Tobin Ehlisfde2fc32015-06-12 12:49:01 -06001817 header_txt.append('%s' % self.lineinfo.get())
Mike Stroyanb326d2c2015-04-02 11:59:05 -06001818 header_txt.append('#include <stdio.h>')
1819 header_txt.append('#include <stdlib.h>')
1820 header_txt.append('#include <string.h>')
1821 header_txt.append('#include <unordered_map>')
Tobin Ehlis7a51d902015-07-03 10:34:49 -06001822 header_txt.append('#include "vk_loader_platform.h"')
Tobin Ehlis2d1d9702015-07-03 09:42:57 -06001823 header_txt.append('#include "vk_layer.h"')
Mike Stroyanb326d2c2015-04-02 11:59:05 -06001824 header_txt.append('#include "threading.h"')
Tobin Ehlis56d204a2015-07-03 10:15:26 -06001825 header_txt.append('#include "vk_layer_config.h"')
Courtney Goeltzenleuchter03f1c8c2015-07-06 09:11:12 -06001826 header_txt.append('#include "vk_layer_extension_utils.h"')
Mike Stroyanb326d2c2015-04-02 11:59:05 -06001827 header_txt.append('#include "vk_enum_validate_helper.h"')
1828 header_txt.append('#include "vk_struct_validate_helper.h"')
Mike Stroyan90a166e2015-08-10 16:42:53 -06001829 header_txt.append('#include "vk_layer_table.h"')
1830 header_txt.append('#include "vk_layer_logging.h"')
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -05001831 header_txt.append('')
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -05001832 header_txt.append('')
1833 header_txt.append('static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(initOnce);')
1834 header_txt.append('')
Mike Stroyanb326d2c2015-04-02 11:59:05 -06001835 header_txt.append('using namespace std;')
Mike Stroyanc0e49b22015-07-09 11:01:07 -06001836 for ty in self.thread_check_dispatchable_objects:
1837 header_txt.append('static unordered_map<%s, loader_platform_thread_id> %sObjectsInUse;' % (ty, ty))
1838 for ty in self.thread_check_nondispatchable_objects:
1839 header_txt.append('static unordered_map<uint64_t, loader_platform_thread_id> %sObjectsInUse;' % ty)
Mike Stroyanb326d2c2015-04-02 11:59:05 -06001840 header_txt.append('static int threadingLockInitialized = 0;')
1841 header_txt.append('static loader_platform_thread_mutex threadingLock;')
Mike Stroyan354ed672015-05-15 08:50:57 -06001842 header_txt.append('static loader_platform_thread_cond threadingCond;')
Tobin Ehlisfde2fc32015-06-12 12:49:01 -06001843 header_txt.append('%s' % self.lineinfo.get())
Mike Stroyan90a166e2015-08-10 16:42:53 -06001844 for ty in self.thread_check_dispatchable_objects + self.thread_check_nondispatchable_objects:
1845 header_txt.append(self.generate_useObject(ty))
1846 header_txt.append(self.generate_finishUsingObject(ty))
Mike Stroyanc0e49b22015-07-09 11:01:07 -06001847 header_txt.append('%s' % self.lineinfo.get())
Mike Stroyanb326d2c2015-04-02 11:59:05 -06001848 return "\n".join(header_txt)
1849
1850 def generate_intercept(self, proto, qual):
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001851 if proto.name in [ 'DbgCreateMsgCallback' ]:
Mike Stroyanb326d2c2015-04-02 11:59:05 -06001852 # use default version
1853 return None
1854 decl = proto.c_func(prefix="vk", attr="VKAPI")
1855 ret_val = ''
1856 stmt = ''
1857 funcs = []
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -05001858 table = 'device'
Mike Stroyanb326d2c2015-04-02 11:59:05 -06001859 if proto.ret != "void":
Jon Ashburn53c16772015-05-06 10:15:07 -06001860 ret_val = "%s result = " % proto.ret
Mike Stroyanb326d2c2015-04-02 11:59:05 -06001861 stmt = " return result;\n"
Jon Ashburn2666e2f2015-05-15 15:09:35 -06001862 if proto_is_global(proto):
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -05001863 table = 'instance'
Jon Ashburn52c1d732015-05-12 17:23:55 -06001864
Mike Stroyanae0eafd2015-05-11 17:18:14 -06001865 # Memory range calls are special in needed thread checking within structs
1866 if proto.name in ["FlushMappedMemoryRanges","InvalidateMappedMemoryRanges"]:
Mike Stroyan90a166e2015-08-10 16:42:53 -06001867 funcs.append('%s' % self.lineinfo.get())
1868 funcs.append('%s%s\n' % (qual, decl) +
Mike Stroyanae0eafd2015-05-11 17:18:14 -06001869 '{\n'
Tony Barbourb2cc40a2015-07-13 15:28:47 -06001870 ' for (uint32_t i=0; i<memRangeCount; i++) {\n'
Mike Stroyan90a166e2015-08-10 16:42:53 -06001871 ' useObject((const void *) %s, pMemRanges[i].mem);\n' % proto.params[0].name +
Mike Stroyanae0eafd2015-05-11 17:18:14 -06001872 ' }\n'
Mike Stroyan90a166e2015-08-10 16:42:53 -06001873 ' VkLayerDispatchTable *pDeviceTable = get_dispatch_table(Threading_%s_table_map, %s);\n' % (table, proto.params[0].name) +
1874 ' %s pDeviceTable->%s;\n' % (ret_val, proto.c_call()) +
Tony Barbourb2cc40a2015-07-13 15:28:47 -06001875 ' for (uint32_t i=0; i<memRangeCount; i++) {\n'
Mike Stroyanc0e49b22015-07-09 11:01:07 -06001876 ' finishUsingObject(pMemRanges[i].mem);\n'
Mike Stroyanae0eafd2015-05-11 17:18:14 -06001877 ' }\n'
Mike Stroyan90a166e2015-08-10 16:42:53 -06001878 '%s' % (stmt) +
1879 '}')
Mike Stroyanae0eafd2015-05-11 17:18:14 -06001880 return "\n".join(funcs)
Mike Stroyanb326d2c2015-04-02 11:59:05 -06001881 # All functions that do a Get are thread safe
Mike Stroyanae0eafd2015-05-11 17:18:14 -06001882 if 'Get' in proto.name:
Mike Stroyanb326d2c2015-04-02 11:59:05 -06001883 return None
Mike Stroyanae0eafd2015-05-11 17:18:14 -06001884 # All WSI functions are thread safe
Ian Elliott338dedb2015-08-21 15:09:33 -06001885 if 'KHR' in proto.name:
Mike Stroyanb326d2c2015-04-02 11:59:05 -06001886 return None
Mike Stroyanae0eafd2015-05-11 17:18:14 -06001887 # Initialize in early calls
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -06001888 if proto.name == "CreateDevice":
1889 funcs.append('%s' % self.lineinfo.get())
Mike Stroyan90a166e2015-08-10 16:42:53 -06001890 funcs.append('%s%s\n' % (qual, decl) +
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -06001891 '{\n'
Mike Stroyan90a166e2015-08-10 16:42:53 -06001892 ' VkLayerDispatchTable *pDeviceTable = get_dispatch_table(Threading_device_table_map, (void *) *pDevice);\n'
1893 ' VkResult result = pDeviceTable->%s;\n' % (proto.c_call()) +
1894 ' if (result == VK_SUCCESS) {\n'
1895 ' layer_data *my_instance_data = get_my_data_ptr(get_dispatch_key(%s), layer_data_map);\n' % proto.params[0].name +
1896 ' layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(*pDevice), layer_data_map);\n'
1897 ' my_device_data->report_data = layer_debug_report_create_device(my_instance_data->report_data, *pDevice);\n'
1898 ' }\n'
1899 '\n'
1900 ' return result;'
1901 '}')
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -06001902 return "\n".join(funcs)
1903 elif proto.params[0].ty == "VkPhysicalDevice":
Mike Stroyan90a166e2015-08-10 16:42:53 -06001904 return None
Mike Stroyanae0eafd2015-05-11 17:18:14 -06001905 # Functions changing command buffers need thread safe use of first parameter
1906 if proto.params[0].ty == "VkCmdBuffer":
Tobin Ehlisfde2fc32015-06-12 12:49:01 -06001907 funcs.append('%s' % self.lineinfo.get())
Mike Stroyan90a166e2015-08-10 16:42:53 -06001908 funcs.append('%s%s\n' % (qual, decl) +
Mike Stroyanb326d2c2015-04-02 11:59:05 -06001909 '{\n'
Mike Stroyan90a166e2015-08-10 16:42:53 -06001910 ' useObject((const void *) %s, %s);\n' % (proto.params[0].name, proto.params[0].name) +
1911 ' VkLayerDispatchTable *pDeviceTable = get_dispatch_table(Threading_%s_table_map, %s);\n' % (table, proto.params[0].name) +
1912 ' %spDeviceTable->%s;\n' % (ret_val, proto.c_call()) +
1913 ' finishUsingObject(%s);\n' % proto.params[0].name +
1914 '%s' % stmt +
1915 '}')
Mike Stroyanae0eafd2015-05-11 17:18:14 -06001916 return "\n".join(funcs)
1917 # Non-Cmd functions that do a Wait are thread safe
1918 if 'Wait' in proto.name:
1919 return None
1920 # Watch use of certain types of objects passed as any parameter
1921 checked_params = []
1922 for param in proto.params:
Mike Stroyanc0e49b22015-07-09 11:01:07 -06001923 if param.ty in self.thread_check_dispatchable_objects or param.ty in self.thread_check_nondispatchable_objects:
Mike Stroyanae0eafd2015-05-11 17:18:14 -06001924 checked_params.append(param)
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -05001925 if proto.name == "DestroyDevice":
Mike Stroyan90a166e2015-08-10 16:42:53 -06001926 funcs.append('%s%s\n' % (qual, decl) +
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -05001927 '{\n'
Courtney Goeltzenleuchter9f171942015-06-13 21:22:12 -06001928 ' dispatch_key key = get_dispatch_key(device);\n'
Mike Stroyan90a166e2015-08-10 16:42:53 -06001929 ' VkLayerDispatchTable *pDeviceTable = get_dispatch_table(Threading_%s_table_map, %s);\n' % (table, proto.params[0].name) +
1930 ' %spDeviceTable->%s;\n' % (ret_val, proto.c_call()) +
1931 ' Threading_device_table_map.erase(key);\n'
Mike Stroyan90a166e2015-08-10 16:42:53 -06001932 '}\n')
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -05001933 return "\n".join(funcs);
1934 elif proto.name == "DestroyInstance":
Mike Stroyan90a166e2015-08-10 16:42:53 -06001935 funcs.append('%s%s\n' % (qual, decl) +
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -05001936 '{\n'
Courtney Goeltzenleuchter9f171942015-06-13 21:22:12 -06001937 ' dispatch_key key = get_dispatch_key(instance);\n'
Mike Stroyan90a166e2015-08-10 16:42:53 -06001938 ' VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(Threading_instance_table_map, %s);\n' % proto.params[0].name +
1939 ' %spInstanceTable->%s;\n' % (ret_val, proto.c_call()) +
Courtney Goeltzenleuchter9f171942015-06-13 21:22:12 -06001940 ' destroy_instance_dispatch_table(key);\n'
Mike Stroyan90a166e2015-08-10 16:42:53 -06001941 '\n'
1942 ' // Clean up logging callback, if any\n'
1943 ' layer_data *my_data = get_my_data_ptr(key, layer_data_map);\n'
1944 ' if (my_data->logging_callback) {\n'
1945 ' layer_destroy_msg_callback(my_data->report_data, my_data->logging_callback);\n'
1946 ' }\n'
1947 '\n'
1948 ' layer_debug_report_destroy_instance(my_data->report_data);\n'
1949 ' layer_data_map.erase(pInstanceTable);\n'
1950 '\n'
1951 ' Threading_instance_table_map.erase(key);\n'
Mike Stroyan90a166e2015-08-10 16:42:53 -06001952 '}\n')
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -05001953 return "\n".join(funcs);
Courtney Goeltzenleuchterf4a2eba2015-06-08 14:58:39 -06001954 elif proto.name == "CreateInstance":
1955 funcs.append('%s%s\n'
1956 '{\n'
Mike Stroyan90a166e2015-08-10 16:42:53 -06001957 ' VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(Threading_instance_table_map, *pInstance);\n'
1958 ' VkResult result = pInstanceTable->CreateInstance(pCreateInfo, pInstance);\n'
Courtney Goeltzenleuchterf4a2eba2015-06-08 14:58:39 -06001959 '\n'
1960 ' if (result == VK_SUCCESS) {\n'
Mike Stroyan90a166e2015-08-10 16:42:53 -06001961 ' layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);\n'
1962 ' my_data->report_data = debug_report_create_instance(\n'
1963 ' pInstanceTable,\n'
1964 ' *pInstance,\n'
1965 ' pCreateInfo->extensionCount,\n'
1966 ' pCreateInfo->ppEnabledExtensionNames);\n'
1967 ' initThreading(my_data);\n'
Courtney Goeltzenleuchterf4a2eba2015-06-08 14:58:39 -06001968 ' }\n'
1969 ' return result;\n'
Mike Stroyan90a166e2015-08-10 16:42:53 -06001970 '}\n' % (qual, decl))
Courtney Goeltzenleuchterf4a2eba2015-06-08 14:58:39 -06001971 return "\n".join(funcs);
Mike Stroyanae0eafd2015-05-11 17:18:14 -06001972 if len(checked_params) == 0:
1973 return None
1974 # Surround call with useObject and finishUsingObject for each checked_param
Tobin Ehlisfde2fc32015-06-12 12:49:01 -06001975 funcs.append('%s' % self.lineinfo.get())
Mike Stroyanae0eafd2015-05-11 17:18:14 -06001976 funcs.append('%s%s' % (qual, decl))
1977 funcs.append('{')
1978 for param in checked_params:
Mike Stroyan90a166e2015-08-10 16:42:53 -06001979 funcs.append(' useObject((const void *) %s, %s);' % (proto.params[0].name, param.name))
1980 funcs.append(' VkLayerDispatchTable *pDeviceTable = get_dispatch_table(Threading_%s_table_map, %s);' % (table, proto.params[0].name));
1981 funcs.append(' %spDeviceTable->%s;' % (ret_val, proto.c_call()))
Mike Stroyanae0eafd2015-05-11 17:18:14 -06001982 for param in checked_params:
Mike Stroyanc0e49b22015-07-09 11:01:07 -06001983 funcs.append(' finishUsingObject(%s);' % param.name)
Mike Stroyanae0eafd2015-05-11 17:18:14 -06001984 funcs.append('%s'
1985 '}' % stmt)
1986 return "\n".join(funcs)
Mike Stroyanb326d2c2015-04-02 11:59:05 -06001987
1988 def generate_body(self):
1989 self.layer_name = "Threading"
Mike Stroyan90a166e2015-08-10 16:42:53 -06001990 body = [self._generate_new_layer_initialization(True, lockname='threading', condname='threading'),
Mike Stroyanb326d2c2015-04-02 11:59:05 -06001991 self._generate_dispatch_entrypoints("VK_LAYER_EXPORT"),
Courtney Goeltzenleuchterce445ad2015-06-01 14:52:57 -06001992 self._generate_layer_gpa_function(extensions=[],
Jon Ashburnde4f1102015-09-17 10:00:32 -06001993 instance_extensions=[('msg_callback_get_proc_addr', [])]),
Courtney Goeltzenleuchterce445ad2015-06-01 14:52:57 -06001994 self._gen_create_msg_callback(),
1995 self._gen_destroy_msg_callback()]
Mike Stroyanb326d2c2015-04-02 11:59:05 -06001996 return "\n\n".join(body)
1997
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001998def main():
1999 subcommands = {
2000 "layer-funcs" : LayerFuncsSubcommand,
Tobin Ehlis907a0522014-11-25 16:59:27 -07002001 "Generic" : GenericLayerSubcommand,
Tobin Ehlis4a636a12015-04-09 09:19:36 -06002002 "APIDump" : APIDumpSubcommand,
Tobin Ehlis907a0522014-11-25 16:59:27 -07002003 "ObjectTracker" : ObjectTrackerSubcommand,
Mike Stroyanb326d2c2015-04-02 11:59:05 -06002004 "Threading" : ThreadingSubcommand,
Tobin Ehlis92dbf802014-10-22 09:06:33 -06002005 }
2006
Tobin Ehlis6cd06372014-12-17 17:44:50 -07002007 if len(sys.argv) < 3 or sys.argv[1] not in subcommands or not os.path.exists(sys.argv[2]):
2008 print("Usage: %s <subcommand> <input_header> [options]" % sys.argv[0])
Tobin Ehlis92dbf802014-10-22 09:06:33 -06002009 print
Tobin Ehlis2f3726c2015-01-15 17:51:52 -07002010 print("Available subcommands are: %s" % " ".join(subcommands))
Tobin Ehlis92dbf802014-10-22 09:06:33 -06002011 exit(1)
2012
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002013 hfp = vk_helper.HeaderFileParser(sys.argv[2])
Tobin Ehlis6cd06372014-12-17 17:44:50 -07002014 hfp.parse()
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002015 vk_helper.enum_val_dict = hfp.get_enum_val_dict()
2016 vk_helper.enum_type_dict = hfp.get_enum_type_dict()
2017 vk_helper.struct_dict = hfp.get_struct_dict()
2018 vk_helper.typedef_fwd_dict = hfp.get_typedef_fwd_dict()
2019 vk_helper.typedef_rev_dict = hfp.get_typedef_rev_dict()
2020 vk_helper.types_dict = hfp.get_types_dict()
Tobin Ehlis6cd06372014-12-17 17:44:50 -07002021
Tobin Ehlis92dbf802014-10-22 09:06:33 -06002022 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
2023 subcmd.run()
2024
2025if __name__ == "__main__":
2026 main()