diff --git a/cocos/core/root.ts b/cocos/core/root.ts index 12d17b31b0..e9496722b4 100644 --- a/cocos/core/root.ts +++ b/cocos/core/root.ts @@ -3,8 +3,8 @@ 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 { IRenderSceneInfo, RenderScene } from '../renderer/scene/render-scene'; import { GUI } from '../renderer/gui/gui'; +import { IRenderSceneInfo, RenderScene } from '../renderer/scene/render-scene'; export let _createSceneFun; export let _createViewFun; @@ -104,7 +104,7 @@ export class Root { this._frameTime = deltaTime; for (const view of this._views) { - if (view.isEnable() && view.isAttached) { + if (view.isEnable()) { ( this._pipeline as RenderPipeline).render(view); } } @@ -141,14 +141,11 @@ export class Root { this._windows = []; } - public createScene (info: IRenderSceneInfo): RenderScene | null { - const scene = this._createSceneFun(this); - if (scene.initialize(info)) { - this._scenes.push(scene); - return scene; - } else { - return null; - } + public createScene (info: IRenderSceneInfo): RenderScene { + const scene: RenderScene = this._createSceneFun(this); + scene.initialize(info); + this._scenes.push(scene); + return scene; } public destroyScene (scene: RenderScene) { @@ -168,17 +165,16 @@ export class Root { this._scenes = []; } - public createView (info: IRenderViewInfo): RenderView | null { - const view = this._createViewFun(this); - if (view.initialize(info)) { - this._views.push(view); - this._views.sort((a: RenderView, b: RenderView) => { - return a.priority - b.priority; - }); - return view; - } else { - return null; - } + public createView (info: IRenderViewInfo): RenderView { + const view: RenderView = this._createViewFun(this, info.camera); + view.initialize(info); + + this._views.push(view); + this._views.sort((a: RenderView, b: RenderView) => { + return a.priority - b.priority; + }); + + return view; } public destroyView (view: RenderView) { diff --git a/cocos/pipeline/render-pipeline.ts b/cocos/pipeline/render-pipeline.ts index cddb7ab4cf..5b97ca1bf6 100644 --- a/cocos/pipeline/render-pipeline.ts +++ b/cocos/pipeline/render-pipeline.ts @@ -338,8 +338,8 @@ export abstract class RenderPipeline { protected sceneCulling (view: RenderView) { - const scene = view.scene; const camera = view.camera; + const scene = camera.scene; this._queue.clear(); diff --git a/cocos/pipeline/render-queue.ts b/cocos/pipeline/render-queue.ts index e2f87ad394..8b0fc6850f 100644 --- a/cocos/pipeline/render-queue.ts +++ b/cocos/pipeline/render-queue.ts @@ -6,13 +6,14 @@ import { Camera } from '../renderer/scene/camera'; import { Model } from '../renderer/scene/model'; export class RenderItem { - public hash: number; + public hash: number = 0; + public depth: number = 0; + public shaderId: number = 0; public model: Model; public pass: Pass; public cmdBuff: GFXCommandBuffer; - constructor (hash: number, model: Model, pass: Pass, cmdBuff: GFXCommandBuffer) { - this.hash = hash; + constructor (model: Model, pass: Pass, cmdBuff: GFXCommandBuffer) { this.model = model; this.pass = pass; this.cmdBuff = cmdBuff; @@ -24,22 +25,38 @@ export class RenderQueue { public transparents: CachedArray; public cmdBuffs: CachedArray; public cmdBuffCount: number = 0; - private _buffer: ArrayBuffer; - private _f32View: Float32Array; - private _ui32View: Uint32Array; constructor () { - const compareFn = (a: RenderItem, b: RenderItem) => { - return a.hash - b.hash; + + // Opaque objects are sorted by depth front to back -> pass priority -> shader id. + const opaqueCompareFn = (a: RenderItem, b: RenderItem) => { + if (a.hash === b.hash) { + if (a.depth === b.depth) { + return a.shaderId - b.shaderId; + } else { + return a.depth - b.depth; + } + } else { + return a.hash - b.hash; + } }; - this.opaques = new CachedArray(64, compareFn); - this.transparents = new CachedArray(64, compareFn); - this.cmdBuffs = new CachedArray(64); + // Transparent objects are sorted by depth back to front -> pass priority -> shader id. + const transparentCompareFn = (a: RenderItem, b: RenderItem) => { + if (a.hash === b.hash) { + if (a.depth === b.depth) { + return a.shaderId - b.shaderId; + } else { + return b.depth - a.depth; + } + } else { + return a.hash - b.hash; + } + }; - this._buffer = new ArrayBuffer(Float32Array.BYTES_PER_ELEMENT); - this._f32View = new Float32Array(this._buffer); - this._ui32View = new Uint32Array(this._buffer); + this.opaques = new CachedArray(64, opaqueCompareFn); + this.transparents = new CachedArray(64, transparentCompareFn); + this.cmdBuffs = new CachedArray(64); } public clear () { @@ -50,11 +67,7 @@ export class RenderQueue { public add (model: Model, camera: Camera) { - this._f32View[0] = vec3.distance(camera.node.getPosition(), model.node.getPosition()); - let ui32Depth: number = this._ui32View[0]; - const mask = -(ui32Depth >> 31) | 0x8000000000000000; - ui32Depth = (ui32Depth ^ mask); - const ui32DepthInv = (ui32Depth ^ 0x00000000ffffffff); + const depth = vec3.distance(camera.node.getPosition(), model.node.getPosition()); for (let i = 0; i < model.passes.length; ++i) { const pass = model.passes[i]; @@ -69,15 +82,15 @@ export class RenderQueue { // TODO: model priority if (!isTransparent) { - // Opaque objects are sorted by depth front to back -> pass priority -> shader id. - const hash = (0 << 61) | (i << 45) | (ui32Depth << 13) | pso.shader.id; + const hash = (0 << 30) | i; - this.opaques.push({hash, model, pass, cmdBuff: model.commandBuffers[i]}); + this.opaques.push({hash, depth, shaderId: pso.shader.id, + model, pass, cmdBuff: model.commandBuffers[i]}); } else { - // Transparent objects are sorted by depth back to front -> pass priority -> shader id. - const hash = (1 << 61) | (i << 45) | (ui32DepthInv << 13) | pso.shader.id; + const hash = (1 << 30) | i; - this.transparents.push({hash, model, pass, cmdBuff: model.commandBuffers[i]}); + this.transparents.push({hash, depth, shaderId: pso.shader.id, + model, pass, cmdBuff: model.commandBuffers[i]}); } } } diff --git a/cocos/pipeline/render-view.ts b/cocos/pipeline/render-view.ts index f413551b7f..563accdb74 100644 --- a/cocos/pipeline/render-view.ts +++ b/cocos/pipeline/render-view.ts @@ -1,15 +1,14 @@ import { Root } from '../core/root'; import { GFXWindow } from '../gfx/window'; import { Camera } from '../renderer/scene/camera'; -import { RenderScene } from '../renderer/scene/render-scene'; export enum RenderViewPriority { GENERAL = 100, } export interface IRenderViewInfo { + camera: Camera; name: string; - window: GFXWindow; priority: number; } @@ -42,12 +41,8 @@ export class RenderView { return this._camera as Camera; } - public get scene () { - return this._scene as RenderScene; - } - public static registerCreateFunc (root: Root) { - root._createViewFun = (_root: Root): RenderView => new RenderView(_root); + root._createViewFun = (_root: Root, _camera: Camera): RenderView => new RenderView(_root, _camera); } private _root: Root; @@ -55,24 +50,17 @@ export class RenderView { private _window: GFXWindow | null = null; private _priority: number = 0; private _visibility: number = 0; - private _scene: RenderScene | null = null; private _camera: Camera | null = null; - private _isAttached: boolean = false; private _isEnable: boolean = true; - private constructor (root: Root) { + private constructor (root: Root, camera: Camera) { this._root = root; + this._camera = camera; } public initialize (info: IRenderViewInfo): boolean { - if (!info.window) { - console.error('RenderViewInfo.window is null.'); - return false; - } - this._name = info.name; - this._window = info.window; this._priority = info.priority; return true; @@ -81,24 +69,13 @@ export class RenderView { public destroy () { this._window = null; this._priority = 0; + this._camera = null; } public resize (width, height) { } - public attach (camera: Camera) { - this._scene = camera.scene; - this._camera = camera; - this._isAttached = true; - } - - public detach () { - this._scene = null; - this._camera = null; - this._isAttached = false; - } - public enable (isEnable: boolean) { this._isEnable = isEnable; } @@ -106,8 +83,4 @@ export class RenderView { public isEnable (): boolean { return this._isEnable; } - - public isAttached (): boolean { - return this._isAttached; - } } diff --git a/cocos/renderer/gui/gui.ts b/cocos/renderer/gui/gui.ts index 0a1c6843f6..a26eee950a 100644 --- a/cocos/renderer/gui/gui.ts +++ b/cocos/renderer/gui/gui.ts @@ -1,4 +1,5 @@ import { Root } from '../../core/root'; +import { RenderScene } from '../scene/render-scene'; export class GUI { @@ -6,10 +7,18 @@ export class GUI { return this._root; } + public get scene (): RenderScene { + return this._scene; + } + private _root: Root; + private _scene: RenderScene; constructor (root: Root) { this._root = root; + this._scene = this._root.createScene({ + name: 'GUIScene', + }); } public frameMove (deltaTime: number) { diff --git a/cocos/renderer/scene/camera.ts b/cocos/renderer/scene/camera.ts index 299307c783..9b549e644e 100644 --- a/cocos/renderer/scene/camera.ts +++ b/cocos/renderer/scene/camera.ts @@ -53,7 +53,7 @@ export class Camera { private _matViewProjInv: mat4 = new Mat4(); private _frustum: frustum = new frustum(); private _node: Node | null = null; - private _view: RenderView | null = null; + private _view: RenderView; private _visibility: number = 0; constructor (scene: RenderScene, info: ICameraInfo) { @@ -74,6 +74,13 @@ export class Camera { this._clearFlag = info.clearFlags; this._aspect = this._width = this._height = 1; + + this._view = scene.root.createView({ + camera: this, + name: this._name, + priority: RenderViewPriority.GENERAL, + }); + this.changeTargetDisplay(info.targetDisplay); } @@ -105,6 +112,10 @@ export class Camera { this._frustum.update(this._matViewProj, this._matViewProjInv); } + get view (): RenderView { + return this._view; + } + set node (val: Node) { this._node = val; } @@ -246,16 +257,7 @@ export class Camera { if (win) { this._width = win.width; this._height = win.height; - if (!this._view) { - this._view = scene.root.createView({ - name: this._name, - window: win, - priority: RenderViewPriority.GENERAL, - }); - if (this._view) { this._view.attach(this); } - } else { - this._view.window = win; - } + this._view.window = win; } this._aspect = this._width / this._height; } diff --git a/cocos/renderer/scene/render-scene.ts b/cocos/renderer/scene/render-scene.ts index 3138f8323a..7f2d562dac 100644 --- a/cocos/renderer/scene/render-scene.ts +++ b/cocos/renderer/scene/render-scene.ts @@ -180,8 +180,8 @@ export class RenderScene { if (!vbuffer || !vbuffer.buffer || !ibuffer || !ibuffer.buffer) { continue; } // narrowphase distance = Infinity; - const vb = new Float32Array(vbuffer.buffer); - const ib = new Uint16Array(ibuffer.buffer); + const vb = new Float32Array(vbuffer.buffer as ArrayBuffer); + const ib = new Uint16Array(ibuffer.buffer as ArrayBuffer); const sides = subModel.doubleSided; for (let j = 0; j < ib.length; j += 3) { const i0 = ib[j] * 3;