From 7600e5e0ebfa6fe64bad07e2ece0b55a93a4e6af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Fri, 27 Feb 2026 23:58:25 -0800 Subject: [PATCH] IDL: Fixed C++ generator. (#3610) --- scripts/bgfx-codegen.lua | 26 +++++++++++--- scripts/bgfx.idl | 10 +++--- scripts/codegen.lua | 73 +++++++++++++++++++++++++++++----------- scripts/temp.bgfx.hpp | 12 ++----- 4 files changed, 82 insertions(+), 39 deletions(-) diff --git a/scripts/bgfx-codegen.lua b/scripts/bgfx-codegen.lua index 526841eea..048ec3977 100644 --- a/scripts/bgfx-codegen.lua +++ b/scripts/bgfx-codegen.lua @@ -98,7 +98,7 @@ local function cppdecl(func) end if doc then local cname - if not func.cpponly then + if not func.cpponly and type(func.cppinline) ~= "string" then if func.multicfunc then cname = {} for _, name in ipairs(func.multicfunc) do @@ -110,7 +110,13 @@ local function cppdecl(func) end doc = codegen.doxygen_type(doc, func, cname) end - local funcdecl = codegen.apply_functemp(func, "$RET $FUNCNAME($ARGS)$CONST;\n") + local funcdecl + if type(func.cppinline) == "string" then + funcdecl = codegen.apply_functemp(func, + "$RET $FUNCNAME($ARGS)$CONST { " .. func.cppinline .. " }\n") + else + funcdecl = codegen.apply_functemp(func, "$RET $FUNCNAME($ARGS)$CONST;\n") + end if doc then return doc .. "\n" .. funcdecl else @@ -155,7 +161,7 @@ end function typegen.enums(typedef) if typedef.enum then - return add_doxygen(typedef, codegen.gen_enum_define(typedef), false, "bgfx_" .. typedef.cname) + return add_doxygen(typedef, codegen.gen_enum_define(typedef), false, typedef.cname) end end @@ -262,6 +268,17 @@ end local codes_tbl = codes() +-- Add namespace indentation to C++ blocks +local function indent_block(text) + return text:gsub("([^\n]+)", "\t%1") +end + +codes_tbl.enums = indent_block(codes_tbl.enums) +codes_tbl.handles = indent_block(codes_tbl.handles) +codes_tbl.structs = indent_block(codes_tbl.structs) +codes_tbl.funcptrs = indent_block(codes_tbl.funcptrs) +codes_tbl.cppdecl = indent_block(codes_tbl.cppdecl) + local function add_path(filename) local path if type(paths) == "string" then @@ -276,7 +293,7 @@ local function change_indent(str, indent) if indent == "\t" then -- strip trailing space only return (str:gsub("(.-)\n", function (line) - return line:gsub("([ \t]*)$","\n") end)) + return line:gsub("[ \t]+$", "") .. "\n" end)) else return (str:gsub("(.-)\n", function (line) return line:gsub("^(\t*)(.-)[ \t]*$", @@ -293,6 +310,7 @@ function gen.apply(tempfile) local f = assert(io.open(tempfile, "rb")) local temp = f:read "a" f:close() + temp = temp:gsub("\r\n", "\n") codes_tbl.source = tempfile return (temp:gsub("$([%l%d_]+)", codes_tbl)) end diff --git a/scripts/bgfx.idl b/scripts/bgfx.idl index 92454caf2..587a6640f 100644 --- a/scripts/bgfx.idl +++ b/scripts/bgfx.idl @@ -767,7 +767,7 @@ struct.Limits { namespace = "Caps" } .maxEncoders "uint32_t" --- Maximum number of encoder threads. .minResourceCbSize "uint32_t" --- Minimum resource command buffer size. .maxTransientVbSize "uint32_t" --- Maximum transient vertex buffer size. - .maxTansientIbSize "uint32_t" --- Maximum transient index buffer size. + .maxTransientIbSize "uint32_t" --- Maximum transient index buffer size. .minUniformBufferSize "uint32_t" --- Mimimum uniform buffer size. --- Renderer capabilities. @@ -1091,7 +1091,7 @@ func.VertexLayout.decode { const } .asInt "bool &" { out } --- Attribute is packed as int. --- Returns `true` if VertexLayout contains attribute. -func.VertexLayout.has { const, cppinline } +func.VertexLayout.has { const, cppinline = "return UINT16_MAX != m_attributes[_attrib];" } "bool" --- True if VertexLayout contains attribute. .attrib "Attrib::Enum" --- Attribute semantics. See: `bgfx::Attrib` @@ -1106,16 +1106,16 @@ func.VertexLayout["end"] "void" --- Returns relative attribute offset from the vertex. -func.VertexLayout.getOffset { const, cppinline } +func.VertexLayout.getOffset { const, cppinline = "return m_offset[_attrib];" } "uint16_t" --- Relative attribute offset from the vertex. .attrib "Attrib::Enum" --- Attribute semantics. See: `bgfx::Attrib` --- Returns vertex stride. -func.VertexLayout.getStride { const, cppinline } +func.VertexLayout.getStride { const, cppinline = "return m_stride;" } "uint16_t" --- Vertex stride. --- Returns size of vertex buffer for number of vertices. -func.VertexLayout.getSize { const, cppinline } +func.VertexLayout.getSize { const, cppinline = "return _num*m_stride;" } "uint32_t" --- Size of vertex buffer for number of vertices. .num "uint32_t" --- Number of vertices. diff --git a/scripts/codegen.lua b/scripts/codegen.lua index 4aa4eef05..afab8b0f4 100644 --- a/scripts/codegen.lua +++ b/scripts/codegen.lua @@ -466,7 +466,8 @@ local function codetemp(func) if arg.array then cname = cname .. (arg.carray or arg.array) end - local name = arg.fulltype .. " " .. arg.name + local argtype = arg.fulltype:gsub("%s+&", "&") + local name = argtype .. " " .. arg.name if arg.array then name = name .. arg.array end @@ -527,7 +528,7 @@ local function codetemp(func) end return { - RET = func.ret.fulltype, + RET = func.ret.fulltype:gsub("%s+&", "&"), CRET = func.ret.ctype, CFUNCNAME = func.cname, CFUNCNAMEUPPER = func.cname:upper(), @@ -594,7 +595,7 @@ local function doxygen_func(r, func, prefix) if arg.out then inout = "out" elseif arg.inout then - inout = "inout" + inout = "in,out" else inout = "in" end @@ -668,6 +669,12 @@ struct $NAME function codegen.gen_enum_define(enum) assert(type(enum.enum) == "table", "Not an enum") + local max_name_len = 0 + for _, item in ipairs(enum.enum) do + if item.comment and #item.name > max_name_len then + max_name_len = #item.name + end + end local items = {} for _, item in ipairs(enum.enum) do local text @@ -676,12 +683,12 @@ function codegen.gen_enum_define(enum) else local comment = table.concat(item.comment, " ") text = string.format("%s,%s //!< %s", - item.name, namealign(item.name), comment) + item.name, namealign(item.name, max_name_len), comment) end items[#items+1] = text end - local comment = "" - if enum.comment then + local comment = "//EMPTYLINE" + if enum.comment and enum.comment ~= "" then comment = "/// " .. enum.comment end local temp = { @@ -689,7 +696,7 @@ function codegen.gen_enum_define(enum) COMMENT = comment, ITEMS = table.concat(items, "\n\t\t"), } - return (enum_temp:gsub("$(%u+)", temp)) + return remove_emptylines(enum_temp:gsub("$(%u+)", temp)) end local cenum_temp = [[ @@ -832,7 +839,7 @@ function codegen.gen_flag_cdefine(flag) return table.concat(s, "\n") end -local function text_with_comments(items, item, cstyle, is_classmember) +local function text_with_comments(items, item, cstyle, is_classmember, name_align, comment_align) local name = item.name if item.array then if cstyle then @@ -850,25 +857,36 @@ local function text_with_comments(items, item, cstyle, is_classmember) if is_classmember then name = "m_" .. name end - local text = string.format("%s%s %s;", typename, namealign(typename), name) + local text = string.format("%s%s %s;", typename, namealign(typename, name_align or DEFAULT_NAME_ALIGN), name) if item.comment then if #item.comment > 1 then - table.insert(items, "") if cstyle then + table.insert(items, "") table.insert(items, "/**") for _, c in ipairs(item.comment) do table.insert(items, " * " .. c) end table.insert(items, " */") else - for _, c in ipairs(item.comment) do - table.insert(items, "/// " .. c) + local comment_col = math.max(#text + 1, comment_align) + local padding = string.rep(" ", comment_col - #text) + text = text .. padding .. "//!< " .. item.comment[1] + items[#items+1] = text + local cont_padding = string.rep(" ", comment_col) .. "/// " + for i = 2, #item.comment do + items[#items+1] = cont_padding .. item.comment[i] end + return end else - text = string.format( - cstyle and "%s %s/** %s%s */" or "%s %s//!< %s", - text, namealign(text, 40), item.comment[1], namealign(item.comment[1], 40)) + if cstyle then + text = string.format("%s %s/** %s%s */", + text, namealign(text, comment_align), item.comment[1], namealign(item.comment[1], 40)) + else + local comment_col = math.max(#text + 1, comment_align) + local padding = string.rep(" ", comment_col - #text) + text = text .. padding .. "//!< " .. item.comment[1] + end end end items[#items+1] = text @@ -885,9 +903,27 @@ struct $NAME function codegen.gen_struct_define(struct, methods) assert(type(struct.struct) == "table", "Not a struct") + local is_classmember = methods ~= nil and not struct.shortname + local max_text_len = 0 + for _, item in ipairs(struct.struct) do + if item.comment then + local name = item.name + if item.array then + name = name .. item.array + end + if is_classmember then + name = "m_" .. name + end + local text_len = #item.fulltype + 1 + #name + 1 + if text_len > max_text_len then + max_text_len = text_len + end + end + end + local comment_align = max_text_len + 1 local items = {} for _, item in ipairs(struct.struct) do - text_with_comments(items, item, false, methods ~= nil and not struct.shortname) + text_with_comments(items, item, false, is_classmember, 0, comment_align) end local ctor = {} if struct.ctor then @@ -957,10 +993,7 @@ function codegen.gen_chandle(handle) return (chandle_temp:gsub("$(%u+)", { NAME = handle.cname:match "(.-)_t$" })) end -local handle_temp = [[ -struct $NAME { uint16_t idx; }; -inline bool isValid($NAME _handle) { return bgfx::kInvalidHandle != _handle.idx; } -]] +local handle_temp = "BGFX_HANDLE($NAME)" function codegen.gen_handle(handle) assert(handle.handle, "Not a handle") return (handle_temp:gsub("$(%u+)", { NAME = handle.name })) diff --git a/scripts/temp.bgfx.hpp b/scripts/temp.bgfx.hpp index a5b02518f..0516b6c0a 100644 --- a/scripts/temp.bgfx.hpp +++ b/scripts/temp.bgfx.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2011-2019 Branimir Karadzic. All rights reserved. + * Copyright 2011-2026 Branimir Karadzic. All rights reserved. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE */ @@ -36,7 +36,7 @@ namespace bgfx $enums -static const uint16_t kInvalidHandle = UINT16_MAX; + constexpr uint16_t kInvalidHandle = UINT16_MAX; /// View id. typedef uint16_t ViewId; @@ -247,14 +247,6 @@ $funcptrs $cppdecl -inline bool VertexLayout::has(Attrib::Enum _attrib) const { return UINT16_MAX != m_attributes[_attrib]; } - -inline uint16_t VertexLayout::getOffset(Attrib::Enum _attrib) const { return m_offset[_attrib]; } - -inline uint16_t VertexLayout::getStride() const { return m_stride; } - -inline uint32_t VertexLayout::getSize(uint32_t _num) const { return _num*m_stride; } - } // namespace bgfx #endif // BGFX_H_HEADER_GUARD