From a307b8e96f878c8021eef51f6e852a0870e54900 Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Thu, 17 Jan 2019 17:57:33 +0800 Subject: [PATCH 1/2] move PSO out of pass --- cocos/3d/assets/material.ts | 43 ++++------ cocos/renderer/core/pass.ts | 140 +++++++++++-------------------- cocos/renderer/scene/submodel.ts | 20 ++--- 3 files changed, 71 insertions(+), 132 deletions(-) diff --git a/cocos/3d/assets/material.ts b/cocos/3d/assets/material.ts index a6f2d9797c..61e59f1d2b 100644 --- a/cocos/3d/assets/material.ts +++ b/cocos/3d/assets/material.ts @@ -27,14 +27,9 @@ import { ccclass, property } from '../../core/data/class-decorator'; import { murmurhash2_32_gc } from '../../core/utils/murmurhash2_gc'; import { GFXBindingType } from '../../gfx/define'; import { Effect } from '../../renderer/core/effect'; -import { IPassOverrides, Pass } from '../../renderer/core/pass'; -import { EffectAsset } from './effect-asset'; - -export interface IMaterialInfo { - technique?: number; - effectName?: string; - defines?: Record; -} +import { Pass } from '../../renderer/core/pass'; +import { RenderableComponent } from '../framework/renderable-component'; +import { EffectAsset, IPassInfo } from './effect-asset'; @ccclass('cc.Material') export class Material extends Asset { @@ -60,18 +55,9 @@ export class Material extends Asset { protected _props: Array> = []; protected _passes: Pass[] = []; - protected _owner: any = null; + protected _owner: RenderableComponent | null = null; protected _hash = 0; - constructor (opts?: IMaterialInfo) { - super(); - if (opts) { - if (opts.technique) { this._techIdx = opts.technique; } - if (opts.defines) { this._defines = opts.defines; } - if (opts.effectName) { this.effectName = opts.effectName; } - } - } - @property set effectAsset (val: EffectAsset | null) { if (!val || this.effectName !== val.name) { this.update(val, false); } @@ -157,17 +143,11 @@ export class Material extends Asset { this.update(); } - public rebuildWithOverrides (overrides: IPassOverrides, idx?: number) { + public overridePipelineStates (overrides: Partial, passIdx: number = 0) { if (!this._passes || !this._effectAsset) { return; } const passInfos = Effect.getPassInfos(this._effectAsset, this._techIdx); - if (idx === undefined) { - for (let i = 0; i < this._passes.length; i++) { - this._passes[i].rebuildWithOverrides(passInfos[i], overrides); - } - } else { - this._passes[idx].rebuildWithOverrides(passInfos[idx], overrides); - } - this.prepareHash(); + this._passes[passIdx].overridePipelineStates(passInfos[passIdx], overrides); + this.onPassChange(); } public update (asset: EffectAsset | string | null = this._effectAsset, keepProps: boolean = true) { @@ -202,7 +182,7 @@ export class Material extends Asset { } else { this._props.fill({}); } - this.prepareHash(); + this.onPassChange(); } protected _uploadProperty (pass: Pass, name: string, val: any) { @@ -219,12 +199,17 @@ export class Material extends Asset { return true; } - protected prepareHash () { + protected onPassChange () { let str = ''; for (const pass of this._passes) { str += pass.serializePipelineStates(); } this._hash = murmurhash2_32_gc(str, 666); + if (this._owner) { + const comp = this._owner; + const index = comp.sharedMaterials.findIndex((m) => m === this); + if (index >= 0) { comp._onMaterialModified(index, this); } + } } } diff --git a/cocos/renderer/core/pass.ts b/cocos/renderer/core/pass.ts index ad6a1d20c3..da15752f91 100644 --- a/cocos/renderer/core/pass.ts +++ b/cocos/renderer/core/pass.ts @@ -2,13 +2,12 @@ import { IBlockInfo, IPassInfo, ISamplerInfo } from '../../3d/assets/effect-asset'; import { color4, mat2, mat3, mat4, vec2, vec3, vec4 } from '../../core/vmath'; -import { GFXBindingLayout, IGFXBinding } from '../../gfx/binding-layout'; +import { IGFXBinding } from '../../gfx/binding-layout'; import { GFXBuffer } from '../../gfx/buffer'; import { GFXBindingType, GFXBufferUsageBit, GFXMemoryUsageBit, GFXPrimitiveMode, GFXType } from '../../gfx/define'; import { GFXDevice } from '../../gfx/device'; -import { GFXPipelineLayout } from '../../gfx/pipeline-layout'; import { GFXBlendState, GFXBlendTarget, GFXDepthStencilState, - GFXInputState, GFXPipelineState, GFXRasterizerState, IGFXPipelineStateInfo } from '../../gfx/pipeline-state'; + GFXRasterizerState } from '../../gfx/pipeline-state'; import { GFXRenderPass } from '../../gfx/render-pass'; import { GFXSampler } from '../../gfx/sampler'; import { GFXShader } from '../../gfx/shader'; @@ -22,14 +21,6 @@ export interface IPassInfoFull extends IPassInfo { samplers: ISamplerInfo[]; shader: GFXShader; renderPass: GFXRenderPass; - globals: GFXBuffer; -} - -export interface IPassOverrides { - bs: GFXBlendState; - dss: GFXDepthStencilState; - rs: GFXRasterizerState; - primitive: GFXPrimitiveMode; } const _type2fn = { @@ -87,21 +78,19 @@ export class Pass { public static getBindingFromHandle = getBindingFromHandle; public static getIndexFromHandle = getIndexFromHandle; // internal resources - protected _pipelineState: GFXPipelineState | null = null; - protected _pipelineLayout: GFXPipelineLayout | null = null; - protected _bindingLayout: GFXBindingLayout | null = null; - protected _buffers: GFXBuffer[] = []; - protected _samplers: GFXSampler[] = []; + protected _buffers: Record = {}; + protected _samplers: Record = {}; // internal data protected _programName: string = ''; protected _primitive: GFXPrimitiveMode = GFXPrimitiveMode.TRIANGLE_LIST; protected _stage: RenderPassStage = RenderPassStage.DEFAULT; - protected _handleMap: Record = {}; - protected _blocks: IBlock[] = []; protected _bindings: IGFXBinding[] = []; protected _bs: GFXBlendState = new GFXBlendState(); protected _dss: GFXDepthStencilState = new GFXDepthStencilState(); protected _rs: GFXRasterizerState = new GFXRasterizerState(); + protected _handleMap: Record = {}; + protected _blocks: IBlock[] = []; + protected _textureViews: Record = {}; // external references protected _device: GFXDevice; protected _shader: GFXShader | null = null; @@ -115,18 +104,14 @@ export class Pass { this._programName = info.program; // pipeline state const device = this._device; - const bindings = this._bindings = info.blocks.map((u) => + this._bindings = info.blocks.map((u) => ({ name: u.name, binding: u.binding, type: GFXBindingType.UNIFORM_BUFFER }), ).concat(info.samplers.map((u) => ({ name: u.name, binding: u.binding, type: GFXBindingType.SAMPLER }), )); - this._bindingLayout = device.createBindingLayout({ bindings }); - if (!this._bindingLayout) { console.error('create binding layout failed'); return; } - this._pipelineLayout = device.createPipelineLayout({ layouts: [this._bindingLayout] }); - if (!this._pipelineLayout) { console.error('create pipeline layout failed'); return; } this._shader = info.shader; this._renderPass = info.renderPass; - this._createPipelineState(info); + this._fillinPipelineInfo(info); for (const u of info.blocks) { if (builtinRE.test(u.name)) { @@ -140,7 +125,6 @@ export class Pass { usage: GFXBufferUsageBit.UNIFORM, }); if (buffer) { - this._bindingLayout.bindBuffer(u.binding, buffer); this._buffers[u.binding] = buffer; } else { console.error('create buffer failed.'); @@ -167,8 +151,6 @@ export class Pass { }, 0); // === u.size buffer.update(block.buffer); } - // also bind builtin globals - this._bindingLayout.bindBuffer(UBOGlobal.BLOCK.binding, info.globals); for (const u of info.samplers) { this._handleMap[u.name] = genHandle(GFXBindingType.SAMPLER, u.type, u.binding); @@ -177,13 +159,11 @@ export class Pass { // TODO: specify filter modes in effect }); if (sampler) { - this._bindingLayout.bindSampler(u.binding, sampler); - this._samplers.push(sampler); + this._samplers[u.binding] = sampler; } else { console.error('create sampler failed.'); } } - this._bindingLayout.update(); } public getHandle (name: string) { @@ -204,38 +184,19 @@ export class Pass { } public bindTextureView (binding: number, value: GFXTextureView) { - (this._bindingLayout as GFXBindingLayout).bindTextureView(binding, value); + this._textureViews[binding] = value; } public bindSampler (binding: number, value: GFXSampler) { - (this._bindingLayout as GFXBindingLayout).bindSampler(binding, value); + this._samplers[binding] = value; } - public rebuildWithOverrides (info: Partial, overrides: IPassOverrides) { - if (this._pipelineState) { this._pipelineState.destroy(); } - const ors = Object.assign({}, overrides); - this._createPipelineState(info, (states) => { - if (overrides.primitive !== undefined) { this._primitive = states.primitive = overrides.primitive; } - Object.assign(states.rs, ors.rs); - Object.assign(states.dss, ors.dss); - if (ors.bs) { - if (ors.bs.targets) { - for (let i = 0; i < ors.bs.targets.length; i++) { - Object.assign(states.bs.targets[i], ors.bs.targets[i]); - } - delete ors.bs.targets; - } - Object.assign(states.bs, ors.bs); - } - }); - } - - public destroy () { - if (this._buffers) { this._buffers.forEach((b) => b.destroy()); this._buffers.length = 0; } - if (this._samplers) { this._samplers.forEach((b) => b.destroy()); this._samplers.length = 0; } - if (this._bindingLayout) { this._bindingLayout.destroy(); this._bindingLayout = null; } - if (this._pipelineLayout) { this._pipelineLayout.destroy(); this._pipelineLayout = null; } - if (this._pipelineState) { this._pipelineState.destroy(); this._pipelineState = null; } + public overridePipelineStates (info: Partial, overrides: Partial) { + this._bs = new GFXBlendState(); + this._dss = new GFXDepthStencilState(); + this._rs = new GFXRasterizerState(); + this._fillinPipelineInfo(info); + this._fillinPipelineInfo(overrides); } public update () { @@ -247,27 +208,36 @@ export class Pass { block.dirty = false; } } - (this._bindingLayout as GFXBindingLayout).update(); + } + + public destroy () { + if (this._buffers) { + for (const b of Object.values(this._buffers)) { + b.destroy(); + } + this._buffers = {}; + } + if (this._samplers) { + for (const s of Object.values(this._samplers)) { + s.destroy(); + } + this._samplers = {}; + } } public serializePipelineStates () { - const ps = this._pipelineState as GFXPipelineState; - let res = `${ps.shader.name},${this._stage},${ps.primitive}`; - res += serializeBlendState(ps.blendState); - res += serializeDepthStencilState(ps.depthStencilState); - res += serializeRasterizerState(ps.rasterizerState); + let res = `${this._shader && this._shader.name},${this._stage},${this._primitive}`; + res += serializeBlendState(this._bs); + res += serializeDepthStencilState(this._dss); + res += serializeRasterizerState(this._rs); return res; } - protected _createPipelineState (info: Partial, override?: (states: IGFXPipelineStateInfo) => any) { - if (info.primitive) { this._primitive = info.primitive; } - if (info.stage) { this._stage = info.stage; } + protected _fillinPipelineInfo (info: Partial) { + if (info.primitive !== undefined) { this._primitive = info.primitive; } + if (info.stage !== undefined) { this._stage = info.stage; } - if (!this._shader || !this._renderPass || !this._pipelineLayout) { - console.error('gfx resouces incomplete, create pipeline state failed.'); - return null; - } - const bs = new GFXBlendState(); + const bs = this._bs; if (info.blendState) { const bsInfo = Object.assign({}, info.blendState); if (bsInfo.targets) { @@ -277,33 +247,19 @@ export class Pass { delete bsInfo.targets; Object.assign(bs, bsInfo); } - const stateInfo = { - bs: this._bs = bs, - dss: this._dss = Object.assign(new GFXDepthStencilState(), info.depthStencilState), - is: new GFXInputState(), - layout: this._pipelineLayout, - primitive: info.primitive || this._primitive, - renderPass: this._renderPass, - rs: this._rs = Object.assign(new GFXRasterizerState(), info.rasterizerState), - shader: this._shader, - }; - if (override) { override(stateInfo); } - if (this._pipelineState) { - this._pipelineState.initialize(stateInfo); - } else { - this._pipelineState = this._device.createPipelineState(stateInfo); - } + Object.assign(this._rs, info.rasterizerState); + Object.assign(this._dss, info.depthStencilState); } get programName () { return this._programName; } - get shader (): GFXShader { return this._shader!; } - get pipelineState () { return this._pipelineState as GFXPipelineState; } - get bindingLayout () { return this._bindingLayout as GFXBindingLayout; } - get primitive () { return this._primitive; } - get bindings () { return this._bindings; } + get primitive (): GFXPrimitiveMode { return this._primitive; } + get stage (): RenderPassStage { return this._stage; } + get bindings (): IGFXBinding[] { return this._bindings; } get blendState (): GFXBlendState { return this._bs; } get depthStencilState (): GFXDepthStencilState { return this._dss; } get rasterizerState (): GFXRasterizerState { return this._rs; } + get shader (): GFXShader | null { return this._shader; } + get renderPass (): GFXRenderPass | null { return this._renderPass; } } function serializeBlendState (bs: GFXBlendState) { diff --git a/cocos/renderer/scene/submodel.ts b/cocos/renderer/scene/submodel.ts index b893f95aa0..aece160ebe 100644 --- a/cocos/renderer/scene/submodel.ts +++ b/cocos/renderer/scene/submodel.ts @@ -1,14 +1,12 @@ -import { IRenderingSubmesh } from "../../3d/assets/mesh"; -import { GFXDevice } from "../../gfx/device"; -import { Material } from "../../3d/assets/material"; -import { GFXCommandBuffer } from "../../gfx/command-buffer"; -import { aabb } from "../../3d/geom-utils"; -import { Vec3 } from "../../core/value-types"; -import { GFXCommandBufferType } from "../../gfx/define"; -import { GFXBuffer } from "../../gfx/buffer"; -import { UBOLocal } from "../../pipeline/render-pipeline"; -import { Pass } from "../core/pass"; -import { GFXInputAssembler, IGFXInputAssemblerInfo } from "../../gfx/input-assembler"; +import { Material } from '../../3d/assets/material'; +import { IRenderingSubmesh } from '../../3d/assets/mesh'; +import { GFXBuffer } from '../../gfx/buffer'; +import { GFXCommandBuffer } from '../../gfx/command-buffer'; +import { GFXCommandBufferType } from '../../gfx/define'; +import { GFXDevice } from '../../gfx/device'; +import { GFXInputAssembler, IGFXInputAssemblerInfo } from '../../gfx/input-assembler'; +import { UBOLocal } from '../../pipeline/render-pipeline'; +import { Pass } from '../core/pass'; export class SubModel { protected _subMeshObject: IRenderingSubmesh | null; From 86bce7fc2b5a45149d9fab694f01609e9ec4ff61 Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Thu, 17 Jan 2019 20:51:44 +0800 Subject: [PATCH 2/2] pass refactor --- cocos/3d/assets/material.ts | 6 ++-- cocos/renderer/core/pass.ts | 69 +++++++++++++++++++++++++++++++++---- tslint.json | 3 +- 3 files changed, 68 insertions(+), 10 deletions(-) diff --git a/cocos/3d/assets/material.ts b/cocos/3d/assets/material.ts index 61e59f1d2b..b015831c8f 100644 --- a/cocos/3d/assets/material.ts +++ b/cocos/3d/assets/material.ts @@ -27,9 +27,9 @@ import { ccclass, property } from '../../core/data/class-decorator'; import { murmurhash2_32_gc } from '../../core/utils/murmurhash2_gc'; import { GFXBindingType } from '../../gfx/define'; import { Effect } from '../../renderer/core/effect'; -import { Pass } from '../../renderer/core/pass'; +import { Pass, PassOverrides } from '../../renderer/core/pass'; import { RenderableComponent } from '../framework/renderable-component'; -import { EffectAsset, IPassInfo } from './effect-asset'; +import { EffectAsset } from './effect-asset'; @ccclass('cc.Material') export class Material extends Asset { @@ -143,7 +143,7 @@ export class Material extends Asset { this.update(); } - public overridePipelineStates (overrides: Partial, passIdx: number = 0) { + public overridePipelineStates (overrides: PassOverrides, passIdx: number = 0) { if (!this._passes || !this._effectAsset) { return; } const passInfos = Effect.getPassInfos(this._effectAsset, this._techIdx); this._passes[passIdx].overridePipelineStates(passInfos[passIdx], overrides); diff --git a/cocos/renderer/core/pass.ts b/cocos/renderer/core/pass.ts index da15752f91..a747292daa 100644 --- a/cocos/renderer/core/pass.ts +++ b/cocos/renderer/core/pass.ts @@ -2,12 +2,13 @@ import { IBlockInfo, IPassInfo, ISamplerInfo } from '../../3d/assets/effect-asset'; import { color4, mat2, mat3, mat4, vec2, vec3, vec4 } from '../../core/vmath'; -import { IGFXBinding } from '../../gfx/binding-layout'; +import { GFXBindingLayout, IGFXBinding } from '../../gfx/binding-layout'; import { GFXBuffer } from '../../gfx/buffer'; import { GFXBindingType, GFXBufferUsageBit, GFXMemoryUsageBit, GFXPrimitiveMode, GFXType } from '../../gfx/define'; import { GFXDevice } from '../../gfx/device'; +import { GFXPipelineLayout } from '../../gfx/pipeline-layout'; import { GFXBlendState, GFXBlendTarget, GFXDepthStencilState, - GFXRasterizerState } from '../../gfx/pipeline-state'; + GFXInputState, GFXPipelineState, GFXRasterizerState } from '../../gfx/pipeline-state'; import { GFXRenderPass } from '../../gfx/render-pass'; import { GFXSampler } from '../../gfx/sampler'; import { GFXShader } from '../../gfx/shader'; @@ -23,6 +24,13 @@ export interface IPassInfoFull extends IPassInfo { renderPass: GFXRenderPass; } +type RecursivePartial = { + [P in keyof T]?: + T[P] extends Array ? Array> : + T[P] extends ReadonlyArray ? ReadonlyArray> : RecursivePartial; +}; +export type PassOverrides = RecursivePartial; + const _type2fn = { [GFXType.INT]: (a: Float32Array, v: any) => a[0] = v, [GFXType.INT2]: (a: Float32Array, v: any) => vec2.array(a, v), @@ -72,6 +80,12 @@ interface IBlock { dirty: boolean; } +interface IPassResources { + bindingLayout: GFXBindingLayout; + pipelineLayout: GFXPipelineLayout; + pipelineState: GFXPipelineState; +} + export class Pass { public static getBindingTypeFromHandle = getBindingTypeFromHandle; public static getTypeFromHandle = getTypeFromHandle; @@ -80,6 +94,8 @@ export class Pass { // internal resources protected _buffers: Record = {}; protected _samplers: Record = {}; + protected _textureViews: Record = {}; + protected _resources: IPassResources[] = []; // internal data protected _programName: string = ''; protected _primitive: GFXPrimitiveMode = GFXPrimitiveMode.TRIANGLE_LIST; @@ -90,7 +106,6 @@ export class Pass { protected _rs: GFXRasterizerState = new GFXRasterizerState(); protected _handleMap: Record = {}; protected _blocks: IBlock[] = []; - protected _textureViews: Record = {}; // external references protected _device: GFXDevice; protected _shader: GFXShader | null = null; @@ -132,7 +147,7 @@ export class Pass { } // buffer data processing system const block: IBlock = this._blocks[u.binding] = { - buffer: new ArrayBuffer(u.size), + buffer: buffer.buffer as ArrayBuffer, dirty: false, views: [], }; @@ -185,17 +200,23 @@ export class Pass { public bindTextureView (binding: number, value: GFXTextureView) { this._textureViews[binding] = value; + for (const res of this._resources) { + res.bindingLayout.bindTextureView(binding, value); + } } public bindSampler (binding: number, value: GFXSampler) { this._samplers[binding] = value; + for (const res of this._resources) { + res.bindingLayout.bindSampler(binding, value); + } } - public overridePipelineStates (info: Partial, overrides: Partial) { + public overridePipelineStates (original: IPassInfo, overrides: PassOverrides) { this._bs = new GFXBlendState(); this._dss = new GFXDepthStencilState(); this._rs = new GFXRasterizerState(); - this._fillinPipelineInfo(info); + this._fillinPipelineInfo(original); this._fillinPipelineInfo(overrides); } @@ -225,6 +246,42 @@ export class Pass { } } + public createPipelineState () { + if (!this._renderPass || !this._shader) { return null; } + const bindingLayout = this._device.createBindingLayout({ bindings: this._bindings }); + for (const b of Object.keys(this._buffers)) { + bindingLayout.bindBuffer(parseInt(b), this._buffers[b]); + } + for (const s of Object.keys(this._samplers)) { + bindingLayout.bindSampler(parseInt(s), this._samplers[s]); + } + for (const t of Object.keys(this._textureViews)) { + bindingLayout.bindTextureView(parseInt(t), this._textureViews[t]); + } + const pipelineLayout = this._device.createPipelineLayout({ layouts: [bindingLayout] }); + const pipelineState = this._device.createPipelineState({ + bs: this._bs, + dss: this._dss, + is: new GFXInputState(), + layout: pipelineLayout, + primitive: this._primitive, + renderPass: this._renderPass, + rs: this._rs, + shader: this._shader, + }); + this._resources.push({ bindingLayout, pipelineLayout, pipelineState }); + return pipelineState; + } + + public destroyPipelineState (pipelineStates: GFXPipelineState) { + const idx = this._resources.findIndex((res) => res.pipelineState === pipelineStates); + if (idx >= 0) { + const { bindingLayout: bl, pipelineLayout: pl, pipelineState: ps } = this._resources[idx]; + bl.destroy(); pl.destroy(); ps.destroy(); + this._resources.splice(idx, 1); + } + } + public serializePipelineStates () { let res = `${this._shader && this._shader.name},${this._stage},${this._primitive}`; res += serializeBlendState(this._bs); diff --git a/tslint.json b/tslint.json index 8087fac659..fcac35efac 100644 --- a/tslint.json +++ b/tslint.json @@ -16,7 +16,8 @@ "object-literal-sort-keys": false, "ban-types": false, "no-bitwise": false, - "no-empty": false + "no-empty": false, + "radix": false }, "rulesDirectory": [] }