/
PostBase.ts
102 lines (93 loc) · 3.67 KB
/
PostBase.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
import { ViewQuad } from '../../../core/ViewQuad';
import { VirtualTexture } from '../../../textures/VirtualTexture';
import { Texture } from '../../graphics/webGpu/core/texture/Texture';
import { UniformNode } from '../../graphics/webGpu/core/uniforms/UniformNode';
import { GPUContext } from '../GPUContext';
import { RTResourceMap } from '../frame/RTResourceMap';
import { ComputeShader } from '../../../gfx/graphics/webGpu/shader/ComputeShader';
import { RTResourceConfig } from '../config/RTResourceConfig';
import { PostRenderer } from '../passRenderer/post/PostRenderer';
import { View3D } from '../../../core/View3D';
import { Reference } from '../../../util/Reference';
import { CResizeEvent } from '../../../event/CResizeEvent';
import { webGPUContext } from '../../graphics/webGpu/Context3D';
/**
* @internal
* Base class for post-processing effects
* @group Post Effects
*/
export class PostBase {
public enable: boolean = true;
public postRenderer: PostRenderer;
protected rtViewQuad: Map<string, ViewQuad>;
protected virtualTexture: Map<string, VirtualTexture>;
constructor() {
this.rtViewQuad = new Map<string, ViewQuad>();
this.virtualTexture = new Map<string, VirtualTexture>();
webGPUContext.addEventListener(CResizeEvent.RESIZE, this.onResize, this);
}
protected createRTTexture(name: string, rtWidth: number, rtHeight: number, format: GPUTextureFormat, useMipmap: boolean = false, sampleCount: number = 0) {
let rt = RTResourceMap.createRTTexture(name, rtWidth, rtHeight, format, useMipmap, sampleCount);
rt.name = name;
this.virtualTexture.set(name, rt);
Reference.getInstance().attached(rt, this);
return rt;
}
protected createViewQuad(name: string, shaderName: string, outRtTexture: VirtualTexture, msaa: number = 0) {
let viewQuad = RTResourceMap.createViewQuad(name, 'Quad_vert_wgsl', shaderName, outRtTexture, msaa);
this.rtViewQuad.set(name, viewQuad);
return viewQuad;
}
protected getOutTexture(): Texture {
let colorTexture: Texture;
let renderTargets = GPUContext.lastRenderPassState.renderTargets;
if (renderTargets.length > 0) {
colorTexture = renderTargets[0];
} else {
colorTexture = RTResourceMap.getTexture(RTResourceConfig.colorBufferTex_NAME);
}
return colorTexture;
}
protected autoSetColorTexture(name: string, compute: ComputeShader): void {
let input = this.getOutTexture() as VirtualTexture;
compute.setSamplerTexture(name, input);
}
/**
* @internal
*/
public compute(view: View3D) { }
/**
* @internal
*/
public onAttach(view: View3D) { }
/**
* @internal
*/
public onDetach(view: View3D) { }
public onResize() {
}
/**
* @internal
*/
public render(view: View3D, command: GPUCommandEncoder) {
this.compute(view);
this.rtViewQuad.forEach((viewQuad, k) => {
let lastTexture = GPUContext.lastRenderPassState.getLastRenderTexture();
viewQuad.renderToViewQuad(view, viewQuad, command, lastTexture);
});
}
public destroy(force?: boolean) {
this.postRenderer = null;
for (let i = 0; i < this.rtViewQuad.size; i++) {
const quad = this.rtViewQuad.values[i] as ViewQuad;
quad.destroy(force);
}
this.rtViewQuad.clear();
this.rtViewQuad = null;
for (let i = 0; i < this.virtualTexture.size; i++) {
const tex = this.virtualTexture.values[i] as VirtualTexture;
Reference.getInstance().detached(tex, this);
tex.destroy(force);
}
}
}