cocos2d-x渲染流程分析

来源:互联网 发布:大数据最重要的职位 编辑:程序博客网 时间:2024/05/19 07:09
上层的渲染函数为 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 255C++
 cpp-tests.exe!cocos2d::Renderer::addCommand(cocos2d::RenderCommand * command=0x05d80150) Line 248C++
 cpp-tests.exe!cocos2d::Sprite::draw(cocos2d::Renderer * renderer=0x00370040, const cocos2d::Mat4 & transform={...}, unsigned int flags=0) Line 603C++
 cpp-tests.exe!cocos2d::Node::visit(cocos2d::Renderer * renderer=0x00370040, const cocos2d::Mat4 & parentTransform={...}, unsigned int parentFlags=0) Line 1266C++
 cpp-tests.exe!cocos2d::Node::visit(cocos2d::Renderer * renderer=0x00370040, const cocos2d::Mat4 & parentTransform={...}, unsigned int parentFlags=0) Line 1262C++
 cpp-tests.exe!cocos2d::Node::visit(cocos2d::Renderer * renderer=0x00370040, const cocos2d::Mat4 & parentTransform={...}, unsigned int parentFlags=0) Line 1262C++
 cpp-tests.exe!cocos2d::Node::visit(cocos2d::Renderer * renderer=0x00370040, const cocos2d::Mat4 & parentTransform={...}, unsigned int parentFlags=0) Line 1262C++
 cpp-tests.exe!cocos2d::Node::visit(cocos2d::Renderer * renderer=0x00370040, const cocos2d::Mat4 & parentTransform={...}, unsigned int parentFlags=0) Line 1262C++
 cpp-tests.exe!cocos2d::Director::drawScene() Line 290C++
 其中关键函数:
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 126C++
        ...
        ... 
 cpp-tests.exe!cocos2d::CustomCommand::execute() Line 51C++
     cpp-tests.exe!cocos2d::Renderer::visitRenderQueue(const cocos2d::RenderQueue & queue={...}) Line 314C++
 cpp-tests.exe!cocos2d::Renderer::render() Line 367C++
 cpp-tests.exe!cocos2d::Director::drawScene() Line 306C++
看关键代码:
        
 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