-
Notifications
You must be signed in to change notification settings - Fork 205
/
program-manager.js
168 lines (135 loc) · 4.2 KB
/
program-manager.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
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
import {assembleShaders} from '@luma.gl/shadertools';
import {Program} from '@luma.gl/webgl';
export default class ProgramManager {
static getDefaultProgramManager(gl) {
gl.luma = gl.luma || {};
gl.luma.defaultProgramManager = gl.luma.defaultProgramManager || new ProgramManager(gl);
return gl.luma.defaultProgramManager;
}
constructor(gl) {
this.gl = gl;
this._programCache = {};
this._getUniforms = {};
this._registeredModules = {};
this._hookFunctions = [];
this._defaultModules = [];
this._hashes = {};
this._hashCounter = 0;
this.stateHash = 0; // Used change hashing if hooks are modified
this._useCounts = {};
}
addDefaultModule(module) {
if (!this._defaultModules.find(m => m.name === module.name)) {
this._defaultModules.push(module);
}
this.stateHash++;
}
removeDefaultModule(module) {
const moduleName = typeof module === 'string' ? module : module.name;
this._defaultModules = this._defaultModules.filter(m => m.name !== moduleName);
this.stateHash++;
}
addShaderHook(hook, opts) {
if (opts) {
hook = Object.assign(opts, {hook});
}
this._hookFunctions.push(hook);
this.stateHash++;
}
get(props = {}) {
const {
vs = '',
fs = '',
defines = {},
inject = {},
varyings = [],
bufferMode = 0x8c8d,
transpileShaders = false
} = props; // varyings/bufferMode for xform feedback, 0x8c8d = SEPARATE_ATTRIBS
const modules = this._getModuleList(props.modules); // Combine with default modules
const vsHash = this._getHash(vs);
const fsHash = this._getHash(fs);
const moduleHashes = modules.map(m => this._getHash(m.name)).sort();
const varyingHashes = varyings.map(v => this._getHash(v));
const defineKeys = Object.keys(defines).sort();
const injectKeys = Object.keys(inject).sort();
const defineHashes = [];
const injectHashes = [];
for (const key of defineKeys) {
defineHashes.push(this._getHash(key));
defineHashes.push(this._getHash(defines[key]));
}
for (const key of injectKeys) {
injectHashes.push(this._getHash(key));
injectHashes.push(this._getHash(inject[key]));
}
const hash = `${vsHash}/${fsHash}D${defineHashes.join('/')}M${moduleHashes.join(
'/'
)}I${injectHashes.join('/')}V${varyingHashes.join('/')}H${this.stateHash}B${bufferMode}${
transpileShaders ? 'T' : ''
}`;
if (!this._programCache[hash]) {
const assembled = assembleShaders(this.gl, {
vs,
fs,
modules,
inject,
defines,
hookFunctions: this._hookFunctions,
transpile: transpileShaders
});
this._programCache[hash] = new Program(this.gl, {
hash,
vs: assembled.vs,
fs: assembled.fs,
varyings,
bufferMode
});
this._getUniforms[hash] = assembled.getUniforms || (x => {});
this._useCounts[hash] = 0;
}
this._useCounts[hash]++;
return this._programCache[hash];
}
getUniforms(program) {
return this._getUniforms[program.hash] || null;
}
release(program) {
const hash = program.hash;
this._useCounts[hash]--;
if (this._useCounts[hash] === 0) {
this._programCache[hash].delete();
delete this._programCache[hash];
delete this._getUniforms[hash];
delete this._useCounts[hash];
}
}
_getHash(key) {
if (this._hashes[key] === undefined) {
this._hashes[key] = this._hashCounter++;
}
return this._hashes[key];
}
// Dedup and combine with default modules
_getModuleList(appModules = []) {
const modules = new Array(this._defaultModules.length + appModules.length);
const seen = {};
let count = 0;
for (let i = 0, len = this._defaultModules.length; i < len; ++i) {
const module = this._defaultModules[i];
const name = module.name;
modules[count++] = module;
seen[name] = true;
}
for (let i = 0, len = appModules.length; i < len; ++i) {
const module = appModules[i];
const name = module.name;
if (!seen[name]) {
modules[count++] = module;
seen[name] = true;
}
}
modules.length = count;
return modules;
}
}