-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprograms.js
47 lines (40 loc) · 1.27 KB
/
programs.js
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
import assert from 'assert';
import Program from './program';
const PROGRAMS = Symbol('programs');
export default class Programs {
/**
* @param {WebGLRenderingContext} gl
*/
constructor(gl) {
this.gl = gl;
this[PROGRAMS] = new Map();
}
/**
* @param {Material} material
* @return {Program}
*/
getProgram(material) {
// Get the program from the cache if we've seen this
// material before
if (this[PROGRAMS].has(material)) {
return this[PROGRAMS].get(material);
}
const vertSrc = material.getVertexSource();
const fragSrc = material.getFragmentSource();
// Otherwise, check to see if the frag and vert shaders
// are the same as another program already in the cache;
// this can occur when we use default materials,
// creating new instances so each instance has its own
// uniforms, but still should use the same underlying program/shaders.
for (let program of this[PROGRAMS].values()) {
if (program.getVertexSource() === vertSrc &&
program.getFragmentSource() === fragSrc) {
this[PROGRAMS].set(material, program);
return program;
}
}
const program = new Program(this.gl, vertSrc, fragSrc);
this[PROGRAMS].set(material, program);
return program;
}
}