Render Primitive added

20 views
Skip to first unread message

huabing.xu

unread,
Aug 8, 2014, 2:53:20 AM8/8/14
to Ricardo Quesada, cocos2d-js-devel

Hi,

Render primitive was working now. 

It was opened in pull request: https://github.com/cocos2d/cocos2d-x/pull/7701

The current status are listed here


FEATURES:

1. Encapsulate openGL call, user do not need to worry about glcalls

2. Support GL_POINTS, GL_LINES, GL_TRIANGLES type

3. Support int32 and short16 indices

4. Support VertexBuffer and IndexBuffer sharing across multiple primitives

5. Support multiple streams for rendering.


USAGE and RENDERING:

The creation of primitive has several steps:


//create vertexBuffer and vertexData

vertexBuffer1 = VertexBuffer:create(…)

vertexBuffer2 = VertexBuffer:create(…)

vertexData = VertexData::create()

vertexData->setStream(vertexBuffer1, VertexStreamAttribute(0, GLProgram::VERTEX_ATTRIB_POSITION, GL_FLOAT, 3))

vertexData->setStream(vertexBuffer1VertexStreamAttribute(12, GLProgram::VERTEX_ATTRIB_NORMAL, GL_FLOAT, 3))

vertexData->setStream(vertexBuffer1, VertexStreamAttribute(0, GLProgram::VERTEX_ATTRIB_TEXCOORD, GL_FLOAT, 2))


//createIndexBuffer if has one

indexBuffer = indexBuffer:create()


//createPrimitive

//using null instead of indexbuffer if no indices

primitive =Primitve:create(vertexData, indexBuffer, GL_POINTS)

primitive =Primitve:create(vertexData, null, GL_TRIANGLES)


//set begin and count, because we may only using a part of indexbuffer and vertexbuffer for rendering

primitive->setBegin(begin);

primitive->setCount(count);


The PrimitiveCommand is used for rendering Primitive, its interface is almost the same as QuadCommand, except a primitive is used instead of quads.

FAST_TMX migrated to primitive:
our experimental fast_tmx has been migrated to primitive and primitive command for its rendering.

NEXT STEP:
1. adding vao support if it is enabled on device
2. remove redundant glbindBuffer calling
3. try to add batching
4. adding new type (such as GL_TRIANGLE_STIP etc) if it is needed.


BENEFIT of using it:
1. do not worry about low level detail of device (VAO, resource recreation on android,avoiding depressed glerror  etc), user can keep focus on how to arrange you data
2. very easy to change data type: I tried to chang indices type from int32 to short16 in fast_tmx, git diff only shows 8 line differences, in which 6 out of them are added to cast int to short.
3. vertexBuffer  and IndexBuffer sharing means we can use a big buffer to avoid memory fragment.

BTW, the new feature-sprite mesh will be based on the primitive. I will keep working on it.
Comments and suggestions are highly appreciated.

Best Regards
Harrison Xu

Ricardo Quesada

unread,
Aug 21, 2014, 6:35:45 PM8/21/14
to huabing.xu, cocos2d-js-devel
Hi,

Thanks.

Questions:

a)
Can you batch primitive commands ? Let's say that I submit two primitive commands, will they be batched ?
If not, why not ?
One of the requirements that we have from Spine, is to batch multiple Spine animations.
If we are going to use PrimitiveCommand there, then it must be possible to batch multiple PrimitiveCommands

b)
What about batching a QuadCommand with a PrimitiveCommand, Can they be batched ?
If not, why not ?

c)
Does it make sense to replace QuadCommand with PrimitiveCommand ?


d)
Where should we use it ?
I think we should replace the CustomCommands either with PrimitiveCommand or QuadCommand.
We should stop using CustomCommand internally


Some comments:
 - I think we are polluting the "cocos2d" namespace with too many internal classes.
  As an example, a new class called Primitive was added into the "cocos2d".
  I think the "cocos2d" namespace should have public classes, and internal / helper classes should be in anonymous namespaces, or something internal like  cocos2d::renderer::Primitive.

- Please, take a look at the cocos2d-x C++ coding style: https://github.com/cocos2d/cocos2d-x/blob/v3/docs/CODING_STYLE.md

Thanks.

huabing.xu

unread,
Aug 21, 2014, 10:13:27 PM8/21/14
to Ricardo Quesada, cocos2d-js-devel

Thanks, Riq

Here are my feedbacks.

1) PrimitiveCommand could replace many CustomCommand.

We have mesh command, Quad command and batch command for submitting renderable geometries (Triangles, Quads etc). Other renderable geometries can only be rendered as CustomCommand, these custom command could be replaced by Primitive command.

2) Primitve and PrimitiveCommand now have some constrains:

 a. can not be batched
 b. vertices and indices must be pre allocated.

So the answers of your questions:

a) and b) : They can not be batched. due to the constrains listed above. 
Far more, multiple streams supporting make batching much more difficult.

c) Quad Command can be replaced, but the performance could be worse than Quad Command.

What am now doing is to improvement Primitive and Primitive Command, which includs:

  1. Vertices and Indices could be added on the fly.

  2. Add Batch support for primitive. The core of batching is that we can merge two primitives into one.

In order to do this, multiple stream support will be removed temporarily, which will make batching much more complex and increasing the overhead for batching computation, and there is no requirements for multiple stream support in our engine now.

Primitive could be batched only when they have the same vertex specification.

BTW, Thanks for your comments, I will adjust code as you suggested.


Best Regards
Harrison Xu

 Original Message 
Sender: Ricardo Quesada<ricardo...@gmail.com>
Recipient: huabing.xu<huabi...@cocos2d-x.org>
Cc: cocos2d-js-devel<cocos2d-...@googlegroups.com>
Date: Friday, Aug 22, 2014 06:35
Subject: [cocos2d-js-devel] Re: Render Primitive added

--
You received this message because you are subscribed to the Google Groups "cocos2d JS development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cocos2d-js-dev...@googlegroups.com.
To post to this group, send email to cocos2d-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ricardo Quesada

unread,
Aug 21, 2014, 10:50:01 PM8/21/14
to huabing.xu, cocos2d-js-devel
Thanks.

So I think the PrimitiveCommand should replace the BatchCommand.
The BatchCommand was used to batch some geometry based on some pre-defined values. PrimitiveCommand seems to be more flexible.

What I don't know is if we need to:
  - add batching support to multiple PrimitiveCommand
  - or create a new command that supports that.

For example, I know if I submit the TMX map I don't want to batch it with other commands.
But if I submit an Spine animation, I want to batch it with another animations.

 

huabing.xu

unread,
Aug 22, 2014, 12:00:43 AM8/22/14
to Ricardo Quesada, cocos2d-js-devel, cocos3d-x-devel

Yes,

It seems that Batch command and Custom command in LayerColor can be replaced by primitive command.

And 3D team is migrating into Primitive to support submesh.


For batching support. How about trying the second option (- or create a new command that supports that.)? Which will not affect the current features of Primitive.


Best Regards
Harrison Xu

 Original Message 
Sender: Ricardo Quesada<ricardo...@gmail.com>
Recipient: huabing.xu<huabi...@cocos2d-x.org>
Cc: cocos2d-js-devel<cocos2d-...@googlegroups.com>
Date: Friday, Aug 22, 2014 10:50
Subject: Re: [cocos2d-js-devel] Re: Render Primitive added

Ricardo Quesada

unread,
Aug 26, 2014, 3:02:33 PM8/26/14
to huabing.xu, cocos2d-js-devel, cocos3d-x-devel
Hi,

So, let's define the requirements:

a) The new PrimitiveCommand, in order to be used in Spine, must support batching. 
b) If it cannot support batching, then we need a new command that supports batching for Spine and other 2d features
c) If the new PrimitiveCommand is better than the current BatchCommand, then the BatchCommand must be deprecated, and ALL the code that is using BatchCommand needs to be updated.

For c), one way to do it, is that the BatchCommand, internally, creates a PrimitiveCommand and uses that instead.

Thanks.


huabing.xu

unread,
Aug 28, 2014, 3:19:13 AM8/28/14
to Ricardo Quesada, cocos2d-js-devel, cocos3d-x-devel

Thanks, Riq


Progess for requirements 1) and 2)


After discussing with minggo. We found that supporting batching in Primitive and PrimitiveCommand is difficult because there are so many factors involved. 

These factors include: 

1.  Vertex Semantic: some primitive may have GLProgram:VERTEX_ATTRIB_COLOR, some may not.

2. Same Semantic, but with different data types: Some may use 3 float for position, some use 4 float.

3. Same Semantic, but with different streams: Given they have same Semantic, one use interleaved array, another one use seperate array.

4. Some Primitive Type can not be Batched: If we want to support GL_TRIANGLE_STRIP, primitives batches can not be supported.


Given these, we decide that if we want to batch Primitive, a new wrapper need to be placed on top of primitive, Managing the vertices and indices, generate primitves and primitiveCommands for rendering.

Eg, In Particle system, A Particle Renderer which is hold by particle systems can be used to batch Particles, and provide primitive and primitive command for rendering.


So the role the primitive and primitive Command is to wrapper complex meshes for rendering, which need to be done by custom command or other commands.


And in those days, I did some work to improve our auto batching, it now support more types than QuadCommand.


A new command called TrianglesCommand is introduced, which will support Triangles for rendering. Triangles are defined like this.

      // pointers are weak reference    

     struct Triangles

    {

        V3F_C4B_T2F* verts;

        unsigned short* indices;

        ssize_t vertCount;

        ssize_t indexCount;

    };


TrianglesCommand can be auto batched, users do not need any extra work.
What they need to do is creating this simple Triangles, initing the TrianglesCommand, and adding it to renderer.
And Renderer now only knows about TrianglesCommand. Every command which inherit from TrianglesCommand and can tessellate their Mesh into Triangles can support auto batching.
Now the quad command is a subclass of TrianglesCommand. and I have tested to use TrianglesCommand for sprite rendering. It works well.

So for the new feature SpriteMesh and spine, given that they can use a Triangles for rendering. their renderCommands can be auto batched.

Here is the link of the TrianglesCommand:

Please take a look at it, If you agree with that, I will do some clean up work and send a pull request.


For Spine features

I have send a pull request to Spine official repository to move blend func into PolygonBatch. It can also works with the 3.2 release of cocos2d-x. the PR link is https://github.com/EsotericSoftware/spine-runtimes/pull/274. It seems there is no response. How can I get in touch with spine guys to review my code?
Besides this, some discussion for using new renderCommand (Triangles command or primitive command) to render spine is also needed.

For PrimitiveCommand migration
I will work on this before I get feedbacks and can continue working on Spine Features.

Best Regards
Harrison Xu

 Original Message 
Sender: Ricardo Quesada<ricardo...@gmail.com>
Recipient: huabing.xu<huabi...@cocos2d-x.org>
Cc: cocos2d-js-devel<cocos2d-...@googlegroups.com>; cocos3d-x-devel<cocos3d...@googlegroups.com>
Date: Wednesday, Aug 27, 2014 03:02
Reply all
Reply to author
Forward
0 new messages