Home | History | Annotate | Download | only in smoke
      1 #!/usr/bin/env python3
      2 #
      3 # Copyright (C) 2016 Google, Inc.
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License");
      6 # you may not use this file except in compliance with the License.
      7 # You may obtain a copy of the License at
      8 #
      9 #     http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 # Unless required by applicable law or agreed to in writing, software
     12 # distributed under the License is distributed on an "AS IS" BASIS,
     13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 # See the License for the specific language governing permissions and
     15 # limitations under the License.
     16 
     17 
     18 """Generate Vulkan dispatch table.
     19 """
     20 
     21 import os
     22 import sys
     23 
     24 class Command(object):
     25     PLATFORM = 0
     26     LOADER = 1
     27     INSTANCE = 2
     28     DEVICE = 3
     29 
     30     def __init__(self, name, dispatch):
     31         self.name = name
     32         self.dispatch = dispatch
     33         self.ty = self._get_type()
     34 
     35     @staticmethod
     36     def valid_c_typedef(c):
     37         return (c.startswith("typedef") and
     38                 c.endswith(");") and
     39                 "*PFN_vkVoidFunction" not in c)
     40 
     41     @classmethod
     42     def from_c_typedef(cls, c):
     43         name_begin = c.find("*PFN_vk") + 7
     44         name_end = c.find(")(", name_begin)
     45         name = c[name_begin:name_end]
     46 
     47         dispatch_begin = name_end + 2
     48         dispatch_end = c.find(" ", dispatch_begin)
     49         dispatch = c[dispatch_begin:dispatch_end]
     50         if not dispatch.startswith("Vk"):
     51             dispatch = None
     52 
     53         return cls(name, dispatch)
     54 
     55     def _get_type(self):
     56         if self.dispatch:
     57             if self.dispatch in ["VkDevice", "VkQueue", "VkCommandBuffer"]:
     58                 return self.DEVICE
     59             else:
     60                 return self.INSTANCE
     61         else:
     62             if self.name in ["GetInstanceProcAddr"]:
     63                 return self.PLATFORM
     64             else:
     65                 return self.LOADER
     66 
     67     def __repr__(self):
     68         return "Command(name=%s, dispatch=%s)" % \
     69                 (repr(self.name), repr(self.dispatch))
     70 
     71 class Extension(object):
     72     def __init__(self, name, version, guard=None, commands=[]):
     73         self.name = name
     74         self.version = version
     75         self.guard = guard
     76         self.commands = commands[:]
     77 
     78     def add_command(self, cmd):
     79         self.commands.append(cmd)
     80 
     81     def __repr__(self):
     82         lines = []
     83         lines.append("Extension(name=%s, version=%s, guard=%s, commands=[" %
     84                 (repr(self.name), repr(self.version), repr(self.guard)))
     85 
     86         for cmd in self.commands:
     87             lines.append("    %s," % repr(cmd))
     88 
     89         lines.append("])")
     90 
     91         return "\n".join(lines)
     92 
     93 # generated by "generate-dispatch-table.py parse vulkan.h"
     94 vk_core = Extension(name='VK_core', version=0, guard=None, commands=[
     95     Command(name='CreateInstance', dispatch=None),
     96     Command(name='DestroyInstance', dispatch='VkInstance'),
     97     Command(name='EnumeratePhysicalDevices', dispatch='VkInstance'),
     98     Command(name='GetPhysicalDeviceFeatures', dispatch='VkPhysicalDevice'),
     99     Command(name='GetPhysicalDeviceFormatProperties', dispatch='VkPhysicalDevice'),
    100     Command(name='GetPhysicalDeviceImageFormatProperties', dispatch='VkPhysicalDevice'),
    101     Command(name='GetPhysicalDeviceProperties', dispatch='VkPhysicalDevice'),
    102     Command(name='GetPhysicalDeviceQueueFamilyProperties', dispatch='VkPhysicalDevice'),
    103     Command(name='GetPhysicalDeviceMemoryProperties', dispatch='VkPhysicalDevice'),
    104     Command(name='GetInstanceProcAddr', dispatch='VkInstance'),
    105     Command(name='GetDeviceProcAddr', dispatch='VkDevice'),
    106     Command(name='CreateDevice', dispatch='VkPhysicalDevice'),
    107     Command(name='DestroyDevice', dispatch='VkDevice'),
    108     Command(name='EnumerateInstanceExtensionProperties', dispatch=None),
    109     Command(name='EnumerateDeviceExtensionProperties', dispatch='VkPhysicalDevice'),
    110     Command(name='EnumerateInstanceLayerProperties', dispatch=None),
    111     Command(name='GetDeviceQueue', dispatch='VkDevice'),
    112     Command(name='QueueSubmit', dispatch='VkQueue'),
    113     Command(name='QueueWaitIdle', dispatch='VkQueue'),
    114     Command(name='DeviceWaitIdle', dispatch='VkDevice'),
    115     Command(name='AllocateMemory', dispatch='VkDevice'),
    116     Command(name='FreeMemory', dispatch='VkDevice'),
    117     Command(name='MapMemory', dispatch='VkDevice'),
    118     Command(name='UnmapMemory', dispatch='VkDevice'),
    119     Command(name='FlushMappedMemoryRanges', dispatch='VkDevice'),
    120     Command(name='InvalidateMappedMemoryRanges', dispatch='VkDevice'),
    121     Command(name='GetDeviceMemoryCommitment', dispatch='VkDevice'),
    122     Command(name='BindBufferMemory', dispatch='VkDevice'),
    123     Command(name='BindImageMemory', dispatch='VkDevice'),
    124     Command(name='GetBufferMemoryRequirements', dispatch='VkDevice'),
    125     Command(name='GetImageMemoryRequirements', dispatch='VkDevice'),
    126     Command(name='GetImageSparseMemoryRequirements', dispatch='VkDevice'),
    127     Command(name='GetPhysicalDeviceSparseImageFormatProperties', dispatch='VkPhysicalDevice'),
    128     Command(name='QueueBindSparse', dispatch='VkQueue'),
    129     Command(name='CreateFence', dispatch='VkDevice'),
    130     Command(name='DestroyFence', dispatch='VkDevice'),
    131     Command(name='ResetFences', dispatch='VkDevice'),
    132     Command(name='GetFenceStatus', dispatch='VkDevice'),
    133     Command(name='WaitForFences', dispatch='VkDevice'),
    134     Command(name='CreateSemaphore', dispatch='VkDevice'),
    135     Command(name='DestroySemaphore', dispatch='VkDevice'),
    136     Command(name='CreateEvent', dispatch='VkDevice'),
    137     Command(name='DestroyEvent', dispatch='VkDevice'),
    138     Command(name='GetEventStatus', dispatch='VkDevice'),
    139     Command(name='SetEvent', dispatch='VkDevice'),
    140     Command(name='ResetEvent', dispatch='VkDevice'),
    141     Command(name='CreateQueryPool', dispatch='VkDevice'),
    142     Command(name='DestroyQueryPool', dispatch='VkDevice'),
    143     Command(name='GetQueryPoolResults', dispatch='VkDevice'),
    144     Command(name='CreateBuffer', dispatch='VkDevice'),
    145     Command(name='DestroyBuffer', dispatch='VkDevice'),
    146     Command(name='CreateBufferView', dispatch='VkDevice'),
    147     Command(name='DestroyBufferView', dispatch='VkDevice'),
    148     Command(name='CreateImage', dispatch='VkDevice'),
    149     Command(name='DestroyImage', dispatch='VkDevice'),
    150     Command(name='GetImageSubresourceLayout', dispatch='VkDevice'),
    151     Command(name='CreateImageView', dispatch='VkDevice'),
    152     Command(name='DestroyImageView', dispatch='VkDevice'),
    153     Command(name='CreateShaderModule', dispatch='VkDevice'),
    154     Command(name='DestroyShaderModule', dispatch='VkDevice'),
    155     Command(name='CreatePipelineCache', dispatch='VkDevice'),
    156     Command(name='DestroyPipelineCache', dispatch='VkDevice'),
    157     Command(name='GetPipelineCacheData', dispatch='VkDevice'),
    158     Command(name='MergePipelineCaches', dispatch='VkDevice'),
    159     Command(name='CreateGraphicsPipelines', dispatch='VkDevice'),
    160     Command(name='CreateComputePipelines', dispatch='VkDevice'),
    161     Command(name='DestroyPipeline', dispatch='VkDevice'),
    162     Command(name='CreatePipelineLayout', dispatch='VkDevice'),
    163     Command(name='DestroyPipelineLayout', dispatch='VkDevice'),
    164     Command(name='CreateSampler', dispatch='VkDevice'),
    165     Command(name='DestroySampler', dispatch='VkDevice'),
    166     Command(name='CreateDescriptorSetLayout', dispatch='VkDevice'),
    167     Command(name='DestroyDescriptorSetLayout', dispatch='VkDevice'),
    168     Command(name='CreateDescriptorPool', dispatch='VkDevice'),
    169     Command(name='DestroyDescriptorPool', dispatch='VkDevice'),
    170     Command(name='ResetDescriptorPool', dispatch='VkDevice'),
    171     Command(name='AllocateDescriptorSets', dispatch='VkDevice'),
    172     Command(name='FreeDescriptorSets', dispatch='VkDevice'),
    173     Command(name='UpdateDescriptorSets', dispatch='VkDevice'),
    174     Command(name='CreateFramebuffer', dispatch='VkDevice'),
    175     Command(name='DestroyFramebuffer', dispatch='VkDevice'),
    176     Command(name='CreateRenderPass', dispatch='VkDevice'),
    177     Command(name='DestroyRenderPass', dispatch='VkDevice'),
    178     Command(name='GetRenderAreaGranularity', dispatch='VkDevice'),
    179     Command(name='CreateCommandPool', dispatch='VkDevice'),
    180     Command(name='DestroyCommandPool', dispatch='VkDevice'),
    181     Command(name='ResetCommandPool', dispatch='VkDevice'),
    182     Command(name='AllocateCommandBuffers', dispatch='VkDevice'),
    183     Command(name='FreeCommandBuffers', dispatch='VkDevice'),
    184     Command(name='BeginCommandBuffer', dispatch='VkCommandBuffer'),
    185     Command(name='EndCommandBuffer', dispatch='VkCommandBuffer'),
    186     Command(name='ResetCommandBuffer', dispatch='VkCommandBuffer'),
    187     Command(name='CmdBindPipeline', dispatch='VkCommandBuffer'),
    188     Command(name='CmdSetViewport', dispatch='VkCommandBuffer'),
    189     Command(name='CmdSetScissor', dispatch='VkCommandBuffer'),
    190     Command(name='CmdSetLineWidth', dispatch='VkCommandBuffer'),
    191     Command(name='CmdSetDepthBias', dispatch='VkCommandBuffer'),
    192     Command(name='CmdSetBlendConstants', dispatch='VkCommandBuffer'),
    193     Command(name='CmdSetDepthBounds', dispatch='VkCommandBuffer'),
    194     Command(name='CmdSetStencilCompareMask', dispatch='VkCommandBuffer'),
    195     Command(name='CmdSetStencilWriteMask', dispatch='VkCommandBuffer'),
    196     Command(name='CmdSetStencilReference', dispatch='VkCommandBuffer'),
    197     Command(name='CmdBindDescriptorSets', dispatch='VkCommandBuffer'),
    198     Command(name='CmdBindIndexBuffer', dispatch='VkCommandBuffer'),
    199     Command(name='CmdBindVertexBuffers', dispatch='VkCommandBuffer'),
    200     Command(name='CmdDraw', dispatch='VkCommandBuffer'),
    201     Command(name='CmdDrawIndexed', dispatch='VkCommandBuffer'),
    202     Command(name='CmdDrawIndirect', dispatch='VkCommandBuffer'),
    203     Command(name='CmdDrawIndexedIndirect', dispatch='VkCommandBuffer'),
    204     Command(name='CmdDispatch', dispatch='VkCommandBuffer'),
    205     Command(name='CmdDispatchIndirect', dispatch='VkCommandBuffer'),
    206     Command(name='CmdCopyBuffer', dispatch='VkCommandBuffer'),
    207     Command(name='CmdCopyImage', dispatch='VkCommandBuffer'),
    208     Command(name='CmdBlitImage', dispatch='VkCommandBuffer'),
    209     Command(name='CmdCopyBufferToImage', dispatch='VkCommandBuffer'),
    210     Command(name='CmdCopyImageToBuffer', dispatch='VkCommandBuffer'),
    211     Command(name='CmdUpdateBuffer', dispatch='VkCommandBuffer'),
    212     Command(name='CmdFillBuffer', dispatch='VkCommandBuffer'),
    213     Command(name='CmdClearColorImage', dispatch='VkCommandBuffer'),
    214     Command(name='CmdClearDepthStencilImage', dispatch='VkCommandBuffer'),
    215     Command(name='CmdClearAttachments', dispatch='VkCommandBuffer'),
    216     Command(name='CmdResolveImage', dispatch='VkCommandBuffer'),
    217     Command(name='CmdSetEvent', dispatch='VkCommandBuffer'),
    218     Command(name='CmdResetEvent', dispatch='VkCommandBuffer'),
    219     Command(name='CmdWaitEvents', dispatch='VkCommandBuffer'),
    220     Command(name='CmdPipelineBarrier', dispatch='VkCommandBuffer'),
    221     Command(name='CmdBeginQuery', dispatch='VkCommandBuffer'),
    222     Command(name='CmdEndQuery', dispatch='VkCommandBuffer'),
    223     Command(name='CmdResetQueryPool', dispatch='VkCommandBuffer'),
    224     Command(name='CmdWriteTimestamp', dispatch='VkCommandBuffer'),
    225     Command(name='CmdCopyQueryPoolResults', dispatch='VkCommandBuffer'),
    226     Command(name='CmdPushConstants', dispatch='VkCommandBuffer'),
    227     Command(name='CmdBeginRenderPass', dispatch='VkCommandBuffer'),
    228     Command(name='CmdNextSubpass', dispatch='VkCommandBuffer'),
    229     Command(name='CmdEndRenderPass', dispatch='VkCommandBuffer'),
    230     Command(name='CmdExecuteCommands', dispatch='VkCommandBuffer'),
    231 ])
    232 
    233 vk_khr_surface = Extension(name='VK_KHR_surface', version=25, guard=None, commands=[
    234     Command(name='DestroySurfaceKHR', dispatch='VkInstance'),
    235     Command(name='GetPhysicalDeviceSurfaceSupportKHR', dispatch='VkPhysicalDevice'),
    236     Command(name='GetPhysicalDeviceSurfaceCapabilitiesKHR', dispatch='VkPhysicalDevice'),
    237     Command(name='GetPhysicalDeviceSurfaceFormatsKHR', dispatch='VkPhysicalDevice'),
    238     Command(name='GetPhysicalDeviceSurfacePresentModesKHR', dispatch='VkPhysicalDevice'),
    239 ])
    240 
    241 vk_khr_swapchain = Extension(name='VK_KHR_swapchain', version=67, guard=None, commands=[
    242     Command(name='CreateSwapchainKHR', dispatch='VkDevice'),
    243     Command(name='DestroySwapchainKHR', dispatch='VkDevice'),
    244     Command(name='GetSwapchainImagesKHR', dispatch='VkDevice'),
    245     Command(name='AcquireNextImageKHR', dispatch='VkDevice'),
    246     Command(name='QueuePresentKHR', dispatch='VkQueue'),
    247 ])
    248 
    249 vk_khr_display = Extension(name='VK_KHR_display', version=21, guard=None, commands=[
    250     Command(name='GetPhysicalDeviceDisplayPropertiesKHR', dispatch='VkPhysicalDevice'),
    251     Command(name='GetPhysicalDeviceDisplayPlanePropertiesKHR', dispatch='VkPhysicalDevice'),
    252     Command(name='GetDisplayPlaneSupportedDisplaysKHR', dispatch='VkPhysicalDevice'),
    253     Command(name='GetDisplayModePropertiesKHR', dispatch='VkPhysicalDevice'),
    254     Command(name='CreateDisplayModeKHR', dispatch='VkPhysicalDevice'),
    255     Command(name='GetDisplayPlaneCapabilitiesKHR', dispatch='VkPhysicalDevice'),
    256     Command(name='CreateDisplayPlaneSurfaceKHR', dispatch='VkInstance'),
    257 ])
    258 
    259 vk_khr_display_swapchain = Extension(name='VK_KHR_display_swapchain', version=9, guard=None, commands=[
    260     Command(name='CreateSharedSwapchainsKHR', dispatch='VkDevice'),
    261 ])
    262 
    263 vk_khr_xlib_surface = Extension(name='VK_KHR_xlib_surface', version=6, guard='VK_USE_PLATFORM_XLIB_KHR', commands=[
    264     Command(name='CreateXlibSurfaceKHR', dispatch='VkInstance'),
    265     Command(name='GetPhysicalDeviceXlibPresentationSupportKHR', dispatch='VkPhysicalDevice'),
    266 ])
    267 
    268 vk_khr_xcb_surface = Extension(name='VK_KHR_xcb_surface', version=6, guard='VK_USE_PLATFORM_XCB_KHR', commands=[
    269     Command(name='CreateXcbSurfaceKHR', dispatch='VkInstance'),
    270     Command(name='GetPhysicalDeviceXcbPresentationSupportKHR', dispatch='VkPhysicalDevice'),
    271 ])
    272 
    273 vk_khr_wayland_surface = Extension(name='VK_KHR_wayland_surface', version=5, guard='VK_USE_PLATFORM_WAYLAND_KHR', commands=[
    274     Command(name='CreateWaylandSurfaceKHR', dispatch='VkInstance'),
    275     Command(name='GetPhysicalDeviceWaylandPresentationSupportKHR', dispatch='VkPhysicalDevice'),
    276 ])
    277 
    278 vk_khr_mir_surface = Extension(name='VK_KHR_mir_surface', version=4, guard='VK_USE_PLATFORM_MIR_KHR', commands=[
    279     Command(name='CreateMirSurfaceKHR', dispatch='VkInstance'),
    280     Command(name='GetPhysicalDeviceMirPresentationSupportKHR', dispatch='VkPhysicalDevice'),
    281 ])
    282 
    283 vk_khr_android_surface = Extension(name='VK_KHR_android_surface', version=6, guard='VK_USE_PLATFORM_ANDROID_KHR', commands=[
    284     Command(name='CreateAndroidSurfaceKHR', dispatch='VkInstance'),
    285 ])
    286 
    287 vk_khr_win32_surface = Extension(name='VK_KHR_win32_surface', version=5, guard='VK_USE_PLATFORM_WIN32_KHR', commands=[
    288     Command(name='CreateWin32SurfaceKHR', dispatch='VkInstance'),
    289     Command(name='GetPhysicalDeviceWin32PresentationSupportKHR', dispatch='VkPhysicalDevice'),
    290 ])
    291 
    292 vk_ext_debug_report = Extension(name='VK_EXT_debug_report', version=1, guard=None, commands=[
    293     Command(name='CreateDebugReportCallbackEXT', dispatch='VkInstance'),
    294     Command(name='DestroyDebugReportCallbackEXT', dispatch='VkInstance'),
    295     Command(name='DebugReportMessageEXT', dispatch='VkInstance'),
    296 ])
    297 
    298 extensions = [
    299     vk_core,
    300     vk_khr_surface,
    301     vk_khr_swapchain,
    302     vk_khr_display,
    303     vk_khr_display_swapchain,
    304     vk_khr_xlib_surface,
    305     vk_khr_xcb_surface,
    306     vk_khr_wayland_surface,
    307     vk_khr_mir_surface,
    308     vk_khr_android_surface,
    309     vk_khr_win32_surface,
    310     vk_ext_debug_report,
    311 ]
    312 
    313 def generate_header(guard):
    314     lines = []
    315     lines.append("// This file is generated.")
    316     lines.append("#ifndef %s" % guard)
    317     lines.append("#define %s" % guard)
    318     lines.append("")
    319     lines.append("#include <vulkan/vulkan.h>")
    320     lines.append("")
    321     lines.append("namespace vk {")
    322     lines.append("")
    323 
    324     for ext in extensions:
    325         if ext.guard:
    326             lines.append("#ifdef %s" % ext.guard)
    327 
    328         lines.append("// %s" % ext.name)
    329         for cmd in ext.commands:
    330             lines.append("extern PFN_vk%s %s;" % (cmd.name, cmd.name))
    331 
    332         if ext.guard:
    333             lines.append("#endif")
    334         lines.append("")
    335 
    336     lines.append("void init_dispatch_table_top(PFN_vkGetInstanceProcAddr get_instance_proc_addr);")
    337     lines.append("void init_dispatch_table_middle(VkInstance instance, bool include_bottom);")
    338     lines.append("void init_dispatch_table_bottom(VkInstance instance, VkDevice dev);")
    339     lines.append("")
    340     lines.append("} // namespace vk")
    341     lines.append("")
    342     lines.append("#endif // %s" % guard)
    343 
    344     return "\n".join(lines)
    345 
    346 def get_proc_addr(dispatchable, cmd, guard=None):
    347     if dispatchable == "dev":
    348         func = "GetDeviceProcAddr"
    349     else:
    350         func = "GetInstanceProcAddr"
    351 
    352     c = "    %s = reinterpret_cast<PFN_vk%s>(%s(%s, \"vk%s\"));" % \
    353             (cmd.name, cmd.name, func, dispatchable, cmd.name)
    354 
    355     if guard:
    356         c = ("#ifdef %s\n" % guard) + c + "\n#endif"
    357 
    358     return c
    359 
    360 def generate_source(header):
    361     lines = []
    362     lines.append("// This file is generated.")
    363     lines.append("#include \"%s\"" % header)
    364     lines.append("")
    365     lines.append("namespace vk {")
    366     lines.append("")
    367 
    368     commands_by_types = {}
    369     get_instance_proc_addr = None
    370     get_device_proc_addr = None
    371     for ext in extensions:
    372         if ext.guard:
    373             lines.append("#ifdef %s" % ext.guard)
    374 
    375         for cmd in ext.commands:
    376             lines.append("PFN_vk%s %s;" % (cmd.name, cmd.name))
    377 
    378             if cmd.ty not in commands_by_types:
    379                 commands_by_types[cmd.ty] = []
    380             commands_by_types[cmd.ty].append([cmd, ext.guard])
    381 
    382             if cmd.name == "GetInstanceProcAddr":
    383                 get_instance_proc_addr = cmd
    384             elif cmd.name == "GetDeviceProcAddr":
    385                 get_device_proc_addr = cmd
    386 
    387         if ext.guard:
    388             lines.append("#endif")
    389     lines.append("")
    390 
    391     lines.append("void init_dispatch_table_top(PFN_vkGetInstanceProcAddr get_instance_proc_addr)")
    392     lines.append("{")
    393     lines.append("    GetInstanceProcAddr = get_instance_proc_addr;")
    394     lines.append("")
    395     for cmd, guard in commands_by_types[Command.LOADER]:
    396         lines.append(get_proc_addr("VK_NULL_HANDLE", cmd, guard))
    397     lines.append("}")
    398     lines.append("")
    399 
    400     lines.append("void init_dispatch_table_middle(VkInstance instance, bool include_bottom)")
    401     lines.append("{")
    402     lines.append(get_proc_addr("instance", get_instance_proc_addr))
    403     lines.append("")
    404     for cmd, guard in commands_by_types[Command.INSTANCE]:
    405         if cmd == get_instance_proc_addr:
    406             continue
    407         lines.append(get_proc_addr("instance", cmd, guard))
    408     lines.append("")
    409     lines.append("    if (!include_bottom)")
    410     lines.append("        return;")
    411     lines.append("")
    412     for cmd, guard in commands_by_types[Command.DEVICE]:
    413         lines.append(get_proc_addr("instance", cmd, guard))
    414     lines.append("}")
    415     lines.append("")
    416 
    417     lines.append("void init_dispatch_table_bottom(VkInstance instance, VkDevice dev)")
    418     lines.append("{")
    419     lines.append(get_proc_addr("instance", get_device_proc_addr))
    420     lines.append(get_proc_addr("dev", get_device_proc_addr))
    421     lines.append("")
    422     for cmd, guard in commands_by_types[Command.DEVICE]:
    423         if cmd == get_device_proc_addr:
    424             continue
    425         lines.append(get_proc_addr("dev", cmd, guard))
    426     lines.append("}")
    427 
    428     lines.append("")
    429     lines.append("} // namespace vk")
    430 
    431     return "\n".join(lines)
    432 
    433 def parse_vulkan_h(filename):
    434     extensions = []
    435 
    436     with open(filename, "r") as f:
    437         current_ext = None
    438         ext_guard = None
    439         spec_version = None
    440 
    441         for line in f:
    442             line = line.strip();
    443 
    444             if line.startswith("#define VK_API_VERSION"):
    445                 minor_end = line.rfind(",")
    446                 minor_begin = line.rfind(",", 0, minor_end) + 1
    447                 spec_version = int(line[minor_begin:minor_end])
    448                 # add core
    449                 current_ext = Extension("VK_core", spec_version)
    450                 extensions.append(current_ext)
    451             elif Command.valid_c_typedef(line):
    452                 current_ext.add_command(Command.from_c_typedef(line))
    453             elif line.startswith("#ifdef VK_USE_PLATFORM"):
    454                 guard_begin = line.find(" ") + 1
    455                 ext_guard = line[guard_begin:]
    456             elif line.startswith("#define") and "SPEC_VERSION " in line:
    457                 version_begin = line.rfind(" ") + 1
    458                 spec_version = int(line[version_begin:])
    459             elif line.startswith("#define") and "EXTENSION_NAME " in line:
    460                 name_end = line.rfind("\"")
    461                 name_begin = line.rfind("\"", 0, name_end) + 1
    462                 name = line[name_begin:name_end]
    463                 # add extension
    464                 current_ext = Extension(name, spec_version, ext_guard)
    465                 extensions.append(current_ext)
    466             elif ext_guard and line.startswith("#endif") and ext_guard in line:
    467                 ext_guard = None
    468 
    469     for ext in extensions:
    470         print("%s = %s" % (ext.name.lower(), repr(ext)))
    471         print("")
    472 
    473     print("extensions = [")
    474     for ext in extensions:
    475         print("    %s," % ext.name.lower())
    476     print("]")
    477 
    478 if __name__ == "__main__":
    479     if sys.argv[1] == "parse":
    480         parse_vulkan_h(sys.argv[2])
    481     else:
    482         filename = sys.argv[1]
    483         base = os.path.basename(filename)
    484         contents = []
    485 
    486         if base.endswith(".h"):
    487             contents = generate_header(base.replace(".", "_").upper())
    488         elif base.endswith(".cpp"):
    489             contents = generate_source(base.replace(".cpp", ".h"))
    490 
    491         with open(filename, "w") as f:
    492             print(contents, file=f)
    493