-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathwebcv-gpu.js
158 lines (115 loc) · 5.45 KB
/
webcv-gpu.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
/*jslint es5: true, browser: true, devel: true */
/*global WebCV, Float32Array, Uint8Array, Uint8ClampedArray */
(function (WebCV, window, document, undefined) {
"use strict";
var gpu = function () {
// Internal functions
/**
* Set the texture parameters according to params object
*/
function setParams(gl, params) {
var flip = params.flip;
if (flip === undefined) {
flip = 1;
}
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flip);
// Support LightGL style paramater names
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, params.TEXTURE_MAG_FILTER || params.filter || params.magFilter || gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, params.TEXTURE_MIN_FILTER || params.filter || params.minFilter || gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, params.wrap || params.TEXTURE_WRAP_S || params.wrapS || gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, params.wrap || params.TEXTURE_WRAP_T || params.wrapT || gl.CLAMP_TO_EDGE);
}
/**
* Upload texture from image or array, using the right texImage2D function
*/
function texImage(gl, pixels, params) {
// w,h only needed in case of array
var internalFormat,
format,
type;
internalFormat = params.internalFormat || params.format || gl.RGBA;
format = params.format || gl.RGBA;
type = params.type || gl.UNSIGNED_BYTE;
/*
There are 2 main versions of texImage2D, depending if we are using a raw array, or an image-like element.
For the array width and height are needed.
Spec:
void texImage2D(GLenum target, GLint level, GLenum internalformat,
GLsizei width, GLsizei height, GLint border, GLenum format,
GLenum type, ArrayBufferView pixels);
"If pixels is null, a buffer of sufficient size initialized to 0 is passed. "
void texImage2D(GLenum target, GLint level, GLenum internalformat, GLenum format, GLenum type, ImageData pixels)
*/
// Bad way to check if Array, since "instanceof ArrayBufferView" not currently exposed (it is in latest spec)
if (pixels === null || Object.prototype.toString.call(pixels).indexOf("Array") !== -1) {
if (params.width !== undefined && params.height !== undefined) {
//console.log(pixels);
gl.texImage2D(gl.TEXTURE_2D, 0, internalFormat, params.width, params.height, 0, format, type, pixels);
} else {
throw "Width and height not specified in params, and using Array";
}
} else {
gl.texImage2D(gl.TEXTURE_2D, 0, internalFormat, format, type, pixels);
}
}
// Public functions
return {
blankTexture: function (w, h, params) {
return this.uploadArrayToTexture(null, null, w, h, params);
},
uploadArrayToTexture: function (array, texture, w, h, params) {
params = params || {};
params.width = w;
params.height = h;
return this.uploadToTexture(array, texture, params);
},
uploadToTexture: function (pixels, texture, params) {
var gl = this.core.gl,
// Save current texture binding
oldTexture = gl.getParameter(gl.TEXTURE_BINDING_2D);
params = params || {};
texture = texture || gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
setParams(gl, params);
texImage(gl, pixels, params);
// Restore previous texture binding
gl.bindTexture(gl.TEXTURE_2D, oldTexture);
return texture;
},
downloadFramebuffer: function (fb, outArray, params) {
var gl = this.core.gl,
oldfb = gl.getParameter(gl.FRAMEBUFFER_BINDING),
view = gl.getParameter(gl.VIEWPORT),
x = 0,
y = 0,
w = view[2],
h = view[3],
format = gl.RGBA,
type = gl.UNSIGNED_BYTE;
params = params || {};
if (params.x !== undefined) {
x = params.x;
}
if (params.y !== undefined) {
y = params.y;
}
if (params.width !== undefined) {
w = params.width;
}
if (params.height !== undefined) {
h = params.height;
}
format = params.format || format;
type = params.type || type;
gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
if (outArray === undefined || outArray === null) {
outArray = new Uint8Array(w * h * 4);
}
gl.readPixels(x, y, w, h, format, type, outArray);
gl.bindFramebuffer(gl.FRAMEBUFFER, oldfb);
return outArray;
}
};
};
WebCV.registerModule("gpu", gpu);
}(WebCV, window, document));