Merge pull request #22 from YunHsiao/gfx

pass properties
This commit is contained in:
Weiliang Wang
2019-01-09 17:52:56 +08:00
committed by GitHub
7 changed files with 156 additions and 285 deletions
+21 -12
View File
@@ -1,20 +1,32 @@
import { Asset } from '../../assets/asset';
import { ccclass, property } from '../../core/data/class-decorator';
import { IPassInfoBase } from '../../renderer/core/pass';
import { GFXPrimitiveMode } from '../../gfx/define';
import { GFXBlendState, GFXDepthStencilState, GFXRasterizerState } from '../../gfx/pipeline-state';
import { RenderPassStage } from '../../pipeline/render-pipeline';
export interface ITechniqueInfo {
passes: IPassInfoBase[];
queue?: number;
priority?: number;
lod?: number;
}
export interface IPropertyInfo {
type?: number;
value?: number | number[] | null;
type: number; // auto-extracted if not specified
value?: number[] | string;
displayName?: string;
tech?: number;
pass?: number;
}
export interface IPassInfo {
program: string;
// effect-writer part
primitive?: GFXPrimitiveMode;
stage?: RenderPassStage;
rasterizerState?: GFXRasterizerState;
depthStencilState?: GFXDepthStencilState;
blendState?: GFXBlendState;
properties?: Record<string, IPropertyInfo>;
}
export interface ITechniqueInfo {
passes: IPassInfo[];
queue?: number;
priority?: number;
lod?: number;
}
export interface IBlockMember {
name: string;
@@ -81,9 +93,6 @@ export class EffectAsset extends Asset {
@property
public techniques: ITechniqueInfo[] = [];
@property
public properties: Record<string, IPropertyInfo> = {};
@property
public shaders: IShaderInfo[] = [];
+36 -56
View File
@@ -1,29 +1,42 @@
export default [
{
"name": "builtin-effect-unlit",
"techniques": [{"queue": "opaque", "lod": 100, "passes": [{"properties": {"color": {"type": 17, "value": [1, 1, 1, 1]}, "mainTiling": {"type": 14, "value": [1, 1]}, "mainOffset": {"type": 14, "value": [0, 0]}, "mainTexture": {"type": 29, "value": "grey"}}, "program": "e238b675dacba30ad29961fac06cb18d8c771e16d4fb0f9662acbe3c"}]}],
"shaders": [
{
"name": "e238b675dacba30ad29961fac06cb18d8c771e16d4fb0f9662acbe3c",
"vert": "\nattribute vec3 a_position;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform vec4 cc_cameraSize;\n uniform vec4 cc_cameraPos;\n uniform vec4 cc_dirLightDirection;\n uniform vec4 cc_dirLightColor;\n uniform vec4 cc_pointLightPositionAndRange;\n uniform vec4 cc_pointLightColor;\n uniform vec4 cc_spotLightPositionAndRange;\n uniform vec4 cc_spotLightDirection;\n uniform vec4 cc_spotLightColor;\n uniform mat4 cc_matViewProjLight;\n uniform vec4 cc_shadowParam1;\n uniform vec4 cc_shadowParam2;\n uniform mat4 cc_matWorld;\n uniform mat4 cc_matWorldIT;\n#if USE_TEXTURE\n attribute vec2 a_texCoord1;\n uniform vec2 mainTiling;\n uniform vec2 mainOffset;\n varying vec2 uv0;\n#endif\n#if CC_USE_SKINNING\n \nattribute vec4 a_weights;\nattribute vec4 a_joints;\n uniform mat4 cc_matJoint;\n uniform vec4 cc_jointsTextureSize;\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 4.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = texture2D(cc_jointsTexture, vec2(dx * (x + 0.5), y));\n vec4 v2 = texture2D(cc_jointsTexture, vec2(dx * (x + 1.5), y));\n vec4 v3 = texture2D(cc_jointsTexture, vec2(dx * (x + 2.5), y));\n vec4 v4 = texture2D(cc_jointsTexture, vec2(dx * (x + 3.5), y));\n return mat4(v1, v2, v3, v4);\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n return cc_matJoint[int(i)];\n}\n#endif\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x) * a_weights.x +\n getBoneMatrix(a_joints.y) * a_weights.y +\n getBoneMatrix(a_joints.z) * a_weights.z +\n getBoneMatrix(a_joints.w) * a_weights.w;\n}\nvoid skinVertex(inout vec4 a1) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2, inout vec4 a3) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n a3 = m * a3;\n}\n#endif\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n #if CC_USE_SKINNING\n skinVertex(pos);\n #endif\n pos = cc_matViewProj * cc_matWorld * pos;\n #if USE_TEXTURE\n uv0 = a_texCoord1 * mainTiling + mainOffset;\n #endif\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n",
"frag": "\n#if USE_TEXTURE\n uniform sampler2D mainTexture;\n varying vec2 uv0;\n#endif\n#if USE_COLOR\n uniform vec4 color;\n#endif\nvec4 frag () {\n vec4 o = vec4(1, 1, 1, 1);\n #if USE_TEXTURE\n o *= texture2D(mainTexture, uv0);\n #endif\n #if USE_COLOR\n o *= color;\n #endif\n return o;\n}\nvoid main() { gl_FragColor = frag(); }\n",
"defines": [
{"name": "USE_TEXTURE", "type": "boolean", "defines": []},
{"name": "CC_USE_SKINNING", "type": "boolean", "defines": []},
{"name": "CC_USE_JOINTS_TEXTURE", "type": "boolean", "defines": ["CC_USE_SKINNING"]},
{"name": "USE_COLOR", "type": "boolean", "defines": []}
],
"blocks": [
{"name": "TexCoords", "size": 16, "defines": ["USE_TEXTURE"], "binding": 0, "members": [
{"name": "mainTiling", "type": 14, "count": 1, "size": 8},
{"name": "mainOffset", "type": 14, "count": 1, "size": 8}
]},
{"name": "Constant", "size": 16, "defines": ["USE_COLOR"], "binding": 1, "members": [
{"name": "color", "type": 16, "count": 1, "size": 16}
]}
],
"samplers": [
{"name": "mainTexture", "type": 29, "count": 1, "defines": ["USE_TEXTURE"], "binding": 2}
],
"dependencies": {}
}
]
},
{
"name": "test",
"techniques": [{"queue": 0, "lod": 500, "passes": [{"stage": 100, "program": "f4c45e6cc7794309d86202e6ec9036a3871fd1ec167a9e02eb1e1574"}], "priority": 0}],
"properties": {"u_color": {"type": 17, "value": [1, 1, 1, 1]}, "u_sampler": {"type": 29, "value": null}},
"techniques": [{"queue": "opaque", "lod": 500, "passes": [{"stage": 100, "properties": {"u_color": {"type": 17, "value": [1, 1, 1, 1]}, "u_sampler": {"type": 29, "value": "grey"}}, "program": "f4c45e6cc7794309d86202e6ec9036a3871fd1ec167a9e02eb1e1574"}]}],
"shaders": [
{
"name": "f4c45e6cc7794309d86202e6ec9036a3871fd1ec167a9e02eb1e1574",
"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;
}
`,
"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",
"defines": [],
"blocks": [
{"name": "Constants", "size": 16, "defines": [], "binding": 0, "members": [
@@ -37,41 +50,9 @@ void main() {
}
]
},
{
"name": "builtin-effect-unlit",
"techniques": [{"queue": 0, "lod": 100, "passes": [{"program": "e238b675dacba30ad29961fac06cb18d8c771e16d4fb0f9662acbe3c"}], "priority": 0}],
"properties": {"color": {"type": 17, "value": [1, 1, 1, 1]}, "mainTiling": {"type": 14, "value": [1, 1]}, "mainOffset": {"type": 14, "value": [0, 0]}, "mainTexture": {"type": 29, "value": null}},
"shaders": [
{
"name": "e238b675dacba30ad29961fac06cb18d8c771e16d4fb0f9662acbe3c",
"vert": "\nattribute vec3 a_position;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform vec4 cc_cameraSize;\n uniform vec4 cc_cameraPos;\n uniform vec4 cc_dirLightDirection;\n uniform vec4 cc_dirLightColor;\n uniform vec4 cc_pointLightPositionAndRange;\n uniform vec4 cc_pointLightColor;\n uniform vec4 cc_spotLightPositionAndRange;\n uniform vec4 cc_spotLightDirection;\n uniform vec4 cc_spotLightColor;\n uniform mat4 cc_matViewProjLight;\n uniform vec4 cc_shadowParam1;\n uniform vec4 cc_shadowParam2;\n uniform mat4 cc_matWorld;\n uniform mat4 cc_matWorldIT;\n#if USE_TEXTURE\n attribute vec2 a_texCoord1;\n uniform vec2 mainTiling;\n uniform vec2 mainOffset;\n varying vec2 uv0;\n#endif\n#if CC_USE_SKINNING\n \nattribute vec4 a_weights;\nattribute vec4 a_joints;\n uniform mat4 cc_matJoint;\n uniform vec4 cc_jointsTextureSize;\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 4.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = texture2D(cc_jointsTexture, vec2(dx * (x + 0.5), y));\n vec4 v2 = texture2D(cc_jointsTexture, vec2(dx * (x + 1.5), y));\n vec4 v3 = texture2D(cc_jointsTexture, vec2(dx * (x + 2.5), y));\n vec4 v4 = texture2D(cc_jointsTexture, vec2(dx * (x + 3.5), y));\n return mat4(v1, v2, v3, v4);\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n return cc_matJoint[int(i)];\n}\n#endif\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x) * a_weights.x +\n getBoneMatrix(a_joints.y) * a_weights.y +\n getBoneMatrix(a_joints.z) * a_weights.z +\n getBoneMatrix(a_joints.w) * a_weights.w;\n}\nvoid skinVertex(inout vec4 a1) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2, inout vec4 a3) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n a3 = m * a3;\n}\n#endif\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n #if CC_USE_SKINNING\n skinVertex(pos);\n #endif\n pos = cc_matViewProj * cc_matWorld * pos;\n #if USE_TEXTURE\n uv0 = a_texCoord1 * mainTiling + mainOffset;\n #endif\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n",
"frag": "\n#if USE_TEXTURE\n uniform sampler2D mainTexture;\n varying vec2 uv0;\n#endif\n#if USE_COLOR\n uniform vec4 color;\n#endif\nvec4 frag () {\n vec4 o = vec4(1, 1, 1, 1);\n #if USE_TEXTURE\n o *= texture2D(mainTexture, uv0);\n #endif\n #if USE_COLOR\n o *= color;\n #endif\n return o;\n}\nvoid main() { gl_FragColor = frag(); }\n",
"defines": [
{"name": "USE_TEXTURE", "type": "boolean", "defines": []},
{"name": "CC_USE_SKINNING", "type": "boolean", "defines": []},
{"name": "CC_USE_JOINTS_TEXTURE", "type": "boolean", "defines": ["CC_USE_SKINNING"]},
{"name": "USE_COLOR", "type": "boolean", "defines": []}
],
"blocks": [
{"name": "TexCoords", "defines": ["USE_TEXTURE"], "binding": 0, "size": 16, "members": [
{"name": "mainTiling", "type": 14, "count": 1},
{"name": "mainOffset", "type": 14, "count": 1}
]},
{"name": "Constant", "defines": ["USE_COLOR"], "binding": 1, "size": 16, "members": [
{"name": "color", "type": 16, "count": 1}
]}
],
"samplers": [
{"name": "mainTexture", "type": 29, "count": 1, "defines": ["USE_TEXTURE"], "binding": 2}
],
"dependencies": {}
}
]
},
{
"name": "builtin-effect-skybox",
"techniques": [{"queue": 0, "lod": 200, "passes": [{"rasterizerState": {"cullMode": 0}, "depthStencilState": {"depthTest": false, "depthWrite": false}, "program": "d9cd35729516d899db5cd6b6ecf025e61a484094405a391a3056591d"}], "priority": -1000}],
"properties": {"cubeMap": {"type": 32, "value": null}},
"techniques": [{"queue": "opaque-1000", "lod": 200, "passes": [{"rasterizerState": {"cullMode": 0}, "depthStencilState": {"depthTest": false, "depthWrite": false}, "properties": {"cubeMap": {"type": 32, "value": "greyCube"}}, "program": "d9cd35729516d899db5cd6b6ecf025e61a484094405a391a3056591d"}]}],
"shaders": [
{
"name": "d9cd35729516d899db5cd6b6ecf025e61a484094405a391a3056591d",
@@ -90,8 +71,7 @@ void main() {
},
{
"name": "builtin-effect-sprite",
"techniques": [{"queue": 2, "passes": [{"depthStencilState": {"depthTest": true, "depthWrite": false}, "blendState": {"targets": [{"blend": true, "blendSrc": 2, "blendDst": 4, "blendDstAlpha": 4}]}, "program": "a86b95b64dc75bf2cc5b528318547813f86f5cd9e03ee48ca69556f6"}], "priority": 0}],
"properties": {"mainTexture": {"type": 29, "value": null}},
"techniques": [{"queue": "overlay", "passes": [{"depthStencilState": {"depthTest": true, "depthWrite": false}, "blendState": {"targets": [{"blend": true, "blendSrc": 2, "blendDst": 4, "blendDstAlpha": 4}]}, "properties": {"mainTexture": {"type": 29, "value": "grey"}}, "program": "a86b95b64dc75bf2cc5b528318547813f86f5cd9e03ee48ca69556f6"}]}],
"shaders": [
{
"name": "a86b95b64dc75bf2cc5b528318547813f86f5cd9e03ee48ca69556f6",
@@ -105,5 +85,5 @@ void main() {
"dependencies": {}
}
]
}
},
];
+6 -6
View File
@@ -78,18 +78,18 @@ let builtinResMgr = {
});
// default material
// let defaultMtl = new Material();
// defaultMtl._uuid = 'default-material';
// defaultMtl._defines['USE_COLOR'] = true;
// defaultMtl.effectAsset = efxs[0];
// defaultMtl.setProperty('color', cc.color('#FF00FF'));
let defaultMtl = new Material();
defaultMtl._uuid = 'default-material';
defaultMtl.setDefines({ USE_COLOR: true });
defaultMtl.effectAsset = efxs[0];
defaultMtl.setProperty('color', cc.color('#FF00FF'));
let builtins = {
// [defaultTexture._uuid]: defaultTexture,
// [defaultTextureCube._uuid]: defaultTextureCube,
// [blackTexture._uuid]: blackTexture,
// [whiteTexture._uuid]: whiteTexture,
// [defaultMtl._uuid]: defaultMtl
[defaultMtl._uuid]: defaultMtl
};
return Object.assign(builtinResMgr, builtins);
},
+3 -5
View File
@@ -32,21 +32,19 @@ export class ForwardFlow extends RenderFlow {
priority: ForwardStagePriority.FORWARD,
framebuffer: mainWindow.framebuffer,
});
/* */
/*
/* *
this.createStage<TestMaterialStage>(TestMaterialStage, {
name: 'TestMaterialStage',
priority: ForwardStagePriority.FORWARD,
framebuffer: mainWindow.framebuffer,
});
*/
/* */
this.createStage<TestStage>(TestStage, {
name: 'TestStage',
priority: ForwardStagePriority.FORWARD,
framebuffer: mainWindow.framebuffer,
});
/* */
return true;
}
+22 -70
View File
@@ -1,21 +1,17 @@
import { Material } from '../../3d/assets/material';
import ImageAsset from '../../assets/image-asset';
import Texture2D from '../../assets/texture-2d';
import { GFXCommandBuffer } from '../../gfx/command-buffer';
import { GFXBufferTextureCopy, GFXCommandBufferType, GFXFormat,
GFXTextureFlagBit, GFXTextureType, GFXTextureUsageBit, GFXTextureViewType } from '../../gfx/define';
import { GFXCommandBufferType } from '../../gfx/define';
import { GFXFramebuffer } from '../../gfx/framebuffer';
import { GFXInputAssembler } from '../../gfx/input-assembler';
import { GFXTexture } from '../../gfx/texture';
import { GFXTextureView } from '../../gfx/texture-view';
import { RenderFlow } from '../render-flow';
import { RenderStage, RenderStageInfo } from '../render-stage';
import { RenderView } from '../render-view';
import Texture2D from '../../assets/texture-2d';
import ImageAsset from '../../assets/image-asset';
export class TestMaterialStage extends RenderStage {
private _texture: GFXTexture | null = null;
private _texView: GFXTextureView | null = null;
private _textureAsset: Texture2D | null = null;
private _material: Material = new Material();
constructor (flow: RenderFlow) {
@@ -54,17 +50,10 @@ export class TestMaterialStage extends RenderStage {
}
public destroy () {
if (this._texView) {
this._texView.destroy();
this._texView = null;
if (this._textureAsset) {
this._textureAsset.destroy();
this._textureAsset = null;
}
if (this._texture) {
this._texture.destroy();
this._texture = null;
}
if (this._cmdBuff) {
this._cmdBuff.destroy();
this._cmdBuff = null;
@@ -78,18 +67,18 @@ export class TestMaterialStage extends RenderStage {
// material
mtl.effectName = 'test'; // parsed-effect file is embedded in cocos/3d/builtin/effects.js
const pass = mtl.passes[0];
/* *
/* efficient pass API *
const texBinding = pass.getBinding('u_sampler');
pass.bindTextureView(texBinding, this._texView as GFXTextureView);
const texView = this._textureAsset && this._textureAsset.getGFXTextureView();
if (texView) { pass.bindTextureView(texBinding, texView); }
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'));
/* */
/* convenient material API */
mtl.setProperty('u_sampler', this._textureAsset);
mtl.setProperty('u_color', cc.color('#ff0000'));
/**/
pass.update();
// @ts-ignore
mtl.inited = true;
@@ -115,50 +104,13 @@ export class TestMaterialStage extends RenderStage {
}
private loadTexture (image: HTMLImageElement): boolean {
// this._texture = this._device.createTexture({
// type: GFXTextureType.TEX2D,
// usage: GFXTextureUsageBit.SAMPLED | GFXTextureUsageBit.TRANSFER_DST,
// format: GFXFormat.RGBA8,
// width: image.width,
// height: image.height,
// flags: GFXTextureFlagBit.NONE,
// });
// if (!this._texture) {
// return false;
// }
// this._texView = this._device.createTextureView({
// texture: this._texture,
// type: GFXTextureViewType.TV2D,
// format: GFXFormat.RGBA8,
// });
// if (!this._texView) {
// return false;
// }
// const region: GFXBufferTextureCopy = {
// buffOffset: 0,
// buffStride: 0,
// buffTexHeight: 0,
// texOffset: { x: 0, y: 0, z: 0 },
// texExtent: { width: image.width, height: image.height, depth: 1 },
// texSubres: { baseMipLevel: 0, levelCount: 1, baseArrayLayer: 0, layerCount: 1 },
// };
// this._device.copyImageSourceToTexture(image, this._texture, [region]);
const textureAsset = new Texture2D();
const textureAsset = this._textureAsset = new Texture2D();
textureAsset.image = new ImageAsset(image);
this._texture = textureAsset.getGFXTexture();
if (!this._texture) {
return false;
}
this._texView = textureAsset.getGFXTextureView();
if (!this._texView) {
return false;
}
const texture = textureAsset.getGFXTexture();
if (!texture) { return false; }
const texView = textureAsset.getGFXTextureView();
if (!texView) { return false; }
return true;
}
+43 -117
View File
@@ -1,13 +1,13 @@
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
import { EffectAsset, IBlockMember, IPropertyInfo, ISamplerInfo, IShaderInfo } from '../../3d/assets/effect-asset';
import { EffectAsset, 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 { IPassInfo, Pass } from './pass';
import { IPassInfoFull, Pass } from './pass';
const _typeMap = {
[GFXType.INT]: 'Number',
@@ -35,7 +35,7 @@ export interface IEffectInfo {
defines?: IDefineMap;
}
type DefaultValue = number | boolean | number[] | null;
type DefaultValue = number | boolean | number[] | string;
const _ctorMap = {
Boolean: (v?: DefaultValue) => v || false,
Number: (v?: DefaultValue) => v || 0,
@@ -55,140 +55,66 @@ const _ctorMap = {
[CCObject]: () => null,
};
const getInstanceCtor = (t: number | string) => _ctorMap[getInstanceType(t)];
const getInvolvedPrograms = (effect: EffectAsset, techIdx?: number) => {
const programs: Record<string, IShaderInfo> = {};
const lib = cc.game._programLib;
if (techIdx) {
effect.techniques[techIdx].passes.forEach((pass) => {
programs[pass.program] = lib.getTemplate(pass.program);
});
} else {
effect.techniques.forEach((tech) => {
tech.passes.forEach((pass) => {
programs[pass.program] = lib.getTemplate(pass.program);
});
});
}
return programs;
};
interface IExtractedPropInfo {
type: number;
instanceType: object | string;
value: number | boolean | object;
defines: string[];
displayName?: string;
tech: number;
pass: number;
}
const parseProperties = (() => {
interface IUniformInfo { type: number; defines: string[]; }
function genPropInfo (propInfo: IPropertyInfo, uniformInfo: IUniformInfo, tech: number, pass: number)
: IExtractedPropInfo {
const type = propInfo.type || uniformInfo.type;
return {
defines: uniformInfo.defines,
displayName: propInfo.displayName,
instanceType: getInstanceType(type),
pass, tech, type,
value: getInstanceCtor(type)(propInfo.value),
};
}
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
effect.techniques.forEach((tech, i) => {
tech.passes.forEach((pass, j) => {
if (pass.program === programName) {
return [ i, j ];
}
});
});
return [ 0, 0 ];
}
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: IUniformInfo | undefined;
let techIdx = 0;
let passIdx = 0;
// always try getting the type from shaders first
if (propInfo.tech !== undefined && propInfo.pass !== undefined) {
const p = effect.techniques[propInfo.tech].passes[propInfo.pass].program;
uniformInfo = findUniformInfo(programs[p], prop);
techIdx = propInfo.tech; passIdx = propInfo.pass;
} else {
for (const p of Object.keys(programs)) {
uniformInfo = findUniformInfo(programs[p], prop);
if (uniformInfo) { [techIdx, passIdx] = findPassIdx(effect, p); break; }
}
}
// the property is not defined in all the shaders used in techs
if (!uniformInfo) {
console.warn(`illegal property: ${prop}`);
continue;
}
props[prop] = genPropInfo(propInfo, uniformInfo, techIdx, passIdx);
}
return props;
};
})();
const traverseUniforms = (shaderInfo: IShaderInfo, fn: (u: IBlockMember | ISamplerInfo) => any) => {
shaderInfo.blocks.forEach((b) => b.members.forEach(fn));
shaderInfo.samplers.forEach(fn);
const getProgram = (name: string): IShaderInfo => cc.game._programLib.getTemplate(name);
const getDefines = (name: string, prog: IShaderInfo) => {
const block = prog.blocks.find((b) => b.members.find((u) => u.name === name) !== undefined);
if (block) { return block.defines; }
const s = prog.samplers.find((u) => u.name === name);
if (s) { return s.defines; }
};
export class Effect {
public static parseEffect (effect: EffectAsset, info?: IEffectInfo) {
// techniques
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 = tech.passes[k] as IPassInfo;
const uniforms: IPropertyMap = passInfo.uniforms = {};
traverseUniforms(programs[passInfo.program], (u) => {
const prop = props[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)(),
};
});
const passInfo = tech.passes[k] as IPassInfoFull;
const prog = getProgram(passInfo.program);
passInfo.shader = cc.game._programLib.getGFXShader(passInfo.program, defines || {});
passInfo.renderPass = cc.director.root.pipeline.getRenderPass(passInfo.stage || RenderPassStage.DEFAULT);
passInfo.blocks = programs[passInfo.program].blocks;
passInfo.samplers = programs[passInfo.program].samplers;
passInfo.blocks = prog.blocks;
passInfo.samplers = prog.samplers;
const pass = new Pass(cc.game._gfxDevice);
pass.initialize(passInfo);
passes[k] = pass;
}
return passes;
}
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)(),
};
});
const res: object[] = [];
for (const tech of effect.techniques) {
const props: Array<Record<string, object>> = [];
const defines: Record<string, object> = {};
for (const pass of tech.passes) {
const prog = getProgram(pass.program);
prog.defines.forEach((define) => {
defines[define.name] = {
defines: define.defines,
instanceType: getInstanceType(define.type),
value: getInstanceCtor(define.type)(),
};
});
const list: Record<string, object> = {};
props.push(list);
if (!pass.properties) { continue; }
for (const p of Object.keys(pass.properties)) {
const prop = pass.properties[p];
const defs = getDefines(p, prog);
if (!defs) { continue; }
list[p] = {
defines: defs,
instanceType: getInstanceType(prop.type),
value: getInstanceCtor(prop.type)(),
};
}
}
res.push({ props, defines });
}
return { props, defines };
return res;
}
}
+25 -19
View File
@@ -1,6 +1,6 @@
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
import { IBlockInfo, ISamplerInfo } from '../../3d/assets/effect-asset';
import { IBlockInfo, IPassInfo, 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,24 +14,13 @@ import { GFXSampler } from '../../gfx/sampler';
import { GFXShader } from '../../gfx/shader';
import { GFXTextureView } from '../../gfx/texture-view';
import { RenderPassStage } from '../../pipeline/render-pipeline';
import { IPropertyMap } from './effect';
export interface IPassInfoBase {
program: string;
// effect-writer part
primitive?: GFXPrimitiveMode;
stage?: RenderPassStage;
rasterizerState?: GFXRasterizerState;
depthStencilState?: GFXDepthStencilState;
blendState?: GFXBlendState;
}
export interface IPassInfo extends IPassInfoBase {
export interface IPassInfoFull extends IPassInfo {
// generated part
blocks: IBlockInfo[];
samplers: ISamplerInfo[];
shader: GFXShader;
renderPass: GFXRenderPass;
uniforms: IPropertyMap;
}
const _type2fn = {
@@ -49,6 +38,21 @@ const _type2fn = {
[GFXType.MAT4]: (a: Float32Array, v: any) => mat4.array(a, v),
};
const _type2default = {
[GFXType.INT]: 0,
[GFXType.INT2]: [0, 0],
[GFXType.INT3]: [0, 0, 0],
[GFXType.INT4]: [0, 0, 0, 0],
[GFXType.FLOAT]: 0,
[GFXType.FLOAT2]: [0, 0],
[GFXType.FLOAT3]: [0, 0, 0],
[GFXType.FLOAT4]: [0, 0, 0, 0],
[GFXType.COLOR4]: [0, 0, 0, 1],
[GFXType.MAT2]: [1, 0, 0, 1],
[GFXType.MAT3]: [1, 0, 0, 0, 1, 0, 0, 0, 1],
[GFXType.MAT4]: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
};
const btMask = 0xf0000000; // 4 bits, 16 slots
const typeMask = 0x0fc00000; // 6 bits, 64 slots
const bindingMask = 0x003ff800; // 11 bits, 2048 slots
@@ -93,7 +97,7 @@ export class Pass {
this._device = device;
}
public initialize (info: IPassInfo) {
public initialize (info: IPassInfoFull) {
this._programName = info.program;
// pipeline state
const device = this._device;
@@ -136,9 +140,11 @@ export class Pass {
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.uniforms[cur.name];
this._handleMap[cur.name] = genHandle(GFXBindingType.UNIFORM_BUFFER, inf.type, u.binding, idx);
_type2fn[inf.type](view, inf.value);
const inf = info.properties && info.properties[cur.name];
const type = inf && inf.type || cur.type; // property overloads
const value = inf && inf.value;
view.set(value ? value as number[] : _type2default[type]);
this._handleMap[cur.name] = genHandle(GFXBindingType.UNIFORM_BUFFER, type, u.binding, idx);
// proceed the counter
return acc + cur.size;
}, 0); // === u.size
@@ -193,7 +199,7 @@ export class Pass {
}
}
public setStates (info: IPassInfoBase) {
public setStates (info: IPassInfo) {
if (this._pipelineState) { this._pipelineState.destroy(); }
this.createPipelineState(info);
}
@@ -221,7 +227,7 @@ export class Pass {
}
}
protected createPipelineState (info: IPassInfoBase) {
protected createPipelineState (info: IPassInfo) {
if (info.primitive) { this._primitive = info.primitive; }
if (info.stage) { this._stage = info.stage; }