annotate src/vulkan_api/vulkan_api_generator.nim @ 547:c3b6981764a5

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