+}""", output_encoding='utf-8')
+
+NONE = 0xffff
+HASH_SIZE = 256
+U32_MASK = 2**32 - 1
+HASH_MASK = HASH_SIZE - 1
+
+PRIME_FACTOR = 5024183
+PRIME_STEP = 19
+
+
+def cal_hash(name):
+ """Calculate the same hash value that Mesa will calculate in C."""
+ return functools.reduce(
+ lambda h, c: (h * PRIME_FACTOR + ord(c)) & U32_MASK, name, 0)
+
+
+def get_entrypoints(doc, entrypoints_to_defines, start_index):
+ """Extract the entry points from the registry."""
+ entrypoints = []
+
+ enabled_commands = set()
+ for feature in doc.findall('./feature'):
+ assert feature.attrib['api'] == 'vulkan'
+ if VkVersion(feature.attrib['number']) > MAX_API_VERSION:
+ continue
+
+ for command in feature.findall('./require/command'):
+ enabled_commands.add(command.attrib['name'])
+
+ supported = set(ext.name for ext in EXTENSIONS)
+ for extension in doc.findall('.extensions/extension'):
+ if extension.attrib['name'] not in supported:
+ continue
+
+ assert extension.attrib['supported'] == 'vulkan'
+ for command in extension.findall('./require/command'):
+ enabled_commands.add(command.attrib['name'])
+
+ index = start_index
+ for command in doc.findall('./commands/command'):
+ type = command.find('./proto/type').text
+ fullname = command.find('./proto/name').text
+
+ if fullname not in enabled_commands:
+ continue
+
+ shortname = fullname[2:]
+ params = (''.join(p.itertext()) for p in command.findall('./param'))
+ params = ', '.join(params)
+ guard = entrypoints_to_defines.get(fullname)
+ entrypoints.append((type, shortname, params, index, cal_hash(fullname), guard))
+ index += 1
+
+ return entrypoints
+
+
+def get_entrypoints_defines(doc):
+ """Maps entry points to extension defines."""
+ entrypoints_to_defines = {}
+
+ for extension in doc.findall('./extensions/extension[@protect]'):
+ define = extension.attrib['protect']
+
+ for entrypoint in extension.findall('./require/command'):
+ fullname = entrypoint.attrib['name']
+ entrypoints_to_defines[fullname] = define
+
+ return entrypoints_to_defines
+
+
+def gen_code(entrypoints):
+ """Generate the C code."""
+ i = 0
+ offsets = []
+ for _, name, _, _, _, _ in entrypoints:
+ offsets.append(i)
+ i += 2 + len(name) + 1
+
+ mapping = [NONE] * HASH_SIZE
+ collisions = [0] * 10
+ for _, name, _, num, h, _ in entrypoints:
+ level = 0
+ while mapping[h & HASH_MASK] != NONE:
+ h = h + PRIME_STEP
+ level = level + 1
+ if level > 9:
+ collisions[9] += 1
+ else:
+ collisions[level] += 1
+ mapping[h & HASH_MASK] = num
+
+ return TEMPLATE_C.render(entrypoints=entrypoints,
+ offsets=offsets,
+ collisions=collisions,
+ mapping=mapping,
+ hash_mask=HASH_MASK,
+ prime_step=PRIME_STEP,
+ prime_factor=PRIME_FACTOR,
+ none=NONE,
+ hash_size=HASH_SIZE,
+ filename=os.path.basename(__file__))
+
+
+def main():
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--outdir', help='Where to write the files.',
+ required=True)
+ parser.add_argument('--xml',
+ help='Vulkan API XML file.',
+ required=True,
+ action='append',
+ dest='xml_files')
+ args = parser.parse_args()
+
+ entrypoints = []
+
+ for filename in args.xml_files:
+ doc = et.parse(filename)
+ entrypoints += get_entrypoints(doc, get_entrypoints_defines(doc),
+ start_index=len(entrypoints))
+
+ # For outputting entrypoints.h we generate a radv_EntryPoint() prototype
+ # per entry point.
+ with open(os.path.join(args.outdir, 'radv_entrypoints.h'), 'wb') as f:
+ f.write(TEMPLATE_H.render(entrypoints=entrypoints,
+ filename=os.path.basename(__file__)))
+ with open(os.path.join(args.outdir, 'radv_entrypoints.c'), 'wb') as f:
+ f.write(gen_code(entrypoints))
+
+
+if __name__ == '__main__':
+ main()