blob: d8d47ad403bd75afc6a5aad3b239abce81961988 [file] [log] [blame]
Mark Lobodzinskid1461482017-07-18 13:56:09 -06001#!/usr/bin/python3 -i
2#
Mark Lobodzinskicd05c1e2019-01-17 15:33:46 -07003# Copyright (c) 2015-2019 The Khronos Group Inc.
4# Copyright (c) 2015-2019 Valve Corporation
5# Copyright (c) 2015-2019 LunarG, Inc.
6# Copyright (c) 2015-2019 Google Inc.
Mark Lobodzinskid1461482017-07-18 13:56:09 -06007#
8# Licensed under the Apache License, Version 2.0 (the "License");
9# you may not use this file except in compliance with the License.
10# You may obtain a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS,
16# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17# See the License for the specific language governing permissions and
18# limitations under the License.
19#
20# Author: Mark Lobodzinski <mark@lunarg.com>
Dave Houlton57ae22f2018-05-18 16:20:52 -060021# Author: Dave Houlton <daveh@lunarg.com>
Mark Lobodzinskid1461482017-07-18 13:56:09 -060022
Dave Houlton57ae22f2018-05-18 16:20:52 -060023import os,re,sys,string,json
Mark Lobodzinskid1461482017-07-18 13:56:09 -060024import xml.etree.ElementTree as etree
25from generator import *
26from collections import namedtuple
Mark Lobodzinski62f71562017-10-24 13:41:18 -060027from common_codegen import *
Mark Lobodzinskid1461482017-07-18 13:56:09 -060028
Jamie Madill8d4cda22017-11-08 13:40:09 -050029# This is a workaround to use a Python 2.7 and 3.x compatible syntax.
30from io import open
31
Mark Lobodzinskid1461482017-07-18 13:56:09 -060032# ObjectTrackerGeneratorOptions - subclass of GeneratorOptions.
33#
34# Adds options used by ObjectTrackerOutputGenerator objects during
35# object_tracker layer generation.
36#
37# Additional members
38# prefixText - list of strings to prefix generated header with
39# (usually a copyright statement + calling convention macros).
40# protectFile - True if multiple inclusion protection should be
41# generated (based on the filename) around the entire header.
42# protectFeature - True if #ifndef..#endif protection should be
43# generated around a feature interface in the header file.
44# genFuncPointers - True if function pointer typedefs should be
45# generated
46# protectProto - If conditional protection should be generated
47# around prototype declarations, set to either '#ifdef'
48# to require opt-in (#ifdef protectProtoStr) or '#ifndef'
49# to require opt-out (#ifndef protectProtoStr). Otherwise
50# set to None.
51# protectProtoStr - #ifdef/#ifndef symbol to use around prototype
52# declarations, if protectProto is set
53# apicall - string to use for the function declaration prefix,
54# such as APICALL on Windows.
55# apientry - string to use for the calling convention macro,
56# in typedefs, such as APIENTRY.
57# apientryp - string to use for the calling convention macro
58# in function pointer typedefs, such as APIENTRYP.
59# indentFuncProto - True if prototype declarations should put each
60# parameter on a separate line
61# indentFuncPointer - True if typedefed function pointers should put each
62# parameter on a separate line
63# alignFuncParam - if nonzero and parameters are being put on a
64# separate line, align parameter names at the specified column
65class ObjectTrackerGeneratorOptions(GeneratorOptions):
66 def __init__(self,
Mike Schuchardt21638df2019-03-16 10:52:02 -070067 conventions = None,
Mark Lobodzinskid1461482017-07-18 13:56:09 -060068 filename = None,
69 directory = '.',
70 apiname = None,
71 profile = None,
72 versions = '.*',
73 emitversions = '.*',
74 defaultExtensions = None,
75 addExtensions = None,
76 removeExtensions = None,
Mark Lobodzinski62f71562017-10-24 13:41:18 -060077 emitExtensions = None,
Mark Lobodzinskid1461482017-07-18 13:56:09 -060078 sortProcedure = regSortFeatures,
79 prefixText = "",
80 genFuncPointers = True,
81 protectFile = True,
82 protectFeature = True,
Mark Lobodzinskid1461482017-07-18 13:56:09 -060083 apicall = '',
84 apientry = '',
85 apientryp = '',
86 indentFuncProto = True,
87 indentFuncPointer = False,
Mark Lobodzinski62f71562017-10-24 13:41:18 -060088 alignFuncParam = 0,
Mark Lobodzinski27a9e7c2018-05-31 16:01:57 -060089 expandEnumerants = True,
90 valid_usage_path = ''):
Mike Schuchardt21638df2019-03-16 10:52:02 -070091 GeneratorOptions.__init__(self, conventions, filename, directory, apiname, profile,
Mark Lobodzinskid1461482017-07-18 13:56:09 -060092 versions, emitversions, defaultExtensions,
Mark Lobodzinski62f71562017-10-24 13:41:18 -060093 addExtensions, removeExtensions, emitExtensions, sortProcedure)
Mark Lobodzinskid1461482017-07-18 13:56:09 -060094 self.prefixText = prefixText
95 self.genFuncPointers = genFuncPointers
96 self.protectFile = protectFile
97 self.protectFeature = protectFeature
Mark Lobodzinskid1461482017-07-18 13:56:09 -060098 self.apicall = apicall
99 self.apientry = apientry
100 self.apientryp = apientryp
101 self.indentFuncProto = indentFuncProto
102 self.indentFuncPointer = indentFuncPointer
103 self.alignFuncParam = alignFuncParam
Mark Lobodzinski62f71562017-10-24 13:41:18 -0600104 self.expandEnumerants = expandEnumerants
Mark Lobodzinski27a9e7c2018-05-31 16:01:57 -0600105 self.valid_usage_path = valid_usage_path
Mark Lobodzinski62f71562017-10-24 13:41:18 -0600106
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600107
108# ObjectTrackerOutputGenerator - subclass of OutputGenerator.
109# Generates object_tracker layer object validation code
110#
111# ---- methods ----
112# ObjectTrackerOutputGenerator(errFile, warnFile, diagFile) - args as for OutputGenerator. Defines additional internal state.
113# ---- methods overriding base class ----
114# beginFile(genOpts)
115# endFile()
116# beginFeature(interface, emit)
117# endFeature()
118# genCmd(cmdinfo)
119# genStruct()
120# genType()
121class ObjectTrackerOutputGenerator(OutputGenerator):
122 """Generate ObjectTracker code based on XML element attributes"""
123 # This is an ordered list of sections in the header file.
124 ALL_SECTIONS = ['command']
125 def __init__(self,
126 errFile = sys.stderr,
127 warnFile = sys.stderr,
128 diagFile = sys.stdout):
129 OutputGenerator.__init__(self, errFile, warnFile, diagFile)
130 self.INDENT_SPACES = 4
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600131 self.prototypes = []
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600132 self.instance_extensions = []
133 self.device_extensions = []
134 # Commands which are not autogenerated but still intercepted
135 self.no_autogen_list = [
136 'vkDestroyInstance',
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600137 'vkCreateInstance',
138 'vkEnumeratePhysicalDevices',
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600139 'vkGetPhysicalDeviceQueueFamilyProperties',
140 'vkGetPhysicalDeviceQueueFamilyProperties2',
141 'vkGetPhysicalDeviceQueueFamilyProperties2KHR',
142 'vkGetDeviceQueue',
143 'vkGetDeviceQueue2',
144 'vkCreateDescriptorSetLayout',
145 'vkDestroyDescriptorPool',
146 'vkDestroyCommandPool',
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600147 'vkAllocateCommandBuffers',
148 'vkAllocateDescriptorSets',
149 'vkFreeDescriptorSets',
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600150 'vkFreeCommandBuffers',
151 'vkUpdateDescriptorSets',
152 'vkBeginCommandBuffer',
Mark Lobodzinski5a1c8d22018-07-02 10:28:12 -0600153 'vkGetDescriptorSetLayoutSupport',
154 'vkGetDescriptorSetLayoutSupportKHR',
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600155 'vkDestroySwapchainKHR',
156 'vkGetSwapchainImagesKHR',
157 'vkCmdPushDescriptorSetKHR',
158 'vkDestroyDevice',
159 'vkResetDescriptorPool',
Shannon McPherson9d5167f2018-05-02 15:24:37 -0600160 'vkGetPhysicalDeviceDisplayPropertiesKHR',
Piers Daniell16c253f2018-05-30 14:34:05 -0600161 'vkGetPhysicalDeviceDisplayProperties2KHR',
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600162 'vkGetDisplayModePropertiesKHR',
Piers Daniell16c253f2018-05-30 14:34:05 -0600163 'vkGetDisplayModeProperties2KHR',
Shannon McPhersonf7d9cf62019-06-26 09:23:57 -0600164 'vkAcquirePerformanceConfigurationINTEL',
165 'vkReleasePerformanceConfigurationINTEL',
166 'vkQueueSetPerformanceConfigurationINTEL',
Tobias Hectorc9057422019-07-23 12:15:52 +0100167 'vkCreateFramebuffer',
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600168 ]
169 # These VUIDS are not implicit, but are best handled in this layer. Codegen for vkDestroy calls will generate a key
170 # which is translated here into a good VU. Saves ~40 checks.
171 self.manual_vuids = dict()
172 self.manual_vuids = {
Dave Houlton57ae22f2018-05-18 16:20:52 -0600173 "fence-compatalloc": "\"VUID-vkDestroyFence-fence-01121\"",
174 "fence-nullalloc": "\"VUID-vkDestroyFence-fence-01122\"",
175 "event-compatalloc": "\"VUID-vkDestroyEvent-event-01146\"",
176 "event-nullalloc": "\"VUID-vkDestroyEvent-event-01147\"",
177 "buffer-compatalloc": "\"VUID-vkDestroyBuffer-buffer-00923\"",
178 "buffer-nullalloc": "\"VUID-vkDestroyBuffer-buffer-00924\"",
179 "image-compatalloc": "\"VUID-vkDestroyImage-image-01001\"",
180 "image-nullalloc": "\"VUID-vkDestroyImage-image-01002\"",
181 "shaderModule-compatalloc": "\"VUID-vkDestroyShaderModule-shaderModule-01092\"",
182 "shaderModule-nullalloc": "\"VUID-vkDestroyShaderModule-shaderModule-01093\"",
183 "pipeline-compatalloc": "\"VUID-vkDestroyPipeline-pipeline-00766\"",
184 "pipeline-nullalloc": "\"VUID-vkDestroyPipeline-pipeline-00767\"",
185 "sampler-compatalloc": "\"VUID-vkDestroySampler-sampler-01083\"",
186 "sampler-nullalloc": "\"VUID-vkDestroySampler-sampler-01084\"",
187 "renderPass-compatalloc": "\"VUID-vkDestroyRenderPass-renderPass-00874\"",
188 "renderPass-nullalloc": "\"VUID-vkDestroyRenderPass-renderPass-00875\"",
189 "descriptorUpdateTemplate-compatalloc": "\"VUID-vkDestroyDescriptorUpdateTemplate-descriptorSetLayout-00356\"",
190 "descriptorUpdateTemplate-nullalloc": "\"VUID-vkDestroyDescriptorUpdateTemplate-descriptorSetLayout-00357\"",
191 "imageView-compatalloc": "\"VUID-vkDestroyImageView-imageView-01027\"",
192 "imageView-nullalloc": "\"VUID-vkDestroyImageView-imageView-01028\"",
193 "pipelineCache-compatalloc": "\"VUID-vkDestroyPipelineCache-pipelineCache-00771\"",
194 "pipelineCache-nullalloc": "\"VUID-vkDestroyPipelineCache-pipelineCache-00772\"",
195 "pipelineLayout-compatalloc": "\"VUID-vkDestroyPipelineLayout-pipelineLayout-00299\"",
196 "pipelineLayout-nullalloc": "\"VUID-vkDestroyPipelineLayout-pipelineLayout-00300\"",
197 "descriptorSetLayout-compatalloc": "\"VUID-vkDestroyDescriptorSetLayout-descriptorSetLayout-00284\"",
198 "descriptorSetLayout-nullalloc": "\"VUID-vkDestroyDescriptorSetLayout-descriptorSetLayout-00285\"",
199 "semaphore-compatalloc": "\"VUID-vkDestroySemaphore-semaphore-01138\"",
200 "semaphore-nullalloc": "\"VUID-vkDestroySemaphore-semaphore-01139\"",
201 "queryPool-compatalloc": "\"VUID-vkDestroyQueryPool-queryPool-00794\"",
202 "queryPool-nullalloc": "\"VUID-vkDestroyQueryPool-queryPool-00795\"",
203 "bufferView-compatalloc": "\"VUID-vkDestroyBufferView-bufferView-00937\"",
204 "bufferView-nullalloc": "\"VUID-vkDestroyBufferView-bufferView-00938\"",
205 "surface-compatalloc": "\"VUID-vkDestroySurfaceKHR-surface-01267\"",
206 "surface-nullalloc": "\"VUID-vkDestroySurfaceKHR-surface-01268\"",
207 "framebuffer-compatalloc": "\"VUID-vkDestroyFramebuffer-framebuffer-00893\"",
208 "framebuffer-nullalloc": "\"VUID-vkDestroyFramebuffer-framebuffer-00894\"",
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600209 }
210
211 # Commands shadowed by interface functions and are not implemented
212 self.interface_functions = [
213 ]
214 self.headerVersion = None
215 # Internal state - accumulators for different inner block text
216 self.sections = dict([(section, []) for section in self.ALL_SECTIONS])
John Zulaufbb6e5e42018-08-06 16:00:07 -0600217 self.cmd_list = [] # list of commands processed to maintain ordering
218 self.cmd_info_dict = {} # Per entry-point data for code generation and validation
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600219 self.structMembers = [] # List of StructMemberData records for all Vulkan structs
220 self.extension_structs = [] # List of all structs or sister-structs containing handles
221 # A sister-struct may contain no handles but shares <validextensionstructs> with one that does
222 self.structTypes = dict() # Map of Vulkan struct typename to required VkStructureType
223 self.struct_member_dict = dict()
224 # Named tuples to store struct and command data
225 self.StructType = namedtuple('StructType', ['name', 'value'])
John Zulaufbb6e5e42018-08-06 16:00:07 -0600226 self.CmdInfoData = namedtuple('CmdInfoData', ['name', 'cmdinfo', 'members', 'extra_protect', 'alias', 'iscreate', 'isdestroy', 'allocator'])
227 self.CommandParam = namedtuple('CommandParam', ['type', 'name', 'isconst', 'isoptional', 'iscount', 'iscreate', 'len', 'extstructs', 'cdecl', 'islocal'])
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600228 self.StructMemberData = namedtuple('StructMemberData', ['name', 'members'])
229 self.object_types = [] # List of all handle types
230 self.valid_vuids = set() # Set of all valid VUIDs
Dave Houlton57ae22f2018-05-18 16:20:52 -0600231 self.vuid_dict = dict() # VUID dictionary (from JSON)
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600232 #
233 # Check if the parameter passed in is optional
234 def paramIsOptional(self, param):
235 # See if the handle is optional
236 isoptional = False
237 # Simple, if it's optional, return true
238 optString = param.attrib.get('optional')
239 if optString:
240 if optString == 'true':
241 isoptional = True
242 elif ',' in optString:
243 opts = []
244 for opt in optString.split(','):
245 val = opt.strip()
246 if val == 'true':
247 opts.append(True)
248 elif val == 'false':
249 opts.append(False)
250 else:
251 print('Unrecognized len attribute value',val)
252 isoptional = opts
John Zulauf9f6788c2018-04-04 14:54:11 -0600253 if not isoptional:
254 # Matching logic in parameter validation and ValidityOutputGenerator.isHandleOptional
255 optString = param.attrib.get('noautovalidity')
256 if optString and optString == 'true':
257 isoptional = True;
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600258 return isoptional
259 #
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600260 # Get VUID identifier from implicit VUID tag
Dave Houlton57ae22f2018-05-18 16:20:52 -0600261 def GetVuid(self, parent, suffix):
262 vuid_string = 'VUID-%s-%s' % (parent, suffix)
263 vuid = "kVUIDUndefined"
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600264 if '->' in vuid_string:
Dave Houlton57ae22f2018-05-18 16:20:52 -0600265 return vuid
266 if vuid_string in self.valid_vuids:
267 vuid = "\"%s\"" % vuid_string
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600268 else:
John Zulaufbb6e5e42018-08-06 16:00:07 -0600269 alias = self.cmd_info_dict[parent].alias if parent in self.cmd_info_dict else None
270 if alias:
271 alias_string = 'VUID-%s-%s' % (alias, suffix)
Dave Houlton57ae22f2018-05-18 16:20:52 -0600272 if alias_string in self.valid_vuids:
Shannon McPhersonb1a8dd62019-06-06 11:42:47 -0600273 vuid = "\"%s\"" % alias_string
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600274 return vuid
275 #
276 # Increases indent by 4 spaces and tracks it globally
277 def incIndent(self, indent):
278 inc = ' ' * self.INDENT_SPACES
279 if indent:
280 return indent + inc
281 return inc
282 #
283 # Decreases indent by 4 spaces and tracks it globally
284 def decIndent(self, indent):
285 if indent and (len(indent) > self.INDENT_SPACES):
286 return indent[:-self.INDENT_SPACES]
287 return ''
288 #
289 # Override makeProtoName to drop the "vk" prefix
290 def makeProtoName(self, name, tail):
291 return self.genOpts.apientry + name[2:] + tail
292 #
293 # Check if the parameter passed in is a pointer to an array
294 def paramIsArray(self, param):
295 return param.attrib.get('len') is not None
Gabríel Arthúr Péturssonfdcb5402018-03-20 21:52:06 +0000296
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600297 #
298 # Generate the object tracker undestroyed object validation function
299 def GenReportFunc(self):
300 output_func = ''
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600301 output_func += 'bool ObjectLifetimes::ReportUndestroyedObjects(VkDevice device, const std::string& error_code) {\n'
Mark Lobodzinski5183a032018-09-13 14:44:28 -0600302 output_func += ' bool skip = false;\n'
303 output_func += ' skip |= DeviceReportUndestroyedObjects(device, kVulkanObjectTypeCommandBuffer, error_code);\n'
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600304 for handle in self.object_types:
Mike Schuchardtf8690262019-07-11 10:08:33 -0700305 if self.handle_types.IsNonDispatchable(handle):
Mark Lobodzinski5183a032018-09-13 14:44:28 -0600306 output_func += ' skip |= DeviceReportUndestroyedObjects(device, %s, error_code);\n' % (self.GetVulkanObjType(handle))
307 output_func += ' return skip;\n'
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600308 output_func += '}\n'
309 return output_func
Gabríel Arthúr Péturssonfdcb5402018-03-20 21:52:06 +0000310
311 #
312 # Generate the object tracker undestroyed object destruction function
313 def GenDestroyFunc(self):
314 output_func = ''
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600315 output_func += 'void ObjectLifetimes::DestroyUndestroyedObjects(VkDevice device) {\n'
Gabríel Arthúr Péturssonfdcb5402018-03-20 21:52:06 +0000316 output_func += ' DeviceDestroyUndestroyedObjects(device, kVulkanObjectTypeCommandBuffer);\n'
317 for handle in self.object_types:
Mike Schuchardtf8690262019-07-11 10:08:33 -0700318 if self.handle_types.IsNonDispatchable(handle):
Gabríel Arthúr Péturssonfdcb5402018-03-20 21:52:06 +0000319 output_func += ' DeviceDestroyUndestroyedObjects(device, %s);\n' % (self.GetVulkanObjType(handle))
320 output_func += '}\n'
321 return output_func
322
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600323 #
Dave Houlton57ae22f2018-05-18 16:20:52 -0600324 # Walk the JSON-derived dict and find all "vuid" key values
325 def ExtractVUIDs(self, d):
326 if hasattr(d, 'items'):
327 for k, v in d.items():
328 if k == "vuid":
329 yield v
330 elif isinstance(v, dict):
331 for s in self.ExtractVUIDs(v):
332 yield s
333 elif isinstance (v, list):
334 for l in v:
335 for s in self.ExtractVUIDs(l):
336 yield s
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600337 #
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600338 # Separate content for validation source and header files
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600339 def otwrite(self, dest, formatstring):
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600340 if 'object_tracker.h' in self.genOpts.filename and (dest == 'hdr' or dest == 'both'):
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600341 write(formatstring, file=self.outFile)
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600342 elif 'object_tracker.cpp' in self.genOpts.filename and (dest == 'cpp' or dest == 'both'):
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600343 write(formatstring, file=self.outFile)
Dave Houlton57ae22f2018-05-18 16:20:52 -0600344
345 #
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600346 # Called at beginning of processing as file is opened
347 def beginFile(self, genOpts):
348 OutputGenerator.beginFile(self, genOpts)
Mark Lobodzinski27a9e7c2018-05-31 16:01:57 -0600349
Mike Schuchardt09a1c752019-06-20 12:04:38 -0700350 # Initialize members that require the tree
351 self.handle_types = GetHandleTypes(self.registry.tree)
352 self.type_categories = GetTypeCategories(self.registry.tree)
353
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600354 header_file = (genOpts.filename == 'object_tracker.h')
355 source_file = (genOpts.filename == 'object_tracker.cpp')
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600356
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600357 if not header_file and not source_file:
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600358 print("Error: Output Filenames have changed, update generator source.\n")
359 sys.exit(1)
360
Mark Lobodzinski27a9e7c2018-05-31 16:01:57 -0600361 self.valid_usage_path = genOpts.valid_usage_path
362 vu_json_filename = os.path.join(self.valid_usage_path + os.sep, 'validusage.json')
363 if os.path.isfile(vu_json_filename):
364 json_file = open(vu_json_filename, 'r')
365 self.vuid_dict = json.load(json_file)
366 json_file.close()
367 if len(self.vuid_dict) == 0:
368 print("Error: Could not find, or error loading %s/validusage.json\n", vu_json_filename)
369 sys.exit(1)
370
Dave Houlton57ae22f2018-05-18 16:20:52 -0600371 # Build a set of all vuid text strings found in validusage.json
372 for json_vuid_string in self.ExtractVUIDs(self.vuid_dict):
373 self.valid_vuids.add(json_vuid_string)
374
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600375 # File Comment
376 file_comment = '// *** THIS FILE IS GENERATED - DO NOT EDIT ***\n'
377 file_comment += '// See object_tracker_generator.py for modifications\n'
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600378 self.otwrite('both', file_comment)
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600379 # Copyright Statement
380 copyright = ''
381 copyright += '\n'
382 copyright += '/***************************************************************************\n'
383 copyright += ' *\n'
Mark Lobodzinskicd05c1e2019-01-17 15:33:46 -0700384 copyright += ' * Copyright (c) 2015-2019 The Khronos Group Inc.\n'
385 copyright += ' * Copyright (c) 2015-2019 Valve Corporation\n'
386 copyright += ' * Copyright (c) 2015-2019 LunarG, Inc.\n'
387 copyright += ' * Copyright (c) 2015-2019 Google Inc.\n'
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600388 copyright += ' *\n'
389 copyright += ' * Licensed under the Apache License, Version 2.0 (the "License");\n'
390 copyright += ' * you may not use this file except in compliance with the License.\n'
391 copyright += ' * You may obtain a copy of the License at\n'
392 copyright += ' *\n'
393 copyright += ' * http://www.apache.org/licenses/LICENSE-2.0\n'
394 copyright += ' *\n'
395 copyright += ' * Unless required by applicable law or agreed to in writing, software\n'
396 copyright += ' * distributed under the License is distributed on an "AS IS" BASIS,\n'
397 copyright += ' * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n'
398 copyright += ' * See the License for the specific language governing permissions and\n'
399 copyright += ' * limitations under the License.\n'
400 copyright += ' *\n'
401 copyright += ' * Author: Mark Lobodzinski <mark@lunarg.com>\n'
Dave Houlton57ae22f2018-05-18 16:20:52 -0600402 copyright += ' * Author: Dave Houlton <daveh@lunarg.com>\n'
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600403 copyright += ' *\n'
404 copyright += ' ****************************************************************************/\n'
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600405 self.otwrite('both', copyright)
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600406 self.newline()
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600407 self.otwrite('cpp', '#include "chassis.h"')
408 self.otwrite('cpp', '#include "object_lifetime_validation.h"')
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600409
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600410 #
411 # Now that the data is all collected and complete, generate and output the object validation routines
412 def endFile(self):
413 self.struct_member_dict = dict(self.structMembers)
414 # Generate the list of APIs that might need to handle wrapped extension structs
415 # self.GenerateCommandWrapExtensionList()
416 self.WrapCommands()
417 # Build undestroyed objects reporting function
418 report_func = self.GenReportFunc()
419 self.newline()
Gabríel Arthúr Péturssonfdcb5402018-03-20 21:52:06 +0000420 # Build undestroyed objects destruction function
421 destroy_func = self.GenDestroyFunc()
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600422 self.otwrite('cpp', '\n')
423 self.otwrite('cpp', '// ObjectTracker undestroyed objects validation function')
424 self.otwrite('cpp', '%s' % report_func)
425 self.otwrite('cpp', '%s' % destroy_func)
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600426 # Actually write the interface to the output file.
427 if (self.emit):
428 self.newline()
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600429 if self.featureExtraProtect is not None:
430 prot = '#ifdef %s' % self.featureExtraProtect
431 self.otwrite('both', '%s' % prot)
432 # Write the object_tracker code to the file
433 if self.sections['command']:
434 source = ('\n'.join(self.sections['command']))
435 self.otwrite('both', '%s' % source)
Michał Janiszewski3c3ce9e2018-10-30 23:25:21 +0100436 if (self.featureExtraProtect is not None):
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600437 prot = '\n#endif // %s', self.featureExtraProtect
438 self.otwrite('both', prot)
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600439 else:
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600440 self.otwrite('both', '\n')
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600441
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600442
443 self.otwrite('hdr', 'void PostCallRecordDestroyInstance(VkInstance instance, const VkAllocationCallbacks *pAllocator);')
444 self.otwrite('hdr', 'void PreCallRecordResetDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool, VkDescriptorPoolResetFlags flags);')
445 self.otwrite('hdr', 'void PostCallRecordGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice, uint32_t *pQueueFamilyPropertyCount, VkQueueFamilyProperties *pQueueFamilyProperties);')
446 self.otwrite('hdr', 'void PreCallRecordFreeCommandBuffers(VkDevice device, VkCommandPool commandPool, uint32_t commandBufferCount, const VkCommandBuffer *pCommandBuffers);')
447 self.otwrite('hdr', 'void PreCallRecordFreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool, uint32_t descriptorSetCount, const VkDescriptorSet *pDescriptorSets);')
448 self.otwrite('hdr', 'void PostCallRecordGetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice, uint32_t *pQueueFamilyPropertyCount, VkQueueFamilyProperties2KHR *pQueueFamilyProperties);')
449 self.otwrite('hdr', 'void PostCallRecordGetPhysicalDeviceQueueFamilyProperties2KHR(VkPhysicalDevice physicalDevice, uint32_t *pQueueFamilyPropertyCount, VkQueueFamilyProperties2KHR *pQueueFamilyProperties);')
Mark Lobodzinskicd05c1e2019-01-17 15:33:46 -0700450 self.otwrite('hdr', 'void PostCallRecordGetPhysicalDeviceDisplayPropertiesKHR(VkPhysicalDevice physicalDevice, uint32_t *pPropertyCount, VkDisplayPropertiesKHR *pProperties, VkResult result);')
451 self.otwrite('hdr', 'void PostCallRecordGetDisplayModePropertiesKHR(VkPhysicalDevice physicalDevice, VkDisplayKHR display, uint32_t *pPropertyCount, VkDisplayModePropertiesKHR *pProperties, VkResult result);')
452 self.otwrite('hdr', 'void PostCallRecordGetPhysicalDeviceDisplayProperties2KHR(VkPhysicalDevice physicalDevice, uint32_t *pPropertyCount, VkDisplayProperties2KHR *pProperties, VkResult result);')
453 self.otwrite('hdr', 'void PostCallRecordGetDisplayModeProperties2KHR(VkPhysicalDevice physicalDevice, VkDisplayKHR display, uint32_t *pPropertyCount, VkDisplayModeProperties2KHR *pProperties, VkResult result);')
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600454 OutputGenerator.endFile(self)
455 #
456 # Processing point at beginning of each extension definition
457 def beginFeature(self, interface, emit):
458 # Start processing in superclass
459 OutputGenerator.beginFeature(self, interface, emit)
460 self.headerVersion = None
Mark Lobodzinski62f71562017-10-24 13:41:18 -0600461 self.featureExtraProtect = GetFeatureProtect(interface)
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600462
Mark Lobodzinski31964ca2017-09-18 14:15:09 -0600463 if self.featureName != 'VK_VERSION_1_0' and self.featureName != 'VK_VERSION_1_1':
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600464 white_list_entry = []
Michał Janiszewski3c3ce9e2018-10-30 23:25:21 +0100465 if (self.featureExtraProtect is not None):
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600466 white_list_entry += [ '#ifdef %s' % self.featureExtraProtect ]
467 white_list_entry += [ '"%s"' % self.featureName ]
Michał Janiszewski3c3ce9e2018-10-30 23:25:21 +0100468 if (self.featureExtraProtect is not None):
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600469 white_list_entry += [ '#endif' ]
470 featureType = interface.get('type')
471 if featureType == 'instance':
472 self.instance_extensions += white_list_entry
473 elif featureType == 'device':
474 self.device_extensions += white_list_entry
475 #
476 # Processing point at end of each extension definition
477 def endFeature(self):
478 # Finish processing in superclass
479 OutputGenerator.endFeature(self)
480 #
481 # Process enums, structs, etc.
Mike Schuchardtf375c7c2017-12-28 11:23:48 -0700482 def genType(self, typeinfo, name, alias):
483 OutputGenerator.genType(self, typeinfo, name, alias)
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600484 typeElem = typeinfo.elem
485 # If the type is a struct type, traverse the imbedded <member> tags generating a structure.
486 # Otherwise, emit the tag text.
487 category = typeElem.get('category')
488 if (category == 'struct' or category == 'union'):
Mike Schuchardtf375c7c2017-12-28 11:23:48 -0700489 self.genStruct(typeinfo, name, alias)
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600490 if category == 'handle':
491 self.object_types.append(name)
492 #
493 # Append a definition to the specified section
494 def appendSection(self, section, text):
495 # self.sections[section].append('SECTION: ' + section + '\n')
496 self.sections[section].append(text)
497 #
498 # Check if the parameter passed in is a pointer
499 def paramIsPointer(self, param):
500 ispointer = False
501 for elem in param:
Raul Tambre7b300182019-05-04 11:25:14 +0300502 if elem.tag == 'type' and elem.tail is not None and '*' in elem.tail:
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600503 ispointer = True
504 return ispointer
505 #
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600506 # Retrieve the type and name for a parameter
507 def getTypeNameTuple(self, param):
508 type = ''
509 name = ''
510 for elem in param:
511 if elem.tag == 'type':
512 type = noneStr(elem.text)
513 elif elem.tag == 'name':
514 name = noneStr(elem.text)
515 return (type, name)
516 #
517 # Retrieve the value of the len tag
518 def getLen(self, param):
519 result = None
520 len = param.attrib.get('len')
521 if len and len != 'null-terminated':
522 # For string arrays, 'len' can look like 'count,null-terminated', indicating that we
523 # have a null terminated array of strings. We strip the null-terminated from the
524 # 'len' field and only return the parameter specifying the string count
525 if 'null-terminated' in len:
526 result = len.split(',')[0]
527 else:
528 result = len
529 # Spec has now notation for len attributes, using :: instead of platform specific pointer symbol
530 result = str(result).replace('::', '->')
531 return result
532 #
533 # Generate a VkStructureType based on a structure typename
534 def genVkStructureType(self, typename):
535 # Add underscore between lowercase then uppercase
536 value = re.sub('([a-z0-9])([A-Z])', r'\1_\2', typename)
537 # Change to uppercase
538 value = value.upper()
539 # Add STRUCTURE_TYPE_
540 return re.sub('VK_', 'VK_STRUCTURE_TYPE_', value)
541 #
542 # Struct parameter check generation.
543 # This is a special case of the <type> tag where the contents are interpreted as a set of
544 # <member> tags instead of freeform C type declarations. The <member> tags are just like
545 # <param> tags - they are a declaration of a struct or union member. Only simple member
546 # declarations are supported (no nested structs etc.)
Mike Schuchardtf375c7c2017-12-28 11:23:48 -0700547 def genStruct(self, typeinfo, typeName, alias):
548 OutputGenerator.genStruct(self, typeinfo, typeName, alias)
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600549 members = typeinfo.elem.findall('.//member')
550 # Iterate over members once to get length parameters for arrays
551 lens = set()
552 for member in members:
553 len = self.getLen(member)
554 if len:
555 lens.add(len)
556 # Generate member info
557 membersInfo = []
558 for member in members:
559 # Get the member's type and name
560 info = self.getTypeNameTuple(member)
561 type = info[0]
562 name = info[1]
563 cdecl = self.makeCParamDecl(member, 0)
564 # Process VkStructureType
565 if type == 'VkStructureType':
566 # Extract the required struct type value from the comments
567 # embedded in the original text defining the 'typeinfo' element
568 rawXml = etree.tostring(typeinfo.elem).decode('ascii')
569 result = re.search(r'VK_STRUCTURE_TYPE_\w+', rawXml)
570 if result:
571 value = result.group(0)
572 else:
573 value = self.genVkStructureType(typeName)
574 # Store the required type value
575 self.structTypes[typeName] = self.StructType(name=name, value=value)
576 # Store pointer/array/string info
577 extstructs = member.attrib.get('validextensionstructs') if name == 'pNext' else None
578 membersInfo.append(self.CommandParam(type=type,
579 name=name,
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600580 isconst=True if 'const' in cdecl else False,
581 isoptional=self.paramIsOptional(member),
582 iscount=True if name in lens else False,
583 len=self.getLen(member),
584 extstructs=extstructs,
585 cdecl=cdecl,
586 islocal=False,
John Zulaufbb6e5e42018-08-06 16:00:07 -0600587 iscreate=False))
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600588 self.structMembers.append(self.StructMemberData(name=typeName, members=membersInfo))
589 #
590 # Insert a lock_guard line
591 def lock_guard(self, indent):
592 return '%sstd::lock_guard<std::mutex> lock(global_lock);\n' % indent
593 #
594 # Determine if a struct has an object as a member or an embedded member
595 def struct_contains_object(self, struct_item):
596 struct_member_dict = dict(self.structMembers)
597 struct_members = struct_member_dict[struct_item]
598
599 for member in struct_members:
Mike Schuchardtf8690262019-07-11 10:08:33 -0700600 if member.type in self.handle_types:
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600601 return True
Mike Schuchardtcf2eda02018-08-11 20:34:07 -0700602 # recurse for member structs, guard against infinite recursion
603 elif member.type in struct_member_dict and member.type != struct_item:
604 if self.struct_contains_object(member.type):
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600605 return True
606 return False
607 #
608 # Return list of struct members which contain, or whose sub-structures contain an obj in a given list of parameters or members
609 def getParmeterStructsWithObjects(self, item_list):
610 struct_list = set()
611 for item in item_list:
612 paramtype = item.find('type')
Mike Schuchardt09a1c752019-06-20 12:04:38 -0700613 typecategory = self.type_categories[paramtype.text]
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600614 if typecategory == 'struct':
615 if self.struct_contains_object(paramtype.text) == True:
616 struct_list.add(item)
617 return struct_list
618 #
619 # Return list of objects from a given list of parameters or members
620 def getObjectsInParameterList(self, item_list, create_func):
621 object_list = set()
622 if create_func == True:
623 member_list = item_list[0:-1]
624 else:
625 member_list = item_list
626 for item in member_list:
Mike Schuchardtf8690262019-07-11 10:08:33 -0700627 if paramtype.text in self.handle_types:
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600628 object_list.add(item)
629 return object_list
630 #
631 # Construct list of extension structs containing handles, or extension structs that share a <validextensionstructs>
Shannon McPherson9d5167f2018-05-02 15:24:37 -0600632 # tag WITH an extension struct containing handles.
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600633 def GenerateCommandWrapExtensionList(self):
634 for struct in self.structMembers:
635 if (len(struct.members) > 1) and struct.members[1].extstructs is not None:
636 found = False;
637 for item in struct.members[1].extstructs.split(','):
638 if item != '' and self.struct_contains_object(item) == True:
639 found = True
640 if found == True:
641 for item in struct.members[1].extstructs.split(','):
642 if item != '' and item not in self.extension_structs:
643 self.extension_structs.append(item)
644 #
645 # Returns True if a struct may have a pNext chain containing an object
646 def StructWithExtensions(self, struct_type):
647 if struct_type in self.struct_member_dict:
648 param_info = self.struct_member_dict[struct_type]
649 if (len(param_info) > 1) and param_info[1].extstructs is not None:
650 for item in param_info[1].extstructs.split(','):
651 if item in self.extension_structs:
652 return True
653 return False
654 #
655 # Generate VulkanObjectType from object type
656 def GetVulkanObjType(self, type):
657 return 'kVulkanObjectType%s' % type[2:]
658 #
659 # Return correct dispatch table type -- instance or device
660 def GetDispType(self, type):
661 return 'instance' if type in ['VkInstance', 'VkPhysicalDevice'] else 'device'
662 #
663 # Generate source for creating a Vulkan object
John Zulaufbb6e5e42018-08-06 16:00:07 -0600664 def generate_create_object_code(self, indent, proto, params, cmd_info, allocator):
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600665 create_obj_code = ''
666 handle_type = params[-1].find('type')
Mark Lobodzinskidcf2bd22019-02-27 16:30:35 -0700667 is_create_pipelines = False
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600668
Mike Schuchardtf8690262019-07-11 10:08:33 -0700669 if handle_type.text in self.handle_types:
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600670 # Check for special case where multiple handles are returned
671 object_array = False
672 if cmd_info[-1].len is not None:
673 object_array = True;
674 handle_name = params[-1].find('name')
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600675 object_dest = '*%s' % handle_name.text
676 if object_array == True:
Mark Lobodzinskidcf2bd22019-02-27 16:30:35 -0700677 if 'CreateGraphicsPipelines' in proto.text or 'CreateComputePipelines' in proto.text or 'CreateRayTracingPipelines' in proto.text:
678 is_create_pipelines = True
679 create_obj_code += '%sif (VK_ERROR_VALIDATION_FAILED_EXT == result) return;\n' % indent
Mark Lobodzinski1b3b6cc2019-07-09 09:19:13 -0600680 create_obj_code += '%sif (%s) {\n' % (indent, handle_name.text)
681 indent = self.incIndent(indent)
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600682 countispointer = ''
683 if 'uint32_t*' in cmd_info[-2].cdecl:
684 countispointer = '*'
685 create_obj_code += '%sfor (uint32_t index = 0; index < %s%s; index++) {\n' % (indent, countispointer, cmd_info[-1].len)
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600686 indent = self.incIndent(indent)
687 object_dest = '%s[index]' % cmd_info[-1].name
Mark Lobodzinskic3ff7a92018-09-20 11:05:57 -0600688
689 dispobj = params[0].find('type').text
Mark Lobodzinskidcf2bd22019-02-27 16:30:35 -0700690 if is_create_pipelines:
Mark Lobodzinskifd2d0a42019-02-20 16:34:56 -0700691 create_obj_code += '%sif (!pPipelines[index]) continue;\n' % indent
Mark Lobodzinskiadd93232018-10-09 11:49:42 -0600692 create_obj_code += '%sCreateObject(%s, %s, %s, %s);\n' % (indent, params[0].find('name').text, object_dest, self.GetVulkanObjType(cmd_info[-1].type), allocator)
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600693 if object_array == True:
694 indent = self.decIndent(indent)
695 create_obj_code += '%s}\n' % indent
Mark Lobodzinski1b3b6cc2019-07-09 09:19:13 -0600696 indent = self.decIndent(indent)
697 create_obj_code += '%s}\n' % indent
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600698 indent = self.decIndent(indent)
Mark Lobodzinski1b3b6cc2019-07-09 09:19:13 -0600699
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600700 return create_obj_code
701 #
702 # Generate source for destroying a non-dispatchable object
703 def generate_destroy_object_code(self, indent, proto, cmd_info):
Mark Lobodzinskia9e7e9c2018-09-13 13:29:49 -0600704 validate_code = ''
705 record_code = ''
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600706 object_array = False
707 if True in [destroy_txt in proto.text for destroy_txt in ['Destroy', 'Free']]:
708 # Check for special case where multiple handles are returned
709 if cmd_info[-1].len is not None:
710 object_array = True;
711 param = -1
712 else:
713 param = -2
714 compatalloc_vuid_string = '%s-compatalloc' % cmd_info[param].name
715 nullalloc_vuid_string = '%s-nullalloc' % cmd_info[param].name
Dave Houlton57ae22f2018-05-18 16:20:52 -0600716 compatalloc_vuid = self.manual_vuids.get(compatalloc_vuid_string, "kVUIDUndefined")
717 nullalloc_vuid = self.manual_vuids.get(nullalloc_vuid_string, "kVUIDUndefined")
Mike Schuchardtf8690262019-07-11 10:08:33 -0700718 if cmd_info[param].type in self.handle_types:
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600719 if object_array == True:
720 # This API is freeing an array of handles -- add loop control
Mark Lobodzinskia9e7e9c2018-09-13 13:29:49 -0600721 validate_code += 'HEY, NEED TO DESTROY AN ARRAY\n'
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600722 else:
Mark Lobodzinskic3ff7a92018-09-20 11:05:57 -0600723 dispobj = cmd_info[0].type
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600724 # Call Destroy a single time
Mark Lobodzinskiadd93232018-10-09 11:49:42 -0600725 validate_code += '%sskip |= ValidateDestroyObject(%s, %s, %s, pAllocator, %s, %s);\n' % (indent, cmd_info[0].name, cmd_info[param].name, self.GetVulkanObjType(cmd_info[param].type), compatalloc_vuid, nullalloc_vuid)
726 record_code += '%sRecordDestroyObject(%s, %s, %s);\n' % (indent, cmd_info[0].name, cmd_info[param].name, self.GetVulkanObjType(cmd_info[param].type))
Mark Lobodzinskia9e7e9c2018-09-13 13:29:49 -0600727 return object_array, validate_code, record_code
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600728 #
729 # Output validation for a single object (obj_count is NULL) or a counted list of objects
Mark Lobodzinski8c0d3f92018-09-13 10:45:20 -0600730 def outputObjects(self, obj_type, obj_name, obj_count, prefix, index, indent, disp_name, parent_name, null_allowed, top_level):
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600731 pre_call_code = ''
Dave Houlton57ae22f2018-05-18 16:20:52 -0600732 param_suffix = '%s-parameter' % (obj_name)
733 parent_suffix = '%s-parent' % (obj_name)
734 param_vuid = self.GetVuid(parent_name, param_suffix)
735 parent_vuid = self.GetVuid(parent_name, parent_suffix)
736
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600737 # If no parent VUID for this member, look for a commonparent VUID
Dave Houlton57ae22f2018-05-18 16:20:52 -0600738 if parent_vuid == 'kVUIDUndefined':
739 parent_vuid = self.GetVuid(parent_name, 'commonparent')
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600740 if obj_count is not None:
Mark Lobodzinski1b3b6cc2019-07-09 09:19:13 -0600741
742 pre_call_code += '%sif (%s%s) {\n' % (indent, prefix, obj_name)
743 indent = self.incIndent(indent)
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600744 pre_call_code += '%sfor (uint32_t %s = 0; %s < %s; ++%s) {\n' % (indent, index, index, obj_count, index)
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600745 indent = self.incIndent(indent)
Mark Lobodzinskiadd93232018-10-09 11:49:42 -0600746 pre_call_code += '%sskip |= ValidateObject(%s, %s%s[%s], %s, %s, %s, %s);\n' % (indent, disp_name, prefix, obj_name, index, self.GetVulkanObjType(obj_type), null_allowed, param_vuid, parent_vuid)
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600747 indent = self.decIndent(indent)
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600748 pre_call_code += '%s}\n' % indent
Mark Lobodzinski1b3b6cc2019-07-09 09:19:13 -0600749 indent = self.decIndent(indent)
750 pre_call_code += '%s}\n' % indent
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600751 else:
Mark Lobodzinskiadd93232018-10-09 11:49:42 -0600752 pre_call_code += '%sskip |= ValidateObject(%s, %s%s, %s, %s, %s, %s);\n' % (indent, disp_name, prefix, obj_name, self.GetVulkanObjType(obj_type), null_allowed, param_vuid, parent_vuid)
Mark Lobodzinski8c0d3f92018-09-13 10:45:20 -0600753 return pre_call_code
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600754 #
755 # first_level_param indicates if elements are passed directly into the function else they're below a ptr/struct
Mark Lobodzinski8c0d3f92018-09-13 10:45:20 -0600756 def validate_objects(self, members, indent, prefix, array_index, disp_name, parent_name, first_level_param):
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600757 pre_code = ''
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600758 index = 'index%s' % str(array_index)
759 array_index += 1
760 # Process any objects in this structure and recurse for any sub-structs in this struct
761 for member in members:
762 # Handle objects
763 if member.iscreate and first_level_param and member == members[-1]:
764 continue
Mike Schuchardtf8690262019-07-11 10:08:33 -0700765 if member.type in self.handle_types:
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600766 count_name = member.len
767 if (count_name is not None):
768 count_name = '%s%s' % (prefix, member.len)
769 null_allowed = member.isoptional
Mark Lobodzinski8c0d3f92018-09-13 10:45:20 -0600770 tmp_pre = self.outputObjects(member.type, member.name, count_name, prefix, index, indent, disp_name, parent_name, str(null_allowed).lower(), first_level_param)
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600771 pre_code += tmp_pre
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600772 # Handle Structs that contain objects at some level
773 elif member.type in self.struct_member_dict:
774 # Structs at first level will have an object
775 if self.struct_contains_object(member.type) == True:
776 struct_info = self.struct_member_dict[member.type]
Jeff Bolz38b3ce72018-09-19 12:53:38 -0500777 # TODO (jbolz): Can this use paramIsPointer?
Jeff Bolzba74d972018-09-12 15:54:57 -0500778 ispointer = '*' in member.cdecl;
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600779 # Struct Array
780 if member.len is not None:
781 # Update struct prefix
782 new_prefix = '%s%s' % (prefix, member.name)
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600783 pre_code += '%sif (%s%s) {\n' % (indent, prefix, member.name)
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600784 indent = self.incIndent(indent)
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600785 pre_code += '%sfor (uint32_t %s = 0; %s < %s%s; ++%s) {\n' % (indent, index, index, prefix, member.len, index)
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600786 indent = self.incIndent(indent)
787 local_prefix = '%s[%s].' % (new_prefix, index)
788 # Process sub-structs in this struct
Mark Lobodzinski8c0d3f92018-09-13 10:45:20 -0600789 tmp_pre = self.validate_objects(struct_info, indent, local_prefix, array_index, disp_name, member.type, False)
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600790 pre_code += tmp_pre
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600791 indent = self.decIndent(indent)
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600792 pre_code += '%s}\n' % indent
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600793 indent = self.decIndent(indent)
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600794 pre_code += '%s}\n' % indent
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600795 # Single Struct
Jeff Bolzba74d972018-09-12 15:54:57 -0500796 elif ispointer:
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600797 # Update struct prefix
798 new_prefix = '%s%s->' % (prefix, member.name)
799 # Declare safe_VarType for struct
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600800 pre_code += '%sif (%s%s) {\n' % (indent, prefix, member.name)
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600801 indent = self.incIndent(indent)
802 # Process sub-structs in this struct
Mark Lobodzinski8c0d3f92018-09-13 10:45:20 -0600803 tmp_pre = self.validate_objects(struct_info, indent, new_prefix, array_index, disp_name, member.type, False)
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600804 pre_code += tmp_pre
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600805 indent = self.decIndent(indent)
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600806 pre_code += '%s}\n' % indent
Mark Lobodzinski8c0d3f92018-09-13 10:45:20 -0600807 return pre_code
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600808 #
809 # For a particular API, generate the object handling code
810 def generate_wrapping_code(self, cmd):
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600811 indent = ' '
Mark Lobodzinskia9e7e9c2018-09-13 13:29:49 -0600812 pre_call_validate = ''
813 pre_call_record = ''
814 post_call_record = ''
815
816 destroy_array = False
817 validate_destroy_code = ''
818 record_destroy_code = ''
819
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600820 proto = cmd.find('proto/name')
821 params = cmd.findall('param')
822 if proto.text is not None:
John Zulaufbb6e5e42018-08-06 16:00:07 -0600823 cmddata = self.cmd_info_dict[proto.text]
824 cmd_info = cmddata.members
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600825 disp_name = cmd_info[0].name
John Zulaufbb6e5e42018-08-06 16:00:07 -0600826 # Handle object create operations if last parameter is created by this call
827 if cmddata.iscreate:
Mark Lobodzinskia9e7e9c2018-09-13 13:29:49 -0600828 post_call_record += self.generate_create_object_code(indent, proto, params, cmd_info, cmddata.allocator)
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600829 # Handle object destroy operations
John Zulaufbb6e5e42018-08-06 16:00:07 -0600830 if cmddata.isdestroy:
Mark Lobodzinskia9e7e9c2018-09-13 13:29:49 -0600831 (destroy_array, validate_destroy_code, record_destroy_code) = self.generate_destroy_object_code(indent, proto, cmd_info)
Mark Lobodzinski2a51e802018-09-12 16:07:01 -0600832
Mark Lobodzinskia9e7e9c2018-09-13 13:29:49 -0600833 pre_call_record += record_destroy_code
834 pre_call_validate += self.validate_objects(cmd_info, indent, '', 0, disp_name, proto.text, True)
835 pre_call_validate += validate_destroy_code
836
837 return pre_call_validate, pre_call_record, post_call_record
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600838 #
839 # Capture command parameter info needed to create, destroy, and validate objects
Mike Schuchardtf375c7c2017-12-28 11:23:48 -0700840 def genCmd(self, cmdinfo, cmdname, alias):
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600841
842 # Add struct-member type information to command parameter information
Mike Schuchardtf375c7c2017-12-28 11:23:48 -0700843 OutputGenerator.genCmd(self, cmdinfo, cmdname, alias)
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600844 members = cmdinfo.elem.findall('.//param')
845 # Iterate over members once to get length parameters for arrays
846 lens = set()
847 for member in members:
John Zulaufbb6e5e42018-08-06 16:00:07 -0600848 length = self.getLen(member)
849 if length:
850 lens.add(length)
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600851 struct_member_dict = dict(self.structMembers)
John Zulaufbb6e5e42018-08-06 16:00:07 -0600852
853 # Set command invariant information needed at a per member level in validate...
854 is_create_command = any(filter(lambda pat: pat in cmdname, ('Create', 'Allocate', 'Enumerate', 'RegisterDeviceEvent', 'RegisterDisplayEvent')))
855 last_member_is_pointer = len(members) and self.paramIsPointer(members[-1])
856 iscreate = is_create_command or ('vkGet' in cmdname and last_member_is_pointer)
857 isdestroy = any([destroy_txt in cmdname for destroy_txt in ['Destroy', 'Free']])
858
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600859 # Generate member info
860 membersInfo = []
861 constains_extension_structs = False
John Zulaufbb6e5e42018-08-06 16:00:07 -0600862 allocator = 'nullptr'
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600863 for member in members:
864 # Get type and name of member
865 info = self.getTypeNameTuple(member)
866 type = info[0]
867 name = info[1]
868 cdecl = self.makeCParamDecl(member, 0)
869 # Check for parameter name in lens set
870 iscount = True if name in lens else False
John Zulaufbb6e5e42018-08-06 16:00:07 -0600871 length = self.getLen(member)
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600872 isconst = True if 'const' in cdecl else False
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600873 # Mark param as local if it is an array of objects
874 islocal = False;
Mike Schuchardtf8690262019-07-11 10:08:33 -0700875 if type in self.handle_types:
John Zulaufbb6e5e42018-08-06 16:00:07 -0600876 if (length is not None) and (isconst == True):
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600877 islocal = True
878 # Or if it's a struct that contains an object
879 elif type in struct_member_dict:
880 if self.struct_contains_object(type) == True:
881 islocal = True
John Zulaufbb6e5e42018-08-06 16:00:07 -0600882 if type == 'VkAllocationCallbacks':
883 allocator = name
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600884 extstructs = member.attrib.get('validextensionstructs') if name == 'pNext' else None
885 membersInfo.append(self.CommandParam(type=type,
886 name=name,
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600887 isconst=isconst,
888 isoptional=self.paramIsOptional(member),
889 iscount=iscount,
John Zulaufbb6e5e42018-08-06 16:00:07 -0600890 len=length,
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600891 extstructs=extstructs,
892 cdecl=cdecl,
893 islocal=islocal,
John Zulaufbb6e5e42018-08-06 16:00:07 -0600894 iscreate=iscreate))
895
896 self.cmd_list.append(cmdname)
897 self.cmd_info_dict[cmdname] =self.CmdInfoData(name=cmdname, cmdinfo=cmdinfo, members=membersInfo, iscreate=iscreate, isdestroy=isdestroy, allocator=allocator, extra_protect=self.featureExtraProtect, alias=alias)
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600898 #
899 # Create code Create, Destroy, and validate Vulkan objects
900 def WrapCommands(self):
John Zulaufbb6e5e42018-08-06 16:00:07 -0600901 for cmdname in self.cmd_list:
902 cmddata = self.cmd_info_dict[cmdname]
903 cmdinfo = cmddata.cmdinfo
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600904 if cmdname in self.interface_functions:
905 continue
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600906 manual = False
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600907 if cmdname in self.no_autogen_list:
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600908 manual = True
909
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600910 # Generate object handling code
Mark Lobodzinskia9e7e9c2018-09-13 13:29:49 -0600911 (pre_call_validate, pre_call_record, post_call_record) = self.generate_wrapping_code(cmdinfo.elem)
912
John Zulaufbb6e5e42018-08-06 16:00:07 -0600913 feature_extra_protect = cmddata.extra_protect
Michał Janiszewski3c3ce9e2018-10-30 23:25:21 +0100914 if (feature_extra_protect is not None):
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600915 self.appendSection('command', '')
916 self.appendSection('command', '#ifdef '+ feature_extra_protect)
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600917 self.prototypes += [ '#ifdef %s' % feature_extra_protect ]
Mark Lobodzinskia9e7e9c2018-09-13 13:29:49 -0600918
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600919 # Add intercept to procmap
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600920 self.prototypes += [ ' {"%s", (void*)%s},' % (cmdname,cmdname[2:]) ]
Mark Lobodzinskia9e7e9c2018-09-13 13:29:49 -0600921
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600922 decls = self.makeCDecls(cmdinfo.elem)
Mark Lobodzinskia9e7e9c2018-09-13 13:29:49 -0600923
924 # Gather the parameter items
925 params = cmdinfo.elem.findall('param/name')
926 # Pull out the text for each of the parameters, separate them by commas in a list
927 paramstext = ', '.join([str(param.text) for param in params])
928 # Generate the API call template
929 fcn_call = cmdinfo.elem.attrib.get('name').replace('vk', 'TOKEN', 1) + '(' + paramstext + ');'
930
931 func_decl_template = decls[0][:-1].split('VKAPI_CALL ')
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600932 func_decl_template = func_decl_template[1]
Mark Lobodzinskia9e7e9c2018-09-13 13:29:49 -0600933
Mark Lobodzinskicd05c1e2019-01-17 15:33:46 -0700934 result_type = cmdinfo.elem.find('proto/type')
935
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600936 if 'object_tracker.h' in self.genOpts.filename:
937 # Output PreCallValidateAPI prototype if necessary
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600938 if pre_call_validate:
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600939 pre_cv_func_decl = 'bool PreCallValidate' + func_decl_template + ';'
940 self.appendSection('command', pre_cv_func_decl)
941
942 # Output PreCallRecordAPI prototype if necessary
943 if pre_call_record:
944 pre_cr_func_decl = 'void PreCallRecord' + func_decl_template + ';'
945 self.appendSection('command', pre_cr_func_decl)
946
947 # Output PosCallRecordAPI prototype if necessary
948 if post_call_record:
949 post_cr_func_decl = 'void PostCallRecord' + func_decl_template + ';'
Mark Lobodzinskicd05c1e2019-01-17 15:33:46 -0700950 if result_type.text == 'VkResult':
951 post_cr_func_decl = post_cr_func_decl.replace(')', ',\n VkResult result)')
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600952 self.appendSection('command', post_cr_func_decl)
953
954 if 'object_tracker.cpp' in self.genOpts.filename:
955 # Output PreCallValidateAPI function if necessary
956 if pre_call_validate and not manual:
957 pre_cv_func_decl = 'bool ObjectLifetimes::PreCallValidate' + func_decl_template + ' {'
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600958 self.appendSection('command', '')
959 self.appendSection('command', pre_cv_func_decl)
960 self.appendSection('command', ' bool skip = false;')
961 self.appendSection('command', pre_call_validate)
962 self.appendSection('command', ' return skip;')
963 self.appendSection('command', '}')
964
965 # Output PreCallRecordAPI function if necessary
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600966 if pre_call_record and not manual:
967 pre_cr_func_decl = 'void ObjectLifetimes::PreCallRecord' + func_decl_template + ' {'
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600968 self.appendSection('command', '')
969 self.appendSection('command', pre_cr_func_decl)
970 self.appendSection('command', pre_call_record)
971 self.appendSection('command', '}')
972
973 # Output PosCallRecordAPI function if necessary
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600974 if post_call_record and not manual:
975 post_cr_func_decl = 'void ObjectLifetimes::PostCallRecord' + func_decl_template + ' {'
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600976 self.appendSection('command', '')
Mark Lobodzinskicd05c1e2019-01-17 15:33:46 -0700977
978 if result_type.text == 'VkResult':
979 post_cr_func_decl = post_cr_func_decl.replace(')', ',\n VkResult result)')
Mark Lobodzinskifd2d0a42019-02-20 16:34:56 -0700980 # The two createpipelines APIs may create on failure -- skip the success result check
981 if 'CreateGraphicsPipelines' not in cmdname and 'CreateComputePipelines' not in cmdname and 'CreateRayTracingPipelines' not in cmdname:
982 post_cr_func_decl = post_cr_func_decl.replace('{', '{\n if (result != VK_SUCCESS) return;')
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600983 self.appendSection('command', post_cr_func_decl)
Mark Lobodzinskicd05c1e2019-01-17 15:33:46 -0700984
985
Mark Lobodzinski63902f02018-09-21 10:36:44 -0600986 self.appendSection('command', post_call_record)
987 self.appendSection('command', '}')
988
Michał Janiszewski3c3ce9e2018-10-30 23:25:21 +0100989 if (feature_extra_protect is not None):
Mark Lobodzinskid1461482017-07-18 13:56:09 -0600990 self.appendSection('command', '#endif // '+ feature_extra_protect)
Mark Lobodzinski0c668462018-09-27 10:13:19 -0600991 self.prototypes += [ '#endif' ]