From 23573ad407013fda4561bbbf71e58700cb5421b2 Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Mon, 18 Feb 2019 10:01:40 +0800 Subject: [PATCH 1/2] performance improves --- cocos/3d/framework/camera-component.ts | 2 +- cocos/assets/render-texture.ts | 61 ++---------- cocos/core/root.ts | 7 +- cocos/core/vmath/mat4.ts | 120 +++++++++++++----------- cocos/gfx/command-buffer.ts | 2 +- cocos/gfx/webgl/webgl-command-buffer.ts | 6 +- cocos/gfx/webgl/webgl-commands.ts | 10 +- cocos/gfx/webgl/webgl-device.ts | 15 +-- cocos/gfx/webgl2/webgl2-commands.ts | 10 +- cocos/pipeline/forward/forward-stage.ts | 11 ++- cocos/pipeline/render-pipeline.ts | 2 + cocos/renderer/core/effect.ts | 2 - playground/simple.html | 1 - rollup/config.js | 6 +- 14 files changed, 124 insertions(+), 131 deletions(-) diff --git a/cocos/3d/framework/camera-component.ts b/cocos/3d/framework/camera-component.ts index 7dcc8f2be1..5a2b486b9c 100644 --- a/cocos/3d/framework/camera-component.ts +++ b/cocos/3d/framework/camera-component.ts @@ -100,7 +100,7 @@ export class CameraComponent extends Component { @property protected _rect = new Rect(0, 0, 1, 1); @property - protected _targetDisplay = 0; + protected _targetDisplay = -1; protected _camera: Camera | null = null; diff --git a/cocos/assets/render-texture.ts b/cocos/assets/render-texture.ts index d535942467..c5c15529ab 100644 --- a/cocos/assets/render-texture.ts +++ b/cocos/assets/render-texture.ts @@ -42,7 +42,7 @@ export class RenderTexture extends Texture2D { * @param [string] * @method initWithSize */ - public initWithSize (width?: number, height?: number, format?: DSFormat) { + public initWithSize (width: number, height: number, format: DSFormat = 'D24S8') { this.destroy(); const gfxDevice = this._getGlobalDevice(); @@ -50,29 +50,17 @@ export class RenderTexture extends Texture2D { return; } - const w = Math.floor(width || cc.visibleRect.width); - const h = Math.floor(height || cc.visibleRect.height); const image = new ImageAsset({ - width: w, - height: h, + width, + height, format: GFXFormat.RGBA8UI, - _data: new Uint8Array(w * h * 4), + _data: new Uint8Array(width * height * 4), _compressed: false, }); this.image = image; - if (!this._texture) { - return; - } - - const colorView = gfxDevice.createTextureView({ - texture: this._texture, - type: GFXTextureViewType.TV2D, - format: this._getGfxFormat(), - }); - - if (!colorView) { + if (!this._textureView) { return; } @@ -81,12 +69,9 @@ export class RenderTexture extends Texture2D { type: GFXTextureType.TEX2D, usage: GFXTextureUsageBit.DEPTH_STENCIL_ATTACHMENT, format: _formatMap[format], - width: w, - height: h, + width, + height, }); - if (!this._depthStencilTexture) { - return; - } this._depthStencilTextureView = gfxDevice.createTextureView({ texture: this._depthStencilTexture, type: GFXTextureViewType.TV2D, @@ -94,19 +79,14 @@ export class RenderTexture extends Texture2D { }); } - if (this._depthStencilTextureView === null) { - return; - } - this._framebuffer = gfxDevice.createFramebuffer({ renderPass: cc.director.root.pipeline.getRenderPass(RenderPassStage.DEFAULT), - colorViews: [ colorView ], - depthStencilView: this._depthStencilTextureView === null ? undefined : this._depthStencilTextureView, + colorViews: [ this._textureView ], + depthStencilView: this._depthStencilTextureView!, isOffscreen: true, }); this.loaded = true; - // @ts-ignore this.emit('load'); } @@ -138,31 +118,10 @@ export class RenderTexture extends Texture2D { // this._framebuffer._height = height; // } - /** - * !#en Draw a texture to the specified position - * !#zh 将指定的图片渲染到指定的位置上 - * @param texture - * @param x - * @param y - */ - public drawTextureAt (texture: Texture2D, x: number, y: number) { - if (!this._texture) { - return; - } - - const image = texture.image; - - if (!image) { - return; - } - - this._assignImage(image, 0); - } - /** * !#en * Get pixels from render texture, the pixels data stores in a RGBA Uint8Array. - * It will return a new (width * height * 4) length Uint8Array by default。 + * It will return a new (width * height * 4) length Uint8Array by default. * You can specify a data to store the pixels to reuse the data, * you and can specify other params to specify the texture region to read. * !#zh diff --git a/cocos/core/root.ts b/cocos/core/root.ts index cfd9c400bf..85c0151065 100644 --- a/cocos/core/root.ts +++ b/cocos/core/root.ts @@ -10,9 +10,8 @@ import { UI } from '../renderer/ui/ui'; export let _createSceneFun; export let _createViewFun; -// tslint:disable-next-line:no-empty-interface -export interface IRootInfo { -} +// export interface IRootInfo { +// } export interface ISceneInfo { name: string; @@ -76,7 +75,7 @@ export class Root { RenderView.registerCreateFunc(this); } - public initialize (info: IRootInfo): boolean { + public initialize (/*info: IRootInfo*/): boolean { if (!this._device.mainWindow) { return false; diff --git a/cocos/core/vmath/mat4.ts b/cocos/core/vmath/mat4.ts index 3890527d26..b719e5b449 100644 --- a/cocos/core/vmath/mat4.ts +++ b/cocos/core/vmath/mat4.ts @@ -1,6 +1,8 @@ import { EPSILON } from './utils'; // tslint:disable: one-variable-per-declaration +// tslint:disable: max-line-length +// tslint:disable: class-name /** * Mathematical 4x4 matrix. @@ -29,6 +31,70 @@ import { EPSILON } from './utils'; * apparent lack of consistency between the memory layout and the documentation. */ class mat4 { + + /** + * Inverts a matrix. + * + * @param out - Matrix to store result. + * @param a - Matrix to invert. + * @return out. + */ + public static invert = (() => { + // cache temps due to the high frequency invocations of this function + let a00 = 1, a01 = 0, a02 = 0, a03 = 0; + let a10 = 0, a11 = 1, a12 = 0, a13 = 0; + let a20 = 0, a21 = 0, a22 = 1, a23 = 0; + let a30 = 0, a31 = 0, a32 = 0, a33 = 1; + let b00 = 1, b01 = 0, b02 = 0, b03 = 0; + let b04 = 0, b05 = 0, b06 = 0, b07 = 0; + let b08 = 0, b09 = 0, b10 = 0, b11 = 1; + let det = 1; + return (out, a) => { + a00 = a.m00; a01 = a.m01; a02 = a.m02; a03 = a.m03; + a10 = a.m04; a11 = a.m05; a12 = a.m06; a13 = a.m07; + a20 = a.m08; a21 = a.m09; a22 = a.m10; a23 = a.m11; + a30 = a.m12; a31 = a.m13; a32 = a.m14; a33 = a.m15; + + b00 = a00 * a11 - a01 * a10; + b01 = a00 * a12 - a02 * a10; + b02 = a00 * a13 - a03 * a10; + b03 = a01 * a12 - a02 * a11; + b04 = a01 * a13 - a03 * a11; + b05 = a02 * a13 - a03 * a12; + b06 = a20 * a31 - a21 * a30; + b07 = a20 * a32 - a22 * a30; + b08 = a20 * a33 - a23 * a30; + b09 = a21 * a32 - a22 * a31; + b10 = a21 * a33 - a23 * a31; + b11 = a22 * a33 - a23 * a32; + + // Calculate the determinant + det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; + + if (det === 0) { return null; } + det = 1.0 / det; + + out.m00 = (a11 * b11 - a12 * b10 + a13 * b09) * det; + out.m01 = (a02 * b10 - a01 * b11 - a03 * b09) * det; + out.m02 = (a31 * b05 - a32 * b04 + a33 * b03) * det; + out.m03 = (a22 * b04 - a21 * b05 - a23 * b03) * det; + out.m04 = (a12 * b08 - a10 * b11 - a13 * b07) * det; + out.m05 = (a00 * b11 - a02 * b08 + a03 * b07) * det; + out.m06 = (a32 * b02 - a30 * b05 - a33 * b01) * det; + out.m07 = (a20 * b05 - a22 * b02 + a23 * b01) * det; + out.m08 = (a10 * b10 - a11 * b08 + a13 * b06) * det; + out.m09 = (a01 * b08 - a00 * b10 - a03 * b06) * det; + out.m10 = (a30 * b04 - a31 * b02 + a33 * b00) * det; + out.m11 = (a21 * b02 - a20 * b04 - a23 * b00) * det; + out.m12 = (a11 * b07 - a10 * b09 - a12 * b06) * det; + out.m13 = (a00 * b09 - a01 * b07 + a02 * b06) * det; + out.m14 = (a31 * b01 - a30 * b03 - a32 * b00) * det; + out.m15 = (a20 * b03 - a21 * b01 + a22 * b00) * det; + + return out; + }; + })(); + /** * Creates a matrix, with elements specified separately. * @@ -221,60 +287,6 @@ class mat4 { return out; } - /** - * Inverts a matrix. - * - * @param out - Matrix to store result. - * @param a - Matrix to invert. - * @return out. - */ - public static invert (out, a) { - const a00 = a.m00, a01 = a.m01, a02 = a.m02, a03 = a.m03, - a10 = a.m04, a11 = a.m05, a12 = a.m06, a13 = a.m07, - a20 = a.m08, a21 = a.m09, a22 = a.m10, a23 = a.m11, - a30 = a.m12, a31 = a.m13, a32 = a.m14, a33 = a.m15; - - const b00 = a00 * a11 - a01 * a10; - const b01 = a00 * a12 - a02 * a10; - const b02 = a00 * a13 - a03 * a10; - const b03 = a01 * a12 - a02 * a11; - const b04 = a01 * a13 - a03 * a11; - const b05 = a02 * a13 - a03 * a12; - const b06 = a20 * a31 - a21 * a30; - const b07 = a20 * a32 - a22 * a30; - const b08 = a20 * a33 - a23 * a30; - const b09 = a21 * a32 - a22 * a31; - const b10 = a21 * a33 - a23 * a31; - const b11 = a22 * a33 - a23 * a32; - - // Calculate the determinant - let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; - - if (!det) { - return null; - } - det = 1.0 / det; - - out.m00 = (a11 * b11 - a12 * b10 + a13 * b09) * det; - out.m01 = (a02 * b10 - a01 * b11 - a03 * b09) * det; - out.m02 = (a31 * b05 - a32 * b04 + a33 * b03) * det; - out.m03 = (a22 * b04 - a21 * b05 - a23 * b03) * det; - out.m04 = (a12 * b08 - a10 * b11 - a13 * b07) * det; - out.m05 = (a00 * b11 - a02 * b08 + a03 * b07) * det; - out.m06 = (a32 * b02 - a30 * b05 - a33 * b01) * det; - out.m07 = (a20 * b05 - a22 * b02 + a23 * b01) * det; - out.m08 = (a10 * b10 - a11 * b08 + a13 * b06) * det; - out.m09 = (a01 * b08 - a00 * b10 - a03 * b06) * det; - out.m10 = (a30 * b04 - a31 * b02 + a33 * b00) * det; - out.m11 = (a21 * b02 - a20 * b04 - a23 * b00) * det; - out.m12 = (a11 * b07 - a10 * b09 - a12 * b06) * det; - out.m13 = (a00 * b09 - a01 * b07 + a02 * b06) * det; - out.m14 = (a31 * b01 - a30 * b03 - a32 * b00) * det; - out.m15 = (a20 * b03 - a21 * b01 + a22 * b00) * det; - - return out; - } - /** * Calculates the adjugate of a matrix. * diff --git a/cocos/gfx/command-buffer.ts b/cocos/gfx/command-buffer.ts index 8da0e7380b..9d11504b55 100644 --- a/cocos/gfx/command-buffer.ts +++ b/cocos/gfx/command-buffer.ts @@ -77,7 +77,7 @@ export abstract class GFXCommandBuffer extends GFXObject { public abstract setDepthBias (depthBiasConstantFacotr: number, depthBiasClamp: number, depthBiasSlopeFactor: number); public abstract setBlendConstants (blendConstants: number[]); public abstract setDepthBound (minDepthBounds: number, maxDepthBounds: number); - public abstract setStencilWirteMask (faceMask: GFXStencilFace, writeMask: number); + public abstract setStencilWriteMask (faceMask: GFXStencilFace, writeMask: number); public abstract setStencilCompareMask (face: GFXStencilFace, reference: number, compareMask: number); public abstract draw (inputAssembler: GFXInputAssembler); public abstract updateBuffer (buffer: GFXBuffer, data: ArrayBuffer, offset?: number); diff --git a/cocos/gfx/webgl/webgl-command-buffer.ts b/cocos/gfx/webgl/webgl-command-buffer.ts index 440da5a98b..32e379926b 100644 --- a/cocos/gfx/webgl/webgl-command-buffer.ts +++ b/cocos/gfx/webgl/webgl-command-buffer.ts @@ -135,7 +135,9 @@ export class WebGLGFXCommandBuffer extends GFXCommandBuffer { const cmd = this._webGLAllocator!.beginRenderPassCmdPool.alloc(WebGLCmdBeginRenderPass); cmd.gpuFramebuffer = ( framebuffer as WebGLGFXFramebuffer).gpuFramebuffer; cmd.renderArea = renderArea; - cmd.clearColors = clearColors.slice(); + for (let i = 0; i < clearColors.length; i++) { + cmd.clearColors[i] = clearColors[i]; + } cmd.clearDepth = clearDepth; cmd.clearStencil = clearStencil; this.cmdPackage.beginRenderPassCmds.push(cmd); @@ -287,7 +289,7 @@ export class WebGLGFXCommandBuffer extends GFXCommandBuffer { } } - public setStencilWirteMask (face: GFXStencilFace, writeMask: number) { + public setStencilWriteMask (face: GFXStencilFace, writeMask: number) { if (!this._curStencilWriteMask) { this._curStencilWriteMask = { face, diff --git a/cocos/gfx/webgl/webgl-commands.ts b/cocos/gfx/webgl/webgl-commands.ts index 91560f4686..54ee945f48 100644 --- a/cocos/gfx/webgl/webgl-commands.ts +++ b/cocos/gfx/webgl/webgl-commands.ts @@ -1226,10 +1226,14 @@ export function WebGLCmdFuncCreateInputAssember (device: WebGLGFXDevice, gpuInpu export function WebGLCmdFuncDestroyInputAssembler (device: WebGLGFXDevice, gpuInputAssembler: WebGLGPUInputAssembler) { } +const cmdIds = [0, 0, 0, 0, 0, 0]; // 6 +const invalidateAttachments: GLenum[] = [0, 0, 0, 0]; export function WebGLCmdFuncExecuteCmds (device: WebGLGFXDevice, cmdPackage: WebGLCmdPackage) { const gl = device.gl; - const cmdIds = [0, 0, 0, 0, 0, 0]; // 6 + for (let initID = 0; initID < cmdIds.length; initID++) { + cmdIds[initID] = 0; + } let gpuPipelineState: WebGLGPUPipelineState | null = null; let gpuShader: WebGLGPUShader | null = null; @@ -1281,7 +1285,9 @@ export function WebGLCmdFuncExecuteCmds (device: WebGLGFXDevice, cmdPackage: Web const curGPURenderPass = cmd0.gpuFramebuffer.gpuRenderPass; - const invalidateAttachments: GLenum[] = [0, 0, 0, 0]; + for (let initAtt = 0; initAtt < invalidateAttachments.length; initAtt++) { + invalidateAttachments[initAtt] = 0; + } let numInvalidAttach = 0; if (curGPURenderPass.colorAttachments.length > 0 && cmd0.clearColors.length) { diff --git a/cocos/gfx/webgl/webgl-device.ts b/cocos/gfx/webgl/webgl-device.ts index 23c8928954..5f7cff5931 100644 --- a/cocos/gfx/webgl/webgl-device.ts +++ b/cocos/gfx/webgl/webgl-device.ts @@ -469,12 +469,15 @@ export class WebGLGFXDevice extends GFXDevice { if (!context) { return; } const data: Uint8ClampedArray[] = []; - for (let i = 0; i < source.length; i++) { - const level = regions[i].texSubres.baseMipLevel; - this._canvas2D.width = texture.width >> level; - this._canvas2D.height = texture.height >> level; - context.drawImage(source[i], 0, 0); - data.push(context.getImageData(0, 0, this._canvas2D.width, this._canvas2D.height).data); + const faces = source.length / regions.length; + for (let j = 0; j < regions.length; j++) { + for (let i = 0; i < faces; i++) { + const level = regions[j].texSubres.baseMipLevel; + this._canvas2D.width = texture.width >> level; + this._canvas2D.height = texture.height >> level; + context.drawImage(source[j * faces + i], 0, 0); + data.push(context.getImageData(0, 0, this._canvas2D.width, this._canvas2D.height).data); + } } const buffer = new Uint8ClampedArray(data.reduce((acc, cur) => acc + cur.length, 0)); diff --git a/cocos/gfx/webgl2/webgl2-commands.ts b/cocos/gfx/webgl2/webgl2-commands.ts index 01ee9dd26d..6070fc208b 100644 --- a/cocos/gfx/webgl2/webgl2-commands.ts +++ b/cocos/gfx/webgl2/webgl2-commands.ts @@ -1345,10 +1345,14 @@ export function WebGL2CmdFuncCreateInputAssember (device: WebGL2GFXDevice, gpuIn export function WebGL2CmdFuncDestroyInputAssembler (device: WebGL2GFXDevice, gpuInputAssembler: WebGL2GPUInputAssembler) { } +const cmdIds = [0, 0, 0, 0, 0, 0]; // 6 +const invalidateAttachments: GLenum[] = [0, 0, 0, 0]; export function WebGL2CmdFuncExecuteCmds (device: WebGL2GFXDevice, cmdPackage: WebGL2CmdPackage) { const gl = device.gl; - const cmdIds = [0, 0, 0, 0, 0, 0]; // 6 + for (let initID = 0; initID < cmdIds.length; initID++) { + cmdIds[initID] = 0; + } let gpuPipelineState: WebGL2GPUPipelineState | null = null; let gpuShader: WebGL2GPUShader | null = null; @@ -1400,7 +1404,9 @@ export function WebGL2CmdFuncExecuteCmds (device: WebGL2GFXDevice, cmdPackage: W const curGPURenderPass = cmd0.gpuFramebuffer.gpuRenderPass; - const invalidateAttachments: GLenum[] = [0, 0, 0, 0]; + for (let initAtt = 0; initAtt < invalidateAttachments.length; initAtt++) { + invalidateAttachments[initAtt] = 0; + } let numInvalidAttach = 0; if (curGPURenderPass.colorAttachments.length > 0 && cmd0.clearColors.length) { diff --git a/cocos/pipeline/forward/forward-stage.ts b/cocos/pipeline/forward/forward-stage.ts index 28d57a5681..1b09cd09a5 100644 --- a/cocos/pipeline/forward/forward-stage.ts +++ b/cocos/pipeline/forward/forward-stage.ts @@ -1,11 +1,14 @@ import { GFXCommandBuffer } from '../../gfx/command-buffer'; -import { GFXCommandBufferType } from '../../gfx/define'; +import { GFXCommandBufferType, IGFXColor } from '../../gfx/define'; import { GFXFramebuffer } from '../../gfx/framebuffer'; import { Camera } from '../../renderer/scene/camera'; import { RenderFlow } from '../render-flow'; import { IRenderStageInfo, RenderStage } from '../render-stage'; import { RenderView } from '../render-view'; +const colors: IGFXColor[] = []; +const bufs: GFXCommandBuffer[] = []; + export class ForwardStage extends RenderStage { constructor (flow: RenderFlow) { @@ -51,16 +54,18 @@ export class ForwardStage extends RenderStage { this._renderArea.width = camera.width; this._renderArea.height = camera.height; + colors[0] = camera.clearColor; cmdBuff.begin(); cmdBuff.beginRenderPass(framebuffer, this._renderArea, - [camera.clearColor], camera.clearDepth, camera.clearStencil); + colors, camera.clearDepth, camera.clearStencil); cmdBuff.execute(queue.cmdBuffs.array, queue.cmdBuffCount); cmdBuff.endRenderPass(); cmdBuff.end(); - this._device.queue.submit([cmdBuff]); + bufs[0] = cmdBuff; + this._device.queue.submit(bufs); } } diff --git a/cocos/pipeline/render-pipeline.ts b/cocos/pipeline/render-pipeline.ts index 369f8dad4a..707843d03a 100644 --- a/cocos/pipeline/render-pipeline.ts +++ b/cocos/pipeline/render-pipeline.ts @@ -14,6 +14,7 @@ import { IRenderFlowInfo, RenderFlow } from './render-flow'; import { RenderQueue } from './render-queue'; import { RenderView } from './render-view'; +// tslint:disable: max-line-length export class UBOGlobal { public static MAX_DIR_LIGHTS = 4; public static MAX_POINT_LIGHTS = 4; @@ -66,6 +67,7 @@ export class UBOGlobal { public view: Float32Array = new Float32Array(UBOGlobal.COUNT); } +// tslint:enable: max-line-length export class UBOLocal { public static MAT_WORLD_OFFSET: number = 0; diff --git a/cocos/renderer/core/effect.ts b/cocos/renderer/core/effect.ts index 125ae93f1d..0e89ef1d03 100644 --- a/cocos/renderer/core/effect.ts +++ b/cocos/renderer/core/effect.ts @@ -42,5 +42,3 @@ export class Effect { return passes; } } - -cc.Effect = Effect; diff --git a/playground/simple.html b/playground/simple.html index 8e89448a42..796a55398d 100644 --- a/playground/simple.html +++ b/playground/simple.html @@ -121,7 +121,6 @@ .map((t) => [Math.random(), Math.random(), Math.random(), 0.8]) .reduce((c, a) => a.concat(c), []); modelCom.mesh = cc.utils.createMesh(shape); - modelNode.setPosition(0, 3, 100); } let buttonDown = false, pos = cc.v2(); diff --git a/rollup/config.js b/rollup/config.js index b907d75a2a..0aa5009faf 100644 --- a/rollup/config.js +++ b/rollup/config.js @@ -39,12 +39,14 @@ export default { extensions: ['.js', '.ts'], presets: [ - '@babel/preset-env', + [ + '@babel/preset-env', + // { targets: { "esmodules": true } } + ], '@babel/preset-typescript' ], plugins: [ - //'./rollup/babel-plugin-mix-ts-js', ['@babel/plugin-proposal-decorators', { legacy: true }], From c099c576cb9c269405040fb69b3215f12491f390 Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Mon, 18 Feb 2019 17:33:17 +0800 Subject: [PATCH 2/2] seperated light UBO --- cocos/3d/assets/effect-asset.ts | 4 +- cocos/3d/builtin/effects.js | 42 +++++-- cocos/pipeline/forward/forward-pipeline.ts | 126 +++++++++++++++++++-- cocos/pipeline/render-pipeline.ts | 64 +---------- cocos/renderer/core/effect.ts | 1 + cocos/renderer/core/pass.ts | 5 + cocos/renderer/core/program-lib.ts | 18 +-- cocos/renderer/models/model-uniforms.ts | 4 +- cocos/renderer/ui/ui.ts | 2 +- 9 files changed, 173 insertions(+), 93 deletions(-) diff --git a/cocos/3d/assets/effect-asset.ts b/cocos/3d/assets/effect-asset.ts index 3cb9b4e16d..e541198d77 100644 --- a/cocos/3d/assets/effect-asset.ts +++ b/cocos/3d/assets/effect-asset.ts @@ -59,8 +59,8 @@ export interface IDefineInfo { } export interface IShaderInfo { name: string; - vert: string; - frag: string; + glsl3: { vert: string, frag: string }; + glsl1: { vert: string, frag: string }; defines: IDefineInfo[]; blocks: IBlockInfo[]; samplers: ISamplerInfo[]; diff --git a/cocos/3d/builtin/effects.js b/cocos/3d/builtin/effects.js index 12932537a0..39718eafb6 100644 --- a/cocos/3d/builtin/effects.js +++ b/cocos/3d/builtin/effects.js @@ -6,8 +6,14 @@ export default [ "shaders": [ { "name": "builtin-effect-particle-add|particle-vs-legacy:lpvs_main|particle-add-fs", - "vert": "\n uniform vec2 frameTile;\n uniform vec2 mainTiling;\n uniform vec2 mainOffset;\n uniform float velocityScale;\n uniform float lengthScale;\n#define MAX_DIR_LIGHTS 4\n#define MAX_POINT_LIGHTS 4\n#define MAX_SPOT_LIGHTS 4\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\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_cameraPos;\n uniform vec4 cc_dirLightDirection[4];\n uniform vec4 cc_dirLightColor[4];\n uniform vec4 cc_pointLightPositionAndRange[4];\n uniform vec4 cc_pointLightColor[4];\n uniform vec4 cc_spotLightPositionAndRange[4];\n uniform vec4 cc_spotLightDirection[4];\n uniform vec4 cc_spotLightColor[4];\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;\nvarying vec2 uv;\nvarying vec4 color;\nvoid computeVertPos(inout vec4 pos, vec2 vertOffset\n#if USE_BILLBOARD || USE_VERTICAL_BILLBOARD\n , mat4 view\n#endif\n#if USE_STRETCHED_BILLBOARD\n , vec3 eye\n , vec4 velocity\n , float velocityScale\n , float lengthScale\n , float size\n , float xIndex\n#endif\n) {\n#if USE_BILLBOARD\n vec3 camRight = normalize(vec3(view[0][0], view[1][0], view[2][0]));\n vec3 camUp = normalize(vec3(view[0][1], view[1][1], view[2][1]));\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#elif USE_STRETCHED_BILLBOARD\n vec3 camRight = normalize(cross(pos.xyz - eye, velocity.xyz));\n vec3 camUp = velocity.xyz * velocityScale + normalize(velocity.xyz) * lengthScale * size;\n pos.xyz += (camRight * abs(vertOffset.x) * sign(vertOffset.y)) - camUp * xIndex;\n#elif USE_HORIZONTAL_BILLBOARD\n vec3 camRight = vec3(1, 0, 0);\n vec3 camUp = vec3(0, 0, -1);\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#elif USE_VERTICAL_BILLBOARD\n vec3 camRight = normalize(vec3(view[0][0], view[1][0], view[2][0]));\n vec3 camUp = vec3(0, 1, 0);\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#else\n pos.x += vertOffset.x;\n pos.y += vertOffset.y;\n#endif\n}\nvec2 computeUV(float frameIndex, vec2 vertIndex, vec2 frameTile) {\n vec2 aniUV = vec2(0, floor(frameIndex * frameTile.y));\n aniUV.x = floor(frameIndex * frameTile.x * frameTile.y - aniUV.y * frameTile.x);\n \n vertIndex.x = 1. - vertIndex.x;\n return (aniUV.xy + vertIndex) / vec2(frameTile.x, frameTile.y);\n}\nvoid rotateCorner(inout vec2 corner, float angle) {\n float xOS = cos(angle) * corner.x - sin(angle) * corner.y;\n float yOS = sin(angle) * corner.x + cos(angle) * corner.y;\n corner.x = xOS;\n corner.y = yOS;\n}\nattribute vec3 a_position; \nattribute vec3 a_texCoord; \nattribute vec2 a_texCoord1; \nattribute vec4 a_color;\n#if USE_STRETCHED_BILLBOARD\nattribute vec3 a_color1; \n#endif\nvec4 lpvs_main() {\n vec4 pos = vec4(a_position, 1);\n#if USE_STRETCHED_BILLBOARD\n vec4 velocity = vec4(a_color1.xyz, 0);\n#endif\n#if !USE_WORLD_SPACE\n pos = cc_matWorld * pos;\n #if USE_STRETCHED_BILLBOARD\n velocity = cc_matWorld * velocity;\n #endif\n#endif\n vec2 cornerOffset = vec2((a_texCoord.xy - 0.5) * a_texCoord1.x);\n#if !USE_STRETCHED_BILLBOARD\n \n rotateCorner(cornerOffset, a_texCoord1.y);\n#endif\n computeVertPos(pos, cornerOffset\n #if USE_BILLBOARD || USE_VERTICAL_BILLBOARD\n , cc_matView\n #endif\n #if USE_STRETCHED_BILLBOARD\n , cc_cameraPos.xyz\n , velocity\n , velocityScale\n , lengthScale\n , a_texCoord1.x\n , a_texCoord.x\n #endif\n );\n pos = cc_matViewProj * pos;\n uv = computeUV(a_texCoord.z, a_texCoord.xy, frameTile) * mainTiling + mainOffset;\n color = a_color;\n return pos;\n}\nvoid main() { gl_Position = lpvs_main(); }\n", - "frag": "\n uniform sampler2D mainTexture;\n uniform vec4 tintColor;\n varying vec2 uv;\n varying vec4 color;\n void main () {\n \n gl_FragColor = 2.0 * color * tintColor * texture2D(mainTexture, uv);\n }\n", + "glsl3": { + "vert": "\nuniform Constants {\n vec2 frameTile;\n vec2 mainTiling;\n vec2 mainOffset;\n float velocityScale;\n float lengthScale;\n};\n#define MAX_DIR_LIGHTS 4\n#define MAX_POINT_LIGHTS 4\n#define MAX_SPOT_LIGHTS 4\nuniform CCGlobal {\n \n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n \n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos; \n \n vec4 cc_dirLightDirection[4]; \n vec4 cc_dirLightColor[4]; \n \n vec4 cc_pointLightPositionAndRange[4]; \n vec4 cc_pointLightColor[4]; \n \n vec4 cc_spotLightPositionAndRange[4]; \n vec4 cc_spotLightDirection[4]; \n vec4 cc_spotLightColor[4]; \n \n mat4 cc_matViewProjLight;\n vec4 cc_shadowParam1; \n vec4 cc_shadowParam2; \n};\nuniform CCLocal {\n mat4 cc_matWorld;\n mat4 cc_matWorldIT;\n};\nvarying vec2 uv;\nvarying vec4 color;\nvoid computeVertPos(inout vec4 pos, vec2 vertOffset\n#if USE_BILLBOARD || USE_VERTICAL_BILLBOARD\n , mat4 view\n#endif\n#if USE_STRETCHED_BILLBOARD\n , vec3 eye\n , vec4 velocity\n , float velocityScale\n , float lengthScale\n , float size\n , float xIndex\n#endif\n) {\n#if USE_BILLBOARD\n vec3 camRight = normalize(vec3(view[0][0], view[1][0], view[2][0]));\n vec3 camUp = normalize(vec3(view[0][1], view[1][1], view[2][1]));\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#elif USE_STRETCHED_BILLBOARD\n vec3 camRight = normalize(cross(pos.xyz - eye, velocity.xyz));\n vec3 camUp = velocity.xyz * velocityScale + normalize(velocity.xyz) * lengthScale * size;\n pos.xyz += (camRight * abs(vertOffset.x) * sign(vertOffset.y)) - camUp * xIndex;\n#elif USE_HORIZONTAL_BILLBOARD\n vec3 camRight = vec3(1, 0, 0);\n vec3 camUp = vec3(0, 0, -1);\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#elif USE_VERTICAL_BILLBOARD\n vec3 camRight = normalize(vec3(view[0][0], view[1][0], view[2][0]));\n vec3 camUp = vec3(0, 1, 0);\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#else\n pos.x += vertOffset.x;\n pos.y += vertOffset.y;\n#endif\n}\nvec2 computeUV(float frameIndex, vec2 vertIndex, vec2 frameTile) {\n vec2 aniUV = vec2(0, floor(frameIndex * frameTile.y));\n aniUV.x = floor(frameIndex * frameTile.x * frameTile.y - aniUV.y * frameTile.x);\n \n vertIndex.x = 1. - vertIndex.x;\n return (aniUV.xy + vertIndex) / vec2(frameTile.x, frameTile.y);\n}\nvoid rotateCorner(inout vec2 corner, float angle) {\n float xOS = cos(angle) * corner.x - sin(angle) * corner.y;\n float yOS = sin(angle) * corner.x + cos(angle) * corner.y;\n corner.x = xOS;\n corner.y = yOS;\n}\nattribute vec3 a_position; \nattribute vec3 a_texCoord; \nattribute vec2 a_texCoord1; \nattribute vec4 a_color;\n#if USE_STRETCHED_BILLBOARD\nattribute vec3 a_color1; \n#endif\nvec4 lpvs_main() {\n vec4 pos = vec4(a_position, 1);\n#if USE_STRETCHED_BILLBOARD\n vec4 velocity = vec4(a_color1.xyz, 0);\n#endif\n#if !USE_WORLD_SPACE\n pos = cc_matWorld * pos;\n #if USE_STRETCHED_BILLBOARD\n velocity = cc_matWorld * velocity;\n #endif\n#endif\n vec2 cornerOffset = vec2((a_texCoord.xy - 0.5) * a_texCoord1.x);\n#if !USE_STRETCHED_BILLBOARD\n \n rotateCorner(cornerOffset, a_texCoord1.y);\n#endif\n computeVertPos(pos, cornerOffset\n #if USE_BILLBOARD || USE_VERTICAL_BILLBOARD\n , cc_matView\n #endif\n #if USE_STRETCHED_BILLBOARD\n , cc_cameraPos.xyz\n , velocity\n , velocityScale\n , lengthScale\n , a_texCoord1.x\n , a_texCoord.x\n #endif\n );\n pos = cc_matViewProj * pos;\n uv = computeUV(a_texCoord.z, a_texCoord.xy, frameTile) * mainTiling + mainOffset;\n color = a_color;\n return pos;\n}\nvoid main() { gl_Position = lpvs_main(); }\n", + "frag": "\n uniform sampler2D mainTexture;\n uniform FragConstants {\n vec4 tintColor;\n };\n varying vec2 uv;\n varying vec4 color;\n void main () {\n \n gl_FragColor = 2.0 * color * tintColor * texture2D(mainTexture, uv);\n }\n" + }, + "glsl1": { + "vert": "\n uniform vec2 frameTile;\n uniform vec2 mainTiling;\n uniform vec2 mainOffset;\n uniform float velocityScale;\n uniform float lengthScale;\n#define MAX_DIR_LIGHTS 4\n#define MAX_POINT_LIGHTS 4\n#define MAX_SPOT_LIGHTS 4\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\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_cameraPos;\n uniform vec4 cc_dirLightDirection[4];\n uniform vec4 cc_dirLightColor[4];\n uniform vec4 cc_pointLightPositionAndRange[4];\n uniform vec4 cc_pointLightColor[4];\n uniform vec4 cc_spotLightPositionAndRange[4];\n uniform vec4 cc_spotLightDirection[4];\n uniform vec4 cc_spotLightColor[4];\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;\nvarying vec2 uv;\nvarying vec4 color;\nvoid computeVertPos(inout vec4 pos, vec2 vertOffset\n#if USE_BILLBOARD || USE_VERTICAL_BILLBOARD\n , mat4 view\n#endif\n#if USE_STRETCHED_BILLBOARD\n , vec3 eye\n , vec4 velocity\n , float velocityScale\n , float lengthScale\n , float size\n , float xIndex\n#endif\n) {\n#if USE_BILLBOARD\n vec3 camRight = normalize(vec3(view[0][0], view[1][0], view[2][0]));\n vec3 camUp = normalize(vec3(view[0][1], view[1][1], view[2][1]));\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#elif USE_STRETCHED_BILLBOARD\n vec3 camRight = normalize(cross(pos.xyz - eye, velocity.xyz));\n vec3 camUp = velocity.xyz * velocityScale + normalize(velocity.xyz) * lengthScale * size;\n pos.xyz += (camRight * abs(vertOffset.x) * sign(vertOffset.y)) - camUp * xIndex;\n#elif USE_HORIZONTAL_BILLBOARD\n vec3 camRight = vec3(1, 0, 0);\n vec3 camUp = vec3(0, 0, -1);\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#elif USE_VERTICAL_BILLBOARD\n vec3 camRight = normalize(vec3(view[0][0], view[1][0], view[2][0]));\n vec3 camUp = vec3(0, 1, 0);\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#else\n pos.x += vertOffset.x;\n pos.y += vertOffset.y;\n#endif\n}\nvec2 computeUV(float frameIndex, vec2 vertIndex, vec2 frameTile) {\n vec2 aniUV = vec2(0, floor(frameIndex * frameTile.y));\n aniUV.x = floor(frameIndex * frameTile.x * frameTile.y - aniUV.y * frameTile.x);\n \n vertIndex.x = 1. - vertIndex.x;\n return (aniUV.xy + vertIndex) / vec2(frameTile.x, frameTile.y);\n}\nvoid rotateCorner(inout vec2 corner, float angle) {\n float xOS = cos(angle) * corner.x - sin(angle) * corner.y;\n float yOS = sin(angle) * corner.x + cos(angle) * corner.y;\n corner.x = xOS;\n corner.y = yOS;\n}\nattribute vec3 a_position; \nattribute vec3 a_texCoord; \nattribute vec2 a_texCoord1; \nattribute vec4 a_color;\n#if USE_STRETCHED_BILLBOARD\nattribute vec3 a_color1; \n#endif\nvec4 lpvs_main() {\n vec4 pos = vec4(a_position, 1);\n#if USE_STRETCHED_BILLBOARD\n vec4 velocity = vec4(a_color1.xyz, 0);\n#endif\n#if !USE_WORLD_SPACE\n pos = cc_matWorld * pos;\n #if USE_STRETCHED_BILLBOARD\n velocity = cc_matWorld * velocity;\n #endif\n#endif\n vec2 cornerOffset = vec2((a_texCoord.xy - 0.5) * a_texCoord1.x);\n#if !USE_STRETCHED_BILLBOARD\n \n rotateCorner(cornerOffset, a_texCoord1.y);\n#endif\n computeVertPos(pos, cornerOffset\n #if USE_BILLBOARD || USE_VERTICAL_BILLBOARD\n , cc_matView\n #endif\n #if USE_STRETCHED_BILLBOARD\n , cc_cameraPos.xyz\n , velocity\n , velocityScale\n , lengthScale\n , a_texCoord1.x\n , a_texCoord.x\n #endif\n );\n pos = cc_matViewProj * pos;\n uv = computeUV(a_texCoord.z, a_texCoord.xy, frameTile) * mainTiling + mainOffset;\n color = a_color;\n return pos;\n}\nvoid main() { gl_Position = lpvs_main(); }\n", + "frag": "\n uniform sampler2D mainTexture;\n uniform vec4 tintColor;\n varying vec2 uv;\n varying vec4 color;\n void main () {\n \n gl_FragColor = 2.0 * color * tintColor * texture2D(mainTexture, uv);\n }\n" + }, "defines": [ {"name":"USE_BILLBOARD", "type":"boolean", "defines":[]}, {"name":"USE_STRETCHED_BILLBOARD", "type":"boolean", "defines":[]}, @@ -40,8 +46,14 @@ export default [ "shaders": [ { "name": "builtin-effect-skybox|sky-vs|sky-fs", - "vert": "\n attribute vec3 a_position;\n \n#define MAX_DIR_LIGHTS 4\n#define MAX_POINT_LIGHTS 4\n#define MAX_SPOT_LIGHTS 4\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\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_cameraPos;\n uniform vec4 cc_dirLightDirection[4];\n uniform vec4 cc_dirLightColor[4];\n uniform vec4 cc_pointLightPositionAndRange[4];\n uniform vec4 cc_pointLightColor[4];\n uniform vec4 cc_spotLightPositionAndRange[4];\n uniform vec4 cc_spotLightDirection[4];\n uniform vec4 cc_spotLightColor[4];\n uniform mat4 cc_matViewProjLight;\n uniform vec4 cc_shadowParam1;\n uniform vec4 cc_shadowParam2;\n varying vec3 viewDir;\n void main() {\n mat4 matViewRotOnly = mat4(mat3(cc_matView));\n vec4 clipPos = cc_matProj * matViewRotOnly * vec4(a_position, 1.0);\n gl_Position = clipPos;\n gl_Position.z = clipPos.w * 0.99999;\n viewDir = a_position;\n }\n", - "frag": "\n varying vec3 viewDir;\n uniform samplerCube cubeMap;\n \nvec3 gammaToLinearSpaceRGB(vec3 sRGB) { \n return sRGB * (sRGB * (sRGB * 0.305306011 + 0.682171111) + 0.012522878);\n}\nvec3 linearToGammaSpaceRGB(vec3 RGB) { \n vec3 S1 = sqrt(RGB);\n vec3 S2 = sqrt(S1);\n vec3 S3 = sqrt(S2);\n return 0.585122381 * S1 + 0.783140355 * S2 - 0.368262736 * S3;\n}\nvec4 gammaToLinearSpaceRGBA(vec4 sRGBA) {\n return vec4(gammaToLinearSpaceRGB(sRGBA.rgb), sRGBA.a);\n}\nvec4 linearToGammaSpaceRGBA(vec4 RGBA) {\n return vec4(linearToGammaSpaceRGB(RGBA.rgb), RGBA.a);\n}\nfloat gammaToLinearSpaceExact(float val) {\n if (val <= 0.04045) {\n return val / 12.92;\n } else if (val < 1.0) {\n return pow((val + 0.055) / 1.055, 2.4);\n } else {\n return pow(val, 2.2);\n }\n}\nfloat linearToGammaSpaceExact(float val) {\n if (val <= 0.0) {\n return 0.0;\n } else if (val <= 0.0031308) {\n return 12.92 * val;\n } else if (val < 1.0) {\n return 1.055 * pow(val, 0.4166667) - 0.055;\n } else {\n return pow(val, 0.45454545);\n }\n}\n \nvec3 unpackNormal(vec4 nmap) {\n return nmap.xyz * 2.0 - 1.0;\n}\nvec3 unpackRGBE(vec4 rgbe) {\n return rgbe.rgb * pow(2.0, rgbe.a * 255.0 - 128.0);\n}\n void main() {\n #if USE_RGBE_CUBEMAP\n vec3 c = unpackRGBE(textureCube(cubeMap, viewDir));\n c = linearToGammaSpaceRGB(c / (1.0 + c));\n gl_FragColor = vec4(c, 1.0);\n #else\n gl_FragColor = textureCube(cubeMap, viewDir);\n #endif\n }\n", + "glsl3": { + "vert": "\n attribute vec3 a_position;\n \n#define MAX_DIR_LIGHTS 4\n#define MAX_POINT_LIGHTS 4\n#define MAX_SPOT_LIGHTS 4\nuniform CCGlobal {\n \n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n \n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos; \n \n vec4 cc_dirLightDirection[4]; \n vec4 cc_dirLightColor[4]; \n \n vec4 cc_pointLightPositionAndRange[4]; \n vec4 cc_pointLightColor[4]; \n \n vec4 cc_spotLightPositionAndRange[4]; \n vec4 cc_spotLightDirection[4]; \n vec4 cc_spotLightColor[4]; \n \n mat4 cc_matViewProjLight;\n vec4 cc_shadowParam1; \n vec4 cc_shadowParam2; \n};\n varying vec3 viewDir;\n void main() {\n mat4 matViewRotOnly = mat4(mat3(cc_matView));\n vec4 clipPos = cc_matProj * matViewRotOnly * vec4(a_position, 1.0);\n gl_Position = clipPos;\n gl_Position.z = clipPos.w * 0.99999;\n viewDir = a_position;\n }\n", + "frag": "\n varying vec3 viewDir;\n uniform samplerCube cubeMap;\n \nvec3 gammaToLinearSpaceRGB(vec3 sRGB) { \n return sRGB * (sRGB * (sRGB * 0.305306011 + 0.682171111) + 0.012522878);\n}\nvec3 linearToGammaSpaceRGB(vec3 RGB) { \n vec3 S1 = sqrt(RGB);\n vec3 S2 = sqrt(S1);\n vec3 S3 = sqrt(S2);\n return 0.585122381 * S1 + 0.783140355 * S2 - 0.368262736 * S3;\n}\nvec4 gammaToLinearSpaceRGBA(vec4 sRGBA) {\n return vec4(gammaToLinearSpaceRGB(sRGBA.rgb), sRGBA.a);\n}\nvec4 linearToGammaSpaceRGBA(vec4 RGBA) {\n return vec4(linearToGammaSpaceRGB(RGBA.rgb), RGBA.a);\n}\nfloat gammaToLinearSpaceExact(float val) {\n if (val <= 0.04045) {\n return val / 12.92;\n } else if (val < 1.0) {\n return pow((val + 0.055) / 1.055, 2.4);\n } else {\n return pow(val, 2.2);\n }\n}\nfloat linearToGammaSpaceExact(float val) {\n if (val <= 0.0) {\n return 0.0;\n } else if (val <= 0.0031308) {\n return 12.92 * val;\n } else if (val < 1.0) {\n return 1.055 * pow(val, 0.4166667) - 0.055;\n } else {\n return pow(val, 0.45454545);\n }\n}\n \nvec3 unpackNormal(vec4 nmap) {\n return nmap.xyz * 2.0 - 1.0;\n}\nvec3 unpackRGBE(vec4 rgbe) {\n return rgbe.rgb * pow(2.0, rgbe.a * 255.0 - 128.0);\n}\n void main() {\n #if USE_RGBE_CUBEMAP\n vec3 c = unpackRGBE(textureCube(cubeMap, viewDir));\n c = linearToGammaSpaceRGB(c / (1.0 + c));\n gl_FragColor = vec4(c, 1.0);\n #else\n gl_FragColor = textureCube(cubeMap, viewDir);\n #endif\n }\n" + }, + "glsl1": { + "vert": "\n attribute vec3 a_position;\n \n#define MAX_DIR_LIGHTS 4\n#define MAX_POINT_LIGHTS 4\n#define MAX_SPOT_LIGHTS 4\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\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_cameraPos;\n uniform vec4 cc_dirLightDirection[4];\n uniform vec4 cc_dirLightColor[4];\n uniform vec4 cc_pointLightPositionAndRange[4];\n uniform vec4 cc_pointLightColor[4];\n uniform vec4 cc_spotLightPositionAndRange[4];\n uniform vec4 cc_spotLightDirection[4];\n uniform vec4 cc_spotLightColor[4];\n uniform mat4 cc_matViewProjLight;\n uniform vec4 cc_shadowParam1;\n uniform vec4 cc_shadowParam2;\n varying vec3 viewDir;\n void main() {\n mat4 matViewRotOnly = mat4(mat3(cc_matView));\n vec4 clipPos = cc_matProj * matViewRotOnly * vec4(a_position, 1.0);\n gl_Position = clipPos;\n gl_Position.z = clipPos.w * 0.99999;\n viewDir = a_position;\n }\n", + "frag": "\n varying vec3 viewDir;\n uniform samplerCube cubeMap;\n \nvec3 gammaToLinearSpaceRGB(vec3 sRGB) { \n return sRGB * (sRGB * (sRGB * 0.305306011 + 0.682171111) + 0.012522878);\n}\nvec3 linearToGammaSpaceRGB(vec3 RGB) { \n vec3 S1 = sqrt(RGB);\n vec3 S2 = sqrt(S1);\n vec3 S3 = sqrt(S2);\n return 0.585122381 * S1 + 0.783140355 * S2 - 0.368262736 * S3;\n}\nvec4 gammaToLinearSpaceRGBA(vec4 sRGBA) {\n return vec4(gammaToLinearSpaceRGB(sRGBA.rgb), sRGBA.a);\n}\nvec4 linearToGammaSpaceRGBA(vec4 RGBA) {\n return vec4(linearToGammaSpaceRGB(RGBA.rgb), RGBA.a);\n}\nfloat gammaToLinearSpaceExact(float val) {\n if (val <= 0.04045) {\n return val / 12.92;\n } else if (val < 1.0) {\n return pow((val + 0.055) / 1.055, 2.4);\n } else {\n return pow(val, 2.2);\n }\n}\nfloat linearToGammaSpaceExact(float val) {\n if (val <= 0.0) {\n return 0.0;\n } else if (val <= 0.0031308) {\n return 12.92 * val;\n } else if (val < 1.0) {\n return 1.055 * pow(val, 0.4166667) - 0.055;\n } else {\n return pow(val, 0.45454545);\n }\n}\n \nvec3 unpackNormal(vec4 nmap) {\n return nmap.xyz * 2.0 - 1.0;\n}\nvec3 unpackRGBE(vec4 rgbe) {\n return rgbe.rgb * pow(2.0, rgbe.a * 255.0 - 128.0);\n}\n void main() {\n #if USE_RGBE_CUBEMAP\n vec3 c = unpackRGBE(textureCube(cubeMap, viewDir));\n c = linearToGammaSpaceRGB(c / (1.0 + c));\n gl_FragColor = vec4(c, 1.0);\n #else\n gl_FragColor = textureCube(cubeMap, viewDir);\n #endif\n }\n" + }, "defines": [ {"name":"USE_RGBE_CUBEMAP", "type":"boolean", "defines":[]} ], @@ -55,12 +67,18 @@ export default [ }, { "name": "builtin-effect-sprite", - "techniques": [{"passes":[{"depthStencilState":{"depthTest":false, "depthWrite":false}, "blendState":{"targets":[{"blend":true, "blendSrc":2, "blendDst":4, "blendSrcAlpha":1, "blendDstAlpha":1}]}, "program":"builtin-effect-sprite|sprite-vs:vert|sprite-fs:frag", "properties":{"mainTexture":{"type":29, "value":"white"}}}]}], + "techniques": [{"passes":[{"depthStencilState":{"depthTest":true, "depthWrite":false}, "blendState":{"targets":[{"blend":true, "blendSrc":2, "blendDst":4, "blendDstAlpha":4}]}, "program":"builtin-effect-sprite|sprite-vs:vert|sprite-fs:frag", "properties":{"mainTexture":{"type":29, "value":"white"}}}]}], "shaders": [ { "name": "builtin-effect-sprite|sprite-vs:vert|sprite-fs:frag", - "vert": "\nattribute vec3 a_position;\nattribute vec2 a_texCoord;\nattribute vec4 a_color;\nvarying vec2 uv0;\nvarying vec4 color;\n uniform mat4 cc_matViewProj;\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n pos = cc_matViewProj * pos;\n uv0 = a_texCoord;\n color = a_color;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n", - "frag": "\nuniform sampler2D mainTexture;\nvarying vec2 uv0;\nvarying vec4 color;\nvec4 frag () {\n vec4 o = vec4(1, 1, 1, 1);\n o *= texture2D(mainTexture, uv0);\n o *= color;\n return o;\n}\nvoid main() { gl_FragColor = frag(); }\n", + "glsl3": { + "vert": "\nattribute vec3 a_position;\nattribute vec2 a_texCoord;\nattribute vec4 a_color;\nvarying vec2 uv0;\nvarying vec4 color;\nuniform ui {\n mat4 cc_matViewProj;\n};\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n pos = cc_matViewProj * pos;\n uv0 = a_texCoord;\n color = a_color;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n", + "frag": "\nuniform sampler2D mainTexture;\nvarying vec2 uv0;\nvarying vec4 color;\nvec4 frag () {\n vec4 o = vec4(1, 1, 1, 1);\n o *= texture2D(mainTexture, uv0);\n o *= color;\n return o;\n}\nvoid main() { gl_FragColor = frag(); }\n" + }, + "glsl1": { + "vert": "\nattribute vec3 a_position;\nattribute vec2 a_texCoord;\nattribute vec4 a_color;\nvarying vec2 uv0;\nvarying vec4 color;\n uniform mat4 cc_matViewProj;\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n pos = cc_matViewProj * pos;\n uv0 = a_texCoord;\n color = a_color;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n", + "frag": "\nuniform sampler2D mainTexture;\nvarying vec2 uv0;\nvarying vec4 color;\nvec4 frag () {\n vec4 o = vec4(1, 1, 1, 1);\n o *= texture2D(mainTexture, uv0);\n o *= color;\n return o;\n}\nvoid main() { gl_FragColor = frag(); }\n" + }, "defines": [], "blocks": [ {"name": "ui", "size": 64, "defines": [], "binding": 0, "members": [ @@ -80,8 +98,14 @@ export default [ "shaders": [ { "name": "builtin-effect-unlit|unlit-vs:vert|unlit-fs:frag", - "vert": "\nattribute vec3 a_position;\n#define MAX_DIR_LIGHTS 4\n#define MAX_POINT_LIGHTS 4\n#define MAX_SPOT_LIGHTS 4\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\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_cameraPos;\n uniform vec4 cc_dirLightDirection[4];\n uniform vec4 cc_dirLightColor[4];\n uniform vec4 cc_pointLightPositionAndRange[4];\n uniform vec4 cc_pointLightColor[4];\n uniform vec4 cc_spotLightPositionAndRange[4];\n uniform vec4 cc_spotLightDirection[4];\n uniform vec4 cc_spotLightColor[4];\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_texCoord;\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[128];\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_texCoord * 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", + "glsl3": { + "vert": "\nattribute vec3 a_position;\n#define MAX_DIR_LIGHTS 4\n#define MAX_POINT_LIGHTS 4\n#define MAX_SPOT_LIGHTS 4\nuniform CCGlobal {\n \n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n \n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos; \n \n vec4 cc_dirLightDirection[4]; \n vec4 cc_dirLightColor[4]; \n \n vec4 cc_pointLightPositionAndRange[4]; \n vec4 cc_pointLightColor[4]; \n \n vec4 cc_spotLightPositionAndRange[4]; \n vec4 cc_spotLightDirection[4]; \n vec4 cc_spotLightColor[4]; \n \n mat4 cc_matViewProjLight;\n vec4 cc_shadowParam1; \n vec4 cc_shadowParam2; \n};\nuniform CCLocal {\n mat4 cc_matWorld;\n mat4 cc_matWorldIT;\n};\n#if USE_TEXTURE\n attribute vec2 a_texCoord;\n uniform TexCoords {\n vec2 mainTiling;\n vec2 mainOffset;\n };\n varying vec2 uv0;\n#endif\n#if CC_USE_SKINNING\n \nattribute vec4 a_weights;\nattribute vec4 a_joints;\nuniform CCSkinning {\n mat4 cc_matJoint[128];\n vec4 cc_jointsTextureSize;\n};\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_texCoord * 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 Constant {\n vec4 color;\n };\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" + }, + "glsl1": { + "vert": "\nattribute vec3 a_position;\n#define MAX_DIR_LIGHTS 4\n#define MAX_POINT_LIGHTS 4\n#define MAX_SPOT_LIGHTS 4\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\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_cameraPos;\n uniform vec4 cc_dirLightDirection[4];\n uniform vec4 cc_dirLightColor[4];\n uniform vec4 cc_pointLightPositionAndRange[4];\n uniform vec4 cc_pointLightColor[4];\n uniform vec4 cc_spotLightPositionAndRange[4];\n uniform vec4 cc_spotLightDirection[4];\n uniform vec4 cc_spotLightColor[4];\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_texCoord;\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[128];\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_texCoord * 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":[]}, diff --git a/cocos/pipeline/forward/forward-pipeline.ts b/cocos/pipeline/forward/forward-pipeline.ts index af8351d9a4..9083eb7886 100644 --- a/cocos/pipeline/forward/forward-pipeline.ts +++ b/cocos/pipeline/forward/forward-pipeline.ts @@ -1,16 +1,60 @@ import { Root } from '../../core/root'; -import { GFXBufferUsageBit, GFXMemoryUsageBit } from '../../gfx/define'; +import { GFXBuffer } from '../../gfx/buffer'; +import { GFXBufferUsageBit, GFXMemoryUsageBit, GFXType } from '../../gfx/define'; import { GFXRenderPass } from '../../gfx/render-pass'; +import { GFXUniformBlock } from '../../gfx/shader'; import { RenderPassStage } from '../define'; -import { RenderPipeline, UBOGlobal } from '../render-pipeline'; +import { RenderPipeline } from '../render-pipeline'; +import { RenderView } from '../render-view'; import { ForwardFlow } from './forward-flow'; export enum ForwardFlowPriority { FORWARD = 0, } +// tslint:disable: max-line-length +export class UBOForwardLights { + public static MAX_DIR_LIGHTS = 4; + public static MAX_POINT_LIGHTS = 4; + public static MAX_SPOT_LIGHTS = 4; + + public static DIR_LIGHT_DIR_OFFSET: number = 0; + public static DIR_LIGHT_COLOR_OFFSET: number = UBOForwardLights.DIR_LIGHT_DIR_OFFSET + UBOForwardLights.MAX_DIR_LIGHTS * 4; + public static POINT_LIGHT_POS_RANGE_OFFSET: number = UBOForwardLights.DIR_LIGHT_COLOR_OFFSET + UBOForwardLights.MAX_DIR_LIGHTS * 4; + public static POINT_LIGHT_COLOR_OFFSET: number = UBOForwardLights.POINT_LIGHT_POS_RANGE_OFFSET + UBOForwardLights.MAX_POINT_LIGHTS * 4; + public static SPOT_LIGHT_POS_RANGE_OFFSET: number = UBOForwardLights.POINT_LIGHT_COLOR_OFFSET + UBOForwardLights.MAX_POINT_LIGHTS * 4; + public static SPOT_LIGHT_DIR_OFFSET: number = UBOForwardLights.SPOT_LIGHT_POS_RANGE_OFFSET + UBOForwardLights.MAX_SPOT_LIGHTS * 4; + public static SPOT_LIGHT_COLOR_OFFSET: number = UBOForwardLights.SPOT_LIGHT_DIR_OFFSET + UBOForwardLights.MAX_SPOT_LIGHTS * 4; + public static COUNT: number = UBOForwardLights.SPOT_LIGHT_COLOR_OFFSET + UBOForwardLights.MAX_SPOT_LIGHTS * 4; + public static SIZE: number = UBOForwardLights.COUNT * 4; + + public static BLOCK: GFXUniformBlock = { + binding: 29, name: 'CCForwardLights', members: [ + { name: 'cc_dirLightDirection', type: GFXType.FLOAT4, count: UBOForwardLights.MAX_DIR_LIGHTS }, + { name: 'cc_dirLightColor', type: GFXType.FLOAT4, count: UBOForwardLights.MAX_DIR_LIGHTS }, + { name: 'cc_pointLightPositionAndRange', type: GFXType.FLOAT4, count: UBOForwardLights.MAX_POINT_LIGHTS }, + { name: 'cc_pointLightColor', type: GFXType.FLOAT4, count: UBOForwardLights.MAX_POINT_LIGHTS }, + { name: 'cc_spotLightPositionAndRange', type: GFXType.FLOAT4, count: UBOForwardLights.MAX_SPOT_LIGHTS }, + { name: 'cc_spotLightDirection', type: GFXType.FLOAT4, count: UBOForwardLights.MAX_SPOT_LIGHTS }, + { name: 'cc_spotLightColor', type: GFXType.FLOAT4, count: UBOForwardLights.MAX_SPOT_LIGHTS }, + ], + }; + + public view: Float32Array = new Float32Array(UBOForwardLights.COUNT); +} +// tslint:enable: max-line-length + +const _idVec4Array = Float32Array.from([0, 0, 0, 1]); + export class ForwardPipeline extends RenderPipeline { + protected _uboLights: UBOForwardLights = new UBOForwardLights(); + protected _lightsUBO: GFXBuffer | null = null; + + public get lightsUBO (): GFXBuffer { + return this._lightsUBO!; + } + constructor (root: Root) { super(root); } @@ -39,13 +83,6 @@ export class ForwardPipeline extends RenderPipeline { this.addRenderPass(RenderPassStage.DEFAULT, windowPass); - // create UBOs - this._globalUBO = this._root.device.createBuffer({ - usage: GFXBufferUsageBit.UNIFORM | GFXBufferUsageBit.TRANSFER_DST, - memUsage: GFXMemoryUsageBit.HOST, - size: UBOGlobal.SIZE, - }); - // create flows this.createFlow(ForwardFlow, { name: 'ForwardFlow', @@ -62,4 +99,75 @@ export class ForwardPipeline extends RenderPipeline { this.destroyQuadInputAssembler(); } + protected createUBOs (): boolean { + super.createUBOs(); + + this._lightsUBO = this._root.device.createBuffer({ + usage: GFXBufferUsageBit.UNIFORM | GFXBufferUsageBit.TRANSFER_DST, + memUsage: GFXMemoryUsageBit.HOST, + size: UBOForwardLights.SIZE, + }); + + if (!this._lightsUBO) { + return false; + } + + return true; + } + + protected destroyUBOs () { + super.destroyUBOs(); + + if (this._lightsUBO) { + this._lightsUBO.destroy(); + this._lightsUBO = null; + } + } + + protected updateUBOs (view: RenderView) { + super.updateUBOs(view); + + const scene = view.camera.scene; + const dLights = scene.directionalLights; + for (let i = 0; i < UBOForwardLights.MAX_DIR_LIGHTS; i++) { + const light = dLights[i]; + if (light && light.enabled) { + light.update(); + this._uboLights.view.set(light.directionArray, UBOForwardLights.DIR_LIGHT_DIR_OFFSET + i * 4); + this._uboLights.view.set(light.colorData, UBOForwardLights.DIR_LIGHT_COLOR_OFFSET + i * 4); + } else { + this._uboLights.view.set(_idVec4Array, UBOForwardLights.DIR_LIGHT_DIR_OFFSET + i * 4); + this._uboLights.view.set(_idVec4Array, UBOForwardLights.DIR_LIGHT_COLOR_OFFSET + i * 4); + } + } + const pLights = scene.pointLights; + for (let i = 0; i < UBOForwardLights.MAX_POINT_LIGHTS; i++) { + const light = pLights[i]; + if (light && light.enabled) { + light.update(); + this._uboLights.view.set(light.positionAndRange, UBOForwardLights.POINT_LIGHT_POS_RANGE_OFFSET + i * 4); + this._uboLights.view.set(light.colorData, UBOForwardLights.POINT_LIGHT_COLOR_OFFSET + i * 4); + } else { + this._uboLights.view.set(_idVec4Array, UBOForwardLights.POINT_LIGHT_POS_RANGE_OFFSET + i * 4); + this._uboLights.view.set(_idVec4Array, UBOForwardLights.POINT_LIGHT_COLOR_OFFSET + i * 4); + } + } + const sLights = scene.spotLights; + for (let i = 0; i < UBOForwardLights.MAX_SPOT_LIGHTS; i++) { + const light = sLights[i]; + if (light && light.enabled) { + light.update(); + this._uboLights.view.set(light.positionAndRange, UBOForwardLights.SPOT_LIGHT_POS_RANGE_OFFSET + i * 4); + this._uboLights.view.set(light.directionArray, UBOForwardLights.SPOT_LIGHT_DIR_OFFSET + i * 4); + this._uboLights.view.set(light.colorData, UBOForwardLights.SPOT_LIGHT_COLOR_OFFSET + i * 4); + } else { + this._uboLights.view.set(_idVec4Array, UBOForwardLights.SPOT_LIGHT_POS_RANGE_OFFSET + i * 4); + this._uboLights.view.set(_idVec4Array, UBOForwardLights.SPOT_LIGHT_DIR_OFFSET + i * 4); + this._uboLights.view.set(_idVec4Array, UBOForwardLights.SPOT_LIGHT_COLOR_OFFSET + i * 4); + } + } + + // update ubos + this._lightsUBO!.update(this._uboLights.view); + } } diff --git a/cocos/pipeline/render-pipeline.ts b/cocos/pipeline/render-pipeline.ts index 707843d03a..8da2d9849c 100644 --- a/cocos/pipeline/render-pipeline.ts +++ b/cocos/pipeline/render-pipeline.ts @@ -14,12 +14,7 @@ import { IRenderFlowInfo, RenderFlow } from './render-flow'; import { RenderQueue } from './render-queue'; import { RenderView } from './render-view'; -// tslint:disable: max-line-length export class UBOGlobal { - public static MAX_DIR_LIGHTS = 4; - public static MAX_POINT_LIGHTS = 4; - public static MAX_SPOT_LIGHTS = 4; - public static TIME_OFFSET: number = 0; public static SCREEN_SIZE_OFFSET: number = UBOGlobal.TIME_OFFSET + 4; public static SCREEN_SCALE_OFFSET: number = UBOGlobal.SCREEN_SIZE_OFFSET + 4; @@ -30,14 +25,7 @@ export class UBOGlobal { public static MAT_VIEW_PROJ_OFFSET: number = UBOGlobal.MAT_PROJ_INV_OFFSET + 16; public static MAT_VIEW_PROJ_INV_OFFSET: number = UBOGlobal.MAT_VIEW_PROJ_OFFSET + 16; public static CAMERA_POS_OFFSET: number = UBOGlobal.MAT_VIEW_PROJ_INV_OFFSET + 16; - public static DIR_LIGHT_DIR_OFFSET: number = UBOGlobal.CAMERA_POS_OFFSET + 4; - public static DIR_LIGHT_COLOR_OFFSET: number = UBOGlobal.DIR_LIGHT_DIR_OFFSET + UBOGlobal.MAX_DIR_LIGHTS * 4; - public static POINT_LIGHT_POS_RANGE_OFFSET: number = UBOGlobal.DIR_LIGHT_COLOR_OFFSET + UBOGlobal.MAX_DIR_LIGHTS * 4; - public static POINT_LIGHT_COLOR_OFFSET: number = UBOGlobal.POINT_LIGHT_POS_RANGE_OFFSET + UBOGlobal.MAX_POINT_LIGHTS * 4; - public static SPOT_LIGHT_POS_RANGE_OFFSET: number = UBOGlobal.POINT_LIGHT_COLOR_OFFSET + UBOGlobal.MAX_POINT_LIGHTS * 4; - public static SPOT_LIGHT_DIR_OFFSET: number = UBOGlobal.SPOT_LIGHT_POS_RANGE_OFFSET + UBOGlobal.MAX_SPOT_LIGHTS * 4; - public static SPOT_LIGHT_COLOR_OFFSET: number = UBOGlobal.SPOT_LIGHT_DIR_OFFSET + UBOGlobal.MAX_SPOT_LIGHTS * 4; - public static COUNT: number = UBOGlobal.SPOT_LIGHT_COLOR_OFFSET + UBOGlobal.MAX_SPOT_LIGHTS * 4; + public static COUNT: number = UBOGlobal.CAMERA_POS_OFFSET + 4; public static SIZE: number = UBOGlobal.COUNT * 4; public static BLOCK: GFXUniformBlock = { @@ -54,20 +42,11 @@ export class UBOGlobal { { name: 'cc_matViewProj', type: GFXType.MAT4, count: 1 }, { name: 'cc_matViewProjInv', type: GFXType.MAT4, count: 1 }, { name: 'cc_cameraPos', type: GFXType.FLOAT4, count: 1 }, - // lights - { name: 'cc_dirLightDirection', type: GFXType.FLOAT4, count: UBOGlobal.MAX_DIR_LIGHTS }, - { name: 'cc_dirLightColor', type: GFXType.FLOAT4, count: UBOGlobal.MAX_DIR_LIGHTS }, - { name: 'cc_pointLightPositionAndRange', type: GFXType.FLOAT4, count: UBOGlobal.MAX_POINT_LIGHTS }, - { name: 'cc_pointLightColor', type: GFXType.FLOAT4, count: UBOGlobal.MAX_POINT_LIGHTS }, - { name: 'cc_spotLightPositionAndRange', type: GFXType.FLOAT4, count: UBOGlobal.MAX_SPOT_LIGHTS }, - { name: 'cc_spotLightDirection', type: GFXType.FLOAT4, count: UBOGlobal.MAX_SPOT_LIGHTS }, - { name: 'cc_spotLightColor', type: GFXType.FLOAT4, count: UBOGlobal.MAX_SPOT_LIGHTS }, ], }; public view: Float32Array = new Float32Array(UBOGlobal.COUNT); } -// tslint:enable: max-line-length export class UBOLocal { public static MAT_WORLD_OFFSET: number = 0; @@ -85,7 +64,6 @@ export class UBOLocal { public view: Float32Array = new Float32Array(UBOLocal.COUNT); } -const _idVec4Array = Float32Array.from([0, 0, 0, 1]); const _vec4Array = new Float32Array(4); const _mat4Array = new Float32Array(16); const _outMat = new Mat4(); @@ -359,46 +337,6 @@ export abstract class RenderPipeline { _vec4Array[3] = 1.0; this._uboGlobal.view.set(_vec4Array, UBOGlobal.CAMERA_POS_OFFSET); - const scene = view.camera.scene; - const dLights = scene.directionalLights; - for (let i = 0; i < UBOGlobal.MAX_DIR_LIGHTS; i++) { - const light = dLights[i]; - if (light && light.enabled) { - light.update(); - this._uboGlobal.view.set(light.directionArray, UBOGlobal.DIR_LIGHT_DIR_OFFSET + i * 4); - this._uboGlobal.view.set(light.colorData, UBOGlobal.DIR_LIGHT_COLOR_OFFSET + i * 4); - } else { - this._uboGlobal.view.set(_idVec4Array, UBOGlobal.DIR_LIGHT_DIR_OFFSET + i * 4); - this._uboGlobal.view.set(_idVec4Array, UBOGlobal.DIR_LIGHT_COLOR_OFFSET + i * 4); - } - } - const pLights = scene.pointLights; - for (let i = 0; i < UBOGlobal.MAX_POINT_LIGHTS; i++) { - const light = pLights[i]; - if (light && light.enabled) { - light.update(); - this._uboGlobal.view.set(light.positionAndRange, UBOGlobal.POINT_LIGHT_POS_RANGE_OFFSET + i * 4); - this._uboGlobal.view.set(light.colorData, UBOGlobal.POINT_LIGHT_COLOR_OFFSET + i * 4); - } else { - this._uboGlobal.view.set(_idVec4Array, UBOGlobal.POINT_LIGHT_POS_RANGE_OFFSET + i * 4); - this._uboGlobal.view.set(_idVec4Array, UBOGlobal.POINT_LIGHT_COLOR_OFFSET + i * 4); - } - } - const sLights = scene.spotLights; - for (let i = 0; i < UBOGlobal.MAX_SPOT_LIGHTS; i++) { - const light = sLights[i]; - if (light && light.enabled) { - light.update(); - this._uboGlobal.view.set(light.positionAndRange, UBOGlobal.SPOT_LIGHT_POS_RANGE_OFFSET + i * 4); - this._uboGlobal.view.set(light.directionArray, UBOGlobal.SPOT_LIGHT_DIR_OFFSET + i * 4); - this._uboGlobal.view.set(light.colorData, UBOGlobal.SPOT_LIGHT_COLOR_OFFSET + i * 4); - } else { - this._uboGlobal.view.set(_idVec4Array, UBOGlobal.SPOT_LIGHT_POS_RANGE_OFFSET + i * 4); - this._uboGlobal.view.set(_idVec4Array, UBOGlobal.SPOT_LIGHT_DIR_OFFSET + i * 4); - this._uboGlobal.view.set(_idVec4Array, UBOGlobal.SPOT_LIGHT_COLOR_OFFSET + i * 4); - } - } - // update ubos this._globalUBO!.update(this._uboGlobal.view); } diff --git a/cocos/renderer/core/effect.ts b/cocos/renderer/core/effect.ts index 0e89ef1d03..b7092dd981 100644 --- a/cocos/renderer/core/effect.ts +++ b/cocos/renderer/core/effect.ts @@ -31,6 +31,7 @@ export class Effect { passInfo.blocks = prog.blocks; passInfo.samplers = prog.samplers; passInfo.globals = cc.director.root.pipeline.globalUBO; + passInfo.lights = cc.director.root.pipeline.lightsUBO; if (shader) { passInfo.shader = shader; } else { console.warn(`create shader ${passInfo.program} failed`); } if (renderPass) { passInfo.renderPass = renderPass; } diff --git a/cocos/renderer/core/pass.ts b/cocos/renderer/core/pass.ts index 24b89048de..747697cc3a 100644 --- a/cocos/renderer/core/pass.ts +++ b/cocos/renderer/core/pass.ts @@ -17,6 +17,7 @@ import { GFXSampler } from '../../gfx/sampler'; import { GFXShader } from '../../gfx/shader'; import { GFXTextureView } from '../../gfx/texture-view'; import { RenderPassStage, RenderPriority } from '../../pipeline/define'; +import { UBOForwardLights } from '../../pipeline/forward/forward-pipeline'; import { UBOGlobal } from '../../pipeline/render-pipeline'; export interface IPassInfoFull extends IPassInfo { @@ -26,6 +27,7 @@ export interface IPassInfoFull extends IPassInfo { shader: GFXShader; renderPass: GFXRenderPass; globals: GFXBuffer; + lights: GFXBuffer; } export type PassOverrides = RecursivePartial; @@ -122,6 +124,7 @@ export class Pass { protected _shader: GFXShader | null = null; protected _renderPass: GFXRenderPass | null = null; protected _globalUBO: GFXBuffer | null = null; + protected _lightsUBO: GFXBuffer | null = null; public constructor (device: GFXDevice) { this._device = device; @@ -139,6 +142,7 @@ export class Pass { this._shader = info.shader; this._renderPass = info.renderPass; this._globalUBO = info.globals; + this._lightsUBO = info.lights; this._fillinPipelineInfo(info); for (const u of info.blocks) { @@ -292,6 +296,7 @@ export class Pass { bindingLayout.bindTextureView(parseInt(t), this._textureViews[t]); } bindingLayout.bindBuffer(UBOGlobal.BLOCK.binding, this._globalUBO!); + bindingLayout.bindBuffer(UBOForwardLights.BLOCK.binding, this._lightsUBO!); const pipelineLayout = this._device.createPipelineLayout({ layouts: [bindingLayout] }); const pipelineState = this._device.createPipelineState({ bs: this._bs, diff --git a/cocos/renderer/core/program-lib.ts b/cocos/renderer/core/program-lib.ts index 6bfa9afe00..53566f8272 100644 --- a/cocos/renderer/core/program-lib.ts +++ b/cocos/renderer/core/program-lib.ts @@ -2,11 +2,12 @@ import { IBlockInfo, IBlockMember, IDefineInfo, ISamplerInfo, IShaderInfo } from '../../3d/assets/effect-asset'; import { GFXGetTypeSize, GFXShaderType } from '../../gfx/define'; -import { GFXDevice } from '../../gfx/device'; +import { GFXAPI, GFXDevice } from '../../gfx/device'; import { GFXShader, GFXUniform, GFXUniformBlock, GFXUniformSampler } from '../../gfx/shader'; import { UBOGlobal, UBOLocal } from '../../pipeline/render-pipeline'; import { SkinningUBO } from '../models/model-uniforms'; import { IDefineMap } from './effect'; +import { UBOForwardLights } from '../../pipeline/forward/forward-pipeline'; function _generateDefines ( device: GFXDevice, @@ -69,9 +70,7 @@ class ProgramLib { */ public define (prog: IShaderInfo) { const tmpl = Object.assign({ id: ++_shdID }, prog) as IProgramInfo; - tmpl.vert = this._precision + prog.vert; - tmpl.frag = this._precision + prog.frag; - tmpl.blocks = tmpl.blocks.concat(globals, locals); + tmpl.blocks = tmpl.blocks.concat(globals, locals, lights); // calculate option mask offset let offset = 0; @@ -130,8 +129,12 @@ class ProgramLib { // get template const tmpl = this._templates[name]; const customDef = _generateDefines(device, defines, tmpl.defines, tmpl.dependencies) + '\n'; - const vert = customDef + tmpl.vert; - const frag = customDef + tmpl.frag; + + const source = tmpl.glsl1; // temporary measure + // const source = (device.gfxAPI === GFXAPI.WEBGL2 ? tmpl.glsl3 : tmpl.glsl1); + + const vert = customDef + this._precision + source.vert; + const frag = customDef + this._precision + source.frag; const instanceName = Object.keys(defines).reduce((acc, cur) => defines[cur] ? `${acc}|${cur}` : acc, name); program = device.createShader({ @@ -150,6 +153,7 @@ class ProgramLib { const globals = convertToBlockInfo(UBOGlobal.BLOCK); const locals = convertToBlockInfo(UBOLocal.BLOCK); +const lights = convertToBlockInfo(UBOForwardLights.BLOCK); const skinning = convertToBlockInfo(SkinningUBO.BLOCK); const jointsTexture = convertToSamplerInfo(SkinningUBO.JOINT_TEXTURE); @@ -184,7 +188,7 @@ function convertToSamplerInfo (sampler: GFXUniformSampler): ISamplerInfo { binding: sampler.binding, defines: [], type: sampler.type, - count: sampler.count + count: sampler.count, }; } diff --git a/cocos/renderer/models/model-uniforms.ts b/cocos/renderer/models/model-uniforms.ts index dc7fc83596..0c24d96ff1 100644 --- a/cocos/renderer/models/model-uniforms.ts +++ b/cocos/renderer/models/model-uniforms.ts @@ -8,13 +8,13 @@ export class SkinningUBO { public static SIZE: number = SkinningUBO.COUNT * 4; public static BLOCK: GFXUniformBlock = { - binding: 29, name: 'CCSkinning', members: [ + binding: 28, name: 'CCSkinning', members: [ { name: 'cc_matJoint', type: GFXType.MAT4, count: 128 }, { name: 'cc_jointsTextureSize', type: GFXType.FLOAT4, count: 1 }, ], }; public static JOINT_TEXTURE: GFXUniformSampler = { - binding: 28, name: 'cc_jointsTexture', type: GFXType.SAMPLER2D, count: 1, + binding: 27, name: 'cc_jointsTexture', type: GFXType.SAMPLER2D, count: 1, }; } diff --git a/cocos/renderer/ui/ui.ts b/cocos/renderer/ui/ui.ts index 8e1e8e49e3..2e78a59801 100644 --- a/cocos/renderer/ui/ui.ts +++ b/cocos/renderer/ui/ui.ts @@ -39,7 +39,7 @@ export class UBOUI { public static SIZE: number = UBOUI.COUNT * 4; public static BLOCK: GFXUniformBlock = { - binding: 30, name: 'CCUI', members: [ + binding: 26, name: 'CCUI', members: [ { name: 'cc_matViewProj', type: GFXType.MAT4, count: 1 }, ], };