-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathGPUMath.js
176 lines (146 loc) · 6.21 KB
/
GPUMath.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* Created by ghassaei on 2/24/16.
*/
function initGPUMath(){
var glBoilerplate = initBoilerPlate();
var canvas = document.getElementById("glcanvas");
var gl = canvas.getContext("webgl", {antialias:false}) || canvas.getContext("experimental-webgl", {antialias:false});
var ext = gl.getExtension("OES_texture_half_float") || gl.getExtension("EXT_color_buffer_half_float");
function notSupported(){
var elm = '<div id="coverImg" ' +
'style="background: url(vortexshedding.gif) no-repeat center center fixed;' +
'-webkit-background-size: cover;' +
'-moz-background-size: cover;' +
'-o-background-size: cover;' +
'background-size: cover;">'+
'</div>';
$(elm).appendTo(body);
$("#noSupportModal").modal("show");
console.warn("floating point textures are not supported on your system");
}
if (!ext) {
notSupported();
}
gl.disable(gl.DEPTH_TEST);
var maxTexturesInFragmentShader = gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS);
console.log(maxTexturesInFragmentShader + " textures max");
function GPUMath(){
this.reset();
}
GPUMath.prototype.createProgram = function(programName, vertexShader, fragmentShader){
var programs = this.programs;
var program = programs[programName];
if (program) {
console.warn("already a program with the name " + programName);
return;
}
program = glBoilerplate.createProgramFromScripts(gl, vertexShader, fragmentShader);
gl.useProgram(program);
glBoilerplate.loadVertexData(gl, program);
programs[programName] = {
program: program,
uniforms: {}
};
};
GPUMath.prototype.initTextureFromData = function(name, width, height, typeName, data, shouldReplace){
var texture = this.textures[name];
if (!shouldReplace && texture) {
console.warn("already a texture with the name " + name);
return;
}
var type;
if (typeName == "HALF_FLOAT") type = ext.HALF_FLOAT_OES;
else type = gl[typeName];
texture = glBoilerplate.makeTexture(gl, width, height, type, null);
this.textures[name] = texture;
};
GPUMath.prototype.initFrameBufferForTexture = function(textureName, shouldReplace){
if (!shouldReplace) {
var framebuffer = this.frameBuffers[textureName];
if (framebuffer) {
console.warn("framebuffer already exists for texture " + textureName);
return;
}
}
var texture = this.textures[textureName];
if (!texture){
console.warn("texture " + textureName + " does not exist");
return;
}
framebuffer = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
var check = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
if(check != gl.FRAMEBUFFER_COMPLETE){
notSupported();
}
this.frameBuffers[textureName] = framebuffer;
};
GPUMath.prototype.setUniformForProgram = function(programName, name, val, type){
if (!this.programs[programName]){
console.warn("no program with name " + programName);
return;
}
var uniforms = this.programs[programName].uniforms;
var location = uniforms[name];
if (!location) {
location = gl.getUniformLocation(this.programs[programName].program, name);
uniforms[name] = location;
}
if (type == "1f") gl.uniform1f(location, val);
else if (type == "2f") gl.uniform2f(location, val[0], val[1]);
else if (type == "3f") gl.uniform3f(location, val[0], val[1], val[2]);
else if (type == "1i") gl.uniform1i(location, val);
else {
console.warn("no uniform for type " + type);
}
};
GPUMath.prototype.setSize = function(width, height){
gl.viewport(0, 0, width, height);
// canvas.clientWidth = width;
// canvas.clientHeight = height;
};
GPUMath.prototype.setProgram = function(programName){
gl.useProgram(this.programs[programName].program);
};
GPUMath.prototype.step = function(programName, inputTextures, outputTexture){
gl.useProgram(this.programs[programName].program);
gl.bindFramebuffer(gl.FRAMEBUFFER, this.frameBuffers[outputTexture]);
for (var i=0;i<inputTextures.length;i++){
gl.activeTexture(gl.TEXTURE0 + i);
gl.bindTexture(gl.TEXTURE_2D, this.textures[inputTextures[i]]);
}
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);//draw to framebuffer
};
GPUMath.prototype.swapTextures = function(texture1Name, texture2Name){
var temp = this.textures[texture1Name];
this.textures[texture1Name] = this.textures[texture2Name];
this.textures[texture2Name] = temp;
temp = this.frameBuffers[texture1Name];
this.frameBuffers[texture1Name] = this.frameBuffers[texture2Name];
this.frameBuffers[texture2Name] = temp;
};
GPUMath.prototype.swap3Textures = function(texture1Name, texture2Name, texture3Name){
var temp = this.textures[texture3Name];
this.textures[texture3Name] = this.textures[texture2Name];
this.textures[texture2Name] = this.textures[texture1Name];
this.textures[texture1Name] = temp;
temp = this.frameBuffers[texture3Name];
this.frameBuffers[texture3Name] = this.frameBuffers[texture2Name];
this.frameBuffers[texture2Name] = this.frameBuffers[texture1Name];
this.frameBuffers[texture1Name] = temp;
};
GPUMath.prototype.readyToRead = function(){
return gl.checkFramebufferStatus(gl.FRAMEBUFFER) == gl.FRAMEBUFFER_COMPLETE;
};
GPUMath.prototype.readPixels = function(xMin, yMin, width, height, array){
gl.readPixels(xMin, yMin, width, height, gl.RGBA, gl.UNSIGNED_BYTE, array);
};
GPUMath.prototype.reset = function(){
this.programs = {};
this.frameBuffers = {};
this.textures = {};
this.index = 0;
};
return new GPUMath;
}