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