|
| 1 | +/** |
| 2 | +* WebCLGLBuffer Object |
| 3 | +* @class |
| 4 | +* @constructor |
| 5 | +* @property {WebGLTexture} textureData |
| 6 | +* @property {Array<Float>} inData Original array |
| 7 | +* @property {Int} [offset=0] offset of buffer |
| 8 | +*/ |
| 9 | +WebCLGLBufferItem = function(gl, length, type, offset, linear, mode) { |
| 10 | + this.gl = gl; |
| 11 | + |
| 12 | + if(length.constructor === Array) { |
| 13 | + this.length = length[0]*length[1]; |
| 14 | + this.W = length[0]; |
| 15 | + this.H = length[1]; |
| 16 | + } else { |
| 17 | + this.length = length; |
| 18 | + this.W = Math.ceil(Math.sqrt(this.length)); |
| 19 | + this.H = this.W; |
| 20 | + } |
| 21 | + this.utils = new WebCLGLUtils(); |
| 22 | + |
| 23 | + this.type = (type != undefined) ? type : 'FLOAT'; |
| 24 | + this._supportFormat = this.gl.FLOAT; |
| 25 | + //this._supportFormat = this.gl.UNSIGNED_BYTE; |
| 26 | + |
| 27 | + this.offset = (offset != undefined) ? offset : 0; |
| 28 | + this.linear = (linear != undefined && linear == true) ? true : false; |
| 29 | + |
| 30 | + this.inData; // enqueueWriteBuffer user data |
| 31 | + |
| 32 | + this.mode = (mode != undefined) ? mode : "FRAGMENT"; // "FRAGMENT", "VERTEX", "VERTEX_INDEX", "VERTEX_FROM_KERNEL", "VERTEX_AND_FRAGMENT" |
| 33 | + |
| 34 | + // readPixel arrays |
| 35 | + this.outArray4Uint8ArrayX = new Uint8Array((this.W*this.H)*4); |
| 36 | + this.outArray4Uint8ArrayY = new Uint8Array((this.W*this.H)*4); |
| 37 | + this.outArray4Uint8ArrayZ = new Uint8Array((this.W*this.H)*4); |
| 38 | + this.outArray4Uint8ArrayW = new Uint8Array((this.W*this.H)*4); |
| 39 | + /*this.outArray4x4Uint8Array = new Uint8Array((this.W*this.H)*4*4);*/ |
| 40 | + |
| 41 | + this.Packet4Uint8Array_Float = []; // [this.outArray4Uint8ArrayX] |
| 42 | + this.Float = []; // [unpack(this.outArray4Uint8ArrayX)] |
| 43 | + this.Packet4Uint8Array_Float4 = []; // [this.outArray4Uint8ArrayX, ..Y, ..Z, ..W] |
| 44 | + this.Float4 = []; // [unpack(this.outArray4Uint8ArrayX), unpack(..Y), unpack(..Z), unpack(..W)] |
| 45 | + |
| 46 | + |
| 47 | + // Create FrameBuffer & RenderBuffer |
| 48 | + this.rBuffer = this.gl.createRenderbuffer(); |
| 49 | + this.gl.bindRenderbuffer(this.gl.RENDERBUFFER, this.rBuffer); |
| 50 | + this.gl.renderbufferStorage(this.gl.RENDERBUFFER, this.gl.DEPTH_COMPONENT16, this.W, this.H); |
| 51 | + this.gl.bindRenderbuffer(this.gl.RENDERBUFFER, null); |
| 52 | + |
| 53 | + this.fBuffer = this.gl.createFramebuffer(); |
| 54 | + this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, this.fBuffer); |
| 55 | + this.gl.framebufferRenderbuffer(this.gl.FRAMEBUFFER, this.gl.DEPTH_ATTACHMENT, this.gl.RENDERBUFFER, this.rBuffer); |
| 56 | + |
| 57 | + if(this.mode == "FRAGMENT" || this.mode == "VERTEX_FROM_KERNEL" || this.mode == "VERTEX_AND_FRAGMENT") { |
| 58 | + // Create WebGLTexture buffer |
| 59 | + this.textureData = this.createWebGLTextureBuffer(); |
| 60 | + } |
| 61 | + if(this.mode == "VERTEX" || this.mode == "VERTEX_INDEX" || this.mode == "VERTEX_FROM_KERNEL" || this.mode == "VERTEX_AND_FRAGMENT") { |
| 62 | + // Create WebGL buffer |
| 63 | + this.vertexData0 = this.createWebGLBuffer(); |
| 64 | + } |
| 65 | +}; |
| 66 | + |
| 67 | +/** |
| 68 | +* Create the WebGLTexture buffer |
| 69 | +* @type Void |
| 70 | + */ |
| 71 | +WebCLGLBufferItem.prototype.createWebGLTextureBuffer = function() { |
| 72 | + this.gl.pixelStorei(this.gl.UNPACK_FLIP_Y_WEBGL, false); |
| 73 | + this.gl.pixelStorei(this.gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); |
| 74 | + |
| 75 | + var textureData = this.gl.createTexture(); |
| 76 | + this.gl.bindTexture(this.gl.TEXTURE_2D, textureData); |
| 77 | + if(this.linear != undefined && this.linear == true) { |
| 78 | + this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.W,this.H, 0, this.gl.RGBA, this._supportFormat, null); |
| 79 | + this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MAG_FILTER, this.gl.LINEAR); |
| 80 | + this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MIN_FILTER, this.gl.LINEAR_MIPMAP_NEAREST); |
| 81 | + this.gl.generateMipmap(this.gl.TEXTURE_2D); |
| 82 | + } else { |
| 83 | + this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.W,this.H, 0, this.gl.RGBA, this._supportFormat, null); |
| 84 | + this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MAG_FILTER, this.gl.NEAREST); |
| 85 | + this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MIN_FILTER, this.gl.NEAREST); |
| 86 | + this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_S, this.gl.CLAMP_TO_EDGE); |
| 87 | + this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_T, this.gl.CLAMP_TO_EDGE); |
| 88 | + } |
| 89 | + |
| 90 | + return textureData; |
| 91 | +}; |
| 92 | + |
| 93 | +/** |
| 94 | +* Create the WebGL buffer |
| 95 | +* @type Void |
| 96 | + */ |
| 97 | +WebCLGLBufferItem.prototype.createWebGLBuffer = function() { |
| 98 | + var vertexData = this.gl.createBuffer(); |
| 99 | + |
| 100 | + return vertexData; |
| 101 | +}; |
| 102 | + |
| 103 | +/** |
| 104 | +* Write WebGLTexture buffer |
| 105 | +* @param {Array|Float32Array|Uint8Array|WebGLTexture|HTMLImageElement} array |
| 106 | +* @param {Bool} [flip=false] |
| 107 | +* @type Void |
| 108 | + */ |
| 109 | +WebCLGLBufferItem.prototype.writeWebGLTextureBuffer = function(arr, flip) { |
| 110 | + this.inData = arr; |
| 111 | + |
| 112 | + if(arr instanceof WebGLTexture) this.textureData = arr; |
| 113 | + else { |
| 114 | + if(flip == false || flip == undefined) |
| 115 | + this.gl.pixelStorei(this.gl.UNPACK_FLIP_Y_WEBGL, false); |
| 116 | + else |
| 117 | + this.gl.pixelStorei(this.gl.UNPACK_FLIP_Y_WEBGL, true); |
| 118 | + this.gl.pixelStorei(this.gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); |
| 119 | + this.gl.bindTexture(this.gl.TEXTURE_2D, this.textureData); |
| 120 | + if(arr instanceof HTMLImageElement) { |
| 121 | + this.inData = this.utils.getUint8ArrayFromHTMLImageElement(arr); |
| 122 | + //texImage2D( target, level, internalformat, format, type, TexImageSource); |
| 123 | + if(this.type == 'FLOAT4') { |
| 124 | + this.gl.texImage2D( this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.gl.RGBA, this.gl.FLOAT, arr); |
| 125 | + }/* else if(this.type == 'INT4') { |
| 126 | + this.gl.texImage2D( this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.gl.RGBA, this.gl.UNSIGNED_BYTE, arr); |
| 127 | + }*/ |
| 128 | + } else { |
| 129 | + //console.log("Write arr with length of "+arr.length+" in Buffer "+this.type+" with length of "+this.length+" (W: "+this.W+"; H: "+this.H+")"); |
| 130 | + |
| 131 | + if(this.type == 'FLOAT4') { |
| 132 | + var arrt = new Float32Array((this.W*this.H)*4); |
| 133 | + for(var n=0; n < arr.length; n++) arrt[n] = arr[n]; |
| 134 | + //texImage2D( target, level, internalformat, width, height, border, format, type, pixels); |
| 135 | + if(arr instanceof Uint8Array) { |
| 136 | + this.gl.texImage2D( this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.W, this.H, 0, this.gl.RGBA, this.gl.FLOAT, arrt); |
| 137 | + } else if(arr instanceof Float32Array) { |
| 138 | + this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.W, this.H, 0, this.gl.RGBA, this.gl.FLOAT, arrt); |
| 139 | + } else { |
| 140 | + this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.W, this.H, 0, this.gl.RGBA, this.gl.FLOAT, arrt); |
| 141 | + } |
| 142 | + } else if(this.type == 'FLOAT') { |
| 143 | + var arrayTemp = new Float32Array(this.W*this.H*4); |
| 144 | + |
| 145 | + for(var n = 0, f = this.W*this.H; n < f; n++) { |
| 146 | + var idd = n*4; |
| 147 | + arrayTemp[idd] = arr[n]; |
| 148 | + arrayTemp[idd+1] = 0.0; |
| 149 | + arrayTemp[idd+2] = 0.0; |
| 150 | + arrayTemp[idd+3] = 0.0; |
| 151 | + } |
| 152 | + arr = arrayTemp; |
| 153 | + this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.W, this.H, 0, this.gl.RGBA, this.gl.FLOAT, arr); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + if(this.linear) this.gl.generateMipmap(this.gl.TEXTURE_2D); |
| 158 | +}; |
| 159 | + |
| 160 | +/** |
| 161 | +* Write WebGL buffer |
| 162 | +* @param {Array|Float32Array|Uint8Array|WebGLTexture|HTMLImageElement} array |
| 163 | +* @param {Bool} [flip=false] |
| 164 | +* @type Void |
| 165 | + */ |
| 166 | +WebCLGLBufferItem.prototype.writeWebGLBuffer = function(arr, flip) { |
| 167 | + this.inData = arr; |
| 168 | + if(this.mode == "VERTEX_INDEX") { // "VERTEX_INDEX" ELEMENT_ARRAY_BUFFER |
| 169 | + this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, this.vertexData0); |
| 170 | + this.gl.bufferData(this.gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(arr), this.gl.DYNAMIC_DRAW); |
| 171 | + } else { // "VERTEX" || "VERTEX_AND_FRAGMENT" ARRAY_BUFFER |
| 172 | + this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.vertexData0); |
| 173 | + this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(arr), this.gl.DYNAMIC_DRAW); |
| 174 | + } |
| 175 | +}; |
| 176 | + |
| 177 | +/** |
| 178 | +* Remove this buffer |
| 179 | +* @type Void |
| 180 | + */ |
| 181 | +WebCLGLBufferItem.prototype.remove = function() { |
| 182 | + this.gl.deleteRenderbuffer(this.rBuffer); |
| 183 | + this.gl.deleteFramebuffer(this.fBuffer); |
| 184 | + |
| 185 | + if(this.mode == "FRAGMENT" || this.mode == "VERTEX_FROM_KERNEL" || this.mode == "VERTEX_AND_FRAGMENT") |
| 186 | + this.gl.deleteTexture(this.textureData); |
| 187 | + |
| 188 | + if(this.mode == "VERTEX" || this.mode == "VERTEX_INDEX" || this.mode == "VERTEX_FROM_KERNEL" || this.mode == "VERTEX_AND_FRAGMENT") |
| 189 | + this.gl.deleteBuffer(this.vertexData0); |
| 190 | +}; |
0 commit comments