blob: 14354df96eb0e26a4abbf60899e3c6878413711e [file] [log] [blame]
Mark Lobodzinskib13c4ea2016-05-02 13:11:50 -06001#!/usr/bin/env python3
Tobin Ehlis12076fc2014-10-22 09:06:33 -06002#
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06003# VK
Tobin Ehlis12076fc2014-10-22 09:06:33 -06004#
Mark Lobodzinski6eda00a2016-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 Ehlis12076fc2014-10-22 09:06:33 -06009#
Jon Ashburn3ebf1252016-04-19 11:30:31 -060010# Licensed under the Apache License, Version 2.0 (the "License");
11# you may not use this file except in compliance with the License.
12# You may obtain a copy of the License at
Tobin Ehlis12076fc2014-10-22 09:06:33 -060013#
Jon Ashburn3ebf1252016-04-19 11:30:31 -060014# http://www.apache.org/licenses/LICENSE-2.0
Tobin Ehlis12076fc2014-10-22 09:06:33 -060015#
Jon Ashburn3ebf1252016-04-19 11:30:31 -060016# Unless required by applicable law or agreed to in writing, software
17# distributed under the License is distributed on an "AS IS" BASIS,
18# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19# See the License for the specific language governing permissions and
20# limitations under the License.
Tobin Ehlis12076fc2014-10-22 09:06:33 -060021#
Tobin Ehlisd34a4c52015-12-08 10:50:10 -070022# Author: Tobin Ehlis <tobine@google.com>
23# Author: Courtney Goeltzenleuchter <courtneygo@google.com>
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060024# Author: Jon Ashburn <jon@lunarg.com>
25# Author: Mark Lobodzinski <mark@lunarg.com>
Tobin Ehlisd34a4c52015-12-08 10:50:10 -070026# Author: Mike Stroyan <stroyan@google.com>
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060027# Author: Tony Barbour <tony@LunarG.com>
Tobin Ehlisd34a4c52015-12-08 10:50:10 -070028# Author: Chia-I Wu <olv@google.com>
Mun, Gwan-gyeongbd4dd592016-02-22 09:43:09 +090029# Author: Gwan-gyeong Mun <kk.moon@samsung.com>
Tobin Ehlis12076fc2014-10-22 09:06:33 -060030
31import sys
Tobin Ehlis14ff0852014-12-17 17:44:50 -070032import os
Mark Lobodzinski7c75b852015-05-05 15:01:37 -050033import re
Tobin Ehlis12076fc2014-10-22 09:06:33 -060034
Courtney Goeltzenleuchterf53c3cb2015-04-14 14:55:44 -060035import vulkan
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060036import vk_helper
Tobin Ehlis08fafd02015-06-12 12:49:01 -060037from source_line_info import sourcelineinfo
Tobin Ehlis154e0462015-08-26 11:22:09 -060038from collections import defaultdict
Tobin Ehlis12076fc2014-10-22 09:06:33 -060039
Jon Ashburn95a77ba2015-05-15 15:09:35 -060040def proto_is_global(proto):
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -070041 global_function_names = [
42 "CreateInstance",
43 "EnumerateInstanceLayerProperties",
44 "EnumerateInstanceExtensionProperties",
45 "EnumerateDeviceLayerProperties",
46 "EnumerateDeviceExtensionProperties",
Petros Bantolas2b40be72016-04-15 11:02:59 +010047 "CreateDisplayPlaneSurfaceKHR",
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -070048 "CreateXcbSurfaceKHR",
Jon Ashburn00dc7412016-01-07 16:13:06 -070049 "GetPhysicalDeviceXcbPresentationSupportKHR",
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -070050 "CreateXlibSurfaceKHR",
Jon Ashburn00dc7412016-01-07 16:13:06 -070051 "GetPhysicalDeviceXlibPresentationSupportKHR",
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -070052 "CreateWaylandSurfaceKHR",
Jon Ashburn00dc7412016-01-07 16:13:06 -070053 "GetPhysicalDeviceWaylandPresentationSupportKHR",
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -070054 "CreateMirSurfaceKHR",
Jon Ashburn00dc7412016-01-07 16:13:06 -070055 "GetPhysicalDeviceMirPresentationSupportKHR",
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -070056 "CreateAndroidSurfaceKHR",
57 "CreateWin32SurfaceKHR",
Jon Ashburn00dc7412016-01-07 16:13:06 -070058 "GetPhysicalDeviceWin32PresentationSupportKHR"
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -070059 ]
60 if proto.params[0].ty == "VkInstance" or proto.params[0].ty == "VkPhysicalDevice" or proto.name in global_function_names:
Jon Ashburn95a77ba2015-05-15 15:09:35 -060061 return True
62 else:
63 return False
64
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -070065def wsi_name(ext_name):
66 wsi_prefix = ""
67 if 'Xcb' in ext_name:
68 wsi_prefix = 'XCB'
69 elif 'Xlib' in ext_name:
70 wsi_prefix = 'XLIB'
71 elif 'Win32' in ext_name:
72 wsi_prefix = 'WIN32'
73 elif 'Mir' in ext_name:
74 wsi_prefix = 'MIR'
75 elif 'Wayland' in ext_name:
76 wsi_prefix = 'WAYLAND'
77 elif 'Android' in ext_name:
78 wsi_prefix = 'ANDROID'
79 else:
80 wsi_prefix = ''
81 return wsi_prefix
82
83def wsi_ifdef(ext_name):
84 wsi_prefix = wsi_name(ext_name)
85 if not wsi_prefix:
86 return ''
87 else:
88 return "#ifdef VK_USE_PLATFORM_%s_KHR" % wsi_prefix
89
90def wsi_endif(ext_name):
91 wsi_prefix = wsi_name(ext_name)
92 if not wsi_prefix:
93 return ''
94 else:
95 return "#endif // VK_USE_PLATFORM_%s_KHR" % wsi_prefix
96
Mike Stroyan938c2532015-04-03 13:58:35 -060097def generate_get_proc_addr_check(name):
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060098 return " if (!%s || %s[0] != 'v' || %s[1] != 'k')\n" \
99 " return NULL;" % ((name,) * 3)
Mike Stroyan938c2532015-04-03 13:58:35 -0600100
Mark Lobodzinski7c75b852015-05-05 15:01:37 -0500101def ucc_to_U_C_C(CamelCase):
102 temp = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', CamelCase)
103 return re.sub('([a-z0-9])([A-Z])', r'\1_\2', temp).upper()
104
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700105# Parse complete struct chain and add any new ndo_uses to the dict
106def gather_object_uses_in_struct(obj_list, struct_type):
107 struct_uses = {}
108 if vk_helper.typedef_rev_dict[struct_type] in vk_helper.struct_dict:
109 struct_type = vk_helper.typedef_rev_dict[struct_type]
110 # Parse elements of this struct param to identify objects and/or arrays of objects
111 for m in sorted(vk_helper.struct_dict[struct_type]):
112 array_len = "%s" % (str(vk_helper.struct_dict[struct_type][m]['array_size']))
113 base_type = vk_helper.struct_dict[struct_type][m]['type']
114 mem_name = vk_helper.struct_dict[struct_type][m]['name']
115 if array_len != '0':
116 mem_name = "%s[%s]" % (mem_name, array_len)
117 if base_type in obj_list:
118 #if array_len not in ndo_uses:
119 # struct_uses[array_len] = []
120 #struct_uses[array_len].append("%s%s,%s" % (name_prefix, struct_name, base_type))
121 struct_uses[mem_name] = base_type
122 elif vk_helper.is_type(base_type, 'struct'):
123 sub_uses = gather_object_uses_in_struct(obj_list, base_type)
124 if len(sub_uses) > 0:
125 struct_uses[mem_name] = sub_uses
126 return struct_uses
127
128# For the given list of object types, Parse the given list of params
129# and return dict of params that use one of the obj_list types
130# Format of the dict is that terminal elements have <name>,<type>
131# non-terminal elements will have <name>[<array_size>]
132# TODO : This analysis could be done up-front at vk_helper time
133def get_object_uses(obj_list, params):
134 obj_uses = {}
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -0700135 local_decls = {}
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700136 param_count = 'NONE' # track params that give array sizes
137 for p in params:
138 base_type = p.ty.replace('const ', '').strip('*')
139 array_len = ''
140 is_ptr = False
141 if 'count' in p.name.lower():
142 param_count = p.name
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -0700143 ptr_txt = ''
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700144 if '*' in p.ty:
145 is_ptr = True
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -0700146 ptr_txt = '*'
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700147 if base_type in obj_list:
148 if is_ptr and 'const' in p.ty and param_count != 'NONE':
149 array_len = "[%s]" % param_count
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -0700150 # Non-arrays we can overwrite in place, but need local decl for arrays
151 local_decls[p.name] = '%s%s' % (base_type, ptr_txt)
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700152 #if array_len not in obj_uses:
153 # obj_uses[array_len] = {}
154 # obj_uses[array_len][p.name] = base_type
155 obj_uses["%s%s" % (p.name, array_len)] = base_type
156 elif vk_helper.is_type(base_type, 'struct'):
157 struct_name = p.name
158 if 'NONE' != param_count:
159 struct_name = "%s[%s]" % (struct_name, param_count)
160 struct_uses = gather_object_uses_in_struct(obj_list, base_type)
161 if len(struct_uses) > 0:
162 obj_uses[struct_name] = struct_uses
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -0700163 # This is a top-level struct w/ uses below it, so need local decl
164 local_decls['%s' % (p.name)] = '%s%s' % (base_type, ptr_txt)
165 return (obj_uses, local_decls)
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700166
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600167class Subcommand(object):
Jamie Madilldbda66b2016-05-10 07:36:20 -0700168 def __init__(self, outfile):
169 self.outfile = outfile
Courtney Goeltzenleuchterf53c3cb2015-04-14 14:55:44 -0600170 self.headers = vulkan.headers
171 self.protos = vulkan.protos
Mike Stroyan3e3a1eb2015-04-03 17:13:23 -0600172 self.no_addr = False
173 self.layer_name = ""
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600174 self.lineinfo = sourcelineinfo()
Mun, Gwan-gyeongbd4dd592016-02-22 09:43:09 +0900175 self.wsi = sys.argv[1]
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600176
177 def run(self):
Jamie Madilldbda66b2016-05-10 07:36:20 -0700178 if self.outfile:
179 with open(self.outfile, "w") as outfile:
180 outfile.write(self.generate())
181 else:
182 print(self.generate())
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600183
184 def generate(self):
185 copyright = self.generate_copyright()
186 header = self.generate_header()
187 body = self.generate_body()
188 footer = self.generate_footer()
189
190 contents = []
191 if copyright:
192 contents.append(copyright)
193 if header:
194 contents.append(header)
195 if body:
196 contents.append(body)
197 if footer:
198 contents.append(footer)
199
200 return "\n\n".join(contents)
201
202 def generate_copyright(self):
203 return """/* THIS FILE IS GENERATED. DO NOT EDIT. */
204
205/*
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -0700206 * Copyright (c) 2015-2016 The Khronos Group Inc.
207 * Copyright (c) 2015-2016 Valve Corporation
208 * Copyright (c) 2015-2016 LunarG, Inc.
Tobin Ehlis10ba1de2016-04-13 12:59:43 -0600209 * Copyright (c) 2015-2016 Google, Inc.
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600210 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -0600211 * Licensed under the Apache License, Version 2.0 (the "License");
212 * you may not use this file except in compliance with the License.
213 * You may obtain a copy of the License at
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600214 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -0600215 * http://www.apache.org/licenses/LICENSE-2.0
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600216 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -0600217 * Unless required by applicable law or agreed to in writing, software
218 * distributed under the License is distributed on an "AS IS" BASIS,
219 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
220 * See the License for the specific language governing permissions and
221 * limitations under the License.
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -0600222 *
Tobin Ehlisd34a4c52015-12-08 10:50:10 -0700223 * Author: Tobin Ehlis <tobine@google.com>
224 * Author: Courtney Goeltzenleuchter <courtneygo@google.com>
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -0600225 * Author: Jon Ashburn <jon@lunarg.com>
226 * Author: Mark Lobodzinski <mark@lunarg.com>
Tobin Ehlisd34a4c52015-12-08 10:50:10 -0700227 * Author: Mike Stroyan <stroyan@google.com>
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -0600228 * Author: Tony Barbour <tony@LunarG.com>
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600229 */"""
230
231 def generate_header(self):
232 return "\n".join(["#include <" + h + ">" for h in self.headers])
233
234 def generate_body(self):
235 pass
236
237 def generate_footer(self):
238 pass
239
240 # Return set of printf '%' qualifier and input to that qualifier
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600241 def _get_printf_params(self, vk_type, name, output_param, cpp=False):
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600242 # TODO : Need ENUM and STRUCT checks here
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600243 if vk_helper.is_type(vk_type, 'enum'):#"_TYPE" in vk_type: # TODO : This should be generic ENUM check
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600244 return ("%s", "string_%s(%s)" % (vk_type.replace('const ', '').strip('*'), name))
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600245 if "char*" == vk_type:
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600246 return ("%s", name)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600247 if "uint64" in vk_type:
248 if '*' in vk_type:
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600249 return ("%lu", "*%s" % name)
250 return ("%lu", name)
Tobin Ehlisa30e7e52015-07-06 14:02:36 -0600251 if vk_type.strip('*') in vulkan.object_non_dispatch_list:
252 if '*' in vk_type:
Chia-I Wue2fc5522015-10-26 20:04:44 +0800253 return ("%lu", "%s" % name)
254 return ("%lu", "%s" % name)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600255 if "size" in vk_type:
256 if '*' in vk_type:
Mark Lobodzinskia1456492015-10-06 09:57:52 -0600257 return ("%lu", "(unsigned long)*%s" % name)
258 return ("%lu", "(unsigned long)%s" % name)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600259 if "float" in vk_type:
260 if '[' in vk_type: # handle array, current hard-coded to 4 (TODO: Make this dynamic)
Tobin Ehlis99f88672015-01-10 12:42:41 -0700261 if cpp:
262 return ("[%i, %i, %i, %i]", '"[" << %s[0] << "," << %s[1] << "," << %s[2] << "," << %s[3] << "]"' % (name, name, name, name))
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600263 return ("[%f, %f, %f, %f]", "%s[0], %s[1], %s[2], %s[3]" % (name, name, name, name))
264 return ("%f", name)
Courtney Goeltzenleuchtercd2a0992015-07-09 11:44:38 -0600265 if "bool" in vk_type.lower() or 'xcb_randr_crtc_t' in vk_type:
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600266 return ("%u", name)
Tobin Ehlisb870cbb2015-04-15 07:46:12 -0600267 if True in [t in vk_type.lower() for t in ["int", "flags", "mask", "xcb_window_t"]]:
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600268 if '[' in vk_type: # handle array, current hard-coded to 4 (TODO: Make this dynamic)
Tobin Ehlis99f88672015-01-10 12:42:41 -0700269 if cpp:
270 return ("[%i, %i, %i, %i]", "%s[0] << %s[1] << %s[2] << %s[3]" % (name, name, name, name))
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600271 return ("[%i, %i, %i, %i]", "%s[0], %s[1], %s[2], %s[3]" % (name, name, name, name))
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600272 if '*' in vk_type:
Tobin Ehlisd2b88e82015-02-04 15:15:11 -0700273 if 'pUserData' == name:
274 return ("%i", "((pUserData == 0) ? 0 : *(pUserData))")
Tobin Ehlisc62cb892015-04-17 13:26:33 -0600275 if 'const' in vk_type.lower():
Mark Muelleraab36502016-05-03 13:17:29 -0600276 return ("0x%p", "(void*)(%s)" % name)
Jon Ashburn52f79b52014-12-12 16:10:45 -0700277 return ("%i", "*(%s)" % name)
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600278 return ("%i", name)
Tobin Ehlis3a1cc8d2014-11-11 17:28:22 -0700279 # TODO : This is special-cased as there's only one "format" param currently and it's nice to expand it
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600280 if "VkFormat" == vk_type:
Tobin Ehlis99f88672015-01-10 12:42:41 -0700281 if cpp:
Mark Muelleraab36502016-05-03 13:17:29 -0600282 return ("0x%p", "&%s" % name)
Chia-I Wu1b99bb22015-10-27 19:25:11 +0800283 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 Ehlise7271572014-11-19 15:52:46 -0700284 if output_param:
Mark Muelleraab36502016-05-03 13:17:29 -0600285 return ("0x%p", "(void*)*%s" % name)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600286 if vk_helper.is_type(vk_type, 'struct') and '*' not in vk_type:
Mark Muelleraab36502016-05-03 13:17:29 -0600287 return ("0x%p", "(void*)(&%s)" % name)
288 return ("0x%p", "(void*)(%s)" % name)
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600289
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600290 def _gen_create_msg_callback(self):
Tobin Ehlise8185062014-12-17 08:01:59 -0700291 r_body = []
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600292 r_body.append('%s' % self.lineinfo.get())
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800293 r_body.append('VKAPI_ATTR VkResult VKAPI_CALL CreateDebugReportCallbackEXT(')
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700294 r_body.append(' VkInstance instance,')
295 r_body.append(' const VkDebugReportCallbackCreateInfoEXT* pCreateInfo,')
296 r_body.append(' const VkAllocationCallbacks* pAllocator,')
297 r_body.append(' VkDebugReportCallbackEXT* pCallback)')
Tobin Ehlise8185062014-12-17 08:01:59 -0700298 r_body.append('{')
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600299 # Switch to this code section for the new per-instance storage and debug callbacks
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -0600300 if self.layer_name in ['object_tracker', 'unique_objects']:
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600301 r_body.append(' VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(%s_instance_table_map, instance);' % self.layer_name )
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700302 r_body.append(' VkResult result = pInstanceTable->CreateDebugReportCallbackEXT(instance, pCreateInfo, pAllocator, pCallback);')
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600303 r_body.append(' if (VK_SUCCESS == result) {')
304 r_body.append(' layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);')
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700305 r_body.append(' result = layer_create_msg_callback(my_data->report_data,')
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600306 r_body.append(' false,')
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700307 r_body.append(' pCreateInfo,')
308 r_body.append(' pAllocator,')
309 r_body.append(' pCallback);')
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600310 r_body.append(' }')
311 r_body.append(' return result;')
312 else:
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700313 r_body.append(' VkResult result = instance_dispatch_table(instance)->CreateDebugReportCallbackEXT(instance, pCreateInfo, pAllocator, pCallback);')
Jon Ashburn3a278b72015-10-06 17:05:21 -0600314 r_body.append(' if (VK_SUCCESS == result) {')
315 r_body.append(' layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);')
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600316 r_body.append(' result = layer_create_msg_callback(my_data->report_data, false, pCreateInfo, pAllocator, pCallback);')
Jon Ashburn3a278b72015-10-06 17:05:21 -0600317 r_body.append(' }')
318 r_body.append(' return result;')
Tobin Ehlise8185062014-12-17 08:01:59 -0700319 r_body.append('}')
320 return "\n".join(r_body)
321
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600322 def _gen_destroy_msg_callback(self):
323 r_body = []
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600324 r_body.append('%s' % self.lineinfo.get())
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800325 r_body.append('VKAPI_ATTR void VKAPI_CALL DestroyDebugReportCallbackEXT(VkInstance instance, VkDebugReportCallbackEXT msgCallback, const VkAllocationCallbacks *pAllocator)')
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600326 r_body.append('{')
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600327 # Switch to this code section for the new per-instance storage and debug callbacks
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -0600328 if self.layer_name in ['object_tracker', 'unique_objects']:
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600329 r_body.append(' VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(%s_instance_table_map, instance);' % self.layer_name )
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600330 else:
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700331 r_body.append(' VkLayerInstanceDispatchTable *pInstanceTable = instance_dispatch_table(instance);')
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700332 r_body.append(' pInstanceTable->DestroyDebugReportCallbackEXT(instance, msgCallback, pAllocator);')
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700333 r_body.append(' layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);')
334 r_body.append(' layer_destroy_msg_callback(my_data->report_data, msgCallback, pAllocator);')
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600335 r_body.append('}')
336 return "\n".join(r_body)
Tobin Ehlise8185062014-12-17 08:01:59 -0700337
Courtney Goeltzenleuchterf0de7242015-12-01 14:10:55 -0700338 def _gen_debug_report_msg(self):
339 r_body = []
340 r_body.append('%s' % self.lineinfo.get())
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800341 r_body.append('VKAPI_ATTR void VKAPI_CALL DebugReportMessageEXT(VkInstance instance, VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT objType, uint64_t object, size_t location, int32_t msgCode, const char *pLayerPrefix, const char *pMsg)')
Courtney Goeltzenleuchterf0de7242015-12-01 14:10:55 -0700342 r_body.append('{')
343 # Switch to this code section for the new per-instance storage and debug callbacks
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -0600344 if self.layer_name == 'object_tracker':
Courtney Goeltzenleuchterf0de7242015-12-01 14:10:55 -0700345 r_body.append(' VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(%s_instance_table_map, instance);' % self.layer_name )
346 else:
347 r_body.append(' VkLayerInstanceDispatchTable *pInstanceTable = instance_dispatch_table(instance);')
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700348 r_body.append(' pInstanceTable->DebugReportMessageEXT(instance, flags, objType, object, location, msgCode, pLayerPrefix, pMsg);')
Courtney Goeltzenleuchterf0de7242015-12-01 14:10:55 -0700349 r_body.append('}')
350 return "\n".join(r_body)
351
Chia-I Wucdb70962016-05-13 14:07:36 +0800352 def _gen_layer_logging_workaround(self):
353 body = []
354 body.append('%s' % self.lineinfo.get())
355 body.append('// vk_layer_logging.h expects these to be defined')
356 body.append('')
357 body.append('VKAPI_ATTR VkResult VKAPI_CALL')
358 body.append('vkCreateDebugReportCallbackEXT(VkInstance instance, const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,')
359 body.append(' const VkAllocationCallbacks *pAllocator, VkDebugReportCallbackEXT *pMsgCallback) {')
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800360 body.append(' return %s::CreateDebugReportCallbackEXT(instance, pCreateInfo, pAllocator, pMsgCallback);' % self.layer_name)
Chia-I Wucdb70962016-05-13 14:07:36 +0800361 body.append('}')
362 body.append('')
363 body.append('VKAPI_ATTR void VKAPI_CALL vkDestroyDebugReportCallbackEXT(VkInstance instance,')
364 body.append(' VkDebugReportCallbackEXT msgCallback,')
365 body.append(' const VkAllocationCallbacks *pAllocator) {')
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800366 body.append(' %s::DestroyDebugReportCallbackEXT(instance, msgCallback, pAllocator);' % self.layer_name)
Chia-I Wucdb70962016-05-13 14:07:36 +0800367 body.append('}')
368 body.append('')
369 body.append('VKAPI_ATTR void VKAPI_CALL')
370 body.append('vkDebugReportMessageEXT(VkInstance instance, VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT objType, uint64_t object,')
371 body.append(' size_t location, int32_t msgCode, const char *pLayerPrefix, const char *pMsg) {')
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800372 body.append(' %s::DebugReportMessageEXT(instance, flags, objType, object, location, msgCode, pLayerPrefix, pMsg);' % self.layer_name)
Chia-I Wucdb70962016-05-13 14:07:36 +0800373 body.append('}')
374
375 return "\n".join(body)
376
Chia-I Wu40b8bab2016-05-13 14:06:08 +0800377 def _gen_layer_interface_v0_functions(self):
378 body = []
379 body.append('%s' % self.lineinfo.get())
Chia-I Wub02600c2016-05-20 07:11:22 +0800380 body.append('// loader-layer interface v0, just wrappers since there is only a layer')
Chia-I Wu40b8bab2016-05-13 14:06:08 +0800381 body.append('')
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600382
Chia-I Wu40b8bab2016-05-13 14:06:08 +0800383 body.append('VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount, VkExtensionProperties* pProperties)')
384 body.append('{')
Chia-I Wub02600c2016-05-20 07:11:22 +0800385 body.append(' return %s::EnumerateInstanceExtensionProperties(pLayerName, pCount, pProperties);' % self.layer_name)
Chia-I Wu40b8bab2016-05-13 14:06:08 +0800386 body.append('}')
387 body.append('')
388 body.append('VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties(uint32_t *pCount, VkLayerProperties* pProperties)')
389 body.append('{')
Chia-I Wub02600c2016-05-20 07:11:22 +0800390 body.append(' return %s::EnumerateInstanceLayerProperties(pCount, pProperties);' % self.layer_name)
Chia-I Wu40b8bab2016-05-13 14:06:08 +0800391 body.append('}')
392 body.append('')
393 body.append('VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount, VkLayerProperties* pProperties)')
394 body.append('{')
Chia-I Wub02600c2016-05-20 07:11:22 +0800395 body.append(' // the layer command handles VK_NULL_HANDLE just fine internally')
396 body.append(' assert(physicalDevice == VK_NULL_HANDLE);')
397 body.append(' return %s::EnumerateDeviceLayerProperties(VK_NULL_HANDLE, pCount, pProperties);' % self.layer_name)
Chia-I Wu40b8bab2016-05-13 14:06:08 +0800398 body.append('}')
Chia-I Wucdb70962016-05-13 14:07:36 +0800399 body.append('')
400 body.append('VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice dev, const char *funcName)')
401 body.append('{')
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800402 body.append(' return %s::GetDeviceProcAddr(dev, funcName);' % self.layer_name)
Chia-I Wucdb70962016-05-13 14:07:36 +0800403 body.append('}')
404 body.append('')
405 body.append('VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char *funcName)')
406 body.append('{')
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800407 body.append(' return %s::GetInstanceProcAddr(instance, funcName);' % self.layer_name)
Chia-I Wucdb70962016-05-13 14:07:36 +0800408 body.append('}')
Chia-I Wub02600c2016-05-20 07:11:22 +0800409 body.append('')
410 body.append('VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,')
411 body.append(' const char *pLayerName, uint32_t *pCount,')
412 body.append(' VkExtensionProperties *pProperties)')
413 body.append('{')
414 body.append(' // the layer command handles VK_NULL_HANDLE just fine internally')
415 body.append(' assert(physicalDevice == VK_NULL_HANDLE);')
416 body.append(' return %s::EnumerateDeviceExtensionProperties(VK_NULL_HANDLE, pLayerName, pCount, pProperties);' % self.layer_name)
417 body.append('}')
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600418
Chia-I Wu40b8bab2016-05-13 14:06:08 +0800419 return "\n".join(body)
Courtney Goeltzenleuchter3f9f7c42015-07-06 09:11:12 -0600420
Mike Stroyanbf237d72015-04-03 17:45:53 -0600421 def _generate_dispatch_entrypoints(self, qual=""):
Mike Stroyan938c2532015-04-03 13:58:35 -0600422 if qual:
423 qual += " "
424
Mike Stroyan938c2532015-04-03 13:58:35 -0600425 funcs = []
426 intercepted = []
427 for proto in self.protos:
Chia-I Wud579a5d2016-05-16 07:43:38 +0800428 if proto.name in ["EnumerateInstanceExtensionProperties",
Chia-I Wu40b8bab2016-05-13 14:06:08 +0800429 "EnumerateInstanceLayerProperties",
Chia-I Wub02600c2016-05-20 07:11:22 +0800430 "EnumerateDeviceLayerProperties",
431 "GetDeviceProcAddr",
432 "GetInstanceProcAddr"]:
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800433 funcs.append(proto.c_func(attr="VKAPI") + ';')
Chia-I Wu2985b142016-05-16 12:27:03 +0800434 intercepted.append(proto)
Mike Stroyan70c05e82015-04-08 10:27:43 -0600435 else:
Mike Stroyan3e3a1eb2015-04-03 17:13:23 -0600436 intercept = self.generate_intercept(proto, qual)
Mike Stroyan938c2532015-04-03 13:58:35 -0600437 if intercept is None:
438 # fill in default intercept for certain entrypoints
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700439 if 'CreateDebugReportCallbackEXT' == proto.name:
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600440 intercept = self._gen_layer_dbg_create_msg_callback()
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700441 elif 'DestroyDebugReportCallbackEXT' == proto.name:
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600442 intercept = self._gen_layer_dbg_destroy_msg_callback()
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700443 elif 'DebugReportMessageEXT' == proto.name:
Courtney Goeltzenleuchterf0de7242015-12-01 14:10:55 -0700444 intercept = self._gen_debug_report_msg()
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600445 elif 'CreateDevice' == proto.name:
446 funcs.append('/* CreateDevice HERE */')
Tony Barbour59a47322015-06-24 16:06:58 -0600447
Mike Stroyan938c2532015-04-03 13:58:35 -0600448 if intercept is not None:
449 funcs.append(intercept)
Ian Elliott7e40db92015-08-21 15:09:33 -0600450 if not "KHR" in proto.name:
Jon Ashburn747f2b62015-06-18 15:02:58 -0600451 intercepted.append(proto)
Mike Stroyan938c2532015-04-03 13:58:35 -0600452
Chia-I Wu2985b142016-05-16 12:27:03 +0800453 instance_lookups = []
454 device_lookups = []
Mike Stroyan938c2532015-04-03 13:58:35 -0600455 for proto in intercepted:
Chia-I Wu2985b142016-05-16 12:27:03 +0800456 if proto_is_global(proto):
457 instance_lookups.append("if (!strcmp(name, \"%s\"))" % proto.name)
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800458 instance_lookups.append(" return (PFN_vkVoidFunction) %s;" % (proto.name))
Chia-I Wu2985b142016-05-16 12:27:03 +0800459 else:
460 device_lookups.append("if (!strcmp(name, \"%s\"))" % proto.name)
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800461 device_lookups.append(" return (PFN_vkVoidFunction) %s;" % (proto.name))
Mike Stroyan938c2532015-04-03 13:58:35 -0600462
Chia-I Wu2985b142016-05-16 12:27:03 +0800463 # add customized intercept_core_device_command
Mike Stroyan938c2532015-04-03 13:58:35 -0600464 body = []
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600465 body.append('%s' % self.lineinfo.get())
Chia-I Wu2985b142016-05-16 12:27:03 +0800466 body.append("static inline PFN_vkVoidFunction intercept_core_device_command(const char *name)")
Mike Stroyan938c2532015-04-03 13:58:35 -0600467 body.append("{")
468 body.append(generate_get_proc_addr_check("name"))
469 body.append("")
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600470 body.append(" name += 2;")
Chia-I Wu2985b142016-05-16 12:27:03 +0800471 body.append(" %s" % "\n ".join(device_lookups))
Mike Stroyan938c2532015-04-03 13:58:35 -0600472 body.append("")
473 body.append(" return NULL;")
474 body.append("}")
Chia-I Wu2985b142016-05-16 12:27:03 +0800475 # add intercept_core_instance_command
476 body.append("static inline PFN_vkVoidFunction intercept_core_instance_command(const char *name)")
Jon Ashburnf6b33db2015-05-05 14:22:52 -0600477 body.append("{")
478 body.append(generate_get_proc_addr_check("name"))
479 body.append("")
480 body.append(" name += 2;")
Chia-I Wu2985b142016-05-16 12:27:03 +0800481 body.append(" %s" % "\n ".join(instance_lookups))
Jon Ashburnf6b33db2015-05-05 14:22:52 -0600482 body.append("")
483 body.append(" return NULL;")
484 body.append("}")
485
Mike Stroyan938c2532015-04-03 13:58:35 -0600486 funcs.append("\n".join(body))
Mike Stroyan938c2532015-04-03 13:58:35 -0600487 return "\n\n".join(funcs)
488
Tobin Ehlisca915872014-11-18 11:28:33 -0700489 def _generate_extensions(self):
490 exts = []
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600491 exts.append('%s' % self.lineinfo.get())
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600492 exts.append(self._gen_create_msg_callback())
493 exts.append(self._gen_destroy_msg_callback())
Courtney Goeltzenleuchterf0de7242015-12-01 14:10:55 -0700494 exts.append(self._gen_debug_report_msg())
Tobin Ehlisb870cbb2015-04-15 07:46:12 -0600495 return "\n".join(exts)
496
Chia-I Wub02600c2016-05-20 07:11:22 +0800497 def _generate_layer_introspection_function(self):
498 body = []
499 body.append('%s' % self.lineinfo.get())
500 if self.layer_name == 'object_tracker':
501 body.append('static const VkExtensionProperties instance_extensions[] = {')
502 body.append(' {')
503 body.append(' VK_EXT_DEBUG_REPORT_EXTENSION_NAME,')
504 body.append(' VK_EXT_DEBUG_REPORT_SPEC_VERSION')
505 body.append(' }')
506 body.append('};')
507 body.append('')
508
509 body.append('static const VkLayerProperties globalLayerProps = {')
510 body.append(' "VK_LAYER_LUNARG_%s",' % self.layer_name)
511 body.append(' VK_LAYER_API_VERSION, // specVersion')
512 body.append(' 1, // implementationVersion')
513 body.append(' "LunarG Validation Layer"')
514 body.append('};')
515 body.append('')
516 else:
517 body.append('static const VkLayerProperties globalLayerProps = {')
518 body.append(' "VK_LAYER_GOOGLE_%s",' % self.layer_name)
519 body.append(' VK_LAYER_API_VERSION, // specVersion')
520 body.append(' 1, // implementationVersion')
521 body.append(' "Google Validation Layer"')
522 body.append('};')
523 body.append('')
524
525 body.append('VKAPI_ATTR VkResult VKAPI_CALL EnumerateInstanceLayerProperties(uint32_t *pCount, VkLayerProperties* pProperties)')
526 body.append('{')
527 body.append(' return util_GetLayerProperties(1, &globalLayerProps, pCount, pProperties);')
528 body.append('}')
529 body.append('')
530 body.append('VKAPI_ATTR VkResult VKAPI_CALL EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount, VkLayerProperties* pProperties)')
531 body.append('{')
532 body.append(' return util_GetLayerProperties(1, &globalLayerProps, pCount, pProperties);')
533 body.append('}')
534 body.append('')
535 body.append('VKAPI_ATTR VkResult VKAPI_CALL EnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount, VkExtensionProperties* pProperties)')
536 body.append('{')
537 body.append(' if (pLayerName && !strcmp(pLayerName, globalLayerProps.layerName))')
538 if self.layer_name == 'object_tracker':
539 body.append(' return util_GetExtensionProperties(1, instance_extensions, pCount, pProperties);')
540 else:
541 body.append(' return util_GetExtensionProperties(0, NULL, pCount, pProperties);')
542 body.append('')
543 body.append(' return VK_ERROR_LAYER_NOT_PRESENT;')
544 body.append('}')
545 body.append('')
546 body.append('VKAPI_ATTR VkResult VKAPI_CALL EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,')
547 body.append(' const char *pLayerName, uint32_t *pCount,')
548 body.append(' VkExtensionProperties *pProperties)')
549 body.append('{')
550 body.append(' if (pLayerName && !strcmp(pLayerName, globalLayerProps.layerName))')
551 body.append(' return util_GetExtensionProperties(0, nullptr, pCount, pProperties);')
552 body.append('')
553 body.append(' assert(physicalDevice);')
554 body.append(' VkLayerInstanceDispatchTable* pTable = get_dispatch_table(%s_instance_table_map, physicalDevice);' % self.layer_name)
555 body.append(' return pTable->EnumerateDeviceExtensionProperties(physicalDevice, NULL, pCount, pProperties);')
556 body.append('}')
557
558 return "\n".join(body)
559
Jon Ashburnf6b33db2015-05-05 14:22:52 -0600560 def _generate_layer_gpa_function(self, extensions=[], instance_extensions=[]):
Jon Ashburnbacb0f52015-04-06 10:58:22 -0600561 func_body = []
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600562#
Courtney Goeltzenleuchter3f9f7c42015-07-06 09:11:12 -0600563# New style of GPA Functions for the new layer_data/layer_logging changes
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600564#
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -0600565 if self.layer_name in ['object_tracker', 'unique_objects']:
Chia-I Wufcf7eb92016-05-16 13:01:39 +0800566 for ext_enable, ext_list in extensions:
567 func_body.append('%s' % self.lineinfo.get())
568 func_body.append('static inline PFN_vkVoidFunction intercept_%s_command(const char *name, VkDevice dev)' % ext_enable)
569 func_body.append('{')
Chia-I Wu5b2f0572016-05-17 07:43:59 +0800570 func_body.append(' if (dev) {')
571 func_body.append(' layer_data *my_data = get_my_data_ptr(get_dispatch_key(dev), layer_data_map);')
572 func_body.append(' if (!my_data->%s)' % ext_enable)
573 func_body.append(' return nullptr;')
574 func_body.append(' }\n')
Chia-I Wufcf7eb92016-05-16 13:01:39 +0800575
576 for ext_name in ext_list:
577 func_body.append(' if (!strcmp("%s", name))\n'
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800578 ' return reinterpret_cast<PFN_vkVoidFunction>(%s);' % (ext_name, ext_name[2:]))
Chia-I Wufcf7eb92016-05-16 13:01:39 +0800579 func_body.append('\n return nullptr;')
580 func_body.append('}\n')
581
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800582 func_body.append("VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetDeviceProcAddr(VkDevice device, const char* funcName)\n"
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600583 "{\n"
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -0600584 " PFN_vkVoidFunction addr;\n"
Chia-I Wu2985b142016-05-16 12:27:03 +0800585 " addr = intercept_core_device_command(funcName);\n"
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600586 " if (addr)\n"
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700587 " return addr;\n"
Chia-I Wuf5b039d2016-05-17 07:39:31 +0800588 " assert(device);\n")
Chia-I Wufcf7eb92016-05-16 13:01:39 +0800589 for ext_enable, _ in extensions:
590 func_body.append(' addr = intercept_%s_command(funcName, device);' % ext_enable)
591 func_body.append(' if (addr)\n'
592 ' return addr;')
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600593 func_body.append("\n if (get_dispatch_table(%s_device_table_map, device)->GetDeviceProcAddr == NULL)\n"
594 " return NULL;\n"
595 " return get_dispatch_table(%s_device_table_map, device)->GetDeviceProcAddr(device, funcName);\n"
596 "}\n" % (self.layer_name, self.layer_name))
Chia-I Wufcf7eb92016-05-16 13:01:39 +0800597
Mark Lobodzinskife1f0662016-06-24 09:57:32 -0600598 # The WSI-related extensions have independent extension enables
599 wsi_sub_enables = {'WIN32': 'win32_enabled',
600 'XLIB': 'xlib_enabled',
601 'XCB': 'xcb_enabled',
602 'MIR': 'mir_enabled',
603 'WAYLAND': 'wayland_enabled',
604 'ANDROID': 'android_enabled'}
605
Chia-I Wufcf7eb92016-05-16 13:01:39 +0800606 for ext_enable, ext_list in instance_extensions:
607 func_body.append('%s' % self.lineinfo.get())
608 func_body.append('static inline PFN_vkVoidFunction intercept_%s_command(const char *name, VkInstance instance)' % ext_enable)
609 func_body.append('{')
610 if ext_enable == 'msg_callback_get_proc_addr':
611 func_body.append(" layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);\n"
612 " return debug_report_get_instance_proc_addr(my_data->report_data, name);")
613 else:
614 func_body.append(" VkLayerInstanceDispatchTable* pTable = get_dispatch_table(%s_instance_table_map, instance);" % self.layer_name)
615 func_body.append(' if (instanceExtMap.size() == 0 || !instanceExtMap[pTable].%s)' % ext_enable)
616 func_body.append(' return nullptr;\n')
617
618 for ext_name in ext_list:
619 if wsi_name(ext_name):
620 func_body.append('%s' % wsi_ifdef(ext_name))
Mark Lobodzinskife1f0662016-06-24 09:57:32 -0600621 if wsi_sub_enables[wsi_name(ext_name)]:
622 func_body.append(' if ((instanceExtMap[pTable].%s == true) && !strcmp("%s", name))\n'
623 ' return reinterpret_cast<PFN_vkVoidFunction>(%s);' % (wsi_sub_enables[wsi_name(ext_name)], ext_name, ext_name[2:]))
624 else:
625 func_body.append(' if (!strcmp("%s", name))\n'
626 ' return reinterpret_cast<PFN_vkVoidFunction>(%s);' % (ext_name, ext_name[2:]))
Chia-I Wufcf7eb92016-05-16 13:01:39 +0800627 if wsi_name(ext_name):
628 func_body.append('%s' % wsi_endif(ext_name))
629
630 func_body.append('\n return nullptr;')
631 func_body.append('}\n')
632
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800633 func_body.append("VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetInstanceProcAddr(VkInstance instance, const char* funcName)\n"
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600634 "{\n"
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -0600635 " PFN_vkVoidFunction addr;\n"
Chia-I Wu2985b142016-05-16 12:27:03 +0800636 " addr = intercept_core_instance_command(funcName);\n"
Chia-I Wu5b2f0572016-05-17 07:43:59 +0800637 " if (!addr) {\n"
638 " addr = intercept_core_device_command(funcName);\n"
639 " }")
640
641 for ext_enable, _ in extensions:
642 func_body.append(" if (!addr) {\n"
643 " addr = intercept_%s_command(funcName, VkDevice(VK_NULL_HANDLE));\n"
644 " }" % ext_enable)
645
646 func_body.append(" if (addr) {\n"
Chia-I Wufcf7eb92016-05-16 13:01:39 +0800647 " return addr;\n"
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700648 " }\n"
Chia-I Wuf5b039d2016-05-17 07:39:31 +0800649 " assert(instance);\n"
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700650 )
Jon Ashburnbacb0f52015-04-06 10:58:22 -0600651
Chia-I Wufcf7eb92016-05-16 13:01:39 +0800652 for ext_enable, _ in instance_extensions:
653 func_body.append(' addr = intercept_%s_command(funcName, instance);' % ext_enable)
654 func_body.append(' if (addr)\n'
655 ' return addr;\n')
Jon Ashburn3dc39382015-09-17 10:00:32 -0600656
657 func_body.append(" if (get_dispatch_table(%s_instance_table_map, instance)->GetInstanceProcAddr == NULL) {\n"
658 " return NULL;\n"
659 " }\n"
660 " return get_dispatch_table(%s_instance_table_map, instance)->GetInstanceProcAddr(instance, funcName);\n"
661 "}\n" % (self.layer_name, self.layer_name))
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600662 return "\n".join(func_body)
663 else:
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600664 func_body.append('%s' % self.lineinfo.get())
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800665 func_body.append("VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetDeviceProcAddr(VkDevice device, const char* funcName)\n"
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600666 "{\n"
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700667 " PFN_vkVoidFunction addr;\n")
Jon Ashburn1f32a442016-02-02 13:13:01 -0700668 func_body.append("\n"
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600669 " loader_platform_thread_once(&initOnce, init%s);\n\n"
Chia-I Wu2985b142016-05-16 12:27:03 +0800670 " addr = intercept_core_device_command(funcName);\n"
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600671 " if (addr)\n"
672 " return addr;" % self.layer_name)
Chia-I Wuf5b039d2016-05-17 07:39:31 +0800673 func_body.append(" assert(device);\n")
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600674 func_body.append('')
675 func_body.append(' VkLayerDispatchTable *pDisp = device_dispatch_table(device);')
676 if 0 != len(extensions):
677 extra_space = ""
678 for (ext_enable, ext_list) in extensions:
679 if 0 != len(ext_enable):
Jon Ashburn8acd2332015-09-16 18:08:32 -0600680 func_body.append(' if (deviceExtMap.size() != 0 && deviceExtMap[pDisp].%s)' % ext_enable)
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600681 func_body.append(' {')
682 extra_space = " "
683 for ext_name in ext_list:
684 func_body.append(' %sif (!strcmp("%s", funcName))\n'
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -0600685 ' return reinterpret_cast<PFN_vkVoidFunction>(%s);' % (extra_space, ext_name, ext_name))
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600686 if 0 != len(ext_enable):
687 func_body.append(' }')
688 func_body.append('%s' % self.lineinfo.get())
689 func_body.append(" {\n"
690 " if (pDisp->GetDeviceProcAddr == NULL)\n"
691 " return NULL;\n"
692 " return pDisp->GetDeviceProcAddr(device, funcName);\n"
693 " }\n"
694 "}\n")
Jon Ashburn3dc39382015-09-17 10:00:32 -0600695 func_body.append('%s' % self.lineinfo.get())
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800696 func_body.append("VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetInstanceProcAddr(VkInstance instance, const char* funcName)\n"
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600697 "{\n"
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -0600698 " PFN_vkVoidFunction addr;\n"
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700699 )
Jon Ashburn1f32a442016-02-02 13:13:01 -0700700 func_body.append(
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600701 " loader_platform_thread_once(&initOnce, init%s);\n\n"
Chia-I Wu2985b142016-05-16 12:27:03 +0800702 " addr = intercept_core_instance_command(funcName);\n"
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600703 " if (addr)\n"
704 " return addr;" % self.layer_name)
Chia-I Wuf5b039d2016-05-17 07:39:31 +0800705 func_body.append(" assert(instance);\n")
Jon Ashburn3dc39382015-09-17 10:00:32 -0600706 func_body.append("")
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700707 func_body.append(" VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance);\n")
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600708 if 0 != len(instance_extensions):
Jon Ashburn3dc39382015-09-17 10:00:32 -0600709 extra_space = ""
710 for (ext_enable, ext_list) in instance_extensions:
711 if 0 != len(ext_enable):
Jon Ashburn3a278b72015-10-06 17:05:21 -0600712 if ext_enable == 'msg_callback_get_proc_addr':
713 func_body.append(" layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);\n"
714 " addr = debug_report_get_instance_proc_addr(my_data->report_data, funcName);\n"
715 " if (addr) {\n"
716 " return addr;\n"
717 " }\n")
718 else:
719 func_body.append(' if (instanceExtMap.size() != 0 && instanceExtMap[pTable].%s)' % ext_enable)
720 func_body.append(' {')
721 extra_space = " "
722 for ext_name in ext_list:
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -0700723 if wsi_name(ext_name):
724 func_body.append('%s' % wsi_ifdef(ext_name))
Jon Ashburn3a278b72015-10-06 17:05:21 -0600725 func_body.append(' %sif (!strcmp("%s", funcName))\n'
Jon Ashburn3dc39382015-09-17 10:00:32 -0600726 ' return reinterpret_cast<PFN_vkVoidFunction>(%s);' % (extra_space, ext_name, ext_name))
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -0700727 if wsi_name(ext_name):
728 func_body.append('%s' % wsi_endif(ext_name))
Jon Ashburn3a278b72015-10-06 17:05:21 -0600729 if 0 != len(ext_enable):
730 func_body.append(' }\n')
Jon Ashburn3dc39382015-09-17 10:00:32 -0600731
732 func_body.append(" if (pTable->GetInstanceProcAddr == NULL)\n"
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600733 " return NULL;\n"
734 " return pTable->GetInstanceProcAddr(instance, funcName);\n"
735 "}\n")
736 return "\n".join(func_body)
Jon Ashburnf6b33db2015-05-05 14:22:52 -0600737
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600738
Mike Stroyaned238bb2015-05-15 08:50:57 -0600739 def _generate_layer_initialization(self, init_opts=False, prefix='vk', lockname=None, condname=None):
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600740 func_body = ["#include \"vk_dispatch_table_helper.h\""]
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600741 func_body.append('%s' % self.lineinfo.get())
Mark Lobodzinski0d054fe2015-12-30 08:16:12 -0700742 func_body.append('static void init_%s(layer_data *my_data, const VkAllocationCallbacks *pAllocator)\n'
Mike Stroyan3e3a1eb2015-04-03 17:13:23 -0600743 '{\n' % self.layer_name)
Jon Ashburnd6badbc2015-02-16 08:26:50 -0700744 if init_opts:
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600745 func_body.append('%s' % self.lineinfo.get())
Jon Ashburnd6badbc2015-02-16 08:26:50 -0700746 func_body.append('')
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -0600747 func_body.append(' layer_debug_actions(my_data->report_data, my_data->logging_callback, pAllocator, "lunarg_%s");' % self.layer_name)
Mike Stroyan313f7e62015-08-10 16:42:53 -0600748 func_body.append('')
749 if lockname is not None:
750 func_body.append('%s' % self.lineinfo.get())
751 func_body.append(" if (!%sLockInitialized)" % lockname)
752 func_body.append(" {")
753 func_body.append(" // TODO/TBD: Need to delete this mutex sometime. How???")
754 func_body.append(" loader_platform_thread_create_mutex(&%sLock);" % lockname)
755 if condname is not None:
756 func_body.append(" loader_platform_thread_init_cond(&%sCond);" % condname)
757 func_body.append(" %sLockInitialized = 1;" % lockname)
758 func_body.append(" }")
759 func_body.append("}\n")
760 func_body.append('')
761 return "\n".join(func_body)
762
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600763class ObjectTrackerSubcommand(Subcommand):
764 def generate_header(self):
765 header_txt = []
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600766 header_txt.append('%s' % self.lineinfo.get())
Jamie Madilldf5d5732016-04-04 11:54:43 -0400767 header_txt.append('#include "vk_loader_platform.h"')
768 header_txt.append('#include "vulkan/vulkan.h"')
769 header_txt.append('')
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600770 header_txt.append('#include <stdio.h>')
771 header_txt.append('#include <stdlib.h>')
772 header_txt.append('#include <string.h>')
Karl Schultzd7f37542016-05-10 11:36:08 -0600773 header_txt.append('#include <cinttypes>')
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600774 header_txt.append('')
Tobin Ehlis803cc492015-06-08 17:36:28 -0600775 header_txt.append('#include <unordered_map>')
Mark Lobodzinskif93272b2016-05-02 12:08:24 -0600776 header_txt.append('')
David Pinedo9316d3b2015-11-06 12:54:48 -0700777 header_txt.append('#include "vulkan/vk_layer.h"')
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -0600778 header_txt.append('#include "vk_layer_config.h"')
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -0600779 header_txt.append('#include "vk_layer_table.h"')
780 header_txt.append('#include "vk_layer_data.h"')
781 header_txt.append('#include "vk_layer_logging.h"')
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600782 header_txt.append('')
Mark Lobodzinski0d054fe2015-12-30 08:16:12 -0700783# NOTE: The non-autoGenerated code is in the object_tracker.h header file
784 header_txt.append('#include "object_tracker.h"')
Mark Lobodzinskifb5437a2015-05-22 14:15:36 -0500785 header_txt.append('')
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600786 return "\n".join(header_txt)
787
Tony Barboura05dbaa2015-07-09 17:31:46 -0600788 def generate_maps(self):
789 maps_txt = []
Tobin Ehlis86684f92016-01-05 10:33:58 -0700790 for o in vulkan.object_type_list:
Mark Lobodzinskif93272b2016-05-02 12:08:24 -0600791 maps_txt.append('std::unordered_map<uint64_t, OBJTRACK_NODE*> %sMap;' % (o))
Tony Barboura05dbaa2015-07-09 17:31:46 -0600792 return "\n".join(maps_txt)
793
Tobin Ehlis86684f92016-01-05 10:33:58 -0700794 def _gather_object_uses(self, obj_list, struct_type, obj_set):
795 # for each member of struct_type
796 # add objs in obj_list to obj_set
797 # call self for structs
Mike Stroyan04be7832016-04-07 12:14:30 -0600798 for m in sorted(vk_helper.struct_dict[struct_type]):
Tobin Ehlis86684f92016-01-05 10:33:58 -0700799 if vk_helper.struct_dict[struct_type][m]['type'] in obj_list:
800 obj_set.add(vk_helper.struct_dict[struct_type][m]['type'])
801 elif vk_helper.is_type(vk_helper.struct_dict[struct_type][m]['type'], 'struct'):
802 obj_set = obj_set.union(self._gather_object_uses(obj_list, vk_helper.struct_dict[struct_type][m]['type'], obj_set))
803 return obj_set
804
Tony Barboura05dbaa2015-07-09 17:31:46 -0600805 def generate_procs(self):
806 procs_txt = []
Tobin Ehlis86684f92016-01-05 10:33:58 -0700807 # First parse through funcs and gather dict of all objects seen by each call
808 obj_use_dict = {}
809 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
810 for proto in proto_list:
811 disp_obj = proto.params[0].ty.strip('*').replace('const ', '')
812 if disp_obj in vulkan.object_dispatch_list:
813 if disp_obj not in obj_use_dict:
814 obj_use_dict[disp_obj] = set()
815 for p in proto.params[1:]:
816 base_type = p.ty.strip('*').replace('const ', '')
817 if base_type in vulkan.object_type_list:
818 obj_use_dict[disp_obj].add(base_type)
819 if vk_helper.is_type(base_type, 'struct'):
820 obj_use_dict[disp_obj] = self._gather_object_uses(vulkan.object_type_list, base_type, obj_use_dict[disp_obj])
821 #for do in obj_use_dict:
822 # print "Disp obj %s has uses for objs: %s" % (do, ', '.join(obj_use_dict[do]))
823
824 for o in vulkan.object_type_list:# vulkan.core.objects:
Tony Barboura05dbaa2015-07-09 17:31:46 -0600825 procs_txt.append('%s' % self.lineinfo.get())
Michael Lentine13803dc2015-11-04 14:35:12 -0800826 name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', o)
827 name = re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower()[3:]
Tobin Ehlis154e0462015-08-26 11:22:09 -0600828 if o in vulkan.object_dispatch_list:
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700829 procs_txt.append('static void create_%s(%s dispatchable_object, %s vkObj, VkDebugReportObjectTypeEXT objType)' % (name, o, o))
Tony Barboura05dbaa2015-07-09 17:31:46 -0600830 else:
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700831 procs_txt.append('static void create_%s(VkDevice dispatchable_object, %s vkObj, VkDebugReportObjectTypeEXT objType)' % (name, o))
Chia-I Wue2fc5522015-10-26 20:04:44 +0800832 procs_txt.append('{')
Mark Lobodzinski510e20d2016-02-11 09:26:16 -0700833 procs_txt.append(' log_msg(mdd(dispatchable_object), VK_DEBUG_REPORT_INFORMATION_BIT_EXT, objType,(uint64_t)(vkObj), __LINE__, OBJTRACK_NONE, "OBJTRACK",')
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700834 procs_txt.append(' "OBJ[%llu] : CREATE %s object 0x%" PRIxLEAST64 , object_track_index++, string_VkDebugReportObjectTypeEXT(objType),')
Mark Young93ecb1d2016-01-13 13:47:16 -0700835 procs_txt.append(' (uint64_t)(vkObj));')
Tony Barboura05dbaa2015-07-09 17:31:46 -0600836 procs_txt.append('')
837 procs_txt.append(' OBJTRACK_NODE* pNewObjNode = new OBJTRACK_NODE;')
Mark Lobodzinskic857fb32016-03-08 15:10:00 -0700838 procs_txt.append(' pNewObjNode->belongsTo = (uint64_t)dispatchable_object;')
Tony Barboura05dbaa2015-07-09 17:31:46 -0600839 procs_txt.append(' pNewObjNode->objType = objType;')
840 procs_txt.append(' pNewObjNode->status = OBJSTATUS_NONE;')
Mark Young93ecb1d2016-01-13 13:47:16 -0700841 procs_txt.append(' pNewObjNode->vkObj = (uint64_t)(vkObj);')
Michael Lentine13803dc2015-11-04 14:35:12 -0800842 procs_txt.append(' %sMap[(uint64_t)vkObj] = pNewObjNode;' % (o))
Tony Barboura05dbaa2015-07-09 17:31:46 -0600843 procs_txt.append(' uint32_t objIndex = objTypeToIndex(objType);')
844 procs_txt.append(' numObjs[objIndex]++;')
845 procs_txt.append(' numTotalObjs++;')
846 procs_txt.append('}')
847 procs_txt.append('')
848 procs_txt.append('%s' % self.lineinfo.get())
Tobin Ehlis154e0462015-08-26 11:22:09 -0600849 if o in vulkan.object_dispatch_list:
Michael Lentine13803dc2015-11-04 14:35:12 -0800850 procs_txt.append('static void destroy_%s(%s dispatchable_object, %s object)' % (name, o, o))
Tony Barboura05dbaa2015-07-09 17:31:46 -0600851 else:
Michael Lentine13803dc2015-11-04 14:35:12 -0800852 procs_txt.append('static void destroy_%s(VkDevice dispatchable_object, %s object)' % (name, o))
Tony Barboura05dbaa2015-07-09 17:31:46 -0600853 procs_txt.append('{')
Mark Young93ecb1d2016-01-13 13:47:16 -0700854 procs_txt.append(' uint64_t object_handle = (uint64_t)(object);')
Chris Forbesbdbc1132016-03-09 12:06:45 +1300855 procs_txt.append(' auto it = %sMap.find(object_handle);' % o)
856 procs_txt.append(' if (it != %sMap.end()) {' % o)
857 procs_txt.append(' OBJTRACK_NODE* pNode = it->second;')
Tony Barboura05dbaa2015-07-09 17:31:46 -0600858 procs_txt.append(' uint32_t objIndex = objTypeToIndex(pNode->objType);')
859 procs_txt.append(' assert(numTotalObjs > 0);')
860 procs_txt.append(' numTotalObjs--;')
861 procs_txt.append(' assert(numObjs[objIndex] > 0);')
862 procs_txt.append(' numObjs[objIndex]--;')
Mark Lobodzinski510e20d2016-02-11 09:26:16 -0700863 procs_txt.append(' log_msg(mdd(dispatchable_object), VK_DEBUG_REPORT_INFORMATION_BIT_EXT, pNode->objType, object_handle, __LINE__, OBJTRACK_NONE, "OBJTRACK",')
Michael Lentine010f4692015-11-03 16:19:46 -0800864 procs_txt.append(' "OBJ_STAT Destroy %s obj 0x%" PRIxLEAST64 " (%" PRIu64 " total objs remain & %" PRIu64 " %s objs).",')
Mark Young93ecb1d2016-01-13 13:47:16 -0700865 procs_txt.append(' string_VkDebugReportObjectTypeEXT(pNode->objType), (uint64_t)(object), numTotalObjs, numObjs[objIndex],')
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700866 procs_txt.append(' string_VkDebugReportObjectTypeEXT(pNode->objType));')
Tony Barboura05dbaa2015-07-09 17:31:46 -0600867 procs_txt.append(' delete pNode;')
Chris Forbesbdbc1132016-03-09 12:06:45 +1300868 procs_txt.append(' %sMap.erase(it);' % (o))
Chia-I Wue2fc5522015-10-26 20:04:44 +0800869 procs_txt.append(' } else {')
Mark Lobodzinskia1bf5db2016-05-02 13:19:15 -0600870 procs_txt.append(' log_msg(mdd(dispatchable_object), VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT ) 0,')
871 procs_txt.append(' object_handle, __LINE__, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK",')
Chia-I Wue2fc5522015-10-26 20:04:44 +0800872 procs_txt.append(' "Unable to remove obj 0x%" PRIxLEAST64 ". Was it created? Has it already been destroyed?",')
Mark Lobodzinskia1bf5db2016-05-02 13:19:15 -0600873 procs_txt.append(' object_handle);')
Chia-I Wue2fc5522015-10-26 20:04:44 +0800874 procs_txt.append(' }')
Tony Barboura05dbaa2015-07-09 17:31:46 -0600875 procs_txt.append('}')
876 procs_txt.append('')
Tobin Ehlis86684f92016-01-05 10:33:58 -0700877 # Generate the permutations of validate_* functions where for each
878 # dispatchable object type, we have a corresponding validate_* function
879 # for that object and all non-dispatchable objects that are used in API
880 # calls with that dispatchable object.
Mike Stroyan04be7832016-04-07 12:14:30 -0600881 procs_txt.append('//%s' % str(sorted(obj_use_dict)))
882 for do in sorted(obj_use_dict):
Tobin Ehlis86684f92016-01-05 10:33:58 -0700883 name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', do)
884 name = re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower()[3:]
885 # First create validate_* func for disp obj
886 procs_txt.append('%s' % self.lineinfo.get())
Mark Lobodzinski2abefa92016-05-05 11:45:57 -0600887 procs_txt.append('static bool validate_%s(%s dispatchable_object, %s object, VkDebugReportObjectTypeEXT objType, bool null_allowed)' % (name, do, do))
Tobin Ehlis86684f92016-01-05 10:33:58 -0700888 procs_txt.append('{')
889 procs_txt.append(' if (null_allowed && (object == VK_NULL_HANDLE))')
Mark Lobodzinski2abefa92016-05-05 11:45:57 -0600890 procs_txt.append(' return false;')
Tobin Ehlis86684f92016-01-05 10:33:58 -0700891 procs_txt.append(' if (%sMap.find((uint64_t)object) == %sMap.end()) {' % (do, do))
Mark Young93ecb1d2016-01-13 13:47:16 -0700892 procs_txt.append(' return log_msg(mdd(dispatchable_object), VK_DEBUG_REPORT_ERROR_BIT_EXT, objType, (uint64_t)(object), __LINE__, OBJTRACK_INVALID_OBJECT, "OBJTRACK",')
893 procs_txt.append(' "Invalid %s Object 0x%%" PRIx64 ,(uint64_t)(object));' % do)
Tobin Ehlis86684f92016-01-05 10:33:58 -0700894 procs_txt.append(' }')
Mark Lobodzinski2abefa92016-05-05 11:45:57 -0600895 procs_txt.append(' return false;')
Tobin Ehlis86684f92016-01-05 10:33:58 -0700896 procs_txt.append('}')
897 procs_txt.append('')
Mike Stroyan04be7832016-04-07 12:14:30 -0600898 for o in sorted(obj_use_dict[do]):
Tobin Ehlis86684f92016-01-05 10:33:58 -0700899 if o == do: # We already generated this case above so skip here
900 continue
901 name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', o)
902 name = re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower()[3:]
903 procs_txt.append('%s' % self.lineinfo.get())
Mark Lobodzinski2abefa92016-05-05 11:45:57 -0600904 procs_txt.append('static bool validate_%s(%s dispatchable_object, %s object, VkDebugReportObjectTypeEXT objType, bool null_allowed)' % (name, do, o))
Tobin Ehlis86684f92016-01-05 10:33:58 -0700905 procs_txt.append('{')
906 procs_txt.append(' if (null_allowed && (object == VK_NULL_HANDLE))')
Mark Lobodzinski2abefa92016-05-05 11:45:57 -0600907 procs_txt.append(' return false;')
Tobin Ehlis86684f92016-01-05 10:33:58 -0700908 if o == "VkImage":
909 procs_txt.append(' // We need to validate normal image objects and those from the swapchain')
910 procs_txt.append(' if ((%sMap.find((uint64_t)object) == %sMap.end()) &&' % (o, o))
911 procs_txt.append(' (swapchainImageMap.find((uint64_t)object) == swapchainImageMap.end())) {')
912 else:
913 procs_txt.append(' if (%sMap.find((uint64_t)object) == %sMap.end()) {' % (o, o))
Mark Young93ecb1d2016-01-13 13:47:16 -0700914 procs_txt.append(' return log_msg(mdd(dispatchable_object), VK_DEBUG_REPORT_ERROR_BIT_EXT, objType, (uint64_t)(object), __LINE__, OBJTRACK_INVALID_OBJECT, "OBJTRACK",')
915 procs_txt.append(' "Invalid %s Object 0x%%" PRIx64, (uint64_t)(object));' % o)
Tobin Ehlis86684f92016-01-05 10:33:58 -0700916 procs_txt.append(' }')
Mark Lobodzinski2abefa92016-05-05 11:45:57 -0600917 procs_txt.append(' return false;')
Tobin Ehlis86684f92016-01-05 10:33:58 -0700918 procs_txt.append('}')
919 procs_txt.append('')
920 procs_txt.append('')
Tony Barboura05dbaa2015-07-09 17:31:46 -0600921 return "\n".join(procs_txt)
922
Mark Lobodzinski64d57752015-07-17 11:51:24 -0600923 def generate_destroy_instance(self):
Tony Barboura05dbaa2015-07-09 17:31:46 -0600924 gedi_txt = []
925 gedi_txt.append('%s' % self.lineinfo.get())
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800926 gedi_txt.append('VKAPI_ATTR void VKAPI_CALL DestroyInstance(')
Chia-I Wuf7458c52015-10-26 21:10:41 +0800927 gedi_txt.append('VkInstance instance,')
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800928 gedi_txt.append('const VkAllocationCallbacks* pAllocator)')
Tony Barboura05dbaa2015-07-09 17:31:46 -0600929 gedi_txt.append('{')
Jeremy Hayes2f065b12016-04-13 10:54:17 -0600930 gedi_txt.append(' std::unique_lock<std::mutex> lock(global_lock);')
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600931 gedi_txt.append('')
932 gedi_txt.append(' dispatch_key key = get_dispatch_key(instance);')
933 gedi_txt.append(' layer_data *my_data = get_my_data_ptr(key, layer_data_map);')
934 gedi_txt.append('')
935 gedi_txt.append(' // Enable the temporary callback(s) here to catch cleanup issues:')
936 gedi_txt.append(' bool callback_setup = false;')
937 gedi_txt.append(' if (my_data->num_tmp_callbacks > 0) {')
938 gedi_txt.append(' if (!layer_enable_tmp_callbacks(my_data->report_data,')
939 gedi_txt.append(' my_data->num_tmp_callbacks,')
940 gedi_txt.append(' my_data->tmp_dbg_create_infos,')
941 gedi_txt.append(' my_data->tmp_callbacks)) {')
942 gedi_txt.append(' callback_setup = true;')
943 gedi_txt.append(' }')
944 gedi_txt.append(' }')
945 gedi_txt.append('')
Tobin Ehlis86684f92016-01-05 10:33:58 -0700946 gedi_txt.append(' validate_instance(instance, instance, VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT, false);')
Tony Barboura05dbaa2015-07-09 17:31:46 -0600947 gedi_txt.append('')
Michael Lentine13803dc2015-11-04 14:35:12 -0800948 gedi_txt.append(' destroy_instance(instance, instance);')
Tony Barboura05dbaa2015-07-09 17:31:46 -0600949 gedi_txt.append(' // Report any remaining objects in LL')
Mark Lobodzinskic857fb32016-03-08 15:10:00 -0700950 gedi_txt.append('')
951 gedi_txt.append(' for (auto iit = VkDeviceMap.begin(); iit != VkDeviceMap.end();) {')
952 gedi_txt.append(' OBJTRACK_NODE* pNode = iit->second;')
953 gedi_txt.append(' if (pNode->belongsTo == (uint64_t)instance) {')
954 gedi_txt.append(' log_msg(mid(instance), VK_DEBUG_REPORT_ERROR_BIT_EXT, pNode->objType, pNode->vkObj, __LINE__, OBJTRACK_OBJECT_LEAK, "OBJTRACK",')
955 gedi_txt.append(' "OBJ ERROR : %s object 0x%" PRIxLEAST64 " has not been destroyed.", string_VkDebugReportObjectTypeEXT(pNode->objType),')
956 gedi_txt.append(' pNode->vkObj);')
Tony Barboura05dbaa2015-07-09 17:31:46 -0600957 for o in vulkan.core.objects:
Mark Lobodzinskic857fb32016-03-08 15:10:00 -0700958 if o in ['VkInstance', 'VkPhysicalDevice', 'VkQueue', 'VkDevice']:
Tony Barboura05dbaa2015-07-09 17:31:46 -0600959 continue
Mark Lobodzinskic857fb32016-03-08 15:10:00 -0700960 gedi_txt.append(' for (auto idt = %sMap.begin(); idt != %sMap.end();) {' % (o, o))
961 gedi_txt.append(' OBJTRACK_NODE* pNode = idt->second;')
962 gedi_txt.append(' if (pNode->belongsTo == iit->first) {')
963 gedi_txt.append(' log_msg(mid(instance), VK_DEBUG_REPORT_ERROR_BIT_EXT, pNode->objType, pNode->vkObj, __LINE__, OBJTRACK_OBJECT_LEAK, "OBJTRACK",')
964 gedi_txt.append(' "OBJ ERROR : %s object 0x%" PRIxLEAST64 " has not been destroyed.", string_VkDebugReportObjectTypeEXT(pNode->objType),')
965 gedi_txt.append(' pNode->vkObj);')
966 gedi_txt.append(' %sMap.erase(idt++);' % o )
967 gedi_txt.append(' } else {')
968 gedi_txt.append(' ++idt;')
969 gedi_txt.append(' }')
970 gedi_txt.append(' }')
971 gedi_txt.append(' VkDeviceMap.erase(iit++);')
972 gedi_txt.append(' } else {')
973 gedi_txt.append(' ++iit;')
974 gedi_txt.append(' }')
975 gedi_txt.append(' }')
976 gedi_txt.append('')
Mark Lobodzinski0d054fe2015-12-30 08:16:12 -0700977 gedi_txt.append(' VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(object_tracker_instance_table_map, instance);')
Chia-I Wuf7458c52015-10-26 21:10:41 +0800978 gedi_txt.append(' pInstanceTable->DestroyInstance(instance, pAllocator);')
Tony Barboura05dbaa2015-07-09 17:31:46 -0600979 gedi_txt.append('')
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600980 gedi_txt.append(' // Disable and cleanup the temporary callback(s):')
981 gedi_txt.append(' if (callback_setup) {')
982 gedi_txt.append(' layer_disable_tmp_callbacks(my_data->report_data,')
983 gedi_txt.append(' my_data->num_tmp_callbacks,')
984 gedi_txt.append(' my_data->tmp_callbacks);')
985 gedi_txt.append(' }')
986 gedi_txt.append(' if (my_data->num_tmp_callbacks > 0) {')
987 gedi_txt.append(' layer_free_tmp_callbacks(my_data->tmp_dbg_create_infos,')
988 gedi_txt.append(' my_data->tmp_callbacks);')
989 gedi_txt.append(' my_data->num_tmp_callbacks = 0;')
990 gedi_txt.append(' }')
991 gedi_txt.append('')
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -0600992 gedi_txt.append(' // Clean up logging callback, if any')
993 gedi_txt.append(' while (my_data->logging_callback.size() > 0) {')
994 gedi_txt.append(' VkDebugReportCallbackEXT callback = my_data->logging_callback.back();')
995 gedi_txt.append(' layer_destroy_msg_callback(my_data->report_data, callback, pAllocator);')
996 gedi_txt.append(' my_data->logging_callback.pop_back();')
Tony Barboura05dbaa2015-07-09 17:31:46 -0600997 gedi_txt.append(' }')
998 gedi_txt.append('')
999 gedi_txt.append(' layer_debug_report_destroy_instance(mid(instance));')
Tobin Ehlis4192fdf2016-04-18 15:40:59 -06001000 gedi_txt.append(' layer_data_map.erase(key);')
Tony Barboura05dbaa2015-07-09 17:31:46 -06001001 gedi_txt.append('')
Jon Ashburn3dc39382015-09-17 10:00:32 -06001002 gedi_txt.append(' instanceExtMap.erase(pInstanceTable);')
Jeremy Hayes2f065b12016-04-13 10:54:17 -06001003 gedi_txt.append(' lock.unlock();')
Mike Stroyan0699a792015-08-18 14:48:34 -06001004 # The loader holds a mutex that protects this from other threads
Mark Lobodzinski0d054fe2015-12-30 08:16:12 -07001005 gedi_txt.append(' object_tracker_instance_table_map.erase(key);')
Tony Barboura05dbaa2015-07-09 17:31:46 -06001006 gedi_txt.append('}')
1007 gedi_txt.append('')
1008 return "\n".join(gedi_txt)
1009
Mark Lobodzinski64d57752015-07-17 11:51:24 -06001010 def generate_destroy_device(self):
Tony Barboura05dbaa2015-07-09 17:31:46 -06001011 gedd_txt = []
1012 gedd_txt.append('%s' % self.lineinfo.get())
Chia-I Wuc57d5c82016-05-13 14:37:49 +08001013 gedd_txt.append('VKAPI_ATTR void VKAPI_CALL DestroyDevice(')
Chia-I Wuf7458c52015-10-26 21:10:41 +08001014 gedd_txt.append('VkDevice device,')
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001015 gedd_txt.append('const VkAllocationCallbacks* pAllocator)')
Tony Barboura05dbaa2015-07-09 17:31:46 -06001016 gedd_txt.append('{')
Jeremy Hayes2f065b12016-04-13 10:54:17 -06001017 gedd_txt.append(' std::unique_lock<std::mutex> lock(global_lock);')
Tobin Ehlis86684f92016-01-05 10:33:58 -07001018 gedd_txt.append(' validate_device(device, device, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, false);')
Tony Barboura05dbaa2015-07-09 17:31:46 -06001019 gedd_txt.append('')
Michael Lentine13803dc2015-11-04 14:35:12 -08001020 gedd_txt.append(' destroy_device(device, device);')
Mark Lobodzinskic857fb32016-03-08 15:10:00 -07001021 gedd_txt.append(' // Report any remaining objects associated with this VkDevice object in LL')
Tony Barboura05dbaa2015-07-09 17:31:46 -06001022 for o in vulkan.core.objects:
Mark Lobodzinski5f5c0e12015-11-12 16:02:35 -07001023 # DescriptorSets and Command Buffers are destroyed through their pools, not explicitly
1024 if o in ['VkInstance', 'VkPhysicalDevice', 'VkQueue', 'VkDevice', 'VkDescriptorSet', 'VkCommandBuffer']:
Tony Barboura05dbaa2015-07-09 17:31:46 -06001025 continue
Mark Lobodzinskic857fb32016-03-08 15:10:00 -07001026 gedd_txt.append(' for (auto it = %sMap.begin(); it != %sMap.end();) {' % (o, o))
Mark Lobodzinski5f5c0e12015-11-12 16:02:35 -07001027 gedd_txt.append(' OBJTRACK_NODE* pNode = it->second;')
Mark Lobodzinskic857fb32016-03-08 15:10:00 -07001028 gedd_txt.append(' if (pNode->belongsTo == (uint64_t)device) {')
1029 gedd_txt.append(' log_msg(mdd(device), VK_DEBUG_REPORT_ERROR_BIT_EXT, pNode->objType, pNode->vkObj, __LINE__, OBJTRACK_OBJECT_LEAK, "OBJTRACK",')
1030 gedd_txt.append(' "OBJ ERROR : %s object 0x%" PRIxLEAST64 " has not been destroyed.", string_VkDebugReportObjectTypeEXT(pNode->objType),')
1031 gedd_txt.append(' pNode->vkObj);')
1032 gedd_txt.append(' %sMap.erase(it++);' % o )
1033 gedd_txt.append(' } else {')
1034 gedd_txt.append(' ++it;')
1035 gedd_txt.append(' }')
Mark Lobodzinski5f5c0e12015-11-12 16:02:35 -07001036 gedd_txt.append(' }')
Tony Barboura05dbaa2015-07-09 17:31:46 -06001037 gedd_txt.append('')
1038 gedd_txt.append(" // Clean up Queue's MemRef Linked Lists")
1039 gedd_txt.append(' destroyQueueMemRefLists();')
1040 gedd_txt.append('')
Jeremy Hayes2f065b12016-04-13 10:54:17 -06001041 gedd_txt.append(' lock.unlock();')
Tony Barboura05dbaa2015-07-09 17:31:46 -06001042 gedd_txt.append('')
1043 gedd_txt.append(' dispatch_key key = get_dispatch_key(device);')
Mark Lobodzinski0d054fe2015-12-30 08:16:12 -07001044 gedd_txt.append(' VkLayerDispatchTable *pDisp = get_dispatch_table(object_tracker_device_table_map, device);')
Chia-I Wuf7458c52015-10-26 21:10:41 +08001045 gedd_txt.append(' pDisp->DestroyDevice(device, pAllocator);')
Mark Lobodzinski0d054fe2015-12-30 08:16:12 -07001046 gedd_txt.append(' object_tracker_device_table_map.erase(key);')
Tony Barboura05dbaa2015-07-09 17:31:46 -06001047 gedd_txt.append('')
Tony Barboura05dbaa2015-07-09 17:31:46 -06001048 gedd_txt.append('}')
1049 gedd_txt.append('')
1050 return "\n".join(gedd_txt)
1051
Mark Lobodzinski2fba0322016-01-23 18:31:23 -07001052 # Special-case validating some objects -- they may be non-NULL but should
1053 # only be validated upon meeting some condition specified below.
1054 def _dereference_conditionally(self, indent, prefix, type_name, name):
Mark Lobodzinski9fde6392016-01-19 09:57:24 -07001055 s_code = ''
1056 if type_name == 'pBufferInfo':
1057 s_code += '%sif ((%sdescriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||\n' % (indent, prefix)
1058 s_code += '%s (%sdescriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER) ||\n' % (indent, prefix)
1059 s_code += '%s (%sdescriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||\n' % (indent, prefix)
1060 s_code += '%s (%sdescriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) ) {\n' % (indent, prefix)
1061 elif type_name == 'pImageInfo':
1062 s_code += '%sif ((%sdescriptorType == VK_DESCRIPTOR_TYPE_SAMPLER) ||\n' % (indent, prefix)
1063 s_code += '%s (%sdescriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) ||\n' % (indent, prefix)
1064 s_code += '%s (%sdescriptorType == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) ||\n' % (indent, prefix)
1065 s_code += '%s (%sdescriptorType == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) ||\n' % (indent, prefix)
1066 s_code += '%s (%sdescriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) ) {\n' % (indent, prefix)
1067 elif type_name == 'pTexelBufferView':
Mark Lobodzinski2fba0322016-01-23 18:31:23 -07001068 s_code += '%sif ((%sdescriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER) ||\n' % (indent, prefix)
1069 s_code += '%s (%sdescriptorType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER) ) {\n' % (indent, prefix)
1070 elif name == 'pBeginInfo->pInheritanceInfo':
1071 s_code += '%sOBJTRACK_NODE* pNode = VkCommandBufferMap[(uint64_t)commandBuffer];\n' % (indent)
1072 s_code += '%sif ((%s) && (pNode->status & OBJSTATUS_COMMAND_BUFFER_SECONDARY)) {\n' % (indent, name)
Mark Lobodzinski9fde6392016-01-19 09:57:24 -07001073 else:
1074 s_code += '%sif (%s) {\n' % (indent, name)
1075 return s_code
1076
Tobin Ehlis86684f92016-01-05 10:33:58 -07001077 def _gen_obj_validate_code(self, struct_uses, obj_type_mapping, func_name, valid_null_dict, param0_name, indent, prefix, array_index):
1078 pre_code = ''
1079 for obj in sorted(struct_uses):
1080 name = obj
1081 array = ''
Mark Lobodzinski9fde6392016-01-19 09:57:24 -07001082 type_name = ''
Tobin Ehlis86684f92016-01-05 10:33:58 -07001083 if '[' in obj:
1084 (name, array) = obj.split('[')
Mark Lobodzinski9fde6392016-01-19 09:57:24 -07001085 type_name = name
Tobin Ehlis86684f92016-01-05 10:33:58 -07001086 array = array.strip(']')
1087 if isinstance(struct_uses[obj], dict):
1088 local_prefix = ''
1089 name = '%s%s' % (prefix, name)
1090 ptr_type = False
1091 if 'p' == obj[0]:
1092 ptr_type = True
Mark Lobodzinski2fba0322016-01-23 18:31:23 -07001093 tmp_pre = self._dereference_conditionally(indent, prefix, type_name, name)
Mark Lobodzinski9fde6392016-01-19 09:57:24 -07001094 pre_code += tmp_pre
Tobin Ehlis86684f92016-01-05 10:33:58 -07001095 indent += ' '
1096 if array != '':
1097 idx = 'idx%s' % str(array_index)
1098 array_index += 1
1099 pre_code += '%s\n' % self.lineinfo.get()
1100 pre_code += '%sfor (uint32_t %s=0; %s<%s%s; ++%s) {\n' % (indent, idx, idx, prefix, array, idx)
1101 indent += ' '
1102 local_prefix = '%s[%s].' % (name, idx)
1103 elif ptr_type:
1104 local_prefix = '%s->' % (name)
1105 else:
1106 local_prefix = '%s.' % (name)
1107 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)
1108 pre_code += tmp_pre
1109 if array != '':
1110 indent = indent[4:]
1111 pre_code += '%s}\n' % (indent)
1112 if ptr_type:
1113 indent = indent[4:]
1114 pre_code += '%s}\n' % (indent)
1115 else:
1116 ptype = struct_uses[obj]
1117 dbg_obj_type = obj_type_mapping[ptype]
1118 fname = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', ptype)
1119 fname = re.sub('([a-z0-9])([A-Z])', r'\1_\2', fname).lower()[3:]
1120 full_name = '%s%s' % (prefix, name)
1121 null_obj_ok = 'false'
1122 # If a valid null param is defined for this func and we have a match, allow NULL
Mike Stroyan04be7832016-04-07 12:14:30 -06001123 if func_name in valid_null_dict and True in [name in pn for pn in sorted(valid_null_dict[func_name])]:
Tobin Ehlis86684f92016-01-05 10:33:58 -07001124 null_obj_ok = 'true'
1125 if (array_index > 0) or '' != array:
Mark Lobodzinski2fba0322016-01-23 18:31:23 -07001126 tmp_pre = self._dereference_conditionally(indent, prefix, type_name, full_name)
Mark Lobodzinski9fde6392016-01-19 09:57:24 -07001127 pre_code += tmp_pre
Tobin Ehlis86684f92016-01-05 10:33:58 -07001128 indent += ' '
1129 if array != '':
1130 idx = 'idx%s' % str(array_index)
1131 array_index += 1
1132 pre_code += '%sfor (uint32_t %s=0; %s<%s%s; ++%s) {\n' % (indent, idx, idx, prefix, array, idx)
1133 indent += ' '
1134 full_name = '%s[%s]' % (full_name, idx)
1135 pre_code += '%s\n' % self.lineinfo.get()
1136 pre_code += '%sskipCall |= validate_%s(%s, %s, %s, %s);\n' %(indent, fname, param0_name, full_name, dbg_obj_type, null_obj_ok)
1137 if array != '':
1138 indent = indent[4:]
1139 pre_code += '%s}\n' % (indent)
1140 indent = indent[4:]
1141 pre_code += '%s}\n' % (indent)
1142 else:
1143 pre_code += '%s\n' % self.lineinfo.get()
1144 pre_code += '%sskipCall |= validate_%s(%s, %s, %s, %s);\n' %(indent, fname, param0_name, full_name, dbg_obj_type, null_obj_ok)
1145 return pre_code
Tony Barboura05dbaa2015-07-09 17:31:46 -06001146
Mike Stroyan3e3a1eb2015-04-03 17:13:23 -06001147 def generate_intercept(self, proto, qual):
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001148 if proto.name in [ 'CreateDebugReportCallbackEXT', 'EnumerateInstanceLayerProperties', 'EnumerateInstanceExtensionProperties','EnumerateDeviceLayerProperties', 'EnumerateDeviceExtensionProperties' ]:
Mike Stroyan00087e62015-04-03 14:39:16 -06001149 # use default version
1150 return None
Mark Lobodzinski7c75b852015-05-05 15:01:37 -05001151
Tony Barboura05dbaa2015-07-09 17:31:46 -06001152 # Create map of object names to object type enums of the form VkName : VkObjectTypeName
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001153 obj_type_mapping = {base_t : base_t.replace("Vk", "VkDebugReportObjectType") for base_t in vulkan.object_type_list}
Mark Lobodzinski7c75b852015-05-05 15:01:37 -05001154 # Convert object type enum names from UpperCamelCase to UPPER_CASE_WITH_UNDERSCORES
1155 for objectName, objectTypeEnum in obj_type_mapping.items():
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001156 obj_type_mapping[objectName] = ucc_to_U_C_C(objectTypeEnum) + '_EXT';
Mark Lobodzinski7c75b852015-05-05 15:01:37 -05001157 # Command Buffer Object doesn't follow the rule.
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -07001158 obj_type_mapping['VkCommandBuffer'] = "VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT"
1159 obj_type_mapping['VkShaderModule'] = "VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT"
Mike Stroyan00087e62015-04-03 14:39:16 -06001160
Mark Lobodzinskifae78852015-06-23 11:35:12 -06001161 explicit_object_tracker_functions = [
1162 "CreateInstance",
Tobin Ehlisec598302015-09-15 15:02:17 -06001163 "EnumeratePhysicalDevices",
Cody Northropd0802882015-08-03 17:04:53 -06001164 "GetPhysicalDeviceQueueFamilyProperties",
Mark Lobodzinskifae78852015-06-23 11:35:12 -06001165 "CreateDevice",
Mark Lobodzinskifae78852015-06-23 11:35:12 -06001166 "GetDeviceQueue",
Chia-I Wu1ff4c3d2015-10-26 16:55:27 +08001167 "QueueBindSparse",
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001168 "AllocateDescriptorSets",
Tony Barbour770f80d2015-07-20 10:52:13 -06001169 "FreeDescriptorSets",
Mark Lobodzinski154329b2016-01-26 09:55:28 -07001170 "CreateGraphicsPipelines",
1171 "CreateComputePipelines",
Mark Lobodzinski5f5c0e12015-11-12 16:02:35 -07001172 "AllocateCommandBuffers",
1173 "FreeCommandBuffers",
1174 "DestroyDescriptorPool",
1175 "DestroyCommandPool",
Mark Lobodzinskifae78852015-06-23 11:35:12 -06001176 "MapMemory",
1177 "UnmapMemory",
1178 "FreeMemory",
Mark Lobodzinskie6d3f2c2015-10-14 13:16:33 -06001179 "DestroySwapchainKHR",
1180 "GetSwapchainImagesKHR"
Mark Lobodzinskifae78852015-06-23 11:35:12 -06001181 ]
Chia-I Wuc57d5c82016-05-13 14:37:49 +08001182 decl = proto.c_func(attr="VKAPI")
Mike Stroyan00087e62015-04-03 14:39:16 -06001183 param0_name = proto.params[0].name
Mark Lobodzinski48bd16d2015-05-08 09:12:28 -05001184 using_line = ''
Mike Stroyan00087e62015-04-03 14:39:16 -06001185 create_line = ''
Mark Lobodzinski64d57752015-07-17 11:51:24 -06001186 destroy_line = ''
Tobin Ehlis154e0462015-08-26 11:22:09 -06001187 # 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 Ehlisd34a4c52015-12-08 10:50:10 -07001188 # TODO : Should integrate slightly better code for this purpose from unique_objects layer
Tobin Ehlis154e0462015-08-26 11:22:09 -06001189 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 Lentine13803dc2015-11-04 14:35:12 -08001190 loop_types = defaultdict(list)
Tobin Ehlis2717d132015-07-10 18:25:07 -06001191 # TODO : For now skipping objs that can be NULL. Really should check these and have special case that allows them to be NULL
Tobin Ehlis154e0462015-08-26 11:22:09 -06001192 # or better yet, these should be encoded into an API json definition and we generate checks from there
1193 # 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)
1194 # param names may be directly passed to the function, or may be a field in a struct param
1195 valid_null_object_names = {'CreateGraphicsPipelines' : ['basePipelineHandle'],
1196 'CreateComputePipelines' : ['basePipelineHandle'],
1197 'BeginCommandBuffer' : ['renderPass', 'framebuffer'],
Tobin Ehlisec598302015-09-15 15:02:17 -06001198 'QueueSubmit' : ['fence'],
Jon Ashburn9216ae42016-01-14 15:11:55 -07001199 'AcquireNextImageKHR' : ['fence', 'semaphore' ],
Tobin Ehlisba31cab2015-11-02 15:24:32 -07001200 'UpdateDescriptorSets' : ['pTexelBufferView'],
Tobin Ehlis86684f92016-01-05 10:33:58 -07001201 'CreateSwapchainKHR' : ['oldSwapchain'],
Petros Bantolas2b40be72016-04-15 11:02:59 +01001202 'CreateDisplayModeKHR': ['pAllocator'],
Tobin Ehlis154e0462015-08-26 11:22:09 -06001203 }
Tobin Ehlis154e0462015-08-26 11:22:09 -06001204 param_count = 'NONE' # keep track of arrays passed directly into API functions
Tobin Ehlis803cc492015-06-08 17:36:28 -06001205 for p in proto.params:
Tobin Ehlisec598302015-09-15 15:02:17 -06001206 base_type = p.ty.replace('const ', '').strip('*')
Tobin Ehlis154e0462015-08-26 11:22:09 -06001207 if 'count' in p.name.lower():
1208 param_count = p.name
Tobin Ehlisec598302015-09-15 15:02:17 -06001209 if base_type in vulkan.core.objects:
1210 # This is an object to potentially check for validity. First see if it's an array
1211 if '*' in p.ty and 'const' in p.ty and param_count != 'NONE':
1212 loop_params[param_count].append(p.name)
Michael Lentine13803dc2015-11-04 14:35:12 -08001213 loop_types[param_count].append(str(p.ty[6:-1]))
Tobin Ehlisec598302015-09-15 15:02:17 -06001214 # Not an array, check for just a base Object that's not in exceptions
1215 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 Ehlis154e0462015-08-26 11:22:09 -06001216 loop_params[0].append(p.name)
Michael Lentine13803dc2015-11-04 14:35:12 -08001217 loop_types[0].append(str(p.ty))
Tobin Ehlisec598302015-09-15 15:02:17 -06001218 elif vk_helper.is_type(base_type, 'struct'):
1219 struct_type = base_type
Tobin Ehlis9d675942015-06-30 14:32:16 -06001220 if vk_helper.typedef_rev_dict[struct_type] in vk_helper.struct_dict:
1221 struct_type = vk_helper.typedef_rev_dict[struct_type]
Tobin Ehlis82b3db52015-10-23 17:52:53 -06001222 # Parse elements of this struct param to identify objects and/or arrays of objects
Tobin Ehlis9d675942015-06-30 14:32:16 -06001223 for m in sorted(vk_helper.struct_dict[struct_type]):
1224 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 Ehlis154e0462015-08-26 11:22:09 -06001225 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 Ehlis82b3db52015-10-23 17:52:53 -06001226 # This is not great, but gets the job done for now, but If we have a count and this param is a ptr w/
1227 # last letter 's' OR non-'count' string of count is in the param name, then this is a dynamically sized array param
1228 param_array = False
1229 if param_count != 'NONE':
1230 if '*' in p.ty:
1231 if 's' == p.name[-1] or param_count.lower().replace('count', '') in p.name.lower():
1232 param_array = True
1233 if param_array:
Tobin Ehlis154e0462015-08-26 11:22:09 -06001234 param_name = '%s[i].%s' % (p.name, vk_helper.struct_dict[struct_type][m]['name'])
Tobin Ehlis46d53622015-07-10 11:10:21 -06001235 else:
Tobin Ehlis154e0462015-08-26 11:22:09 -06001236 param_name = '%s->%s' % (p.name, vk_helper.struct_dict[struct_type][m]['name'])
1237 if vk_helper.struct_dict[struct_type][m]['dyn_array']:
Tobin Ehlis82b3db52015-10-23 17:52:53 -06001238 if param_count != 'NONE': # this will be a double-embedded loop, use comma delineated 'count,name' for param_name
1239 loop_count = '%s[i].%s' % (p.name, vk_helper.struct_dict[struct_type][m]['array_size'])
1240 loop_params[param_count].append('%s,%s' % (loop_count, param_name))
Michael Lentine13803dc2015-11-04 14:35:12 -08001241 loop_types[param_count].append('%s' % (vk_helper.struct_dict[struct_type][m]['type']))
Tobin Ehlis82b3db52015-10-23 17:52:53 -06001242 else:
1243 loop_count = '%s->%s' % (p.name, vk_helper.struct_dict[struct_type][m]['array_size'])
1244 loop_params[loop_count].append(param_name)
Michael Lentine13803dc2015-11-04 14:35:12 -08001245 loop_types[loop_count].append('%s' % (vk_helper.struct_dict[struct_type][m]['type']))
Tobin Ehlis154e0462015-08-26 11:22:09 -06001246 else:
1247 if '[' in param_name: # dynamic array param, set size
1248 loop_params[param_count].append(param_name)
Michael Lentine13803dc2015-11-04 14:35:12 -08001249 loop_types[param_count].append('%s' % (vk_helper.struct_dict[struct_type][m]['type']))
Tobin Ehlis154e0462015-08-26 11:22:09 -06001250 else:
1251 loop_params[0].append(param_name)
Michael Lentine13803dc2015-11-04 14:35:12 -08001252 loop_types[0].append('%s' % (vk_helper.struct_dict[struct_type][m]['type']))
Tobin Ehlis86684f92016-01-05 10:33:58 -07001253 last_param_index = None
1254 create_func = False
1255 if True in [create_txt in proto.name for create_txt in ['Create', 'Allocate']]:
1256 create_func = True
1257 last_param_index = -1 # For create funcs don't validate last object
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -07001258 (struct_uses, local_decls) = get_object_uses(vulkan.object_type_list, proto.params[:last_param_index])
Mike Stroyan00087e62015-04-03 14:39:16 -06001259 funcs = []
Tobin Ehlis803cc492015-06-08 17:36:28 -06001260 mutex_unlock = False
Tobin Ehlis154e0462015-08-26 11:22:09 -06001261 funcs.append('%s\n' % self.lineinfo.get())
Mark Lobodzinskifae78852015-06-23 11:35:12 -06001262 if proto.name in explicit_object_tracker_functions:
Jon Ashburn4d9f4652015-04-08 21:33:34 -06001263 funcs.append('%s%s\n'
1264 '{\n'
Mark Lobodzinskifae78852015-06-23 11:35:12 -06001265 ' return explicit_%s;\n'
1266 '}' % (qual, decl, proto.c_call()))
1267 return "".join(funcs)
Mark Lobodzinski308d7792015-11-24 10:28:31 -07001268 # Temporarily prevent DestroySurface call from being generated until WSI layer support is fleshed out
Petros Bantolas2b40be72016-04-15 11:02:59 +01001269 elif 'DestroyInstance' in proto.name or 'DestroyDevice' in proto.name or 'CreateDisplayModeKHR' in proto.name:
Mark Lobodzinski64d57752015-07-17 11:51:24 -06001270 return ""
Jon Ashburn4d9f4652015-04-08 21:33:34 -06001271 else:
Tobin Ehlis86684f92016-01-05 10:33:58 -07001272 if create_func:
Michael Lentine13803dc2015-11-04 14:35:12 -08001273 typ = proto.params[-1].ty.strip('*').replace('const ', '');
1274 name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', typ)
1275 name = re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower()[3:]
Jeremy Hayes2f065b12016-04-13 10:54:17 -06001276 create_line = ' {\n'
1277 create_line += ' std::lock_guard<std::mutex> lock(global_lock);\n'
1278 create_line += ' if (result == VK_SUCCESS) {\n'
1279 create_line += ' create_%s(%s, *%s, %s);\n' % (name, param0_name, proto.params[-1].name, obj_type_mapping[typ])
1280 create_line += ' }\n'
Mark Lobodzinskifae78852015-06-23 11:35:12 -06001281 create_line += ' }\n'
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -06001282 if 'FreeCommandBuffers' in proto.name:
Michael Lentine13803dc2015-11-04 14:35:12 -08001283 typ = proto.params[-1].ty.strip('*').replace('const ', '');
1284 name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', typ)
1285 name = re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower()[3:]
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -06001286 funcs.append('%s\n' % self.lineinfo.get())
1287 destroy_line = ' loader_platform_thread_lock_mutex(&objLock);\n'
Chia-I Wud50a7d72015-10-26 20:48:51 +08001288 destroy_line += ' for (uint32_t i = 0; i < commandBufferCount; i++) {\n'
Michael Lentine13803dc2015-11-04 14:35:12 -08001289 destroy_line += ' destroy_%s(%s[i], %s[i]);\n' % (name, proto.params[-1].name, proto.params[-1].name)
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -06001290 destroy_line += ' }\n'
1291 destroy_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
Mark Lobodzinski64d57752015-07-17 11:51:24 -06001292 if 'Destroy' in proto.name:
Michael Lentine13803dc2015-11-04 14:35:12 -08001293 typ = proto.params[-2].ty.strip('*').replace('const ', '');
1294 name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', typ)
1295 name = re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower()[3:]
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -06001296 funcs.append('%s\n' % self.lineinfo.get())
Jeremy Hayes2f065b12016-04-13 10:54:17 -06001297 destroy_line = ' {\n'
1298 destroy_line += ' std::lock_guard<std::mutex> lock(global_lock);\n'
1299 destroy_line += ' destroy_%s(%s, %s);\n' % (name, param0_name, proto.params[-2].name)
1300 destroy_line += ' }\n'
Tobin Ehlis86684f92016-01-05 10:33:58 -07001301 indent = ' '
1302 if len(struct_uses) > 0:
Mark Lobodzinski2abefa92016-05-05 11:45:57 -06001303 using_line += '%sbool skipCall = false;\n' % (indent)
Mark Lobodzinskifae78852015-06-23 11:35:12 -06001304 if not mutex_unlock:
Jeremy Hayes2f065b12016-04-13 10:54:17 -06001305 using_line += '%s{\n' % (indent)
1306 indent += ' '
1307 using_line += '%sstd::lock_guard<std::mutex> lock(global_lock);\n' % (indent)
Mark Lobodzinskifae78852015-06-23 11:35:12 -06001308 mutex_unlock = True
Mike Stroyan04be7832016-04-07 12:14:30 -06001309 using_line += '// objects to validate: %s\n' % str(sorted(struct_uses))
Jeremy Hayes2f065b12016-04-13 10:54:17 -06001310 using_line += self._gen_obj_validate_code(struct_uses, obj_type_mapping, proto.name, valid_null_object_names, param0_name, indent, '', 0)
Mark Lobodzinskifae78852015-06-23 11:35:12 -06001311 if mutex_unlock:
Jeremy Hayes2f065b12016-04-13 10:54:17 -06001312 indent = indent[4:]
1313 using_line += '%s}\n' % (indent)
Tobin Ehlis86684f92016-01-05 10:33:58 -07001314 if len(struct_uses) > 0:
Tobin Ehlisc9ac2b62015-09-11 12:57:55 -06001315 using_line += ' if (skipCall)\n'
Mark Lobodzinski2abefa92016-05-05 11:45:57 -06001316 if proto.ret == "bool":
1317 using_line += ' return false;\n'
Jamie Madill940c4bd2016-05-11 16:11:47 -04001318 elif proto.ret == "VkBool32":
1319 using_line += ' return VK_FALSE;\n'
Jamie Madill2bf385b2016-04-04 12:15:39 -04001320 elif proto.ret != "void":
Courtney Goeltzenleuchter52fee652015-12-10 16:41:22 -07001321 using_line += ' return VK_ERROR_VALIDATION_FAILED_EXT;\n'
Tobin Ehlisc9ac2b62015-09-11 12:57:55 -06001322 else:
1323 using_line += ' return;\n'
Mark Lobodzinskifae78852015-06-23 11:35:12 -06001324 ret_val = ''
1325 stmt = ''
Mark Lobodzinskifae78852015-06-23 11:35:12 -06001326 if proto.ret != "void":
1327 ret_val = "%s result = " % proto.ret
1328 stmt = " return result;\n"
1329
1330 dispatch_param = proto.params[0].name
1331 if 'CreateInstance' in proto.name:
1332 dispatch_param = '*' + proto.params[1].name
1333
Mark Lobodzinskifb5437a2015-05-22 14:15:36 -05001334 # Must use 'instance' table for these APIs, 'device' table otherwise
1335 table_type = ""
1336 if proto_is_global(proto):
1337 table_type = "instance"
1338 else:
1339 table_type = "device"
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -07001340 if wsi_name(proto.name):
1341 funcs.append('%s' % wsi_ifdef(proto.name))
Mike Stroyan00087e62015-04-03 14:39:16 -06001342 funcs.append('%s%s\n'
1343 '{\n'
1344 '%s'
Mike Stroyan00087e62015-04-03 14:39:16 -06001345 '%s'
Mark Lobodzinski0d054fe2015-12-30 08:16:12 -07001346 ' %sget_dispatch_table(object_tracker_%s_table_map, %s)->%s;\n'
Mike Stroyan38820b32015-09-28 13:47:29 -06001347 '%s'
1348 '%s'
1349 '}' % (qual, decl, using_line, destroy_line, ret_val, table_type, dispatch_param, proto.c_call(), create_line, stmt))
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -07001350 if wsi_name(proto.name):
1351 funcs.append('%s' % wsi_endif(proto.name))
Mike Stroyan00087e62015-04-03 14:39:16 -06001352 return "\n\n".join(funcs)
1353
Tobin Ehlis12076fc2014-10-22 09:06:33 -06001354 def generate_body(self):
Mark Lobodzinski0d054fe2015-12-30 08:16:12 -07001355 self.layer_name = "object_tracker"
Ian Elliott1064fe32015-07-06 14:31:32 -06001356 extensions=[('wsi_enabled',
Ian Elliott05846062015-11-20 14:13:17 -07001357 ['vkCreateSwapchainKHR',
Jon Ashburn8acd2332015-09-16 18:08:32 -06001358 'vkDestroySwapchainKHR', 'vkGetSwapchainImagesKHR',
1359 'vkAcquireNextImageKHR', 'vkQueuePresentKHR'])]
Petros Bantolas2b40be72016-04-15 11:02:59 +01001360 additional_instance_extensions = [
1361 'vkDestroySurfaceKHR',
1362 'vkGetPhysicalDeviceSurfaceSupportKHR',
1363 'vkGetPhysicalDeviceSurfaceCapabilitiesKHR',
1364 'vkGetPhysicalDeviceSurfaceFormatsKHR',
1365 'vkGetPhysicalDeviceSurfacePresentModesKHR',
1366 'vkCreateDisplayPlaneSurfaceKHR',
1367 ]
Mun, Gwan-gyeongbd4dd592016-02-22 09:43:09 +09001368 if self.wsi == 'Win32':
Michael Lentine64e2ebd2015-12-03 14:33:09 -08001369 instance_extensions=[('msg_callback_get_proc_addr', []),
1370 ('wsi_enabled',
Petros Bantolas2b40be72016-04-15 11:02:59 +01001371 ['vkCreateWin32SurfaceKHR',
1372 'vkGetPhysicalDeviceWin32PresentationSupportKHR'] + additional_instance_extensions)]
Mun, Gwan-gyeongbd4dd592016-02-22 09:43:09 +09001373 elif self.wsi == 'Android':
1374 instance_extensions=[('msg_callback_get_proc_addr', []),
1375 ('wsi_enabled',
Petros Bantolas2b40be72016-04-15 11:02:59 +01001376 ['vkCreateAndroidSurfaceKHR'] + additional_instance_extensions)]
Karl Schultz9daf7a32016-03-08 15:14:11 -07001377 elif self.wsi == 'Xcb' or self.wsi == 'Xlib' or self.wsi == 'Wayland' or self.wsi == 'Mir':
Michael Lentine64e2ebd2015-12-03 14:33:09 -08001378 instance_extensions=[('msg_callback_get_proc_addr', []),
1379 ('wsi_enabled',
Petros Bantolas2b40be72016-04-15 11:02:59 +01001380 additional_instance_extensions + ['vkCreateXcbSurfaceKHR',
Karl Schultz9daf7a32016-03-08 15:14:11 -07001381 'vkGetPhysicalDeviceXcbPresentationSupportKHR',
Mun, Gwan-gyeongbd4dd592016-02-22 09:43:09 +09001382 'vkCreateXlibSurfaceKHR',
Karl Schultz9daf7a32016-03-08 15:14:11 -07001383 'vkGetPhysicalDeviceXlibPresentationSupportKHR',
Mun, Gwan-gyeongbd4dd592016-02-22 09:43:09 +09001384 'vkCreateWaylandSurfaceKHR',
Karl Schultz9daf7a32016-03-08 15:14:11 -07001385 'vkGetPhysicalDeviceWaylandPresentationSupportKHR',
Mun, Gwan-gyeongbd4dd592016-02-22 09:43:09 +09001386 'vkCreateMirSurfaceKHR',
1387 'vkGetPhysicalDeviceMirPresentationSupportKHR'])]
Mark Lobodzinskid53098f2016-02-25 18:14:56 -07001388 else:
1389 print('Error: Undefined DisplayServer')
1390 instance_extensions=[]
1391
Chia-I Wucdb70962016-05-13 14:07:36 +08001392 body = ["namespace %s {" % self.layer_name,
1393 self.generate_maps(),
Tony Barboura05dbaa2015-07-09 17:31:46 -06001394 self.generate_procs(),
Mark Lobodzinski64d57752015-07-17 11:51:24 -06001395 self.generate_destroy_instance(),
1396 self.generate_destroy_device(),
Chia-I Wuc57d5c82016-05-13 14:37:49 +08001397 self._generate_dispatch_entrypoints(),
Tobin Ehlisca915872014-11-18 11:28:33 -07001398 self._generate_extensions(),
Chia-I Wub02600c2016-05-20 07:11:22 +08001399 self._generate_layer_introspection_function(),
Jon Ashburn747f2b62015-06-18 15:02:58 -06001400 self._generate_layer_gpa_function(extensions,
Chia-I Wu40b8bab2016-05-13 14:06:08 +08001401 instance_extensions),
Chia-I Wucdb70962016-05-13 14:07:36 +08001402 "} // namespace %s" % self.layer_name,
1403 self._gen_layer_logging_workaround(),
Chia-I Wu40b8bab2016-05-13 14:06:08 +08001404 self._gen_layer_interface_v0_functions()]
Tobin Ehlis12076fc2014-10-22 09:06:33 -06001405 return "\n\n".join(body)
Courtney Goeltzenleuchterb412d212014-11-18 10:40:29 -07001406
Tobin Ehlisd34a4c52015-12-08 10:50:10 -07001407class UniqueObjectsSubcommand(Subcommand):
1408 def generate_header(self):
1409 header_txt = []
1410 header_txt.append('%s' % self.lineinfo.get())
1411 header_txt.append('#include "unique_objects.h"')
Tobin Ehlisd34a4c52015-12-08 10:50:10 -07001412 return "\n".join(header_txt)
1413
Tobin Ehlis65f44e42016-01-05 09:46:03 -07001414 # Generate UniqueObjects code for given struct_uses dict of objects that need to be unwrapped
Tobin Ehlisa39c26a2016-01-05 16:34:59 -07001415 # vector_name_set is used to make sure we don't replicate vector names
1416 # first_level_param indicates if elements are passed directly into the function else they're below a ptr/struct
Tobin Ehlis65f44e42016-01-05 09:46:03 -07001417 # TODO : Comment this code
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -07001418 def _gen_obj_code(self, struct_uses, param_type, indent, prefix, array_index, vector_name_set, first_level_param):
Tobin Ehlis65f44e42016-01-05 09:46:03 -07001419 decls = ''
1420 pre_code = ''
1421 post_code = ''
Tobin Ehlisa39c26a2016-01-05 16:34:59 -07001422 for obj in sorted(struct_uses):
Tobin Ehlis65f44e42016-01-05 09:46:03 -07001423 name = obj
1424 array = ''
1425 if '[' in obj:
1426 (name, array) = obj.split('[')
1427 array = array.strip(']')
Tobin Ehlisa39c26a2016-01-05 16:34:59 -07001428 ptr_type = False
Tobin Ehlis10ba1de2016-04-13 12:59:43 -06001429 if 'p' == obj[0] and obj[1] != obj[1].lower(): # TODO : Not ideal way to determine ptr
Tobin Ehlisa39c26a2016-01-05 16:34:59 -07001430 ptr_type = True
Tobin Ehlis65f44e42016-01-05 09:46:03 -07001431 if isinstance(struct_uses[obj], dict):
1432 local_prefix = ''
1433 name = '%s%s' % (prefix, name)
Tobin Ehlisa39c26a2016-01-05 16:34:59 -07001434 if ptr_type:
Tobin Ehlis6dd0fc32016-02-12 14:37:09 -07001435 if first_level_param and name in param_type:
1436 pre_code += '%sif (%s) {\n' % (indent, name)
1437 else: # shadow ptr will have been initialized at this point so check it vs. source ptr
1438 pre_code += '%sif (local_%s) {\n' % (indent, name)
Tobin Ehlis65f44e42016-01-05 09:46:03 -07001439 indent += ' '
1440 if array != '':
1441 idx = 'idx%s' % str(array_index)
1442 array_index += 1
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -07001443 if first_level_param and name in param_type:
1444 pre_code += '%slocal_%s = new safe_%s[%s];\n' % (indent, name, param_type[name].strip('*'), array)
1445 post_code += ' if (local_%s)\n' % (name)
1446 post_code += ' delete[] local_%s;\n' % (name)
Tobin Ehlis65f44e42016-01-05 09:46:03 -07001447 pre_code += '%sfor (uint32_t %s=0; %s<%s%s; ++%s) {\n' % (indent, idx, idx, prefix, array, idx)
Tobin Ehlis65f44e42016-01-05 09:46:03 -07001448 indent += ' '
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -07001449 if first_level_param:
1450 pre_code += '%slocal_%s[%s].initialize(&%s[%s]);\n' % (indent, name, idx, name, idx)
Tobin Ehlis65f44e42016-01-05 09:46:03 -07001451 local_prefix = '%s[%s].' % (name, idx)
1452 elif ptr_type:
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -07001453 if first_level_param and name in param_type:
1454 pre_code += '%slocal_%s = new safe_%s(%s);\n' % (indent, name, param_type[name].strip('*'), name)
1455 post_code += ' if (local_%s)\n' % (name)
1456 post_code += ' delete local_%s;\n' % (name)
Tobin Ehlis65f44e42016-01-05 09:46:03 -07001457 local_prefix = '%s->' % (name)
1458 else:
1459 local_prefix = '%s.' % (name)
1460 assert isinstance(decls, object)
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -07001461 (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 Ehlis65f44e42016-01-05 09:46:03 -07001462 decls += tmp_decl
1463 pre_code += tmp_pre
1464 post_code += tmp_post
1465 if array != '':
1466 indent = indent[4:]
1467 pre_code += '%s}\n' % (indent)
Tobin Ehlis65f44e42016-01-05 09:46:03 -07001468 if ptr_type:
1469 indent = indent[4:]
1470 pre_code += '%s}\n' % (indent)
Tobin Ehlis65f44e42016-01-05 09:46:03 -07001471 else:
Tobin Ehlisa39c26a2016-01-05 16:34:59 -07001472 if (array_index > 0) or array != '': # TODO : This is not ideal, really want to know if we're anywhere under an array
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -07001473 if first_level_param:
Tobin Ehlis10ba1de2016-04-13 12:59:43 -06001474 decls += '%s%s* local_%s = NULL;\n' % (indent, struct_uses[obj], name)
Tobin Ehlis6dd0fc32016-02-12 14:37:09 -07001475 if array != '' and not first_level_param: # ptrs under structs will have been initialized so use local_*
1476 pre_code += '%sif (local_%s%s) {\n' %(indent, prefix, name)
1477 else:
1478 pre_code += '%sif (%s%s) {\n' %(indent, prefix, name)
Tobin Ehlis65f44e42016-01-05 09:46:03 -07001479 indent += ' '
Tobin Ehlis65f44e42016-01-05 09:46:03 -07001480 if array != '':
1481 idx = 'idx%s' % str(array_index)
1482 array_index += 1
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -07001483 if first_level_param:
1484 pre_code += '%slocal_%s = new %s[%s];\n' % (indent, name, struct_uses[obj], array)
1485 post_code += ' if (local_%s)\n' % (name)
1486 post_code += ' delete[] local_%s;\n' % (name)
Tobin Ehlis65f44e42016-01-05 09:46:03 -07001487 pre_code += '%sfor (uint32_t %s=0; %s<%s%s; ++%s) {\n' % (indent, idx, idx, prefix, array, idx)
Tobin Ehlis65f44e42016-01-05 09:46:03 -07001488 indent += ' '
1489 name = '%s[%s]' % (name, idx)
1490 pName = 'p%s' % (struct_uses[obj][2:])
Tobin Ehlisa39c26a2016-01-05 16:34:59 -07001491 if name not in vector_name_set:
1492 vector_name_set.add(name)
Dustin Gravesa7622d82016-04-14 17:29:20 -06001493 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 Ehlis65f44e42016-01-05 09:46:03 -07001494 if array != '':
1495 indent = indent[4:]
1496 pre_code += '%s}\n' % (indent)
Tobin Ehlis65f44e42016-01-05 09:46:03 -07001497 indent = indent[4:]
1498 pre_code += '%s}\n' % (indent)
Tobin Ehlis65f44e42016-01-05 09:46:03 -07001499 else:
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -07001500 pre_code += '%s\n' % (self.lineinfo.get())
Tobin Ehlisa39c26a2016-01-05 16:34:59 -07001501 deref_txt = '&'
1502 if ptr_type:
1503 deref_txt = ''
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -07001504 if '->' in prefix: # need to update local struct
Dustin Gravesa7622d82016-04-14 17:29:20 -06001505 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 Ehlis8bb7c2f2016-02-10 15:38:45 -07001506 else:
Dustin Gravesa7622d82016-04-14 17:29:20 -06001507 pre_code += '%s%s = (%s)my_map_data->unique_id_mapping[reinterpret_cast<uint64_t &>(%s)];\n' % (indent, name, struct_uses[obj], name)
Tobin Ehlis65f44e42016-01-05 09:46:03 -07001508 return decls, pre_code, post_code
Tobin Ehlisd34a4c52015-12-08 10:50:10 -07001509
1510 def generate_intercept(self, proto, qual):
1511 create_func = False
1512 destroy_func = False
1513 last_param_index = None #typcially we look at all params for ndos
1514 pre_call_txt = '' # code prior to calling down chain such as unwrap uses of ndos
1515 post_call_txt = '' # code following call down chain such to wrap newly created ndos, or destroy local wrap struct
1516 funcs = []
1517 indent = ' ' # indent level for generated code
Chia-I Wuc57d5c82016-05-13 14:37:49 +08001518 decl = proto.c_func(attr="VKAPI")
Tobin Ehlis65f44e42016-01-05 09:46:03 -07001519 # A few API cases that are manual code
Tobin Ehlisa39c26a2016-01-05 16:34:59 -07001520 # TODO : Special case Create*Pipelines funcs to handle creating multiple unique objects
Tobin Ehlis65f44e42016-01-05 09:46:03 -07001521 explicit_object_tracker_functions = ['GetSwapchainImagesKHR',
Tobin Ehlis10ba1de2016-04-13 12:59:43 -06001522 'CreateSwapchainKHR',
Tobin Ehlisd34a4c52015-12-08 10:50:10 -07001523 'CreateInstance',
Tobin Ehlis10ba1de2016-04-13 12:59:43 -06001524 'DestroyInstance',
Tobin Ehlisa39c26a2016-01-05 16:34:59 -07001525 'CreateDevice',
Tobin Ehlis10ba1de2016-04-13 12:59:43 -06001526 'DestroyDevice',
Tobin Ehlisa39c26a2016-01-05 16:34:59 -07001527 'CreateComputePipelines',
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -07001528 'CreateGraphicsPipelines'
1529 ]
Tobin Ehlis453e91f2016-01-29 14:24:42 -07001530 # TODO : This is hacky, need to make this a more general-purpose solution for all layers
Cody Northrop0a179fe2016-02-24 12:28:41 -07001531 ifdef_dict = {'CreateXcbSurfaceKHR': 'VK_USE_PLATFORM_XCB_KHR',
1532 'CreateAndroidSurfaceKHR': 'VK_USE_PLATFORM_ANDROID_KHR',
Tony Barboure66d4e42016-04-12 13:35:51 -06001533 'CreateWin32SurfaceKHR': 'VK_USE_PLATFORM_WIN32_KHR',
1534 'CreateXlibSurfaceKHR': 'VK_USE_PLATFORM_XLIB_KHR',
1535 'CreateWaylandSurfaceKHR': 'VK_USE_PLATFORM_WAYLAND_KHR',
1536 'CreateMirSurfaceKHR': 'VK_USE_PLATFORM_MIR_KHR'}
Tobin Ehlisd34a4c52015-12-08 10:50:10 -07001537 # Give special treatment to create functions that return multiple new objects
1538 # This dict stores array name and size of array
Jon Ashburnf19916e2016-01-11 13:12:43 -07001539 custom_create_dict = {'pDescriptorSets' : 'pAllocateInfo->descriptorSetCount'}
Courtney Goeltzenleuchter5a0f2832016-02-11 11:44:04 -07001540 pre_call_txt += '%s\n' % (self.lineinfo.get())
Tobin Ehlisd34a4c52015-12-08 10:50:10 -07001541 if proto.name in explicit_object_tracker_functions:
1542 funcs.append('%s%s\n'
1543 '{\n'
1544 ' return explicit_%s;\n'
1545 '}' % (qual, decl, proto.c_call()))
1546 return "".join(funcs)
1547 if True in [create_txt in proto.name for create_txt in ['Create', 'Allocate']]:
1548 create_func = True
1549 last_param_index = -1 # For create funcs don't care if last param is ndo
1550 if True in [destroy_txt in proto.name for destroy_txt in ['Destroy', 'Free']]:
1551 destroy_obj_type = proto.params[-2].ty
1552 if destroy_obj_type in vulkan.object_non_dispatch_list:
1553 destroy_func = True
1554
Tobin Ehlis65f44e42016-01-05 09:46:03 -07001555 # First thing we need to do is gather uses of non-dispatchable-objects (ndos)
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -07001556 (struct_uses, local_decls) = get_object_uses(vulkan.object_non_dispatch_list, proto.params[1:last_param_index])
Tobin Ehlis65f44e42016-01-05 09:46:03 -07001557
Tobin Ehlis10ba1de2016-04-13 12:59:43 -06001558 dispatch_param = proto.params[0].name
1559 if 'CreateInstance' in proto.name:
1560 dispatch_param = '*' + proto.params[1].name
1561 pre_call_txt += '%slayer_data *my_map_data = get_my_data_ptr(get_dispatch_key(%s), layer_data_map);\n' % (indent, dispatch_param)
Tobin Ehlis65f44e42016-01-05 09:46:03 -07001562 if len(struct_uses) > 0:
Mike Stroyan04be7832016-04-07 12:14:30 -06001563 pre_call_txt += '// STRUCT USES:%s\n' % sorted(struct_uses)
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -07001564 if len(local_decls) > 0:
Mike Stroyan04be7832016-04-07 12:14:30 -06001565 pre_call_txt += '//LOCAL DECLS:%s\n' % sorted(local_decls)
Tobin Ehlis65f44e42016-01-05 09:46:03 -07001566 if destroy_func: # only one object
Karl Schultz2d6c90e2016-04-29 17:22:50 -06001567 pre_call_txt += '%sstd::unique_lock<std::mutex> lock(global_lock);\n' % (indent)
Mike Stroyan04be7832016-04-07 12:14:30 -06001568 for del_obj in sorted(struct_uses):
Dustin Gravesa7622d82016-04-14 17:29:20 -06001569 pre_call_txt += '%suint64_t local_%s = reinterpret_cast<uint64_t &>(%s);\n' % (indent, del_obj, del_obj)
Tobin Ehlis10ba1de2016-04-13 12:59:43 -06001570 pre_call_txt += '%s%s = (%s)my_map_data->unique_id_mapping[local_%s];\n' % (indent, del_obj, struct_uses[del_obj], del_obj)
Karl Schultz2d6c90e2016-04-29 17:22:50 -06001571 pre_call_txt += '%slock.unlock();\n' % (indent)
1572 (pre_decl, pre_code, post_code) = ('', '', '')
Tobin Ehlis10ba1de2016-04-13 12:59:43 -06001573 else:
1574 (pre_decl, pre_code, post_code) = self._gen_obj_code(struct_uses, local_decls, ' ', '', 0, set(), True)
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -07001575 # This is a bit hacky but works for now. Need to decl local versions of top-level structs
Mike Stroyan04be7832016-04-07 12:14:30 -06001576 for ld in sorted(local_decls):
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -07001577 init_null_txt = 'NULL';
1578 if '*' not in local_decls[ld]:
1579 init_null_txt = '{}';
1580 if local_decls[ld].strip('*') not in vulkan.object_non_dispatch_list:
1581 pre_decl += ' safe_%s local_%s = %s;\n' % (local_decls[ld], ld, init_null_txt)
Tobin Ehlis10ba1de2016-04-13 12:59:43 -06001582 if pre_code != '': # lock around map uses
1583 pre_code = '%s{\n%sstd::lock_guard<std::mutex> lock(global_lock);\n%s%s}\n' % (indent, indent, pre_code, indent)
Tobin Ehlis65f44e42016-01-05 09:46:03 -07001584 pre_call_txt += '%s%s' % (pre_decl, pre_code)
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -07001585 post_call_txt += '%s' % (post_code)
Tobin Ehlisd34a4c52015-12-08 10:50:10 -07001586 elif create_func:
1587 base_type = proto.params[-1].ty.replace('const ', '').strip('*')
1588 if base_type not in vulkan.object_non_dispatch_list:
1589 return None
1590 else:
1591 return None
1592
1593 ret_val = ''
1594 ret_stmt = ''
1595 if proto.ret != "void":
1596 ret_val = "%s result = " % proto.ret
1597 ret_stmt = " return result;\n"
Tobin Ehlisd34a4c52015-12-08 10:50:10 -07001598 if create_func:
1599 obj_type = proto.params[-1].ty.strip('*')
1600 obj_name = proto.params[-1].name
1601 if obj_type in vulkan.object_non_dispatch_list:
1602 local_name = "unique%s" % obj_type[2:]
1603 post_call_txt += '%sif (VK_SUCCESS == result) {\n' % (indent)
1604 indent += ' '
Tobin Ehlis10ba1de2016-04-13 12:59:43 -06001605 post_call_txt += '%sstd::lock_guard<std::mutex> lock(global_lock);\n' % (indent)
Tobin Ehlisd34a4c52015-12-08 10:50:10 -07001606 if obj_name in custom_create_dict:
1607 post_call_txt += '%s\n' % (self.lineinfo.get())
1608 local_name = '%ss' % (local_name) # add 's' to end for vector of many
Tobin Ehlisd34a4c52015-12-08 10:50:10 -07001609 post_call_txt += '%sfor (uint32_t i=0; i<%s; ++i) {\n' % (indent, custom_create_dict[obj_name])
1610 indent += ' '
Mark Lobodzinskifdf8f472016-04-28 16:36:58 -06001611 post_call_txt += '%suint64_t unique_id = global_unique_id++;\n' % (indent)
Dustin Gravesa7622d82016-04-14 17:29:20 -06001612 post_call_txt += '%smy_map_data->unique_id_mapping[unique_id] = reinterpret_cast<uint64_t &>(%s[i]);\n' % (indent, obj_name)
1613 post_call_txt += '%s%s[i] = reinterpret_cast<%s&>(unique_id);\n' % (indent, obj_name, obj_type)
Tobin Ehlisd34a4c52015-12-08 10:50:10 -07001614 indent = indent[4:]
1615 post_call_txt += '%s}\n' % (indent)
1616 else:
1617 post_call_txt += '%s\n' % (self.lineinfo.get())
Mark Lobodzinskifdf8f472016-04-28 16:36:58 -06001618 post_call_txt += '%suint64_t unique_id = global_unique_id++;\n' % (indent)
Dustin Gravesa7622d82016-04-14 17:29:20 -06001619 post_call_txt += '%smy_map_data->unique_id_mapping[unique_id] = reinterpret_cast<uint64_t &>(*%s);\n' % (indent, obj_name)
1620 post_call_txt += '%s*%s = reinterpret_cast<%s&>(unique_id);\n' % (indent, obj_name, obj_type)
Tobin Ehlisd34a4c52015-12-08 10:50:10 -07001621 indent = indent[4:]
1622 post_call_txt += '%s}\n' % (indent)
1623 elif destroy_func:
1624 del_obj = proto.params[-2].name
1625 if 'count' in del_obj.lower():
1626 post_call_txt += '%s\n' % (self.lineinfo.get())
1627 post_call_txt += '%sfor (uint32_t i=0; i<%s; ++i) {\n' % (indent, del_obj)
1628 del_obj = proto.params[-1].name
1629 indent += ' '
1630 post_call_txt += '%sdelete (VkUniqueObject*)%s[i];\n' % (indent, del_obj)
1631 indent = indent[4:]
1632 post_call_txt += '%s}\n' % (indent)
1633 else:
1634 post_call_txt += '%s\n' % (self.lineinfo.get())
Karl Schultz2d6c90e2016-04-29 17:22:50 -06001635 post_call_txt += '%slock.lock();\n' % (indent)
Tobin Ehlis10ba1de2016-04-13 12:59:43 -06001636 post_call_txt += '%smy_map_data->unique_id_mapping.erase(local_%s);\n' % (indent, proto.params[-2].name)
Tobin Ehlisd34a4c52015-12-08 10:50:10 -07001637
1638 call_sig = proto.c_call()
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -07001639 # Replace default params with any custom local params
1640 for ld in local_decls:
1641 call_sig = call_sig.replace(ld, '(const %s)local_%s' % (local_decls[ld], ld))
Tobin Ehlisd34a4c52015-12-08 10:50:10 -07001642 if proto_is_global(proto):
1643 table_type = "instance"
1644 else:
1645 table_type = "device"
1646 pre_call_txt += '%s\n' % (self.lineinfo.get())
Tobin Ehlis453e91f2016-01-29 14:24:42 -07001647 open_ifdef = ''
1648 close_ifdef = ''
1649 if proto.name in ifdef_dict:
1650 open_ifdef = '#ifdef %s\n' % (ifdef_dict[proto.name])
1651 close_ifdef = '#endif\n'
1652 funcs.append('%s'
1653 '%s%s\n'
Tobin Ehlisd34a4c52015-12-08 10:50:10 -07001654 '{\n'
1655 '%s'
1656 ' %sget_dispatch_table(unique_objects_%s_table_map, %s)->%s;\n'
1657 '%s'
1658 '%s'
Tobin Ehlis453e91f2016-01-29 14:24:42 -07001659 '}\n'
1660 '%s' % (open_ifdef, qual, decl, pre_call_txt, ret_val, table_type, dispatch_param, call_sig, post_call_txt, ret_stmt, close_ifdef))
Tobin Ehlisd34a4c52015-12-08 10:50:10 -07001661 return "\n\n".join(funcs)
1662
1663 def generate_body(self):
1664 self.layer_name = "unique_objects"
1665 extensions=[('wsi_enabled',
1666 ['vkCreateSwapchainKHR',
1667 'vkDestroySwapchainKHR', 'vkGetSwapchainImagesKHR',
1668 'vkAcquireNextImageKHR', 'vkQueuePresentKHR'])]
Petros Bantolas2b40be72016-04-15 11:02:59 +01001669 additional_instance_extensions = [
1670 'vkDestroySurfaceKHR',
1671 'vkGetPhysicalDeviceSurfaceSupportKHR',
1672 'vkGetPhysicalDeviceSurfaceCapabilitiesKHR',
1673 'vkGetPhysicalDeviceSurfaceFormatsKHR',
1674 'vkGetPhysicalDeviceSurfacePresentModesKHR',
1675 'vkCreateDisplayPlaneSurfaceKHR']
Mun, Gwan-gyeongbd4dd592016-02-22 09:43:09 +09001676 if self.wsi == 'Win32':
Tobin Ehlisd34a4c52015-12-08 10:50:10 -07001677 instance_extensions=[('wsi_enabled',
Petros Bantolas2b40be72016-04-15 11:02:59 +01001678 additional_instance_extensions + ['vkCreateWin32SurfaceKHR'])]
Mun, Gwan-gyeongbd4dd592016-02-22 09:43:09 +09001679 elif self.wsi == 'Android':
1680 instance_extensions=[('wsi_enabled',
Petros Bantolas2b40be72016-04-15 11:02:59 +01001681 additional_instance_extensions + ['vkCreateAndroidSurfaceKHR'])]
Karl Schultz9daf7a32016-03-08 15:14:11 -07001682 elif self.wsi == 'Xcb' or self.wsi == 'Xlib' or self.wsi == 'Wayland' or self.wsi == 'Mir':
Tobin Ehlisd34a4c52015-12-08 10:50:10 -07001683 instance_extensions=[('wsi_enabled',
Petros Bantolas2b40be72016-04-15 11:02:59 +01001684 additional_instance_extensions +
1685 ['vkCreateXcbSurfaceKHR',
Karl Schultz9daf7a32016-03-08 15:14:11 -07001686 'vkCreateXlibSurfaceKHR',
1687 'vkCreateWaylandSurfaceKHR',
Mun, Gwan-gyeongbd4dd592016-02-22 09:43:09 +09001688 'vkCreateMirSurfaceKHR'
1689 ])]
Karl Schultz9daf7a32016-03-08 15:14:11 -07001690 else:
1691 print('Error: Undefined DisplayServer')
1692 instance_extensions=[]
1693
Chia-I Wucdb70962016-05-13 14:07:36 +08001694 body = ["namespace %s {" % self.layer_name,
Chia-I Wuc57d5c82016-05-13 14:37:49 +08001695 self._generate_dispatch_entrypoints(),
Chia-I Wub02600c2016-05-20 07:11:22 +08001696 self._generate_layer_introspection_function(),
Tobin Ehlisd34a4c52015-12-08 10:50:10 -07001697 self._generate_layer_gpa_function(extensions,
Chia-I Wu40b8bab2016-05-13 14:06:08 +08001698 instance_extensions),
Chia-I Wucdb70962016-05-13 14:07:36 +08001699 "} // namespace %s" % self.layer_name,
Chia-I Wu40b8bab2016-05-13 14:06:08 +08001700 self._gen_layer_interface_v0_functions()]
Tobin Ehlisd34a4c52015-12-08 10:50:10 -07001701 return "\n\n".join(body)
1702
Tobin Ehlis12076fc2014-10-22 09:06:33 -06001703def main():
Mun, Gwan-gyeongbd4dd592016-02-22 09:43:09 +09001704 wsi = {
1705 "Win32",
1706 "Android",
1707 "Xcb",
1708 "Xlib",
1709 "Wayland",
1710 "Mir",
1711 }
1712
Tobin Ehlis12076fc2014-10-22 09:06:33 -06001713 subcommands = {
Mark Lobodzinski0d054fe2015-12-30 08:16:12 -07001714 "object_tracker" : ObjectTrackerSubcommand,
Tobin Ehlisd34a4c52015-12-08 10:50:10 -07001715 "unique_objects" : UniqueObjectsSubcommand,
Tobin Ehlis12076fc2014-10-22 09:06:33 -06001716 }
1717
Mun, Gwan-gyeongbd4dd592016-02-22 09:43:09 +09001718 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]):
Jamie Madilldbda66b2016-05-10 07:36:20 -07001719 print("Usage: %s <wsi> <subcommand> <input_header> [outdir]" % sys.argv[0])
Tobin Ehlis12076fc2014-10-22 09:06:33 -06001720 print
Tobin Ehlis7e65d752015-01-15 17:51:52 -07001721 print("Available subcommands are: %s" % " ".join(subcommands))
Tobin Ehlis12076fc2014-10-22 09:06:33 -06001722 exit(1)
1723
Mun, Gwan-gyeongbd4dd592016-02-22 09:43:09 +09001724 hfp = vk_helper.HeaderFileParser(sys.argv[3])
Tobin Ehlis14ff0852014-12-17 17:44:50 -07001725 hfp.parse()
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001726 vk_helper.enum_val_dict = hfp.get_enum_val_dict()
1727 vk_helper.enum_type_dict = hfp.get_enum_type_dict()
1728 vk_helper.struct_dict = hfp.get_struct_dict()
1729 vk_helper.typedef_fwd_dict = hfp.get_typedef_fwd_dict()
1730 vk_helper.typedef_rev_dict = hfp.get_typedef_rev_dict()
1731 vk_helper.types_dict = hfp.get_types_dict()
Tobin Ehlis14ff0852014-12-17 17:44:50 -07001732
Jamie Madilldbda66b2016-05-10 07:36:20 -07001733 outfile = None
1734 if len(sys.argv) >= 5:
1735 outfile = sys.argv[4]
1736
1737 subcmd = subcommands[sys.argv[2]](outfile)
Tobin Ehlis12076fc2014-10-22 09:06:33 -06001738 subcmd.run()
1739
1740if __name__ == "__main__":
1741 main()