Skip to content

Commit

Permalink
fix some defects
Browse files Browse the repository at this point in the history
  • Loading branch information
zxg0622 committed Dec 24, 2019
1 parent 2129f84 commit f66aaf1
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 48 deletions.
12 changes: 8 additions & 4 deletions cocos/core/3d/framework/renderable-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,18 @@ export class RenderableComponent extends Component {
* @param material 材质对象
*/
public setMaterial (material: Material | null, index: number) {
if (material && material instanceof MaterialInstance) {
console.error('Can\'t set a material instance to a sharedMaterial slot');
}
this._materials[index] = material;
if (this._materialInstances[index]) {
if (this._materialInstances[index]!.parent !== material) {
this.getMaterialInstance(index);
this._onMaterialModified(index, material);
if (this._materialInstances[index]!.parent !== this._materials[index]) {
this._materialInstances[index]!.destroy();
this._materialInstances[index] = null;
this._onMaterialModified(index, this._materials[index]);
}
} else {
this._onMaterialModified(index, material);
this._onMaterialModified(index, this._materials[index]);
}
}

Expand Down
13 changes: 9 additions & 4 deletions cocos/core/assets/material.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,13 @@ export class Material extends Asset {
return this._hash;
}

get parent (): Material | null { return null; }
get owner (): RenderableComponent | null { return null; }
get parent (): Material | null {
return null;
}

get owner (): RenderableComponent | null {
return null;
}

constructor () {
super();
Expand Down Expand Up @@ -189,7 +194,7 @@ export class Material extends Asset {
* @param passIdx 要编译的 pass 索引,默认编译所有 pass。
*/
public recompileShaders (overrides: IDefineMap, passIdx?: number) {
console.warn('shaders in material asset cannot be modified at runtime, please instantiate the material first');
console.warn('Material:' + this.name + ' \'s shader cannot be modified at runtime, please use material instance.');
}

/**
Expand All @@ -199,7 +204,7 @@ export class Material extends Asset {
* @param passIdx 要重载的 pass 索引,默认重载所有 pass。
*/
public overridePipelineStates (overrides: PassOverrides, passIdx?: number) {
console.warn('pipeline states in material asset cannot be modified at runtime, please instantiate the material first');
console.warn('Material:' + this.name + ' \'s pipeline states cannot be modified at runtime, please use material instance.');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions cocos/core/renderer/core/material-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ export interface IMaterialInstanceInfo {
*/
export class MaterialInstance extends Material {

get parent () {
get parent (): Material | null {
return this._parent;
}

get owner () {
get owner (): RenderableComponent | null {
return this._owner;
}

Expand Down
31 changes: 27 additions & 4 deletions cocos/core/renderer/core/pass-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@

import { IPassInfo } from '../../assets/effect-asset';
import { GFXBlendState, GFXDepthStencilState, GFXRasterizerState } from '../../gfx/pipeline-state';
import { isBuiltinBinding } from '../../pipeline/define';
import { MaterialInstance } from './material-instance';
import { Pass, PassOverrides } from './pass';
import { IDefineMap } from './pass-utils';
import { IBlock, Pass, PassOverrides } from './pass';
import { assignDefines, IDefineMap } from './pass-utils';

export class PassInstance extends Pass {

Expand All @@ -45,7 +46,27 @@ export class PassInstance extends Pass {
super(parent.device);
this._parent = parent;
this._owner = owner;
this.initialize(this._parent);
this.resetPassInfo(this._parent);
for (const u of this._shaderInfo.blocks) {
if (isBuiltinBinding(u.binding)) {
continue;
}
const block: IBlock = this._blocks[u.binding];
const parentBlock: IBlock = this._parent.blocks[u.binding];
block.view.set(parentBlock.view);
block.dirty = true;
}

for (const u of this._shaderInfo.samplers) {
if (isBuiltinBinding(u.binding)) {
continue;
}
// @ts-ignore 2466
this._textureViews[u.binding] = this._parent._textureViews[u.binding];
// @ts-ignore 2466
this._samplers[u.binding] = this._parent._samplers[u.binding];
}
this.tryCompile();
}

public overridePipelineStates (original: IPassInfo, overrides: PassOverrides): void {
Expand All @@ -59,7 +80,9 @@ export class PassInstance extends Pass {

public tryCompile (defineOverrides?: IDefineMap) {
if (defineOverrides) {
this._defines = Object.assign({}, this._defines, defineOverrides);
if (!assignDefines(this._defines, defineOverrides)) {
return false;
}
}
const res = super.tryCompile();
this._onStateChange();
Expand Down
69 changes: 36 additions & 33 deletions cocos/core/renderer/core/pass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,39 +220,7 @@ export class Pass {
* 根据指定参数初始化当前 pass,shader 会在这一阶段就尝试编译。
*/
public initialize (info: IPassInfoFull) {
this._idxInTech = info.idxInTech;
this._programName = info.program;
this._defines = info.defines;
this._shaderInfo = programLib.getTemplate(info.program);
this._properties = info.properties || this._properties;
// pipeline state
const device = this._device;
Pass.fillinPipelineInfo(this, info);
if (info.stateOverrides) { Pass.fillinPipelineInfo(this, info.stateOverrides); }
this._hash = Pass.getPSOHash(this);

const blocks = this._shaderInfo.blocks;
for (let i = 0; i < blocks.length; i++) {
const { size, binding } = blocks[i];
if (isBuiltinBinding(binding)) { continue; }
// create gfx buffer resource
_bfInfo.size = Math.ceil(size / 16) * 16; // https://bugs.chromium.org/p/chromium/issues/detail?id=988988
this._buffers[binding] = device.createBuffer(_bfInfo);
// non-builtin UBO data pools, note that the effect compiler
// guarantees these bindings to be consecutive, starting from 0
const buffer = new ArrayBuffer(size);
this._blocks[binding] = { buffer, dirty: false, view: new Float32Array(buffer) };
}
// store handles
const directHandleMap = this._handleMap = this._shaderInfo.handleMap;
const indirectHandleMap: Record<string, number> = {};
for (const name in this._properties) {
const prop = this._properties[name];
if (!prop.handleInfo) { continue; }
indirectHandleMap[name] = this.getHandle.apply(this, prop.handleInfo)!;
}
Object.assign(directHandleMap, indirectHandleMap);

this.resetPassInfo(info);
this.resetUBOs();
this.resetTextures();
this.tryCompile();
Expand Down Expand Up @@ -454,6 +422,41 @@ export class Pass {
}
}

public resetPassInfo (info: IPassInfoFull) {
this._idxInTech = info.idxInTech;
this._programName = info.program;
this._defines = info.defines;
this._shaderInfo = programLib.getTemplate(info.program);
this._properties = info.properties || this._properties;
// pipeline state
const device = this._device;
Pass.fillinPipelineInfo(this, info);
if (info.stateOverrides) { Pass.fillinPipelineInfo(this, info.stateOverrides); }
this._hash = Pass.getPSOHash(this);

const blocks = this._shaderInfo.blocks;
for (let i = 0; i < blocks.length; i++) {
const { size, binding } = blocks[i];
if (isBuiltinBinding(binding)) { continue; }
// create gfx buffer resource
_bfInfo.size = Math.ceil(size / 16) * 16; // https://bugs.chromium.org/p/chromium/issues/detail?id=988988
this._buffers[binding] = device.createBuffer(_bfInfo);
// non-builtin UBO data pools, note that the effect compiler
// guarantees these bindings to be consecutive, starting from 0
const buffer = new ArrayBuffer(size);
this._blocks[binding] = { buffer, dirty: false, view: new Float32Array(buffer) };
}
// store handles
const directHandleMap = this._handleMap = this._shaderInfo.handleMap;
const indirectHandleMap: Record<string, number> = {};
for (const name in this._properties) {
const prop = this._properties[name];
if (!prop.handleInfo) { continue; }
indirectHandleMap[name] = this.getHandle.apply(this, prop.handleInfo)!;
}
Object.assign(directHandleMap, indirectHandleMap);
}

/**
* @zh
* 重置所有 UBO 为初始默认值。
Expand Down
2 changes: 1 addition & 1 deletion cocos/core/renderer/ui/ui-material.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class UIMaterial {

this._material = new Material();

this._material.copy(info.material instanceof MaterialInstance ? info.material.parent : info.material);
this._material.copy(info.material instanceof MaterialInstance ? info.material.parent! : info.material);

this._pass = this._material.passes[0];
this._pass.update();
Expand Down

0 comments on commit f66aaf1

Please sign in to comment.