forked from DavidPeicho/Teaching
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathgl.ts
More file actions
722 lines (656 loc) · 20.2 KB
/
gl.ts
File metadata and controls
722 lines (656 loc) · 20.2 KB
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
import { Geometry } from './geometries/geometry';
import { Shader } from './shader/shader';
import { Texture, Texture2D } from './textures/texture';
import { PixelArray, UniformType } from './types';
/**
* Wrapper on a WebGL2RenderingContext. This hold the entire WebGL context, its
* current state, and some data.
*
* This file is used to simplify resource management, and to simplify the
* process of sending draw calls to WebGL.
*
* ## Note
*
* This abstraction is **far** from perfect. Making a really good abstraction
* is anyway not the subject here, and would be a really time consuming job.
* A lot of errors aren't handled, and some corner cases are clearly not
* handled.
*
* However, it's robust enough to handle a lot of uniforms, textures, and
* state changes.
*
* ## Advice
*
* I advise you to not spend too much time trying to figure out at first exactly
* what that does. You should first finish the assignment.
*
* When you are done with the assignment, you can go over this file to
* understand how I abstracted WebGL to simplify the usage. I am not saying
* that this wrapper is perfect. However, for the use case of this project,
* it does a pretty good job without going over ~200 lines.
*/
export class GLContext {
/** Inner WebGL context */
private _gl: WebGL2RenderingContext;
/**
* Cache for geometries. This cache allows us to retrieve the GL object
* associated to an instance of [[Geometry]] (our class).
*
* @private
*/
private _geometries: WeakMap<Geometry, GeometryCache>;
/**
* Cache for textures. This cache allows us to retrieve the GL object
* associated to an instance of [[Texture]] (our class)
*
* @private
*/
private _textures: WeakMap<Texture, TextureCache>;
/**
* Cache for programs. This cache allows us to retrieve the GL object
* associated to an instance of [[Texture]] (our class)
*
* @private
*/
private _programs: WeakMap<Shader, ProgramCache | null>;
/**
* Currently bound texture unit slot.
*
* @private
*/
private _textureUnitSlot: number;
public constructor(canvas: HTMLCanvasElement) {
this._gl = canvas.getContext('webgl2') as WebGL2RenderingContext;
this._gl.clearColor(0.1, 0.1, 0.1, 1.0);
this._geometries = new WeakMap();
this._textures = new WeakMap();
this._programs = new WeakMap();
this._textureUnitSlot = 0;
this.resetViewport();
}
/**
* Set the GL viewport to match the main framebuffer size
*/
public resetViewport(): void {
const gl = this._gl;
gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
}
/**
* Set the GL viewport width and height
*/
public setViewport(width: number, height: number): void {
this._gl.viewport(0, 0, width, height);
}
/**
* Sets the GL culling.
*
* @param type - GL culling parameter. Using `null` will disable culling.
*
* @return This instance, for chaining
*/
public setCulling(type: number | null): this {
const gl = this._gl;
// @note: clearly unoptimized. Enabling / disabling culling just to change
// the culling type is useless.
if (type == null) {
gl.disable(gl.CULL_FACE);
} else {
gl.enable(gl.CULL_FACE);
gl.cullFace(type);
}
return this;
}
/**
* Enables / Disables depth testing.
*
* @param flag - If `true`, depth testing will be enabled.
*
* @return This instance, for chaining
*/
public setDepthTest(flag: boolean): this {
const gl = this._gl;
if (flag) {
gl.enable(gl.DEPTH_TEST);
} else {
gl.disable(gl.DEPTH_TEST);
}
return this;
}
/**
* Compiles a given [[Shader]] instance.
*
* ## Note
*
* Errors will be reported in the console.
*
* @param shader - The [[Shader]] instancce to compile
*
* @return This instance, for chaining
*/
public compileProgram(shader: Shader): this {
const gl = this._gl;
const cache = this._programs.get(shader);
if (cache) {
gl.deleteShader(cache.vertexObjectGL);
gl.deleteShader(cache.fragmentObjectGL);
gl.deleteProgram(cache.programObjectGL);
}
const vxSource = preprocessShader(shader.vertexSource, shader.defines);
const fgSource = preprocessShader(shader.fragmentSource, shader.defines);
let programObjectGL = null;
const vertexObjectGL = compileShader(gl, gl.VERTEX_SHADER, vxSource);
const fragmentObjectGL = compileShader(gl, gl.FRAGMENT_SHADER, fgSource);
if (!!vertexObjectGL && !!fragmentObjectGL) {
programObjectGL = compileProgram(gl, vertexObjectGL!, fragmentObjectGL!);
}
if (programObjectGL === null) {
if (vertexObjectGL) {
gl.deleteShader(vertexObjectGL);
}
if (fragmentObjectGL) {
gl.deleteShader(fragmentObjectGL);
}
this._programs.set(shader, null);
return this;
}
const uniformLocationLUT = getActiveUniformsInfo(gl, programObjectGL);
this._programs.set(shader, {
vertexObjectGL,
fragmentObjectGL,
programObjectGL,
uniformLocationLUT,
vaoList: new Map()
});
return this;
}
/**
* Uploads a geometry to the GPU.
*
* ## Note
*
* Geometry are represented as a set of Vertex Buffer Object (VBO), For each
* shader, a VAO is created for the geometry.
*
* @param geometry - The [[Geometry]] instance to upload
*/
public uploadGeometry(geometry: Geometry) {
const gl = this._gl;
if (!this._geometries.has(geometry)) {
// Note: we are missing `null` check here.
const cache = {
positionBuffer: this._gl.createBuffer(),
normalBuffer: this._gl.createBuffer(),
uvBuffer: geometry.uvs != null ? this._gl.createBuffer() : null,
indexBuffer: this._gl.createBuffer()
} as GeometryCache;
this._geometries.set(geometry, cache);
}
const cache = this._geometries.get(geometry)!;
switch (geometry.indices.BYTES_PER_ELEMENT) {
case 1:
cache.indexType = gl.UNSIGNED_BYTE;
break;
case 2:
cache.indexType = gl.UNSIGNED_SHORT;
break;
case 4:
cache.indexType = gl.UNSIGNED_INT;
break;
}
gl.bindBuffer(gl.ARRAY_BUFFER, cache.positionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, geometry.positions, gl.STATIC_DRAW);
if (cache.normalBuffer != null) {
gl.bindBuffer(gl.ARRAY_BUFFER, cache.normalBuffer);
gl.bufferData(gl.ARRAY_BUFFER, geometry.normals, gl.STATIC_DRAW);
}
if (cache.uvBuffer != null) {
gl.bindBuffer(gl.ARRAY_BUFFER, cache.uvBuffer);
gl.bufferData(gl.ARRAY_BUFFER, geometry.uvs, gl.STATIC_DRAW);
}
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cache.indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, geometry.indices, gl.STATIC_DRAW);
}
/**
* Uploads a texture to the GPU.
*
* ## Note
*
* Only a few texture parameters are used. If you need more fancy stuff,
* please feel free to improve this method.
*
* For the scope of the assignment, the method should work perfectly to
* upload your enviroment maps, as well as your model textures
*
* @param texture - The [[Texture]] instance to upload
*/
public uploadTexture(texture: Texture) {
const gl = this._gl;
const glObject = gl.createTexture()!;
const bindType = texture.glBindType();
gl.bindTexture(bindType, glObject);
if (texture instanceof Texture2D) {
if ((texture.data as PixelArray).buffer != null) {
// Raw data.
gl.texImage2D(
gl.TEXTURE_2D,
0,
texture.internalFormatGL,
texture.width,
texture.height,
0,
texture.formatGL,
// gl.FLOAT,
texture.typeGL,
texture.data,
0
);
} else {
// HTML element data.
gl.texImage2D(
gl.TEXTURE_2D,
0,
texture.internalFormatGL,
texture.formatGL,
texture.typeGL,
texture.data
);
}
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, texture.flipY);
}
gl.texParameteri(bindType, gl.TEXTURE_MIN_FILTER, texture.minFilterGL);
gl.texParameteri(bindType, gl.TEXTURE_MAG_FILTER, texture.maxFilterGL);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, texture.wrapS);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, texture.wrapT);
this._textures.set(texture, { glObject });
}
/**
* Attach a texture as the color output of the currently bound framebuffer
*
* @param texture - The [[Texture]] to attach
*/
public setFramebufferTexture(texture: Texture) {
let envDiffuseTextureObject = this._textures.get(texture)?.glObject;
if (envDiffuseTextureObject === undefined) {
console.error('Attempting to attach a non-existing texture.');
return;
}
this._gl.framebufferTexture2D(this._gl.FRAMEBUFFER, this._gl.COLOR_ATTACHMENT0, this._gl.TEXTURE_2D, envDiffuseTextureObject, 0);
}
/**
* Deletes a geometry from the WebGL context.
*
* @param geometry - The geometry to delete
*/
public destroyGeometry(geometry: Geometry) {
const cache = this._geometries.get(geometry);
if (cache) {
const gl = this._gl;
gl.deleteBuffer(cache.indexBuffer);
gl.deleteBuffer(cache.positionBuffer);
}
}
/**
* Deletes a program from the WebGL context.
*
* ## Note
*
* This will delete the program, the vertex shader, as well as the frament
* shader.
*
* @param geometry - The geometry to delete
*/
public destroyProgram(program: Shader) {
const gl = this._gl;
const cache = this._programs.get(program);
if (cache === undefined || cache === null) {
return;
}
if (cache.programObjectGL) {
if (cache.vertexObjectGL) {
gl.detachShader(cache.programObjectGL, cache.vertexObjectGL);
}
if (cache.fragmentObjectGL) {
gl.detachShader(cache.programObjectGL, cache.fragmentObjectGL);
}
gl.deleteProgram(cache.programObjectGL);
}
if (cache.vertexObjectGL) {
gl.deleteShader(cache.vertexObjectGL);
}
if (cache.fragmentObjectGL) {
gl.deleteShader(cache.fragmentObjectGL);
}
}
/**
* Draws a geometry using the specified shader and feeds the shader with
* the given uniforms.
*
* @param {Geometry} geometry - The geometry to draw
* @param {Shader} shader - The shader to use for drawing
* @param {(Record<string, UniformType | Texture>)} uniforms - The object
* containing uniforms to forward to the shader
*/
public draw(
geometry: Geometry,
shader: Shader,
uniforms: Record<string, UniformType | Texture>
) {
const geometryCache = this._geometries.get(geometry);
if (!geometryCache) {
console.error('Attempting to draw a geometry that isnt on the GPU');
return;
}
const programCache = this._programs.get(shader);
if (programCache === undefined) {
console.error('Attempting to use a non-compiled program.');
return;
} else if (programCache === null) {
return;
}
const gl = this._gl;
const uniformsLUT = programCache.uniformLocationLUT;
const VAOs = programCache.vaoList;
if (!VAOs.has(geometry)) {
VAOs.set(geometry, {
glObject: this._createVAO(programCache.programObjectGL!, geometryCache)
});
}
const vao = programCache.vaoList.get(geometry)!;
gl.useProgram(programCache.programObjectGL);
gl.bindVertexArray(vao.glObject);
// Sends uniforms to GPU.
for (const uniformId in uniforms) {
// **Note**: optimization possible here. For heavy uniforms, it's possible
// to keep a cache in order to avoid making useless GPU calls (which are always expensive).
const info = uniformsLUT.get(uniformId);
if (info === undefined) {
continue;
}
const uniformValue = uniforms[uniformId];
if ((uniformValue as Texture).isTexture) {
// @todo: check for max texture unit slot.
const unit = this._textureUnitSlot++;
gl.activeTexture(gl.TEXTURE0 + unit);
this._bindTexture(uniformValue as Texture);
uploadUniform(gl, info.location, info.type, unit);
} else {
uploadUniform(
gl,
info.location,
info.type,
uniformValue as UniformType
);
}
}
gl.drawElements(
geometry.mode,
geometry.indices.length,
geometryCache.indexType,
0
);
// @note: this is a clearly not the most optimized things to do,
// but we have more interesting stuff to work on :)
this._textureUnitSlot = 0;
}
/**
* Clears the currently bound framebuffer
*/
public clear() {
// NOTE: we know we never change the clear color. If we were to change it
// somewhere, it would need to be set back to the good color before.
this._gl.clear(WebGL2RenderingContext.COLOR_BUFFER_BIT);
}
/**
* Internal method handling the creation of a Vertex Array Object.
*
* @param program - The program to which the VAO will be run with
* @param geometryCache - The cache of the geometry associated to the VAO
* to create
*
* ## Note
*
* Failures aren't handled.
*
* @private
*
* @return The created VAO
*/
private _createVAO(
program: WebGLProgram,
geometryCache: GeometryCache
): WebGLVertexArrayObject {
const gl = this._gl;
const vao = gl.createVertexArray()!;
// @fix: this can be cached.
const positionAttributeLocation = gl.getAttribLocation(
program,
'in_position'
);
const normalAttributeLocation = gl.getAttribLocation(program, 'in_normal');
const uvAttributeLocation = gl.getAttribLocation(program, 'in_uv');
const hasNormal =
normalAttributeLocation != -1 && geometryCache.normalBuffer !== null;
const hasUV = uvAttributeLocation != -1 && geometryCache.uvBuffer !== null;
let strideComp = 3;
strideComp = hasNormal ? strideComp + 3 : strideComp;
strideComp = hasUV ? strideComp + 2 : strideComp;
const byteSize = Float32Array.BYTES_PER_ELEMENT;
gl.bindVertexArray(vao);
gl.enableVertexAttribArray(positionAttributeLocation);
gl.bindBuffer(gl.ARRAY_BUFFER, geometryCache.positionBuffer);
gl.vertexAttribPointer(
positionAttributeLocation,
3,
gl.FLOAT,
false,
3 * byteSize,
0
);
// **Note**: the code assumes a geometry **always** has normals.
// To be as generic as possible, you would:
// * Upload normal only when available
// * Recompile a shader variant that strips the code for the normal away
if (hasNormal) {
gl.enableVertexAttribArray(normalAttributeLocation);
gl.bindBuffer(gl.ARRAY_BUFFER, geometryCache.normalBuffer);
gl.vertexAttribPointer(
normalAttributeLocation,
3,
gl.FLOAT,
false,
3 * byteSize,
0
);
}
if (hasUV) {
// Only setup the UV attribute if the geometry has UVs.
gl.enableVertexAttribArray(uvAttributeLocation);
gl.bindBuffer(gl.ARRAY_BUFFER, geometryCache.uvBuffer);
gl.vertexAttribPointer(
uvAttributeLocation,
2,
gl.FLOAT,
false,
2 * byteSize,
0
);
}
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, geometryCache.indexBuffer);
return vao;
}
/**
* Binds a given texture.
*
* @param texture - The texture to bind
*
* @private
*/
private _bindTexture(texture: Texture) {
const gl = this._gl;
const cache = this._textures.get(texture);
if (!cache) {
return;
}
gl.bindTexture(texture.glBindType(), cache.glObject);
}
/** Returns the inner [[WebGL2RenderingContext]] instance */
public get gl(): WebGL2RenderingContext {
return this._gl;
}
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////// UTILS //////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
export function compileShader(
gl: WebGL2RenderingContext,
type: number,
source: string
): WebGLShader | null {
const shader = gl.createShader(type);
if (!shader) {
return null;
}
gl.shaderSource(shader, source);
gl.compileShader(shader);
const success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
if (!success) {
console.error('[ SHADER ]: Failed to compile shader. Info:');
console.error(gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
return null;
}
return shader;
}
export function compileProgram(
gl: WebGL2RenderingContext,
vertexShader: WebGLShader,
fragmentShader: WebGLShader
): WebGLProgram | null {
const program = gl.createProgram();
if (!program) {
return null;
}
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
const success = gl.getProgramParameter(program, gl.LINK_STATUS);
if (!success) {
console.error('[ PROGRAM ]: Failed to compile program. Info:');
console.error(gl.getProgramInfoLog(program));
gl.deleteProgram(program);
return null;
}
return program;
}
export function preprocessShader(
source: string,
defines: Record<string, boolean | number>
): string {
let definesStr = '';
for (const name in defines) {
const val = defines[name];
if (val) {
// Here we really want to test `=== true` and not simply something like:
// `!!val`.
definesStr +=
val === true ? `#define ${name}\n` : `#define ${name} ${val}\n`;
}
}
return `#version 300 es\n\n${definesStr}\n${source}`;
}
export function getActiveUniformsInfo(
gl: WebGL2RenderingContext,
program: WebGLProgram
): Map<string, UniformInfo> {
const output = new Map();
const count = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
for (let i = 0; i < count; ++i) {
const info = gl.getActiveUniform(program, i);
if (info !== null) {
const location = gl.getUniformLocation(program, info.name);
if (location !== null) {
output.set(info.name, { location, type: info.type });
}
}
}
return output;
}
export function uploadUniform(
gl: WebGL2RenderingContext,
loc: WebGLUniformLocation,
type: number,
value: UniformType
) {
// **Note**: possible optimization would be to
// save function pointer per type.
switch (type) {
case gl.BOOL:
gl.uniform1ui(loc, value as number);
case gl.FLOAT:
gl.uniform1f(loc, value as number);
break;
case gl.FLOAT_VEC2:
gl.uniform2fv(loc, value as Float32Array);
break;
case gl.FLOAT_VEC3:
gl.uniform3fv(loc, value as Float32Array);
break;
case gl.FLOAT_VEC4:
gl.uniform4fv(loc, value as Float32Array);
break;
case gl.FLOAT_MAT2:
gl.uniformMatrix2fv(loc, false, value as Float32Array);
break;
case gl.FLOAT_MAT3:
gl.uniformMatrix3fv(loc, false, value as Float32Array);
break;
case gl.FLOAT_MAT4:
gl.uniformMatrix4fv(loc, false, value as Float32Array);
break;
case gl.INT:
gl.uniform1i(loc, value as number);
break;
case gl.INT_VEC2:
gl.uniform2iv(loc, value as Uint16Array);
break;
case gl.INT_VEC3:
gl.uniform3iv(loc, value as Uint16Array);
break;
case gl.INT_VEC4:
gl.uniform4iv(loc, value as Uint16Array);
break;
// **Note**: texture arrays not supported. Several texture units
// would need to be set.
case gl.SAMPLER_2D:
case gl.SAMPLER_CUBE:
gl.uniform1i(loc, value as number);
break;
}
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////// TYPES //////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
export interface UniformInfo {
location: WebGLUniformLocation;
type: number;
}
interface GeometryCache {
positionBuffer: WebGLBuffer;
normalBuffer: WebGLBuffer;
uvBuffer: WebGLBuffer | null;
indexBuffer: WebGLBuffer;
indexType: number;
}
interface VAOCache {
glObject: WebGLVertexArrayObject;
}
interface TextureCache {
glObject: WebGLTexture;
}
interface ProgramCache {
programObjectGL: WebGLProgram | null;
vertexObjectGL: WebGLShader | null;
fragmentObjectGL: WebGLShader | null;
uniformLocationLUT: Map<string, UniformInfo>;
vaoList: Map<Geometry, VAOCache>;
}