1
- 'use strict'
2
-
3
- // This module implements caching for shader objects
4
-
5
- var REGLError = require ( './error' )
1
+ var check = require ( './check' )
6
2
var formatCompilerError = require ( 'gl-format-compiler-error' )
7
3
8
- function REGLProgram ( program , uniforms , attributes ) {
9
- this . program = program
10
- this . uniforms = uniforms
11
- this . attributes = attributes
12
- }
13
-
14
4
module . exports = function createShaderCache ( gl ) {
15
5
var shaders = { }
16
6
var programs = { }
17
7
8
+ var GL_FRAGMENT_SHADER = gl . FRAGMENT_SHADER
9
+ var GL_VERTEX_SHADER = gl . VERTEX_SHADER
10
+
11
+ function REGLProgram ( program , uniforms , attributes ) {
12
+ this . program = program
13
+ this . uniforms = uniforms
14
+ this . attributes = attributes
15
+ }
16
+
18
17
function clearCache ( ) {
19
- shaders [ gl . FRAGMENT_SHADER ] = { }
20
- shaders [ gl . VERTEX_SHADER ] = { }
21
- programs = { }
18
+ Object . keys ( shaders ) . forEach ( function ( type ) {
19
+ Object . keys ( shaders [ type ] ) . forEach ( function ( shader ) {
20
+ gl . destroyShader ( shader )
21
+ } )
22
+ } )
23
+ shaders [ GL_FRAGMENT_SHADER ] = { }
24
+ shaders [ GL_VERTEX_SHADER ] = { }
25
+
26
+ // TODO destroy programs
22
27
}
23
28
24
29
function refreshCache ( ) {
30
+ shaders [ GL_FRAGMENT_SHADER ] = { }
31
+ shaders [ GL_VERTEX_SHADER ] = { }
32
+
33
+ // TODO recompile and link all programs
25
34
}
26
35
27
36
function getShader ( type , source ) {
@@ -38,9 +47,9 @@ module.exports = function createShaderCache (gl) {
38
47
try {
39
48
var fmt = formatCompilerError ( errLog , source , type )
40
49
} catch ( e ) {
41
- throw new REGLError ( errLog , 'Error compiling shader:\n' + errLog )
50
+ check . raiseRuntime ( errLog , 'Error compiling shader:\n' + errLog )
42
51
}
43
- throw new REGLError ( errLog , fmt . short , fmt . long )
52
+ check . raiseRuntime ( errLog , fmt . short , fmt . long )
44
53
}
45
54
cache [ source ] = shader
46
55
}
@@ -99,7 +108,7 @@ module.exports = function createShaderCache (gl) {
99
108
100
109
if ( ! gl . getProgramParameter ( program , gl . LINK_STATUS ) ) {
101
110
var errLog = gl . getProgramInfoLog ( program )
102
- throw new REGLError ( errLog , 'Error linking program:' + errLog )
111
+ check . raiseRuntime ( errLog , 'Error linking program:' + errLog )
103
112
}
104
113
105
114
// Construct result
@@ -123,11 +132,10 @@ module.exports = function createShaderCache (gl) {
123
132
return program
124
133
}
125
134
126
- // Invalidate cache
127
135
clearCache ( )
128
136
129
137
return {
130
- get : getProgram ,
138
+ create : getProgram ,
131
139
clear : clearCache ,
132
140
refresh : refreshCache
133
141
}
0 commit comments