• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python3 -i
2#
3# Copyright (c) 2015-2017 The Khronos Group Inc.
4# Copyright (c) 2015-2017 Valve Corporation
5# Copyright (c) 2015-2017 LunarG, Inc.
6# Copyright (c) 2015-2017 Google Inc.
7#
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>
21# Author: Tobin Ehlis <tobine@google.com>
22# Author: John Zulauf <jzulauf@lunarg.com>
23
24import os,re,sys
25import xml.etree.ElementTree as etree
26from generator import *
27from collections import namedtuple
28from common_codegen import *
29
30#
31# HelperFileOutputGeneratorOptions - subclass of GeneratorOptions.
32class HelperFileOutputGeneratorOptions(GeneratorOptions):
33    def __init__(self,
34                 conventions = None,
35                 filename = None,
36                 directory = '.',
37                 genpath = None,
38                 apiname = None,
39                 profile = None,
40                 versions = '.*',
41                 emitversions = '.*',
42                 defaultExtensions = None,
43                 addExtensions = None,
44                 removeExtensions = None,
45                 emitExtensions = None,
46                 sortProcedure = regSortFeatures,
47                 prefixText = "",
48                 genFuncPointers = True,
49                 protectFile = True,
50                 protectFeature = True,
51                 apicall = '',
52                 apientry = '',
53                 apientryp = '',
54                 alignFuncParam = 0,
55                 library_name = '',
56                 expandEnumerants = True,
57                 helper_file_type = ''):
58        GeneratorOptions.__init__(self,
59                conventions = conventions,
60                filename = filename,
61                directory = directory,
62                genpath = genpath,
63                apiname = apiname,
64                profile = profile,
65                versions = versions,
66                emitversions = emitversions,
67                defaultExtensions = defaultExtensions,
68                addExtensions = addExtensions,
69                removeExtensions = removeExtensions,
70                emitExtensions = emitExtensions,
71                sortProcedure = sortProcedure)
72        self.prefixText       = prefixText
73        self.genFuncPointers  = genFuncPointers
74        self.protectFile      = protectFile
75        self.protectFeature   = protectFeature
76        self.apicall          = apicall
77        self.apientry         = apientry
78        self.apientryp        = apientryp
79        self.alignFuncParam   = alignFuncParam
80        self.library_name     = library_name
81        self.helper_file_type = helper_file_type
82#
83# HelperFileOutputGenerator - subclass of OutputGenerator. Outputs Vulkan helper files
84class HelperFileOutputGenerator(OutputGenerator):
85    """Generate helper file based on XML element attributes"""
86    def __init__(self,
87                 errFile = sys.stderr,
88                 warnFile = sys.stderr,
89                 diagFile = sys.stdout):
90        OutputGenerator.__init__(self, errFile, warnFile, diagFile)
91        # Internal state - accumulators for different inner block text
92        self.enum_output = ''                             # string built up of enum string routines
93        # Internal state - accumulators for different inner block text
94        self.structNames = []                             # List of Vulkan struct typenames
95        self.structTypes = dict()                         # Map of Vulkan struct typename to required VkStructureType
96        self.structMembers = []                           # List of StructMemberData records for all Vulkan structs
97        self.object_types = []                            # List of all handle types
98        self.object_type_aliases = []                     # Aliases to handles types (for handles that were extensions)
99        self.debug_report_object_types = []               # Handy copy of debug_report_object_type enum data
100        self.core_object_types = []                       # Handy copy of core_object_type enum data
101        self.device_extension_info = dict()               # Dict of device extension name defines and ifdef values
102        self.instance_extension_info = dict()             # Dict of instance extension name defines and ifdef values
103
104        # Named tuples to store struct and command data
105        self.StructType = namedtuple('StructType', ['name', 'value'])
106        self.CommandParam = namedtuple('CommandParam', ['type', 'name', 'ispointer', 'isstaticarray', 'isconst', 'iscount', 'len', 'extstructs', 'cdecl'])
107        self.StructMemberData = namedtuple('StructMemberData', ['name', 'members', 'ifdef_protect'])
108
109        self.custom_construct_params = {
110            # safe_VkGraphicsPipelineCreateInfo needs to know if subpass has color and\or depth\stencil attachments to use its pointers
111            'VkGraphicsPipelineCreateInfo' :
112                ', const bool uses_color_attachment, const bool uses_depthstencil_attachment',
113            # safe_VkPipelineViewportStateCreateInfo needs to know if viewport and scissor is dynamic to use its pointers
114            'VkPipelineViewportStateCreateInfo' :
115                ', const bool is_dynamic_viewports, const bool is_dynamic_scissors',
116        }
117    #
118    # Called once at the beginning of each run
119    def beginFile(self, genOpts):
120        OutputGenerator.beginFile(self, genOpts)
121        # User-supplied prefix text, if any (list of strings)
122        self.helper_file_type = genOpts.helper_file_type
123        self.library_name = genOpts.library_name
124        # File Comment
125        file_comment = '// *** THIS FILE IS GENERATED - DO NOT EDIT ***\n'
126        file_comment += '// See helper_file_generator.py for modifications\n'
127        write(file_comment, file=self.outFile)
128        # Copyright Notice
129        copyright = ''
130        copyright += '\n'
131        copyright += '/***************************************************************************\n'
132        copyright += ' *\n'
133        copyright += ' * Copyright (c) 2015-2017 The Khronos Group Inc.\n'
134        copyright += ' * Copyright (c) 2015-2017 Valve Corporation\n'
135        copyright += ' * Copyright (c) 2015-2017 LunarG, Inc.\n'
136        copyright += ' * Copyright (c) 2015-2017 Google Inc.\n'
137        copyright += ' *\n'
138        copyright += ' * Licensed under the Apache License, Version 2.0 (the "License");\n'
139        copyright += ' * you may not use this file except in compliance with the License.\n'
140        copyright += ' * You may obtain a copy of the License at\n'
141        copyright += ' *\n'
142        copyright += ' *     http://www.apache.org/licenses/LICENSE-2.0\n'
143        copyright += ' *\n'
144        copyright += ' * Unless required by applicable law or agreed to in writing, software\n'
145        copyright += ' * distributed under the License is distributed on an "AS IS" BASIS,\n'
146        copyright += ' * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n'
147        copyright += ' * See the License for the specific language governing permissions and\n'
148        copyright += ' * limitations under the License.\n'
149        copyright += ' *\n'
150        copyright += ' * Author: Mark Lobodzinski <mark@lunarg.com>\n'
151        copyright += ' * Author: Courtney Goeltzenleuchter <courtneygo@google.com>\n'
152        copyright += ' * Author: Tobin Ehlis <tobine@google.com>\n'
153        copyright += ' * Author: Chris Forbes <chrisforbes@google.com>\n'
154        copyright += ' * Author: John Zulauf<jzulauf@lunarg.com>\n'
155        copyright += ' *\n'
156        copyright += ' ****************************************************************************/\n'
157        write(copyright, file=self.outFile)
158    #
159    # Write generated file content to output file
160    def endFile(self):
161        dest_file = ''
162        dest_file += self.OutputDestFile()
163        # Remove blank lines at EOF
164        if dest_file.endswith('\n'):
165            dest_file = dest_file[:-1]
166        write(dest_file, file=self.outFile);
167        # Finish processing in superclass
168        OutputGenerator.endFile(self)
169    #
170    # Override parent class to be notified of the beginning of an extension
171    def beginFeature(self, interface, emit):
172        # Start processing in superclass
173        OutputGenerator.beginFeature(self, interface, emit)
174        self.featureExtraProtect = GetFeatureProtect(interface)
175
176        if interface.tag != 'extension':
177            return
178        name = self.featureName
179        nameElem = interface[0][1]
180        name_define = nameElem.get('name')
181        if 'EXTENSION_NAME' not in name_define:
182            print("Error in vk.xml file -- extension name is not available")
183        requires = interface.get('requires')
184        if requires is not None:
185            required_extensions = requires.split(',')
186        else:
187            required_extensions = list()
188        info = { 'define': name_define, 'ifdef':self.featureExtraProtect, 'reqs':required_extensions }
189        if interface.get('type') == 'instance':
190            self.instance_extension_info[name] = info
191        else:
192            self.device_extension_info[name] = info
193
194    #
195    # Override parent class to be notified of the end of an extension
196    def endFeature(self):
197        # Finish processing in superclass
198        OutputGenerator.endFeature(self)
199    #
200    # Grab group (e.g. C "enum" type) info to output for enum-string conversion helper
201    def genGroup(self, groupinfo, groupName, alias):
202        OutputGenerator.genGroup(self, groupinfo, groupName, alias)
203        groupElem = groupinfo.elem
204        # For enum_string_header
205        if self.helper_file_type == 'enum_string_header':
206            value_set = set()
207            for elem in groupElem.findall('enum'):
208                if elem.get('supported') != 'disabled' and elem.get('alias') is None:
209                    value_set.add(elem.get('name'))
210            self.enum_output += self.GenerateEnumStringConversion(groupName, value_set)
211        elif self.helper_file_type == 'object_types_header':
212            if groupName == 'VkDebugReportObjectTypeEXT':
213                for elem in groupElem.findall('enum'):
214                    if elem.get('supported') != 'disabled':
215                        item_name = elem.get('name')
216                        self.debug_report_object_types.append(item_name)
217            elif groupName == 'VkObjectType':
218                for elem in groupElem.findall('enum'):
219                    if elem.get('supported') != 'disabled':
220                        item_name = elem.get('name')
221                        self.core_object_types.append(item_name)
222
223    #
224    # Called for each type -- if the type is a struct/union, grab the metadata
225    def genType(self, typeinfo, name, alias):
226        OutputGenerator.genType(self, typeinfo, name, alias)
227        typeElem = typeinfo.elem
228        # If the type is a struct type, traverse the imbedded <member> tags generating a structure.
229        # Otherwise, emit the tag text.
230        category = typeElem.get('category')
231        if category == 'handle':
232            if alias:
233                self.object_type_aliases.append((name,alias))
234            else:
235                self.object_types.append(name)
236        elif (category == 'struct' or category == 'union'):
237            self.structNames.append(name)
238            self.genStruct(typeinfo, name, alias)
239    #
240    # Generate a VkStructureType based on a structure typename
241    def genVkStructureType(self, typename):
242        # Add underscore between lowercase then uppercase
243        value = re.sub('([a-z0-9])([A-Z])', r'\1_\2', typename)
244        # Change to uppercase
245        value = value.upper()
246        # Add STRUCTURE_TYPE_
247        return re.sub('VK_', 'VK_STRUCTURE_TYPE_', value)
248    #
249    # Check if the parameter passed in is a pointer
250    def paramIsPointer(self, param):
251        ispointer = False
252        for elem in param:
253            if ((elem.tag != 'type') and (elem.tail is not None)) and '*' in elem.tail:
254                ispointer = True
255        return ispointer
256    #
257    # Check if the parameter passed in is a static array
258    def paramIsStaticArray(self, param):
259        isstaticarray = 0
260        paramname = param.find('name')
261        if (paramname.tail is not None) and ('[' in paramname.tail):
262            isstaticarray = paramname.tail.count('[')
263        return isstaticarray
264    #
265    # Retrieve the type and name for a parameter
266    def getTypeNameTuple(self, param):
267        type = ''
268        name = ''
269        for elem in param:
270            if elem.tag == 'type':
271                type = noneStr(elem.text)
272            elif elem.tag == 'name':
273                name = noneStr(elem.text)
274        return (type, name)
275    #
276    # Retrieve the value of the len tag
277    def getLen(self, param):
278        result = None
279        len = param.attrib.get('len')
280        if len and len != 'null-terminated':
281            # For string arrays, 'len' can look like 'count,null-terminated', indicating that we
282            # have a null terminated array of strings.  We strip the null-terminated from the
283            # 'len' field and only return the parameter specifying the string count
284            if 'null-terminated' in len:
285                result = len.split(',')[0]
286            else:
287                result = len
288            if 'altlen' in param.attrib:
289                # Elements with latexmath 'len' also contain a C equivalent 'altlen' attribute
290                # Use indexing operator instead of get() so we fail if the attribute is missing
291                result = param.attrib['altlen']
292            # Spec has now notation for len attributes, using :: instead of platform specific pointer symbol
293            result = str(result).replace('::', '->')
294        return result
295    #
296    # Check if a structure is or contains a dispatchable (dispatchable = True) or
297    # non-dispatchable (dispatchable = False) handle
298    def TypeContainsObjectHandle(self, handle_type, dispatchable):
299        if dispatchable:
300            type_key = 'VK_DEFINE_HANDLE'
301        else:
302            type_key = 'VK_DEFINE_NON_DISPATCHABLE_HANDLE'
303        handle = self.registry.tree.find("types/type/[name='" + handle_type + "'][@category='handle']")
304        if handle is not None and handle.find('type').text == type_key:
305            return True
306        # if handle_type is a struct, search its members
307        if handle_type in self.structNames:
308            member_index = next((i for i, v in enumerate(self.structMembers) if v[0] == handle_type), None)
309            if member_index is not None:
310                for item in self.structMembers[member_index].members:
311                    handle = self.registry.tree.find("types/type/[name='" + item.type + "'][@category='handle']")
312                    if handle is not None and handle.find('type').text == type_key:
313                        return True
314        return False
315    #
316    # Generate local ready-access data describing Vulkan structures and unions from the XML metadata
317    def genStruct(self, typeinfo, typeName, alias):
318        OutputGenerator.genStruct(self, typeinfo, typeName, alias)
319        members = typeinfo.elem.findall('.//member')
320        # Iterate over members once to get length parameters for arrays
321        lens = set()
322        for member in members:
323            len = self.getLen(member)
324            if len:
325                lens.add(len)
326        # Generate member info
327        membersInfo = []
328        for member in members:
329            # Get the member's type and name
330            info = self.getTypeNameTuple(member)
331            type = info[0]
332            name = info[1]
333            cdecl = self.makeCParamDecl(member, 1)
334            # Process VkStructureType
335            if type == 'VkStructureType':
336                # Extract the required struct type value from the comments
337                # embedded in the original text defining the 'typeinfo' element
338                rawXml = etree.tostring(typeinfo.elem).decode('ascii')
339                result = re.search(r'VK_STRUCTURE_TYPE_\w+', rawXml)
340                if result:
341                    value = result.group(0)
342                else:
343                    value = self.genVkStructureType(typeName)
344                # Store the required type value
345                self.structTypes[typeName] = self.StructType(name=name, value=value)
346            # Store pointer/array/string info
347            isstaticarray = self.paramIsStaticArray(member)
348            membersInfo.append(self.CommandParam(type=type,
349                                                 name=name,
350                                                 ispointer=self.paramIsPointer(member),
351                                                 isstaticarray=isstaticarray,
352                                                 isconst=True if 'const' in cdecl else False,
353                                                 iscount=True if name in lens else False,
354                                                 len=self.getLen(member),
355                                                 extstructs=self.registry.validextensionstructs[typeName] if name == 'pNext' else None,
356                                                 cdecl=cdecl))
357        self.structMembers.append(self.StructMemberData(name=typeName, members=membersInfo, ifdef_protect=self.featureExtraProtect))
358    #
359    # Enum_string_header: Create a routine to convert an enumerated value into a string
360    def GenerateEnumStringConversion(self, groupName, value_list):
361        outstring = '\n'
362        outstring += 'static inline const char* string_%s(%s input_value)\n' % (groupName, groupName)
363        outstring += '{\n'
364        outstring += '    switch ((%s)input_value)\n' % groupName
365        outstring += '    {\n'
366        for item in value_list:
367            outstring += '        case %s:\n' % item
368            outstring += '            return "%s";\n' % item
369        outstring += '        default:\n'
370        outstring += '            return "Unhandled %s";\n' % groupName
371        outstring += '    }\n'
372        outstring += '}\n'
373        return outstring
374    #
375    # Combine object types helper header file preamble with body text and return
376    def GenerateObjectTypesHelperHeader(self):
377        object_types_helper_header = '\n'
378        object_types_helper_header += '#pragma once\n'
379        object_types_helper_header += '\n'
380        object_types_helper_header += '#include <vulkan/vulkan.h>\n\n'
381        object_types_helper_header += self.GenerateObjectTypesHeader()
382        return object_types_helper_header
383    #
384    # Object types header: create object enum type header file
385    def GenerateObjectTypesHeader(self):
386        object_types_header = ''
387        object_types_header += '// Object Type enum for validation layer internal object handling\n'
388        object_types_header += 'typedef enum VulkanObjectType {\n'
389        object_types_header += '    kVulkanObjectTypeUnknown = 0,\n'
390        enum_num = 1
391        type_list = [];
392        enum_entry_map = {}
393
394        # Output enum definition as each handle is processed, saving the names to use for the conversion routine
395        for item in self.object_types:
396            fixup_name = item[2:]
397            enum_entry = 'kVulkanObjectType%s' % fixup_name
398            enum_entry_map[item] = enum_entry
399            object_types_header += '    ' + enum_entry
400            object_types_header += ' = %d,\n' % enum_num
401            enum_num += 1
402            type_list.append(enum_entry)
403        object_types_header += '    kVulkanObjectTypeMax = %d,\n' % enum_num
404        object_types_header += '    // Aliases for backwards compatibilty of "promoted" types\n'
405        for (name, alias) in self.object_type_aliases:
406            fixup_name = name[2:]
407            object_types_header += '    kVulkanObjectType{} = {},\n'.format(fixup_name, enum_entry_map[alias])
408        object_types_header += '} VulkanObjectType;\n\n'
409
410        # Output name string helper
411        object_types_header += '// Array of object name strings for OBJECT_TYPE enum conversion\n'
412        object_types_header += 'static const char * const object_string[kVulkanObjectTypeMax] = {\n'
413        object_types_header += '    "Unknown",\n'
414        for item in self.object_types:
415            fixup_name = item[2:]
416            object_types_header += '    "%s",\n' % fixup_name
417        object_types_header += '};\n'
418
419        # Key creation helper for map comprehensions that convert between k<Name> and VK<Name> symbols
420        def to_key(regex, raw_key): return re.search(regex, raw_key).group(1).lower().replace("_","")
421
422        # Output a conversion routine from the layer object definitions to the debug report definitions
423        # As the VK_DEBUG_REPORT types are not being updated, specify UNKNOWN for unmatched types
424        object_types_header += '\n'
425        object_types_header += '// Helper array to get Vulkan VK_EXT_debug_report object type enum from the internal layers version\n'
426        object_types_header += 'const VkDebugReportObjectTypeEXT get_debug_report_enum[] = {\n'
427        object_types_header += '    VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, // kVulkanObjectTypeUnknown\n'
428
429        dbg_re = '^VK_DEBUG_REPORT_OBJECT_TYPE_(.*)_EXT$'
430        dbg_map = {to_key(dbg_re, dbg) : dbg for dbg in self.debug_report_object_types}
431        dbg_default = 'VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT'
432        for object_type in type_list:
433            vk_object_type = dbg_map.get(object_type.replace("kVulkanObjectType", "").lower(), dbg_default)
434            object_types_header += '    %s,   // %s\n' % (vk_object_type, object_type)
435        object_types_header += '};\n'
436
437        # Output a conversion routine from the layer object definitions to the core object type definitions
438        # This will intentionally *fail* for unmatched types as the VK_OBJECT_TYPE list should match the kVulkanObjectType list
439        object_types_header += '\n'
440        object_types_header += '// Helper array to get Official Vulkan VkObjectType enum from the internal layers version\n'
441        object_types_header += 'const VkObjectType get_object_type_enum[] = {\n'
442        object_types_header += '    VK_OBJECT_TYPE_UNKNOWN, // kVulkanObjectTypeUnknown\n'
443
444        vko_re = '^VK_OBJECT_TYPE_(.*)'
445        vko_map = {to_key(vko_re, vko) : vko for vko in self.core_object_types}
446        for object_type in type_list:
447            vk_object_type = vko_map[object_type.replace("kVulkanObjectType", "").lower()]
448            object_types_header += '    %s,   // %s\n' % (vk_object_type, object_type)
449        object_types_header += '};\n'
450
451        # Create a function to convert from VkDebugReportObjectTypeEXT to VkObjectType
452        object_types_header += '\n'
453        object_types_header += '// Helper function to convert from VkDebugReportObjectTypeEXT to VkObjectType\n'
454        object_types_header += 'static inline VkObjectType convertDebugReportObjectToCoreObject(VkDebugReportObjectTypeEXT debug_report_obj){\n'
455        object_types_header += '    if (debug_report_obj == VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT) {\n'
456        object_types_header += '        return VK_OBJECT_TYPE_UNKNOWN;\n'
457        for core_object_type in self.core_object_types:
458            core_target_type = core_object_type.replace("VK_OBJECT_TYPE_", "").lower()
459            core_target_type = core_target_type.replace("_", "")
460            for dr_object_type in self.debug_report_object_types:
461                dr_target_type = dr_object_type.replace("VK_DEBUG_REPORT_OBJECT_TYPE_", "").lower()
462                dr_target_type = dr_target_type[:-4]
463                dr_target_type = dr_target_type.replace("_", "")
464                if core_target_type == dr_target_type:
465                    object_types_header += '    } else if (debug_report_obj == %s) {\n' % dr_object_type
466                    object_types_header += '        return %s;\n' % core_object_type
467                    break
468        object_types_header += '    }\n'
469        object_types_header += '    return VK_OBJECT_TYPE_UNKNOWN;\n'
470        object_types_header += '}\n'
471
472        # Create a function to convert from VkObjectType to VkDebugReportObjectTypeEXT
473        object_types_header += '\n'
474        object_types_header += '// Helper function to convert from VkDebugReportObjectTypeEXT to VkObjectType\n'
475        object_types_header += 'static inline VkDebugReportObjectTypeEXT convertCoreObjectToDebugReportObject(VkObjectType core_report_obj){\n'
476        object_types_header += '    if (core_report_obj == VK_OBJECT_TYPE_UNKNOWN) {\n'
477        object_types_header += '        return VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT;\n'
478        for core_object_type in self.core_object_types:
479            core_target_type = core_object_type.replace("VK_OBJECT_TYPE_", "").lower()
480            core_target_type = core_target_type.replace("_", "")
481            for dr_object_type in self.debug_report_object_types:
482                dr_target_type = dr_object_type.replace("VK_DEBUG_REPORT_OBJECT_TYPE_", "").lower()
483                dr_target_type = dr_target_type[:-4]
484                dr_target_type = dr_target_type.replace("_", "")
485                if core_target_type == dr_target_type:
486                    object_types_header += '    } else if (core_report_obj == %s) {\n' % core_object_type
487                    object_types_header += '        return %s;\n' % dr_object_type
488                    break
489        object_types_header += '    }\n'
490        object_types_header += '    return VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT;\n'
491        object_types_header += '}\n'
492        return object_types_header
493
494    #
495    # Create a helper file and return it as a string
496    def OutputDestFile(self):
497        if self.helper_file_type == 'object_types_header':
498            return self.GenerateObjectTypesHelperHeader()
499        else:
500            return 'Bad Helper File Generator Option %s' % self.helper_file_type
501