-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathGPGPU.js
191 lines (146 loc) · 6.84 KB
/
GPGPU.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"use strict"
class GPGPU {
constructor ({gl, height, width}) {
this.ready = false
this.height = height
this.width = width
this.attribs = {}
this.uniforms = {}
this.textures = []
this.standardGeometryVals = new Float32Array([
-1.0, 1.0, 0.0, 0.0, 1.0, // top left
-1.0, -1.0, 0.0, 0.0, 0.0, // bottom left
1.0, 1.0, 0.0, 1.0, 1.0, // top right
1.0, -1.0, 0.0, 1.0, 0.0 // bottom right
])
this.standardVertex = `
attribute vec3 position;
attribute vec2 textureCoord;
varying highp vec2 vTextureCoord;
void main() {
gl_Position = vec4(position, 1.0);
vTextureCoord = textureCoord;
}
`
if (gl) {
this.gl = gl
this.height = gl.drawingBufferHeight
this.width = gl.drawingBufferWidth
} else {
const canvas = document.createElement("canvas")
canvas.width = width
canvas.height = height
this.gl = canvas.getContext("webgl", {premultipliedAlpha: false})
}
const textureFloat = this.gl.getExtension("OES_texture_float")
if (!textureFloat) {
alert("Floating point textures not supported")
}
}
makeTexture (data, width=this.width, height=this.height) {
this.textures.push({tex: this.gl.createTexture(), width, height})
this.gl.bindTexture(this.gl.TEXTURE_2D, this.textures[this.textures.length-1].tex)
this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MIN_FILTER, this.gl.NEAREST)
this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MAG_FILTER, this.gl.NEAREST)
this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_S, this.gl.CLAMP_TO_EDGE)
this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_T, this.gl.CLAMP_TO_EDGE)
this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, width, height, 0, this.gl.RGBA, this.gl.FLOAT, data)
}
updateTexture (data, index=0) {
const tex = this.textures[index]
this.gl.bindTexture(this.gl.TEXTURE_2D, tex.tex)
this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, tex.width, tex.height, 0, this.gl.RGBA, this.gl.FLOAT, data)
}
makeFrameBuffer (width=this.width, height=this.height) {
const texture = this.gl.createTexture()
this.gl.bindTexture(this.gl.TEXTURE_2D, texture)
this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, width, height, 0, this.gl.RGBA, this.gl.FLOAT, null)
this.gl.bindTexture(this.gl.TEXTURE_2D, null)
this.framebuffer = this.gl.createFramebuffer()
this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, this.framebuffer)
this.gl.framebufferTexture2D(this.gl.FRAMEBUFFER, this.gl.COLOR_ATTACHMENT0, this.gl.TEXTURE_2D, texture, 0)
// Check the frameBuffer status
const status = this.gl.checkFramebufferStatus(this.gl.FRAMEBUFFER)
if (status != this.gl.FRAMEBUFFER_COMPLETE) {
switch (status) {
case this.gl.FRAMEBUFFER_UNSUPPORTED:
throw("FRAMEBUFFER_UNSUPPORTED")
case this.gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
throw("FRAMEBUFFER_INCOMPLETE_ATTACHMENT")
case this.gl.FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
throw("FRAMEBUFFER_INCOMPLETE_DIMENSIONS")
case this.gl.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
throw("FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT")
default:
throw(`Framebuffer error: ${status}`)
}
}
}
buildProgram (fragmentSource, vertexSource=this.standardVertex) {
this.program = this.gl.createProgram()
const vertex = this.compileShader(vertexSource, this.gl.VERTEX_SHADER)
const fragment = this.compileShader(fragmentSource, this.gl.FRAGMENT_SHADER)
this.gl.attachShader(this.program, vertex)
this.gl.attachShader(this.program, fragment)
this.gl.linkProgram(this.program)
this.gl.deleteShader(fragment)
this.gl.deleteShader(vertex)
this.gl.useProgram(this.program)
const standardGeometry = this.gl.createBuffer()
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, standardGeometry)
this.gl.bufferData(this.gl.ARRAY_BUFFER, this.standardGeometryVals, this.gl.STATIC_DRAW)
}
compileShader (source, type) {
const shader = this.gl.createShader(type)
this.gl.shaderSource(shader, source)
this.gl.compileShader(shader)
const compileStatus = this.gl.getShaderParameter(shader, this.gl.COMPILE_STATUS)
if (!compileStatus) {
throw `${type} compilation failed: ${this.gl.getShaderInfoLog(shader)}`
}
return shader
}
addAttrib (name, {numElements=3, stride=20, offset=0}={}) {
this.gl.useProgram(this.program)
this.attribs[name] = this.gl.getAttribLocation(this.program, name)
this.gl.enableVertexAttribArray(this.attribs[name])
this.gl.vertexAttribPointer(this.attribs[name], numElements, this.gl.FLOAT, this.gl.FALSE, stride, offset)
}
addUniform (name, value, type="uniform1f") {
this.uniforms[name] = this.gl.getUniformLocation(this.program, name)
if (value != undefined) {
this.gl[type](this.uniforms[name], value)
}
}
draw (texture=this.textures[this.textures.length-1]) {
this.gl.activeTexture(this.gl.TEXTURE0)
this.gl.bindTexture(this.gl.TEXTURE_2D, texture.tex)
if (!this.ready) {
this.ready = true
this.addUniform("texture0", 0, "uniform1i")
const texToAdd = texture==this.textures[this.textures.length-1] ? this.textures.length-1 : this.textures.length
for (let t=0; t<texToAdd; t++) {
this.gl.activeTexture(this.gl[`TEXTURE${t+1}`])
this.gl.bindTexture(this.gl.TEXTURE_2D, this.textures[t].tex)
this.addUniform(`texture${t+1}`, t+1, "uniform1i")
}
this.gl.activeTexture(this.gl.TEXTURE0)
}
this.gl.drawArrays(this.gl.TRIANGLE_STRIP, 0, 4)
}
getPixels (startX=0, startY=0, spanX=this.width-startX, spanY=this.height-startY) {
const buffer = new Float32Array(spanX * spanY * 4)
this.gl.readPixels(startX, startY, spanX, spanY, this.gl.RGBA, this.gl.FLOAT, buffer)
return buffer
}
delete () {
this.gl.deleteProgram(this.program)
this.gl.deleteFramebuffer(this.framebuffer)
for (let t=0; t<this.textures.length; t++) {
this.gl.deleteTexture(this.textures[t].tex)
this.textures.splice(t, 1)
}
this.gl.getExtension("WEBGL_lose_context").loseContext()
delete this.gl
}
}