/
ExcaliburGraphicsContextWebGL.ts
726 lines (618 loc) · 23 KB
/
ExcaliburGraphicsContextWebGL.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
import {
ExcaliburGraphicsContext,
LineGraphicsOptions,
RectGraphicsOptions,
PointGraphicsOptions,
ExcaliburGraphicsContextOptions,
DebugDraw,
HTMLImageSource
} from './ExcaliburGraphicsContext';
import { Matrix } from '../../Math/matrix';
import { TransformStack } from './transform-stack';
import { Vector, vec } from '../../Math/vector';
import { Color } from '../../Color';
import { StateStack } from './state-stack';
import { Logger } from '../../Util/Log';
import { DebugText } from './debug-text';
import { ScreenDimension } from '../../Screen';
import { RenderTarget } from './render-target';
import { PostProcessor } from '../PostProcessor/PostProcessor';
import { TextureLoader } from './texture-loader';
import { RendererPlugin } from './renderer';
// renderers
import { LineRenderer } from './line-renderer/line-renderer';
import { PointRenderer } from './point-renderer/point-renderer';
import { ScreenPassPainter } from './screen-pass-painter/screen-pass-painter';
import { ImageRenderer } from './image-renderer/image-renderer';
import { RectangleRenderer } from './rectangle-renderer/rectangle-renderer';
import { CircleRenderer } from './circle-renderer/circle-renderer';
import { Pool } from '../../Util/Pool';
import { DrawCall } from './draw-call';
import { AffineMatrix } from '../../Math/affine-matrix';
import { Material, MaterialOptions } from './material';
import { MaterialRenderer } from './material-renderer/material-renderer';
import { Shader, ShaderOptions } from './shader';
export const pixelSnapEpsilon = 0.0001;
class ExcaliburGraphicsContextWebGLDebug implements DebugDraw {
private _debugText = new DebugText();
constructor(private _webglCtx: ExcaliburGraphicsContextWebGL) {}
/**
* Draw a debugging rectangle to the context
* @param x
* @param y
* @param width
* @param height
*/
drawRect(x: number, y: number, width: number, height: number, rectOptions: RectGraphicsOptions = { color: Color.Black }): void {
this.drawLine(vec(x, y), vec(x + width, y), { ...rectOptions });
this.drawLine(vec(x + width, y), vec(x + width, y + height), { ...rectOptions });
this.drawLine(vec(x + width, y + height), vec(x, y + height), { ...rectOptions });
this.drawLine(vec(x, y + height), vec(x, y), { ...rectOptions });
}
/**
* Draw a debugging line to the context
* @param start
* @param end
* @param lineOptions
*/
drawLine(start: Vector, end: Vector, lineOptions: LineGraphicsOptions = { color: Color.Black }): void {
this._webglCtx.draw<LineRenderer>('ex.line', start, end, lineOptions.color);
}
/**
* Draw a debugging point to the context
* @param point
* @param pointOptions
*/
drawPoint(point: Vector, pointOptions: PointGraphicsOptions = { color: Color.Black, size: 5 }): void {
this._webglCtx.draw<PointRenderer>('ex.point', point, pointOptions.color, pointOptions.size);
}
drawText(text: string, pos: Vector) {
this._debugText.write(this._webglCtx, text, pos);
}
}
export interface WebGLGraphicsContextInfo {
transform: TransformStack;
state: StateStack;
ortho: Matrix;
context: ExcaliburGraphicsContextWebGL;
}
export interface ExcaliburGraphicsContextWebGLOptions extends ExcaliburGraphicsContextOptions {
context?: WebGL2RenderingContext,
handleContextLost?: (e: Event) => void,
handleContextRestored?: (e: Event) => void
}
export class ExcaliburGraphicsContextWebGL implements ExcaliburGraphicsContext {
private _logger = Logger.getInstance();
private _renderers: Map<string, RendererPlugin> = new Map<string, RendererPlugin>();
private _isDrawLifecycle = false;
public useDrawSorting = true;
private _drawCallPool = new Pool<DrawCall>(
() => new DrawCall(),
(instance) => {
instance.priority = 0;
instance.z = 0;
instance.renderer = undefined;
instance.args = undefined;
return instance;
}, 4000);
private _drawCalls: DrawCall[] = [];
// Main render target
private _renderTarget: RenderTarget;
// Quad boundary MSAA
private _msaaTarget: RenderTarget;
// Postprocessing is a tuple with 2 render targets, these are flip-flopped during the postprocessing process
private _postProcessTargets: RenderTarget[] = [];
private _screenRenderer: ScreenPassPainter;
private _postprocessors: PostProcessor[] = [];
/**
* Meant for internal use only. Access the internal context at your own risk and no guarantees this will exist in the future.
* @internal
*/
public __gl: WebGL2RenderingContext;
private _transform = new TransformStack();
private _state = new StateStack();
private _ortho!: Matrix;
/**
* Snaps the drawing x/y coordinate to the nearest whole pixel
*/
public snapToPixel: boolean = false;
/**
* Native context smoothing
*/
public readonly smoothing: boolean = false;
/**
* Whether the pixel art sampler is enabled for smooth sub pixel anti-aliasing
*/
public readonly pixelArtSampler: boolean = false;
/**
* UV padding in pixels to use in internal image rendering to prevent texture bleed
*
*/
public uvPadding = .01;
public backgroundColor: Color = Color.ExcaliburBlue;
public textureLoader: TextureLoader;
public materialScreenTexture: WebGLTexture;
public get z(): number {
return this._state.current.z;
}
public set z(value: number) {
this._state.current.z = value;
}
public get opacity(): number {
return this._state.current.opacity;
}
public set opacity(value: number) {
this._state.current.opacity = value;
}
public get tint(): Color {
return this._state.current.tint;
}
public set tint(color: Color) {
this._state.current.tint = color;
}
public get width() {
return this.__gl.canvas.width;
}
public get height() {
return this.__gl.canvas.height;
}
public get ortho(): Matrix {
return this._ortho;
}
/**
* Checks the underlying webgl implementation if the requested internal resolution is supported
* @param dim
*/
public checkIfResolutionSupported(dim: ScreenDimension): boolean {
// Slight hack based on this thread https://groups.google.com/g/webgl-dev-list/c/AHONvz3oQTo
let supported = true;
if (dim.width > 4096 || dim.height > 4096) {
supported = false;
}
return supported;
}
public readonly multiSampleAntialiasing: boolean = true;
public readonly samples?: number;
public readonly transparency: boolean = true;
private _isContextLost = false;
constructor(options: ExcaliburGraphicsContextWebGLOptions) {
const {
canvasElement,
context,
enableTransparency,
antialiasing,
uvPadding,
multiSampleAntialiasing,
pixelArtSampler,
powerPreference,
snapToPixel,
backgroundColor,
useDrawSorting,
handleContextLost,
handleContextRestored
} = options;
this.__gl = context ?? canvasElement.getContext('webgl2', {
antialias: antialiasing ?? this.smoothing,
premultipliedAlpha: false,
alpha: enableTransparency ?? this.transparency,
depth: false,
powerPreference: powerPreference ?? 'high-performance'
});
if (!this.__gl) {
throw Error('Failed to retrieve webgl context from browser');
}
if (handleContextLost) {
this.__gl.canvas.addEventListener('webglcontextlost', handleContextLost, false);
}
if (handleContextRestored) {
this.__gl.canvas.addEventListener('webglcontextrestored', handleContextRestored, false);
}
this.__gl.canvas.addEventListener('webglcontextlost', () => {
this._isContextLost = true;
});
this.__gl.canvas.addEventListener('webglcontextrestored', () => {
this._isContextLost = false;
});
this.textureLoader = new TextureLoader(this.__gl);
this.snapToPixel = snapToPixel ?? this.snapToPixel;
this.smoothing = antialiasing ?? this.smoothing;
this.transparency = enableTransparency ?? this.transparency;
this.pixelArtSampler = pixelArtSampler ?? this.pixelArtSampler;
this.uvPadding = uvPadding ?? this.uvPadding;
this.multiSampleAntialiasing = typeof multiSampleAntialiasing === 'boolean' ? multiSampleAntialiasing : this.multiSampleAntialiasing;
this.samples = typeof multiSampleAntialiasing === 'object' ? multiSampleAntialiasing.samples : undefined;
this.backgroundColor = backgroundColor ?? this.backgroundColor;
this.useDrawSorting = useDrawSorting ?? this.useDrawSorting;
this._drawCallPool.disableWarnings = true;
this._drawCallPool.preallocate();
this._init();
}
private _disposed = false;
public dispose() {
if (!this._disposed) {
this._disposed = true;
this.textureLoader.dispose();
for (const renderer of this._renderers.values()) {
renderer.dispose();
}
this._renderers.clear();
this._drawCallPool.dispose();
this._drawCalls.length = 0;
this.__gl = null;
}
}
private _init() {
const gl = this.__gl;
// Setup viewport and view matrix
this._ortho = Matrix.ortho(0, gl.canvas.width, gl.canvas.height, 0, 400, -400);
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
// Clear background
gl.clearColor(this.backgroundColor.r / 255, this.backgroundColor.g / 255, this.backgroundColor.b / 255, this.backgroundColor.a);
gl.clear(gl.COLOR_BUFFER_BIT);
// Enable alpha blending
// https://www.realtimerendering.com/blog/gpus-prefer-premultiplication/
gl.enable(gl.BLEND);
gl.blendEquation(gl.FUNC_ADD);
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
gl.blendEquationSeparate(gl.FUNC_ADD, gl.FUNC_ADD);
gl.blendFuncSeparate(gl.ONE, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
// Setup builtin renderers
this.register(new ImageRenderer({
uvPadding: this.uvPadding,
pixelArtSampler: this.pixelArtSampler
}));
this.register(new MaterialRenderer());
this.register(new RectangleRenderer());
this.register(new CircleRenderer());
this.register(new PointRenderer());
this.register(new LineRenderer());
this.materialScreenTexture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, this.materialScreenTexture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, this.width, this.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);
gl.bindTexture(gl.TEXTURE_2D, null);
this._screenRenderer = new ScreenPassPainter(gl);
this._renderTarget = new RenderTarget({
gl,
transparency: this.transparency,
width: gl.canvas.width,
height: gl.canvas.height
});
this._postProcessTargets = [
new RenderTarget({
gl,
transparency: this.transparency,
width: gl.canvas.width,
height: gl.canvas.height
}),
new RenderTarget({
gl,
transparency: this.transparency,
width: gl.canvas.width,
height: gl.canvas.height
})
];
this._msaaTarget = new RenderTarget({
gl,
transparency: this.transparency,
width: gl.canvas.width,
height: gl.canvas.height,
antialias: this.multiSampleAntialiasing,
samples: this.samples
});
}
public register<T extends RendererPlugin>(renderer: T) {
this._renderers.set(renderer.type, renderer);
renderer.initialize(this.__gl, this);
}
public get(rendererName: string): RendererPlugin {
return this._renderers.get(rendererName);
}
private _currentRenderer: RendererPlugin;
private _isCurrentRenderer(renderer: RendererPlugin): boolean {
if (!this._currentRenderer || this._currentRenderer === renderer) {
return true;
}
return false;
}
public beginDrawLifecycle() {
this._isDrawLifecycle = true;
}
public endDrawLifecycle() {
this._isDrawLifecycle = false;
}
public draw<TRenderer extends RendererPlugin>(rendererName: TRenderer['type'], ...args: Parameters<TRenderer['draw']>) {
if (!this._isDrawLifecycle) {
this._logger.warnOnce(
`Attempting to draw outside the the drawing lifecycle (preDraw/postDraw) is not supported and is a source of bugs/errors.\n` +
`If you want to do custom drawing, use Actor.graphics, or any onPreDraw or onPostDraw handler.`);
}
if (this._isContextLost) {
this._logger.errorOnce(`Unable to draw ${rendererName}, the webgl context is lost`);
return;
}
const renderer = this._renderers.get(rendererName);
if (renderer) {
if (this.useDrawSorting) {
const drawCall = this._drawCallPool.get();
drawCall.z = this._state.current.z;
drawCall.priority = renderer.priority;
drawCall.renderer = rendererName;
this.getTransform().clone(drawCall.transform);
drawCall.state.z = this._state.current.z;
drawCall.state.opacity = this._state.current.opacity;
drawCall.state.tint = this._state.current.tint;
drawCall.state.material = this._state.current.material;
drawCall.args = args;
this._drawCalls.push(drawCall);
} else {
// Set the current renderer if not defined
if (!this._currentRenderer) {
this._currentRenderer = renderer;
}
if (!this._isCurrentRenderer(renderer)) {
// switching graphics means we must flush the previous
this._currentRenderer.flush();
}
// If we are still using the same renderer we can add to the current batch
renderer.draw(...args);
this._currentRenderer = renderer;
}
} else {
throw Error(`No renderer with name ${rendererName} has been registered`);
}
}
public resetTransform(): void {
this._transform.current = AffineMatrix.identity();
}
public updateViewport(resolution: ScreenDimension): void {
const gl = this.__gl;
this._ortho = this._ortho = Matrix.ortho(0, resolution.width, resolution.height, 0, 400, -400);
this._renderTarget.setResolution(gl.canvas.width, gl.canvas.height);
this._msaaTarget.setResolution(gl.canvas.width, gl.canvas.height);
this._postProcessTargets[0].setResolution(gl.canvas.width, gl.canvas.height);
this._postProcessTargets[1].setResolution(gl.canvas.width, gl.canvas.height);
}
drawImage(image: HTMLImageSource, x: number, y: number): void;
drawImage(image: HTMLImageSource, x: number, y: number, width: number, height: number): void;
drawImage(
image: HTMLImageSource,
sx: number,
sy: number,
swidth?: number,
sheight?: number,
dx?: number,
dy?: number,
dwidth?: number,
dheight?: number
): void;
drawImage(
image: HTMLImageSource,
sx: number,
sy: number,
swidth?: number,
sheight?: number,
dx?: number,
dy?: number,
dwidth?: number,
dheight?: number
): void {
if (swidth === 0 || sheight === 0) {
return; // zero dimension dest exit early
} else if (dwidth === 0 || dheight === 0) {
return; // zero dimension dest exit early
} else if (image.width === 0 || image.height === 0) {
return; // zero dimension source exit early
}
if (!image) {
Logger.getInstance().warn('Cannot draw a null or undefined image');
// tslint:disable-next-line: no-console
if (console.trace) {
// tslint:disable-next-line: no-console
console.trace();
}
return;
}
if (this._state.current.material) {
this.draw<MaterialRenderer>('ex.material', image, sx, sy, swidth, sheight, dx, dy, dwidth, dheight);
} else {
this.draw<ImageRenderer>('ex.image', image, sx, sy, swidth, sheight, dx, dy, dwidth, dheight);
}
}
public drawLine(start: Vector, end: Vector, color: Color, thickness = 1) {
this.draw<RectangleRenderer>('ex.rectangle', start, end, color, thickness);
}
public drawRectangle(pos: Vector, width: number, height: number, color: Color, stroke?: Color, strokeThickness?: number) {
this.draw<RectangleRenderer>('ex.rectangle', pos, width, height, color, stroke, strokeThickness);
}
public drawCircle(pos: Vector, radius: number, color: Color, stroke?: Color, thickness?: number) {
this.draw<CircleRenderer>('ex.circle', pos, radius, color, stroke, thickness);
}
debug = new ExcaliburGraphicsContextWebGLDebug(this);
public save(): void {
this._transform.save();
this._state.save();
}
public restore(): void {
this._transform.restore();
this._state.restore();
}
public translate(x: number, y: number): void {
this._transform.translate(this.snapToPixel ? ~~(x + pixelSnapEpsilon) : x, this.snapToPixel ? ~~(y + pixelSnapEpsilon) : y);
}
public rotate(angle: number): void {
this._transform.rotate(angle);
}
public scale(x: number, y: number): void {
this._transform.scale(x, y);
}
public transform(matrix: AffineMatrix) {
this._transform.current = matrix;
}
public getTransform(): AffineMatrix {
return this._transform.current;
}
public multiply(m: AffineMatrix) {
this._transform.current.multiply(m, this._transform.current);
}
public addPostProcessor(postprocessor: PostProcessor) {
this._postprocessors.push(postprocessor);
postprocessor.initialize(this.__gl);
}
public removePostProcessor(postprocessor: PostProcessor) {
const index = this._postprocessors.indexOf(postprocessor);
if (index !== -1) {
this._postprocessors.splice(index, 1);
}
}
public clearPostProcessors() {
this._postprocessors.length = 0;
}
private _totalPostProcessorTime = 0;
public updatePostProcessors(delta: number) {
for (const postprocessor of this._postprocessors) {
const shader = postprocessor.getShader();
shader.use();
const uniforms = shader.getUniforms();
this._totalPostProcessorTime += delta;
if (uniforms.find(u => u.name ==='u_time_ms')) {
shader.setUniformFloat('u_time_ms', this._totalPostProcessorTime);
}
if (uniforms.find(u => u.name ==='u_elapsed_ms')) {
shader.setUniformFloat('u_elapsed_ms', delta);
}
if (uniforms.find(u => u.name ==='u_resolution')) {
shader.setUniformFloatVector('u_resolution', vec(this.width, this.height));
}
if (postprocessor.onUpdate) {
postprocessor.onUpdate(delta);
}
}
}
public set material(material: Material) {
this._state.current.material = material;
}
public get material(): Material | null {
return this._state.current.material;
}
/**
* Creates and initializes the material which compiles the internal shader
* @param options
* @returns Material
*/
public createMaterial(options: Omit<MaterialOptions, 'graphicsContext'>): Material {
const material = new Material({...options, graphicsContext: this});
return material;
}
public createShader(options: Omit<ShaderOptions, 'gl'>): Shader {
const gl = this.__gl;
const { vertexSource, fragmentSource } = options;
const shader = new Shader({
gl,
vertexSource,
fragmentSource
});
shader.compile();
return shader;
}
clear() {
const gl = this.__gl;
const currentTarget = this.multiSampleAntialiasing ? this._msaaTarget : this._renderTarget;
currentTarget.use();
gl.clearColor(this.backgroundColor.r / 255, this.backgroundColor.g / 255, this.backgroundColor.b / 255, this.backgroundColor.a);
// Clear the context with the newly set color. This is
// the function call that actually does the drawing.
gl.clear(gl.COLOR_BUFFER_BIT);
}
/**
* Flushes all batched rendering to the screen
*/
flush() {
if (this._isContextLost) {
this._logger.errorOnce(`Unable to flush the webgl context is lost`);
return;
}
// render target captures all draws and redirects to the render target
let currentTarget = this.multiSampleAntialiasing ? this._msaaTarget : this._renderTarget;
currentTarget.use();
if (this.useDrawSorting) {
// sort draw calls
// Find the original order of the first instance of the draw call
const originalSort = new Map<string, number>();
for (const [name] of this._renderers) {
const firstIndex = this._drawCalls.findIndex(dc => dc.renderer === name);
originalSort.set(name, firstIndex);
}
this._drawCalls.sort((a, b) => {
const zIndex = a.z - b.z;
const originalSortOrder = originalSort.get(a.renderer) - originalSort.get(b.renderer);
const priority = a.priority - b.priority;
if (zIndex === 0) { // sort by z first
if (priority === 0) { // sort by priority
return originalSortOrder; // use the original order to inform draw call packing to maximally preserve painter order
}
return priority;
}
return zIndex;
});
const oldTransform = this._transform.current;
const oldState = this._state.current;
if (this._drawCalls.length) {
let currentRendererName = this._drawCalls[0].renderer;
let currentRenderer = this._renderers.get(currentRendererName);
for (let i = 0; i < this._drawCalls.length; i++) {
// hydrate the state for renderers
this._transform.current = this._drawCalls[i].transform;
this._state.current = this._drawCalls[i].state;
if (this._drawCalls[i].renderer !== currentRendererName) {
// switching graphics renderer means we must flush the previous
currentRenderer.flush();
currentRendererName = this._drawCalls[i].renderer;
currentRenderer = this._renderers.get(currentRendererName);
}
// ! hack to grab screen texture before materials run because they might want it
if (currentRenderer instanceof MaterialRenderer && this.material.isUsingScreenTexture) {
currentTarget.copyToTexture(this.materialScreenTexture);
currentTarget.use();
}
// If we are still using the same renderer we can add to the current batch
currentRenderer.draw(...this._drawCalls[i].args);
}
if (currentRenderer.hasPendingDraws()) {
currentRenderer.flush();
}
}
// reset state
this._transform.current = oldTransform;
this._state.current = oldState;
// reclaim draw calls
this._drawCallPool.done();
this._drawCalls.length = 0;
} else {
// This is the final flush at the moment to draw any leftover pending draw
for (const renderer of this._renderers.values()) {
if (renderer.hasPendingDraws()) {
renderer.flush();
}
}
}
currentTarget.disable();
// post process step
if (this._postprocessors.length > 0) {
const source = currentTarget.toRenderSource();
source.use();
}
// flip flop render targets for post processing
for (let i = 0; i < this._postprocessors.length; i++) {
currentTarget = this._postProcessTargets[i % 2];
this._postProcessTargets[i % 2].use();
this._screenRenderer.renderWithPostProcessor(this._postprocessors[i]);
this._postProcessTargets[i % 2].toRenderSource().use();
}
// Final blit to the screen
currentTarget.blitToScreen();
}
}