bindings-cs.lua 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. local codegen = require "codegen"
  2. local idl = codegen.idl "bgfx.idl"
  3. local csharp_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. 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-2020 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 .. "{ public ushort idx; }")
  232. elseif hasSuffix(typ.name, "::Enum") then
  233. lastCombinedFlagBlock()
  234. yield("public enum " .. typ.typename)
  235. yield("{")
  236. for idx, enum in ipairs(typ.enum) do
  237. if enum.comment ~= nil then
  238. if idx ~= 1 then
  239. yield("")
  240. end
  241. yield("\t/// <summary>")
  242. for _, comment in ipairs(enum.comment) do
  243. yield("\t/// " .. comment)
  244. end
  245. yield("\t/// </summary>")
  246. end
  247. yield("\t" .. enum.name .. ",")
  248. end
  249. yield("");
  250. yield("\tCount")
  251. yield("}")
  252. enum["[" .. typ.typename .. "::Count]"] = #typ.enum
  253. elseif typ.bits ~= nil then
  254. local prefix, name = typ.name:match "(%u%l+)(.*)"
  255. if prefix ~= lastCombinedFlag then
  256. lastCombinedFlagBlock()
  257. lastCombinedFlag = prefix
  258. end
  259. local combinedFlag = combined[prefix]
  260. if combinedFlag then
  261. combinedFlag.bits = typ.bits
  262. combinedFlag.name = prefix
  263. local flags = combinedFlag.flag or {}
  264. combinedFlag.flag = flags
  265. local lookup = combinedFlag.lookup or {}
  266. combinedFlag.lookup = lookup
  267. for _, flag in ipairs(typ.flag) do
  268. local flagName = name .. flag.name:gsub("_", "")
  269. local value = flag.value
  270. if value == nil then
  271. -- It's a combined flag
  272. value = 0
  273. for _, v in ipairs(flag) do
  274. value = value | assert(lookup[name .. v], v .. " is not defined for " .. flagName)
  275. end
  276. end
  277. lookup[flagName] = value
  278. table.insert(flags, {
  279. name = flagName,
  280. value = value,
  281. comment = flag.comment,
  282. })
  283. end
  284. if typ.shift then
  285. table.insert(flags, {
  286. name = name .. "Shift",
  287. value = typ.shift,
  288. format = "%d",
  289. comment = typ.comment,
  290. })
  291. end
  292. if typ.mask then
  293. -- generate Mask
  294. table.insert(flags, {
  295. name = name .. "Mask",
  296. value = typ.mask,
  297. comment = typ.comment,
  298. })
  299. lookup[name .. "Mask"] = typ.mask
  300. end
  301. else
  302. FlagBlock(typ)
  303. end
  304. elseif typ.struct ~= nil then
  305. local skip = false
  306. if typ.namespace ~= nil then
  307. if namespace ~= typ.namespace then
  308. yield("public unsafe struct " .. typ.namespace)
  309. yield("{")
  310. namespace = typ.namespace
  311. indent = "\t"
  312. end
  313. elseif namespace ~= "" then
  314. indent = ""
  315. namespace = ""
  316. skip = true
  317. end
  318. if not skip then
  319. yield(indent .. "public unsafe struct " .. typ.name)
  320. yield(indent .. "{")
  321. end
  322. for _, member in ipairs(typ.struct) do
  323. yield(
  324. indent .. "\tpublic " .. convert_struct_member(member) .. ";"
  325. )
  326. end
  327. yield(indent .. "}")
  328. end
  329. end
  330. function converter.funcs(func)
  331. if func.cpponly then
  332. return
  333. end
  334. if func.comments ~= nil then
  335. yield("/// <summary>")
  336. for _, line in ipairs(func.comments) do
  337. yield("/// " .. line)
  338. end
  339. yield("/// </summary>")
  340. yield("///")
  341. local hasParams = false
  342. for _, arg in ipairs(func.args) do
  343. if arg.comment ~= nil then
  344. local comment = table.concat(arg.comment, " ")
  345. yield("/// <param name=\""
  346. .. arg.name
  347. .. "\">"
  348. .. comment
  349. .. "</param>"
  350. )
  351. hasParams = true
  352. end
  353. end
  354. if hasParams then
  355. yield("///")
  356. end
  357. end
  358. yield("[DllImport(DllName, EntryPoint=\"bgfx_" .. func.cname .. "\", CallingConvention = CallingConvention.Cdecl)]")
  359. if func.ret.cpptype == "bool" then
  360. yield("[return: MarshalAs(UnmanagedType.I1)]")
  361. end
  362. local args = {}
  363. if func.this ~= nil then
  364. args[1] = func.this_type.type .. "* _this"
  365. end
  366. for _, arg in ipairs(func.args) do
  367. table.insert(args, convert_type(arg) .. " " .. arg.name)
  368. end
  369. yield("public static extern unsafe " .. convert_ret_type(func.ret) .. " " .. func.cname
  370. .. "(" .. table.concat(args, ", ") .. ");")
  371. end
  372. -- printtable("idl types", idl.types)
  373. -- printtable("idl funcs", idl.funcs)
  374. function gen.write(codes, outputfile)
  375. local out = assert(io.open(outputfile, "wb"))
  376. out:write(codes)
  377. out:close()
  378. print("Generating: " .. outputfile)
  379. end
  380. if (...) == nil then
  381. -- run `lua bindings-cs.lua` in command line
  382. print(gen.gen())
  383. end
  384. return gen