From be21e64f15d8ae2aa735c9e0da022bb206801409 Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Fri, 28 Aug 2020 11:47:28 +0800 Subject: [PATCH] fix instanced buffers hashing (#7240) --- cocos/core/gfx/webgl/webgl-commands.ts | 8 +++++++- cocos/core/gfx/webgl2/webgl2-commands.ts | 10 ++++++++-- cocos/core/pipeline/batched-buffer.ts | 16 +++++----------- cocos/core/pipeline/instanced-buffer.ts | 14 +++++--------- 4 files changed, 25 insertions(+), 23 deletions(-) diff --git a/cocos/core/gfx/webgl/webgl-commands.ts b/cocos/core/gfx/webgl/webgl-commands.ts index 9563b2f442..7c751ee3ff 100644 --- a/cocos/core/gfx/webgl/webgl-commands.ts +++ b/cocos/core/gfx/webgl/webgl-commands.ts @@ -1604,7 +1604,13 @@ export function WebGLCmdFuncBeginRenderPass ( gl.bindFramebuffer(gl.FRAMEBUFFER, gpuFramebuffer.glFramebuffer); cache.glFramebuffer = gpuFramebuffer.glFramebuffer; // render targets are drawn with flipped-Y - gfxStateCache.reverseCW = !!gpuFramebuffer.glFramebuffer; + const reverseCW = !!gpuFramebuffer.glFramebuffer; + if (reverseCW !== gfxStateCache.reverseCW) { + gfxStateCache.reverseCW = reverseCW; + const isCCW = !device.stateCache.rs.isFrontFaceCCW; + gl.frontFace(isCCW ? gl.CCW : gl.CW); + device.stateCache.rs.isFrontFaceCCW = isCCW; + } } if (cache.viewport.left !== renderArea.x || diff --git a/cocos/core/gfx/webgl2/webgl2-commands.ts b/cocos/core/gfx/webgl2/webgl2-commands.ts index 843eb21ac4..6dffd3ce08 100644 --- a/cocos/core/gfx/webgl2/webgl2-commands.ts +++ b/cocos/core/gfx/webgl2/webgl2-commands.ts @@ -1830,7 +1830,13 @@ export function WebGL2CmdFuncBeginRenderPass ( gl.bindFramebuffer(gl.FRAMEBUFFER, gpuFramebuffer.glFramebuffer); cache.glFramebuffer = gpuFramebuffer.glFramebuffer; // render targets are drawn with flipped-Y - gfxStateCache.reverseCW = !!gpuFramebuffer.glFramebuffer; + const reverseCW = !!gpuFramebuffer.glFramebuffer; + if (reverseCW !== gfxStateCache.reverseCW) { + gfxStateCache.reverseCW = reverseCW; + const isCCW = !device.stateCache.rs.isFrontFaceCCW; + gl.frontFace(isCCW ? gl.CCW : gl.CW); + device.stateCache.rs.isFrontFaceCCW = isCCW; + } } if (cache.viewport.left !== renderArea.x || @@ -2045,7 +2051,7 @@ export function WebGL2CmdFuncBindStates ( device.stateCache.rs.cullMode = rs.cullMode; } - const isFrontFaceCCW = gfxStateCache.reverseCW ? !rs.isFrontFaceCCW : rs.isFrontFaceCCW; + const isFrontFaceCCW = rs.isFrontFaceCCW !== gfxStateCache.reverseCW; // boolean XOR if (device.stateCache.rs.isFrontFaceCCW !== isFrontFaceCCW) { gl.frontFace(isFrontFaceCCW ? gl.CCW : gl.CW); device.stateCache.rs.isFrontFaceCCW = isFrontFaceCCW; diff --git a/cocos/core/pipeline/batched-buffer.ts b/cocos/core/pipeline/batched-buffer.ts index d1ff4ec60a..368ed35f64 100644 --- a/cocos/core/pipeline/batched-buffer.ts +++ b/cocos/core/pipeline/batched-buffer.ts @@ -26,21 +26,15 @@ export interface IBatchedItem { hShader: ShaderHandle; } -const _localBatched = new UBOLocalBatched(); - export class BatchedBuffer { - private static _buffers = new Map(); + private static _buffers = new Map>(); - public static get (pass: Pass, extraKey?: number) { - const hash = extraKey ? pass.hash ^ extraKey : pass.hash; + public static get (pass: Pass, extraKey = 0) { const buffers = BatchedBuffer._buffers; - if (!buffers.has(hash)) { - const buffer = new BatchedBuffer(pass); - buffers.set(hash, buffer); - return buffer; - } - return buffers.get(hash)!; + if (!buffers.has(pass)) buffers.set(pass, {}); + const record = buffers.get(pass)!; + return record[extraKey] || (record[extraKey] = new BatchedBuffer(pass)); } public batches: IBatchedItem[] = []; diff --git a/cocos/core/pipeline/instanced-buffer.ts b/cocos/core/pipeline/instanced-buffer.ts index 8a32e19d0a..7adfb315ad 100644 --- a/cocos/core/pipeline/instanced-buffer.ts +++ b/cocos/core/pipeline/instanced-buffer.ts @@ -27,17 +27,13 @@ const MAX_CAPACITY = 1024; export class InstancedBuffer { - private static _buffers = new Map(); + private static _buffers = new Map>(); - public static get (pass: Pass, extraKey?: number) { - const hash = extraKey ? pass.hash ^ extraKey : pass.hash; + public static get (pass: Pass, extraKey = 0) { const buffers = InstancedBuffer._buffers; - if (!buffers.has(hash)) { - const buffer = new InstancedBuffer(pass); - buffers.set(hash, buffer); - return buffer; - } - return buffers.get(hash)!; + if (!buffers.has(pass)) buffers.set(pass, {}); + const record = buffers.get(pass)!; + return record[extraKey] || (record[extraKey] = new InstancedBuffer(pass)); } public instances: IInstancedItem[] = [];