bindings-cs.lua 9.8 KB

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