cocos2d-x渲染流程分析

上层的渲染函数为  Director::drawScene()  。 Renderer::render()  为真正的渲染,但是在 Renderer::render()  之前,有 _runningScene->visit,其中 _runningScene继承与Node,Node是cocos2d-x的基本渲染单位。node::visit的作用在于把要渲染的东西塞入到渲染队列中,堆栈如下:
cpp-tests.exe!cocos2d::Renderer::addCommand(cocos2d::RenderCommand * command=0x05d80150, int renderQueue=0) Line 255 C++
  cpp-tests.exe!cocos2d::Renderer::addCommand(cocos2d::RenderCommand * command=0x05d80150) Line 248 C++
  cpp-tests.exe!cocos2d::Sprite::draw(cocos2d::Renderer * renderer=0x00370040, const cocos2d::Mat4 & transform={...}, unsigned int flags=0) Line 603 C++
  cpp-tests.exe!cocos2d::Node::visit(cocos2d::Renderer * renderer=0x00370040, const cocos2d::Mat4 & parentTransform={...}, unsigned int parentFlags=0) Line 1266 C++
  cpp-tests.exe!cocos2d::Node::visit(cocos2d::Renderer * renderer=0x00370040, const cocos2d::Mat4 & parentTransform={...}, unsigned int parentFlags=0) Line 1262 C++
  cpp-tests.exe!cocos2d::Node::visit(cocos2d::Renderer * renderer=0x00370040, const cocos2d::Mat4 & parentTransform={...}, unsigned int parentFlags=0) Line 1262 C++
  cpp-tests.exe!cocos2d::Node::visit(cocos2d::Renderer * renderer=0x00370040, const cocos2d::Mat4 & parentTransform={...}, unsigned int parentFlags=0) Line 1262 C++
  cpp-tests.exe!cocos2d::Node::visit(cocos2d::Renderer * renderer=0x00370040, const cocos2d::Mat4 & parentTransform={...}, unsigned int parentFlags=0) Line 1262 C++
  cpp-tests.exe!cocos2d::Director::drawScene() Line 290 C++
 其中关键函数:
void Renderer::addCommand(RenderCommand* command, int renderQueue)
{
    CCASSERT(!_isRendering, "Cannot add command while rendering");
    CCASSERT(renderQueue >=0, "Invalid render queue");
    CCASSERT(command->getType() != RenderCommand::Type::UNKNOWN_COMMAND, "Invalid Command Type");
    _renderGroups[renderQueue].push_back(command);
}  
_renderGroups就是渲染队列。RenderCommand记录了渲染操作相关信息,到真正渲染的时候会用上。
    下一步就是真正的渲染操作,堆栈如下:
        cpp-tests.exe!DrawPrimitivesTest::onDraw(const cocos2d::Mat4 & transform={...}, unsigned int flags=0) Line 126 C++
        ...
        ... 
  cpp-tests.exe!cocos2d::CustomCommand::execute() Line 51 C++
     cpp-tests.exe!cocos2d::Renderer::visitRenderQueue(const cocos2d::RenderQueue & queue={...}) Line 314 C++
  cpp-tests.exe!cocos2d::Renderer::render() Line 367 C++
  cpp-tests.exe!cocos2d::Director::drawScene() Line 306 C++
看关键代码:
        
 for (ssize_t index = 0; index < size; ++index)
        {
            auto command = queue[index];
            auto commandType = command->getType();
            if(RenderCommand::Type::QUAD_COMMAND == commandType)  
            {
                ...
                _batchedQuadCommands.push_back(cmd);//注意这里,连续的
QUAD渲染是会合并的(在同材质的情况下),知道下个渲染command不再是QUAD_COMMAND类型,才会被提交渲染
                ...
            } 
            
else if(RenderCommand::Type::GROUP_COMMAND == commandType)
            {
                ...
            } 
            else if(RenderCommand::Type::CUSTOM_COMMAND == commandType)  //自定义渲染操作
            {
                flush();
                auto cmd = static_cast<CustomCommand*>(command);
                cmd->execute();//执行渲染,最终调到DrawPrimitivesTest::onDraw这个函数中
            } 
最后
Director::drawScene() 调用
    // swap buffers
    if (_openGLView)
    {
        _openGLView->swapBuffers();
    }  
至此一帧的渲染流程结束。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值