diff --git a/GPUMath.js b/GPUMath.js new file mode 100644 index 0000000000000000000000000000000000000000..fb9d499b73dc4359b57ff372083e5e05d165be72 --- /dev/null +++ b/GPUMath.js @@ -0,0 +1,147 @@ +/** + * 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}); + gl.getExtension('OES_texture_float'); + 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; + } + texture = glBoilerplate.makeTexture(gl, width, height, gl[typeName], data); + this.textures[name] = texture; + }; + + + + GPUMath.prototype.initFrameBufferForTexture = function(textureName){ + 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); + 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 precision mediump float; - uniform sampler2D u_image; - uniform vec2 u_textureSize; + //uniform sampler2D u_velocities; + //uniform vec2 u_textureSize; + + void main() { + gl_FragColor = vec4(1, 0, 1, 1); + } + + + @@ -74,7 +96,8 @@ - + + @@ -89,9 +112,10 @@