mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 07:42:38 +08:00
fix conflict
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
import { Asset } from '../../assets/asset';
|
||||
import { ccclass, property } from '../../core/data/class-decorator';
|
||||
import { PassInfoBase } from '../../renderer/core/pass';
|
||||
import { IPassInfoBase } from '../../renderer/core/pass';
|
||||
|
||||
export interface TechniqueInfo {
|
||||
passes: PassInfoBase[];
|
||||
export interface ITechniqueInfo {
|
||||
passes: IPassInfoBase[];
|
||||
queue?: number;
|
||||
priority?: number;
|
||||
lod?: number;
|
||||
}
|
||||
export interface PropertyInfo {
|
||||
export interface IPropertyInfo {
|
||||
type?: number;
|
||||
value?: number | number[] | null;
|
||||
displayName?: string;
|
||||
@@ -16,39 +16,39 @@ export interface PropertyInfo {
|
||||
pass?: number;
|
||||
}
|
||||
|
||||
export interface BlockMember {
|
||||
export interface IBlockMember {
|
||||
name: string;
|
||||
type: number;
|
||||
count: number;
|
||||
size: number;
|
||||
}
|
||||
export interface BlockInfo {
|
||||
export interface IBlockInfo {
|
||||
name: string;
|
||||
binding: number;
|
||||
defines: string[];
|
||||
members: BlockMember[];
|
||||
members: IBlockMember[];
|
||||
size: number;
|
||||
}
|
||||
export interface SamplerInfo {
|
||||
export interface ISamplerInfo {
|
||||
name: string;
|
||||
binding: number;
|
||||
defines: string[];
|
||||
type: number;
|
||||
count: number;
|
||||
}
|
||||
export interface DefineInfo {
|
||||
export interface IDefineInfo {
|
||||
name: string;
|
||||
type: string;
|
||||
range?: number[];
|
||||
defines: string[];
|
||||
}
|
||||
export interface ShaderInfo {
|
||||
export interface IShaderInfo {
|
||||
name: string;
|
||||
vert: string;
|
||||
frag: string;
|
||||
defines: DefineInfo[];
|
||||
blocks: BlockInfo[];
|
||||
samplers: SamplerInfo[];
|
||||
defines: IDefineInfo[];
|
||||
blocks: IBlockInfo[];
|
||||
samplers: ISamplerInfo[];
|
||||
dependencies: Record<string, string>;
|
||||
}
|
||||
|
||||
@@ -56,8 +56,8 @@ const effects: Record<string, EffectAsset> = {};
|
||||
|
||||
@ccclass('cc.EffectAsset')
|
||||
export class EffectAsset extends Asset {
|
||||
public static register(asset: EffectAsset) { effects[asset.name] = asset; }
|
||||
public static remove(name: string) {
|
||||
public static register (asset: EffectAsset) { effects[asset.name] = asset; }
|
||||
public static remove (name: string) {
|
||||
if (effects[name]) { delete effects[name]; return; }
|
||||
for (const n in effects) {
|
||||
if (effects[n]._uuid === name) {
|
||||
@@ -66,7 +66,7 @@ export class EffectAsset extends Asset {
|
||||
}
|
||||
}
|
||||
}
|
||||
public static get(name: string) {
|
||||
public static get (name: string) {
|
||||
if (effects[name]) { return effects[name]; }
|
||||
for (const n in effects) {
|
||||
if (effects[n]._uuid === name) {
|
||||
@@ -75,19 +75,19 @@ export class EffectAsset extends Asset {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public static getAll() { return effects; }
|
||||
public static getAll () { return effects; }
|
||||
protected static _effects: Record<string, EffectAsset> = {};
|
||||
|
||||
@property
|
||||
public techniques: TechniqueInfo[] = [];
|
||||
public techniques: ITechniqueInfo[] = [];
|
||||
|
||||
@property
|
||||
public properties: Record<string, PropertyInfo> = {};
|
||||
public properties: Record<string, IPropertyInfo> = {};
|
||||
|
||||
@property
|
||||
public shaders: ShaderInfo[] = [];
|
||||
public shaders: IShaderInfo[] = [];
|
||||
|
||||
public onLoaded() {
|
||||
public onLoaded () {
|
||||
const lib = cc.game._programLib;
|
||||
this.shaders.forEach((s) => lib.define(s));
|
||||
EffectAsset.register(this);
|
||||
|
||||
+53
-62
@@ -23,15 +23,15 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
import { Asset } from '../../assets/asset';
|
||||
import TextureBase from '../../assets/texture-base';
|
||||
import { ccclass, property } from '../../core/data/class-decorator';
|
||||
import { GFXBindingType } from '../../gfx/define';
|
||||
import { Effect } from '../../renderer/core/effect';
|
||||
import { Pass } from '../../renderer/core/pass';
|
||||
import { EffectAsset } from './effect-asset';
|
||||
|
||||
@ccclass('cc.Material')
|
||||
export class Material extends Asset {
|
||||
public static getInstantiatedMaterial(mat: Material, rndCom: any) {
|
||||
public static getInstantiatedMaterial (mat: Material, rndCom: any) {
|
||||
if (mat._owner === rndCom) {
|
||||
return mat;
|
||||
} else {
|
||||
@@ -51,110 +51,94 @@ export class Material extends Asset {
|
||||
protected _defines: Record<string, number | boolean> = {};
|
||||
@property
|
||||
protected _props: Array<Record<string, any>> = [];
|
||||
@property
|
||||
protected _textures: Array<Record<string, any>> = [];
|
||||
|
||||
protected _passes: Pass[] = [];
|
||||
protected _owner: any = null;
|
||||
|
||||
@property
|
||||
set effectAsset(val: EffectAsset | null) {
|
||||
set effectAsset (val: EffectAsset | null) {
|
||||
if (!val || this.effectName !== val.name) { this.update(val, false); }
|
||||
}
|
||||
get effectAsset() {
|
||||
get effectAsset () {
|
||||
return this._effectAsset;
|
||||
}
|
||||
// helper setter
|
||||
@property
|
||||
set effectName(val) {
|
||||
set effectName (val) {
|
||||
if (this.effectName !== val) { this.update(val, false); }
|
||||
}
|
||||
get effectName() {
|
||||
get effectName () {
|
||||
return this._effectAsset ? this._effectAsset.name : '';
|
||||
}
|
||||
|
||||
@property
|
||||
set technique(val: number) {
|
||||
set technique (val: number) {
|
||||
this._techIdx = val;
|
||||
}
|
||||
get technique() {
|
||||
get technique () {
|
||||
return this._techIdx;
|
||||
}
|
||||
|
||||
get passes() {
|
||||
get passes () {
|
||||
return this._passes;
|
||||
}
|
||||
|
||||
public setDefines(defines: Record<string, number | boolean>) {
|
||||
public setDefines (defines: Record<string, number | boolean>) {
|
||||
this._defines = defines;
|
||||
this.update();
|
||||
}
|
||||
|
||||
public setProperty(name: string, val: any, passIdx?: number) {
|
||||
/**
|
||||
* Convenient setter provided for quick material setup.
|
||||
* pass.setUniform should be used instead
|
||||
* if you need to do per-frame property update.
|
||||
*/
|
||||
public setProperty (name: string, val: any, passIdx?: number) {
|
||||
if (!val) { console.warn(`illegal property value: ${val}.`); return; }
|
||||
if (passIdx === undefined) { // set property for all applicable passes
|
||||
const passes = this._passes;
|
||||
const len = passes.length;
|
||||
for (let i = 0; i < len; i++) {
|
||||
const handle = passes[i].getHandleFromName(name);
|
||||
if (handle === undefined) { continue; }
|
||||
passes[i].setUniform(handle, val);
|
||||
this._props[i][name] = val;
|
||||
const pass = passes[i];
|
||||
if (this._uploadProperty(pass, name, val)) {
|
||||
this._props[i][name] = val;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (CC_DEBUG && passIdx >= this._passes.length) { console.warn(`illegal pass index: ${passIdx}.`); return; }
|
||||
if (passIdx >= this._passes.length) { console.warn(`illegal pass index: ${passIdx}.`); return; }
|
||||
const pass = this._passes[passIdx];
|
||||
const handle = pass.getHandleFromName(name);
|
||||
if (CC_DEBUG && handle === undefined) { console.warn(`illegal property name: ${name}.`); return; }
|
||||
pass.setUniform(handle, val);
|
||||
this._props[passIdx][name] = val;
|
||||
if (this._uploadProperty(pass, name, val)) {
|
||||
this._props[passIdx][name] = val;
|
||||
} else {
|
||||
console.warn(`illegal property name: ${name}.`); return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public setTexture(name: string, val: TextureBase, passIdx?: number) {
|
||||
if (passIdx === undefined) { // set property for all applicable passes
|
||||
const passes = this._passes;
|
||||
const len = passes.length;
|
||||
for (let i = 0; i < len; i++) {
|
||||
const binding = passes[i].getBindingFromName(name);
|
||||
if (binding < 0) { continue; }
|
||||
passes[i].bindTextureView(binding, val._texView);
|
||||
this._textures[i][name] = val;
|
||||
}
|
||||
} else {
|
||||
if (CC_DEBUG && passIdx >= this._passes.length) { console.warn(`illegal pass index: ${passIdx}.`); return; }
|
||||
const pass = this._passes[passIdx];
|
||||
const binding = pass.getBindingFromName(name);
|
||||
if (CC_DEBUG && binding < 0) { console.warn(`illegal texture name: ${name}.`); return; }
|
||||
pass.bindTextureView(binding, val._texView);
|
||||
this._textures[passIdx][name] = val;
|
||||
}
|
||||
}
|
||||
|
||||
public copy(mat: Material) {
|
||||
this._props.length = this._textures.length = mat._props.length;
|
||||
public copy (mat: Material) {
|
||||
this._props.length = mat._props.length;
|
||||
for (let i = 0; i < mat._props.length; i++) {
|
||||
Object.assign(this._props[i], mat._props[i]);
|
||||
Object.assign(this._textures[i], mat._textures[i]);
|
||||
}
|
||||
Object.assign(this._defines, mat._defines);
|
||||
this.update(mat.effectAsset);
|
||||
this._uuid = mat._uuid;
|
||||
}
|
||||
|
||||
public onLoaded() {
|
||||
public onLoaded () {
|
||||
this.update();
|
||||
}
|
||||
|
||||
public update(val: EffectAsset | string | null = this._effectAsset, keepProps: boolean = true) {
|
||||
public update (asset: EffectAsset | string | null = this._effectAsset, keepProps: boolean = true) {
|
||||
// get effect asset
|
||||
if (typeof val === 'string') {
|
||||
this._effectAsset = EffectAsset.get(val);
|
||||
if (typeof asset === 'string') {
|
||||
this._effectAsset = EffectAsset.get(asset);
|
||||
if (!this._effectAsset) {
|
||||
console.warn(`no effect named '${val}' found`);
|
||||
console.warn(`no effect named '${asset}' found`);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
this._effectAsset = val;
|
||||
this._effectAsset = asset;
|
||||
}
|
||||
if (!this._effectAsset) { return; }
|
||||
// create passes
|
||||
@@ -163,28 +147,35 @@ export class Material extends Asset {
|
||||
defines: this._defines,
|
||||
});
|
||||
// handle property values
|
||||
this._props.length = this._textures.length = this._passes.length;
|
||||
this._props.length = this._passes.length;
|
||||
if (keepProps) {
|
||||
this._passes.forEach((pass, i) => {
|
||||
let props = this._props[i];
|
||||
if (!props) { props = this._props[i] = {}; }
|
||||
for (const p of Object.keys(props)) {
|
||||
const handle = pass.getHandleFromName(p);
|
||||
if (handle === undefined) { continue; }
|
||||
pass.setUniform(handle, props[p]);
|
||||
}
|
||||
let textures = this._textures[i];
|
||||
if (!textures) { textures = this._textures[i] = {}; }
|
||||
for (const t of Object.keys(textures)) {
|
||||
const binding = pass.getBindingFromName(t);
|
||||
if (binding === undefined) { continue; }
|
||||
pass.bindTextureView(binding, textures[t]._texView);
|
||||
const val = props[p];
|
||||
if (!val) { continue; }
|
||||
this._uploadProperty(pass, p, props[p]);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
this._props.fill({});
|
||||
}
|
||||
}
|
||||
|
||||
protected _uploadProperty (pass: Pass, name: string, val: any) {
|
||||
const handle = pass.getHandle(name);
|
||||
if (handle === undefined) { return false; }
|
||||
const bindingType = Pass.getBindingTypeFromHandle(handle);
|
||||
if (bindingType === GFXBindingType.UNIFORM_BUFFER) {
|
||||
pass.setUniform(handle, val);
|
||||
} else if (bindingType === GFXBindingType.SAMPLER) {
|
||||
const tv = val.getGFXTextureView();
|
||||
if (!tv) { console.warn('texture asset incomplete!'); return; }
|
||||
pass.bindTextureView(Pass.getBindingFromHandle(handle), tv);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
cc.Material = Material;
|
||||
|
||||
Vendored
+18
-2
@@ -6,8 +6,24 @@ export default [
|
||||
"shaders": [
|
||||
{
|
||||
"name": "f4c45e6cc7794309d86202e6ec9036a3871fd1ec167a9e02eb1e1574",
|
||||
"vert": "\n attribute vec2 a_position;\n attribute vec2 a_texCoord;\n varying vec2 v_texCoord;\n void main() {\n gl_Position = vec4(a_position, 0.0, 1.0);\n v_texCoord = a_texCoord;\n }\n",
|
||||
"frag": "\n varying vec2 v_texCoord;\n uniform vec4 u_color;\n uniform sampler2D u_sampler;\n void main() {\n \n gl_FragColor = texture2D(u_sampler, v_texCoord);\n gl_FragColor.r = u_color.r;\n }\n",
|
||||
"vert": `
|
||||
attribute vec2 a_position;
|
||||
attribute vec2 a_texCoord;
|
||||
varying vec2 v_texCoord;
|
||||
void main() {
|
||||
gl_Position = vec4(a_position, 0.0, 1.0);
|
||||
v_texCoord = a_texCoord;
|
||||
}
|
||||
`,
|
||||
"frag": `
|
||||
varying vec2 v_texCoord;
|
||||
uniform vec4 u_color;
|
||||
uniform sampler2D u_sampler;
|
||||
void main() {
|
||||
gl_FragColor = texture2D(u_sampler, v_texCoord);
|
||||
gl_FragColor.r = u_color.r;
|
||||
}
|
||||
`,
|
||||
"defines": [],
|
||||
"blocks": [
|
||||
{"name": "Constants", "size": 16, "defines": [], "binding": 0, "members": [
|
||||
|
||||
@@ -1,20 +1,19 @@
|
||||
import { RenderFlow, RenderFlowInfo } from "../render-flow";
|
||||
import { RenderPipeline } from "../render-pipeline";
|
||||
// import { TestStage } from "./test-stage";
|
||||
import { TestMaterialStage } from "./test-material-stage";
|
||||
import { TestStage } from "./test-stage";
|
||||
import { RenderFlow, RenderFlowInfo } from '../render-flow';
|
||||
import { RenderPipeline } from '../render-pipeline';
|
||||
import { TestMaterialStage } from './test-material-stage';
|
||||
import { TestStage } from './test-stage';
|
||||
|
||||
export enum ForwardStagePriority {
|
||||
FORWARD = 0,
|
||||
};
|
||||
}
|
||||
|
||||
export class ForwardFlow extends RenderFlow {
|
||||
|
||||
constructor(pipeline: RenderPipeline) {
|
||||
constructor (pipeline: RenderPipeline) {
|
||||
super(pipeline);
|
||||
}
|
||||
|
||||
public initialize(info: RenderFlowInfo): boolean {
|
||||
public initialize (info: RenderFlowInfo): boolean {
|
||||
|
||||
if (info.name !== undefined) {
|
||||
this._name = info.name;
|
||||
@@ -22,37 +21,39 @@ export class ForwardFlow extends RenderFlow {
|
||||
|
||||
this._priority = info.priority;
|
||||
|
||||
let mainWindow = this._pipeline.root.mainWindow;
|
||||
const mainWindow = this._pipeline.root.mainWindow;
|
||||
if (!mainWindow || !mainWindow.framebuffer) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
/* *
|
||||
this.createStage<ForwardStage>(ForwardStage, {
|
||||
name: "ForwardStage",
|
||||
priority: ForwardStagePriority.FORWARD,
|
||||
framebuffer: mainWindow.framebuffer,
|
||||
});
|
||||
*/
|
||||
/* */
|
||||
|
||||
/*
|
||||
/* */
|
||||
this.createStage<TestMaterialStage>(TestMaterialStage, {
|
||||
name: "TestMaterialStage",
|
||||
name: 'TestMaterialStage',
|
||||
priority: ForwardStagePriority.FORWARD,
|
||||
framebuffer: mainWindow.framebuffer,
|
||||
});
|
||||
*/
|
||||
/* */
|
||||
|
||||
/* *
|
||||
this.createStage<TestStage>(TestStage, {
|
||||
name: "TestStage",
|
||||
name: 'TestStage',
|
||||
priority: ForwardStagePriority.FORWARD,
|
||||
framebuffer: mainWindow.framebuffer,
|
||||
});
|
||||
/* */
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public destroy(): void {
|
||||
public destroy (): void {
|
||||
this.destroyStages();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -16,11 +16,11 @@ export class TestMaterialStage extends RenderStage {
|
||||
private _texView: GFXTextureView | null = null;
|
||||
private _material: Material = new Material();
|
||||
|
||||
constructor(flow: RenderFlow) {
|
||||
constructor (flow: RenderFlow) {
|
||||
super(flow);
|
||||
}
|
||||
|
||||
public initialize(info: RenderStageInfo): boolean {
|
||||
public initialize (info: RenderStageInfo): boolean {
|
||||
|
||||
if (info.name !== undefined) {
|
||||
this._name = info.name;
|
||||
@@ -40,7 +40,7 @@ export class TestMaterialStage extends RenderStage {
|
||||
// load texture
|
||||
const imgElms = this._device.canvas.getElementsByTagName('img');
|
||||
if (imgElms.length) {
|
||||
if (!this.loadTexture(<HTMLImageElement>imgElms[0])) {
|
||||
if (!this.loadTexture(imgElms[0] as HTMLImageElement)) {
|
||||
console.error('Load texture failed.');
|
||||
return false;
|
||||
}
|
||||
@@ -51,7 +51,7 @@ export class TestMaterialStage extends RenderStage {
|
||||
return true;
|
||||
}
|
||||
|
||||
public destroy() {
|
||||
public destroy () {
|
||||
|
||||
if (this._texView) {
|
||||
this._texView.destroy();
|
||||
@@ -69,41 +69,50 @@ export class TestMaterialStage extends RenderStage {
|
||||
}
|
||||
}
|
||||
|
||||
public render(view: RenderView) {
|
||||
public render (view: RenderView) {
|
||||
const mtl = this._material;
|
||||
// @ts-ignore
|
||||
if (!this._material.inited) {
|
||||
if (!mtl.inited) {
|
||||
// material
|
||||
this._material.effectName = 'test'; // parsed-effect file is embedded in cocos/3d/builtin/effects.js
|
||||
const pass = this._material.passes[0];
|
||||
const texBinding = pass.getBindingFromName('u_sampler');
|
||||
pass.bindTextureView(texBinding, <GFXTextureView>this._texView);
|
||||
const colorHandle = pass.getHandleFromName('u_color');
|
||||
mtl.effectName = 'test'; // parsed-effect file is embedded in cocos/3d/builtin/effects.js
|
||||
const pass = mtl.passes[0];
|
||||
/* *
|
||||
const texBinding = pass.getBinding('u_sampler');
|
||||
pass.bindTextureView(texBinding, this._texView as GFXTextureView);
|
||||
const colorHandle = pass.getHandle('u_color');
|
||||
pass.setUniform(colorHandle, cc.color('#ffffff'));
|
||||
/* */
|
||||
mtl.setProperty('u_sampler', {
|
||||
tv: this._texView,
|
||||
getGFXTextureView () { return this.tv; },
|
||||
});
|
||||
mtl.setProperty('u_color', cc.color('#ffffff'));
|
||||
/* */
|
||||
pass.update();
|
||||
// @ts-ignore
|
||||
this._material.inited = true;
|
||||
mtl.inited = true;
|
||||
}
|
||||
|
||||
const cmdBuff = <GFXCommandBuffer>this._cmdBuff;
|
||||
const cmdBuff = this._cmdBuff as GFXCommandBuffer;
|
||||
this._renderArea.width = view.width;
|
||||
this._renderArea.height = view.height;
|
||||
|
||||
cmdBuff.begin();
|
||||
cmdBuff.beginRenderPass(<GFXFramebuffer>this._framebuffer, this._renderArea, this._clearColors,
|
||||
cmdBuff.beginRenderPass( this._framebuffer as GFXFramebuffer, this._renderArea, this._clearColors,
|
||||
this._clearDepth, this._clearStencil);
|
||||
|
||||
cmdBuff.bindPipelineState(this._material.passes[0].pipelineState);
|
||||
cmdBuff.bindBindingLayout(this._material.passes[0].bindingLayout);
|
||||
|
||||
cmdBuff.bindInputAssembler(<GFXInputAssembler>this._pipeline.quadIA);
|
||||
cmdBuff.draw(<GFXInputAssembler>this._pipeline.quadIA);
|
||||
cmdBuff.bindInputAssembler( this._pipeline.quadIA as GFXInputAssembler);
|
||||
cmdBuff.draw( this._pipeline.quadIA as GFXInputAssembler);
|
||||
cmdBuff.endRenderPass();
|
||||
cmdBuff.end();
|
||||
|
||||
this._device.queue.submit([cmdBuff]);
|
||||
}
|
||||
|
||||
private loadTexture(image: HTMLImageElement): boolean {
|
||||
private loadTexture (image: HTMLImageElement): boolean {
|
||||
this._texture = this._device.createTexture({
|
||||
type: GFXTextureType.TEX2D,
|
||||
usage: GFXTextureUsageBit.SAMPLED | GFXTextureUsageBit.TRANSFER_DST,
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
import { BlockMember, EffectAsset, PropertyInfo, SamplerInfo, ShaderInfo } from '../../3d/assets/effect-asset';
|
||||
import { EffectAsset, IBlockMember, IPropertyInfo, ISamplerInfo, IShaderInfo } from '../../3d/assets/effect-asset';
|
||||
import { CCObject } from '../../core/data';
|
||||
import { Color, Mat4, Vec2, Vec3, Vec4 } from '../../core/value-types';
|
||||
import { GFXType } from '../../gfx/define';
|
||||
import { RenderPassStage } from '../../pipeline/render-pipeline';
|
||||
import Texture2D from '../gfx/texture-2d';
|
||||
import TextureCube from '../gfx/texture-cube';
|
||||
import { Pass, PassInfo } from './pass';
|
||||
import { IPassInfo, Pass } from './pass';
|
||||
|
||||
const _typeMap = {
|
||||
[GFXType.INT]: 'Number',
|
||||
@@ -22,23 +22,23 @@ const _typeMap = {
|
||||
[GFXType.MAT4]: Mat4,
|
||||
[GFXType.SAMPLER2D]: Texture2D,
|
||||
[GFXType.SAMPLER_CUBE]: TextureCube,
|
||||
number: 'Number',
|
||||
boolean: 'Boolean',
|
||||
default: CCObject,
|
||||
number: 'Number',
|
||||
};
|
||||
const getInstanceType = (t: number | string) => _typeMap[t] || _typeMap.default;
|
||||
|
||||
export interface PropertyMap { [name: string]: { type: number, value: any }; }
|
||||
export interface DefineMap { [name: string]: number | boolean; }
|
||||
export interface EffectInfo {
|
||||
export interface IPropertyMap { [name: string]: { type: number, value: any }; }
|
||||
export interface IDefineMap { [name: string]: number | boolean; }
|
||||
export interface IEffectInfo {
|
||||
techIdx?: number;
|
||||
defines?: DefineMap;
|
||||
defines?: IDefineMap;
|
||||
}
|
||||
|
||||
type DefaultValue = number | boolean | number[] | null;
|
||||
const _ctorMap = {
|
||||
Number: (v?: DefaultValue) => v || 0,
|
||||
Boolean: (v?: DefaultValue) => v || false,
|
||||
Number: (v?: DefaultValue) => v || 0,
|
||||
[Vec2]: (v?: DefaultValue) => Array.isArray(v) ? new Vec2(v[0], v[1]) : new Vec2(),
|
||||
[Vec3]: (v?: DefaultValue) => Array.isArray(v) ? new Vec3(v[0], v[1], v[2]) : new Vec3(),
|
||||
[Vec4]: (v?: DefaultValue) => Array.isArray(v) ? new Vec4(v[0], v[1], v[2], v[3]) : new Vec4(),
|
||||
@@ -48,7 +48,7 @@ const _ctorMap = {
|
||||
v[0], v[1], v[2], v[3],
|
||||
v[4], v[5], v[6], v[7],
|
||||
v[8], v[9], v[10], v[11],
|
||||
v[12], v[13], v[14], v[15]
|
||||
v[12], v[13], v[14], v[15],
|
||||
) : new Mat4(),
|
||||
[Texture2D]: () => null,
|
||||
[TextureCube]: () => null,
|
||||
@@ -57,7 +57,7 @@ const _ctorMap = {
|
||||
const getInstanceCtor = (t: number | string) => _ctorMap[getInstanceType(t)];
|
||||
|
||||
const getInvolvedPrograms = (effect: EffectAsset, techIdx?: number) => {
|
||||
const programs: Record<string, ShaderInfo> = {};
|
||||
const programs: Record<string, IShaderInfo> = {};
|
||||
const lib = cc.game._programLib;
|
||||
if (techIdx) {
|
||||
effect.techniques[techIdx].passes.forEach((pass) => {
|
||||
@@ -73,7 +73,7 @@ const getInvolvedPrograms = (effect: EffectAsset, techIdx?: number) => {
|
||||
return programs;
|
||||
};
|
||||
|
||||
interface ExtractedPropInfo {
|
||||
interface IExtractedPropInfo {
|
||||
type: number;
|
||||
instanceType: object | string;
|
||||
value: number | boolean | object;
|
||||
@@ -83,26 +83,26 @@ interface ExtractedPropInfo {
|
||||
pass: number;
|
||||
}
|
||||
const parseProperties = (() => {
|
||||
interface UniformInfo { type: number; defines: string[]; }
|
||||
function genPropInfo(propInfo: PropertyInfo, uniformInfo: UniformInfo, tech: number, pass: number)
|
||||
: ExtractedPropInfo {
|
||||
interface IUniformInfo { type: number; defines: string[]; }
|
||||
function genPropInfo (propInfo: IPropertyInfo, uniformInfo: IUniformInfo, tech: number, pass: number)
|
||||
: IExtractedPropInfo {
|
||||
const type = propInfo.type || uniformInfo.type;
|
||||
return {
|
||||
type, tech, pass,
|
||||
instanceType: getInstanceType(type),
|
||||
value: getInstanceCtor(type)(propInfo.value),
|
||||
displayName: propInfo.displayName,
|
||||
defines: uniformInfo.defines,
|
||||
displayName: propInfo.displayName,
|
||||
instanceType: getInstanceType(type),
|
||||
pass, tech, type,
|
||||
value: getInstanceCtor(type)(propInfo.value),
|
||||
};
|
||||
}
|
||||
function findUniformInfo(program: ShaderInfo, name: string) {
|
||||
function findUniformInfo (program: IShaderInfo, name: string) {
|
||||
for (const b of program.blocks) {
|
||||
const i = b.members.find((u) => u.name === name);
|
||||
if (i) { return Object.assign({ defines: b.defines }, i); }
|
||||
}
|
||||
return program.samplers.find((u) => u.name === name);
|
||||
}
|
||||
function findPassIdx(effect: EffectAsset, programName: string) { // just a heuristic
|
||||
function findPassIdx (effect: EffectAsset, programName: string) { // just a heuristic
|
||||
effect.techniques.forEach((tech, i) => {
|
||||
tech.passes.forEach((pass, j) => {
|
||||
if (pass.program === programName) {
|
||||
@@ -112,11 +112,11 @@ const parseProperties = (() => {
|
||||
});
|
||||
return [ 0, 0 ];
|
||||
}
|
||||
return (effect: EffectAsset, programs: Record<string, ShaderInfo>) => {
|
||||
const props: Record<string, ExtractedPropInfo> = {};
|
||||
return (effect: EffectAsset, programs: Record<string, IShaderInfo>) => {
|
||||
const props: Record<string, IExtractedPropInfo> = {};
|
||||
for (const prop of Object.keys(effect.properties)) {
|
||||
const propInfo = effect.properties[prop];
|
||||
let uniformInfo: UniformInfo | undefined;
|
||||
let uniformInfo: IUniformInfo | undefined;
|
||||
let techIdx = 0;
|
||||
let passIdx = 0;
|
||||
// always try getting the type from shaders first
|
||||
@@ -141,26 +141,26 @@ const parseProperties = (() => {
|
||||
};
|
||||
})();
|
||||
|
||||
const traverseUniforms = (shaderInfo: ShaderInfo, fn: (u: BlockMember | SamplerInfo) => any) => {
|
||||
const traverseUniforms = (shaderInfo: IShaderInfo, fn: (u: IBlockMember | ISamplerInfo) => any) => {
|
||||
shaderInfo.blocks.forEach((b) => b.members.forEach(fn));
|
||||
shaderInfo.samplers.forEach(fn);
|
||||
};
|
||||
|
||||
export class Effect {
|
||||
public static parseEffect(effect: EffectAsset, info?: EffectInfo) {
|
||||
public static parseEffect (effect: EffectAsset, info?: IEffectInfo) {
|
||||
// techniques
|
||||
const { techIdx, defines } = info || <EffectInfo>{};
|
||||
const { techIdx, defines } = info || {} as IEffectInfo;
|
||||
const programs = getInvolvedPrograms(effect, techIdx || 0);
|
||||
const tech = effect.techniques[techIdx || 0];
|
||||
const passNum = tech.passes.length;
|
||||
const passes: Pass[] = new Array(passNum);
|
||||
const props = parseProperties(effect, programs);
|
||||
for (let k = 0; k < passNum; ++k) {
|
||||
const passInfo = <PassInfo>tech.passes[k];
|
||||
const uniforms: PropertyMap = passInfo.uniforms = {};
|
||||
const passInfo = tech.passes[k] as IPassInfo;
|
||||
const uniforms: IPropertyMap = passInfo.uniforms = {};
|
||||
traverseUniforms(programs[passInfo.program], (u) => {
|
||||
const prop = props[u.name];
|
||||
uniforms[u.name] = {
|
||||
uniforms[u.name] = { // property overloads are shared among passes here
|
||||
type: prop ? prop.type : u.type,
|
||||
value: prop ? prop.value : getInstanceCtor(u.type)(),
|
||||
};
|
||||
@@ -175,16 +175,16 @@ export class Effect {
|
||||
}
|
||||
return passes;
|
||||
}
|
||||
public static parseForInspector(effect: EffectAsset) {
|
||||
public static parseForInspector (effect: EffectAsset) {
|
||||
const programs = getInvolvedPrograms(effect);
|
||||
const props = parseProperties(effect, programs);
|
||||
const defines: Record<string, object> = {};
|
||||
for (const pn of Object.keys(programs)) {
|
||||
programs[pn].defines.forEach((define) => {
|
||||
defines[define.name] = {
|
||||
defines: define.defines,
|
||||
instanceType: getInstanceType(define.type),
|
||||
value: getInstanceCtor(define.type)(),
|
||||
defines: define.defines,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
+53
-45
@@ -1,6 +1,6 @@
|
||||
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
import { BlockInfo, SamplerInfo } from '../../3d/assets/effect-asset';
|
||||
import { IBlockInfo, ISamplerInfo } from '../../3d/assets/effect-asset';
|
||||
import { color4, mat2, mat3, mat4, vec2, vec3, vec4 } from '../../core/vmath';
|
||||
import { GFXBindingLayout } from '../../gfx/binding-layout';
|
||||
import { GFXBuffer } from '../../gfx/buffer';
|
||||
@@ -14,9 +14,9 @@ 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';
|
||||
import { IPropertyMap } from './effect';
|
||||
|
||||
export interface PassInfoBase {
|
||||
export interface IPassInfoBase {
|
||||
program: string;
|
||||
// effect-writer part
|
||||
primitive?: GFXPrimitiveMode;
|
||||
@@ -25,13 +25,13 @@ export interface PassInfoBase {
|
||||
depthStencilState?: GFXDepthStencilState;
|
||||
blendState?: GFXBlendState;
|
||||
}
|
||||
export interface PassInfo extends PassInfoBase {
|
||||
export interface IPassInfo extends IPassInfoBase {
|
||||
// generated part
|
||||
blocks: BlockInfo[];
|
||||
samplers: SamplerInfo[];
|
||||
blocks: IBlockInfo[];
|
||||
samplers: ISamplerInfo[];
|
||||
shader: GFXShader;
|
||||
renderPass: GFXRenderPass;
|
||||
uniforms: PropertyMap;
|
||||
uniforms: IPropertyMap;
|
||||
}
|
||||
|
||||
const _type2fn = {
|
||||
@@ -49,18 +49,28 @@ const _type2fn = {
|
||||
[GFXType.MAT4]: (a: Float32Array, v: any) => mat4.array(a, v),
|
||||
};
|
||||
|
||||
const bindingMask = 0x0fff0000;
|
||||
const indexMask = 0x0000ffff;
|
||||
const genHandle = (binding: number, index: number = 0) => (binding << 16) | index;
|
||||
const parseHandle = (handle: number) => [(handle & bindingMask) >> 16, handle & indexMask];
|
||||
const btMask = 0xf0000000; // 4 bits, 16 slots
|
||||
const typeMask = 0x0fc00000; // 6 bits, 64 slots
|
||||
const bindingMask = 0x003ff800; // 11 bits, 2048 slots
|
||||
const indexMask = 0x000007ff; // 11 bits, 2048 slots
|
||||
const genHandle = (bt: GFXBindingType, type: GFXType, binding: number, index: number = 0) =>
|
||||
((bt << 28) & btMask) | ((type << 22) & typeMask) | ((binding << 11) & bindingMask) | (index & indexMask);
|
||||
const getBindingTypeFromHandle = (handle: number) => (handle & btMask) >>> 28;
|
||||
const getTypeFromHandle = (handle: number) => (handle & typeMask) >>> 22;
|
||||
const getBindingFromHandle = (handle: number) => (handle & bindingMask) >>> 11;
|
||||
const getIndexFromHandle = (handle: number) => (handle & indexMask);
|
||||
|
||||
interface Block {
|
||||
interface IBlock {
|
||||
buffer: ArrayBuffer;
|
||||
views: Float32Array[];
|
||||
dirty: boolean;
|
||||
}
|
||||
|
||||
export class Pass {
|
||||
public static getBindingTypeFromHandle = getBindingTypeFromHandle;
|
||||
public static getTypeFromHandle = getTypeFromHandle;
|
||||
public static getBindingFromHandle = getBindingFromHandle;
|
||||
public static getIndexFromHandle = getIndexFromHandle;
|
||||
// internal resources
|
||||
protected _pipelineState: GFXPipelineState | null = null;
|
||||
protected _pipelineLayout: GFXPipelineLayout | null = null;
|
||||
@@ -72,26 +82,25 @@ export class Pass {
|
||||
protected _primitive: GFXPrimitiveMode = GFXPrimitiveMode.TRIANGLE_LIST;
|
||||
protected _stage: RenderPassStage = RenderPassStage.DEFAULT;
|
||||
protected _handleMap: Record<string, number> = {};
|
||||
protected _typeMap: Record<number, GFXType> = {};
|
||||
protected _blocks: Block[] = [];
|
||||
protected _blocks: IBlock[] = [];
|
||||
protected _layoutDirty: boolean = false;
|
||||
// external references
|
||||
protected _device: GFXDevice;
|
||||
protected _shader: GFXShader | null = null;
|
||||
protected _renderPass: GFXRenderPass | null = null;
|
||||
|
||||
public constructor(device: GFXDevice) {
|
||||
public constructor (device: GFXDevice) {
|
||||
this._device = device;
|
||||
}
|
||||
|
||||
public initialize(info: PassInfo) {
|
||||
public initialize (info: IPassInfo) {
|
||||
this._programName = info.program;
|
||||
// pipeline state
|
||||
const device = this._device;
|
||||
const bindings = info.blocks.map((u) =>
|
||||
({ name: u.name, binding: u.binding, type: GFXBindingType.UNIFORM_BUFFER })
|
||||
({ name: u.name, binding: u.binding, type: GFXBindingType.UNIFORM_BUFFER }),
|
||||
).concat(info.samplers.map((u) =>
|
||||
({ name: u.name, binding: u.binding, type: GFXBindingType.SAMPLER })
|
||||
({ name: u.name, binding: u.binding, type: GFXBindingType.SAMPLER }),
|
||||
));
|
||||
this._bindingLayout = device.createBindingLayout({ bindings });
|
||||
if (!this._bindingLayout) { console.error('create binding layout failed'); return; }
|
||||
@@ -104,10 +113,10 @@ export class Pass {
|
||||
for (const u of info.blocks) {
|
||||
// create gfx buffer resource
|
||||
const buffer = device.createBuffer({
|
||||
usage: GFXBufferUsageBit.UNIFORM,
|
||||
memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE,
|
||||
size: u.size,
|
||||
stride: 1, // N/A for blocks
|
||||
usage: GFXBufferUsageBit.UNIFORM,
|
||||
});
|
||||
if (buffer) {
|
||||
this._bindingLayout.bindBuffer(u.binding, buffer);
|
||||
@@ -117,19 +126,18 @@ export class Pass {
|
||||
return;
|
||||
}
|
||||
// buffer data processing system
|
||||
const block: Block = this._blocks[u.binding] = {
|
||||
const block: IBlock = this._blocks[u.binding] = {
|
||||
buffer: new ArrayBuffer(u.size),
|
||||
views: [],
|
||||
dirty: false,
|
||||
views: [],
|
||||
};
|
||||
u.members.reduce((acc, cur, idx) => {
|
||||
// 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 handle = this._handleMap[cur.name] = genHandle(u.binding, idx);
|
||||
const inf = info.uniforms[cur.name];
|
||||
this._typeMap[handle] = inf.type;
|
||||
this._handleMap[cur.name] = genHandle(GFXBindingType.UNIFORM_BUFFER, inf.type, u.binding, idx);
|
||||
_type2fn[inf.type](view, inf.value);
|
||||
// proceed the counter
|
||||
return acc + cur.size;
|
||||
@@ -137,7 +145,7 @@ export class Pass {
|
||||
buffer.update(block.buffer);
|
||||
}
|
||||
for (const u of info.samplers) {
|
||||
this._handleMap[u.name] = genHandle(u.binding);
|
||||
this._handleMap[u.name] = genHandle(GFXBindingType.SAMPLER, u.type, u.binding);
|
||||
const sampler = device.createSampler({
|
||||
name: u.name,
|
||||
// TODO: specify filter modes in effect
|
||||
@@ -152,25 +160,24 @@ export class Pass {
|
||||
this._bindingLayout.update();
|
||||
}
|
||||
|
||||
public getHandleFromName(name: string) {
|
||||
public getHandle (name: string) {
|
||||
return this._handleMap[name];
|
||||
}
|
||||
|
||||
public getBindingFromName(name: string) {
|
||||
const handle = this.getHandleFromName(name);
|
||||
if (handle === undefined) { return -1; }
|
||||
return parseHandle(handle)[0];
|
||||
public getBinding (name: string) {
|
||||
return Pass.getBindingFromHandle(this.getHandle(name));
|
||||
}
|
||||
|
||||
public setUniform(handle: number, value: any) {
|
||||
const [ binding, idx ] = parseHandle(handle);
|
||||
public setUniform (handle: number, value: any) {
|
||||
const binding = Pass.getBindingFromHandle(handle);
|
||||
const type = Pass.getTypeFromHandle(handle);
|
||||
const idx = Pass.getIndexFromHandle(handle);
|
||||
const block = this._blocks[binding];
|
||||
const type = this._typeMap[handle];
|
||||
_type2fn[type](block.views[idx], value);
|
||||
block.dirty = true;
|
||||
}
|
||||
|
||||
public bindTextureView(binding: number, value: GFXTextureView) {
|
||||
public bindTextureView (binding: number, value: GFXTextureView) {
|
||||
const bl = this._bindingLayout;
|
||||
if (bl && bl.getBindingUnit(binding).texView !== value) {
|
||||
bl.bindTextureView(binding, value);
|
||||
@@ -178,7 +185,7 @@ export class Pass {
|
||||
}
|
||||
}
|
||||
|
||||
public bindSampler(binding: number, value: GFXSampler) {
|
||||
public bindSampler (binding: number, value: GFXSampler) {
|
||||
const bl = this._bindingLayout;
|
||||
if (bl && bl.getBindingUnit(binding).sampler !== value) {
|
||||
bl.bindSampler(binding, value);
|
||||
@@ -186,12 +193,12 @@ export class Pass {
|
||||
}
|
||||
}
|
||||
|
||||
public setStates(info: PassInfoBase) {
|
||||
public setStates (info: IPassInfoBase) {
|
||||
if (this._pipelineState) { this._pipelineState.destroy(); }
|
||||
this.createPipelineState(info);
|
||||
}
|
||||
|
||||
public destroy() {
|
||||
public destroy () {
|
||||
if (this._buffers) { this._buffers.forEach((b) => b.destroy()); this._buffers.length = 0; }
|
||||
if (this._samplers) { this._samplers.forEach((b) => b.destroy()); this._samplers.length = 0; }
|
||||
if (this._bindingLayout) { this._bindingLayout.destroy(); this._bindingLayout = null; }
|
||||
@@ -199,7 +206,7 @@ export class Pass {
|
||||
if (this._pipelineState) { this._pipelineState.destroy(); this._pipelineState = null; }
|
||||
}
|
||||
|
||||
public update() {
|
||||
public update () {
|
||||
const len = this._blocks.length;
|
||||
for (let i = 0; i < len; i++) {
|
||||
const block = this._blocks[i];
|
||||
@@ -214,7 +221,7 @@ export class Pass {
|
||||
}
|
||||
}
|
||||
|
||||
protected createPipelineState(info: PassInfoBase) {
|
||||
protected createPipelineState (info: IPassInfoBase) {
|
||||
if (info.primitive) { this._primitive = info.primitive; }
|
||||
if (info.stage) { this._stage = info.stage; }
|
||||
|
||||
@@ -233,13 +240,14 @@ export class Pass {
|
||||
Object.assign(bs, bsInfo);
|
||||
}
|
||||
const stateInfo = {
|
||||
primitive: info.primitive || this._primitive,
|
||||
rs: Object.assign(new GFXRasterizerState(), info.rasterizerState),
|
||||
bs,
|
||||
dss: Object.assign(new GFXDepthStencilState(), info.depthStencilState),
|
||||
is: new GFXInputState(), bs,
|
||||
shader: this._shader,
|
||||
is: new GFXInputState(),
|
||||
layout: this._pipelineLayout,
|
||||
primitive: info.primitive || this._primitive,
|
||||
renderPass: this._renderPass,
|
||||
rs: Object.assign(new GFXRasterizerState(), info.rasterizerState),
|
||||
shader: this._shader,
|
||||
};
|
||||
if (this._pipelineState) {
|
||||
this._pipelineState.initialize(stateInfo);
|
||||
@@ -248,7 +256,7 @@ export class Pass {
|
||||
}
|
||||
}
|
||||
|
||||
get programName() { return this._programName; }
|
||||
get pipelineState() { return <GFXPipelineState>this._pipelineState; }
|
||||
get bindingLayout() { return <GFXBindingLayout>this._bindingLayout; }
|
||||
get programName () { return this._programName; }
|
||||
get pipelineState () { return this._pipelineState as GFXPipelineState; }
|
||||
get bindingLayout () { return this._bindingLayout as GFXBindingLayout; }
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
import { DefineInfo, ShaderInfo } from '../../3d/assets/effect-asset';
|
||||
import { IDefineInfo, IShaderInfo } from '../../3d/assets/effect-asset';
|
||||
import { GFXShaderType } from '../../gfx/define';
|
||||
import { GFXDevice } from '../../gfx/device';
|
||||
import { GFXShader } from '../../gfx/shader';
|
||||
import { DefineMap } from './effect';
|
||||
import { IDefineMap } from './effect';
|
||||
|
||||
function _generateDefines(
|
||||
device: GFXDevice,
|
||||
defs: DefineMap,
|
||||
tDefs: DefineInfo[],
|
||||
defs: IDefineMap,
|
||||
tDefs: IDefineInfo[],
|
||||
deps: Record<string, string>
|
||||
) {
|
||||
const defines: string[] = [];
|
||||
@@ -27,11 +27,11 @@ function _generateDefines(
|
||||
}
|
||||
|
||||
let _shdID = 0;
|
||||
interface DefineRecord extends DefineInfo {
|
||||
interface DefineRecord extends IDefineInfo {
|
||||
_map: (value: any) => number;
|
||||
_offset: number;
|
||||
}
|
||||
interface ProgramInfo extends ShaderInfo {
|
||||
interface ProgramInfo extends IShaderInfo {
|
||||
id: number;
|
||||
defines: DefineRecord[];
|
||||
}
|
||||
@@ -64,7 +64,7 @@ export class ProgramLib {
|
||||
* };
|
||||
* programLib.define(program);
|
||||
*/
|
||||
public define(prog: ShaderInfo) {
|
||||
public define(prog: IShaderInfo) {
|
||||
if (this._templates[prog.name]) { return; }
|
||||
const tmpl = <ProgramInfo>Object.assign({ id: ++_shdID }, prog);
|
||||
tmpl.vert = this._precision + prog.vert;
|
||||
@@ -99,7 +99,7 @@ export class ProgramLib {
|
||||
return this._templates[name] !== undefined;
|
||||
}
|
||||
|
||||
public getKey(name: string, defines: DefineMap) {
|
||||
public getKey(name: string, defines: IDefineMap) {
|
||||
const tmpl = this._templates[name];
|
||||
let key = 0;
|
||||
for (const tmplDef of tmpl.defines) {
|
||||
@@ -112,7 +112,7 @@ export class ProgramLib {
|
||||
return key << 8 | (tmpl.id & 0xff);
|
||||
}
|
||||
|
||||
public getGFXShader(name: string, defines: DefineMap = {}) {
|
||||
public getGFXShader(name: string, defines: IDefineMap = {}) {
|
||||
const key = this.getKey(name, defines);
|
||||
let program = this._cache[key];
|
||||
if (program !== undefined) {
|
||||
|
||||
+2
-2
@@ -12,7 +12,7 @@
|
||||
"variable-name": false,
|
||||
"object-literal-sort-keys": false,
|
||||
"ban-types": false,
|
||||
"no-bitwise:": false,
|
||||
"no-bitwise": false
|
||||
},
|
||||
"rulesDirectory": []
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user