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
181 lines (159 loc) · 6.66 KB
/
program-library.js
File metadata and controls
181 lines (159 loc) · 6.66 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
import { version, revision } from '../core/core.js';
import { Shader } from './shader.js';
import { SHADER_FORWARD, SHADER_FORWARDHDR, SHADER_PICK, SHADER_SHADOW } from '../scene/constants.js';
import { StandardMaterial } from '../scene/materials/standard-material.js';
// Public interface
class ProgramLibrary {
constructor(device) {
this._device = device;
this._cache = {};
this._generators = {};
this._isClearingCache = false;
this._precached = false;
// Unique non-cached programs collection to dump and update game shaders cache
this._programsCollection = [];
this._defaultStdMatOption = {};
this._defaultStdMatOptionMin = {};
var m = new StandardMaterial();
m.shaderOptBuilder.updateRef(
this._defaultStdMatOption, device, {}, m, null, [], SHADER_FORWARD, null, null);
m.shaderOptBuilder.updateMinRef(
this._defaultStdMatOptionMin, device, {}, m, null, [], SHADER_SHADOW, null, null);
}
register(name, generator) {
if (!this.isRegistered(name)) {
this._generators[name] = generator;
}
}
unregister(name) {
if (this.isRegistered(name)) {
delete this._generators[name];
}
}
isRegistered(name) {
var generator = this._generators[name];
return (generator !== undefined);
}
getProgram(name, options) {
var generator = this._generators[name];
if (generator === undefined) {
// #if _DEBUG
console.warn("ProgramLibrary#getProgram: No program library functions registered for: " + name);
// #endif
return null;
}
var gd = this._device;
var key = generator.generateKey(options);
var shader = this._cache[key];
if (!shader) {
var lights;
if (options.lights) {
lights = options.lights;
options.lights = lights.map(function (l) {
var lcopy = l.clone ? l.clone() : l;
lcopy.key = l.key;
return lcopy;
});
}
this.storeNewProgram(name, options);
if (options.lights)
options.lights = lights;
if (this._precached)
console.warn("ProgramLibrary#getProgram: Cache miss for shader", name, "key", key, "after shaders precaching");
var shaderDefinition = generator.createShaderDefinition(gd, options);
shader = this._cache[key] = new Shader(gd, shaderDefinition);
}
return shader;
}
storeNewProgram(name, options) {
var opt = {};
if (name === "standard") {
// For standard material saving all default values is overkill, so we store only diff
var defaultMat = this._getDefaultStdMatOptions(options.pass);
for (var p in options) {
if ((options.hasOwnProperty(p) && defaultMat[p] !== options[p]) || p === "pass")
opt[p] = options[p];
}
} else {
// Other shaders have only dozen params
opt = options;
}
this._programsCollection.push(JSON.stringify({ name: name, options: opt }));
}
// run pc.app.graphicsDevice.programLib.dumpPrograms(); from browser console to build shader options script
dumpPrograms() {
var text = 'var device = pc.app ? pc.app.graphicsDevice : pc.Application.getApplication().graphicsDevice;\n';
text += 'var shaders = [';
if (this._programsCollection[0])
text += '\n\t' + this._programsCollection[0];
for (var i = 1; i < this._programsCollection.length; ++i) {
text += ',\n\t' + this._programsCollection[i];
}
text += '\n];\n';
text += 'device.programLib.precompile(shaders);\n';
text += 'if (pc.version != \"' + version + '\" || pc.revision != \"' + revision + '\")\n';
text += '\tconsole.warn(\"precompile-shaders.js: engine version mismatch, rebuild shaders lib with current engine\");';
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', 'precompile-shaders.js');
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
clearCache() {
var cache = this._cache;
this._isClearingCache = true;
for (var key in cache) {
if (cache.hasOwnProperty(key)) {
cache[key].destroy();
}
}
this._cache = {};
this._isClearingCache = false;
}
removeFromCache(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;
}
}
}
}
_getDefaultStdMatOptions(pass) {
return (pass > SHADER_FORWARDHDR && pass <= SHADER_PICK) ?
this._defaultStdMatOptionMin : this._defaultStdMatOption;
}
precompile(cache) {
if (cache) {
var shaders = new Array(cache.length);
for (var i = 0; i < cache.length; i++) {
if (cache[i].name === "standard") {
var opt = cache[i].options;
var defaultMat = this._getDefaultStdMatOptions(opt.pass);
for (var p in defaultMat) {
if (defaultMat.hasOwnProperty(p) && opt[p] === undefined)
opt[p] = defaultMat[p];
}
// Patch device specific properties
opt.useTexCubeLod = this._device.useTexCubeLod;
}
shaders[i] = this.getProgram(cache[i].name, cache[i].options);
}
// Uncomment to force finish linking after preload
// var device = this._device;
// var forceLink = function () {
// for (var i = 0; i < shaders.length; i++) {
// device.postLink(shaders[i]);
// }
// };
// pc.Application.getApplication().on("preload:end", forceLink);
}
this._precached = true;
}
}
export { ProgramLibrary };