diff --git a/cocos/core/assets/index.ts b/cocos/core/assets/index.ts index d7c8781883..e8bc233aa2 100644 --- a/cocos/core/assets/index.ts +++ b/cocos/core/assets/index.ts @@ -40,7 +40,7 @@ export {default as JsonAsset} from './json-asset'; export {default as AssetLibrary} from './asset-library'; export { ImageAsset } from './image-asset'; export { Texture2D } from './texture-2d'; -export { TextureCube } from './texture-cube' +export { TextureCube } from './texture-cube'; export { TTFFont } from './ttf-font'; export { LabelAtlas } from './label-atlas'; export { BitmapFont } from './bitmap-font'; @@ -53,4 +53,5 @@ export { Material } from './material'; export { Mesh } from './mesh'; export { Skeleton } from './skeleton'; export { RenderTexture } from './render-texture'; +export { default as RenderPipelineAsset } from './render-pipeline-asset'; diff --git a/cocos/core/assets/render-pipeline-asset.ts b/cocos/core/assets/render-pipeline-asset.ts new file mode 100644 index 0000000000..2f01bf3220 --- /dev/null +++ b/cocos/core/assets/render-pipeline-asset.ts @@ -0,0 +1,13 @@ +import { ccclass, property } from '../data/class-decorator'; +import { RenderPipeline } from '../pipeline/render-pipeline'; +import { Asset } from './asset'; + +@ccclass('cc.RenderPipelineAsset') +export default class RenderPipelineAsset extends Asset { + @property({ + type: RenderPipeline, + }) + public renderPipeline: RenderPipeline | null = null; +} + +cc.RenderPipelineAsset = RenderPipelineAsset; diff --git a/cocos/core/data/class.ts b/cocos/core/data/class.ts index bd57635501..bca09e03df 100644 --- a/cocos/core/data/class.ts +++ b/cocos/core/data/class.ts @@ -375,7 +375,36 @@ function define (className, baseClass, mixins, options) { const cls = doDefine(className, baseClass, mixins, options); + // for RenderPipeline, RenderFlow, RenderStage + const isRenderPipeline = js.isChildClassOf(baseClass, cc.RenderPipeline); + const isRenderFlow = js.isChildClassOf(baseClass, cc.RenderFlow); + const isRenderStage = js.isChildClassOf(baseClass, cc.RenderStage); + + const isRender = isRenderPipeline || isRenderFlow || isRenderStage || false; + + if (isRender) { + let renderName = ''; + if (isRenderPipeline) { + renderName = 'render_pipeline'; + } else if (isRenderFlow) { + renderName = 'render_flow'; + } else if (isRenderStage) { + renderName = 'render_stage'; + } + + if (renderName) { + js._setClassId(className, cls); + if (CC_EDITOR) { + // 增加了 hidden: 开头标识,使它最终不会显示在 Editor inspector 的添加组件列表里 + // @ts-ignore + // tslint:disable-next-line:no-unused-expression + window.EditorExtends && window.EditorExtends.Component.addMenu(cls, `hidden:${renderName}/${className}`, -1); + } + } + } + if (frame) { + // 基础的 ts, js 脚本组件 if (js.isChildClassOf(baseClass, Component)) { const uuid = frame.uuid; if (uuid) { diff --git a/cocos/core/game.ts b/cocos/core/game.ts index 036f811c91..03b434704a 100644 --- a/cocos/core/game.ts +++ b/cocos/core/game.ts @@ -32,6 +32,7 @@ import { WebGLGFXDevice } from './gfx/webgl/webgl-device'; import { WebGL2GFXDevice } from './gfx/webgl2/webgl2-device'; import * as debug from './platform/debug'; import { SplashScreen } from './splash-image'; +import { ForwardPipeline } from './pipeline'; /** * @en * The current game configuration, including:
@@ -978,6 +979,15 @@ export class Game extends EventTarget { cc.game.resume(); }); } + + private setRenderPipeline (rppl) { + // cc.director.root.setRenderPipeline(rppl.renderPipeline); + if (!rppl) { + rppl = new ForwardPipeline(); + rppl.initialize(ForwardPipeline.initInfo); + } + cc.director.root.setRenderPipeline(rppl); + } } cc.Game = Game; diff --git a/cocos/core/pipeline/forward/enum.ts b/cocos/core/pipeline/forward/enum.ts new file mode 100644 index 0000000000..382befb906 --- /dev/null +++ b/cocos/core/pipeline/forward/enum.ts @@ -0,0 +1,18 @@ + +/** + * @zh + * 前向阶段优先级。 + */ +export enum ForwardStagePriority { + FORWARD = 0, +} + + +/** + * @zh + * 前向渲染流程优先级。 + */ +export enum ForwardFlowPriority { + FORWARD = 0, + UI = 10, +} \ No newline at end of file diff --git a/cocos/core/pipeline/forward/forward-flow.ts b/cocos/core/pipeline/forward/forward-flow.ts index 75f1981703..c3eadbca95 100644 --- a/cocos/core/pipeline/forward/forward-flow.ts +++ b/cocos/core/pipeline/forward/forward-flow.ts @@ -2,51 +2,37 @@ * @category pipeline.forward */ +import { ccclass } from '../../data/class-decorator'; import { IRenderFlowInfo, RenderFlow } from '../render-flow'; -import { RenderPipeline } from '../render-pipeline'; import { ForwardStage } from './forward-stage'; - -/** - * @zh - * 前向阶段优先级。 - */ -export enum ForwardStagePriority { - FORWARD = 0, -} +import { PIPELINE_FLOW_FORWARD } from '../define'; +import { ForwardFlowPriority } from "./enum"; /** * @zh * 前向渲染流程。 */ +@ccclass('ForwardFlow') export class ForwardFlow extends RenderFlow { + public static initInfo: IRenderFlowInfo = { + name: PIPELINE_FLOW_FORWARD, + priority: ForwardFlowPriority.FORWARD, + }; + /** * 构造函数。 * @param pipeline 渲染管线。 */ - constructor (pipeline: RenderPipeline) { - super(pipeline); + constructor () { + super(); } - /** - * @zh - * 初始化函数。 - * @param info 渲染流程描述信息。 - */ - public initialize (info: IRenderFlowInfo): boolean { - - if (info.name !== undefined) { - this._name = info.name; - } - - this._priority = info.priority; - - this.createStage(ForwardStage, { - name: 'ForwardStage', - priority: ForwardStagePriority.FORWARD, - }); - - return true; + public initialize(info: IRenderFlowInfo) { + super.initialize(info); + const forwardStage = new ForwardStage(); + forwardStage.initialize(ForwardStage.initInfo); + this._stages.push(forwardStage); } /** diff --git a/cocos/core/pipeline/forward/forward-pipeline.ts b/cocos/core/pipeline/forward/forward-pipeline.ts index 74ccdf2687..6cf3d17d4d 100644 --- a/cocos/core/pipeline/forward/forward-pipeline.ts +++ b/cocos/core/pipeline/forward/forward-pipeline.ts @@ -3,6 +3,7 @@ */ import { intersect, sphere } from '../../geom-utils'; +import { ccclass } from '../../data/class-decorator'; import { GFXBuffer } from '../../gfx/buffer'; import { GFXBindingType, GFXBufferUsageBit, GFXMemoryUsageBit } from '../../gfx/define'; import { GFXRenderPass } from '../../gfx/render-pass'; @@ -15,20 +16,12 @@ import { SpotLight } from '../../renderer/scene/spot-light'; import { Root } from '../../root'; import { cullDirectionalLight, cullSphereLight, cullSpotLight } from '../culling'; import { PIPELINE_FLOW_FORWARD, PIPELINE_FLOW_TONEMAP, RenderPassStage, UBOForwardLight } from '../define'; -import { ToneMapFlow } from '../ppfx/tonemap-flow'; -import { IRenderPipelineInfo, RenderPipeline } from '../render-pipeline'; +import { RenderPipeline, IRenderPipelineInfo, IRenderPipelineDesc } from '../render-pipeline'; import { RenderView } from '../render-view'; import { UIFlow } from '../ui/ui-flow'; import { ForwardFlow } from './forward-flow'; - -/** - * @zh - * 前向渲染流程优先级。 - */ -export enum ForwardFlowPriority { - FORWARD = 0, - UI = 10, -} +import { ToneMapFlow } from '../ppfx/tonemap-flow'; +import { ForwardFlowPriority } from './enum'; const _vec4Array = new Float32Array(4); const _sphere = sphere.create(0, 0, 0, 1); @@ -40,8 +33,12 @@ const _tempVec3 = new Vec3(); * @zh * 前向渲染管线。 */ +@ccclass('ForwardPipeline') export class ForwardPipeline extends RenderPipeline { + public static initInfo: IRenderPipelineInfo = { + }; + /** * @zh * 全部光源的UBO结构描述。 @@ -72,7 +69,7 @@ export class ForwardPipeline extends RenderPipeline { */ private _lightIndices: number[]; - public get lightsUBO (): GFXBuffer { + public get lightsUBO(): GFXBuffer { return this._lightsUBO!; } @@ -80,26 +77,45 @@ export class ForwardPipeline extends RenderPipeline { * 构造函数。 * @param root Root类实例。 */ - constructor (root: Root) { - super(root); + constructor() { + super(); this._validLights = []; this._lightIndexOffset = []; this._lightIndices = []; } - /** - * @zh - * 初始化函数。 - * @param info 渲染管线描述信息。 - */ - public initialize (info: IRenderPipelineInfo): boolean { + public initialize(info: IRenderPipelineInfo) { + super.initialize(info); + const forwardFlow = new ForwardFlow(); + forwardFlow.initialize(ForwardFlow.initInfo); + this._flows.push(forwardFlow); + } - if (!this._initialize(info)) { - return false; + public activate(root: Root) { + super.activate(root); + this.getFlow(PIPELINE_FLOW_FORWARD)!.activate(this); + + if (this._usePostProcess) { + if (this._useSMAA) { + /* + this.createFlow(SMAAEdgeFlow, { + name: PIPELINE_FLOW_SMAA, + priority: 0, + }); + */ + } + this.getFlow(PIPELINE_FLOW_TONEMAP)!.activate(this); } - this._name = 'ForwardPipeline'; + const uiFlow = new UIFlow(); + uiFlow.initialize(UIFlow.initInfo); + this._flows.push(uiFlow); + uiFlow.activate(this); + return true; + } + + protected createUBOs(): boolean { if (!this._globalBindings.get(UBOForwardLight.BLOCK.name)) { const lightsUBO = this._root.device.createBuffer({ usage: GFXBufferUsageBit.UNIFORM | GFXBufferUsageBit.TRANSFER_DST, @@ -117,55 +133,14 @@ export class ForwardPipeline extends RenderPipeline { buffer: lightsUBO, }); } - - const mainWindow = this._root.mainWindow; - let windowPass: GFXRenderPass | null = null; - - if (mainWindow) { - windowPass = mainWindow.renderPass; - } - - if (!windowPass) { - console.error('RenderPass of main window is null.'); - return false; - } - - this.addRenderPass(RenderPassStage.DEFAULT, windowPass); - - // create flows - this.createFlow(ForwardFlow, { - name: PIPELINE_FLOW_FORWARD, - priority: ForwardFlowPriority.FORWARD, - }); - - if (this._usePostProcess) { - if (this._useSMAA) { - /* - this.createFlow(SMAAEdgeFlow, { - name: PIPELINE_FLOW_SMAA, - priority: 0, - }); - */ - } - this.createFlow(ToneMapFlow, { - name: PIPELINE_FLOW_TONEMAP, - priority: 0, - }); - } - - this.createFlow(UIFlow, { - name: 'UIFlow', - priority: ForwardFlowPriority.UI, - }); - - return true; + return super.createUBOs(); } /** * @zh * 销毁函数。 */ - public destroy () { + public destroy() { const lightsUBO = this._globalBindings.get(UBOForwardLight.BLOCK.name); if (lightsUBO) { lightsUBO.buffer!.destroy(); @@ -179,10 +154,10 @@ export class ForwardPipeline extends RenderPipeline { * @zh * 重构函数。 */ - public rebuild () { + public rebuild() { super.rebuild(); - for (const flow of this._flows) { - flow.rebuild(); + for (let i = 0; i < this._flows.length; i++) { + this._flows[i].rebuild(); } } @@ -190,7 +165,7 @@ export class ForwardPipeline extends RenderPipeline { * @zh * 更新UBO。 */ - protected updateUBOs (view: RenderView) { + protected updateUBOs(view: RenderView) { super.updateUBOs(view); const exposure = view.camera.exposure; @@ -283,10 +258,11 @@ export class ForwardPipeline extends RenderPipeline { * 场景裁剪。 * @param view 渲染视图。 */ - protected sceneCulling (view: RenderView) { + protected sceneCulling(view: RenderView) { super.sceneCulling(view); this._validLights.splice(0); - for (const light of view.camera.scene.sphereLights) { + for (let i = 0; i < view.camera.scene.sphereLights.length; i++) { + const light = view.camera.scene.sphereLights[i]; if (light.enabled) { light.update(); sphere.set(_sphere, light.position.x, light.position.y, light.position.z, light.range); @@ -295,7 +271,8 @@ export class ForwardPipeline extends RenderPipeline { } } } - for (const light of view.camera.scene.spotLights) { + for (let i = 0; i < view.camera.scene.spotLights.length; i++) { + const light = view.camera.scene.spotLights[i]; if (light.enabled) { light.update(); sphere.set(_sphere, light.position.x, light.position.y, light.position.z, light.range); @@ -320,7 +297,7 @@ export class ForwardPipeline extends RenderPipeline { * 对每个模型裁剪光源。 * @param model 模型。 */ - private cullLightPerModel (model: Model) { + private cullLightPerModel(model: Model) { _tempLightIndex.splice(0); for (let i = 0; i < this._validLights.length; i++) { let isCulled = false; @@ -348,7 +325,7 @@ export class ForwardPipeline extends RenderPipeline { this._lightIndices.push(..._tempLightIndex); } - private sortLight (a, b) { + private sortLight(a, b) { return _tempLightDist[a] - _tempLightDist[b]; } } diff --git a/cocos/core/pipeline/forward/forward-stage.ts b/cocos/core/pipeline/forward/forward-stage.ts index 9e5d0822db..1218225922 100644 --- a/cocos/core/pipeline/forward/forward-stage.ts +++ b/cocos/core/pipeline/forward/forward-stage.ts @@ -2,16 +2,17 @@ * @category pipeline.forward */ +import { ccclass } from '../../data/class-decorator'; import { GFXCommandBuffer } from '../../gfx/command-buffer'; import { GFXClearFlag, GFXCommandBufferType, GFXFilter, IGFXColor } from '../../gfx/define'; import { Layers } from '../../scene-graph'; -import { getPhaseID } from '../pass-phase'; import { SRGBToLinear } from '../pipeline-funcs'; import { RenderBatchedQueue } from '../render-batched-queue'; -import { RenderFlow } from '../render-flow'; -import { opaqueCompareFn, RenderQueue, transparentCompareFn } from '../render-queue'; -import { IRenderStageInfo, RenderStage } from '../render-stage'; +import { RenderQueue } from '../render-queue'; +import { IRenderStageInfo, RenderStage, RenderQueueSortMode } from '../render-stage'; import { RenderView } from '../render-view'; +import { ForwardStagePriority } from './enum'; +import { RenderFlow } from '../render-flow'; const colors: IGFXColor[] = [ { r: 0, g: 0, b: 0, a: 1 } ]; const bufs: GFXCommandBuffer[] = []; @@ -20,51 +21,40 @@ const bufs: GFXCommandBuffer[] = []; * @zh * 前向渲染阶段。 */ +@ccclass('ForwardStage') export class ForwardStage extends RenderStage { - private _opaqueQueue: RenderQueue; - private _transparentQueue: RenderQueue; + public static initInfo: IRenderStageInfo = { + name: 'ForwardStage', + priority: ForwardStagePriority.FORWARD, + renderQueues: [ + { + isTransparent: false, + sortMode: RenderQueueSortMode.FRONT_TO_BACK, + stages: ['default'], + }, + { + isTransparent: true, + sortMode: RenderQueueSortMode.BACK_TO_FRONT, + stages: ['default', 'planarShadow'], + } + ] + }; + private _opaqueBatchedQueue: RenderBatchedQueue; /** * 构造函数。 * @param flow 渲染阶段。 */ - constructor (flow: RenderFlow) { - super(flow); - - this._opaqueQueue = new RenderQueue({ - isTransparent: false, - phases: getPhaseID('default'), - sortFunc: opaqueCompareFn, - }); - this._transparentQueue = new RenderQueue({ - isTransparent: true, - phases: getPhaseID('default') | getPhaseID('planarShadow'), - sortFunc: transparentCompareFn, - }); + constructor () { + super(); this._opaqueBatchedQueue = new RenderBatchedQueue(); } - /** - * @zh - * 初始化函数。 - * @param info 渲染阶段描述信息。 - */ - public initialize (info: IRenderStageInfo): boolean { - - if (info.name !== undefined) { - this._name = info.name; - } - - this._priority = info.priority; - - this._cmdBuff = this._device.createCommandBuffer({ - allocator: this._device.commandAllocator, - type: GFXCommandBufferType.PRIMARY, - }); - - return true; + public activate (flow: RenderFlow) { + super.activate(flow); + this._createCmdBuffer(); } /** @@ -102,8 +92,7 @@ export class ForwardStage extends RenderStage { public render (view: RenderView) { this._opaqueBatchedQueue.clear(); - this._opaqueQueue.clear(); - this._transparentQueue.clear(); + this._renderQueues.forEach(this.clearRenderQueue); const renderObjects = this._pipeline.renderObjects; for (let i = 0; i < renderObjects.length; ++i) { @@ -121,64 +110,32 @@ export class ForwardStage extends RenderStage { pass.batchedBuffer.merge(subModel, ro, pso); this._opaqueBatchedQueue.queue.add(pass.batchedBuffer); } else { - const hash = (0 << 30) | pass.priority << 16 | subModel.priority << 8 | p; - this._transparentQueue.queue.push({ - hash, - depth: ro.depth, - shaderId: pso.shader.id, - subModel, - cmdBuff: subModel.commandBuffers[p], - }); + this._renderQueues[1].insertRenderPass(ro, m, p); } } } } } else { - const subModels = ro.model.subModels; - for (let m = 0; m < subModels.length; ++m) { - const subModel = subModels[m]; - const passes = subModel.passes; - for (let p = 0; p < passes.length; ++p) { - const pass = subModel.passes[p]; - const pso = subModel.psos![p]; - const isTransparent = pso.blendState.targets[0].blend; - const hash = (0 << 30) | pass.priority << 16 | subModel.priority << 8 | p; - if (isTransparent) { - this._transparentQueue.queue.push({ - hash, - depth: ro.depth, - shaderId: pso.shader.id, - subModel, - cmdBuff: subModel.commandBuffers[p], - }); - } else { - this._opaqueQueue.queue.push({ - hash, - depth: ro.depth, - shaderId: pso.shader.id, - subModel, - cmdBuff: subModel.commandBuffers[p], - }); + for (let i = 0; i < ro.model.subModelNum; i++) { + for (let j = 0; j < ro.model.getSubModel(i).passes.length; j++) { + for (let k = 0; k < this._renderQueues.length; k++) { + this._renderQueues[k].insertRenderPass(ro, i, j); } - // this._opaqueQueue.insertRenderPass(ro, m, j); - // this._transparentQueue.insertRenderPass(ro, m, j); } } } } - - this._opaqueQueue.sort(); - this._transparentQueue.sort(); + this._renderQueues.forEach(this.sortRenderQueue); const camera = view.camera; const cmdBuff = this._cmdBuff!; const vp = camera.viewport; - this._renderArea.x = vp.x * camera.width; - this._renderArea.y = vp.y * camera.height; - this._renderArea.width = vp.width * camera.width * this.pipeline.shadingScale; - this._renderArea.height = vp.height * camera.height * this.pipeline.shadingScale; + this._renderArea!.x = vp.x * camera.width; + this._renderArea!.y = vp.y * camera.height; + this._renderArea!.width = vp.width * camera.width * this.pipeline!.shadingScale; + this._renderArea!.height = vp.height * camera.height * this.pipeline!.shadingScale; if (camera.clearFlag & GFXClearFlag.COLOR) { if (this._pipeline.isHDR) { @@ -196,9 +153,9 @@ export class ForwardStage extends RenderStage { if (this._pipeline.usePostProcess) { if (!this._pipeline.useMSAA) { - this._framebuffer = this._pipeline.curShadingFBO; + this._framebuffer = this._pipeline.getFrameBuffer(this._pipeline!.currShading)!; } else { - this._framebuffer = this._pipeline.msaaShadingFBO; + this._framebuffer = this._pipeline.getFrameBuffer('msaa')!; } } else { this._framebuffer = view.window!.framebuffer; @@ -207,31 +164,46 @@ export class ForwardStage extends RenderStage { const planarShadow = camera.scene.planarShadows; cmdBuff.begin(); - cmdBuff.beginRenderPass(this._framebuffer!, this._renderArea, + cmdBuff.beginRenderPass(this._framebuffer!, this._renderArea!, camera.clearFlag, colors, camera.clearDepth, camera.clearStencil); - cmdBuff.execute(this._opaqueQueue.cmdBuffs.array, this._opaqueQueue.cmdBuffCount); + cmdBuff.execute(this._renderQueues[0].cmdBuffs.array, this._renderQueues[0].cmdBuffCount); this._opaqueBatchedQueue.recordCommandBuffer(cmdBuff); if (camera.visibility & Layers.BitMask.DEFAULT) { cmdBuff.execute(planarShadow.cmdBuffs.array, planarShadow.cmdBuffCount); } - cmdBuff.execute(this._transparentQueue.cmdBuffs.array, this._transparentQueue.cmdBuffCount); + cmdBuff.execute(this._renderQueues[1].cmdBuffs.array, this._renderQueues[1].cmdBuffCount); cmdBuff.endRenderPass(); cmdBuff.end(); bufs[0] = cmdBuff; - this._device.queue.submit(bufs); + this._device!.queue.submit(bufs); if (this._pipeline.useMSAA) { - this._device.blitFramebuffer( - this._framebuffer, - this._pipeline.curShadingFBO, - this._renderArea, - this._renderArea, + this._device!.blitFramebuffer( + this._framebuffer!, + this._pipeline.getFrameBuffer(this._pipeline.currShading)!, + this._renderArea!, + this._renderArea!, GFXFilter.POINT); } } + + private _createCmdBuffer () { + this._cmdBuff = this._device!.createCommandBuffer({ + allocator: this._device!.commandAllocator, + type: GFXCommandBufferType.PRIMARY, + }); + } + + private clearRenderQueue (rq: RenderQueue) { + rq.clear(); + } + + private sortRenderQueue (rq: RenderQueue) { + rq.sort(); + } } diff --git a/cocos/core/pipeline/index.ts b/cocos/core/pipeline/index.ts index 97fda13592..c11f682022 100644 --- a/cocos/core/pipeline/index.ts +++ b/cocos/core/pipeline/index.ts @@ -5,4 +5,14 @@ import { RenderPassStage } from './define'; import './render-view'; +export { RenderPipeline } from './render-pipeline'; +export { RenderFlow } from './render-flow'; +export { RenderStage } from './render-stage'; + +export { ForwardPipeline } from './forward/forward-pipeline'; +export { ForwardFlow } from './forward/forward-flow'; +export { ForwardStage } from './forward/forward-stage'; +export { ToneMapFlow } from './ppfx/tonemap-flow'; +export { ToneMapStage } from './ppfx/tonemap-stage'; + cc.RenderPassStage = RenderPassStage; export { RenderPassStage }; diff --git a/cocos/core/pipeline/pipeline-serialization.ts b/cocos/core/pipeline/pipeline-serialization.ts new file mode 100644 index 0000000000..f4bc218377 --- /dev/null +++ b/cocos/core/pipeline/pipeline-serialization.ts @@ -0,0 +1,125 @@ + +import { ccclass, property } from '../data/class-decorator'; +import { ccenum } from '../value-types/enum'; +import { GFXFormat, GFXLoadOp, GFXStoreOp, GFXTextureLayout, GFXTextureType, GFXTextureUsageBit, GFXTextureViewType} from '../gfx/define'; + +ccenum(GFXTextureType); +ccenum(GFXTextureViewType); +ccenum(GFXTextureUsageBit); +ccenum(GFXStoreOp); +ccenum(GFXLoadOp); +ccenum(GFXTextureLayout); + +@ccclass('RenderTextureDesc') +export class RenderTextureDesc { + @property + public name: string = ''; + @property({ + type: GFXTextureType, + }) + public type: GFXTextureType = GFXTextureType.TEX2D; + @property({ + type: GFXTextureViewType, + }) + public viewType: GFXTextureViewType = GFXTextureViewType.TV2D; + @property({ + type: GFXTextureUsageBit, + }) + public usage: GFXTextureUsageBit = GFXTextureUsageBit.COLOR_ATTACHMENT; + @property({ + type: GFXFormat, + }) + public format: GFXFormat = GFXFormat.UNKNOWN; + @property + public width: number = -1; + @property + public height: number = -1; +} + +@ccclass('FrameBufferDesc') +export class FrameBufferDesc { + @property + public name: string = ''; + @property + public renderPass: number = 0; + @property({ + type: [String], + }) + public colorViews = []; + @property + public depthStencilView: string = ''; +} + +@ccclass('ColorDesc') +export class ColorDesc { + @property({ + type: GFXFormat, + }) + public format: GFXFormat = GFXFormat.UNKNOWN; + @property({ + type: GFXLoadOp, + }) + public loadOp: GFXLoadOp = GFXLoadOp.CLEAR; + @property({ + type: GFXStoreOp, + }) + public storeOp: GFXStoreOp = GFXStoreOp.STORE; + @property + public sampleCount: number = 1; + @property({ + type: GFXTextureLayout, + }) + public beginLayout: GFXTextureLayout = GFXTextureLayout.COLOR_ATTACHMENT_OPTIMAL; + @property({ + type: GFXTextureLayout, + }) + public endLayout: GFXTextureLayout = GFXTextureLayout.COLOR_ATTACHMENT_OPTIMAL; +} + +@ccclass('DepthStencilDesc') +export class DepthStencilDesc { + @property({ + type: GFXFormat, + }) + public format: GFXFormat = GFXFormat.UNKNOWN; + @property({ + type: GFXLoadOp, + }) + public depthLoadOp: GFXLoadOp = GFXLoadOp.CLEAR; + @property({ + type: GFXStoreOp, + }) + public depthStoreOp: GFXStoreOp = GFXStoreOp.STORE; + @property({ + type: GFXLoadOp, + }) + public stencilLoadOp: GFXLoadOp = GFXLoadOp.CLEAR; + @property({ + type: GFXStoreOp, + }) + public stencilStoreOp: GFXStoreOp = GFXStoreOp.STORE; + @property + public sampleCount: number = 1; + @property({ + type: GFXTextureLayout, + }) + public beginLayout: GFXTextureLayout = GFXTextureLayout.COLOR_ATTACHMENT_OPTIMAL; + @property({ + type: GFXTextureLayout, + }) + public endLayout: GFXTextureLayout = GFXTextureLayout.COLOR_ATTACHMENT_OPTIMAL; +} + +@ccclass('RenderPassDesc') +export class RenderPassDesc { + @property + public index: number = -1; + @property({ + type: [ColorDesc], + }) + public colorAttachments = []; + @property({ + type: DepthStencilDesc, + }) + public depthStencilAttachment: DepthStencilDesc = new DepthStencilDesc(); +} diff --git a/cocos/core/pipeline/ppfx/tonemap-flow.ts b/cocos/core/pipeline/ppfx/tonemap-flow.ts index 808fd36334..313f4e52c9 100644 --- a/cocos/core/pipeline/ppfx/tonemap-flow.ts +++ b/cocos/core/pipeline/ppfx/tonemap-flow.ts @@ -2,41 +2,39 @@ * @category pipeline.ppfx */ +import { ccclass } from '../../data/class-decorator'; import { IRenderFlowInfo, RenderFlow } from '../render-flow'; -import { RenderPipeline } from '../render-pipeline'; import { ToneMapStage } from './tonemap-stage'; +import { PIPELINE_FLOW_TONEMAP } from '../define'; +import { ForwardFlowPriority } from '../forward/enum'; /** * @zh * 色调映射渲染流程。 */ +@ccclass('ToneMapFlow') export class ToneMapFlow extends RenderFlow { - constructor (pipeline: RenderPipeline) { - super(pipeline); + public static initInfo: IRenderFlowInfo = { + name: PIPELINE_FLOW_TONEMAP, + priority: ForwardFlowPriority.FORWARD + 1, + }; + + constructor () { + super(); } public initialize (info: IRenderFlowInfo): boolean { - - if (info.name !== undefined) { - this._name = info.name; - } - - this._priority = info.priority; + super.initialize(info); const material = this._material; - material.initialize({ - effectName: 'pipeline/tonemap', - defines: { CC_USE_SMAA: this._pipeline.useSMAA }, + material!.recompileShaders({ + CC_USE_SMAA: this._pipeline!.useSMAA }); - const framebuffer = this._pipeline.root.mainWindow!.framebuffer!; - - this.createStage(ToneMapStage, { - name: 'ToneMapStage', - priority: 0, - framebuffer, - }); + const toneStage = new ToneMapStage(); + toneStage.initialize(ToneMapStage.initInfo); + this._stages.push(toneStage); return true; } @@ -50,10 +48,8 @@ export class ToneMapFlow extends RenderFlow { public rebuild () { if (this._material) { - this._material.destroy(); - this._material.initialize({ - effectName: 'pipeline/tonemap', - defines: { CC_USE_SMAA: this._pipeline.useSMAA }, + this._material.recompileShaders({ + CC_USE_SMAA: this._pipeline!.useSMAA }); } diff --git a/cocos/core/pipeline/ppfx/tonemap-stage.ts b/cocos/core/pipeline/ppfx/tonemap-stage.ts index fa41ab09dd..cb28c895ba 100644 --- a/cocos/core/pipeline/ppfx/tonemap-stage.ts +++ b/cocos/core/pipeline/ppfx/tonemap-stage.ts @@ -2,13 +2,14 @@ * @category pipeline.ppfx */ +import { ccclass } from '../../data/class-decorator'; import { GFXBindingLayout } from '../../gfx/binding-layout'; import { GFXCommandBuffer } from '../../gfx/command-buffer'; import { GFXClearFlag, GFXCommandBufferType, IGFXColor } from '../../gfx/define'; import { UBOGlobal } from '../define'; -import { RenderFlow } from '../render-flow'; import { IRenderStageInfo, RenderStage } from '../render-stage'; import { RenderView } from '../render-view'; +import { RenderFlow } from '../render-flow'; const colors: IGFXColor[] = [ { r: 0, g: 0, b: 0, a: 1 } ]; const bufs: GFXCommandBuffer[] = []; @@ -18,36 +19,37 @@ const bufs: GFXCommandBuffer[] = []; * 色调映射渲染阶段。 * */ +@ccclass('ToneMapStage') export class ToneMapStage extends RenderStage { + public static initInfo: IRenderStageInfo = { + name: 'ToneMapStage', + priority: 0, + framebuffer: 'window' + } + private _hTexSampler: number = 0; private _hBlendTexSampler: number = 0; private _bindingLayout: GFXBindingLayout | null = null; - constructor (flow: RenderFlow) { - super(flow); + constructor () { + super(); } - public initialize (info: IRenderStageInfo): boolean { + public activate (flow: RenderFlow) { - if (info.name !== undefined) { - this._name = info.name; - } + super.activate(flow); - this._priority = info.priority; - - if (info.framebuffer !== undefined) { - this._framebuffer = info.framebuffer ; - } - - this._cmdBuff = this._device.createCommandBuffer({ - allocator: this._device.commandAllocator, - type: GFXCommandBufferType.PRIMARY, - }); + this._createCmdBuffer(); this.rebuild(); + } - return true; + private _createCmdBuffer () { + this._cmdBuff = this._device!.createCommandBuffer({ + allocator: this._device!.commandAllocator, + type: GFXCommandBufferType.PRIMARY, + }); } public destroy () { @@ -61,20 +63,20 @@ export class ToneMapStage extends RenderStage { } public rebuild () { - this._pass = this._flow.material.passes[0]; + this._pass = this._flow!.material!.passes[0]; this._hTexSampler = this._pass.getBinding('u_texSampler')!; - const globalUBO = this._pipeline.globalBindings.get(UBOGlobal.BLOCK.name); + const globalUBO = this._pipeline!.globalBindings.get(UBOGlobal.BLOCK.name); this._pso = this._pass.createPipelineState(); this._bindingLayout = this._pso!.pipelineLayout.layouts[0]; this._pass.bindBuffer(UBOGlobal.BLOCK.binding, globalUBO!.buffer!); - this._pass.bindTextureView(this._hTexSampler, this._pipeline.curShadingTexView); + this._pass.bindTextureView(this._hTexSampler, this._pipeline!.getTextureView(this._pipeline!.currShading)!); - if (this._pipeline.useSMAA) { + if (this._pipeline!.useSMAA) { this._hBlendTexSampler = this._pass.getBinding('u_blendTexSampler')!; - this._pass.bindTextureView(this._hBlendTexSampler, this._pipeline.smaaBlendTexView); + this._pass.bindTextureView(this._hBlendTexSampler, this._pipeline!.getTextureView('smaaBlend')!); } this._pass.update(); @@ -87,23 +89,23 @@ export class ToneMapStage extends RenderStage { if (this._cmdBuff) { - this._renderArea.width = camera.width; - this._renderArea.height = camera.height; + this._renderArea!.width = camera.width; + this._renderArea!.height = camera.height; const framebuffer = view.window!.framebuffer; this._cmdBuff.begin(); - this._cmdBuff.beginRenderPass(framebuffer, this._renderArea, - GFXClearFlag.ALL, colors, 1.0, 0); + this._cmdBuff.beginRenderPass(framebuffer, this._renderArea!, + GFXClearFlag.ALL, [{ r: 0.0, g: 0.0, b: 0.0, a: 1.0 }], 1.0, 0); this._cmdBuff.bindPipelineState(this._pso!); this._cmdBuff.bindBindingLayout(this._pso!.pipelineLayout.layouts[0]); - this._cmdBuff.bindInputAssembler(this._pipeline.quadIA); - this._cmdBuff.draw(this._pipeline.quadIA); + this._cmdBuff.bindInputAssembler(this._pipeline!.quadIA); + this._cmdBuff.draw(this._pipeline!.quadIA); this._cmdBuff.endRenderPass(); this._cmdBuff.end(); } bufs[0] = this._cmdBuff!; - this._device.queue.submit(bufs); + this._device!.queue.submit(bufs); // this._pipeline.swapFBOs(); } diff --git a/cocos/core/pipeline/render-flow.ts b/cocos/core/pipeline/render-flow.ts index 4cbe1cf5d2..45ab57ba20 100644 --- a/cocos/core/pipeline/render-flow.ts +++ b/cocos/core/pipeline/render-flow.ts @@ -3,9 +3,10 @@ */ import { Material } from '../assets/material'; +import { ccclass, property } from '../data/class-decorator'; import { GFXDevice } from '../gfx/device'; import { RenderPipeline } from './render-pipeline'; -import { IRenderStageInfo, RenderStage } from './render-stage'; +import { RenderStage } from './render-stage'; import { RenderView } from './render-view'; /** @@ -15,35 +16,37 @@ import { RenderView } from './render-view'; export interface IRenderFlowInfo { name?: string; priority: number; + material?: Material; } /** * @zh * 渲染流程。 */ +@ccclass('RenderFlow') export abstract class RenderFlow { - public get device (): GFXDevice { - return this._device; + public get device(): GFXDevice { + return this._device!; } - public get pipeline (): RenderPipeline { + public get pipeline(): RenderPipeline { return this._pipeline; } - public get name (): string { + public get name(): string { return this._name; } - public get priority (): number { + public get priority(): number { return this._priority; } - public get stages (): RenderStage[] { + public get stages(): RenderStage[] { return this._stages!; } - public get material (): Material { + public get material(): Material | null { return this._material; } @@ -51,45 +54,61 @@ export abstract class RenderFlow { * @zh * GFX设备。 */ - protected _device: GFXDevice; + protected _device: GFXDevice | null = null; /** * @zh * 渲染管线。 */ - protected _pipeline: RenderPipeline; + protected _pipeline: RenderPipeline = null!; /** * @zh * 名称。 */ + @property({ + displayOrder: 0, + visible: true + }) protected _name: string = ''; /** * @zh * 优先级。 */ + @property({ + displayOrder: 1, + visible: true + }) protected _priority: number = 0; - /** - * @zh - * 渲染阶段数组。 - */ - protected _stages: RenderStage[] = []; - /** * @zh * 材质。 */ - protected _material: Material = new Material(); + @property({ + type: cc.Material, + displayOrder: 2, + visible: true + }) + protected _material: Material | null = null; + + /** + * @zh + * 渲染阶段数组。 + */ + @property({ + type: [RenderStage], + displayOrder: 3, + visible: true + }) + protected _stages: RenderStage[] = []; /** * 构造函数。 * @param pipeline 渲染管线。 */ - constructor (pipeline: RenderPipeline) { - this._device = pipeline.device; - this._pipeline = pipeline; + constructor() { } /** @@ -97,19 +116,47 @@ export abstract class RenderFlow { * 初始化函数。 * @param info 渲染流程描述信息。 */ - public abstract initialize (info: IRenderFlowInfo): boolean; + public initialize(info: IRenderFlowInfo) { + if (info.name !== undefined) { + this._name = info.name; + } + + this._priority = info.priority; + + if (info.material) + this._material = info.material; + } + + /** + * 把序列化数据转换成运行时数据 + */ + public activate(pipeline: RenderPipeline) { + this._device = pipeline.device; + this._pipeline = pipeline; + (pipeline as any).activateFlow(this); + this._activateStages(); + } + + protected _activateStages() { + for (let i = 0; i < this._stages.length; i++) { + this._stages[i].activate(this); + } + this._stages.sort((a, b) => { + return a.priority - b.priority; + }) + } /** * @zh * 销毁函数。 */ - public abstract destroy (); + public abstract destroy(); /** * @zh * 重构函数。 */ - public abstract rebuild (); + public abstract rebuild(); /** * @zh @@ -117,9 +164,9 @@ export abstract class RenderFlow { * @param width 屏幕宽度。 * @param height 屏幕高度。 */ - public resize (width: number, height: number) { - for (const stage of this._stages) { - stage.resize(width, height); + public resize(width: number, height: number) { + for (let i = 0; i < this._stages.length; i++) { + this._stages[i].resize(width, height); } } @@ -128,32 +175,9 @@ export abstract class RenderFlow { * 渲染函数。 * @param view 渲染视图。 */ - public render (view: RenderView) { - for (const stage of this._stages) { - stage.render(view); - } - } - - /** - * @zh - * 创建渲染阶段。 - * @param clazz 渲染阶段类。 - * @param info 渲染阶段描述信息。 - */ - public createStage ( - clazz: new(flow: RenderFlow) => T, - info: IRenderStageInfo): RenderStage | null { - - const stage: RenderStage = new clazz(this); - if (stage.initialize(info)) { - this._stages.push(stage); - this._stages.sort((a, b) => { - return a.priority - b.priority; - }); - - return stage; - } else { - return null; + public render(view: RenderView) { + for (let i = 0; i < this._stages.length; i++) { + this._stages[i].render(view); } } @@ -161,10 +185,12 @@ export abstract class RenderFlow { * @zh * 销毁全部渲染阶段。 */ - public destroyStages () { - for (const stage of this._stages) { - stage.destroy(); + public destroyStages() { + for (let i = 0; i < this._stages.length; i++) { + this._stages[i].destroy(); } this._stages = []; } } + +cc.RenderFlow = RenderFlow; diff --git a/cocos/core/pipeline/render-pipeline.ts b/cocos/core/pipeline/render-pipeline.ts index 100ff1aa2a..e0c04fe5e1 100644 --- a/cocos/core/pipeline/render-pipeline.ts +++ b/cocos/core/pipeline/render-pipeline.ts @@ -3,9 +3,15 @@ */ import { intersect } from '../geom-utils'; +import { ccclass, property } from '../data/class-decorator'; import { GFXBuffer } from '../gfx/buffer'; -import { GFXBindingType, GFXBufferUsageBit, GFXFormat, GFXFormatInfos, GFXLoadOp, GFXMemoryUsageBit, - GFXStoreOp, GFXTextureLayout, GFXTextureType, GFXTextureUsageBit, GFXTextureViewType } from '../gfx/define'; +import { + GFXBindingType, + GFXBufferUsageBit, + GFXFormat, + GFXFormatInfos, + GFXMemoryUsageBit, + GFXTextureUsageBit } from '../gfx/define'; import { GFXDevice, GFXFeature } from '../gfx/device'; import { GFXFramebuffer } from '../gfx/framebuffer'; import { GFXInputAssembler, IGFXAttribute } from '../gfx/input-assembler'; @@ -19,10 +25,12 @@ import { programLib } from '../renderer/core/program-lib'; import { SKYBOX_FLAG } from '../renderer/scene/camera'; import { Root } from '../root'; import { Layers } from '../scene-graph'; -import { IRenderObject, UBOGlobal, UBOShadow, UNIFORM_ENVIRONMENT } from './define'; +import { IRenderObject, UBOGlobal, UBOShadow, UNIFORM_ENVIRONMENT, RenderPassStage } from './define'; import { IInternalBindingInst } from './define'; +import { FrameBufferDesc, RenderPassDesc, RenderTextureDesc } from './pipeline-serialization'; import { IRenderFlowInfo, RenderFlow } from './render-flow'; import { RenderView } from './render-view'; +import { Asset } from '../assets'; const _vec4Array = new Float32Array(4); const _v3tmp = new Vec3(); @@ -38,12 +46,20 @@ export interface IRenderPipelineInfo { enableMSAA?: boolean; enableSMAA?: boolean; enableIBL?: boolean; + renderTextures?: RenderTextureDesc[]; + framebuffers?: FrameBufferDesc[]; + renderPasses?: RenderPassDesc[]; +} + +export interface IRenderPipelineDesc { + root: Root; } /** * @zh * 渲染流程。 */ +@ccclass('RenderPipeline') export abstract class RenderPipeline { /** @@ -67,7 +83,7 @@ export abstract class RenderPipeline { * 名称。 */ public get name (): string { - return this._name; + return this.constructor.name; } /** @@ -86,6 +102,10 @@ export abstract class RenderPipeline { return this._flows; } + public get activeFlows (): RenderFlow[] { + return this._activeFlows; + } + /** * @zh * 启用后期处理。 @@ -130,54 +150,6 @@ export abstract class RenderPipeline { return this._lightMeterScale; } - /** - * @zh - * 深度模板纹理视图。 - */ - public get depthStencilTexView (): GFXTextureView { - return this._depthStencilTexView!; - } - - /** - * @zh - * 着色纹理视图数组的当前帧缓冲索引。 - */ - public get curShadingTexView (): GFXTextureView { - return this._shadingTexViews[this._curIdx]; - } - - /** - * @zh - * 着色纹理视图数组的上一帧缓冲索引。 - */ - public get prevShadingTexView (): GFXTextureView { - return this._shadingTexViews[this._prevIdx]; - } - - /** - * @zh - * 着色帧缓冲数组的当前帧缓冲索引。 - */ - public get curShadingFBO (): GFXFramebuffer { - return this._shadingFBOs[this._curIdx]; - } - - /** - * @zh - * 着色帧缓冲数组的上一帧缓冲索引。 - */ - public get prevShadingFBO (): GFXFramebuffer { - return this._shadingFBOs[this._prevIdx]; - } - - /** - * @zh - * MSAA着色帧缓冲。 - */ - public get msaaShadingFBO (): GFXFramebuffer { - return this._msaaShadingFBO!; - } - /** * @zh * 启用MSAA。 @@ -194,38 +166,6 @@ export abstract class RenderPipeline { return this._useSMAA; } - /** - * @zh - * SMAA边缘纹理视图。 - */ - public get smaaEdgeTexView (): GFXTextureView { - return this._smaaEdgeTexView!; - } - - /** - * @zh - * SMAA边缘帧缓冲。 - */ - public get smaaEdgeFBO (): GFXFramebuffer { - return this._smaaEdgeFBO!; - } - - /** - * @zh - * SMAA混合纹理视图。 - */ - public get smaaBlendTexView (): GFXTextureView { - return this._smaaBlendTexView!; - } - - /** - * @zh - * SMAA混合帧缓冲。 - */ - public get smaaBlendFBO (): GFXFramebuffer { - return this._smaaBlendFBO!; - } - /** * @zh * 四边形输入汇集器。 @@ -282,6 +222,14 @@ export abstract class RenderPipeline { return this._uboGlobal!.view; } + get currShading () { + return this._curIdx; + } + + get prevShading () { + return this._prevIdx; + } + /** * @zh * 启用动态合批。 @@ -290,44 +238,32 @@ export abstract class RenderPipeline { return this._useDynamicBatching; } - protected _root: Root; - protected _device: GFXDevice; - protected _name: string = 'BasePipeline'; + protected _root: Root = null!; + protected _device: GFXDevice = null!; protected _renderObjects: IRenderObject[] = []; - protected _renderPasses: Map = new Map(); + + @property({ + type: [RenderFlow], + visible: true + }) protected _flows: RenderFlow[] = []; + + protected _activeFlows: RenderFlow[] = []; + protected _isHDRSupported: boolean = false; protected _isHDR: boolean = false; protected _lightMeterScale: number = 10000.0; - protected _shadingPass: GFXRenderPass | null = null; protected _fboCount: number = 0; - protected _msaaShadingTex: GFXTexture | null = null; - protected _msaaShadingTexView: GFXTextureView | null = null; - protected _msaaDepthStencilTex: GFXTexture | null = null; - protected _msaaDepthStencilTexView: GFXTextureView | null = null; - protected _msaaShadingFBO: GFXFramebuffer | null = null; protected _colorFmt: GFXFormat = GFXFormat.UNKNOWN; protected _depthStencilFmt: GFXFormat = GFXFormat.UNKNOWN; - protected _shadingTextures: GFXTexture[] = []; - protected _shadingTexViews: GFXTextureView[] = []; - protected _depthStencilTex: GFXTexture | null = null; - protected _depthStencilTexView: GFXTextureView | null = null; - protected _shadingFBOs: GFXFramebuffer[] = []; protected _shadingWidth: number = 0.0; protected _shadingHeight: number = 0.0; protected _shadingScale: number = 1.0; - protected _curIdx: number = 0; - protected _prevIdx: number = 1; + protected _curIdx: string = 'shading'; + protected _prevIdx: string = 'shading1'; protected _usePostProcess: boolean = false; protected _useMSAA: boolean = false; protected _useSMAA: boolean = false; - protected _smaaPass: GFXRenderPass | null = null; - protected _smaaEdgeFBO: GFXFramebuffer | null = null; - protected _smaaEdgeTex: GFXTexture | null = null; - protected _smaaEdgeTexView: GFXTextureView | null = null; - protected _smaaBlendFBO: GFXFramebuffer | null = null; - protected _smaaBlendTex: GFXTexture | null = null; - protected _smaaBlendTexView: GFXTextureView | null = null; protected _quadVB: GFXBuffer | null = null; protected _quadIB: GFXBuffer | null = null; protected _quadIA: GFXInputAssembler | null = null; @@ -340,21 +276,93 @@ export abstract class RenderPipeline { protected _macros: IDefineMap = {}; protected _useDynamicBatching = false; + @property({ + type: [RenderTextureDesc], + }) + protected renderTextures: RenderTextureDesc[] = []; + @property({ + type: [FrameBufferDesc], + }) + protected framebuffers: FrameBufferDesc[] = []; + @property({ + type: [RenderPassDesc], + }) + protected renderPasses: RenderPassDesc[] = []; + protected _renderTextures: Map = new Map(); + protected _textureViews: Map = new Map(); + protected _frameBuffers: Map = new Map(); + protected _renderPasses: Map = new Map(); + /** * 构造函数。 * @param root Root类实例。 */ - constructor (root: Root) { - this._root = root; - this._device = root.device; + constructor () { + } + + public getTextureView (name: string) { + return this._textureViews.get(name); + } + + public getRenderTexture (name: string) { + return this._renderTextures.get(name); + } + + public getFrameBuffer (name: string) { + return this._frameBuffers.get(name); } /** * @zh - * 初始化函数。 + * 初始化函数,用于不从资源加载RenderPipeline的情况。 * @param info 渲染管线描述信息。 */ - public abstract initialize (info: IRenderPipelineInfo): boolean; + public initialize (info: IRenderPipelineInfo) { + + if (info.enablePostProcess !== undefined) { + this._usePostProcess = info.enablePostProcess; + } else { + // We disable post process now, post process will be enabled in furture. + this._usePostProcess = false; + } + + this._isHDR = (info.enableHDR !== undefined ? info.enableHDR : false); + + // Config Anti-Aliasing + this._useSMAA = info.enableSMAA !== undefined ? info.enableSMAA : false; + this._useMSAA = info.enableMSAA !== undefined ? info.enableMSAA : false; + + if (info.renderTextures) + this.renderTextures = info.renderTextures; + if (info.framebuffers) + this.framebuffers = info.framebuffers; + if (info.renderPasses) + this.renderPasses = info.renderPasses; + } + + /** + * 当RenderPipeline资源加载完成后,启用相应的flow + * @param desc + */ + public activate (root: Root) { + this._root = root; + this._device = root.device; + + if (!this._initRenderResource()) { + console.error('RenderPipeline:' + this.name + ' startup failed!'); + } + } + + /** + * 激活一个RenderFlow,将其添加到可执行的RenderFlow数组中 + * @param flow 运行时会执行的RenderFlow + */ + private activateFlow (flow: RenderFlow) { + this._activeFlows.push(flow); + this._activeFlows.sort((a: RenderFlow, b: RenderFlow) => { + return a.priority - b.priority; + }); + } /** * @zh @@ -375,8 +383,8 @@ export abstract class RenderPipeline { this.updateUBOs(view); - for (const flow of view.flows) { - flow.render(view); + for (let i = 0; i < view.flows.length; i++) { + view.flows[i].render(view); } } @@ -405,8 +413,8 @@ export abstract class RenderPipeline { this.resizeFBOs(w, h); } - for (const flow of this._flows) { - flow.resize(width, height); + for (let i = 0; i < this._flows.length; i++) { + this._flows[i].resize(width, height); } } @@ -463,33 +471,13 @@ export abstract class RenderPipeline { this._renderPasses.clear(); } - /** - * @zh - * 创建渲染流程。 - */ - public createFlow ( - clazz: new (pipeline: RenderPipeline) => T, - info: IRenderFlowInfo): RenderFlow | null { - const flow: RenderFlow = new clazz(this); - if (flow.initialize(info)) { - this._flows.push(flow); - this._flows.sort((a: RenderFlow, b: RenderFlow) => { - return a.priority - b.priority; - }); - - return flow; - } else { - return null; - } - } - /** * @zh * 销毁全部渲染流程。 */ public destroyFlows () { - for (const flow of this._flows) { - flow.destroy(); + for (let i = 0; i < this._flows.length; i++) { + this._flows[i].destroy(); } this._flows = []; } @@ -500,9 +488,9 @@ export abstract class RenderPipeline { * @param name 名称。 */ public getFlow (name: string): RenderFlow | null { - for (const flow of this._flows) { - if (flow.name === name) { - return flow; + for (let i = 0; i < this._flows.length; i++) { + if (this._flows[i].name === name) { + return this._flows[i]; } } @@ -516,24 +504,12 @@ export abstract class RenderPipeline { public updateMacros () { programLib.destroyShaderByDefines(this._macros); this._macros.CC_USE_HDR = (this._isHDR); - for (const scene of this._root.scenes) { - scene.onPipelineChange(); + for (let i = 0; i < this._root.scenes.length; i++) { + this._root.scenes[i].onPipelineChange(); } } - /** - * @zh - * 内部初始化函数。 - * @param info 渲染流程描述信息。 - */ - protected _initialize (info: IRenderPipelineInfo): boolean { - - if (info.enablePostProcess !== undefined) { - this._usePostProcess = info.enablePostProcess; - } else { - // We disable post process now, post process will be enabled in furture. - this._usePostProcess = false; - } + protected _initRenderResource () { if (this._usePostProcess) { if (this._device.hasFeature(GFXFeature.FORMAT_R11G11B10F) || @@ -544,15 +520,7 @@ export abstract class RenderPipeline { // this._isHDRSupported = false; this._fboCount = 1; - this._shadingTextures = new Array(this._fboCount); - this._shadingTexViews = new Array(this._fboCount); - this._shadingFBOs = new Array(this._fboCount); - this._isHDR = (info.enableHDR !== undefined ? info.enableHDR : true); - - // Config Anti-Aliasing - this._useSMAA = info.enableSMAA !== undefined ? info.enableSMAA : false; - this._useMSAA = info.enableMSAA !== undefined ? info.enableMSAA : false; if (this._useMSAA) { this._useMSAA = this.device.hasFeature(GFXFeature.MSAA); } @@ -569,8 +537,8 @@ export abstract class RenderPipeline { this._colorFmt = GFXFormat.RGBA16F; this._isHDR = true; } - } else if (this._device.hasFeature(GFXFeature.COLOR_FLOAT) && - this._device.hasFeature(GFXFeature.TEXTURE_FLOAT_LINEAR)) { + } else if (this._device!.hasFeature(GFXFeature.COLOR_FLOAT) && + this._device!.hasFeature(GFXFeature.TEXTURE_FLOAT_LINEAR)) { if (this._device.hasFeature(GFXFeature.TEXTURE_FLOAT)) { this._colorFmt = GFXFormat.RGBA32F; this._isHDR = true; @@ -586,9 +554,9 @@ export abstract class RenderPipeline { if (this._device.depthBits === 24) { if (this._device.stencilBits === 8) { - this._depthStencilFmt = GFXFormat.D24S8; + this._depthStencilFmt = GFXFormat.D24S8; } else { - this._depthStencilFmt = GFXFormat.D24; + this._depthStencilFmt = GFXFormat.D24; } } else { this._depthStencilFmt = GFXFormat.D16; @@ -614,151 +582,38 @@ export abstract class RenderPipeline { console.info('SHADING_COLOR_FORMAT: ' + GFXFormatInfos[this._colorFmt].name); console.info('SHADING_DEPTH_FORMAT: ' + GFXFormatInfos[this._depthStencilFmt].name); - this._shadingPass = this._device.createRenderPass({ - colorAttachments: [{ - format: this._colorFmt, - loadOp: GFXLoadOp.CLEAR, - storeOp: GFXStoreOp.STORE, - sampleCount: 1, - beginLayout: GFXTextureLayout.COLOR_ATTACHMENT_OPTIMAL, - endLayout: GFXTextureLayout.COLOR_ATTACHMENT_OPTIMAL, - }], - depthStencilAttachment: { - format : this._depthStencilFmt, - depthLoadOp : GFXLoadOp.CLEAR, - depthStoreOp : GFXStoreOp.STORE, - stencilLoadOp : GFXLoadOp.CLEAR, - stencilStoreOp : GFXStoreOp.STORE, - sampleCount : 1, - beginLayout : GFXTextureLayout.DEPTH_STENCIL_ATTACHMENT_OPTIMAL, - endLayout : GFXTextureLayout.DEPTH_STENCIL_ATTACHMENT_OPTIMAL, - }, - }); - - if (this._useMSAA) { - this._msaaShadingTex = this._device.createTexture({ - type: GFXTextureType.TEX2D, - usage: GFXTextureUsageBit.COLOR_ATTACHMENT | GFXTextureUsageBit.SAMPLED, - format: this._colorFmt, - width: this._shadingWidth, - height: this._shadingHeight, - }); - this._msaaShadingTexView = this._device.createTextureView({ - texture : this._msaaShadingTex, - type : GFXTextureViewType.TV2D, - format : this._colorFmt, - }); - this._msaaDepthStencilTex = this._device.createTexture({ - type : GFXTextureType.TEX2D, - usage : GFXTextureUsageBit.DEPTH_STENCIL_ATTACHMENT | GFXTextureUsageBit.SAMPLED, - format : this._depthStencilFmt, - width : this._shadingWidth, - height : this._shadingHeight, - }); - this._msaaDepthStencilTexView = this._device.createTextureView({ - texture : this._msaaDepthStencilTex, - type : GFXTextureViewType.TV2D, - format : this._depthStencilFmt, - }); - this._msaaShadingFBO = this._device.createFramebuffer({ - renderPass: this._shadingPass, - colorViews: [ this._msaaShadingTexView ], - depthStencilView: this._msaaDepthStencilTexView!, - }); + for (let i = 0; i < this.renderTextures.length; i++) { + const rtd = this.renderTextures[i]; + this._renderTextures.set(rtd.name, this._device.createTexture({ + type: rtd.type, + usage: rtd.usage, + format: this._getTextureFormat(rtd.format, rtd.usage), + width: rtd.width === -1 ? this._shadingWidth : rtd.width, + height: rtd.height === -1 ? this._shadingHeight : rtd.height, + })); + this._textureViews.set(rtd.name, this._device.createTextureView({ + texture: this._renderTextures.get(rtd.name)!, + type: rtd.viewType, + format: this._getTextureFormat(rtd.format, rtd.usage), + })); + } + for (let i = 0; i < this.renderPasses.length; i++) { + const rpd = this.renderPasses[i]; + this._renderPasses.set(rpd.index, this._device.createRenderPass({ + colorAttachments: rpd.colorAttachments, + depthStencilAttachment: rpd.depthStencilAttachment, + })); } - if (this._fboCount > 0) { - this._depthStencilTex = this._device.createTexture({ - type : GFXTextureType.TEX2D, - usage : GFXTextureUsageBit.DEPTH_STENCIL_ATTACHMENT | GFXTextureUsageBit.SAMPLED, - format : this._depthStencilFmt, - width : this._shadingWidth, - height : this._shadingHeight, - }); - - this._depthStencilTexView = this._device.createTextureView({ - texture : this._depthStencilTex, - type : GFXTextureViewType.TV2D, - format : this._depthStencilFmt, - }); - - for (let i = 0; i < this._fboCount; ++i) { - this._shadingTextures[i] = this._device.createTexture({ - type: GFXTextureType.TEX2D, - usage: GFXTextureUsageBit.COLOR_ATTACHMENT | GFXTextureUsageBit.SAMPLED, - format: this._colorFmt, - width: this._shadingWidth, - height: this._shadingHeight, - }); - - this._shadingTexViews[i] = this._device.createTextureView({ - texture : this._shadingTextures[i], - type : GFXTextureViewType.TV2D, - format : this._colorFmt, - }); - - this._shadingFBOs[i] = this._device.createFramebuffer({ - renderPass: this._shadingPass, - colorViews: [ this._shadingTexViews[i] ], - depthStencilView: this._depthStencilTexView!, - }); - } - } - - // create smaa framebuffer - if (this._useSMAA) { - const smaaColorFmt = GFXFormat.RGBA8; - - this._smaaPass = this._device.createRenderPass({ - colorAttachments: [{ - format: smaaColorFmt, - loadOp: GFXLoadOp.CLEAR, - storeOp: GFXStoreOp.STORE, - sampleCount: 1, - beginLayout: GFXTextureLayout.COLOR_ATTACHMENT_OPTIMAL, - endLayout: GFXTextureLayout.COLOR_ATTACHMENT_OPTIMAL, - }], - }); - - this._smaaEdgeTex = this._device.createTexture({ - type: GFXTextureType.TEX2D, - usage: GFXTextureUsageBit.COLOR_ATTACHMENT | GFXTextureUsageBit.SAMPLED, - format: smaaColorFmt, - width: this._shadingWidth, - height: this._shadingHeight, - }); - - this._smaaEdgeTexView = this._device.createTextureView({ - texture : this._smaaEdgeTex, - type : GFXTextureViewType.TV2D, - format : smaaColorFmt, - }); - - this._smaaEdgeFBO = this._device.createFramebuffer({ - renderPass: this._smaaPass, - colorViews: [ this._smaaEdgeTexView ], - depthStencilView: null, - }); - - this._smaaBlendTex = this._device.createTexture({ - type: GFXTextureType.TEX2D, - usage: GFXTextureUsageBit.COLOR_ATTACHMENT | GFXTextureUsageBit.SAMPLED, - format: smaaColorFmt, - width: this._shadingWidth, - height: this._shadingHeight, - }); - - this._smaaBlendTexView = this._device.createTextureView({ - texture : this._smaaBlendTex, - type : GFXTextureViewType.TV2D, - format : smaaColorFmt, - }); - - this._smaaBlendFBO = this._device.createFramebuffer({ - renderPass: this._smaaPass, - colorViews: [ this._smaaBlendTexView ], - depthStencilView: null, - }); + for (let i = 0; i < this.framebuffers.length; i++) { + const fbd = this.framebuffers[i]; + this._frameBuffers.set(fbd.name, this._device.createFramebuffer({ + renderPass: this._renderPasses.get(fbd.renderPass)!, + colorViews: fbd.colorViews.map((value) => { + return this._textureViews.get(value)!; + }, this), + depthStencilView: this._textureViews.get(fbd.depthStencilView)!, + })); } if (!this.createQuadInputAssembler()) { @@ -769,6 +624,20 @@ export abstract class RenderPipeline { return false; } + const mainWindow = this._root.mainWindow; + let windowPass: GFXRenderPass | null = null; + + if (mainWindow) { + windowPass = mainWindow.renderPass; + } + + if (!windowPass) { + console.error('RenderPass of main window is null.'); + return false; + } + + this.addRenderPass(RenderPassStage.DEFAULT, windowPass); + return true; } @@ -783,87 +652,32 @@ export abstract class RenderPipeline { this.destroyQuadInputAssembler(); this.destroyUBOs(); - if (this._smaaEdgeTexView) { - this._smaaEdgeTexView.destroy(); - this._smaaEdgeTexView = null; + let rtIter = this._renderTextures.values(); + let rtRes = rtIter.next(); + while (!rtRes.done) { + rtRes.value.destroy(); + rtRes = rtIter.next(); } - if (this._smaaEdgeTex) { - this._smaaEdgeTex.destroy(); - this._smaaEdgeTex = null; + let tvIter = this._textureViews.values(); + let tvRes = tvIter.next(); + while (!tvRes.done) { + tvRes.value.destroy(); + tvRes = tvIter.next(); } - if (this._smaaEdgeFBO) { - this._smaaEdgeFBO.destroy(); - this._smaaEdgeFBO = null; + let rpIter = this._renderPasses.values(); + let rpRes = rpIter.next(); + while (!rpRes.done) { + rpRes.value.destroy(); + rpRes = rpIter.next(); } - if (this._smaaBlendTexView) { - this._smaaBlendTexView.destroy(); - this._smaaBlendTexView = null; - } - - if (this._smaaBlendTex) { - this._smaaBlendTex.destroy(); - this._smaaBlendTex = null; - } - - if (this._smaaBlendFBO) { - this._smaaBlendFBO.destroy(); - this._smaaBlendFBO = null; - } - - if (this._msaaShadingTexView) { - this._msaaShadingTexView.destroy(); - this._msaaShadingTexView = null; - } - if (this._msaaShadingTex) { - this._msaaShadingTex.destroy(); - this._msaaShadingTex = null; - } - if (this._msaaDepthStencilTexView) { - this._msaaDepthStencilTexView.destroy(); - this._msaaDepthStencilTexView = null; - } - if (this._msaaDepthStencilTex) { - this._msaaDepthStencilTex.destroy(); - this._msaaDepthStencilTex = null; - } - if (this._msaaShadingFBO) { - this._msaaShadingFBO.destroy(); - this._msaaShadingFBO = null; - } - - for (let i = 0; i < this._shadingTexViews.length; ++i) { - if (this._shadingTexViews[i]) { - this._shadingTexViews[i].destroy(); - } - - if (this._shadingTextures[i]) { - this._shadingTextures[i].destroy(); - } - - if (this._shadingFBOs[i]) { - this._shadingFBOs[i].destroy(); - } - } - - this._shadingTexViews.splice(0); - this._shadingTextures.splice(0); - this._shadingFBOs.splice(0); - - if (this._depthStencilTexView) { - this._depthStencilTexView.destroy(); - this._depthStencilTexView = null; - } - if (this._depthStencilTex) { - this._depthStencilTex.destroy(); - this._depthStencilTex = null; - } - - if (this._shadingPass) { - this._shadingPass.destroy(); - this._shadingPass = null; + let fbIter = this._frameBuffers.values(); + let fbRes = fbIter.next(); + while (!fbRes.done) { + fbRes.value.destroy(); + fbRes = fbIter.next(); } } @@ -878,85 +692,31 @@ export abstract class RenderPipeline { this._shadingWidth = width; this._shadingHeight = height; - if (this._depthStencilTex) { - this._depthStencilTex.resize(width, height); - this._depthStencilTexView!.destroy(); - this._depthStencilTexView!.initialize({ - texture : this._depthStencilTex, - type : GFXTextureViewType.TV2D, - format : this._depthStencilFmt, + let rtIter = this.renderTextures.values(); + let rtRes = rtIter.next(); + while (!rtRes.done) { + this._renderTextures.get(rtRes.value.name)!.resize(width, height); + this._textureViews.get(rtRes.value.name)!.destroy(); + this._textureViews.get(rtRes.value.name)!.initialize({ + texture: this._renderTextures.get(rtRes.value.name)!, + type: rtRes.value.viewType, + format: this._getTextureFormat(rtRes.value.format, rtRes.value.usage), }); + rtRes = rtIter.next(); } - for (let i = 0; i < this._fboCount; ++i) { - this._shadingTextures[i].resize(width, height); - this._shadingTexViews[i].destroy(); - this._shadingTexViews[i].initialize({ - texture : this._shadingTextures[i], - type : GFXTextureViewType.TV2D, - format : this._colorFmt, - }); - - this._shadingFBOs[i].destroy(); - this._shadingFBOs[i].initialize({ - renderPass: this._shadingPass!, - colorViews: [ this._shadingTexViews[i] ], - depthStencilView: this._depthStencilTexView!, - }); - } - - if (this._useMSAA) { - this._msaaShadingTex!.resize(width, height); - this._msaaShadingTexView!.destroy(); - this._msaaShadingTexView!.initialize({ - texture : this._msaaShadingTex!, - type : GFXTextureViewType.TV2D, - format : this._colorFmt, - }); - this._msaaDepthStencilTex!.resize(width, height); - this._msaaDepthStencilTexView!.destroy(); - this._msaaDepthStencilTexView!.initialize({ - texture : this._msaaDepthStencilTex!, - type : GFXTextureViewType.TV2D, - format : this._depthStencilFmt, - }); - this._msaaShadingFBO!.destroy(); - this._msaaShadingFBO!.initialize({ - renderPass: this._shadingPass!, - colorViews: [ this._msaaShadingTexView! ], - depthStencilView: this._msaaDepthStencilTexView!, - }); - } - - if (this._useSMAA) { - const smaaColorFmt = this._smaaEdgeTex!.format; - this._smaaEdgeTex!.resize(width, height); - this._smaaEdgeTexView!.destroy(); - this._smaaEdgeTexView!.initialize({ - texture : this._smaaEdgeTex!, - type : GFXTextureViewType.TV2D, - format : smaaColorFmt, - }); - - this._smaaEdgeFBO!.destroy(); - this._smaaEdgeFBO!.initialize({ - renderPass: this._smaaPass!, - colorViews: [ this._smaaEdgeTexView! ], - depthStencilView: null, - }); - this._smaaBlendTex!.resize(width, height); - this._smaaBlendTexView!.destroy(); - this._smaaBlendTexView!.initialize({ - texture : this._smaaBlendTex!, - type : GFXTextureViewType.TV2D, - format : smaaColorFmt, - }); - this._smaaBlendFBO!.destroy(); - this._smaaBlendFBO!.initialize({ - renderPass: this._smaaPass!, - colorViews: [ this._smaaBlendTexView! ], - depthStencilView: null, + let fbIter = this.framebuffers.values(); + let fbRes = fbIter.next(); + while (!fbRes.done) { + this._frameBuffers.get(fbRes.value.name)!.destroy(); + this._frameBuffers.get(fbRes.value.name)!.initialize({ + renderPass: this._renderPasses.get(fbRes.value.renderPass)!, + colorViews: fbRes.value.colorViews.map((value) => { + return this._textureViews.get(value)!; + }, this), + depthStencilView: this._textureViews.get(fbRes.value.depthStencilView)!, }); + fbRes = fbIter.next(); } console.info('Resizing shading fbos: ' + this._shadingWidth + 'x' + this._shadingHeight); @@ -1221,8 +981,8 @@ export abstract class RenderPipeline { this.addVisibleModel(scene.skybox, camera); } - for (const model of scene.models) { - + for (let i = 0; i < scene.models.length; i++) { + const model = scene.models[i]; model._resetUBOUpdateFlag(); // filter model by view visibility @@ -1275,4 +1035,19 @@ export abstract class RenderPipeline { depth, }); } + + private _getTextureFormat (format: GFXFormat, usage: GFXTextureUsageBit) { + if (format === GFXFormat.UNKNOWN) { + if (usage & GFXTextureUsageBit.COLOR_ATTACHMENT) { + return this._colorFmt; + } else if (usage & GFXTextureUsageBit.DEPTH_STENCIL_ATTACHMENT) { + return this._depthStencilFmt; + } else { + return GFXFormat.UNKNOWN; + } + } else { + return format; + } + } } +cc.RenderPipeline = RenderPipeline; diff --git a/cocos/core/pipeline/render-stage.ts b/cocos/core/pipeline/render-stage.ts index b2fad808f7..57e293e14e 100644 --- a/cocos/core/pipeline/render-stage.ts +++ b/cocos/core/pipeline/render-stage.ts @@ -2,16 +2,28 @@ * @category pipeline */ +import { ccclass, property } from '../data/class-decorator'; +import { ccenum } from '../value-types/enum'; import { GFXCommandBuffer } from '../gfx/command-buffer'; import { IGFXColor, IGFXRect } from '../gfx/define'; import { GFXDevice } from '../gfx/device'; import { GFXFramebuffer } from '../gfx/framebuffer'; import { GFXPipelineState } from '../gfx/pipeline-state'; import { Pass } from '../renderer'; +import { IRenderPass } from './define'; +import { getPhaseID } from './pass-phase'; import { RenderFlow } from './render-flow'; import { RenderPipeline } from './render-pipeline'; +import { opaqueCompareFn, RenderQueue, transparentCompareFn } from './render-queue'; import { RenderView } from './render-view'; +export enum RenderQueueSortMode { + FRONT_TO_BACK, + BACK_TO_FRONT, +} + +ccenum(RenderQueueSortMode); + /** * @zh * 渲染阶段描述信息。 @@ -19,20 +31,36 @@ import { RenderView } from './render-view'; export interface IRenderStageInfo { name?: string; priority: number; - framebuffer?: GFXFramebuffer; + renderQueues?: RenderQueueDesc[]; + framebuffer?: string; +} + +@ccclass('RenderQueueDesc') +class RenderQueueDesc { + @property + public isTransparent: boolean = false; + @property({ + type: RenderQueueSortMode, + }) + public sortMode: RenderQueueSortMode = RenderQueueSortMode.FRONT_TO_BACK; + @property({ + type: [String], + }) + public stages: string[] = []; } /** * @zh * 渲染阶段。 */ +@ccclass('RenderStage') export abstract class RenderStage { /** * @zh * 渲染流程。 */ - public get flow (): RenderFlow { + public get flow(): RenderFlow { return this._flow; } @@ -40,7 +68,7 @@ export abstract class RenderStage { * @zh * 渲染管线。 */ - public get pipeline (): RenderPipeline { + public get pipeline(): RenderPipeline { return this._pipeline; } @@ -60,36 +88,59 @@ export abstract class RenderStage { return this._framebuffer; } - /** - * @zh - * 渲染流程。 - */ - protected _flow: RenderFlow; - - /** - * @zh - * 渲染管线。 - */ - protected _pipeline: RenderPipeline; - - /** - * @zh - * GFX设备。 - */ - protected _device: GFXDevice; - /** * @zh * 名称。 */ + @property({ + displayOrder: 0, + visible: true + }) protected _name: string = ''; /** * @zh * 优先级。 */ + @property({ + displayOrder: 1, + visible: true + }) protected _priority: number = 0; + @property({ + displayOrder: 2, + visible: true + }) + protected frameBuffer: string = ''; + + @property({ + type: [RenderQueueDesc], + displayOrder: 3, + visible: true + }) + protected renderQueues: RenderQueueDesc[] = []; + + protected _renderQueues: RenderQueue[] = []; + + /** + * @zh + * 渲染流程。 + */ + protected _flow: RenderFlow = null!; + + /** + * @zh + * 渲染管线。 + */ + protected _pipeline: RenderPipeline = null!; + + /** + * @zh + * GFX设备。 + */ + protected _device: GFXDevice | null = null; + /** * @zh * 渲染流程。 @@ -106,7 +157,7 @@ export abstract class RenderStage { * @zh * 清空颜色数组。 */ - protected _clearColors: IGFXColor[]; + protected _clearColors: IGFXColor[] | null = null; /** * @zh @@ -124,7 +175,7 @@ export abstract class RenderStage { * @zh * 渲染区域。 */ - protected _renderArea: IGFXRect; + protected _renderArea: IGFXRect | null = null; /** * @zh @@ -142,7 +193,35 @@ export abstract class RenderStage { * 构造函数。 * @param flow 渲染流程。 */ - constructor (flow: RenderFlow) { + constructor () { + } + + /** + * @zh + * 初始化函数,用于不从资源加载RenderPipeline时使用。 + * @param info 渲染阶段描述信息。 + */ + public initialize (info: IRenderStageInfo): boolean { + if (info.name !== undefined) { + this._name = info.name; + } + + this._priority = info.priority; + + if(info.framebuffer) + this.frameBuffer = info.framebuffer; + + if (info.renderQueues) { + this.renderQueues = info.renderQueues; + } + + return true; + } + + /** + * 把序列化数据转换成运行时数据 + */ + public activate (flow: RenderFlow) { this._flow = flow; this._pipeline = flow.pipeline; this._device = flow.device; @@ -152,16 +231,37 @@ export abstract class RenderStage { } this._device = this._flow.pipeline.root.device; + this._clearColors = [{ r: 0.3, g: 0.6, b: 0.9, a: 1.0 }]; this._renderArea = { x: 0, y: 0, width: 0, height: 0 }; - } - /** - * @zh - * 初始化函数。 - * @param info 渲染阶段描述信息。 - */ - public abstract initialize (info: IRenderStageInfo): boolean; + for (let i = 0; i < this.renderQueues.length; i++) { + let phase = 0; + for (let j = 0; j < this.renderQueues[i].stages.length; j++) { + phase |= getPhaseID(this.renderQueues[i].stages[j]); + } + let sortFunc: (a: IRenderPass, b: IRenderPass) => number = opaqueCompareFn; + switch (this.renderQueues[i].sortMode) { + case RenderQueueSortMode.BACK_TO_FRONT: + sortFunc = transparentCompareFn; + break; + case RenderQueueSortMode.FRONT_TO_BACK: + sortFunc = opaqueCompareFn; + break; + } + this._renderQueues[i] = new RenderQueue({ + isTransparent: this.renderQueues[i].isTransparent, + phases: phase, + sortFunc, + }); + } + + if (this.frameBuffer === 'window') { + this._framebuffer = this._flow.pipeline.root.mainWindow!.framebuffer!; + } else { + this._framebuffer = this._flow.pipeline.getFrameBuffer(this.frameBuffer)!; + } + } /** * @zh @@ -195,10 +295,10 @@ export abstract class RenderStage { * 设置清空颜色。 */ public setClearColor (color: IGFXColor) { - if (this._clearColors.length > 0) { - this._clearColors[0] = color; + if (this._clearColors!.length > 0) { + this._clearColors![0] = color; } else { - this._clearColors.push(color); + this._clearColors!.push(color); } } @@ -231,7 +331,9 @@ export abstract class RenderStage { * 设置渲染区域。 */ public setRenderArea (width: number, height: number) { - this._renderArea.width = width; - this._renderArea.height = height; + this._renderArea!.width = width; + this._renderArea!.height = height; } } + +cc.RenderStage = RenderStage; diff --git a/cocos/core/pipeline/render-view.ts b/cocos/core/pipeline/render-view.ts index f80c646be3..2c9bfbb07c 100644 --- a/cocos/core/pipeline/render-view.ts +++ b/cocos/core/pipeline/render-view.ts @@ -208,7 +208,7 @@ export class RenderView { public setExecuteFlows (flows: string[]) { this.flows.length = 0; - const pipelineFlows = cc.director.root.pipeline.flows; + const pipelineFlows = cc.director.root.pipeline.activeFlows; for (const f of pipelineFlows) { if (flows.indexOf(f.name) !== -1) { this.flows.push(f); diff --git a/cocos/core/pipeline/ui/ui-flow.ts b/cocos/core/pipeline/ui/ui-flow.ts index c9c060ea8b..924c6bcf71 100644 --- a/cocos/core/pipeline/ui/ui-flow.ts +++ b/cocos/core/pipeline/ui/ui-flow.ts @@ -5,9 +5,10 @@ import { UBOGlobal } from '../define'; import { IRenderFlowInfo, RenderFlow } from '../render-flow'; -import { RenderPipeline } from '../render-pipeline'; import { RenderView } from '../render-view'; import { UIStage } from './ui-stage'; +import { RenderQueueSortMode } from '../render-stage'; +import { ForwardFlowPriority } from '../forward/enum'; /** * @zh @@ -15,28 +16,22 @@ import { UIStage } from './ui-stage'; */ export class UIFlow extends RenderFlow { - constructor (pipeline: RenderPipeline) { - super(pipeline); + public static initInfo: IRenderFlowInfo = { + name: 'UIFlow', + priority: ForwardFlowPriority.UI, + }; + + constructor () { + super(); } public initialize (info: IRenderFlowInfo): boolean { - if (info.name !== undefined) { - this._name = info.name; - } + super.initialize(info); - this._priority = info.priority; - - const mainWindow = this._pipeline.root.mainWindow; - if (!mainWindow || !mainWindow.framebuffer) { - return false; - } - - this.createStage(UIStage, { - name: 'UIStage', - priority: 0, - framebuffer: mainWindow.framebuffer, - }); + let uiStage = new UIStage(); + uiStage.initialize(UIStage.initInfo); + this._stages.push(uiStage); return true; } diff --git a/cocos/core/pipeline/ui/ui-stage.ts b/cocos/core/pipeline/ui/ui-stage.ts index a693bbf094..042f1af076 100644 --- a/cocos/core/pipeline/ui/ui-stage.ts +++ b/cocos/core/pipeline/ui/ui-stage.ts @@ -5,10 +5,10 @@ import { GFXCommandBuffer } from '../../gfx/command-buffer'; import { GFXCommandBufferType, IGFXColor } from '../../gfx/define'; import { getPhaseID } from '../pass-phase'; -import { RenderFlow } from '../render-flow'; import { RenderQueue, transparentCompareFn } from '../render-queue'; -import { IRenderStageInfo, RenderStage } from '../render-stage'; +import { IRenderStageInfo, RenderStage, RenderQueueSortMode } from '../render-stage'; import { RenderView } from '../render-view'; +import { RenderFlow } from '../render-flow'; const bufs: GFXCommandBuffer[] = []; const colors: IGFXColor[] = []; @@ -19,35 +19,27 @@ const colors: IGFXColor[] = []; */ export class UIStage extends RenderStage { - private _uiQueue: RenderQueue; - - constructor (flow: RenderFlow) { - super(flow); - this._uiQueue = new RenderQueue({ + public static initInfo: IRenderStageInfo = { + name: 'UIStage', + priority: 0, + renderQueues: [{ isTransparent: true, - phases: getPhaseID('default'), - sortFunc: transparentCompareFn, - }); + stages: ['default'], + sortMode: RenderQueueSortMode.BACK_TO_FRONT, + }], + framebuffer: 'window' + }; + + constructor () { + super(); } - public initialize (info: IRenderStageInfo): boolean { - - if (info.name !== undefined) { - this._name = info.name; - } - - this._priority = info.priority; - - if (info.framebuffer !== undefined) { - this._framebuffer = info.framebuffer; - } - - this._cmdBuff = this._device.createCommandBuffer({ - allocator: this._device.commandAllocator, + public activate(flow: RenderFlow) { + super.activate(flow); + this._cmdBuff = this._device!.createCommandBuffer({ + allocator: this._device!.commandAllocator, type: GFXCommandBufferType.PRIMARY, }); - - return true; } public destroy () { @@ -66,16 +58,16 @@ export class UIStage extends RenderStage { public render (view: RenderView) { - this._uiQueue.clear(); + this._renderQueues[0].clear(); - for (const ro of this._pipeline.renderObjects) { + for (const ro of this._pipeline!.renderObjects) { for (let i = 0; i < ro.model.subModelNum; i++) { for (let j = 0; j < ro.model.getSubModel(i).passes.length; j++) { - this._uiQueue.insertRenderPass(ro, i, j); + this._renderQueues[0].insertRenderPass(ro, i, j); } } } - this._uiQueue.sort(); + this._renderQueues[0].sort(); const framebuffer = view.window!.framebuffer; @@ -84,23 +76,23 @@ export class UIStage extends RenderStage { const camera = view.camera!; const vp = camera.viewport; - this._renderArea.x = vp.x * camera.width; - this._renderArea.y = vp.y * camera.height; - this._renderArea.width = vp.width * camera.width; - this._renderArea.height = vp.height * camera.height; + this._renderArea!.x = vp.x * camera.width; + this._renderArea!.y = vp.y * camera.height; + this._renderArea!.width = vp.width * camera.width; + this._renderArea!.height = vp.height * camera.height; colors[0] = camera.clearColor; cmdBuff.begin(); - cmdBuff.beginRenderPass(framebuffer, this._renderArea, - camera.clearFlag, colors, camera.clearDepth, camera.clearStencil); + cmdBuff.beginRenderPass(framebuffer, this._renderArea!, + camera.clearFlag, [camera.clearColor], camera.clearDepth, camera.clearStencil); - cmdBuff.execute(this._uiQueue.cmdBuffs.array, this._uiQueue.cmdBuffCount); + cmdBuff.execute(this._renderQueues[0].cmdBuffs.array, this._renderQueues[0].cmdBuffCount); cmdBuff.endRenderPass(); cmdBuff.end(); bufs[0] = cmdBuff; - this._device.queue.submit(bufs); + this._device!.queue.submit(bufs); } } diff --git a/cocos/core/root.ts b/cocos/core/root.ts index 438ccd9b63..5769710c67 100644 --- a/cocos/core/root.ts +++ b/cocos/core/root.ts @@ -5,7 +5,6 @@ import { builtinResMgr } from './3d/builtin'; import { GFXDevice } from './gfx/device'; import { GFXWindow, IGFXWindowInfo } from './gfx/window'; -import { ForwardPipeline } from './pipeline/forward/forward-pipeline'; import { RenderPipeline } from './pipeline/render-pipeline'; import { IRenderViewInfo, RenderView } from './pipeline/render-view'; import { DataPoolManager } from './renderer/data-pool-manager'; @@ -219,18 +218,6 @@ export class Root { builtinResMgr.initBuiltinRes(this._device); - this._pipeline = new ForwardPipeline(this); - if (!this._pipeline.initialize(info)) { - this.destroy(); - return false; - } - - this._ui = new UI(this); - if (!this._ui.initialize()) { - this.destroy(); - return false; - } - cc.view.on('design-resolution-changed', () => { const width = cc.game.canvas.width; const height = cc.game.canvas.height; @@ -290,6 +277,16 @@ export class Root { } } + public setRenderPipeline (rppl: RenderPipeline) { + this._pipeline = rppl; + this._pipeline.activate(this); + this._ui = new UI(this); + if (!this._ui.initialize()) { + this.destroy(); + return false; + } + } + /** * @zh * 激活指定窗口为当前窗口 @@ -343,8 +340,8 @@ export class Root { for (const view of this._views) { if (view.isEnable && (view.window && (view.window.isOffscreen || - (!view.window.isOffscreen && (view.window === this._curWindow))))) { - this._pipeline!.render(view); + (!view.window.isOffscreen && (view.window === this._curWindow)))) && this._pipeline) { + this._pipeline.render(view); } }