annotate src/vulkan_api/vulkan_api_generator.nim @ 83:5e19aead2b61

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