add ubo common header

This commit is contained in:
jerrywwl
2019-01-09 14:10:00 +08:00
parent 13dd1bec0a
commit 2ba51db195
13 changed files with 397 additions and 275 deletions
-119
View File
@@ -1,119 +0,0 @@
import enums from './enums';
import plane from './plane';
import { vec3 } from '../../core/vmath';
let _v = new Array(8);
_v[0] = vec3.create( 1, 1, 1);
_v[1] = vec3.create(-1, 1, 1);
_v[2] = vec3.create(-1, -1, 1);
_v[3] = vec3.create( 1, -1, 1);
_v[4] = vec3.create( 1, 1, -1);
_v[5] = vec3.create(-1, 1, -1);
_v[6] = vec3.create(-1, -1, -1);
_v[7] = vec3.create( 1, -1, -1);
export default class frustum {
constructor() {
this._type = enums.SHAPE_FRUSTUM;
this.planes = new Array(6);
for (let i = 0; i < 6; ++i) {
this.planes[i] = plane.create();
}
this.vertices = new Array(8);
for (let i = 0; i < 8; ++i) {
this.vertices[i] = vec3.create();
}
}
/**
* create a new frustum
*
* @return {frustum}
*/
static create() {
return new frustum();
}
/**
* Clone a frustum
*
* @param {frustum} f frustum to clone
* @return {frustum} the new frustum
*/
static clone(f) {
return frustum.copy(new frustum(), f);
}
/**
* Copy the values from one frustum to another
*
* @param {frustum} out the receiving frustum
* @param {frustum} f the source frustum
* @return {frustum}
*/
static copy(out, f) {
out._type = f._type;
for (let i = 0; i < 6; ++i) {
plane.copy(out.planes[i], f.planes[i]);
}
for (let i = 0; i < 8; ++i) {
vec3.copy(out.vertices[i], f.vertices[i]);
}
return out;
}
/**
* Set whether to use accurate intersection testing function on this frustum
*
* @param {boolean} b
*/
set accurate(b) {
this._type = b ? enums.SHAPE_FRUSTUM_ACCURATE : enums.SHAPE_FRUSTUM;
}
/**
* Update the frustum information according to the given transform matrix.
* Note that the resulting planes are not normalized under normal mode.
*
* @param {mat4} m the view-projection matrix
* @param {mat4} inv the inverse view-projection matrix
*/
update(m, inv) {
// RTR4, ch. 22.14.1, p. 983
// extract frustum planes from view-proj matrix.
// left plane
vec3.set(this.planes[0].n, m.m03 + m.m00, m.m07 + m.m04, m.m11 + m.m08);
this.planes[0].d = -(m.m15 + m.m12);
// right plane
vec3.set(this.planes[1].n, m.m03 - m.m00, m.m07 - m.m04, m.m11 - m.m08);
this.planes[1].d = -(m.m15 - m.m12);
// bottom plane
vec3.set(this.planes[2].n, m.m03 + m.m01, m.m07 + m.m05, m.m11 + m.m09);
this.planes[2].d = -(m.m15 + m.m13);
// top plane
vec3.set(this.planes[3].n, m.m03 - m.m01, m.m07 - m.m05, m.m11 - m.m09);
this.planes[3].d = -(m.m15 - m.m13);
// near plane
vec3.set(this.planes[4].n, m.m03 + m.m02, m.m07 + m.m06, m.m11 + m.m10);
this.planes[4].d = -(m.m15 + m.m14);
// far plane
vec3.set(this.planes[5].n, m.m03 - m.m02, m.m07 - m.m06, m.m11 - m.m10);
this.planes[5].d = -(m.m15 - m.m14);
if (this._type !== enums.SHAPE_FRUSTUM_ACCURATE) return;
// normalize planes
for (let i = 0; i < 6; i++) {
let plane = this.planes[i];
let invDist = 1 / vec3.magnitude(plane.n);
vec3.scale(plane.n, plane.n, invDist);
plane.d *= invDist;
}
// update frustum vertices
for (let i = 0; i < 8; i++) {
vec3.transformMat4(this.vertices[i], _v[i], inv);
}
}
}
+124
View File
@@ -0,0 +1,124 @@
import enums from './enums';
import plane from './plane';
import { vec3, mat4 } from '../../core/vmath';
let _v = new Array(8);
_v[0] = vec3.create(1, 1, 1);
_v[1] = vec3.create(-1, 1, 1);
_v[2] = vec3.create(-1, -1, 1);
_v[3] = vec3.create(1, -1, 1);
_v[4] = vec3.create(1, 1, -1);
_v[5] = vec3.create(-1, 1, -1);
_v[6] = vec3.create(-1, -1, -1);
_v[7] = vec3.create(1, -1, -1);
export class frustum {
public planes: plane[];
public vertices: vec3[];
private _type: number;
constructor() {
this._type = enums.SHAPE_FRUSTUM;
this.planes = new Array(6);
for (let i = 0; i < 6; ++i) {
this.planes[i] = plane.create();
}
this.vertices = new Array(8);
for (let i = 0; i < 8; ++i) {
this.vertices[i] = vec3.create();
}
}
/**
* create a new frustum
*
* @return {frustum}
*/
static create() {
return new frustum();
}
/**
* Clone a frustum
*
* @param {frustum} f frustum to clone
* @return {frustum} the new frustum
*/
static clone(f: frustum) {
return frustum.copy(new frustum(), f);
}
/**
* Copy the values from one frustum to another
*
* @param {frustum} out the receiving frustum
* @param {frustum} f the source frustum
* @return {frustum}
*/
static copy(out, f) {
out._type = f._type;
for (let i = 0; i < 6; ++i) {
plane.copy(out.planes[i], f.planes[i]);
}
for (let i = 0; i < 8; ++i) {
vec3.copy(out.vertices[i], f.vertices[i]);
}
return out;
}
/**
* Set whether to use accurate intersection testing function on this frustum
*
* @param {boolean} b
*/
set accurate(b) {
this._type = b ? enums.SHAPE_FRUSTUM_ACCURATE : enums.SHAPE_FRUSTUM;
}
/**
* Update the frustum information according to the given transform matrix.
* Note that the resulting planes are not normalized under normal mode.
*
* @param {mat4} m the view-projection matrix
* @param {mat4} inv the inverse view-projection matrix
*/
update(m: mat4, inv: mat4) {
// RTR4, ch. 22.14.1, p. 983
// extract frustum planes from view-proj matrix.
// left plane
vec3.set(this.planes[0].n, m.m03 + m.m00, m.m07 + m.m04, m.m11 + m.m08);
this.planes[0].d = -(m.m15 + m.m12);
// right plane
vec3.set(this.planes[1].n, m.m03 - m.m00, m.m07 - m.m04, m.m11 - m.m08);
this.planes[1].d = -(m.m15 - m.m12);
// bottom plane
vec3.set(this.planes[2].n, m.m03 + m.m01, m.m07 + m.m05, m.m11 + m.m09);
this.planes[2].d = -(m.m15 + m.m13);
// top plane
vec3.set(this.planes[3].n, m.m03 - m.m01, m.m07 - m.m05, m.m11 - m.m09);
this.planes[3].d = -(m.m15 - m.m13);
// near plane
vec3.set(this.planes[4].n, m.m03 + m.m02, m.m07 + m.m06, m.m11 + m.m10);
this.planes[4].d = -(m.m15 + m.m14);
// far plane
vec3.set(this.planes[5].n, m.m03 - m.m02, m.m07 - m.m06, m.m11 - m.m10);
this.planes[5].d = -(m.m15 - m.m14);
if (this._type !== enums.SHAPE_FRUSTUM_ACCURATE) return;
// normalize planes
for (let i = 0; i < 6; i++) {
let plane = this.planes[i];
let invDist = 1 / vec3.magnitude(plane.n);
vec3.scale(plane.n, plane.n, invDist);
plane.d *= invDist;
}
// update frustum vertices
for (let i = 0; i < 8; i++) {
vec3.transformMat4(this.vertices[i], _v[i], inv);
}
}
}
+1 -1
View File
@@ -8,6 +8,6 @@ export { default as triangle } from './triangle';
export { default as sphere } from './sphere';
export { default as aabb } from './aabb';
export { default as obb } from './obb';
export { default as frustum } from './frustum';
export { frustum } from './frustum';
export { default as Octree } from './octree';
export { Keyframe, AnimationCurve } from './curve';
+18 -1
View File
@@ -28,6 +28,23 @@ import { EPSILON } from './utils';
*/
class mat4 {
public m00: number = 1;
public m01: number = 0;
public m02: number = 0;
public m03: number = 0;
public m04: number = 0;
public m05: number = 1;
public m06: number = 0;
public m07: number = 0;
public m08: number = 0;
public m09: number = 0;
public m10: number = 1;
public m11: number = 0;
public m12: number = 0;
public m13: number = 0;
public m14: number = 0;
public m15: number = 1;
/**
* Creates a matrix, with elements specified separately.
*
@@ -985,7 +1002,7 @@ class mat4 {
out.x = (mat.m06 - mat.m09) / S;
out.y = (mat.m08 - mat.m02) / S;
out.z = (mat.m01 - mat.m04) / S;
} else if ((mat.m00 > mat.m05) & (mat.m00 > mat.m10)) {
} else if ((mat.m00 > mat.m05) && (mat.m00 > mat.m10)) {
S = Math.sqrt(1.0 + mat.m00 - mat.m05 - mat.m10) * 2;
out.w = (mat.m06 - mat.m09) / S;
out.x = 0.25 * S;
@@ -3,6 +3,7 @@ import { Root } from "../../core/root";
import { ForwardFlow } from "./forward-flow";
import { GFXRenderPass } from "../../gfx/render-pass";
import { GFXBufferUsageBit, GFXMemoryUsageBit } from "../../gfx/define";
import { RenderQueue } from "../render-queue";
export enum ForwardFlowPriority {
FORWARD = 0,
+11 -3
View File
@@ -14,7 +14,7 @@ export class ForwardStage extends RenderStage {
public initialize(info: RenderStageInfo): boolean {
if(info.name !== undefined) {
if (info.name !== undefined) {
this._name = info.name;
}
@@ -38,13 +38,21 @@ export class ForwardStage extends RenderStage {
}
public render(view: RenderView) {
let cmdBuff = <GFXCommandBuffer>this._cmdBuff;
let cmdBuff = this._cmdBuff as GFXCommandBuffer;
let queue = this._pipeline.queue;
this._renderArea.width = view.width;
this._renderArea.height = view.height;
cmdBuff.begin();
cmdBuff.beginRenderPass(<GFXFramebuffer>this._framebuffer, this._renderArea, this._clearColors, this._clearDepth, this._clearStencil);
cmdBuff.draw(<GFXInputAssembler>this._pipeline.quadIA);
for (let item of queue.opaques) {
item.model.commandBuffers
}
cmdBuff.endRenderPass();
cmdBuff.end();
+2 -14
View File
@@ -13,6 +13,7 @@ import { GFXTexture } from "../../gfx/texture";
import { GFXTextureView } from "../../gfx/texture-view";
import { GFXSampler } from "../../gfx/sampler";
import { GFXBuffer } from "../../gfx/buffer";
import { UBOGlobal } from "../render-pipeline";
/*
declare module '*.png' {
@@ -88,20 +89,7 @@ export class TestStage extends RenderStage {
this._shader = this._device.createShader({
name: "test",
stages: [vsStage, fsStage],
blocks: [{
binding: 0, name: "Global", members: [
{ name: "u_time", type: GFXType.FLOAT4, count: 1 },
{ name: "u_screenSize", type: GFXType.FLOAT4, count: 1 },
{ name: "u_screenScale", type: GFXType.FLOAT4, count: 1 },
{ name: "u_matView", type: GFXType.MAT4, count: 1 },
{ name: "u_matViewInv", type: GFXType.MAT4, count: 1 },
{ name: "u_matProj", type: GFXType.MAT4, count: 1 },
{ name: "u_matProjInv", type: GFXType.MAT4, count: 1 },
{ name: "u_matViewProj", type: GFXType.MAT4, count: 1 },
{ name: "u_matViewProjInv", type: GFXType.MAT4, count: 1 },
{ name: "u_cameraPos", type: GFXType.FLOAT4, count: 1 },
]
}, {
blocks: [UBOGlobal.BLOCK, {
binding: 5, name: "UBO", members: [
{ name: "u_color", type: GFXType.FLOAT4, count: 1 },
]
+168 -98
View File
@@ -1,53 +1,127 @@
import { RenderView } from "./render-view";
import { RenderFlow, RenderFlowInfo } from "./render-flow";
import { Root } from "../core/root";
import { GFXRenderPass } from "../gfx/render-pass";
import { GFXDevice } from "../gfx/device";
import { GFXBuffer } from "../gfx/buffer";
import { GFXInputAssembler, GFXInputAttribute } from "../gfx/input-assembler";
import { GFXBufferUsageBit, GFXMemoryUsageBit, GFXFormat } from "../gfx/define";
import { Camera } from "../renderer/scene/camera";
import vec4 from "../core/vmath/vec4";
import mat4 from "../core/vmath/mat4";
import { Root } from '../core/root';
import mat4 from '../core/vmath/mat4';
import { GFXBuffer } from '../gfx/buffer';
import { GFXBufferUsageBit, GFXFormat, GFXMemoryUsageBit, GFXType } from '../gfx/define';
import { GFXDevice } from '../gfx/device';
import { GFXInputAssembler, GFXInputAttribute } from '../gfx/input-assembler';
import { GFXRenderPass } from '../gfx/render-pass';
import { GFXTexture } from '../gfx/texture';
import { GFXTextureView } from '../gfx/texture-view';
import { Camera } from '../renderer/scene/camera';
import { RenderFlow, RenderFlowInfo } from './render-flow';
import { RenderView } from './render-view';
import { vec3 } from '../core/vmath';
import { RenderQueue } from './render-queue';
import { intersect } from '../3d/geom-utils';
import { GFXUniformBlock } from '../gfx/shader';
export enum RenderPassStage {
DEFAULT = 100,
};
}
export class UBOGlobal {
static TIME_OFFSET: number = 0;
static SCREEN_SIZE_OFFSET: number = UBOGlobal.TIME_OFFSET + 4;
static SCREEN_SCALE_OFFSET: number = UBOGlobal.SCREEN_SIZE_OFFSET + 4;
static MAT_VIEW_OFFSET: number = UBOGlobal.SCREEN_SCALE_OFFSET + 4;
static MAT_VIEW_INV_OFFSET: number = UBOGlobal.MAT_VIEW_OFFSET + 16;
static MAT_PROJ_OFFSET: number = UBOGlobal.MAT_VIEW_INV_OFFSET + 16;
static MAT_PROJ_INV_OFFSET: number = UBOGlobal.MAT_PROJ_OFFSET + 16;
static MAT_VIEW_PROJ_OFFSET: number = UBOGlobal.MAT_PROJ_INV_OFFSET + 16;
static MAT_VIEW_PROJ_INV_OFFSET: number = UBOGlobal.MAT_VIEW_PROJ_OFFSET + 16;
static CAMERA_POS_OFFSET: number = UBOGlobal.MAT_VIEW_PROJ_INV_OFFSET + 16;
static COUNT: number = UBOGlobal.CAMERA_POS_OFFSET + 4;
static SIZE: number = UBOGlobal.COUNT * 4;
public static TIME_OFFSET: number = 0;
public static SCREEN_SIZE_OFFSET: number = UBOGlobal.TIME_OFFSET + 4;
public static SCREEN_SCALE_OFFSET: number = UBOGlobal.SCREEN_SIZE_OFFSET + 4;
public static MAT_VIEW_OFFSET: number = UBOGlobal.SCREEN_SCALE_OFFSET + 4;
public static MAT_VIEW_INV_OFFSET: number = UBOGlobal.MAT_VIEW_OFFSET + 16;
public static MAT_PROJ_OFFSET: number = UBOGlobal.MAT_VIEW_INV_OFFSET + 16;
public static MAT_PROJ_INV_OFFSET: number = UBOGlobal.MAT_PROJ_OFFSET + 16;
public static MAT_VIEW_PROJ_OFFSET: number = UBOGlobal.MAT_PROJ_INV_OFFSET + 16;
public static MAT_VIEW_PROJ_INV_OFFSET: number = UBOGlobal.MAT_VIEW_PROJ_OFFSET + 16;
public static CAMERA_POS_OFFSET: number = UBOGlobal.MAT_VIEW_PROJ_INV_OFFSET + 16;
public static COUNT: number = UBOGlobal.CAMERA_POS_OFFSET + 4;
public static SIZE: number = UBOGlobal.COUNT * 4;
view: Float32Array = new Float32Array(UBOGlobal.COUNT);
};
public static BLOCK: GFXUniformBlock = {
binding: 0, name: "Global", members: [
{ name: "u_time", type: GFXType.FLOAT4, count: 1 },
{ name: "u_screenSize", type: GFXType.FLOAT4, count: 1 },
{ name: "u_screenScale", type: GFXType.FLOAT4, count: 1 },
{ name: "u_matView", type: GFXType.MAT4, count: 1 },
{ name: "u_matViewInv", type: GFXType.MAT4, count: 1 },
{ name: "u_matProj", type: GFXType.MAT4, count: 1 },
{ name: "u_matProjInv", type: GFXType.MAT4, count: 1 },
{ name: "u_matViewProj", type: GFXType.MAT4, count: 1 },
{ name: "u_matViewProjInv", type: GFXType.MAT4, count: 1 },
{ name: "u_cameraPos", type: GFXType.FLOAT4, count: 1 },
]
};
public view: Float32Array = new Float32Array(UBOGlobal.COUNT);
}
export class UBOLocal {
static MAT_WORLD_OFFSET: number = 0;
static MAT_WORLD_VIEW_OFFSET: number = UBOLocal.MAT_WORLD_OFFSET + 16;
static MAT_WORLD_VIEW_PROJ_OFFSET: number = UBOLocal.MAT_WORLD_VIEW_OFFSET + 16;
static WORLD_POS_OFFSET: number = UBOLocal.MAT_WORLD_VIEW_PROJ_OFFSET + 16;
static COUNT: number = UBOLocal.WORLD_POS_OFFSET + 4;
static SIZE: number = UBOGlobal.COUNT * 4;
public static MAT_WORLD_OFFSET: number = 0;
public static MAT_WORLD_VIEW_OFFSET: number = UBOLocal.MAT_WORLD_OFFSET + 16;
public static MAT_WORLD_VIEW_PROJ_OFFSET: number = UBOLocal.MAT_WORLD_VIEW_OFFSET + 16;
public static WORLD_POS_OFFSET: number = UBOLocal.MAT_WORLD_VIEW_PROJ_OFFSET + 16;
public static COUNT: number = UBOLocal.WORLD_POS_OFFSET + 4;
public static SIZE: number = UBOLocal.COUNT * 4;
view: Float32Array = new Float32Array(UBOLocal.COUNT);
};
public static BLOCK: GFXUniformBlock = {
binding: 1, name: "Local", members: [
{ name: "u_matWorld", type: GFXType.MAT4, count: 1 },
{ name: "u_matWorldView", type: GFXType.MAT4, count: 1 },
{ name: "u_matWorldViewProj", type: GFXType.MAT4, count: 1 },
{ name: "u_worldPos", type: GFXType.FLOAT4, count: 1 },
]
};
let _vec4Array = new Float32Array(4);
let _mat4Array = new Float32Array(16);
let _outMat = new mat4;
public view: Float32Array = new Float32Array(UBOLocal.COUNT);
}
const _vec4Array = new Float32Array(4);
const _mat4Array = new Float32Array(16);
const _outMat = new mat4;
export abstract class RenderPipeline {
public get root(): Root {
return this._root;
}
public get device(): GFXDevice {
return this._device;
}
public get queue(): RenderQueue {
return this._queue;
}
public get flows(): RenderFlow[] {
return this._flows;
}
public get quadIA(): GFXInputAssembler {
return this._quadIA as GFXInputAssembler;
}
public get uboGlobal(): UBOGlobal {
return this._uboGlobal;
}
public get globalUBO(): GFXBuffer {
return this._globalUBO as GFXBuffer;
}
public get defaultTexture(): GFXTexture {
return this._defaultTex as GFXTexture;
}
protected _root: Root;
protected _device: GFXDevice;
protected _queue: RenderQueue = new RenderQueue();
protected _renderPasses: Map<number, GFXRenderPass> = new Map();
protected _flows: RenderFlow[] = [];
protected _quadVB: GFXBuffer | null = null;
protected _quadIB: GFXBuffer | null = null;
protected _quadIA: GFXInputAssembler | null = null;
protected _uboGlobal: UBOGlobal = new UBOGlobal();
protected _globalUBO: GFXBuffer | null = null;
protected _defaultTex: GFXTexture | null = null;
protected _defaultTexView: GFXTextureView | null = null;
constructor(root: Root) {
this._root = root;
this._device = root.device;
@@ -58,6 +132,10 @@ export abstract class RenderPipeline {
public render(view: RenderView) {
view.camera.update();
this.sceneCulling(view);
this.updateUBOs(view);
for (let i = 0; i < this._flows.length; ++i) {
@@ -72,7 +150,7 @@ export abstract class RenderPipeline {
}
public getRenderPass(stage: number): GFXRenderPass | null {
let renderPass = this._renderPasses.get(stage);
const renderPass = this._renderPasses.get(stage);
if (renderPass) {
return renderPass;
} else {
@@ -89,7 +167,7 @@ export abstract class RenderPipeline {
}
public createFlow<T extends RenderFlow>(clazz: new (pipeline: RenderPipeline) => T, info: RenderFlowInfo): RenderFlow | null {
let flow: RenderFlow = new clazz(this);
const flow: RenderFlow = new clazz(this);
if (flow.initialize(info)) {
this._flows.push(flow);
this._flows.sort((a: RenderFlow, b: RenderFlow) => {
@@ -103,42 +181,18 @@ export abstract class RenderPipeline {
}
public destroyFlows() {
for (let i = 0; i < this._flows.length; ++i) {
this._flows[i].destroy();
for (const flow of this._flows) {
flow.destroy();
}
this._flows = [];
}
public get root(): Root {
return this._root;
}
public get device(): GFXDevice {
return this._device;
}
public get flows(): RenderFlow[] {
return this._flows;
}
public get quadIA(): GFXInputAssembler {
return <GFXInputAssembler>this._quadIA;
}
public get uboGlobal(): UBOGlobal {
return this._uboGlobal;
}
public get globalUBO(): GFXBuffer {
return <GFXBuffer>this._globalUBO;
}
protected createQuadInputAssembler(): boolean {
// create vertex buffer
let vbStride = Float32Array.BYTES_PER_ELEMENT * 4;
let vbSize = vbStride * 4;
const vbStride = Float32Array.BYTES_PER_ELEMENT * 4;
const vbSize = vbStride * 4;
this._quadVB = this._device.createBuffer({
usage: GFXBufferUsageBit.VERTEX | GFXBufferUsageBit.TRANSFER_DST,
@@ -147,11 +201,11 @@ export abstract class RenderPipeline {
stride: vbStride,
});
if(!this._quadVB) {
if (!this._quadVB) {
return false;
}
let verts = new Float32Array(4 * 4);
const verts = new Float32Array(4 * 4);
let n = 0;
verts[n++] = -1.0; verts[n++] = -1.0; verts[n++] = 0.0; verts[n++] = 0.0;
verts[n++] = 1.0; verts[n++] = -1.0; verts[n++] = 1.0; verts[n++] = 0.0;
@@ -161,8 +215,8 @@ export abstract class RenderPipeline {
this._quadVB.update(verts);
// create index buffer
let ibStride = Uint8Array.BYTES_PER_ELEMENT;
let ibSize = ibStride * 6;
const ibStride = Uint8Array.BYTES_PER_ELEMENT;
const ibSize = ibStride * 6;
this._quadIB = this._device.createBuffer({
usage: GFXBufferUsageBit.INDEX | GFXBufferUsageBit.TRANSFER_DST,
@@ -171,11 +225,11 @@ export abstract class RenderPipeline {
stride: ibStride,
});
if(!this._quadIB) {
if (!this._quadIB) {
return false;
}
let indices = new Uint8Array(6);
const indices = new Uint8Array(6);
indices[0] = 0; indices[1] = 1; indices[2] = 2;
indices[3] = 1; indices[4] = 3; indices[5] = 2;
@@ -183,9 +237,9 @@ export abstract class RenderPipeline {
// create input assembler
let attributes: GFXInputAttribute[] = [
{name: "a_position", format: GFXFormat.RG32F},
{name: "a_texCoord", format: GFXFormat.RG32F},
const attributes: GFXInputAttribute[] = [
{ name: 'a_position', format: GFXFormat.RG32F },
{ name: 'a_texCoord', format: GFXFormat.RG32F },
];
this._quadIA = this._device.createInputAssembler({
@@ -198,17 +252,17 @@ export abstract class RenderPipeline {
}
protected destroyQuadInputAssembler() {
if(this._quadVB) {
if (this._quadVB) {
this._quadVB.destroy();
this._quadVB = null;
}
if(this._quadIB) {
if (this._quadIB) {
this._quadIB.destroy();
this._quadIB = null;
}
if(this._quadIA) {
if (this._quadIA) {
this._quadIA.destroy();
this._quadIA = null;
}
@@ -221,7 +275,7 @@ export abstract class RenderPipeline {
size: UBOGlobal.SIZE,
});
if(!this._globalUBO) {
if (!this._globalUBO) {
return false;
}
@@ -229,14 +283,14 @@ export abstract class RenderPipeline {
}
protected destroyUBOs() {
if(this._globalUBO) {
if (this._globalUBO) {
this._globalUBO.destroy();
this._globalUBO = null;
}
}
protected updateUBOs(view: RenderView) {
let camera = <Camera>view.camera;
const camera = view.camera as Camera;
camera.update();
// update UBOGlobal
@@ -248,8 +302,8 @@ export abstract class RenderPipeline {
_vec4Array[0] = this._root.device.width;
_vec4Array[1] = this._root.device.height;
_vec4Array[2] = 1.0/_vec4Array[0];
_vec4Array[3] = 1.0/_vec4Array[1];
_vec4Array[2] = 1.0 / _vec4Array[0];
_vec4Array[3] = 1.0 / _vec4Array[1];
this._uboGlobal.view.set(_vec4Array, UBOGlobal.SCREEN_SIZE_OFFSET);
_vec4Array[0] = 1.0;
@@ -275,25 +329,41 @@ export abstract class RenderPipeline {
mat4.array(_mat4Array, camera.matViewProj);
this._uboGlobal.view.set(_mat4Array, UBOGlobal.MAT_VIEW_PROJ_OFFSET);
mat4.invert(_outMat, camera.matViewProj);
mat4.array(_mat4Array, _outMat);
mat4.array(_mat4Array, camera.matViewProjInv);
this._uboGlobal.view.set(_mat4Array, UBOGlobal.MAT_VIEW_PROJ_INV_OFFSET);
vec4.array(_vec4Array, camera.position);
vec3.array(_vec4Array, camera.position);
_vec4Array[3] = 1.0;
this._uboGlobal.view.set(_vec4Array, UBOGlobal.CAMERA_POS_OFFSET);
// update ubos
(<GFXBuffer>this._globalUBO).update(this._uboGlobal.view);
(this._globalUBO as GFXBuffer).update(this._uboGlobal.view);
}
protected _root: Root;
protected _device: GFXDevice;
protected _renderPasses: Map<number, GFXRenderPass> = new Map;
protected _flows: RenderFlow[] = [];
protected _quadVB: GFXBuffer | null = null;
protected _quadIB: GFXBuffer | null = null;
protected _quadIA: GFXInputAssembler | null = null;
protected _uboGlobal: UBOGlobal = new UBOGlobal;
protected _globalUBO: GFXBuffer | null = null;
};
protected sceneCulling(view: RenderView) {
let scene = view.scene;
let camera = view.camera;
for (let model of scene.models) {
// filter model by view visibility
if (view.visibility > 0 && model._viewID !== view.visibility) {
continue;
}
model._updateTransform();
// frustum culling
if (model._boundingShape) { // if model does not have boundingBox, skip culling.
if (!intersect.aabb_frustum(model._boundingShape, camera.frustum)) {
// console.log('model is not in view frustum.');
continue;
}
}
// add model pass to render queue
this._queue.add(model);
}
}
}
+19 -14
View File
@@ -18,23 +18,28 @@ export class RenderQueue {
constructor() {
}
public add(model: Model, pass: Pass) {
let pso = pass.pipelineState;
public add(model: Model) {
let isTransparent = pso.blendState.targets[0].blend;
for(let i = 0; i < model.passes.length; ++i) {
let pass = model.passes[i];
let hash: number = 0;
let pso = pass.pipelineState;
if(!isTransparent) {
// Opaque objects are sorted by depth front to back, then by shader id.
hash = (0 << 63) | (pso.shader.id << 13);
this._opaques.push({hash, model, pass});
} else {
// Transparent objects are sorted by depth back to front, then by by shader id.
hash = (1 << 63) | (pso.shader.id << 13);
this._transparents.push({hash, model, pass});
let isTransparent = pso.blendState.targets[0].blend;
let hash: number = 0;
if(!isTransparent) {
// Opaque objects are sorted by depth front to back, then by shader id.
hash = (0 << 63) | (pso.shader.id << 13);
this._opaques.push({hash, model, pass});
} else {
// Transparent objects are sorted by depth back to front, then by by shader id.
hash = (1 << 63) | (pso.shader.id << 13);
this._transparents.push({hash, model, pass});
}
}
}
+13 -4
View File
@@ -84,6 +84,10 @@ export class RenderView {
return this._priority;
}
public get visibility(): number {
return this._visiblity;
}
public get width(): number {
return this._width;
}
@@ -92,17 +96,22 @@ export class RenderView {
return this._height;
}
public get camera(): Camera | null {
return this._camera;
public get camera(): Camera {
return this._camera as Camera;
}
public get scene(): RenderScene | null {
return this._scene;
public get scene(): RenderScene {
return this._scene as RenderScene;
}
public set visibility(vis: number) {
this._visiblity = vis;
}
private _name: string = "";
private _window: GFXWindow | null = null;
private _priority: number = 0;
private _visiblity: number = 0;
private _width: number = 0;
private _height: number = 0;
private _scene: RenderScene | null = null;
+31 -17
View File
@@ -1,6 +1,7 @@
import { RenderScene } from "./render-scene";
import Node from "../../scene-graph/node";
import { vec3, mat4, quat } from "../../core/vmath";
import { frustum } from "../../3d/geom-utils/frustum";
export enum NodeSpace
{
@@ -16,6 +17,25 @@ export enum CameraProjection {
export class Camera {
private _scene: RenderScene;
private _name: string;
private _node: Node;
private _proj: CameraProjection;
private _width: number;
private _height: number;
private _aspect: number;
private _fov: number;
private _nearClip: number;
private _farClip: number;
private _matView: mat4 = new mat4;
private _matProj: mat4 = new mat4;
private _matViewProj: mat4 = new mat4;
private _matViewProjInv: mat4 = new mat4;
private _position: vec3 = new vec3;
private _rotation: quat = new quat;
private _direction: vec3 = new vec3;
private _frustum: frustum = new frustum;
constructor(scene: RenderScene, name: string) {
this._scene = scene;
this._name = name;
@@ -59,6 +79,9 @@ export class Camera {
// view-projection
mat4.mul(this._matViewProj, this._matProj, this._matView);
mat4.invert(this._matViewProjInv, this._matViewProj);
this._frustum.update(this._matViewProj, this._matViewProjInv);
}
public set fov(fov: number) {
@@ -121,6 +144,14 @@ export class Camera {
return this._matViewProj;
}
public get matViewProjInv(): mat4 {
return this._matViewProjInv;
}
public get frustum(): frustum {
return this._frustum;
}
public get position(): vec3 {
this._node.getWorldPosition(this._position);
return this._position;
@@ -211,21 +242,4 @@ export class Camera {
quat.rotationTo(this._rotation, vec3.UNIT_Z, dir);
this._node.setRotation(this._rotation);
}
private _scene: RenderScene;
private _name: string;
private _node: Node;
private _proj: CameraProjection;
private _width: number;
private _height: number;
private _aspect: number;
private _fov: number;
private _nearClip: number;
private _farClip: number;
private _matView: mat4 = new mat4;
private _matProj: mat4 = new mat4;
private _matViewProj: mat4 = new mat4;
private _position: vec3 = new vec3;
private _rotation: quat = new quat;
private _direction: vec3 = new vec3;
};
+7 -3
View File
@@ -11,6 +11,7 @@ import { Effect } from '../core/effect';
import { Pass } from '../core/pass';
import { GFXCommandBuffer } from '../../gfx/command-buffer';
import { Material } from '../../3d/assets/material';
import { RenderItem } from '../../pipeline/render-queue';
/**
* A representation of a model
@@ -149,7 +150,10 @@ export default class Model {
this.recordCommandBuffer(i);
}
for (let i = this._cmdBuffers.length - 1; i >= this._material.passes.length; i--) {
this._cmdBuffers.pop().destroy();
let cmdBuff = this._cmdBuffers.pop();
if(cmdBuff) {
cmdBuff.destroy();
}
}
}
@@ -177,11 +181,11 @@ export default class Model {
this._userKey = key;
}
get passList(): Pass[] {
get passes(): Pass[] {
return (<Material>this._material).passes;
}
get cmdBufferList(): GFXCommandBuffer[] {
get commandBuffers(): GFXCommandBuffer[] {
return this._cmdBuffers;
}
+2 -1
View File
@@ -11,7 +11,8 @@
"max-classes-per-file": false,
"variable-name": false,
"object-literal-sort-keys": false,
"ban-types": false
"ban-types": false,
"no-bitwise:": false,
},
"rulesDirectory": []
}