From 005282b6f5a67dabde0a4d7664dcdb34a96b31b2 Mon Sep 17 00:00:00 2001 From: jerrywwl Date: Wed, 30 Jan 2019 11:41:58 +0800 Subject: [PATCH] fix ui batch errors --- cocos/gfx/webgl/webgl-command-buffer.ts | 18 ++++------- cocos/gfx/webgl/webgl-device.ts | 4 +++ cocos/gfx/webgl/webgl-state-cache.ts | 14 -------- cocos/renderer/ui/ui-material.ts | 14 -------- cocos/renderer/ui/ui.ts | 43 +++++++++++++++++++------ 5 files changed, 43 insertions(+), 50 deletions(-) diff --git a/cocos/gfx/webgl/webgl-command-buffer.ts b/cocos/gfx/webgl/webgl-command-buffer.ts index 4843dd2bca..7f21d6eba2 100644 --- a/cocos/gfx/webgl/webgl-command-buffer.ts +++ b/cocos/gfx/webgl/webgl-command-buffer.ts @@ -151,26 +151,20 @@ export class WebGLGFXCommandBuffer extends GFXCommandBuffer { public bindPipelineState (pipelineState: GFXPipelineState) { const gpuPipelineState = (pipelineState as WebGLGFXPipelineState).gpuPipelineState; - if (this._curGPUPipelineState !== gpuPipelineState) { - this._curGPUPipelineState = gpuPipelineState; - this._isStateInvalied = true; - } + this._curGPUPipelineState = gpuPipelineState; + this._isStateInvalied = true; } public bindBindingLayout (bindingLayout: GFXBindingLayout) { const gpuBindingLayout = (bindingLayout as WebGLGFXBindingLayout).gpuBindingLayout; - if (this._curGPUBindingLayout !== gpuBindingLayout) { - this._curGPUBindingLayout = gpuBindingLayout; - this._isStateInvalied = true; - } + this._curGPUBindingLayout = gpuBindingLayout; + this._isStateInvalied = true; } public bindInputAssembler (inputAssembler: GFXInputAssembler) { const gpuInputAssembler = (inputAssembler as WebGLGFXInputAssembler).gpuInputAssembler; - if (this._curGPUInputAssembler !== gpuInputAssembler) { - this._curGPUInputAssembler = gpuInputAssembler; - this._isStateInvalied = true; - } + this._curGPUInputAssembler = gpuInputAssembler; + this._isStateInvalied = true; } public setViewport (viewport: IGFXViewport) { diff --git a/cocos/gfx/webgl/webgl-device.ts b/cocos/gfx/webgl/webgl-device.ts index 262cbde641..de129582fb 100644 --- a/cocos/gfx/webgl/webgl-device.ts +++ b/cocos/gfx/webgl/webgl-device.ts @@ -697,6 +697,10 @@ export class WebGLGFXDevice extends GFXDevice { glType: 0, glUsage: 0, glTexture: 0, + glWrapS: 0, + glWrapT: 0, + glMinFilter: 0, + glMagFilter: 0, }; // TODO: Async diff --git a/cocos/gfx/webgl/webgl-state-cache.ts b/cocos/gfx/webgl/webgl-state-cache.ts index c7c9f8f469..c9d7b931e4 100644 --- a/cocos/gfx/webgl/webgl-state-cache.ts +++ b/cocos/gfx/webgl/webgl-state-cache.ts @@ -35,23 +35,9 @@ export class WebGLStateCache { for (let i = 0; i < GFX_MAX_TEXTURE_UNITS; ++i) { this.glTex2DUnits[i] = { glTexture: 0, - minFilter: WebGLRenderingContext.NONE, - magFilter: WebGLRenderingContext.NONE, - wrapS: WebGLRenderingContext.NONE, - wrapT: WebGLRenderingContext.NONE, - wrapR: WebGLRenderingContext.NONE, - baseLevel: 0, - maxLevel: 1000, }; this.glTexCubeUnits[i] = { glTexture: 0, - minFilter: WebGLRenderingContext.NONE, - magFilter: WebGLRenderingContext.NONE, - wrapS: WebGLRenderingContext.NONE, - wrapT: WebGLRenderingContext.NONE, - wrapR: WebGLRenderingContext.NONE, - baseLevel: 0, - maxLevel: 1000, }; } diff --git a/cocos/renderer/ui/ui-material.ts b/cocos/renderer/ui/ui-material.ts index 16ce74833c..60cfbd776e 100644 --- a/cocos/renderer/ui/ui-material.ts +++ b/cocos/renderer/ui/ui-material.ts @@ -17,18 +17,8 @@ export class UIMaterial { return this._pass!; } - public get pipelineState (): GFXPipelineState { - return this._pipelineState!; - } - - public get bindingLayout (): GFXBindingLayout { - return this._bindingLayout!; - } - protected _material: Material | null = null; protected _pass: Pass | null = null; - protected _pipelineState: GFXPipelineState | null = null; - protected _bindingLayout: GFXBindingLayout | null = null; constructor () { } @@ -42,15 +32,11 @@ export class UIMaterial { this._material = info.material; this._pass = this._material.passes[0]; - this._pipelineState = this._pass.createPipelineState(); - this._bindingLayout = this._pipelineState!.pipelineLayout.layouts[0]; return true; } public destroy () { - this._bindingLayout = null; - this._pipelineState = null; this._material = null; } } diff --git a/cocos/renderer/ui/ui.ts b/cocos/renderer/ui/ui.ts index 612343f167..411c85f0f3 100644 --- a/cocos/renderer/ui/ui.ts +++ b/cocos/renderer/ui/ui.ts @@ -31,6 +31,7 @@ import { RenderScene } from '../scene/render-scene'; import { IUIMaterialInfo, UIMaterial } from './ui-material'; import { Node } from '../../scene-graph/node'; import { Rect } from '../../core/value-types'; +import { GFXPipelineState } from '../../gfx/pipeline-state'; export class UBOUI { public static MAT_VIEW_PROJ_OFFSET: number = 0; @@ -63,14 +64,28 @@ export class UIBufferBatch { export class UIDrawBatch { public camera: Camera | null = null; public bufferBatch: UIBufferBatch | null = null; - public uiMaterial: UIMaterial | null = null; + public material: Material | null = null; public texView: GFXTextureView | null = null; public idxCount: number = 0; + public pipelineState: GFXPipelineState | null = null; + public bindingLayout: GFXBindingLayout | null = null; + + destroy() { + if (this.pipelineState) { + this.pipelineState.destroy(); + this.pipelineState = null; + } + + if (this.bindingLayout) { + this.bindingLayout.destroy(); + this.bindingLayout = null; + } + } clear() { this.camera = null; this.bufferBatch = null; - this.uiMaterial = null; + this.material = null; this.texView = null; this.idxCount = 0; } @@ -196,6 +211,10 @@ export class UI { this._uiUBO = null; } + for (const batch of this._batches.array) { + batch.destroy(); + } + for (const buffBatch of this._bufferBatches) { buffBatch.vb!.destroy(); buffBatch.ib!.destroy(); @@ -351,15 +370,16 @@ export class UI { cmdBuff.beginRenderPass(framebuffer, this._renderArea, [], camera.clearDepth, camera.clearStencil); } - material.bindingLayout.bindBuffer(0, this._uiUBO!); - material.bindingLayout.bindTextureView(1, batch.texView!); - material.bindingLayout.update(); + const bindingLayout = batch.bindingLayout!; + bindingLayout.bindBuffer(0, this._uiUBO!); + bindingLayout.bindTextureView(1, batch.texView!); + bindingLayout.update(); let ia = batch.bufferBatch!.ia! ia.indexCount = batch.idxCount; - cmdBuff.bindPipelineState(material.pipelineState); - cmdBuff.bindBindingLayout(material.bindingLayout); + cmdBuff.bindPipelineState(batch.pipelineState!); + cmdBuff.bindBindingLayout(bindingLayout); cmdBuff.bindInputAssembler(batch.bufferBatch!.ia!); cmdBuff.draw(batch.bufferBatch!.ia!); } @@ -541,8 +561,6 @@ export class UI { curBufferBatch.vui16![idxCount + n] = vui16[n] + vertCount; } - //curBufferBatch.vui16!.set(vui16, idxCount); - vertCount += vCount; idxCount += vui16.length; @@ -555,10 +573,15 @@ export class UI { curDrawBatch = this._drawBatchPool.add(); curDrawBatch.camera = curCamera; curDrawBatch.bufferBatch = curBufferBatch; - curDrawBatch.uiMaterial = this._uiMaterial!; + curDrawBatch.material = uiRenderData.material; curDrawBatch.texView = curTexView!; curDrawBatch.idxCount = idxCount; + if (!curDrawBatch.pipelineState) { + curDrawBatch.pipelineState = this._uiMaterial!.pass.createPipelineState(); + curDrawBatch.bindingLayout = curDrawBatch.pipelineState!.pipelineLayout.layouts[0]; + } + this._batches.push(curDrawBatch); isNewBatch = false; } else {