From e88d1cae3dd22bf2e8dbe6480321734feeb3df4b Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Mon, 15 Jun 2020 09:56:31 +0800 Subject: [PATCH] shader stores attribute info (#6866) --- cocos/core/gfx/shader.ts | 24 ++++++++++++++----- cocos/core/gfx/webgl/webgl-shader.ts | 11 +++------ cocos/core/gfx/webgl2/webgl2-shader.ts | 11 +++------ cocos/core/pipeline/pipeline-state-manager.ts | 13 +--------- cocos/core/renderer/core/pass.ts | 7 ++---- cocos/core/renderer/core/program-lib.ts | 9 ++++--- cocos/core/renderer/scene/model.ts | 2 +- cocos/core/renderer/scene/submodel.ts | 6 ++--- 8 files changed, 34 insertions(+), 49 deletions(-) diff --git a/cocos/core/gfx/shader.ts b/cocos/core/gfx/shader.ts index 991796c370..095230cd53 100644 --- a/cocos/core/gfx/shader.ts +++ b/cocos/core/gfx/shader.ts @@ -4,6 +4,7 @@ import { GFXObject, GFXObjectType, GFXShaderType, GFXType } from './define'; import { GFXDevice } from './device'; +import { IGFXAttribute } from './input-assembler'; export interface IGFXShaderMacro { macro: string; @@ -34,7 +35,6 @@ export class GFXUniformBlock { public shaderStages: GFXShaderType = GFXShaderType.NONE; public binding: number = -1; public name: string = ''; - // instance : string; public members: GFXUniform[] = []; } @@ -53,10 +53,10 @@ export class GFXUniformSampler { export interface IGFXShaderInfo { name: string; stages: IGFXShaderStage[]; - // bindings: GFXBinding[]; - blocks?: GFXUniformBlock[]; - samplers?: GFXUniformSampler[]; + attributes: IGFXAttribute[]; + blocks: GFXUniformBlock[]; + samplers: GFXUniformSampler[]; } /** @@ -81,6 +81,18 @@ export abstract class GFXShader extends GFXObject { return this._name; } + public get attributes () { + return this._attributes; + } + + public get blocks () { + return this._blocks; + } + + public get samplers () { + return this._samplers; + } + protected _device: GFXDevice; protected _id: number; @@ -89,9 +101,9 @@ export abstract class GFXShader extends GFXObject { protected _stages: IGFXShaderStage[] = []; - // protected _bindings: GFXBinding[] = []; + protected _attributes: IGFXAttribute[] = []; - protected _blocks: GFXUniformBlock[] = []; // blocks are used for being compatible with single uniforms + protected _blocks: GFXUniformBlock[] = []; protected _samplers: GFXUniformSampler[] = []; diff --git a/cocos/core/gfx/webgl/webgl-shader.ts b/cocos/core/gfx/webgl/webgl-shader.ts index 99e665254b..ebef83da6d 100644 --- a/cocos/core/gfx/webgl/webgl-shader.ts +++ b/cocos/core/gfx/webgl/webgl-shader.ts @@ -16,14 +16,9 @@ export class WebGLGFXShader extends GFXShader { this._name = info.name; this._stages = info.stages; - - if (info.blocks !== undefined) { - this._blocks = info.blocks; - } - - if (info.samplers !== undefined) { - this._samplers = info.samplers; - } + this._attributes = info.attributes; + this._blocks = info.blocks; + this._samplers = info.samplers; this._gpuShader = { name: info.name ? info.name : '', diff --git a/cocos/core/gfx/webgl2/webgl2-shader.ts b/cocos/core/gfx/webgl2/webgl2-shader.ts index be9c1269d9..53367e35c5 100644 --- a/cocos/core/gfx/webgl2/webgl2-shader.ts +++ b/cocos/core/gfx/webgl2/webgl2-shader.ts @@ -16,14 +16,9 @@ export class WebGL2GFXShader extends GFXShader { this._name = info.name; this._stages = info.stages; - - if (info.blocks !== undefined) { - this._blocks = info.blocks; - } - - if (info.samplers !== undefined) { - this._samplers = info.samplers; - } + this._attributes = info.attributes; + this._blocks = info.blocks; + this._samplers = info.samplers; this._gpuShader = { name: info.name ? info.name : '', diff --git a/cocos/core/pipeline/pipeline-state-manager.ts b/cocos/core/pipeline/pipeline-state-manager.ts index ace828df5a..646e49243a 100644 --- a/cocos/core/pipeline/pipeline-state-manager.ts +++ b/cocos/core/pipeline/pipeline-state-manager.ts @@ -29,18 +29,7 @@ export class PipelineStateManager { const pipelineLayout = device.createPipelineLayout({ layouts: [psoCreateInfo.bindingLayout] }); - const iaAttrs = this._inputState.attributes = ia.attributes; - const shaderAttrs = psoCreateInfo.shaderInput.attributes; - for (let i = 0; i < iaAttrs.length; i++) { - const attr = iaAttrs[i]; let j = 0; - for (; j < shaderAttrs.length; j++) { - const shaderAttr = shaderAttrs[j]; - if (shaderAttr.name === attr.name) { - attr.location = shaderAttr.location; - break; - } - } - } + this._inputState.attributes = ia.attributes; pso = device.createPipelineState({ primitive: psoCreateInfo.primitive, shader: psoCreateInfo.shader, diff --git a/cocos/core/renderer/core/pass.ts b/cocos/core/renderer/core/pass.ts index 7aaaaded27..52053394e9 100644 --- a/cocos/core/renderer/core/pass.ts +++ b/cocos/core/renderer/core/pass.ts @@ -190,7 +190,6 @@ export class Pass { protected _device: GFXDevice; protected _shader: GFXShader | null = null; protected _bindings: IGFXBinding[] = []; - protected _shaderInput: GFXInputState | null = null; // for dynamic batching protected _batchedBuffer: BatchedBuffer | null = null; protected _instancedBuffer: InstancedBuffer | null = null; @@ -497,7 +496,7 @@ export class Pass { this._dynamicBatchingSync(); const res = programLib.getGFXShader(this._device, this._programName, this._defines, pipeline); if (!res.shader) { console.warn(`create shader ${this._programName} failed`); return false; } - this._shader = res.shader; this._bindings = res.bindings; this._shaderInput = res.input; + this._shader = res.shader; this._bindings = res.bindings; return true; } @@ -509,7 +508,7 @@ export class Pass { * @param patches the marcos to be used in shader. */ public createPipelineStateCI (patches?: IMacroPatch[]): IPSOCreateInfo | null { - if ((!this._shader || !this._bindings.length || !this._shaderInput) && !this.tryCompile()) { + if ((!this._shader || !this._bindings.length) && !this.tryCompile()) { console.warn(`pass resources not complete, create PSO hash info failed`); return null; } @@ -552,7 +551,6 @@ export class Pass { dynamicStates: this._dynamicStates, bindingLayout, hash: this._hash, - shaderInput: res && res.input || this._shaderInput!, }; } @@ -679,7 +677,6 @@ export class Pass { // resources get shader () { return this._shader!; } get bindings () { return this._bindings; } - get shaderInput () { return this._shaderInput!; } get dynamics () { return this._dynamics; } get batchedBuffer () { return this._batchedBuffer; } get instancedBuffer () { return this._instancedBuffer; } diff --git a/cocos/core/renderer/core/program-lib.ts b/cocos/core/renderer/core/program-lib.ts index 0d277203c4..e02e3fa65f 100644 --- a/cocos/core/renderer/core/program-lib.ts +++ b/cocos/core/renderer/core/program-lib.ts @@ -177,7 +177,6 @@ function getShaderBindings ( export interface IShaderResources { shader: GFXShader; bindings: IGFXBinding[]; - input: GFXInputState; } /** @@ -338,18 +337,18 @@ class ProgramLib { const blocks: IBlockInfoRT[] = []; const samplers: ISamplerInfoRT[] = []; const bindings: IGFXBinding[] = []; - const inputState = new GFXInputState(); - getShaderBindings(tmpl, defines, blocks, samplers, bindings, inputState.attributes); + const attributes: IGFXAttribute[] = []; + getShaderBindings(tmpl, defines, blocks, samplers, bindings, attributes); const shader = device.createShader({ name: getShaderInstanceName(name, macroArray), - blocks, samplers, + blocks, samplers, attributes, stages: [ { type: GFXShaderType.VERTEX, source: prefix + src.vert }, { type: GFXShaderType.FRAGMENT, source: prefix + src.frag }, ], }); - return this._cache[key] = { shader, bindings, input: inputState }; + return this._cache[key] = { shader, bindings }; } } diff --git a/cocos/core/renderer/scene/model.ts b/cocos/core/renderer/scene/model.ts index b2db9eee70..8ea9395de6 100644 --- a/cocos/core/renderer/scene/model.ts +++ b/cocos/core/renderer/scene/model.ts @@ -297,7 +297,7 @@ export class Model { this.updateLocalBindings(psoCreateInfos[i], subModelIndex); } - this.updateInstancedAttributeList(psoCreateInfos[0].shaderInput.attributes, subModel.passes[0]); + this.updateInstancedAttributeList(psoCreateInfos[0].shader.attributes, subModel.passes[0]); } public getMacroPatches (subModelIndex: number) : IMacroPatch[] | undefined { diff --git a/cocos/core/renderer/scene/submodel.ts b/cocos/core/renderer/scene/submodel.ts index b10d9f070d..3133073b7f 100644 --- a/cocos/core/renderer/scene/submodel.ts +++ b/cocos/core/renderer/scene/submodel.ts @@ -8,9 +8,8 @@ import { RenderPriority } from '../../pipeline/define'; import { IMacroPatch } from '../core/pass'; import { GFXShader } from '../../gfx/shader'; import { GFXDynamicState, GFXPrimitiveMode } from '../../gfx/define'; -import { GFXBlendState, GFXDepthStencilState, GFXRasterizerState, GFXInputState } from '../../gfx/pipeline-state'; -import { GFXPipelineLayout } from '../../gfx/pipeline-layout'; -import { GFXBindingLayout } from '../../gfx'; +import { GFXBlendState, GFXDepthStencilState, GFXRasterizerState } from '../../gfx/pipeline-state'; +import { GFXBindingLayout } from '../../gfx/binding-layout'; import { legacyCC } from '../../global-exports'; export interface IPSOCreateInfo { @@ -21,7 +20,6 @@ export interface IPSOCreateInfo { blendState: GFXBlendState; dynamicStates: GFXDynamicState[]; bindingLayout: GFXBindingLayout; - shaderInput: GFXInputState; hash: number; }