forked from CesiumGS/cesium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShaderCache.js
154 lines (135 loc) · 4.56 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
/*global define*/
define([
'../Core/defined',
'../Core/destroyObject'
], function(
defined,
destroyObject) {
"use strict";
/**
* DOC_TBA
*
* @alias ShaderCache
*
* @internalConstructor
*
* @see Context#getShaderCache
*/
var ShaderCache = function(context) {
this._context = context;
this._shaders = {};
this._shadersToRelease = {};
};
/**
* 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>
*
* @memberof ShaderCache
*
* @param {ShaderProgram} shaderProgram The shader program that is being reassigned. This can be <code>undefined</code>.
* @param {String} vertexShaderSource The GLSL source for the vertex shader.
* @param {String} 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.release();
}
return this.getShaderProgram(vertexShaderSource, fragmentShaderSource, attributeLocations);
};
/**
* DOC_TBA
*
* @memberof ShaderCache
*
* @returns {ShaderProgram} DOC_TBA.
*
* @see ShaderCache#replaceShaderProgram
*/
ShaderCache.prototype.getShaderProgram = function(vertexShaderSource, fragmentShaderSource, attributeLocations) {
var keyword = vertexShaderSource + fragmentShaderSource + 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 sp = this._context.createShaderProgram(vertexShaderSource, fragmentShaderSource, attributeLocations);
cachedShader = {
cache : this,
shaderProgram : sp,
keyword : keyword,
count : 0
};
// A shader can't be in more than one cache.
sp._cachedShader = cachedShader;
this._shaders[keyword] = cachedShader;
}
++cachedShader.count;
return cachedShader.shaderProgram;
};
/**
* DOC_TBA
* @memberof ShaderCache
*/
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.destroy();
}
}
this._shadersToRelease = {};
};
/**
* DOC_TBA
*
* @memberof ShaderCache
*
* @parameter {ShaderProgram} shaderProgram DOC_TBA.
*/
ShaderCache.prototype.releaseShaderProgram = function(shaderProgram) {
if (shaderProgram) {
var cachedShader = shaderProgram._cachedShader;
if (cachedShader && (--cachedShader.count === 0)) {
this._shadersToRelease[cachedShader.keyword] = cachedShader;
}
}
return undefined;
};
/**
* DOC_TBA
* @memberof ShaderCache
*/
ShaderCache.prototype.isDestroyed = function() {
return false;
};
/**
* DOC_TBA
* @memberof ShaderCache
*/
ShaderCache.prototype.destroy = function() {
var shaders = this._shaders;
for ( var keyword in shaders) {
if (shaders.hasOwnProperty(keyword)) {
shaders[keyword].shaderProgram.destroy();
}
}
return destroyObject(this);
};
return ShaderCache;
});