forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram-library.js
More file actions
73 lines (65 loc) · 2.26 KB
/
program-library.js
File metadata and controls
73 lines (65 loc) · 2.26 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
pc.extend(pc, function () {
'use strict';
// Public interface
var ProgramLibrary = function (device) {
this._device = device;
this._cache = {};
this._generators = {};
this._isClearingCache = false;
};
ProgramLibrary.prototype.register = function (name, generator) {
if (!this.isRegistered(name)) {
this._generators[name] = generator;
}
};
ProgramLibrary.prototype.unregister = function (name) {
if (this.isRegistered(name)) {
delete this._generators[name];
}
};
ProgramLibrary.prototype.isRegistered = function (name) {
var generator = this._generators[name];
return (generator !== undefined);
};
ProgramLibrary.prototype.getProgram = function (name, options) {
var generator = this._generators[name];
if (generator === undefined) {
logERROR("No program library functions registered for: " + name);
return null;
}
var gd = this._device;
var key = generator.generateKey(gd, options); // TODO: gd is never used in generateKey(), remove?
var shader = this._cache[key];
if (!shader) {
var shaderDefinition = generator.createShaderDefinition(gd, options);
shader = this._cache[key] = new pc.Shader(gd, shaderDefinition);
}
return shader;
};
ProgramLibrary.prototype.clearCache = function () {
var cache = this._cache;
this._isClearingCache = true;
for(var key in cache) {
if (cache.hasOwnProperty(key)) {
cache[key].destroy();
}
}
this._cache = {};
this._isClearingCache = false;
};
ProgramLibrary.prototype.removeFromCache = function(shader) {
if (this._isClearingCache) return; // don't delete by one when clearing whole cache
var cache = this._cache;
for(var key in cache) {
if (cache.hasOwnProperty(key)) {
if (cache[key]===shader) {
delete cache[key];
break;
}
}
}
};
return {
ProgramLibrary: ProgramLibrary
};
}());