bgfxidl icon indicating copy to clipboard operation
bgfxidl copied to clipboard

Discussion cont.

Open bkaradzic opened this issue 6 years ago • 156 comments

From https://github.com/cloudwu/sproto/issues/83

bkaradzic avatar Feb 26 '19 08:02 bkaradzic

To generate C++ header, we may need annotate the default value.

How about

func.frame
	"uint32_t"
	.capture "bool" { default = false }

cloudwu avatar Feb 26 '19 08:02 cloudwu

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

bkaradzic avatar Feb 26 '19 15:02 bkaradzic

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

bkaradzic avatar Feb 26 '19 16:02 bkaradzic

There are few functions that truncate last character after number: bgfx_create_texture2, bgfx_create_texture3 etc. instead having d at the end.

bkaradzic avatar Feb 27 '19 04:02 bkaradzic

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.

bkaradzic avatar Feb 27 '19 07:02 bkaradzic

It's in: https://github.com/bkaradzic/bgfx/commit/29edbaa2fd9d5869ccd36f7ae43c9a8976bde80c

bkaradzic avatar Feb 27 '19 07:02 bkaradzic

I rewrite the test.lua for better code gen.

https://github.com/cloudwu/bgfxidl/blob/master/test.lua#L973-L1020

cloudwu avatar Feb 27 '19 07:02 cloudwu

Before I move your code into bgfx, could you add your copyright/license info?

bkaradzic avatar Feb 27 '19 07:02 bkaradzic

Ok, I will use the same license of bgfx.

cloudwu avatar Feb 27 '19 07:02 cloudwu

I rewrite the test.lua for better code gen.

Hehe you know Lua magic. :)

bkaradzic avatar Feb 27 '19 07:02 bkaradzic

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.

cloudwu avatar Feb 27 '19 07:02 cloudwu

Ooops... I just forced everyone using C99 to switch to encoder API. :)

bkaradzic avatar Feb 27 '19 07:02 bkaradzic

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

bkaradzic avatar Feb 27 '19 07:02 bkaradzic

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.

bkaradzic avatar Feb 27 '19 08:02 bkaradzic

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

cloudwu avatar Feb 27 '19 08:02 cloudwu

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.

bkaradzic avatar Feb 27 '19 08:02 bkaradzic

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.

cloudwu avatar Feb 27 '19 08:02 cloudwu

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.

bkaradzic avatar Feb 27 '19 08:02 bkaradzic

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.

bkaradzic avatar Feb 27 '19 08:02 bkaradzic

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. :)

bkaradzic avatar Feb 27 '19 23:02 bkaradzic

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"
...

bkaradzic avatar Feb 27 '19 23:02 bkaradzic

I will add enum define first.

cloudwu avatar Feb 28 '19 01:02 cloudwu

oh yeah that could be enum.TextureFormat too

bkaradzic avatar Feb 28 '19 04:02 bkaradzic

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).

cloudwu avatar Feb 28 '19 07:02 cloudwu

Oh wow grabbing --- is actually amazing! :) It would be easy to get doxygen comments across.

bkaradzic avatar Feb 28 '19 07:02 bkaradzic

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 avatar Feb 28 '19 07:02 bkaradzic

@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" }

cloudwu avatar Feb 28 '19 08:02 cloudwu

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;

cloudwu avatar Feb 28 '19 10:02 cloudwu

Array type is supported. See https://github.com/cloudwu/bgfxidl/commit/e3602845ed4bf57069d0971cbb5d453b2190f288

cloudwu avatar Feb 28 '19 11:02 cloudwu

Can you also add Doxygen comments to C API. Only comment style should change from /// ... to block comment /** ... */

bkaradzic avatar Feb 28 '19 17:02 bkaradzic