blob: ec8a97c726c7976d0367f2d82f65ad852adae905 [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)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600126 if True in [t in vk_type for t in ["int", "FLAGS", "MASK", "xcb_window_t"]]:
127 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 Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600137 if "VK_FORMAT" == 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 Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600149 r_body.append('VK_LAYER_EXPORT VK_RESULT VKAPI vkDbgRegisterMsgCallback(VK_INSTANCE 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 Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600163 r_body.append(' VK_RESULT 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 Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600170 ur_body.append('VK_LAYER_EXPORT VK_RESULT VKAPI vkDbgUnregisterMsgCallback(VK_INSTANCE 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')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600190 ur_body.append(' g_debugAction &= ~VK_DBG_LAYER_ACTION_CALLBACK;')
Jon Ashburne4722392015-03-03 15:07:15 -0700191 ur_body.append(' }')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600192 ur_body.append(' VK_RESULT 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 Ashburn25566352015-04-02 12:06:28 -0600197 def _gen_layer_get_extension_support(self, layer="Generic"):
198 ges_body = []
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600199 ges_body.append('VK_LAYER_EXPORT VK_RESULT VKAPI vkGetExtensionSupport(VK_PHYSICAL_GPU gpu, const char* pExtName)')
Jon Ashburn25566352015-04-02 12:06:28 -0600200 ges_body.append('{')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600201 ges_body.append(' VK_RESULT result;')
202 ges_body.append(' VK_BASE_LAYER_OBJECT* gpuw = (VK_BASE_LAYER_OBJECT *) gpu;')
Jon Ashburn25566352015-04-02 12:06:28 -0600203 ges_body.append('')
204 ges_body.append(' /* This entrypoint is NOT going to init its own dispatch table since loader calls here early */')
205 ges_body.append(' if (!strncmp(pExtName, "%s", strlen("%s")))' % (layer, layer))
206 ges_body.append(' {')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600207 ges_body.append(' result = VK_SUCCESS;')
Jon Ashburn25566352015-04-02 12:06:28 -0600208 ges_body.append(' } else if (nextTable.GetExtensionSupport != NULL)')
209 ges_body.append(' {')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600210 ges_body.append(' result = nextTable.GetExtensionSupport((VK_PHYSICAL_GPU)gpuw->nextObject, pExtName);')
Jon Ashburn25566352015-04-02 12:06:28 -0600211 ges_body.append(' } else')
212 ges_body.append(' {')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600213 ges_body.append(' result = VK_ERROR_INVALID_EXTENSION;')
Jon Ashburn25566352015-04-02 12:06:28 -0600214 ges_body.append(' }')
215 ges_body.append(' return result;')
216 ges_body.append('}')
217 return "\n".join(ges_body)
218
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600219 def _generate_dispatch_entrypoints(self, qual=""):
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600220 if qual:
221 qual += " "
222
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600223 funcs = []
224 intercepted = []
225 for proto in self.protos:
226 if proto.name != "GetProcAddr" and proto.name != "InitAndEnumerateGpus":
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600227 intercept = self.generate_intercept(proto, qual)
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600228 if intercept is None:
229 # fill in default intercept for certain entrypoints
230 if 'DbgRegisterMsgCallback' == proto.name:
231 intercept = self._gen_layer_dbg_callback_register()
Jon Ashburn25566352015-04-02 12:06:28 -0600232 elif 'DbgUnregisterMsgCallback' == proto.name:
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600233 intercept = self._gen_layer_dbg_callback_unregister()
Jon Ashburn25566352015-04-02 12:06:28 -0600234 elif 'GetExtensionSupport' == proto.name:
235 funcs.append(self._gen_layer_get_extension_support(self.layer_name))
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600236 if intercept is not None:
237 funcs.append(intercept)
238 intercepted.append(proto)
239
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600240 prefix="vk"
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600241 lookups = []
242 for proto in intercepted:
243 if 'WsiX11' in proto.name:
244 lookups.append("#if defined(__linux__) || defined(XCB_NVIDIA)")
245 lookups.append("if (!strcmp(name, \"%s\"))" % proto.name)
246 lookups.append(" return (void*) %s%s;" %
247 (prefix, proto.name))
248 if 'WsiX11' in proto.name:
249 lookups.append("#endif")
250
251 # add customized layer_intercept_proc
252 body = []
253 body.append("static inline void* layer_intercept_proc(const char *name)")
254 body.append("{")
255 body.append(generate_get_proc_addr_check("name"))
256 body.append("")
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600257 body.append(" name += 2;")
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600258 body.append(" %s" % "\n ".join(lookups))
259 body.append("")
260 body.append(" return NULL;")
261 body.append("}")
262 funcs.append("\n".join(body))
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600263 return "\n\n".join(funcs)
264
265
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700266 def _generate_extensions(self):
267 exts = []
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600268 exts.append('uint64_t objTrackGetObjectCount(VK_OBJECT_TYPE type)')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700269 exts.append('{')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600270 exts.append(' return (type == VK_OBJECT_TYPE_ANY) ? numTotalObjs : numObjs[type];')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700271 exts.append('}')
272 exts.append('')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600273 exts.append('VK_RESULT objTrackGetObjects(VK_OBJECT_TYPE type, uint64_t objCount, OBJTRACK_NODE* pObjNodeArray)')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700274 exts.append('{')
275 exts.append(" // This bool flags if we're pulling all objs or just a single class of objs")
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600276 exts.append(' bool32_t bAllObjs = (type == VK_OBJECT_TYPE_ANY);')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700277 exts.append(' // Check the count first thing')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600278 exts.append(' uint64_t maxObjCount = (bAllObjs) ? numTotalObjs : numObjs[type];')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700279 exts.append(' if (objCount > maxObjCount) {')
280 exts.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600281 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));')
282 exts.append(' layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, 0, 0, OBJTRACK_OBJCOUNT_MAX_EXCEEDED, "OBJTRACK", str);')
283 exts.append(' return VK_ERROR_INVALID_VALUE;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700284 exts.append(' }')
285 exts.append(' objNode* pTrav = (bAllObjs) ? pGlobalHead : pObjectHead[type];')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600286 exts.append(' for (uint64_t i = 0; i < objCount; i++) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700287 exts.append(' if (!pTrav) {')
288 exts.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600289 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);')
290 exts.append(' layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, 0, 0, OBJTRACK_INTERNAL_ERROR, "OBJTRACK", str);')
291 exts.append(' return VK_ERROR_UNKNOWN;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700292 exts.append(' }')
293 exts.append(' memcpy(&pObjNodeArray[i], pTrav, sizeof(OBJTRACK_NODE));')
294 exts.append(' pTrav = (bAllObjs) ? pTrav->pNextGlobal : pTrav->pNextObj;')
295 exts.append(' }')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600296 exts.append(' return VK_SUCCESS;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700297 exts.append('}')
298
299 return "\n".join(exts)
300
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600301 def _generate_layer_gpa_function(self, extensions=[]):
302 func_body = []
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600303 func_body.append("VK_LAYER_EXPORT void* VKAPI vkGetProcAddr(VK_PHYSICAL_GPU gpu, const char* funcName)\n"
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600304 "{\n"
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600305 " VK_BASE_LAYER_OBJECT* gpuw = (VK_BASE_LAYER_OBJECT *) gpu;\n"
Chia-I Wu706533e2015-01-05 13:18:57 +0800306 " void* addr;\n"
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600307 " if (gpu == NULL)\n"
308 " return NULL;\n"
309 " pCurObj = gpuw;\n"
Jon Ashburn21001f62015-02-16 08:26:50 -0700310 " loader_platform_thread_once(&tabOnce, init%s);\n\n"
Chia-I Wu706533e2015-01-05 13:18:57 +0800311 " addr = layer_intercept_proc(funcName);\n"
312 " if (addr)\n"
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600313 " return addr;" % self.layer_name)
Chia-I Wu706533e2015-01-05 13:18:57 +0800314
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700315 if 0 != len(extensions):
316 for ext_name in extensions:
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800317 func_body.append(' else if (!strncmp("%s", funcName, sizeof("%s")))\n'
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700318 ' return %s;' % (ext_name, ext_name, ext_name))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600319 func_body.append(" else {\n"
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600320 " if (gpuw->pGPA == NULL)\n"
321 " return NULL;\n"
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600322 " return gpuw->pGPA((VK_PHYSICAL_GPU)gpuw->nextObject, funcName);\n"
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600323 " }\n"
324 "}\n")
325 return "\n".join(func_body)
326
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600327 def _generate_layer_initialization(self, init_opts=False, prefix='vk', lockname=None):
328 func_body = ["#include \"vk_dispatch_table_helper.h\""]
Jon Ashburn21001f62015-02-16 08:26:50 -0700329 func_body.append('static void init%s(void)\n'
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600330 '{\n' % self.layer_name)
Jon Ashburn21001f62015-02-16 08:26:50 -0700331 if init_opts:
332 func_body.append(' const char *strOpt;')
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600333 func_body.append(' // initialize %s options' % self.layer_name)
334 func_body.append(' getLayerOptionEnum("%sReportLevel", (uint32_t *) &g_reportingLevel);' % self.layer_name)
335 func_body.append(' g_actionIsDefault = getLayerOptionEnum("%sDebugAction", (uint32_t *) &g_debugAction);' % self.layer_name)
Jon Ashburn21001f62015-02-16 08:26:50 -0700336 func_body.append('')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600337 func_body.append(' if (g_debugAction & VK_DBG_LAYER_ACTION_LOG_MSG)')
Jon Ashburn21001f62015-02-16 08:26:50 -0700338 func_body.append(' {')
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600339 func_body.append(' strOpt = getLayerOption("%sLogFilename");' % self.layer_name)
Jon Ashburn21001f62015-02-16 08:26:50 -0700340 func_body.append(' if (strOpt)')
341 func_body.append(' {')
342 func_body.append(' g_logFile = fopen(strOpt, "w");')
343 func_body.append(' }')
344 func_body.append(' if (g_logFile == NULL)')
345 func_body.append(' g_logFile = stdout;')
346 func_body.append(' }')
347 func_body.append('')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600348 func_body.append(' vkGetProcAddrType fpNextGPA;\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600349 ' fpNextGPA = pCurObj->pGPA;\n'
Jon Ashburn21001f62015-02-16 08:26:50 -0700350 ' assert(fpNextGPA);\n')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600351
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600352 func_body.append(" layer_initialize_dispatch_table(&nextTable, fpNextGPA, (VK_PHYSICAL_GPU) pCurObj->nextObject);")
Tobin Ehlis84a8a9b2015-02-23 14:09:16 -0700353 if lockname is not None:
354 func_body.append(" if (!%sLockInitialized)" % lockname)
355 func_body.append(" {")
356 func_body.append(" // TODO/TBD: Need to delete this mutex sometime. How???")
357 func_body.append(" loader_platform_thread_create_mutex(&%sLock);" % lockname)
358 func_body.append(" %sLockInitialized = 1;" % lockname)
359 func_body.append(" }")
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600360 func_body.append("}\n")
361 return "\n".join(func_body)
362
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600363 def _generate_layer_initialization_with_lock(self, prefix='vk'):
364 func_body = ["#include \"vk_dispatch_table_helper.h\""]
Jon Ashburn21001f62015-02-16 08:26:50 -0700365 func_body.append('static void init%s(void)\n'
Ian Elliott81ac44c2015-01-13 17:52:38 -0700366 '{\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600367 ' vkGetProcAddrType fpNextGPA;\n'
Ian Elliott81ac44c2015-01-13 17:52:38 -0700368 ' fpNextGPA = pCurObj->pGPA;\n'
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600369 ' assert(fpNextGPA);\n' % self.layer_name);
Ian Elliott81ac44c2015-01-13 17:52:38 -0700370
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600371 func_body.append(" layer_initialize_dispatch_table(&nextTable, fpNextGPA, (VK_PHYSICAL_GPU) pCurObj->nextObject);\n")
Ian Elliott81ac44c2015-01-13 17:52:38 -0700372 func_body.append(" if (!printLockInitialized)")
373 func_body.append(" {")
374 func_body.append(" // TODO/TBD: Need to delete this mutex sometime. How???")
375 func_body.append(" loader_platform_thread_create_mutex(&printLock);")
376 func_body.append(" printLockInitialized = 1;")
377 func_body.append(" }")
378 func_body.append("}\n")
379 return "\n".join(func_body)
380
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600381class LayerFuncsSubcommand(Subcommand):
382 def generate_header(self):
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600383 return '#include <vkLayer.h>\n#include "loader.h"'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600384
385 def generate_body(self):
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600386 return self._generate_dispatch_entrypoints("static")
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600387
388class LayerDispatchSubcommand(Subcommand):
389 def generate_header(self):
390 return '#include "layer_wrappers.h"'
391
392 def generate_body(self):
Jon Ashburn21001f62015-02-16 08:26:50 -0700393 return self._generate_layer_initialization()
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600394
395class GenericLayerSubcommand(Subcommand):
396 def generate_header(self):
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600397 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 VK_LAYER_DISPATCH_TABLE nextTable;\nstatic VK_BASE_LAYER_OBJECT *pCurObj;\n\nstatic LOADER_PLATFORM_THREAD_ONCE_DECLARATION(tabOnce);'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600398
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600399 def generate_intercept(self, proto, qual):
Jon Ashburn25566352015-04-02 12:06:28 -0600400 if proto.name in [ 'DbgRegisterMsgCallback', 'DbgUnregisterMsgCallback' , 'GetExtensionSupport']:
Mike Stroyan723913e2015-04-03 14:39:16 -0600401 # use default version
402 return None
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600403 decl = proto.c_func(prefix="vk", attr="VKAPI")
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600404 param0_name = proto.params[0].name
405 ret_val = ''
406 stmt = ''
407 funcs = []
408 if proto.ret != "void":
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600409 ret_val = "VK_RESULT result = "
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600410 stmt = " return result;\n"
411 if 'WsiX11AssociateConnection' == proto.name:
412 funcs.append("#if defined(__linux__) || defined(XCB_NVIDIA)")
413 if proto.name == "EnumerateLayers":
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600414 c_call = proto.c_call().replace("(" + proto.params[0].name, "((VK_PHYSICAL_GPU)gpuw->nextObject", 1)
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600415 funcs.append('%s%s\n'
416 '{\n'
417 ' char str[1024];\n'
418 ' if (gpu != NULL) {\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600419 ' VK_BASE_LAYER_OBJECT* gpuw = (VK_BASE_LAYER_OBJECT *) %s;\n'
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600420 ' sprintf(str, "At start of layered %s\\n");\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600421 ' layerCbMsg(VK_DBG_MSG_UNKNOWN, VK_VALIDATION_LEVEL_0, gpu, 0, 0, (char *) "GENERIC", (char *) str);\n'
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600422 ' pCurObj = gpuw;\n'
423 ' loader_platform_thread_once(&tabOnce, init%s);\n'
424 ' %snextTable.%s;\n'
425 ' sprintf(str, "Completed layered %s\\n");\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600426 ' layerCbMsg(VK_DBG_MSG_UNKNOWN, VK_VALIDATION_LEVEL_0, gpu, 0, 0, (char *) "GENERIC", (char *) str);\n'
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600427 ' fflush(stdout);\n'
428 ' %s'
429 ' } else {\n'
430 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600431 ' return VK_ERROR_INVALID_POINTER;\n'
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600432 ' // This layer compatible with all GPUs\n'
433 ' *pOutLayerCount = 1;\n'
434 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600435 ' return VK_SUCCESS;\n'
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600436 ' }\n'
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600437 '}' % (qual, decl, proto.params[0].name, proto.name, self.layer_name, ret_val, c_call, proto.name, stmt, self.layer_name))
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600438 elif proto.params[0].ty != "VK_PHYSICAL_GPU":
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600439 funcs.append('%s%s\n'
440 '{\n'
441 ' %snextTable.%s;\n'
442 '%s'
443 '}' % (qual, decl, ret_val, proto.c_call(), stmt))
444 else:
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600445 c_call = proto.c_call().replace("(" + proto.params[0].name, "((VK_PHYSICAL_GPU)gpuw->nextObject", 1)
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600446 funcs.append('%s%s\n'
447 '{\n'
448 ' char str[1024];'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600449 ' VK_BASE_LAYER_OBJECT* gpuw = (VK_BASE_LAYER_OBJECT *) %s;\n'
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600450 ' sprintf(str, "At start of layered %s\\n");\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600451 ' layerCbMsg(VK_DBG_MSG_UNKNOWN, VK_VALIDATION_LEVEL_0, gpuw, 0, 0, (char *) "GENERIC", (char *) str);\n'
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600452 ' pCurObj = gpuw;\n'
453 ' loader_platform_thread_once(&tabOnce, init%s);\n'
454 ' %snextTable.%s;\n'
455 ' sprintf(str, "Completed layered %s\\n");\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600456 ' layerCbMsg(VK_DBG_MSG_UNKNOWN, VK_VALIDATION_LEVEL_0, gpuw, 0, 0, (char *) "GENERIC", (char *) str);\n'
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600457 ' fflush(stdout);\n'
458 '%s'
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600459 '}' % (qual, decl, proto.params[0].name, proto.name, self.layer_name, ret_val, c_call, proto.name, stmt))
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600460 if 'WsiX11QueuePresent' == proto.name:
461 funcs.append("#endif")
462 return "\n\n".join(funcs)
463
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600464 def generate_body(self):
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600465 self.layer_name = "Generic"
466 body = [self._generate_layer_initialization(True),
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600467 self._generate_dispatch_entrypoints("VK_LAYER_EXPORT"),
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600468 self._generate_layer_gpa_function()]
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600469
470 return "\n\n".join(body)
471
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600472class APIDumpSubcommand(Subcommand):
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600473 def generate_header(self):
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700474 header_txt = []
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600475 header_txt.append('#include <fstream>')
476 header_txt.append('#include <iostream>')
477 header_txt.append('#include <string>')
478 header_txt.append('')
479 header_txt.append('static std::ofstream fileStream;')
480 header_txt.append('static std::string fileName = "vk_apidump.txt";')
481 header_txt.append('std::ostream* outputStream = NULL;')
482 header_txt.append('void ConfigureOutputStream(bool writeToFile, bool flushAfterWrite)')
483 header_txt.append('{')
484 header_txt.append(' if(writeToFile)')
485 header_txt.append(' {')
486 header_txt.append(' fileStream.open(fileName);')
487 header_txt.append(' outputStream = &fileStream;')
488 header_txt.append(' }')
489 header_txt.append(' else')
490 header_txt.append(' {')
491 header_txt.append(' outputStream = &std::cout;')
492 header_txt.append(' }')
493 header_txt.append('')
494 header_txt.append(' if(flushAfterWrite)')
495 header_txt.append(' {')
496 header_txt.append(' outputStream->sync_with_stdio(true);')
497 header_txt.append(' }')
498 header_txt.append(' else')
499 header_txt.append(' {')
500 header_txt.append(' outputStream->sync_with_stdio(false);')
501 header_txt.append(' }')
502 header_txt.append('}')
503 header_txt.append('')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700504 header_txt.append('#include "loader_platform.h"')
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600505 header_txt.append('#include "vkLayer.h"')
506 header_txt.append('#include "vk_struct_string_helper_cpp.h"')
507 header_txt.append('')
Ian Elliott20f06872015-02-12 17:08:34 -0700508 header_txt.append('// The following is #included again to catch certain OS-specific functions being used:')
509 header_txt.append('#include "loader_platform.h"')
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600510 header_txt.append('')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600511 header_txt.append('static VK_LAYER_DISPATCH_TABLE nextTable;')
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600512 header_txt.append('static VK_BASE_LAYER_OBJECT *pCurObj;')
513 header_txt.append('')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700514 header_txt.append('static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(tabOnce);')
515 header_txt.append('static int printLockInitialized = 0;')
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600516 header_txt.append('static loader_platform_thread_mutex printLock;')
517 header_txt.append('')
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700518 header_txt.append('#define MAX_TID 513')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700519 header_txt.append('static loader_platform_thread_id tidMapping[MAX_TID] = {0};')
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700520 header_txt.append('static uint32_t maxTID = 0;')
521 header_txt.append('// Map actual TID to an index value and return that index')
522 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
523 header_txt.append('static uint32_t getTIDIndex() {')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700524 header_txt.append(' loader_platform_thread_id tid = loader_platform_get_thread_id();')
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700525 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
526 header_txt.append(' if (tid == tidMapping[i])')
527 header_txt.append(' return i;')
528 header_txt.append(' }')
529 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
Ian Elliott81ac44c2015-01-13 17:52:38 -0700530 header_txt.append(' uint32_t retVal = (uint32_t) maxTID;')
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700531 header_txt.append(' tidMapping[maxTID++] = tid;')
532 header_txt.append(' assert(maxTID < MAX_TID);')
533 header_txt.append(' return retVal;')
534 header_txt.append('}')
535 return "\n".join(header_txt)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600536
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600537 def generate_init(self):
538 func_body = []
539 func_body.append('#include "vk_dispatch_table_helper.h"')
540 func_body.append('#include "layers_config.h"')
541 func_body.append('')
542 func_body.append('static void init%s(void)' % self.layer_name)
543 func_body.append('{')
544 func_body.append(' using namespace StreamControl;')
545 func_body.append('')
546 func_body.append(' char const*const writeToFileStr = getLayerOption("APIDumpFile");')
547 func_body.append(' bool writeToFile = false;')
548 func_body.append(' if(writeToFileStr != NULL)')
549 func_body.append(' {')
550 func_body.append(' if(strcmp(writeToFileStr, "TRUE") == 0)')
551 func_body.append(' {')
552 func_body.append(' writeToFile = true;')
553 func_body.append(' }')
554 func_body.append(' else if(strcmp(writeToFileStr, "FALSE") == 0)')
555 func_body.append(' {')
556 func_body.append(' writeToFile = false;')
557 func_body.append(' }')
558 func_body.append(' }')
559 func_body.append('')
560 func_body.append(' char const*const noAddrStr = getLayerOption("APIDumpNoAddr");')
561 func_body.append(' if(noAddrStr != NULL)')
562 func_body.append(' {')
563 func_body.append(' if(strcmp(noAddrStr, "FALSE") == 0)')
564 func_body.append(' {')
565 func_body.append(' StreamControl::writeAddress = true;')
566 func_body.append(' }')
567 func_body.append(' else if(strcmp(noAddrStr, "TRUE") == 0)')
568 func_body.append(' {')
569 func_body.append(' StreamControl::writeAddress = false;')
570 func_body.append(' }')
571 func_body.append(' }')
572 func_body.append('')
573 func_body.append(' char const*const flushAfterWriteStr = getLayerOption("APIDumpFlush");')
574 func_body.append(' bool flushAfterWrite = false;')
575 func_body.append(' if(flushAfterWriteStr != NULL)')
576 func_body.append(' {')
577 func_body.append(' if(strcmp(flushAfterWriteStr, "TRUE") == 0)')
578 func_body.append(' {')
579 func_body.append(' flushAfterWrite = true;')
580 func_body.append(' }')
581 func_body.append(' else if(strcmp(flushAfterWriteStr, "FALSE") == 0)')
582 func_body.append(' {')
583 func_body.append(' flushAfterWrite = false;')
584 func_body.append(' }')
585 func_body.append(' }')
586 func_body.append('')
587 func_body.append(' ConfigureOutputStream(writeToFile, flushAfterWrite);')
588 func_body.append('')
589 func_body.append(' vkGetProcAddrType fpNextGPA;')
590 func_body.append(' fpNextGPA = pCurObj->pGPA;')
591 func_body.append(' assert(fpNextGPA);')
592 func_body.append(' layer_initialize_dispatch_table(&nextTable, fpNextGPA, (VK_PHYSICAL_GPU) pCurObj->nextObject);')
593 func_body.append('')
594 func_body.append(' if (!printLockInitialized)')
595 func_body.append(' {')
596 func_body.append(' // TODO/TBD: Need to delete this mutex sometime. How???')
597 func_body.append(' loader_platform_thread_create_mutex(&printLock);')
598 func_body.append(' printLockInitialized = 1;')
599 func_body.append(' }')
600 func_body.append('}')
601 func_body.append('')
602 return "\n".join(func_body)
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700603
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600604 def generate_intercept(self, proto, qual):
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600605 decl = proto.c_func(prefix="vk", attr="VKAPI")
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600606 param0_name = proto.params[0].name
607 ret_val = ''
608 stmt = ''
609 funcs = []
610 sp_param_dict = {} # Store 'index' for struct param to print, or an name of binding "Count" param for array to print
611 create_params = 0 # Num of params at end of function that are created and returned as output values
612 if 'WsiX11CreatePresentableImage' in proto.name or 'AllocDescriptorSets' in proto.name:
613 create_params = -2
614 elif 'Create' in proto.name or 'Alloc' in proto.name or 'MapMemory' in proto.name:
615 create_params = -1
616 if proto.ret != "void":
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600617 ret_val = "VK_RESULT result = "
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600618 stmt = " return result;\n"
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600619 f_open = 'loader_platform_thread_lock_mutex(&printLock);\n '
620 log_func = ' if (StreamControl::writeAddress == true) {'
621 log_func += '\n (*outputStream) << "t{" << getTIDIndex() << "} vk%s(' % proto.name
622 log_func_no_addr = '\n (*outputStream) << "t{" << getTIDIndex() << "} vk%s(' % proto.name
623 f_close = '\n loader_platform_thread_unlock_mutex(&printLock);'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600624 pindex = 0
625 prev_count_name = ''
626 for p in proto.params:
627 cp = False
628 if 0 != create_params:
629 # If this is any of the N last params of the func, treat as output
630 for y in range(-1, create_params-1, -1):
631 if p.name == proto.params[y].name:
632 cp = True
633 (pft, pfi) = self._get_printf_params(p.ty, p.name, cp, cpp=True)
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600634 log_func += '%s = " << %s << ", ' % (p.name, pfi)
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600635 if "%p" == pft:
636 log_func_no_addr += '%s = address, ' % (p.name)
637 else:
638 log_func_no_addr += '%s = " << %s << ", ' % (p.name, pfi)
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600639 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 -0600640 sp_param_dict[pindex] = prev_count_name
641 elif 'pDescriptorSets' == p.name and proto.params[-1].name == 'pCount':
642 sp_param_dict[pindex] = '*pCount'
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600643 elif 'Wsi' not in proto.name and vk_helper.is_type(p.ty.strip('*').replace('const ', ''), 'struct'):
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600644 sp_param_dict[pindex] = 'index'
645 pindex += 1
646 if p.name.endswith('Count'):
647 if '*' in p.ty:
648 prev_count_name = "*%s" % p.name
649 else:
650 prev_count_name = p.name
651 else:
652 prev_count_name = ''
653 log_func = log_func.strip(', ')
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600654 log_func_no_addr = log_func_no_addr.strip(', ')
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600655 if proto.ret != "void":
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600656 log_func += ') = " << string_VK_RESULT((VK_RESULT)result) << endl'
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600657 log_func_no_addr += ') = " << string_VK_RESULT((VK_RESULT)result) << endl'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600658 else:
659 log_func += ')\\n"'
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600660 log_func_no_addr += ')\\n"'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600661 log_func += ';'
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600662 log_func_no_addr += ';'
663 log_func += '\n }\n else {%s;\n }' % log_func_no_addr;
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600664 if len(sp_param_dict) > 0:
665 i_decl = False
666 log_func += '\n string tmp_str;'
667 for sp_index in sp_param_dict:
668 if 'index' == sp_param_dict[sp_index]:
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600669 cis_print_func = 'vk_print_%s' % (proto.params[sp_index].ty.replace('const ', '').strip('*').lower())
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600670 local_name = proto.params[sp_index].name
671 if '*' not in proto.params[sp_index].ty:
672 local_name = '&%s' % proto.params[sp_index].name
673 log_func += '\n if (%s) {' % (local_name)
674 log_func += '\n tmp_str = %s(%s, " ");' % (cis_print_func, local_name)
675 log_func += '\n (*outputStream) << " %s (" << %s << ")" << endl << tmp_str << endl;' % (local_name, local_name)
676 log_func += '\n }'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600677 else: # We have a count value stored to iterate over an array
678 print_cast = ''
679 print_func = ''
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600680 if vk_helper.is_type(proto.params[sp_index].ty.strip('*').replace('const ', ''), 'struct'):
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600681 print_cast = '&'
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600682 print_func = 'vk_print_%s' % proto.params[sp_index].ty.replace('const ', '').strip('*').lower()
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600683 else:
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600684 print_cast = ''
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600685 print_func = 'string_convert_helper'
686 #cis_print_func = 'tmp_str = string_convert_helper((void*)%s[i], " ");' % proto.params[sp_index].name
687 cis_print_func = 'tmp_str = %s(%s%s[i], " ");' % (print_func, print_cast, proto.params[sp_index].name)
688# else:
689# cis_print_func = ''
690 if not i_decl:
691 log_func += '\n uint32_t i;'
692 i_decl = True
693 log_func += '\n for (i = 0; i < %s; i++) {' % (sp_param_dict[sp_index])
694 log_func += '\n %s' % (cis_print_func)
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600695 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 -0600696 log_func += '\n }'
697 if 'WsiX11AssociateConnection' == proto.name:
698 funcs.append("#if defined(__linux__) || defined(XCB_NVIDIA)")
699 if proto.name == "EnumerateLayers":
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600700 c_call = proto.c_call().replace("(" + proto.params[0].name, "((VK_PHYSICAL_GPU)gpuw->nextObject", 1)
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600701 funcs.append('%s%s\n'
702 '{\n'
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600703 ' using namespace StreamControl;\n'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600704 ' if (gpu != NULL) {\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600705 ' VK_BASE_LAYER_OBJECT* gpuw = (VK_BASE_LAYER_OBJECT *) %s;\n'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600706 ' pCurObj = gpuw;\n'
707 ' loader_platform_thread_once(&tabOnce, init%s);\n'
708 ' %snextTable.%s;\n'
709 ' %s %s %s\n'
710 ' %s'
711 ' } else {\n'
712 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600713 ' return VK_ERROR_INVALID_POINTER;\n'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600714 ' // This layer compatible with all GPUs\n'
715 ' *pOutLayerCount = 1;\n'
716 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600717 ' return VK_SUCCESS;\n'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600718 ' }\n'
719 '}' % (qual, decl, proto.params[0].name, self.layer_name, ret_val, c_call,f_open, log_func, f_close, stmt, self.layer_name))
Jon Ashburn25566352015-04-02 12:06:28 -0600720 elif 'GetExtensionSupport' == proto.name:
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600721 c_call = proto.c_call().replace("(" + proto.params[0].name, "((VK_PHYSICAL_GPU)gpuw->nextObject", 1)
Jon Ashburn25566352015-04-02 12:06:28 -0600722 funcs.append('%s%s\n'
723 '{\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600724 ' VK_BASE_LAYER_OBJECT* gpuw = (VK_BASE_LAYER_OBJECT *) %s;\n'
725 ' VK_RESULT result;\n'
Jon Ashburn25566352015-04-02 12:06:28 -0600726 ' /* This entrypoint is NOT going to init its own dispatch table since loader calls here early */\n'
727 ' if (!strncmp(pExtName, "%s", strlen("%s")))\n'
728 ' {\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600729 ' result = VK_SUCCESS;\n'
Jon Ashburn25566352015-04-02 12:06:28 -0600730 ' } else if (nextTable.GetExtensionSupport != NULL)\n'
731 ' {\n'
732 ' result = nextTable.%s;\n'
733 ' %s %s %s\n'
734 ' } else\n'
735 ' {\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600736 ' result = VK_ERROR_INVALID_EXTENSION;\n'
Jon Ashburn25566352015-04-02 12:06:28 -0600737 ' }\n'
738 '%s'
739 '}' % (qual, decl, proto.params[0].name, self.layer_name, self.layer_name, c_call, f_open, log_func, f_close, stmt))
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600740 elif proto.params[0].ty != "VK_PHYSICAL_GPU":
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600741 funcs.append('%s%s\n'
742 '{\n'
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600743 ' using namespace StreamControl;\n'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600744 ' %snextTable.%s;\n'
745 ' %s%s%s\n'
746 '%s'
747 '}' % (qual, decl, ret_val, proto.c_call(), f_open, log_func, f_close, stmt))
748 else:
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600749 c_call = proto.c_call().replace("(" + proto.params[0].name, "((VK_PHYSICAL_GPU)gpuw->nextObject", 1)
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600750 funcs.append('%s%s\n'
751 '{\n'
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600752 ' using namespace StreamControl;\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600753 ' VK_BASE_LAYER_OBJECT* gpuw = (VK_BASE_LAYER_OBJECT *) %s;\n'
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600754 ' pCurObj = gpuw;\n'
755 ' loader_platform_thread_once(&tabOnce, init%s);\n'
756 ' %snextTable.%s;\n'
757 ' %s%s%s\n'
758 '%s'
759 '}' % (qual, decl, proto.params[0].name, self.layer_name, ret_val, c_call, f_open, log_func, f_close, stmt))
760 if 'WsiX11QueuePresent' == proto.name:
761 funcs.append("#endif")
762 return "\n\n".join(funcs)
763
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700764 def generate_body(self):
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600765 self.layer_name = "APIDump"
766 body = [self.generate_init(),
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600767 self._generate_dispatch_entrypoints("VK_LAYER_EXPORT"),
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600768 self._generate_layer_gpa_function()]
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700769 return "\n\n".join(body)
770
Tobin Ehlis4a636a12015-04-09 09:19:36 -0600771## subclass from APIDumpCppSubcommand instead of Subcommand
772#class APIDumpNoAddrCppSubcommand(APIDumpCppSubcommand):
773# def generate_header(self):
774# header_txt = []
775# header_txt.append('#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>')
776# header_txt.append('#include "loader_platform.h"')
777# header_txt.append('#include "vkLayer.h"\n#include "vk_struct_string_helper_no_addr_cpp.h"\n')
778# header_txt.append('// The following is #included again to catch certain OS-specific functions being used:')
779# header_txt.append('#include "loader_platform.h"')
780# header_txt.append('static VK_LAYER_DISPATCH_TABLE nextTable;')
781# header_txt.append('static VK_BASE_LAYER_OBJECT *pCurObj;\n')
782# header_txt.append('static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(tabOnce);')
783# header_txt.append('static int printLockInitialized = 0;')
784# header_txt.append('static loader_platform_thread_mutex printLock;\n')
785# header_txt.append('#define MAX_TID 513')
786# header_txt.append('static loader_platform_thread_id tidMapping[MAX_TID] = {0};')
787# header_txt.append('static uint32_t maxTID = 0;')
788# header_txt.append('// Map actual TID to an index value and return that index')
789# header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
790# header_txt.append('static uint32_t getTIDIndex() {')
791# header_txt.append(' loader_platform_thread_id tid = loader_platform_get_thread_id();')
792# header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
793# header_txt.append(' if (tid == tidMapping[i])')
794# header_txt.append(' return i;')
795# header_txt.append(' }')
796# header_txt.append(" // Don't yet have mapping, set it and return newly set index")
797# header_txt.append(' uint32_t retVal = (uint32_t) maxTID;')
798# header_txt.append(' tidMapping[maxTID++] = tid;')
799# header_txt.append(' assert(maxTID < MAX_TID);')
800# header_txt.append(' return retVal;')
801# header_txt.append('}')
802# return "\n".join(header_txt)
803#
804# def generate_body(self):
805# self.layer_name = "APIDumpNoAddrCpp"
806# self.no_addr = True
807# body = [self._generate_layer_initialization_with_lock(),
808# self._generate_dispatch_entrypoints("VK_LAYER_EXPORT"),
809# self._generate_layer_gpa_function()]
810#
811# return "\n\n".join(body)
812#
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600813class ObjectTrackerSubcommand(Subcommand):
814 def generate_header(self):
815 header_txt = []
Ian Elliott81ac44c2015-01-13 17:52:38 -0700816 header_txt.append('#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include "loader_platform.h"')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600817 header_txt.append('#include "object_track.h"\n\nstatic VK_LAYER_DISPATCH_TABLE nextTable;\nstatic VK_BASE_LAYER_OBJECT *pCurObj;')
Ian Elliott20f06872015-02-12 17:08:34 -0700818 header_txt.append('// The following is #included again to catch certain OS-specific functions being used:')
819 header_txt.append('#include "loader_platform.h"')
Jon Ashburn7a2da4f2015-02-17 11:03:12 -0700820 header_txt.append('#include "layers_config.h"')
Jon Ashburn21001f62015-02-16 08:26:50 -0700821 header_txt.append('#include "layers_msg.h"')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700822 header_txt.append('static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(tabOnce);')
823 header_txt.append('static long long unsigned int object_track_index = 0;')
Tobin Ehlis84a8a9b2015-02-23 14:09:16 -0700824 header_txt.append('static int objLockInitialized = 0;')
825 header_txt.append('static loader_platform_thread_mutex objLock;')
Jon Ashburn21001f62015-02-16 08:26:50 -0700826 header_txt.append('')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700827 header_txt.append('// We maintain a "Global" list which links every object and a')
828 header_txt.append('// per-Object list which just links objects of a given type')
829 header_txt.append('// The object node has both pointers so the actual nodes are shared between the two lists')
830 header_txt.append('typedef struct _objNode {')
831 header_txt.append(' OBJTRACK_NODE obj;')
832 header_txt.append(' struct _objNode *pNextObj;')
833 header_txt.append(' struct _objNode *pNextGlobal;')
834 header_txt.append('} objNode;')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600835 header_txt.append('static objNode *pObjectHead[VK_NUM_OBJECT_TYPE] = {0};')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700836 header_txt.append('static objNode *pGlobalHead = NULL;')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600837 header_txt.append('static uint64_t numObjs[VK_NUM_OBJECT_TYPE] = {0};')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700838 header_txt.append('static uint64_t numTotalObjs = 0;')
Courtney Goeltzenleuchter304a1f82015-04-07 16:23:00 -0600839 header_txt.append('static uint32_t maxMemReferences = 0;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700840 header_txt.append('// Debug function to print global list and each individual object list')
841 header_txt.append('static void ll_print_lists()')
842 header_txt.append('{')
843 header_txt.append(' objNode* pTrav = pGlobalHead;')
844 header_txt.append(' printf("=====GLOBAL OBJECT LIST (%lu total objs):\\n", numTotalObjs);')
845 header_txt.append(' while (pTrav) {')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600846 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 -0700847 header_txt.append(' pTrav = pTrav->pNextGlobal;')
848 header_txt.append(' }')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600849 header_txt.append(' for (uint32_t i = 0; i < VK_NUM_OBJECT_TYPE; i++) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700850 header_txt.append(' pTrav = pObjectHead[i];')
851 header_txt.append(' if (pTrav) {')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600852 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 -0700853 header_txt.append(' while (pTrav) {')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600854 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 -0700855 header_txt.append(' pTrav = pTrav->pNextObj;')
856 header_txt.append(' }')
857 header_txt.append(' }')
858 header_txt.append(' }')
859 header_txt.append('}')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600860 header_txt.append('static void ll_insert_obj(void* pObj, VK_OBJECT_TYPE objType) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700861 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600862 header_txt.append(' sprintf(str, "OBJ[%llu] : CREATE %s object %p", object_track_index++, string_VK_OBJECT_TYPE(objType), (void*)pObj);')
863 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 -0700864 header_txt.append(' objNode* pNewObjNode = (objNode*)malloc(sizeof(objNode));')
865 header_txt.append(' pNewObjNode->obj.pObj = pObj;')
866 header_txt.append(' pNewObjNode->obj.objType = objType;')
Mark Lobodzinski01552702015-02-03 10:06:31 -0600867 header_txt.append(' pNewObjNode->obj.status = OBJSTATUS_NONE;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700868 header_txt.append(' pNewObjNode->obj.numUses = 0;')
869 header_txt.append(' // insert at front of global list')
870 header_txt.append(' pNewObjNode->pNextGlobal = pGlobalHead;')
871 header_txt.append(' pGlobalHead = pNewObjNode;')
872 header_txt.append(' // insert at front of object list')
873 header_txt.append(' pNewObjNode->pNextObj = pObjectHead[objType];')
874 header_txt.append(' pObjectHead[objType] = pNewObjNode;')
875 header_txt.append(' // increment obj counts')
876 header_txt.append(' numObjs[objType]++;')
877 header_txt.append(' numTotalObjs++;')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600878 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 +0800879 header_txt.append(' if (0) ll_print_lists();')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700880 header_txt.append('}')
881 header_txt.append('// Traverse global list and return type for given object')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600882 header_txt.append('static VK_OBJECT_TYPE ll_get_obj_type(VK_OBJECT object) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700883 header_txt.append(' objNode *pTrav = pGlobalHead;')
884 header_txt.append(' while (pTrav) {')
885 header_txt.append(' if (pTrav->obj.pObj == object)')
886 header_txt.append(' return pTrav->obj.objType;')
887 header_txt.append(' pTrav = pTrav->pNextGlobal;')
888 header_txt.append(' }')
889 header_txt.append(' char str[1024];')
890 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 -0600891 header_txt.append(' layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, object, 0, OBJTRACK_MISSING_OBJECT, "OBJTRACK", str);')
892 header_txt.append(' return VK_OBJECT_TYPE_UNKNOWN;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700893 header_txt.append('}')
Chia-I Wudf142a32014-12-16 11:02:06 +0800894 header_txt.append('#if 0')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600895 header_txt.append('static uint64_t ll_get_obj_uses(void* pObj, VK_OBJECT_TYPE objType) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700896 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
897 header_txt.append(' while (pTrav) {')
898 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
899 header_txt.append(' return pTrav->obj.numUses;')
900 header_txt.append(' }')
901 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600902 header_txt.append(' }')
903 header_txt.append(' return 0;')
904 header_txt.append('}')
Chia-I Wudf142a32014-12-16 11:02:06 +0800905 header_txt.append('#endif')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600906 header_txt.append('static void ll_increment_use_count(void* pObj, VK_OBJECT_TYPE objType) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700907 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600908 header_txt.append(' while (pTrav) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700909 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
910 header_txt.append(' pTrav->obj.numUses++;')
911 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600912 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);')
913 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 -0700914 header_txt.append(' return;')
915 header_txt.append(' }')
916 header_txt.append(' pTrav = pTrav->pNextObj;')
917 header_txt.append(' }')
918 header_txt.append(' // If we do not find obj, insert it and then increment count')
919 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600920 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));')
921 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 -0700922 header_txt.append('')
923 header_txt.append(' ll_insert_obj(pObj, objType);')
924 header_txt.append(' ll_increment_use_count(pObj, objType);')
925 header_txt.append('}')
926 header_txt.append('// We usually do not know Obj type when we destroy it so have to fetch')
927 header_txt.append('// Type from global list w/ ll_destroy_obj()')
928 header_txt.append('// and then do the full removal from both lists w/ ll_remove_obj_type()')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600929 header_txt.append('static void ll_remove_obj_type(void* pObj, VK_OBJECT_TYPE objType) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700930 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
931 header_txt.append(' objNode *pPrev = pObjectHead[objType];')
932 header_txt.append(' while (pTrav) {')
933 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
934 header_txt.append(' pPrev->pNextObj = pTrav->pNextObj;')
935 header_txt.append(' // update HEAD of Obj list as needed')
936 header_txt.append(' if (pObjectHead[objType] == pTrav)')
937 header_txt.append(' pObjectHead[objType] = pTrav->pNextObj;')
938 header_txt.append(' assert(numObjs[objType] > 0);')
939 header_txt.append(' numObjs[objType]--;')
940 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600941 header_txt.append(' sprintf(str, "OBJ[%llu] : DESTROY %s object %p", object_track_index++, string_VK_OBJECT_TYPE(objType), (void*)pObj);')
942 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 -0600943 header_txt.append(' return;')
944 header_txt.append(' }')
945 header_txt.append(' pPrev = pTrav;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700946 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600947 header_txt.append(' }')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700948 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600949 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));')
950 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 -0700951 header_txt.append('}')
952 header_txt.append('// Parse global list to find obj type, then remove obj from obj type list, finally')
953 header_txt.append('// remove obj from global list')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600954 header_txt.append('static void ll_destroy_obj(void* pObj) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700955 header_txt.append(' objNode *pTrav = pGlobalHead;')
956 header_txt.append(' objNode *pPrev = pGlobalHead;')
957 header_txt.append(' while (pTrav) {')
958 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
959 header_txt.append(' ll_remove_obj_type(pObj, pTrav->obj.objType);')
960 header_txt.append(' pPrev->pNextGlobal = pTrav->pNextGlobal;')
961 header_txt.append(' // update HEAD of global list if needed')
962 header_txt.append(' if (pGlobalHead == pTrav)')
963 header_txt.append(' pGlobalHead = pTrav->pNextGlobal;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700964 header_txt.append(' assert(numTotalObjs > 0);')
965 header_txt.append(' numTotalObjs--;')
966 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600967 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));')
968 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 -0700969 header_txt.append(' free(pTrav);')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700970 header_txt.append(' return;')
971 header_txt.append(' }')
972 header_txt.append(' pPrev = pTrav;')
973 header_txt.append(' pTrav = pTrav->pNextGlobal;')
974 header_txt.append(' }')
975 header_txt.append(' char str[1024];')
976 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 -0600977 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 -0600978 header_txt.append('}')
Tobin Ehlis235c20e2015-01-16 08:56:30 -0700979 header_txt.append('// Set selected flag state for an object node')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600980 header_txt.append('static void set_status(void* pObj, VK_OBJECT_TYPE objType, OBJECT_STATUS status_flag) {')
Mark Lobodzinskid11fcca2015-02-09 10:16:20 -0600981 header_txt.append(' if (pObj != NULL) {')
982 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
983 header_txt.append(' while (pTrav) {')
984 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
985 header_txt.append(' pTrav->obj.status |= status_flag;')
986 header_txt.append(' return;')
987 header_txt.append(' }')
988 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis235c20e2015-01-16 08:56:30 -0700989 header_txt.append(' }')
Mark Lobodzinskid11fcca2015-02-09 10:16:20 -0600990 header_txt.append(' // If we do not find it print an error')
991 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600992 header_txt.append(' sprintf(str, "Unable to set status for non-existent object %p of %s type", pObj, string_VK_OBJECT_TYPE(objType));')
993 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 -0600994 header_txt.append(' }');
Tobin Ehlis235c20e2015-01-16 08:56:30 -0700995 header_txt.append('}')
996 header_txt.append('')
Mark Lobodzinski01552702015-02-03 10:06:31 -0600997 header_txt.append('// Track selected state for an object node')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600998 header_txt.append('static void track_object_status(void* pObj, VK_STATE_BIND_POINT stateBindPoint) {')
999 header_txt.append(' objNode *pTrav = pObjectHead[VK_OBJECT_TYPE_CMD_BUFFER];')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001000 header_txt.append('')
1001 header_txt.append(' while (pTrav) {')
1002 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001003 header_txt.append(' if (stateBindPoint == VK_STATE_BIND_VIEWPORT) {')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001004 header_txt.append(' pTrav->obj.status |= OBJSTATUS_VIEWPORT_BOUND;')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001005 header_txt.append(' } else if (stateBindPoint == VK_STATE_BIND_RASTER) {')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001006 header_txt.append(' pTrav->obj.status |= OBJSTATUS_RASTER_BOUND;')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001007 header_txt.append(' } else if (stateBindPoint == VK_STATE_BIND_COLOR_BLEND) {')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001008 header_txt.append(' pTrav->obj.status |= OBJSTATUS_COLOR_BLEND_BOUND;')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001009 header_txt.append(' } else if (stateBindPoint == VK_STATE_BIND_DEPTH_STENCIL) {')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001010 header_txt.append(' pTrav->obj.status |= OBJSTATUS_DEPTH_STENCIL_BOUND;')
1011 header_txt.append(' }')
1012 header_txt.append(' return;')
1013 header_txt.append(' }')
1014 header_txt.append(' pTrav = pTrav->pNextObj;')
1015 header_txt.append(' }')
1016 header_txt.append(' // If we do not find it print an error')
1017 header_txt.append(' char str[1024];')
1018 header_txt.append(' sprintf(str, "Unable to track status for non-existent Command Buffer object %p", pObj);')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001019 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 -06001020 header_txt.append('}')
1021 header_txt.append('')
1022 header_txt.append('// Reset selected flag state for an object node')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001023 header_txt.append('static void reset_status(void* pObj, VK_OBJECT_TYPE objType, OBJECT_STATUS status_flag) {')
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001024 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
1025 header_txt.append(' while (pTrav) {')
1026 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001027 header_txt.append(' pTrav->obj.status &= ~status_flag;')
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];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001034 header_txt.append(' sprintf(str, "Unable to reset status for non-existent object %p of %s type", pObj, string_VK_OBJECT_TYPE(objType));')
1035 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('// Check object status for selected flag state')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001039 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 -06001040 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
1041 header_txt.append(' while (pTrav) {')
1042 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
Mark Lobodzinski4186e712015-02-03 11:52:26 -06001043 header_txt.append(' if ((pTrav->obj.status & status_mask) != status_flag) {')
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001044 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001045 header_txt.append(' sprintf(str, "OBJECT VALIDATION WARNING: %s object %p: %s", string_VK_OBJECT_TYPE(objType), (void*)pObj, fail_msg);')
1046 header_txt.append(' layerCbMsg(error_level, VK_VALIDATION_LEVEL_0, pObj, 0, error_code, "OBJTRACK", str);')
1047 header_txt.append(' return VK_FALSE;')
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001048 header_txt.append(' }')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001049 header_txt.append(' return VK_TRUE;')
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001050 header_txt.append(' }')
1051 header_txt.append(' pTrav = pTrav->pNextObj;')
1052 header_txt.append(' }')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001053 header_txt.append(' if (objType != VK_OBJECT_TYPE_PRESENTABLE_IMAGE_MEMORY) {')
Mark Lobodzinskiacb93682015-03-05 12:39:33 -06001054 header_txt.append(' // If we do not find it print an error')
1055 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001056 header_txt.append(' sprintf(str, "Unable to obtain status for non-existent object %p of %s type", pObj, string_VK_OBJECT_TYPE(objType));')
1057 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 -06001058 header_txt.append(' }')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001059 header_txt.append(' return VK_FALSE;')
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001060 header_txt.append('}')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001061 header_txt.append('')
1062 header_txt.append('static void validate_draw_state_flags(void* pObj) {')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001063 header_txt.append(' validate_status((void*)pObj, VK_OBJECT_TYPE_CMD_BUFFER, OBJSTATUS_VIEWPORT_BOUND, OBJSTATUS_VIEWPORT_BOUND, VK_DBG_MSG_ERROR, OBJTRACK_VIEWPORT_NOT_BOUND, "Viewport object not bound to this command buffer");')
1064 header_txt.append(' validate_status((void*)pObj, VK_OBJECT_TYPE_CMD_BUFFER, OBJSTATUS_RASTER_BOUND, OBJSTATUS_RASTER_BOUND, VK_DBG_MSG_ERROR, OBJTRACK_RASTER_NOT_BOUND, "Raster object not bound to this command buffer");')
1065 header_txt.append(' validate_status((void*)pObj, VK_OBJECT_TYPE_CMD_BUFFER, 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");')
1066 header_txt.append(' validate_status((void*)pObj, VK_OBJECT_TYPE_CMD_BUFFER, 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");')
Mark Lobodzinski4186e712015-02-03 11:52:26 -06001067 header_txt.append('}')
1068 header_txt.append('')
Courtney Goeltzenleuchter304a1f82015-04-07 16:23:00 -06001069 header_txt.append('static void setGpuQueueInfoState(void *pData) {')
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001070 header_txt.append(' maxMemReferences = ((VK_PHYSICAL_GPU_QUEUE_PROPERTIES *)pData)->maxMemReferences;')
Mark Lobodzinskie1d3f0c2015-02-09 10:20:53 -06001071 header_txt.append('}')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001072 return "\n".join(header_txt)
1073
Mike Stroyan3aecdb42015-04-03 17:13:23 -06001074 def generate_intercept(self, proto, qual):
Mike Stroyan723913e2015-04-03 14:39:16 -06001075 if proto.name in [ 'DbgRegisterMsgCallback', 'DbgUnregisterMsgCallback' ]:
1076 # use default version
1077 return None
Courtney Goeltzenleuchtera8c06282015-04-14 14:55:44 -06001078 obj_type_mapping = {base_t : base_t.replace("VK_", "VK_OBJECT_TYPE_") for base_t in vulkan.object_type_list}
Mike Stroyan723913e2015-04-03 14:39:16 -06001079 # For the various "super-types" we have to use function to distinguish sub type
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001080 for obj_type in ["VK_BASE_OBJECT", "VK_OBJECT", "VK_DYNAMIC_STATE_OBJECT"]:
Mike Stroyan723913e2015-04-03 14:39:16 -06001081 obj_type_mapping[obj_type] = "ll_get_obj_type(object)"
1082
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001083 decl = proto.c_func(prefix="vk", attr="VKAPI")
Mike Stroyan723913e2015-04-03 14:39:16 -06001084 param0_name = proto.params[0].name
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001085 p0_type = proto.params[0].ty.strip('*').replace('const ', '')
Mike Stroyan723913e2015-04-03 14:39:16 -06001086 create_line = ''
1087 destroy_line = ''
1088 funcs = []
1089 # Special cases for API funcs that don't use an object as first arg
Courtney Goeltzenleuchter4f6fa362015-04-07 16:40:50 -06001090 if True in [no_use_proto in proto.name for no_use_proto in ['GlobalOption', 'CreateInstance', 'QueueSubmit', 'QueueAddMemReference', 'QueueRemoveMemReference', 'QueueWaitIdle', 'CreateDevice', 'GetGpuInfo', 'QueueSignalSemaphore', 'QueueWaitSemaphore', 'WsiX11QueuePresent']]:
Mike Stroyan723913e2015-04-03 14:39:16 -06001091 using_line = ''
1092 else:
1093 using_line = ' loader_platform_thread_lock_mutex(&objLock);\n'
1094 using_line += ' ll_increment_use_count((void*)%s, %s);\n' % (param0_name, obj_type_mapping[p0_type])
1095 using_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
1096 if 'QueueSubmit' in proto.name:
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001097 using_line += ' set_status((void*)fence, VK_OBJECT_TYPE_FENCE, OBJSTATUS_FENCE_IS_SUBMITTED);\n'
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001098 using_line += ' // TODO: Fix for updated memory reference mechanism\n'
1099 using_line += ' // validate_memory_mapping_status(pMemRefs, memRefCount);\n'
1100 using_line += ' // validate_mem_ref_count(memRefCount);\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001101 elif 'GetFenceStatus' in proto.name:
1102 using_line += ' // Warn if submitted_flag is not set\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001103 using_line += ' validate_status((void*)fence, VK_OBJECT_TYPE_FENCE, 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 -06001104 elif 'EndCommandBuffer' in proto.name:
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001105 using_line += ' reset_status((void*)cmdBuffer, VK_OBJECT_TYPE_CMD_BUFFER, (OBJSTATUS_VIEWPORT_BOUND |\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001106 using_line += ' OBJSTATUS_RASTER_BOUND |\n'
1107 using_line += ' OBJSTATUS_COLOR_BLEND_BOUND |\n'
1108 using_line += ' OBJSTATUS_DEPTH_STENCIL_BOUND));\n'
1109 elif 'CmdBindDynamicStateObject' in proto.name:
1110 using_line += ' track_object_status((void*)cmdBuffer, stateBindPoint);\n'
1111 elif 'CmdDraw' in proto.name:
1112 using_line += ' validate_draw_state_flags((void *)cmdBuffer);\n'
1113 elif 'MapMemory' in proto.name:
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001114 using_line += ' set_status((void*)mem, VK_OBJECT_TYPE_GPU_MEMORY, OBJSTATUS_GPU_MEM_MAPPED);\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001115 elif 'UnmapMemory' in proto.name:
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001116 using_line += ' reset_status((void*)mem, VK_OBJECT_TYPE_GPU_MEMORY, OBJSTATUS_GPU_MEM_MAPPED);\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001117 if 'AllocDescriptor' in proto.name: # Allocates array of DSs
1118 create_line = ' for (uint32_t i = 0; i < *pCount; i++) {\n'
1119 create_line += ' loader_platform_thread_lock_mutex(&objLock);\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001120 create_line += ' ll_insert_obj((void*)pDescriptorSets[i], VK_OBJECT_TYPE_DESCRIPTOR_SET);\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001121 create_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
1122 create_line += ' }\n'
1123 elif 'CreatePresentableImage' in proto.name:
1124 create_line = ' loader_platform_thread_lock_mutex(&objLock);\n'
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001125 create_line += ' ll_insert_obj((void*)*%s, %s);\n' % (proto.params[-2].name, obj_type_mapping[proto.params[-2].ty.strip('*').replace('const ', '')])
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001126 create_line += ' ll_insert_obj((void*)*pMem, VK_OBJECT_TYPE_PRESENTABLE_IMAGE_MEMORY);\n'
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001127 # create_line += ' ll_insert_obj((void*)*%s, VK_OBJECT_TYPE_PRESENTABLE_IMAGE_MEMORY);\n' % (obj_type_mapping[proto.params[-1].ty.strip('*').replace('const ', '')])
Mike Stroyan723913e2015-04-03 14:39:16 -06001128 create_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
1129 elif 'Create' in proto.name or 'Alloc' in proto.name:
1130 create_line = ' loader_platform_thread_lock_mutex(&objLock);\n'
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001131 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 -06001132 create_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
1133 if 'DestroyObject' in proto.name:
1134 destroy_line = ' loader_platform_thread_lock_mutex(&objLock);\n'
1135 destroy_line += ' ll_destroy_obj((void*)%s);\n' % (param0_name)
1136 destroy_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
1137 using_line = ''
1138 else:
1139 if 'Destroy' in proto.name or 'Free' in proto.name:
1140 destroy_line = ' loader_platform_thread_lock_mutex(&objLock);\n'
1141 destroy_line += ' ll_destroy_obj((void*)%s);\n' % (param0_name)
1142 destroy_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
1143 using_line = ''
1144 if 'DestroyDevice' in proto.name:
1145 destroy_line += ' // Report any remaining objects in LL\n objNode *pTrav = pGlobalHead;\n while (pTrav) {\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001146 destroy_line += ' if (pTrav->obj.objType == VK_OBJECT_TYPE_PRESENTABLE_IMAGE_MEMORY) {\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001147 destroy_line += ' objNode *pDel = pTrav;\n'
1148 destroy_line += ' pTrav = pTrav->pNextGlobal;\n'
1149 destroy_line += ' ll_destroy_obj((void*)(pDel->obj.pObj));\n'
1150 destroy_line += ' } else {\n'
1151 destroy_line += ' char str[1024];\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001152 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'
1153 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 -06001154 destroy_line += ' pTrav = pTrav->pNextGlobal;\n'
1155 destroy_line += ' }\n'
1156 destroy_line += ' }\n'
1157 ret_val = ''
1158 stmt = ''
1159 if proto.ret != "void":
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001160 ret_val = "VK_RESULT result = "
Mike Stroyan723913e2015-04-03 14:39:16 -06001161 stmt = " return result;\n"
1162 if 'WsiX11AssociateConnection' == proto.name:
1163 funcs.append("#if defined(__linux__) || defined(XCB_NVIDIA)")
1164 if proto.name == "EnumerateLayers":
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001165 c_call = proto.c_call().replace("(" + proto.params[0].name, "((VK_PHYSICAL_GPU)gpuw->nextObject", 1)
Mike Stroyan723913e2015-04-03 14:39:16 -06001166 funcs.append('%s%s\n'
1167 '{\n'
1168 ' if (gpu != NULL) {\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001169 ' VK_BASE_LAYER_OBJECT* gpuw = (VK_BASE_LAYER_OBJECT *) %s;\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001170 ' %s'
1171 ' pCurObj = gpuw;\n'
1172 ' loader_platform_thread_once(&tabOnce, init%s);\n'
1173 ' %snextTable.%s;\n'
1174 ' %s%s'
1175 ' %s'
1176 ' } else {\n'
1177 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001178 ' return VK_ERROR_INVALID_POINTER;\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001179 ' // This layer compatible with all GPUs\n'
1180 ' *pOutLayerCount = 1;\n'
1181 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001182 ' return VK_SUCCESS;\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001183 ' }\n'
Mike Stroyan3aecdb42015-04-03 17:13:23 -06001184 '}' % (qual, decl, proto.params[0].name, using_line, self.layer_name, ret_val, c_call, create_line, destroy_line, stmt, self.layer_name))
Jon Ashburn25566352015-04-02 12:06:28 -06001185 elif 'GetExtensionSupport' == proto.name:
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001186 c_call = proto.c_call().replace("(" + proto.params[0].name, "((VK_PHYSICAL_GPU)gpuw->nextObject", 1)
Jon Ashburn25566352015-04-02 12:06:28 -06001187 funcs.append('%s%s\n'
1188 '{\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001189 ' VK_BASE_LAYER_OBJECT* gpuw = (VK_BASE_LAYER_OBJECT *) %s;\n'
1190 ' VK_RESULT result;\n'
Jon Ashburn25566352015-04-02 12:06:28 -06001191 ' /* This entrypoint is NOT going to init its own dispatch table since loader calls this early */\n'
1192 ' if (!strncmp(pExtName, "%s", strlen("%s")) ||\n'
1193 ' !strncmp(pExtName, "objTrackGetObjectCount", strlen("objTrackGetObjectCount")) ||\n'
1194 ' !strncmp(pExtName, "objTrackGetObjects", strlen("objTrackGetObjects")))\n'
1195 ' {\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001196 ' result = VK_SUCCESS;\n'
Jon Ashburn25566352015-04-02 12:06:28 -06001197 ' } else if (nextTable.GetExtensionSupport != NULL)\n'
1198 ' {\n'
1199 ' %s'
1200 ' result = nextTable.%s;\n'
1201 ' } else\n'
1202 ' {\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001203 ' result = VK_ERROR_INVALID_EXTENSION;\n'
Jon Ashburn25566352015-04-02 12:06:28 -06001204 ' }\n'
1205 '%s'
1206 '}' % (qual, decl, proto.params[0].name, self.layer_name, self.layer_name, using_line, c_call, stmt))
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001207 elif proto.params[0].ty != "VK_PHYSICAL_GPU":
Mike Stroyan723913e2015-04-03 14:39:16 -06001208 funcs.append('%s%s\n'
1209 '{\n'
1210 '%s'
1211 ' %snextTable.%s;\n'
1212 '%s%s'
1213 '%s'
1214 '}' % (qual, decl, using_line, ret_val, proto.c_call(), create_line, destroy_line, stmt))
1215 else:
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001216 c_call = proto.c_call().replace("(" + proto.params[0].name, "((VK_PHYSICAL_GPU)gpuw->nextObject", 1)
Mike Stroyan723913e2015-04-03 14:39:16 -06001217 gpu_state = ''
1218 if 'GetGpuInfo' in proto.name:
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001219 gpu_state = ' if (infoType == VK_INFO_TYPE_PHYSICAL_GPU_QUEUE_PROPERTIES) {\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001220 gpu_state += ' if (pData != NULL) {\n'
Courtney Goeltzenleuchter304a1f82015-04-07 16:23:00 -06001221 gpu_state += ' setGpuQueueInfoState(pData);\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001222 gpu_state += ' }\n'
1223 gpu_state += ' }\n'
1224 funcs.append('%s%s\n'
1225 '{\n'
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001226 ' VK_BASE_LAYER_OBJECT* gpuw = (VK_BASE_LAYER_OBJECT *) %s;\n'
Mike Stroyan723913e2015-04-03 14:39:16 -06001227 '%s'
1228 ' pCurObj = gpuw;\n'
1229 ' loader_platform_thread_once(&tabOnce, init%s);\n'
1230 ' %snextTable.%s;\n'
1231 '%s%s'
1232 '%s'
1233 '%s'
Mike Stroyan3aecdb42015-04-03 17:13:23 -06001234 '}' % (qual, decl, proto.params[0].name, using_line, self.layer_name, ret_val, c_call, create_line, destroy_line, gpu_state, stmt))
Mike Stroyan723913e2015-04-03 14:39:16 -06001235 if 'WsiX11QueuePresent' == proto.name:
1236 funcs.append("#endif")
1237 return "\n\n".join(funcs)
1238
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001239 def generate_body(self):
Mike Stroyan3aecdb42015-04-03 17:13:23 -06001240 self.layer_name = "ObjectTracker"
1241 body = [self._generate_layer_initialization(True, lockname='obj'),
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001242 self._generate_dispatch_entrypoints("VK_LAYER_EXPORT"),
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001243 self._generate_extensions(),
Mike Stroyan2ad66f12015-04-03 17:45:53 -06001244 self._generate_layer_gpa_function(extensions=['objTrackGetObjectCount', 'objTrackGetObjects'])]
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001245
1246 return "\n\n".join(body)
Courtney Goeltzenleuchtere6094fc2014-11-18 10:40:29 -07001247
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001248def main():
1249 subcommands = {
1250 "layer-funcs" : LayerFuncsSubcommand,
1251 "layer-dispatch" : LayerDispatchSubcommand,
Tobin Ehlis907a0522014-11-25 16:59:27 -07001252 "Generic" : GenericLayerSubcommand,
Tobin Ehlis4a636a12015-04-09 09:19:36 -06001253 "APIDump" : APIDumpSubcommand,
Tobin Ehlis907a0522014-11-25 16:59:27 -07001254 "ObjectTracker" : ObjectTrackerSubcommand,
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001255 }
1256
Tobin Ehlis6cd06372014-12-17 17:44:50 -07001257 if len(sys.argv) < 3 or sys.argv[1] not in subcommands or not os.path.exists(sys.argv[2]):
1258 print("Usage: %s <subcommand> <input_header> [options]" % sys.argv[0])
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001259 print
Tobin Ehlis2f3726c2015-01-15 17:51:52 -07001260 print("Available subcommands are: %s" % " ".join(subcommands))
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001261 exit(1)
1262
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001263 hfp = vk_helper.HeaderFileParser(sys.argv[2])
Tobin Ehlis6cd06372014-12-17 17:44:50 -07001264 hfp.parse()
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001265 vk_helper.enum_val_dict = hfp.get_enum_val_dict()
1266 vk_helper.enum_type_dict = hfp.get_enum_type_dict()
1267 vk_helper.struct_dict = hfp.get_struct_dict()
1268 vk_helper.typedef_fwd_dict = hfp.get_typedef_fwd_dict()
1269 vk_helper.typedef_rev_dict = hfp.get_typedef_rev_dict()
1270 vk_helper.types_dict = hfp.get_types_dict()
Tobin Ehlis6cd06372014-12-17 17:44:50 -07001271
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001272 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
1273 subcmd.run()
1274
1275if __name__ == "__main__":
1276 main()