From 00eac3b237fa6ce437b923a0aadeca0fbc23b60e Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Tue, 11 Jun 2019 18:12:21 +0800 Subject: [PATCH] material destroy issue (#4573) * camera clear color * material destroy * fix material destroy issue --- cocos/3d/assets/material.ts | 49 +++++++----- .../batched-skinning-model-component.ts | 4 +- cocos/3d/framework/camera-component.ts | 12 +-- cocos/3d/geom-utils/aabb.ts | 2 +- cocos/renderer/core/pass.ts | 76 +++++++++++++------ cocos/renderer/scene/camera.ts | 4 +- 6 files changed, 94 insertions(+), 53 deletions(-) diff --git a/cocos/3d/assets/material.ts b/cocos/3d/assets/material.ts index b6550f2ba6..10d52035e0 100644 --- a/cocos/3d/assets/material.ts +++ b/cocos/3d/assets/material.ts @@ -155,6 +155,9 @@ export class Material extends Asset { * @param info 初始化材质需要的基本信息 */ public initialize (info: IMaterialInfo) { + if (!this._defines) { this._defines = []; } + if (!this._states) { this._states = []; } + if (!this._props) { this._props = []; } if (info.technique !== undefined) { this._techIdx = info.technique; } if (info.effectAsset) { this._effectAsset = info.effectAsset; } else if (info.effectName) { this._effectAsset = EffectAsset.get(info.effectName); } @@ -165,32 +168,36 @@ export class Material extends Asset { /** * @zh - * 销毁材质,注意这并不会清除材质本身的数据,只会销毁运行时的渲染资源。 + * 彻底销毁材质,注意销毁后无法重新初始化。 + * 如需重新初始化材质,不必先调用 destroy。 */ public destroy () { - if (this._effectAsset) { - if (this._passes) { - for (const pass of this._passes) { - pass.destroy(); - } - this._passes.length = 0; + if (this._passes && this._passes.length) { + for (const pass of this._passes) { + pass.destroy(); } - this._effectAsset = null; } + this._passes = []; + this._effectAsset = null; + + this._props = []; + this._defines = []; + this._states = []; return super.destroy(); } /** * @zh - * 重置材质的所有 uniform 参数数据。 - * @param reinitPass 是否要直接重新创建 pass 数组。 + * 重置材质的所有 uniform 参数数据为 effect 默认初始值。 + * @param clearPasses 是否同时重置当前正在用于渲染的 pass 数组内的信息 */ - public reset (reinitPass = true) { - if (reinitPass) { - this._update(false); - } else { - this._props.length = this._passes.length; - this._props.fill({}); + public resetUniforms (clearPasses = true) { + this._props.length = this._passes.length; + this._props.fill({}); + if (!clearPasses) { return; } + for (const pass of this._passes) { + pass.resetUBOs(); + pass.resetTextures(); } } @@ -338,9 +345,13 @@ export class Material extends Asset { } protected _update (keepProps: boolean = true) { - const asset = this._effectAsset; - if (asset) { - this._passes = createPasses(asset, { + if (this._effectAsset) { + if (this._passes && this._passes.length) { + for (const pass of this._passes) { + pass.destroy(); + } + } + this._passes = createPasses(this._effectAsset, { techIdx: this._techIdx, defines: this._defines, states: this._states, diff --git a/cocos/3d/framework/batched-skinning-model-component.ts b/cocos/3d/framework/batched-skinning-model-component.ts index 2a341fea5c..94a4fb9015 100644 --- a/cocos/3d/framework/batched-skinning-model-component.ts +++ b/cocos/3d/framework/batched-skinning-model-component.ts @@ -117,6 +117,7 @@ export class BatchedSkinningModelComponent extends SkinningModelComponent { public units: SkinningModelUnit[] = []; private _textures: Record = {}; + private _batchMaterial: Material | null = null; @property({ override: true, visible: false }) get mesh () { @@ -143,6 +144,7 @@ export class BatchedSkinningModelComponent extends SkinningModelComponent { } public onLoad () { + this._batchMaterial = this.getSharedMaterial(0); super.onLoad(); this.cook(); } @@ -168,7 +170,7 @@ export class BatchedSkinningModelComponent extends SkinningModelComponent { public cookMaterials () { const mat = this.getMaterial(0); if (!mat) { console.warn('batch material not specified!'); return; } - mat.reset(); this.resizeAtlases(); + mat.copy(this._batchMaterial!); this.resizeAtlases(); const tech = mat.effectAsset!.techniques[mat.technique]; for (let i = 0; i < tech.passes.length; i++) { const pass = tech.passes[i]; diff --git a/cocos/3d/framework/camera-component.ts b/cocos/3d/framework/camera-component.ts index 4f3f54ba4e..61c69286ab 100644 --- a/cocos/3d/framework/camera-component.ts +++ b/cocos/3d/framework/camera-component.ts @@ -25,7 +25,7 @@ import { ray } from '../../3d/geom-utils'; import { Component } from '../../components/component'; import { ccclass, executeInEditMode, menu, property } from '../../core/data/class-decorator'; -import { Enum, Rect, Vec3 } from '../../core/value-types'; +import { Color, Enum, Rect, Vec3 } from '../../core/value-types'; import { color4, toRadian } from '../../core/vmath'; import { GFXClearFlag } from '../../gfx/define'; import { IRenderTargetInfo } from '../../pipeline/render-view'; @@ -69,6 +69,8 @@ const CameraClearFlag = Enum({ DONT_CLEAR: GFXClearFlag.NONE, }); +const c4_1 = color4.create(); + /** * @en The Camera Component * @zh 相机组件 @@ -94,7 +96,7 @@ export class CameraComponent extends Component { @property protected _far = 1000.0; @property - protected _color = cc.color('#334C78'); + protected _color = new Color().fromHEX('#334C78'); @property protected _depth = 1; @property @@ -196,10 +198,10 @@ export class CameraComponent extends Component { } set color (val) { - this._color = val; + this._color.set(val); if (this._camera) { - this._camera.clearColor = - color4.create(val.r / 255, val.g / 255, val.b / 255, val.a / 255); + color4.set(c4_1, val.r / 255, val.g / 255, val.b / 255, val.a / 255); + this._camera.clearColor = c4_1; } } diff --git a/cocos/3d/geom-utils/aabb.ts b/cocos/3d/geom-utils/aabb.ts index 0c6684d8de..916ee0eb85 100644 --- a/cocos/3d/geom-utils/aabb.ts +++ b/cocos/3d/geom-utils/aabb.ts @@ -134,7 +134,7 @@ export default class aabb { * @param m - the transform matrix * @param out - the target shape */ - public transform (m: mat4, pos: vec3 | null, rot: quat | null, scale: vec3 | null, out: aabb){ + public transform (m: mat4, pos: vec3 | null, rot: quat | null, scale: vec3 | null, out: aabb) { if (!out) { out = this; } vec3.transformMat4(out.center, this.center, m); transform_extent_m4(out.halfExtents, this.halfExtents, m); diff --git a/cocos/renderer/core/pass.ts b/cocos/renderer/core/pass.ts index eb130715f6..9ab4ec29a5 100644 --- a/cocos/renderer/core/pass.ts +++ b/cocos/renderer/core/pass.ts @@ -1,6 +1,6 @@ // Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. -import { IPassInfo, IPassStates, IShaderInfo } from '../../3d/assets/effect-asset'; +import { IPassInfo, IPassStates, IPropertyInfo, IShaderInfo } from '../../3d/assets/effect-asset'; import { builtinResMgr } from '../../3d/builtin'; import { TextureBase } from '../../assets/texture-base'; import { mat2, mat3, mat4, vec2, vec3, vec4 } from '../../core/vmath'; @@ -119,6 +119,7 @@ export class Pass { protected _textureViews: Record = {}; protected _resources: IPassResources[] = []; // internal data + protected _phase: number = 0; protected _idxInTech = 0; protected _programName = ''; protected _priority: RenderPriority = RenderPriority.DEFAULT; @@ -135,7 +136,7 @@ export class Pass { protected _blocks: IBlock[] = []; protected _shaderInfo: IShaderInfo = null!; protected _defines: IDefineMap = {}; - protected _phase: number = 0; + protected _properties: Record = {}; // external references protected _device: GFXDevice; protected _renderPass: GFXRenderPass | null = null; @@ -153,16 +154,17 @@ export class Pass { this._idxInTech = info.idxInTech; this._programName = info.program; this._defines = info.curDefs; - const shaderInfo = this._shaderInfo = programLib.getTemplate(info.program); + this._shaderInfo = programLib.getTemplate(info.program); + this._properties = info.properties || this._properties; // pipeline state const device = this._device; this._fillinPipelineInfo(info); this._fillinPipelineInfo(info.states); - for (const u of shaderInfo.blocks) { + for (const u of this._shaderInfo.blocks) { if (u.name.startsWith('CC')) { continue; } // create gfx buffer resource - const buffer = this._buffers[u.binding] = device.createBuffer({ + this._buffers[u.binding] = device.createBuffer({ memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE, size: u.size, usage: GFXBufferUsageBit.UNIFORM | GFXBufferUsageBit.TRANSFER_DST, @@ -177,33 +179,18 @@ export class Pass { // create property-specific buffer views const view = new Float32Array(block.buffer, acc, cur.size / Float32Array.BYTES_PER_ELEMENT); block.views.push(view); - // store handle map, type and initial value - const inf = info.properties && info.properties[cur.name]; - const type = inf && inf.type || cur.type; // property overloads - const givenDefault = inf && inf.value; - const value: number[] = givenDefault ? givenDefault : _type2default[type]; - for (let i = 0; i < cur.count; i++) { view.set(value, i * value.length); } - this._handleMap[cur.name] = genHandle(GFXBindingType.UNIFORM_BUFFER, type, u.binding, idx); + // store handle map + this._handleMap[cur.name] = genHandle(GFXBindingType.UNIFORM_BUFFER, cur.type, u.binding, idx); // proceed the counter return acc + cur.size; }, 0); // === u.size - buffer.update(block.buffer); } - - for (const u of shaderInfo.samplers) { + for (const u of this._shaderInfo.samplers) { this._handleMap[u.name] = genHandle(GFXBindingType.SAMPLER, u.type, u.binding); - const inf = info.properties && info.properties[u.name]; - if (inf && inf.sampler) { this._samplers[u.binding] = samplerLib.getSampler(device, inf.sampler); } - const texName = inf && inf.value ? inf.value + '-texture' : _type2default[u.type]; - const texture = builtinResMgr.get(texName); - if (texture) { - this._textureViews[u.binding] = texture.getGFXTextureView()!; - const samplerInfo = texture.getGFXSamplerInfo(); - if (!this._samplers[u.binding] || samplerInfo.length) { this._samplers[u.binding] = samplerLib.getSampler(device, samplerInfo); } - } - else { console.warn('illegal texture default value ' + texName); } } + this.resetUBOs(); + this.resetTextures(); this.tryCompile(); } @@ -357,6 +344,45 @@ export class Pass { } } + /** + * @zh + * 重置所有 UBO 为初始默认值。 + */ + public resetUBOs () { + for (const u of this._shaderInfo.blocks) { + if (u.name.startsWith('CC')) { continue; } + const block: IBlock = this._blocks[u.binding]; + for (let i = 0; i < u.members.length; i++) { + const cur = u.members[i]; + const view = block.views[i]; + const inf = this._properties[cur.name]; + const givenDefault = inf && inf.value; + const value: number[] = givenDefault ? givenDefault : _type2default[cur.type]; + for (let j = 0; j < cur.count; j++) { view.set(value, j * value.length); } + } + block.dirty = true; + } + } + + /** + * @zh + * 重置所有 texture 和 sampler 为初始默认值。 + */ + public resetTextures () { + const device = this._device; + for (const u of this._shaderInfo.samplers) { + const inf = this._properties[u.name]; + if (inf && inf.sampler) { this._samplers[u.binding] = samplerLib.getSampler(device, inf.sampler); } + const texName = inf && inf.value ? inf.value + '-texture' : _type2default[u.type]; + const texture = builtinResMgr.get(texName); + if (texture) { + this._textureViews[u.binding] = texture.getGFXTextureView()!; + const samplerInfo = texture.getGFXSamplerInfo(); + if (!this._samplers[u.binding] || samplerInfo.length) { this._samplers[u.binding] = samplerLib.getSampler(device, samplerInfo); } + } else { console.warn('illegal texture default value ' + texName); } + } + } + /** * @zh * 尝试编译 shader 并获取相关资源引用。 diff --git a/cocos/renderer/scene/camera.ts b/cocos/renderer/scene/camera.ts index e78ffbe902..197efeb9c2 100644 --- a/cocos/renderer/scene/camera.ts +++ b/cocos/renderer/scene/camera.ts @@ -1,6 +1,6 @@ import { frustum, ray } from '../../3d/geom-utils'; import { Mat4, Rect, Vec3 } from '../../core/value-types'; -import { lerp, mat4, toRadian, vec3 } from '../../core/vmath'; +import { lerp, mat4, toRadian, vec3, color4 } from '../../core/vmath'; import { GFXClearFlag, IGFXColor } from '../../gfx/define'; import { RenderView } from '../../pipeline/render-view'; import { Node } from '../../scene-graph/node'; @@ -317,7 +317,7 @@ export class Camera { } set clearColor (val) { - this._clearColor = val; + color4.copy(this._clearColor, val); } get clearColor () {