Batcher2D合批的疑惑

在渲染的ui-phase阶段,如下

public render (camera: Camera, renderPass: RenderPass) {
    const pipeline = this._pipeline;
    const device = pipeline.device;
    const cmdBuff = pipeline.commandBuffers[0];
    const scene = camera.scene!;
    const batches = scene.batches;
    for (let i = 0; i < batches.length; i++) {
        const batch = batches[i];
        。。。
        const count = batch.shaders.length;
        for (let j = 0; j < count; j++) {
            const pass = batch.passes[j];
            if (pass.phase !== this._phaseID) continue;
            const shader = batch.shaders[j];
            const inputAssembler = batch.inputAssembler!;
            const pso = PipelineStateManager.getOrCreatePipelineState(device, pass, shader, renderPass, inputAssembler);
            。。。
            cmdBuff.draw(inputAssembler);
        }
    }
}

这里面的cmdBuff.draw就是一个drawcall;而scene.batches是在batcher2d的autoMergeBatches里面添加的,关键代码如下

const curDrawBatch = this._currStaticRoot ? this._currStaticRoot._requireDrawBatch() :this._drawBatchPool.alloc();
curDrawBatch.visFlags = this._currLayer;
curDrawBatch.texture = this._currTexture!;
curDrawBatch.sampler = this._currSampler;
curDrawBatch.inputAssembler = ia;
curDrawBatch.useLocalData = this._currTransform;
curDrawBatch.textureHash = this._currTextureHash;
curDrawBatch.samplerHash = this._currSamplerHash;
curDrawBatch.fillPasses(mat, depthStencil, dssHash, null);
this._batches.push(curDrawBatch);

串联起来看并没有减少drawcall不是吗?比如以两个连续节点都是sprite为例,按上面代码依旧产生2个DrawBatch,dc不还是2吗? 不知道哪个地方理解有误,感谢大佬指点!

你是不是以为一个sprite就会有一个batch? batch是"批",如果两个连续的sprite符合合批条件的话会"合批"的 :smiley_cat:

您可以翻一下batcher2d的代码,2个sprite,会调用2此commitComp,最终2次autoMergeBatches,push了DrawBatch2d对象

那可能,你合批条件没符合,就造成打断,所以提交了两次

不是,我是在理解代码,实际是会合批,只是看代码理解不一样,按上面的看下来确实不清楚哪里就减少了dc,感谢感谢

那肯定是满足了这四个不合批的条件中的一个,可以查一查,比如像素图片默认不支持动态合图,它们的dataDash就会不等于this._currDash

要调用这个代码是要满足上面if语句里的不合批的四个条件的

哦哦, 好像有点理解了。 感谢感谢。 (这帖子要删掉吗,好像自己有点白痴了~ :sweat_smile:

不用了,让更多的人知道这个if语句里的四个条件,是不合批的条件也挺好的

问你一个曾经被面试的问题,两个sprite在一个图集。这个图集会被提交给gpu几次?

成功合批就一次,没成功2次,

是这样吗。。。

虽然我还没拆过cocos的渲染管线,不过一个图集之前提交过,就算是打断了合批,之前的图集已经在那了,相信不会再提交一次
所以我猜只要一次 :sunglasses:

纹理提交和 dc 没直接关系!!!

纹理有两个操作,数据提交(显存),使用绑定(地址)

静态图集,图集 1 次提交(静态数据),随意绑定使用

动态合图,上传 2 次提交(局部更新),随意绑定使用

1赞

合图为何会上传2次

要向 sub texture 提交数据 ,
对纹理显存进行,局部数据更新 ,
要调用GPU硬件流程上传的,可以理解提交(局部)

说个引出问题,
如果大量动态合图时候, 是非常卡的,
这个动态合图提交过程,是非常消耗时间

不愧是大佬 :+1:

受教了。这个能在当前引擎源码哪里看呢?

没注意,一直觉得动态合图可控