78
|
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 = {
|
79
|
40 "X11/Xlib.h": @["xlib", "xlib_xrandr"],
|
|
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": @["sci"],
|
|
50 "nvscibuf.h": @["sci"],
|
|
51 "vk_video/vulkan_video_codec_h264std.h": @["provisional"],
|
|
52 "vk_video/vulkan_video_codec_h264std_decode.h": @["provisional"],
|
|
53 "vk_video/vulkan_video_codec_h264std_encode.h": @["provisional"],
|
|
54 "vk_video/vulkan_video_codec_h265std.h": @["provisional"],
|
|
55 "vk_video/vulkan_video_codec_h265std_decode.h": @["provisional"],
|
|
56 "vk_video/vulkan_video_codec_h265std_encode.h": @["provisional"],
|
78
|
57 }.toTable
|
|
58 MAP_KEYWORD = {
|
|
59 "object": "theobject",
|
|
60 "type": "thetype",
|
|
61 }.toTable
|
79
|
62 SPECIAL_DEPENDENCIES = {
|
|
63 "VK_NV_ray_tracing": "VK_KHR_ray_tracing_pipeline",
|
|
64 }.toTable
|
78
|
65
|
|
66 # helpers
|
|
67 func mapType(typename: string): auto =
|
|
68 TYPEMAP.getOrDefault(typename.strip(), typename.strip()).strip(chars={'_'})
|
|
69 func mapName(thename: string): auto =
|
|
70 MAP_KEYWORD.getOrDefault(thename.strip(), thename.strip()).strip(chars={'_'})
|
|
71 func smartParseInt(value: string): int =
|
|
72 if value.startsWith("0x"):
|
|
73 parseHexInt(value)
|
|
74 else:
|
|
75 parseInt(value)
|
|
76 func hasAttr(node: XmlNode, attr: string): bool = node.attr(attr) != ""
|
|
77 func tableSorted(table: Table[int, string]): seq[(int, string)] =
|
|
78 result = toSeq(table.pairs)
|
|
79 result.sort((a, b) => cmp(a[0], b[0]))
|
|
80
|
|
81 # serializers
|
|
82 func serializeEnum(node: XmlNode, root: XmlNode): seq[string] =
|
|
83 let name = node.attr("name")
|
|
84 if name == "":
|
|
85 return result
|
|
86
|
|
87 # find additional enum defintion in feature definitions
|
|
88 var values: Table[int, string]
|
|
89 for feature in root.findAll("feature"):
|
|
90 for require in feature.findAll("require"):
|
|
91 for theenum in require.findAll("enum"):
|
|
92 if theenum.attr("extends") == name:
|
|
93 if theenum.hasAttr("offset"):
|
|
94 let enumBase = 1000000000 + (smartParseInt(theenum.attr("extnumber")) - 1) * 1000
|
|
95 var value = smartParseInt(theenum.attr("offset")) + enumBase
|
|
96 if theenum.attr("dir") == "-":
|
|
97 value = -value
|
|
98 values[value] = theenum.attr("name")
|
|
99 elif theenum.hasAttr("value"):
|
|
100 var value = smartParseInt(theenum.attr("value"))
|
|
101 if theenum.attr("dir") == "-":
|
|
102 value = -value
|
|
103 values[value] = theenum.attr("name")
|
|
104 elif theenum.hasAttr("bitpos"):
|
|
105 var value = smartParseInt(theenum.attr("bitpos"))
|
|
106 if theenum.attr("dir") == "-":
|
|
107 value = -value
|
|
108 values[value] = theenum.attr("name")
|
|
109 elif theenum.hasAttr("alias"):
|
|
110 discard
|
|
111 else:
|
|
112 raise newException(Exception, &"Unknown extension value: {feature}\nvalue:{theenum}")
|
|
113 # find additional enum defintion in extension definitions
|
|
114 for extension in root.findAll("extension"):
|
|
115 let extensionNumber = parseInt(extension.attr("number"))
|
|
116 let enumBase = 1000000000 + (extensionNumber - 1) * 1000
|
|
117 for require in extension.findAll("require"):
|
|
118 for theenum in require.findAll("enum"):
|
|
119 if theenum.attr("extends") == name:
|
|
120 if theenum.hasAttr("offset"):
|
|
121 if theenum.hasAttr("extnumber"):
|
|
122 let otherBase = 1000000000 + (smartParseInt(theenum.attr("extnumber")) - 1) * 1000
|
|
123 var value = smartParseInt(theenum.attr("offset")) + otherBase
|
|
124 if theenum.attr("dir") == "-":
|
|
125 value = -value
|
|
126 values[value] = theenum.attr("name")
|
|
127 else:
|
|
128 var value = smartParseInt(theenum.attr("offset")) + enumBase
|
|
129 if theenum.attr("dir") == "-":
|
|
130 value = -value
|
|
131 values[value] = theenum.attr("name")
|
|
132 elif theenum.hasAttr("value"):
|
|
133 var value = smartParseInt(theenum.attr("value"))
|
|
134 if theenum.attr("dir") == "-":
|
|
135 value = -value
|
|
136 values[value] = theenum.attr("name")
|
|
137 elif theenum.hasAttr("bitpos"):
|
|
138 var value = smartParseInt(theenum.attr("bitpos"))
|
|
139 if theenum.attr("dir") == "-":
|
|
140 value = -value
|
|
141 values[value] = theenum.attr("name")
|
|
142 elif theenum.hasAttr("alias"):
|
|
143 discard
|
|
144 else:
|
|
145 raise newException(Exception, &"Unknown extension value: {extension}\nvalue:{theenum}")
|
|
146
|
|
147 # generate enums
|
|
148 if node.attr("type") == "enum":
|
|
149 for value in node.findAll("enum"):
|
|
150 if value.hasAttr("alias"):
|
|
151 continue
|
|
152 if value.attr("value").startsWith("0x"):
|
|
153 values[parseHexInt(value.attr("value"))] = value.attr("name")
|
|
154 else:
|
|
155 values[smartParseInt(value.attr("value"))] = value.attr("name")
|
|
156 if values.len > 0:
|
|
157 result.add " " & name & "* {.size: sizeof(cint).} = enum"
|
|
158 for (value, name) in tableSorted(values):
|
|
159 let enumEntry = &" {name} = {value}"
|
|
160 result.add enumEntry
|
|
161
|
|
162 # generate bitsets (normal enums in the C API, but bitfield-enums in Nim)
|
|
163 elif node.attr("type") == "bitmask":
|
|
164 for value in node.findAll("enum"):
|
79
|
165 if value.hasAttr("bitpos"):
|
|
166 values[smartParseInt(value.attr("bitpos"))] = value.attr("name")
|
|
167 elif node.attr("name") == "VkVideoEncodeRateControlModeFlagBitsKHR": # special exception, for some reason this has values instead of bitpos
|
|
168 values[smartParseInt(value.attr("value"))] = value.attr("name")
|
78
|
169 if values.len > 0:
|
|
170 if node.hasAttr("bitwidth"):
|
|
171 result.add " " & name & "* {.size: " & $(smartParseInt(node.attr("bitwidth")) div 8) & ".} = enum"
|
|
172 else:
|
|
173 result.add " " & name & "* {.size: sizeof(cint).} = enum"
|
|
174 for (bitpos, enumvalue) in tableSorted(values):
|
|
175 var value = "00000000000000000000000000000000"# makes the bit mask nicely visible
|
|
176 if node.hasAttr("bitwidth"): # assumes this is always 64
|
|
177 value = value & value
|
|
178 value[^(bitpos + 1)] = '1'
|
|
179 let enumEntry = &" {enumvalue} = 0b{value}"
|
|
180 if not (enumEntry in result): # the specs define duplicate entries for backwards compat
|
|
181 result.add enumEntry
|
|
182 let cApiName = name.replace("FlagBits", "Flags")
|
|
183 if node.hasAttr("bitwidth"): # assumes this is always 64
|
|
184 if values.len > 0:
|
|
185 result.add &"""converter BitsetToNumber*(flags: openArray[{name}]): {cApiName} =
|
|
186 for flag in flags:
|
|
187 result = {cApiName}(uint64(result) or uint(flag))"""
|
|
188 result.add "type"
|
|
189 else:
|
|
190 if values.len > 0:
|
|
191 result.add &"""converter BitsetToNumber*(flags: openArray[{name}]): {cApiName} =
|
|
192 for flag in flags:
|
|
193 result = {cApiName}(uint(result) or uint(flag))"""
|
|
194 result.add "type"
|
|
195
|
|
196 func serializeStruct(node: XmlNode, root: XmlNode): seq[string] =
|
|
197 let name = node.attr("name")
|
|
198 var union = ""
|
|
199 if node.attr("category") == "union":
|
79
|
200 union = "{.union.} "
|
|
201 result.add &" {name}* {union}= object"
|
78
|
202 for member in node.findAll("member"):
|
|
203 if not member.hasAttr("api") or member.attr("api") == "vulkan":
|
|
204 let fieldname = member.child("name")[0].text.strip(chars={'_'})
|
|
205 var fieldtype = member.child("type")[0].text.strip(chars={'_'})
|
|
206 if member[member.len - 2].kind == xnText and member[member.len - 2].text.strip() == "*":
|
|
207 fieldtype = &"ptr {mapType(fieldtype)}"
|
|
208 fieldtype = mapType(fieldtype)
|
|
209 result.add &" {mapName(fieldname)}*: {fieldtype}"
|
|
210
|
|
211 func serializeFunctiontypes(api: XmlNode): seq[string] =
|
|
212 for node in api.findAll("type"):
|
|
213 if node.attr("category") == "funcpointer":
|
|
214 let name = node[1][0]
|
|
215 let returntype = mapType(node[0].text[8 .. ^1].split('(', 1)[0])
|
|
216 var params: seq[string]
|
|
217 for i in countup(3, node.len - 1, 2):
|
|
218 var paramname = node[i + 1].text.split(',', 1)[0].split(')', 1)[0]
|
|
219 var paramtype = node[i][0].text
|
|
220 if paramname[0] == '*':
|
|
221 paramname = paramname.rsplit(" ", 1)[1]
|
|
222 paramtype = "ptr " & paramtype
|
|
223 paramname = mapName(paramname)
|
|
224 params.add &"{paramname}: {mapType(paramtype)}"
|
|
225 let paramsstr = params.join(", ")
|
79
|
226 result.add(&" {name}* = proc({paramsstr}): {returntype} {{.cdecl.}}")
|
78
|
227
|
79
|
228 func serializeType(node: XmlNode, headerTypes: Table[string, string]): Table[string, seq[string]] =
|
78
|
229 if node.attrsLen == 0:
|
|
230 return
|
|
231 if node.attr("requires") == "vk_platform" or node.attr("category") == "include":
|
|
232 return
|
|
233 result["basetypes"] = @[]
|
|
234 result["enums"] = @[]
|
|
235
|
|
236 # include-defined types (in platform headers)
|
79
|
237 if node.attr("name") in headerTypes:
|
|
238 for platform in PLATFORM_HEADER_MAP[node.attr("requires")]:
|
|
239 let platformfile = "platform/" & platform
|
|
240 if not result.hasKey(platformfile):
|
|
241 result[platformfile] = @[]
|
|
242 result[platformfile].add " " & node.attr("name").strip(chars={'_'}) & " {.header: \"" & node.attr("requires") & "\".} = object"
|
78
|
243 # generic base types
|
|
244 elif node.attr("category") == "basetype":
|
|
245 let typechild = node.child("type")
|
|
246 let namechild = node.child("name")
|
|
247 if typechild != nil and namechild != nil:
|
|
248 var typename = typechild[0].text
|
|
249 if node[2].kind == xnText and node[2].text.strip() == "*":
|
|
250 typename = &"ptr {typename}"
|
|
251 result["basetypes"].add &" {namechild[0].text}* = {mapType(typename)}"
|
|
252 elif namechild != nil:
|
|
253 result["basetypes"].add &" {namechild[0].text}* = object"
|
|
254 # function pointers need to be handled with structs
|
|
255 elif node.attr("category") == "funcpointer":
|
|
256 discard
|
|
257 # preprocessor defines, ignored
|
|
258 elif node.attr("category") == "define":
|
|
259 discard
|
|
260 # bitmask aliases
|
|
261 elif node.attr("category") == "bitmask":
|
|
262 if node.hasAttr("alias"):
|
|
263 let name = node.attr("name")
|
|
264 let alias = node.attr("alias")
|
|
265 result["enums"].add &" {name}* = {alias}"
|
|
266 # distinct resource ID types aka handles
|
|
267 elif node.attr("category") == "handle":
|
|
268 if not node.hasAttr("alias"):
|
|
269 let name = node.child("name")[0].text
|
|
270 var thetype = mapType(node.child("type")[0].text)
|
|
271 result["basetypes"].add &" {name}* = distinct {thetype}"
|
|
272 # enum aliases
|
|
273 elif node.attr("category") == "enum":
|
|
274 if node.hasAttr("alias"):
|
|
275 let name = node.attr("name")
|
|
276 let alias = node.attr("alias")
|
|
277 result["enums"].add &" {name}* = {alias}"
|
|
278 else:
|
|
279 discard
|
|
280
|
79
|
281 func serializeCommand(node: XmlNode): (string, string) =
|
|
282 let
|
|
283 proto = node.child("proto")
|
|
284 resulttype = mapType(proto.child("type")[0].text)
|
|
285 name = proto.child("name")[0].text
|
|
286 var params: seq[string]
|
|
287 for param in node:
|
|
288 if param.tag == "param" and param.attr("api") in ["", "vulkan"]:
|
|
289 let fieldname = param.child("name")[0].text.strip(chars={'_'})
|
|
290 var fieldtype = param.child("type")[0].text.strip(chars={'_'})
|
|
291 if param[param.len - 2].kind == xnText and param[param.len - 2].text.strip() == "*":
|
|
292 fieldtype = &"ptr {mapType(fieldtype)}"
|
|
293 fieldtype = mapType(fieldtype)
|
|
294 params.add &"{mapName(fieldname)}: {fieldtype}"
|
|
295 let allparams = params.join(", ")
|
|
296 return (name, &"proc({allparams}): {resulttype} {{.stdcall.}}")
|
|
297
|
78
|
298
|
|
299 proc update(a: var Table[string, seq[string]], b: Table[string, seq[string]]) =
|
|
300 for k, v in b.pairs:
|
|
301 if not a.hasKey(k):
|
|
302 a[k] = @[]
|
|
303 a[k].add v
|
|
304
|
|
305
|
|
306 proc main() =
|
|
307 if not os.fileExists("vk.xml"):
|
|
308 let client = newHttpClient()
|
|
309 let glUrl = "https://raw.githubusercontent.com/KhronosGroup/Vulkan-Docs/main/xml/vk.xml"
|
|
310 client.downloadFile(glUrl, "vk.xml")
|
|
311
|
|
312 let api = loadXml("vk.xml")
|
|
313
|
|
314 const outdir = "src/vulkan_api/output"
|
|
315 removeDir outdir
|
|
316 createDir outdir
|
|
317 createDir outdir / "platform"
|
|
318
|
|
319 # index all names that are only available on certain platforms
|
|
320 var platformTypes: Table[string, string]
|
|
321 for extension in api.findAll("extension"):
|
|
322 if extension.hasAttr("platform"):
|
|
323 for thetype in extension.findAll("type"):
|
|
324 platformTypes[thetype.attr("name")] = extension.attr("platform")
|
|
325 for command in extension.findAll("command"):
|
|
326 platformTypes[command.attr("name")] = extension.attr("platform")
|
|
327 elif extension.attr("name").startsWith("VK_KHR_video"):
|
|
328 for thetype in extension.findAll("type"):
|
79
|
329 platformTypes[thetype.attr("name")] = "provisional"
|
78
|
330 for command in extension.findAll("command"):
|
79
|
331 platformTypes[command.attr("name")] = "provisional"
|
78
|
332
|
|
333 var outputFiles = {
|
79
|
334 "basetypes": @[
|
|
335 "import std/dynlib",
|
|
336 "type",
|
|
337 " VkHandle* = distinct pointer",
|
|
338 " VkNonDispatchableHandle* = distinct pointer",
|
|
339 "when defined(linux):",
|
|
340 " let vulkanLib* = loadLib(\"libvulkan.so.1\")",
|
|
341 "when defined(windows):",
|
|
342 " let vulkanLib* = loadLib(\"vulkan-1.dll\")",
|
|
343 "if vulkanLib == nil:",
|
|
344 " raise newException(Exception, \"Unable to load vulkan library\")",
|
|
345 "type",
|
|
346 ],
|
|
347 "structs": @["type"],
|
|
348 "enums": @["type"],
|
|
349 "commands": @[],
|
78
|
350 }.toTable
|
|
351
|
|
352 # enums
|
|
353 for thetype in api.findAll("type"):
|
|
354 if thetype.attr("category") == "bitmask" and not thetype.hasAttr("alias") and (not thetype.hasAttr("api") or thetype.attr("api") == "vulkan"):
|
|
355 let name = thetype.child("name")[0].text
|
|
356 outputFiles["enums"].add &" {name}* = distinct VkFlags"
|
|
357 for theenum in api.findAll("enums"):
|
|
358 outputFiles["enums"].add serializeEnum(theenum, api)
|
|
359
|
|
360 # structs and function types need to be in same "type" block to avoid forward-declarations
|
|
361 outputFiles["structs"].add serializeFunctiontypes(api)
|
|
362 for thetype in api.findAll("type"):
|
|
363 if thetype.attr("category") == "struct" or thetype.attr("category") == "union":
|
|
364 var outfile = "structs"
|
|
365 if thetype.attr("name") in platformTypes:
|
|
366 outfile = "platform/" & platformTypes[thetype.attr("name")]
|
|
367 if not (outfile in outputFiles):
|
|
368 outputFiles[outfile] = @[]
|
|
369 outputFiles[outfile].add serializeStruct(thetype, api)
|
|
370
|
|
371 # types
|
79
|
372 var headerTypes: Table[string, string]
|
|
373 for types in api.findAll("types"):
|
|
374 for thetype in types.findAll("type"):
|
|
375 if thetype.attrsLen == 2 and thetype.hasAttr("requires") and thetype.hasAttr("name") and thetype.attr("requires") != "vk_platform":
|
|
376 let name = thetype.attr("name")
|
|
377 let incld = thetype.attr("requires")
|
|
378 headerTypes[name] = &"{name} {{.header: \"{incld}\".}} = object"
|
|
379
|
78
|
380 for typesgroup in api.findAll("types"):
|
|
381 for thetype in typesgroup.findAll("type"):
|
79
|
382 outputFiles.update serializeType(thetype, headerTypes)
|
|
383
|
|
384 # commands aka functions
|
|
385 var varDecls: Table[string, string]
|
|
386 var procLoads: Table[string, string] # procloads need to be packed into feature/extension loader procs
|
|
387 for commands in api.findAll("commands"):
|
|
388 for command in commands.findAll("command"):
|
|
389 if command.attr("api") != "vulkansc":
|
|
390 if command.hasAttr("alias"):
|
|
391 let name = command.attr("name")
|
|
392 let alias = command.attr("alias")
|
|
393 let thetype = varDecls[alias].split(":", 1)[1].strip()
|
|
394 varDecls[name] = &" {name}*: {thetype}"
|
|
395 procLoads[name] = &" {name} = {alias}"
|
|
396 else:
|
|
397 let (name, thetype) = serializeCommand(command)
|
|
398 varDecls[name] = &" {name}*: {thetype}"
|
|
399 procLoads[name] = &" {name} = cast[{thetype}](checkedSymAddr(vulkanLib, \"{name}\"))"
|
|
400 var declared: seq[string]
|
|
401 var featureloads: seq[string]
|
|
402 for feature in api.findAll("feature"):
|
|
403 if feature.attr("api") in ["vulkan", "vulkan,vulkansc"]:
|
|
404 let name = feature.attr("name")
|
|
405 outputFiles["commands"].add &"# feature {name}"
|
|
406 outputFiles["commands"].add "var"
|
|
407 for command in feature.findAll("command"):
|
|
408 if not (command.attr("name") in declared):
|
|
409 outputFiles["commands"].add varDecls[command.attr("name")]
|
|
410 declared.add command.attr("name")
|
|
411 featureloads.add &"load{name}"
|
|
412 outputFiles["commands"].add &"proc load{name}*() ="
|
|
413 for command in feature.findAll("command"):
|
|
414 outputFiles["commands"].add procLoads[command.attr("name")]
|
|
415 outputFiles["commands"].add ""
|
|
416 outputFiles["commands"].add ["proc loadAll*() ="]
|
|
417 for l in featureloads:
|
|
418 outputFiles["commands"].add [&" {l}()"]
|
|
419 outputFiles["commands"].add ""
|
|
420
|
|
421 # for promoted extensions, dependants need to call the load-function of the promoted feature/extension
|
|
422 # use table to store promotions
|
|
423 var promotions: Table[string, string]
|
|
424 for extensions in api.findAll("extensions"):
|
|
425 for extension in extensions.findAll("extension"):
|
|
426 if extension.hasAttr("promotedto"):
|
|
427 promotions[extension.attr("name")] = extension.attr("promotedto")
|
|
428
|
|
429 var extensionDependencies: Table[string, (seq[string], XmlNode)]
|
|
430 var features: seq[string]
|
|
431 for feature in api.findAll("feature"):
|
|
432 features.add feature.attr("name")
|
|
433 for extensions in api.findAll("extensions"):
|
|
434 for extension in extensions.findAll("extension"):
|
|
435 let name = extension.attr("name")
|
|
436 extensionDependencies[name] = (@[], extension)
|
|
437 if extension.hasAttr("depends"):
|
|
438 extensionDependencies[name] = (extension.attr("depends").split("+"), extension)
|
|
439 if extension.attr("depends").startsWith("("): # no need for full tree parser, only single place where we can use a feature
|
|
440 let dependencies = extension.attr("depends").rsplit({')'}, 1)[1][1 .. ^1].split("+")
|
|
441 extensionDependencies[name] = (dependencies, extension)
|
|
442 if name in SPECIAL_DEPENDENCIES:
|
|
443 extensionDependencies[name][0].add SPECIAL_DEPENDENCIES[name]
|
|
444
|
|
445 var dependencyOrderedExtensions: OrderedTable[string, XmlNode]
|
|
446 while extensionDependencies.len > 0:
|
|
447 var delkeys: seq[string]
|
|
448 for extensionName, (dependencies, extension) in extensionDependencies.pairs:
|
|
449 var missingExtension = false
|
|
450 for dep in dependencies:
|
|
451 let realdep = promotions.getOrDefault(dep, dep)
|
|
452 if not (realdep in dependencyOrderedExtensions) and not (realdep in features):
|
|
453 missingExtension = true
|
|
454 break
|
|
455 if not missingExtension:
|
|
456 dependencyOrderedExtensions[extensionName] = extension
|
|
457 delkeys.add extensionName
|
|
458 for key in delkeys:
|
|
459 extensionDependencies.del key
|
|
460
|
|
461 for extension in dependencyOrderedExtensions.values:
|
|
462 if extension.hasAttr("promotedto"): # will be loaded in promoted place
|
|
463 continue
|
|
464 if extension.attr("supported") in ["", "vulkan", "vulkan,vulkansc"]:
|
|
465 var file = "commands"
|
|
466 if extension.attr("platform") != "":
|
|
467 file = "platform/" & extension.attr("platform")
|
|
468 elif extension.attr("name").startsWith("VK_KHR_video"): # hack since we do not include video headers by default
|
|
469 file = "platform/provisional"
|
|
470 let name = extension.attr("name")
|
|
471 if extension.findAll("command").len > 0:
|
|
472 outputFiles[file].add &"# extension {name}"
|
|
473 outputFiles[file].add "var"
|
|
474 for command in extension.findAll("command"):
|
|
475 if not (command.attr("name") in declared):
|
|
476 outputFiles[file].add varDecls[command.attr("name")]
|
|
477 declared.add command.attr("name")
|
|
478 outputFiles[file].add &"proc load{name}*() ="
|
|
479 var addedFunctionBody = false
|
|
480 if extension.hasAttr("depends"):
|
|
481 for dependency in extension.attr("depends").split("+"):
|
|
482 # need to check since some extensions have no commands and therefore no load-function
|
|
483 outputFiles[file].add &" load{promotions.getOrDefault(dependency, dependency)}()"
|
|
484 addedFunctionBody = true
|
|
485 for command in extension.findAll("command"):
|
|
486 outputFiles[file].add procLoads[command.attr("name")]
|
|
487 addedFunctionBody = true
|
|
488 if not addedFunctionBody:
|
|
489 outputFiles[file].add " discard"
|
|
490 outputFiles[file].add ""
|
|
491
|
|
492 var mainout: seq[string]
|
|
493 for section in ["basetypes", "enums", "structs", "commands"]:
|
|
494 mainout.add outputFiles[section]
|
|
495 writeFile outdir / &"types.nim", mainout.join("\n")
|
|
496
|
78
|
497 for filename, filecontent in outputFiles.pairs:
|
79
|
498 if filename.startsWith("platform/"):
|
|
499 writeFile outdir / &"{filename}.nim", (@[
|
|
500 "import std/dynlib",
|
|
501 "import ../types",
|
|
502 "type"
|
|
503 ] & filecontent).join("\n")
|
78
|
504
|
|
505 when isMainModule:
|
|
506 main()
|