forked from CesiumGS/cesium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShaderCache.js
169 lines (146 loc) · 6.07 KB
/
ShaderCache.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
169
/*global define*/
define([
'../Core/defined',
'../Core/defineProperties',
'../Core/destroyObject',
'./ShaderProgram',
'./ShaderSource'
], function(
defined,
defineProperties,
destroyObject,
ShaderProgram,
ShaderSource) {
"use strict";
/**
* @private
*/
var ShaderCache = function(context) {
this._context = context;
this._shaders = {};
this._numberOfShaders = 0;
this._shadersToRelease = {};
};
defineProperties(ShaderCache.prototype, {
numberOfShaders : {
get : function() {
return this._numberOfShaders;
}
}
});
/**
* Returns a shader program from the cache, or creates and caches a new shader program,
* given the GLSL vertex and fragment shader source and attribute locations.
* <p>
* The difference between this and {@link ShaderCache#getShaderProgram}, is this is used to
* replace an existing reference to a shader program, which is passed as the first argument.
* </p>
*
* @param {ShaderProgram} shaderProgram The shader program that is being reassigned. This can be <code>undefined</code>.
* @param {String|ShaderSource} vertexShaderSource The GLSL source for the vertex shader.
* @param {String|ShaderSource} fragmentShaderSource The GLSL source for the fragment shader.
* @param {Object} attributeLocations Indices for the attribute inputs to the vertex shader.
* @returns {ShaderProgram} The cached or newly created shader program.
*
* @see ShaderCache#getShaderProgram
*
* @example
* this._shaderProgram = context.shaderCache.replaceShaderProgram(this._shaderProgram, vs, fs, attributeLocations);
*/
ShaderCache.prototype.replaceShaderProgram = function(shaderProgram, vertexShaderSource, fragmentShaderSource, attributeLocations) {
if (defined(shaderProgram)) {
shaderProgram.destroy();
}
return this.getShaderProgram(vertexShaderSource, fragmentShaderSource, attributeLocations);
};
/**
* Returns a shader program from the cache, or creates and caches a new shader program,
* given the GLSL vertex and fragment shader source and attribute locations.
*
* @param {String|ShaderSource} vertexShaderSource The GLSL source for the vertex shader.
* @param {String|ShaderSource} fragmentShaderSource The GLSL source for the fragment shader.
* @param {Object} attributeLocations Indices for the attribute inputs to the vertex shader.
*
* @returns {ShaderProgram} The cached or newly created shader program.
*/
ShaderCache.prototype.getShaderProgram = function(vertexShaderSource, fragmentShaderSource, attributeLocations) {
// convert shaders which are provided as strings into ShaderSource objects
// because ShaderSource handles all the automatic including of built-in functions, etc.
if (typeof vertexShaderSource === 'string') {
vertexShaderSource = new ShaderSource({
sources : [vertexShaderSource]
});
}
if (typeof fragmentShaderSource === 'string') {
fragmentShaderSource = new ShaderSource({
sources : [fragmentShaderSource]
});
}
var vertexShaderText = vertexShaderSource.createCombinedVertexShader();
var fragmentShaderText = fragmentShaderSource.createCombinedFragmentShader();
var keyword = vertexShaderText + fragmentShaderText + JSON.stringify(attributeLocations);
var cachedShader;
if (this._shaders[keyword]) {
cachedShader = this._shaders[keyword];
// No longer want to release this if it was previously released.
delete this._shadersToRelease[keyword];
} else {
var context = this._context;
var shaderProgram = new ShaderProgram({
gl : context._gl,
logShaderCompilation : context.logShaderCompilation,
debugShaders : context.debugShaders,
vertexShaderSource : vertexShaderSource,
vertexShaderText : vertexShaderText,
fragmentShaderSource : fragmentShaderSource,
fragmentShaderText : fragmentShaderText,
attributeLocations : attributeLocations
});
cachedShader = {
cache : this,
shaderProgram : shaderProgram,
keyword : keyword,
count : 0
};
// A shader can't be in more than one cache.
shaderProgram._cachedShader = cachedShader;
this._shaders[keyword] = cachedShader;
++this._numberOfShaders;
}
++cachedShader.count;
return cachedShader.shaderProgram;
};
ShaderCache.prototype.destroyReleasedShaderPrograms = function() {
var shadersToRelease = this._shadersToRelease;
for ( var keyword in shadersToRelease) {
if (shadersToRelease.hasOwnProperty(keyword)) {
var cachedShader = shadersToRelease[keyword];
delete this._shaders[cachedShader.keyword];
cachedShader.shaderProgram.finalDestroy();
--this._numberOfShaders;
}
}
this._shadersToRelease = {};
};
ShaderCache.prototype.releaseShaderProgram = function(shaderProgram) {
if (shaderProgram) {
var cachedShader = shaderProgram._cachedShader;
if (cachedShader && (--cachedShader.count === 0)) {
this._shadersToRelease[cachedShader.keyword] = cachedShader;
}
}
};
ShaderCache.prototype.isDestroyed = function() {
return false;
};
ShaderCache.prototype.destroy = function() {
var shaders = this._shaders;
for ( var keyword in shaders) {
if (shaders.hasOwnProperty(keyword)) {
shaders[keyword].shaderProgram.finalDestroy();
}
}
return destroyObject(this);
};
return ShaderCache;
});