comparison src/vulkan_api/vulkan_api_generator.nim @ 539:9400c1cf26a5

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