import copy
import re
import sys
import xml.etree.ElementTree as etree
from collections import defaultdict, deque, namedtuple
from generator import GeneratorOptions, OutputGenerator, noneStr, write
from apiconventions import APIConventions
def apiNameMatch(str, supported):
if str is not None:
return supported is None or str in supported.split(',')
return False
def matchAPIProfile(api, profile, elem):
elem_api = elem.get('api')
if elem_api:
if api is None:
raise UserWarning("No API requested, but 'api' attribute is present with value '"
+ elem_api + "'")
elif api != elem_api:
return False
elem_profile = elem.get('profile')
if elem_profile:
if profile is None:
raise UserWarning("No profile requested, but 'profile' attribute is present with value '"
+ elem_profile + "'")
elif profile != elem_profile:
return False
return True
def mergeAPIs(tree, fromApiNames, toApiName):
stack = deque()
stack.append(tree)
while len(stack) > 0:
parent = stack.pop()
for child in parent.findall('*'):
if child.tag == 'remove':
parent.remove(child)
else:
stack.append(child)
supportedList = child.get('supported')
if supportedList:
supportedList = supportedList.split(',')
for apiName in [toApiName] + fromApiNames:
if apiName in supportedList:
child.set('supported', toApiName)
if child.get('api'):
definitionName = None
definitionVariants = []
if child.tag in ['type']:
if child.get('name') is not None:
definitionName = child.get('name')
definitionVariants = parent.findall(f"{child.tag}[@name='{definitionName}']")
else:
definitionName = child.find('name').text
definitionVariants = parent.findall(f"{child.tag}/name[.='{definitionName}']/..")
elif child.tag in ['member', 'param']:
definitionName = child.find('name').text
definitionVariants = parent.findall(f"{child.tag}/name[.='{definitionName}']/..")
elif child.tag in ['enum', 'feature']:
definitionName = child.get('name')
definitionVariants = parent.findall(f"{child.tag}[@name='{definitionName}']")
elif child.tag in ['require']:
definitionName = child.get('feature')
definitionVariants = parent.findall(f"{child.tag}[@feature='{definitionName}']")
elif child.tag in ['command']:
definitionName = child.find('proto/name').text
definitionVariants = parent.findall(f"{child.tag}/proto/name[.='{definitionName}']/../..")
if definitionName:
bestMatchApi = None
requires = None
for apiName in [toApiName] + fromApiNames:
for variant in definitionVariants:
if variant.get('requires') and variant.get('api') == apiName:
requires = variant.get('requires')
if apiName in variant.get('api').split(',') and bestMatchApi is None:
bestMatchApi = variant.get('api')
if bestMatchApi:
for variant in definitionVariants:
if variant.get('api') != bestMatchApi:
parent.remove(variant)
else:
if requires is not None and variant.get('requires') is None:
variant.set('requires', requires)
variant.set('api', toApiName)
def stripNonmatchingAPIs(tree, apiName, actuallyDelete = True):
stack = deque()
stack.append(tree)
while len(stack) > 0:
parent = stack.pop()
for child in parent.findall('*'):
api = child.get('api')
if apiNameMatch(apiName, api):
stack.append(child)
elif not apiNameMatch(apiName, api):
if actuallyDelete:
parent.remove(child)
class BaseInfo:
def __init__(self, elem):
self.required = False
self.declared = False
"has this feature been defined already?"
self.elem = elem
"etree Element for this feature"
def resetState(self):
self.required = False
self.declared = False
def compareKeys(self, info, key, required = False):
if required and key not in self.elem.keys():
return False
return self.elem.get(key) == info.elem.get(key)
def compareElem(self, info, infoName):
if infoName == 'enum':
if self.compareKeys(info, 'extends'):
if (self.compareKeys(info, 'value', required = True) or
self.compareKeys(info, 'bitpos', required = True)):
return True
elif (self.compareKeys(info, 'extnumber') and
self.compareKeys(info, 'offset') and
self.compareKeys(info, 'dir')):
return True
elif (self.compareKeys(info, 'alias')):
return True
else:
return False
else:
return False
else:
return False
class TypeInfo(BaseInfo):
def __init__(self, elem):
BaseInfo.__init__(self, elem)
self.additionalValidity = []
self.removedValidity = []
def getMembers(self):
return self.elem.findall('member')
def resetState(self):
BaseInfo.resetState(self)
self.additionalValidity = []
self.removedValidity = []
class GroupInfo(BaseInfo):
def __init__(self, elem):
BaseInfo.__init__(self, elem)
class EnumInfo(BaseInfo):
def __init__(self, elem):
BaseInfo.__init__(self, elem)
self.type = elem.get('type')
if self.type is None:
self.type = ''
class CmdInfo(BaseInfo):
def __init__(self, elem):
BaseInfo.__init__(self, elem)
self.additionalValidity = []
self.removedValidity = []
def getParams(self):
return self.elem.findall('param')
def resetState(self):
BaseInfo.resetState(self)
self.additionalValidity = []
self.removedValidity = []
class FeatureInfo(BaseInfo):
def __init__(self, elem):
BaseInfo.__init__(self, elem)
self.name = elem.get('name')
"feature name string (e.g. 'VK_KHR_surface')"
self.emit = False
"has this feature been defined already?"
self.sortorder = int(elem.get('sortorder', 0))
if elem.tag == 'feature':
self.category = 'VERSION'
self.version = elem.get('name')
self.versionNumber = elem.get('number')
self.number = 0
self.supported = None
else:
self.category = self.name.split('_', 2)[1]
self.version = "0"
self.versionNumber = "0"
self.number = int(elem.get('number','0'))
self.supported = elem.get('supported', 'disabled')
class SpirvInfo(BaseInfo):
def __init__(self, elem):
BaseInfo.__init__(self, elem)
class FormatInfo(BaseInfo):
def __init__(self, elem, condition):
BaseInfo.__init__(self, elem)
self.condition = condition
class SyncStageInfo(BaseInfo):
def __init__(self, elem, condition):
BaseInfo.__init__(self, elem)
self.condition = condition
class SyncAccessInfo(BaseInfo):
def __init__(self, elem, condition):
BaseInfo.__init__(self, elem)
self.condition = condition
class SyncPipelineInfo(BaseInfo):
def __init__(self, elem):
BaseInfo.__init__(self, elem)
class Registry:
def __init__(self, gen=None, genOpts=None):
if gen is None:
self.gen = OutputGenerator()
else:
self.gen = gen
"Output generator used to write headers / messages"
if genOpts is None:
self.genOpts = GeneratorOptions(apiname = APIConventions().xml_api_name)
else:
self.genOpts = genOpts
"Options controlling features to write and how to format them"
self.gen.registry = self
self.gen.genOpts = self.genOpts
self.gen.genOpts.registry = self
self.tree = None
"ElementTree containing the root `<registry>`"
self.typedict = {}
"dictionary of TypeInfo objects keyed by type name"
self.groupdict = {}
"dictionary of GroupInfo objects keyed by group name"
self.enumdict = {}
"dictionary of EnumInfo objects keyed by enum name"
self.cmddict = {}
"dictionary of CmdInfo objects keyed by command name"
self.apidict = {}
"dictionary of FeatureInfo objects for `<feature>` elements keyed by API name"
self.extensions = []
"list of `<extension>` Elements"
self.extdict = {}
"dictionary of FeatureInfo objects for `<extension>` elements keyed by extension name"
self.spirvextdict = {}
"dictionary of FeatureInfo objects for `<spirvextension>` elements keyed by spirv extension name"
self.spirvcapdict = {}
"dictionary of FeatureInfo objects for `<spirvcapability>` elements keyed by spirv capability name"
self.formatsdict = {}
"dictionary of FeatureInfo objects for `<format>` elements keyed by VkFormat name"
self.syncstagedict = {}
"dictionary of Sync*Info objects for `<syncstage>` elements keyed by VkPipelineStageFlagBits2 name"
self.syncaccessdict = {}
"dictionary of Sync*Info objects for `<syncaccess>` elements keyed by VkAccessFlagBits2 name"
self.syncpipelinedict = {}
"dictionary of Sync*Info objects for `<syncpipeline>` elements keyed by pipeline type name"
self.emitFeatures = False
self.breakPat = None
"regexp pattern to break on when generating names"
self.requiredextensions = []
self.commandextensiontuple = namedtuple('commandextensiontuple',
['command', 'value', 'extension']) self.validextensionstructs = defaultdict(list)
self.commandextensionsuccesses = []
self.commandextensionerrors = []
self.filename = None
def loadElementTree(self, tree):
self.tree = tree
self.parseTree()
def loadFile(self, file):
self.filename = file
self.tree = etree.parse(file)
self.parseTree()
def setGenerator(self, gen):
self.gen = gen
self.gen.setRegistry(self)
def addElementInfo(self, elem, info, infoName, dictionary):
key = elem.get('name')
if key in dictionary:
if not dictionary[key].compareElem(info, infoName):
self.gen.logMsg('warn', 'Attempt to redefine', key,
'(this should not happen)')
else:
dictionary[key] = info
def lookupElementInfo(self, fname, dictionary):
key = (fname, self.genOpts.apiname)
if key in dictionary:
return dictionary[key]
if fname in dictionary:
return dictionary[fname]
return None
def breakOnName(self, regexp):
self.breakPat = re.compile(regexp)
def parseTree(self):
if self.tree is None:
raise RuntimeError("Tree not initialized!")
self.reg = self.tree.getroot()
if self.genOpts.mergeApiNames:
mergeAPIs(self.reg, self.genOpts.mergeApiNames.split(','), self.genOpts.apiname)
else:
stripNonmatchingAPIs(self.reg, self.genOpts.apiname, actuallyDelete = True)
self.typedict = {}
for type_elem in self.reg.findall('types/type'):
if type_elem.get('name') is None:
name_elem = type_elem.find('name')
if name_elem is None or not name_elem.text:
raise RuntimeError("Type without a name!")
type_elem.set('name', name_elem.text)
self.addElementInfo(type_elem, TypeInfo(type_elem), 'type', self.typedict)
self.groupdict = {}
for group in self.reg.findall('enums'):
self.addElementInfo(group, GroupInfo(group), 'group', self.groupdict)
self.enumdict = {}
for enums in self.reg.findall('enums'):
required = (enums.get('type') is not None)
for enum in enums.findall('enum'):
enumInfo = EnumInfo(enum)
enumInfo.required = required
self.addElementInfo(enum, enumInfo, 'enum', self.enumdict)
self.cmddict = {}
cmdAlias = []
for cmd in self.reg.findall('commands/command'):
name = cmd.get('name')
if name is None:
name_elem = cmd.find('proto/name')
if name_elem is None or not name_elem.text:
raise RuntimeError("Command without a name!")
name = cmd.set('name', name_elem.text)
ci = CmdInfo(cmd)
self.addElementInfo(cmd, ci, 'command', self.cmddict)
alias = cmd.get('alias')
if alias:
cmdAlias.append([name, alias, cmd])
for (name, alias, cmd) in cmdAlias:
if alias in self.cmddict:
aliasInfo = self.cmddict[alias]
cmdElem = copy.deepcopy(aliasInfo.elem)
cmdElem.find('proto/name').text = name
cmdElem.set('name', name)
cmdElem.set('alias', alias)
ci = CmdInfo(cmdElem)
self.cmddict[name] = ci
else:
self.gen.logMsg('warn', 'No matching <command> found for command',
cmd.get('name'), 'alias', alias)
self.apidict = {}
format_condition = dict()
for feature in self.reg.findall('feature'):
featureInfo = FeatureInfo(feature)
self.addElementInfo(feature, featureInfo, 'feature', self.apidict)
for elem in feature.findall('require'):
for enum in elem.findall('enum'):
addEnumInfo = False
groupName = enum.get('extends')
if groupName is not None:
enum.set('version', featureInfo.version)
if groupName in self.groupdict:
gi = self.groupdict[groupName]
gi.elem.append(copy.deepcopy(enum))
else:
self.gen.logMsg('warn', 'NO matching group',
groupName, 'for enum', enum.get('name'), 'found.')
if groupName == "VkFormat":
format_name = enum.get('name')
if enum.get('alias'):
format_name = enum.get('alias')
format_condition[format_name] = featureInfo.name
addEnumInfo = True
elif enum.get('value') or enum.get('bitpos') or enum.get('alias'):
addEnumInfo = True
if addEnumInfo:
enumInfo = EnumInfo(enum)
self.addElementInfo(enum, enumInfo, 'enum', self.enumdict)
sync_pipeline_stage_condition = dict()
sync_access_condition = dict()
self.extensions = self.reg.findall('extensions/extension')
self.extdict = {}
for feature in self.extensions:
featureInfo = FeatureInfo(feature)
self.addElementInfo(feature, featureInfo, 'extension', self.extdict)
for elem in feature.findall('require'):
for enum in elem.findall('enum'):
addEnumInfo = False
groupName = enum.get('extends')
if groupName is not None:
extnumber = enum.get('extnumber')
if not extnumber:
enum.set('extnumber', str(featureInfo.number))
enum.set('extname', featureInfo.name)
enum.set('supported', noneStr(featureInfo.supported))
if groupName in self.groupdict:
gi = self.groupdict[groupName]
gi.elem.append(copy.deepcopy(enum))
else:
self.gen.logMsg('warn', 'NO matching group',
groupName, 'for enum', enum.get('name'), 'found.')
if groupName == "VkFormat":
format_name = enum.get('name')
if enum.get('alias'):
format_name = enum.get('alias')
if format_name in format_condition:
format_condition[format_name] += "," + featureInfo.name
else:
format_condition[format_name] = featureInfo.name
elif groupName == "VkPipelineStageFlagBits2":
stage_flag = enum.get('name')
if enum.get('alias'):
stage_flag = enum.get('alias')
featureName = elem.get('depends') if elem.get('depends') is not None else featureInfo.name
if stage_flag in sync_pipeline_stage_condition:
sync_pipeline_stage_condition[stage_flag] += "," + featureName
else:
sync_pipeline_stage_condition[stage_flag] = featureName
elif groupName == "VkAccessFlagBits2":
access_flag = enum.get('name')
if enum.get('alias'):
access_flag = enum.get('alias')
featureName = elem.get('depends') if elem.get('depends') is not None else featureInfo.name
if access_flag in sync_access_condition:
sync_access_condition[access_flag] += "," + featureName
else:
sync_access_condition[access_flag] = featureName
addEnumInfo = True
elif enum.get('value') or enum.get('bitpos') or enum.get('alias'):
addEnumInfo = True
if addEnumInfo:
enumInfo = EnumInfo(enum)
self.addElementInfo(enum, enumInfo, 'enum', self.enumdict)
for spirv in self.reg.findall('spirvextensions/spirvextension'):
spirvInfo = SpirvInfo(spirv)
self.addElementInfo(spirv, spirvInfo, 'spirvextension', self.spirvextdict)
for spirv in self.reg.findall('spirvcapabilities/spirvcapability'):
spirvInfo = SpirvInfo(spirv)
self.addElementInfo(spirv, spirvInfo, 'spirvcapability', self.spirvcapdict)
for format in self.reg.findall('formats/format'):
condition = None
format_name = format.get('name')
if format_name in format_condition:
condition = format_condition[format_name]
formatInfo = FormatInfo(format, condition)
self.addElementInfo(format, formatInfo, 'format', self.formatsdict)
for stage in self.reg.findall('sync/syncstage'):
condition = None
stage_flag = stage.get('name')
if stage_flag in sync_pipeline_stage_condition:
condition = sync_pipeline_stage_condition[stage_flag]
syncInfo = SyncStageInfo(stage, condition)
self.addElementInfo(stage, syncInfo, 'syncstage', self.syncstagedict)
for access in self.reg.findall('sync/syncaccess'):
condition = None
access_flag = access.get('name')
if access_flag in sync_access_condition:
condition = sync_access_condition[access_flag]
syncInfo = SyncAccessInfo(access, condition)
self.addElementInfo(access, syncInfo, 'syncaccess', self.syncaccessdict)
for pipeline in self.reg.findall('sync/syncpipeline'):
syncInfo = SyncPipelineInfo(pipeline)
self.addElementInfo(pipeline, syncInfo, 'syncpipeline', self.syncpipelinedict)
def dumpReg(self, maxlen=120, filehandle=sys.stdout):
write('***************************************', file=filehandle)
write(' ** Dumping Registry contents **', file=filehandle)
write('***************************************', file=filehandle)
write('// Types', file=filehandle)
for name in self.typedict:
tobj = self.typedict[name]
write(' Type', name, '->', etree.tostring(tobj.elem)[0:maxlen], file=filehandle)
write('// Groups', file=filehandle)
for name in self.groupdict:
gobj = self.groupdict[name]
write(' Group', name, '->', etree.tostring(gobj.elem)[0:maxlen], file=filehandle)
write('// Enums', file=filehandle)
for name in self.enumdict:
eobj = self.enumdict[name]
write(' Enum', name, '->', etree.tostring(eobj.elem)[0:maxlen], file=filehandle)
write('// Commands', file=filehandle)
for name in self.cmddict:
cobj = self.cmddict[name]
write(' Command', name, '->', etree.tostring(cobj.elem)[0:maxlen], file=filehandle)
write('// APIs', file=filehandle)
for key in self.apidict:
write(' API Version ', key, '->',
etree.tostring(self.apidict[key].elem)[0:maxlen], file=filehandle)
write('// Extensions', file=filehandle)
for key in self.extdict:
write(' Extension', key, '->',
etree.tostring(self.extdict[key].elem)[0:maxlen], file=filehandle)
write('// SPIR-V', file=filehandle)
for key in self.spirvextdict:
write(' SPIR-V Extension', key, '->',
etree.tostring(self.spirvextdict[key].elem)[0:maxlen], file=filehandle)
for key in self.spirvcapdict:
write(' SPIR-V Capability', key, '->',
etree.tostring(self.spirvcapdict[key].elem)[0:maxlen], file=filehandle)
write('// VkFormat', file=filehandle)
for key in self.formatsdict:
write(' VkFormat', key, '->',
etree.tostring(self.formatsdict[key].elem)[0:maxlen], file=filehandle)
def markTypeRequired(self, typename, required):
self.gen.logMsg('diag', 'tagging type:', typename, '-> required =', required)
typeinfo = self.lookupElementInfo(typename, self.typedict)
if typeinfo is not None:
if required:
for attrib_name in ['requires', 'alias']:
depname = typeinfo.elem.get(attrib_name)
if depname:
self.gen.logMsg('diag', 'Generating dependent type',
depname, 'for', attrib_name, 'type', typename)
if typename != depname:
self.markTypeRequired(depname, required)
else:
self.gen.logMsg('diag', 'type', typename, 'is self-referential')
for subtype in typeinfo.elem.findall('.//type'):
self.gen.logMsg('diag', 'markRequired: type requires dependent <type>', subtype.text)
if typename != subtype.text:
self.markTypeRequired(subtype.text, required)
else:
self.gen.logMsg('diag', 'type', typename, 'is self-referential')
for subenum in typeinfo.elem.findall('.//enum'):
self.gen.logMsg('diag', 'markRequired: type requires dependent <enum>', subenum.text)
self.markEnumRequired(subenum.text, required)
depType = typeinfo.elem.get('bitvalues')
if depType:
self.gen.logMsg('diag', 'Generating bitflag type',
depType, 'for type', typename)
self.markTypeRequired(depType, required)
group = self.lookupElementInfo(depType, self.groupdict)
if group is not None:
group.flagType = typeinfo
typeinfo.required = required
elif '.h' not in typename:
self.gen.logMsg('warn', 'type:', typename, 'IS NOT DEFINED')
def markEnumRequired(self, enumname, required):
self.gen.logMsg('diag', 'markEnumRequired: tagging enum:', enumname, '-> required =', required)
enum = self.lookupElementInfo(enumname, self.enumdict)
if enum is not None:
if not required:
groupName = enum.elem.get('extends')
if groupName is not None:
self.gen.logMsg('diag', f'markEnumRequired: Removing extending enum {enum.elem.get("name")}')
if groupName in self.groupdict:
gi = self.groupdict[groupName]
gienum = gi.elem.find("enum[@name='" + enumname + "']")
if gienum is not None:
gi.elem.remove(gienum)
else:
self.gen.logMsg('warn', 'markEnumRequired: Cannot remove enum',
enumname, 'not found in group',
groupName)
else:
self.gen.logMsg('warn', 'markEnumRequired: Cannot remove enum',
enumname, 'from nonexistent group',
groupName)
else:
enumName = enum.elem.get('name')
self.gen.logMsg('diag', f'markEnumRequired: Removing non-extending enum {enumName}')
count = 0
for enums in self.reg.findall('enums'):
for thisEnum in enums.findall('enum'):
if thisEnum.get('name') == enumName:
count = count + 1
enums.remove(thisEnum)
if count == 0:
self.gen.logMsg('warn', f'markEnumRequired: {enumName}) not found in any <enums> tag')
enum.required = required
depname = enum.elem.get('alias')
if depname:
self.gen.logMsg('diag', 'markEnumRequired: Generating dependent enum',
depname, 'for alias', enumname, 'required =', enum.required)
self.markEnumRequired(depname, required)
else:
self.gen.logMsg('warn', f'markEnumRequired: {enumname} IS NOT DEFINED')
def markCmdRequired(self, cmdname, required):
self.gen.logMsg('diag', 'tagging command:', cmdname, '-> required =', required)
cmd = self.lookupElementInfo(cmdname, self.cmddict)
if cmd is not None:
cmd.required = required
if self.genOpts.requireCommandAliases:
depname = cmd.elem.get('alias')
if depname:
self.gen.logMsg('diag', 'Generating dependent command',
depname, 'for alias', cmdname)
self.markCmdRequired(depname, required)
if required:
for type_elem in cmd.elem.findall('.//type'):
self.gen.logMsg('diag', 'markRequired: command implicitly requires dependent type', type_elem.text)
self.markTypeRequired(type_elem.text, required)
else:
self.gen.logMsg('warn', 'command:', cmdname, 'IS NOT DEFINED')
def markRequired(self, featurename, feature, required):
self.gen.logMsg('diag', 'markRequired (feature = <too long to print>, required =', required, ')')
for typeElem in feature.findall('type'):
self.markTypeRequired(typeElem.get('name'), required)
for enumElem in feature.findall('enum'):
self.markEnumRequired(enumElem.get('name'), required)
for cmdElem in feature.findall('command'):
self.markCmdRequired(cmdElem.get('name'), required)
for extendElem in feature.findall('extend'):
extendType = extendElem.get('type')
if extendType == 'command':
commandName = extendElem.get('name')
successExtends = extendElem.get('successcodes')
if successExtends is not None:
for success in successExtends.split(','):
self.commandextensionsuccesses.append(self.commandextensiontuple(command=commandName,
value=success,
extension=featurename))
errorExtends = extendElem.get('errorcodes')
if errorExtends is not None:
for error in errorExtends.split(','):
self.commandextensionerrors.append(self.commandextensiontuple(command=commandName,
value=error,
extension=featurename))
else:
self.gen.logMsg('warn', 'extend type:', extendType, 'IS NOT SUPPORTED')
def getAlias(self, elem, dict):
alias = elem.get('alias')
if alias is None:
name = elem.get('name')
typeinfo = self.lookupElementInfo(name, dict)
if not typeinfo:
self.gen.logMsg('error', name, 'is not a known name')
alias = typeinfo.elem.get('alias')
return alias
def checkForCorrectionAliases(self, alias, require, tag):
return False
def fillFeatureDictionary(self, interface, featurename, api, profile):
self.gen.featureDictionary[featurename] = {
"enumconstant": {},
"command": {},
"enum": {},
"struct": {},
"handle": {},
"basetype": {},
"include": {},
"define": {},
"bitmask": {},
"union": {},
"funcpointer": {},
}
for require in interface.findall('require'):
if matchAPIProfile(api, profile, require):
required_key = require.get('depends')
for typeElem in require.findall('type'):
typename = typeElem.get('name')
typeinfo = self.lookupElementInfo(typename, self.typedict)
if typeinfo:
alias = self.getAlias(typeElem, self.typedict)
if not self.checkForCorrectionAliases(alias, require, 'type'):
while alias:
typeinfo = self.lookupElementInfo(alias, self.typedict)
alias = typeinfo.elem.get('alias')
typecat = typeinfo.elem.get('category')
typeextends = typeinfo.elem.get('structextends')
if not required_key in self.gen.featureDictionary[featurename][typecat]:
self.gen.featureDictionary[featurename][typecat][required_key] = {}
if not typeextends in self.gen.featureDictionary[featurename][typecat][required_key]:
self.gen.featureDictionary[featurename][typecat][required_key][typeextends] = []
self.gen.featureDictionary[featurename][typecat][required_key][typeextends].append(typename)
else:
self.gen.logMsg('warn', 'fillFeatureDictionary: NOT filling for {}'.format(typename))
for enumElem in require.findall('enum'):
enumname = enumElem.get('name')
typeinfo = self.lookupElementInfo(enumname, self.enumdict)
alias = self.getAlias(enumElem, self.enumdict)
if not self.checkForCorrectionAliases(alias, require, 'enum'):
enumextends = enumElem.get('extends')
if not required_key in self.gen.featureDictionary[featurename]['enumconstant']:
self.gen.featureDictionary[featurename]['enumconstant'][required_key] = {}
if not enumextends in self.gen.featureDictionary[featurename]['enumconstant'][required_key]:
self.gen.featureDictionary[featurename]['enumconstant'][required_key][enumextends] = []
self.gen.featureDictionary[featurename]['enumconstant'][required_key][enumextends].append(enumname)
else:
self.gen.logMsg('warn', 'fillFeatureDictionary: NOT filling for {}'.format(typename))
for cmdElem in require.findall('command'):
alias = self.getAlias(cmdElem, self.cmddict)
if not self.checkForCorrectionAliases(alias, require, 'command'):
if not required_key in self.gen.featureDictionary[featurename]['command']:
self.gen.featureDictionary[featurename]['command'][required_key] = []
self.gen.featureDictionary[featurename]['command'][required_key].append(cmdElem.get('name'))
else:
self.gen.logMsg('warn', 'fillFeatureDictionary: NOT filling for {}'.format(typename))
def requireFeatures(self, interface, featurename, api, profile):
for feature in interface.findall('require'):
if matchAPIProfile(api, profile, feature):
self.markRequired(featurename, feature, True)
def removeFeatures(self, interface, featurename, api, profile):
for feature in interface.findall('remove'):
if matchAPIProfile(api, profile, feature):
self.markRequired(featurename, feature, False)
def assignAdditionalValidity(self, interface, api, profile):
for feature in interface.findall('require'):
if matchAPIProfile(api, profile, feature):
for v in feature.findall('usage'):
if v.get('command'):
self.cmddict[v.get('command')].additionalValidity.append(copy.deepcopy(v))
if v.get('struct'):
self.typedict[v.get('struct')].additionalValidity.append(copy.deepcopy(v))
def removeAdditionalValidity(self, interface, api, profile):
for feature in interface.findall('remove'):
if matchAPIProfile(api, profile, feature):
for v in feature.findall('usage'):
if v.get('command'):
self.cmddict[v.get('command')].removedValidity.append(copy.deepcopy(v))
if v.get('struct'):
self.typedict[v.get('struct')].removedValidity.append(copy.deepcopy(v))
def generateFeature(self, fname, ftype, dictionary, explicit=False):
self.gen.logMsg('diag', 'generateFeature: generating', ftype, fname)
if not (explicit or self.genOpts.requireDepends):
self.gen.logMsg('diag', 'generateFeature: NOT generating', ftype, fname, 'because generator does not require dependencies')
return
f = self.lookupElementInfo(fname, dictionary)
if f is None:
self.gen.logMsg('diag', 'No entry found for feature', fname,
'returning!')
return
if not f.required:
self.gen.logMsg('diag', 'Skipping', ftype, fname, '(not required)')
return
if f.declared:
self.gen.logMsg('diag', 'Skipping', ftype, fname, '(already declared)')
return
f.declared = True
alias = f.elem.get('alias')
if alias:
self.gen.logMsg('diag', fname, 'is an alias of', alias)
genProc = None
followupFeature = None
if ftype == 'type':
genProc = self.gen.genType
if alias:
self.generateFeature(alias, 'type', self.typedict)
requires = f.elem.get('requires')
if requires:
self.gen.logMsg('diag', 'Generating required dependent type',
requires)
self.generateFeature(requires, 'type', self.typedict)
for subtype in f.elem.findall('.//type'):
self.gen.logMsg('diag', 'Generating required dependent <type>',
subtype.text)
self.generateFeature(subtype.text, 'type', self.typedict)
for subtype in f.elem.findall('.//enum'):
self.gen.logMsg('diag', 'Generating required dependent <enum>',
subtype.text)
self.generateFeature(subtype.text, 'enum', self.enumdict)
if f.elem.get('category') == 'enum':
self.gen.logMsg('diag', 'Type', fname, 'is an enum group, so generate that instead')
group = self.lookupElementInfo(fname, self.groupdict)
if alias is not None:
self.gen.logMsg('diag', 'Generating alias', fname,
'for enumerated type', alias)
genProc = self.gen.genGroup
f = self.lookupElementInfo(alias, self.groupdict)
elif group is None:
self.gen.logMsg('warn', 'Skipping enum type', fname,
': No matching enumerant group')
return
else:
genProc = self.gen.genGroup
f = group
enums = group.elem.findall('enum')
self.gen.logMsg('diag', 'generateFeature: checking enums for group', fname)
enumAliases = []
for elem in enums:
name = elem.get('name')
required = False
extname = elem.get('extname')
version = elem.get('version')
if extname is not None:
supported_list = elem.get('supported').split(",")
if self.genOpts.defaultExtensions in supported_list:
required = True
elif re.match(self.genOpts.addExtensions, extname) is not None:
required = True
elif version is not None:
required = re.match(self.genOpts.emitversions, version) is not None
else:
required = True
self.gen.logMsg('diag', '* required =', required, 'for', name)
if required:
elem.set('required', 'true')
enumAlias = elem.get('alias')
if enumAlias:
enumAliases.append(enumAlias)
for elem in enums:
name = elem.get('name')
if name in enumAliases:
elem.set('required', 'true')
self.gen.logMsg('diag', '* also need to require alias', name)
if f is None:
raise RuntimeError("Should not get here")
if f.elem.get('category') == 'bitmask':
followupFeature = f.elem.get('bitvalues')
elif ftype == 'command':
if alias:
self.generateFeature(alias, 'command', self.cmddict)
genProc = self.gen.genCmd
for type_elem in f.elem.findall('.//type'):
depname = type_elem.text
self.gen.logMsg('diag', 'Generating required parameter type',
depname)
self.generateFeature(depname, 'type', self.typedict)
elif ftype == 'enum':
if alias:
self.generateFeature(alias, 'enum', self.enumdict)
genProc = self.gen.genEnum
if self.emitFeatures:
self.gen.logMsg('diag', 'Emitting', ftype, 'decl for', fname)
if genProc is None:
raise RuntimeError("genProc is None when we should be emitting")
genProc(f, fname, alias)
else:
self.gen.logMsg('diag', 'Skipping', ftype, fname,
'(should not be emitted)')
if followupFeature:
self.gen.logMsg('diag', 'Generating required bitvalues <enum>',
followupFeature)
self.generateFeature(followupFeature, "type", self.typedict)
def generateRequiredInterface(self, interface):
for features in interface.findall('require'):
for t in features.findall('type'):
self.generateFeature(t.get('name'), 'type', self.typedict, explicit=True)
for e in features.findall('enum'):
enumextends = e.get('extends')
if not enumextends:
self.generateFeature(e.get('name'), 'enum', self.enumdict, explicit=True)
for c in features.findall('command'):
self.generateFeature(c.get('name'), 'command', self.cmddict, explicit=True)
def generateSpirv(self, spirv, dictionary):
if spirv is None:
self.gen.logMsg('diag', 'No entry found for element', name,
'returning!')
return
name = spirv.elem.get('name')
alias = None
if spirv.emit:
genProc = self.gen.genSpirv
genProc(spirv, name, alias)
def stripUnsupportedAPIs(self, dictionary, attribute, supportedDictionary):
for key in dictionary:
eleminfo = dictionary[key]
attribstring = eleminfo.elem.get(attribute)
if attribstring is not None:
apis = []
stripped = False
for api in attribstring.split(','):
if api in supportedDictionary and supportedDictionary[api].required:
apis.append(api)
else:
stripped = True
if stripped:
eleminfo.elem.set(attribute, ','.join(apis))
def stripUnsupportedAPIsFromList(self, dictionary, supportedDictionary):
for key in dictionary:
attribstring = dictionary[key]
if attribstring is not None:
apis = []
stripped = False
for api in attribstring:
if supportedDictionary[api].required:
apis.append(api)
else:
stripped = True
if stripped:
dictionary[key] = apis
def generateFormat(self, format, dictionary):
if format is None:
self.gen.logMsg('diag', 'No entry found for format element',
'returning!')
return
name = format.elem.get('name')
alias = None
if format.emit:
genProc = self.gen.genFormat
genProc(format, name, alias)
def generateSyncStage(self, sync):
genProc = self.gen.genSyncStage
genProc(sync)
def generateSyncAccess(self, sync):
genProc = self.gen.genSyncAccess
genProc(sync)
def generateSyncPipeline(self, sync):
genProc = self.gen.genSyncPipeline
genProc(sync)
def tagValidExtensionStructs(self):
for typeinfo in self.typedict.values():
type_elem = typeinfo.elem
if typeinfo.required and type_elem.get('category') == 'struct':
struct_extends = type_elem.get('structextends')
if struct_extends is not None:
for parent in struct_extends.split(','):
self.validextensionstructs[parent].append(type_elem.get('name'))
for parent in self.validextensionstructs:
self.validextensionstructs[parent].sort()
def apiGen(self):
self.gen.logMsg('diag', '*******************************************')
self.gen.logMsg('diag', ' Registry.apiGen file:', self.genOpts.filename,
'api:', self.genOpts.apiname,
'profile:', self.genOpts.profile)
self.gen.logMsg('diag', '*******************************************')
regVersions = re.compile(self.genOpts.versions)
regEmitVersions = re.compile(self.genOpts.emitversions)
regAddExtensions = re.compile(self.genOpts.addExtensions)
regRemoveExtensions = re.compile(self.genOpts.removeExtensions)
regEmitExtensions = re.compile(self.genOpts.emitExtensions)
regEmitSpirv = re.compile(self.genOpts.emitSpirv)
regEmitFormats = re.compile(self.genOpts.emitFormats)
features = []
apiMatch = False
for key in self.apidict:
fi = self.apidict[key]
api = fi.elem.get('api')
if apiNameMatch(self.genOpts.apiname, api):
apiMatch = True
if regVersions.match(fi.name):
fi.emit = (regEmitVersions.match(fi.name) is not None)
features.append(fi)
if not fi.emit:
self.gen.logMsg('diag', 'NOT tagging feature api =', api,
'name =', fi.name, 'version =', fi.version,
'for emission (does not match emitversions pattern)')
else:
self.gen.logMsg('diag', 'Including feature api =', api,
'name =', fi.name, 'version =', fi.version,
'for emission (matches emitversions pattern)')
else:
self.gen.logMsg('diag', 'NOT including feature api =', api,
'name =', fi.name, 'version =', fi.version,
'(does not match requested versions)')
else:
self.gen.logMsg('diag', 'NOT including feature api =', api,
'name =', fi.name,
'(does not match requested API)')
if not apiMatch:
self.gen.logMsg('warn', 'No matching API versions found!')
for (extName, ei) in sorted(self.extdict.items(), key=lambda x: x[1].number if x[1].number is not None else '0'):
extName = ei.name
include = False
if apiNameMatch(self.genOpts.defaultExtensions,
ei.elem.get('supported')):
self.gen.logMsg('diag', 'Including extension',
extName, "(defaultExtensions matches the 'supported' attribute)")
include = True
if regAddExtensions.match(extName) is not None:
if not apiNameMatch(self.genOpts.apiname, ei.elem.get('supported')):
self.gen.logMsg('diag', 'NOT including extension',
extName, '(matches explicitly requested, but does not match the \'supported\' attribute)')
include = False
else:
self.gen.logMsg('diag', 'Including extension',
extName, '(matches explicitly requested extensions to add)')
include = True
if regRemoveExtensions.match(extName) is not None:
self.gen.logMsg('diag', 'Removing extension',
extName, '(matches explicitly requested extensions to remove)')
include = False
if include:
ei.emit = (regEmitExtensions.match(extName) is not None)
features.append(ei)
if not ei.emit:
self.gen.logMsg('diag', 'NOT tagging extension',
extName,
'for emission (does not match emitextensions pattern)')
self.requiredextensions.append(extName)
else:
self.gen.logMsg('diag', 'NOT including extension',
extName, '(does not match api attribute or explicitly requested extensions)')
spirvexts = []
for key in self.spirvextdict:
si = self.spirvextdict[key]
si.emit = (regEmitSpirv.match(key) is not None)
spirvexts.append(si)
spirvcaps = []
for key in self.spirvcapdict:
si = self.spirvcapdict[key]
si.emit = (regEmitSpirv.match(key) is not None)
spirvcaps.append(si)
formats = []
for key in self.formatsdict:
si = self.formatsdict[key]
si.emit = (regEmitFormats.match(key) is not None)
formats.append(si)
if self.genOpts.sortProcedure:
self.genOpts.sortProcedure(features)
self.gen.logMsg('diag', 'PASS 1: TAG FEATURES')
for f in features:
self.gen.logMsg('diag', 'PASS 1: Tagging required and features for', f.name)
self.fillFeatureDictionary(f.elem, f.name, self.genOpts.apiname, self.genOpts.profile)
self.requireFeatures(f.elem, f.name, self.genOpts.apiname, self.genOpts.profile)
self.assignAdditionalValidity(f.elem, self.genOpts.apiname, self.genOpts.profile)
for f in features:
self.gen.logMsg('diag', 'PASS 2: Tagging removed features for', f.name)
self.removeFeatures(f.elem, f.name, self.genOpts.apiname, self.genOpts.profile)
self.removeAdditionalValidity(f.elem, self.genOpts.apiname, self.genOpts.profile)
self.stripUnsupportedAPIs(self.typedict, 'structextends', self.typedict)
self.stripUnsupportedAPIs(self.cmddict, 'successcodes', self.enumdict)
self.stripUnsupportedAPIs(self.cmddict, 'errorcodes', self.enumdict)
self.stripUnsupportedAPIsFromList(self.validextensionstructs, self.typedict)
self.tagValidExtensionStructs()
self.gen.logMsg('diag', 'PASS 3: GENERATE INTERFACES FOR FEATURES')
self.gen.beginFile(self.genOpts)
for f in features:
self.gen.logMsg('diag', 'PASS 3: Generating interface for',
f.name)
emit = self.emitFeatures = f.emit
if not emit:
self.gen.logMsg('diag', 'PASS 3: NOT declaring feature',
f.elem.get('name'), 'because it is not tagged for emission')
self.gen.beginFeature(f.elem, emit)
self.generateRequiredInterface(f.elem)
self.gen.endFeature()
for s in spirvexts:
self.generateSpirv(s, self.spirvextdict)
for s in spirvcaps:
self.generateSpirv(s, self.spirvcapdict)
for s in formats:
self.generateFormat(s, self.formatsdict)
for s in self.syncstagedict:
self.generateSyncStage(self.syncstagedict[s])
for s in self.syncaccessdict:
self.generateSyncAccess(self.syncaccessdict[s])
for s in self.syncpipelinedict:
self.generateSyncPipeline(self.syncpipelinedict[s])
self.gen.endFile()
def apiReset(self):
for datatype in self.typedict:
self.typedict[datatype].resetState()
for enum in self.enumdict:
self.enumdict[enum].resetState()
for cmd in self.cmddict:
self.cmddict[cmd].resetState()
for cmd in self.apidict:
self.apidict[cmd].resetState()