annotate src/vulkan_api/vulkan_api_generator.nim @ 78:f67496a189cb

add: vulkan api generator, not finished yet
author Sam <sam@basx.dev>
date Mon, 20 Feb 2023 10:33:53 +0700
parents
children 031f241de6ca
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
1 import std/os
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
2 import std/sugar
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
3 import std/algorithm
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
4 import std/strformat
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
5 import std/strutils
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
6 import std/sequtils
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
7 import std/streams
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
8 import std/tables
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
9 import httpClient
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
10 import std/xmlparser
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
11 import std/xmltree
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
12
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
13 type
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
14 FileContent = seq[string]
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
15
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
16 const
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
17 TYPEMAP = {
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
18 "void": "void",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
19 "char": "char",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
20 "float": "float32",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
21 "double": "float64",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
22 "int8_t": "int8",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
23 "uint8_t": "uint8",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
24 "int16_t": "int16",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
25 "uint16_t": "uint16",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
26 "int32_t": "int32",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
27 "uint32_t": "uint32",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
28 "uint64_t": "uint64",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
29 "int64_t": "int64",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
30 "size_t": "csize_t",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
31 "int": "cint",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
32 "void*": "pointer",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
33 "char*": "cstring",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
34 "ptr char": "cstring",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
35 "ptr void": "pointer",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
36 "VK_DEFINE_HANDLE": "VkHandle",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
37 "VK_DEFINE_NON_DISPATCHABLE_HANDLE": "VkNonDispatchableHandle",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
38 }.toTable
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
39 PLATFORM_HEADER_MAP = {
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
40 "X11/Xlib.h": "xlib",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
41 "X11/extensions/Xrandr.h": "xlib_xrandr",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
42 "wayland-client.h": "wayland",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
43 "windows.h": "win32",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
44 "xcb/xcb.h": "xcb",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
45 "directfb.h": "directfb",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
46 "zircon/types.h": "fuchsia",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
47 "ggp_c/vulkan_types.h": "ggp",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
48 "screen/screen.h": "screen",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
49 "nvscisync.h": "nvidia",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
50 "nvscibuf.h": "nvidia",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
51 "vk_video/vulkan_video_codec_h264std.h": "vk_video",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
52 "vk_video/vulkan_video_codec_h264std_decode.h": "vk_video",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
53 "vk_video/vulkan_video_codec_h264std_encode.h": "vk_video",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
54 "vk_video/vulkan_video_codec_h265std.h": "vk_video",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
55 "vk_video/vulkan_video_codec_h265std_decode.h": "vk_video",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
56 "vk_video/vulkan_video_codec_h265std_encode.h": "vk_video",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
57 }.toTable
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
58 MAP_KEYWORD = {
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
59 "object": "theobject",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
60 "type": "thetype",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
61 }.toTable
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
62
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
63 # helpers
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
64 func mapType(typename: string): auto =
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
65 TYPEMAP.getOrDefault(typename.strip(), typename.strip()).strip(chars={'_'})
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
66 func mapName(thename: string): auto =
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
67 MAP_KEYWORD.getOrDefault(thename.strip(), thename.strip()).strip(chars={'_'})
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
68 func smartParseInt(value: string): int =
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
69 if value.startsWith("0x"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
70 parseHexInt(value)
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
71 else:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
72 parseInt(value)
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
73 func hasAttr(node: XmlNode, attr: string): bool = node.attr(attr) != ""
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
74 func tableSorted(table: Table[int, string]): seq[(int, string)] =
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
75 result = toSeq(table.pairs)
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
76 result.sort((a, b) => cmp(a[0], b[0]))
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
77
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
78 # serializers
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
79 func serializeEnum(node: XmlNode, root: XmlNode): seq[string] =
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
80 let name = node.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
81 if name == "":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
82 return result
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
83
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
84 # find additional enum defintion in feature definitions
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
85 var values: Table[int, string]
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
86 for feature in root.findAll("feature"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
87 for require in feature.findAll("require"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
88 for theenum in require.findAll("enum"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
89 if theenum.attr("extends") == name:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
90 if theenum.hasAttr("offset"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
91 let enumBase = 1000000000 + (smartParseInt(theenum.attr("extnumber")) - 1) * 1000
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
92 var value = smartParseInt(theenum.attr("offset")) + enumBase
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
93 if theenum.attr("dir") == "-":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
94 value = -value
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
95 values[value] = theenum.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
96 elif theenum.hasAttr("value"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
97 var value = smartParseInt(theenum.attr("value"))
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
98 if theenum.attr("dir") == "-":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
99 value = -value
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
100 values[value] = theenum.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
101 elif theenum.hasAttr("bitpos"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
102 var value = smartParseInt(theenum.attr("bitpos"))
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
103 if theenum.attr("dir") == "-":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
104 value = -value
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
105 values[value] = theenum.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
106 elif theenum.hasAttr("alias"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
107 discard
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
108 else:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
109 raise newException(Exception, &"Unknown extension value: {feature}\nvalue:{theenum}")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
110 # find additional enum defintion in extension definitions
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
111 for extension in root.findAll("extension"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
112 let extensionNumber = parseInt(extension.attr("number"))
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
113 let enumBase = 1000000000 + (extensionNumber - 1) * 1000
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
114 for require in extension.findAll("require"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
115 for theenum in require.findAll("enum"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
116 if theenum.attr("extends") == name:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
117 if theenum.hasAttr("offset"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
118 if theenum.hasAttr("extnumber"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
119 let otherBase = 1000000000 + (smartParseInt(theenum.attr("extnumber")) - 1) * 1000
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
120 var value = smartParseInt(theenum.attr("offset")) + otherBase
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
121 if theenum.attr("dir") == "-":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
122 value = -value
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
123 values[value] = theenum.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
124 else:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
125 var value = smartParseInt(theenum.attr("offset")) + enumBase
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
126 if theenum.attr("dir") == "-":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
127 value = -value
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
128 values[value] = theenum.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
129 elif theenum.hasAttr("value"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
130 var value = smartParseInt(theenum.attr("value"))
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
131 if theenum.attr("dir") == "-":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
132 value = -value
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
133 values[value] = theenum.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
134 elif theenum.hasAttr("bitpos"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
135 var value = smartParseInt(theenum.attr("bitpos"))
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
136 if theenum.attr("dir") == "-":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
137 value = -value
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
138 values[value] = theenum.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
139 elif theenum.hasAttr("alias"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
140 discard
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
141 else:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
142 raise newException(Exception, &"Unknown extension value: {extension}\nvalue:{theenum}")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
143
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
144 # generate enums
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
145 if node.attr("type") == "enum":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
146 for value in node.findAll("enum"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
147 if value.hasAttr("alias"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
148 continue
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
149 if value.attr("value").startsWith("0x"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
150 values[parseHexInt(value.attr("value"))] = value.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
151 else:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
152 values[smartParseInt(value.attr("value"))] = value.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
153 if values.len > 0:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
154 result.add " " & name & "* {.size: sizeof(cint).} = enum"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
155 for (value, name) in tableSorted(values):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
156 let enumEntry = &" {name} = {value}"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
157 result.add enumEntry
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
158
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
159 # generate bitsets (normal enums in the C API, but bitfield-enums in Nim)
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
160 elif node.attr("type") == "bitmask":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
161 for value in node.findAll("enum"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
162 if value.hasAttr("alias") or not value.hasAttr("bitpos"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
163 continue
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
164 values[smartParseInt(value.attr("bitpos"))] = value.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
165 if values.len > 0:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
166 if node.hasAttr("bitwidth"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
167 result.add " " & name & "* {.size: " & $(smartParseInt(node.attr("bitwidth")) div 8) & ".} = enum"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
168 else:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
169 result.add " " & name & "* {.size: sizeof(cint).} = enum"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
170 for (bitpos, enumvalue) in tableSorted(values):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
171 var value = "00000000000000000000000000000000"# makes the bit mask nicely visible
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
172 if node.hasAttr("bitwidth"): # assumes this is always 64
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
173 value = value & value
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
174 value[^(bitpos + 1)] = '1'
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
175 let enumEntry = &" {enumvalue} = 0b{value}"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
176 if not (enumEntry in result): # the specs define duplicate entries for backwards compat
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
177 result.add enumEntry
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
178 let cApiName = name.replace("FlagBits", "Flags")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
179 if node.hasAttr("bitwidth"): # assumes this is always 64
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
180 if values.len > 0:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
181 result.add &"""converter BitsetToNumber*(flags: openArray[{name}]): {cApiName} =
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
182 for flag in flags:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
183 result = {cApiName}(uint64(result) or uint(flag))"""
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
184 result.add "type"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
185 else:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
186 if values.len > 0:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
187 result.add &"""converter BitsetToNumber*(flags: openArray[{name}]): {cApiName} =
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
188 for flag in flags:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
189 result = {cApiName}(uint(result) or uint(flag))"""
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
190 result.add "type"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
191
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
192 func serializeStruct(node: XmlNode, root: XmlNode): seq[string] =
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
193 let name = node.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
194 var union = ""
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
195 if node.attr("category") == "union":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
196 union = "{.union.}"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
197 result.add &" {name}* {union} = object"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
198 for member in node.findAll("member"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
199 if not member.hasAttr("api") or member.attr("api") == "vulkan":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
200 let fieldname = member.child("name")[0].text.strip(chars={'_'})
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
201 var fieldtype = member.child("type")[0].text.strip(chars={'_'})
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
202 if member[member.len - 2].kind == xnText and member[member.len - 2].text.strip() == "*":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
203 fieldtype = &"ptr {mapType(fieldtype)}"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
204 fieldtype = mapType(fieldtype)
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
205 result.add &" {mapName(fieldname)}*: {fieldtype}"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
206
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
207 func serializeFunctiontypes(api: XmlNode): seq[string] =
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
208 for node in api.findAll("type"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
209 if node.attr("category") == "funcpointer":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
210 let name = node[1][0]
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
211 let returntype = mapType(node[0].text[8 .. ^1].split('(', 1)[0])
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
212 var params: seq[string]
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
213 for i in countup(3, node.len - 1, 2):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
214 var paramname = node[i + 1].text.split(',', 1)[0].split(')', 1)[0]
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
215 var paramtype = node[i][0].text
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
216 if paramname[0] == '*':
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
217 paramname = paramname.rsplit(" ", 1)[1]
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
218 paramtype = "ptr " & paramtype
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
219 paramname = mapName(paramname)
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
220 params.add &"{paramname}: {mapType(paramtype)}"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
221 let paramsstr = params.join(", ")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
222 result.add(&" {name} = proc({paramsstr}): {returntype} {{.cdecl.}}")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
223
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
224 func serializeType(node: XmlNode): Table[string, seq[string]] =
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
225 if node.attrsLen == 0:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
226 return
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
227 if node.attr("requires") == "vk_platform" or node.attr("category") == "include":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
228 return
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
229 result["basetypes"] = @[]
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
230 result["enums"] = @[]
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
231
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
232 # include-defined types (in platform headers)
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
233 if node.hasAttr("requires") and node.hasAttr("name") and node.attr("category") != "define":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
234 let platform = "platform/" & PLATFORM_HEADER_MAP[node.attr("requires")]
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
235 if not result.hasKey(platform):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
236 result[platform] = @[]
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
237 result[platform].add "type " & node.attr(
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
238 "name") & " {.header: \"" & node.attr("requires") & "\".} = object"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
239 # generic base types
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
240 elif node.attr("category") == "basetype":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
241 let typechild = node.child("type")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
242 let namechild = node.child("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
243 if typechild != nil and namechild != nil:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
244 var typename = typechild[0].text
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
245 if node[2].kind == xnText and node[2].text.strip() == "*":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
246 typename = &"ptr {typename}"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
247 result["basetypes"].add &" {namechild[0].text}* = {mapType(typename)}"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
248 elif namechild != nil:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
249 result["basetypes"].add &" {namechild[0].text}* = object"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
250 # function pointers need to be handled with structs
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
251 elif node.attr("category") == "funcpointer":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
252 discard
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
253 # preprocessor defines, ignored
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
254 elif node.attr("category") == "define":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
255 discard
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
256 # bitmask aliases
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
257 elif node.attr("category") == "bitmask":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
258 if node.hasAttr("alias"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
259 let name = node.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
260 let alias = node.attr("alias")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
261 result["enums"].add &" {name}* = {alias}"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
262 # distinct resource ID types aka handles
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
263 elif node.attr("category") == "handle":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
264 if not node.hasAttr("alias"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
265 let name = node.child("name")[0].text
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
266 var thetype = mapType(node.child("type")[0].text)
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
267 result["basetypes"].add &" {name}* = distinct {thetype}"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
268 # enum aliases
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
269 elif node.attr("category") == "enum":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
270 if node.hasAttr("alias"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
271 let name = node.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
272 let alias = node.attr("alias")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
273 result["enums"].add &" {name}* = {alias}"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
274 else:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
275 discard
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
276
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
277
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
278 proc update(a: var Table[string, seq[string]], b: Table[string, seq[string]]) =
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
279 for k, v in b.pairs:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
280 if not a.hasKey(k):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
281 a[k] = @[]
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
282 a[k].add v
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
283
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
284
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
285 proc main() =
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
286 if not os.fileExists("vk.xml"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
287 let client = newHttpClient()
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
288 let glUrl = "https://raw.githubusercontent.com/KhronosGroup/Vulkan-Docs/main/xml/vk.xml"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
289 client.downloadFile(glUrl, "vk.xml")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
290
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
291 let api = loadXml("vk.xml")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
292
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
293 const outdir = "src/vulkan_api/output"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
294 removeDir outdir
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
295 createDir outdir
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
296 createDir outdir / "platform"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
297
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
298 # index all names that are only available on certain platforms
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
299 var platformTypes: Table[string, string]
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
300 for extension in api.findAll("extension"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
301 if extension.hasAttr("platform"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
302 for thetype in extension.findAll("type"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
303 platformTypes[thetype.attr("name")] = extension.attr("platform")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
304 for command in extension.findAll("command"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
305 platformTypes[command.attr("name")] = extension.attr("platform")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
306 elif extension.attr("name").startsWith("VK_KHR_video"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
307 for thetype in extension.findAll("type"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
308 platformTypes[thetype.attr("name")] = "vk_video"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
309 for command in extension.findAll("command"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
310 platformTypes[command.attr("name")] = "vk_video"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
311
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
312 var outputFiles = {
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
313 "basetypes": @["type", " VkHandle* = distinct pointer", " VkNonDispatchableHandle* = distinct pointer"],
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
314 "structs": @["import ./enums", "import ./basetypes", "type"],
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
315 "enums": @["import ./basetypes", "type"],
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
316 }.toTable
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
317
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
318 # enums
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
319 for thetype in api.findAll("type"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
320 if thetype.attr("category") == "bitmask" and not thetype.hasAttr("alias") and (not thetype.hasAttr("api") or thetype.attr("api") == "vulkan"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
321 let name = thetype.child("name")[0].text
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
322 outputFiles["enums"].add &" {name}* = distinct VkFlags"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
323 for theenum in api.findAll("enums"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
324 outputFiles["enums"].add serializeEnum(theenum, api)
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
325
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
326 # structs and function types need to be in same "type" block to avoid forward-declarations
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
327 outputFiles["structs"].add serializeFunctiontypes(api)
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
328 for thetype in api.findAll("type"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
329 if thetype.attr("category") == "struct" or thetype.attr("category") == "union":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
330 var outfile = "structs"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
331 if thetype.attr("name") in platformTypes:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
332 outfile = "platform/" & platformTypes[thetype.attr("name")]
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
333 if not (outfile in outputFiles):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
334 outputFiles[outfile] = @[]
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
335 outputFiles[outfile].add "type"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
336 outputFiles[outfile].add serializeStruct(thetype, api)
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
337
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
338 # types
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
339 for typesgroup in api.findAll("types"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
340 for thetype in typesgroup.findAll("type"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
341 outputFiles.update serializeType(thetype)
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
342 for filename, filecontent in outputFiles.pairs:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
343 writeFile outdir / &"{filename}.nim", filecontent.join("\n")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
344
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
345 when isMainModule:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
346 main()