-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathProgramCache.js
44 lines (44 loc) · 1.3 KB
/
ProgramCache.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
import Program from 'nanogl/program';
import { hashString } from './Hash';
const PRAGMA_SLOT = '#pragma SLOT';
const PRAGMA_REGEX = /^\s*#pragma SLOT\s\w+\s*$/gm;
function _slotRegex(token) {
return new RegExp(`${PRAGMA_SLOT}\\s+${token}\\s+`, 'g');
}
function processSlots(source, slots) {
for (const { code, key } of slots.slots) {
source = source.replace(_slotRegex(key), code);
}
PRAGMA_REGEX.lastIndex = 0;
source = source.replace(PRAGMA_REGEX, '');
return source;
}
class ProgramCache {
constructor(gl) {
this.gl = gl;
this._cache = {};
}
static getCache(gl) {
const agl = gl;
if (agl._prgcache === undefined) {
agl._prgcache = new ProgramCache(gl);
}
return agl._prgcache;
}
compile(source) {
const hash = hashString(source.shaderSource.uid, source.slots.getHash());
const cached = this._cache[hash];
if (cached !== undefined) {
return cached;
}
const vert = processSlots(source.shaderSource.vert, source.slots);
const frag = processSlots(source.shaderSource.frag, source.slots);
const prg = new Program(this.gl, vert, frag);
this._cache[hash] = prg;
return prg;
}
release(prg) {
}
}
;
export default ProgramCache;