diff --git a/src/core/geometry/Buffer.js b/src/core/geometry/Buffer.js index 782d2c2..15c656b 100644 --- a/src/core/geometry/Buffer.js +++ b/src/core/geometry/Buffer.js @@ -12,7 +12,7 @@ /** * @param {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} data the data to store in the buffer. */ - constructor(data) + constructor(data, _static = true, index = false) { /** * The data in the buffer, as a typed array @@ -31,9 +31,9 @@ this._updateID = 0; - this.index = false; + this.index = index; - this.static = true; + this.static = _static; this.id = UID++; } diff --git a/src/core/geometry/Buffer.js b/src/core/geometry/Buffer.js index 782d2c2..15c656b 100644 --- a/src/core/geometry/Buffer.js +++ b/src/core/geometry/Buffer.js @@ -12,7 +12,7 @@ /** * @param {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} data the data to store in the buffer. */ - constructor(data) + constructor(data, _static = true, index = false) { /** * The data in the buffer, as a typed array @@ -31,9 +31,9 @@ this._updateID = 0; - this.index = false; + this.index = index; - this.static = true; + this.static = _static; this.id = UID++; } diff --git a/src/core/renderers/webgl/systems/geometry/GeometrySystem.js b/src/core/renderers/webgl/systems/geometry/GeometrySystem.js index 4b948c8..ef7570c 100644 --- a/src/core/renderers/webgl/systems/geometry/GeometrySystem.js +++ b/src/core/renderers/webgl/systems/geometry/GeometrySystem.js @@ -71,13 +71,14 @@ // TODO - optimise later! // don't need to loop through if nothing changed! // maybe look to add an 'autoupdate' to geometry? - this.updateBuffers(geometry); + this.updateBuffers(); } - updateBuffers(geometry) + updateBuffers() { + const geometry = this._activeGeometry; const gl = this.gl; for (let i = 0; i < geometry.buffers.length; i++) diff --git a/src/core/geometry/Buffer.js b/src/core/geometry/Buffer.js index 782d2c2..15c656b 100644 --- a/src/core/geometry/Buffer.js +++ b/src/core/geometry/Buffer.js @@ -12,7 +12,7 @@ /** * @param {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} data the data to store in the buffer. */ - constructor(data) + constructor(data, _static = true, index = false) { /** * The data in the buffer, as a typed array @@ -31,9 +31,9 @@ this._updateID = 0; - this.index = false; + this.index = index; - this.static = true; + this.static = _static; this.id = UID++; } diff --git a/src/core/renderers/webgl/systems/geometry/GeometrySystem.js b/src/core/renderers/webgl/systems/geometry/GeometrySystem.js index 4b948c8..ef7570c 100644 --- a/src/core/renderers/webgl/systems/geometry/GeometrySystem.js +++ b/src/core/renderers/webgl/systems/geometry/GeometrySystem.js @@ -71,13 +71,14 @@ // TODO - optimise later! // don't need to loop through if nothing changed! // maybe look to add an 'autoupdate' to geometry? - this.updateBuffers(geometry); + this.updateBuffers(); } - updateBuffers(geometry) + updateBuffers() { + const geometry = this._activeGeometry; const gl = this.gl; for (let i = 0; i < geometry.buffers.length; i++) diff --git a/src/core/sprites/webgl/SpriteRenderer.js b/src/core/sprites/webgl/SpriteRenderer.js index 5b486ba..d5f644f 100644 --- a/src/core/sprites/webgl/SpriteRenderer.js +++ b/src/core/sprites/webgl/SpriteRenderer.js @@ -7,6 +7,8 @@ import Buffer from './BatchBuffer'; import settings from '../../settings'; import bitTwiddle from 'bit-twiddle'; +import Geometry from '../../geometry/Geometry'; +import Buffer_GEOM from '../../geometry/Buffer'; let TICK = 0; let TEXTURE_TICK = 0; @@ -65,6 +67,7 @@ * @member {Uint16Array} */ this.indices = createIndicesForQuads(this.size); + this.indexBuffer = new Buffer_GEOM(this.indices, true, true); /** * The default shaders that is used if a sprite doesn't have a more specific one. @@ -127,38 +130,23 @@ shader.uniformGroup.add('default', {uSamplers:sampleValues}, true);//this.renderer.globalUniforms; shader.uniforms.globals = this.renderer.globalUniforms; - this.indexBuffer = GLBuffer.createIndexBuffer(gl, this.indices, gl.STATIC_DRAW); - // we use the second shader as the first one depending on your browser may omit aTextureId // as it is not used by the shader so is optimized out. - - this.renderer.geometry.bindVao(null); - for (let i = 0; i < this.vaoMax; i++) { - this.vertexBuffers[i] = GLBuffer.createVertexBuffer(gl, null, gl.DYNAMIC_DRAW); + var buffer = new Buffer_GEOM(null, false); + /* eslint-disable max-len */ - - var attributeData = shader.program.attributeData; - // build the vao object that will render.. - this.vaos[i] = this.renderer.geometry.createVao() - .addIndex(this.indexBuffer) - .addAttribute(this.vertexBuffers[i], attributeData.aVertexPosition, gl.FLOAT, false, this.vertByteSize, 0) - .addAttribute(this.vertexBuffers[i], attributeData.aTextureCoord, gl.UNSIGNED_SHORT, true, this.vertByteSize, 2 * 4) - .addAttribute(this.vertexBuffers[i], attributeData.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4); - - if (attributeData.aTextureId) - { - this.vaos[i].addAttribute(this.vertexBuffers[i], attributeData.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); - } - + this.vaos[i] = new Geometry() + .addAttribute('aVertexPosition', buffer, 2, false, gl.FLOAT) + .addAttribute('aTextureCoord', buffer, 2, true, gl.UNSIGNED_SHORT) + .addAttribute('aColor', buffer, 4, true, gl.UNSIGNED_BYTE) + .addAttribute('aTextureId', buffer, 1, true, gl.FLOAT) + .addIndex(this.indexBuffer) /* eslint-enable max-len */ + + this.vertexBuffers[i] = buffer; } - - this.vao = this.vaos[0]; - this.currentBlendMode = 99999; - - this.boundTextures = new Array(this.MAX_TEXTURES); } /** @@ -221,9 +209,7 @@ const float32View = buffer.float32View; const uint32View = buffer.uint32View; - const boundTextures = this.boundTextures; - const rendererBoundTextures = this.renderer.boundTextures; - const touch = 0//this.renderer.textureGC.count; + const touch = 0;//this.renderer.textureGC.count; let index = 0; let nextTexture; @@ -244,13 +230,6 @@ let i; - // copy textures.. - for (i = 0; i < MAX_TEXTURES; ++i) - { - boundTextures[i] = rendererBoundTextures[i]; - boundTextures[i]._virtalBoundId = i; - } - for (i = 0; i < this.currentIndex; ++i) { // upload the sprite elemetns... @@ -384,6 +363,7 @@ .addAttribute(this.vertexBuffers[this.vertexCount], attributeData.aTextureCoord, gl.UNSIGNED_SHORT, true, this.vertByteSize, 2 * 4) .addAttribute(this.vertexBuffers[this.vertexCount], attributeData.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4); + if (attributeData.aTextureId) { this.vaos[this.vertexCount].addAttribute(this.vertexBuffers[this.vertexCount], attributeData.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); @@ -401,7 +381,9 @@ else { // lets use the faster option, always use buffer number 0 - this.vertexBuffers[this.vertexCount].upload(buffer.vertices, 0, true); + this.vertexBuffers[this.vertexCount].update(buffer.vertices, 0); + + this.renderer.geometry.updateBuffers(); } /// render the groups.. @@ -409,15 +391,6 @@ var group = groups[i]; var groupTextureCount = group.textureCount; - //shader = this.shaders[groupTextureCount-1]; - - //if(!shader) - // { - // shader = this.shaders[groupTextureCount-1] = generateMultiTextureShader(gl, groupTextureCount); - //console.log("SHADER generated for " + textureCount + " textures") - //} - - /// this.renderer.shader.bind(shader); for (var j = 0; j < groupTextureCount; j++) { @@ -425,38 +398,11 @@ } // set the blend mode.. - //this.renderer.state.setBlendMode( group.blend ); + this.renderer.state.setBlendMode( group.blend ); gl.drawElements(gl.TRIANGLES, group.size * 6, gl.UNSIGNED_SHORT, group.start * 6 * 2); } -/* - // render the groups.. - for (i = 0; i < groupCount; ++i) - { - const group = groups[i]; - const groupTextureCount = group.textureCount; - for (let j = 0; j < groupTextureCount; j++) - { - currentTexture = group.textures[j]; - - // reset virtual ids.. - // lets do a quick check.. - if (rendererBoundTextures[group.ids[j]] !== currentTexture) - { - this.renderer.texture.bind(currentTexture, group.ids[j], true); - } - - // reset the virtualId.. - currentTexture._virtalBoundId = -1; - } - - // set the blend mode.. - this.renderer.state.setBlendMode(group.blend); - - gl.drawElements(gl.TRIANGLES, group.size * 6, gl.UNSIGNED_SHORT, group.start * 6 * 2); - } -*/ // reset elements for the next flush this.currentIndex = 0; } @@ -467,15 +413,13 @@ start() { // this.renderer._bindGLShader(this.shader); - this.renderer.shader.bind(this.shader, true); + const glShader = this.renderer.shader.bind(this.shader, true); this.renderer.shader.syncUniformGroup(this.shader.uniformGroup); if (settings.CAN_UPLOAD_SAME_BUFFER) { // bind buffer #0, we don't need others - this.renderer.geometry.bindVao(this.vaos[this.vertexCount]); - - this.vertexBuffers[this.vertexCount].bind(); + this.renderer.geometry.bind(this.vaos[this.vertexCount], glShader); } } diff --git a/src/core/geometry/Buffer.js b/src/core/geometry/Buffer.js index 782d2c2..15c656b 100644 --- a/src/core/geometry/Buffer.js +++ b/src/core/geometry/Buffer.js @@ -12,7 +12,7 @@ /** * @param {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} data the data to store in the buffer. */ - constructor(data) + constructor(data, _static = true, index = false) { /** * The data in the buffer, as a typed array @@ -31,9 +31,9 @@ this._updateID = 0; - this.index = false; + this.index = index; - this.static = true; + this.static = _static; this.id = UID++; } diff --git a/src/core/renderers/webgl/systems/geometry/GeometrySystem.js b/src/core/renderers/webgl/systems/geometry/GeometrySystem.js index 4b948c8..ef7570c 100644 --- a/src/core/renderers/webgl/systems/geometry/GeometrySystem.js +++ b/src/core/renderers/webgl/systems/geometry/GeometrySystem.js @@ -71,13 +71,14 @@ // TODO - optimise later! // don't need to loop through if nothing changed! // maybe look to add an 'autoupdate' to geometry? - this.updateBuffers(geometry); + this.updateBuffers(); } - updateBuffers(geometry) + updateBuffers() { + const geometry = this._activeGeometry; const gl = this.gl; for (let i = 0; i < geometry.buffers.length; i++) diff --git a/src/core/sprites/webgl/SpriteRenderer.js b/src/core/sprites/webgl/SpriteRenderer.js index 5b486ba..d5f644f 100644 --- a/src/core/sprites/webgl/SpriteRenderer.js +++ b/src/core/sprites/webgl/SpriteRenderer.js @@ -7,6 +7,8 @@ import Buffer from './BatchBuffer'; import settings from '../../settings'; import bitTwiddle from 'bit-twiddle'; +import Geometry from '../../geometry/Geometry'; +import Buffer_GEOM from '../../geometry/Buffer'; let TICK = 0; let TEXTURE_TICK = 0; @@ -65,6 +67,7 @@ * @member {Uint16Array} */ this.indices = createIndicesForQuads(this.size); + this.indexBuffer = new Buffer_GEOM(this.indices, true, true); /** * The default shaders that is used if a sprite doesn't have a more specific one. @@ -127,38 +130,23 @@ shader.uniformGroup.add('default', {uSamplers:sampleValues}, true);//this.renderer.globalUniforms; shader.uniforms.globals = this.renderer.globalUniforms; - this.indexBuffer = GLBuffer.createIndexBuffer(gl, this.indices, gl.STATIC_DRAW); - // we use the second shader as the first one depending on your browser may omit aTextureId // as it is not used by the shader so is optimized out. - - this.renderer.geometry.bindVao(null); - for (let i = 0; i < this.vaoMax; i++) { - this.vertexBuffers[i] = GLBuffer.createVertexBuffer(gl, null, gl.DYNAMIC_DRAW); + var buffer = new Buffer_GEOM(null, false); + /* eslint-disable max-len */ - - var attributeData = shader.program.attributeData; - // build the vao object that will render.. - this.vaos[i] = this.renderer.geometry.createVao() - .addIndex(this.indexBuffer) - .addAttribute(this.vertexBuffers[i], attributeData.aVertexPosition, gl.FLOAT, false, this.vertByteSize, 0) - .addAttribute(this.vertexBuffers[i], attributeData.aTextureCoord, gl.UNSIGNED_SHORT, true, this.vertByteSize, 2 * 4) - .addAttribute(this.vertexBuffers[i], attributeData.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4); - - if (attributeData.aTextureId) - { - this.vaos[i].addAttribute(this.vertexBuffers[i], attributeData.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); - } - + this.vaos[i] = new Geometry() + .addAttribute('aVertexPosition', buffer, 2, false, gl.FLOAT) + .addAttribute('aTextureCoord', buffer, 2, true, gl.UNSIGNED_SHORT) + .addAttribute('aColor', buffer, 4, true, gl.UNSIGNED_BYTE) + .addAttribute('aTextureId', buffer, 1, true, gl.FLOAT) + .addIndex(this.indexBuffer) /* eslint-enable max-len */ + + this.vertexBuffers[i] = buffer; } - - this.vao = this.vaos[0]; - this.currentBlendMode = 99999; - - this.boundTextures = new Array(this.MAX_TEXTURES); } /** @@ -221,9 +209,7 @@ const float32View = buffer.float32View; const uint32View = buffer.uint32View; - const boundTextures = this.boundTextures; - const rendererBoundTextures = this.renderer.boundTextures; - const touch = 0//this.renderer.textureGC.count; + const touch = 0;//this.renderer.textureGC.count; let index = 0; let nextTexture; @@ -244,13 +230,6 @@ let i; - // copy textures.. - for (i = 0; i < MAX_TEXTURES; ++i) - { - boundTextures[i] = rendererBoundTextures[i]; - boundTextures[i]._virtalBoundId = i; - } - for (i = 0; i < this.currentIndex; ++i) { // upload the sprite elemetns... @@ -384,6 +363,7 @@ .addAttribute(this.vertexBuffers[this.vertexCount], attributeData.aTextureCoord, gl.UNSIGNED_SHORT, true, this.vertByteSize, 2 * 4) .addAttribute(this.vertexBuffers[this.vertexCount], attributeData.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4); + if (attributeData.aTextureId) { this.vaos[this.vertexCount].addAttribute(this.vertexBuffers[this.vertexCount], attributeData.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); @@ -401,7 +381,9 @@ else { // lets use the faster option, always use buffer number 0 - this.vertexBuffers[this.vertexCount].upload(buffer.vertices, 0, true); + this.vertexBuffers[this.vertexCount].update(buffer.vertices, 0); + + this.renderer.geometry.updateBuffers(); } /// render the groups.. @@ -409,15 +391,6 @@ var group = groups[i]; var groupTextureCount = group.textureCount; - //shader = this.shaders[groupTextureCount-1]; - - //if(!shader) - // { - // shader = this.shaders[groupTextureCount-1] = generateMultiTextureShader(gl, groupTextureCount); - //console.log("SHADER generated for " + textureCount + " textures") - //} - - /// this.renderer.shader.bind(shader); for (var j = 0; j < groupTextureCount; j++) { @@ -425,38 +398,11 @@ } // set the blend mode.. - //this.renderer.state.setBlendMode( group.blend ); + this.renderer.state.setBlendMode( group.blend ); gl.drawElements(gl.TRIANGLES, group.size * 6, gl.UNSIGNED_SHORT, group.start * 6 * 2); } -/* - // render the groups.. - for (i = 0; i < groupCount; ++i) - { - const group = groups[i]; - const groupTextureCount = group.textureCount; - for (let j = 0; j < groupTextureCount; j++) - { - currentTexture = group.textures[j]; - - // reset virtual ids.. - // lets do a quick check.. - if (rendererBoundTextures[group.ids[j]] !== currentTexture) - { - this.renderer.texture.bind(currentTexture, group.ids[j], true); - } - - // reset the virtualId.. - currentTexture._virtalBoundId = -1; - } - - // set the blend mode.. - this.renderer.state.setBlendMode(group.blend); - - gl.drawElements(gl.TRIANGLES, group.size * 6, gl.UNSIGNED_SHORT, group.start * 6 * 2); - } -*/ // reset elements for the next flush this.currentIndex = 0; } @@ -467,15 +413,13 @@ start() { // this.renderer._bindGLShader(this.shader); - this.renderer.shader.bind(this.shader, true); + const glShader = this.renderer.shader.bind(this.shader, true); this.renderer.shader.syncUniformGroup(this.shader.uniformGroup); if (settings.CAN_UPLOAD_SAME_BUFFER) { // bind buffer #0, we don't need others - this.renderer.geometry.bindVao(this.vaos[this.vertexCount]); - - this.vertexBuffers[this.vertexCount].bind(); + this.renderer.geometry.bind(this.vaos[this.vertexCount], glShader); } } diff --git a/src/mesh/Mesh.js b/src/mesh/Mesh.js index dd11031..ec46e24 100644 --- a/src/mesh/Mesh.js +++ b/src/mesh/Mesh.js @@ -1,5 +1,5 @@ import RawMesh from './RawMesh'; -import Geometry from './geometry/Geometry'; +import Geometry from '../core/geometry/Geometry'; import * as core from '../core'; import { readFileSync } from 'fs'; import { join } from 'path';