美文网首页
cocos2dx3.7.1版本的auto_batching 20

cocos2dx3.7.1版本的auto_batching 20

作者: onlyfans | 来源:发表于2020-10-23 12:12 被阅读0次

    cocos2dx的2.x版本和cocos2dx的3.x版本在渲染流程上有了很大的不同,通过某度大家可以查到。

    其中还有一个优化就是3.x使用了auto_batching技术来替代了2.x时代中的spriteBatchNode,通过了解我们能知道,想要通过auto_batching来降低drawcall的话,有三个条件:

    1.需确保精灵对象拥有相同的TextureId(精灵表单spritesheet);

    2.确保它们都使用相同的混合函数

    3.没有使用shader去进行修改

    但为什么会有这三个条件呐?或者说都满足这几个条件后,那是因为什么导致这些精灵可以auto_batching呐?

    首先,咱们先来看下3.x的渲染流程:(以下都是coocs2dx 3.7.1版本的源码,没用的我就删掉了)

    1.drawScene开始绘制场景

    if (_runningScene)

        {

            //clear draw stats

            _renderer->clearDrawStats();

            //render the scene

            _runningScene->render(_renderer);

            _eventDispatcher->dispatchEvent(_eventAfterVisit);

        }

    2.遍历场景的子节点

    第一步中 的_runningScene->render(_renderer) render函数中会有一步visit(renderer, transform, 0)操作,去遍历所有子节点

    3.调用每一个子节点的draw函数

    voidSprite::draw(Renderer*renderer,constkmMat4&transform,booltransformUpdated)

    {

            // Don't do calculate the culling if the transform was not updated

            _insideBounds=transformUpdated?isInsideBounds():_insideBounds;

            //在这个渲染命令上会有差别,我用的3.7.1版本的渲染命令已经换成了下面的_trianglesCommand,之前的是_quadCommand

    具体是哪个版本换的,我没有去跟踪。

            if(_insideBounds)

            {

                    _quadCommand.init(_globalZOrder, _texture->getName(), _shaderProgram, _blendFunc,&_quad,1, transform);

                    renderer->addCommand(&_quadCommand);

            }

            if(_insideBounds)

            {

                    _trianglesCommand.init(_globalZOrder, _texture->getName(), getGLProgramState(), _blendFunc, _polyInfo.triangles, transform, flags);

                    renderer->addCommand(&_trianglesCommand);

            }

    }

    4.初始化TrianglesCommand对象,这就是渲染命令

    上面的代码就是重点了,初始化_trianglesCommand对象,这就是TrianglesCommand,渲染命令。

    其实渲染命令不仅仅只有TrianglesCommand,还有其他的,比如CustomCommand,自定义渲染命令,顾名思义,就是我们用户自己定制的命令,由于我没有使用过,就不介绍了。

    然后,接着就调用addCommand函数将渲染命令加入队列。

    这里有一点,也很重要,由于渲染命令有好几种,所以addCommand的时候,其实是会根据不同的命令类型把渲染命令添加到不同的队列。本文只想针对TrianglesCommand,所以就忽略这一点,假设我们的所有命令都是TrianglesCommand。

    5.draw函数执行完,就轮到渲染逻辑干事了

    6.开始渲染

    Render的processRenderCommand会根据不同的Command来进行筛选,并做对应的处理

    //以下是对TRIANGLES_COMMAND 命令的处理

    if( RenderCommand::Type::TRIANGLES_COMMAND == commandType)

        {

            // flush other queues

            flush3D();

            auto cmd = static_cast<TrianglesCommand*>(command);

            // flush own queue when buffer is full

            if(_filledVertex + cmd->getVertexCount() > VBO_SIZE || _filledIndex + cmd->getIndexCount() > INDEX_VBO_SIZE)

            {

                CCASSERT(cmd->getVertexCount()>= 0 && cmd->getVertexCount() < VBO_SIZE, "VBO for vertex is not big enough, please break the data down or use customized render command");

                CCASSERT(cmd->getIndexCount()>= 0 && cmd->getIndexCount() < INDEX_VBO_SIZE, "VBO for index is not big enough, please break the data down or use customized render command");

                drawBatchedTriangles();

            }

            // queue it

            _queuedTriangleCommands.push_back(cmd);

            _filledIndex += cmd->getIndexCount();

            _filledVertex += cmd->getVertexCount();

        }

    在这个函数drawBatchedTriangles()中,你会发现:(想自己了解细节的话,可以到CCRenderer.cpp中查看)

    for(const auto& cmd : _queuedTriangleCommands)

    {

            auto currentMaterialID = cmd->getMaterialID();

            const bool batchable = !cmd->isSkipBatching();

            fillVerticesAndIndices(cmd);

            // in the same batch ?

            if (batchable && (prevMaterialID == currentMaterialID || firstCommand))

            {

                _triBatchesToDraw[batchesTotal].indicesToDraw += cmd->getIndexCount();

                _triBatchesToDraw[batchesTotal].cmd = cmd;

            }else{

            }

    }

    //上面的粗体部分就是判断是否是同一纹理。

    你肯定会惊讶,what???这就行了?为什么要用auto_batching必须要相同纹理、相同混合函数、相同shader呐?

    关键是这个:(sprite的draw函数)

    _trianglesCommand.init(_globalZOrder, _texture->getName(), getGLProgramState(), _blendFunc, _polyInfo.triangles, transform, flags);

    跟这 TrianglesCommand::init()进去看下,你会发现:

    if( _textureID != textureID || _blendType.src != blendType.src || _blendType.dst != blendType.dst || _glProgramState != glProgramState) {

            _textureID = textureID;

            _blendType = blendType;

            _glProgramState = glProgramState;

            generateMaterialID();

        }

    在这个加粗的函数中,你会发现,它做了什么呐?

    // glProgramState is hashed because it contains:

    //  *  uniforms/values

    //  *  glProgram

    //

    // we safely can when the same glProgramState is being used then they share those states

    // if they don't have the same glProgramState, they might still have the same

    // uniforms/values and glProgram, but it would be too expensive to check the uniforms.

    struct {

    void* glProgramState;

    GLuint textureId;

    GLenum blendSrc;

    GLenum blendDst;

    } hashMe;

    // NOTE: Initialize hashMe struct to make the value of padding bytes be filled with zero.

    // It's important since XXH32 below will also consider the padding bytes which probably

    // are set to random values by different compilers.

    memset(&hashMe, 0, sizeof(hashMe));

    hashMe.textureId = _textureID;

    hashMe.blendSrc = _blendType.src;

    hashMe.blendDst = _blendType.dst;

    hashMe.glProgramState = _glProgramState;

    _materialID = XXH32((const void*)&hashMe, sizeof(hashMe), 0);

    他会给自身的command命令绑定一个_materialID 。这就是auto_batching的原因。

    相关文章

      网友评论

          本文标题:cocos2dx3.7.1版本的auto_batching 20

          本文链接:https://www.haomeiwen.com/subject/iexvmktx.html