Mercurial > games > semicongine
comparison svk/generate.nim @ 1487:f786797a0cbe
did: stuff
| author | sam <sam@basx.dev> |
|---|---|
| date | Thu, 15 May 2025 00:10:08 +0700 |
| parents | 0ba3f0b2be2e |
| children | 3ce7c132fdac |
comparison
equal
deleted
inserted
replaced
| 1486:0ba3f0b2be2e | 1487:f786797a0cbe |
|---|---|
| 236 outFile.writeLine """ | 236 outFile.writeLine """ |
| 237 | 237 |
| 238 import std/dynlib | 238 import std/dynlib |
| 239 import std/strutils | 239 import std/strutils |
| 240 import std/tables | 240 import std/tables |
| 241 import std/macros | |
| 242 import std/typetraits | |
| 241 | 243 |
| 242 import ../semicongine/thirdparty/winim/winim/inc/winbase | 244 import ../semicongine/thirdparty/winim/winim/inc/winbase |
| 243 import ../semicongine/thirdparty/winim/winim/inc/windef | 245 import ../semicongine/thirdparty/winim/winim/inc/windef |
| 244 import ../semicongine/thirdparty/x11/xlib | 246 import ../semicongine/thirdparty/x11/xlib |
| 245 import ../semicongine/thirdparty/x11/x | 247 import ../semicongine/thirdparty/x11/x |
| 247 | 249 |
| 248 func VK_MAKE_API_VERSION*( | 250 func VK_MAKE_API_VERSION*( |
| 249 variant: uint32, major: uint32, minor: uint32, patch: uint32 | 251 variant: uint32, major: uint32, minor: uint32, patch: uint32 |
| 250 ): uint32 = | 252 ): uint32 = |
| 251 (variant shl 29) or (major shl 22) or (minor shl 12) or patch | 253 (variant shl 29) or (major shl 22) or (minor shl 12) or patch |
| 254 | |
| 255 macro enumFullRange(a: typed): untyped = | |
| 256 newNimNode(nnkBracket).add(a.getType[1][1 ..^ 1]) | |
| 257 | |
| 258 func asBits[T, S](flags: openArray[T]): S = | |
| 259 for flag in flags: | |
| 260 let a = distinctBase(S)(result) | |
| 261 let b = distinctBase(S)(flag) | |
| 262 result = S(a or b) | |
| 263 | |
| 264 func toEnums[T, S](number: T): seq[S] = | |
| 265 for value in enumFullRange(T): | |
| 266 if (value.ord and cint(number)) > 0: | |
| 267 result.add value | |
| 252 """ | 268 """ |
| 253 | 269 |
| 254 outFile.writeLine "type" | 270 outFile.writeLine "type" |
| 255 outFile.writeLine """ | 271 outFile.writeLine """ |
| 272 | |
| 256 # some unused native types | 273 # some unused native types |
| 257 # | 274 # |
| 258 # android | 275 # android |
| 259 ANativeWindow = object | 276 ANativeWindow = object |
| 260 AHardwareBuffer = object | 277 AHardwareBuffer = object |
| 330 const nameCollisions = [ | 347 const nameCollisions = [ |
| 331 "VK_PIPELINE_CACHE_HEADER_VERSION_ONE", | 348 "VK_PIPELINE_CACHE_HEADER_VERSION_ONE", |
| 332 "VK_PIPELINE_CACHE_HEADER_VERSION_SAFETY_CRITICAL_ONE", | 349 "VK_PIPELINE_CACHE_HEADER_VERSION_SAFETY_CRITICAL_ONE", |
| 333 "VK_DEVICE_FAULT_VENDOR_BINARY_HEADER_VERSION_ONE_EXT", | 350 "VK_DEVICE_FAULT_VENDOR_BINARY_HEADER_VERSION_ONE_EXT", |
| 334 ] | 351 ] |
| 335 outFile.writeLine "type" | |
| 336 | 352 |
| 337 for edef in enums.values(): | 353 for edef in enums.values(): |
| 338 if edef.values.len > 0: | 354 if edef.values.len > 0: |
| 339 outFile.writeLine &" {edef.name}* {{.size: 4.}} = enum" | 355 outFile.writeLine &"type {edef.name}* {{.size: 4.}} = enum" |
| 340 for ee in edef.values: | 356 for ee in edef.values: |
| 341 # due to the nim identifier-system, there might be collisions between typenames and enum-member names | 357 # due to the nim identifier-system, there might be collisions between typenames and enum-member names |
| 342 if ee.name in nameCollisions: | 358 if ee.name in nameCollisions: |
| 343 outFile.writeLine &" {ee.name}_VALUE = {ee.value}" | 359 outFile.writeLine &" {ee.name}_VALUE = {ee.value}" |
| 344 else: | 360 else: |
| 345 outFile.writeLine &" {ee.name} = {ee.value}" | 361 outFile.writeLine &" {ee.name} = {ee.value}" |
| 346 | 362 |
| 347 outFile.writeLine "" | 363 outFile.writeLine "type" |
| 348 | 364 |
| 349 # generate types =============================================================================== | 365 # generate types =============================================================================== |
| 350 var stringConverters: seq[string] | 366 var stringConverters: seq[string] |
| 351 for t in types: | 367 for t in types: |
| 352 let category = t.attr("category") | 368 let category = t.attr("category") |
| 411 outFile.writeLine &"): {doTypename(retName, 0)} {{.cdecl.}}" | 427 outFile.writeLine &"): {doTypename(retName, 0)} {{.cdecl.}}" |
| 412 else: | 428 else: |
| 413 doAssert category in ["", "basetype", "enum"], "unknown type category: " & category | 429 doAssert category in ["", "basetype", "enum"], "unknown type category: " & category |
| 414 outFile.writeLine "" | 430 outFile.writeLine "" |
| 415 | 431 |
| 432 for edef in enums.values(): | |
| 433 if edef.values.len > 0: | |
| 434 if edef.isBitmask: | |
| 435 let bitsName = edef.name | |
| 436 let p = bitsName.rfind("Flag") | |
| 437 let flagsName = bitsName[0 ..< p] & "Flags" | |
| 438 | |
| 439 outFile.writeLine &"converter {bitsName}ToBits*(flags: openArray[{bitsName}]): {flagsName} =" | |
| 440 outFile.writeLine &" asBits[{bitsName}, {flagsName}](flags)" | |
| 441 outFile.writeLine &"func `$`*(bits: {flagsName}): string =" | |
| 442 outFile.writeLine &" $toEnums[{flagsName}, {bitsName}](bits)" | |
| 443 outFile.writeLine "" | |
| 444 | |
| 416 for command in commands: | 445 for command in commands: |
| 417 if command.attr("api") == "vulkansc": | 446 if command.attr("api") == "vulkansc": |
| 418 continue | 447 continue |
| 419 if command.attr("alias") != "": | 448 if command.attr("alias") != "": |
| 420 let funcName = command.attr("name") | 449 let funcName = command.attr("name") |
| 524 | 553 |
| 525 for strCon in stringConverters: | 554 for strCon in stringConverters: |
| 526 outFile.writeLine &"""proc `$`*(v: {strCon}): string = "0x" & cast[uint](v).toHex()""" | 555 outFile.writeLine &"""proc `$`*(v: {strCon}): string = "0x" & cast[uint](v).toHex()""" |
| 527 outFile.writeLine "" | 556 outFile.writeLine "" |
| 528 | 557 |
| 529 # we preload the vkCreateInstance function, so we can create an instance | |
| 530 outFile.writeLine "" | 558 outFile.writeLine "" |
| 531 | 559 |
| 532 outFile.close() | 560 outFile.close() |
| 533 | 561 |
| 534 assert execCmd("nim c " & outPath) == 0 | 562 assert execCmd("nim c " & outPath) == 0 |
