Mercurial > games > semicongine
annotate svk/generate.nim @ 1481:a99f3227130c
did: continue on vulkan-api generator
| author | sam <sam@basx.dev> |
|---|---|
| date | Sun, 27 Apr 2025 01:06:59 +0700 |
| parents | 0b302531f5c5 |
| children | bca8f65ed4ed |
| rev | line source |
|---|---|
| 1479 | 1 import std/strtabs |
|
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
2 import std/syncio |
| 1479 | 3 import std/xmltree |
| 4 import std/tables | |
| 5 import std/options | |
| 6 import std/sequtils | |
| 7 import std/strutils | |
| 8 import std/strformat | |
| 9 import std/xmlparser | |
| 10 import std/os | |
|
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
11 import std/osproc |
| 1479 | 12 import std/paths |
| 13 | |
| 14 # helpers | |
| 15 func smartParseInt(value: string): int = | |
| 16 if value.startsWith("0x"): | |
| 17 parseHexInt(value) | |
| 18 else: | |
| 19 parseInt(value) | |
| 20 | |
| 21 const TYPEMAP = { | |
| 22 "void": "void", | |
| 23 "char": "char", | |
| 24 "float": "float32", | |
| 25 "double": "float64", | |
| 26 "int8_t": "int8", | |
| 27 "uint8_t": "uint8", | |
| 28 "int16_t": "int16", | |
| 29 "uint16_t": "uint16", | |
| 30 "int32_t": "int32", | |
| 31 "uint32_t": "uint32", | |
| 32 "uint64_t": "uint64", | |
| 33 "int64_t": "int64", | |
| 34 "size_t": "csize_t", | |
| 35 "int": "cint", | |
| 36 }.toTable | |
| 37 | |
| 38 # load xml | |
| 39 let xml = (system.currentSourcePath.parentDir() / "vk.xml").loadXml() | |
| 40 let platforms = xml.findAll("platforms")[0] | |
| 41 let types = xml.findAll("types")[0] | |
| 42 let xmlenums = xml.findAll("enums") | |
| 43 let commands = xml.findAll("commands")[0] | |
| 44 let features = xml.findAll("feature") # features has extends="<ENUM>" | |
| 45 let extensions = xml.findAll("extensions")[0] # extensions has extends="<ENUM>" | |
| 46 let formats = xml.findAll("formats")[0] | |
| 47 | |
| 48 # gather all enums | |
| 49 | |
| 50 type | |
| 51 EnumEntry = object | |
| 52 name: string | |
| 53 value: string | |
| 54 | |
| 55 EnumDef = object | |
| 56 name: string | |
| 57 values: seq[EnumEntry] | |
| 58 isBitmask: bool | |
| 59 | |
| 60 ConstantDef = object | |
| 61 name: string | |
| 62 datatype: string | |
| 63 value: string | |
| 64 | |
| 65 var consts: seq[ConstantDef] | |
| 66 var enums: Table[string, EnumDef] | |
| 67 | |
| 68 func addValue(edef: var EnumDef, n: XmlNode) = | |
| 69 if n.attr("deprecated") != "aliased" and n.attr("alias") == "": | |
| 70 if n.attr("name") in edef.values.mapIt(it.name): | |
| 71 return | |
| 72 if n.attr("name").endsWith("_EXT") and | |
| 73 n.attr("name")[0 ..< ^4] in edef.values.mapIt(it.name): | |
| 74 return | |
| 75 | |
| 76 var value = "" | |
| 77 if n.attr("value") != "": | |
| 78 value = n.attr("value") | |
| 79 elif n.attr("bitpos") != "": | |
| 80 value = $(1 shl parseInt(n.attr("bitpos"))) | |
| 81 elif n.attr("offset") != "": | |
| 82 var enumBase = 1000000000 | |
| 83 if n.attr("extnumber") != "": | |
| 84 enumBase += (smartParseInt(n.attr("extnumber")) - 1) * 1000 | |
| 85 var v = smartParseInt(n.attr("offset")) + enumBase | |
| 86 if n.attr("dir") == "-": | |
| 87 v = -v | |
| 88 value = $(v) | |
| 89 | |
| 90 edef.values.add EnumEntry(name: n.attr("name"), value: value) | |
| 91 | |
| 1481 | 92 func doTypename(typename: string, isPointer: bool): string = |
| 93 result = TYPEMAP.getOrDefault(typename.strip(), typename.strip()).strip(chars = {'_'}) | |
| 94 | |
| 95 if typename == "void": | |
| 96 assert isPointer | |
| 97 | |
| 98 if isPointer: | |
| 99 if typename == "void": | |
| 100 result = "pointer" | |
| 101 elif typename == "char": | |
| 102 result = "cstring" | |
| 103 else: | |
| 104 result = "ptr " & result | |
| 105 | |
| 106 func doIdentifier(typename: string): string = | |
| 107 if typename in ["type", "object"]: | |
| 108 return &"`{typename}`" | |
| 109 return typename.strip() | |
| 110 | |
| 111 func doMember(typename, theType: string, isPointer: bool, value: string): string = | |
| 112 if value == "": | |
| 113 &"{doIdentifier(typename)}: {doTypename(theType, isPointer)}" | |
| 114 else: | |
| 115 &"{doIdentifier(typename)}: {doTypename(theType, isPointer)} = {value}" | |
| 116 | |
|
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
117 func memberDecl(n: XmlNode): string = |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
118 for i in 0 ..< n.len: |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
119 if n[i].kind == xnElement and n[i].tag == "comment": |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
120 n.delete(i) |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
121 break |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
122 assert n.tag == "member" |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
123 if n.len == 2: |
| 1481 | 124 return doMember(n[1][0].text, n[0][0].text, false, n.attr("values")) |
|
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
125 elif n.len == 3: |
| 1481 | 126 assert "*" notin n[0][0].text.strip() |
|
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
127 if n[1].kind == xnElement and n[1].tag == "name": |
| 1481 | 128 # bitfield |
| 129 if n[2].text.strip().startsWith(":"): | |
| 130 return | |
| 131 &"{doIdentifier(n[1][0].text)} {{.bitsize:{n[2].text.strip()[1 .. ^1]}.}}: {doTypename(n[0][0].text, false)}" | |
| 132 # array definition | |
| 133 elif n[2].text.strip().startsWith("["): | |
| 134 let arrayDim = n[2].text[1 ..< ^1] | |
| 135 if "][" in arrayDim: | |
| 136 let dims = arrayDim.split("][", 1) | |
| 137 let (dim1, dim2) = (dims[0], dims[1]) | |
| 138 return doMember( | |
| 139 n[1][0].text, | |
| 140 &"array[{dim1}, array[{dim2}, {doTypename(n[0][0].text, false)}]]", | |
| 141 false, | |
| 142 n.attr("values"), | |
| 143 ) | |
| 144 else: | |
| 145 return doMember( | |
| 146 n[1][0].text, | |
| 147 &"array[{arrayDim}, {doTypename(n[0][0].text, false)}]", | |
| 148 false, | |
| 149 n.attr("values"), | |
| 150 ) | |
| 151 else: | |
| 152 debugecho n.toSeq | |
| 153 doAssert false, "This should not happen" | |
|
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
154 else: |
| 1481 | 155 # pointer definition |
|
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
156 assert n[1].text.strip() == "*" |
| 1481 | 157 return doMember(n[2][0].text, n[0][0].text, true, n.attr("values")) |
|
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
158 elif n.len == 4: |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
159 if n[0].text.strip() in ["struct", "const struct"]: |
| 1481 | 160 return doMember(n[3][0].text, n[1][0].text, true, n.attr("values")) |
|
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
161 else: |
| 1481 | 162 assert n[0].text.strip() == "const" # can be ignored |
| 163 assert n[1].tag == "type" | |
|
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
164 assert n[2].text.strip() in ["*", "* const *", "* const*"] |
| 1481 | 165 # can be ignored, basically every type is a pointer |
| 166 assert n[3].tag == "name" | |
| 167 assert n[1].len == 1 | |
| 168 assert n[3].len == 1 | |
| 169 return doMember(n[3][0].text, n[1][0].text, true, n.attr("values")) | |
|
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
170 elif n.len in [5, 6]: |
| 1481 | 171 # array definition, using const-value for array length |
| 172 # <type>uint8_t</type>,<name>pipelineCacheUUID</name>[<enum>VK_UUID_SIZE</enum>] | |
| 173 assert n[2].text.strip() == "[" | |
| 174 assert n[4].text.strip() == "]" | |
| 175 return doMember( | |
| 176 n[1][0].text, | |
| 177 &"array[{n[3][0].text}, {doTypename(n[0][0].text, false)}]", | |
| 178 false, | |
| 179 n.attr("values"), | |
| 180 ) | |
|
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
181 assert false |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
182 |
| 1479 | 183 for e in xmlenums: |
| 184 if e.attr("type") == "constants": | |
| 185 for c in e.findAll("enum"): | |
| 186 var value = c.attr("value").strip(chars = {'(', ')'}) | |
| 187 consts.add ConstantDef( | |
| 188 name: c.attr("name"), datatype: TYPEMAP[c.attr("type")], value: value | |
| 189 ) | |
| 190 elif e.attr("type") == "enum": | |
| 191 var edef = EnumDef(name: e.attr("name"), isBitmask: false) | |
| 192 for ee in e.findAll("enum"): | |
| 193 edef.addValue(ee) | |
| 194 enums[edef.name] = edef | |
| 195 elif e.attr("type") == "bitmask": | |
| 196 var edef = EnumDef(name: e.attr("name"), isBitmask: true) | |
| 197 for ee in e.findAll("enum"): | |
| 198 edef.addValue(ee) | |
| 199 enums[edef.name] = edef | |
| 200 | |
| 201 for f in features: | |
| 202 for extendenum in f.findAll("enum"): | |
| 203 if extendenum.attr("extends") != "": | |
| 204 enums[extendenum.attr("extends")].addValue(extendenum) | |
| 205 | |
| 206 for extension in extensions.findAll("extension"): | |
| 207 let extNum = extension.attr("number") | |
| 208 for extendenum in extension.findAll("enum"): | |
| 209 if extendenum.attr("extends") != "": | |
| 210 if extendenum.attr("extnumber") == "": | |
| 211 extendenum.attrs["extnumber"] = extNum | |
| 212 enums[extendenum.attr("extends")].addValue(extendenum) | |
| 213 | |
| 1481 | 214 let outPath = (system.currentSourcePath.parentDir() / "vkapi.nim") |
|
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
215 let outFile = open(outPath, fmWrite) |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
216 |
| 1479 | 217 # generate core types =============================================================================== |
| 218 # preamble, much easier to hardcode than to generate from xml | |
|
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
219 outFile.writeLine """ |
| 1481 | 220 |
| 221 import ../semicongine/thirdparty/winim/winim/inc/winbase | |
| 222 import ../semicongine/thirdparty/winim/winim/inc/windef | |
| 223 import ../semicongine/thirdparty/x11/xlib | |
| 224 import ../semicongine/thirdparty/x11/x | |
| 225 import ../semicongine/thirdparty/x11/xrandr | |
| 226 | |
| 1479 | 227 func VK_MAKE_API_VERSION*( |
| 228 variant: uint32, major: uint32, minor: uint32, patch: uint32 | |
| 229 ): uint32 {.compileTime.} = | |
| 230 (variant shl 29) or (major shl 22) or (minor shl 12) or patch | |
| 231 """ | |
| 232 | |
|
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
233 outFile.writeLine "type" |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
234 outFile.writeLine """ |
| 1481 | 235 # some unused native types |
| 236 # | |
| 237 # android | |
| 238 ANativeWindow = object | |
| 239 AHardwareBuffer = object | |
| 240 | |
| 241 # apple | |
| 242 CAMetalLayer = object | |
| 243 MTLDevice = object | |
| 244 MTLCommandQueue = object | |
| 245 MTLBuffer = object | |
| 246 MTLTexture = object | |
| 247 MTLSharedEvent = object | |
| 248 MTLSharedEvent_id = object | |
| 249 | |
| 250 # wayland | |
| 251 wl_display = object | |
| 252 wl_surface = object | |
| 253 | |
| 254 # XCB | |
| 255 xcb_connection_t = object | |
| 256 xcb_window_t = object | |
| 257 xcb_visualid_t = object | |
| 258 | |
| 259 # directfb | |
| 260 IDirectFB = object | |
| 261 IDirectFBSurface = object | |
| 262 | |
| 263 # Zircon | |
| 264 zx_handle_t = object | |
| 265 | |
| 266 # GGP C | |
| 267 GgpStreamDescriptor = object | |
| 268 GgpFrameToken = object | |
| 269 | |
| 270 # Screen (nintendo switch?) | |
| 271 screen_context = object | |
| 272 screen_window = object | |
| 273 screen_buffer = object | |
| 274 | |
| 275 # Nvidia | |
| 276 NvSciSyncAttrList = object | |
| 277 NvSciSyncObj = object | |
| 278 NvSciSyncFence = object | |
| 279 NvSciBufAttrList = object | |
| 280 NvSciBufObj = object | |
| 281 | |
| 282 # some base vulkan base types | |
|
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
283 VkSampleMask = distinct uint32 |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
284 VkBool32 = distinct uint32 |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
285 VkFlags = distinct uint32 |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
286 VkFlags64 = distinct uint64 |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
287 VkDeviceSize = distinct uint64 |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
288 VkDeviceAddress = distinct uint64 |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
289 VkRemoteAddressNV = pointer |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
290 """ |
| 1479 | 291 |
| 292 # generate consts =============================================================================== | |
|
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
293 outFile.writeLine "const" |
| 1479 | 294 for c in consts: |
| 295 var value = c.value | |
| 296 if value.endsWith("U"): | |
| 297 value = value[0 ..^ 2] & "'u32" | |
| 298 elif value.endsWith("ULL"): | |
| 299 value = value[0 ..^ 4] & "'u64" | |
| 300 if value[0] == '~': | |
| 301 value = "not " & value[1 ..^ 1] | |
|
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
302 outFile.writeLine &" {c.name}*: {c.datatype} = {value}" |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
303 outFile.writeLine "" |
| 1479 | 304 |
| 305 # generate enums =============================================================================== | |
| 1481 | 306 const nameCollisions = [ |
| 307 "VK_PIPELINE_CACHE_HEADER_VERSION_ONE", | |
| 308 "VK_PIPELINE_CACHE_HEADER_VERSION_SAFETY_CRITICAL_ONE", | |
| 309 "VK_DEVICE_FAULT_VENDOR_BINARY_HEADER_VERSION_ONE_EXT", | |
| 310 ] | |
|
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
311 outFile.writeLine "type" |
| 1479 | 312 for edef in enums.values(): |
| 313 if edef.values.len > 0: | |
|
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
314 outFile.writeLine &" {edef.name}* {{.size: 4.}} = enum" |
| 1479 | 315 for ee in edef.values: |
| 1481 | 316 # due to the nim identifier-system, there might be collisions between typenames and enum-member names |
| 317 if ee.name in nameCollisions: | |
| 318 outFile.writeLine &" {ee.name}_VALUE = {ee.value}" | |
| 319 else: | |
| 320 outFile.writeLine &" {ee.name} = {ee.value}" | |
| 321 | |
| 322 outFile.writeLine "" | |
| 323 | |
| 324 # generate types =============================================================================== | |
| 325 for t in types: | |
| 326 let category = t.attr("category") | |
| 327 if t.attr("api") == "vulkansc": | |
| 328 continue | |
| 329 elif t.attr("deprecated") == "true": | |
| 330 continue | |
| 331 elif category == "include": | |
| 332 continue | |
| 333 elif category == "define": | |
| 334 continue | |
| 335 elif t.attr("requires").startsWith("vk_video"): | |
| 336 continue | |
| 337 elif t.attr("alias") != "": | |
| 338 let a = t.attr("alias") | |
| 339 let n = t.attr("name") | |
| 340 outFile.writeLine &" {n} = {a}" | |
| 341 elif category == "bitmask": | |
| 342 if t.len > 0 and t[0].text.startsWith("typedef"): | |
| 343 outFile.writeLine &" {t[2][0].text} = distinct {t[1][0].text}" | |
| 344 elif category == "union": | |
| 345 let n = t.attr("name") | |
| 346 outFile.writeLine &" {n}* {{.union.}} = object" | |
| 347 for member in t.findAll("member"): | |
| 348 outFile.writeLine &" {member.memberDecl()}" | |
| 349 elif category == "handle": | |
| 350 outFile.writeLine &" {t[2][0].text} = distinct pointer" | |
| 351 elif category == "struct": | |
| 352 let n = t.attr("name") | |
| 353 outFile.writeLine &" {n}* = object" | |
| 354 for member in t.findAll("member"): | |
| 355 if member.attr("api") == "vulkansc": | |
| 356 continue | |
| 357 outFile.writeLine &" {member.memberDecl()}" | |
| 358 elif category == "funcpointer": | |
| 359 #[ | |
| 360 <type category="funcpointer">typedef void* (VKAPI_PTR *<name>PFN_vkAllocationFunction</name>)( | |
| 361 <type>void</type>* pUserData, | |
| 362 <type>size_t</type> size, | |
| 363 <type>size_t</type> alignment, | |
| 364 <type>VkSystemAllocationScope</type> allocationScope); | |
| 365 </type> | |
| 366 PFN_vkAllocationFunction* = proc( | |
| 367 pUserData: pointer, | |
| 368 size: csize_t, | |
| 369 alignment: csize_t, | |
| 370 allocationScope: VkSystemAllocationScope, | |
| 371 ): pointer {.cdecl.} | |
| 372 ]# | |
| 373 assert t[0].text.startsWith("typedef ") | |
| 374 outFile.writeLine &" {t[1][0].text}* = proc()" | |
| 375 else: | |
| 376 doAssert category in ["", "basetype", "enum"], "unknown type category: " & category | |
|
1480
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
377 outFile.writeLine "" |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
378 |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
379 outFile.writeLine """ |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
380 when defined(linux): |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
381 include ../semicongine/rendering/vulkan/platform/xlib |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
382 when defined(windows): |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
383 include ../semicongine/rendering/vulkan/platform/win32 |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
384 """ |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
385 |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
386 outFile.close() |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
387 |
|
0b302531f5c5
did: continue on vulkan nim api generator
sam <sam@basx.dev>
parents:
1479
diff
changeset
|
388 assert execCmd("nim c " & outPath) == 0 |
