shader precompile interface (#4876)

This commit is contained in:
Yun Hsiao Wu
2019-07-15 10:55:47 +08:00
committed by Weiliang Wang
parent 8481d7b9bf
commit b9d5c1732f
2 changed files with 34 additions and 1 deletions
+33
View File
@@ -29,10 +29,12 @@
import { Asset } from '../../assets/asset';
import { ccclass, property } from '../../core/data/class-decorator';
import { Root } from '../../core/root';
import { GFXDynamicState, GFXPrimitiveMode } from '../../gfx/define';
import { GFXBlendState, GFXDepthStencilState, GFXRasterizerState } from '../../gfx/pipeline-state';
import { GFXUniformBlock, GFXUniformSampler } from '../../gfx/shader';
import { RenderPassStage } from '../../pipeline/define';
import { IDefineMap } from '../../renderer/core/pass';
import { programLib } from '../../renderer/core/program-lib';
export interface IPropertyInfo {
@@ -93,6 +95,9 @@ export interface IShaderInfo {
samplers: ISamplerInfo[];
dependencies: Record<string, string>;
}
export interface IPreCompileInfo {
[name: string]: boolean[] | number[] | string[];
}
const effects: Record<string, EffectAsset> = {};
@@ -102,11 +107,13 @@ const effects: Record<string, EffectAsset> = {};
*/
@ccclass('cc.EffectAsset')
export class EffectAsset extends Asset {
/**
* @zh
* 将指定 effect 注册到全局管理器。
*/
public static register (asset: EffectAsset) { effects[asset.name] = asset; }
/**
* @zh
* 将指定 effect 从全局管理器移除。
@@ -120,6 +127,7 @@ export class EffectAsset extends Asset {
}
}
}
/**
* @zh
* 获取指定名字的 effect 资源。
@@ -133,6 +141,7 @@ export class EffectAsset extends Asset {
}
return null;
}
/**
* @zh
* 获取所有已注册的 effect 资源。
@@ -154,12 +163,20 @@ export class EffectAsset extends Asset {
@property
public shaders: IShaderInfo[] = [];
/**
* @zh
* 每个 shader 需要预编译的宏定义组合。
*/
@property
public combinations: IPreCompileInfo[] = [];
/**
* @zh
* 通过 Loader 加载完成时的回调,将自动注册 effect 资源。
*/
public onLoaded () {
this.shaders.forEach((s) => programLib.define(s));
cc.game.once(cc.Game.EVENT_ENGINE_INITED, this._precompile, this);
EffectAsset.register(this);
// replace null with undefined
this.techniques.forEach((t) => t.passes.forEach((p) => {
@@ -170,6 +187,22 @@ export class EffectAsset extends Asset {
}
}));
}
protected _precompile () {
const root = cc.director.root as Root;
for (let i = 0; i < this.shaders.length; i++) {
const shader = this.shaders[i];
const combination = this.combinations[i];
if (!combination) { continue; }
Object.keys(combination).reduce((out, name) => out.reduce((acc, cur) => {
const choices = combination[name];
const next = [cur].concat([...Array(choices.length - 1)].map(() => Object.assign({}, cur)));
next.forEach((defines, idx) => defines[name] = choices[idx]);
return acc.concat(next);
}, [] as IDefineMap[]), [{}] as IDefineMap[]).forEach(
(defines) => programLib.getGFXShader(root.device, shader.name, defines, root.pipeline));
}
}
}
cc.EffectAsset = EffectAsset;
+1 -1
View File
@@ -96,7 +96,7 @@ class SamplerLib {
gfxInfo.mipLODBias = info[SamplerInfoIndex.mipLODBias];
if (info.length >= SamplerInfoIndex.total) {
const ofs = SamplerInfoIndex.borderColor;
gfxInfo.borderColor = { r: info[ofs], g: info[ofs + 1], b: info[ofs + 2], a: info[ofs + 3] };
gfxInfo.borderColor = { r: info[ofs]!, g: info[ofs + 1]!, b: info[ofs + 2]!, a: info[ofs + 3]! };
}
}
const sampler = this._cache[hash] = device.createSampler(gfxInfo);