modify ui renderpipeline

This commit is contained in:
zhuxuguang
2019-02-25 16:16:41 +08:00
parent e61c194750
commit 64573f6ca2
15 changed files with 359 additions and 94 deletions
+36 -18
View File
@@ -99,6 +99,18 @@ export class CanvasComponent extends Component {
}
}
@property()
get priority () {
return this._priority;
}
set priority (val: number) {
this._priority = val;
if (this._camera) {
this._camera.priority = val;
}
}
get visibility () {
if (this._camera) {
return this._camera.view.visibility;
@@ -125,6 +137,8 @@ export class CanvasComponent extends Component {
private _fitWidth = false;
@property
private _fitHeight = true;
@property
private _priority = 0;
// @property
// private _priority = 0;
@@ -157,25 +171,29 @@ export class CanvasComponent extends Component {
public __preload () {
const cameraNode = new Node('UICamera');
cameraNode.setPosition(0, 0, 1000);
this._camera = this._getRenderScene().createCamera({
name: 'ui',
node: cameraNode,
projection: CameraComponent.ProjectionType.ORTHO,
fov: 45,
stencil: 0,
orthoHeight: 10,
far: 4096,
near: 0.1,
color: new Color(0, 0, 0, 255),
clearFlags: GFXClearFlag.DEPTH | GFXClearFlag.STENCIL,
rect: new Rect(0, 0, 1, 1),
depth: 1,
targetDisplay: 0,
isUI: true,
});
if (!CC_EDITOR) {
this._camera = cc.director.root.ui.renderScene.createCamera({
name: 'ui',
node: cameraNode,
projection: CameraComponent.ProjectionType.ORTHO,
fov: 45,
stencil: 0,
orthoHeight: 10,
far: 4096,
near: 0.1,
color: new Color(0, 0, 0, 255),
clearFlags: GFXClearFlag.DEPTH | GFXClearFlag.STENCIL,
rect: new Rect(0, 0, 1, 1),
depth: 1,
targetDisplay: 0,
priority: this._priority,
isUI: true,
pipeline: 'ui',
});
const device = cc.director.root.device;
this._camera.resize(device.width, device.height);
}
const device = cc.director.root.device;
this._camera.resize(device.width, device.height);
cc.director.root.ui.addScreen(this);
+24 -11
View File
@@ -6,6 +6,7 @@ import { RenderPipeline } from '../pipeline/render-pipeline';
import { IRenderViewInfo, RenderView } from '../pipeline/render-view';
import { IRenderSceneInfo, RenderScene } from '../renderer/scene/render-scene';
import { UI } from '../renderer/ui/ui';
import { UIPipeline } from '../pipeline/ui/ui-pipeline';
export let _createSceneFun;
export let _createViewFun;
@@ -67,6 +68,7 @@ export class Root {
private _scenes: RenderScene[] = [];
private _views: RenderView[] = [];
private _frameTime: number = 0;
private _pipelines: Map<string, RenderPipeline> = new Map<string, RenderPipeline>();
constructor (device: GFXDevice) {
this._device = device;
@@ -84,7 +86,7 @@ export class Root {
this._mainWindow = this._device.mainWindow;
this._curWindow = this._mainWindow;
const pipeline = new ForwardPipeline(this);
let pipeline: RenderPipeline = new ForwardPipeline(this);
if (!pipeline.initialize()) {
this.destroy();
return false;
@@ -92,6 +94,14 @@ export class Root {
this._pipeline = pipeline;
this.registerRenderPipeline('forward', pipeline);
pipeline = new UIPipeline(this);
if (!pipeline.initialize()) {
this.destroy();
return false;
}
this.registerRenderPipeline('ui', pipeline);
builtinResMgr.initBuiltinRes(this._device);
this._ui = new UI(this);
@@ -121,6 +131,14 @@ export class Root {
this._mainWindow = null;
}
public registerRenderPipeline (name: string, rp: RenderPipeline) {
this._pipelines.set(name, rp);
}
public getRenderPipeline (name: string) {
return this._pipelines.get(name);
}
public resize (width: number, height: number) {
this._device.resize(width, height);
@@ -150,13 +168,10 @@ export class Root {
for (const view of this._views) {
if (view.isEnable && view.window === this._mainWindow) {
this._pipeline!.render(view);
this._pipelines.get(view.camera.pipeline)!.render(view);
}
}
this._ui!.update(deltaTime);
this._ui!.render();
this._device.present();
}
@@ -217,12 +232,10 @@ export class Root {
const view: RenderView = this._createViewFun(this, info.camera);
view.initialize(info);
if (!info.isUI) {
this._views.push(view);
this._views.sort((a: RenderView, b: RenderView) => {
return a.priority - b.priority;
});
}
this._views.push(view);
this._views.sort((a: RenderView, b: RenderView) => {
return a.priority - b.priority;
});
return view;
}
+8 -5
View File
@@ -62,9 +62,12 @@ export class RenderQueue {
}
public add (model: Model, camera: Camera) {
model.node.getWorldPosition(_v3tmp);
vec3.sub(_v3tmp, _v3tmp, camera.position);
const depth = vec3.dot(_v3tmp, camera.forward);
let depth = 0;
if (model.node) {
model.node.getWorldPosition(_v3tmp);
vec3.sub(_v3tmp, _v3tmp, camera.position);
depth = vec3.dot(_v3tmp, camera.forward);
}
for (let i = 0; i < model.subModelNum; ++i) {
const subModel = model.getSubModel(i);
@@ -77,13 +80,13 @@ export class RenderQueue {
// TODO: model priority
if (!isTransparent) {
const hash = (0 << 30) | pass.priority | subModel.priority | p;
const hash = (0 << 30) | pass.priority << 16 | subModel.priority << 8 | p;
this.opaques.push({
hash, depth, shaderId: pso.shader.id,
subModel, cmdBuff: subModel.commandBuffers[p]});
} else {
const hash = (1 << 30) | pass.priority | subModel.priority | p;
const hash = (1 << 30) | pass.priority << 16 | subModel.priority << 8 | p;
this.transparents.push({
hash, depth, shaderId: pso.shader.id,
+8 -1
View File
@@ -31,6 +31,13 @@ export class RenderView {
return this._priority;
}
public set priority (val: number) {
this._priority = val;
if (this._isUI) {
this._priority |= 1 << 30;
}
}
public set visibility (vis) {
this._visibility = vis;
}
@@ -71,8 +78,8 @@ export class RenderView {
public initialize (info: IRenderViewInfo): boolean {
this._name = info.name;
this._priority = info.priority;
this._isUI = info.isUI;
this.priority = info.priority;
return true;
}
+36
View File
@@ -0,0 +1,36 @@
import { IRenderFlowInfo, RenderFlow } from '../render-flow';
import { RenderPipeline } from '../render-pipeline';
import { UIStage } from './ui-stage';
export class UIFlow extends RenderFlow {
constructor (pipeline: RenderPipeline) {
super(pipeline);
}
public initialize (info: IRenderFlowInfo): boolean {
if (info.name !== undefined) {
this._name = info.name;
}
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,
});
return true;
}
public destroy (): void {
this.destroyStages();
}
}
+39
View File
@@ -0,0 +1,39 @@
import { Root } from '../../core/root';
import { RenderPassStage } from '../define';
import { RenderPipeline } from '../render-pipeline';
import { RenderView } from '../render-view';
import { UIFlow } from './ui-flow';
export class UIPipeline extends RenderPipeline {
constructor (root: Root) {
super(root);
}
public initialize (): boolean {
if (!this.createUBOs()) {
return false;
}
// create flows
this.createFlow<UIFlow>(UIFlow, {
name: 'UIFlow',
priority: 0,
});
return true;
}
public destroy () {
this.destroyFlows();
this.clearRenderPasses();
this.destroyUBOs();
this.destroyQuadInputAssembler();
}
protected updateUBOs (view: RenderView) {
// view.camera.matViewProj.m14=1E-100;
super.updateUBOs(view);
}
}
+67
View File
@@ -0,0 +1,67 @@
import { GFXCommandBuffer } from '../../gfx/command-buffer';
import { GFXCommandBufferType } from '../../gfx/define';
import { RenderFlow } from '../render-flow';
import { IRenderStageInfo, RenderStage } from '../render-stage';
import { RenderView } from '../render-view';
const bufs: GFXCommandBuffer[] = [];
export class UIStage extends RenderStage {
constructor (flow: RenderFlow) {
super(flow);
}
public initialize (info: IRenderStageInfo): boolean {
if (info.name !== undefined) {
this._name = info.name;
}
this._priority = info.priority;
this._framebuffer = info.framebuffer;
this._cmdBuff = this._device.createCommandBuffer({
allocator: this._device.commandAllocator,
type: GFXCommandBufferType.PRIMARY,
});
return true;
}
public destroy () {
if (this._cmdBuff) {
this._cmdBuff.destroy();
this._cmdBuff = null;
}
}
public resize (width: number, height: number) {
}
public render (view: RenderView) {
const framebuffer = view.window!.framebuffer;
const cmdBuff = this._cmdBuff!;
const queue = this._pipeline.queue;
const camera = view.camera!;
this._renderArea.width = this.flow.pipeline.root.device.width;
this._renderArea.height = this.flow.pipeline.root.device.height;
cmdBuff.begin();
cmdBuff.beginRenderPass(framebuffer, this._renderArea,
[], camera.clearDepth, camera.clearStencil);
cmdBuff.execute(queue.cmdBuffs.array, queue.cmdBuffCount);
cmdBuff.endRenderPass();
cmdBuff.end();
bufs[0] = cmdBuff;
this._device.queue.submit(bufs);
}
}
+2 -2
View File
@@ -188,13 +188,13 @@ export class Pass {
const texName = inf && inf.value ? inf.value + '-texture' : _type2default[u.type];
const texture = builtinResMgr.get<TextureBase>(texName);
if (texture) { this._textureViews[u.binding] = texture.getGFXTextureView()!; }
else { console.warn(`illegal texture default value ${texName}`); }
else { console.warn('illegal texture default value ' + texName); }
}
}
public getHandle (name: string) {
const handle = this._handleMap[name];
if (!handle) { console.warn(`illegal property name ${name}`); }
if (!handle) { console.warn('illegal property name ' + name); }
return handle;
}
+20 -1
View File
@@ -25,6 +25,8 @@ export interface ICameraInfo {
clearFlags: GFXClearFlag;
rect: Rect;
targetDisplay: number;
priority: number;
pipeline: string;
isUI?: boolean;
}
@@ -60,6 +62,8 @@ export class Camera {
private _node: Node | null = null;
private _view: RenderView;
private _visibility: number = 0;
private _priority: number = 0;
private _pipeline: string;
constructor (scene: RenderScene, info: ICameraInfo) {
this._scene = scene;
@@ -71,6 +75,8 @@ export class Camera {
this._nearClip = info.near;
this._farClip = info.far;
this._viewport = info.rect;
this._pipeline = info.pipeline || 'forward';
this._priority = info.priority || 0;
const c = info.color;
color4.set(this._clearColor, c.r / 255, c.g / 255, c.b / 255, c.a / 255);
@@ -83,7 +89,7 @@ export class Camera {
this._view = scene.root.createView({
camera: this,
name: this._name,
priority: RenderViewPriority.GENERAL,
priority: this._priority,
isUI: (info.isUI !== undefined ? info.isUI : false),
});
@@ -295,6 +301,19 @@ export class Camera {
return this._visibility;
}
get pipeline () {
return this._pipeline;
}
get priority () {
return this._priority;
}
set priority (val: number) {
this._priority = val;
this._view.priority = this._priority;
}
public changeTargetDisplay (val: number) {
const scene = this._scene;
const win = scene.root.windows[val] || scene.root.mainWindow;
+9 -5
View File
@@ -60,7 +60,7 @@ export class Model {
* @returns the hosting node
*/
get node () {
return this._node;
return this._node!;
}
/**
@@ -82,6 +82,10 @@ export class Model {
return this._viewID;
}
set viewID (id: number) {
this._viewID = id;
}
/**
* Set the user key
* @param {number} key
@@ -101,7 +105,7 @@ export class Model {
protected _type: string = 'default';
protected _device: GFXDevice;
private _scene: RenderScene;
private _node: Node;
private _node: Node | null;
private _id: number;
private _enabled: boolean = false;
private _viewID: number = -1;
@@ -119,7 +123,7 @@ export class Model {
/**
* Setup a default empty model
*/
constructor (scene: RenderScene, node: Node) {
constructor (scene: RenderScene, node: Node | null) {
this._device = cc.director.root.device;
this._scene = scene;
this._node = node;
@@ -154,7 +158,7 @@ export class Model {
}
public _updateTransform () {
if (this._node.hasChanged) {
if (this._node && this._node.hasChanged) {
this._node.updateWorldTransformFull();
if (this._modelBounds) {
this._modelBounds.transform(
@@ -205,7 +209,7 @@ export class Model {
} else {
const oldMat = this._subModels[idx].material;
this._subModels[idx].destroy();
this.releasePSO(oldMat);
this.releasePSO(oldMat!);
}
this.allocatePSO(mat);
this._subModels[idx].initialize(subMeshData, mat, this._matPSORecord.get(mat)!);
+2 -2
View File
@@ -170,7 +170,7 @@ export class RenderScene {
this._spotLights = [];
}
public createModel<T extends Model> (clazz: new (scene: RenderScene, node: Node) => T, node: Node): T {
public createModel<T extends Model> (clazz: new (scene: RenderScene, node: Node | null) => T, node: Node | null): T {
const model = new clazz(this, node);
this._models.push(model);
return model;
@@ -206,7 +206,7 @@ export class RenderScene {
pool.reset();
for (const m of this._models) {
const node = m.node;
if (!m.enabled || !cc.Layers.check(node.layer, mask) || !m.modelBounds) { continue; }
if (!node || !m.enabled || !cc.Layers.check(node.layer, mask) || !m.modelBounds) { continue; }
// transform ray back to model space
mat4.invert(m4, node.getWorldMatrix(m4));
vec3.transformMat4(modelRay.o, worldRay.o, m4);
+2 -2
View File
@@ -94,7 +94,7 @@ export class SubModel {
return;
}
for (let i = 0; i < this._material!.passes.length; i++) {
if (this._material!.passes[i].primitive !== this._subMeshObject!.primitiveMode) {
if (this._subMeshObject && this._material!.passes[i].primitive !== this._subMeshObject.primitiveMode) {
cc.error(`the model(%d)'s primitive type doesn't match its pass's`, i);
}
this.recordCommandBuffer(i);
@@ -123,7 +123,7 @@ export class SubModel {
this._castShadow = val;
}
private recordCommandBuffer (index: number) {
protected recordCommandBuffer (index: number) {
const device = cc.director.root.device as GFXDevice;
const pso = this._psos![index];
if (this._cmdBuffers[index] == null) {
+46
View File
@@ -1,5 +1,51 @@
import { Material } from '../../3d/assets/material';
import { GFXInputAssembler } from '../../gfx/input-assembler';
import { GFXPipelineState } from '../../gfx/pipeline-state';
import { Model } from '../scene/model';
import { RenderScene } from '../scene/render-scene';
import { SubModel } from '../scene/submodel';
import { UIDrawBatch } from './ui';
export class UIBatchModel extends Model {
private _subModel: UISubModel;
constructor (scene: RenderScene) {
super(scene, null);
this._subModel = new UISubModel();
}
public _updateTransform () {
}
public updateUBOs () {
}
public initialize (ia: GFXInputAssembler, batch: UIDrawBatch) {
this._subModel.directInitialize(ia, batch.material!, batch.pipelineState!);
this._subModels[0] = this._subModel;
}
public destroy () {
this._subModel.destroy();
}
}
class UISubModel extends SubModel {
constructor () {
super();
this.psos = [];
}
public directInitialize (ia: GFXInputAssembler, mat: Material, pso: GFXPipelineState) {
this._inputAssembler = ia;
this.psos[0] = pso;
this.material = mat;
}
public destroy () {
if (this.commandBuffers.length > 0) {
this.commandBuffers[0].destroy();
}
}
}
+8 -1
View File
@@ -3,6 +3,7 @@ import { GFXBindingLayout } from '../../gfx/binding-layout';
import { GFXPipelineState } from '../../gfx/pipeline-state';
import { Pass } from '../core/pass';
import Pool from '../../3d/memop/pool';
import { UBOGlobal } from '../../pipeline/render-pipeline';
export interface IUIMaterialInfo {
material: Material;
@@ -36,7 +37,13 @@ export class UIMaterial {
this._pass = this._material.passes[0];
this._psos = new Pool(() => this._pass!.createPipelineState()!, 1);
this._psos = new Pool(() => {
const pso = this._pass!.createPipelineState()!;
if (!CC_EDITOR) {
pso.pipelineLayout.layouts[0].bindBuffer(UBOGlobal.BLOCK.binding, cc.director.root.getRenderPipeline('ui').globalUBO);
}
return pso;
}, 1);
return true;
}
+52 -46
View File
@@ -31,7 +31,8 @@ import { vfmt } from '../../gfx/vertex-format-sample';
import { Node } from '../../scene-graph/node';
import { Camera } from '../scene/camera';
import { RenderScene } from '../scene/render-scene';
import { UIMaterial } from './ui-material';
import { IUIMaterialInfo, UIMaterial } from './ui-material';
import { UIBatchModel } from './ui-batch-model';
export class UBOUI {
public static MAT_VIEW_PROJ_OFFSET: number = 0;
@@ -143,12 +144,29 @@ export class UI {
private _sortChildList: Pool<any[]> = new Pool(() => {
return [];
}, 128);
private _uiModelPool: Pool<UIBatchModel> | null = null;
private _modelInUse: CachedArray<UIBatchModel>;
get renderScene (): RenderScene {
return this._scene;
}
constructor (private _root: Root) {
this._device = _root.device;
this._scene = this._root.createScene({
name: 'GUIScene',
});
if (CC_EDITOR) {
cc.director.on(cc.Director.EVENT_BEFORE_SCENE_LAUNCH, (scene) => {
this._scene = scene.renderScene;
// the models will be destroyed in renderScene
this._modelInUse.clear();
this._uiModelPool = new Pool(() => this._scene.createModel<UIBatchModel>(UIBatchModel, null), 2);
});
} else {
this._scene = this._root.createScene({
name: 'GUIScene',
});
this._uiModelPool = new Pool(() => this._scene.createModel<UIBatchModel>(UIBatchModel, null), 2);
}
this._modelInUse = new CachedArray<UIBatchModel>(10);
// this._cameraNode = new Node('UICameraNode');
@@ -179,6 +197,8 @@ export class UI {
this._vertF32Count = 9;
this._vertStride = this._vertF32Count * 4;
cc.director.on(cc.Director.EVENT_BEFORE_DRAW, this.update, this);
// this.resize(this._camera.width, this._camera.height);
}
@@ -261,6 +281,9 @@ export class UI {
public addScreen (comp) {
this._screens.push(comp);
if (comp.camera) {
comp.camera.view.visibility = this._screens.length;
}
}
public getScreen (visibility: number) {
@@ -280,6 +303,11 @@ export class UI {
if (idx !== -1) {
this._screens.splice(idx, 1);
}
for (let i = idx; i < this._screens.length;) {
if (this._screens[i].camera) {
this._screens[i].camera.view.visibility = ++i;
}
}
}
public createBuffer (vertexCount: number, indiceCount: number) {
@@ -327,64 +355,42 @@ export class UI {
bufferBatch.ib!.update(bufferBatch.vui16!);
}
}
this.render();
}
public render () {
let batchPriority = 0;
for (let i = 0; i < this._modelInUse.length; i++) {
this._modelInUse.get(i).enabled = false;
this._uiModelPool!.free(this._modelInUse.get(i));
}
this._modelInUse.clear();
if (this._batches.length) {
const framebuffer = this._root.curWindow!.framebuffer;
const cmdBuff = this._cmdBuff!;
cmdBuff.begin();
let curCamera: Camera | null = null;
for (let i = 0; i < this._batches.length; ++i) {
const batch = this._batches.array[i];
const camera = batch.camera!;
if (curCamera !== camera) {
if (curCamera) {
cmdBuff.endRenderPass();
}
// this._renderArea.width = camera.width;
// this._renderArea.height = camera.height;
this._renderArea.width = this._root.device.width;
this._renderArea.height = this._root.device.height;
// TODO: add screen adapter
// update ubo
camera.matViewProj.m14 /= 1000; // hack:to be resolved later
mat4.array(_mat4Array, camera.matViewProj);
this._uboUI.view.set(_mat4Array, UBOUI.MAT_VIEW_PROJ_OFFSET);
this._uiUBO!.update(this._uboUI.view);
curCamera = camera;
cmdBuff.beginRenderPass(framebuffer, this._renderArea, [], camera.clearDepth, camera.clearStencil);
}
const bindingLayout = batch.bindingLayout!;
bindingLayout.bindBuffer(0, this._uiUBO!);
bindingLayout.bindTextureView(1, batch.texView!);
bindingLayout.bindTextureView(0, batch.texView!);
bindingLayout.update();
const ia = batch.bufferBatch!.ia!;
ia.firstIndex = batch.firstIdx;
ia.indexCount = batch.idxCount;
cmdBuff.bindPipelineState(batch.pipelineState!);
cmdBuff.bindBindingLayout(bindingLayout);
cmdBuff.bindInputAssembler(batch.bufferBatch!.ia!);
cmdBuff.draw(batch.bufferBatch!.ia!);
const uiModel = this._uiModelPool!.alloc();
uiModel.initialize(ia, batch);
uiModel.enabled = true;
uiModel.getSubModel(0).priority = batchPriority++;
if (batch.camera) {
uiModel.viewID = batch.camera.view.visibility;
}
this._modelInUse.push(uiModel);
}
cmdBuff.endRenderPass();
cmdBuff.end();
this._device.queue.submit([cmdBuff]);
}
}
@@ -569,7 +575,7 @@ export class UI {
}
if (curMaterialHash !== uiRenderData.material.hash ||
curTexView !== uiRenderData.texture.getGFXTextureView()) {
curTexView !== uiRenderData.texture.getGFXTextureView()) {
if (curTexView) {
curDrawBatch = this._drawBatchPool.alloc();
curDrawBatch.camera = uiCanvas.camera;