forked from xeolabs/scenejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader.js
46 lines (36 loc) · 1.37 KB
/
shader.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
/**
* A vertex/fragment shader in a program
*
* @private
* @param gl WebGL gl
* @param gl.VERTEX_SHADER | gl.FRAGMENT_SHADER
* @param source Source code for shader
* @param logging Shader will write logging's debug channel as it compiles
*/
SceneJS._webgl.Shader = function (gl, type, source) {
/**
* True as soon as this shader is allocated and ready to go
* @type {boolean}
*/
this.allocated = false;
this.handle = gl.createShader(type);
if (!this.handle) {
throw SceneJS_error.fatalError(SceneJS.errors.OUT_OF_VRAM, "Failed to create WebGL shader");
}
gl.shaderSource(this.handle, source);
gl.compileShader(this.handle);
this.valid = (gl.getShaderParameter(this.handle, gl.COMPILE_STATUS) != 0);
if (!this.valid) {
if (!gl.isContextLost()) { // Handled explicitely elsewhere, so wont rehandle here
SceneJS.log.error("Shader program failed to compile: " + gl.getShaderInfoLog(this.handle));
SceneJS.log.error("Shader source:");
var lines = source.split('\n');
for (var j = 0; j < lines.length; j++) {
SceneJS.log.error((j + 1) + ": " + lines[j]);
}
throw SceneJS_error.fatalError(
SceneJS.errors.SHADER_COMPILATION_FAILURE, "Shader program failed to compile");
}
}
this.allocated = true;
};