Mercurial > games > semicongine
annotate generators/vulkan_api/vulkan_api_generator.nim @ 398:194f0a398141
add: better api for fullscreen management
author | sam <sam@basx.dev> |
---|---|
date | Wed, 27 Dec 2023 14:29:15 +0700 |
parents | e76822e943af |
children | c66503386e8b |
rev | line source |
---|---|
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 | |
83
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
62 # will be directly loaded at startup |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
63 IGNORED_COMMANDS = @["vkGetInstanceProcAddr"] |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
64 # can be loaded without a vulkan instance |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
65 GLOBAL_COMMANDS = @[ |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
66 "vkEnumerateInstanceVersion", |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
67 "vkEnumerateInstanceExtensionProperties", |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
68 "vkEnumerateInstanceLayerProperties", |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
69 "vkCreateInstance", |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
70 ] |
78 | 71 |
72 # helpers | |
73 func mapType(typename: string): auto = | |
74 TYPEMAP.getOrDefault(typename.strip(), typename.strip()).strip(chars={'_'}) | |
75 func mapName(thename: string): auto = | |
76 MAP_KEYWORD.getOrDefault(thename.strip(), thename.strip()).strip(chars={'_'}) | |
77 func smartParseInt(value: string): int = | |
78 if value.startsWith("0x"): | |
79 parseHexInt(value) | |
80 else: | |
81 parseInt(value) | |
82 func hasAttr(node: XmlNode, attr: string): bool = node.attr(attr) != "" | |
83 func tableSorted(table: Table[int, string]): seq[(int, string)] = | |
84 result = toSeq(table.pairs) | |
85 result.sort((a, b) => cmp(a[0], b[0])) | |
84
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
86 func findType(declNode: XmlNode): string = |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
87 # examples: |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
88 # char** -> cstringArray |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
89 # void* -> pointer |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
90 # char* -> cstring |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
91 # |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
92 # int* -> ptr int |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
93 # void** -> ptr pointer |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
94 # int** -> ptr ptr int |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
95 var basetype = "" |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
96 var apointer = "" |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
97 var arraylen = "" |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
98 for child in declNode: |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
99 if child.kind == xnText: |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
100 if "[" in child.text: |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
101 if "[" in child.text and "]" in child.text: |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
102 arraylen = child.text.strip(chars={'[', ']'}).replace("][", "*") |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
103 else: |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
104 arraylen = declNode.child("enum")[0].text |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
105 else: |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
106 for i in 0 ..< child.text.count('*'): |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
107 apointer = apointer & "ptr " |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
108 elif child.tag == "type": |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
109 basetype = mapType(child[0].text) |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
110 if basetype == "void": |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
111 if apointer.count("ptr ") > 0: |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
112 basetype = "pointer" |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
113 apointer = apointer[0 ..< ^4] |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
114 elif basetype == "char": |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
115 if apointer.count("ptr ") == 1: |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
116 basetype = "cstring" |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
117 apointer = "" |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
118 elif apointer.count("ptr ") == 2: |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
119 basetype = "cstringArray" |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
120 apointer = "" |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
121 elif apointer.count("ptr ") > 2: |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
122 basetype = "cstringArray" |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
123 apointer = apointer[0 ..< ^8] |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
124 |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
125 result = &"{apointer}{basetype}" |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
126 if arraylen != "": |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
127 result = &"array[{arraylen}, {result}]" |
78 | 128 |
129 # serializers | |
92
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
130 # return values and whether this is a bitfield |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
131 func serializeEnum(node: XmlNode, api: XmlNode): (seq[string], string) = |
78 | 132 let name = node.attr("name") |
133 if name == "": | |
134 return result | |
135 | |
80 | 136 var reservedNames: seq[string] |
137 for t in api.findAll("type"): | |
138 reservedNames.add t.attr("name").replace("_", "").toLower() | |
139 | |
78 | 140 # find additional enum defintion in feature definitions |
141 var values: Table[int, string] | |
80 | 142 for feature in api.findAll("feature"): |
78 | 143 for require in feature.findAll("require"): |
144 for theenum in require.findAll("enum"): | |
145 if theenum.attr("extends") == name: | |
146 if theenum.hasAttr("offset"): | |
147 let enumBase = 1000000000 + (smartParseInt(theenum.attr("extnumber")) - 1) * 1000 | |
148 var value = smartParseInt(theenum.attr("offset")) + enumBase | |
149 if theenum.attr("dir") == "-": | |
150 value = -value | |
151 values[value] = theenum.attr("name") | |
152 elif theenum.hasAttr("value"): | |
153 var value = smartParseInt(theenum.attr("value")) | |
154 if theenum.attr("dir") == "-": | |
155 value = -value | |
156 values[value] = theenum.attr("name") | |
157 elif theenum.hasAttr("bitpos"): | |
158 var value = smartParseInt(theenum.attr("bitpos")) | |
159 if theenum.attr("dir") == "-": | |
160 value = -value | |
161 values[value] = theenum.attr("name") | |
162 elif theenum.hasAttr("alias"): | |
163 discard | |
164 else: | |
165 raise newException(Exception, &"Unknown extension value: {feature}\nvalue:{theenum}") | |
166 # find additional enum defintion in extension definitions | |
80 | 167 for extension in api.findAll("extension"): |
78 | 168 let extensionNumber = parseInt(extension.attr("number")) |
169 let enumBase = 1000000000 + (extensionNumber - 1) * 1000 | |
170 for require in extension.findAll("require"): | |
171 for theenum in require.findAll("enum"): | |
172 if theenum.attr("extends") == name: | |
173 if theenum.hasAttr("offset"): | |
174 if theenum.hasAttr("extnumber"): | |
175 let otherBase = 1000000000 + (smartParseInt(theenum.attr("extnumber")) - 1) * 1000 | |
176 var value = smartParseInt(theenum.attr("offset")) + otherBase | |
177 if theenum.attr("dir") == "-": | |
178 value = -value | |
179 values[value] = theenum.attr("name") | |
180 else: | |
181 var value = smartParseInt(theenum.attr("offset")) + enumBase | |
182 if theenum.attr("dir") == "-": | |
183 value = -value | |
184 values[value] = theenum.attr("name") | |
185 elif theenum.hasAttr("value"): | |
186 var value = smartParseInt(theenum.attr("value")) | |
187 if theenum.attr("dir") == "-": | |
188 value = -value | |
189 values[value] = theenum.attr("name") | |
190 elif theenum.hasAttr("bitpos"): | |
191 var value = smartParseInt(theenum.attr("bitpos")) | |
192 if theenum.attr("dir") == "-": | |
193 value = -value | |
194 values[value] = theenum.attr("name") | |
195 elif theenum.hasAttr("alias"): | |
196 discard | |
197 else: | |
198 raise newException(Exception, &"Unknown extension value: {extension}\nvalue:{theenum}") | |
199 | |
200 # generate enums | |
201 if node.attr("type") == "enum": | |
202 for value in node.findAll("enum"): | |
203 if value.hasAttr("alias"): | |
204 continue | |
205 if value.attr("value").startsWith("0x"): | |
206 values[parseHexInt(value.attr("value"))] = value.attr("name") | |
207 else: | |
208 values[smartParseInt(value.attr("value"))] = value.attr("name") | |
209 if values.len > 0: | |
92
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
210 result[0].add " " & name & "* {.size: sizeof(cint).} = enum" |
78 | 211 for (value, name) in tableSorted(values): |
80 | 212 var thename = name |
213 if name.replace("_", "").toLower() in reservedNames: | |
214 thename = thename & "_ENUM" | |
215 let enumEntry = &" {thename} = {value}" | |
92
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
216 result[0].add enumEntry |
78 | 217 |
218 # generate bitsets (normal enums in the C API, but bitfield-enums in Nim) | |
219 elif node.attr("type") == "bitmask": | |
84
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
220 var predefined_enum_sets: seq[string] |
78 | 221 for value in node.findAll("enum"): |
79 | 222 if value.hasAttr("bitpos"): |
223 values[smartParseInt(value.attr("bitpos"))] = value.attr("name") | |
224 elif node.attr("name") == "VkVideoEncodeRateControlModeFlagBitsKHR": # special exception, for some reason this has values instead of bitpos | |
225 values[smartParseInt(value.attr("value"))] = value.attr("name") | |
84
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
226 elif value.hasAttr("value"): # create a const that has multiple bits set |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
227 predefined_enum_sets.add &" {value.attr(\"name\")}* = {value.attr(\"value\")}" |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
228 |
78 | 229 if values.len > 0: |
92
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
230 let cApiName = name.replace("FlagBits", "Flags") |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
231 result[1] = cApiName |
78 | 232 if node.hasAttr("bitwidth"): |
92
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
233 result[0].add " " & name & "* {.size: 8.} = enum" |
78 | 234 else: |
92
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
235 result[0].add " " & name & "* {.size: sizeof(cint).} = enum" |
78 | 236 for (bitpos, enumvalue) in tableSorted(values): |
237 var value = "00000000000000000000000000000000"# makes the bit mask nicely visible | |
238 if node.hasAttr("bitwidth"): # assumes this is always 64 | |
239 value = value & value | |
240 value[^(bitpos + 1)] = '1' | |
241 let enumEntry = &" {enumvalue} = 0b{value}" | |
92
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
242 if not (enumEntry in result[0]): # the specs define duplicate entries for backwards compat |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
243 result[0].add enumEntry |
84
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
244 if node.hasAttr("bitwidth"): # assuming this attribute is always 64 |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
245 if values.len > 0: |
92
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
246 result[0].add &"""func toBits*(flags: openArray[{name}]): {cApiName} = |
84
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
247 for flag in flags: |
92
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
248 result = {cApiName}(uint64(result) or uint64(flag))""" |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
249 result[0].add &"""func toEnums*(number: {cApiName}): seq[{name}] = |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
250 for value in {name}.items: |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
251 if (cast[uint64](value) and uint64(number)) > 0: |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
252 result.add value""" |
94 | 253 result[0].add &"proc `==`*(a, b: {cApiName}): bool = uint64(a) == uint64(b)" |
84
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
254 else: |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
255 if values.len > 0: |
92
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
256 result[0].add &"""func toBits*(flags: openArray[{name}]): {cApiName} = |
84
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
257 for flag in flags: |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
258 result = {cApiName}(uint(result) or uint(flag))""" |
92
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
259 result[0].add &"""func toEnums*(number: {cApiName}): seq[{name}] = |
84
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
260 for value in {name}.items: |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
261 if (value.ord and cint(number)) > 0: |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
262 result.add value""" |
94 | 263 result[0].add &"proc `==`*(a, b: {cApiName}): bool = cint(a) == cint(b)" |
84
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
264 if predefined_enum_sets.len > 0: |
92
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
265 result[0].add "const" |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
266 result[0].add predefined_enum_sets |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
267 result[0].add "type" |
84
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
268 |
78 | 269 |
80 | 270 func serializeStruct(node: XmlNode): seq[string] = |
78 | 271 let name = node.attr("name") |
272 var union = "" | |
273 if node.attr("category") == "union": | |
79 | 274 union = "{.union.} " |
275 result.add &" {name}* {union}= object" | |
78 | 276 for member in node.findAll("member"): |
277 if not member.hasAttr("api") or member.attr("api") == "vulkan": | |
278 let fieldname = member.child("name")[0].text.strip(chars={'_'}) | |
84
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
279 result.add &" {mapName(fieldname)}*: {findType(member)}" |
78 | 280 |
281 func serializeFunctiontypes(api: XmlNode): seq[string] = | |
282 for node in api.findAll("type"): | |
283 if node.attr("category") == "funcpointer": | |
284 let name = node[1][0] | |
285 let returntype = mapType(node[0].text[8 .. ^1].split('(', 1)[0]) | |
286 var params: seq[string] | |
287 for i in countup(3, node.len - 1, 2): | |
288 var paramname = node[i + 1].text.split(',', 1)[0].split(')', 1)[0] | |
289 var paramtype = node[i][0].text | |
290 if paramname[0] == '*': | |
291 paramname = paramname.rsplit(" ", 1)[1] | |
292 paramtype = "ptr " & paramtype | |
293 paramname = mapName(paramname) | |
294 params.add &"{paramname}: {mapType(paramtype)}" | |
295 let paramsstr = params.join(", ") | |
79 | 296 result.add(&" {name}* = proc({paramsstr}): {returntype} {{.cdecl.}}") |
78 | 297 |
83
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
298 func serializeConsts(api: XmlNode): seq[string] = |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
299 result = @["const"] |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
300 for enums in api.findAll("enums"): |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
301 if enums.attr("name") == "API Constants": |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
302 for theenum in enums.findAll("enum"): |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
303 if theenum.hasAttr("alias"): |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
304 result.add &" {theenum.attr(\"name\")}* = {theenum.attr(\"alias\")}" |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
305 else: |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
306 var value = theenum.attr("value").strip(chars={'(', ')'}) |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
307 if value.endsWith("U"): |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
308 value = value[0..^2] & "'u32" |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
309 elif value.endsWith("ULL"): |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
310 value = value[0..^4] & "'u64" |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
311 if value[0] == '~': |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
312 value = "not " & value[1..^1] |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
313 result.add &" {theenum.attr(\"name\")}*: {mapType(theenum.attr(\"type\"))} = {value}" |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
314 |
79 | 315 func serializeType(node: XmlNode, headerTypes: Table[string, string]): Table[string, seq[string]] = |
78 | 316 if node.attrsLen == 0: |
317 return | |
318 if node.attr("requires") == "vk_platform" or node.attr("category") == "include": | |
319 return | |
320 result["basetypes"] = @[] | |
321 result["enums"] = @[] | |
322 | |
323 # include-defined types (in platform headers) | |
79 | 324 if node.attr("name") in headerTypes: |
325 for platform in PLATFORM_HEADER_MAP[node.attr("requires")]: | |
326 let platformfile = "platform/" & platform | |
327 if not result.hasKey(platformfile): | |
328 result[platformfile] = @[] | |
83
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
329 result[platformfile].add " " & node.attr("name").strip(chars={'_'}) & " *{.header: \"" & node.attr("requires") & "\".} = object" |
78 | 330 # generic base types |
331 elif node.attr("category") == "basetype": | |
332 let typechild = node.child("type") | |
333 let namechild = node.child("name") | |
334 if typechild != nil and namechild != nil: | |
335 var typename = typechild[0].text | |
336 if node[2].kind == xnText and node[2].text.strip() == "*": | |
337 typename = &"ptr {typename}" | |
338 result["basetypes"].add &" {namechild[0].text}* = {mapType(typename)}" | |
339 elif namechild != nil: | |
340 result["basetypes"].add &" {namechild[0].text}* = object" | |
341 # function pointers need to be handled with structs | |
342 elif node.attr("category") == "funcpointer": | |
343 discard | |
344 # preprocessor defines, ignored | |
345 elif node.attr("category") == "define": | |
346 discard | |
347 # bitmask aliases | |
348 elif node.attr("category") == "bitmask": | |
349 if node.hasAttr("alias"): | |
350 let name = node.attr("name") | |
351 let alias = node.attr("alias") | |
352 result["enums"].add &" {name}* = {alias}" | |
353 # distinct resource ID types aka handles | |
354 elif node.attr("category") == "handle": | |
355 if not node.hasAttr("alias"): | |
356 let name = node.child("name")[0].text | |
357 var thetype = mapType(node.child("type")[0].text) | |
358 result["basetypes"].add &" {name}* = distinct {thetype}" | |
359 # enum aliases | |
360 elif node.attr("category") == "enum": | |
361 if node.hasAttr("alias"): | |
362 let name = node.attr("name") | |
363 let alias = node.attr("alias") | |
364 result["enums"].add &" {name}* = {alias}" | |
365 else: | |
366 discard | |
367 | |
79 | 368 func serializeCommand(node: XmlNode): (string, string) = |
369 let | |
370 proto = node.child("proto") | |
371 resulttype = mapType(proto.child("type")[0].text) | |
372 name = proto.child("name")[0].text | |
373 var params: seq[string] | |
374 for param in node: | |
375 if param.tag == "param" and param.attr("api") in ["", "vulkan"]: | |
376 let fieldname = param.child("name")[0].text.strip(chars={'_'}) | |
84
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
377 params.add &"{mapName(fieldname)}: {findType(param)}" |
79 | 378 let allparams = params.join(", ") |
379 return (name, &"proc({allparams}): {resulttype} {{.stdcall.}}") | |
380 | |
78 | 381 |
382 proc update(a: var Table[string, seq[string]], b: Table[string, seq[string]]) = | |
383 for k, v in b.pairs: | |
384 if not a.hasKey(k): | |
385 a[k] = @[] | |
386 a[k].add v | |
387 | |
388 | |
389 proc main() = | |
81 | 390 let file = getTempDir() / "vk.xml" |
391 if not os.fileExists(file): | |
78 | 392 let client = newHttpClient() |
393 let glUrl = "https://raw.githubusercontent.com/KhronosGroup/Vulkan-Docs/main/xml/vk.xml" | |
81 | 394 client.downloadFile(glUrl, file) |
78 | 395 |
81 | 396 let api = loadXml(file) |
78 | 397 |
398 const outdir = "src/vulkan_api/output" | |
399 removeDir outdir | |
400 createDir outdir | |
401 createDir outdir / "platform" | |
402 | |
403 # index all names that are only available on certain platforms | |
404 var platformTypes: Table[string, string] | |
405 for extension in api.findAll("extension"): | |
406 if extension.hasAttr("platform"): | |
407 for thetype in extension.findAll("type"): | |
408 platformTypes[thetype.attr("name")] = extension.attr("platform") | |
409 for command in extension.findAll("command"): | |
410 platformTypes[command.attr("name")] = extension.attr("platform") | |
411 elif extension.attr("name").startsWith("VK_KHR_video"): | |
412 for thetype in extension.findAll("type"): | |
79 | 413 platformTypes[thetype.attr("name")] = "provisional" |
78 | 414 for command in extension.findAll("command"): |
79 | 415 platformTypes[command.attr("name")] = "provisional" |
78 | 416 |
417 var outputFiles = { | |
79 | 418 "basetypes": @[ |
419 "import std/dynlib", | |
83
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
420 "import std/tables", |
84
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
421 "import std/strutils", |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
422 "import std/logging", |
92
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
423 "import std/typetraits", |
84
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
424 "import std/macros", |
79 | 425 "type", |
83
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
426 " VkHandle* = distinct uint", |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
427 " VkNonDispatchableHandle* = distinct uint", |
79 | 428 "when defined(linux):", |
429 " let vulkanLib* = loadLib(\"libvulkan.so.1\")", | |
430 "when defined(windows):", | |
431 " let vulkanLib* = loadLib(\"vulkan-1.dll\")", | |
432 "if vulkanLib == nil:", | |
433 " raise newException(Exception, \"Unable to load vulkan library\")", | |
80 | 434 "func VK_MAKE_API_VERSION*(variant: uint32, major: uint32, minor: uint32, patch: uint32): uint32 {.compileTime.} =", |
435 " (variant shl 29) or (major shl 22) or (minor shl 12) or patch", | |
436 "", | |
81 | 437 """template checkVkResult*(call: untyped) = |
438 when defined(release): | |
439 discard call | |
440 else: | |
441 # yes, a bit cheap, but this is only for nice debug output | |
442 var callstr = astToStr(call).replace("\n", "") | |
443 while callstr.find(" ") >= 0: | |
444 callstr = callstr.replace(" ", " ") | |
445 debug "CALLING vulkan: ", callstr | |
446 let value = call | |
447 if value != VK_SUCCESS: | |
448 error "Vulkan error: ", astToStr(call), " returned ", $value | |
449 raise newException(Exception, "Vulkan error: " & astToStr(call) & | |
450 " returned " & $value)""", | |
84
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
451 """ |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
452 # custom enum iteration (for enum values > 2^16) |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
453 macro enumFullRange(a: typed): untyped = |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
454 newNimNode(nnkBracket).add(a.getType[1][1..^1]) |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
455 |
97
110ed3ee5df8
add: swapchain, images, fix some destroctors
Sam <sam@basx.dev>
parents:
94
diff
changeset
|
456 iterator items*[T: HoleyEnum](E: typedesc[T]): T = |
84
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
457 for a in enumFullRange(E): yield a""", |
79 | 458 ], |
459 "structs": @["type"], | |
460 "enums": @["type"], | |
461 "commands": @[], | |
78 | 462 }.toTable |
83
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
463 outputFiles["basetypes"].add serializeConsts(api) |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
464 outputFiles["basetypes"].add "type" |
78 | 465 |
466 # enums | |
467 for thetype in api.findAll("type"): | |
468 if thetype.attr("category") == "bitmask" and not thetype.hasAttr("alias") and (not thetype.hasAttr("api") or thetype.attr("api") == "vulkan"): | |
469 let name = thetype.child("name")[0].text | |
470 outputFiles["enums"].add &" {name}* = distinct VkFlags" | |
92
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
471 |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
472 var bitfields: Table[string, string] |
83
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
473 outputFiles["enums"].add "let vkGetInstanceProcAddr = cast[proc(instance: VkInstance, name: cstring): pointer {.stdcall.}](checkedSymAddr(vulkanLib, \"vkGetInstanceProcAddr\"))" |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
474 outputFiles["enums"].add "type" |
78 | 475 for theenum in api.findAll("enums"): |
92
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
476 let (enums, bitFieldName) = serializeEnum(theenum, api) |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
477 outputFiles["enums"].add enums |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
478 if bitFieldName != "": |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
479 bitfields[theenum.attr("name")] = bitFieldName |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
480 |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
481 # bitmask-to-string functions |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
482 for thetype in api.findAll("type"): |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
483 if thetype.attr("name") in bitfields: |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
484 let name = bitfields[thetype.attr("name")] |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
485 let stringfunc = &"proc `$`*(bitset: {name}): string = $toEnums(bitset)" |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
486 if not (stringfunc in outputFiles["enums"]): |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
487 outputFiles["enums"].add stringfunc |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
488 outputFiles["enums"].add "type" |
78 | 489 |
490 # structs and function types need to be in same "type" block to avoid forward-declarations | |
491 outputFiles["structs"].add serializeFunctiontypes(api) | |
492 for thetype in api.findAll("type"): | |
493 if thetype.attr("category") == "struct" or thetype.attr("category") == "union": | |
494 var outfile = "structs" | |
495 if thetype.attr("name") in platformTypes: | |
496 outfile = "platform/" & platformTypes[thetype.attr("name")] | |
497 if not (outfile in outputFiles): | |
498 outputFiles[outfile] = @[] | |
80 | 499 outputFiles[outfile].add serializeStruct(thetype) |
78 | 500 |
501 # types | |
79 | 502 var headerTypes: Table[string, string] |
503 for types in api.findAll("types"): | |
504 for thetype in types.findAll("type"): | |
505 if thetype.attrsLen == 2 and thetype.hasAttr("requires") and thetype.hasAttr("name") and thetype.attr("requires") != "vk_platform": | |
506 let name = thetype.attr("name") | |
507 let incld = thetype.attr("requires") | |
508 headerTypes[name] = &"{name} {{.header: \"{incld}\".}} = object" | |
509 | |
78 | 510 for typesgroup in api.findAll("types"): |
511 for thetype in typesgroup.findAll("type"): | |
79 | 512 outputFiles.update serializeType(thetype, headerTypes) |
513 | |
92
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
514 for typesgroup in api.findAll("types"): |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
515 for node in typesgroup.findAll("type"): |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
516 if node.attr("category") == "handle": |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
517 if not node.hasAttr("alias"): |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
518 let name = node.child("name")[0].text |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
519 outputFiles["basetypes"].add &"proc `$`*(handle: {name}): string = \"{name}(\" & $(uint(handle)) & \")\"" |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
520 outputFiles["basetypes"].add &"proc valid*(handle: {name}): bool = uint(handle) != 0" |
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
521 outputFiles["basetypes"].add &"proc reset*(handle: var {name}) = handle = {name}(0)" |
94 | 522 outputFiles["basetypes"].add &"proc `==`*(a, b: {name}): bool = uint(a) == uint(b)" |
523 | |
92
e872cf354110
add: more stuff for the vulkan API wrappers
Sam <sam@basx.dev>
parents:
84
diff
changeset
|
524 |
79 | 525 # commands aka functions |
526 var varDecls: Table[string, string] | |
527 var procLoads: Table[string, string] # procloads need to be packed into feature/extension loader procs | |
528 for commands in api.findAll("commands"): | |
529 for command in commands.findAll("command"): | |
83
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
530 if command.attr("api") != "vulkansc" and not (command.attr("name") in IGNORED_COMMANDS): |
79 | 531 if command.hasAttr("alias"): |
532 let name = command.attr("name") | |
533 let alias = command.attr("alias") | |
534 let thetype = varDecls[alias].split(":", 1)[1].strip() | |
535 varDecls[name] = &" {name}*: {thetype}" | |
536 procLoads[name] = &" {name} = {alias}" | |
537 else: | |
538 let (name, thetype) = serializeCommand(command) | |
539 varDecls[name] = &" {name}*: {thetype}" | |
83
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
540 procLoads[name] = &" {name} = cast[{thetype}](vkGetInstanceProcAddr(instance, \"{name}\"))" |
79 | 541 var declared: seq[string] |
542 var featureloads: seq[string] | |
543 for feature in api.findAll("feature"): | |
544 if feature.attr("api") in ["vulkan", "vulkan,vulkansc"]: | |
545 let name = feature.attr("name") | |
546 outputFiles["commands"].add &"# feature {name}" | |
547 outputFiles["commands"].add "var" | |
548 for command in feature.findAll("command"): | |
83
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
549 if not (command.attr("name") in declared) and not (command.attr("name") in IGNORED_COMMANDS): |
79 | 550 outputFiles["commands"].add varDecls[command.attr("name")] |
551 declared.add command.attr("name") | |
552 featureloads.add &"load{name}" | |
83
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
553 outputFiles["commands"].add &"proc load{name}*(instance: VkInstance) =" |
79 | 554 for command in feature.findAll("command"): |
83
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
555 if not (command.attr("name") in IGNORED_COMMANDS & GLOBAL_COMMANDS): |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
556 outputFiles["commands"].add procLoads[command.attr("name")] |
79 | 557 outputFiles["commands"].add "" |
83
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
558 outputFiles["commands"].add ["proc loadVulkan*(instance: VkInstance) ="] |
79 | 559 for l in featureloads: |
83
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
560 outputFiles["commands"].add [&" {l}(instance)"] |
79 | 561 outputFiles["commands"].add "" |
562 | |
563 # for promoted extensions, dependants need to call the load-function of the promoted feature/extension | |
564 # use table to store promotions | |
565 var promotions: Table[string, string] | |
566 for extensions in api.findAll("extensions"): | |
567 for extension in extensions.findAll("extension"): | |
568 if extension.hasAttr("promotedto"): | |
569 promotions[extension.attr("name")] = extension.attr("promotedto") | |
570 | |
571 var extensionDependencies: Table[string, (seq[string], XmlNode)] | |
572 var features: seq[string] | |
573 for feature in api.findAll("feature"): | |
574 features.add feature.attr("name") | |
575 for extensions in api.findAll("extensions"): | |
576 for extension in extensions.findAll("extension"): | |
577 let name = extension.attr("name") | |
578 extensionDependencies[name] = (@[], extension) | |
579 if extension.hasAttr("depends"): | |
580 extensionDependencies[name] = (extension.attr("depends").split("+"), extension) | |
581 if extension.attr("depends").startsWith("("): # no need for full tree parser, only single place where we can use a feature | |
582 let dependencies = extension.attr("depends").rsplit({')'}, 1)[1][1 .. ^1].split("+") | |
583 extensionDependencies[name] = (dependencies, extension) | |
584 if name in SPECIAL_DEPENDENCIES: | |
585 extensionDependencies[name][0].add SPECIAL_DEPENDENCIES[name] | |
586 | |
83
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
587 # order dependencies to generate them in correct order |
79 | 588 var dependencyOrderedExtensions: OrderedTable[string, XmlNode] |
589 while extensionDependencies.len > 0: | |
590 var delkeys: seq[string] | |
591 for extensionName, (dependencies, extension) in extensionDependencies.pairs: | |
592 var missingExtension = false | |
593 for dep in dependencies: | |
594 let realdep = promotions.getOrDefault(dep, dep) | |
595 if not (realdep in dependencyOrderedExtensions) and not (realdep in features): | |
596 missingExtension = true | |
597 break | |
598 if not missingExtension: | |
599 dependencyOrderedExtensions[extensionName] = extension | |
600 delkeys.add extensionName | |
601 for key in delkeys: | |
602 extensionDependencies.del key | |
603 | |
83
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
604 var extensionLoaderMap: Table[string, Table[string, string]] |
79 | 605 for extension in dependencyOrderedExtensions.values: |
606 if extension.hasAttr("promotedto"): # will be loaded in promoted place | |
607 continue | |
608 if extension.attr("supported") in ["", "vulkan", "vulkan,vulkansc"]: | |
609 var file = "commands" | |
83
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
610 var platform = extension.attr("platform") |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
611 if extension.attr("name").startsWith("VK_KHR_video"): |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
612 platform = "provisional" |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
613 if platform != "": |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
614 file = "platform/" & platform |
79 | 615 let name = extension.attr("name") |
616 if extension.findAll("command").len > 0: | |
617 outputFiles[file].add &"# extension {name}" | |
618 outputFiles[file].add "var" | |
619 for command in extension.findAll("command"): | |
83
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
620 if not (command.attr("name") in declared) and not (command.attr("name") in IGNORED_COMMANDS): |
79 | 621 outputFiles[file].add varDecls[command.attr("name")] |
622 declared.add command.attr("name") | |
83
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
623 outputFiles[file].add &"proc load{name}*(instance: VkInstance) =" |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
624 if not (platform in extensionLoaderMap): |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
625 extensionLoaderMap[platform] = Table[string, string]() |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
626 extensionLoaderMap[platform][name] = &"load{name}" |
79 | 627 var addedFunctionBody = false |
628 if extension.hasAttr("depends"): | |
629 for dependency in extension.attr("depends").split("+"): | |
630 # need to check since some extensions have no commands and therefore no load-function | |
83
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
631 outputFiles[file].add &" load{promotions.getOrDefault(dependency, dependency)}(instance)" |
79 | 632 addedFunctionBody = true |
633 for command in extension.findAll("command"): | |
634 outputFiles[file].add procLoads[command.attr("name")] | |
635 addedFunctionBody = true | |
636 if not addedFunctionBody: | |
637 outputFiles[file].add " discard" | |
638 outputFiles[file].add "" | |
639 | |
640 var mainout: seq[string] | |
641 for section in ["basetypes", "enums", "structs", "commands"]: | |
642 mainout.add outputFiles[section] | |
83
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
643 mainout.add "var EXTENSION_LOADERS = {" |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
644 for extension, loader in extensionLoaderMap[""].pairs: |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
645 mainout.add &" \"{extension}\": {loader}," |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
646 mainout.add "}.toTable" |
80 | 647 for platform in api.findAll("platform"): |
648 mainout.add &"when defined({platform.attr(\"protect\")}):" | |
206
7f921d7d0a2b
did: small refactoring of module structure
Sam <sam@basx.dev>
parents:
97
diff
changeset
|
649 mainout.add &" ../vulkan/include platform/{platform.attr(\"name\")}" |
83
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
650 if platform.attr("name") in extensionLoaderMap: |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
651 for extension, loader in extensionLoaderMap[platform.attr("name")].pairs: |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
652 mainout.add &" EXTENSION_LOADERS[\"{extension}\"] = {loader}" |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
653 |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
654 mainout.add "" |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
655 mainout.add "proc loadExtension*(instance: VkInstance, extension: string) = EXTENSION_LOADERS[extension](instance)" |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
656 mainout.add "" |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
657 mainout.add "# load global functions immediately" |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
658 mainout.add "block globalFunctions:" |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
659 mainout.add " let instance = VkInstance(0)" |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
660 for l in GLOBAL_COMMANDS: |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
661 mainout.add procLoads[l] |
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
662 mainout.add "" |
222
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
206
diff
changeset
|
663 # produces error if enable both implicit converters |
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
206
diff
changeset
|
664 # mainout.add "converter VkBool2NimBool*(a: VkBool32): bool = a > 0" |
83
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
665 mainout.add "converter NimBool2VkBool*(a: bool): VkBool32 = VkBool32(a)" |
84
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
666 |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
667 writeFile outdir / &"api.nim", mainout.join("\n") |
8412f433dc46
fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
Sam <sam@basx.dev>
parents:
83
diff
changeset
|
668 |
83
5e19aead2b61
add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents:
81
diff
changeset
|
669 |
78 | 670 for filename, filecontent in outputFiles.pairs: |
79 | 671 if filename.startsWith("platform/"): |
672 writeFile outdir / &"{filename}.nim", (@[ | |
673 "type" | |
674 ] & filecontent).join("\n") | |
78 | 675 |
676 when isMainModule: | |
677 main() |