From 6d17dbe55a5fb8f931cd7396dd19a1ed2fbf5843 Mon Sep 17 00:00:00 2001 From: jerrywwl Date: Tue, 15 Jan 2019 17:07:19 +0800 Subject: [PATCH] fix indirect buffer bug --- cocos/gfx/webgl/webgl-commands.ts | 8 +++----- cocos/gfx/webgl/webgl-device.ts | 8 +++++++- cocos/gfx/webgl/webgl-gpu-objects.ts | 2 +- cocos/gfx/webgl/webgl-input-assembler.ts | 23 +++++++++++------------ 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/cocos/gfx/webgl/webgl-commands.ts b/cocos/gfx/webgl/webgl-commands.ts index a989e288a0..74abceeab3 100644 --- a/cocos/gfx/webgl/webgl-commands.ts +++ b/cocos/gfx/webgl/webgl-commands.ts @@ -394,8 +394,6 @@ export class WebGLCmdDraw extends WebGLCmdObject { firstInstance: 0, }; - public gpuIndirectBuffer: WebGLGPUBuffer | null = null; - constructor () { super(WebGLCmd.DRAW); } @@ -1886,7 +1884,7 @@ export function WebGLCmdFuncExecuteCmds (device: WebGLGFXDevice, cmdPackage: Web case WebGLCmd.DRAW: { const cmd3: WebGLCmdDraw = cmdPackage.drawCmds.array[cmdId]; if (gpuInputAssembler && gpuShader) { - if (!cmd3.gpuIndirectBuffer) { + if (!gpuInputAssembler.gpuIndirectBuffer) { const drawInfo = cmd3.drawInfo as IGFXDrawInfo; const gpuBuffer = gpuInputAssembler.gpuIndexBuffer; @@ -1897,8 +1895,8 @@ export function WebGLCmdFuncExecuteCmds (device: WebGLGFXDevice, cmdPackage: Web gl.drawArrays(glPrimitive, drawInfo.firstVertex, drawInfo.vertexCount); } } else { - if (cmd3.gpuIndirectBuffer) { - const drawInfos = (cmd3.gpuIndirectBuffer.buffer as IGFXIndirectBuffer).drawInfos; + if (gpuInputAssembler.gpuIndirectBuffer) { + const drawInfos = (gpuInputAssembler.gpuIndirectBuffer.buffer as IGFXIndirectBuffer).drawInfos; for (const drawInfo of drawInfos) { const gpuBuffer = gpuInputAssembler.gpuIndexBuffer; if (gpuBuffer && drawInfo.indexCount > 0) { diff --git a/cocos/gfx/webgl/webgl-device.ts b/cocos/gfx/webgl/webgl-device.ts index 8fd2b7167f..ffffe7dc4e 100644 --- a/cocos/gfx/webgl/webgl-device.ts +++ b/cocos/gfx/webgl/webgl-device.ts @@ -973,12 +973,18 @@ export class WebGLGFXDevice extends GFXDevice { } } + let gpuIndirectBuffer: WebGLGPUBuffer | null = null; + + if (info.indirectBuffer !== undefined) { + gpuIndirectBuffer = (info.indirectBuffer as WebGLGFXBuffer).gpuBuffer; + } + const gpuInputAssembler: WebGLGPUInputAssembler = { objType: WebGLGPUObjectType.INPUT_ASSEMBLER, attributes: info.attributes, gpuVertexBuffers, gpuIndexBuffer, - gpuIndirectBuffers: [], + gpuIndirectBuffer, glAttribs: [], glIndexType, diff --git a/cocos/gfx/webgl/webgl-gpu-objects.ts b/cocos/gfx/webgl/webgl-gpu-objects.ts index f0257b0f9b..6691d777e0 100644 --- a/cocos/gfx/webgl/webgl-gpu-objects.ts +++ b/cocos/gfx/webgl/webgl-gpu-objects.ts @@ -270,7 +270,7 @@ export class WebGLGPUInputAssembler extends WebGLGPUObject { public attributes: IGFXInputAttribute[] = []; public gpuVertexBuffers: WebGLGPUBuffer[] = []; public gpuIndexBuffer: WebGLGPUBuffer | null = null; - public gpuIndirectBuffers: IWebGLIndirectBuffer[] = []; + public gpuIndirectBuffer: WebGLGPUBuffer | null = null; public glAttribs: WebGLAttrib[] = []; public glIndexType: GLenum = 0; diff --git a/cocos/gfx/webgl/webgl-input-assembler.ts b/cocos/gfx/webgl/webgl-input-assembler.ts index 54f1d9facc..f515cea02d 100644 --- a/cocos/gfx/webgl/webgl-input-assembler.ts +++ b/cocos/gfx/webgl/webgl-input-assembler.ts @@ -40,6 +40,10 @@ export class WebGLGFXInputAssembler extends GFXInputAssembler { this._vertexCount = vertBuff.size / vertBuff.stride; } + if (info.indirectBuffer !== undefined) { + this._indirectBuffer = info.indirectBuffer; + } + this._gpuInputAssembler = this.webGLDevice.emitCmdCreateGPUInputAssembler(info); this._status = GFXStatus.SUCCESS; @@ -55,17 +59,12 @@ export class WebGLGFXInputAssembler extends GFXInputAssembler { } public extractCmdDraw (cmd: WebGLCmdDraw) { - if (!this._indirectBuffer) { - cmd.drawInfo.vertexCount = this._vertexCount; - cmd.drawInfo.firstVertex = this._firstVertex; - cmd.drawInfo.indexCount = this._indexCount; - cmd.drawInfo.firstIndex = this._firstIndex; - cmd.drawInfo.vertexOffset = this._vertexOffset; - cmd.drawInfo.instanceCount = this._instanceCount; - cmd.drawInfo.firstInstance = this._firstInstance; - cmd.gpuIndirectBuffer = null; - } else { - cmd.gpuIndirectBuffer = (this._indirectBuffer as WebGLGFXBuffer).gpuBuffer; - } + cmd.drawInfo.vertexCount = this._vertexCount; + cmd.drawInfo.firstVertex = this._firstVertex; + cmd.drawInfo.indexCount = this._indexCount; + cmd.drawInfo.firstIndex = this._firstIndex; + cmd.drawInfo.vertexOffset = this._vertexOffset; + cmd.drawInfo.instanceCount = this._instanceCount; + cmd.drawInfo.firstInstance = this._firstInstance; } }