forked from cornerstonejs/cornerstone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateProgramFromString.js
More file actions
83 lines (65 loc) · 2.36 KB
/
Copy pathcreateProgramFromString.js
File metadata and controls
83 lines (65 loc) · 2.36 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
/**
* Creates and compiles a shader.
*
* @param {!WebGLRenderingContext} gl The WebGL Context.
* @param {string} shaderSource The GLSL source code for the shader.
* @param {number} shaderType The type of shader, VERTEX_SHADER or FRAGMENT_SHADER.
*
* @return {!WebGLShader} The shader.
* @memberof WebGLRendering
*/
function compileShader (gl, shaderSource, shaderType) {
// Create the shader object
const shader = gl.createShader(shaderType);
// Set the shader source code.
gl.shaderSource(shader, shaderSource);
// Compile the shader
gl.compileShader(shader);
// Check if it compiled
const success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
if (!success && !gl.isContextLost()) {
// Something went wrong during compilation; get the error
const infoLog = gl.getShaderInfoLog(shader);
console.error(`Could not compile shader:\n${infoLog}`);
}
return shader;
}
/**
* Creates a program from 2 shaders.
*
* @param {!WebGLRenderingContext} gl The WebGL context.
* @param {!WebGLShader} vertexShader A vertex shader.
* @param {!WebGLShader} fragmentShader A fragment shader.
* @return {!WebGLProgram} A program.
* @memberof WebGLRendering
*/
function createProgram (gl, vertexShader, fragmentShader) {
// Create a program.
const program = gl.createProgram();
// Attach the shaders.
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
// Link the program.
gl.linkProgram(program);
// Check if it linked.
const success = gl.getProgramParameter(program, gl.LINK_STATUS);
if (!success && !gl.isContextLost()) {
// Something went wrong with the link
const infoLog = gl.getProgramInfoLog(program);
console.error(`WebGL program filed to link:\n${infoLog}`);
}
return program;
}
/**
* Creates a program from 2 shaders source (Strings)
* @param {!WebGLRenderingContext} gl The WebGL context.
* @param {!WebGLShader} vertexShaderSrc Vertex shader string
* @param {!WebGLShader} fragShaderSrc Fragment shader string
* @return {!WebGLProgram} A program
* @memberof WebGLRendering
*/
export default function (gl, vertexShaderSrc, fragShaderSrc) {
const vertexShader = compileShader(gl, vertexShaderSrc, gl.VERTEX_SHADER);
const fragShader = compileShader(gl, fragShaderSrc, gl.FRAGMENT_SHADER);
return createProgram(gl, vertexShader, fragShader);
}