/
accumulatepass.ts
331 lines (266 loc) · 12.5 KB
/
accumulatepass.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
/* spellchecker: disable */
import { assert, log, logIf, LogLevel } from './auxiliaries';
import { GLsizei2 } from './tuples';
import { ChangeLookup } from './changelookup';
import { Context } from './context';
import { Framebuffer } from './framebuffer';
import { Initializable } from './initializable';
import { NdcFillingTriangle } from './ndcfillingtriangle';
import { Program } from './program';
import { Shader } from './shader';
import { Texture2D } from './texture2d';
import { Wizard } from './wizard';
/* spellchecker: enable */
/**
* This pass accumulates the color attachment 0 of a framebuffer, e.g., the result of an intermediate frame, into an
* accumulation buffer. For accumulation the frame number is used to derive the accumulation weight. For rendering to
* texture, a textured ndc-filling triangle is used.
*
* The accumulation pass can be used as follows:
* ```
* this.accumulate.initialize();
* this.accumulate.texture = this.intermediateFBO.texture(gl2facade.COLOR_ATTACHMENT0);
* this.accumulate.update();
* this.accumulate.frame(frameNumber);
* ```
*/
export class AccumulatePass extends Initializable {
/**
* Read-only access to the objects context, used to get context information and WebGL API access.
*/
protected _context: Context;
/**
* Alterable auxiliary object for tracking changes on this object's input and lazy updates.
*/
protected readonly _altered = Object.assign(new ChangeLookup(), {
any: false, texture: false, precision: false, passThrough: false,
});
/** @see {@link texture} */
protected _texture: Texture2D;
/** @see {@link precision} */
protected _precision: Wizard.Precision = Wizard.Precision.half;
/** @see {@link passThrough} */
protected _passThrough: boolean;
/**
* Two rgba-framebuffers used for accumulation (buffer ping-ponging is used for alternating the buffers for read
* and write access due to a limitation in WebGL).
*/
protected _accumulationFBOs: [Framebuffer, Framebuffer];
protected _accumulationTextures: [Texture2D, Texture2D];
/**
* Stores the index of the last buffer written to.
*/
protected _write: GLuint = 0;
/**
* Geometry used to draw on. This is not provided by default to allow for geometry sharing. If no triangle is given,
* the ndc triangle will be created and managed internally.
*/
protected _ndcTriangle: NdcFillingTriangle;
/**
* Tracks ownership of the ndc-filling triangle.
*/
protected _ndcTriangleShared = false;
protected _program: Program;
protected _uWeight: WebGLUniformLocation;
constructor(context: Context) {
super();
this._context = context;
}
/**
* Specializes this pass's initialization. This pass requires an ndc-filling triangle, a single accumulation
* program, and two accumulation framebuffers for ping pong (simultaneous read and write is currently not allowed
* by webgl). All attribute and dynamic uniform locations are cached.
* @param ndcTriangle - If specified, assumed to be used as shared geometry. If none is specified, a ndc-filling
* triangle will be created internally.
*/
@Initializable.initialize()
initialize(ndcTriangle?: NdcFillingTriangle): boolean {
const gl = this._context.gl;
this._accumulationFBOs = [
new Framebuffer(this._context, 'AccumPingFBO'),
new Framebuffer(this._context, 'AccumPongFBO')];
this._accumulationTextures = [
new Texture2D(this._context, 'AccumPingTexture'),
new Texture2D(this._context, 'AccumPongTexture')];
if (ndcTriangle === undefined) {
this._ndcTriangle = new NdcFillingTriangle(this._context, 'NdcFillingTriangle-Accumulate');
} else {
this._ndcTriangle = ndcTriangle;
this._ndcTriangleShared = true;
}
/* Configure program-based accumulate. */
const vert = new Shader(this._context, gl.VERTEX_SHADER, 'ndcvertices.vert (accumulate)');
vert.initialize(require('./shaders/ndcvertices.vert'));
const frag = new Shader(this._context, gl.FRAGMENT_SHADER, 'accumulate.frag');
frag.initialize(require('./shaders/accumulate.frag'));
this._program = new Program(this._context, 'AccumulateProgram');
this._program.initialize([vert, frag], false);
if (!this._ndcTriangle.initialized) {
this._ndcTriangle.initialize();
}
this._program.attribute('a_vertex', this._ndcTriangle.vertexLocation);
this._program.link();
this._uWeight = this._program.uniform('u_weight');
this._program.bind();
gl.uniform1f(this._uWeight, 0.0);
gl.uniform1i(this._program.uniform('u_accumulationTexture'), 0);
gl.uniform1i(this._program.uniform('u_currentFrameTexture'), 1);
this._program.unbind();
return true;
}
/**
* Specializes this pass's uninitialization. Program and geometry resources are released (if allocated). Cached
* uniform and attribute locations are invalidated.
*/
@Initializable.uninitialize()
uninitialize(): void {
if (!this._ndcTriangleShared && this._ndcTriangle.initialized) {
this._ndcTriangle.uninitialize();
}
this._program.uninitialize();
this._accumulationFBOs[0].uninitialize();
this._accumulationFBOs[1].uninitialize();
this._accumulationTextures[0].uninitialize();
this._accumulationTextures[1].uninitialize();
this._write = 0;
}
/**
* Initialize accumulation textures and FBOs (if not initialized yet). Then verifies if the texture's size has
* changed, and if so, resizes the accumulation buffers.
*/
@Initializable.assert_initialized()
update(): void {
if (!this._texture || !this._texture.valid) {
log(LogLevel.Warning, `valid texture for accumulation update expected, given ${this._texture}`);
return;
}
if (this._passThrough) {
return;
}
const sizeAltered = this._altered.texture || this._accumulationTextures[0].width !== this._texture.width ||
this._accumulationTextures[0].height !== this._texture.height;
if (!this._altered.any && !sizeAltered) {
assert(this._accumulationFBOs[0].valid && this._accumulationFBOs[1].valid,
`valid accumulation framebuffers expected`);
return;
}
const gl = this._context.gl;
const gl2facade = this._context.gl2facade;
/* Create and initialize accumulation texture and FBOs. */
const textureSize = this._texture.size;
if (!this._accumulationTextures[0].initialized) {
const internalFormat = Wizard.queryInternalTextureFormat(this._context, gl.RGBA, this._precision);
this._accumulationTextures[0].initialize(textureSize[0], textureSize[1],
internalFormat[0], gl.RGBA, internalFormat[1]);
this._accumulationTextures[1].initialize(textureSize[0], textureSize[1],
internalFormat[0], gl.RGBA, internalFormat[1]);
} else {
if (this._altered.texture || sizeAltered) {
this._accumulationTextures[0].resize(this._texture.width, this._texture.height);
this._accumulationTextures[1].resize(this._texture.width, this._texture.height);
}
if (this._altered.precision) {
const internalFormat = Wizard.queryInternalTextureFormat(this._context, gl.RGBA, this._precision);
this._accumulationTextures[0].reformat(internalFormat[0], gl.RGBA, internalFormat[1]);
this._accumulationTextures[1].reformat(internalFormat[0], gl.RGBA, internalFormat[1]);
}
}
if (!this._accumulationFBOs[0].initialized) {
this._accumulationFBOs[0].initialize([[gl2facade.COLOR_ATTACHMENT0, this._accumulationTextures[0]]]);
this._accumulationFBOs[1].initialize([[gl2facade.COLOR_ATTACHMENT0, this._accumulationTextures[1]]]);
}
assert(this._accumulationFBOs[0].valid && this._accumulationFBOs[1].valid,
`valid accumulation framebuffers expected`);
this._altered.reset();
}
/**
* An accumulation frame binds the two accumulation textures (ping-pong framebuffer), one for read, the other for
* write/accumulating into. A screen-aligned triangle is used to fill the viewport and mix the input texture with
* the weight of 1 / (frameNumber + 1) with the previous accumulation result. If no texture is specified, pass
* through is used.
* @param frameNumber - Frame number used to select the current read and write framebuffer as well as frame weight.
* @param viewport - If specified, the viewport for accumulation will be set to the given width and height. If not,
* the currently set viewport is used.
*/
@Initializable.assert_initialized()
frame(frameNumber: number, viewport?: GLsizei2): void {
assert(this._accumulationFBOs[0].valid && this._accumulationFBOs[1].valid,
`valid framebuffer objects for accumulation expected (initialize or update was probably not be called`);
if (this._passThrough || this._texture === undefined) {
return;
}
logIf(!this._texture || !this._texture.valid, LogLevel.Warning,
`valid texture for accumulation frame expected, given ${this._texture}`);
const gl = this._context.gl;
if (viewport !== undefined) {
gl.viewport(0, 0, viewport[0], viewport[1]);
}
const readIndex = frameNumber % 2;
const writeIndex = this._write = 1 - readIndex;
const accumTexture = this._accumulationTextures[readIndex];
const frameTexture = this._texture;
accumTexture.bind(gl.TEXTURE0);
frameTexture.bind(gl.TEXTURE1);
this._program.bind();
gl.uniform1f(this._uWeight, 1.0 / (frameNumber + 1));
this._accumulationFBOs[writeIndex].bind(gl.DRAW_FRAMEBUFFER); // bind draw only does not work for IE and EDGE
this._ndcTriangle.bind();
this._ndcTriangle.draw();
this._ndcTriangle.unbind();
this._accumulationFBOs[writeIndex].unbind(gl.DRAW_FRAMEBUFFER);
/** Every pass is expected to bind its own program when drawing, thus, unbinding is not necessary. */
// this.program.unbind();
accumTexture.unbind(gl.TEXTURE0);
frameTexture.unbind(gl.TEXTURE1);
}
/**
* Sets the texture that is to be accumulated. The ping and pong render textures will be resized on next frame
* automatically if the texture size changed.
* @param texture - Framebuffer that is to be accumulated.
*/
set texture(texture: Texture2D) {
this.assertInitialized();
if (this._texture !== texture) {
this._texture = texture;
this._altered.alter('texture');
}
}
/**
* Allows to specify the accumulation precision.
*/
set precision(precision: Wizard.Precision) {
this.assertInitialized();
if (this._precision !== precision) {
this._precision = precision;
this._altered.alter('precision');
}
}
/**
* Allows to skip accumulation. If pass through is enabled, nothing will be rendered on frame at all and the
* ping pong render textures will be reduced to a minimum size of [1, 1] until pass through is disabled.
*/
set passThrough(passThrough: boolean) {
this.assertInitialized();
if (this._passThrough === passThrough) {
return;
}
if (this._passThrough && this._accumulationTextures[0].initialized) {
this._accumulationTextures[0].uninitialize();
this._accumulationTextures[1].uninitialize();
}
if (this._passThrough && this._accumulationFBOs[0].initialized) {
this._accumulationFBOs[0].uninitialize();
this._accumulationFBOs[1].uninitialize();
}
this._passThrough = passThrough;
this._altered.alter('passThrough');
}
/**
* Returns the framebuffer last accumulated into. Note: the accumulation buffer is represented by two framebuffers
* swapped for read and write every frame. The accumulation result is in the first color attachment.
* @returns - The rgba framebuffer last accumulated into.
*/
get framebuffer(): Framebuffer | undefined {
return this._passThrough ? undefined : this._accumulationFBOs[this._write];
}
}