annotate src/vulkan_api/vulkan_api_generator.nim @ 542:1cb1422247e8

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