bgfxidl
bgfxidl copied to clipboard
Discussion cont.
From https://github.com/cloudwu/sproto/issues/83
To generate C++ header, we may need annotate the default value.
How about
func.frame
"uint32_t"
.capture "bool" { default = false }
Sounds good.
Also doxygen comments. But ignore doxygen for now until all these C99 bindings are replaced.
I want to replace: https://github.com/bkaradzic/bgfx/blob/a356a1d05193aafe90999db1a87ddce8c0e80856/include/bgfx/c99/platform.h#L56 https://github.com/bkaradzic/bgfx/blob/a356a1d05193aafe90999db1a87ddce8c0e80856/src/bgfx.cpp#L6383
Btw, I'm testing current generated code like this:
diff --git a/src/bgfx.cpp b/src/bgfx.cpp
index 5aa4945..95d5a63 100644
--- a/src/bgfx.cpp
+++ b/src/bgfx.cpp
@@ -5294,6 +5294,9 @@ namespace bgfx
} // namespace bgfx
+#if 1
+# include "../test.idl.cpp"
+#else
BGFX_C_API void bgfx_vertex_decl_begin(bgfx_vertex_decl_t* _decl, bgfx_renderer_type_t _renderer)
{
bgfx::VertexDecl* decl = (bgfx::VertexDecl*)_decl;
@@ -6352,6 +6355,7 @@ BGFX_C_API void bgfx_request_screen_shot(bgfx_frame_buffer_handle_t _handle, con
union { bgfx_frame_buffer_handle_t c; bgfx::FrameBufferHandle cpp; } handle = { _handle };
bgfx::requestScreenShot(handle.cpp, _filePath);
}
+#endif
There are few functions that truncate last character after number: bgfx_create_texture2, bgfx_create_texture3 etc. instead having d at the end.
So now that everything is fixed I'm thinking of putting it into bgfx.
Right now I'm setting up files. include/bgfx/c99/platform.h is going away (it's just easier without it), and there will be src/bgfx.idl.inl, and include/bgfx/c99/bgfx.idl.h. I'm going to manually do this first time to setup whole thing into working state, before getting script straight into bgfx.
It's in: https://github.com/bkaradzic/bgfx/commit/29edbaa2fd9d5869ccd36f7ae43c9a8976bde80c
I rewrite the test.lua for better code gen.
https://github.com/cloudwu/bgfxidl/blob/master/test.lua#L973-L1020
Before I move your code into bgfx, could you add your copyright/license info?
Ok, I will use the same license of bgfx.
I rewrite the test.lua for better code gen.
Hehe you know Lua magic. :)
License added.
Maybe we can design a new DSL for bgfx shader. lua interpreter is small as C Preprocessor , and we can keep it simple but more flexible.
Ooops... I just forced everyone using C99 to switch to encoder API. :)
Maybe we can design a new DSL for bgfx shader. lua interpreter is small as C Preprocessor , and we can keep it simple but more flexible.
Interesting idea. Do you have some examples of existing DSL made in Lua?
For IDL integration to bgfx it should dump output to two different files, and it should be invoked with genie idl. Something like this: https://github.com/bkaradzic/GENie/blob/49b3a835be16156b62fad9f547f5221098f8f954/scripts/genie.lua#L123-L129
Another thing. It would be good that IDL for class members automatically add their class name to function.
For example:
func.Encoder.setInstanceDataBuffer { cname = "encoder_set_instance_data_from_dynamic_vertex_buffer" }
cname just needs to be set_instance_data_from_dynamic_vertex_buffer.
Another thing. It would be good that IDL for class members automatically add their class name to function.
For example:
func.Encoder.setInstanceDataBuffer { cname = "encoder_set_instance_data_from_dynamic_vertex_buffer" }cname just needs to be
set_instance_data_from_dynamic_vertex_buffer.
https://github.com/cloudwu/bgfxidl/commit/3865955c77247bd1091de106be61c4d085b04658
Accept my pull request here. I did some touchups how output looks like. And then you can prepare PR for bgfx. I think everything is good.
For PR:
test.lua should be bgfx-idl.lua, other files could be the same, and it should just overwrite include/bgfx/c99/bgfx.idl.h and src/bgfx.idl.inl when invoked with genie idl.
Maybe we can design a new DSL for bgfx shader. lua interpreter is small as C Preprocessor , and we can keep it simple but more flexible.
Interesting idea. Do you have some examples of existing DSL made in Lua?
Just google it.
https://developer.download.nvidia.cn/gameworks/events/GDC2016/tfoley_shader_programming.pdf
Terra: framework for easily constructing DSLs Use Lua scripts to extend language/compiler generate code
But I haven't seen how the DSL like.
Lua has a powerful lib lpeg ( http://www.inf.puc-rio.br/~roberto/lpeg/ ) , it's easy to create a DSL . Without lpeg, we can also generate codes by lua string api (like this IDL) easily , it would be a better choice than C Preprocessor.
My opinion is use lua to replace C preprocessor first, keep compatibility , and then add some new syntax to replace C macros.
Just google it.
I meant specifically one that uses Lua. I've seen other DSL, but it was usually regular lexer/parser type of stuff.
My opinion is use lua to replace C preprocessor first, keep compatibility , and then add some new syntax to replace C macros.
Ah ok. I actually have lexer/parser front end for new shaderc. And it will remove all macro magic currently in shaderc.
So in order to get C++ API to work with IDL we need:
- Defines
- Structs and types
- Doxygen comments
As first implementation it's enough just to define IDL fields that I can fill up, without generating anything. And once I move everything into IDL, you would have everything you need to do Lua magic for C99 headers. :)
For example for enums is this is possible:
typedef.TextureFormat { enum }
.BC1 "DXT1"
.BC2 "DXT3"
.BC3 "DXT5"
.BC4 "LATC1/ATI1"
.BC5 "LATC2/ATI2"
.BC6H "BC6H"
.BC7 "BC7"
...
I will add enum define first.
oh yeah that could be enum.TextureFormat too
Have a look https://github.com/cloudwu/bgfxidl/blob/master/bgfx.idl#L42-L62 .
--- Vertex attribute enum.
enum.Attrib { comment = "Corresponds to vertex shader attribute." }
.Position "a_position"
.Normal "a_normal"
.Tangent "a_tangent"
.Bitangent "a_bitangent"
.Color0 "a_color0"
.Color1 "a_color1"
.Color2 "a_color2"
.Color3 "a_color3"
.Indices "a_indices"
.Weight "a_weight"
.TexCoord0 "a_texcoord0"
.TexCoord1 "a_texcoord1"
.TexCoord2 "a_texcoord2"
.TexCoord3 "a_texcoord3"
.TexCoord4 "a_texcoord4"
.TexCoord5 "a_texcoord5"
.TexCoord6 "a_texcoord6"
.TexCoord7 "a_texcoord7"
Use lua bgfx-idl.lua . to generate bgfx.types.h and bgfx.ctypes.h
/// Vertex attribute enum.
///
/// @attention C99 equivalent is `bgfx_attrib_t`.
///
struct Attrib
{
/// Corresponds to vertex shader attribute.
enum Enum
{
Position, //!< a_position
Normal, //!< a_normal
Tangent, //!< a_tangent
Bitangent, //!< a_bitangent
Color0, //!< a_color0
Color1, //!< a_color1
Color2, //!< a_color2
Color3, //!< a_color3
Indices, //!< a_indices
Weight, //!< a_weight
TexCoord0, //!< a_texcoord0
TexCoord1, //!< a_texcoord1
TexCoord2, //!< a_texcoord2
TexCoord3, //!< a_texcoord3
TexCoord4, //!< a_texcoord4
TexCoord5, //!< a_texcoord5
TexCoord6, //!< a_texcoord6
TexCoord7, //!< a_texcoord7
Count
};
};
typedef enum bgfx_attrib
{
BGFX_ATTRIB_POSITION,
BGFX_ATTRIB_NORMAL,
BGFX_ATTRIB_TANGENT,
BGFX_ATTRIB_BITANGENT,
BGFX_ATTRIB_COLOR0,
BGFX_ATTRIB_COLOR1,
BGFX_ATTRIB_COLOR2,
BGFX_ATTRIB_COLOR3,
BGFX_ATTRIB_INDICES,
BGFX_ATTRIB_WEIGHT,
BGFX_ATTRIB_TEXCOORD0,
BGFX_ATTRIB_TEXCOORD1,
BGFX_ATTRIB_TEXCOORD2,
BGFX_ATTRIB_TEXCOORD3,
BGFX_ATTRIB_TEXCOORD4,
BGFX_ATTRIB_TEXCOORD5,
BGFX_ATTRIB_TEXCOORD6,
BGFX_ATTRIB_TEXCOORD7
BGFX_ATTRIB_COUNT
} bgfx_attrib_t;
I can't find a good way to add doxygen comments in lua syntax directly.
So I add idl.comment to add a comment to a type. https://github.com/cloudwu/bgfxidl/blob/master/test.lua#L19
For the .idl files, we can use doxygen.import(filename) to grab all the lua comment lines with --- as the doxygen comments, and call idl.comment
@bkaradzic You can complete bgfx.idl with the same syntax , including func's doxygen comments.
I will do the next part ( c++ function declaration).
Oh wow grabbing --- is actually amazing! :)
It would be easy to get doxygen comments across.
TYPE: bool bool
TYPE: Attrib::Enum bgfx_attrib_t
/// Vertex attribute enum.
///
/// @attention C99 equivalent is `bgfx_attrib_t`.
///
/home/bkaradzic/Private/projects/_github/bgfxidl/test.lua:82: attempt to call a nil value (field 'typegen_enums')
stack traceback:
/home/bkaradzic/Private/projects/_github/bgfxidl/test.lua:82: in main chunk
@bkaradzic You can annotate default values for functions first.
https://github.com/cloudwu/bgfxidl/commit/312ee1c309414193cc625976b215c5d3b4fe2671
numbers, true, false, NULL, and INT*_MAX can be used directly, others should be quoted like { default = "TextureFormat::Count" }
define struct
--- Platform data.
struct.PlatformData { ctor }
.ndt "void*" "Native display type." -- or { comment = "Native display type." }
.nwh "void*" "Native window handle."
.context "void*" "GL context, or D3D device."
.backBuffer "void*" "GL backbuffer, or D3D render target view."
.backBufferDS "void*" "Backbuffer depth/stencil."
--- Memory must be obtained by calling `bgfx::alloc`, `bgfx::copy`, or `bgfx::makeRef`.
---
--- @attention It is illegal to create this structure on stack and pass it to any bgfx API.
struct.Memory
.data "uint8_t *" "Pointer to data."
.size "uint32_t" "Data size."
C++
/// Memory must be obtained by calling `bgfx::alloc`, `bgfx::copy`, or `bgfx::makeRef`.
///
/// @attention It is illegal to create this structure on stack and pass it to any bgfx API.
///
/// @attention C99 equivalent is `bgfx_memory_t`.
///
struct Memory
{
uint8_t * data; //!< Pointer to data.
uint32_t size; //!< Data size.
};
/// Platform data.
///
/// @attention C99 equivalent is `bgfx_platform_data_t`.
///
struct PlatformData
{
PlatformData();
void* ndt; //!< Native display type.
void* nwh; //!< Native window handle.
void* context; //!< GL context, or D3D device.
void* backBuffer; //!< GL backbuffer, or D3D render target view.
void* backBufferDS; //!< Backbuffer depth/stencil.
};
C
typedef struct bgfx_memory_s
{
uint8_t * data;
uint32_t size;
} bgfx_memory_t;
typedef struct bgfx_platform_data_s
{
void* ndt;
void* nwh;
void* context;
void* backBuffer;
void* backBufferDS;
} bgfx_platform_data_t;
Array type is supported. See https://github.com/cloudwu/bgfxidl/commit/e3602845ed4bf57069d0971cbb5d453b2190f288
Can you also add Doxygen comments to C API. Only comment style should change from /// ... to block comment /** ... */