cocos2dx 3.0 研究(4)渲染分析

来源:互联网 发布:windows域管理 编辑:程序博客网 时间:2024/05/16 09:26

http://blog.csdn.net/epeaktop/article/details/26730909中已经说明了程序的设计,让我们来看看在cocos2dx 3.0中代码是如何实现的。

void Sprite::draw(Renderer *renderer, const kmMat4 &transform, bool transformUpdated){    // Don't do calculate the culling if the transform was not updated    _insideBounds = transformUpdated ? renderer->checkVisibility(transform, _contentSize) : _insideBounds;    if(_insideBounds)    {                _quadCommand.init(_globalZOrder, _texture->getName(), _shaderProgram, _blendFunc, &_quad, 1, transform);        renderer->addCommand(&_quadCommand);    }}


renderer->addCommand 将执行的指令放到一个队列中。

然后在统一的处理,在drawScence函数中可以观察到先调用visit(),然后在调用render()

然而没有发现http://blog.csdn.net/epeaktop/article/details/26730909的设计的相关实现,依旧是主线程在做这些事情。why?目前还在确认之中。有知道的童鞋请告诉我吧!

对此我还是有点失望,引擎还是没有质的飞跃。

官方文档阐述相关实现如图


visit()函数加入render queque中;render 负责排序;然后执行命令。

官方的说法这样做是为了解决v2.2这样的问题

1、draw() order == visit() order //  这么做的目的是?

2、Difficult to optimize(No auto-batching)// 不好优化,后面会讲到

3、Difficult to extend and maintain(Lots of OpenGL command in Nodes)// 不好扩展和维护

在游戏的绘制渲染中,往往消耗很多资源和内存,当绘制精灵数量越多,游戏的卡顿会很明显,为了优化和提升渲染效率。Cocos2d-x为我们提供了Auto-batching和SpriteBatchNode。

Auto-batching 意思是Renderer将多次draw的调用打包成一次big Draw 调用。(又名批处理)。

SpriteBatchNode 主要用于批量绘制精灵提高精灵的绘制效率的,需要绘制的精灵数量越多,效果越明显。

Auto-batching

在3.0版本实现了引擎的逻辑代码与渲染代码的分离,实现了Auto Batch与Auto Culling功能。不再推荐使用SpriteBatchNode提高精灵的绘制效率。

Auto-culling的支持,Sprite在绘制时会进行检查,超出屏幕的不会发给渲染, 可见渲染的效率还是有改进的,

            auto cmd = static_cast<QuadCommand*>(command);            //Batch quads            if(_numQuads + cmd->getQuadCount() > VBO_SIZE)            {                                //Draw batched quads if VBO is full                drawBatchedQuads(); // 真正的调用显卡渲染程序glDrawElements            }



针对大资源的渲染解决方案参考

http://www.jb51.net/article/49439.htm

1 0
原创粉丝点击