Automatic Batching in Cocos2d 3

77 views
Skip to first unread message

Hao Wu

unread,
Jun 26, 2013, 11:51:18 PM6/26/13
to cocos2d-...@googlegroups.com
Hi guys,

Yes I know that we are going to support automatic batching to "Reduce" the number of draw calls

but there is a way to make the number of draw calls to 1. yes 1, how?

While ago we were investigating the possibility of inserting a sprite into a batch node sprites, so that it renders in-between the batch node sprites.

this was not possible, because its 2 different draw calls.
and this was limited by a draw call must bind a single texture.

What we found is that we could bind multiple texture into a single draw call.
By using multi texture in a single draw, we could potentially bind all texture to a single call, thus everything can be drawn in 1 batch


here is the shader (all credit to Dingping (David) lv)
precision lowp float;                                                       
                                                     
        varying vec4 v_fragmentColor;                
        varying vec2 v_texCoord;                     
        varying float v_texIndex;                    
                                                     
        uniform sampler2D CC_Texture0;               
        uniform sampler2D CC_Texture1;               
        uniform sampler2D CC_Texture2;               
        uniform sampler2D CC_Texture3;               
        uniform sampler2D CC_Texture4;               
        uniform sampler2D CC_Texture5;               
        uniform sampler2D CC_Texture6;               
        uniform sampler2D CC_Texture7;               
                                                     
        void main()                                  
        {                                            
            gl_FragColor = v_fragmentColor * texture2D(CC_Texture[texIndex], v_texCoord);}                            
        };


OpenGL specification states that multi texture supports at least 8 textures.

We have not tried this approach on C++ and real devices yet, we have only tried it in webGL.
It works but the only caveat is that in webGL, you cannot use array index like CC_Texture[texIndex], you must use if else which slows it down a little.

I theory, this should work fine on c++, real device, and should really speed up drawing process by a lot, it also simplifies your render queue sorting process

Carlo could you investigate this further? and what do you guys think?

Regards

Hao

Ricardo Quesada

unread,
Jun 27, 2013, 12:31:51 AM6/27/13
to Hao Wu, cocos2d-...@googlegroups.com
Thanks!

I think multi texturing is very useful, but I am not sure whether or not we could use it an "general purpose" way.
Carlo, any idea if it could be used in a "general purpose" way ? thanks.


--
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/groups/opt_out.
 
 

Carlo Morgantini

unread,
Jun 27, 2013, 3:05:34 PM6/27/13
to Ricardo Quesada, Hao Wu, cocos2d-...@googlegroups.com
Hi, 
yes, multi texturing is definitely something we are looking into for mitigating the cost of too many draw calls per frame. (I've used this techniques already @ zyngra for a project I've worked on few months back)
I believe (but I could be wrong, need to do some more investigations/research on this topic)  samplers array on iOS are "legal" but the index used for picking the sampler must be a constant OR a uniform...so it would not work for our purpose of rendering multiple triangles in one draw call picking textures "dynamically" based on shader attributes.
We could still use IF (dynamic branching) or look up all the textures and use only the one we actually need to use...even if expensive, this could, in certain cases, still be a win, compared to the overhead of invoking "drawTriangles" and binding textures thousands of times per frame.
So, I would keep the auto batching but I would also explore the multi texturing option (the two techniques are in fact not orthogonal and can be used together).
Thank you for pointing this out Hao!

Carlo  
Reply all
Reply to author
Forward
0 new messages