def translate_command(variables, constants, tag): restype, cname = vkparser.parse_member(tag.proto) name = re.sub('^vk([A-Z]?)', lambda m: m.group(1).lower(), cname) variables[name] = command = {} command["name"] = cname command["type"] = ctype = {"type": "cfunc"} ctype["restype"] = writeout_type(constants, restype) ctype["argtypes"] = argtypes = [] for param in tag: if param.name == "param": argtype, _ = vkparser.parse_member(param) argtypes.append(writeout_type(constants, argtype))
def translate_struct(types, constants, struct): name = struct["name"] name = re.subn("^Vk", "", name)[0] types[name] = this = {"type": "struct"} this["fields"] = fields = [] this["autoarrays"] = autoarrays = {} this["aliases"] = aliases = {} this["defaults"] = defaults = {} has_sType = False for tag in struct: if tag.name == "member": tp, name_ = vkparser.parse_member(tag) fields.append([name_, writeout_type(constants, tp)]) if "len" in tag.attrs and tag[ "len"] != "null-terminated" and not tag["len"].startswith( "latexmath:"): counter = tag["len"].split(",", 1)[0] alias = re.sub('^p+([A-Z])', lambda m: m.group(1).lower(), name_) autoarrays[alias] = [counter, name_] elif re.match("^p+[A-Z]", name_) and name_ != "pNext": assert re.match("^p+[A-Z]", name_), name_ alias = re.sub('^p+([A-Z])', lambda m: m.group(1).lower(), name_) aliases[alias] = name_ has_sType = has_sType or name_ == "sType" if has_sType: defaults["sType"] = "_".join(x.upper() for x in split_case(name))
def epilogue(): print "{0}.declare([".format(name) for tag in struct: if tag.name == "member": tp, name_ = vkparser.parse_member(tag) print " ({!r}, {}),".format(name_, format_type(tp)) print "])"
def epilogue(): autoarrays = [] aliases = [] has_sType = False print "{0}.declare([".format(name) for tag in struct: if tag.name == "member": tp, name_ = vkparser.parse_member(tag) print " ({!r}, {}),".format(name_, format_type(tp)) if "len" in tag.attrs and tag["len"] != "null-terminated": autoarrays.append((tag["len"].split(",", 1)[0], name_)) elif re.match("^p+[A-Z]", name_) and name_ != "pNext": aliases.append(name_) has_sType = has_sType or name_ == "sType" print "])" if len(autoarrays) > 0: print "{0}.declare_autoarrays([".format(name) for counter, pointer in autoarrays: if counter.startswith("latexmath:"): continue assert re.match("^p+[A-Z]", pointer), pointer name_ = re.sub('^p+([A-Z])', lambda m: m.group(1).lower(), pointer) print " ({!r}, ({!r}, {!r})),".format(name_, counter, pointer) print "])" if len(aliases) > 0: print "{0}.declare_aliases([".format(name) for pointer in aliases: assert re.match("^p+[A-Z]", pointer), pointer name_ = re.sub('^p+([A-Z])', lambda m: m.group(1).lower(), pointer) print " ({!r}, {!r}),".format(name_, pointer) print "])" if has_sType: print "{}.sType = {!r}".format(name, "_".join((x.upper() for x in split_case(name))))
def translate_union(types, constants, struct): name = struct["name"] name = re.subn("^Vk", "", name)[0] types[name] = this = {"type":"union"} this["fields"] = fields = [] for tag in struct: if tag.name == "member": tp, name_ = vkparser.parse_member(tag) fields.append([name_, writeout_type(constants, tp)])
def epilogue(): autoarrays = [] aliases = [] has_sType = False print "{0}.declare([".format(name) for tag in struct: if tag.name == "member": tp, name_ = vkparser.parse_member(tag) print " ({!r}, {}),".format(name_, format_type(tp)) if "len" in tag.attrs and tag["len"] != "null-terminated": autoarrays.append((tag["len"].split(",", 1)[0], name_)) elif re.match("^p+[A-Z]", name_) and name_ != "pNext": aliases.append(name_) has_sType = has_sType or name_ == "sType" print "])" if len(autoarrays) > 0: print "{0}.declare_autoarrays([".format(name) for counter, pointer in autoarrays: if counter.startswith("latexmath:"): continue assert re.match("^p+[A-Z]", pointer), pointer name_ = re.sub('^p+([A-Z])', lambda m: m.group(1).lower(), pointer) print " ({!r}, ({!r}, {!r})),".format( name_, counter, pointer) print "])" if len(aliases) > 0: print "{0}.declare_aliases([".format(name) for pointer in aliases: assert re.match("^p+[A-Z]", pointer), pointer name_ = re.sub('^p+([A-Z])', lambda m: m.group(1).lower(), pointer) print " ({!r}, {!r}),".format(name_, pointer) print "])" if has_sType: print "{}.sType = {!r}".format( name, "_".join((x.upper() for x in split_case(name))))
def translate_struct(types, constants, struct): name = struct["name"] name = re.subn("^Vk", "", name)[0] types[name] = this = {"type":"struct"} this["fields"] = fields = [] this["autoarrays"] = autoarrays = {} this["aliases"] = aliases = {} this["defaults"] = defaults = {} has_sType = False for tag in struct: if tag.name == "member": tp, name_ = vkparser.parse_member(tag) fields.append([name_, writeout_type(constants, tp)]) if "len" in tag.attrs and tag["len"] != "null-terminated" and not tag["len"].startswith("latexmath:"): counter = tag["len"].split(",", 1)[0] alias = re.sub('^p+([A-Z])', lambda m: m.group(1).lower(), name_) autoarrays[alias] = [counter, name_] elif re.match("^p+[A-Z]", name_) and name_ != "pNext": assert re.match("^p+[A-Z]", name_), name_ alias = re.sub('^p+([A-Z])', lambda m: m.group(1).lower(), name_) aliases[alias] = name_ has_sType = has_sType or name_ == "sType" if has_sType: defaults["sType"] = "_".join(x.upper() for x in split_case(name))
def translate(): print "# This file was automatically generated by vkstruct.py" print "# You may want to edit the generator rather than this file." print "import vkbuilder, ctypes" print print "SampleMask = ctypes.c_uint32" print "Bool32 = ctypes.c_uint32" print "DeviceSize = ctypes.c_uint64" print print "lib = vkbuilder.load_vulkan()" with open(vk_xml) as fd: soup = BeautifulSoup(fd.read(), 'xml') registry = soup.registry enumerations = set() for tag in registry: if tag.name == "enums": if tag.get("name") == "API Constants": translate_api_constants(tag) continue if tag.get("type") == "enum": name = translate_enumeration(tag, "vkbuilder.Enumeration") enumerations.add(name) continue if tag.get("type") == "bitmask": name = translate_enumeration(tag, "vkbuilder.Bitmask") enumerations.add(name) continue assert False, tag print "class VulkanError(Exception):" print " def __init__(self, result):" print " self.result = result" print " " print " def __str__(self):" print " return str(self.result)" print "ResultCheck = vkbuilder.AutoCheck(Result, VulkanError)" epilogues = [] for tag in registry.types: if tag.name == "type": category = tag.get("category") if category == "include": continue if category == "define": continue if category == "basetype": continue if category == "bitmask": name = tag.find("name").text name = rename_enumeration(name) if name not in enumerations and name: print "{} = ctypes.c_uint32".format(name) continue if category == "handle": translate_handle(tag) continue if category == "enum": name = tag["name"] name = rename_enumeration(name) if name not in enumerations and name: print "{} = ctypes.c_uint32".format(name) continue if category == "funcpointer": translate_funcpointer(tag) continue if category == "struct": epilogues.append(translate_struct(tag)) continue if category == "union": epilogues.append(translate_union(tag)) continue if "requires" in tag.attrs: continue assert False, tag print for epilogue in epilogues: epilogue() for tag in registry.commands: if tag.name == "command": restype, cname = vkparser.parse_member(tag.proto) restype = format_type(restype) if restype == "Result": restype = "ResultCheck" name = re.sub('^vk([A-Z]?)', lambda m: m.group(1).lower(), cname) print print "try:" print " {} = lib.{}".format(name, cname) print "except AttributeError as e: pass" print "else:" print " {}.restype = {}".format(name, restype) print " {}.argtypes = [ ".format(name) for param in tag: if param.name == "param": argtype, _ = vkparser.parse_member(param) argtype = format_type(argtype) print " {},".format(argtype) print " ]"