bindings-cs.lua 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. local codegen = require "codegen"
  2. local idl = codegen.idl "bgfx.idl"
  3. local csharp_template = [[
  4. using System;
  5. using System.Runtime.InteropServices;
  6. using System.Security;
  7. internal struct bgfx
  8. {
  9. $types
  10. $funcs
  11. #if DEBUG
  12. const string DllName = "bgfx_debug.dll";
  13. #else
  14. const string DllName = "bgfx.dll";
  15. #endif
  16. }
  17. ]]
  18. local function hasPrefix(str, prefix)
  19. return prefix == "" or str:sub(1, #prefix) == prefix
  20. end
  21. local function hasSuffix(str, suffix)
  22. return suffix == "" or str:sub(-#suffix) == suffix
  23. end
  24. local function convert_type_0(arg)
  25. if hasPrefix(arg.ctype, "uint64_t") then
  26. return arg.ctype:gsub("uint64_t", "ulong")
  27. elseif hasPrefix(arg.ctype, "int64_t") then
  28. return arg.ctype:gsub("int64_t", "long")
  29. elseif hasPrefix(arg.ctype, "uint32_t") then
  30. return arg.ctype:gsub("uint32_t", "uint")
  31. elseif hasPrefix(arg.ctype, "int32_t") then
  32. return arg.ctype:gsub("int32_t", "int")
  33. elseif hasPrefix(arg.ctype, "uint16_t") then
  34. return arg.ctype:gsub("uint16_t", "ushort")
  35. elseif hasPrefix(arg.ctype, "bgfx_view_id_t") then
  36. return arg.ctype:gsub("bgfx_view_id_t", "ushort")
  37. elseif hasPrefix(arg.ctype, "uint8_t") then
  38. return arg.ctype:gsub("uint8_t", "byte")
  39. elseif hasPrefix(arg.ctype, "uintptr_t") then
  40. return arg.ctype:gsub("uintptr_t", "UIntPtr")
  41. elseif arg.ctype == "const char*" then
  42. return "[MarshalAs(UnmanagedType.LPStr)] string"
  43. elseif hasSuffix(arg.fulltype, "Handle") then
  44. return arg.fulltype
  45. elseif arg.ctype == "..." then
  46. return "[MarshalAs(UnmanagedType.LPStr)] string args"
  47. elseif arg.ctype == "va_list"
  48. or arg.fulltype == "bx::AllocatorI*"
  49. or arg.fulltype == "CallbackI*"
  50. or arg.fulltype == "ReleaseFn" then
  51. return "IntPtr"
  52. elseif arg.fulltype == "const ViewId*" then
  53. return "ushort*"
  54. end
  55. return arg.fulltype
  56. end
  57. local function convert_type(arg)
  58. local ctype = convert_type_0(arg)
  59. ctype = ctype:gsub("::Enum", "")
  60. ctype = ctype:gsub("const ", "")
  61. ctype = ctype:gsub(" &", "*")
  62. ctype = ctype:gsub("&", "*")
  63. return ctype
  64. end
  65. local function convert_ret_type(arg)
  66. local ctype = convert_type(arg)
  67. if hasPrefix(ctype, "[MarshalAs(UnmanagedType.LPStr)]") then
  68. return "string"
  69. end
  70. return ctype
  71. end
  72. local converter = {}
  73. local yield = coroutine.yield
  74. local gen = {}
  75. function gen.gen()
  76. local r = csharp_template:gsub("$(%l+)", function(what)
  77. local tmp = {}
  78. for _, object in ipairs(idl[what]) do
  79. local co = coroutine.create(converter[what])
  80. local any
  81. while true do
  82. local ok, v = coroutine.resume(co, object)
  83. assert(ok, debug.traceback(co, v))
  84. if not v then
  85. break
  86. end
  87. table.insert(tmp, v)
  88. any = true
  89. end
  90. if any then
  91. table.insert(tmp, "")
  92. end
  93. end
  94. return table.concat(tmp, "\n\t")
  95. end)
  96. return r
  97. end
  98. local combined = { "State", "Stencil", "Buffer", "Texture", "Sampler", "Reset" }
  99. for _, v in ipairs(combined) do
  100. combined[v] = {}
  101. end
  102. local lastCombinedFlag
  103. local function FlagBlock(typ)
  104. if typ == nil then
  105. return
  106. end
  107. local format = "0x%08x"
  108. local enumType = " : uint"
  109. if typ.bits == 64 then
  110. format = "0x%016x"
  111. enumType = " : ulong"
  112. elseif typ.bits == 16 then
  113. format = "0x%04x"
  114. enumType = " : ushort"
  115. end
  116. yield("[Flags]")
  117. yield("public enum " .. typ.name .. "Flags" .. enumType)
  118. yield("{")
  119. for _, flag in ipairs(typ.flag) do
  120. local flagName = flag.name:gsub("_", "")
  121. yield("\t"
  122. .. flagName
  123. .. string.rep(" ", 22 - #(flagName))
  124. .. " = "
  125. .. string.format(flag.format or format, flag.value)
  126. .. ","
  127. )
  128. end
  129. if typ.shift then
  130. yield("\t"
  131. .. "Shift"
  132. .. string.rep(" ", 22 - #("Shift"))
  133. .. " = "
  134. .. flag.shift
  135. )
  136. end
  137. -- generate Mask
  138. if typ.mask then
  139. yield("\t"
  140. .. "Mask"
  141. .. string.rep(" ", 22 - #("Mask"))
  142. .. " = "
  143. .. string.format(format, flag.mask)
  144. )
  145. end
  146. yield("}")
  147. end
  148. local function lastCombinedFlagBlock()
  149. if lastCombinedFlag then
  150. FlagBlock(combined[lastCombinedFlag])
  151. lastCombinedFlag = nil
  152. end
  153. end
  154. function converter.types(typ)
  155. if typ.handle then
  156. lastCombinedFlagBlock()
  157. yield("public struct " .. typ.name .. "{ public ushort idx; }")
  158. elseif hasSuffix(typ.name, "::Enum") then
  159. lastCombinedFlagBlock()
  160. yield("public enum " .. typ.typename)
  161. yield("{")
  162. for _, enum in ipairs(typ.enum) do
  163. yield("\t" .. enum.name .. ",")
  164. end
  165. yield("}")
  166. elseif typ.bits ~= nil then
  167. local prefix, name = typ.name:match "(%u%l+)(.*)"
  168. if prefix ~= lastCombinedFlag then
  169. lastCombinedFlagBlock()
  170. lastCombinedFlag = prefix
  171. end
  172. local combinedFlag = combined[prefix]
  173. if combinedFlag then
  174. combinedFlag.bits = typ.bits
  175. combinedFlag.name = prefix
  176. local flags = combinedFlag.flag or {}
  177. combinedFlag.flag = flags
  178. local lookup = combinedFlag.lookup or {}
  179. combinedFlag.lookup = lookup
  180. for _, flag in ipairs(typ.flag) do
  181. local flagName = name .. flag.name:gsub("_", "")
  182. local value = flag.value
  183. if value == nil then
  184. -- It's a combined flag
  185. value = 0
  186. for _, v in ipairs(flag) do
  187. value = value | assert(lookup[name .. v], v .. " is not defined for " .. flagName)
  188. end
  189. end
  190. lookup[flagName] = value
  191. table.insert(flags, {
  192. name = flagName,
  193. value = value,
  194. })
  195. end
  196. if typ.shift then
  197. table.insert(flags, {
  198. name = name .. "Shift",
  199. value = typ.shift,
  200. format = "%d",
  201. })
  202. end
  203. if typ.mask then
  204. -- generate Mask
  205. table.insert(flags, {
  206. name = name .. "Mask",
  207. value = typ.mask,
  208. })
  209. lookup[name .. "Mask"] = typ.mask
  210. end
  211. else
  212. FlagBlock(typ)
  213. end
  214. elseif typ.struct ~= nil then
  215. if typ.namespace ~= nil then
  216. yield("public unsafe struct " .. typ.namespace .. typ.name)
  217. else
  218. yield("public unsafe struct " .. typ.name)
  219. end
  220. yield("{")
  221. for _, member in ipairs(typ.struct) do
  222. yield(
  223. "\tpublic " .. convert_type(member) .. " " .. member.name .. ";"
  224. )
  225. end
  226. yield("}")
  227. end
  228. end
  229. function converter.funcs(func)
  230. if func.comments ~= nil then
  231. yield("/// <summary>")
  232. for _, line in ipairs(func.comments) do
  233. yield("/// " .. line)
  234. end
  235. yield("/// </summary>")
  236. end
  237. yield("[DllImport(DllName, EntryPoint=\"bgfx_" .. func.cname .. "\", CallingConvention = CallingConvention.Cdecl)]")
  238. if func.ret.cpptype == "bool" then
  239. yield("[return: MarshalAs(UnmanagedType.I1)]")
  240. elseif func.ret.cpptype == "const char*" then
  241. yield("[return: MarshalAs(UnmanagedType.LPStr)]")
  242. end
  243. local first = ""
  244. local args = "("
  245. if func.this ~= nil then
  246. local thisType = func.this:gsub("const ", "")
  247. if thisType == "bgfx_encoder_t*" then
  248. thisType = "Encoder*"
  249. elseif thisType == "bgfx_attachment_t*" then
  250. thisType = "Attachment*"
  251. elseif thisType == "bgfx_vertex_decl_t*" then
  252. thisType = "VertexDecl*"
  253. end
  254. args = args .. thisType .. " " .. "_this"
  255. first = ", "
  256. end
  257. for _, arg in ipairs(func.args) do
  258. local argtype = convert_type(arg)
  259. args = args .. first .. argtype .. " " .. arg.name
  260. first = ", "
  261. end
  262. yield("internal static extern unsafe " .. convert_ret_type(func.ret) .. " " .. func.cname .. args .. ");")
  263. end
  264. -- printtable("idl types", idl.types)
  265. -- printtable("idl funcs", idl.funcs)
  266. function gen.write(codes, outputfile)
  267. local out = assert(io.open(outputfile, "wb"))
  268. out:write(codes)
  269. out:close()
  270. end
  271. return gen