diff --git a/cocos/pipeline/render-queue.ts b/cocos/pipeline/render-queue.ts index d51a85a92c..32049be23a 100644 --- a/cocos/pipeline/render-queue.ts +++ b/cocos/pipeline/render-queue.ts @@ -5,13 +5,14 @@ import { Pass } from '../renderer/core/pass'; import { Camera } from '../renderer/scene/camera'; import { Model } from '../renderer/scene/model'; import { SubModel } from '../renderer/scene/submodel'; +import { GFXPipelineState } from '../gfx/pipeline-state'; export interface IRenderItem { hash: number; depth: number; shaderId: number; subModel: SubModel; - pass: Pass; + pso: GFXPipelineState; cmdBuff: GFXCommandBuffer; } @@ -69,11 +70,7 @@ export class RenderQueue { for (let i = 0; i < model.subModelNum; ++i) { const subModel = model.getSubModel(i); - for (const pass of subModel.passes) { - // update pass - pass.update(); - - const pso = pass.pipelineState; + for (const pso of subModel.psos) { const isTransparent = pso.blendState.targets[0].blend; @@ -84,13 +81,13 @@ export class RenderQueue { this.opaques.push({ hash, depth, shaderId: pso.shader.id, - subModel, pass, cmdBuff: subModel.commandBuffers[i]}); + subModel, pso, cmdBuff: subModel.commandBuffers[i]}); } else { const hash = (1 << 30) | i; this.transparents.push({ hash, depth, shaderId: pso.shader.id, - subModel, pass, cmdBuff: subModel.commandBuffers[i]}); + subModel, pso, cmdBuff: subModel.commandBuffers[i]}); } } } diff --git a/cocos/renderer/core/pass.ts b/cocos/renderer/core/pass.ts index a747292daa..d488f981bf 100644 --- a/cocos/renderer/core/pass.ts +++ b/cocos/renderer/core/pass.ts @@ -22,6 +22,7 @@ export interface IPassInfoFull extends IPassInfo { samplers: ISamplerInfo[]; shader: GFXShader; renderPass: GFXRenderPass; + globals: GFXBuffer; } type RecursivePartial = { @@ -110,6 +111,7 @@ export class Pass { protected _device: GFXDevice; protected _shader: GFXShader | null = null; protected _renderPass: GFXRenderPass | null = null; + protected _globalUBO: GFXBuffer | null = null; public constructor (device: GFXDevice) { this._device = device; @@ -126,6 +128,7 @@ export class Pass { )); this._shader = info.shader; this._renderPass = info.renderPass; + this._globalUBO = info.globals; this._fillinPipelineInfo(info); for (const u of info.blocks) { @@ -258,6 +261,7 @@ export class Pass { for (const t of Object.keys(this._textureViews)) { bindingLayout.bindTextureView(parseInt(t), this._textureViews[t]); } + bindingLayout.bindBuffer(UBOGlobal.BLOCK.binding, this._globalUBO!); const pipelineLayout = this._device.createPipelineLayout({ layouts: [bindingLayout] }); const pipelineState = this._device.createPipelineState({ bs: this._bs, diff --git a/cocos/renderer/scene/model.ts b/cocos/renderer/scene/model.ts index 561631eeb8..49f07fc930 100644 --- a/cocos/renderer/scene/model.ts +++ b/cocos/renderer/scene/model.ts @@ -15,6 +15,7 @@ import { RenderScene } from './render-scene'; import { Node } from '../../scene-graph/node'; import { RecyclePool } from '../../3d/memop'; import { SubModel } from './submodel'; +import { GFXPipelineState } from '../../gfx/pipeline-state'; const _temp_floatx16 = new Float32Array(16); const _temp_mat4 = mat4.create(); @@ -43,6 +44,8 @@ export class Model { private _uboLocal: UBOLocal; private _localUBO: GFXBuffer | null; private _subModels: SubModel[]; + private _matPSORecord: Map; + private _matRefCount: Map; /** * Setup a default empty model */ @@ -61,6 +64,8 @@ export class Model { this._localUBO = null; this._device = cc.director.root.device; this._subModels = new Array(); + this._matPSORecord = new Map(); + this._matRefCount = new Map(); } public initialize () { @@ -129,6 +134,15 @@ export class Model { this._uboLocal.view.set(_temp_floatx16, UBOLocal.MAT_WORLD_IT_OFFSET); this._localUBO!.update(this._uboLocal.view); + + for (const mat of this._matPSORecord.keys()) { + for (const pass of mat.passes) { + pass.update(); + } + for (const pso of this._matPSORecord.get(mat)!) { + pso.pipelineLayout.layouts[0].update(); + } + } } /** @@ -181,16 +195,60 @@ export class Model { if (this._subModels[idx] == null) { this._subModels[idx] = _subMeshPool.add(); } else { + const oldMat = this._subModels[idx].material; this._subModels[idx].destroy(); + this.releasePSO(oldMat); } - this._subModels[idx].initialize(subMeshData, mat, this._localUBO!); + this.allocatePSO(mat); + this._subModels[idx].initialize(subMeshData, mat, this._matPSORecord.get(mat)!); } public setSubModelMaterial (idx: number, mat: Material) { if (this._subModels[idx] == null) { return; } + if (this._subModels[idx].material === mat) { + this.destroyPipelineState(mat, this._matPSORecord.get(mat)!); + this._matPSORecord.set(mat, this.createPipelineState(mat)); + } else { + this.releasePSO(this._subModels[idx].material); + this.allocatePSO(mat); + } this._subModels[idx].material = mat; + this._subModels[idx].psos = this._matPSORecord.get(mat)!; + } + + protected createPipelineState (mat: Material): GFXPipelineState[] { + const ret = new Array(mat.passes.length); + for (let i = 0; i < ret.length; i++) { + ret[i] = mat.passes[i].createPipelineState()!; + ret[i].pipelineLayout.layouts[0].bindBuffer(UBOLocal.BLOCK.binding, this.localUBO); + } + return ret; + } + + protected destroyPipelineState (mat: Material, pso: GFXPipelineState[]) { + for (let i = 0; i < mat.passes.length; i++) { + mat.passes[i].destroyPipelineState(pso[i]); + } + } + + private allocatePSO (mat: Material) { + if (this._matRefCount.get(mat) == null) { + this._matRefCount.set(mat, 1); + this._matPSORecord.set(mat, this.createPipelineState(mat)); + } else { + this._matRefCount.set(mat, this._matRefCount.get(mat)! + 1); + } + } + + private releasePSO (mat: Material) { + this._matRefCount.set(mat, this._matRefCount.get(mat)! - 1); + if (this._matRefCount.get(mat) === 0) { + this.destroyPipelineState(mat, this._matPSORecord.get(mat)!); + this._matPSORecord.delete(mat); + this._matRefCount.delete(mat); + } } /** diff --git a/cocos/renderer/scene/submodel.ts b/cocos/renderer/scene/submodel.ts index aece160ebe..976c3216ac 100644 --- a/cocos/renderer/scene/submodel.ts +++ b/cocos/renderer/scene/submodel.ts @@ -7,26 +7,29 @@ import { GFXDevice } from '../../gfx/device'; import { GFXInputAssembler, IGFXInputAssemblerInfo } from '../../gfx/input-assembler'; import { UBOLocal } from '../../pipeline/render-pipeline'; import { Pass } from '../core/pass'; +import { GFXPipelineState, GFXInputState } from '../../gfx/pipeline-state'; +import { GFXBindingLayout } from '../../gfx/binding-layout'; +import { GFXPipelineLayout } from '../../gfx/pipeline-layout'; export class SubModel { protected _subMeshObject: IRenderingSubmesh | null; private _inputAssembler: GFXInputAssembler | null; private _material: Material | null; private _cmdBuffers: GFXCommandBuffer[]; - private _localUBO: GFXBuffer | null; + private _psos: GFXPipelineState[] | null; private _castShadow: boolean; constructor () { this._subMeshObject = null; this._material = null; this._cmdBuffers = new Array(); - this._localUBO = null; + this._psos = null; this._castShadow = false; this._inputAssembler = null; } - public initialize (subMesh: IRenderingSubmesh, mat: Material, localUBO: GFXBuffer) { - this.localUBO = localUBO; + public initialize (subMesh: IRenderingSubmesh, mat: Material, psos: GFXPipelineState[]) { + this._psos = psos; this.subMeshData = subMesh; const iaInfo = {} as IGFXInputAssemblerInfo; iaInfo.attributes = this._subMeshObject!.attributes; @@ -53,6 +56,14 @@ export class SubModel { return this._subMeshObject!; } + get psos (): GFXPipelineState[] { + return this._psos!; + } + + set psos (val: GFXPipelineState[]) { + this._psos = val; + } + set material (material: Material) { this._material = material; if (material == null) { @@ -72,8 +83,8 @@ export class SubModel { } } - set localUBO (val: GFXBuffer) { - this._localUBO = val; + get material (): Material { + return this._material!; } get castShadow (): boolean { @@ -85,8 +96,8 @@ export class SubModel { } private recordCommandBuffer (index: number) { - const device = cc.director.root.device; - const pass = this._material!.passes[index]; + const device = cc.director.root.device as GFXDevice; + const pso = this._psos![index]; if (this._cmdBuffers[index] == null) { const cmdBufferInfo = { allocator: device.commandAllocator, @@ -94,16 +105,12 @@ export class SubModel { }; this._cmdBuffers[index] = device.createCommandBuffer(cmdBufferInfo); } - - const localUBO = this._localUBO as GFXBuffer; const inputAssembler = this._inputAssembler as GFXInputAssembler; - pass.bindingLayout.bindBuffer(UBOLocal.BLOCK.binding, localUBO); - const cmdBuff = this._cmdBuffers[index]; cmdBuff.begin(); - cmdBuff.bindPipelineState(pass.pipelineState); - cmdBuff.bindBindingLayout(pass.bindingLayout); + cmdBuff.bindPipelineState(pso); + cmdBuff.bindBindingLayout(pso.pipelineLayout.layouts[0]); cmdBuff.bindInputAssembler(inputAssembler); cmdBuff.draw(inputAssembler); cmdBuff.end();