annotate src/vulkan_api/vulkan_api_generator.nim @ 206:7f921d7d0a2b

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