From 86bce7fc2b5a45149d9fab694f01609e9ec4ff61 Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Thu, 17 Jan 2019 20:51:44 +0800 Subject: [PATCH] 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": [] }