blob: 3af04f14699932c4b8aa1743beb7218d2643f3c2 [file] [log] [blame]
Mike Stroyanf1d4d042016-04-07 12:07:41 -06001#!/usr/bin/env python3
Tobin Ehlis92dbf802014-10-22 09:06:33 -06002#
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06003# VK
Tobin Ehlis92dbf802014-10-22 09:06:33 -06004#
Mark Lobodzinski288e4f72016-02-02 15:55:36 -07005# Copyright (c) 2015-2016 The Khronos Group Inc.
6# Copyright (c) 2015-2016 Valve Corporation
7# Copyright (c) 2015-2016 LunarG, Inc.
8# Copyright (c) 2015-2016 Google Inc.
Tobin Ehlis92dbf802014-10-22 09:06:33 -06009#
Mark Lobodzinski288e4f72016-02-02 15:55:36 -070010# Permission is hereby granted, free of charge, to any person obtaining a copy
11# of this software and/or associated documentation files (the "Materials"), to
12# deal in the Materials without restriction, including without limitation the
13# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
14# sell copies of the Materials, and to permit persons to whom the Materials
15# are furnished to do so, subject to the following conditions:
Tobin Ehlis92dbf802014-10-22 09:06:33 -060016#
Mark Lobodzinski288e4f72016-02-02 15:55:36 -070017# The above copyright notice(s) and this permission notice shall be included
18# in all copies or substantial portions of the Materials.
Tobin Ehlis92dbf802014-10-22 09:06:33 -060019#
Mark Lobodzinski288e4f72016-02-02 15:55:36 -070020# THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Tobin Ehlis92dbf802014-10-22 09:06:33 -060021# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Mark Lobodzinski288e4f72016-02-02 15:55:36 -070022# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23#
24# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
26# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
27# USE OR OTHER DEALINGS IN THE MATERIALS
Tobin Ehlis92dbf802014-10-22 09:06:33 -060028#
Tobin Ehlis0b99d032015-12-08 10:50:10 -070029# Author: Tobin Ehlis <tobine@google.com>
30# Author: Courtney Goeltzenleuchter <courtneygo@google.com>
Courtney Goeltzenleuchter96cd7952015-10-30 11:14:30 -060031# Author: Jon Ashburn <jon@lunarg.com>
32# Author: Mark Lobodzinski <mark@lunarg.com>
Tobin Ehlis0b99d032015-12-08 10:50:10 -070033# Author: Mike Stroyan <stroyan@google.com>
Courtney Goeltzenleuchter96cd7952015-10-30 11:14:30 -060034# Author: Tony Barbour <tony@LunarG.com>
Tobin Ehlis0b99d032015-12-08 10:50:10 -070035# Author: Chia-I Wu <olv@google.com>
Mun, Gwan-gyeongc9e2e172016-02-22 09:43:09 +090036# Author: Gwan-gyeong Mun <kk.moon@samsung.com>
Tobin Ehlis92dbf802014-10-22 09:06:33 -060037
38import sys
Tobin Ehlis6cd06372014-12-17 17:44:50 -070039import os
Mark Lobodzinski4e5016f2015-05-05 15:01:37 -050040import re
Tobin Ehlis92dbf802014-10-22 09:06:33 -060041
Courtney Goeltzenleuchtera8c06282015-04-14 14:55:44 -060042import vulkan
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060043import vk_helper
Tobin Ehlisfde2fc32015-06-12 12:49:01 -060044from source_line_info import sourcelineinfo
Tobin Ehlis18cf7262015-08-26 11:22:09 -060045from collections import defaultdict
Tobin Ehlis92dbf802014-10-22 09:06:33 -060046
Jon Ashburn2666e2f2015-05-15 15:09:35 -060047def proto_is_global(proto):
Mark Lobodzinskib3e934d2015-12-10 16:25:21 -070048 global_function_names = [
49 "CreateInstance",
50 "EnumerateInstanceLayerProperties",
51 "EnumerateInstanceExtensionProperties",
52 "EnumerateDeviceLayerProperties",
53 "EnumerateDeviceExtensionProperties",
54 "CreateXcbSurfaceKHR",
Jon Ashburn2be61582016-01-07 16:13:06 -070055 "GetPhysicalDeviceXcbPresentationSupportKHR",
Mark Lobodzinskib3e934d2015-12-10 16:25:21 -070056 "CreateXlibSurfaceKHR",
Jon Ashburn2be61582016-01-07 16:13:06 -070057 "GetPhysicalDeviceXlibPresentationSupportKHR",
Mark Lobodzinskib3e934d2015-12-10 16:25:21 -070058 "CreateWaylandSurfaceKHR",
Jon Ashburn2be61582016-01-07 16:13:06 -070059 "GetPhysicalDeviceWaylandPresentationSupportKHR",
Mark Lobodzinskib3e934d2015-12-10 16:25:21 -070060 "CreateMirSurfaceKHR",
Jon Ashburn2be61582016-01-07 16:13:06 -070061 "GetPhysicalDeviceMirPresentationSupportKHR",
Mark Lobodzinskib3e934d2015-12-10 16:25:21 -070062 "CreateAndroidSurfaceKHR",
63 "CreateWin32SurfaceKHR",
Jon Ashburn2be61582016-01-07 16:13:06 -070064 "GetPhysicalDeviceWin32PresentationSupportKHR"
Mark Lobodzinskib3e934d2015-12-10 16:25:21 -070065 ]
66 if proto.params[0].ty == "VkInstance" or proto.params[0].ty == "VkPhysicalDevice" or proto.name in global_function_names:
Jon Ashburn2666e2f2015-05-15 15:09:35 -060067 return True
68 else:
69 return False
70
Mark Lobodzinskib3e934d2015-12-10 16:25:21 -070071def wsi_name(ext_name):
72 wsi_prefix = ""
73 if 'Xcb' in ext_name:
74 wsi_prefix = 'XCB'
75 elif 'Xlib' in ext_name:
76 wsi_prefix = 'XLIB'
77 elif 'Win32' in ext_name:
78 wsi_prefix = 'WIN32'
79 elif 'Mir' in ext_name:
80 wsi_prefix = 'MIR'
81 elif 'Wayland' in ext_name:
82 wsi_prefix = 'WAYLAND'
83 elif 'Android' in ext_name:
84 wsi_prefix = 'ANDROID'
85 else:
86 wsi_prefix = ''
87 return wsi_prefix
88
89def wsi_ifdef(ext_name):
90 wsi_prefix = wsi_name(ext_name)
91 if not wsi_prefix:
92 return ''
93 else:
94 return "#ifdef VK_USE_PLATFORM_%s_KHR" % wsi_prefix
95
96def wsi_endif(ext_name):
97 wsi_prefix = wsi_name(ext_name)
98 if not wsi_prefix:
99 return ''
100 else:
101 return "#endif // VK_USE_PLATFORM_%s_KHR" % wsi_prefix
102
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600103def generate_get_proc_addr_check(name):
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600104 return " if (!%s || %s[0] != 'v' || %s[1] != 'k')\n" \
105 " return NULL;" % ((name,) * 3)
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600106
Mark Lobodzinski4e5016f2015-05-05 15:01:37 -0500107def ucc_to_U_C_C(CamelCase):
108 temp = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', CamelCase)
109 return re.sub('([a-z0-9])([A-Z])', r'\1_\2', temp).upper()
110
Tobin Ehlis2ece1592016-01-05 09:46:03 -0700111# Parse complete struct chain and add any new ndo_uses to the dict
112def gather_object_uses_in_struct(obj_list, struct_type):
113 struct_uses = {}
114 if vk_helper.typedef_rev_dict[struct_type] in vk_helper.struct_dict:
115 struct_type = vk_helper.typedef_rev_dict[struct_type]
116 # Parse elements of this struct param to identify objects and/or arrays of objects
117 for m in sorted(vk_helper.struct_dict[struct_type]):
118 array_len = "%s" % (str(vk_helper.struct_dict[struct_type][m]['array_size']))
119 base_type = vk_helper.struct_dict[struct_type][m]['type']
120 mem_name = vk_helper.struct_dict[struct_type][m]['name']
121 if array_len != '0':
122 mem_name = "%s[%s]" % (mem_name, array_len)
123 if base_type in obj_list:
124 #if array_len not in ndo_uses:
125 # struct_uses[array_len] = []
126 #struct_uses[array_len].append("%s%s,%s" % (name_prefix, struct_name, base_type))
127 struct_uses[mem_name] = base_type
128 elif vk_helper.is_type(base_type, 'struct'):
129 sub_uses = gather_object_uses_in_struct(obj_list, base_type)
130 if len(sub_uses) > 0:
131 struct_uses[mem_name] = sub_uses
132 return struct_uses
133
134# For the given list of object types, Parse the given list of params
135# and return dict of params that use one of the obj_list types
136# Format of the dict is that terminal elements have <name>,<type>
137# non-terminal elements will have <name>[<array_size>]
138# TODO : This analysis could be done up-front at vk_helper time
139def get_object_uses(obj_list, params):
140 obj_uses = {}
Tobin Ehlis9aac5552016-02-10 15:38:45 -0700141 local_decls = {}
Tobin Ehlis2ece1592016-01-05 09:46:03 -0700142 param_count = 'NONE' # track params that give array sizes
143 for p in params:
144 base_type = p.ty.replace('const ', '').strip('*')
145 array_len = ''
146 is_ptr = False
147 if 'count' in p.name.lower():
148 param_count = p.name
Tobin Ehlis9aac5552016-02-10 15:38:45 -0700149 ptr_txt = ''
Tobin Ehlis2ece1592016-01-05 09:46:03 -0700150 if '*' in p.ty:
151 is_ptr = True
Tobin Ehlis9aac5552016-02-10 15:38:45 -0700152 ptr_txt = '*'
Tobin Ehlis2ece1592016-01-05 09:46:03 -0700153 if base_type in obj_list:
154 if is_ptr and 'const' in p.ty and param_count != 'NONE':
155 array_len = "[%s]" % param_count
Tobin Ehlis9aac5552016-02-10 15:38:45 -0700156 # Non-arrays we can overwrite in place, but need local decl for arrays
157 local_decls[p.name] = '%s%s' % (base_type, ptr_txt)
Tobin Ehlis2ece1592016-01-05 09:46:03 -0700158 #if array_len not in obj_uses:
159 # obj_uses[array_len] = {}
160 # obj_uses[array_len][p.name] = base_type
161 obj_uses["%s%s" % (p.name, array_len)] = base_type
162 elif vk_helper.is_type(base_type, 'struct'):
163 struct_name = p.name
164 if 'NONE' != param_count:
165 struct_name = "%s[%s]" % (struct_name, param_count)
166 struct_uses = gather_object_uses_in_struct(obj_list, base_type)
167 if len(struct_uses) > 0:
168 obj_uses[struct_name] = struct_uses
Tobin Ehlis9aac5552016-02-10 15:38:45 -0700169 # This is a top-level struct w/ uses below it, so need local decl
170 local_decls['%s' % (p.name)] = '%s%s' % (base_type, ptr_txt)
171 return (obj_uses, local_decls)
Tobin Ehlis2ece1592016-01-05 09:46:03 -0700172
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600173class Subcommand(object):
174 def __init__(self, argv):
175 self.argv = argv
Courtney Goeltzenleuchtera8c06282015-04-14 14:55:44 -0600176 self.headers = vulkan.headers
177 self.protos = vulkan.protos
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600178 self.no_addr = False
179 self.layer_name = ""
Tobin Ehlisfde2fc32015-06-12 12:49:01 -0600180 self.lineinfo = sourcelineinfo()
Mun, Gwan-gyeongc9e2e172016-02-22 09:43:09 +0900181 self.wsi = sys.argv[1]
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600182
183 def run(self):
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600184 print(self.generate())
185
186 def generate(self):
187 copyright = self.generate_copyright()
188 header = self.generate_header()
189 body = self.generate_body()
190 footer = self.generate_footer()
191
192 contents = []
193 if copyright:
194 contents.append(copyright)
195 if header:
196 contents.append(header)
197 if body:
198 contents.append(body)
199 if footer:
200 contents.append(footer)
201
202 return "\n\n".join(contents)
203
204 def generate_copyright(self):
205 return """/* THIS FILE IS GENERATED. DO NOT EDIT. */
206
207/*
Mark Lobodzinski288e4f72016-02-02 15:55:36 -0700208 * Copyright (c) 2015-2016 The Khronos Group Inc.
209 * Copyright (c) 2015-2016 Valve Corporation
210 * Copyright (c) 2015-2016 LunarG, Inc.
Tobin Ehlis2fb0dba2016-04-13 12:59:43 -0600211 * Copyright (c) 2015-2016 Google, Inc.
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600212 *
Mark Lobodzinski288e4f72016-02-02 15:55:36 -0700213 * Permission is hereby granted, free of charge, to any person obtaining a copy
214 * of this software and/or associated documentation files (the "Materials"), to
215 * deal in the Materials without restriction, including without limitation the
216 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
217 * sell copies of the Materials, and to permit persons to whom the Materials
218 * are furnished to do so, subject to the following conditions:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600219 *
Mark Lobodzinski288e4f72016-02-02 15:55:36 -0700220 * The above copyright notice(s) and this permission notice shall be included
221 * in all copies or substantial portions of the Materials.
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600222 *
Mark Lobodzinski288e4f72016-02-02 15:55:36 -0700223 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600224 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Mark Lobodzinski288e4f72016-02-02 15:55:36 -0700225 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
226 *
227 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
228 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
229 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
230 * USE OR OTHER DEALINGS IN THE MATERIALS
Courtney Goeltzenleuchter96cd7952015-10-30 11:14:30 -0600231 *
Tobin Ehlis0b99d032015-12-08 10:50:10 -0700232 * Author: Tobin Ehlis <tobine@google.com>
233 * Author: Courtney Goeltzenleuchter <courtneygo@google.com>
Courtney Goeltzenleuchter96cd7952015-10-30 11:14:30 -0600234 * Author: Jon Ashburn <jon@lunarg.com>
235 * Author: Mark Lobodzinski <mark@lunarg.com>
Tobin Ehlis0b99d032015-12-08 10:50:10 -0700236 * Author: Mike Stroyan <stroyan@google.com>
Courtney Goeltzenleuchter96cd7952015-10-30 11:14:30 -0600237 * Author: Tony Barbour <tony@LunarG.com>
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600238 */"""
239
240 def generate_header(self):
241 return "\n".join(["#include <" + h + ">" for h in self.headers])
242
243 def generate_body(self):
244 pass
245
246 def generate_footer(self):
247 pass
248
249 # Return set of printf '%' qualifier and input to that qualifier
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600250 def _get_printf_params(self, vk_type, name, output_param, cpp=False):
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600251 # TODO : Need ENUM and STRUCT checks here
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600252 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 -0600253 return ("%s", "string_%s(%s)" % (vk_type.replace('const ', '').strip('*'), name))
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600254 if "char*" == vk_type:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600255 return ("%s", name)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600256 if "uint64" in vk_type:
257 if '*' in vk_type:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600258 return ("%lu", "*%s" % name)
259 return ("%lu", name)
Tobin Ehlis0b9c1952015-07-06 14:02:36 -0600260 if vk_type.strip('*') in vulkan.object_non_dispatch_list:
261 if '*' in vk_type:
Chia-I Wue420a332015-10-26 20:04:44 +0800262 return ("%lu", "%s" % name)
263 return ("%lu", "%s" % name)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600264 if "size" in vk_type:
265 if '*' in vk_type:
Mark Lobodzinskib7c5b232015-10-06 09:57:52 -0600266 return ("%lu", "(unsigned long)*%s" % name)
267 return ("%lu", "(unsigned long)%s" % name)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600268 if "float" in vk_type:
269 if '[' in vk_type: # handle array, current hard-coded to 4 (TODO: Make this dynamic)
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700270 if cpp:
271 return ("[%i, %i, %i, %i]", '"[" << %s[0] << "," << %s[1] << "," << %s[2] << "," << %s[3] << "]"' % (name, name, name, name))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600272 return ("[%f, %f, %f, %f]", "%s[0], %s[1], %s[2], %s[3]" % (name, name, name, name))
273 return ("%f", name)
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -0600274 if "bool" in vk_type.lower() or 'xcb_randr_crtc_t' in vk_type:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600275 return ("%u", name)
Tobin Ehlisf29da382015-04-15 07:46:12 -0600276 if True in [t in vk_type.lower() for t in ["int", "flags", "mask", "xcb_window_t"]]:
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600277 if '[' in vk_type: # handle array, current hard-coded to 4 (TODO: Make this dynamic)
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700278 if cpp:
279 return ("[%i, %i, %i, %i]", "%s[0] << %s[1] << %s[2] << %s[3]" % (name, name, name, name))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600280 return ("[%i, %i, %i, %i]", "%s[0], %s[1], %s[2], %s[3]" % (name, name, name, name))
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600281 if '*' in vk_type:
Tobin Ehlis1336c8d2015-02-04 15:15:11 -0700282 if 'pUserData' == name:
283 return ("%i", "((pUserData == 0) ? 0 : *(pUserData))")
Tobin Ehlisa74d53a2015-04-17 13:26:33 -0600284 if 'const' in vk_type.lower():
285 return ("%p", "(void*)(%s)" % name)
Jon Ashburn1f7e2d72014-12-12 16:10:45 -0700286 return ("%i", "*(%s)" % name)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600287 return ("%i", name)
Tobin Ehlis0a1e06d2014-11-11 17:28:22 -0700288 # 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 -0600289 if "VkFormat" == vk_type:
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700290 if cpp:
291 return ("%p", "&%s" % name)
Chia-I Wuc51b1212015-10-27 19:25:11 +0800292 return ("{%s.channelFormat = %%s, %s.numericFormat = %%s}" % (name, name), "string_VK_COLOR_COMPONENT_FORMAT(%s.channelFormat), string_VK_FORMAT_RANGE_SIZE(%s.numericFormat)" % (name, name))
Tobin Ehlisa554dc32014-11-19 15:52:46 -0700293 if output_param:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600294 return ("%p", "(void*)*%s" % name)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600295 if vk_helper.is_type(vk_type, 'struct') and '*' not in vk_type:
Courtney Goeltzenleuchter9a1ded82015-04-03 16:35:32 -0600296 return ("%p", "(void*)(&%s)" % name)
Jon Ashburn1f7e2d72014-12-12 16:10:45 -0700297 return ("%p", "(void*)(%s)" % name)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600298
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600299 def _gen_create_msg_callback(self):
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700300 r_body = []
Tobin Ehlisfde2fc32015-06-12 12:49:01 -0600301 r_body.append('%s' % self.lineinfo.get())
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700302 r_body.append('VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateDebugReportCallbackEXT(')
303 r_body.append(' VkInstance instance,')
304 r_body.append(' const VkDebugReportCallbackCreateInfoEXT* pCreateInfo,')
305 r_body.append(' const VkAllocationCallbacks* pAllocator,')
306 r_body.append(' VkDebugReportCallbackEXT* pCallback)')
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700307 r_body.append('{')
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600308 # Switch to this code section for the new per-instance storage and debug callbacks
Mark Lobodzinskid11c4ee2016-03-15 14:21:59 -0600309 if self.layer_name in ['object_tracker', 'unique_objects']:
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600310 r_body.append(' VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(%s_instance_table_map, instance);' % self.layer_name )
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700311 r_body.append(' VkResult result = pInstanceTable->CreateDebugReportCallbackEXT(instance, pCreateInfo, pAllocator, pCallback);')
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600312 r_body.append(' if (VK_SUCCESS == result) {')
313 r_body.append(' layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);')
Courtney Goeltzenleuchterf6a6e222015-11-30 12:13:14 -0700314 r_body.append(' result = layer_create_msg_callback(my_data->report_data,')
315 r_body.append(' pCreateInfo,')
316 r_body.append(' pAllocator,')
317 r_body.append(' pCallback);')
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600318 r_body.append(' }')
319 r_body.append(' return result;')
320 else:
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700321 r_body.append(' VkResult result = instance_dispatch_table(instance)->CreateDebugReportCallbackEXT(instance, pCreateInfo, pAllocator, pCallback);')
Jon Ashburn649d0ca2015-10-06 17:05:21 -0600322 r_body.append(' if (VK_SUCCESS == result) {')
323 r_body.append(' layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);')
Courtney Goeltzenleuchterf6a6e222015-11-30 12:13:14 -0700324 r_body.append(' result = layer_create_msg_callback(my_data->report_data, pCreateInfo, pAllocator, pCallback);')
Jon Ashburn649d0ca2015-10-06 17:05:21 -0600325 r_body.append(' }')
326 r_body.append(' return result;')
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700327 r_body.append('}')
328 return "\n".join(r_body)
329
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600330 def _gen_destroy_msg_callback(self):
331 r_body = []
Tobin Ehlisfde2fc32015-06-12 12:49:01 -0600332 r_body.append('%s' % self.lineinfo.get())
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700333 r_body.append('VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyDebugReportCallbackEXT(VkInstance instance, VkDebugReportCallbackEXT msgCallback, const VkAllocationCallbacks *pAllocator)')
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600334 r_body.append('{')
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600335 # Switch to this code section for the new per-instance storage and debug callbacks
Mark Lobodzinskid11c4ee2016-03-15 14:21:59 -0600336 if self.layer_name in ['object_tracker', 'unique_objects']:
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600337 r_body.append(' VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(%s_instance_table_map, instance);' % self.layer_name )
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600338 else:
Courtney Goeltzenleuchterf6a6e222015-11-30 12:13:14 -0700339 r_body.append(' VkLayerInstanceDispatchTable *pInstanceTable = instance_dispatch_table(instance);')
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700340 r_body.append(' pInstanceTable->DestroyDebugReportCallbackEXT(instance, msgCallback, pAllocator);')
Courtney Goeltzenleuchterf6a6e222015-11-30 12:13:14 -0700341 r_body.append(' layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);')
342 r_body.append(' layer_destroy_msg_callback(my_data->report_data, msgCallback, pAllocator);')
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600343 r_body.append('}')
344 return "\n".join(r_body)
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700345
Courtney Goeltzenleuchter5a424ce2015-12-01 14:10:55 -0700346 def _gen_debug_report_msg(self):
347 r_body = []
348 r_body.append('%s' % self.lineinfo.get())
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700349 r_body.append('VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDebugReportMessageEXT(VkInstance instance, VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT objType, uint64_t object, size_t location, int32_t msgCode, const char *pLayerPrefix, const char *pMsg)')
Courtney Goeltzenleuchter5a424ce2015-12-01 14:10:55 -0700350 r_body.append('{')
351 # Switch to this code section for the new per-instance storage and debug callbacks
Mark Lobodzinskid11c4ee2016-03-15 14:21:59 -0600352 if self.layer_name == 'object_tracker':
Courtney Goeltzenleuchter5a424ce2015-12-01 14:10:55 -0700353 r_body.append(' VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(%s_instance_table_map, instance);' % self.layer_name )
354 else:
355 r_body.append(' VkLayerInstanceDispatchTable *pInstanceTable = instance_dispatch_table(instance);')
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700356 r_body.append(' pInstanceTable->DebugReportMessageEXT(instance, flags, objType, object, location, msgCode, pLayerPrefix, pMsg);')
Courtney Goeltzenleuchter5a424ce2015-12-01 14:10:55 -0700357 r_body.append('}')
358 return "\n".join(r_body)
359
Jon Ashburn0e3f8602016-02-02 13:13:01 -0700360 def _gen_layer_get_global_extension_props(self, layer="object_tracker"):
Tony Barbour426b9052015-06-24 16:06:58 -0600361 ggep_body = []
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600362 # generated layers do not provide any global extensions
363 ggep_body.append('%s' % self.lineinfo.get())
364
365 ggep_body.append('')
Mark Lobodzinskid11c4ee2016-03-15 14:21:59 -0600366 if self.layer_name == 'object_tracker':
Courtney Goeltzenleuchter5a424ce2015-12-01 14:10:55 -0700367 ggep_body.append('static const VkExtensionProperties instance_extensions[] = {')
368 ggep_body.append(' {')
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700369 ggep_body.append(' VK_EXT_DEBUG_REPORT_EXTENSION_NAME,')
Courtney Goeltzenleuchterac7b68b2016-01-19 16:08:39 -0700370 ggep_body.append(' VK_EXT_DEBUG_REPORT_SPEC_VERSION')
Courtney Goeltzenleuchter5a424ce2015-12-01 14:10:55 -0700371 ggep_body.append(' }')
372 ggep_body.append('};')
Chia-I Wuaf9e4fd2015-11-06 06:42:02 +0800373 ggep_body.append('VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount, VkExtensionProperties* pProperties)')
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600374 ggep_body.append('{')
Mark Lobodzinskid11c4ee2016-03-15 14:21:59 -0600375 if self.layer_name == 'object_tracker':
Courtney Goeltzenleuchter5a424ce2015-12-01 14:10:55 -0700376 ggep_body.append(' return util_GetExtensionProperties(1, instance_extensions, pCount, pProperties);')
377 else:
378 ggep_body.append(' return util_GetExtensionProperties(0, NULL, pCount, pProperties);')
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600379 ggep_body.append('}')
380 return "\n".join(ggep_body)
381
Jon Ashburn0e3f8602016-02-02 13:13:01 -0700382 def _gen_layer_get_global_layer_props(self, layer="object_tracker"):
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600383 ggep_body = []
Jon Ashburn0e3f8602016-02-02 13:13:01 -0700384 layer_name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', layer)
385 layer_name = re.sub('([a-z0-9])([A-Z])', r'\1_\2', layer_name).lower()
386 ggep_body.append('%s' % self.lineinfo.get())
387 ggep_body.append('static const VkLayerProperties globalLayerProps[] = {')
388 ggep_body.append(' {')
Mark Lobodzinskid11c4ee2016-03-15 14:21:59 -0600389 if self.layer_name in ['unique_objects']:
Courtney Goeltzenleuchter7b76aee2016-02-08 11:16:21 -0700390 ggep_body.append(' "VK_LAYER_GOOGLE_%s",' % layer)
Jon Ashburnf1ea4182016-03-22 12:57:13 -0600391 ggep_body.append(' VK_LAYER_API_VERSION, // specVersion')
Courtney Goeltzenleuchter7b76aee2016-02-08 11:16:21 -0700392 ggep_body.append(' 1, // implementationVersion')
393 ggep_body.append(' "Google Validation Layer"')
394 else:
395 ggep_body.append(' "VK_LAYER_LUNARG_%s",' % layer)
Jon Ashburnf1ea4182016-03-22 12:57:13 -0600396 ggep_body.append(' VK_LAYER_API_VERSION, // specVersion')
Courtney Goeltzenleuchter7b76aee2016-02-08 11:16:21 -0700397 ggep_body.append(' 1, // implementationVersion')
398 ggep_body.append(' "LunarG Validation Layer"')
Jon Ashburn0e3f8602016-02-02 13:13:01 -0700399 ggep_body.append(' }')
400 ggep_body.append('};')
Tony Barbour426b9052015-06-24 16:06:58 -0600401 ggep_body.append('')
402 ggep_body.append('%s' % self.lineinfo.get())
Tony Barbour426b9052015-06-24 16:06:58 -0600403 ggep_body.append('')
Chia-I Wuaf9e4fd2015-11-06 06:42:02 +0800404 ggep_body.append('VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties(uint32_t *pCount, VkLayerProperties* pProperties)')
Tony Barbour426b9052015-06-24 16:06:58 -0600405 ggep_body.append('{')
Courtney Goeltzenleuchter79d8d2c2015-07-07 17:51:45 -0600406 ggep_body.append(' return util_GetLayerProperties(ARRAY_SIZE(globalLayerProps), globalLayerProps, pCount, pProperties);')
Tony Barbour426b9052015-06-24 16:06:58 -0600407 ggep_body.append('}')
408 return "\n".join(ggep_body)
409
Jon Ashburn0e3f8602016-02-02 13:13:01 -0700410 def _gen_layer_get_physical_device_layer_props(self, layer="object_tracker"):
Courtney Goeltzenleuchter03f1c8c2015-07-06 09:11:12 -0600411 gpdlp_body = []
Jon Ashburn0e3f8602016-02-02 13:13:01 -0700412 gpdlp_body.append('%s' % self.lineinfo.get())
413 gpdlp_body.append('static const VkLayerProperties deviceLayerProps[] = {')
414 gpdlp_body.append(' {')
Mark Lobodzinskid11c4ee2016-03-15 14:21:59 -0600415 if self.layer_name in ['unique_objects']:
Courtney Goeltzenleuchter7b76aee2016-02-08 11:16:21 -0700416 gpdlp_body.append(' "VK_LAYER_GOOGLE_%s",' % layer)
Jon Ashburnf1ea4182016-03-22 12:57:13 -0600417 gpdlp_body.append(' VK_LAYER_API_VERSION, // specVersion')
Courtney Goeltzenleuchter7b76aee2016-02-08 11:16:21 -0700418 gpdlp_body.append(' 1, // implementationVersion')
419 gpdlp_body.append(' "Google Validation Layer"')
420 else:
421 gpdlp_body.append(' "VK_LAYER_LUNARG_%s",' % layer)
Jon Ashburnf1ea4182016-03-22 12:57:13 -0600422 gpdlp_body.append(' VK_LAYER_API_VERSION, // specVersion')
Courtney Goeltzenleuchter7b76aee2016-02-08 11:16:21 -0700423 gpdlp_body.append(' 1, // implementationVersion')
424 gpdlp_body.append(' "LunarG Validation Layer"')
Jon Ashburn0e3f8602016-02-02 13:13:01 -0700425 gpdlp_body.append(' }')
426 gpdlp_body.append('};')
Chia-I Wuaf9e4fd2015-11-06 06:42:02 +0800427 gpdlp_body.append('VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount, VkLayerProperties* pProperties)')
Courtney Goeltzenleuchter03f1c8c2015-07-06 09:11:12 -0600428 gpdlp_body.append('{')
Courtney Goeltzenleuchter79d8d2c2015-07-07 17:51:45 -0600429 gpdlp_body.append(' return util_GetLayerProperties(ARRAY_SIZE(deviceLayerProps), deviceLayerProps, pCount, pProperties);')
Courtney Goeltzenleuchter03f1c8c2015-07-06 09:11:12 -0600430 gpdlp_body.append('}')
431 gpdlp_body.append('')
432 return "\n".join(gpdlp_body)
433
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600434 def _generate_dispatch_entrypoints(self, qual=""):
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600435 if qual:
436 qual += " "
437
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600438 funcs = []
439 intercepted = []
440 for proto in self.protos:
Jon Ashburn1245cec2015-05-18 13:20:15 -0600441 if proto.name == "GetDeviceProcAddr" or proto.name == "GetInstanceProcAddr":
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600442 continue
Mike Stroyan88f0ecf2015-04-08 10:27:43 -0600443 else:
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600444 intercept = self.generate_intercept(proto, qual)
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600445 if intercept is None:
446 # fill in default intercept for certain entrypoints
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700447 if 'CreateDebugReportCallbackEXT' == proto.name:
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600448 intercept = self._gen_layer_dbg_create_msg_callback()
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700449 elif 'DestroyDebugReportCallbackEXT' == proto.name:
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600450 intercept = self._gen_layer_dbg_destroy_msg_callback()
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700451 elif 'DebugReportMessageEXT' == proto.name:
Courtney Goeltzenleuchter5a424ce2015-12-01 14:10:55 -0700452 intercept = self._gen_debug_report_msg()
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600453 elif 'CreateDevice' == proto.name:
454 funcs.append('/* CreateDevice HERE */')
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600455 elif 'EnumerateInstanceExtensionProperties' == proto.name:
Tony Barbour426b9052015-06-24 16:06:58 -0600456 intercept = self._gen_layer_get_global_extension_props(self.layer_name)
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600457 elif 'EnumerateInstanceLayerProperties' == proto.name:
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600458 intercept = self._gen_layer_get_global_layer_props(self.layer_name)
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600459 elif 'EnumerateDeviceLayerProperties' == proto.name:
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600460 intercept = self._gen_layer_get_physical_device_layer_props(self.layer_name)
Tony Barbour426b9052015-06-24 16:06:58 -0600461
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600462 if intercept is not None:
463 funcs.append(intercept)
Ian Elliott338dedb2015-08-21 15:09:33 -0600464 if not "KHR" in proto.name:
Jon Ashburn7e07faf2015-06-18 15:02:58 -0600465 intercepted.append(proto)
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600466
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600467 prefix="vk"
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600468 lookups = []
469 for proto in intercepted:
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600470 lookups.append("if (!strcmp(name, \"%s\"))" % proto.name)
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600471 lookups.append(" return (PFN_vkVoidFunction) %s%s;" %
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600472 (prefix, proto.name))
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600473
474 # add customized layer_intercept_proc
475 body = []
Tobin Ehlisfde2fc32015-06-12 12:49:01 -0600476 body.append('%s' % self.lineinfo.get())
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600477 body.append("static inline PFN_vkVoidFunction layer_intercept_proc(const char *name)")
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600478 body.append("{")
479 body.append(generate_get_proc_addr_check("name"))
480 body.append("")
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600481 body.append(" name += 2;")
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600482 body.append(" %s" % "\n ".join(lookups))
483 body.append("")
484 body.append(" return NULL;")
485 body.append("}")
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600486 # add layer_intercept_instance_proc
487 lookups = []
488 for proto in self.protos:
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600489 if not proto_is_global(proto):
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600490 continue
491
492 if not proto in intercepted:
493 continue
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700494 if proto.name == "CreateInstance":
495 continue
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600496 if proto.name == "CreateDevice":
497 continue
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600498 lookups.append("if (!strcmp(name, \"%s\"))" % proto.name)
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600499 lookups.append(" return (PFN_vkVoidFunction) %s%s;" % (prefix, proto.name))
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600500
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600501 body.append("static inline PFN_vkVoidFunction layer_intercept_instance_proc(const char *name)")
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600502 body.append("{")
503 body.append(generate_get_proc_addr_check("name"))
504 body.append("")
505 body.append(" name += 2;")
506 body.append(" %s" % "\n ".join(lookups))
507 body.append("")
508 body.append(" return NULL;")
509 body.append("}")
510
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600511 funcs.append("\n".join(body))
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600512 return "\n\n".join(funcs)
513
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700514 def _generate_extensions(self):
515 exts = []
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600516 exts.append('%s' % self.lineinfo.get())
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600517 exts.append(self._gen_create_msg_callback())
518 exts.append(self._gen_destroy_msg_callback())
Courtney Goeltzenleuchter5a424ce2015-12-01 14:10:55 -0700519 exts.append(self._gen_debug_report_msg())
Tobin Ehlisf29da382015-04-15 07:46:12 -0600520 return "\n".join(exts)
521
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600522 def _generate_layer_gpa_function(self, extensions=[], instance_extensions=[]):
Jon Ashburn301c5f02015-04-06 10:58:22 -0600523 func_body = []
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600524#
Courtney Goeltzenleuchter03f1c8c2015-07-06 09:11:12 -0600525# New style of GPA Functions for the new layer_data/layer_logging changes
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600526#
Mark Lobodzinskid11c4ee2016-03-15 14:21:59 -0600527 if self.layer_name in ['object_tracker', 'unique_objects']:
Chia-I Wuaf9e4fd2015-11-06 06:42:02 +0800528 func_body.append("VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice device, const char* funcName)\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600529 "{\n"
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600530 " PFN_vkVoidFunction addr;\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600531 " if (!strcmp(\"vkGetDeviceProcAddr\", funcName)) {\n"
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600532 " return (PFN_vkVoidFunction) vkGetDeviceProcAddr;\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600533 " }\n\n"
534 " addr = layer_intercept_proc(funcName);\n"
535 " if (addr)\n"
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700536 " return addr;\n"
537 " if (device == VK_NULL_HANDLE) {\n"
538 " return NULL;\n"
539 " }\n")
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600540 if 0 != len(extensions):
Courtney Goeltzenleuchter03f1c8c2015-07-06 09:11:12 -0600541 func_body.append('%s' % self.lineinfo.get())
542 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 -0600543 for (ext_enable, ext_list) in extensions:
544 extra_space = ""
545 if 0 != len(ext_enable):
Courtney Goeltzenleuchter03f1c8c2015-07-06 09:11:12 -0600546 func_body.append(' if (my_device_data->%s) {' % ext_enable)
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600547 extra_space = " "
548 for ext_name in ext_list:
549 func_body.append(' %sif (!strcmp("%s", funcName))\n'
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600550 ' %sreturn reinterpret_cast<PFN_vkVoidFunction>(%s);' % (extra_space, ext_name, extra_space, ext_name))
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600551 if 0 != len(ext_enable):
552 func_body.append(' }\n')
553 func_body.append("\n if (get_dispatch_table(%s_device_table_map, device)->GetDeviceProcAddr == NULL)\n"
554 " return NULL;\n"
555 " return get_dispatch_table(%s_device_table_map, device)->GetDeviceProcAddr(device, funcName);\n"
556 "}\n" % (self.layer_name, self.layer_name))
Chia-I Wuaf9e4fd2015-11-06 06:42:02 +0800557 func_body.append("VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char* funcName)\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600558 "{\n"
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600559 " PFN_vkVoidFunction addr;\n"
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700560 " if (!strcmp(funcName, \"vkGetInstanceProcAddr\"))\n"
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600561 " return (PFN_vkVoidFunction) vkGetInstanceProcAddr;\n"
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700562 " if (!strcmp(funcName, \"vkCreateInstance\"))\n"
563 " return (PFN_vkVoidFunction) vkCreateInstance;\n"
564 " if (!strcmp(funcName, \"vkCreateDevice\"))\n"
565 " return (PFN_vkVoidFunction) vkCreateDevice;\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600566 " addr = layer_intercept_instance_proc(funcName);\n"
567 " if (addr) {\n"
568 " return addr;"
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700569 " }\n"
570 " if (instance == VK_NULL_HANDLE) {\n"
571 " return NULL;\n"
572 " }\n"
573 )
Jon Ashburn301c5f02015-04-06 10:58:22 -0600574
Jon Ashburnde4f1102015-09-17 10:00:32 -0600575 table_declared = False
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600576 if 0 != len(instance_extensions):
Jon Ashburnde4f1102015-09-17 10:00:32 -0600577 for (ext_enable, ext_list) in instance_extensions:
578 extra_space = ""
579 if 0 != len(ext_enable):
580 if ext_enable == 'msg_callback_get_proc_addr':
581 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 -0600582 " addr = debug_report_get_instance_proc_addr(my_data->report_data, funcName);\n"
583 " if (addr) {\n"
584 " return addr;\n"
Jon Ashburnde4f1102015-09-17 10:00:32 -0600585 " }\n")
586 else:
587 if table_declared == False:
588 func_body.append(" VkLayerInstanceDispatchTable* pTable = get_dispatch_table(%s_instance_table_map, instance);" % self.layer_name)
589 table_declared = True
590 func_body.append(' if (instanceExtMap.size() != 0 && instanceExtMap[pTable].%s)' % ext_enable)
591 func_body.append(' {')
592 extra_space = " "
593 for ext_name in ext_list:
Mark Lobodzinskib3e934d2015-12-10 16:25:21 -0700594 if wsi_name(ext_name):
595 func_body.append('%s' % wsi_ifdef(ext_name))
Jon Ashburnde4f1102015-09-17 10:00:32 -0600596 func_body.append(' %sif (!strcmp("%s", funcName))\n'
597 ' return reinterpret_cast<PFN_vkVoidFunction>(%s);' % (extra_space, ext_name, ext_name))
Mark Lobodzinskib3e934d2015-12-10 16:25:21 -0700598 if wsi_name(ext_name):
599 func_body.append('%s' % wsi_endif(ext_name))
Jon Ashburnde4f1102015-09-17 10:00:32 -0600600 if 0 != len(ext_enable):
601 func_body.append(' }\n')
602
603 func_body.append(" if (get_dispatch_table(%s_instance_table_map, instance)->GetInstanceProcAddr == NULL) {\n"
604 " return NULL;\n"
605 " }\n"
606 " return get_dispatch_table(%s_instance_table_map, instance)->GetInstanceProcAddr(instance, funcName);\n"
607 "}\n" % (self.layer_name, self.layer_name))
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600608 return "\n".join(func_body)
609 else:
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600610 func_body.append('%s' % self.lineinfo.get())
Chia-I Wuaf9e4fd2015-11-06 06:42:02 +0800611 func_body.append("VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice device, const char* funcName)\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600612 "{\n"
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700613 " PFN_vkVoidFunction addr;\n")
Jon Ashburn0e3f8602016-02-02 13:13:01 -0700614 func_body.append("\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600615 " loader_platform_thread_once(&initOnce, init%s);\n\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600616 " if (!strcmp(\"vkGetDeviceProcAddr\", funcName)) {\n"
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600617 " return (PFN_vkVoidFunction) vkGetDeviceProcAddr;\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600618 " }\n\n"
619 " addr = layer_intercept_proc(funcName);\n"
620 " if (addr)\n"
621 " return addr;" % self.layer_name)
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700622 func_body.append(" if (device == VK_NULL_HANDLE) {\n"
623 " return NULL;\n"
624 " }\n")
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600625 func_body.append('')
626 func_body.append(' VkLayerDispatchTable *pDisp = device_dispatch_table(device);')
627 if 0 != len(extensions):
628 extra_space = ""
629 for (ext_enable, ext_list) in extensions:
630 if 0 != len(ext_enable):
Jon Ashburn83334db2015-09-16 18:08:32 -0600631 func_body.append(' if (deviceExtMap.size() != 0 && deviceExtMap[pDisp].%s)' % ext_enable)
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600632 func_body.append(' {')
633 extra_space = " "
634 for ext_name in ext_list:
635 func_body.append(' %sif (!strcmp("%s", funcName))\n'
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600636 ' return reinterpret_cast<PFN_vkVoidFunction>(%s);' % (extra_space, ext_name, ext_name))
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600637 if 0 != len(ext_enable):
638 func_body.append(' }')
639 func_body.append('%s' % self.lineinfo.get())
640 func_body.append(" {\n"
641 " if (pDisp->GetDeviceProcAddr == NULL)\n"
642 " return NULL;\n"
643 " return pDisp->GetDeviceProcAddr(device, funcName);\n"
644 " }\n"
645 "}\n")
Jon Ashburnde4f1102015-09-17 10:00:32 -0600646 func_body.append('%s' % self.lineinfo.get())
Chia-I Wuaf9e4fd2015-11-06 06:42:02 +0800647 func_body.append("VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char* funcName)\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600648 "{\n"
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600649 " PFN_vkVoidFunction addr;\n"
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700650 " if (!strcmp(funcName, \"vkGetInstanceProcAddr\"))\n"
651 " return (PFN_vkVoidFunction) vkGetInstanceProcAddr;\n"
652 " if (!strcmp(funcName, \"vkCreateInstance\"))\n"
653 " return (PFN_vkVoidFunction) vkCreateInstance;\n"
654 " if (!strcmp(funcName, \"vkCreateDevice\"))\n"
655 " return (PFN_vkVoidFunction) vkCreateDevice;\n"
656 )
Jon Ashburn0e3f8602016-02-02 13:13:01 -0700657 func_body.append(
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600658 " loader_platform_thread_once(&initOnce, init%s);\n\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600659 " addr = layer_intercept_instance_proc(funcName);\n"
660 " if (addr)\n"
661 " return addr;" % self.layer_name)
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700662 func_body.append(" if (instance == VK_NULL_HANDLE) {\n"
663 " return NULL;\n"
664 " }\n")
Jon Ashburnde4f1102015-09-17 10:00:32 -0600665 func_body.append("")
Courtney Goeltzenleuchter2bdf6da2016-01-08 12:18:43 -0700666 func_body.append(" VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance);\n")
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600667 if 0 != len(instance_extensions):
Jon Ashburnde4f1102015-09-17 10:00:32 -0600668 extra_space = ""
669 for (ext_enable, ext_list) in instance_extensions:
670 if 0 != len(ext_enable):
Jon Ashburn649d0ca2015-10-06 17:05:21 -0600671 if ext_enable == 'msg_callback_get_proc_addr':
672 func_body.append(" layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);\n"
673 " addr = debug_report_get_instance_proc_addr(my_data->report_data, funcName);\n"
674 " if (addr) {\n"
675 " return addr;\n"
676 " }\n")
677 else:
678 func_body.append(' if (instanceExtMap.size() != 0 && instanceExtMap[pTable].%s)' % ext_enable)
679 func_body.append(' {')
680 extra_space = " "
681 for ext_name in ext_list:
Mark Lobodzinskib3e934d2015-12-10 16:25:21 -0700682 if wsi_name(ext_name):
683 func_body.append('%s' % wsi_ifdef(ext_name))
Jon Ashburn649d0ca2015-10-06 17:05:21 -0600684 func_body.append(' %sif (!strcmp("%s", funcName))\n'
Jon Ashburnde4f1102015-09-17 10:00:32 -0600685 ' return reinterpret_cast<PFN_vkVoidFunction>(%s);' % (extra_space, ext_name, ext_name))
Mark Lobodzinskib3e934d2015-12-10 16:25:21 -0700686 if wsi_name(ext_name):
687 func_body.append('%s' % wsi_endif(ext_name))
Jon Ashburn649d0ca2015-10-06 17:05:21 -0600688 if 0 != len(ext_enable):
689 func_body.append(' }\n')
Jon Ashburnde4f1102015-09-17 10:00:32 -0600690
691 func_body.append(" if (pTable->GetInstanceProcAddr == NULL)\n"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600692 " return NULL;\n"
693 " return pTable->GetInstanceProcAddr(instance, funcName);\n"
694 "}\n")
695 return "\n".join(func_body)
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600696
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600697
Mike Stroyan354ed672015-05-15 08:50:57 -0600698 def _generate_layer_initialization(self, init_opts=False, prefix='vk', lockname=None, condname=None):
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600699 func_body = ["#include \"vk_dispatch_table_helper.h\""]
Tobin Ehlisfde2fc32015-06-12 12:49:01 -0600700 func_body.append('%s' % self.lineinfo.get())
Mark Lobodzinski2e87e612015-12-30 08:16:12 -0700701 func_body.append('static void init_%s(layer_data *my_data, const VkAllocationCallbacks *pAllocator)\n'
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600702 '{\n' % self.layer_name)
Jon Ashburn21001f62015-02-16 08:26:50 -0700703 if init_opts:
Tobin Ehlisfde2fc32015-06-12 12:49:01 -0600704 func_body.append('%s' % self.lineinfo.get())
Jon Ashburn21001f62015-02-16 08:26:50 -0700705 func_body.append('')
Mark Lobodzinskid11c4ee2016-03-15 14:21:59 -0600706 func_body.append(' layer_debug_actions(my_data->report_data, my_data->logging_callback, pAllocator, "lunarg_%s");' % self.layer_name)
Mike Stroyan90a166e2015-08-10 16:42:53 -0600707 func_body.append('')
708 if lockname is not None:
709 func_body.append('%s' % self.lineinfo.get())
710 func_body.append(" if (!%sLockInitialized)" % lockname)
711 func_body.append(" {")
712 func_body.append(" // TODO/TBD: Need to delete this mutex sometime. How???")
713 func_body.append(" loader_platform_thread_create_mutex(&%sLock);" % lockname)
714 if condname is not None:
715 func_body.append(" loader_platform_thread_init_cond(&%sCond);" % condname)
716 func_body.append(" %sLockInitialized = 1;" % lockname)
717 func_body.append(" }")
718 func_body.append("}\n")
719 func_body.append('')
720 return "\n".join(func_body)
721
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600722class ObjectTrackerSubcommand(Subcommand):
723 def generate_header(self):
724 header_txt = []
Tobin Ehlisfde2fc32015-06-12 12:49:01 -0600725 header_txt.append('%s' % self.lineinfo.get())
Jamie Madill6f25bb72016-04-04 11:54:43 -0400726 header_txt.append('#include "vk_loader_platform.h"')
727 header_txt.append('#include "vulkan/vulkan.h"')
728 header_txt.append('')
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600729 header_txt.append('#include <stdio.h>')
730 header_txt.append('#include <stdlib.h>')
731 header_txt.append('#include <string.h>')
732 header_txt.append('#include <inttypes.h>')
733 header_txt.append('')
Tobin Ehlis586aa012015-06-08 17:36:28 -0600734 header_txt.append('#include <unordered_map>')
735 header_txt.append('using namespace std;')
David Pinedo329ca9e2015-11-06 12:54:48 -0700736 header_txt.append('#include "vulkan/vk_layer.h"')
Tobin Ehlis56d204a2015-07-03 10:15:26 -0600737 header_txt.append('#include "vk_layer_config.h"')
Tobin Ehlis56d204a2015-07-03 10:15:26 -0600738 header_txt.append('#include "vk_layer_table.h"')
739 header_txt.append('#include "vk_layer_data.h"')
740 header_txt.append('#include "vk_layer_logging.h"')
Mark Lobodzinski14305ad2015-06-23 11:35:12 -0600741 header_txt.append('')
Mark Lobodzinski2e87e612015-12-30 08:16:12 -0700742# NOTE: The non-autoGenerated code is in the object_tracker.h header file
743 header_txt.append('#include "object_tracker.h"')
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -0500744 header_txt.append('')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600745 return "\n".join(header_txt)
746
Tony Barbour2a199c12015-07-09 17:31:46 -0600747 def generate_maps(self):
748 maps_txt = []
Tobin Ehlisc6a25752016-01-05 10:33:58 -0700749 for o in vulkan.object_type_list:
Michael Lentine1a85aa12015-11-04 14:35:12 -0800750 maps_txt.append('unordered_map<uint64_t, OBJTRACK_NODE*> %sMap;' % (o))
Tony Barbour2a199c12015-07-09 17:31:46 -0600751 return "\n".join(maps_txt)
752
Tobin Ehlisc6a25752016-01-05 10:33:58 -0700753 def _gather_object_uses(self, obj_list, struct_type, obj_set):
754 # for each member of struct_type
755 # add objs in obj_list to obj_set
756 # call self for structs
Mike Stroyan16d3c6c2016-04-07 12:14:30 -0600757 for m in sorted(vk_helper.struct_dict[struct_type]):
Tobin Ehlisc6a25752016-01-05 10:33:58 -0700758 if vk_helper.struct_dict[struct_type][m]['type'] in obj_list:
759 obj_set.add(vk_helper.struct_dict[struct_type][m]['type'])
760 elif vk_helper.is_type(vk_helper.struct_dict[struct_type][m]['type'], 'struct'):
761 obj_set = obj_set.union(self._gather_object_uses(obj_list, vk_helper.struct_dict[struct_type][m]['type'], obj_set))
762 return obj_set
763
Tony Barbour2a199c12015-07-09 17:31:46 -0600764 def generate_procs(self):
765 procs_txt = []
Tobin Ehlisc6a25752016-01-05 10:33:58 -0700766 # First parse through funcs and gather dict of all objects seen by each call
767 obj_use_dict = {}
768 proto_list = vulkan.core.protos + vulkan.ext_khr_surface.protos + vulkan.ext_khr_surface.protos + vulkan.ext_khr_win32_surface.protos + vulkan.ext_khr_device_swapchain.protos
769 for proto in proto_list:
770 disp_obj = proto.params[0].ty.strip('*').replace('const ', '')
771 if disp_obj in vulkan.object_dispatch_list:
772 if disp_obj not in obj_use_dict:
773 obj_use_dict[disp_obj] = set()
774 for p in proto.params[1:]:
775 base_type = p.ty.strip('*').replace('const ', '')
776 if base_type in vulkan.object_type_list:
777 obj_use_dict[disp_obj].add(base_type)
778 if vk_helper.is_type(base_type, 'struct'):
779 obj_use_dict[disp_obj] = self._gather_object_uses(vulkan.object_type_list, base_type, obj_use_dict[disp_obj])
780 #for do in obj_use_dict:
781 # print "Disp obj %s has uses for objs: %s" % (do, ', '.join(obj_use_dict[do]))
782
783 for o in vulkan.object_type_list:# vulkan.core.objects:
Tony Barbour2a199c12015-07-09 17:31:46 -0600784 procs_txt.append('%s' % self.lineinfo.get())
Michael Lentine1a85aa12015-11-04 14:35:12 -0800785 name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', o)
786 name = re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower()[3:]
Tobin Ehlis18cf7262015-08-26 11:22:09 -0600787 if o in vulkan.object_dispatch_list:
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700788 procs_txt.append('static void create_%s(%s dispatchable_object, %s vkObj, VkDebugReportObjectTypeEXT objType)' % (name, o, o))
Tony Barbour2a199c12015-07-09 17:31:46 -0600789 else:
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700790 procs_txt.append('static void create_%s(VkDevice dispatchable_object, %s vkObj, VkDebugReportObjectTypeEXT objType)' % (name, o))
Chia-I Wue420a332015-10-26 20:04:44 +0800791 procs_txt.append('{')
Mark Lobodzinski5c13d4d2016-02-11 09:26:16 -0700792 procs_txt.append(' log_msg(mdd(dispatchable_object), VK_DEBUG_REPORT_INFORMATION_BIT_EXT, objType,(uint64_t)(vkObj), __LINE__, OBJTRACK_NONE, "OBJTRACK",')
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700793 procs_txt.append(' "OBJ[%llu] : CREATE %s object 0x%" PRIxLEAST64 , object_track_index++, string_VkDebugReportObjectTypeEXT(objType),')
Mark Young2acdd152016-01-13 13:47:16 -0700794 procs_txt.append(' (uint64_t)(vkObj));')
Tony Barbour2a199c12015-07-09 17:31:46 -0600795 procs_txt.append('')
796 procs_txt.append(' OBJTRACK_NODE* pNewObjNode = new OBJTRACK_NODE;')
Mark Lobodzinskic2470602016-03-08 15:10:00 -0700797 procs_txt.append(' pNewObjNode->belongsTo = (uint64_t)dispatchable_object;')
Tony Barbour2a199c12015-07-09 17:31:46 -0600798 procs_txt.append(' pNewObjNode->objType = objType;')
799 procs_txt.append(' pNewObjNode->status = OBJSTATUS_NONE;')
Mark Young2acdd152016-01-13 13:47:16 -0700800 procs_txt.append(' pNewObjNode->vkObj = (uint64_t)(vkObj);')
Michael Lentine1a85aa12015-11-04 14:35:12 -0800801 procs_txt.append(' %sMap[(uint64_t)vkObj] = pNewObjNode;' % (o))
Tony Barbour2a199c12015-07-09 17:31:46 -0600802 procs_txt.append(' uint32_t objIndex = objTypeToIndex(objType);')
803 procs_txt.append(' numObjs[objIndex]++;')
804 procs_txt.append(' numTotalObjs++;')
805 procs_txt.append('}')
806 procs_txt.append('')
807 procs_txt.append('%s' % self.lineinfo.get())
Tobin Ehlis18cf7262015-08-26 11:22:09 -0600808 if o in vulkan.object_dispatch_list:
Michael Lentine1a85aa12015-11-04 14:35:12 -0800809 procs_txt.append('static void destroy_%s(%s dispatchable_object, %s object)' % (name, o, o))
Tony Barbour2a199c12015-07-09 17:31:46 -0600810 else:
Michael Lentine1a85aa12015-11-04 14:35:12 -0800811 procs_txt.append('static void destroy_%s(VkDevice dispatchable_object, %s object)' % (name, o))
Tony Barbour2a199c12015-07-09 17:31:46 -0600812 procs_txt.append('{')
Mark Young2acdd152016-01-13 13:47:16 -0700813 procs_txt.append(' uint64_t object_handle = (uint64_t)(object);')
Chris Forbesadb084a2016-03-09 12:06:45 +1300814 procs_txt.append(' auto it = %sMap.find(object_handle);' % o)
815 procs_txt.append(' if (it != %sMap.end()) {' % o)
816 procs_txt.append(' OBJTRACK_NODE* pNode = it->second;')
Tony Barbour2a199c12015-07-09 17:31:46 -0600817 procs_txt.append(' uint32_t objIndex = objTypeToIndex(pNode->objType);')
818 procs_txt.append(' assert(numTotalObjs > 0);')
819 procs_txt.append(' numTotalObjs--;')
820 procs_txt.append(' assert(numObjs[objIndex] > 0);')
821 procs_txt.append(' numObjs[objIndex]--;')
Mark Lobodzinski5c13d4d2016-02-11 09:26:16 -0700822 procs_txt.append(' log_msg(mdd(dispatchable_object), VK_DEBUG_REPORT_INFORMATION_BIT_EXT, pNode->objType, object_handle, __LINE__, OBJTRACK_NONE, "OBJTRACK",')
Michael Lentinecbc4a5e2015-11-03 16:19:46 -0800823 procs_txt.append(' "OBJ_STAT Destroy %s obj 0x%" PRIxLEAST64 " (%" PRIu64 " total objs remain & %" PRIu64 " %s objs).",')
Mark Young2acdd152016-01-13 13:47:16 -0700824 procs_txt.append(' string_VkDebugReportObjectTypeEXT(pNode->objType), (uint64_t)(object), numTotalObjs, numObjs[objIndex],')
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700825 procs_txt.append(' string_VkDebugReportObjectTypeEXT(pNode->objType));')
Tony Barbour2a199c12015-07-09 17:31:46 -0600826 procs_txt.append(' delete pNode;')
Chris Forbesadb084a2016-03-09 12:06:45 +1300827 procs_txt.append(' %sMap.erase(it);' % (o))
Chia-I Wue420a332015-10-26 20:04:44 +0800828 procs_txt.append(' } else {')
Mark Lobodzinski78996602016-01-04 15:48:11 -0700829 procs_txt.append(' log_msg(mdd(dispatchable_object), VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT ) 0, object_handle, __LINE__, OBJTRACK_NONE, "OBJTRACK",')
Chia-I Wue420a332015-10-26 20:04:44 +0800830 procs_txt.append(' "Unable to remove obj 0x%" PRIxLEAST64 ". Was it created? Has it already been destroyed?",')
Michael Lentine1a85aa12015-11-04 14:35:12 -0800831 procs_txt.append(' object_handle);')
Chia-I Wue420a332015-10-26 20:04:44 +0800832 procs_txt.append(' }')
Tony Barbour2a199c12015-07-09 17:31:46 -0600833 procs_txt.append('}')
834 procs_txt.append('')
835 procs_txt.append('%s' % self.lineinfo.get())
Tobin Ehlis18cf7262015-08-26 11:22:09 -0600836 if o in vulkan.object_dispatch_list:
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700837 procs_txt.append('static VkBool32 set_%s_status(%s dispatchable_object, %s object, VkDebugReportObjectTypeEXT objType, ObjectStatusFlags status_flag)' % (name, o, o))
Tony Barbour2a199c12015-07-09 17:31:46 -0600838 else:
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700839 procs_txt.append('static VkBool32 set_%s_status(VkDevice dispatchable_object, %s object, VkDebugReportObjectTypeEXT objType, ObjectStatusFlags status_flag)' % (name, o))
Chia-I Wue420a332015-10-26 20:04:44 +0800840 procs_txt.append('{')
841 procs_txt.append(' if (object != VK_NULL_HANDLE) {')
Mark Young2acdd152016-01-13 13:47:16 -0700842 procs_txt.append(' uint64_t object_handle = (uint64_t)(object);')
Chris Forbesadb084a2016-03-09 12:06:45 +1300843 procs_txt.append(' auto it = %sMap.find(object_handle);' % o)
844 procs_txt.append(' if (it != %sMap.end()) {' % o)
845 procs_txt.append(' it->second->status |= status_flag;')
Tony Barbour2a199c12015-07-09 17:31:46 -0600846 procs_txt.append(' }')
847 procs_txt.append(' else {')
848 procs_txt.append(' // If we do not find it print an error')
Mark Lobodzinski78996602016-01-04 15:48:11 -0700849 procs_txt.append(' return log_msg(mdd(dispatchable_object), VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT ) 0, object_handle, __LINE__, OBJTRACK_NONE, "OBJTRACK",')
Chia-I Wue420a332015-10-26 20:04:44 +0800850 procs_txt.append(' "Unable to set status for non-existent object 0x%" PRIxLEAST64 " of %s type",')
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700851 procs_txt.append(' object_handle, string_VkDebugReportObjectTypeEXT(objType));')
Tony Barbour2a199c12015-07-09 17:31:46 -0600852 procs_txt.append(' }')
853 procs_txt.append(' }')
Tobin Ehlisf2f97402015-09-11 12:57:55 -0600854 procs_txt.append(' return VK_FALSE;')
Tony Barbour2a199c12015-07-09 17:31:46 -0600855 procs_txt.append('}')
856 procs_txt.append('')
857 procs_txt.append('%s' % self.lineinfo.get())
Michael Lentine1a85aa12015-11-04 14:35:12 -0800858 procs_txt.append('static VkBool32 validate_%s_status(' % (name))
Tobin Ehlis18cf7262015-08-26 11:22:09 -0600859 if o in vulkan.object_dispatch_list:
Tony Barbour2a199c12015-07-09 17:31:46 -0600860 procs_txt.append('%s dispatchable_object, %s object,' % (o, o))
861 else:
862 procs_txt.append('VkDevice dispatchable_object, %s object,' % (o))
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700863 procs_txt.append(' VkDebugReportObjectTypeEXT objType,')
Tony Barbour2a199c12015-07-09 17:31:46 -0600864 procs_txt.append(' ObjectStatusFlags status_mask,')
865 procs_txt.append(' ObjectStatusFlags status_flag,')
866 procs_txt.append(' VkFlags msg_flags,')
867 procs_txt.append(' OBJECT_TRACK_ERROR error_code,')
868 procs_txt.append(' const char *fail_msg)')
869 procs_txt.append('{')
Mark Young2acdd152016-01-13 13:47:16 -0700870 procs_txt.append(' uint64_t object_handle = (uint64_t)(object);')
Chris Forbesadb084a2016-03-09 12:06:45 +1300871 procs_txt.append(' auto it = %sMap.find(object_handle);' % o)
872 procs_txt.append(' if (it != %sMap.end()) {' % o)
873 procs_txt.append(' OBJTRACK_NODE* pNode = it->second;')
Tony Barbour2a199c12015-07-09 17:31:46 -0600874 procs_txt.append(' if ((pNode->status & status_mask) != status_flag) {')
Mark Lobodzinski78996602016-01-04 15:48:11 -0700875 procs_txt.append(' log_msg(mdd(dispatchable_object), msg_flags, pNode->objType, object_handle, __LINE__, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK",')
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700876 procs_txt.append(' "OBJECT VALIDATION WARNING: %s object 0x%" PRIxLEAST64 ": %s", string_VkDebugReportObjectTypeEXT(objType),')
Michael Lentine1a85aa12015-11-04 14:35:12 -0800877 procs_txt.append(' object_handle, fail_msg);')
Tony Barbour2a199c12015-07-09 17:31:46 -0600878 procs_txt.append(' return VK_FALSE;')
879 procs_txt.append(' }')
880 procs_txt.append(' return VK_TRUE;')
881 procs_txt.append(' }')
882 procs_txt.append(' else {')
883 procs_txt.append(' // If we do not find it print an error')
Mark Lobodzinski78996602016-01-04 15:48:11 -0700884 procs_txt.append(' log_msg(mdd(dispatchable_object), msg_flags, (VkDebugReportObjectTypeEXT) 0, object_handle, __LINE__, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK",')
Chia-I Wue420a332015-10-26 20:04:44 +0800885 procs_txt.append(' "Unable to obtain status for non-existent object 0x%" PRIxLEAST64 " of %s type",')
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700886 procs_txt.append(' object_handle, string_VkDebugReportObjectTypeEXT(objType));')
Tony Barbour2a199c12015-07-09 17:31:46 -0600887 procs_txt.append(' return VK_FALSE;')
888 procs_txt.append(' }')
889 procs_txt.append('}')
890 procs_txt.append('')
891 procs_txt.append('%s' % self.lineinfo.get())
Tobin Ehlis18cf7262015-08-26 11:22:09 -0600892 if o in vulkan.object_dispatch_list:
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700893 procs_txt.append('static VkBool32 reset_%s_status(%s dispatchable_object, %s object, VkDebugReportObjectTypeEXT objType, ObjectStatusFlags status_flag)' % (name, o, o))
Tony Barbour2a199c12015-07-09 17:31:46 -0600894 else:
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700895 procs_txt.append('static VkBool32 reset_%s_status(VkDevice dispatchable_object, %s object, VkDebugReportObjectTypeEXT objType, ObjectStatusFlags status_flag)' % (name, o))
Chia-I Wue420a332015-10-26 20:04:44 +0800896 procs_txt.append('{')
Mark Young2acdd152016-01-13 13:47:16 -0700897 procs_txt.append(' uint64_t object_handle = (uint64_t)(object);')
Chris Forbesadb084a2016-03-09 12:06:45 +1300898 procs_txt.append(' auto it = %sMap.find(object_handle);' % o)
899 procs_txt.append(' if (it != %sMap.end()) {' % o)
900 procs_txt.append(' it->second->status &= ~status_flag;')
Tony Barbour2a199c12015-07-09 17:31:46 -0600901 procs_txt.append(' }')
902 procs_txt.append(' else {')
903 procs_txt.append(' // If we do not find it print an error')
Mark Lobodzinski78996602016-01-04 15:48:11 -0700904 procs_txt.append(' return log_msg(mdd(dispatchable_object), VK_DEBUG_REPORT_ERROR_BIT_EXT, objType, object_handle, __LINE__, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK",')
Chia-I Wue420a332015-10-26 20:04:44 +0800905 procs_txt.append(' "Unable to reset status for non-existent object 0x%" PRIxLEAST64 " of %s type",')
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700906 procs_txt.append(' object_handle, string_VkDebugReportObjectTypeEXT(objType));')
Tony Barbour2a199c12015-07-09 17:31:46 -0600907 procs_txt.append(' }')
Tobin Ehlisf2f97402015-09-11 12:57:55 -0600908 procs_txt.append(' return VK_FALSE;')
Tony Barbour2a199c12015-07-09 17:31:46 -0600909 procs_txt.append('}')
910 procs_txt.append('')
Tobin Ehlisc6a25752016-01-05 10:33:58 -0700911 procs_txt.append('%s' % self.lineinfo.get())
912 # Generate the permutations of validate_* functions where for each
913 # dispatchable object type, we have a corresponding validate_* function
914 # for that object and all non-dispatchable objects that are used in API
915 # calls with that dispatchable object.
Mike Stroyan16d3c6c2016-04-07 12:14:30 -0600916 procs_txt.append('//%s' % str(sorted(obj_use_dict)))
917 for do in sorted(obj_use_dict):
Tobin Ehlisc6a25752016-01-05 10:33:58 -0700918 name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', do)
919 name = re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower()[3:]
920 # First create validate_* func for disp obj
921 procs_txt.append('%s' % self.lineinfo.get())
922 procs_txt.append('static VkBool32 validate_%s(%s dispatchable_object, %s object, VkDebugReportObjectTypeEXT objType, bool null_allowed)' % (name, do, do))
923 procs_txt.append('{')
924 procs_txt.append(' if (null_allowed && (object == VK_NULL_HANDLE))')
925 procs_txt.append(' return VK_FALSE;')
926 procs_txt.append(' if (%sMap.find((uint64_t)object) == %sMap.end()) {' % (do, do))
Mark Young2acdd152016-01-13 13:47:16 -0700927 procs_txt.append(' return log_msg(mdd(dispatchable_object), VK_DEBUG_REPORT_ERROR_BIT_EXT, objType, (uint64_t)(object), __LINE__, OBJTRACK_INVALID_OBJECT, "OBJTRACK",')
928 procs_txt.append(' "Invalid %s Object 0x%%" PRIx64 ,(uint64_t)(object));' % do)
Tobin Ehlisc6a25752016-01-05 10:33:58 -0700929 procs_txt.append(' }')
930 procs_txt.append(' return VK_FALSE;')
931 procs_txt.append('}')
932 procs_txt.append('')
Mike Stroyan16d3c6c2016-04-07 12:14:30 -0600933 for o in sorted(obj_use_dict[do]):
Tobin Ehlisc6a25752016-01-05 10:33:58 -0700934 if o == do: # We already generated this case above so skip here
935 continue
936 name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', o)
937 name = re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower()[3:]
938 procs_txt.append('%s' % self.lineinfo.get())
939 procs_txt.append('static VkBool32 validate_%s(%s dispatchable_object, %s object, VkDebugReportObjectTypeEXT objType, bool null_allowed)' % (name, do, o))
940 procs_txt.append('{')
941 procs_txt.append(' if (null_allowed && (object == VK_NULL_HANDLE))')
942 procs_txt.append(' return VK_FALSE;')
943 if o == "VkImage":
944 procs_txt.append(' // We need to validate normal image objects and those from the swapchain')
945 procs_txt.append(' if ((%sMap.find((uint64_t)object) == %sMap.end()) &&' % (o, o))
946 procs_txt.append(' (swapchainImageMap.find((uint64_t)object) == swapchainImageMap.end())) {')
947 else:
948 procs_txt.append(' if (%sMap.find((uint64_t)object) == %sMap.end()) {' % (o, o))
Mark Young2acdd152016-01-13 13:47:16 -0700949 procs_txt.append(' return log_msg(mdd(dispatchable_object), VK_DEBUG_REPORT_ERROR_BIT_EXT, objType, (uint64_t)(object), __LINE__, OBJTRACK_INVALID_OBJECT, "OBJTRACK",')
950 procs_txt.append(' "Invalid %s Object 0x%%" PRIx64, (uint64_t)(object));' % o)
Tobin Ehlisc6a25752016-01-05 10:33:58 -0700951 procs_txt.append(' }')
952 procs_txt.append(' return VK_FALSE;')
953 procs_txt.append('}')
954 procs_txt.append('')
955 procs_txt.append('')
Tony Barbour2a199c12015-07-09 17:31:46 -0600956 return "\n".join(procs_txt)
957
Mark Lobodzinski4a611132015-07-17 11:51:24 -0600958 def generate_destroy_instance(self):
Tony Barbour2a199c12015-07-09 17:31:46 -0600959 gedi_txt = []
960 gedi_txt.append('%s' % self.lineinfo.get())
Mark Young2acdd152016-01-13 13:47:16 -0700961 gedi_txt.append('VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyInstance(')
Chia-I Wu69f40122015-10-26 21:10:41 +0800962 gedi_txt.append('VkInstance instance,')
Chia-I Wu1f851912015-10-27 18:04:07 +0800963 gedi_txt.append('const VkAllocationCallbacks* pAllocator)')
Tony Barbour2a199c12015-07-09 17:31:46 -0600964 gedi_txt.append('{')
Jeremy Hayes241a2db2016-04-13 10:54:17 -0600965 gedi_txt.append(' std::unique_lock<std::mutex> lock(global_lock);')
Tobin Ehlisc6a25752016-01-05 10:33:58 -0700966 gedi_txt.append(' validate_instance(instance, instance, VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT, false);')
Tony Barbour2a199c12015-07-09 17:31:46 -0600967 gedi_txt.append('')
Michael Lentine1a85aa12015-11-04 14:35:12 -0800968 gedi_txt.append(' destroy_instance(instance, instance);')
Tony Barbour2a199c12015-07-09 17:31:46 -0600969 gedi_txt.append(' // Report any remaining objects in LL')
Mark Lobodzinskic2470602016-03-08 15:10:00 -0700970 gedi_txt.append('')
971 gedi_txt.append(' for (auto iit = VkDeviceMap.begin(); iit != VkDeviceMap.end();) {')
972 gedi_txt.append(' OBJTRACK_NODE* pNode = iit->second;')
973 gedi_txt.append(' if (pNode->belongsTo == (uint64_t)instance) {')
974 gedi_txt.append(' log_msg(mid(instance), VK_DEBUG_REPORT_ERROR_BIT_EXT, pNode->objType, pNode->vkObj, __LINE__, OBJTRACK_OBJECT_LEAK, "OBJTRACK",')
975 gedi_txt.append(' "OBJ ERROR : %s object 0x%" PRIxLEAST64 " has not been destroyed.", string_VkDebugReportObjectTypeEXT(pNode->objType),')
976 gedi_txt.append(' pNode->vkObj);')
Tony Barbour2a199c12015-07-09 17:31:46 -0600977 for o in vulkan.core.objects:
Mark Lobodzinskic2470602016-03-08 15:10:00 -0700978 if o in ['VkInstance', 'VkPhysicalDevice', 'VkQueue', 'VkDevice']:
Tony Barbour2a199c12015-07-09 17:31:46 -0600979 continue
Mark Lobodzinskic2470602016-03-08 15:10:00 -0700980 gedi_txt.append(' for (auto idt = %sMap.begin(); idt != %sMap.end();) {' % (o, o))
981 gedi_txt.append(' OBJTRACK_NODE* pNode = idt->second;')
982 gedi_txt.append(' if (pNode->belongsTo == iit->first) {')
983 gedi_txt.append(' log_msg(mid(instance), VK_DEBUG_REPORT_ERROR_BIT_EXT, pNode->objType, pNode->vkObj, __LINE__, OBJTRACK_OBJECT_LEAK, "OBJTRACK",')
984 gedi_txt.append(' "OBJ ERROR : %s object 0x%" PRIxLEAST64 " has not been destroyed.", string_VkDebugReportObjectTypeEXT(pNode->objType),')
985 gedi_txt.append(' pNode->vkObj);')
986 gedi_txt.append(' %sMap.erase(idt++);' % o )
987 gedi_txt.append(' } else {')
988 gedi_txt.append(' ++idt;')
989 gedi_txt.append(' }')
990 gedi_txt.append(' }')
991 gedi_txt.append(' VkDeviceMap.erase(iit++);')
992 gedi_txt.append(' } else {')
993 gedi_txt.append(' ++iit;')
994 gedi_txt.append(' }')
995 gedi_txt.append(' }')
996 gedi_txt.append('')
Tony Barbour2a199c12015-07-09 17:31:46 -0600997 gedi_txt.append(' dispatch_key key = get_dispatch_key(instance);')
Mark Lobodzinski2e87e612015-12-30 08:16:12 -0700998 gedi_txt.append(' VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(object_tracker_instance_table_map, instance);')
Chia-I Wu69f40122015-10-26 21:10:41 +0800999 gedi_txt.append(' pInstanceTable->DestroyInstance(instance, pAllocator);')
Tony Barbour2a199c12015-07-09 17:31:46 -06001000 gedi_txt.append('')
Tony Barbour2a199c12015-07-09 17:31:46 -06001001 gedi_txt.append(' layer_data *my_data = get_my_data_ptr(key, layer_data_map);')
Mark Lobodzinskid11c4ee2016-03-15 14:21:59 -06001002 gedi_txt.append(' // Clean up logging callback, if any')
1003 gedi_txt.append(' while (my_data->logging_callback.size() > 0) {')
1004 gedi_txt.append(' VkDebugReportCallbackEXT callback = my_data->logging_callback.back();')
1005 gedi_txt.append(' layer_destroy_msg_callback(my_data->report_data, callback, pAllocator);')
1006 gedi_txt.append(' my_data->logging_callback.pop_back();')
Tony Barbour2a199c12015-07-09 17:31:46 -06001007 gedi_txt.append(' }')
1008 gedi_txt.append('')
1009 gedi_txt.append(' layer_debug_report_destroy_instance(mid(instance));')
Tobin Ehlis914b7762016-04-18 15:40:59 -06001010 gedi_txt.append(' layer_data_map.erase(key);')
Tony Barbour2a199c12015-07-09 17:31:46 -06001011 gedi_txt.append('')
Jon Ashburnde4f1102015-09-17 10:00:32 -06001012 gedi_txt.append(' instanceExtMap.erase(pInstanceTable);')
Jeremy Hayes241a2db2016-04-13 10:54:17 -06001013 gedi_txt.append(' lock.unlock();')
Mike Stroyan09b75852015-08-18 14:48:34 -06001014 # The loader holds a mutex that protects this from other threads
Mark Lobodzinski2e87e612015-12-30 08:16:12 -07001015 gedi_txt.append(' object_tracker_instance_table_map.erase(key);')
Tony Barbour2a199c12015-07-09 17:31:46 -06001016 gedi_txt.append('}')
1017 gedi_txt.append('')
1018 return "\n".join(gedi_txt)
1019
Mark Lobodzinski4a611132015-07-17 11:51:24 -06001020 def generate_destroy_device(self):
Tony Barbour2a199c12015-07-09 17:31:46 -06001021 gedd_txt = []
1022 gedd_txt.append('%s' % self.lineinfo.get())
Mark Young2acdd152016-01-13 13:47:16 -07001023 gedd_txt.append('VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyDevice(')
Chia-I Wu69f40122015-10-26 21:10:41 +08001024 gedd_txt.append('VkDevice device,')
Chia-I Wu1f851912015-10-27 18:04:07 +08001025 gedd_txt.append('const VkAllocationCallbacks* pAllocator)')
Tony Barbour2a199c12015-07-09 17:31:46 -06001026 gedd_txt.append('{')
Jeremy Hayes241a2db2016-04-13 10:54:17 -06001027 gedd_txt.append(' std::unique_lock<std::mutex> lock(global_lock);')
Tobin Ehlisc6a25752016-01-05 10:33:58 -07001028 gedd_txt.append(' validate_device(device, device, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, false);')
Tony Barbour2a199c12015-07-09 17:31:46 -06001029 gedd_txt.append('')
Michael Lentine1a85aa12015-11-04 14:35:12 -08001030 gedd_txt.append(' destroy_device(device, device);')
Mark Lobodzinskic2470602016-03-08 15:10:00 -07001031 gedd_txt.append(' // Report any remaining objects associated with this VkDevice object in LL')
Tony Barbour2a199c12015-07-09 17:31:46 -06001032 for o in vulkan.core.objects:
Mark Lobodzinski60b2b332015-11-12 16:02:35 -07001033 # DescriptorSets and Command Buffers are destroyed through their pools, not explicitly
1034 if o in ['VkInstance', 'VkPhysicalDevice', 'VkQueue', 'VkDevice', 'VkDescriptorSet', 'VkCommandBuffer']:
Tony Barbour2a199c12015-07-09 17:31:46 -06001035 continue
Mark Lobodzinskic2470602016-03-08 15:10:00 -07001036 gedd_txt.append(' for (auto it = %sMap.begin(); it != %sMap.end();) {' % (o, o))
Mark Lobodzinski60b2b332015-11-12 16:02:35 -07001037 gedd_txt.append(' OBJTRACK_NODE* pNode = it->second;')
Mark Lobodzinskic2470602016-03-08 15:10:00 -07001038 gedd_txt.append(' if (pNode->belongsTo == (uint64_t)device) {')
1039 gedd_txt.append(' log_msg(mdd(device), VK_DEBUG_REPORT_ERROR_BIT_EXT, pNode->objType, pNode->vkObj, __LINE__, OBJTRACK_OBJECT_LEAK, "OBJTRACK",')
1040 gedd_txt.append(' "OBJ ERROR : %s object 0x%" PRIxLEAST64 " has not been destroyed.", string_VkDebugReportObjectTypeEXT(pNode->objType),')
1041 gedd_txt.append(' pNode->vkObj);')
1042 gedd_txt.append(' %sMap.erase(it++);' % o )
1043 gedd_txt.append(' } else {')
1044 gedd_txt.append(' ++it;')
1045 gedd_txt.append(' }')
Mark Lobodzinski60b2b332015-11-12 16:02:35 -07001046 gedd_txt.append(' }')
Tony Barbour2a199c12015-07-09 17:31:46 -06001047 gedd_txt.append('')
1048 gedd_txt.append(" // Clean up Queue's MemRef Linked Lists")
1049 gedd_txt.append(' destroyQueueMemRefLists();')
1050 gedd_txt.append('')
Jeremy Hayes241a2db2016-04-13 10:54:17 -06001051 gedd_txt.append(' lock.unlock();')
Tony Barbour2a199c12015-07-09 17:31:46 -06001052 gedd_txt.append('')
1053 gedd_txt.append(' dispatch_key key = get_dispatch_key(device);')
Mark Lobodzinski2e87e612015-12-30 08:16:12 -07001054 gedd_txt.append(' VkLayerDispatchTable *pDisp = get_dispatch_table(object_tracker_device_table_map, device);')
Chia-I Wu69f40122015-10-26 21:10:41 +08001055 gedd_txt.append(' pDisp->DestroyDevice(device, pAllocator);')
Mark Lobodzinski2e87e612015-12-30 08:16:12 -07001056 gedd_txt.append(' object_tracker_device_table_map.erase(key);')
Tony Barbour2a199c12015-07-09 17:31:46 -06001057 gedd_txt.append('')
Tony Barbour2a199c12015-07-09 17:31:46 -06001058 gedd_txt.append('}')
1059 gedd_txt.append('')
1060 return "\n".join(gedd_txt)
1061
Mark Lobodzinskif980af42016-01-23 18:31:23 -07001062 # Special-case validating some objects -- they may be non-NULL but should
1063 # only be validated upon meeting some condition specified below.
1064 def _dereference_conditionally(self, indent, prefix, type_name, name):
Mark Lobodzinski16199c42016-01-19 09:57:24 -07001065 s_code = ''
1066 if type_name == 'pBufferInfo':
1067 s_code += '%sif ((%sdescriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||\n' % (indent, prefix)
1068 s_code += '%s (%sdescriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER) ||\n' % (indent, prefix)
1069 s_code += '%s (%sdescriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||\n' % (indent, prefix)
1070 s_code += '%s (%sdescriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) ) {\n' % (indent, prefix)
1071 elif type_name == 'pImageInfo':
1072 s_code += '%sif ((%sdescriptorType == VK_DESCRIPTOR_TYPE_SAMPLER) ||\n' % (indent, prefix)
1073 s_code += '%s (%sdescriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) ||\n' % (indent, prefix)
1074 s_code += '%s (%sdescriptorType == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) ||\n' % (indent, prefix)
1075 s_code += '%s (%sdescriptorType == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) ||\n' % (indent, prefix)
1076 s_code += '%s (%sdescriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) ) {\n' % (indent, prefix)
1077 elif type_name == 'pTexelBufferView':
Mark Lobodzinskif980af42016-01-23 18:31:23 -07001078 s_code += '%sif ((%sdescriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER) ||\n' % (indent, prefix)
1079 s_code += '%s (%sdescriptorType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER) ) {\n' % (indent, prefix)
1080 elif name == 'pBeginInfo->pInheritanceInfo':
1081 s_code += '%sOBJTRACK_NODE* pNode = VkCommandBufferMap[(uint64_t)commandBuffer];\n' % (indent)
1082 s_code += '%sif ((%s) && (pNode->status & OBJSTATUS_COMMAND_BUFFER_SECONDARY)) {\n' % (indent, name)
Mark Lobodzinski16199c42016-01-19 09:57:24 -07001083 else:
1084 s_code += '%sif (%s) {\n' % (indent, name)
1085 return s_code
1086
Tobin Ehlisc6a25752016-01-05 10:33:58 -07001087 def _gen_obj_validate_code(self, struct_uses, obj_type_mapping, func_name, valid_null_dict, param0_name, indent, prefix, array_index):
1088 pre_code = ''
1089 for obj in sorted(struct_uses):
1090 name = obj
1091 array = ''
Mark Lobodzinski16199c42016-01-19 09:57:24 -07001092 type_name = ''
Tobin Ehlisc6a25752016-01-05 10:33:58 -07001093 if '[' in obj:
1094 (name, array) = obj.split('[')
Mark Lobodzinski16199c42016-01-19 09:57:24 -07001095 type_name = name
Tobin Ehlisc6a25752016-01-05 10:33:58 -07001096 array = array.strip(']')
1097 if isinstance(struct_uses[obj], dict):
1098 local_prefix = ''
1099 name = '%s%s' % (prefix, name)
1100 ptr_type = False
1101 if 'p' == obj[0]:
1102 ptr_type = True
Mark Lobodzinskif980af42016-01-23 18:31:23 -07001103 tmp_pre = self._dereference_conditionally(indent, prefix, type_name, name)
Mark Lobodzinski16199c42016-01-19 09:57:24 -07001104 pre_code += tmp_pre
Tobin Ehlisc6a25752016-01-05 10:33:58 -07001105 indent += ' '
1106 if array != '':
1107 idx = 'idx%s' % str(array_index)
1108 array_index += 1
1109 pre_code += '%s\n' % self.lineinfo.get()
1110 pre_code += '%sfor (uint32_t %s=0; %s<%s%s; ++%s) {\n' % (indent, idx, idx, prefix, array, idx)
1111 indent += ' '
1112 local_prefix = '%s[%s].' % (name, idx)
1113 elif ptr_type:
1114 local_prefix = '%s->' % (name)
1115 else:
1116 local_prefix = '%s.' % (name)
1117 tmp_pre = self._gen_obj_validate_code(struct_uses[obj], obj_type_mapping, func_name, valid_null_dict, param0_name, indent, local_prefix, array_index)
1118 pre_code += tmp_pre
1119 if array != '':
1120 indent = indent[4:]
1121 pre_code += '%s}\n' % (indent)
1122 if ptr_type:
1123 indent = indent[4:]
1124 pre_code += '%s}\n' % (indent)
1125 else:
1126 ptype = struct_uses[obj]
1127 dbg_obj_type = obj_type_mapping[ptype]
1128 fname = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', ptype)
1129 fname = re.sub('([a-z0-9])([A-Z])', r'\1_\2', fname).lower()[3:]
1130 full_name = '%s%s' % (prefix, name)
1131 null_obj_ok = 'false'
1132 # If a valid null param is defined for this func and we have a match, allow NULL
Mike Stroyan16d3c6c2016-04-07 12:14:30 -06001133 if func_name in valid_null_dict and True in [name in pn for pn in sorted(valid_null_dict[func_name])]:
Tobin Ehlisc6a25752016-01-05 10:33:58 -07001134 null_obj_ok = 'true'
1135 if (array_index > 0) or '' != array:
Mark Lobodzinskif980af42016-01-23 18:31:23 -07001136 tmp_pre = self._dereference_conditionally(indent, prefix, type_name, full_name)
Mark Lobodzinski16199c42016-01-19 09:57:24 -07001137 pre_code += tmp_pre
Tobin Ehlisc6a25752016-01-05 10:33:58 -07001138 indent += ' '
1139 if array != '':
1140 idx = 'idx%s' % str(array_index)
1141 array_index += 1
1142 pre_code += '%sfor (uint32_t %s=0; %s<%s%s; ++%s) {\n' % (indent, idx, idx, prefix, array, idx)
1143 indent += ' '
1144 full_name = '%s[%s]' % (full_name, idx)
1145 pre_code += '%s\n' % self.lineinfo.get()
1146 pre_code += '%sskipCall |= validate_%s(%s, %s, %s, %s);\n' %(indent, fname, param0_name, full_name, dbg_obj_type, null_obj_ok)
1147 if array != '':
1148 indent = indent[4:]
1149 pre_code += '%s}\n' % (indent)
1150 indent = indent[4:]
1151 pre_code += '%s}\n' % (indent)
1152 else:
1153 pre_code += '%s\n' % self.lineinfo.get()
1154 pre_code += '%sskipCall |= validate_%s(%s, %s, %s, %s);\n' %(indent, fname, param0_name, full_name, dbg_obj_type, null_obj_ok)
1155 return pre_code
Tony Barbour2a199c12015-07-09 17:31:46 -06001156
Mike Stroyan3aecdb42015-04-03 17:13:23 -06001157 def generate_intercept(self, proto, qual):
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -07001158 if proto.name in [ 'CreateDebugReportCallbackEXT', 'EnumerateInstanceLayerProperties', 'EnumerateInstanceExtensionProperties','EnumerateDeviceLayerProperties', 'EnumerateDeviceExtensionProperties' ]:
Mike Stroyan723913e2015-04-03 14:39:16 -06001159 # use default version
1160 return None
Mark Lobodzinski4e5016f2015-05-05 15:01:37 -05001161
Tony Barbour2a199c12015-07-09 17:31:46 -06001162 # Create map of object names to object type enums of the form VkName : VkObjectTypeName
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -07001163 obj_type_mapping = {base_t : base_t.replace("Vk", "VkDebugReportObjectType") for base_t in vulkan.object_type_list}
Mark Lobodzinski4e5016f2015-05-05 15:01:37 -05001164 # Convert object type enum names from UpperCamelCase to UPPER_CASE_WITH_UNDERSCORES
1165 for objectName, objectTypeEnum in obj_type_mapping.items():
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -07001166 obj_type_mapping[objectName] = ucc_to_U_C_C(objectTypeEnum) + '_EXT';
Mark Lobodzinski4e5016f2015-05-05 15:01:37 -05001167 # Command Buffer Object doesn't follow the rule.
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -07001168 obj_type_mapping['VkCommandBuffer'] = "VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT"
1169 obj_type_mapping['VkShaderModule'] = "VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT"
Mike Stroyan723913e2015-04-03 14:39:16 -06001170
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001171 explicit_object_tracker_functions = [
1172 "CreateInstance",
Tobin Ehlis87f115c2015-09-15 15:02:17 -06001173 "EnumeratePhysicalDevices",
Cody Northropef72e2a2015-08-03 17:04:53 -06001174 "GetPhysicalDeviceQueueFamilyProperties",
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001175 "CreateDevice",
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001176 "GetDeviceQueue",
Chia-I Wu06809d52015-10-26 16:55:27 +08001177 "QueueBindSparse",
Chia-I Wu1f851912015-10-27 18:04:07 +08001178 "AllocateDescriptorSets",
Tony Barbour912e8152015-07-20 10:52:13 -06001179 "FreeDescriptorSets",
Mark Lobodzinski884f0b62016-01-26 09:55:28 -07001180 "CreateGraphicsPipelines",
1181 "CreateComputePipelines",
Mark Lobodzinski60b2b332015-11-12 16:02:35 -07001182 "AllocateCommandBuffers",
1183 "FreeCommandBuffers",
1184 "DestroyDescriptorPool",
1185 "DestroyCommandPool",
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001186 "MapMemory",
1187 "UnmapMemory",
1188 "FreeMemory",
Mark Lobodzinski9c483302015-10-14 13:16:33 -06001189 "DestroySwapchainKHR",
1190 "GetSwapchainImagesKHR"
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001191 ]
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001192 decl = proto.c_func(prefix="vk", attr="VKAPI")
Mike Stroyan723913e2015-04-03 14:39:16 -06001193 param0_name = proto.params[0].name
Mark Lobodzinskic44a5a12015-05-08 09:12:28 -05001194 using_line = ''
Mike Stroyan723913e2015-04-03 14:39:16 -06001195 create_line = ''
Mark Lobodzinski4a611132015-07-17 11:51:24 -06001196 destroy_line = ''
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001197 # 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
Tobin Ehlis0b99d032015-12-08 10:50:10 -07001198 # TODO : Should integrate slightly better code for this purpose from unique_objects layer
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001199 loop_params = defaultdict(list) # Dict uses loop count as key to make final code generation cleaner so params shared in single loop where needed
Michael Lentine1a85aa12015-11-04 14:35:12 -08001200 loop_types = defaultdict(list)
Tobin Ehlis33ce8fd2015-07-10 18:25:07 -06001201 # 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 -06001202 # or better yet, these should be encoded into an API json definition and we generate checks from there
1203 # 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)
1204 # param names may be directly passed to the function, or may be a field in a struct param
1205 valid_null_object_names = {'CreateGraphicsPipelines' : ['basePipelineHandle'],
1206 'CreateComputePipelines' : ['basePipelineHandle'],
1207 'BeginCommandBuffer' : ['renderPass', 'framebuffer'],
Tobin Ehlis87f115c2015-09-15 15:02:17 -06001208 'QueueSubmit' : ['fence'],
Jon Ashburn941a8f12016-01-14 15:11:55 -07001209 'AcquireNextImageKHR' : ['fence', 'semaphore' ],
Tobin Ehlis093ef922015-11-02 15:24:32 -07001210 'UpdateDescriptorSets' : ['pTexelBufferView'],
Tobin Ehlisc6a25752016-01-05 10:33:58 -07001211 'CreateSwapchainKHR' : ['oldSwapchain'],
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001212 }
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001213 param_count = 'NONE' # keep track of arrays passed directly into API functions
Tobin Ehlis586aa012015-06-08 17:36:28 -06001214 for p in proto.params:
Tobin Ehlis87f115c2015-09-15 15:02:17 -06001215 base_type = p.ty.replace('const ', '').strip('*')
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001216 if 'count' in p.name.lower():
1217 param_count = p.name
Tobin Ehlis87f115c2015-09-15 15:02:17 -06001218 if base_type in vulkan.core.objects:
1219 # This is an object to potentially check for validity. First see if it's an array
1220 if '*' in p.ty and 'const' in p.ty and param_count != 'NONE':
1221 loop_params[param_count].append(p.name)
Michael Lentine1a85aa12015-11-04 14:35:12 -08001222 loop_types[param_count].append(str(p.ty[6:-1]))
Tobin Ehlis87f115c2015-09-15 15:02:17 -06001223 # Not an array, check for just a base Object that's not in exceptions
1224 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 -06001225 loop_params[0].append(p.name)
Michael Lentine1a85aa12015-11-04 14:35:12 -08001226 loop_types[0].append(str(p.ty))
Tobin Ehlis87f115c2015-09-15 15:02:17 -06001227 elif vk_helper.is_type(base_type, 'struct'):
1228 struct_type = base_type
Tobin Ehlis25756af2015-06-30 14:32:16 -06001229 if vk_helper.typedef_rev_dict[struct_type] in vk_helper.struct_dict:
1230 struct_type = vk_helper.typedef_rev_dict[struct_type]
Tobin Ehlis04c05842015-10-23 17:52:53 -06001231 # Parse elements of this struct param to identify objects and/or arrays of objects
Tobin Ehlis25756af2015-06-30 14:32:16 -06001232 for m in sorted(vk_helper.struct_dict[struct_type]):
1233 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 -06001234 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]:
Tobin Ehlis04c05842015-10-23 17:52:53 -06001235 # This is not great, but gets the job done for now, but If we have a count and this param is a ptr w/
1236 # last letter 's' OR non-'count' string of count is in the param name, then this is a dynamically sized array param
1237 param_array = False
1238 if param_count != 'NONE':
1239 if '*' in p.ty:
1240 if 's' == p.name[-1] or param_count.lower().replace('count', '') in p.name.lower():
1241 param_array = True
1242 if param_array:
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001243 param_name = '%s[i].%s' % (p.name, vk_helper.struct_dict[struct_type][m]['name'])
Tobin Ehlis157e28d2015-07-10 11:10:21 -06001244 else:
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001245 param_name = '%s->%s' % (p.name, vk_helper.struct_dict[struct_type][m]['name'])
1246 if vk_helper.struct_dict[struct_type][m]['dyn_array']:
Tobin Ehlis04c05842015-10-23 17:52:53 -06001247 if param_count != 'NONE': # this will be a double-embedded loop, use comma delineated 'count,name' for param_name
1248 loop_count = '%s[i].%s' % (p.name, vk_helper.struct_dict[struct_type][m]['array_size'])
1249 loop_params[param_count].append('%s,%s' % (loop_count, param_name))
Michael Lentine1a85aa12015-11-04 14:35:12 -08001250 loop_types[param_count].append('%s' % (vk_helper.struct_dict[struct_type][m]['type']))
Tobin Ehlis04c05842015-10-23 17:52:53 -06001251 else:
1252 loop_count = '%s->%s' % (p.name, vk_helper.struct_dict[struct_type][m]['array_size'])
1253 loop_params[loop_count].append(param_name)
Michael Lentine1a85aa12015-11-04 14:35:12 -08001254 loop_types[loop_count].append('%s' % (vk_helper.struct_dict[struct_type][m]['type']))
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001255 else:
1256 if '[' in param_name: # dynamic array param, set size
1257 loop_params[param_count].append(param_name)
Michael Lentine1a85aa12015-11-04 14:35:12 -08001258 loop_types[param_count].append('%s' % (vk_helper.struct_dict[struct_type][m]['type']))
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001259 else:
1260 loop_params[0].append(param_name)
Michael Lentine1a85aa12015-11-04 14:35:12 -08001261 loop_types[0].append('%s' % (vk_helper.struct_dict[struct_type][m]['type']))
Tobin Ehlisc6a25752016-01-05 10:33:58 -07001262 last_param_index = None
1263 create_func = False
1264 if True in [create_txt in proto.name for create_txt in ['Create', 'Allocate']]:
1265 create_func = True
1266 last_param_index = -1 # For create funcs don't validate last object
Tobin Ehlis9aac5552016-02-10 15:38:45 -07001267 (struct_uses, local_decls) = get_object_uses(vulkan.object_type_list, proto.params[:last_param_index])
Mike Stroyan723913e2015-04-03 14:39:16 -06001268 funcs = []
Tobin Ehlis586aa012015-06-08 17:36:28 -06001269 mutex_unlock = False
Tobin Ehlis18cf7262015-08-26 11:22:09 -06001270 funcs.append('%s\n' % self.lineinfo.get())
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001271 if proto.name in explicit_object_tracker_functions:
Jon Ashburn630e44f2015-04-08 21:33:34 -06001272 funcs.append('%s%s\n'
1273 '{\n'
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001274 ' return explicit_%s;\n'
1275 '}' % (qual, decl, proto.c_call()))
1276 return "".join(funcs)
Mark Lobodzinski956e4ac2015-11-24 10:28:31 -07001277 # Temporarily prevent DestroySurface call from being generated until WSI layer support is fleshed out
Mark Lobodzinskid4585f32016-01-05 11:32:53 -07001278 elif 'DestroyInstance' in proto.name or 'DestroyDevice' in proto.name:
Mark Lobodzinski4a611132015-07-17 11:51:24 -06001279 return ""
Jon Ashburn630e44f2015-04-08 21:33:34 -06001280 else:
Tobin Ehlisc6a25752016-01-05 10:33:58 -07001281 if create_func:
Michael Lentine1a85aa12015-11-04 14:35:12 -08001282 typ = proto.params[-1].ty.strip('*').replace('const ', '');
1283 name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', typ)
1284 name = re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower()[3:]
Jeremy Hayes241a2db2016-04-13 10:54:17 -06001285 create_line = ' {\n'
1286 create_line += ' std::lock_guard<std::mutex> lock(global_lock);\n'
1287 create_line += ' if (result == VK_SUCCESS) {\n'
1288 create_line += ' create_%s(%s, *%s, %s);\n' % (name, param0_name, proto.params[-1].name, obj_type_mapping[typ])
1289 create_line += ' }\n'
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001290 create_line += ' }\n'
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06001291 if 'FreeCommandBuffers' in proto.name:
Michael Lentine1a85aa12015-11-04 14:35:12 -08001292 typ = proto.params[-1].ty.strip('*').replace('const ', '');
1293 name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', typ)
1294 name = re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower()[3:]
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06001295 funcs.append('%s\n' % self.lineinfo.get())
1296 destroy_line = ' loader_platform_thread_lock_mutex(&objLock);\n'
Chia-I Wu763a7492015-10-26 20:48:51 +08001297 destroy_line += ' for (uint32_t i = 0; i < commandBufferCount; i++) {\n'
Michael Lentine1a85aa12015-11-04 14:35:12 -08001298 destroy_line += ' destroy_%s(%s[i], %s[i]);\n' % (name, proto.params[-1].name, proto.params[-1].name)
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06001299 destroy_line += ' }\n'
1300 destroy_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
Mark Lobodzinski4a611132015-07-17 11:51:24 -06001301 if 'Destroy' in proto.name:
Michael Lentine1a85aa12015-11-04 14:35:12 -08001302 typ = proto.params[-2].ty.strip('*').replace('const ', '');
1303 name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', typ)
1304 name = re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower()[3:]
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06001305 funcs.append('%s\n' % self.lineinfo.get())
Jeremy Hayes241a2db2016-04-13 10:54:17 -06001306 destroy_line = ' {\n'
1307 destroy_line += ' std::lock_guard<std::mutex> lock(global_lock);\n'
1308 destroy_line += ' destroy_%s(%s, %s);\n' % (name, param0_name, proto.params[-2].name)
1309 destroy_line += ' }\n'
Tobin Ehlisc6a25752016-01-05 10:33:58 -07001310 indent = ' '
1311 if len(struct_uses) > 0:
1312 using_line += '%sVkBool32 skipCall = VK_FALSE;\n' % (indent)
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001313 if not mutex_unlock:
Jeremy Hayes241a2db2016-04-13 10:54:17 -06001314 using_line += '%s{\n' % (indent)
1315 indent += ' '
1316 using_line += '%sstd::lock_guard<std::mutex> lock(global_lock);\n' % (indent)
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001317 mutex_unlock = True
Mike Stroyan16d3c6c2016-04-07 12:14:30 -06001318 using_line += '// objects to validate: %s\n' % str(sorted(struct_uses))
Jeremy Hayes241a2db2016-04-13 10:54:17 -06001319 using_line += self._gen_obj_validate_code(struct_uses, obj_type_mapping, proto.name, valid_null_object_names, param0_name, indent, '', 0)
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001320 if mutex_unlock:
Jeremy Hayes241a2db2016-04-13 10:54:17 -06001321 indent = indent[4:]
1322 using_line += '%s}\n' % (indent)
Tobin Ehlisc6a25752016-01-05 10:33:58 -07001323 if len(struct_uses) > 0:
Tobin Ehlisf2f97402015-09-11 12:57:55 -06001324 using_line += ' if (skipCall)\n'
Jamie Madill59040f92016-04-04 12:15:39 -04001325 if proto.ret == "VkBool32":
1326 using_line += ' return VK_FALSE;\n'
1327 elif proto.ret != "void":
Courtney Goeltzenleuchter48605a52015-12-10 16:41:22 -07001328 using_line += ' return VK_ERROR_VALIDATION_FAILED_EXT;\n'
Tobin Ehlisf2f97402015-09-11 12:57:55 -06001329 else:
1330 using_line += ' return;\n'
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001331 ret_val = ''
1332 stmt = ''
Mark Lobodzinski14305ad2015-06-23 11:35:12 -06001333 if proto.ret != "void":
1334 ret_val = "%s result = " % proto.ret
1335 stmt = " return result;\n"
1336
1337 dispatch_param = proto.params[0].name
1338 if 'CreateInstance' in proto.name:
1339 dispatch_param = '*' + proto.params[1].name
1340
Mark Lobodzinskibbbc4842015-05-22 14:15:36 -05001341 # Must use 'instance' table for these APIs, 'device' table otherwise
1342 table_type = ""
1343 if proto_is_global(proto):
1344 table_type = "instance"
1345 else:
1346 table_type = "device"
Mark Lobodzinskib3e934d2015-12-10 16:25:21 -07001347 if wsi_name(proto.name):
1348 funcs.append('%s' % wsi_ifdef(proto.name))
Mike Stroyan723913e2015-04-03 14:39:16 -06001349 funcs.append('%s%s\n'
1350 '{\n'
1351 '%s'
Mike Stroyan723913e2015-04-03 14:39:16 -06001352 '%s'
Mark Lobodzinski2e87e612015-12-30 08:16:12 -07001353 ' %sget_dispatch_table(object_tracker_%s_table_map, %s)->%s;\n'
Mike Stroyane78816e2015-09-28 13:47:29 -06001354 '%s'
1355 '%s'
1356 '}' % (qual, decl, using_line, destroy_line, ret_val, table_type, dispatch_param, proto.c_call(), create_line, stmt))
Mark Lobodzinskib3e934d2015-12-10 16:25:21 -07001357 if wsi_name(proto.name):
1358 funcs.append('%s' % wsi_endif(proto.name))
Mike Stroyan723913e2015-04-03 14:39:16 -06001359 return "\n\n".join(funcs)
1360
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001361 def generate_body(self):
Mark Lobodzinski2e87e612015-12-30 08:16:12 -07001362 self.layer_name = "object_tracker"
Ian Elliottb134e842015-07-06 14:31:32 -06001363 extensions=[('wsi_enabled',
Ian Elliottc623ba52015-11-20 14:13:17 -07001364 ['vkCreateSwapchainKHR',
Jon Ashburn83334db2015-09-16 18:08:32 -06001365 'vkDestroySwapchainKHR', 'vkGetSwapchainImagesKHR',
1366 'vkAcquireNextImageKHR', 'vkQueuePresentKHR'])]
Mun, Gwan-gyeongc9e2e172016-02-22 09:43:09 +09001367 if self.wsi == 'Win32':
Michael Lentine81f1fd92015-12-03 14:33:09 -08001368 instance_extensions=[('msg_callback_get_proc_addr', []),
1369 ('wsi_enabled',
Michael Lentine90ee20e2016-03-02 17:28:55 -06001370 ['vkDestroySurfaceKHR',
1371 'vkGetPhysicalDeviceSurfaceSupportKHR',
Michael Lentine81f1fd92015-12-03 14:33:09 -08001372 'vkGetPhysicalDeviceSurfaceCapabilitiesKHR',
1373 'vkGetPhysicalDeviceSurfaceFormatsKHR',
1374 'vkGetPhysicalDeviceSurfacePresentModesKHR',
1375 'vkCreateWin32SurfaceKHR',
1376 'vkGetPhysicalDeviceWin32PresentationSupportKHR'])]
Mun, Gwan-gyeongc9e2e172016-02-22 09:43:09 +09001377 elif self.wsi == 'Android':
1378 instance_extensions=[('msg_callback_get_proc_addr', []),
1379 ('wsi_enabled',
Michael Lentine90ee20e2016-03-02 17:28:55 -06001380 ['vkDestroySurfaceKHR',
1381 'vkGetPhysicalDeviceSurfaceSupportKHR',
Mun, Gwan-gyeongc9e2e172016-02-22 09:43:09 +09001382 'vkGetPhysicalDeviceSurfaceCapabilitiesKHR',
1383 'vkGetPhysicalDeviceSurfaceFormatsKHR',
Michael Lentine90ee20e2016-03-02 17:28:55 -06001384 'vkGetPhysicalDeviceSurfacePresentModesKHR',
1385 'vkCreateAndroidSurfaceKHR'])]
Karl Schultzf6662232016-03-08 15:14:11 -07001386 elif self.wsi == 'Xcb' or self.wsi == 'Xlib' or self.wsi == 'Wayland' or self.wsi == 'Mir':
Michael Lentine81f1fd92015-12-03 14:33:09 -08001387 instance_extensions=[('msg_callback_get_proc_addr', []),
1388 ('wsi_enabled',
Michael Lentine90ee20e2016-03-02 17:28:55 -06001389 ['vkDestroySurfaceKHR',
1390 'vkGetPhysicalDeviceSurfaceSupportKHR',
Michael Lentine81f1fd92015-12-03 14:33:09 -08001391 'vkGetPhysicalDeviceSurfaceCapabilitiesKHR',
1392 'vkGetPhysicalDeviceSurfaceFormatsKHR',
1393 'vkGetPhysicalDeviceSurfacePresentModesKHR',
1394 'vkCreateXcbSurfaceKHR',
Karl Schultzf6662232016-03-08 15:14:11 -07001395 'vkGetPhysicalDeviceXcbPresentationSupportKHR',
Mun, Gwan-gyeongc9e2e172016-02-22 09:43:09 +09001396 'vkCreateXlibSurfaceKHR',
Karl Schultzf6662232016-03-08 15:14:11 -07001397 'vkGetPhysicalDeviceXlibPresentationSupportKHR',
Mun, Gwan-gyeongc9e2e172016-02-22 09:43:09 +09001398 'vkCreateWaylandSurfaceKHR',
Karl Schultzf6662232016-03-08 15:14:11 -07001399 'vkGetPhysicalDeviceWaylandPresentationSupportKHR',
Mun, Gwan-gyeongc9e2e172016-02-22 09:43:09 +09001400 'vkCreateMirSurfaceKHR',
1401 'vkGetPhysicalDeviceMirPresentationSupportKHR'])]
Mark Lobodzinski6effa802016-02-25 18:14:56 -07001402 else:
1403 print('Error: Undefined DisplayServer')
1404 instance_extensions=[]
1405
Tony Barbour2a199c12015-07-09 17:31:46 -06001406 body = [self.generate_maps(),
1407 self.generate_procs(),
Mark Lobodzinski4a611132015-07-17 11:51:24 -06001408 self.generate_destroy_instance(),
1409 self.generate_destroy_device(),
Tony Barbour2a199c12015-07-09 17:31:46 -06001410 self._generate_dispatch_entrypoints("VK_LAYER_EXPORT"),
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001411 self._generate_extensions(),
Jon Ashburn7e07faf2015-06-18 15:02:58 -06001412 self._generate_layer_gpa_function(extensions,
Jon Ashburnde4f1102015-09-17 10:00:32 -06001413 instance_extensions)]
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001414 return "\n\n".join(body)
Courtney Goeltzenleuchtere6094fc2014-11-18 10:40:29 -07001415
Tobin Ehlis0b99d032015-12-08 10:50:10 -07001416class UniqueObjectsSubcommand(Subcommand):
1417 def generate_header(self):
1418 header_txt = []
1419 header_txt.append('%s' % self.lineinfo.get())
1420 header_txt.append('#include "unique_objects.h"')
Tobin Ehlis0b99d032015-12-08 10:50:10 -07001421 return "\n".join(header_txt)
1422
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001423 # Generate UniqueObjects code for given struct_uses dict of objects that need to be unwrapped
Tobin Ehlis115790b2016-01-05 16:34:59 -07001424 # vector_name_set is used to make sure we don't replicate vector names
1425 # first_level_param indicates if elements are passed directly into the function else they're below a ptr/struct
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001426 # TODO : Comment this code
Tobin Ehlis9aac5552016-02-10 15:38:45 -07001427 def _gen_obj_code(self, struct_uses, param_type, indent, prefix, array_index, vector_name_set, first_level_param):
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001428 decls = ''
1429 pre_code = ''
1430 post_code = ''
Tobin Ehlis115790b2016-01-05 16:34:59 -07001431 for obj in sorted(struct_uses):
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001432 name = obj
1433 array = ''
1434 if '[' in obj:
1435 (name, array) = obj.split('[')
1436 array = array.strip(']')
Tobin Ehlis115790b2016-01-05 16:34:59 -07001437 ptr_type = False
Tobin Ehlis2fb0dba2016-04-13 12:59:43 -06001438 if 'p' == obj[0] and obj[1] != obj[1].lower(): # TODO : Not ideal way to determine ptr
Tobin Ehlis115790b2016-01-05 16:34:59 -07001439 ptr_type = True
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001440 if isinstance(struct_uses[obj], dict):
1441 local_prefix = ''
1442 name = '%s%s' % (prefix, name)
Tobin Ehlis115790b2016-01-05 16:34:59 -07001443 if ptr_type:
Tobin Ehlise05b1672016-02-12 14:37:09 -07001444 if first_level_param and name in param_type:
1445 pre_code += '%sif (%s) {\n' % (indent, name)
1446 else: # shadow ptr will have been initialized at this point so check it vs. source ptr
1447 pre_code += '%sif (local_%s) {\n' % (indent, name)
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001448 indent += ' '
1449 if array != '':
1450 idx = 'idx%s' % str(array_index)
1451 array_index += 1
Tobin Ehlis9aac5552016-02-10 15:38:45 -07001452 if first_level_param and name in param_type:
1453 pre_code += '%slocal_%s = new safe_%s[%s];\n' % (indent, name, param_type[name].strip('*'), array)
1454 post_code += ' if (local_%s)\n' % (name)
1455 post_code += ' delete[] local_%s;\n' % (name)
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001456 pre_code += '%sfor (uint32_t %s=0; %s<%s%s; ++%s) {\n' % (indent, idx, idx, prefix, array, idx)
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001457 indent += ' '
Tobin Ehlis9aac5552016-02-10 15:38:45 -07001458 if first_level_param:
1459 pre_code += '%slocal_%s[%s].initialize(&%s[%s]);\n' % (indent, name, idx, name, idx)
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001460 local_prefix = '%s[%s].' % (name, idx)
1461 elif ptr_type:
Tobin Ehlis9aac5552016-02-10 15:38:45 -07001462 if first_level_param and name in param_type:
1463 pre_code += '%slocal_%s = new safe_%s(%s);\n' % (indent, name, param_type[name].strip('*'), name)
1464 post_code += ' if (local_%s)\n' % (name)
1465 post_code += ' delete local_%s;\n' % (name)
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001466 local_prefix = '%s->' % (name)
1467 else:
1468 local_prefix = '%s.' % (name)
1469 assert isinstance(decls, object)
Tobin Ehlis9aac5552016-02-10 15:38:45 -07001470 (tmp_decl, tmp_pre, tmp_post) = self._gen_obj_code(struct_uses[obj], param_type, indent, local_prefix, array_index, vector_name_set, False)
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001471 decls += tmp_decl
1472 pre_code += tmp_pre
1473 post_code += tmp_post
1474 if array != '':
1475 indent = indent[4:]
1476 pre_code += '%s}\n' % (indent)
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001477 if ptr_type:
1478 indent = indent[4:]
1479 pre_code += '%s}\n' % (indent)
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001480 else:
Tobin Ehlis115790b2016-01-05 16:34:59 -07001481 if (array_index > 0) or array != '': # TODO : This is not ideal, really want to know if we're anywhere under an array
Tobin Ehlis9aac5552016-02-10 15:38:45 -07001482 if first_level_param:
Tobin Ehlis2fb0dba2016-04-13 12:59:43 -06001483 decls += '%s%s* local_%s = NULL;\n' % (indent, struct_uses[obj], name)
Tobin Ehlise05b1672016-02-12 14:37:09 -07001484 if array != '' and not first_level_param: # ptrs under structs will have been initialized so use local_*
1485 pre_code += '%sif (local_%s%s) {\n' %(indent, prefix, name)
1486 else:
1487 pre_code += '%sif (%s%s) {\n' %(indent, prefix, name)
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001488 indent += ' '
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001489 if array != '':
1490 idx = 'idx%s' % str(array_index)
1491 array_index += 1
Tobin Ehlis9aac5552016-02-10 15:38:45 -07001492 if first_level_param:
1493 pre_code += '%slocal_%s = new %s[%s];\n' % (indent, name, struct_uses[obj], array)
1494 post_code += ' if (local_%s)\n' % (name)
1495 post_code += ' delete[] local_%s;\n' % (name)
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001496 pre_code += '%sfor (uint32_t %s=0; %s<%s%s; ++%s) {\n' % (indent, idx, idx, prefix, array, idx)
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001497 indent += ' '
1498 name = '%s[%s]' % (name, idx)
1499 pName = 'p%s' % (struct_uses[obj][2:])
Tobin Ehlis115790b2016-01-05 16:34:59 -07001500 if name not in vector_name_set:
1501 vector_name_set.add(name)
Dustin Graves88f2e922016-04-14 17:29:20 -06001502 pre_code += '%slocal_%s%s = (%s)my_map_data->unique_id_mapping[reinterpret_cast<const uint64_t &>(%s%s)];\n' % (indent, prefix, name, struct_uses[obj], prefix, name)
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001503 if array != '':
1504 indent = indent[4:]
1505 pre_code += '%s}\n' % (indent)
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001506 indent = indent[4:]
1507 pre_code += '%s}\n' % (indent)
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001508 else:
Tobin Ehlis9aac5552016-02-10 15:38:45 -07001509 pre_code += '%s\n' % (self.lineinfo.get())
Tobin Ehlis115790b2016-01-05 16:34:59 -07001510 deref_txt = '&'
1511 if ptr_type:
1512 deref_txt = ''
Tobin Ehlis9aac5552016-02-10 15:38:45 -07001513 if '->' in prefix: # need to update local struct
Dustin Graves88f2e922016-04-14 17:29:20 -06001514 pre_code += '%slocal_%s%s = (%s)my_map_data->unique_id_mapping[reinterpret_cast<const uint64_t &>(%s%s)];\n' % (indent, prefix, name, struct_uses[obj], prefix, name)
Tobin Ehlis9aac5552016-02-10 15:38:45 -07001515 else:
Dustin Graves88f2e922016-04-14 17:29:20 -06001516 pre_code += '%s%s = (%s)my_map_data->unique_id_mapping[reinterpret_cast<uint64_t &>(%s)];\n' % (indent, name, struct_uses[obj], name)
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001517 return decls, pre_code, post_code
Tobin Ehlis0b99d032015-12-08 10:50:10 -07001518
1519 def generate_intercept(self, proto, qual):
1520 create_func = False
1521 destroy_func = False
1522 last_param_index = None #typcially we look at all params for ndos
1523 pre_call_txt = '' # code prior to calling down chain such as unwrap uses of ndos
1524 post_call_txt = '' # code following call down chain such to wrap newly created ndos, or destroy local wrap struct
1525 funcs = []
1526 indent = ' ' # indent level for generated code
1527 decl = proto.c_func(prefix="vk", attr="VKAPI")
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001528 # A few API cases that are manual code
Tobin Ehlis115790b2016-01-05 16:34:59 -07001529 # TODO : Special case Create*Pipelines funcs to handle creating multiple unique objects
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001530 explicit_object_tracker_functions = ['GetSwapchainImagesKHR',
Tobin Ehlis2fb0dba2016-04-13 12:59:43 -06001531 'CreateSwapchainKHR',
Tobin Ehlis0b99d032015-12-08 10:50:10 -07001532 'CreateInstance',
Tobin Ehlis2fb0dba2016-04-13 12:59:43 -06001533 'DestroyInstance',
Tobin Ehlis115790b2016-01-05 16:34:59 -07001534 'CreateDevice',
Tobin Ehlis2fb0dba2016-04-13 12:59:43 -06001535 'DestroyDevice',
Tobin Ehlis115790b2016-01-05 16:34:59 -07001536 'CreateComputePipelines',
Tobin Ehlis9aac5552016-02-10 15:38:45 -07001537 'CreateGraphicsPipelines'
1538 ]
Tobin Ehlis6e7dc242016-01-29 14:24:42 -07001539 # TODO : This is hacky, need to make this a more general-purpose solution for all layers
Cody Northrop6d865462016-02-24 12:28:41 -07001540 ifdef_dict = {'CreateXcbSurfaceKHR': 'VK_USE_PLATFORM_XCB_KHR',
1541 'CreateAndroidSurfaceKHR': 'VK_USE_PLATFORM_ANDROID_KHR',
Tony Barbourb38f69d2016-04-12 13:35:51 -06001542 'CreateWin32SurfaceKHR': 'VK_USE_PLATFORM_WIN32_KHR',
1543 'CreateXlibSurfaceKHR': 'VK_USE_PLATFORM_XLIB_KHR',
1544 'CreateWaylandSurfaceKHR': 'VK_USE_PLATFORM_WAYLAND_KHR',
1545 'CreateMirSurfaceKHR': 'VK_USE_PLATFORM_MIR_KHR'}
Tobin Ehlis0b99d032015-12-08 10:50:10 -07001546 # Give special treatment to create functions that return multiple new objects
1547 # This dict stores array name and size of array
Jon Ashburna4ae48b2016-01-11 13:12:43 -07001548 custom_create_dict = {'pDescriptorSets' : 'pAllocateInfo->descriptorSetCount'}
Courtney Goeltzenleuchter7d703822016-02-11 11:44:04 -07001549 pre_call_txt += '%s\n' % (self.lineinfo.get())
Tobin Ehlis0b99d032015-12-08 10:50:10 -07001550 if proto.name in explicit_object_tracker_functions:
1551 funcs.append('%s%s\n'
1552 '{\n'
1553 ' return explicit_%s;\n'
1554 '}' % (qual, decl, proto.c_call()))
1555 return "".join(funcs)
1556 if True in [create_txt in proto.name for create_txt in ['Create', 'Allocate']]:
1557 create_func = True
1558 last_param_index = -1 # For create funcs don't care if last param is ndo
1559 if True in [destroy_txt in proto.name for destroy_txt in ['Destroy', 'Free']]:
1560 destroy_obj_type = proto.params[-2].ty
1561 if destroy_obj_type in vulkan.object_non_dispatch_list:
1562 destroy_func = True
1563
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001564 # First thing we need to do is gather uses of non-dispatchable-objects (ndos)
Tobin Ehlis9aac5552016-02-10 15:38:45 -07001565 (struct_uses, local_decls) = get_object_uses(vulkan.object_non_dispatch_list, proto.params[1:last_param_index])
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001566
Tobin Ehlis2fb0dba2016-04-13 12:59:43 -06001567 dispatch_param = proto.params[0].name
1568 if 'CreateInstance' in proto.name:
1569 dispatch_param = '*' + proto.params[1].name
1570 pre_call_txt += '%slayer_data *my_map_data = get_my_data_ptr(get_dispatch_key(%s), layer_data_map);\n' % (indent, dispatch_param)
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001571 if len(struct_uses) > 0:
Mike Stroyan16d3c6c2016-04-07 12:14:30 -06001572 pre_call_txt += '// STRUCT USES:%s\n' % sorted(struct_uses)
Tobin Ehlis9aac5552016-02-10 15:38:45 -07001573 if len(local_decls) > 0:
Mike Stroyan16d3c6c2016-04-07 12:14:30 -06001574 pre_call_txt += '//LOCAL DECLS:%s\n' % sorted(local_decls)
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001575 if destroy_func: # only one object
Mike Stroyan16d3c6c2016-04-07 12:14:30 -06001576 for del_obj in sorted(struct_uses):
Tobin Ehlis2fb0dba2016-04-13 12:59:43 -06001577 #pre_call_txt += '%s%s local_%s = %s;\n' % (indent, struct_uses[del_obj], del_obj, del_obj)
Dustin Graves88f2e922016-04-14 17:29:20 -06001578 pre_call_txt += '%suint64_t local_%s = reinterpret_cast<uint64_t &>(%s);\n' % (indent, del_obj, del_obj)
Tobin Ehlis2fb0dba2016-04-13 12:59:43 -06001579 pre_call_txt += '%s%s = (%s)my_map_data->unique_id_mapping[local_%s];\n' % (indent, del_obj, struct_uses[del_obj], del_obj)
1580 (pre_decl, pre_code, post_code) = ('', '', '')
1581 else:
1582 (pre_decl, pre_code, post_code) = self._gen_obj_code(struct_uses, local_decls, ' ', '', 0, set(), True)
Tobin Ehlis9aac5552016-02-10 15:38:45 -07001583 # This is a bit hacky but works for now. Need to decl local versions of top-level structs
Mike Stroyan16d3c6c2016-04-07 12:14:30 -06001584 for ld in sorted(local_decls):
Tobin Ehlis9aac5552016-02-10 15:38:45 -07001585 init_null_txt = 'NULL';
1586 if '*' not in local_decls[ld]:
1587 init_null_txt = '{}';
1588 if local_decls[ld].strip('*') not in vulkan.object_non_dispatch_list:
1589 pre_decl += ' safe_%s local_%s = %s;\n' % (local_decls[ld], ld, init_null_txt)
Tobin Ehlis2fb0dba2016-04-13 12:59:43 -06001590 if pre_code != '': # lock around map uses
1591 pre_code = '%s{\n%sstd::lock_guard<std::mutex> lock(global_lock);\n%s%s}\n' % (indent, indent, pre_code, indent)
Tobin Ehlis2ece1592016-01-05 09:46:03 -07001592 pre_call_txt += '%s%s' % (pre_decl, pre_code)
Tobin Ehlis9aac5552016-02-10 15:38:45 -07001593 post_call_txt += '%s' % (post_code)
Tobin Ehlis0b99d032015-12-08 10:50:10 -07001594 elif create_func:
1595 base_type = proto.params[-1].ty.replace('const ', '').strip('*')
1596 if base_type not in vulkan.object_non_dispatch_list:
1597 return None
1598 else:
1599 return None
1600
1601 ret_val = ''
1602 ret_stmt = ''
1603 if proto.ret != "void":
1604 ret_val = "%s result = " % proto.ret
1605 ret_stmt = " return result;\n"
Tobin Ehlis0b99d032015-12-08 10:50:10 -07001606 if create_func:
1607 obj_type = proto.params[-1].ty.strip('*')
1608 obj_name = proto.params[-1].name
1609 if obj_type in vulkan.object_non_dispatch_list:
1610 local_name = "unique%s" % obj_type[2:]
1611 post_call_txt += '%sif (VK_SUCCESS == result) {\n' % (indent)
1612 indent += ' '
Tobin Ehlis2fb0dba2016-04-13 12:59:43 -06001613 post_call_txt += '%sstd::lock_guard<std::mutex> lock(global_lock);\n' % (indent)
Tobin Ehlis0b99d032015-12-08 10:50:10 -07001614 if obj_name in custom_create_dict:
1615 post_call_txt += '%s\n' % (self.lineinfo.get())
1616 local_name = '%ss' % (local_name) # add 's' to end for vector of many
Tobin Ehlis0b99d032015-12-08 10:50:10 -07001617 post_call_txt += '%sfor (uint32_t i=0; i<%s; ++i) {\n' % (indent, custom_create_dict[obj_name])
1618 indent += ' '
Tobin Ehlis2fb0dba2016-04-13 12:59:43 -06001619 post_call_txt += '%suint64_t unique_id = my_map_data->unique_id++;\n' % (indent)
Dustin Graves88f2e922016-04-14 17:29:20 -06001620 post_call_txt += '%smy_map_data->unique_id_mapping[unique_id] = reinterpret_cast<uint64_t &>(%s[i]);\n' % (indent, obj_name)
1621 post_call_txt += '%s%s[i] = reinterpret_cast<%s&>(unique_id);\n' % (indent, obj_name, obj_type)
Tobin Ehlis0b99d032015-12-08 10:50:10 -07001622 indent = indent[4:]
1623 post_call_txt += '%s}\n' % (indent)
1624 else:
1625 post_call_txt += '%s\n' % (self.lineinfo.get())
Tobin Ehlis2fb0dba2016-04-13 12:59:43 -06001626 post_call_txt += '%suint64_t unique_id = my_map_data->unique_id++;\n' % (indent)
Dustin Graves88f2e922016-04-14 17:29:20 -06001627 post_call_txt += '%smy_map_data->unique_id_mapping[unique_id] = reinterpret_cast<uint64_t &>(*%s);\n' % (indent, obj_name)
1628 post_call_txt += '%s*%s = reinterpret_cast<%s&>(unique_id);\n' % (indent, obj_name, obj_type)
Tobin Ehlis0b99d032015-12-08 10:50:10 -07001629 indent = indent[4:]
1630 post_call_txt += '%s}\n' % (indent)
1631 elif destroy_func:
1632 del_obj = proto.params[-2].name
1633 if 'count' in del_obj.lower():
1634 post_call_txt += '%s\n' % (self.lineinfo.get())
1635 post_call_txt += '%sfor (uint32_t i=0; i<%s; ++i) {\n' % (indent, del_obj)
1636 del_obj = proto.params[-1].name
1637 indent += ' '
1638 post_call_txt += '%sdelete (VkUniqueObject*)%s[i];\n' % (indent, del_obj)
1639 indent = indent[4:]
1640 post_call_txt += '%s}\n' % (indent)
1641 else:
1642 post_call_txt += '%s\n' % (self.lineinfo.get())
Tobin Ehlis2fb0dba2016-04-13 12:59:43 -06001643 post_call_txt += '%sstd::lock_guard<std::mutex> lock(global_lock);\n' % (indent)
1644 post_call_txt += '%smy_map_data->unique_id_mapping.erase(local_%s);\n' % (indent, proto.params[-2].name)
Tobin Ehlis0b99d032015-12-08 10:50:10 -07001645
1646 call_sig = proto.c_call()
Tobin Ehlis9aac5552016-02-10 15:38:45 -07001647 # Replace default params with any custom local params
1648 for ld in local_decls:
1649 call_sig = call_sig.replace(ld, '(const %s)local_%s' % (local_decls[ld], ld))
Tobin Ehlis0b99d032015-12-08 10:50:10 -07001650 if proto_is_global(proto):
1651 table_type = "instance"
1652 else:
1653 table_type = "device"
1654 pre_call_txt += '%s\n' % (self.lineinfo.get())
Tobin Ehlis6e7dc242016-01-29 14:24:42 -07001655 open_ifdef = ''
1656 close_ifdef = ''
1657 if proto.name in ifdef_dict:
1658 open_ifdef = '#ifdef %s\n' % (ifdef_dict[proto.name])
1659 close_ifdef = '#endif\n'
1660 funcs.append('%s'
1661 '%s%s\n'
Tobin Ehlis0b99d032015-12-08 10:50:10 -07001662 '{\n'
1663 '%s'
1664 ' %sget_dispatch_table(unique_objects_%s_table_map, %s)->%s;\n'
1665 '%s'
1666 '%s'
Tobin Ehlis6e7dc242016-01-29 14:24:42 -07001667 '}\n'
1668 '%s' % (open_ifdef, qual, decl, pre_call_txt, ret_val, table_type, dispatch_param, call_sig, post_call_txt, ret_stmt, close_ifdef))
Tobin Ehlis0b99d032015-12-08 10:50:10 -07001669 return "\n\n".join(funcs)
1670
1671 def generate_body(self):
1672 self.layer_name = "unique_objects"
1673 extensions=[('wsi_enabled',
1674 ['vkCreateSwapchainKHR',
1675 'vkDestroySwapchainKHR', 'vkGetSwapchainImagesKHR',
1676 'vkAcquireNextImageKHR', 'vkQueuePresentKHR'])]
Mun, Gwan-gyeongc9e2e172016-02-22 09:43:09 +09001677 if self.wsi == 'Win32':
Tobin Ehlis0b99d032015-12-08 10:50:10 -07001678 instance_extensions=[('wsi_enabled',
Michael Lentine90ee20e2016-03-02 17:28:55 -06001679 ['vkDestroySurfaceKHR',
1680 'vkGetPhysicalDeviceSurfaceSupportKHR',
Tobin Ehlis0b99d032015-12-08 10:50:10 -07001681 'vkGetPhysicalDeviceSurfaceCapabilitiesKHR',
1682 'vkGetPhysicalDeviceSurfaceFormatsKHR',
1683 'vkGetPhysicalDeviceSurfacePresentModesKHR',
Jon Ashburn2be61582016-01-07 16:13:06 -07001684 'vkCreateWin32SurfaceKHR'
1685 ])]
Mun, Gwan-gyeongc9e2e172016-02-22 09:43:09 +09001686 elif self.wsi == 'Android':
1687 instance_extensions=[('wsi_enabled',
Michael Lentine90ee20e2016-03-02 17:28:55 -06001688 ['vkDestroySurfaceKHR',
1689 'vkGetPhysicalDeviceSurfaceSupportKHR',
Mun, Gwan-gyeongc9e2e172016-02-22 09:43:09 +09001690 'vkGetPhysicalDeviceSurfaceCapabilitiesKHR',
1691 'vkGetPhysicalDeviceSurfaceFormatsKHR',
Michael Lentine90ee20e2016-03-02 17:28:55 -06001692 'vkGetPhysicalDeviceSurfacePresentModesKHR',
1693 'vkCreateAndroidSurfaceKHR'])]
Karl Schultzf6662232016-03-08 15:14:11 -07001694 elif self.wsi == 'Xcb' or self.wsi == 'Xlib' or self.wsi == 'Wayland' or self.wsi == 'Mir':
Tobin Ehlis0b99d032015-12-08 10:50:10 -07001695 instance_extensions=[('wsi_enabled',
Michael Lentine90ee20e2016-03-02 17:28:55 -06001696 ['vkDestroySurfaceKHR',
1697 'vkGetPhysicalDeviceSurfaceSupportKHR',
Tobin Ehlis0b99d032015-12-08 10:50:10 -07001698 'vkGetPhysicalDeviceSurfaceCapabilitiesKHR',
1699 'vkGetPhysicalDeviceSurfaceFormatsKHR',
1700 'vkGetPhysicalDeviceSurfacePresentModesKHR',
Courtney Goeltzenleuchter7d703822016-02-11 11:44:04 -07001701 'vkCreateXcbSurfaceKHR',
Karl Schultzf6662232016-03-08 15:14:11 -07001702 'vkCreateXlibSurfaceKHR',
1703 'vkCreateWaylandSurfaceKHR',
Mun, Gwan-gyeongc9e2e172016-02-22 09:43:09 +09001704 'vkCreateMirSurfaceKHR'
1705 ])]
Karl Schultzf6662232016-03-08 15:14:11 -07001706 else:
1707 print('Error: Undefined DisplayServer')
1708 instance_extensions=[]
1709
Tobin Ehlis0b99d032015-12-08 10:50:10 -07001710 body = [self._generate_dispatch_entrypoints("VK_LAYER_EXPORT"),
1711 self._generate_layer_gpa_function(extensions,
1712 instance_extensions)]
1713 return "\n\n".join(body)
1714
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001715def main():
Mun, Gwan-gyeongc9e2e172016-02-22 09:43:09 +09001716 wsi = {
1717 "Win32",
1718 "Android",
1719 "Xcb",
1720 "Xlib",
1721 "Wayland",
1722 "Mir",
1723 }
1724
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001725 subcommands = {
Mark Lobodzinski2e87e612015-12-30 08:16:12 -07001726 "object_tracker" : ObjectTrackerSubcommand,
Tobin Ehlis0b99d032015-12-08 10:50:10 -07001727 "unique_objects" : UniqueObjectsSubcommand,
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001728 }
1729
Mun, Gwan-gyeongc9e2e172016-02-22 09:43:09 +09001730 if len(sys.argv) < 4 or sys.argv[1] not in wsi or sys.argv[2] not in subcommands or not os.path.exists(sys.argv[3]):
1731 print("Usage: %s <wsi> <subcommand> <input_header> [options]" % sys.argv[0])
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001732 print
Tobin Ehlis2f3726c2015-01-15 17:51:52 -07001733 print("Available subcommands are: %s" % " ".join(subcommands))
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001734 exit(1)
1735
Mun, Gwan-gyeongc9e2e172016-02-22 09:43:09 +09001736 hfp = vk_helper.HeaderFileParser(sys.argv[3])
Tobin Ehlis6cd06372014-12-17 17:44:50 -07001737 hfp.parse()
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001738 vk_helper.enum_val_dict = hfp.get_enum_val_dict()
1739 vk_helper.enum_type_dict = hfp.get_enum_type_dict()
1740 vk_helper.struct_dict = hfp.get_struct_dict()
1741 vk_helper.typedef_fwd_dict = hfp.get_typedef_fwd_dict()
1742 vk_helper.typedef_rev_dict = hfp.get_typedef_rev_dict()
1743 vk_helper.types_dict = hfp.get_types_dict()
Tobin Ehlis6cd06372014-12-17 17:44:50 -07001744
Mun, Gwan-gyeongc9e2e172016-02-22 09:43:09 +09001745 subcmd = subcommands[sys.argv[2]](sys.argv[3:])
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001746 subcmd.run()
1747
1748if __name__ == "__main__":
1749 main()