forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebGLProgram.js
More file actions
115 lines (76 loc) · 2.62 KB
/
Copy pathWebGLProgram.js
File metadata and controls
115 lines (76 loc) · 2.62 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
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
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../../utils/Class');
var WebGLAttribute = require('./WebGLAttribute');
var programCount = 0;
var WebGLProgram = new Class({
initialize:
function WebGLProgram (renderer, vertexShader, fragmentShader)
{
this.id = programCount++;
this.renderer = renderer;
this.usedTimes = 1;
this.vertexShaderSrc = vertexShader;
this.fragmentShaderSrc = fragmentShader;
this.program = this.createProgram();
this.attributes = this.getAttributes();
// uniforms
},
createProgram: function ()
{
var gl = this.renderer.gl;
var vertexShader = this.createShader(this.vertexShaderSrc, gl.VERTEX_SHADER);
var fragmentShader = this.createShader(this.fragmentShaderSrc, gl.FRAGMENT_SHADER);
var program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
var linked = gl.getProgramParameter(program, gl.LINK_STATUS);
if (!linked)
{
console.warn('linkProgram failed: ', gl.getProgramInfoLog(program));
}
return program;
},
createShader: function (source, type)
{
var gl = this.renderer.gl;
var shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
var compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
if (!compiled)
{
console.warn('compileShader failed: ', gl.getShaderInfoLog(shader), this.addLineNumbers(source));
}
return shader;
},
addLineNumbers: function (string)
{
var lines = string.split('\n');
for (var i = 0; i < lines.length; i++)
{
lines[i] = (i + 1) + ': ' + lines[i];
}
return lines.join('\n');
},
getAttributes: function ()
{
var gl = this.renderer.gl;
var program = this.program;
var attributes = {};
var totalAttributes = gl.getProgramParameter(program, gl.ACTIVE_ATTRIBUTES);
for (var i = 0; i < totalAttributes; i++)
{
var attribData = gl.getActiveAttrib(program, i);
var name = attribData.name;
var attribute = new WebGLAttribute(gl, program, attribData);
attributes[name] = attribute;
}
return attributes;
}
});
module.exports = WebGLProgram;