blob: a74b4ebd81ea5ce697bb53def4c4c2a46b1c2a5c [file] [log] [blame]
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001#!/usr/bin/env python3
2#
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06003# VK
Tobin Ehlis92dbf802014-10-22 09:06:33 -06004#
5# Copyright (C) 2014 LunarG, Inc.
6#
7# Permission is hereby granted, free of charge, to any person obtaining a
8# copy of this software and associated documentation files (the "Software"),
9# to deal in the Software without restriction, including without limitation
10# the rights to use, copy, modify, merge, publish, distribute, sublicense,
11# and/or sell copies of the Software, and to permit persons to whom the
12# Software is furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included
15# in all copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23# DEALINGS IN THE SOFTWARE.
24#
25# Authors:
26# Chia-I Wu <olv@lunarg.com>
27
28import sys
Tobin Ehlis6cd06372014-12-17 17:44:50 -070029import os
Tobin Ehlis92dbf802014-10-22 09:06:33 -060030
Courtney Goeltzenleuchtera8c06282015-04-14 14:55:44 -060031import vulkan
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060032import vk_helper
Tobin Ehlis92dbf802014-10-22 09:06:33 -060033
Mike Stroyan7c2efaa2015-04-03 13:58:35 -060034def generate_get_proc_addr_check(name):
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060035 return " if (!%s || %s[0] != 'v' || %s[1] != 'k')\n" \
36 " return NULL;" % ((name,) * 3)
Mike Stroyan7c2efaa2015-04-03 13:58:35 -060037
Tobin Ehlis92dbf802014-10-22 09:06:33 -060038class Subcommand(object):
39 def __init__(self, argv):
40 self.argv = argv
Courtney Goeltzenleuchtera8c06282015-04-14 14:55:44 -060041 self.headers = vulkan.headers
42 self.protos = vulkan.protos
Mike Stroyan3aecdb42015-04-03 17:13:23 -060043 self.no_addr = False
44 self.layer_name = ""
Tobin Ehlis92dbf802014-10-22 09:06:33 -060045
46 def run(self):
Tobin Ehlis92dbf802014-10-22 09:06:33 -060047 print(self.generate())
48
49 def generate(self):
50 copyright = self.generate_copyright()
51 header = self.generate_header()
52 body = self.generate_body()
53 footer = self.generate_footer()
54
55 contents = []
56 if copyright:
57 contents.append(copyright)
58 if header:
59 contents.append(header)
60 if body:
61 contents.append(body)
62 if footer:
63 contents.append(footer)
64
65 return "\n\n".join(contents)
66
67 def generate_copyright(self):
68 return """/* THIS FILE IS GENERATED. DO NOT EDIT. */
69
70/*
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060071 * Vulkan
Tobin Ehlis92dbf802014-10-22 09:06:33 -060072 *
73 * Copyright (C) 2014 LunarG, Inc.
74 *
75 * Permission is hereby granted, free of charge, to any person obtaining a
76 * copy of this software and associated documentation files (the "Software"),
77 * to deal in the Software without restriction, including without limitation
78 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
79 * and/or sell copies of the Software, and to permit persons to whom the
80 * Software is furnished to do so, subject to the following conditions:
81 *
82 * The above copyright notice and this permission notice shall be included
83 * in all copies or substantial portions of the Software.
84 *
85 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
86 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
87 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
88 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
89 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
90 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
91 * DEALINGS IN THE SOFTWARE.
92 */"""
93
94 def generate_header(self):
95 return "\n".join(["#include <" + h + ">" for h in self.headers])
96
97 def generate_body(self):
98 pass
99
100 def generate_footer(self):
101 pass
102
103 # Return set of printf '%' qualifier and input to that qualifier
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600104 def _get_printf_params(self, vk_type, name, output_param, cpp=False):
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600105 # TODO : Need ENUM and STRUCT checks here
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600106 if vk_helper.is_type(vk_type, 'enum'):#"_TYPE" in vk_type: # TODO : This should be generic ENUM check
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600107 return ("%s", "string_%s(%s)" % (vk_type.replace('const ', '').strip('*'), name))
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600108 if "char*" == vk_type:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600109 return ("%s", name)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600110 if "uint64" in vk_type:
111 if '*' in vk_type:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600112 return ("%lu", "*%s" % name)
113 return ("%lu", name)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600114 if "size" in vk_type:
115 if '*' in vk_type:
Chia-I Wu54ed0792014-12-27 14:14:50 +0800116 return ("%zu", "*%s" % name)
117 return ("%zu", name)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600118 if "float" in vk_type:
119 if '[' in vk_type: # handle array, current hard-coded to 4 (TODO: Make this dynamic)
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700120 if cpp:
121 return ("[%i, %i, %i, %i]", '"[" << %s[0] << "," << %s[1] << "," << %s[2] << "," << %s[3] << "]"' % (name, name, name, name))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600122 return ("[%f, %f, %f, %f]", "%s[0], %s[1], %s[2], %s[3]" % (name, name, name, name))
123 return ("%f", name)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600124 if "bool" in vk_type or 'xcb_randr_crtc_t' in vk_type:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600125 return ("%u", name)
Tobin Ehlisf29da382015-04-15 07:46:12 -0600126 if True in [t in vk_type.lower() for t in ["int", "flags", "mask", "xcb_window_t"]]:
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600127 if '[' in vk_type: # handle array, current hard-coded to 4 (TODO: Make this dynamic)
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700128 if cpp:
129 return ("[%i, %i, %i, %i]", "%s[0] << %s[1] << %s[2] << %s[3]" % (name, name, name, name))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600130 return ("[%i, %i, %i, %i]", "%s[0], %s[1], %s[2], %s[3]" % (name, name, name, name))
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600131 if '*' in vk_type:
Tobin Ehlis1336c8d2015-02-04 15:15:11 -0700132 if 'pUserData' == name:
133 return ("%i", "((pUserData == 0) ? 0 : *(pUserData))")
Jon Ashburn1f7e2d72014-12-12 16:10:45 -0700134 return ("%i", "*(%s)" % name)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600135 return ("%i", name)
Tobin Ehlis0a1e06d2014-11-11 17:28:22 -0700136 # TODO : This is special-cased as there's only one "format" param currently and it's nice to expand it
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600137 if "VkFormat" == vk_type:
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700138 if cpp:
139 return ("%p", "&%s" % name)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600140 return ("{%s.channelFormat = %%s, %s.numericFormat = %%s}" % (name, name), "string_VK_CHANNEL_FORMAT(%s.channelFormat), string_VK_NUM_FORMAT(%s.numericFormat)" % (name, name))
Tobin Ehlisa554dc32014-11-19 15:52:46 -0700141 if output_param:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600142 return ("%p", "(void*)*%s" % name)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600143 if vk_helper.is_type(vk_type, 'struct') and '*' not in vk_type:
Courtney Goeltzenleuchter9a1ded82015-04-03 16:35:32 -0600144 return ("%p", "(void*)(&%s)" % name)
Jon Ashburn1f7e2d72014-12-12 16:10:45 -0700145 return ("%p", "(void*)(%s)" % name)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600146
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700147 def _gen_layer_dbg_callback_register(self):
148 r_body = []
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600149 r_body.append('VK_LAYER_EXPORT VkResult VKAPI vkDbgRegisterMsgCallback(VkInstance instance, VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, void* pUserData)')
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700150 r_body.append('{')
151 r_body.append(' // This layer intercepts callbacks')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600152 r_body.append(' VK_LAYER_DBG_FUNCTION_NODE *pNewDbgFuncNode = (VK_LAYER_DBG_FUNCTION_NODE*)malloc(sizeof(VK_LAYER_DBG_FUNCTION_NODE));')
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700153 r_body.append(' if (!pNewDbgFuncNode)')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600154 r_body.append(' return VK_ERROR_OUT_OF_MEMORY;')
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700155 r_body.append(' pNewDbgFuncNode->pfnMsgCallback = pfnMsgCallback;')
156 r_body.append(' pNewDbgFuncNode->pUserData = pUserData;')
Jon Ashburn21001f62015-02-16 08:26:50 -0700157 r_body.append(' pNewDbgFuncNode->pNext = g_pDbgFunctionHead;')
158 r_body.append(' g_pDbgFunctionHead = pNewDbgFuncNode;')
Jon Ashburne4722392015-03-03 15:07:15 -0700159 r_body.append(' // force callbacks if DebugAction hasn\'t been set already other than initial value')
Ian Elliottc9473d92015-03-05 12:28:53 -0700160 r_body.append(' if (g_actionIsDefault) {')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600161 r_body.append(' g_debugAction = VK_DBG_LAYER_ACTION_CALLBACK;')
Ian Elliottc9473d92015-03-05 12:28:53 -0700162 r_body.append(' }')
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600163 r_body.append(' VkResult result = nextTable.DbgRegisterMsgCallback(instance, pfnMsgCallback, pUserData);')
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700164 r_body.append(' return result;')
165 r_body.append('}')
166 return "\n".join(r_body)
167
168 def _gen_layer_dbg_callback_unregister(self):
169 ur_body = []
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600170 ur_body.append('VK_LAYER_EXPORT VkResult VKAPI vkDbgUnregisterMsgCallback(VkInstance instance, VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)')
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700171 ur_body.append('{')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600172 ur_body.append(' VK_LAYER_DBG_FUNCTION_NODE *pTrav = g_pDbgFunctionHead;')
173 ur_body.append(' VK_LAYER_DBG_FUNCTION_NODE *pPrev = pTrav;')
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700174 ur_body.append(' while (pTrav) {')
175 ur_body.append(' if (pTrav->pfnMsgCallback == pfnMsgCallback) {')
176 ur_body.append(' pPrev->pNext = pTrav->pNext;')
Jon Ashburn21001f62015-02-16 08:26:50 -0700177 ur_body.append(' if (g_pDbgFunctionHead == pTrav)')
178 ur_body.append(' g_pDbgFunctionHead = pTrav->pNext;')
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700179 ur_body.append(' free(pTrav);')
180 ur_body.append(' break;')
181 ur_body.append(' }')
182 ur_body.append(' pPrev = pTrav;')
183 ur_body.append(' pTrav = pTrav->pNext;')
184 ur_body.append(' }')
Jon Ashburne4722392015-03-03 15:07:15 -0700185 ur_body.append(' if (g_pDbgFunctionHead == NULL)')
186 ur_body.append(' {')
187 ur_body.append(' if (g_actionIsDefault)')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600188 ur_body.append(' g_debugAction = VK_DBG_LAYER_ACTION_LOG_MSG;')
Jon Ashburne4722392015-03-03 15:07:15 -0700189 ur_body.append(' else')
Mike Stroyanb326d2c2015-04-02 11:59:05 -0600190 ur_body.append(' g_debugAction = (VK_LAYER_DBG_ACTION)(g_debugAction & ~((uint32_t)VK_DBG_LAYER_ACTION_CALLBACK));')
Jon Ashburne4722392015-03-03 15:07:15 -0700191 ur_body.append(' }')
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600192 ur_body.append(' VkResult result = nextTable.DbgUnregisterMsgCallback(instance, pfnMsgCallback);')
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700193 ur_body.append(' return result;')
194 ur_body.append('}')
195 return "\n".join(ur_body)
196
Jon Ashburneb2728b2015-04-10 14:33:07 -0600197 def _gen_layer_get_global_extension_info(self, layer="Generic"):
198 ggei_body = []
199 ggei_body.append('struct extProps {')
200 ggei_body.append(' uint32_t version;')
201 ggei_body.append(' const char * const name;')
202 ggei_body.append('};')
Jon Ashburnbdcd7562015-04-14 14:12:59 -0600203 if layer == 'ObjectTracker':
204 ggei_body.append('#define LAYER_EXT_ARRAY_SIZE 4')
205 ggei_body.append('static const struct extProps layerExts[LAYER_EXT_ARRAY_SIZE] = {')
206 ggei_body.append(' // TODO what is the version?')
207 ggei_body.append(' {0x10, "%s"},' % layer)
208 ggei_body.append(' {0x10, "Validation"},')
209 ggei_body.append(' {0x10, "objTrackGetObjectCount"},')
210 ggei_body.append(' {0x10, "objTrackGetObjects"}')
211 ggei_body.append('};')
Tobin Ehlisa53add02015-04-15 17:19:18 -0600212 elif layer == 'Threading':
213 ggei_body.append('#define LAYER_EXT_ARRAY_SIZE 2')
214 ggei_body.append('static const struct extProps layerExts[LAYER_EXT_ARRAY_SIZE] = {')
215 ggei_body.append(' // TODO what is the version?')
216 ggei_body.append(' {0x10, "%s"},' % layer)
217 ggei_body.append(' {0x10, "Validation"},')
218 ggei_body.append('};')
Jon Ashburnbdcd7562015-04-14 14:12:59 -0600219 else:
220 ggei_body.append('#define LAYER_EXT_ARRAY_SIZE 1')
221 ggei_body.append('static const struct extProps layerExts[LAYER_EXT_ARRAY_SIZE] = {')
222 ggei_body.append(' // TODO what is the version?')
223 ggei_body.append(' {0x10, "%s"}' % layer)
224 ggei_body.append('};')
Jon Ashburneb2728b2015-04-10 14:33:07 -0600225 ggei_body.append('')
Mark Lobodzinski8bae7d42015-04-13 16:35:52 -0500226 ggei_body.append('VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo(VkExtensionInfoType infoType, uint32_t extensionIndex, size_t* pDataSize, void* pData)')
Jon Ashburneb2728b2015-04-10 14:33:07 -0600227 ggei_body.append('{')
228 ggei_body.append(' VkExtensionProperties *ext_props;')
229 ggei_body.append(' uint32_t *count;')
230 ggei_body.append('')
231 ggei_body.append(' if (pDataSize == NULL)')
232 ggei_body.append(' return VK_ERROR_INVALID_POINTER;')
233 ggei_body.append('')
234 ggei_body.append(' switch (infoType) {')
235 ggei_body.append(' case VK_EXTENSION_INFO_TYPE_COUNT:')
236 ggei_body.append(' *pDataSize = sizeof(uint32_t);')
237 ggei_body.append(' if (pData == NULL)')
238 ggei_body.append(' return VK_SUCCESS;')
239 ggei_body.append(' count = (uint32_t *) pData;')
240 ggei_body.append(' *count = LAYER_EXT_ARRAY_SIZE;')
241 ggei_body.append(' break;')
242 ggei_body.append(' case VK_EXTENSION_INFO_TYPE_PROPERTIES:')
243 ggei_body.append(' *pDataSize = sizeof(VkExtensionProperties);')
244 ggei_body.append(' if (pData == NULL)')
245 ggei_body.append(' return VK_SUCCESS;')
246 ggei_body.append(' if (extensionIndex >= LAYER_EXT_ARRAY_SIZE)')
247 ggei_body.append(' return VK_ERROR_INVALID_VALUE;')
248 ggei_body.append(' ext_props = (VkExtensionProperties *) pData;')
249 ggei_body.append(' ext_props->version = layerExts[extensionIndex].version;')
250 ggei_body.append(' strncpy(ext_props->extName, layerExts[extensionIndex].name,')
251 ggei_body.append(' VK_MAX_EXTENSION_NAME);')
252 ggei_body.append(" ext_props->extName[VK_MAX_EXTENSION_NAME - 1] = '\\0';")
253 ggei_body.append(' break;')
254 ggei_body.append(' default:')
255 ggei_body.append(' return VK_ERROR_INVALID_VALUE;')
256 ggei_body.append(' };')
257 ggei_body.append(' return VK_SUCCESS;')
258 ggei_body.append('}')
259 return "\n".join(ggei_body)
Jon Ashburn25566352015-04-02 12:06:28 -0600260
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600261 def _generate_dispatch_entrypoints(self, qual=""):
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600262 if qual:
263 qual += " "
264
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600265 funcs = []
266 intercepted = []
267 for proto in self.protos:
Mike Stroyan88f0ecf2015-04-08 10:27:43 -0600268 if proto.name == "GetProcAddr":
269 intercepted.append(proto)
270 else:
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600271 intercept = self.generate_intercept(proto, qual)
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600272 if intercept is None:
273 # fill in default intercept for certain entrypoints
274 if 'DbgRegisterMsgCallback' == proto.name:
275 intercept = self._gen_layer_dbg_callback_register()
Jon Ashburn25566352015-04-02 12:06:28 -0600276 elif 'DbgUnregisterMsgCallback' == proto.name:
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600277 intercept = self._gen_layer_dbg_callback_unregister()
Jon Ashburneb2728b2015-04-10 14:33:07 -0600278 elif 'GetGlobalExtensionInfo' == proto.name:
279 funcs.append(self._gen_layer_get_global_extension_info(self.layer_name))
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600280 if intercept is not None:
281 funcs.append(intercept)
282 intercepted.append(proto)
283
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600284 prefix="vk"
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600285 lookups = []
286 for proto in intercepted:
287 if 'WsiX11' in proto.name:
288 lookups.append("#if defined(__linux__) || defined(XCB_NVIDIA)")
289 lookups.append("if (!strcmp(name, \"%s\"))" % proto.name)
290 lookups.append(" return (void*) %s%s;" %
291 (prefix, proto.name))
292 if 'WsiX11' in proto.name:
293 lookups.append("#endif")
294
Jon Ashburneb2728b2015-04-10 14:33:07 -0600295 prefix="vk"
296 lookups = []
297 for proto in intercepted:
298 if 'WsiX11' in proto.name:
299 lookups.append("#if defined(__linux__) || defined(XCB_NVIDIA)")
300 lookups.append("if (!strcmp(name, \"%s\"))" % proto.name)
301 lookups.append(" return (void*) %s%s;" %
302 (prefix, proto.name))
303 if 'WsiX11' in proto.name:
304 lookups.append("#endif")
305
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600306 # add customized layer_intercept_proc
307 body = []
308 body.append("static inline void* layer_intercept_proc(const char *name)")
309 body.append("{")
310 body.append(generate_get_proc_addr_check("name"))
311 body.append("")
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600312 body.append(" name += 2;")
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600313 body.append(" %s" % "\n ".join(lookups))
314 body.append("")
315 body.append(" return NULL;")
316 body.append("}")
317 funcs.append("\n".join(body))
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600318 return "\n\n".join(funcs)
319
320
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700321 def _generate_extensions(self):
322 exts = []
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600323 exts.append('uint64_t objTrackGetObjectCount(VK_OBJECT_TYPE type)')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700324 exts.append('{')
Tobin Ehlisf29da382015-04-15 07:46:12 -0600325 exts.append(' return (type == VkObjectTypeAny) ? numTotalObjs : numObjs[type];')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700326 exts.append('}')
327 exts.append('')
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600328 exts.append('VkResult objTrackGetObjects(VK_OBJECT_TYPE type, uint64_t objCount, OBJTRACK_NODE* pObjNodeArray)')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700329 exts.append('{')
330 exts.append(" // This bool flags if we're pulling all objs or just a single class of objs")
Tobin Ehlisf29da382015-04-15 07:46:12 -0600331 exts.append(' bool32_t bAllObjs = (type == VkObjectTypeAny);')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700332 exts.append(' // Check the count first thing')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600333 exts.append(' uint64_t maxObjCount = (bAllObjs) ? numTotalObjs : numObjs[type];')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700334 exts.append(' if (objCount > maxObjCount) {')
335 exts.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600336 exts.append(' sprintf(str, "OBJ ERROR : Received objTrackGetObjects() request for %lu objs, but there are only %lu objs of type %s", objCount, maxObjCount, string_VK_OBJECT_TYPE(type));')
337 exts.append(' layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, 0, 0, OBJTRACK_OBJCOUNT_MAX_EXCEEDED, "OBJTRACK", str);')
338 exts.append(' return VK_ERROR_INVALID_VALUE;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700339 exts.append(' }')
340 exts.append(' objNode* pTrav = (bAllObjs) ? pGlobalHead : pObjectHead[type];')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600341 exts.append(' for (uint64_t i = 0; i < objCount; i++) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700342 exts.append(' if (!pTrav) {')
343 exts.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600344 exts.append(' sprintf(str, "OBJ INTERNAL ERROR : Ran out of %s objs! Should have %lu, but only copied %lu and not the requested %lu.", string_VK_OBJECT_TYPE(type), maxObjCount, i, objCount);')
345 exts.append(' layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, 0, 0, OBJTRACK_INTERNAL_ERROR, "OBJTRACK", str);')
346 exts.append(' return VK_ERROR_UNKNOWN;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700347 exts.append(' }')
348 exts.append(' memcpy(&pObjNodeArray[i], pTrav, sizeof(OBJTRACK_NODE));')
349 exts.append(' pTrav = (bAllObjs) ? pTrav->pNextGlobal : pTrav->pNextObj;')
350 exts.append(' }')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600351 exts.append(' return VK_SUCCESS;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700352 exts.append('}')
Tobin Ehlisf29da382015-04-15 07:46:12 -0600353 return "\n".join(exts)
354
Jon Ashburn301c5f02015-04-06 10:58:22 -0600355 def _generate_layer_gpa_function(self, extensions=[]):
356 func_body = []
357 func_body.append("VK_LAYER_EXPORT void* VKAPI vkGetProcAddr(VkPhysicalGpu gpu, const char* funcName)\n"
358 "{\n"
359 " VkBaseLayerObject* gpuw = (VkBaseLayerObject *) gpu;\n"
360 " void* addr;\n"
361 " if (gpu == NULL)\n"
362 " return NULL;\n"
363 " pCurObj = gpuw;\n"
364 " loader_platform_thread_once(&tabOnce, init%s);\n\n"
365 " addr = layer_intercept_proc(funcName);\n"
366 " if (addr)\n"
367 " return addr;" % self.layer_name)
368
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700369 if 0 != len(extensions):
370 for ext_name in extensions:
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800371 func_body.append(' else if (!strncmp("%s", funcName, sizeof("%s")))\n'
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700372 ' return %s;' % (ext_name, ext_name, ext_name))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600373 func_body.append(" else {\n"
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600374 " if (gpuw->pGPA == NULL)\n"
375 " return NULL;\n"
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600376 " return gpuw->pGPA((VkPhysicalGpu)gpuw->nextObject, funcName);\n"
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600377 " }\n"
378 "}\n")
379 return "\n".join(func_body)
380
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600381 def _generate_layer_initialization(self, init_opts=False, prefix='vk', lockname=None):
382 func_body = ["#include \"vk_dispatch_table_helper.h\""]
Jon Ashburn21001f62015-02-16 08:26:50 -0700383 func_body.append('static void init%s(void)\n'
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600384 '{\n' % self.layer_name)
Jon Ashburn21001f62015-02-16 08:26:50 -0700385 if init_opts:
386 func_body.append(' const char *strOpt;')
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600387 func_body.append(' // initialize %s options' % self.layer_name)
388 func_body.append(' getLayerOptionEnum("%sReportLevel", (uint32_t *) &g_reportingLevel);' % self.layer_name)
389 func_body.append(' g_actionIsDefault = getLayerOptionEnum("%sDebugAction", (uint32_t *) &g_debugAction);' % self.layer_name)
Jon Ashburn21001f62015-02-16 08:26:50 -0700390 func_body.append('')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600391 func_body.append(' if (g_debugAction & VK_DBG_LAYER_ACTION_LOG_MSG)')
Jon Ashburn21001f62015-02-16 08:26:50 -0700392 func_body.append(' {')
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600393 func_body.append(' strOpt = getLayerOption("%sLogFilename");' % self.layer_name)
Jon Ashburn21001f62015-02-16 08:26:50 -0700394 func_body.append(' if (strOpt)')
395 func_body.append(' {')
396 func_body.append(' g_logFile = fopen(strOpt, "w");')
397 func_body.append(' }')
398 func_body.append(' if (g_logFile == NULL)')
399 func_body.append(' g_logFile = stdout;')
400 func_body.append(' }')
401 func_body.append('')
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600402 func_body.append(' PFN_vkGetProcAddr fpNextGPA;\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600403 ' fpNextGPA = pCurObj->pGPA;\n'
Jon Ashburn21001f62015-02-16 08:26:50 -0700404 ' assert(fpNextGPA);\n')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600405
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600406 func_body.append(" layer_initialize_dispatch_table(&nextTable, fpNextGPA, (VkPhysicalGpu) pCurObj->nextObject);")
Tobin Ehlis84a8a9b2015-02-23 14:09:16 -0700407 if lockname is not None:
408 func_body.append(" if (!%sLockInitialized)" % lockname)
409 func_body.append(" {")
410 func_body.append(" // TODO/TBD: Need to delete this mutex sometime. How???")
411 func_body.append(" loader_platform_thread_create_mutex(&%sLock);" % lockname)
412 func_body.append(" %sLockInitialized = 1;" % lockname)
413 func_body.append(" }")
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600414 func_body.append("}\n")
415 return "\n".join(func_body)
416
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600417 def _generate_layer_initialization_with_lock(self, prefix='vk'):
418 func_body = ["#include \"vk_dispatch_table_helper.h\""]
Jon Ashburn21001f62015-02-16 08:26:50 -0700419 func_body.append('static void init%s(void)\n'
Ian Elliott81ac44c2015-01-13 17:52:38 -0700420 '{\n'
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600421 ' PFN_vkGetProcAddr fpNextGPA;\n'
Ian Elliott81ac44c2015-01-13 17:52:38 -0700422 ' fpNextGPA = pCurObj->pGPA;\n'
Mike Stroyanb326d2c2015-04-02 11:59:05 -0600423 ' assert(fpNextGPA);\n' % self.layer_name)
Ian Elliott81ac44c2015-01-13 17:52:38 -0700424
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600425 func_body.append(" layer_initialize_dispatch_table(&nextTable, fpNextGPA, (VkPhysicalGpu) pCurObj->nextObject);\n")
Ian Elliott81ac44c2015-01-13 17:52:38 -0700426 func_body.append(" if (!printLockInitialized)")
427 func_body.append(" {")
428 func_body.append(" // TODO/TBD: Need to delete this mutex sometime. How???")
429 func_body.append(" loader_platform_thread_create_mutex(&printLock);")
430 func_body.append(" printLockInitialized = 1;")
431 func_body.append(" }")
432 func_body.append("}\n")
433 return "\n".join(func_body)
434
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600435class LayerFuncsSubcommand(Subcommand):
436 def generate_header(self):
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600437 return '#include <vkLayer.h>\n#include "loader.h"'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600438
439 def generate_body(self):
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600440 return self._generate_dispatch_entrypoints("static")
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600441
442class LayerDispatchSubcommand(Subcommand):
443 def generate_header(self):
444 return '#include "layer_wrappers.h"'
445
446 def generate_body(self):
Jon Ashburn21001f62015-02-16 08:26:50 -0700447 return self._generate_layer_initialization()
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600448
449class GenericLayerSubcommand(Subcommand):
450 def generate_header(self):
Jon Ashburn301c5f02015-04-06 10:58:22 -0600451 return '#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include "loader_platform.h"\n#include "vkLayer.h"\n//The following is #included again to catch certain OS-specific functions being used:\n#include "loader_platform.h"\n\n#include "layers_config.h"\n#include "layers_msg.h"\n\nstatic VkLayerDispatchTable nextTable;\nstatic VkBaseLayerObject *pCurObj;\n\nstatic LOADER_PLATFORM_THREAD_ONCE_DECLARATION(tabOnce);'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600452
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600453 def generate_intercept(self, proto, qual):
Tobin Ehlis46c96652015-04-16 11:17:12 -0600454 if proto.name in [ 'DbgRegisterMsgCallback', 'DbgUnregisterMsgCallback' , 'GetGlobalExtensionInfo']:
Mike Stroyan723913e2015-04-03 14:39:16 -0600455 # use default version
456 return None
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600457 decl = proto.c_func(prefix="vk", attr="VKAPI")
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600458 ret_val = ''
459 stmt = ''
460 funcs = []
461 if proto.ret != "void":
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600462 ret_val = "VkResult result = "
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600463 stmt = " return result;\n"
464 if 'WsiX11AssociateConnection' == proto.name:
465 funcs.append("#if defined(__linux__) || defined(XCB_NVIDIA)")
466 if proto.name == "EnumerateLayers":
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600467 funcs.append('%s%s\n'
468 '{\n'
469 ' char str[1024];\n'
470 ' if (gpu != NULL) {\n'
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600471 ' sprintf(str, "At start of layered %s\\n");\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600472 ' layerCbMsg(VK_DBG_MSG_UNKNOWN, VK_VALIDATION_LEVEL_0, gpu, 0, 0, (char *) "GENERIC", (char *) str);\n'
Jon Ashburn630e44f2015-04-08 21:33:34 -0600473 ' pCurObj = (VkBaseLayerObject *) gpu;\n'
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600474 ' loader_platform_thread_once(&tabOnce, init%s);\n'
475 ' %snextTable.%s;\n'
476 ' sprintf(str, "Completed layered %s\\n");\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600477 ' layerCbMsg(VK_DBG_MSG_UNKNOWN, VK_VALIDATION_LEVEL_0, gpu, 0, 0, (char *) "GENERIC", (char *) str);\n'
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600478 ' fflush(stdout);\n'
479 ' %s'
480 ' } else {\n'
481 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600482 ' return VK_ERROR_INVALID_POINTER;\n'
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600483 ' // This layer compatible with all GPUs\n'
484 ' *pOutLayerCount = 1;\n'
485 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600486 ' return VK_SUCCESS;\n'
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600487 ' }\n'
Jon Ashburn630e44f2015-04-08 21:33:34 -0600488 '}' % (qual, decl, proto.name, self.layer_name, ret_val, proto.c_call(), proto.name, stmt, self.layer_name))
489 else:
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600490 funcs.append('%s%s\n'
491 '{\n'
492 ' %snextTable.%s;\n'
493 '%s'
494 '}' % (qual, decl, ret_val, proto.c_call(), stmt))
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600495 if 'WsiX11QueuePresent' == proto.name:
496 funcs.append("#endif")
497 return "\n\n".join(funcs)
498
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600499 def generate_body(self):
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600500 self.layer_name = "Generic"
501 body = [self._generate_layer_initialization(True),
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600502 self._generate_dispatch_entrypoints("VK_LAYER_EXPORT"),
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600503 self._generate_layer_gpa_function()]
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600504
505 return "\n\n".join(body)
506
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600507class APIDumpSubcommand(Subcommand):
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600508 def generate_header(self):
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700509 header_txt = []
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600510 header_txt.append('#include <fstream>')
511 header_txt.append('#include <iostream>')
512 header_txt.append('#include <string>')
513 header_txt.append('')
514 header_txt.append('static std::ofstream fileStream;')
515 header_txt.append('static std::string fileName = "vk_apidump.txt";')
516 header_txt.append('std::ostream* outputStream = NULL;')
517 header_txt.append('void ConfigureOutputStream(bool writeToFile, bool flushAfterWrite)')
518 header_txt.append('{')
519 header_txt.append(' if(writeToFile)')
520 header_txt.append(' {')
521 header_txt.append(' fileStream.open(fileName);')
522 header_txt.append(' outputStream = &fileStream;')
523 header_txt.append(' }')
524 header_txt.append(' else')
525 header_txt.append(' {')
526 header_txt.append(' outputStream = &std::cout;')
527 header_txt.append(' }')
528 header_txt.append('')
529 header_txt.append(' if(flushAfterWrite)')
530 header_txt.append(' {')
531 header_txt.append(' outputStream->sync_with_stdio(true);')
532 header_txt.append(' }')
533 header_txt.append(' else')
534 header_txt.append(' {')
535 header_txt.append(' outputStream->sync_with_stdio(false);')
536 header_txt.append(' }')
537 header_txt.append('}')
538 header_txt.append('')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700539 header_txt.append('#include "loader_platform.h"')
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600540 header_txt.append('#include "vkLayer.h"')
541 header_txt.append('#include "vk_struct_string_helper_cpp.h"')
542 header_txt.append('')
Ian Elliott20f06872015-02-12 17:08:34 -0700543 header_txt.append('// The following is #included again to catch certain OS-specific functions being used:')
544 header_txt.append('#include "loader_platform.h"')
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600545 header_txt.append('')
Jon Ashburn301c5f02015-04-06 10:58:22 -0600546 header_txt.append('static VkLayerDispatchTable nextTable;')
547 header_txt.append('static VkBaseLayerObject *pCurObj;')
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600548 header_txt.append('')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700549 header_txt.append('static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(tabOnce);')
550 header_txt.append('static int printLockInitialized = 0;')
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600551 header_txt.append('static loader_platform_thread_mutex printLock;')
552 header_txt.append('')
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700553 header_txt.append('#define MAX_TID 513')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700554 header_txt.append('static loader_platform_thread_id tidMapping[MAX_TID] = {0};')
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700555 header_txt.append('static uint32_t maxTID = 0;')
556 header_txt.append('// Map actual TID to an index value and return that index')
557 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
558 header_txt.append('static uint32_t getTIDIndex() {')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700559 header_txt.append(' loader_platform_thread_id tid = loader_platform_get_thread_id();')
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700560 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
561 header_txt.append(' if (tid == tidMapping[i])')
562 header_txt.append(' return i;')
563 header_txt.append(' }')
564 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
Ian Elliott81ac44c2015-01-13 17:52:38 -0700565 header_txt.append(' uint32_t retVal = (uint32_t) maxTID;')
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700566 header_txt.append(' tidMapping[maxTID++] = tid;')
567 header_txt.append(' assert(maxTID < MAX_TID);')
568 header_txt.append(' return retVal;')
569 header_txt.append('}')
570 return "\n".join(header_txt)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600571
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600572 def generate_init(self):
573 func_body = []
574 func_body.append('#include "vk_dispatch_table_helper.h"')
575 func_body.append('#include "layers_config.h"')
576 func_body.append('')
577 func_body.append('static void init%s(void)' % self.layer_name)
578 func_body.append('{')
579 func_body.append(' using namespace StreamControl;')
580 func_body.append('')
581 func_body.append(' char const*const writeToFileStr = getLayerOption("APIDumpFile");')
582 func_body.append(' bool writeToFile = false;')
583 func_body.append(' if(writeToFileStr != NULL)')
584 func_body.append(' {')
585 func_body.append(' if(strcmp(writeToFileStr, "TRUE") == 0)')
586 func_body.append(' {')
587 func_body.append(' writeToFile = true;')
588 func_body.append(' }')
589 func_body.append(' else if(strcmp(writeToFileStr, "FALSE") == 0)')
590 func_body.append(' {')
591 func_body.append(' writeToFile = false;')
592 func_body.append(' }')
593 func_body.append(' }')
594 func_body.append('')
595 func_body.append(' char const*const noAddrStr = getLayerOption("APIDumpNoAddr");')
596 func_body.append(' if(noAddrStr != NULL)')
597 func_body.append(' {')
598 func_body.append(' if(strcmp(noAddrStr, "FALSE") == 0)')
599 func_body.append(' {')
600 func_body.append(' StreamControl::writeAddress = true;')
601 func_body.append(' }')
602 func_body.append(' else if(strcmp(noAddrStr, "TRUE") == 0)')
603 func_body.append(' {')
604 func_body.append(' StreamControl::writeAddress = false;')
605 func_body.append(' }')
606 func_body.append(' }')
607 func_body.append('')
608 func_body.append(' char const*const flushAfterWriteStr = getLayerOption("APIDumpFlush");')
609 func_body.append(' bool flushAfterWrite = false;')
610 func_body.append(' if(flushAfterWriteStr != NULL)')
611 func_body.append(' {')
612 func_body.append(' if(strcmp(flushAfterWriteStr, "TRUE") == 0)')
613 func_body.append(' {')
614 func_body.append(' flushAfterWrite = true;')
615 func_body.append(' }')
616 func_body.append(' else if(strcmp(flushAfterWriteStr, "FALSE") == 0)')
617 func_body.append(' {')
618 func_body.append(' flushAfterWrite = false;')
619 func_body.append(' }')
620 func_body.append(' }')
621 func_body.append('')
622 func_body.append(' ConfigureOutputStream(writeToFile, flushAfterWrite);')
623 func_body.append('')
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600624 func_body.append(' PFN_vkGetProcAddr fpNextGPA;')
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600625 func_body.append(' fpNextGPA = pCurObj->pGPA;')
626 func_body.append(' assert(fpNextGPA);')
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600627 func_body.append(' layer_initialize_dispatch_table(&nextTable, fpNextGPA, (VkPhysicalGpu) pCurObj->nextObject);')
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600628 func_body.append('')
629 func_body.append(' if (!printLockInitialized)')
630 func_body.append(' {')
631 func_body.append(' // TODO/TBD: Need to delete this mutex sometime. How???')
632 func_body.append(' loader_platform_thread_create_mutex(&printLock);')
633 func_body.append(' printLockInitialized = 1;')
634 func_body.append(' }')
635 func_body.append('}')
636 func_body.append('')
637 return "\n".join(func_body)
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700638
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600639 def generate_intercept(self, proto, qual):
Jon Ashburneb2728b2015-04-10 14:33:07 -0600640 if proto.name in [ 'GetGlobalExtensionInfo']:
641 return None
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600642 decl = proto.c_func(prefix="vk", attr="VKAPI")
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600643 ret_val = ''
644 stmt = ''
645 funcs = []
646 sp_param_dict = {} # Store 'index' for struct param to print, or an name of binding "Count" param for array to print
647 create_params = 0 # Num of params at end of function that are created and returned as output values
648 if 'WsiX11CreatePresentableImage' in proto.name or 'AllocDescriptorSets' in proto.name:
649 create_params = -2
650 elif 'Create' in proto.name or 'Alloc' in proto.name or 'MapMemory' in proto.name:
651 create_params = -1
652 if proto.ret != "void":
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600653 ret_val = "VkResult result = "
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600654 stmt = " return result;\n"
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600655 f_open = 'loader_platform_thread_lock_mutex(&printLock);\n '
656 log_func = ' if (StreamControl::writeAddress == true) {'
657 log_func += '\n (*outputStream) << "t{" << getTIDIndex() << "} vk%s(' % proto.name
658 log_func_no_addr = '\n (*outputStream) << "t{" << getTIDIndex() << "} vk%s(' % proto.name
659 f_close = '\n loader_platform_thread_unlock_mutex(&printLock);'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600660 pindex = 0
661 prev_count_name = ''
662 for p in proto.params:
663 cp = False
664 if 0 != create_params:
665 # If this is any of the N last params of the func, treat as output
666 for y in range(-1, create_params-1, -1):
667 if p.name == proto.params[y].name:
668 cp = True
669 (pft, pfi) = self._get_printf_params(p.ty, p.name, cp, cpp=True)
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600670 log_func += '%s = " << %s << ", ' % (p.name, pfi)
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600671 if "%p" == pft:
672 log_func_no_addr += '%s = address, ' % (p.name)
673 else:
674 log_func_no_addr += '%s = " << %s << ", ' % (p.name, pfi)
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600675 if prev_count_name != '' and (prev_count_name.replace('Count', '')[1:] in p.name or 'slotCount' == prev_count_name):
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600676 sp_param_dict[pindex] = prev_count_name
677 elif 'pDescriptorSets' == p.name and proto.params[-1].name == 'pCount':
678 sp_param_dict[pindex] = '*pCount'
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600679 elif 'Wsi' not in proto.name and vk_helper.is_type(p.ty.strip('*').replace('const ', ''), 'struct'):
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600680 sp_param_dict[pindex] = 'index'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600681 if p.name.endswith('Count'):
682 if '*' in p.ty:
683 prev_count_name = "*%s" % p.name
684 else:
685 prev_count_name = p.name
Jon Ashburneb2728b2015-04-10 14:33:07 -0600686 pindex += 1
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600687 log_func = log_func.strip(', ')
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600688 log_func_no_addr = log_func_no_addr.strip(', ')
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600689 if proto.ret != "void":
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600690 log_func += ') = " << string_VkResult((VkResult)result) << endl'
691 log_func_no_addr += ') = " << string_VkResult((VkResult)result) << endl'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600692 else:
693 log_func += ')\\n"'
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600694 log_func_no_addr += ')\\n"'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600695 log_func += ';'
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600696 log_func_no_addr += ';'
697 log_func += '\n }\n else {%s;\n }' % log_func_no_addr;
Tobin Ehlisf29da382015-04-15 07:46:12 -0600698 #print("Proto %s has param_dict: %s" % (proto.name, sp_param_dict))
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600699 if len(sp_param_dict) > 0:
700 i_decl = False
701 log_func += '\n string tmp_str;'
702 for sp_index in sp_param_dict:
Tobin Ehlisf29da382015-04-15 07:46:12 -0600703 #print("sp_index: %s" % str(sp_index))
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600704 if 'index' == sp_param_dict[sp_index]:
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600705 cis_print_func = 'vk_print_%s' % (proto.params[sp_index].ty.replace('const ', '').strip('*').lower())
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600706 local_name = proto.params[sp_index].name
707 if '*' not in proto.params[sp_index].ty:
708 local_name = '&%s' % proto.params[sp_index].name
709 log_func += '\n if (%s) {' % (local_name)
710 log_func += '\n tmp_str = %s(%s, " ");' % (cis_print_func, local_name)
711 log_func += '\n (*outputStream) << " %s (" << %s << ")" << endl << tmp_str << endl;' % (local_name, local_name)
712 log_func += '\n }'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600713 else: # We have a count value stored to iterate over an array
714 print_cast = ''
715 print_func = ''
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600716 if vk_helper.is_type(proto.params[sp_index].ty.strip('*').replace('const ', ''), 'struct'):
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600717 print_cast = '&'
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600718 print_func = 'vk_print_%s' % proto.params[sp_index].ty.replace('const ', '').strip('*').lower()
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600719 else:
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600720 print_cast = ''
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600721 print_func = 'string_convert_helper'
722 #cis_print_func = 'tmp_str = string_convert_helper((void*)%s[i], " ");' % proto.params[sp_index].name
723 cis_print_func = 'tmp_str = %s(%s%s[i], " ");' % (print_func, print_cast, proto.params[sp_index].name)
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600724 if not i_decl:
725 log_func += '\n uint32_t i;'
726 i_decl = True
727 log_func += '\n for (i = 0; i < %s; i++) {' % (sp_param_dict[sp_index])
728 log_func += '\n %s' % (cis_print_func)
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600729 log_func += '\n (*outputStream) << " %s[" << i << "] (" << %s%s[i] << ")" << endl << tmp_str << endl;' % (proto.params[sp_index].name, '&', proto.params[sp_index].name)
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600730 log_func += '\n }'
731 if 'WsiX11AssociateConnection' == proto.name:
732 funcs.append("#if defined(__linux__) || defined(XCB_NVIDIA)")
733 if proto.name == "EnumerateLayers":
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600734 c_call = proto.c_call().replace("(" + proto.params[0].name, "((VkPhysicalGpu)gpuw->nextObject", 1)
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600735 funcs.append('%s%s\n'
736 '{\n'
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600737 ' using namespace StreamControl;\n'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600738 ' if (gpu != NULL) {\n'
Jon Ashburn630e44f2015-04-08 21:33:34 -0600739 ' pCurObj = (VkBaseLayerObject *) gpu;\n'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600740 ' loader_platform_thread_once(&tabOnce, init%s);\n'
741 ' %snextTable.%s;\n'
742 ' %s %s %s\n'
743 ' %s'
744 ' } else {\n'
745 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600746 ' return VK_ERROR_INVALID_POINTER;\n'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600747 ' // This layer compatible with all GPUs\n'
748 ' *pOutLayerCount = 1;\n'
749 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600750 ' return VK_SUCCESS;\n'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600751 ' }\n'
Jon Ashburn630e44f2015-04-08 21:33:34 -0600752 '}' % (qual, decl, self.layer_name, ret_val, proto.c_call(),f_open, log_func, f_close, stmt, self.layer_name))
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600753 else:
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600754 funcs.append('%s%s\n'
755 '{\n'
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600756 ' using namespace StreamControl;\n'
Jon Ashburn301c5f02015-04-06 10:58:22 -0600757 ' %snextTable.%s;\n'
758 ' %s%s%s\n'
759 '%s'
760 '}' % (qual, decl, ret_val, proto.c_call(), f_open, log_func, f_close, stmt))
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600761 if 'WsiX11QueuePresent' == proto.name:
762 funcs.append("#endif")
763 return "\n\n".join(funcs)
764
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700765 def generate_body(self):
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600766 self.layer_name = "APIDump"
767 body = [self.generate_init(),
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600768 self._generate_dispatch_entrypoints("VK_LAYER_EXPORT"),
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600769 self._generate_layer_gpa_function()]
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700770 return "\n\n".join(body)
771
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600772class ObjectTrackerSubcommand(Subcommand):
773 def generate_header(self):
774 header_txt = []
Ian Elliott81ac44c2015-01-13 17:52:38 -0700775 header_txt.append('#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include "loader_platform.h"')
Jon Ashburn301c5f02015-04-06 10:58:22 -0600776 header_txt.append('#include "object_track.h"\n\nstatic VkLayerDispatchTable nextTable;\nstatic VkBaseLayerObject *pCurObj;')
Ian Elliott20f06872015-02-12 17:08:34 -0700777 header_txt.append('// The following is #included again to catch certain OS-specific functions being used:')
778 header_txt.append('#include "loader_platform.h"')
Jon Ashburn7a2da4f2015-02-17 11:03:12 -0700779 header_txt.append('#include "layers_config.h"')
Jon Ashburn21001f62015-02-16 08:26:50 -0700780 header_txt.append('#include "layers_msg.h"')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700781 header_txt.append('static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(tabOnce);')
782 header_txt.append('static long long unsigned int object_track_index = 0;')
Tobin Ehlis84a8a9b2015-02-23 14:09:16 -0700783 header_txt.append('static int objLockInitialized = 0;')
784 header_txt.append('static loader_platform_thread_mutex objLock;')
Jon Ashburn21001f62015-02-16 08:26:50 -0700785 header_txt.append('')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700786 header_txt.append('// We maintain a "Global" list which links every object and a')
787 header_txt.append('// per-Object list which just links objects of a given type')
788 header_txt.append('// The object node has both pointers so the actual nodes are shared between the two lists')
789 header_txt.append('typedef struct _objNode {')
790 header_txt.append(' OBJTRACK_NODE obj;')
791 header_txt.append(' struct _objNode *pNextObj;')
792 header_txt.append(' struct _objNode *pNextGlobal;')
793 header_txt.append('} objNode;')
Mark Lobodzinski93fdbd22015-04-09 12:09:45 -0500794 header_txt.append('')
795 header_txt.append('static objNode *pObjectHead[VkNumObjectType] = {0};')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700796 header_txt.append('static objNode *pGlobalHead = NULL;')
Mark Lobodzinski93fdbd22015-04-09 12:09:45 -0500797 header_txt.append('static uint64_t numObjs[VkNumObjectType] = {0};')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700798 header_txt.append('static uint64_t numTotalObjs = 0;')
Courtney Goeltzenleuchter304a1f82015-04-07 16:23:00 -0600799 header_txt.append('static uint32_t maxMemReferences = 0;')
Mark Lobodzinski93fdbd22015-04-09 12:09:45 -0500800 header_txt.append('')
801 header_txt.append('// For each Queue\'s doubly linked-list of mem refs')
802 header_txt.append('typedef struct _OT_MEM_INFO {')
803 header_txt.append(' VkGpuMemory mem;')
804 header_txt.append(' struct _OT_MEM_INFO *pNextMI;')
805 header_txt.append(' struct _OT_MEM_INFO *pPrevMI;')
806 header_txt.append('')
807 header_txt.append('} OT_MEM_INFO;')
808 header_txt.append('')
809 header_txt.append('// Track Queue information')
810 header_txt.append('typedef struct _OT_QUEUE_INFO {')
811 header_txt.append(' OT_MEM_INFO *pMemRefList;')
812 header_txt.append(' struct _OT_QUEUE_INFO *pNextQI;')
813 header_txt.append(' VkQueue queue;')
814 header_txt.append(' uint32_t refCount;')
815 header_txt.append('} OT_QUEUE_INFO;')
816 header_txt.append('')
817 header_txt.append('// Global list of QueueInfo structures, one per queue')
818 header_txt.append('static OT_QUEUE_INFO *g_pQueueInfo;')
819 header_txt.append('')
820 header_txt.append('// Add new queue to head of global queue list')
821 header_txt.append('static void addQueueInfo(VkQueue queue)')
822 header_txt.append('{')
823 header_txt.append(' OT_QUEUE_INFO *pQueueInfo = malloc(sizeof(OT_QUEUE_INFO));')
824 header_txt.append(' memset(pQueueInfo, 0, sizeof(OT_QUEUE_INFO));')
825 header_txt.append(' pQueueInfo->queue = queue;')
826 header_txt.append('')
827 header_txt.append(' if (pQueueInfo != NULL) {')
828 header_txt.append(' pQueueInfo->pNextQI = g_pQueueInfo;')
829 header_txt.append(' g_pQueueInfo = pQueueInfo;')
830 header_txt.append(' }')
831 header_txt.append(' else {')
832 header_txt.append(' char str[1024];')
833 header_txt.append(' sprintf(str, "ERROR: VK_ERROR_OUT_OF_MEMORY -- could not allocate memory for Queue Information");')
834 header_txt.append(' layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, queue, 0, OBJTRACK_INTERNAL_ERROR, "OBJTRACK", str);')
835 header_txt.append(' }')
836 header_txt.append('}')
837 header_txt.append('')
Mark Lobodzinski93fdbd22015-04-09 12:09:45 -0500838 header_txt.append('// Destroy memRef lists and free all memory')
839 header_txt.append('static void destroyQueueMemRefLists()')
840 header_txt.append('{')
841 header_txt.append(' OT_QUEUE_INFO *pQueueInfo = g_pQueueInfo;')
842 header_txt.append(' OT_QUEUE_INFO *pDelQueueInfo = NULL;')
843 header_txt.append(' while (pQueueInfo != NULL) {')
844 header_txt.append(' OT_MEM_INFO *pMemInfo = pQueueInfo->pMemRefList;')
845 header_txt.append(' while (pMemInfo != NULL) {')
846 header_txt.append(' OT_MEM_INFO *pDelMemInfo = pMemInfo;')
847 header_txt.append(' pMemInfo = pMemInfo->pNextMI;')
848 header_txt.append(' free(pDelMemInfo);')
849 header_txt.append(' }')
850 header_txt.append(' pDelQueueInfo = pQueueInfo;')
851 header_txt.append(' pQueueInfo = pQueueInfo->pNextQI;')
852 header_txt.append(' free(pDelQueueInfo);')
853 header_txt.append(' }')
854 header_txt.append('}')
855 header_txt.append('')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700856 header_txt.append('// Debug function to print global list and each individual object list')
857 header_txt.append('static void ll_print_lists()')
858 header_txt.append('{')
859 header_txt.append(' objNode* pTrav = pGlobalHead;')
860 header_txt.append(' printf("=====GLOBAL OBJECT LIST (%lu total objs):\\n", numTotalObjs);')
861 header_txt.append(' while (pTrav) {')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600862 header_txt.append(' printf(" ObjNode (%p) w/ %s obj %p has pNextGlobal %p\\n", (void*)pTrav, string_VK_OBJECT_TYPE(pTrav->obj.objType), pTrav->obj.pObj, (void*)pTrav->pNextGlobal);')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700863 header_txt.append(' pTrav = pTrav->pNextGlobal;')
864 header_txt.append(' }')
Mark Lobodzinski93fdbd22015-04-09 12:09:45 -0500865 header_txt.append(' for (uint32_t i = 0; i < VkNumObjectType; i++) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700866 header_txt.append(' pTrav = pObjectHead[i];')
867 header_txt.append(' if (pTrav) {')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600868 header_txt.append(' printf("=====%s OBJECT LIST (%lu objs):\\n", string_VK_OBJECT_TYPE(pTrav->obj.objType), numObjs[i]);')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700869 header_txt.append(' while (pTrav) {')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600870 header_txt.append(' printf(" ObjNode (%p) w/ %s obj %p has pNextObj %p\\n", (void*)pTrav, string_VK_OBJECT_TYPE(pTrav->obj.objType), pTrav->obj.pObj, (void*)pTrav->pNextObj);')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700871 header_txt.append(' pTrav = pTrav->pNextObj;')
872 header_txt.append(' }')
873 header_txt.append(' }')
874 header_txt.append(' }')
875 header_txt.append('}')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600876 header_txt.append('static void ll_insert_obj(void* pObj, VK_OBJECT_TYPE objType) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700877 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600878 header_txt.append(' sprintf(str, "OBJ[%llu] : CREATE %s object %p", object_track_index++, string_VK_OBJECT_TYPE(objType), (void*)pObj);')
879 header_txt.append(' layerCbMsg(VK_DBG_MSG_UNKNOWN, VK_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700880 header_txt.append(' objNode* pNewObjNode = (objNode*)malloc(sizeof(objNode));')
881 header_txt.append(' pNewObjNode->obj.pObj = pObj;')
882 header_txt.append(' pNewObjNode->obj.objType = objType;')
Mark Lobodzinski01552702015-02-03 10:06:31 -0600883 header_txt.append(' pNewObjNode->obj.status = OBJSTATUS_NONE;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700884 header_txt.append(' pNewObjNode->obj.numUses = 0;')
885 header_txt.append(' // insert at front of global list')
886 header_txt.append(' pNewObjNode->pNextGlobal = pGlobalHead;')
887 header_txt.append(' pGlobalHead = pNewObjNode;')
888 header_txt.append(' // insert at front of object list')
889 header_txt.append(' pNewObjNode->pNextObj = pObjectHead[objType];')
890 header_txt.append(' pObjectHead[objType] = pNewObjNode;')
891 header_txt.append(' // increment obj counts')
892 header_txt.append(' numObjs[objType]++;')
893 header_txt.append(' numTotalObjs++;')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600894 header_txt.append(' //sprintf(str, "OBJ_STAT : %lu total objs & %lu %s objs.", numTotalObjs, numObjs[objType], string_VK_OBJECT_TYPE(objType));')
Chia-I Wudf142a32014-12-16 11:02:06 +0800895 header_txt.append(' if (0) ll_print_lists();')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700896 header_txt.append('}')
897 header_txt.append('// Traverse global list and return type for given object')
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600898 header_txt.append('static VK_OBJECT_TYPE ll_get_obj_type(VkObject object) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700899 header_txt.append(' objNode *pTrav = pGlobalHead;')
900 header_txt.append(' while (pTrav) {')
901 header_txt.append(' if (pTrav->obj.pObj == object)')
902 header_txt.append(' return pTrav->obj.objType;')
903 header_txt.append(' pTrav = pTrav->pNextGlobal;')
904 header_txt.append(' }')
905 header_txt.append(' char str[1024];')
906 header_txt.append(' sprintf(str, "Attempting look-up on obj %p but it is NOT in the global list!", (void*)object);')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600907 header_txt.append(' layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, object, 0, OBJTRACK_MISSING_OBJECT, "OBJTRACK", str);')
Mark Lobodzinski93fdbd22015-04-09 12:09:45 -0500908 header_txt.append(' return VkObjectTypeUnknown;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700909 header_txt.append('}')
Chia-I Wudf142a32014-12-16 11:02:06 +0800910 header_txt.append('#if 0')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600911 header_txt.append('static uint64_t ll_get_obj_uses(void* pObj, VK_OBJECT_TYPE objType) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700912 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
913 header_txt.append(' while (pTrav) {')
914 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
915 header_txt.append(' return pTrav->obj.numUses;')
916 header_txt.append(' }')
917 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600918 header_txt.append(' }')
919 header_txt.append(' return 0;')
920 header_txt.append('}')
Chia-I Wudf142a32014-12-16 11:02:06 +0800921 header_txt.append('#endif')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600922 header_txt.append('static void ll_increment_use_count(void* pObj, VK_OBJECT_TYPE objType) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700923 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600924 header_txt.append(' while (pTrav) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700925 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
926 header_txt.append(' pTrav->obj.numUses++;')
927 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600928 header_txt.append(' sprintf(str, "OBJ[%llu] : USING %s object %p (%lu total uses)", object_track_index++, string_VK_OBJECT_TYPE(objType), (void*)pObj, pTrav->obj.numUses);')
929 header_txt.append(' layerCbMsg(VK_DBG_MSG_UNKNOWN, VK_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700930 header_txt.append(' return;')
931 header_txt.append(' }')
932 header_txt.append(' pTrav = pTrav->pNextObj;')
933 header_txt.append(' }')
934 header_txt.append(' // If we do not find obj, insert it and then increment count')
935 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600936 header_txt.append(' sprintf(str, "Unable to increment count for obj %p, will add to list as %s type and increment count", pObj, string_VK_OBJECT_TYPE(objType));')
937 header_txt.append(' layerCbMsg(VK_DBG_MSG_WARNING, VK_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK", str);')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700938 header_txt.append('')
939 header_txt.append(' ll_insert_obj(pObj, objType);')
940 header_txt.append(' ll_increment_use_count(pObj, objType);')
941 header_txt.append('}')
942 header_txt.append('// We usually do not know Obj type when we destroy it so have to fetch')
943 header_txt.append('// Type from global list w/ ll_destroy_obj()')
944 header_txt.append('// and then do the full removal from both lists w/ ll_remove_obj_type()')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600945 header_txt.append('static void ll_remove_obj_type(void* pObj, VK_OBJECT_TYPE objType) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700946 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
947 header_txt.append(' objNode *pPrev = pObjectHead[objType];')
948 header_txt.append(' while (pTrav) {')
949 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
950 header_txt.append(' pPrev->pNextObj = pTrav->pNextObj;')
951 header_txt.append(' // update HEAD of Obj list as needed')
952 header_txt.append(' if (pObjectHead[objType] == pTrav)')
953 header_txt.append(' pObjectHead[objType] = pTrav->pNextObj;')
954 header_txt.append(' assert(numObjs[objType] > 0);')
955 header_txt.append(' numObjs[objType]--;')
956 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600957 header_txt.append(' sprintf(str, "OBJ[%llu] : DESTROY %s object %p", object_track_index++, string_VK_OBJECT_TYPE(objType), (void*)pObj);')
958 header_txt.append(' layerCbMsg(VK_DBG_MSG_UNKNOWN, VK_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600959 header_txt.append(' return;')
960 header_txt.append(' }')
961 header_txt.append(' pPrev = pTrav;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700962 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600963 header_txt.append(' }')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700964 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600965 header_txt.append(' sprintf(str, "OBJ INTERNAL ERROR : Obj %p was in global list but not in %s list", pObj, string_VK_OBJECT_TYPE(objType));')
966 header_txt.append(' layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_INTERNAL_ERROR, "OBJTRACK", str);')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700967 header_txt.append('}')
968 header_txt.append('// Parse global list to find obj type, then remove obj from obj type list, finally')
969 header_txt.append('// remove obj from global list')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600970 header_txt.append('static void ll_destroy_obj(void* pObj) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700971 header_txt.append(' objNode *pTrav = pGlobalHead;')
972 header_txt.append(' objNode *pPrev = pGlobalHead;')
973 header_txt.append(' while (pTrav) {')
974 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
975 header_txt.append(' ll_remove_obj_type(pObj, pTrav->obj.objType);')
976 header_txt.append(' pPrev->pNextGlobal = pTrav->pNextGlobal;')
977 header_txt.append(' // update HEAD of global list if needed')
978 header_txt.append(' if (pGlobalHead == pTrav)')
979 header_txt.append(' pGlobalHead = pTrav->pNextGlobal;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700980 header_txt.append(' assert(numTotalObjs > 0);')
981 header_txt.append(' numTotalObjs--;')
982 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600983 header_txt.append(' sprintf(str, "OBJ_STAT Removed %s obj %p that was used %lu times (%lu total objs remain & %lu %s objs).", string_VK_OBJECT_TYPE(pTrav->obj.objType), pTrav->obj.pObj, pTrav->obj.numUses, numTotalObjs, numObjs[pTrav->obj.objType], string_VK_OBJECT_TYPE(pTrav->obj.objType));')
984 header_txt.append(' layerCbMsg(VK_DBG_MSG_UNKNOWN, VK_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
Tobin Ehlis84a8a9b2015-02-23 14:09:16 -0700985 header_txt.append(' free(pTrav);')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700986 header_txt.append(' return;')
987 header_txt.append(' }')
988 header_txt.append(' pPrev = pTrav;')
989 header_txt.append(' pTrav = pTrav->pNextGlobal;')
990 header_txt.append(' }')
991 header_txt.append(' char str[1024];')
992 header_txt.append(' sprintf(str, "Unable to remove obj %p. Was it created? Has it already been destroyed?", pObj);')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600993 header_txt.append(' layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_DESTROY_OBJECT_FAILED, "OBJTRACK", str);')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600994 header_txt.append('}')
Tobin Ehlis235c20e2015-01-16 08:56:30 -0700995 header_txt.append('// Set selected flag state for an object node')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600996 header_txt.append('static void set_status(void* pObj, VK_OBJECT_TYPE objType, OBJECT_STATUS status_flag) {')
Mark Lobodzinskid11fcca2015-02-09 10:16:20 -0600997 header_txt.append(' if (pObj != NULL) {')
998 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
999 header_txt.append(' while (pTrav) {')
1000 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1001 header_txt.append(' pTrav->obj.status |= status_flag;')
1002 header_txt.append(' return;')
1003 header_txt.append(' }')
1004 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001005 header_txt.append(' }')
Mark Lobodzinskid11fcca2015-02-09 10:16:20 -06001006 header_txt.append(' // If we do not find it print an error')
1007 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001008 header_txt.append(' sprintf(str, "Unable to set status for non-existent object %p of %s type", pObj, string_VK_OBJECT_TYPE(objType));')
1009 header_txt.append(' layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK", str);')
Mark Lobodzinskid11fcca2015-02-09 10:16:20 -06001010 header_txt.append(' }');
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001011 header_txt.append('}')
1012 header_txt.append('')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001013 header_txt.append('// Track selected state for an object node')
Mark Lobodzinski93fdbd22015-04-09 12:09:45 -05001014 header_txt.append('static void track_object_status(void* pObj, VkStateBindPoint stateBindPoint) {')
1015 header_txt.append(' objNode *pTrav = pObjectHead[VkObjectTypeCmdBuffer];')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001016 header_txt.append('')
1017 header_txt.append(' while (pTrav) {')
1018 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001019 header_txt.append(' if (stateBindPoint == VK_STATE_BIND_VIEWPORT) {')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001020 header_txt.append(' pTrav->obj.status |= OBJSTATUS_VIEWPORT_BOUND;')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001021 header_txt.append(' } else if (stateBindPoint == VK_STATE_BIND_RASTER) {')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001022 header_txt.append(' pTrav->obj.status |= OBJSTATUS_RASTER_BOUND;')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001023 header_txt.append(' } else if (stateBindPoint == VK_STATE_BIND_COLOR_BLEND) {')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001024 header_txt.append(' pTrav->obj.status |= OBJSTATUS_COLOR_BLEND_BOUND;')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001025 header_txt.append(' } else if (stateBindPoint == VK_STATE_BIND_DEPTH_STENCIL) {')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001026 header_txt.append(' pTrav->obj.status |= OBJSTATUS_DEPTH_STENCIL_BOUND;')
1027 header_txt.append(' }')
1028 header_txt.append(' return;')
1029 header_txt.append(' }')
1030 header_txt.append(' pTrav = pTrav->pNextObj;')
1031 header_txt.append(' }')
1032 header_txt.append(' // If we do not find it print an error')
1033 header_txt.append(' char str[1024];')
1034 header_txt.append(' sprintf(str, "Unable to track status for non-existent Command Buffer object %p", pObj);')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001035 header_txt.append(' layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK", str);')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001036 header_txt.append('}')
1037 header_txt.append('')
1038 header_txt.append('// Reset selected flag state for an object node')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001039 header_txt.append('static void reset_status(void* pObj, VK_OBJECT_TYPE objType, OBJECT_STATUS status_flag) {')
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001040 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
1041 header_txt.append(' while (pTrav) {')
1042 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001043 header_txt.append(' pTrav->obj.status &= ~status_flag;')
1044 header_txt.append(' return;')
1045 header_txt.append(' }')
1046 header_txt.append(' pTrav = pTrav->pNextObj;')
1047 header_txt.append(' }')
1048 header_txt.append(' // If we do not find it print an error')
1049 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001050 header_txt.append(' sprintf(str, "Unable to reset status for non-existent object %p of %s type", pObj, string_VK_OBJECT_TYPE(objType));')
1051 header_txt.append(' layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK", str);')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001052 header_txt.append('}')
1053 header_txt.append('')
1054 header_txt.append('// Check object status for selected flag state')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001055 header_txt.append('static bool32_t validate_status(void* pObj, VK_OBJECT_TYPE objType, OBJECT_STATUS status_mask, OBJECT_STATUS status_flag, VK_DBG_MSG_TYPE error_level, OBJECT_TRACK_ERROR error_code, char* fail_msg) {')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001056 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
1057 header_txt.append(' while (pTrav) {')
1058 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
Mark Lobodzinski4186e712015-02-03 11:52:26 -06001059 header_txt.append(' if ((pTrav->obj.status & status_mask) != status_flag) {')
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001060 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001061 header_txt.append(' sprintf(str, "OBJECT VALIDATION WARNING: %s object %p: %s", string_VK_OBJECT_TYPE(objType), (void*)pObj, fail_msg);')
1062 header_txt.append(' layerCbMsg(error_level, VK_VALIDATION_LEVEL_0, pObj, 0, error_code, "OBJTRACK", str);')
1063 header_txt.append(' return VK_FALSE;')
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001064 header_txt.append(' }')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001065 header_txt.append(' return VK_TRUE;')
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001066 header_txt.append(' }')
1067 header_txt.append(' pTrav = pTrav->pNextObj;')
1068 header_txt.append(' }')
Mark Lobodzinski93fdbd22015-04-09 12:09:45 -05001069 header_txt.append(' if (objType != VkObjectTypePresentableImageMemory) {')
Mark Lobodzinskiacb93682015-03-05 12:39:33 -06001070 header_txt.append(' // If we do not find it print an error')
1071 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001072 header_txt.append(' sprintf(str, "Unable to obtain status for non-existent object %p of %s type", pObj, string_VK_OBJECT_TYPE(objType));')
1073 header_txt.append(' layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK", str);')
Mark Lobodzinskiacb93682015-03-05 12:39:33 -06001074 header_txt.append(' }')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001075 header_txt.append(' return VK_FALSE;')
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001076 header_txt.append('}')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001077 header_txt.append('')
1078 header_txt.append('static void validate_draw_state_flags(void* pObj) {')
Mark Lobodzinski93fdbd22015-04-09 12:09:45 -05001079 header_txt.append(' validate_status((void*)pObj, VkObjectTypeCmdBuffer, OBJSTATUS_VIEWPORT_BOUND, OBJSTATUS_VIEWPORT_BOUND, VK_DBG_MSG_ERROR, OBJTRACK_VIEWPORT_NOT_BOUND, "Viewport object not bound to this command buffer");')
1080 header_txt.append(' validate_status((void*)pObj, VkObjectTypeCmdBuffer, OBJSTATUS_RASTER_BOUND, OBJSTATUS_RASTER_BOUND, VK_DBG_MSG_ERROR, OBJTRACK_RASTER_NOT_BOUND, "Raster object not bound to this command buffer");')
1081 header_txt.append(' validate_status((void*)pObj, VkObjectTypeCmdBuffer, OBJSTATUS_COLOR_BLEND_BOUND, OBJSTATUS_COLOR_BLEND_BOUND, VK_DBG_MSG_UNKNOWN, OBJTRACK_COLOR_BLEND_NOT_BOUND, "Color-blend object not bound to this command buffer");')
1082 header_txt.append(' validate_status((void*)pObj, VkObjectTypeCmdBuffer, OBJSTATUS_DEPTH_STENCIL_BOUND, OBJSTATUS_DEPTH_STENCIL_BOUND, VK_DBG_MSG_UNKNOWN, OBJTRACK_DEPTH_STENCIL_NOT_BOUND, "Depth-stencil object not bound to this command buffer");')
1083 header_txt.append('}')
1084 header_txt.append('')
Courtney Goeltzenleuchter304a1f82015-04-07 16:23:00 -06001085 header_txt.append('static void setGpuQueueInfoState(void *pData) {')
Mark Lobodzinski93fdbd22015-04-09 12:09:45 -05001086 header_txt.append(' maxMemReferences = ((VkPhysicalGpuQueueProperties *)pData)->maxMemReferences;')
Mark Lobodzinskie1d3f0c2015-02-09 10:20:53 -06001087 header_txt.append('}')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001088 return "\n".join(header_txt)
1089
Mike Stroyan3aecdb42015-04-03 17:13:23 -06001090 def generate_intercept(self, proto, qual):
Jon Ashburneb2728b2015-04-10 14:33:07 -06001091 if proto.name in [ 'DbgRegisterMsgCallback', 'DbgUnregisterMsgCallback', 'GetGlobalExtensionInfo' ]:
Mike Stroyan723913e2015-04-03 14:39:16 -06001092 # use default version
1093 return None
Tobin Ehlisf29da382015-04-15 07:46:12 -06001094 obj_type_mapping = {base_t : base_t.replace("Vk", "VkObjectType") for base_t in vulkan.object_type_list}
Mike Stroyan723913e2015-04-03 14:39:16 -06001095 # For the various "super-types" we have to use function to distinguish sub type
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001096 for obj_type in ["VK_BASE_OBJECT", "VK_OBJECT", "VK_DYNAMIC_STATE_OBJECT", "VkObject", "VkBaseObject"]:
Mike Stroyan723913e2015-04-03 14:39:16 -06001097 obj_type_mapping[obj_type] = "ll_get_obj_type(object)"
1098
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001099 decl = proto.c_func(prefix="vk", attr="VKAPI")
Mike Stroyan723913e2015-04-03 14:39:16 -06001100 param0_name = proto.params[0].name
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001101 p0_type = proto.params[0].ty.strip('*').replace('const ', '')
Mike Stroyan723913e2015-04-03 14:39:16 -06001102 create_line = ''
1103 destroy_line = ''
1104 funcs = []
1105 # Special cases for API funcs that don't use an object as first arg
Jon Ashburneb2728b2015-04-10 14:33:07 -06001106 if True in [no_use_proto in proto.name for no_use_proto in ['GlobalOption', 'CreateInstance', 'QueueSubmit', 'QueueAddMemReference', 'QueueRemoveMemReference', 'QueueWaitIdle', 'GetGlobalExtensionInfo', 'CreateDevice', 'GetGpuInfo', 'QueueSignalSemaphore', 'QueueWaitSemaphore', 'WsiX11QueuePresent']]:
Mike Stroyan723913e2015-04-03 14:39:16 -06001107 using_line = ''
1108 else:
1109 using_line = ' loader_platform_thread_lock_mutex(&objLock);\n'
1110 using_line += ' ll_increment_use_count((void*)%s, %s);\n' % (param0_name, obj_type_mapping[p0_type])
1111 using_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
1112 if 'QueueSubmit' in proto.name:
Tobin Ehlisf29da382015-04-15 07:46:12 -06001113 using_line += ' set_status((void*)fence, VkObjectTypeFence, OBJSTATUS_FENCE_IS_SUBMITTED);\n'
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001114 using_line += ' // TODO: Fix for updated memory reference mechanism\n'
1115 using_line += ' // validate_memory_mapping_status(pMemRefs, memRefCount);\n'
1116 using_line += ' // validate_mem_ref_count(memRefCount);\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001117 elif 'GetFenceStatus' in proto.name:
1118 using_line += ' // Warn if submitted_flag is not set\n'
Tobin Ehlisf29da382015-04-15 07:46:12 -06001119 using_line += ' validate_status((void*)fence, VkObjectTypeFence, OBJSTATUS_FENCE_IS_SUBMITTED, OBJSTATUS_FENCE_IS_SUBMITTED, VK_DBG_MSG_ERROR, OBJTRACK_INVALID_FENCE, "Status Requested for Unsubmitted Fence");\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001120 elif 'EndCommandBuffer' in proto.name:
Tobin Ehlisf29da382015-04-15 07:46:12 -06001121 using_line += ' reset_status((void*)cmdBuffer, VkObjectTypeCmdBuffer, (OBJSTATUS_VIEWPORT_BOUND |\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001122 using_line += ' OBJSTATUS_RASTER_BOUND |\n'
1123 using_line += ' OBJSTATUS_COLOR_BLEND_BOUND |\n'
1124 using_line += ' OBJSTATUS_DEPTH_STENCIL_BOUND));\n'
1125 elif 'CmdBindDynamicStateObject' in proto.name:
1126 using_line += ' track_object_status((void*)cmdBuffer, stateBindPoint);\n'
1127 elif 'CmdDraw' in proto.name:
1128 using_line += ' validate_draw_state_flags((void *)cmdBuffer);\n'
1129 elif 'MapMemory' in proto.name:
Tobin Ehlisf29da382015-04-15 07:46:12 -06001130 using_line += ' set_status((void*)mem, VkObjectTypeGpuMemory, OBJSTATUS_GPU_MEM_MAPPED);\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001131 elif 'UnmapMemory' in proto.name:
Tobin Ehlisf29da382015-04-15 07:46:12 -06001132 using_line += ' reset_status((void*)mem, VkObjectTypeGpuMemory, OBJSTATUS_GPU_MEM_MAPPED);\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001133 if 'AllocDescriptor' in proto.name: # Allocates array of DSs
1134 create_line = ' for (uint32_t i = 0; i < *pCount; i++) {\n'
1135 create_line += ' loader_platform_thread_lock_mutex(&objLock);\n'
Tobin Ehlisf29da382015-04-15 07:46:12 -06001136 create_line += ' ll_insert_obj((void*)pDescriptorSets[i], VkObjectTypeDescriptorSet);\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001137 create_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
1138 create_line += ' }\n'
1139 elif 'CreatePresentableImage' in proto.name:
1140 create_line = ' loader_platform_thread_lock_mutex(&objLock);\n'
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001141 create_line += ' ll_insert_obj((void*)*%s, %s);\n' % (proto.params[-2].name, obj_type_mapping[proto.params[-2].ty.strip('*').replace('const ', '')])
Tobin Ehlisf29da382015-04-15 07:46:12 -06001142 create_line += ' ll_insert_obj((void*)*pMem, VkObjectTypePresentableImageMemory);\n'
Tobin Ehlis293ff632015-04-15 13:20:56 -06001143
Mike Stroyan723913e2015-04-03 14:39:16 -06001144 create_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
1145 elif 'Create' in proto.name or 'Alloc' in proto.name:
1146 create_line = ' loader_platform_thread_lock_mutex(&objLock);\n'
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001147 create_line += ' ll_insert_obj((void*)*%s, %s);\n' % (proto.params[-1].name, obj_type_mapping[proto.params[-1].ty.strip('*').replace('const ', '')])
Mike Stroyan723913e2015-04-03 14:39:16 -06001148 create_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
1149 if 'DestroyObject' in proto.name:
1150 destroy_line = ' loader_platform_thread_lock_mutex(&objLock);\n'
1151 destroy_line += ' ll_destroy_obj((void*)%s);\n' % (param0_name)
1152 destroy_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
1153 using_line = ''
1154 else:
1155 if 'Destroy' in proto.name or 'Free' in proto.name:
1156 destroy_line = ' loader_platform_thread_lock_mutex(&objLock);\n'
1157 destroy_line += ' ll_destroy_obj((void*)%s);\n' % (param0_name)
1158 destroy_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
1159 using_line = ''
1160 if 'DestroyDevice' in proto.name:
1161 destroy_line += ' // Report any remaining objects in LL\n objNode *pTrav = pGlobalHead;\n while (pTrav) {\n'
Tobin Ehlisf29da382015-04-15 07:46:12 -06001162 destroy_line += ' if (pTrav->obj.objType == VkObjectTypePresentableImageMemory) {\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001163 destroy_line += ' objNode *pDel = pTrav;\n'
1164 destroy_line += ' pTrav = pTrav->pNextGlobal;\n'
1165 destroy_line += ' ll_destroy_obj((void*)(pDel->obj.pObj));\n'
1166 destroy_line += ' } else {\n'
1167 destroy_line += ' char str[1024];\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001168 destroy_line += ' sprintf(str, "OBJ ERROR : %s object %p has not been destroyed (was used %lu times).", string_VK_OBJECT_TYPE(pTrav->obj.objType), pTrav->obj.pObj, pTrav->obj.numUses);\n'
1169 destroy_line += ' layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, device, 0, OBJTRACK_OBJECT_LEAK, "OBJTRACK", str);\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001170 destroy_line += ' pTrav = pTrav->pNextGlobal;\n'
1171 destroy_line += ' }\n'
1172 destroy_line += ' }\n'
Mark Lobodzinski93fdbd22015-04-09 12:09:45 -05001173 destroy_line += ' // Clean up Queue\'s MemRef Linked Lists\n'
1174 destroy_line += ' destroyQueueMemRefLists();\n'
1175 if 'GetDeviceQueue' in proto.name:
1176 destroy_line = ' addQueueInfo(*pQueue);\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001177 ret_val = ''
1178 stmt = ''
1179 if proto.ret != "void":
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001180 ret_val = "VkResult result = "
Mike Stroyan723913e2015-04-03 14:39:16 -06001181 stmt = " return result;\n"
1182 if 'WsiX11AssociateConnection' == proto.name:
1183 funcs.append("#if defined(__linux__) || defined(XCB_NVIDIA)")
1184 if proto.name == "EnumerateLayers":
Mike Stroyan723913e2015-04-03 14:39:16 -06001185 funcs.append('%s%s\n'
1186 '{\n'
1187 ' if (gpu != NULL) {\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001188 ' %s'
Jon Ashburn630e44f2015-04-08 21:33:34 -06001189 ' pCurObj = (VkBaseLayerObject *) gpu;\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001190 ' loader_platform_thread_once(&tabOnce, init%s);\n'
1191 ' %snextTable.%s;\n'
1192 ' %s%s'
1193 ' %s'
1194 ' } else {\n'
1195 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001196 ' return VK_ERROR_INVALID_POINTER;\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001197 ' // This layer compatible with all GPUs\n'
1198 ' *pOutLayerCount = 1;\n'
1199 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001200 ' return VK_SUCCESS;\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001201 ' }\n'
Jon Ashburn630e44f2015-04-08 21:33:34 -06001202 '}' % (qual, decl, using_line, self.layer_name, ret_val, proto.c_call(), create_line, destroy_line, stmt, self.layer_name))
Jon Ashburn630e44f2015-04-08 21:33:34 -06001203 elif 'GetGpuInfo' in proto.name:
1204 gpu_state = ' if (infoType == VK_INFO_TYPE_PHYSICAL_GPU_QUEUE_PROPERTIES) {\n'
1205 gpu_state += ' if (pData != NULL) {\n'
1206 gpu_state += ' setGpuQueueInfoState(pData);\n'
1207 gpu_state += ' }\n'
1208 gpu_state += ' }\n'
1209 funcs.append('%s%s\n'
1210 '{\n'
1211 '%s'
1212 ' pCurObj = (VkBaseLayerObject *) gpu;\n'
1213 ' loader_platform_thread_once(&tabOnce, init%s);\n'
1214 ' %snextTable.%s;\n'
1215 '%s%s'
1216 '%s'
1217 '%s'
1218 '}' % (qual, decl, using_line, self.layer_name, ret_val, proto.c_call(), create_line, destroy_line, gpu_state, stmt))
1219 else:
Mike Stroyan723913e2015-04-03 14:39:16 -06001220 funcs.append('%s%s\n'
1221 '{\n'
1222 '%s'
1223 ' %snextTable.%s;\n'
1224 '%s%s'
1225 '%s'
1226 '}' % (qual, decl, using_line, ret_val, proto.c_call(), create_line, destroy_line, stmt))
Mike Stroyan723913e2015-04-03 14:39:16 -06001227 if 'WsiX11QueuePresent' == proto.name:
1228 funcs.append("#endif")
1229 return "\n\n".join(funcs)
1230
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001231 def generate_body(self):
Mike Stroyan3aecdb42015-04-03 17:13:23 -06001232 self.layer_name = "ObjectTracker"
1233 body = [self._generate_layer_initialization(True, lockname='obj'),
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001234 self._generate_dispatch_entrypoints("VK_LAYER_EXPORT"),
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001235 self._generate_extensions(),
Mike Stroyan2ad66f12015-04-03 17:45:53 -06001236 self._generate_layer_gpa_function(extensions=['objTrackGetObjectCount', 'objTrackGetObjects'])]
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001237
1238 return "\n\n".join(body)
Courtney Goeltzenleuchtere6094fc2014-11-18 10:40:29 -07001239
Mike Stroyanb326d2c2015-04-02 11:59:05 -06001240class ThreadingSubcommand(Subcommand):
1241 def generate_header(self):
1242 header_txt = []
1243 header_txt.append('#include <stdio.h>')
1244 header_txt.append('#include <stdlib.h>')
1245 header_txt.append('#include <string.h>')
1246 header_txt.append('#include <unordered_map>')
1247 header_txt.append('#include "loader_platform.h"')
1248 header_txt.append('#include "vkLayer.h"')
1249 header_txt.append('#include "threading.h"')
1250 header_txt.append('#include "layers_config.h"')
1251 header_txt.append('#include "vk_enum_validate_helper.h"')
1252 header_txt.append('#include "vk_struct_validate_helper.h"')
1253 header_txt.append('//The following is #included again to catch certain OS-specific functions being used:')
1254 header_txt.append('#include "loader_platform.h"\n')
1255 header_txt.append('#include "layers_msg.h"\n')
1256 header_txt.append('static VkLayerDispatchTable nextTable;')
1257 header_txt.append('static VkBaseLayerObject *pCurObj;')
1258 header_txt.append('static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(tabOnce);\n')
1259 header_txt.append('using namespace std;')
1260 header_txt.append('static unordered_map<int, void*> proxy_objectsInUse;\n')
1261 header_txt.append('static unordered_map<VkObject, loader_platform_thread_id> objectsInUse;\n')
1262 header_txt.append('static int threadingLockInitialized = 0;')
1263 header_txt.append('static loader_platform_thread_mutex threadingLock;')
1264 header_txt.append('static int printLockInitialized = 0;')
1265 header_txt.append('static loader_platform_thread_mutex printLock;\n')
1266 header_txt.append('')
1267 header_txt.append('static void useObject(VkObject object, const char* type)')
1268 header_txt.append('{')
1269 header_txt.append(' loader_platform_thread_id tid = loader_platform_get_thread_id();')
1270 header_txt.append(' loader_platform_thread_lock_mutex(&threadingLock);')
1271 header_txt.append(' if (objectsInUse.find(object) == objectsInUse.end()) {')
1272 header_txt.append(' objectsInUse[object] = tid;')
1273 header_txt.append(' } else {')
1274 header_txt.append(' if (objectsInUse[object] == tid) {')
1275 header_txt.append(' char str[1024];')
1276 header_txt.append(' sprintf(str, "THREADING ERROR : object of type %s is simultaneously used in thread %ld and thread %ld", type, objectsInUse[object], tid);')
1277 header_txt.append(' layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, 0, 0, THREADING_CHECKER_MULTIPLE_THREADS, "THREADING", str);')
1278 header_txt.append(' } else {')
1279 header_txt.append(' char str[1024];')
1280 header_txt.append(' sprintf(str, "THREADING ERROR : object of type %s is recursively used in thread %ld", type, tid);')
1281 header_txt.append(' layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, 0, 0, THREADING_CHECKER_SINGLE_THREAD_REUSE, "THREADING", str);')
1282 header_txt.append(' }')
1283 header_txt.append(' }')
1284 header_txt.append(' loader_platform_thread_unlock_mutex(&threadingLock);')
1285 header_txt.append('}')
1286 header_txt.append('static void finishUsingObject(VkObject object)')
1287 header_txt.append('{')
1288 header_txt.append(' // Object is no longer in use')
1289 header_txt.append(' loader_platform_thread_lock_mutex(&threadingLock);')
1290 header_txt.append(' objectsInUse.erase(object);')
1291 header_txt.append(' loader_platform_thread_unlock_mutex(&threadingLock);')
1292 header_txt.append('}')
1293 return "\n".join(header_txt)
1294
1295 def generate_intercept(self, proto, qual):
1296 if proto.name in [ 'DbgRegisterMsgCallback', 'DbgUnregisterMsgCallback' ]:
1297 # use default version
1298 return None
1299 decl = proto.c_func(prefix="vk", attr="VKAPI")
1300 ret_val = ''
1301 stmt = ''
1302 funcs = []
1303 if proto.ret != "void":
1304 ret_val = "VkResult result = "
1305 stmt = " return result;\n"
1306 if proto.name == "EnumerateLayers":
1307 funcs.append('%s%s\n'
1308 '{\n'
1309 ' char str[1024];\n'
1310 ' if (gpu != NULL) {\n'
1311 ' pCurObj = (VkBaseLayerObject *) %s;\n'
1312 ' loader_platform_thread_once(&tabOnce, init%s);\n'
1313 ' %snextTable.%s;\n'
1314 ' fflush(stdout);\n'
1315 ' %s'
1316 ' } else {\n'
1317 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
1318 ' return VK_ERROR_INVALID_POINTER;\n'
1319 ' // This layer compatible with all GPUs\n'
1320 ' *pOutLayerCount = 1;\n'
1321 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
1322 ' return VK_SUCCESS;\n'
1323 ' }\n'
1324 '}' % (qual, decl, proto.params[0].name, self.layer_name, ret_val, proto.c_call(), stmt, self.layer_name))
1325 # All functions that do a Get are thread safe
1326 elif 'Get' in proto.name:
1327 return None
1328 # All Wsi functions are thread safe
1329 elif 'WsiX11' in proto.name:
1330 return None
1331 # All functions that start with a device parameter are thread safe
1332 elif proto.params[0].ty in { "VkDevice" }:
1333 return None
1334 # Only watch core objects passed as first parameter
1335 elif proto.params[0].ty not in vulkan.core.objects:
1336 return None
1337 elif proto.params[0].ty != "VkPhysicalGpu":
1338 funcs.append('%s%s\n'
1339 '{\n'
1340 ' useObject((VkObject) %s, "%s");\n'
1341 ' %snextTable.%s;\n'
1342 ' finishUsingObject((VkObject) %s);\n'
1343 '%s'
1344 '}' % (qual, decl, proto.params[0].name, proto.params[0].ty, ret_val, proto.c_call(), proto.params[0].name, stmt))
1345 else:
1346 funcs.append('%s%s\n'
1347 '{\n'
1348 ' pCurObj = (VkBaseLayerObject *) %s;\n'
1349 ' loader_platform_thread_once(&tabOnce, init%s);\n'
1350 ' %snextTable.%s;\n'
1351 '%s'
1352 '}' % (qual, decl, proto.params[0].name, self.layer_name, ret_val, proto.c_call(), stmt))
1353 return "\n\n".join(funcs)
1354
1355 def generate_body(self):
1356 self.layer_name = "Threading"
1357 body = [self._generate_layer_initialization(True, lockname='threading'),
1358 self._generate_dispatch_entrypoints("VK_LAYER_EXPORT"),
1359 self._generate_layer_gpa_function()]
1360 return "\n\n".join(body)
1361
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001362def main():
1363 subcommands = {
1364 "layer-funcs" : LayerFuncsSubcommand,
1365 "layer-dispatch" : LayerDispatchSubcommand,
Tobin Ehlis907a0522014-11-25 16:59:27 -07001366 "Generic" : GenericLayerSubcommand,
Tobin Ehlis4a636a12015-04-09 09:19:36 -06001367 "APIDump" : APIDumpSubcommand,
Tobin Ehlis907a0522014-11-25 16:59:27 -07001368 "ObjectTracker" : ObjectTrackerSubcommand,
Mike Stroyanb326d2c2015-04-02 11:59:05 -06001369 "Threading" : ThreadingSubcommand,
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001370 }
1371
Tobin Ehlis6cd06372014-12-17 17:44:50 -07001372 if len(sys.argv) < 3 or sys.argv[1] not in subcommands or not os.path.exists(sys.argv[2]):
1373 print("Usage: %s <subcommand> <input_header> [options]" % sys.argv[0])
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001374 print
Tobin Ehlis2f3726c2015-01-15 17:51:52 -07001375 print("Available subcommands are: %s" % " ".join(subcommands))
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001376 exit(1)
1377
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001378 hfp = vk_helper.HeaderFileParser(sys.argv[2])
Tobin Ehlis6cd06372014-12-17 17:44:50 -07001379 hfp.parse()
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001380 vk_helper.enum_val_dict = hfp.get_enum_val_dict()
1381 vk_helper.enum_type_dict = hfp.get_enum_type_dict()
1382 vk_helper.struct_dict = hfp.get_struct_dict()
1383 vk_helper.typedef_fwd_dict = hfp.get_typedef_fwd_dict()
1384 vk_helper.typedef_rev_dict = hfp.get_typedef_rev_dict()
1385 vk_helper.types_dict = hfp.get_types_dict()
Tobin Ehlis6cd06372014-12-17 17:44:50 -07001386
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001387 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
1388 subcmd.run()
1389
1390if __name__ == "__main__":
1391 main()