annotate src/vulkan_api/vulkan_api_generator.nim @ 84:8412f433dc46

fix: tons of errors in wrapper generator, can now compile, extension function not loaded yet it seems
author Sam <sam@basx.dev>
date Fri, 24 Feb 2023 01:32:45 +0700
parents 5e19aead2b61
children e872cf354110
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
1 import std/os
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
2 import std/sugar
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
3 import std/algorithm
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
4 import std/strformat
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
5 import std/strutils
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
6 import std/sequtils
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
7 import std/streams
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
8 import std/tables
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
9 import httpClient
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
10 import std/xmlparser
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
11 import std/xmltree
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
12
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
13 const
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
14 TYPEMAP = {
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
15 "void": "void",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
16 "char": "char",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
17 "float": "float32",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
18 "double": "float64",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
19 "int8_t": "int8",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
20 "uint8_t": "uint8",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
21 "int16_t": "int16",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
22 "uint16_t": "uint16",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
23 "int32_t": "int32",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
24 "uint32_t": "uint32",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
25 "uint64_t": "uint64",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
26 "int64_t": "int64",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
27 "size_t": "csize_t",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
28 "int": "cint",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
29 "void*": "pointer",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
30 "char*": "cstring",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
31 "ptr char": "cstring",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
32 "ptr void": "pointer",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
33 "VK_DEFINE_HANDLE": "VkHandle",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
34 "VK_DEFINE_NON_DISPATCHABLE_HANDLE": "VkNonDispatchableHandle",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
35 }.toTable
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
36 PLATFORM_HEADER_MAP = {
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
37 "X11/Xlib.h": @["xlib", "xlib_xrandr"],
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
38 "X11/extensions/Xrandr.h": @["xlib_xrandr"],
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
39 "wayland-client.h": @["wayland"],
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
40 "windows.h": @["win32"],
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
41 "xcb/xcb.h": @["xcb"],
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
42 "directfb.h": @["directfb"],
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
43 "zircon/types.h": @["fuchsia"],
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
44 "ggp_c/vulkan_types.h": @["ggp"],
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
45 "screen/screen.h": @["screen"],
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
46 "nvscisync.h": @["sci"],
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
47 "nvscibuf.h": @["sci"],
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
48 "vk_video/vulkan_video_codec_h264std.h": @["provisional"],
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
49 "vk_video/vulkan_video_codec_h264std_decode.h": @["provisional"],
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
50 "vk_video/vulkan_video_codec_h264std_encode.h": @["provisional"],
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
51 "vk_video/vulkan_video_codec_h265std.h": @["provisional"],
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
52 "vk_video/vulkan_video_codec_h265std_decode.h": @["provisional"],
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
53 "vk_video/vulkan_video_codec_h265std_encode.h": @["provisional"],
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
54 }.toTable
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
55 MAP_KEYWORD = {
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
56 "object": "theobject",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
57 "type": "thetype",
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
58 }.toTable
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
59 SPECIAL_DEPENDENCIES = {
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
60 "VK_NV_ray_tracing": "VK_KHR_ray_tracing_pipeline",
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
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
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
71
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
72 # helpers
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
73 func mapType(typename: string): auto =
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
74 TYPEMAP.getOrDefault(typename.strip(), typename.strip()).strip(chars={'_'})
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
75 func mapName(thename: string): auto =
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
76 MAP_KEYWORD.getOrDefault(thename.strip(), thename.strip()).strip(chars={'_'})
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
77 func smartParseInt(value: string): int =
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
78 if value.startsWith("0x"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
79 parseHexInt(value)
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
80 else:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
81 parseInt(value)
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
82 func hasAttr(node: XmlNode, attr: string): bool = node.attr(attr) != ""
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
83 func tableSorted(table: Table[int, string]): seq[(int, string)] =
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
84 result = toSeq(table.pairs)
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
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
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
128
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
129 # serializers
80
637da715b604 add: fix name clashes
Sam <sam@basx.dev>
parents: 79
diff changeset
130 func serializeEnum(node: XmlNode, api: XmlNode): seq[string] =
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
131 let name = node.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
132 if name == "":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
133 return result
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
134
80
637da715b604 add: fix name clashes
Sam <sam@basx.dev>
parents: 79
diff changeset
135 var reservedNames: seq[string]
637da715b604 add: fix name clashes
Sam <sam@basx.dev>
parents: 79
diff changeset
136 for t in api.findAll("type"):
637da715b604 add: fix name clashes
Sam <sam@basx.dev>
parents: 79
diff changeset
137 reservedNames.add t.attr("name").replace("_", "").toLower()
637da715b604 add: fix name clashes
Sam <sam@basx.dev>
parents: 79
diff changeset
138
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
139 # find additional enum defintion in feature definitions
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
140 var values: Table[int, string]
80
637da715b604 add: fix name clashes
Sam <sam@basx.dev>
parents: 79
diff changeset
141 for feature in api.findAll("feature"):
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
142 for require in feature.findAll("require"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
143 for theenum in require.findAll("enum"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
144 if theenum.attr("extends") == name:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
145 if theenum.hasAttr("offset"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
146 let enumBase = 1000000000 + (smartParseInt(theenum.attr("extnumber")) - 1) * 1000
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
147 var value = smartParseInt(theenum.attr("offset")) + enumBase
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
148 if theenum.attr("dir") == "-":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
149 value = -value
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
150 values[value] = theenum.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
151 elif theenum.hasAttr("value"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
152 var value = smartParseInt(theenum.attr("value"))
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
153 if theenum.attr("dir") == "-":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
154 value = -value
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
155 values[value] = theenum.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
156 elif theenum.hasAttr("bitpos"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
157 var value = smartParseInt(theenum.attr("bitpos"))
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
158 if theenum.attr("dir") == "-":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
159 value = -value
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
160 values[value] = theenum.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
161 elif theenum.hasAttr("alias"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
162 discard
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
163 else:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
164 raise newException(Exception, &"Unknown extension value: {feature}\nvalue:{theenum}")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
165 # find additional enum defintion in extension definitions
80
637da715b604 add: fix name clashes
Sam <sam@basx.dev>
parents: 79
diff changeset
166 for extension in api.findAll("extension"):
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
167 let extensionNumber = parseInt(extension.attr("number"))
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
168 let enumBase = 1000000000 + (extensionNumber - 1) * 1000
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
169 for require in extension.findAll("require"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
170 for theenum in require.findAll("enum"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
171 if theenum.attr("extends") == name:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
172 if theenum.hasAttr("offset"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
173 if theenum.hasAttr("extnumber"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
174 let otherBase = 1000000000 + (smartParseInt(theenum.attr("extnumber")) - 1) * 1000
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
175 var value = smartParseInt(theenum.attr("offset")) + otherBase
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
176 if theenum.attr("dir") == "-":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
177 value = -value
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
178 values[value] = theenum.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
179 else:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
180 var value = smartParseInt(theenum.attr("offset")) + enumBase
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
181 if theenum.attr("dir") == "-":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
182 value = -value
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
183 values[value] = theenum.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
184 elif theenum.hasAttr("value"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
185 var value = smartParseInt(theenum.attr("value"))
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
186 if theenum.attr("dir") == "-":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
187 value = -value
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
188 values[value] = theenum.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
189 elif theenum.hasAttr("bitpos"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
190 var value = smartParseInt(theenum.attr("bitpos"))
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
191 if theenum.attr("dir") == "-":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
192 value = -value
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
193 values[value] = theenum.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
194 elif theenum.hasAttr("alias"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
195 discard
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
196 else:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
197 raise newException(Exception, &"Unknown extension value: {extension}\nvalue:{theenum}")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
198
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
199 # generate enums
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
200 if node.attr("type") == "enum":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
201 for value in node.findAll("enum"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
202 if value.hasAttr("alias"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
203 continue
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
204 if value.attr("value").startsWith("0x"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
205 values[parseHexInt(value.attr("value"))] = value.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
206 else:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
207 values[smartParseInt(value.attr("value"))] = value.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
208 if values.len > 0:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
209 result.add " " & name & "* {.size: sizeof(cint).} = enum"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
210 for (value, name) in tableSorted(values):
80
637da715b604 add: fix name clashes
Sam <sam@basx.dev>
parents: 79
diff changeset
211 var thename = name
637da715b604 add: fix name clashes
Sam <sam@basx.dev>
parents: 79
diff changeset
212 if name.replace("_", "").toLower() in reservedNames:
637da715b604 add: fix name clashes
Sam <sam@basx.dev>
parents: 79
diff changeset
213 thename = thename & "_ENUM"
637da715b604 add: fix name clashes
Sam <sam@basx.dev>
parents: 79
diff changeset
214 let enumEntry = &" {thename} = {value}"
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
215 result.add enumEntry
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
216
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
217 # generate bitsets (normal enums in the C API, but bitfield-enums in Nim)
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
218 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
219 var predefined_enum_sets: seq[string]
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
220 for value in node.findAll("enum"):
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
221 if value.hasAttr("bitpos"):
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
222 values[smartParseInt(value.attr("bitpos"))] = value.attr("name")
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
223 elif node.attr("name") == "VkVideoEncodeRateControlModeFlagBitsKHR": # special exception, for some reason this has values instead of bitpos
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
224 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
225 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
226 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
227
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
228 if values.len > 0:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
229 if node.hasAttr("bitwidth"):
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
230 result.add " " & name & "* {.size: 8.} = enum"
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
231 else:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
232 result.add " " & name & "* {.size: sizeof(cint).} = enum"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
233 for (bitpos, enumvalue) in tableSorted(values):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
234 var value = "00000000000000000000000000000000"# makes the bit mask nicely visible
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
235 if node.hasAttr("bitwidth"): # assumes this is always 64
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
236 value = value & value
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
237 value[^(bitpos + 1)] = '1'
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
238 let enumEntry = &" {enumvalue} = 0b{value}"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
239 if not (enumEntry in result): # the specs define duplicate entries for backwards compat
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
240 result.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
241 let cApiName = name.replace("FlagBits", "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
242 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
243 if values.len > 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
244 result.add &"""converter BitsetToNumber*(flags: openArray[{name}]): {cApiName} =
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 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
246 result = {cApiName}(int64(result) or int64(flag))"""
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 result.add &"""converter NumberToBitset*(number: {cApiName}): seq[{name}] =
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
248 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
249 if (value.ord and int64(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
250 result.add 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
251 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
252 if values.len > 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
253 result.add &"""func toBits*(flags: openArray[{name}]): {cApiName} =
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 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
255 result = {cApiName}(uint(result) or uint(flag))"""
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
256 result.add &"""func toEnums*(number: {cApiName}): seq[{name}] =
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 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
258 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
259 result.add 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
260 if predefined_enum_sets.len > 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
261 result.add "const"
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 predefined_enum_sets
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
263 result.add "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
264
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
265
80
637da715b604 add: fix name clashes
Sam <sam@basx.dev>
parents: 79
diff changeset
266 func serializeStruct(node: XmlNode): seq[string] =
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
267 let name = node.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
268 var union = ""
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
269 if node.attr("category") == "union":
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
270 union = "{.union.} "
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
271 result.add &" {name}* {union}= object"
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
272 for member in node.findAll("member"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
273 if not member.hasAttr("api") or member.attr("api") == "vulkan":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
274 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
275 result.add &" {mapName(fieldname)}*: {findType(member)}"
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
276
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
277 func serializeFunctiontypes(api: XmlNode): seq[string] =
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
278 for node in api.findAll("type"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
279 if node.attr("category") == "funcpointer":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
280 let name = node[1][0]
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
281 let returntype = mapType(node[0].text[8 .. ^1].split('(', 1)[0])
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
282 var params: seq[string]
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
283 for i in countup(3, node.len - 1, 2):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
284 var paramname = node[i + 1].text.split(',', 1)[0].split(')', 1)[0]
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
285 var paramtype = node[i][0].text
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
286 if paramname[0] == '*':
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
287 paramname = paramname.rsplit(" ", 1)[1]
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
288 paramtype = "ptr " & paramtype
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
289 paramname = mapName(paramname)
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
290 params.add &"{paramname}: {mapType(paramtype)}"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
291 let paramsstr = params.join(", ")
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
292 result.add(&" {name}* = proc({paramsstr}): {returntype} {{.cdecl.}}")
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
293
83
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
294 func serializeConsts(api: XmlNode): seq[string] =
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
295 result = @["const"]
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
296 for enums in api.findAll("enums"):
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
297 if enums.attr("name") == "API Constants":
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
298 for theenum in enums.findAll("enum"):
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
299 if theenum.hasAttr("alias"):
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
300 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
301 else:
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
302 var value = theenum.attr("value").strip(chars={'(', ')'})
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
303 if value.endsWith("U"):
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
304 value = value[0..^2] & "'u32"
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
305 elif value.endsWith("ULL"):
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
306 value = value[0..^4] & "'u64"
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
307 if value[0] == '~':
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
308 value = "not " & value[1..^1]
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
309 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
310
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
311 func serializeType(node: XmlNode, headerTypes: Table[string, string]): Table[string, seq[string]] =
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
312 if node.attrsLen == 0:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
313 return
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
314 if node.attr("requires") == "vk_platform" or node.attr("category") == "include":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
315 return
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
316 result["basetypes"] = @[]
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
317 result["enums"] = @[]
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
318
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
319 # include-defined types (in platform headers)
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
320 if node.attr("name") in headerTypes:
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
321 for platform in PLATFORM_HEADER_MAP[node.attr("requires")]:
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
322 let platformfile = "platform/" & platform
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
323 if not result.hasKey(platformfile):
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
324 result[platformfile] = @[]
83
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
325 result[platformfile].add " " & node.attr("name").strip(chars={'_'}) & " *{.header: \"" & node.attr("requires") & "\".} = object"
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
326 # generic base types
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
327 elif node.attr("category") == "basetype":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
328 let typechild = node.child("type")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
329 let namechild = node.child("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
330 if typechild != nil and namechild != nil:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
331 var typename = typechild[0].text
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
332 if node[2].kind == xnText and node[2].text.strip() == "*":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
333 typename = &"ptr {typename}"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
334 result["basetypes"].add &" {namechild[0].text}* = {mapType(typename)}"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
335 elif namechild != nil:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
336 result["basetypes"].add &" {namechild[0].text}* = object"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
337 # function pointers need to be handled with structs
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
338 elif node.attr("category") == "funcpointer":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
339 discard
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
340 # preprocessor defines, ignored
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
341 elif node.attr("category") == "define":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
342 discard
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
343 # bitmask aliases
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
344 elif node.attr("category") == "bitmask":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
345 if node.hasAttr("alias"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
346 let name = node.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
347 let alias = node.attr("alias")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
348 result["enums"].add &" {name}* = {alias}"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
349 # distinct resource ID types aka handles
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
350 elif node.attr("category") == "handle":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
351 if not node.hasAttr("alias"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
352 let name = node.child("name")[0].text
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
353 var thetype = mapType(node.child("type")[0].text)
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
354 result["basetypes"].add &" {name}* = distinct {thetype}"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
355 # enum aliases
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
356 elif node.attr("category") == "enum":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
357 if node.hasAttr("alias"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
358 let name = node.attr("name")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
359 let alias = node.attr("alias")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
360 result["enums"].add &" {name}* = {alias}"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
361 else:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
362 discard
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
363
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
364 func serializeCommand(node: XmlNode): (string, string) =
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
365 let
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
366 proto = node.child("proto")
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
367 resulttype = mapType(proto.child("type")[0].text)
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
368 name = proto.child("name")[0].text
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
369 var params: seq[string]
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
370 for param in node:
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
371 if param.tag == "param" and param.attr("api") in ["", "vulkan"]:
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
372 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
373 params.add &"{mapName(fieldname)}: {findType(param)}"
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
374 let allparams = params.join(", ")
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
375 return (name, &"proc({allparams}): {resulttype} {{.stdcall.}}")
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
376
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
377
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
378 proc update(a: var Table[string, seq[string]], b: Table[string, seq[string]]) =
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
379 for k, v in b.pairs:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
380 if not a.hasKey(k):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
381 a[k] = @[]
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
382 a[k].add v
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
383
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
384
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
385 proc main() =
81
fa1b6107deae did: final cleanup for now
Sam <sam@basx.dev>
parents: 80
diff changeset
386 let file = getTempDir() / "vk.xml"
fa1b6107deae did: final cleanup for now
Sam <sam@basx.dev>
parents: 80
diff changeset
387 if not os.fileExists(file):
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
388 let client = newHttpClient()
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
389 let glUrl = "https://raw.githubusercontent.com/KhronosGroup/Vulkan-Docs/main/xml/vk.xml"
81
fa1b6107deae did: final cleanup for now
Sam <sam@basx.dev>
parents: 80
diff changeset
390 client.downloadFile(glUrl, file)
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
391
81
fa1b6107deae did: final cleanup for now
Sam <sam@basx.dev>
parents: 80
diff changeset
392 let api = loadXml(file)
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
393
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
394 const outdir = "src/vulkan_api/output"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
395 removeDir outdir
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
396 createDir outdir
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
397 createDir outdir / "platform"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
398
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
399 # index all names that are only available on certain platforms
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
400 var platformTypes: Table[string, string]
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
401 for extension in api.findAll("extension"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
402 if extension.hasAttr("platform"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
403 for thetype in extension.findAll("type"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
404 platformTypes[thetype.attr("name")] = extension.attr("platform")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
405 for command in extension.findAll("command"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
406 platformTypes[command.attr("name")] = extension.attr("platform")
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
407 elif extension.attr("name").startsWith("VK_KHR_video"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
408 for thetype in extension.findAll("type"):
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
409 platformTypes[thetype.attr("name")] = "provisional"
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
410 for command in extension.findAll("command"):
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
411 platformTypes[command.attr("name")] = "provisional"
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
412
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
413 var outputFiles = {
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
414 "basetypes": @[
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
415 "import std/dynlib",
83
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
416 "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
417 "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
418 "import std/logging",
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
419 "import std/macros",
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
420 "import std/private/digitsutils",
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 "from typetraits import HoleyEnum",
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
422 "type",
83
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
423 " VkHandle* = distinct uint",
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
424 " VkNonDispatchableHandle* = distinct uint",
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
425 "when defined(linux):",
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
426 " let vulkanLib* = loadLib(\"libvulkan.so.1\")",
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
427 "when defined(windows):",
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
428 " let vulkanLib* = loadLib(\"vulkan-1.dll\")",
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
429 "if vulkanLib == nil:",
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
430 " raise newException(Exception, \"Unable to load vulkan library\")",
80
637da715b604 add: fix name clashes
Sam <sam@basx.dev>
parents: 79
diff changeset
431 "func VK_MAKE_API_VERSION*(variant: uint32, major: uint32, minor: uint32, patch: uint32): uint32 {.compileTime.} =",
637da715b604 add: fix name clashes
Sam <sam@basx.dev>
parents: 79
diff changeset
432 " (variant shl 29) or (major shl 22) or (minor shl 12) or patch",
637da715b604 add: fix name clashes
Sam <sam@basx.dev>
parents: 79
diff changeset
433 "",
81
fa1b6107deae did: final cleanup for now
Sam <sam@basx.dev>
parents: 80
diff changeset
434 """template checkVkResult*(call: untyped) =
fa1b6107deae did: final cleanup for now
Sam <sam@basx.dev>
parents: 80
diff changeset
435 when defined(release):
fa1b6107deae did: final cleanup for now
Sam <sam@basx.dev>
parents: 80
diff changeset
436 discard call
fa1b6107deae did: final cleanup for now
Sam <sam@basx.dev>
parents: 80
diff changeset
437 else:
fa1b6107deae did: final cleanup for now
Sam <sam@basx.dev>
parents: 80
diff changeset
438 # yes, a bit cheap, but this is only for nice debug output
fa1b6107deae did: final cleanup for now
Sam <sam@basx.dev>
parents: 80
diff changeset
439 var callstr = astToStr(call).replace("\n", "")
fa1b6107deae did: final cleanup for now
Sam <sam@basx.dev>
parents: 80
diff changeset
440 while callstr.find(" ") >= 0:
fa1b6107deae did: final cleanup for now
Sam <sam@basx.dev>
parents: 80
diff changeset
441 callstr = callstr.replace(" ", " ")
fa1b6107deae did: final cleanup for now
Sam <sam@basx.dev>
parents: 80
diff changeset
442 debug "CALLING vulkan: ", callstr
fa1b6107deae did: final cleanup for now
Sam <sam@basx.dev>
parents: 80
diff changeset
443 let value = call
fa1b6107deae did: final cleanup for now
Sam <sam@basx.dev>
parents: 80
diff changeset
444 if value != VK_SUCCESS:
fa1b6107deae did: final cleanup for now
Sam <sam@basx.dev>
parents: 80
diff changeset
445 error "Vulkan error: ", astToStr(call), " returned ", $value
fa1b6107deae did: final cleanup for now
Sam <sam@basx.dev>
parents: 80
diff changeset
446 raise newException(Exception, "Vulkan error: " & astToStr(call) &
fa1b6107deae did: final cleanup for now
Sam <sam@basx.dev>
parents: 80
diff changeset
447 " 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
448 """
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
449 # 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
450 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
451 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
452
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 iterator items[T: HoleyEnum](E: typedesc[T]): T =
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 for a in enumFullRange(E): yield a""",
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
455 ],
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
456 "structs": @["type"],
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
457 "enums": @["type"],
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
458 "commands": @[],
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
459 }.toTable
83
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
460 outputFiles["basetypes"].add serializeConsts(api)
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
461 outputFiles["basetypes"].add "type"
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
462
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
463 # enums
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
464 for thetype in api.findAll("type"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
465 if thetype.attr("category") == "bitmask" and not thetype.hasAttr("alias") and (not thetype.hasAttr("api") or thetype.attr("api") == "vulkan"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
466 let name = thetype.child("name")[0].text
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
467 outputFiles["enums"].add &" {name}* = distinct VkFlags"
83
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
468 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
469 outputFiles["enums"].add "type"
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
470 for theenum in api.findAll("enums"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
471 outputFiles["enums"].add serializeEnum(theenum, api)
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
472
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
473 # structs and function types need to be in same "type" block to avoid forward-declarations
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
474 outputFiles["structs"].add serializeFunctiontypes(api)
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
475 for thetype in api.findAll("type"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
476 if thetype.attr("category") == "struct" or thetype.attr("category") == "union":
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
477 var outfile = "structs"
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
478 if thetype.attr("name") in platformTypes:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
479 outfile = "platform/" & platformTypes[thetype.attr("name")]
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
480 if not (outfile in outputFiles):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
481 outputFiles[outfile] = @[]
80
637da715b604 add: fix name clashes
Sam <sam@basx.dev>
parents: 79
diff changeset
482 outputFiles[outfile].add serializeStruct(thetype)
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
483
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
484 # types
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
485 var headerTypes: Table[string, string]
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
486 for types in api.findAll("types"):
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
487 for thetype in types.findAll("type"):
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
488 if thetype.attrsLen == 2 and thetype.hasAttr("requires") and thetype.hasAttr("name") and thetype.attr("requires") != "vk_platform":
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
489 let name = thetype.attr("name")
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
490 let incld = thetype.attr("requires")
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
491 headerTypes[name] = &"{name} {{.header: \"{incld}\".}} = object"
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
492
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
493 for typesgroup in api.findAll("types"):
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
494 for thetype in typesgroup.findAll("type"):
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
495 outputFiles.update serializeType(thetype, headerTypes)
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
496
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
497 # commands aka functions
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
498 var varDecls: Table[string, string]
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
499 var procLoads: Table[string, string] # procloads need to be packed into feature/extension loader procs
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
500 for commands in api.findAll("commands"):
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
501 for command in commands.findAll("command"):
83
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
502 if command.attr("api") != "vulkansc" and not (command.attr("name") in IGNORED_COMMANDS):
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
503 if command.hasAttr("alias"):
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
504 let name = command.attr("name")
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
505 let alias = command.attr("alias")
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
506 let thetype = varDecls[alias].split(":", 1)[1].strip()
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
507 varDecls[name] = &" {name}*: {thetype}"
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
508 procLoads[name] = &" {name} = {alias}"
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
509 else:
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
510 let (name, thetype) = serializeCommand(command)
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
511 varDecls[name] = &" {name}*: {thetype}"
83
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
512 procLoads[name] = &" {name} = cast[{thetype}](vkGetInstanceProcAddr(instance, \"{name}\"))"
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
513 var declared: seq[string]
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
514 var featureloads: seq[string]
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
515 for feature in api.findAll("feature"):
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
516 if feature.attr("api") in ["vulkan", "vulkan,vulkansc"]:
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
517 let name = feature.attr("name")
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
518 outputFiles["commands"].add &"# feature {name}"
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
519 outputFiles["commands"].add "var"
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
520 for command in feature.findAll("command"):
83
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
521 if not (command.attr("name") in declared) and not (command.attr("name") in IGNORED_COMMANDS):
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
522 outputFiles["commands"].add varDecls[command.attr("name")]
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
523 declared.add command.attr("name")
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
524 featureloads.add &"load{name}"
83
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
525 outputFiles["commands"].add &"proc load{name}*(instance: VkInstance) ="
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
526 for command in feature.findAll("command"):
83
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
527 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
528 outputFiles["commands"].add procLoads[command.attr("name")]
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
529 outputFiles["commands"].add ""
83
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
530 outputFiles["commands"].add ["proc loadVulkan*(instance: VkInstance) ="]
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
531 for l in featureloads:
83
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
532 outputFiles["commands"].add [&" {l}(instance)"]
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
533 outputFiles["commands"].add ""
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
534
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
535 # for promoted extensions, dependants need to call the load-function of the promoted feature/extension
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
536 # use table to store promotions
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
537 var promotions: Table[string, string]
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
538 for extensions in api.findAll("extensions"):
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
539 for extension in extensions.findAll("extension"):
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
540 if extension.hasAttr("promotedto"):
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
541 promotions[extension.attr("name")] = extension.attr("promotedto")
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
542
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
543 var extensionDependencies: Table[string, (seq[string], XmlNode)]
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
544 var features: seq[string]
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
545 for feature in api.findAll("feature"):
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
546 features.add feature.attr("name")
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
547 for extensions in api.findAll("extensions"):
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
548 for extension in extensions.findAll("extension"):
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
549 let name = extension.attr("name")
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
550 extensionDependencies[name] = (@[], extension)
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
551 if extension.hasAttr("depends"):
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
552 extensionDependencies[name] = (extension.attr("depends").split("+"), extension)
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
553 if extension.attr("depends").startsWith("("): # no need for full tree parser, only single place where we can use a feature
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
554 let dependencies = extension.attr("depends").rsplit({')'}, 1)[1][1 .. ^1].split("+")
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
555 extensionDependencies[name] = (dependencies, extension)
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
556 if name in SPECIAL_DEPENDENCIES:
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
557 extensionDependencies[name][0].add SPECIAL_DEPENDENCIES[name]
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
558
83
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
559 # order dependencies to generate them in correct order
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
560 var dependencyOrderedExtensions: OrderedTable[string, XmlNode]
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
561 while extensionDependencies.len > 0:
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
562 var delkeys: seq[string]
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
563 for extensionName, (dependencies, extension) in extensionDependencies.pairs:
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
564 var missingExtension = false
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
565 for dep in dependencies:
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
566 let realdep = promotions.getOrDefault(dep, dep)
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
567 if not (realdep in dependencyOrderedExtensions) and not (realdep in features):
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
568 missingExtension = true
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
569 break
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
570 if not missingExtension:
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
571 dependencyOrderedExtensions[extensionName] = extension
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
572 delkeys.add extensionName
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
573 for key in delkeys:
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
574 extensionDependencies.del key
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
575
83
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
576 var extensionLoaderMap: Table[string, Table[string, string]]
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
577 for extension in dependencyOrderedExtensions.values:
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
578 if extension.hasAttr("promotedto"): # will be loaded in promoted place
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
579 continue
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
580 if extension.attr("supported") in ["", "vulkan", "vulkan,vulkansc"]:
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
581 var file = "commands"
83
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
582 var platform = extension.attr("platform")
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
583 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
584 platform = "provisional"
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
585 if platform != "":
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
586 file = "platform/" & platform
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
587 let name = extension.attr("name")
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
588 if extension.findAll("command").len > 0:
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
589 outputFiles[file].add &"# extension {name}"
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
590 outputFiles[file].add "var"
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
591 for command in extension.findAll("command"):
83
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
592 if not (command.attr("name") in declared) and not (command.attr("name") in IGNORED_COMMANDS):
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
593 outputFiles[file].add varDecls[command.attr("name")]
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
594 declared.add command.attr("name")
83
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
595 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
596 if not (platform in extensionLoaderMap):
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
597 extensionLoaderMap[platform] = Table[string, string]()
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
598 extensionLoaderMap[platform][name] = &"load{name}"
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
599 var addedFunctionBody = false
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
600 if extension.hasAttr("depends"):
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
601 for dependency in extension.attr("depends").split("+"):
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
602 # 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
603 outputFiles[file].add &" load{promotions.getOrDefault(dependency, dependency)}(instance)"
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
604 addedFunctionBody = true
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
605 for command in extension.findAll("command"):
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
606 outputFiles[file].add procLoads[command.attr("name")]
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
607 addedFunctionBody = true
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
608 if not addedFunctionBody:
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
609 outputFiles[file].add " discard"
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
610 outputFiles[file].add ""
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
611
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
612 var mainout: seq[string]
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
613 for section in ["basetypes", "enums", "structs", "commands"]:
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
614 mainout.add outputFiles[section]
83
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
615 mainout.add "var EXTENSION_LOADERS = {"
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
616 for extension, loader in extensionLoaderMap[""].pairs:
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
617 mainout.add &" \"{extension}\": {loader},"
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
618 mainout.add "}.toTable"
80
637da715b604 add: fix name clashes
Sam <sam@basx.dev>
parents: 79
diff changeset
619 for platform in api.findAll("platform"):
637da715b604 add: fix name clashes
Sam <sam@basx.dev>
parents: 79
diff changeset
620 mainout.add &"when defined({platform.attr(\"protect\")}):"
637da715b604 add: fix name clashes
Sam <sam@basx.dev>
parents: 79
diff changeset
621 mainout.add &" include platform/{platform.attr(\"name\")}"
83
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
622 if platform.attr("name") in extensionLoaderMap:
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
623 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
624 mainout.add &" EXTENSION_LOADERS[\"{extension}\"] = {loader}"
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
625
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
626 mainout.add ""
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
627 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
628 mainout.add ""
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
629 mainout.add "# load global functions immediately"
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
630 mainout.add "block globalFunctions:"
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
631 mainout.add " let instance = VkInstance(0)"
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
632 for l in GLOBAL_COMMANDS:
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
633 mainout.add procLoads[l]
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
634 mainout.add ""
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
635 mainout.add "converter VkBool2NimBool*(a: VkBool32): bool = a > 0"
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
636 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
637 mainout.add "proc `$`*(x: uint32): string {.raises: [].} = addInt(result, x)"
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
638
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
639 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
640
83
5e19aead2b61 add: new vulkan api wrapper, not done yet
Sam <sam@basx.dev>
parents: 81
diff changeset
641
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
642 for filename, filecontent in outputFiles.pairs:
79
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
643 if filename.startsWith("platform/"):
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
644 writeFile outdir / &"{filename}.nim", (@[
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
645 "type"
031f241de6ca add: beta version
Sam <sam@basx.dev>
parents: 78
diff changeset
646 ] & filecontent).join("\n")
78
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
647
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
648 when isMainModule:
f67496a189cb add: vulkan api generator, not finished yet
Sam <sam@basx.dev>
parents:
diff changeset
649 main()