diff --git a/cocos/3d/assets/material.ts b/cocos/3d/assets/material.ts index e401a4f50f..2e7aca848c 100644 --- a/cocos/3d/assets/material.ts +++ b/cocos/3d/assets/material.ts @@ -91,21 +91,15 @@ export class Material extends Asset { } public setProperty(name: string, val: any, passIdx = 0) { - if (passIdx >= this._passes.length) { - console.warn('illegal pass index.'); - return; - } + console.assert(passIdx < this._passes.length, 'illegal pass index.'); this._props[passIdx][name] = val; const pass = this._passes[passIdx]; const handle = pass.getHandleFromName(name); - pass.setBlockMember(handle, val); + pass.setUniformMember(handle, val); } public setTexture(name: string, val: TextureBase, passIdx = 0) { - if (passIdx >= this._passes.length) { - console.warn('illegal pass index.'); - return; - } + console.assert(passIdx < this._passes.length, 'illegal pass index.'); this._textures[passIdx][name] = val; const pass = this._passes[passIdx]; const handle = pass.getHandleFromName(name); @@ -152,7 +146,7 @@ export class Material extends Asset { if (!props) { props = this._props[i] = {}; } for (const p of Object.keys(props)) { const handle = pass.getHandleFromName(p); - pass.setBlockMember(handle, props[p]); + pass.setUniformMember(handle, props[p]); } let textures = this._textures[i]; if (!textures) { textures = this._textures[i] = {}; } diff --git a/cocos/pipeline/forward/test-material-stage.ts b/cocos/pipeline/forward/test-material-stage.ts index a3a645be25..28c8bb5975 100644 --- a/cocos/pipeline/forward/test-material-stage.ts +++ b/cocos/pipeline/forward/test-material-stage.ts @@ -75,11 +75,13 @@ export class TestMaterialStage extends RenderStage { this._material.effectName = 'test'; // parsed-effect file is embedded in cocos/3d/builtin/effects.js const pass = this._material.passes[0]; const handle1 = pass.getHandleFromName('u_sampler'); - pass.setTextureView(handle1, this._texView); + pass.setTextureView(handle1, this._texView); const handle2 = pass.getHandleFromName('u_color'); - pass.setBlockMember(handle2, cc.color('#ffffff')); + pass.setUniformMember(handle2, cc.color('#ffffff')); + pass.update(); this._material.inited = true; } + const cmdBuff = this._cmdBuff; this._renderArea.width = view.width; this._renderArea.height = view.height; diff --git a/cocos/renderer/core/pass.ts b/cocos/renderer/core/pass.ts index 2642e1d224..9b4c797e05 100644 --- a/cocos/renderer/core/pass.ts +++ b/cocos/renderer/core/pass.ts @@ -12,6 +12,7 @@ import { GFXBlendState, GFXBlendTarget, GFXDepthStencilState, import { GFXRenderPass } from '../../gfx/render-pass'; import { GFXSampler } from '../../gfx/sampler'; import { GFXShader } from '../../gfx/shader'; +import { GFXTextureView } from '../../gfx/texture-view'; import { RenderPassStage } from '../../pipeline/render-pipeline'; import { PropertyMap } from './effect'; @@ -56,6 +57,7 @@ const parseHandle = (handle: number) => [(handle & bindingMask) >> 16, handle & interface Block { buffer: ArrayBuffer; views: Float32Array[]; + dirty: boolean; } export class Pass { @@ -72,6 +74,7 @@ export class Pass { protected _handleMap: Record = {}; protected _typeMap: Record = {}; protected _blocks: Block[] = []; + protected _layoutDirty: boolean = false; // external references protected _device: GFXDevice; protected _shader: GFXShader | null = null; @@ -117,6 +120,7 @@ export class Pass { const block: Block = this._blocks[u.binding] = { buffer: new ArrayBuffer(u.size), views: [], + dirty: false, }; u.members.reduce((acc, cur, idx) => { // create property-specific buffer views @@ -152,19 +156,20 @@ export class Pass { return this._handleMap[name]; } - public setBlockMember(handle: number, value: any) { + public setUniformMember(handle: number, value: any) { const [ binding, idx ] = parseHandle(handle); const block = this._blocks[binding]; const type = this._typeMap[handle]; _type2fn[type](block.views[idx], value); - this._buffers[binding].update(block.buffer); + block.dirty = true; } - public setTextureView(handle: number, value: any) { + public setTextureView(handle: number, value: GFXTextureView) { const [ binding ] = parseHandle(handle); - if (this._bindingLayout) { - this._bindingLayout.bindTextureView(binding, value); - this._bindingLayout.update(); + const bl = this._bindingLayout; + if (bl && bl.getBindingUnit(binding).texView !== value) { + bl.bindTextureView(binding, value); + this._layoutDirty = true; } } @@ -181,6 +186,19 @@ export class Pass { if (this._pipelineState) { this._pipelineState.destroy(); this._pipelineState = null; } } + public update() { + const len = this._blocks.length; + for (let i = 0; i < len; i++) { + const block = this._blocks[i]; + if (block.dirty) { + this._buffers[i].update(block.buffer); + } + } + if (this._bindingLayout && this._layoutDirty) { + this._bindingLayout.update(); + } + } + protected createPipelineState(info: PassInfoBase) { if (info.primitive) { this._primitive = info.primitive; } if (info.stage) { this._stage = info.stage; }