bindings-bf.lua 9.4 KB

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