2
0

bindings-bf.lua 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. local codegen = require "codegen"
  2. local idl = codegen.idl "bgfx.idl"
  3. local beef_template = [[
  4. /*
  5. * Copyright 2011-2025 Branimir Karadzic. All rights reserved.
  6. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  7. */
  8. /*
  9. *
  10. * AUTO GENERATED! DO NOT EDIT!
  11. *
  12. */
  13. using System;
  14. namespace Bgfx
  15. {
  16. public static class bgfx
  17. {
  18. public typealias ViewId = uint16;
  19. $types
  20. $funcs
  21. public static bgfx.StateFlags blend_function_separate(bgfx.StateFlags _srcRGB, bgfx.StateFlags _dstRGB, bgfx.StateFlags _srcA, bgfx.StateFlags _dstA)
  22. {
  23. return (bgfx.StateFlags)(uint64(0) | (((uint64)(_srcRGB) | ((uint64)(_dstRGB) << 4))) | (((uint64)(_srcA) | ((uint64)(_dstA) << 4)) << 8));
  24. }
  25. public static bgfx.StateFlags blend_function(bgfx.StateFlags _srcRGB, bgfx.StateFlags _dstRGB)
  26. {
  27. return blend_function_separate(_srcRGB, _dstRGB, _srcRGB, _dstRGB);
  28. }
  29. }
  30. }
  31. ]]
  32. local function hasPrefix(str, prefix)
  33. return prefix == "" or str:sub(1, #prefix) == prefix
  34. end
  35. local function hasSuffix(str, suffix)
  36. return suffix == "" or str:sub(-#suffix) == suffix
  37. end
  38. local function convert_type_0(arg)
  39. if hasPrefix(arg.ctype, "uint64_t") then
  40. return arg.ctype:gsub("uint64_t", "uint64")
  41. elseif hasPrefix(arg.ctype, "int64_t") then
  42. return arg.ctype:gsub("int64_t", "int64")
  43. elseif hasPrefix(arg.ctype, "uint32_t") then
  44. return arg.ctype:gsub("uint32_t", "uint32")
  45. elseif hasPrefix(arg.ctype, "int32_t") then
  46. return arg.ctype:gsub("int32_t", "int32")
  47. elseif hasPrefix(arg.ctype, "uint16_t") then
  48. return arg.ctype:gsub("uint16_t", "uint16")
  49. elseif hasPrefix(arg.ctype, "uint8_t") then
  50. return arg.ctype:gsub("uint8_t", "uint8")
  51. elseif hasPrefix(arg.ctype, "uintptr_t") then
  52. return arg.ctype:gsub("uintptr_t", "void*")
  53. elseif arg.ctype == "const char*" then
  54. return "char8*"
  55. elseif hasPrefix(arg.ctype, "char") then
  56. return arg.ctype:gsub("char", "char8")
  57. elseif hasPrefix(arg.ctype, "byte") then
  58. return arg.ctype:gsub("byte", "uint8")
  59. elseif arg.ctype == "va_list"
  60. or arg.fulltype == "bx::AllocatorI*"
  61. or arg.fulltype == "CallbackI*"
  62. or arg.fulltype == "ReleaseFn" then
  63. return "void*"
  64. end
  65. return arg.fulltype
  66. end
  67. local function convert_type(arg)
  68. local ctype = convert_type_0(arg)
  69. ctype = ctype:gsub("::Enum", "")
  70. ctype = ctype:gsub("const ", "")
  71. ctype = ctype:gsub(" &", "*")
  72. ctype = ctype:gsub("&", "*")
  73. return ctype
  74. end
  75. local function convert_struct_type(arg)
  76. local ctype = convert_type(arg)
  77. if hasPrefix(arg.ctype, "bool") then
  78. ctype = ctype:gsub("bool", "uint8")
  79. end
  80. return ctype
  81. end
  82. local function convert_ret_type(arg)
  83. local ctype = convert_type(arg)
  84. return ctype
  85. end
  86. local converter = {}
  87. local yield = coroutine.yield
  88. local indent = ""
  89. local gen = {}
  90. function gen.gen()
  91. local r = beef_template:gsub("$(%l+)", function(what)
  92. local tmp = {}
  93. for _, object in ipairs(idl[what]) do
  94. local co = coroutine.create(converter[what])
  95. local any
  96. while true do
  97. local ok, v = coroutine.resume(co, object)
  98. assert(ok, debug.traceback(co, v))
  99. if not v then
  100. break
  101. end
  102. table.insert(tmp, v)
  103. any = true
  104. end
  105. if any and tmp[#tmp] ~= "" then
  106. table.insert(tmp, "")
  107. end
  108. end
  109. return table.concat(tmp, "\n\t")
  110. end)
  111. return r
  112. end
  113. local combined = { "State", "Stencil", "Buffer", "Texture", "Sampler", "Reset" }
  114. for _, v in ipairs(combined) do
  115. combined[v] = {}
  116. end
  117. local lastCombinedFlag
  118. local function FlagBlock(typ)
  119. local format = "0x%08x"
  120. local enumType = " : uint32"
  121. if typ.bits == 64 then
  122. format = "0x%016x"
  123. enumType = " : uint64"
  124. elseif typ.bits == 16 then
  125. format = "0x%04x"
  126. enumType = " : uint16"
  127. end
  128. yield("[AllowDuplicates]")
  129. yield("public enum " .. typ.name .. "Flags" .. enumType)
  130. yield("{")
  131. for idx, flag in ipairs(typ.flag) do
  132. if flag.comment ~= nil then
  133. if idx ~= 1 then
  134. yield("")
  135. end
  136. yield("\t/// <summary>")
  137. for _, comment in ipairs(flag.comment) do
  138. yield("\t/// " .. comment)
  139. end
  140. yield("\t/// </summary>")
  141. end
  142. local flagName = flag.name:gsub("_", "")
  143. yield("\t"
  144. .. flagName
  145. .. string.rep(" ", 22 - #(flagName))
  146. .. " = "
  147. .. string.format(flag.format or format, flag.value)
  148. .. ","
  149. )
  150. end
  151. if typ.shift then
  152. yield("\t"
  153. .. "Shift"
  154. .. string.rep(" ", 22 - #("Shift"))
  155. .. " = "
  156. .. flag.shift
  157. )
  158. end
  159. -- generate Mask
  160. if typ.mask then
  161. yield("\t"
  162. .. "Mask"
  163. .. string.rep(" ", 22 - #("Mask"))
  164. .. " = "
  165. .. string.format(format, flag.mask)
  166. )
  167. end
  168. yield("}")
  169. end
  170. local function lastCombinedFlagBlock()
  171. if lastCombinedFlag then
  172. local typ = combined[lastCombinedFlag]
  173. if typ then
  174. FlagBlock(combined[lastCombinedFlag])
  175. yield("")
  176. end
  177. lastCombinedFlag = nil
  178. end
  179. end
  180. local enum = {}
  181. local function convert_array(member)
  182. if string.find(member.array, "::") then
  183. return string.format("[%d]", enum[member.array])
  184. else
  185. return member.array
  186. end
  187. end
  188. local function convert_struct_member(member)
  189. if member.array then
  190. return convert_struct_type(member) .. convert_array(member) .. " " .. member.name
  191. else
  192. return convert_struct_type(member) .. " " .. member.name
  193. end
  194. end
  195. local namespace = ""
  196. function converter.types(typ)
  197. if typ.handle then
  198. lastCombinedFlagBlock()
  199. yield("[CRepr]")
  200. yield("public struct " .. typ.name .. " {")
  201. yield(" public uint16 idx;")
  202. yield(" public bool Valid => idx != uint16.MaxValue;")
  203. yield("}")
  204. elseif hasSuffix(typ.name, "::Enum") then
  205. lastCombinedFlagBlock()
  206. yield("[AllowDuplicates]")
  207. yield("public enum " .. typ.typename .. " : uint32")
  208. yield("{")
  209. for idx, enum in ipairs(typ.enum) do
  210. if enum.comment ~= nil then
  211. if idx ~= 1 then
  212. yield("")
  213. end
  214. yield("\t/// <summary>")
  215. for _, comment in ipairs(enum.comment) do
  216. yield("\t/// " .. comment)
  217. end
  218. yield("\t/// </summary>")
  219. end
  220. yield("\t" .. enum.name .. ",")
  221. end
  222. yield("");
  223. yield("\tCount")
  224. yield("}")
  225. enum["[" .. typ.typename .. "::Count]"] = #typ.enum
  226. elseif typ.bits ~= nil then
  227. local prefix, name = typ.name:match "(%u%l+)(.*)"
  228. if prefix ~= lastCombinedFlag then
  229. lastCombinedFlagBlock()
  230. lastCombinedFlag = prefix
  231. end
  232. local combinedFlag = combined[prefix]
  233. if combinedFlag then
  234. combinedFlag.bits = typ.bits
  235. combinedFlag.name = prefix
  236. local flags = combinedFlag.flag or {}
  237. combinedFlag.flag = flags
  238. local lookup = combinedFlag.lookup or {}
  239. combinedFlag.lookup = lookup
  240. for _, flag in ipairs(typ.flag) do
  241. local flagName = name .. flag.name:gsub("_", "")
  242. local value = flag.value
  243. if value == nil then
  244. -- It's a combined flag
  245. value = 0
  246. for _, v in ipairs(flag) do
  247. value = value | assert(lookup[name .. v], v .. " is not defined for " .. flagName)
  248. end
  249. end
  250. lookup[flagName] = value
  251. table.insert(flags, {
  252. name = flagName,
  253. value = value,
  254. comment = flag.comment,
  255. })
  256. end
  257. if typ.shift then
  258. table.insert(flags, {
  259. name = name .. "Shift",
  260. value = typ.shift,
  261. format = "%d",
  262. comment = typ.comment,
  263. })
  264. end
  265. if typ.mask then
  266. -- generate Mask
  267. table.insert(flags, {
  268. name = name .. "Mask",
  269. value = typ.mask,
  270. comment = typ.comment,
  271. })
  272. lookup[name .. "Mask"] = typ.mask
  273. end
  274. else
  275. FlagBlock(typ)
  276. end
  277. elseif typ.struct ~= nil then
  278. local skip = false
  279. if typ.namespace ~= nil then
  280. if namespace ~= typ.namespace then
  281. yield("[CRepr]")
  282. yield("public struct " .. typ.namespace)
  283. yield("{")
  284. namespace = typ.namespace
  285. indent = "\t"
  286. end
  287. elseif namespace ~= "" then
  288. indent = ""
  289. namespace = ""
  290. skip = true
  291. end
  292. if not skip then
  293. yield(indent .. "[CRepr]")
  294. yield(indent .. "public struct " .. typ.name)
  295. yield(indent .. "{")
  296. end
  297. for _, member in ipairs(typ.struct) do
  298. yield(
  299. indent .. "\tpublic " .. convert_struct_member(member) .. ";"
  300. )
  301. end
  302. yield(indent .. "}")
  303. end
  304. end
  305. function converter.funcs(func)
  306. if func.cpponly then
  307. return
  308. elseif func.cppinline and not func.conly then
  309. return
  310. end
  311. if func.comments ~= nil then
  312. yield("/// <summary>")
  313. for _, line in ipairs(func.comments) do
  314. yield("/// " .. line)
  315. end
  316. yield("/// </summary>")
  317. yield("///")
  318. local hasParams = false
  319. for _, arg in ipairs(func.args) do
  320. if arg.comment ~= nil then
  321. local comment = table.concat(arg.comment, " ")
  322. yield("/// <param name=\""
  323. .. arg.name
  324. .. "\">"
  325. .. comment
  326. .. "</param>"
  327. )
  328. hasParams = true
  329. end
  330. end
  331. if hasParams then
  332. yield("///")
  333. end
  334. end
  335. yield("[LinkName(\"bgfx_" .. func.cname .. "\")]")
  336. local args = {}
  337. if func.this ~= nil then
  338. args[1] = func.this_type.type .. "* _this"
  339. end
  340. for _, arg in ipairs(func.args) do
  341. table.insert(args, convert_type(arg) .. " " .. arg.name)
  342. end
  343. yield("public static extern " .. convert_ret_type(func.ret) .. " " .. func.cname
  344. .. "(" .. table.concat(args, ", ") .. ");")
  345. end
  346. -- printtable("idl types", idl.types)
  347. -- printtable("idl funcs", idl.funcs)
  348. function gen.write(codes, outputfile)
  349. local out = assert(io.open(outputfile, "wb"))
  350. out:write(codes)
  351. out:close()
  352. print("Generating: " .. outputfile)
  353. end
  354. if (...) == nil then
  355. -- run `lua bindings-bf.lua` in command line
  356. print(gen.gen())
  357. end
  358. return gen