-
Notifications
You must be signed in to change notification settings - Fork 4
/
RenderPass.ts
452 lines (403 loc) · 14.6 KB
/
RenderPass.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import {RnObject} from '../core/RnObject';
import {IEntity} from '../core/Entity';
import {FrameBuffer} from './FrameBuffer';
import {SceneGraphComponent} from '../components/SceneGraph/SceneGraphComponent';
import {MeshComponent} from '../components/Mesh/MeshComponent';
import {Vector4} from '../math/Vector4';
import {EntityUID, Index} from '../../types/CommonTypes';
import {Material} from '../materials/core/Material';
import {WebGLStrategy} from '../../webgl/main';
import {System} from '../system/System';
import {ModuleManager} from '../system/ModuleManager';
import {WebGLResourceRepository} from '../../webgl/WebGLResourceRepository';
import {Primitive} from '../geometry/Primitive';
import {MutableVector4} from '../math/MutableVector4';
import {IVector4} from '../math/IVector';
import {ISceneGraphEntity, IMeshEntity} from '../helpers/EntityHelper';
import {WellKnownComponentTIDs} from '../components/WellKnownComponentTIDs';
import {CameraComponent} from '../components/Camera/CameraComponent';
import {RenderBufferTargetEnum} from '../definitions';
/**
* A render pass is a collection of the resources which is used in rendering process.
*/
export class RenderPass extends RnObject {
private __entities: (IMeshEntity | ISceneGraphEntity)[] = [];
private __sceneGraphDirectlyAdded: SceneGraphComponent[] = [];
private __topLevelSceneGraphComponents?: SceneGraphComponent[] = [];
private __meshComponents?: MeshComponent[];
private __frameBuffer?: FrameBuffer;
private __resolveFrameBuffer?: FrameBuffer;
private __resolveFrameBuffer2?: FrameBuffer;
private __viewport?: MutableVector4;
public toClearColorBuffer = false;
public toClearDepthBuffer = true;
public toClearStencilBuffer = false;
public isDepthTest = true;
public clearColor = Vector4.fromCopyArray([1, 1, 1, 1]);
public clearDepth = 1;
public clearStencil = 0;
public cameraComponent?: CameraComponent;
public cullFrontFaceCCW = true;
private __material?: Material;
private __primitiveMaterial: Map<Primitive, Material> = new Map();
private __webglRenderingStrategy?: WebGLStrategy;
public isVrRendering = true;
public isOutputForVr = false;
public toRenderOpaquePrimitives = true;
public toRenderTransparentPrimitives = true;
public toRenderEffekseerEffects = false;
public __renderTargetColorAttachments?: RenderBufferTargetEnum[];
private __preRenderFunc?: Function;
private static __tmp_Vector4_0 = MutableVector4.zero();
constructor() {
super();
}
clone() {
const renderPass = new RenderPass();
renderPass.__entities = this.__entities.concat();
renderPass.__sceneGraphDirectlyAdded =
this.__sceneGraphDirectlyAdded.concat();
renderPass.__topLevelSceneGraphComponents =
this.__topLevelSceneGraphComponents?.concat();
renderPass.__meshComponents = this.__meshComponents?.concat();
renderPass.__frameBuffer = this.__frameBuffer;
renderPass.__resolveFrameBuffer = this.__resolveFrameBuffer;
renderPass.__resolveFrameBuffer2 = this.__resolveFrameBuffer2;
renderPass.__viewport = this.__viewport?.clone();
renderPass.toClearColorBuffer = this.toClearColorBuffer;
renderPass.toClearDepthBuffer = this.toClearDepthBuffer;
renderPass.toClearStencilBuffer = this.toClearStencilBuffer;
renderPass.isDepthTest = this.isDepthTest;
renderPass.clearColor = this.clearColor.clone();
renderPass.clearDepth = this.clearDepth;
renderPass.clearStencil = this.clearStencil;
renderPass.cameraComponent = this.cameraComponent;
renderPass.cullFrontFaceCCW = this.cullFrontFaceCCW;
renderPass.__material = this.__material;
renderPass.__primitiveMaterial = new Map(this.__primitiveMaterial);
renderPass.__webglRenderingStrategy = this.__webglRenderingStrategy;
renderPass.isVrRendering = this.isVrRendering;
renderPass.isOutputForVr = this.isOutputForVr;
renderPass.toRenderOpaquePrimitives = this.toRenderOpaquePrimitives;
renderPass.toRenderTransparentPrimitives =
this.toRenderTransparentPrimitives;
renderPass.__preRenderFunc = this.__preRenderFunc;
return renderPass;
}
setPreRenderFunction(func: Function) {
this.__preRenderFunc = func;
}
unsetPreRenderFunction() {
this.__preRenderFunc = void 0;
}
doPreRender() {
if (this.__preRenderFunc != null) {
this.__preRenderFunc();
}
}
/**
* Add entities to draw.
* @param entities An array of entities.
*/
addEntities(entities: (IMeshEntity | ISceneGraphEntity)[]) {
for (const entity of entities) {
const sceneGraphComponent = entity.getSceneGraph()!;
this.__sceneGraphDirectlyAdded.push(sceneGraphComponent);
const collectedSgComponents = SceneGraphComponent.flattenHierarchy(
sceneGraphComponent,
false
);
const collectedEntities = collectedSgComponents.map(
(sg: SceneGraphComponent) => {
return sg.entity;
}
);
// Eliminate duplicates
const map: Map<EntityUID, IMeshEntity | ISceneGraphEntity> =
this.__entities
.concat(collectedEntities)
.reduce(
(
map: Map<EntityUID, IMeshEntity | ISceneGraphEntity>,
entity: IMeshEntity | ISceneGraphEntity
) => {
map.set(entity.entityUID, entity);
return map;
},
new Map()
);
this.__entities = Array.from(map.values());
}
this.__meshComponents = void 0;
this.__topLevelSceneGraphComponents = void 0;
this.__collectMeshComponents();
this.__collectTopLevelSceneGraphComponents();
}
/**
* Gets the list of entities on this render pass.
* @return An array of entities
*/
get entities(): IEntity[] {
return this.__entities;
}
/**
* Clear entities on this render pass.
*/
clearEntities() {
this.__meshComponents = void 0;
this.__topLevelSceneGraphComponents = void 0;
this.__entities = [];
}
private __collectTopLevelSceneGraphComponents() {
if (this.__topLevelSceneGraphComponents == null) {
const goToTopLevel = (sg: SceneGraphComponent) => {
if (sg.parent) {
goToTopLevel(sg.parent);
}
return sg;
};
this.__topLevelSceneGraphComponents = this.__sceneGraphDirectlyAdded.map(
(sg: SceneGraphComponent) => {
return goToTopLevel(sg);
}
);
const set = new Set(this.__topLevelSceneGraphComponents);
this.__topLevelSceneGraphComponents = Array.from(set);
}
}
private __collectMeshComponents() {
if (this.__meshComponents == null) {
this.__meshComponents = [];
this.__entities.filter(entity => {
const meshComponent = entity.getComponentByComponentTID(
WellKnownComponentTIDs.MeshComponentTID
) as MeshComponent | undefined;
if (meshComponent) {
this.__meshComponents!.push(meshComponent);
}
});
}
}
/**
* Get all the MeshComponents list of the entities on this render pass.
* @return An array of MeshComponents
*/
get meshComponents() {
this.__collectMeshComponents();
return this.__meshComponents;
}
/**
* Get all the highest level SceneGraphComponents list of the entities on this render pass.
* @return An array of SceneGraphComponents
*/
get sceneTopLevelGraphComponents(): SceneGraphComponent[] {
this.__collectTopLevelSceneGraphComponents();
return this.__topLevelSceneGraphComponents != null
? this.__topLevelSceneGraphComponents
: [];
}
/**
* Sets the target framebuffer of this render pass.
* If two or more render pass share a framebuffer, Rhodonite renders entities to the same framebuffer in those render passes.
* @param framebuffer A framebuffer
*/
setFramebuffer(framebuffer: FrameBuffer) {
this.__frameBuffer = framebuffer;
if (framebuffer != null) {
this.setViewport(
Vector4.fromCopyArray([0, 0, framebuffer.width, framebuffer.height])
);
} else {
this.__viewport = undefined;
}
}
setRenderTargetColorAttachments(indeces?: RenderBufferTargetEnum[]) {
this.__renderTargetColorAttachments = indeces;
}
getRenderTargetColorAttachments(): RenderBufferTargetEnum[] | undefined {
return this.__renderTargetColorAttachments;
}
/**
* Gets the framebuffer if this render pass has the target framebuffer.
* @return A framebuffer
*/
getFramebuffer(): FrameBuffer | undefined {
return this.__frameBuffer;
}
/**
* Remove the existing framebuffer
*/
removeFramebuffer() {
this.__frameBuffer = undefined;
}
/**
* Sets the viewport of this render pass.
* @param vec A Vector4 (Origin of coordinatesX, origin of coordinatesY, width, height).
*/
setViewport(vec: IVector4) {
if (this.__viewport != null) {
this.__viewport.copyComponents(vec);
} else {
this.__viewport = MutableVector4.fromCopyArray([
vec.x,
vec.y,
vec.z,
vec.w,
]);
}
}
/**
* Gets the viewport if this render pass has the viewport.
* @return A Vector4 (Origin of coordinatesX, origin of coordinatesY, width, height).
*/
getViewport() {
let viewport = this.__viewport;
if (viewport != null) {
viewport = RenderPass.__tmp_Vector4_0.copyComponents(viewport);
}
return viewport;
}
setResolveFramebuffer(framebuffer: FrameBuffer) {
const repo = WebGLResourceRepository.getInstance();
const glw = repo.currentWebGLContextWrapper!;
if (!glw || !glw.isWebGL2) {
console.error('resolve framebuffer is unavailable in this webgl context');
return;
}
this.__resolveFrameBuffer = framebuffer;
}
getResolveFramebuffer(): FrameBuffer | undefined {
return this.__resolveFrameBuffer;
}
setResolveFramebuffer2(framebuffer: FrameBuffer) {
const repo = WebGLResourceRepository.getInstance();
const glw = repo.currentWebGLContextWrapper!;
if (!glw || !glw.isWebGL2) {
console.error('resolve framebuffer is unavailable in this webgl context');
return;
}
this.__resolveFrameBuffer2 = framebuffer;
}
getResolveFramebuffer2(): FrameBuffer | undefined {
return this.__resolveFrameBuffer2;
}
copyFramebufferToResolveFramebuffer() {
const repo = WebGLResourceRepository.getInstance();
const webGLResourceFrameBuffer = repo.getWebGLResource(
this.__frameBuffer!.cgApiResourceUid
) as WebGLFramebuffer;
const webGLResourceResolveFramebuffer = repo.getWebGLResource(
this.__resolveFrameBuffer!.cgApiResourceUid
) as WebGLFramebuffer;
const glw = repo.currentWebGLContextWrapper!;
const gl = glw.getRawContextAsWebGL2();
gl.bindFramebuffer(gl.READ_FRAMEBUFFER, webGLResourceFrameBuffer);
gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, webGLResourceResolveFramebuffer);
gl.blitFramebuffer(
0,
0,
this.__frameBuffer!.width,
this.__frameBuffer!.height,
0,
0,
this.__resolveFrameBuffer!.width,
this.__resolveFrameBuffer!.height,
gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT,
gl.NEAREST
);
gl.bindFramebuffer(gl.READ_FRAMEBUFFER, null);
gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, null);
}
copyFramebufferToResolveFramebuffer2() {
const repo = WebGLResourceRepository.getInstance();
const webGLResourceFrameBuffer = repo.getWebGLResource(
this.__frameBuffer!.cgApiResourceUid
) as WebGLFramebuffer;
const webGLResourceResolveFramebuffer2 = repo.getWebGLResource(
this.__resolveFrameBuffer2!.cgApiResourceUid
) as WebGLFramebuffer;
const glw = repo.currentWebGLContextWrapper!;
const gl = glw.getRawContextAsWebGL2();
gl.bindFramebuffer(gl.READ_FRAMEBUFFER, webGLResourceFrameBuffer);
gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, webGLResourceResolveFramebuffer2);
gl.blitFramebuffer(
0,
0,
this.__frameBuffer!.width,
this.__frameBuffer!.height,
0,
0,
this.__resolveFrameBuffer2!.width,
this.__resolveFrameBuffer2!.height,
gl.COLOR_BUFFER_BIT,
gl.NEAREST
);
gl.bindFramebuffer(gl.READ_FRAMEBUFFER, null);
gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, null);
const renderTargetTexture =
this.getResolveFramebuffer2()!.getColorAttachedRenderTargetTexture(0)!;
renderTargetTexture.generateMipmap();
}
private __setupMaterial(material: Material) {
if (material.isEmptyMaterial()) return;
const webglRenderingStrategy = this.__setWebglRenderingStrategyIfNotYet(
this.__webglRenderingStrategy
);
webglRenderingStrategy.setupShaderForMaterial(material);
}
/**
* Sets a material for the primitive on this render pass.
* If Rhodonite draw the primitive using this render pass, Rhodonite uses this material instead of the material on the primitive.
* @param material A material attaching to the primitive
* @param primitive A target primitive
*/
setMaterialForPrimitive(material: Material, primitive: Primitive) {
this.__primitiveMaterial.set(primitive, material);
this.__setupMaterial(material);
}
/**
* Sets a material for all the primitive on this render pass.
* For all the primitive, Rhodonite uses this material instead of the material on the primitive.
* Where if this render pass has a map between primitive and material by setMaterialForPrimitive, Rhodonite uses the material mapped by primitive.
* @param material A material attaching to the primitive
*/
setMaterial(material: Material) {
this.__material = material;
this.__setupMaterial(material);
}
get material() {
return this.__material;
}
private __setWebglRenderingStrategyIfNotYet(
webglRenderingStrategy?: WebGLStrategy
): WebGLStrategy {
if (webglRenderingStrategy != null) {
return webglRenderingStrategy;
}
const processApproach = System.processApproach;
const moduleManager = ModuleManager.getInstance();
const moduleName = 'webgl';
const webglModule = moduleManager.getModule(moduleName)! as any;
const newWebglRenderingStrategyRef =
webglModule.getRenderingStrategy(processApproach);
this.__webglRenderingStrategy = newWebglRenderingStrategyRef;
return newWebglRenderingStrategyRef;
}
private __getMaterialOf(primitive: Primitive) {
return this.__primitiveMaterial.get(primitive);
}
private __hasMaterialOf(primitive: Primitive) {
return this.__primitiveMaterial.has(primitive);
}
getAppropriateMaterial(primitive: Primitive): Material {
let material;
if (this.__hasMaterialOf(primitive)) {
material = this.__getMaterialOf(primitive) as Material;
} else if (this.material != null) {
material = this.material;
} else {
material = primitive.material;
}
return material;
}
}