blob: 015826f4c7b77c8feffeef5e57b0522e0faa162d [file] [log] [blame]
Jon Ashburn5e026df2016-06-15 08:19:07 -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",
47 "CreateXcbSurfaceKHR",
Jon Ashburn00dc7412016-01-07 16:13:06 -070048 "GetPhysicalDeviceXcbPresentationSupportKHR",
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -070049 "CreateXlibSurfaceKHR",
Jon Ashburn00dc7412016-01-07 16:13:06 -070050 "GetPhysicalDeviceXlibPresentationSupportKHR",
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -070051 "CreateWaylandSurfaceKHR",
Jon Ashburn00dc7412016-01-07 16:13:06 -070052 "GetPhysicalDeviceWaylandPresentationSupportKHR",
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -070053 "CreateMirSurfaceKHR",
Jon Ashburn00dc7412016-01-07 16:13:06 -070054 "GetPhysicalDeviceMirPresentationSupportKHR",
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -070055 "CreateAndroidSurfaceKHR",
56 "CreateWin32SurfaceKHR",
Jon Ashburn5e026df2016-06-15 08:19:07 -060057 "GetPhysicalDeviceWin32PresentationSupportKHR",
58 "GetPhysicalDeviceDisplayPropertiesKHR",
59 "GetPhysicalDeviceDisplayPlanePropertiesKHR",
60 "GetDisplayPlaneSupportedDisplaysKHR",
61 "GetDisplayModePropertiesKHR",
62 "CreateDisplayModeKHR",
63 "GetDisplayPlaneCapabilitiesKHR",
64 "CreateDisplayPlaneSurfaceKHR"
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -070065 ]
66 if proto.params[0].ty == "VkInstance" or proto.params[0].ty == "VkPhysicalDevice" or proto.name in global_function_names:
Jon Ashburn95a77ba2015-05-15 15:09:35 -060067 return True
68 else:
69 return False
70
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -070071def wsi_name(ext_name):
72 wsi_prefix = ""
73 if 'Xcb' in ext_name:
74 wsi_prefix = 'XCB'
75 elif 'Xlib' in ext_name:
76 wsi_prefix = 'XLIB'
77 elif 'Win32' in ext_name:
78 wsi_prefix = 'WIN32'
79 elif 'Mir' in ext_name:
80 wsi_prefix = 'MIR'
81 elif 'Wayland' in ext_name:
82 wsi_prefix = 'WAYLAND'
83 elif 'Android' in ext_name:
84 wsi_prefix = 'ANDROID'
85 else:
86 wsi_prefix = ''
87 return wsi_prefix
88
89def wsi_ifdef(ext_name):
90 wsi_prefix = wsi_name(ext_name)
91 if not wsi_prefix:
92 return ''
93 else:
94 return "#ifdef VK_USE_PLATFORM_%s_KHR" % wsi_prefix
95
96def wsi_endif(ext_name):
97 wsi_prefix = wsi_name(ext_name)
98 if not wsi_prefix:
99 return ''
100 else:
101 return "#endif // VK_USE_PLATFORM_%s_KHR" % wsi_prefix
102
Mike Stroyan938c2532015-04-03 13:58:35 -0600103def generate_get_proc_addr_check(name):
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600104 return " if (!%s || %s[0] != 'v' || %s[1] != 'k')\n" \
105 " return NULL;" % ((name,) * 3)
Mike Stroyan938c2532015-04-03 13:58:35 -0600106
Mark Lobodzinski7c75b852015-05-05 15:01:37 -0500107def ucc_to_U_C_C(CamelCase):
108 temp = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', CamelCase)
109 return re.sub('([a-z0-9])([A-Z])', r'\1_\2', temp).upper()
110
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700111# Parse complete struct chain and add any new ndo_uses to the dict
112def gather_object_uses_in_struct(obj_list, struct_type):
113 struct_uses = {}
114 if vk_helper.typedef_rev_dict[struct_type] in vk_helper.struct_dict:
115 struct_type = vk_helper.typedef_rev_dict[struct_type]
116 # Parse elements of this struct param to identify objects and/or arrays of objects
117 for m in sorted(vk_helper.struct_dict[struct_type]):
118 array_len = "%s" % (str(vk_helper.struct_dict[struct_type][m]['array_size']))
119 base_type = vk_helper.struct_dict[struct_type][m]['type']
120 mem_name = vk_helper.struct_dict[struct_type][m]['name']
121 if array_len != '0':
122 mem_name = "%s[%s]" % (mem_name, array_len)
123 if base_type in obj_list:
124 #if array_len not in ndo_uses:
125 # struct_uses[array_len] = []
126 #struct_uses[array_len].append("%s%s,%s" % (name_prefix, struct_name, base_type))
127 struct_uses[mem_name] = base_type
128 elif vk_helper.is_type(base_type, 'struct'):
129 sub_uses = gather_object_uses_in_struct(obj_list, base_type)
130 if len(sub_uses) > 0:
131 struct_uses[mem_name] = sub_uses
132 return struct_uses
133
134# For the given list of object types, Parse the given list of params
135# and return dict of params that use one of the obj_list types
136# Format of the dict is that terminal elements have <name>,<type>
137# non-terminal elements will have <name>[<array_size>]
138# TODO : This analysis could be done up-front at vk_helper time
139def get_object_uses(obj_list, params):
140 obj_uses = {}
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -0700141 local_decls = {}
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700142 param_count = 'NONE' # track params that give array sizes
143 for p in params:
144 base_type = p.ty.replace('const ', '').strip('*')
145 array_len = ''
146 is_ptr = False
147 if 'count' in p.name.lower():
148 param_count = p.name
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -0700149 ptr_txt = ''
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700150 if '*' in p.ty:
151 is_ptr = True
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -0700152 ptr_txt = '*'
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700153 if base_type in obj_list:
154 if is_ptr and 'const' in p.ty and param_count != 'NONE':
155 array_len = "[%s]" % param_count
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -0700156 # Non-arrays we can overwrite in place, but need local decl for arrays
157 local_decls[p.name] = '%s%s' % (base_type, ptr_txt)
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700158 #if array_len not in obj_uses:
159 # obj_uses[array_len] = {}
160 # obj_uses[array_len][p.name] = base_type
161 obj_uses["%s%s" % (p.name, array_len)] = base_type
162 elif vk_helper.is_type(base_type, 'struct'):
163 struct_name = p.name
164 if 'NONE' != param_count:
165 struct_name = "%s[%s]" % (struct_name, param_count)
166 struct_uses = gather_object_uses_in_struct(obj_list, base_type)
167 if len(struct_uses) > 0:
168 obj_uses[struct_name] = struct_uses
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -0700169 # This is a top-level struct w/ uses below it, so need local decl
170 local_decls['%s' % (p.name)] = '%s%s' % (base_type, ptr_txt)
171 return (obj_uses, local_decls)
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700172
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600173class Subcommand(object):
Jamie Madilldbda66b2016-05-10 07:36:20 -0700174 def __init__(self, outfile):
175 self.outfile = outfile
Courtney Goeltzenleuchterf53c3cb2015-04-14 14:55:44 -0600176 self.headers = vulkan.headers
177 self.protos = vulkan.protos
Mike Stroyan3e3a1eb2015-04-03 17:13:23 -0600178 self.no_addr = False
179 self.layer_name = ""
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600180 self.lineinfo = sourcelineinfo()
Mun, Gwan-gyeongbd4dd592016-02-22 09:43:09 +0900181 self.wsi = sys.argv[1]
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600182
183 def run(self):
Jamie Madilldbda66b2016-05-10 07:36:20 -0700184 if self.outfile:
185 with open(self.outfile, "w") as outfile:
186 outfile.write(self.generate())
187 else:
188 print(self.generate())
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600189
190 def generate(self):
191 copyright = self.generate_copyright()
192 header = self.generate_header()
193 body = self.generate_body()
194 footer = self.generate_footer()
195
196 contents = []
197 if copyright:
198 contents.append(copyright)
199 if header:
200 contents.append(header)
201 if body:
202 contents.append(body)
203 if footer:
204 contents.append(footer)
205
206 return "\n\n".join(contents)
207
208 def generate_copyright(self):
209 return """/* THIS FILE IS GENERATED. DO NOT EDIT. */
210
211/*
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -0700212 * Copyright (c) 2015-2016 The Khronos Group Inc.
213 * Copyright (c) 2015-2016 Valve Corporation
214 * Copyright (c) 2015-2016 LunarG, Inc.
Tobin Ehlis10ba1de2016-04-13 12:59:43 -0600215 * Copyright (c) 2015-2016 Google, Inc.
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600216 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -0600217 * Licensed under the Apache License, Version 2.0 (the "License");
218 * you may not use this file except in compliance with the License.
219 * You may obtain a copy of the License at
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600220 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -0600221 * http://www.apache.org/licenses/LICENSE-2.0
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600222 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -0600223 * Unless required by applicable law or agreed to in writing, software
224 * distributed under the License is distributed on an "AS IS" BASIS,
225 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
226 * See the License for the specific language governing permissions and
227 * limitations under the License.
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -0600228 *
Tobin Ehlisd34a4c52015-12-08 10:50:10 -0700229 * Author: Tobin Ehlis <tobine@google.com>
230 * Author: Courtney Goeltzenleuchter <courtneygo@google.com>
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -0600231 * Author: Jon Ashburn <jon@lunarg.com>
232 * Author: Mark Lobodzinski <mark@lunarg.com>
Tobin Ehlisd34a4c52015-12-08 10:50:10 -0700233 * Author: Mike Stroyan <stroyan@google.com>
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -0600234 * Author: Tony Barbour <tony@LunarG.com>
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600235 */"""
236
237 def generate_header(self):
238 return "\n".join(["#include <" + h + ">" for h in self.headers])
239
240 def generate_body(self):
241 pass
242
243 def generate_footer(self):
244 pass
245
246 # Return set of printf '%' qualifier and input to that qualifier
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600247 def _get_printf_params(self, vk_type, name, output_param, cpp=False):
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600248 # TODO : Need ENUM and STRUCT checks here
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600249 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 -0600250 return ("%s", "string_%s(%s)" % (vk_type.replace('const ', '').strip('*'), name))
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600251 if "char*" == vk_type:
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600252 return ("%s", name)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600253 if "uint64" in vk_type:
254 if '*' in vk_type:
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600255 return ("%lu", "*%s" % name)
256 return ("%lu", name)
Tobin Ehlisa30e7e52015-07-06 14:02:36 -0600257 if vk_type.strip('*') in vulkan.object_non_dispatch_list:
258 if '*' in vk_type:
Chia-I Wue2fc5522015-10-26 20:04:44 +0800259 return ("%lu", "%s" % name)
260 return ("%lu", "%s" % name)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600261 if "size" in vk_type:
262 if '*' in vk_type:
Mark Lobodzinskia1456492015-10-06 09:57:52 -0600263 return ("%lu", "(unsigned long)*%s" % name)
264 return ("%lu", "(unsigned long)%s" % name)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600265 if "float" in vk_type:
266 if '[' in vk_type: # handle array, current hard-coded to 4 (TODO: Make this dynamic)
Tobin Ehlis99f88672015-01-10 12:42:41 -0700267 if cpp:
268 return ("[%i, %i, %i, %i]", '"[" << %s[0] << "," << %s[1] << "," << %s[2] << "," << %s[3] << "]"' % (name, name, name, name))
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600269 return ("[%f, %f, %f, %f]", "%s[0], %s[1], %s[2], %s[3]" % (name, name, name, name))
270 return ("%f", name)
Courtney Goeltzenleuchtercd2a0992015-07-09 11:44:38 -0600271 if "bool" in vk_type.lower() or 'xcb_randr_crtc_t' in vk_type:
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600272 return ("%u", name)
Tobin Ehlisb870cbb2015-04-15 07:46:12 -0600273 if True in [t in vk_type.lower() for t in ["int", "flags", "mask", "xcb_window_t"]]:
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600274 if '[' in vk_type: # handle array, current hard-coded to 4 (TODO: Make this dynamic)
Tobin Ehlis99f88672015-01-10 12:42:41 -0700275 if cpp:
276 return ("[%i, %i, %i, %i]", "%s[0] << %s[1] << %s[2] << %s[3]" % (name, name, name, name))
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600277 return ("[%i, %i, %i, %i]", "%s[0], %s[1], %s[2], %s[3]" % (name, name, name, name))
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600278 if '*' in vk_type:
Tobin Ehlisd2b88e82015-02-04 15:15:11 -0700279 if 'pUserData' == name:
280 return ("%i", "((pUserData == 0) ? 0 : *(pUserData))")
Tobin Ehlisc62cb892015-04-17 13:26:33 -0600281 if 'const' in vk_type.lower():
Mark Muelleraab36502016-05-03 13:17:29 -0600282 return ("0x%p", "(void*)(%s)" % name)
Jon Ashburn52f79b52014-12-12 16:10:45 -0700283 return ("%i", "*(%s)" % name)
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600284 return ("%i", name)
Tobin Ehlis3a1cc8d2014-11-11 17:28:22 -0700285 # 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 -0600286 if "VkFormat" == vk_type:
Tobin Ehlis99f88672015-01-10 12:42:41 -0700287 if cpp:
Mark Muelleraab36502016-05-03 13:17:29 -0600288 return ("0x%p", "&%s" % name)
Chia-I Wu1b99bb22015-10-27 19:25:11 +0800289 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 -0700290 if output_param:
Mark Muelleraab36502016-05-03 13:17:29 -0600291 return ("0x%p", "(void*)*%s" % name)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600292 if vk_helper.is_type(vk_type, 'struct') and '*' not in vk_type:
Mark Muelleraab36502016-05-03 13:17:29 -0600293 return ("0x%p", "(void*)(&%s)" % name)
294 return ("0x%p", "(void*)(%s)" % name)
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600295
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600296 def _gen_create_msg_callback(self):
Tobin Ehlise8185062014-12-17 08:01:59 -0700297 r_body = []
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600298 r_body.append('%s' % self.lineinfo.get())
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800299 r_body.append('VKAPI_ATTR VkResult VKAPI_CALL CreateDebugReportCallbackEXT(')
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700300 r_body.append(' VkInstance instance,')
301 r_body.append(' const VkDebugReportCallbackCreateInfoEXT* pCreateInfo,')
302 r_body.append(' const VkAllocationCallbacks* pAllocator,')
303 r_body.append(' VkDebugReportCallbackEXT* pCallback)')
Tobin Ehlise8185062014-12-17 08:01:59 -0700304 r_body.append('{')
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600305 # Switch to this code section for the new per-instance storage and debug callbacks
Mark Lobodzinskif9684e62016-07-21 09:09:35 -0600306 if self.layer_name in ['unique_objects']:
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600307 r_body.append(' VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(%s_instance_table_map, instance);' % self.layer_name )
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700308 r_body.append(' VkResult result = pInstanceTable->CreateDebugReportCallbackEXT(instance, pCreateInfo, pAllocator, pCallback);')
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600309 r_body.append(' if (VK_SUCCESS == result) {')
310 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 -0700311 r_body.append(' result = layer_create_msg_callback(my_data->report_data,')
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600312 r_body.append(' false,')
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700313 r_body.append(' pCreateInfo,')
314 r_body.append(' pAllocator,')
315 r_body.append(' pCallback);')
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600316 r_body.append(' }')
317 r_body.append(' return result;')
318 else:
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700319 r_body.append(' VkResult result = instance_dispatch_table(instance)->CreateDebugReportCallbackEXT(instance, pCreateInfo, pAllocator, pCallback);')
Jon Ashburn3a278b72015-10-06 17:05:21 -0600320 r_body.append(' if (VK_SUCCESS == result) {')
321 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 -0600322 r_body.append(' result = layer_create_msg_callback(my_data->report_data, false, pCreateInfo, pAllocator, pCallback);')
Jon Ashburn3a278b72015-10-06 17:05:21 -0600323 r_body.append(' }')
324 r_body.append(' return result;')
Tobin Ehlise8185062014-12-17 08:01:59 -0700325 r_body.append('}')
326 return "\n".join(r_body)
327
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600328 def _gen_destroy_msg_callback(self):
329 r_body = []
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600330 r_body.append('%s' % self.lineinfo.get())
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800331 r_body.append('VKAPI_ATTR void VKAPI_CALL DestroyDebugReportCallbackEXT(VkInstance instance, VkDebugReportCallbackEXT msgCallback, const VkAllocationCallbacks *pAllocator)')
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600332 r_body.append('{')
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600333 # Switch to this code section for the new per-instance storage and debug callbacks
Mark Lobodzinskif9684e62016-07-21 09:09:35 -0600334 if self.layer_name in ['unique_objects']:
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600335 r_body.append(' VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(%s_instance_table_map, instance);' % self.layer_name )
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600336 else:
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700337 r_body.append(' VkLayerInstanceDispatchTable *pInstanceTable = instance_dispatch_table(instance);')
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700338 r_body.append(' pInstanceTable->DestroyDebugReportCallbackEXT(instance, msgCallback, pAllocator);')
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700339 r_body.append(' layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);')
340 r_body.append(' layer_destroy_msg_callback(my_data->report_data, msgCallback, pAllocator);')
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600341 r_body.append('}')
342 return "\n".join(r_body)
Tobin Ehlise8185062014-12-17 08:01:59 -0700343
Courtney Goeltzenleuchterf0de7242015-12-01 14:10:55 -0700344 def _gen_debug_report_msg(self):
345 r_body = []
346 r_body.append('%s' % self.lineinfo.get())
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800347 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 -0700348 r_body.append('{')
349 # Switch to this code section for the new per-instance storage and debug callbacks
Mark Lobodzinskif9684e62016-07-21 09:09:35 -0600350 r_body.append(' VkLayerInstanceDispatchTable *pInstanceTable = instance_dispatch_table(instance);')
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700351 r_body.append(' pInstanceTable->DebugReportMessageEXT(instance, flags, objType, object, location, msgCode, pLayerPrefix, pMsg);')
Courtney Goeltzenleuchterf0de7242015-12-01 14:10:55 -0700352 r_body.append('}')
353 return "\n".join(r_body)
354
Chia-I Wucdb70962016-05-13 14:07:36 +0800355 def _gen_layer_logging_workaround(self):
356 body = []
357 body.append('%s' % self.lineinfo.get())
358 body.append('// vk_layer_logging.h expects these to be defined')
359 body.append('')
360 body.append('VKAPI_ATTR VkResult VKAPI_CALL')
361 body.append('vkCreateDebugReportCallbackEXT(VkInstance instance, const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,')
362 body.append(' const VkAllocationCallbacks *pAllocator, VkDebugReportCallbackEXT *pMsgCallback) {')
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800363 body.append(' return %s::CreateDebugReportCallbackEXT(instance, pCreateInfo, pAllocator, pMsgCallback);' % self.layer_name)
Chia-I Wucdb70962016-05-13 14:07:36 +0800364 body.append('}')
365 body.append('')
366 body.append('VKAPI_ATTR void VKAPI_CALL vkDestroyDebugReportCallbackEXT(VkInstance instance,')
367 body.append(' VkDebugReportCallbackEXT msgCallback,')
368 body.append(' const VkAllocationCallbacks *pAllocator) {')
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800369 body.append(' %s::DestroyDebugReportCallbackEXT(instance, msgCallback, pAllocator);' % self.layer_name)
Chia-I Wucdb70962016-05-13 14:07:36 +0800370 body.append('}')
371 body.append('')
372 body.append('VKAPI_ATTR void VKAPI_CALL')
373 body.append('vkDebugReportMessageEXT(VkInstance instance, VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT objType, uint64_t object,')
374 body.append(' size_t location, int32_t msgCode, const char *pLayerPrefix, const char *pMsg) {')
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800375 body.append(' %s::DebugReportMessageEXT(instance, flags, objType, object, location, msgCode, pLayerPrefix, pMsg);' % self.layer_name)
Chia-I Wucdb70962016-05-13 14:07:36 +0800376 body.append('}')
377
378 return "\n".join(body)
379
Chia-I Wu40b8bab2016-05-13 14:06:08 +0800380 def _gen_layer_interface_v0_functions(self):
381 body = []
382 body.append('%s' % self.lineinfo.get())
Chia-I Wub02600c2016-05-20 07:11:22 +0800383 body.append('// loader-layer interface v0, just wrappers since there is only a layer')
Chia-I Wu40b8bab2016-05-13 14:06:08 +0800384 body.append('')
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600385
Chia-I Wu40b8bab2016-05-13 14:06:08 +0800386 body.append('VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount, VkExtensionProperties* pProperties)')
387 body.append('{')
Chia-I Wub02600c2016-05-20 07:11:22 +0800388 body.append(' return %s::EnumerateInstanceExtensionProperties(pLayerName, pCount, pProperties);' % self.layer_name)
Chia-I Wu40b8bab2016-05-13 14:06:08 +0800389 body.append('}')
390 body.append('')
391 body.append('VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties(uint32_t *pCount, VkLayerProperties* pProperties)')
392 body.append('{')
Chia-I Wub02600c2016-05-20 07:11:22 +0800393 body.append(' return %s::EnumerateInstanceLayerProperties(pCount, pProperties);' % self.layer_name)
Chia-I Wu40b8bab2016-05-13 14:06:08 +0800394 body.append('}')
395 body.append('')
396 body.append('VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount, VkLayerProperties* pProperties)')
397 body.append('{')
Chia-I Wub02600c2016-05-20 07:11:22 +0800398 body.append(' // the layer command handles VK_NULL_HANDLE just fine internally')
399 body.append(' assert(physicalDevice == VK_NULL_HANDLE);')
400 body.append(' return %s::EnumerateDeviceLayerProperties(VK_NULL_HANDLE, pCount, pProperties);' % self.layer_name)
Chia-I Wu40b8bab2016-05-13 14:06:08 +0800401 body.append('}')
Chia-I Wucdb70962016-05-13 14:07:36 +0800402 body.append('')
403 body.append('VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice dev, const char *funcName)')
404 body.append('{')
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800405 body.append(' return %s::GetDeviceProcAddr(dev, funcName);' % self.layer_name)
Chia-I Wucdb70962016-05-13 14:07:36 +0800406 body.append('}')
407 body.append('')
408 body.append('VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char *funcName)')
409 body.append('{')
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800410 body.append(' return %s::GetInstanceProcAddr(instance, funcName);' % self.layer_name)
Chia-I Wucdb70962016-05-13 14:07:36 +0800411 body.append('}')
Chia-I Wub02600c2016-05-20 07:11:22 +0800412 body.append('')
413 body.append('VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,')
414 body.append(' const char *pLayerName, uint32_t *pCount,')
415 body.append(' VkExtensionProperties *pProperties)')
416 body.append('{')
417 body.append(' // the layer command handles VK_NULL_HANDLE just fine internally')
418 body.append(' assert(physicalDevice == VK_NULL_HANDLE);')
419 body.append(' return %s::EnumerateDeviceExtensionProperties(VK_NULL_HANDLE, pLayerName, pCount, pProperties);' % self.layer_name)
420 body.append('}')
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600421
Chia-I Wu40b8bab2016-05-13 14:06:08 +0800422 return "\n".join(body)
Courtney Goeltzenleuchter3f9f7c42015-07-06 09:11:12 -0600423
Mike Stroyanbf237d72015-04-03 17:45:53 -0600424 def _generate_dispatch_entrypoints(self, qual=""):
Mike Stroyan938c2532015-04-03 13:58:35 -0600425 if qual:
426 qual += " "
427
Mike Stroyan938c2532015-04-03 13:58:35 -0600428 funcs = []
429 intercepted = []
430 for proto in self.protos:
Chia-I Wud579a5d2016-05-16 07:43:38 +0800431 if proto.name in ["EnumerateInstanceExtensionProperties",
Chia-I Wu40b8bab2016-05-13 14:06:08 +0800432 "EnumerateInstanceLayerProperties",
Chia-I Wub02600c2016-05-20 07:11:22 +0800433 "EnumerateDeviceLayerProperties",
434 "GetDeviceProcAddr",
435 "GetInstanceProcAddr"]:
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800436 funcs.append(proto.c_func(attr="VKAPI") + ';')
Chia-I Wu2985b142016-05-16 12:27:03 +0800437 intercepted.append(proto)
Mike Stroyan70c05e82015-04-08 10:27:43 -0600438 else:
Mike Stroyan3e3a1eb2015-04-03 17:13:23 -0600439 intercept = self.generate_intercept(proto, qual)
Mike Stroyan938c2532015-04-03 13:58:35 -0600440 if intercept is None:
441 # fill in default intercept for certain entrypoints
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700442 if 'CreateDebugReportCallbackEXT' == proto.name:
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600443 intercept = self._gen_layer_dbg_create_msg_callback()
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700444 elif 'DestroyDebugReportCallbackEXT' == proto.name:
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600445 intercept = self._gen_layer_dbg_destroy_msg_callback()
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700446 elif 'DebugReportMessageEXT' == proto.name:
Courtney Goeltzenleuchterf0de7242015-12-01 14:10:55 -0700447 intercept = self._gen_debug_report_msg()
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600448 elif 'CreateDevice' == proto.name:
449 funcs.append('/* CreateDevice HERE */')
Tony Barbour59a47322015-06-24 16:06:58 -0600450
Mike Stroyan938c2532015-04-03 13:58:35 -0600451 if intercept is not None:
452 funcs.append(intercept)
Ian Elliott7e40db92015-08-21 15:09:33 -0600453 if not "KHR" in proto.name:
Jon Ashburn747f2b62015-06-18 15:02:58 -0600454 intercepted.append(proto)
Mike Stroyan938c2532015-04-03 13:58:35 -0600455
Chia-I Wu2985b142016-05-16 12:27:03 +0800456 instance_lookups = []
457 device_lookups = []
Mike Stroyan938c2532015-04-03 13:58:35 -0600458 for proto in intercepted:
Chia-I Wu2985b142016-05-16 12:27:03 +0800459 if proto_is_global(proto):
460 instance_lookups.append("if (!strcmp(name, \"%s\"))" % proto.name)
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800461 instance_lookups.append(" return (PFN_vkVoidFunction) %s;" % (proto.name))
Chia-I Wu2985b142016-05-16 12:27:03 +0800462 else:
463 device_lookups.append("if (!strcmp(name, \"%s\"))" % proto.name)
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800464 device_lookups.append(" return (PFN_vkVoidFunction) %s;" % (proto.name))
Mike Stroyan938c2532015-04-03 13:58:35 -0600465
Chia-I Wu2985b142016-05-16 12:27:03 +0800466 # add customized intercept_core_device_command
Mike Stroyan938c2532015-04-03 13:58:35 -0600467 body = []
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600468 body.append('%s' % self.lineinfo.get())
Chia-I Wu2985b142016-05-16 12:27:03 +0800469 body.append("static inline PFN_vkVoidFunction intercept_core_device_command(const char *name)")
Mike Stroyan938c2532015-04-03 13:58:35 -0600470 body.append("{")
471 body.append(generate_get_proc_addr_check("name"))
472 body.append("")
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600473 body.append(" name += 2;")
Chia-I Wu2985b142016-05-16 12:27:03 +0800474 body.append(" %s" % "\n ".join(device_lookups))
Mike Stroyan938c2532015-04-03 13:58:35 -0600475 body.append("")
476 body.append(" return NULL;")
477 body.append("}")
Chia-I Wu2985b142016-05-16 12:27:03 +0800478 # add intercept_core_instance_command
479 body.append("static inline PFN_vkVoidFunction intercept_core_instance_command(const char *name)")
Jon Ashburnf6b33db2015-05-05 14:22:52 -0600480 body.append("{")
481 body.append(generate_get_proc_addr_check("name"))
482 body.append("")
483 body.append(" name += 2;")
Chia-I Wu2985b142016-05-16 12:27:03 +0800484 body.append(" %s" % "\n ".join(instance_lookups))
Jon Ashburnf6b33db2015-05-05 14:22:52 -0600485 body.append("")
486 body.append(" return NULL;")
487 body.append("}")
488
Mike Stroyan938c2532015-04-03 13:58:35 -0600489 funcs.append("\n".join(body))
Mike Stroyan938c2532015-04-03 13:58:35 -0600490 return "\n\n".join(funcs)
491
Tobin Ehlisca915872014-11-18 11:28:33 -0700492 def _generate_extensions(self):
493 exts = []
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600494 exts.append('%s' % self.lineinfo.get())
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600495 exts.append(self._gen_create_msg_callback())
496 exts.append(self._gen_destroy_msg_callback())
Courtney Goeltzenleuchterf0de7242015-12-01 14:10:55 -0700497 exts.append(self._gen_debug_report_msg())
Tobin Ehlisb870cbb2015-04-15 07:46:12 -0600498 return "\n".join(exts)
499
Chia-I Wub02600c2016-05-20 07:11:22 +0800500 def _generate_layer_introspection_function(self):
501 body = []
502 body.append('%s' % self.lineinfo.get())
Mark Lobodzinskif9684e62016-07-21 09:09:35 -0600503 body.append('static const VkLayerProperties globalLayerProps = {')
504 body.append(' "VK_LAYER_GOOGLE_%s",' % self.layer_name)
505 body.append(' VK_LAYER_API_VERSION, // specVersion')
506 body.append(' 1, // implementationVersion')
507 body.append(' "Google Validation Layer"')
508 body.append('};')
509 body.append('')
Chia-I Wub02600c2016-05-20 07:11:22 +0800510
511 body.append('VKAPI_ATTR VkResult VKAPI_CALL EnumerateInstanceLayerProperties(uint32_t *pCount, VkLayerProperties* pProperties)')
512 body.append('{')
513 body.append(' return util_GetLayerProperties(1, &globalLayerProps, pCount, pProperties);')
514 body.append('}')
515 body.append('')
516 body.append('VKAPI_ATTR VkResult VKAPI_CALL EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount, VkLayerProperties* pProperties)')
517 body.append('{')
518 body.append(' return util_GetLayerProperties(1, &globalLayerProps, pCount, pProperties);')
519 body.append('}')
520 body.append('')
521 body.append('VKAPI_ATTR VkResult VKAPI_CALL EnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pCount, VkExtensionProperties* pProperties)')
522 body.append('{')
523 body.append(' if (pLayerName && !strcmp(pLayerName, globalLayerProps.layerName))')
Mark Lobodzinskif9684e62016-07-21 09:09:35 -0600524 body.append(' return util_GetExtensionProperties(0, NULL, pCount, pProperties);')
Chia-I Wub02600c2016-05-20 07:11:22 +0800525 body.append('')
526 body.append(' return VK_ERROR_LAYER_NOT_PRESENT;')
527 body.append('}')
528 body.append('')
529 body.append('VKAPI_ATTR VkResult VKAPI_CALL EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,')
530 body.append(' const char *pLayerName, uint32_t *pCount,')
531 body.append(' VkExtensionProperties *pProperties)')
532 body.append('{')
533 body.append(' if (pLayerName && !strcmp(pLayerName, globalLayerProps.layerName))')
534 body.append(' return util_GetExtensionProperties(0, nullptr, pCount, pProperties);')
535 body.append('')
536 body.append(' assert(physicalDevice);')
537 body.append(' VkLayerInstanceDispatchTable* pTable = get_dispatch_table(%s_instance_table_map, physicalDevice);' % self.layer_name)
538 body.append(' return pTable->EnumerateDeviceExtensionProperties(physicalDevice, NULL, pCount, pProperties);')
539 body.append('}')
540
541 return "\n".join(body)
542
Jon Ashburnf6b33db2015-05-05 14:22:52 -0600543 def _generate_layer_gpa_function(self, extensions=[], instance_extensions=[]):
Jon Ashburnbacb0f52015-04-06 10:58:22 -0600544 func_body = []
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600545#
Courtney Goeltzenleuchter3f9f7c42015-07-06 09:11:12 -0600546# New style of GPA Functions for the new layer_data/layer_logging changes
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600547#
Mark Lobodzinskif9684e62016-07-21 09:09:35 -0600548 if self.layer_name in ['unique_objects']:
Chia-I Wufcf7eb92016-05-16 13:01:39 +0800549 for ext_enable, ext_list in extensions:
550 func_body.append('%s' % self.lineinfo.get())
551 func_body.append('static inline PFN_vkVoidFunction intercept_%s_command(const char *name, VkDevice dev)' % ext_enable)
552 func_body.append('{')
Chia-I Wu5b2f0572016-05-17 07:43:59 +0800553 func_body.append(' if (dev) {')
554 func_body.append(' layer_data *my_data = get_my_data_ptr(get_dispatch_key(dev), layer_data_map);')
555 func_body.append(' if (!my_data->%s)' % ext_enable)
556 func_body.append(' return nullptr;')
557 func_body.append(' }\n')
Chia-I Wufcf7eb92016-05-16 13:01:39 +0800558
559 for ext_name in ext_list:
560 func_body.append(' if (!strcmp("%s", name))\n'
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800561 ' return reinterpret_cast<PFN_vkVoidFunction>(%s);' % (ext_name, ext_name[2:]))
Chia-I Wufcf7eb92016-05-16 13:01:39 +0800562 func_body.append('\n return nullptr;')
563 func_body.append('}\n')
564
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800565 func_body.append("VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetDeviceProcAddr(VkDevice device, const char* funcName)\n"
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600566 "{\n"
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -0600567 " PFN_vkVoidFunction addr;\n"
Chia-I Wu2985b142016-05-16 12:27:03 +0800568 " addr = intercept_core_device_command(funcName);\n"
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600569 " if (addr)\n"
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700570 " return addr;\n"
Chia-I Wuf5b039d2016-05-17 07:39:31 +0800571 " assert(device);\n")
Chia-I Wufcf7eb92016-05-16 13:01:39 +0800572 for ext_enable, _ in extensions:
573 func_body.append(' addr = intercept_%s_command(funcName, device);' % ext_enable)
574 func_body.append(' if (addr)\n'
575 ' return addr;')
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600576 func_body.append("\n if (get_dispatch_table(%s_device_table_map, device)->GetDeviceProcAddr == NULL)\n"
577 " return NULL;\n"
578 " return get_dispatch_table(%s_device_table_map, device)->GetDeviceProcAddr(device, funcName);\n"
579 "}\n" % (self.layer_name, self.layer_name))
Chia-I Wufcf7eb92016-05-16 13:01:39 +0800580
Mark Lobodzinskife1f0662016-06-24 09:57:32 -0600581 # The WSI-related extensions have independent extension enables
582 wsi_sub_enables = {'WIN32': 'win32_enabled',
583 'XLIB': 'xlib_enabled',
584 'XCB': 'xcb_enabled',
585 'MIR': 'mir_enabled',
586 'WAYLAND': 'wayland_enabled',
587 'ANDROID': 'android_enabled'}
588
Chia-I Wufcf7eb92016-05-16 13:01:39 +0800589 for ext_enable, ext_list in instance_extensions:
590 func_body.append('%s' % self.lineinfo.get())
591 func_body.append('static inline PFN_vkVoidFunction intercept_%s_command(const char *name, VkInstance instance)' % ext_enable)
592 func_body.append('{')
593 if ext_enable == 'msg_callback_get_proc_addr':
594 func_body.append(" layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);\n"
595 " return debug_report_get_instance_proc_addr(my_data->report_data, name);")
596 else:
597 func_body.append(" VkLayerInstanceDispatchTable* pTable = get_dispatch_table(%s_instance_table_map, instance);" % self.layer_name)
598 func_body.append(' if (instanceExtMap.size() == 0 || !instanceExtMap[pTable].%s)' % ext_enable)
599 func_body.append(' return nullptr;\n')
600
601 for ext_name in ext_list:
602 if wsi_name(ext_name):
603 func_body.append('%s' % wsi_ifdef(ext_name))
Mark Lobodzinskife1f0662016-06-24 09:57:32 -0600604 if wsi_sub_enables[wsi_name(ext_name)]:
605 func_body.append(' if ((instanceExtMap[pTable].%s == true) && !strcmp("%s", name))\n'
606 ' return reinterpret_cast<PFN_vkVoidFunction>(%s);' % (wsi_sub_enables[wsi_name(ext_name)], ext_name, ext_name[2:]))
607 else:
608 func_body.append(' if (!strcmp("%s", name))\n'
609 ' return reinterpret_cast<PFN_vkVoidFunction>(%s);' % (ext_name, ext_name[2:]))
Chia-I Wufcf7eb92016-05-16 13:01:39 +0800610 if wsi_name(ext_name):
611 func_body.append('%s' % wsi_endif(ext_name))
612
613 func_body.append('\n return nullptr;')
614 func_body.append('}\n')
615
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800616 func_body.append("VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetInstanceProcAddr(VkInstance instance, const char* funcName)\n"
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600617 "{\n"
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -0600618 " PFN_vkVoidFunction addr;\n"
Chia-I Wu2985b142016-05-16 12:27:03 +0800619 " addr = intercept_core_instance_command(funcName);\n"
Chia-I Wu5b2f0572016-05-17 07:43:59 +0800620 " if (!addr) {\n"
621 " addr = intercept_core_device_command(funcName);\n"
622 " }")
623
624 for ext_enable, _ in extensions:
625 func_body.append(" if (!addr) {\n"
626 " addr = intercept_%s_command(funcName, VkDevice(VK_NULL_HANDLE));\n"
627 " }" % ext_enable)
628
629 func_body.append(" if (addr) {\n"
Chia-I Wufcf7eb92016-05-16 13:01:39 +0800630 " return addr;\n"
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700631 " }\n"
Chia-I Wuf5b039d2016-05-17 07:39:31 +0800632 " assert(instance);\n"
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700633 )
Jon Ashburnbacb0f52015-04-06 10:58:22 -0600634
Chia-I Wufcf7eb92016-05-16 13:01:39 +0800635 for ext_enable, _ in instance_extensions:
636 func_body.append(' addr = intercept_%s_command(funcName, instance);' % ext_enable)
637 func_body.append(' if (addr)\n'
638 ' return addr;\n')
Jon Ashburn3dc39382015-09-17 10:00:32 -0600639
640 func_body.append(" if (get_dispatch_table(%s_instance_table_map, instance)->GetInstanceProcAddr == NULL) {\n"
641 " return NULL;\n"
642 " }\n"
643 " return get_dispatch_table(%s_instance_table_map, instance)->GetInstanceProcAddr(instance, funcName);\n"
644 "}\n" % (self.layer_name, self.layer_name))
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600645 return "\n".join(func_body)
646 else:
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600647 func_body.append('%s' % self.lineinfo.get())
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800648 func_body.append("VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetDeviceProcAddr(VkDevice device, const char* funcName)\n"
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600649 "{\n"
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700650 " PFN_vkVoidFunction addr;\n")
Jon Ashburn1f32a442016-02-02 13:13:01 -0700651 func_body.append("\n"
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600652 " loader_platform_thread_once(&initOnce, init%s);\n\n"
Chia-I Wu2985b142016-05-16 12:27:03 +0800653 " addr = intercept_core_device_command(funcName);\n"
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600654 " if (addr)\n"
655 " return addr;" % self.layer_name)
Chia-I Wuf5b039d2016-05-17 07:39:31 +0800656 func_body.append(" assert(device);\n")
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600657 func_body.append('')
658 func_body.append(' VkLayerDispatchTable *pDisp = device_dispatch_table(device);')
659 if 0 != len(extensions):
660 extra_space = ""
661 for (ext_enable, ext_list) in extensions:
662 if 0 != len(ext_enable):
Jon Ashburn8acd2332015-09-16 18:08:32 -0600663 func_body.append(' if (deviceExtMap.size() != 0 && deviceExtMap[pDisp].%s)' % ext_enable)
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600664 func_body.append(' {')
665 extra_space = " "
666 for ext_name in ext_list:
667 func_body.append(' %sif (!strcmp("%s", funcName))\n'
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -0600668 ' return reinterpret_cast<PFN_vkVoidFunction>(%s);' % (extra_space, ext_name, ext_name))
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600669 if 0 != len(ext_enable):
670 func_body.append(' }')
671 func_body.append('%s' % self.lineinfo.get())
672 func_body.append(" {\n"
673 " if (pDisp->GetDeviceProcAddr == NULL)\n"
674 " return NULL;\n"
675 " return pDisp->GetDeviceProcAddr(device, funcName);\n"
676 " }\n"
677 "}\n")
Jon Ashburn3dc39382015-09-17 10:00:32 -0600678 func_body.append('%s' % self.lineinfo.get())
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800679 func_body.append("VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetInstanceProcAddr(VkInstance instance, const char* funcName)\n"
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600680 "{\n"
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -0600681 " PFN_vkVoidFunction addr;\n"
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700682 )
Jon Ashburn1f32a442016-02-02 13:13:01 -0700683 func_body.append(
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600684 " loader_platform_thread_once(&initOnce, init%s);\n\n"
Chia-I Wu2985b142016-05-16 12:27:03 +0800685 " addr = intercept_core_instance_command(funcName);\n"
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600686 " if (addr)\n"
687 " return addr;" % self.layer_name)
Chia-I Wuf5b039d2016-05-17 07:39:31 +0800688 func_body.append(" assert(instance);\n")
Jon Ashburn3dc39382015-09-17 10:00:32 -0600689 func_body.append("")
Courtney Goeltzenleuchter00150eb2016-01-08 12:18:43 -0700690 func_body.append(" VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance);\n")
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600691 if 0 != len(instance_extensions):
Jon Ashburn3dc39382015-09-17 10:00:32 -0600692 extra_space = ""
693 for (ext_enable, ext_list) in instance_extensions:
694 if 0 != len(ext_enable):
Jon Ashburn3a278b72015-10-06 17:05:21 -0600695 if ext_enable == 'msg_callback_get_proc_addr':
696 func_body.append(" layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);\n"
697 " addr = debug_report_get_instance_proc_addr(my_data->report_data, funcName);\n"
698 " if (addr) {\n"
699 " return addr;\n"
700 " }\n")
701 else:
702 func_body.append(' if (instanceExtMap.size() != 0 && instanceExtMap[pTable].%s)' % ext_enable)
703 func_body.append(' {')
704 extra_space = " "
705 for ext_name in ext_list:
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -0700706 if wsi_name(ext_name):
707 func_body.append('%s' % wsi_ifdef(ext_name))
Jon Ashburn3a278b72015-10-06 17:05:21 -0600708 func_body.append(' %sif (!strcmp("%s", funcName))\n'
Jon Ashburn3dc39382015-09-17 10:00:32 -0600709 ' return reinterpret_cast<PFN_vkVoidFunction>(%s);' % (extra_space, ext_name, ext_name))
Mark Lobodzinskia8a5f852015-12-10 16:25:21 -0700710 if wsi_name(ext_name):
711 func_body.append('%s' % wsi_endif(ext_name))
Jon Ashburn3a278b72015-10-06 17:05:21 -0600712 if 0 != len(ext_enable):
713 func_body.append(' }\n')
Jon Ashburn3dc39382015-09-17 10:00:32 -0600714
715 func_body.append(" if (pTable->GetInstanceProcAddr == NULL)\n"
Mark Lobodzinskifae78852015-06-23 11:35:12 -0600716 " return NULL;\n"
717 " return pTable->GetInstanceProcAddr(instance, funcName);\n"
718 "}\n")
719 return "\n".join(func_body)
Jon Ashburnf6b33db2015-05-05 14:22:52 -0600720
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600721
Mike Stroyaned238bb2015-05-15 08:50:57 -0600722 def _generate_layer_initialization(self, init_opts=False, prefix='vk', lockname=None, condname=None):
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600723 func_body = ["#include \"vk_dispatch_table_helper.h\""]
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600724 func_body.append('%s' % self.lineinfo.get())
Mark Lobodzinski0d054fe2015-12-30 08:16:12 -0700725 func_body.append('static void init_%s(layer_data *my_data, const VkAllocationCallbacks *pAllocator)\n'
Mike Stroyan3e3a1eb2015-04-03 17:13:23 -0600726 '{\n' % self.layer_name)
Jon Ashburnd6badbc2015-02-16 08:26:50 -0700727 if init_opts:
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600728 func_body.append('%s' % self.lineinfo.get())
Jon Ashburnd6badbc2015-02-16 08:26:50 -0700729 func_body.append('')
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -0600730 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 -0600731 func_body.append('')
732 if lockname is not None:
733 func_body.append('%s' % self.lineinfo.get())
734 func_body.append(" if (!%sLockInitialized)" % lockname)
735 func_body.append(" {")
736 func_body.append(" // TODO/TBD: Need to delete this mutex sometime. How???")
737 func_body.append(" loader_platform_thread_create_mutex(&%sLock);" % lockname)
738 if condname is not None:
739 func_body.append(" loader_platform_thread_init_cond(&%sCond);" % condname)
740 func_body.append(" %sLockInitialized = 1;" % lockname)
741 func_body.append(" }")
742 func_body.append("}\n")
743 func_body.append('')
744 return "\n".join(func_body)
745
Tobin Ehlisd34a4c52015-12-08 10:50:10 -0700746class UniqueObjectsSubcommand(Subcommand):
747 def generate_header(self):
748 header_txt = []
749 header_txt.append('%s' % self.lineinfo.get())
750 header_txt.append('#include "unique_objects.h"')
Tobin Ehlisd34a4c52015-12-08 10:50:10 -0700751 return "\n".join(header_txt)
752
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700753 # Generate UniqueObjects code for given struct_uses dict of objects that need to be unwrapped
Tobin Ehlisa39c26a2016-01-05 16:34:59 -0700754 # vector_name_set is used to make sure we don't replicate vector names
755 # 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 -0700756 # TODO : Comment this code
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -0700757 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 -0700758 decls = ''
759 pre_code = ''
760 post_code = ''
Tobin Ehlisa39c26a2016-01-05 16:34:59 -0700761 for obj in sorted(struct_uses):
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700762 name = obj
763 array = ''
764 if '[' in obj:
765 (name, array) = obj.split('[')
766 array = array.strip(']')
Tobin Ehlisa39c26a2016-01-05 16:34:59 -0700767 ptr_type = False
Tobin Ehlis10ba1de2016-04-13 12:59:43 -0600768 if 'p' == obj[0] and obj[1] != obj[1].lower(): # TODO : Not ideal way to determine ptr
Tobin Ehlisa39c26a2016-01-05 16:34:59 -0700769 ptr_type = True
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700770 if isinstance(struct_uses[obj], dict):
771 local_prefix = ''
772 name = '%s%s' % (prefix, name)
Tobin Ehlisa39c26a2016-01-05 16:34:59 -0700773 if ptr_type:
Tobin Ehlis6dd0fc32016-02-12 14:37:09 -0700774 if first_level_param and name in param_type:
775 pre_code += '%sif (%s) {\n' % (indent, name)
776 else: # shadow ptr will have been initialized at this point so check it vs. source ptr
777 pre_code += '%sif (local_%s) {\n' % (indent, name)
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700778 indent += ' '
779 if array != '':
Jon Ashburn5e026df2016-06-15 08:19:07 -0600780 if 'p' == array[0] and array[1] != array[1].lower(): # TODO : Not ideal way to determine ptr
781 count_prefix = '*'
782 else:
783 count_prefix = ''
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700784 idx = 'idx%s' % str(array_index)
785 array_index += 1
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -0700786 if first_level_param and name in param_type:
Jon Ashburn5e026df2016-06-15 08:19:07 -0600787 pre_code += '%slocal_%s = new safe_%s[%s%s];\n' % (indent, name, param_type[name].strip('*'), count_prefix, array)
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -0700788 post_code += ' if (local_%s)\n' % (name)
789 post_code += ' delete[] local_%s;\n' % (name)
Jon Ashburn5e026df2016-06-15 08:19:07 -0600790 pre_code += '%sfor (uint32_t %s=0; %s<%s%s%s; ++%s) {\n' % (indent, idx, idx, count_prefix, prefix, array, idx)
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700791 indent += ' '
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -0700792 if first_level_param:
793 pre_code += '%slocal_%s[%s].initialize(&%s[%s]);\n' % (indent, name, idx, name, idx)
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700794 local_prefix = '%s[%s].' % (name, idx)
795 elif ptr_type:
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -0700796 if first_level_param and name in param_type:
797 pre_code += '%slocal_%s = new safe_%s(%s);\n' % (indent, name, param_type[name].strip('*'), name)
798 post_code += ' if (local_%s)\n' % (name)
799 post_code += ' delete local_%s;\n' % (name)
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700800 local_prefix = '%s->' % (name)
801 else:
802 local_prefix = '%s.' % (name)
803 assert isinstance(decls, object)
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -0700804 (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 -0700805 decls += tmp_decl
806 pre_code += tmp_pre
807 post_code += tmp_post
808 if array != '':
809 indent = indent[4:]
810 pre_code += '%s}\n' % (indent)
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700811 if ptr_type:
812 indent = indent[4:]
813 pre_code += '%s}\n' % (indent)
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700814 else:
Tobin Ehlisa39c26a2016-01-05 16:34:59 -0700815 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 -0700816 if first_level_param:
Tobin Ehlis10ba1de2016-04-13 12:59:43 -0600817 decls += '%s%s* local_%s = NULL;\n' % (indent, struct_uses[obj], name)
Tobin Ehlis6dd0fc32016-02-12 14:37:09 -0700818 if array != '' and not first_level_param: # ptrs under structs will have been initialized so use local_*
819 pre_code += '%sif (local_%s%s) {\n' %(indent, prefix, name)
820 else:
821 pre_code += '%sif (%s%s) {\n' %(indent, prefix, name)
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700822 indent += ' '
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700823 if array != '':
824 idx = 'idx%s' % str(array_index)
825 array_index += 1
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -0700826 if first_level_param:
827 pre_code += '%slocal_%s = new %s[%s];\n' % (indent, name, struct_uses[obj], array)
828 post_code += ' if (local_%s)\n' % (name)
829 post_code += ' delete[] local_%s;\n' % (name)
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700830 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 -0700831 indent += ' '
832 name = '%s[%s]' % (name, idx)
Tobin Ehlisa39c26a2016-01-05 16:34:59 -0700833 if name not in vector_name_set:
834 vector_name_set.add(name)
Dustin Gravesa7622d82016-04-14 17:29:20 -0600835 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 -0700836 if array != '':
837 indent = indent[4:]
838 pre_code += '%s}\n' % (indent)
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700839 indent = indent[4:]
840 pre_code += '%s}\n' % (indent)
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700841 else:
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -0700842 pre_code += '%s\n' % (self.lineinfo.get())
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -0700843 if '->' in prefix: # need to update local struct
Dustin Gravesa7622d82016-04-14 17:29:20 -0600844 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 -0700845 else:
Dustin Gravesa7622d82016-04-14 17:29:20 -0600846 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 -0700847 return decls, pre_code, post_code
Tobin Ehlisd34a4c52015-12-08 10:50:10 -0700848
849 def generate_intercept(self, proto, qual):
850 create_func = False
851 destroy_func = False
852 last_param_index = None #typcially we look at all params for ndos
853 pre_call_txt = '' # code prior to calling down chain such as unwrap uses of ndos
854 post_call_txt = '' # code following call down chain such to wrap newly created ndos, or destroy local wrap struct
855 funcs = []
856 indent = ' ' # indent level for generated code
Chia-I Wuc57d5c82016-05-13 14:37:49 +0800857 decl = proto.c_func(attr="VKAPI")
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700858 # A few API cases that are manual code
Tobin Ehlisa39c26a2016-01-05 16:34:59 -0700859 # TODO : Special case Create*Pipelines funcs to handle creating multiple unique objects
Jon Ashburn5e026df2016-06-15 08:19:07 -0600860 explicit_unique_objects_functions = ['GetSwapchainImagesKHR',
Tobin Ehlis10ba1de2016-04-13 12:59:43 -0600861 'CreateSwapchainKHR',
Tobin Ehlisd34a4c52015-12-08 10:50:10 -0700862 'CreateInstance',
Tobin Ehlis10ba1de2016-04-13 12:59:43 -0600863 'DestroyInstance',
Tobin Ehlisa39c26a2016-01-05 16:34:59 -0700864 'CreateDevice',
Tobin Ehlis10ba1de2016-04-13 12:59:43 -0600865 'DestroyDevice',
Dustin Graves176f9df2016-07-14 17:28:11 -0600866 'AllocateMemory',
Tobin Ehlisa39c26a2016-01-05 16:34:59 -0700867 'CreateComputePipelines',
Jon Ashburn5e026df2016-06-15 08:19:07 -0600868 'CreateGraphicsPipelines',
Jon Ashburn73a34352016-06-29 16:12:51 -0600869 'GetPhysicalDeviceDisplayPropertiesKHR',
Jon Ashburn32e042d2016-06-28 14:46:12 -0600870 'GetDisplayPlaneSupportedDisplaysKHR',
871 'GetDisplayModePropertiesKHR'
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -0700872 ]
Tobin Ehlis453e91f2016-01-29 14:24:42 -0700873 # TODO : This is hacky, need to make this a more general-purpose solution for all layers
Cody Northrop0a179fe2016-02-24 12:28:41 -0700874 ifdef_dict = {'CreateXcbSurfaceKHR': 'VK_USE_PLATFORM_XCB_KHR',
875 'CreateAndroidSurfaceKHR': 'VK_USE_PLATFORM_ANDROID_KHR',
Tony Barboure66d4e42016-04-12 13:35:51 -0600876 'CreateWin32SurfaceKHR': 'VK_USE_PLATFORM_WIN32_KHR',
877 'CreateXlibSurfaceKHR': 'VK_USE_PLATFORM_XLIB_KHR',
878 'CreateWaylandSurfaceKHR': 'VK_USE_PLATFORM_WAYLAND_KHR',
879 'CreateMirSurfaceKHR': 'VK_USE_PLATFORM_MIR_KHR'}
Tobin Ehlisd34a4c52015-12-08 10:50:10 -0700880 # Give special treatment to create functions that return multiple new objects
881 # This dict stores array name and size of array
Jon Ashburnf19916e2016-01-11 13:12:43 -0700882 custom_create_dict = {'pDescriptorSets' : 'pAllocateInfo->descriptorSetCount'}
Courtney Goeltzenleuchter5a0f2832016-02-11 11:44:04 -0700883 pre_call_txt += '%s\n' % (self.lineinfo.get())
Jon Ashburn5e026df2016-06-15 08:19:07 -0600884 if proto.name in explicit_unique_objects_functions:
Tobin Ehlisd34a4c52015-12-08 10:50:10 -0700885 funcs.append('%s%s\n'
886 '{\n'
887 ' return explicit_%s;\n'
888 '}' % (qual, decl, proto.c_call()))
889 return "".join(funcs)
890 if True in [create_txt in proto.name for create_txt in ['Create', 'Allocate']]:
891 create_func = True
892 last_param_index = -1 # For create funcs don't care if last param is ndo
893 if True in [destroy_txt in proto.name for destroy_txt in ['Destroy', 'Free']]:
894 destroy_obj_type = proto.params[-2].ty
895 if destroy_obj_type in vulkan.object_non_dispatch_list:
896 destroy_func = True
897
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700898 # First thing we need to do is gather uses of non-dispatchable-objects (ndos)
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -0700899 (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 -0700900
Tobin Ehlis10ba1de2016-04-13 12:59:43 -0600901 dispatch_param = proto.params[0].name
902 if 'CreateInstance' in proto.name:
903 dispatch_param = '*' + proto.params[1].name
904 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 -0700905 if len(struct_uses) > 0:
Mike Stroyan04be7832016-04-07 12:14:30 -0600906 pre_call_txt += '// STRUCT USES:%s\n' % sorted(struct_uses)
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -0700907 if len(local_decls) > 0:
Mike Stroyan04be7832016-04-07 12:14:30 -0600908 pre_call_txt += '//LOCAL DECLS:%s\n' % sorted(local_decls)
Tobin Ehlis65f44e42016-01-05 09:46:03 -0700909 if destroy_func: # only one object
Karl Schultz2d6c90e2016-04-29 17:22:50 -0600910 pre_call_txt += '%sstd::unique_lock<std::mutex> lock(global_lock);\n' % (indent)
Mike Stroyan04be7832016-04-07 12:14:30 -0600911 for del_obj in sorted(struct_uses):
Dustin Gravesa7622d82016-04-14 17:29:20 -0600912 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 -0600913 pre_call_txt += '%s%s = (%s)my_map_data->unique_id_mapping[local_%s];\n' % (indent, del_obj, struct_uses[del_obj], del_obj)
Tobin Ehlis4263cd12016-07-14 14:37:51 -0600914 pre_call_txt += '%smy_map_data->unique_id_mapping.erase(local_%s);\n' % (indent, proto.params[-2].name)
Karl Schultz2d6c90e2016-04-29 17:22:50 -0600915 pre_call_txt += '%slock.unlock();\n' % (indent)
916 (pre_decl, pre_code, post_code) = ('', '', '')
Tobin Ehlis10ba1de2016-04-13 12:59:43 -0600917 else:
918 (pre_decl, pre_code, post_code) = self._gen_obj_code(struct_uses, local_decls, ' ', '', 0, set(), True)
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -0700919 # 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 -0600920 for ld in sorted(local_decls):
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -0700921 init_null_txt = 'NULL';
922 if '*' not in local_decls[ld]:
923 init_null_txt = '{}';
924 if local_decls[ld].strip('*') not in vulkan.object_non_dispatch_list:
925 pre_decl += ' safe_%s local_%s = %s;\n' % (local_decls[ld], ld, init_null_txt)
Tobin Ehlis10ba1de2016-04-13 12:59:43 -0600926 if pre_code != '': # lock around map uses
927 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 -0700928 pre_call_txt += '%s%s' % (pre_decl, pre_code)
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -0700929 post_call_txt += '%s' % (post_code)
Tobin Ehlisd34a4c52015-12-08 10:50:10 -0700930 elif create_func:
931 base_type = proto.params[-1].ty.replace('const ', '').strip('*')
932 if base_type not in vulkan.object_non_dispatch_list:
933 return None
934 else:
935 return None
936
937 ret_val = ''
938 ret_stmt = ''
939 if proto.ret != "void":
940 ret_val = "%s result = " % proto.ret
941 ret_stmt = " return result;\n"
Tobin Ehlisd34a4c52015-12-08 10:50:10 -0700942 if create_func:
943 obj_type = proto.params[-1].ty.strip('*')
944 obj_name = proto.params[-1].name
945 if obj_type in vulkan.object_non_dispatch_list:
946 local_name = "unique%s" % obj_type[2:]
947 post_call_txt += '%sif (VK_SUCCESS == result) {\n' % (indent)
948 indent += ' '
Tobin Ehlis10ba1de2016-04-13 12:59:43 -0600949 post_call_txt += '%sstd::lock_guard<std::mutex> lock(global_lock);\n' % (indent)
Tobin Ehlisd34a4c52015-12-08 10:50:10 -0700950 if obj_name in custom_create_dict:
951 post_call_txt += '%s\n' % (self.lineinfo.get())
952 local_name = '%ss' % (local_name) # add 's' to end for vector of many
Tobin Ehlisd34a4c52015-12-08 10:50:10 -0700953 post_call_txt += '%sfor (uint32_t i=0; i<%s; ++i) {\n' % (indent, custom_create_dict[obj_name])
954 indent += ' '
Mark Lobodzinskifdf8f472016-04-28 16:36:58 -0600955 post_call_txt += '%suint64_t unique_id = global_unique_id++;\n' % (indent)
Dustin Gravesa7622d82016-04-14 17:29:20 -0600956 post_call_txt += '%smy_map_data->unique_id_mapping[unique_id] = reinterpret_cast<uint64_t &>(%s[i]);\n' % (indent, obj_name)
957 post_call_txt += '%s%s[i] = reinterpret_cast<%s&>(unique_id);\n' % (indent, obj_name, obj_type)
Tobin Ehlisd34a4c52015-12-08 10:50:10 -0700958 indent = indent[4:]
959 post_call_txt += '%s}\n' % (indent)
960 else:
961 post_call_txt += '%s\n' % (self.lineinfo.get())
Mark Lobodzinskifdf8f472016-04-28 16:36:58 -0600962 post_call_txt += '%suint64_t unique_id = global_unique_id++;\n' % (indent)
Dustin Gravesa7622d82016-04-14 17:29:20 -0600963 post_call_txt += '%smy_map_data->unique_id_mapping[unique_id] = reinterpret_cast<uint64_t &>(*%s);\n' % (indent, obj_name)
964 post_call_txt += '%s*%s = reinterpret_cast<%s&>(unique_id);\n' % (indent, obj_name, obj_type)
Tobin Ehlisd34a4c52015-12-08 10:50:10 -0700965 indent = indent[4:]
966 post_call_txt += '%s}\n' % (indent)
Tobin Ehlisd34a4c52015-12-08 10:50:10 -0700967
968 call_sig = proto.c_call()
Tobin Ehlis8bb7c2f2016-02-10 15:38:45 -0700969 # Replace default params with any custom local params
970 for ld in local_decls:
Jon Ashburn5e026df2016-06-15 08:19:07 -0600971 const_qualifier = ''
972 for p in proto.params:
973 if ld == p.name and 'const' in p.ty:
974 const_qualifier = 'const'
975 call_sig = call_sig.replace(ld, '(%s %s)local_%s' % (const_qualifier, local_decls[ld], ld))
Tobin Ehlisd34a4c52015-12-08 10:50:10 -0700976 if proto_is_global(proto):
977 table_type = "instance"
978 else:
979 table_type = "device"
980 pre_call_txt += '%s\n' % (self.lineinfo.get())
Tobin Ehlis453e91f2016-01-29 14:24:42 -0700981 open_ifdef = ''
982 close_ifdef = ''
983 if proto.name in ifdef_dict:
984 open_ifdef = '#ifdef %s\n' % (ifdef_dict[proto.name])
985 close_ifdef = '#endif\n'
986 funcs.append('%s'
987 '%s%s\n'
Tobin Ehlisd34a4c52015-12-08 10:50:10 -0700988 '{\n'
989 '%s'
990 ' %sget_dispatch_table(unique_objects_%s_table_map, %s)->%s;\n'
991 '%s'
992 '%s'
Tobin Ehlis453e91f2016-01-29 14:24:42 -0700993 '}\n'
994 '%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 -0700995 return "\n\n".join(funcs)
996
997 def generate_body(self):
998 self.layer_name = "unique_objects"
999 extensions=[('wsi_enabled',
1000 ['vkCreateSwapchainKHR',
1001 'vkDestroySwapchainKHR', 'vkGetSwapchainImagesKHR',
1002 'vkAcquireNextImageKHR', 'vkQueuePresentKHR'])]
Jon Ashburn5e026df2016-06-15 08:19:07 -06001003 surface_wsi_instance_exts = [
Petros Bantolas2b40be72016-04-15 11:02:59 +01001004 'vkDestroySurfaceKHR',
1005 'vkGetPhysicalDeviceSurfaceSupportKHR',
1006 'vkGetPhysicalDeviceSurfaceCapabilitiesKHR',
1007 'vkGetPhysicalDeviceSurfaceFormatsKHR',
Jon Ashburn5e026df2016-06-15 08:19:07 -06001008 'vkGetPhysicalDeviceSurfacePresentModesKHR']
1009 display_wsi_instance_exts = [
1010 'vkGetPhysicalDeviceDisplayPropertiesKHR',
1011 'vkGetPhysicalDeviceDisplayPlanePropertiesKHR',
1012 'vkGetDisplayPlaneSupportedDisplaysKHR',
1013 'vkGetDisplayModePropertiesKHR',
1014 'vkCreateDisplayModeKHR',
1015 'vkGetDisplayPlaneCapabilitiesKHR',
Petros Bantolas2b40be72016-04-15 11:02:59 +01001016 'vkCreateDisplayPlaneSurfaceKHR']
Mun, Gwan-gyeongbd4dd592016-02-22 09:43:09 +09001017 if self.wsi == 'Win32':
Jon Ashburn5e026df2016-06-15 08:19:07 -06001018 instance_extensions=[('wsi_enabled', surface_wsi_instance_exts),
1019 ('display_enabled', display_wsi_instance_exts),
1020 ('win32_enabled', ['vkCreateWin32SurfaceKHR'])]
Mun, Gwan-gyeongbd4dd592016-02-22 09:43:09 +09001021 elif self.wsi == 'Android':
Jon Ashburn5e026df2016-06-15 08:19:07 -06001022 instance_extensions=[('wsi_enabled', surface_wsi_instance_exts),
Jon Ashburn5e026df2016-06-15 08:19:07 -06001023 ('android_enabled', ['vkCreateAndroidSurfaceKHR'])]
Karl Schultz9daf7a32016-03-08 15:14:11 -07001024 elif self.wsi == 'Xcb' or self.wsi == 'Xlib' or self.wsi == 'Wayland' or self.wsi == 'Mir':
Jon Ashburn5e026df2016-06-15 08:19:07 -06001025 instance_extensions=[('wsi_enabled', surface_wsi_instance_exts),
1026 ('display_enabled', display_wsi_instance_exts),
1027 ('xcb_enabled', ['vkCreateXcbSurfaceKHR']),
1028 ('xlib_enabled', ['vkCreateXlibSurfaceKHR']),
1029 ('wayland_enabled', ['vkCreateWaylandSurfaceKHR']),
1030 ('mir_enabled', ['vkCreateMirSurfaceKHR'])]
Norbert Nopper15a45db2016-09-08 09:31:36 +02001031 elif self.wsi == 'Display':
1032 instance_extensions=[('wsi_enabled', surface_wsi_instance_exts),
1033 ('display_enabled', display_wsi_instance_exts)]
Karl Schultz9daf7a32016-03-08 15:14:11 -07001034 else:
1035 print('Error: Undefined DisplayServer')
1036 instance_extensions=[]
1037
Chia-I Wucdb70962016-05-13 14:07:36 +08001038 body = ["namespace %s {" % self.layer_name,
Chia-I Wuc57d5c82016-05-13 14:37:49 +08001039 self._generate_dispatch_entrypoints(),
Chia-I Wub02600c2016-05-20 07:11:22 +08001040 self._generate_layer_introspection_function(),
Tobin Ehlisd34a4c52015-12-08 10:50:10 -07001041 self._generate_layer_gpa_function(extensions,
Chia-I Wu40b8bab2016-05-13 14:06:08 +08001042 instance_extensions),
Chia-I Wucdb70962016-05-13 14:07:36 +08001043 "} // namespace %s" % self.layer_name,
Chia-I Wu40b8bab2016-05-13 14:06:08 +08001044 self._gen_layer_interface_v0_functions()]
Tobin Ehlisd34a4c52015-12-08 10:50:10 -07001045 return "\n\n".join(body)
1046
Tobin Ehlis12076fc2014-10-22 09:06:33 -06001047def main():
Mun, Gwan-gyeongbd4dd592016-02-22 09:43:09 +09001048 wsi = {
1049 "Win32",
1050 "Android",
1051 "Xcb",
1052 "Xlib",
1053 "Wayland",
1054 "Mir",
Norbert Nopper15a45db2016-09-08 09:31:36 +02001055 "Display",
Mun, Gwan-gyeongbd4dd592016-02-22 09:43:09 +09001056 }
1057
Tobin Ehlis12076fc2014-10-22 09:06:33 -06001058 subcommands = {
Tobin Ehlisd34a4c52015-12-08 10:50:10 -07001059 "unique_objects" : UniqueObjectsSubcommand,
Tobin Ehlis12076fc2014-10-22 09:06:33 -06001060 }
1061
Mun, Gwan-gyeongbd4dd592016-02-22 09:43:09 +09001062 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 -07001063 print("Usage: %s <wsi> <subcommand> <input_header> [outdir]" % sys.argv[0])
Tobin Ehlis12076fc2014-10-22 09:06:33 -06001064 print
Tobin Ehlis7e65d752015-01-15 17:51:52 -07001065 print("Available subcommands are: %s" % " ".join(subcommands))
Tobin Ehlis12076fc2014-10-22 09:06:33 -06001066 exit(1)
1067
Mun, Gwan-gyeongbd4dd592016-02-22 09:43:09 +09001068 hfp = vk_helper.HeaderFileParser(sys.argv[3])
Tobin Ehlis14ff0852014-12-17 17:44:50 -07001069 hfp.parse()
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001070 vk_helper.enum_val_dict = hfp.get_enum_val_dict()
1071 vk_helper.enum_type_dict = hfp.get_enum_type_dict()
1072 vk_helper.struct_dict = hfp.get_struct_dict()
1073 vk_helper.typedef_fwd_dict = hfp.get_typedef_fwd_dict()
1074 vk_helper.typedef_rev_dict = hfp.get_typedef_rev_dict()
1075 vk_helper.types_dict = hfp.get_types_dict()
Tobin Ehlis14ff0852014-12-17 17:44:50 -07001076
Jamie Madilldbda66b2016-05-10 07:36:20 -07001077 outfile = None
1078 if len(sys.argv) >= 5:
1079 outfile = sys.argv[4]
1080
1081 subcmd = subcommands[sys.argv[2]](outfile)
Tobin Ehlis12076fc2014-10-22 09:06:33 -06001082 subcmd.run()
1083
1084if __name__ == "__main__":
1085 main()