2
0

bindings-c3.lua 11 KB

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