bgfx/scripts/bindings-cs.lua

457 lines
9.8 KiB
Lua
Raw Permalink Normal View History

2019-07-05 04:47:08 +03:00
local codegen = require "codegen"
local idl = codegen.idl "bgfx.idl"
local csharp_template = [[
2019-07-10 08:43:50 +03:00
/*
2024-01-14 12:56:36 +03:00
* Copyright 2011-2024 Branimir Karadzic. All rights reserved.
2019-07-10 08:43:50 +03:00
* License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
*/
/*
*
* AUTO GENERATED! DO NOT EDIT!
*
*/
using System;
using System.Runtime.InteropServices;
namespace Bgfx
{
public static partial class bgfx
{
$types
$funcs
}
}
]]
local csharp_dllname_template = [[
/*
2024-01-14 12:56:36 +03:00
* Copyright 2011-2024 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
*/
/*
*
* AUTO GENERATED! DO NOT EDIT!
*
* Include this file in your build if you want to use the default DllImport
* names of bgfx.dll and bgfx_debug.dll. Otherwise, define your own
* partial class like the below with a const DllName for your use.
*
*/
namespace Bgfx
{
public static partial class bgfx
{
#if DEBUG
const string DllName = "bgfx_debug.dll";
#else
const string DllName = "bgfx.dll";
#endif
}
}
]]
2019-07-04 17:30:32 +03:00
local function hasPrefix(str, prefix)
return prefix == "" or str:sub(1, #prefix) == prefix
end
local function hasSuffix(str, suffix)
return suffix == "" or str:sub(-#suffix) == suffix
end
local function convert_type_0(arg)
2019-07-04 17:30:32 +03:00
if hasPrefix(arg.ctype, "uint64_t") then
return arg.ctype:gsub("uint64_t", "ulong")
2019-07-04 18:00:35 +03:00
elseif hasPrefix(arg.ctype, "int64_t") then
return arg.ctype:gsub("int64_t", "long")
2019-07-04 17:30:32 +03:00
elseif hasPrefix(arg.ctype, "uint32_t") then
return arg.ctype:gsub("uint32_t", "uint")
elseif hasPrefix(arg.ctype, "int32_t") then
return arg.ctype:gsub("int32_t", "int")
elseif hasPrefix(arg.ctype, "uint16_t") then
return arg.ctype:gsub("uint16_t", "ushort")
2019-07-04 18:00:35 +03:00
elseif hasPrefix(arg.ctype, "bgfx_view_id_t") then
return arg.ctype:gsub("bgfx_view_id_t", "ushort")
2019-07-04 17:30:32 +03:00
elseif hasPrefix(arg.ctype, "uint8_t") then
return arg.ctype:gsub("uint8_t", "byte")
elseif hasPrefix(arg.ctype, "uintptr_t") then
return arg.ctype:gsub("uintptr_t", "UIntPtr")
elseif arg.ctype == "bgfx_caps_gpu_t" then
return arg.ctype:gsub("bgfx_caps_gpu_t", "uint")
elseif arg.ctype == "const char*" then
return "[MarshalAs(UnmanagedType.LPStr)] string"
elseif hasPrefix(arg.ctype, "char") then
return arg.ctype:gsub("char", "byte")
elseif hasSuffix(arg.fulltype, "Handle") then
return arg.fulltype
2019-07-04 19:22:09 +03:00
elseif arg.ctype == "..." then
return "[MarshalAs(UnmanagedType.LPStr)] string args"
elseif arg.ctype == "va_list"
or arg.fulltype == "bx::AllocatorI*"
or arg.fulltype == "CallbackI*"
or arg.fulltype == "ReleaseFn" then
return "IntPtr"
elseif arg.fulltype == "const ViewId*" then
return "ushort*"
end
2019-07-04 17:30:32 +03:00
return arg.fulltype
end
local function convert_type(arg)
local ctype = convert_type_0(arg)
2019-07-04 17:30:32 +03:00
ctype = ctype:gsub("::Enum", "")
ctype = ctype:gsub("const ", "")
ctype = ctype:gsub(" &", "*")
ctype = ctype:gsub("&", "*")
return ctype
end
local function convert_struct_type(arg)
local ctype = convert_type(arg)
if hasPrefix(arg.ctype, "bool") then
ctype = ctype:gsub("bool", "byte")
end
return ctype
end
2019-07-04 17:30:32 +03:00
local function convert_ret_type(arg)
local ctype = convert_type(arg)
if hasPrefix(ctype, "[MarshalAs(UnmanagedType.LPStr)]") then
return "IntPtr"
2019-07-04 17:30:32 +03:00
end
return ctype
end
local converter = {}
local yield = coroutine.yield
2019-07-06 20:15:41 +03:00
local indent = ""
2019-07-05 02:46:25 +03:00
local gen = {}
function gen.gen()
local r = csharp_template:gsub("$(%l+)", function(what)
local tmp = {}
for _, object in ipairs(idl[what]) do
local co = coroutine.create(converter[what])
local any
while true do
local ok, v = coroutine.resume(co, object)
assert(ok, debug.traceback(co, v))
if not v then
break
end
table.insert(tmp, v)
any = true
end
2019-07-08 04:41:26 +03:00
if any and tmp[#tmp] ~= "" then
table.insert(tmp, "")
end
end
return table.concat(tmp, "\n\t")
end)
return r
end
function gen.gen_dllname()
return csharp_dllname_template
end
local combined = { "State", "Stencil", "Buffer", "Texture", "Sampler", "Reset" }
2019-07-04 07:24:44 +03:00
for _, v in ipairs(combined) do
combined[v] = {}
end
local lastCombinedFlag
local function FlagBlock(typ)
local format = "0x%08x"
2019-07-04 17:30:32 +03:00
local enumType = " : uint"
2019-07-04 07:24:44 +03:00
if typ.bits == 64 then
format = "0x%016x"
enumType = " : ulong"
elseif typ.bits == 16 then
format = "0x%04x"
enumType = " : ushort"
end
2019-07-04 07:24:44 +03:00
yield("[Flags]")
2019-07-04 18:58:43 +03:00
yield("public enum " .. typ.name .. "Flags" .. enumType)
2019-07-04 07:24:44 +03:00
yield("{")
2019-07-13 08:40:21 +03:00
for idx, flag in ipairs(typ.flag) do
if flag.comment ~= nil then
if idx ~= 1 then
yield("")
end
yield("\t/// <summary>")
2019-07-15 07:19:46 +03:00
for _, comment in ipairs(flag.comment) do
yield("\t/// " .. comment)
2019-07-15 04:23:57 +03:00
end
2019-07-13 08:40:21 +03:00
yield("\t/// </summary>")
end
2019-07-04 20:50:29 +03:00
local flagName = flag.name:gsub("_", "")
2019-07-04 07:24:44 +03:00
yield("\t"
2019-07-04 20:50:29 +03:00
.. flagName
.. string.rep(" ", 22 - #(flagName))
2019-07-04 07:24:44 +03:00
.. " = "
2019-07-04 16:36:08 +03:00
.. string.format(flag.format or format, flag.value)
2019-07-04 07:24:44 +03:00
.. ","
)
end
2019-07-04 20:50:29 +03:00
2019-07-04 16:36:08 +03:00
if typ.shift then
yield("\t"
.. "Shift"
.. string.rep(" ", 22 - #("Shift"))
.. " = "
.. flag.shift
)
end
2019-07-04 07:24:44 +03:00
-- generate Mask
if typ.mask then
yield("\t"
.. "Mask"
.. string.rep(" ", 22 - #("Mask"))
.. " = "
.. string.format(format, flag.mask)
)
end
2019-07-04 07:24:44 +03:00
yield("}")
end
2019-07-04 16:36:08 +03:00
local function lastCombinedFlagBlock()
if lastCombinedFlag then
2019-07-08 04:41:26 +03:00
local typ = combined[lastCombinedFlag]
if typ then
FlagBlock(combined[lastCombinedFlag])
yield("")
end
2019-07-04 16:36:08 +03:00
lastCombinedFlag = nil
end
end
2019-07-10 08:43:50 +03:00
local enum = {}
local function convert_array(member)
2019-07-10 08:43:50 +03:00
if string.find(member.array, "::") then
return string.format("[%d]", enum[member.array])
else
return member.array
end
end
local function convert_struct_member(member)
if member.array then
return "fixed " .. convert_struct_type(member) .. " " .. member.name .. convert_array(member)
else
return convert_struct_type(member) .. " " .. member.name
2019-07-10 08:43:50 +03:00
end
end
2019-07-06 20:15:41 +03:00
local namespace = ""
function converter.types(typ)
if typ.handle then
2019-07-04 16:36:08 +03:00
lastCombinedFlagBlock()
2020-02-18 20:42:47 +03:00
yield("public struct " .. typ.name .. " {")
yield(" public ushort idx;")
yield(" public bool Valid => idx != UInt16.MaxValue;")
yield("}")
elseif hasSuffix(typ.name, "::Enum") then
2019-07-04 16:36:08 +03:00
lastCombinedFlagBlock()
yield("public enum " .. typ.typename)
yield("{")
2019-07-12 07:31:18 +03:00
for idx, enum in ipairs(typ.enum) do
if enum.comment ~= nil then
if idx ~= 1 then
yield("")
end
yield("\t/// <summary>")
2019-07-15 07:19:46 +03:00
for _, comment in ipairs(enum.comment) do
yield("\t/// " .. comment)
end
2019-07-12 07:31:18 +03:00
yield("\t/// </summary>")
end
yield("\t" .. enum.name .. ",")
end
yield("");
yield("\tCount")
yield("}")
2019-07-10 08:43:50 +03:00
enum["[" .. typ.typename .. "::Count]"] = #typ.enum
elseif typ.bits ~= nil then
2019-07-04 07:24:44 +03:00
local prefix, name = typ.name:match "(%u%l+)(.*)"
if prefix ~= lastCombinedFlag then
2019-07-04 16:36:08 +03:00
lastCombinedFlagBlock()
lastCombinedFlag = prefix
end
2019-07-04 07:24:44 +03:00
local combinedFlag = combined[prefix]
if combinedFlag then
combinedFlag.bits = typ.bits
combinedFlag.name = prefix
local flags = combinedFlag.flag or {}
combinedFlag.flag = flags
local lookup = combinedFlag.lookup or {}
combinedFlag.lookup = lookup
for _, flag in ipairs(typ.flag) do
2019-07-04 20:50:29 +03:00
local flagName = name .. flag.name:gsub("_", "")
2019-07-04 07:24:44 +03:00
local value = flag.value
if value == nil then
-- It's a combined flag
value = 0
for _, v in ipairs(flag) do
value = value | assert(lookup[name .. v], v .. " is not defined for " .. flagName)
end
end
lookup[flagName] = value
2019-07-15 07:19:46 +03:00
table.insert(flags, {
name = flagName,
value = value,
comment = flag.comment,
})
end
2019-07-15 04:23:57 +03:00
2019-07-04 16:36:08 +03:00
if typ.shift then
table.insert(flags, {
name = name .. "Shift",
value = typ.shift,
format = "%d",
2019-07-15 04:23:57 +03:00
comment = typ.comment,
2019-07-04 16:36:08 +03:00
})
end
2019-07-15 04:23:57 +03:00
2019-07-04 07:24:44 +03:00
if typ.mask then
-- generate Mask
table.insert(flags, {
name = name .. "Mask",
value = typ.mask,
2019-07-15 04:23:57 +03:00
comment = typ.comment,
2019-07-04 07:24:44 +03:00
})
lookup[name .. "Mask"] = typ.mask
end
2019-07-04 07:24:44 +03:00
else
FlagBlock(typ)
end
2019-07-04 18:00:35 +03:00
elseif typ.struct ~= nil then
2019-07-04 18:58:43 +03:00
2019-07-06 20:15:41 +03:00
local skip = false
2019-07-04 18:58:43 +03:00
if typ.namespace ~= nil then
2019-07-06 20:15:41 +03:00
if namespace ~= typ.namespace then
yield("public unsafe struct " .. typ.namespace)
yield("{")
namespace = typ.namespace
indent = "\t"
end
elseif namespace ~= "" then
indent = ""
namespace = ""
skip = true
2019-07-04 18:58:43 +03:00
end
2019-07-06 20:15:41 +03:00
if not skip then
yield(indent .. "public unsafe struct " .. typ.name)
yield(indent .. "{")
end
2019-07-04 18:00:35 +03:00
for _, member in ipairs(typ.struct) do
2019-07-10 08:43:50 +03:00
yield(
indent .. "\tpublic " .. convert_struct_member(member) .. ";"
2019-07-04 18:00:35 +03:00
)
end
2019-07-06 20:15:41 +03:00
yield(indent .. "}")
end
end
function converter.funcs(func)
2019-07-06 01:59:19 +03:00
2019-07-06 21:25:12 +03:00
if func.cpponly then
return
elseif func.cppinline and not func.conly then
return
2019-07-06 21:25:12 +03:00
end
2019-07-06 01:59:19 +03:00
if func.comments ~= nil then
yield("/// <summary>")
for _, line in ipairs(func.comments) do
yield("/// " .. line)
end
yield("/// </summary>")
2019-07-08 17:51:39 +03:00
yield("///")
local hasParams = false
for _, arg in ipairs(func.args) do
if arg.comment ~= nil then
2019-07-15 07:19:46 +03:00
local comment = table.concat(arg.comment, " ")
2019-07-08 17:51:39 +03:00
yield("/// <param name=\""
.. arg.name
.. "\">"
.. comment
.. "</param>"
)
hasParams = true
end
end
if hasParams then
yield("///")
end
2019-07-06 01:59:19 +03:00
end
2019-07-05 02:07:18 +03:00
yield("[DllImport(DllName, EntryPoint=\"bgfx_" .. func.cname .. "\", CallingConvention = CallingConvention.Cdecl)]")
if func.ret.cpptype == "bool" then
2019-07-04 17:30:32 +03:00
yield("[return: MarshalAs(UnmanagedType.I1)]")
end
2019-07-07 07:35:46 +03:00
local args = {}
if func.this ~= nil then
2019-07-07 07:35:46 +03:00
args[1] = func.this_type.type .. "* _this"
end
for _, arg in ipairs(func.args) do
2019-07-07 07:35:46 +03:00
table.insert(args, convert_type(arg) .. " " .. arg.name)
end
yield("public static extern unsafe " .. convert_ret_type(func.ret) .. " " .. func.cname
2019-07-07 07:35:46 +03:00
.. "(" .. table.concat(args, ", ") .. ");")
end
-- printtable("idl types", idl.types)
-- printtable("idl funcs", idl.funcs)
2019-07-05 02:46:25 +03:00
function gen.write(codes, outputfile)
local out = assert(io.open(outputfile, "wb"))
out:write(codes)
out:close()
print("Generating: " .. outputfile)
2019-07-05 02:46:25 +03:00
end
2019-07-15 07:19:46 +03:00
if (...) == nil then
-- run `lua bindings-cs.lua` in command line
print(gen.gen())
end
2019-07-05 02:46:25 +03:00
return gen