3.0引擎的自动合并(Auto-batching)~

本文深入解析Cocos2d-X v3.0版本中渲染器的改进,包括新API、性能优化、功能扩展等关键特性。着重介绍了自动批处理、自动裁剪等功能,以及与旧版SpriteBatchNode的对比,旨在提高游戏性能。

https://github.com/cocos2d/cocos2d-x/blob/develop/docs/RELEASE_NOTES.md#misc-information

说白了就是可以放node了

 

New Renderer

Feature added in v3.0-beta and improved in v3.0-beta2

The way currently cocos2d-x v2.2 does rendering is OK but it is difficult to optimize, difficult to add new functionality and difficult to port to new platforms. So cocos2d-x v3.0 has a new renderer that is more performing, elegant, scalable, flexible but still simple to use and to understand. Also existing cocos2d-x users will find the new API familiar and they will feel immediately comfortable with, without having to bother about what’s changed or what's new under the hood.

Features of the new renderer:

  • It has been decoupled from the Scene Graph. The draw() method, instead of "drawing" it sends aRenderCommand to the Renderer, and Renderer is responsible for drawing the queuedRenderCommand commands.
  • QuadCommands (used by Sprite and ParticleSystem objects) will be automatically batched.
  • CustomCommand objects allow the user to use custom OpenGL code, using a API similar to v2.2
  • GroupCommand objects allow to have "stacks" in the Renderer with different OpenGL values.
  • Auto-culling for Sprite objects (although, technically, Auto-culling is not performed inRenderer code, but in the Sprite code)
  • Global Z ordering (local Z ordering is still supported)

For detailed information, please read the following doc: Renderer Specification document

Renderer features

Auto-batching

Auto-batching means that the Renderer will package "multiple draw calls" in just one "big draw call" (AKA batch). In order to group "draw calls" certain conditions are needed:

  • It only works with QuadCommand commands (used by Sprite and ParticleSystem objects)
  • The QuadCommands must share the same Material ID: same Texture ID, same GLProgram and same blending function
  • The QuadCommands must consecutive

If those conditions are met, the Renderer will create create a batch (one draw call) with all thoseQuadCommand objects.

In case you are unfamiliar with the OpenGL best practices, batching is very important to have decent speed in your games. The less batches (draw calls) the more performance your game is going to be.

Auto-culling

For the moment auto-culling is only implemented on Sprite objects.

When the method Sprite::draw() is called, it will check if the Sprite is outside the screen. If so, it won't send the QuadCommand command to theRenderer, and thus, it will gain some performance.

 

Sprite vs. SpriteBatchNode

In v2.2 the recommended way to have good performance was to parent Sprite objects to aSpriteBatchNode object. Although the performance was (is still) very good by usingSpriteBatchNode objects, they had (still have) some limitations like:

  • Sprite objects can only have Sprite objects as children (if not, cocos2d-x will raise an Assert)
    • You cannot add a ParticleSystem as a child of Sprite, when theSprite is parented to a SpriteBatchNode
    • As a consequence of that, you cannot use ParallaxNode with Sprites parented to SpriteBatchNode
  • All Sprite objects must share the same TextureId (if not, cocos2d-x will raise an Assert)
  • Sprite objects use the SpriteBatchNode's blending function and shader.

And although v3.0 still supports SpriteBatchNode (with the same features and limitations), we no longer encourage its usage. Instead, we recommend to useSprite objects without parenting them to a SpriteBatchNode.

But in order to have a very good performance in v3.0, you have to make sure that yourSprite objects:

  • Share the same TextureId (place them in a spritesheet, like if you were using aSpriteBatchNode)
  • Make sure all of them use the same shader and blending function (like if you were using aSpriteBatchNode)

If you do so, the Sprites will perform almost as fast as to usingSpriteBatchNode... (about 10% slower on old devices. On newer devices the difference is almost imperceptible)

The big differences between v2.2 and v3.0 are:

  • Sprite objects can have different Texture IDs.
  • Sprite objects can have any kind of Node as children, includingParticleSystem.
  • Sprite objects can have different blending functions and use different shaders.

But if you do that, the Renderer might not be able to batch all its children (less performant). But the game will keep running, without raising any Assert.

To summarize:

  • Keep putting all your sprites in a big spritesheet
  • Use the same Blending Function (just use the default one)
  • Use the same Shader (just use the default one)
  • And don't parent your sprites to a SpriteBatchNode

Only use SpriteBatchNode as the last resort, when you really need an extra (although minor) boost in performance (and you are OK with its limitations).

 

 

这个是渲染的文档。

https://docs.google.com/document/d/17zjC55vbP_PYTftTZEuvqXuMb9PbYNxRFu0EGTULPK8/edit#

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值