blob: 44c0b3698a36d3b7c136aa404a4d2c581d254ddc [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)
Tobin Ehlisc99cb0d2015-04-16 15:56:11 -0600675 if prev_count_name != '' and (prev_count_name.replace('Count', '')[1:] in p.name):
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600676 sp_param_dict[pindex] = prev_count_name
Tobin Ehlisc99cb0d2015-04-16 15:56:11 -0600677 prev_count_name = ''
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600678 elif 'pDescriptorSets' == p.name and proto.params[-1].name == 'pCount':
679 sp_param_dict[pindex] = '*pCount'
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600680 elif 'Wsi' not in proto.name and vk_helper.is_type(p.ty.strip('*').replace('const ', ''), 'struct'):
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600681 sp_param_dict[pindex] = 'index'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600682 if p.name.endswith('Count'):
683 if '*' in p.ty:
684 prev_count_name = "*%s" % p.name
685 else:
686 prev_count_name = p.name
Jon Ashburneb2728b2015-04-10 14:33:07 -0600687 pindex += 1
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600688 log_func = log_func.strip(', ')
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600689 log_func_no_addr = log_func_no_addr.strip(', ')
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600690 if proto.ret != "void":
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600691 log_func += ') = " << string_VkResult((VkResult)result) << endl'
692 log_func_no_addr += ') = " << string_VkResult((VkResult)result) << endl'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600693 else:
694 log_func += ')\\n"'
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600695 log_func_no_addr += ')\\n"'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600696 log_func += ';'
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600697 log_func_no_addr += ';'
698 log_func += '\n }\n else {%s;\n }' % log_func_no_addr;
Tobin Ehlisf29da382015-04-15 07:46:12 -0600699 #print("Proto %s has param_dict: %s" % (proto.name, sp_param_dict))
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600700 if len(sp_param_dict) > 0:
701 i_decl = False
702 log_func += '\n string tmp_str;'
703 for sp_index in sp_param_dict:
Tobin Ehlisf29da382015-04-15 07:46:12 -0600704 #print("sp_index: %s" % str(sp_index))
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600705 if 'index' == sp_param_dict[sp_index]:
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600706 cis_print_func = 'vk_print_%s' % (proto.params[sp_index].ty.replace('const ', '').strip('*').lower())
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600707 local_name = proto.params[sp_index].name
708 if '*' not in proto.params[sp_index].ty:
709 local_name = '&%s' % proto.params[sp_index].name
710 log_func += '\n if (%s) {' % (local_name)
711 log_func += '\n tmp_str = %s(%s, " ");' % (cis_print_func, local_name)
712 log_func += '\n (*outputStream) << " %s (" << %s << ")" << endl << tmp_str << endl;' % (local_name, local_name)
713 log_func += '\n }'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600714 else: # We have a count value stored to iterate over an array
715 print_cast = ''
716 print_func = ''
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600717 if vk_helper.is_type(proto.params[sp_index].ty.strip('*').replace('const ', ''), 'struct'):
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600718 print_cast = '&'
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600719 print_func = 'vk_print_%s' % proto.params[sp_index].ty.replace('const ', '').strip('*').lower()
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600720 else:
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600721 print_cast = ''
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600722 print_func = 'string_convert_helper'
723 #cis_print_func = 'tmp_str = string_convert_helper((void*)%s[i], " ");' % proto.params[sp_index].name
724 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 -0600725 if not i_decl:
726 log_func += '\n uint32_t i;'
727 i_decl = True
728 log_func += '\n for (i = 0; i < %s; i++) {' % (sp_param_dict[sp_index])
729 log_func += '\n %s' % (cis_print_func)
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600730 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 -0600731 log_func += '\n }'
732 if 'WsiX11AssociateConnection' == proto.name:
733 funcs.append("#if defined(__linux__) || defined(XCB_NVIDIA)")
734 if proto.name == "EnumerateLayers":
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600735 c_call = proto.c_call().replace("(" + proto.params[0].name, "((VkPhysicalGpu)gpuw->nextObject", 1)
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600736 funcs.append('%s%s\n'
737 '{\n'
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600738 ' using namespace StreamControl;\n'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600739 ' if (gpu != NULL) {\n'
Jon Ashburn630e44f2015-04-08 21:33:34 -0600740 ' pCurObj = (VkBaseLayerObject *) gpu;\n'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600741 ' loader_platform_thread_once(&tabOnce, init%s);\n'
742 ' %snextTable.%s;\n'
743 ' %s %s %s\n'
744 ' %s'
745 ' } else {\n'
746 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600747 ' return VK_ERROR_INVALID_POINTER;\n'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600748 ' // This layer compatible with all GPUs\n'
749 ' *pOutLayerCount = 1;\n'
750 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600751 ' return VK_SUCCESS;\n'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600752 ' }\n'
Jon Ashburn630e44f2015-04-08 21:33:34 -0600753 '}' % (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 -0600754 else:
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600755 funcs.append('%s%s\n'
756 '{\n'
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600757 ' using namespace StreamControl;\n'
Jon Ashburn301c5f02015-04-06 10:58:22 -0600758 ' %snextTable.%s;\n'
759 ' %s%s%s\n'
760 '%s'
761 '}' % (qual, decl, ret_val, proto.c_call(), f_open, log_func, f_close, stmt))
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600762 if 'WsiX11QueuePresent' == proto.name:
763 funcs.append("#endif")
764 return "\n\n".join(funcs)
765
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700766 def generate_body(self):
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600767 self.layer_name = "APIDump"
768 body = [self.generate_init(),
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600769 self._generate_dispatch_entrypoints("VK_LAYER_EXPORT"),
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600770 self._generate_layer_gpa_function()]
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700771 return "\n\n".join(body)
772
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600773class ObjectTrackerSubcommand(Subcommand):
774 def generate_header(self):
775 header_txt = []
Ian Elliott81ac44c2015-01-13 17:52:38 -0700776 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 -0600777 header_txt.append('#include "object_track.h"\n\nstatic VkLayerDispatchTable nextTable;\nstatic VkBaseLayerObject *pCurObj;')
Ian Elliott20f06872015-02-12 17:08:34 -0700778 header_txt.append('// The following is #included again to catch certain OS-specific functions being used:')
779 header_txt.append('#include "loader_platform.h"')
Jon Ashburn7a2da4f2015-02-17 11:03:12 -0700780 header_txt.append('#include "layers_config.h"')
Jon Ashburn21001f62015-02-16 08:26:50 -0700781 header_txt.append('#include "layers_msg.h"')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700782 header_txt.append('static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(tabOnce);')
783 header_txt.append('static long long unsigned int object_track_index = 0;')
Tobin Ehlis84a8a9b2015-02-23 14:09:16 -0700784 header_txt.append('static int objLockInitialized = 0;')
785 header_txt.append('static loader_platform_thread_mutex objLock;')
Jon Ashburn21001f62015-02-16 08:26:50 -0700786 header_txt.append('')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700787 header_txt.append('// We maintain a "Global" list which links every object and a')
788 header_txt.append('// per-Object list which just links objects of a given type')
789 header_txt.append('// The object node has both pointers so the actual nodes are shared between the two lists')
790 header_txt.append('typedef struct _objNode {')
791 header_txt.append(' OBJTRACK_NODE obj;')
792 header_txt.append(' struct _objNode *pNextObj;')
793 header_txt.append(' struct _objNode *pNextGlobal;')
794 header_txt.append('} objNode;')
Mark Lobodzinski93fdbd22015-04-09 12:09:45 -0500795 header_txt.append('')
796 header_txt.append('static objNode *pObjectHead[VkNumObjectType] = {0};')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700797 header_txt.append('static objNode *pGlobalHead = NULL;')
Mark Lobodzinski93fdbd22015-04-09 12:09:45 -0500798 header_txt.append('static uint64_t numObjs[VkNumObjectType] = {0};')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700799 header_txt.append('static uint64_t numTotalObjs = 0;')
Courtney Goeltzenleuchter304a1f82015-04-07 16:23:00 -0600800 header_txt.append('static uint32_t maxMemReferences = 0;')
Mark Lobodzinski93fdbd22015-04-09 12:09:45 -0500801 header_txt.append('')
802 header_txt.append('// For each Queue\'s doubly linked-list of mem refs')
803 header_txt.append('typedef struct _OT_MEM_INFO {')
804 header_txt.append(' VkGpuMemory mem;')
805 header_txt.append(' struct _OT_MEM_INFO *pNextMI;')
806 header_txt.append(' struct _OT_MEM_INFO *pPrevMI;')
807 header_txt.append('')
808 header_txt.append('} OT_MEM_INFO;')
809 header_txt.append('')
810 header_txt.append('// Track Queue information')
811 header_txt.append('typedef struct _OT_QUEUE_INFO {')
812 header_txt.append(' OT_MEM_INFO *pMemRefList;')
813 header_txt.append(' struct _OT_QUEUE_INFO *pNextQI;')
814 header_txt.append(' VkQueue queue;')
815 header_txt.append(' uint32_t refCount;')
816 header_txt.append('} OT_QUEUE_INFO;')
817 header_txt.append('')
818 header_txt.append('// Global list of QueueInfo structures, one per queue')
819 header_txt.append('static OT_QUEUE_INFO *g_pQueueInfo;')
820 header_txt.append('')
821 header_txt.append('// Add new queue to head of global queue list')
822 header_txt.append('static void addQueueInfo(VkQueue queue)')
823 header_txt.append('{')
824 header_txt.append(' OT_QUEUE_INFO *pQueueInfo = malloc(sizeof(OT_QUEUE_INFO));')
825 header_txt.append(' memset(pQueueInfo, 0, sizeof(OT_QUEUE_INFO));')
826 header_txt.append(' pQueueInfo->queue = queue;')
827 header_txt.append('')
828 header_txt.append(' if (pQueueInfo != NULL) {')
829 header_txt.append(' pQueueInfo->pNextQI = g_pQueueInfo;')
830 header_txt.append(' g_pQueueInfo = pQueueInfo;')
831 header_txt.append(' }')
832 header_txt.append(' else {')
833 header_txt.append(' char str[1024];')
834 header_txt.append(' sprintf(str, "ERROR: VK_ERROR_OUT_OF_MEMORY -- could not allocate memory for Queue Information");')
835 header_txt.append(' layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, queue, 0, OBJTRACK_INTERNAL_ERROR, "OBJTRACK", str);')
836 header_txt.append(' }')
837 header_txt.append('}')
838 header_txt.append('')
Mark Lobodzinski93fdbd22015-04-09 12:09:45 -0500839 header_txt.append('// Destroy memRef lists and free all memory')
840 header_txt.append('static void destroyQueueMemRefLists()')
841 header_txt.append('{')
842 header_txt.append(' OT_QUEUE_INFO *pQueueInfo = g_pQueueInfo;')
843 header_txt.append(' OT_QUEUE_INFO *pDelQueueInfo = NULL;')
844 header_txt.append(' while (pQueueInfo != NULL) {')
845 header_txt.append(' OT_MEM_INFO *pMemInfo = pQueueInfo->pMemRefList;')
846 header_txt.append(' while (pMemInfo != NULL) {')
847 header_txt.append(' OT_MEM_INFO *pDelMemInfo = pMemInfo;')
848 header_txt.append(' pMemInfo = pMemInfo->pNextMI;')
849 header_txt.append(' free(pDelMemInfo);')
850 header_txt.append(' }')
851 header_txt.append(' pDelQueueInfo = pQueueInfo;')
852 header_txt.append(' pQueueInfo = pQueueInfo->pNextQI;')
853 header_txt.append(' free(pDelQueueInfo);')
854 header_txt.append(' }')
855 header_txt.append('}')
856 header_txt.append('')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700857 header_txt.append('// Debug function to print global list and each individual object list')
858 header_txt.append('static void ll_print_lists()')
859 header_txt.append('{')
860 header_txt.append(' objNode* pTrav = pGlobalHead;')
861 header_txt.append(' printf("=====GLOBAL OBJECT LIST (%lu total objs):\\n", numTotalObjs);')
862 header_txt.append(' while (pTrav) {')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600863 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 -0700864 header_txt.append(' pTrav = pTrav->pNextGlobal;')
865 header_txt.append(' }')
Mark Lobodzinski93fdbd22015-04-09 12:09:45 -0500866 header_txt.append(' for (uint32_t i = 0; i < VkNumObjectType; i++) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700867 header_txt.append(' pTrav = pObjectHead[i];')
868 header_txt.append(' if (pTrav) {')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600869 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 -0700870 header_txt.append(' while (pTrav) {')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600871 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 -0700872 header_txt.append(' pTrav = pTrav->pNextObj;')
873 header_txt.append(' }')
874 header_txt.append(' }')
875 header_txt.append(' }')
876 header_txt.append('}')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600877 header_txt.append('static void ll_insert_obj(void* pObj, VK_OBJECT_TYPE objType) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700878 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600879 header_txt.append(' sprintf(str, "OBJ[%llu] : CREATE %s object %p", object_track_index++, string_VK_OBJECT_TYPE(objType), (void*)pObj);')
880 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 -0700881 header_txt.append(' objNode* pNewObjNode = (objNode*)malloc(sizeof(objNode));')
882 header_txt.append(' pNewObjNode->obj.pObj = pObj;')
883 header_txt.append(' pNewObjNode->obj.objType = objType;')
Mark Lobodzinski01552702015-02-03 10:06:31 -0600884 header_txt.append(' pNewObjNode->obj.status = OBJSTATUS_NONE;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700885 header_txt.append(' pNewObjNode->obj.numUses = 0;')
886 header_txt.append(' // insert at front of global list')
887 header_txt.append(' pNewObjNode->pNextGlobal = pGlobalHead;')
888 header_txt.append(' pGlobalHead = pNewObjNode;')
889 header_txt.append(' // insert at front of object list')
890 header_txt.append(' pNewObjNode->pNextObj = pObjectHead[objType];')
891 header_txt.append(' pObjectHead[objType] = pNewObjNode;')
892 header_txt.append(' // increment obj counts')
893 header_txt.append(' numObjs[objType]++;')
894 header_txt.append(' numTotalObjs++;')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600895 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 +0800896 header_txt.append(' if (0) ll_print_lists();')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700897 header_txt.append('}')
898 header_txt.append('// Traverse global list and return type for given object')
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600899 header_txt.append('static VK_OBJECT_TYPE ll_get_obj_type(VkObject object) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700900 header_txt.append(' objNode *pTrav = pGlobalHead;')
901 header_txt.append(' while (pTrav) {')
902 header_txt.append(' if (pTrav->obj.pObj == object)')
903 header_txt.append(' return pTrav->obj.objType;')
904 header_txt.append(' pTrav = pTrav->pNextGlobal;')
905 header_txt.append(' }')
906 header_txt.append(' char str[1024];')
907 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 -0600908 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 -0500909 header_txt.append(' return VkObjectTypeUnknown;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700910 header_txt.append('}')
Chia-I Wudf142a32014-12-16 11:02:06 +0800911 header_txt.append('#if 0')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600912 header_txt.append('static uint64_t ll_get_obj_uses(void* pObj, VK_OBJECT_TYPE objType) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700913 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
914 header_txt.append(' while (pTrav) {')
915 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
916 header_txt.append(' return pTrav->obj.numUses;')
917 header_txt.append(' }')
918 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600919 header_txt.append(' }')
920 header_txt.append(' return 0;')
921 header_txt.append('}')
Chia-I Wudf142a32014-12-16 11:02:06 +0800922 header_txt.append('#endif')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600923 header_txt.append('static void ll_increment_use_count(void* pObj, VK_OBJECT_TYPE objType) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700924 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600925 header_txt.append(' while (pTrav) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700926 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
927 header_txt.append(' pTrav->obj.numUses++;')
928 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600929 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);')
930 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 -0700931 header_txt.append(' return;')
932 header_txt.append(' }')
933 header_txt.append(' pTrav = pTrav->pNextObj;')
934 header_txt.append(' }')
935 header_txt.append(' // If we do not find obj, insert it and then increment count')
936 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600937 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));')
938 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 -0700939 header_txt.append('')
940 header_txt.append(' ll_insert_obj(pObj, objType);')
941 header_txt.append(' ll_increment_use_count(pObj, objType);')
942 header_txt.append('}')
943 header_txt.append('// We usually do not know Obj type when we destroy it so have to fetch')
944 header_txt.append('// Type from global list w/ ll_destroy_obj()')
945 header_txt.append('// and then do the full removal from both lists w/ ll_remove_obj_type()')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600946 header_txt.append('static void ll_remove_obj_type(void* pObj, VK_OBJECT_TYPE objType) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700947 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
948 header_txt.append(' objNode *pPrev = pObjectHead[objType];')
949 header_txt.append(' while (pTrav) {')
950 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
951 header_txt.append(' pPrev->pNextObj = pTrav->pNextObj;')
952 header_txt.append(' // update HEAD of Obj list as needed')
953 header_txt.append(' if (pObjectHead[objType] == pTrav)')
954 header_txt.append(' pObjectHead[objType] = pTrav->pNextObj;')
955 header_txt.append(' assert(numObjs[objType] > 0);')
956 header_txt.append(' numObjs[objType]--;')
957 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600958 header_txt.append(' sprintf(str, "OBJ[%llu] : DESTROY %s object %p", object_track_index++, string_VK_OBJECT_TYPE(objType), (void*)pObj);')
959 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 -0600960 header_txt.append(' return;')
961 header_txt.append(' }')
962 header_txt.append(' pPrev = pTrav;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700963 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600964 header_txt.append(' }')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700965 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600966 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));')
967 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 -0700968 header_txt.append('}')
969 header_txt.append('// Parse global list to find obj type, then remove obj from obj type list, finally')
970 header_txt.append('// remove obj from global list')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600971 header_txt.append('static void ll_destroy_obj(void* pObj) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700972 header_txt.append(' objNode *pTrav = pGlobalHead;')
973 header_txt.append(' objNode *pPrev = pGlobalHead;')
974 header_txt.append(' while (pTrav) {')
975 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
976 header_txt.append(' ll_remove_obj_type(pObj, pTrav->obj.objType);')
977 header_txt.append(' pPrev->pNextGlobal = pTrav->pNextGlobal;')
978 header_txt.append(' // update HEAD of global list if needed')
979 header_txt.append(' if (pGlobalHead == pTrav)')
980 header_txt.append(' pGlobalHead = pTrav->pNextGlobal;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700981 header_txt.append(' assert(numTotalObjs > 0);')
982 header_txt.append(' numTotalObjs--;')
983 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600984 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));')
985 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 -0700986 header_txt.append(' free(pTrav);')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700987 header_txt.append(' return;')
988 header_txt.append(' }')
989 header_txt.append(' pPrev = pTrav;')
990 header_txt.append(' pTrav = pTrav->pNextGlobal;')
991 header_txt.append(' }')
992 header_txt.append(' char str[1024];')
993 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 -0600994 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 -0600995 header_txt.append('}')
Tobin Ehlis235c20e2015-01-16 08:56:30 -0700996 header_txt.append('// Set selected flag state for an object node')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600997 header_txt.append('static void set_status(void* pObj, VK_OBJECT_TYPE objType, OBJECT_STATUS status_flag) {')
Mark Lobodzinskid11fcca2015-02-09 10:16:20 -0600998 header_txt.append(' if (pObj != NULL) {')
999 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
1000 header_txt.append(' while (pTrav) {')
1001 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1002 header_txt.append(' pTrav->obj.status |= status_flag;')
1003 header_txt.append(' return;')
1004 header_txt.append(' }')
1005 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001006 header_txt.append(' }')
Mark Lobodzinskid11fcca2015-02-09 10:16:20 -06001007 header_txt.append(' // If we do not find it print an error')
1008 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001009 header_txt.append(' sprintf(str, "Unable to set status for non-existent object %p of %s type", pObj, string_VK_OBJECT_TYPE(objType));')
1010 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 -06001011 header_txt.append(' }');
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001012 header_txt.append('}')
1013 header_txt.append('')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001014 header_txt.append('// Track selected state for an object node')
Mark Lobodzinski93fdbd22015-04-09 12:09:45 -05001015 header_txt.append('static void track_object_status(void* pObj, VkStateBindPoint stateBindPoint) {')
1016 header_txt.append(' objNode *pTrav = pObjectHead[VkObjectTypeCmdBuffer];')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001017 header_txt.append('')
1018 header_txt.append(' while (pTrav) {')
1019 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001020 header_txt.append(' if (stateBindPoint == VK_STATE_BIND_VIEWPORT) {')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001021 header_txt.append(' pTrav->obj.status |= OBJSTATUS_VIEWPORT_BOUND;')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001022 header_txt.append(' } else if (stateBindPoint == VK_STATE_BIND_RASTER) {')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001023 header_txt.append(' pTrav->obj.status |= OBJSTATUS_RASTER_BOUND;')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001024 header_txt.append(' } else if (stateBindPoint == VK_STATE_BIND_COLOR_BLEND) {')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001025 header_txt.append(' pTrav->obj.status |= OBJSTATUS_COLOR_BLEND_BOUND;')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001026 header_txt.append(' } else if (stateBindPoint == VK_STATE_BIND_DEPTH_STENCIL) {')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001027 header_txt.append(' pTrav->obj.status |= OBJSTATUS_DEPTH_STENCIL_BOUND;')
1028 header_txt.append(' }')
1029 header_txt.append(' return;')
1030 header_txt.append(' }')
1031 header_txt.append(' pTrav = pTrav->pNextObj;')
1032 header_txt.append(' }')
1033 header_txt.append(' // If we do not find it print an error')
1034 header_txt.append(' char str[1024];')
1035 header_txt.append(' sprintf(str, "Unable to track status for non-existent Command Buffer object %p", pObj);')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001036 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 -06001037 header_txt.append('}')
1038 header_txt.append('')
1039 header_txt.append('// Reset selected flag state for an object node')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001040 header_txt.append('static void reset_status(void* pObj, VK_OBJECT_TYPE objType, OBJECT_STATUS status_flag) {')
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001041 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
1042 header_txt.append(' while (pTrav) {')
1043 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001044 header_txt.append(' pTrav->obj.status &= ~status_flag;')
1045 header_txt.append(' return;')
1046 header_txt.append(' }')
1047 header_txt.append(' pTrav = pTrav->pNextObj;')
1048 header_txt.append(' }')
1049 header_txt.append(' // If we do not find it print an error')
1050 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001051 header_txt.append(' sprintf(str, "Unable to reset status for non-existent object %p of %s type", pObj, string_VK_OBJECT_TYPE(objType));')
1052 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 -06001053 header_txt.append('}')
1054 header_txt.append('')
1055 header_txt.append('// Check object status for selected flag state')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001056 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 -06001057 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
1058 header_txt.append(' while (pTrav) {')
1059 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
Mark Lobodzinski4186e712015-02-03 11:52:26 -06001060 header_txt.append(' if ((pTrav->obj.status & status_mask) != status_flag) {')
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001061 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001062 header_txt.append(' sprintf(str, "OBJECT VALIDATION WARNING: %s object %p: %s", string_VK_OBJECT_TYPE(objType), (void*)pObj, fail_msg);')
1063 header_txt.append(' layerCbMsg(error_level, VK_VALIDATION_LEVEL_0, pObj, 0, error_code, "OBJTRACK", str);')
1064 header_txt.append(' return VK_FALSE;')
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001065 header_txt.append(' }')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001066 header_txt.append(' return VK_TRUE;')
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001067 header_txt.append(' }')
1068 header_txt.append(' pTrav = pTrav->pNextObj;')
1069 header_txt.append(' }')
Mark Lobodzinski93fdbd22015-04-09 12:09:45 -05001070 header_txt.append(' if (objType != VkObjectTypePresentableImageMemory) {')
Mark Lobodzinskiacb93682015-03-05 12:39:33 -06001071 header_txt.append(' // If we do not find it print an error')
1072 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001073 header_txt.append(' sprintf(str, "Unable to obtain status for non-existent object %p of %s type", pObj, string_VK_OBJECT_TYPE(objType));')
1074 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 -06001075 header_txt.append(' }')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001076 header_txt.append(' return VK_FALSE;')
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001077 header_txt.append('}')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001078 header_txt.append('')
1079 header_txt.append('static void validate_draw_state_flags(void* pObj) {')
Mark Lobodzinski93fdbd22015-04-09 12:09:45 -05001080 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");')
1081 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");')
1082 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");')
1083 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");')
1084 header_txt.append('}')
1085 header_txt.append('')
Courtney Goeltzenleuchter304a1f82015-04-07 16:23:00 -06001086 header_txt.append('static void setGpuQueueInfoState(void *pData) {')
Mark Lobodzinski93fdbd22015-04-09 12:09:45 -05001087 header_txt.append(' maxMemReferences = ((VkPhysicalGpuQueueProperties *)pData)->maxMemReferences;')
Mark Lobodzinskie1d3f0c2015-02-09 10:20:53 -06001088 header_txt.append('}')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001089 return "\n".join(header_txt)
1090
Mike Stroyan3aecdb42015-04-03 17:13:23 -06001091 def generate_intercept(self, proto, qual):
Jon Ashburneb2728b2015-04-10 14:33:07 -06001092 if proto.name in [ 'DbgRegisterMsgCallback', 'DbgUnregisterMsgCallback', 'GetGlobalExtensionInfo' ]:
Mike Stroyan723913e2015-04-03 14:39:16 -06001093 # use default version
1094 return None
Tobin Ehlisf29da382015-04-15 07:46:12 -06001095 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 -06001096 # For the various "super-types" we have to use function to distinguish sub type
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001097 for obj_type in ["VK_BASE_OBJECT", "VK_OBJECT", "VK_DYNAMIC_STATE_OBJECT", "VkObject", "VkBaseObject"]:
Mike Stroyan723913e2015-04-03 14:39:16 -06001098 obj_type_mapping[obj_type] = "ll_get_obj_type(object)"
1099
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001100 decl = proto.c_func(prefix="vk", attr="VKAPI")
Mike Stroyan723913e2015-04-03 14:39:16 -06001101 param0_name = proto.params[0].name
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001102 p0_type = proto.params[0].ty.strip('*').replace('const ', '')
Mike Stroyan723913e2015-04-03 14:39:16 -06001103 create_line = ''
1104 destroy_line = ''
1105 funcs = []
1106 # Special cases for API funcs that don't use an object as first arg
Courtney Goeltzenleuchter46962942015-04-16 13:38:46 -06001107 if True in [no_use_proto in proto.name for no_use_proto in ['GlobalOption', 'CreateInstance', 'QueueSubmit', 'QueueAddMemReferences', 'QueueRemoveMemReferences', 'QueueWaitIdle', 'GetGlobalExtensionInfo', 'CreateDevice', 'GetGpuInfo', 'QueueSignalSemaphore', 'QueueWaitSemaphore', 'WsiX11QueuePresent']]:
Mike Stroyan723913e2015-04-03 14:39:16 -06001108 using_line = ''
1109 else:
1110 using_line = ' loader_platform_thread_lock_mutex(&objLock);\n'
1111 using_line += ' ll_increment_use_count((void*)%s, %s);\n' % (param0_name, obj_type_mapping[p0_type])
1112 using_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
1113 if 'QueueSubmit' in proto.name:
Tobin Ehlisf29da382015-04-15 07:46:12 -06001114 using_line += ' set_status((void*)fence, VkObjectTypeFence, OBJSTATUS_FENCE_IS_SUBMITTED);\n'
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001115 using_line += ' // TODO: Fix for updated memory reference mechanism\n'
1116 using_line += ' // validate_memory_mapping_status(pMemRefs, memRefCount);\n'
1117 using_line += ' // validate_mem_ref_count(memRefCount);\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001118 elif 'GetFenceStatus' in proto.name:
1119 using_line += ' // Warn if submitted_flag is not set\n'
Tobin Ehlisf29da382015-04-15 07:46:12 -06001120 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 -06001121 elif 'EndCommandBuffer' in proto.name:
Tobin Ehlisf29da382015-04-15 07:46:12 -06001122 using_line += ' reset_status((void*)cmdBuffer, VkObjectTypeCmdBuffer, (OBJSTATUS_VIEWPORT_BOUND |\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001123 using_line += ' OBJSTATUS_RASTER_BOUND |\n'
1124 using_line += ' OBJSTATUS_COLOR_BLEND_BOUND |\n'
1125 using_line += ' OBJSTATUS_DEPTH_STENCIL_BOUND));\n'
1126 elif 'CmdBindDynamicStateObject' in proto.name:
1127 using_line += ' track_object_status((void*)cmdBuffer, stateBindPoint);\n'
1128 elif 'CmdDraw' in proto.name:
1129 using_line += ' validate_draw_state_flags((void *)cmdBuffer);\n'
1130 elif 'MapMemory' in proto.name:
Tobin Ehlisf29da382015-04-15 07:46:12 -06001131 using_line += ' set_status((void*)mem, VkObjectTypeGpuMemory, OBJSTATUS_GPU_MEM_MAPPED);\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001132 elif 'UnmapMemory' in proto.name:
Tobin Ehlisf29da382015-04-15 07:46:12 -06001133 using_line += ' reset_status((void*)mem, VkObjectTypeGpuMemory, OBJSTATUS_GPU_MEM_MAPPED);\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001134 if 'AllocDescriptor' in proto.name: # Allocates array of DSs
1135 create_line = ' for (uint32_t i = 0; i < *pCount; i++) {\n'
1136 create_line += ' loader_platform_thread_lock_mutex(&objLock);\n'
Tobin Ehlisf29da382015-04-15 07:46:12 -06001137 create_line += ' ll_insert_obj((void*)pDescriptorSets[i], VkObjectTypeDescriptorSet);\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001138 create_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
1139 create_line += ' }\n'
1140 elif 'CreatePresentableImage' in proto.name:
1141 create_line = ' loader_platform_thread_lock_mutex(&objLock);\n'
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001142 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 -06001143 create_line += ' ll_insert_obj((void*)*pMem, VkObjectTypePresentableImageMemory);\n'
Tobin Ehlis293ff632015-04-15 13:20:56 -06001144
Mike Stroyan723913e2015-04-03 14:39:16 -06001145 create_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
1146 elif 'Create' in proto.name or 'Alloc' in proto.name:
1147 create_line = ' loader_platform_thread_lock_mutex(&objLock);\n'
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001148 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 -06001149 create_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
1150 if 'DestroyObject' in proto.name:
1151 destroy_line = ' loader_platform_thread_lock_mutex(&objLock);\n'
1152 destroy_line += ' ll_destroy_obj((void*)%s);\n' % (param0_name)
1153 destroy_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
1154 using_line = ''
1155 else:
1156 if 'Destroy' in proto.name or 'Free' in proto.name:
1157 destroy_line = ' loader_platform_thread_lock_mutex(&objLock);\n'
1158 destroy_line += ' ll_destroy_obj((void*)%s);\n' % (param0_name)
1159 destroy_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
1160 using_line = ''
1161 if 'DestroyDevice' in proto.name:
1162 destroy_line += ' // Report any remaining objects in LL\n objNode *pTrav = pGlobalHead;\n while (pTrav) {\n'
Tobin Ehlisf29da382015-04-15 07:46:12 -06001163 destroy_line += ' if (pTrav->obj.objType == VkObjectTypePresentableImageMemory) {\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001164 destroy_line += ' objNode *pDel = pTrav;\n'
1165 destroy_line += ' pTrav = pTrav->pNextGlobal;\n'
1166 destroy_line += ' ll_destroy_obj((void*)(pDel->obj.pObj));\n'
1167 destroy_line += ' } else {\n'
1168 destroy_line += ' char str[1024];\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001169 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'
1170 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 -06001171 destroy_line += ' pTrav = pTrav->pNextGlobal;\n'
1172 destroy_line += ' }\n'
1173 destroy_line += ' }\n'
Mark Lobodzinski93fdbd22015-04-09 12:09:45 -05001174 destroy_line += ' // Clean up Queue\'s MemRef Linked Lists\n'
1175 destroy_line += ' destroyQueueMemRefLists();\n'
1176 if 'GetDeviceQueue' in proto.name:
1177 destroy_line = ' addQueueInfo(*pQueue);\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001178 ret_val = ''
1179 stmt = ''
1180 if proto.ret != "void":
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001181 ret_val = "VkResult result = "
Mike Stroyan723913e2015-04-03 14:39:16 -06001182 stmt = " return result;\n"
1183 if 'WsiX11AssociateConnection' == proto.name:
1184 funcs.append("#if defined(__linux__) || defined(XCB_NVIDIA)")
1185 if proto.name == "EnumerateLayers":
Mike Stroyan723913e2015-04-03 14:39:16 -06001186 funcs.append('%s%s\n'
1187 '{\n'
1188 ' if (gpu != NULL) {\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001189 ' %s'
Jon Ashburn630e44f2015-04-08 21:33:34 -06001190 ' pCurObj = (VkBaseLayerObject *) gpu;\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001191 ' loader_platform_thread_once(&tabOnce, init%s);\n'
1192 ' %snextTable.%s;\n'
1193 ' %s%s'
1194 ' %s'
1195 ' } else {\n'
1196 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001197 ' return VK_ERROR_INVALID_POINTER;\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001198 ' // This layer compatible with all GPUs\n'
1199 ' *pOutLayerCount = 1;\n'
1200 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001201 ' return VK_SUCCESS;\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001202 ' }\n'
Jon Ashburn630e44f2015-04-08 21:33:34 -06001203 '}' % (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 -06001204 elif 'GetGpuInfo' in proto.name:
1205 gpu_state = ' if (infoType == VK_INFO_TYPE_PHYSICAL_GPU_QUEUE_PROPERTIES) {\n'
1206 gpu_state += ' if (pData != NULL) {\n'
1207 gpu_state += ' setGpuQueueInfoState(pData);\n'
1208 gpu_state += ' }\n'
1209 gpu_state += ' }\n'
1210 funcs.append('%s%s\n'
1211 '{\n'
1212 '%s'
1213 ' pCurObj = (VkBaseLayerObject *) gpu;\n'
1214 ' loader_platform_thread_once(&tabOnce, init%s);\n'
1215 ' %snextTable.%s;\n'
1216 '%s%s'
1217 '%s'
1218 '%s'
1219 '}' % (qual, decl, using_line, self.layer_name, ret_val, proto.c_call(), create_line, destroy_line, gpu_state, stmt))
1220 else:
Mike Stroyan723913e2015-04-03 14:39:16 -06001221 funcs.append('%s%s\n'
1222 '{\n'
1223 '%s'
1224 ' %snextTable.%s;\n'
1225 '%s%s'
1226 '%s'
1227 '}' % (qual, decl, using_line, ret_val, proto.c_call(), create_line, destroy_line, stmt))
Mike Stroyan723913e2015-04-03 14:39:16 -06001228 if 'WsiX11QueuePresent' == proto.name:
1229 funcs.append("#endif")
1230 return "\n\n".join(funcs)
1231
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001232 def generate_body(self):
Mike Stroyan3aecdb42015-04-03 17:13:23 -06001233 self.layer_name = "ObjectTracker"
1234 body = [self._generate_layer_initialization(True, lockname='obj'),
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001235 self._generate_dispatch_entrypoints("VK_LAYER_EXPORT"),
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001236 self._generate_extensions(),
Mike Stroyan2ad66f12015-04-03 17:45:53 -06001237 self._generate_layer_gpa_function(extensions=['objTrackGetObjectCount', 'objTrackGetObjects'])]
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001238
1239 return "\n\n".join(body)
Courtney Goeltzenleuchtere6094fc2014-11-18 10:40:29 -07001240
Mike Stroyanb326d2c2015-04-02 11:59:05 -06001241class ThreadingSubcommand(Subcommand):
1242 def generate_header(self):
1243 header_txt = []
1244 header_txt.append('#include <stdio.h>')
1245 header_txt.append('#include <stdlib.h>')
1246 header_txt.append('#include <string.h>')
1247 header_txt.append('#include <unordered_map>')
1248 header_txt.append('#include "loader_platform.h"')
1249 header_txt.append('#include "vkLayer.h"')
1250 header_txt.append('#include "threading.h"')
1251 header_txt.append('#include "layers_config.h"')
1252 header_txt.append('#include "vk_enum_validate_helper.h"')
1253 header_txt.append('#include "vk_struct_validate_helper.h"')
1254 header_txt.append('//The following is #included again to catch certain OS-specific functions being used:')
1255 header_txt.append('#include "loader_platform.h"\n')
1256 header_txt.append('#include "layers_msg.h"\n')
1257 header_txt.append('static VkLayerDispatchTable nextTable;')
1258 header_txt.append('static VkBaseLayerObject *pCurObj;')
1259 header_txt.append('static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(tabOnce);\n')
1260 header_txt.append('using namespace std;')
1261 header_txt.append('static unordered_map<int, void*> proxy_objectsInUse;\n')
1262 header_txt.append('static unordered_map<VkObject, loader_platform_thread_id> objectsInUse;\n')
1263 header_txt.append('static int threadingLockInitialized = 0;')
1264 header_txt.append('static loader_platform_thread_mutex threadingLock;')
1265 header_txt.append('static int printLockInitialized = 0;')
1266 header_txt.append('static loader_platform_thread_mutex printLock;\n')
1267 header_txt.append('')
1268 header_txt.append('static void useObject(VkObject object, const char* type)')
1269 header_txt.append('{')
1270 header_txt.append(' loader_platform_thread_id tid = loader_platform_get_thread_id();')
1271 header_txt.append(' loader_platform_thread_lock_mutex(&threadingLock);')
1272 header_txt.append(' if (objectsInUse.find(object) == objectsInUse.end()) {')
1273 header_txt.append(' objectsInUse[object] = tid;')
1274 header_txt.append(' } else {')
1275 header_txt.append(' if (objectsInUse[object] == tid) {')
1276 header_txt.append(' char str[1024];')
1277 header_txt.append(' sprintf(str, "THREADING ERROR : object of type %s is simultaneously used in thread %ld and thread %ld", type, objectsInUse[object], tid);')
1278 header_txt.append(' layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, 0, 0, THREADING_CHECKER_MULTIPLE_THREADS, "THREADING", str);')
1279 header_txt.append(' } else {')
1280 header_txt.append(' char str[1024];')
1281 header_txt.append(' sprintf(str, "THREADING ERROR : object of type %s is recursively used in thread %ld", type, tid);')
1282 header_txt.append(' layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, 0, 0, THREADING_CHECKER_SINGLE_THREAD_REUSE, "THREADING", str);')
1283 header_txt.append(' }')
1284 header_txt.append(' }')
1285 header_txt.append(' loader_platform_thread_unlock_mutex(&threadingLock);')
1286 header_txt.append('}')
1287 header_txt.append('static void finishUsingObject(VkObject object)')
1288 header_txt.append('{')
1289 header_txt.append(' // Object is no longer in use')
1290 header_txt.append(' loader_platform_thread_lock_mutex(&threadingLock);')
1291 header_txt.append(' objectsInUse.erase(object);')
1292 header_txt.append(' loader_platform_thread_unlock_mutex(&threadingLock);')
1293 header_txt.append('}')
1294 return "\n".join(header_txt)
1295
1296 def generate_intercept(self, proto, qual):
1297 if proto.name in [ 'DbgRegisterMsgCallback', 'DbgUnregisterMsgCallback' ]:
1298 # use default version
1299 return None
1300 decl = proto.c_func(prefix="vk", attr="VKAPI")
1301 ret_val = ''
1302 stmt = ''
1303 funcs = []
1304 if proto.ret != "void":
1305 ret_val = "VkResult result = "
1306 stmt = " return result;\n"
1307 if proto.name == "EnumerateLayers":
1308 funcs.append('%s%s\n'
1309 '{\n'
1310 ' char str[1024];\n'
1311 ' if (gpu != NULL) {\n'
1312 ' pCurObj = (VkBaseLayerObject *) %s;\n'
1313 ' loader_platform_thread_once(&tabOnce, init%s);\n'
1314 ' %snextTable.%s;\n'
1315 ' fflush(stdout);\n'
1316 ' %s'
1317 ' } else {\n'
1318 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
1319 ' return VK_ERROR_INVALID_POINTER;\n'
1320 ' // This layer compatible with all GPUs\n'
1321 ' *pOutLayerCount = 1;\n'
1322 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
1323 ' return VK_SUCCESS;\n'
1324 ' }\n'
1325 '}' % (qual, decl, proto.params[0].name, self.layer_name, ret_val, proto.c_call(), stmt, self.layer_name))
1326 # All functions that do a Get are thread safe
1327 elif 'Get' in proto.name:
1328 return None
1329 # All Wsi functions are thread safe
1330 elif 'WsiX11' in proto.name:
1331 return None
1332 # All functions that start with a device parameter are thread safe
1333 elif proto.params[0].ty in { "VkDevice" }:
1334 return None
1335 # Only watch core objects passed as first parameter
1336 elif proto.params[0].ty not in vulkan.core.objects:
1337 return None
1338 elif proto.params[0].ty != "VkPhysicalGpu":
1339 funcs.append('%s%s\n'
1340 '{\n'
1341 ' useObject((VkObject) %s, "%s");\n'
1342 ' %snextTable.%s;\n'
1343 ' finishUsingObject((VkObject) %s);\n'
1344 '%s'
1345 '}' % (qual, decl, proto.params[0].name, proto.params[0].ty, ret_val, proto.c_call(), proto.params[0].name, stmt))
1346 else:
1347 funcs.append('%s%s\n'
1348 '{\n'
1349 ' pCurObj = (VkBaseLayerObject *) %s;\n'
1350 ' loader_platform_thread_once(&tabOnce, init%s);\n'
1351 ' %snextTable.%s;\n'
1352 '%s'
1353 '}' % (qual, decl, proto.params[0].name, self.layer_name, ret_val, proto.c_call(), stmt))
1354 return "\n\n".join(funcs)
1355
1356 def generate_body(self):
1357 self.layer_name = "Threading"
1358 body = [self._generate_layer_initialization(True, lockname='threading'),
1359 self._generate_dispatch_entrypoints("VK_LAYER_EXPORT"),
1360 self._generate_layer_gpa_function()]
1361 return "\n\n".join(body)
1362
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001363def main():
1364 subcommands = {
1365 "layer-funcs" : LayerFuncsSubcommand,
1366 "layer-dispatch" : LayerDispatchSubcommand,
Tobin Ehlis907a0522014-11-25 16:59:27 -07001367 "Generic" : GenericLayerSubcommand,
Tobin Ehlis4a636a12015-04-09 09:19:36 -06001368 "APIDump" : APIDumpSubcommand,
Tobin Ehlis907a0522014-11-25 16:59:27 -07001369 "ObjectTracker" : ObjectTrackerSubcommand,
Mike Stroyanb326d2c2015-04-02 11:59:05 -06001370 "Threading" : ThreadingSubcommand,
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001371 }
1372
Tobin Ehlis6cd06372014-12-17 17:44:50 -07001373 if len(sys.argv) < 3 or sys.argv[1] not in subcommands or not os.path.exists(sys.argv[2]):
1374 print("Usage: %s <subcommand> <input_header> [options]" % sys.argv[0])
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001375 print
Tobin Ehlis2f3726c2015-01-15 17:51:52 -07001376 print("Available subcommands are: %s" % " ".join(subcommands))
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001377 exit(1)
1378
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001379 hfp = vk_helper.HeaderFileParser(sys.argv[2])
Tobin Ehlis6cd06372014-12-17 17:44:50 -07001380 hfp.parse()
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001381 vk_helper.enum_val_dict = hfp.get_enum_val_dict()
1382 vk_helper.enum_type_dict = hfp.get_enum_type_dict()
1383 vk_helper.struct_dict = hfp.get_struct_dict()
1384 vk_helper.typedef_fwd_dict = hfp.get_typedef_fwd_dict()
1385 vk_helper.typedef_rev_dict = hfp.get_typedef_rev_dict()
1386 vk_helper.types_dict = hfp.get_types_dict()
Tobin Ehlis6cd06372014-12-17 17:44:50 -07001387
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001388 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
1389 subcmd.run()
1390
1391if __name__ == "__main__":
1392 main()