-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathshader-core.js
129 lines (110 loc) · 3.25 KB
/
shader-core.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
'use strict'
var createUniformWrapper = require('./lib/create-uniforms')
var createAttributeWrapper = require('./lib/create-attributes')
var makeReflect = require('./lib/reflect')
//Shader object
function Shader(gl, prog, vertShader, fragShader) {
this.gl = gl
this.handle = prog
this.attributes = null
this.uniforms = null
this.types = null
this.vertexShader = vertShader
this.fragmentShader = fragShader
}
//Binds the shader
Shader.prototype.bind = function() {
this.gl.useProgram(this.handle)
}
//Destroy shader, release resources
Shader.prototype.dispose = function() {
var gl = this.gl
gl.deleteShader(this.vertexShader)
gl.deleteShader(this.fragmentShader)
gl.deleteProgram(this.handle)
}
Shader.prototype.updateExports = function(uniforms, attributes) {
var locations = new Array(uniforms.length)
var program = this.handle
var gl = this.gl
var doLink = relinkUniforms.bind(void 0,
gl,
program,
locations,
uniforms
)
doLink()
this.types = {
uniforms: makeReflect(uniforms),
attributes: makeReflect(attributes)
}
this.attributes = createAttributeWrapper(
gl,
program,
attributes,
doLink
)
Object.defineProperty(this, 'uniforms', createUniformWrapper(
gl,
program,
uniforms,
locations
))
}
//Relinks all uniforms
function relinkUniforms(gl, program, locations, uniforms) {
for(var i=0; i<uniforms.length; ++i) {
locations[i] = gl.getUniformLocation(program, uniforms[i].name)
}
}
//Compiles and links a shader program with the given attribute and vertex list
function createShader(
gl
, vertSource
, fragSource
, uniforms
, attributes) {
//Compile vertex shader
var vertShader = gl.createShader(gl.VERTEX_SHADER)
gl.shaderSource(vertShader, vertSource)
gl.compileShader(vertShader)
if(!gl.getShaderParameter(vertShader, gl.COMPILE_STATUS)) {
var errLog = gl.getShaderInfoLog(vertShader)
console.error('gl-shader: Error compling vertex shader:', errLog)
throw new Error('gl-shader: Error compiling vertex shader:' + errLog)
}
//Compile fragment shader
var fragShader = gl.createShader(gl.FRAGMENT_SHADER)
gl.shaderSource(fragShader, fragSource)
gl.compileShader(fragShader)
if(!gl.getShaderParameter(fragShader, gl.COMPILE_STATUS)) {
var errLog = gl.getShaderInfoLog(fragShader)
console.error('gl-shader: Error compiling fragment shader:', errLog)
throw new Error('gl-shader: Error compiling fragment shader:' + errLog)
}
//Link program
var program = gl.createProgram()
gl.attachShader(program, fragShader)
gl.attachShader(program, vertShader)
//Optional default attriubte locations
attributes.forEach(function(a) {
if (typeof a.location === 'number')
gl.bindAttribLocation(program, a.location, a.name)
})
gl.linkProgram(program)
if(!gl.getProgramParameter(program, gl.LINK_STATUS)) {
var errLog = gl.getProgramInfoLog(program)
console.error('gl-shader: Error linking shader program:', errLog)
throw new Error('gl-shader: Error linking shader program:' + errLog)
}
//Return final linked shader object
var shader = new Shader(
gl,
program,
vertShader,
fragShader
)
shader.updateExports(uniforms, attributes)
return shader
}
module.exports = createShader