Mercurial > games > semicongine
annotate svk/generate.nim @ 1486:0ba3f0b2be2e default tip main
did: more
author | sam <sam@basx.dev> |
---|---|
date | Sat, 03 May 2025 20:16:04 +0700 |
parents | 6e062a84c157 |
children |
rev | line source |
---|---|
1479 | 1 import std/strtabs |
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
2 import std/syncio |
1479 | 3 import std/xmltree |
4 import std/tables | |
5 import std/options | |
6 import std/sequtils | |
7 import std/strutils | |
8 import std/strformat | |
9 import std/xmlparser | |
10 import std/os | |
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
11 import std/osproc |
1479 | 12 import std/paths |
13 | |
14 # helpers | |
15 func smartParseInt(value: string): int = | |
16 if value.startsWith("0x"): | |
17 parseHexInt(value) | |
18 else: | |
19 parseInt(value) | |
20 | |
21 const TYPEMAP = { | |
22 "void": "void", | |
23 "char": "char", | |
24 "float": "float32", | |
25 "double": "float64", | |
26 "int8_t": "int8", | |
27 "uint8_t": "uint8", | |
28 "int16_t": "int16", | |
29 "uint16_t": "uint16", | |
30 "int32_t": "int32", | |
31 "uint32_t": "uint32", | |
32 "uint64_t": "uint64", | |
33 "int64_t": "int64", | |
34 "size_t": "csize_t", | |
35 "int": "cint", | |
36 }.toTable | |
37 # load xml | |
38 let xml = (system.currentSourcePath.parentDir() / "vk.xml").loadXml() | |
39 let platforms = xml.findAll("platforms")[0] | |
40 let types = xml.findAll("types")[0] | |
41 let xmlenums = xml.findAll("enums") | |
42 let commands = xml.findAll("commands")[0] | |
43 let features = xml.findAll("feature") # features has extends="<ENUM>" | |
44 let extensions = xml.findAll("extensions")[0] # extensions has extends="<ENUM>" | |
45 let formats = xml.findAll("formats")[0] | |
46 | |
47 # gather all enums | |
48 | |
49 type | |
50 EnumEntry = object | |
51 name: string | |
52 value: string | |
53 | |
54 EnumDef = object | |
55 name: string | |
56 values: seq[EnumEntry] | |
57 isBitmask: bool | |
58 | |
59 ConstantDef = object | |
60 name: string | |
61 datatype: string | |
62 value: string | |
63 | |
64 var consts: seq[ConstantDef] | |
65 var enums: Table[string, EnumDef] | |
66 | |
67 func addValue(edef: var EnumDef, n: XmlNode) = | |
68 if n.attr("deprecated") != "aliased" and n.attr("alias") == "": | |
69 if n.attr("name") in edef.values.mapIt(it.name): | |
70 return | |
71 | |
72 var value = "" | |
73 if n.attr("value") != "": | |
74 value = n.attr("value") | |
75 elif n.attr("bitpos") != "": | |
76 value = $(1 shl parseInt(n.attr("bitpos"))) | |
77 elif n.attr("offset") != "": | |
78 var enumBase = 1000000000 | |
79 if n.attr("extnumber") != "": | |
80 enumBase += (smartParseInt(n.attr("extnumber")) - 1) * 1000 | |
81 var v = smartParseInt(n.attr("offset")) + enumBase | |
82 if n.attr("dir") == "-": | |
83 v = -v | |
84 value = $(v) | |
85 | |
1482 | 86 if value notin edef.values.mapIt(it.value): |
87 edef.values.add EnumEntry(name: n.attr("name"), value: value) | |
1479 | 88 |
1484 | 89 func doTypename(typename: string, pointerType: int): string = |
90 ## pointerType == 0: no pointer | |
91 ## pointerType == 1: normal pointer (e.g. char *) | |
92 ## pointerType == 2: double pointer (e.g. void **) | |
93 assert pointerType in [0, 1, 2] | |
1481 | 94 result = TYPEMAP.getOrDefault(typename.strip(), typename.strip()).strip(chars = {'_'}) |
95 | |
96 if typename == "void": | |
1484 | 97 assert pointerType > 0 |
1481 | 98 |
1484 | 99 if pointerType > 0: |
1481 | 100 if typename == "void": |
101 result = "pointer" | |
102 elif typename == "char": | |
1485 | 103 if pointerType == 1: |
104 result = "cstring" | |
105 elif pointerType == 2: | |
106 result = "cstringArray" | |
107 else: | |
108 assert false, "Unsupported char pointer type" | |
1481 | 109 else: |
110 result = "ptr " & result | |
1485 | 111 |
112 if pointerType == 2 and typename != "char": | |
1484 | 113 result = "ptr " & result |
1481 | 114 |
115 func doIdentifier(typename: string): string = | |
116 if typename in ["type", "object"]: | |
117 return &"`{typename}`" | |
118 return typename.strip() | |
119 | |
1484 | 120 func doMember(typename, theType: string, pointerType: int, value: string): string = |
1481 | 121 if value == "": |
1484 | 122 &"{doIdentifier(typename)}: {doTypename(theType, pointerType)}" |
1481 | 123 else: |
1484 | 124 &"{doIdentifier(typename)}: {doTypename(theType, pointerType)} = {value}" |
1481 | 125 |
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
126 func memberDecl(n: XmlNode): string = |
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
127 for i in 0 ..< n.len: |
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
128 if n[i].kind == xnElement and n[i].tag == "comment": |
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
129 n.delete(i) |
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
130 break |
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
131 assert n.tag == "member" |
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
132 if n.len == 2: |
1484 | 133 return doMember(n[1][0].text, n[0][0].text, 0, n.attr("values")) |
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
134 elif n.len == 3: |
1481 | 135 assert "*" notin n[0][0].text.strip() |
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
136 if n[1].kind == xnElement and n[1].tag == "name": |
1481 | 137 # bitfield |
138 if n[2].text.strip().startsWith(":"): | |
139 return | |
1484 | 140 &"{doIdentifier(n[1][0].text)} {{.bitsize:{n[2].text.strip()[1 .. ^1]}.}}: {doTypename(n[0][0].text, 0)}" |
1481 | 141 # array definition |
142 elif n[2].text.strip().startsWith("["): | |
143 let arrayDim = n[2].text[1 ..< ^1] | |
144 if "][" in arrayDim: | |
145 let dims = arrayDim.split("][", 1) | |
146 let (dim1, dim2) = (dims[0], dims[1]) | |
147 return doMember( | |
148 n[1][0].text, | |
1484 | 149 &"array[{dim1}, array[{dim2}, {doTypename(n[0][0].text, 0)}]]", |
150 0, | |
1481 | 151 n.attr("values"), |
152 ) | |
153 else: | |
154 return doMember( | |
155 n[1][0].text, | |
1484 | 156 &"array[{arrayDim}, {doTypename(n[0][0].text, 0)}]", |
157 0, | |
1481 | 158 n.attr("values"), |
159 ) | |
160 else: | |
161 debugecho n.toSeq | |
162 doAssert false, "This should not happen" | |
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
163 else: |
1481 | 164 # pointer definition |
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
165 assert n[1].text.strip() == "*" |
1484 | 166 return doMember(n[2][0].text, n[0][0].text, 1, n.attr("values")) |
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
167 elif n.len == 4: |
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
168 if n[0].text.strip() in ["struct", "const struct"]: |
1484 | 169 return doMember(n[3][0].text, n[1][0].text, 1, n.attr("values")) |
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
170 else: |
1481 | 171 assert n[0].text.strip() == "const" # can be ignored |
172 assert n[1].tag == "type" | |
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
173 assert n[2].text.strip() in ["*", "* const *", "* const*"] |
1481 | 174 assert n[3].tag == "name" |
175 assert n[1].len == 1 | |
176 assert n[3].len == 1 | |
1485 | 177 return doMember( |
178 n[3][0].text, n[1][0].text, n[2].text.strip().count("*"), n.attr("values") | |
179 ) | |
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
180 elif n.len in [5, 6]: |
1481 | 181 # array definition, using const-value for array length |
182 # <type>uint8_t</type>,<name>pipelineCacheUUID</name>[<enum>VK_UUID_SIZE</enum>] | |
183 assert n[2].text.strip() == "[" | |
184 assert n[4].text.strip() == "]" | |
185 return doMember( | |
186 n[1][0].text, | |
1484 | 187 &"array[{n[3][0].text}, {doTypename(n[0][0].text, 0)}]", |
188 0, | |
1481 | 189 n.attr("values"), |
190 ) | |
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
191 assert false |
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
192 |
1479 | 193 for e in xmlenums: |
194 if e.attr("type") == "constants": | |
195 for c in e.findAll("enum"): | |
196 var value = c.attr("value").strip(chars = {'(', ')'}) | |
197 consts.add ConstantDef( | |
198 name: c.attr("name"), datatype: TYPEMAP[c.attr("type")], value: value | |
199 ) | |
200 elif e.attr("type") == "enum": | |
201 var edef = EnumDef(name: e.attr("name"), isBitmask: false) | |
202 for ee in e.findAll("enum"): | |
203 edef.addValue(ee) | |
204 enums[edef.name] = edef | |
205 elif e.attr("type") == "bitmask": | |
206 var edef = EnumDef(name: e.attr("name"), isBitmask: true) | |
207 for ee in e.findAll("enum"): | |
208 edef.addValue(ee) | |
209 enums[edef.name] = edef | |
210 | |
211 for f in features: | |
212 for extendenum in f.findAll("enum"): | |
213 if extendenum.attr("extends") != "": | |
214 enums[extendenum.attr("extends")].addValue(extendenum) | |
215 | |
1486 | 216 var extensionLoaders: seq[(string, seq[string])] |
217 | |
1479 | 218 for extension in extensions.findAll("extension"): |
219 let extNum = extension.attr("number") | |
1486 | 220 extensionLoaders.add (extension.attr("name"), newSeq[string]()) |
221 for c in extension.findAll("command"): | |
222 if "Video" notin c.attr("name"): | |
223 extensionLoaders[^1][1].add c.attr("name") | |
224 | |
1479 | 225 for extendenum in extension.findAll("enum"): |
226 if extendenum.attr("extends") != "": | |
227 if extendenum.attr("extnumber") == "": | |
228 extendenum.attrs["extnumber"] = extNum | |
229 enums[extendenum.attr("extends")].addValue(extendenum) | |
230 | |
1481 | 231 let outPath = (system.currentSourcePath.parentDir() / "vkapi.nim") |
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
232 let outFile = open(outPath, fmWrite) |
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
233 |
1479 | 234 # generate core types =============================================================================== |
235 # preamble, much easier to hardcode than to generate from xml | |
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
236 outFile.writeLine """ |
1481 | 237 |
1484 | 238 import std/dynlib |
1486 | 239 import std/strutils |
240 import std/tables | |
1484 | 241 |
1481 | 242 import ../semicongine/thirdparty/winim/winim/inc/winbase |
243 import ../semicongine/thirdparty/winim/winim/inc/windef | |
244 import ../semicongine/thirdparty/x11/xlib | |
245 import ../semicongine/thirdparty/x11/x | |
246 import ../semicongine/thirdparty/x11/xrandr | |
247 | |
1479 | 248 func VK_MAKE_API_VERSION*( |
249 variant: uint32, major: uint32, minor: uint32, patch: uint32 | |
1485 | 250 ): uint32 = |
1479 | 251 (variant shl 29) or (major shl 22) or (minor shl 12) or patch |
252 """ | |
253 | |
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
254 outFile.writeLine "type" |
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
255 outFile.writeLine """ |
1481 | 256 # some unused native types |
257 # | |
258 # android | |
259 ANativeWindow = object | |
260 AHardwareBuffer = object | |
261 | |
1483 | 262 # apple/metal |
1481 | 263 CAMetalLayer = object |
264 MTLSharedEvent_id = object | |
1483 | 265 MTLDevice_id = object |
266 MTLCommandQueue_id = object | |
267 MTLBuffer_id = object | |
268 MTLTexture_id = object | |
269 IOSurfaceRef = object | |
1481 | 270 |
271 # wayland | |
272 wl_display = object | |
273 wl_surface = object | |
274 | |
275 # XCB | |
276 xcb_connection_t = object | |
277 xcb_window_t = object | |
278 xcb_visualid_t = object | |
279 | |
280 # directfb | |
281 IDirectFB = object | |
282 IDirectFBSurface = object | |
283 | |
284 # Zircon | |
285 zx_handle_t = object | |
286 | |
287 # GGP C | |
288 GgpStreamDescriptor = object | |
289 GgpFrameToken = object | |
290 | |
291 # Screen (nintendo switch?) | |
292 screen_context = object | |
293 screen_window = object | |
294 screen_buffer = object | |
295 | |
296 # Nvidia | |
297 NvSciSyncAttrList = object | |
298 NvSciSyncObj = object | |
299 NvSciSyncFence = object | |
300 NvSciBufAttrList = object | |
301 NvSciBufObj = object | |
302 | |
303 # some base vulkan base types | |
1483 | 304 VkSampleMask* = distinct uint32 |
305 VkBool32* = distinct uint32 | |
306 VkFlags* = distinct uint32 | |
307 VkFlags64* = distinct uint64 | |
308 VkDeviceSize* = distinct uint64 | |
309 VkDeviceAddress* = distinct uint64 | |
310 VkRemoteAddressNV* = pointer | |
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
311 """ |
1479 | 312 |
313 # generate consts =============================================================================== | |
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
314 outFile.writeLine "const" |
1479 | 315 for c in consts: |
316 var value = c.value | |
317 if value.endsWith("U"): | |
318 value = value[0 ..^ 2] & "'u32" | |
319 elif value.endsWith("ULL"): | |
320 value = value[0 ..^ 4] & "'u64" | |
321 if value[0] == '~': | |
322 value = "not " & value[1 ..^ 1] | |
1486 | 323 if c.name in ["VK_TRUE", "VK_FALSE"]: |
324 outFile.writeLine &" {c.name}*: VkBool32 = VkBool32({value})" | |
325 else: | |
326 outFile.writeLine &" {c.name}*: {c.datatype} = {value}" | |
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
327 outFile.writeLine "" |
1479 | 328 |
329 # generate enums =============================================================================== | |
1481 | 330 const nameCollisions = [ |
331 "VK_PIPELINE_CACHE_HEADER_VERSION_ONE", | |
332 "VK_PIPELINE_CACHE_HEADER_VERSION_SAFETY_CRITICAL_ONE", | |
333 "VK_DEVICE_FAULT_VENDOR_BINARY_HEADER_VERSION_ONE_EXT", | |
334 ] | |
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
335 outFile.writeLine "type" |
1482 | 336 |
1479 | 337 for edef in enums.values(): |
338 if edef.values.len > 0: | |
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
339 outFile.writeLine &" {edef.name}* {{.size: 4.}} = enum" |
1479 | 340 for ee in edef.values: |
1481 | 341 # due to the nim identifier-system, there might be collisions between typenames and enum-member names |
342 if ee.name in nameCollisions: | |
343 outFile.writeLine &" {ee.name}_VALUE = {ee.value}" | |
344 else: | |
345 outFile.writeLine &" {ee.name} = {ee.value}" | |
346 | |
347 outFile.writeLine "" | |
348 | |
349 # generate types =============================================================================== | |
1486 | 350 var stringConverters: seq[string] |
1481 | 351 for t in types: |
352 let category = t.attr("category") | |
1483 | 353 let tName = t.attr("name") |
354 if tName.startsWith("VkVideo"): # we are not doing the video API, sorry | |
355 continue | |
356 if tName.startsWith("VkPhysicalDeviceVideo"): # we are not doing the video API, sorry | |
357 continue | |
1481 | 358 if t.attr("api") == "vulkansc": |
359 continue | |
360 elif t.attr("deprecated") == "true": | |
361 continue | |
362 elif category == "include": | |
363 continue | |
364 elif category == "define": | |
365 continue | |
366 elif t.attr("requires").startsWith("vk_video"): | |
367 continue | |
368 elif t.attr("alias") != "": | |
369 let a = t.attr("alias") | |
1483 | 370 outFile.writeLine &" {tName}* = {a}" |
1481 | 371 elif category == "bitmask": |
372 if t.len > 0 and t[0].text.startsWith("typedef"): | |
1486 | 373 outFile.writeLine &" {t[2][0].text.strip()}* = distinct {t[1][0].text.strip()}" |
1481 | 374 elif category == "union": |
1483 | 375 outFile.writeLine &" {tName}* {{.union.}} = object" |
1481 | 376 for member in t.findAll("member"): |
377 outFile.writeLine &" {member.memberDecl()}" | |
378 elif category == "handle": | |
1486 | 379 outFile.writeLine &" {t[2][0].text.strip()} = distinct pointer" |
380 stringConverters.add t[2][0].text.strip() | |
1481 | 381 elif category == "struct": |
1483 | 382 outFile.writeLine &" {tName}* = object" |
1481 | 383 for member in t.findAll("member"): |
384 if member.attr("api") == "vulkansc": | |
385 continue | |
386 outFile.writeLine &" {member.memberDecl()}" | |
387 elif category == "funcpointer": | |
388 assert t[0].text.startsWith("typedef ") | |
1483 | 389 let retName = t[0].text[8 ..< ^13].strip() |
390 let funcName = t.findAll("name")[0][0].text | |
391 | |
392 outFile.write &" {funcname}* = proc(" | |
1484 | 393 let nParams = (t.len - 3) div 2 |
394 for i in 0 ..< nParams: | |
395 assert t[i * 2 + 3].tag == "type" | |
396 let typename = t[i * 2 + 3][0].text.strip() | |
397 var identifier = t[i * 2 + 4].text.strip(chars = {' ', ')', ';', ',', '\n'}) | |
398 var pointerType = if identifier.startsWith("*"): 1 else: 0 | |
399 if pointerType > 0: | |
400 identifier = identifier[1 .. ^1].strip(chars = {' ', ')', ';', ',', '\n'}) | |
401 if identifier.endsWith("const"): | |
402 identifier = identifier[0 .. ^6].strip(chars = {' ', ')', ';', ',', '\n'}) | |
403 identifier = identifier.strip(chars = {','}) | |
404 outFile.write &"{doIdentifier(identifier)}: {doTypename(typename, pointerType)}, " | |
1483 | 405 |
1484 | 406 if retName == "void": |
1483 | 407 outFile.writeLine &") {{.cdecl.}}" |
1484 | 408 elif retName == "void*": |
1483 | 409 outFile.writeLine &"): pointer {{.cdecl.}}" |
410 else: | |
1484 | 411 outFile.writeLine &"): {doTypename(retName, 0)} {{.cdecl.}}" |
1481 | 412 else: |
413 doAssert category in ["", "basetype", "enum"], "unknown type category: " & category | |
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
414 outFile.writeLine "" |
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
415 |
1483 | 416 for command in commands: |
417 if command.attr("api") == "vulkansc": | |
418 continue | |
419 if command.attr("alias") != "": | |
420 let funcName = command.attr("name") | |
421 let funcAlias = command.attr("alias") | |
1485 | 422 outFile.write &"var {funcName}* = {funcAlias}\n" |
1483 | 423 continue |
424 | |
425 let proto = command.findAll("proto")[0] | |
426 let retType = proto.findAll("type")[0][0].text.strip() | |
427 let funcName = proto.findAll("name")[0][0].text.strip() | |
428 | |
429 if "Video" in funcName: # Video API not supported at this time | |
430 continue | |
431 | |
1485 | 432 outFile.write &"var {funcName}*: proc(" |
1483 | 433 for param in command: |
434 if param.tag != "param": | |
435 continue | |
436 if param.attr("api") == "vulkansc": | |
437 continue | |
438 assert param.len in [2, 3, 4] | |
439 let paramType = param.findAll("type")[0][0].text | |
440 let paramName = param.findAll("name")[0][0].text | |
441 assert "*" notin paramType, $param | |
1484 | 442 |
443 if param.len == 4: | |
444 param.delete(0) | |
445 | |
446 var pointerType = 0 | |
447 | |
448 if param.len == 3: | |
449 if param[param.len - 1].kind == xnText: | |
450 assert param[param.len - 1].text[^1] == ']' | |
451 else: | |
452 assert param[0].tag == "type" | |
453 assert param[param.len - 1].tag == "name" | |
454 if param[1].text.strip() == "*": | |
455 pointerType = 1 | |
456 elif param[1].text.strip() == "**": | |
457 pointerType = 2 | |
458 # echo "3: ", param[1].text, " ", paramType, " ", paramName | |
459 outFile.write &"{doIdentifier(paramName)}: {doTypename(paramType, pointerType)}, " | |
1483 | 460 |
461 outFile.write &")" | |
462 if retType != "void": | |
463 assert "*" notin retType | |
1484 | 464 outFile.write &": {doTypename(retType, 0)}" |
1483 | 465 outFile.write " {.stdcall.}\n" |
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
466 |
1484 | 467 outFile.write """ |
1485 | 468 |
469 proc loadFunc[T](instance: VkInstance, f: var T, name: string) = | |
470 f = cast[T](vkGetInstanceProcAddr(instance, name)) | |
471 | |
1486 | 472 proc initVulkanLoader*() = |
473 if vkGetInstanceProcAddr != nil: | |
474 return | |
475 | |
476 when defined(linux): | |
477 let vulkanLib = loadLib("libvulkan.so.1") | |
478 when defined(windows): | |
479 let vulkanLib = loadLib("vulkan-1.dll") | |
480 if vulkanLib == nil: | |
481 raise newException(Exception, "Unable to load vulkan library") | |
482 | |
483 # init two global functions | |
484 vkGetInstanceProcAddr = cast[proc(instance: VkInstance, pName: cstring, ): PFN_vkVoidFunction {.stdcall.}](checkedSymAddr(vulkanLib, "vkGetInstanceProcAddr")) | |
485 | |
486 loadFunc(VkInstance(nil), vkCreateInstance, "vkCreateInstance") | |
487 | |
1485 | 488 """ |
489 | |
490 for f in features: | |
491 let name = f.attr("name").replace(",", "_") | |
492 if f.attr("struct") != "": | |
493 continue | |
1486 | 494 outFile.writeLine &"proc load_{name}(instance: VkInstance) =" |
1485 | 495 var hasEntries = false |
496 for cmd in f.findAll("command"): | |
1486 | 497 if cmd.attr("name") == "vkCreateInstance": |
498 continue | |
1485 | 499 hasEntries = true |
500 let cName = cmd.attr("name") | |
501 outFile.writeLine &" loadFunc(instance, {cName}, \"{cName}\")" | |
502 if not hasEntries: | |
503 outFile.writeLine " discard" | |
504 outFile.writeLine "" | |
1484 | 505 |
1486 | 506 for (extName, commands) in extensionLoaders: |
507 outFile.writeLine &"proc load_{extName}(instance: VkInstance) =" | |
508 for c in commands: | |
509 outFile.writeLine &" loadFunc(instance, {c}, \"{c}\")" | |
510 if commands.len == 0: | |
511 outFile.writeLine &" discard" | |
512 outFile.writeLine "" | |
513 | |
514 outFile.writeLine "const EXTENSION_LOADERS = {" | |
515 for (extName, commands) in extensionLoaders: | |
516 outFile.writeLine &" \"{extName}\": load_{extName}," | |
517 outFile.writeLine "}.toTable" | |
518 outFile.writeLine "" | |
519 | |
520 outFile.writeLine "proc loadExtension*(instance: VkInstance, name: string) =" | |
521 outFile.writeLine " assert name in EXTENSION_LOADERS" | |
522 outFile.writeLine " EXTENSION_LOADERS[name](instance)" | |
523 outFile.writeLine "" | |
524 | |
525 for strCon in stringConverters: | |
526 outFile.writeLine &"""proc `$`*(v: {strCon}): string = "0x" & cast[uint](v).toHex()""" | |
527 outFile.writeLine "" | |
528 | |
529 # we preload the vkCreateInstance function, so we can create an instance | |
530 outFile.writeLine "" | |
531 | |
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
532 outFile.close() |
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
533 |
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
534 assert execCmd("nim c " & outPath) == 0 |