Skip to content

Commit

Permalink
Update WebGLAttributes
Browse files Browse the repository at this point in the history
refactor WebGLAttributes to class
  • Loading branch information
epreston committed Dec 1, 2023
1 parent 321f808 commit 8205548
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 34 deletions.
2 changes: 1 addition & 1 deletion packages/renderers/src/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ class WebGLRenderer {
textures = new WebGLTextures(_gl, extensions, state, properties, capabilities, utils, info);
cubemaps = WebGLCubeMaps(_this);
cubeuvmaps = WebGLCubeUVMaps(_this);
attributes = WebGLAttributes(_gl, capabilities);
attributes = new WebGLAttributes(_gl, capabilities);
bindingStates = WebGLBindingStates(_gl, extensions, attributes, capabilities);
geometries = WebGLGeometries(_gl, attributes, info, bindingStates);
objects = WebGLObjects(_gl, geometries, attributes, info);
Expand Down
57 changes: 25 additions & 32 deletions packages/webgl/src/WebGLAttributes.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
/** @param { WebGL2RenderingContext} gl */
function WebGLAttributes(gl, capabilities) {
const buffers = new WeakMap();
class WebGLAttributes {
/** @param { WebGL2RenderingContext} gl */
constructor(gl, capabilities) {
this.gl = gl;
this.capabilities = capabilities;
this.buffers = new WeakMap();
}

function createBuffer(attribute, bufferType) {
const array = attribute.array;
const usage = attribute.usage;
_createBuffer(attribute, bufferType) {
const { array, usage } = attribute;
const { gl } = this;

const buffer = gl.createBuffer();

Expand Down Expand Up @@ -47,15 +51,14 @@ function WebGLAttributes(gl, capabilities) {
};
}

function updateBuffer(buffer, attribute, bufferType) {
const array = attribute.array;
const updateRange = attribute.updateRange;
_updateBuffer(buffer, attribute, bufferType) {
const { array, updateRange } = attribute;
const { gl } = this;

gl.bindBuffer(bufferType, buffer);

if (updateRange.count === -1) {
// Not using update ranges

gl.bufferSubData(bufferType, 0, array);
} else {
gl.bufferSubData(
Expand All @@ -72,32 +75,29 @@ function WebGLAttributes(gl, capabilities) {
attribute.onUploadCallback();
}

//

function get(attribute) {
get(attribute) {
if (attribute.isInterleavedBufferAttribute) attribute = attribute.data;

return buffers.get(attribute);
return this.buffers.get(attribute);
}

function remove(attribute) {
remove(attribute) {
if (attribute.isInterleavedBufferAttribute) attribute = attribute.data;

const data = buffers.get(attribute);
const data = this.buffers.get(attribute);

if (data) {
gl.deleteBuffer(data.buffer);

buffers.delete(attribute);
this.gl.deleteBuffer(data.buffer);
this.buffers.delete(attribute);
}
}

function update(attribute, bufferType) {
update(attribute, bufferType) {
if (attribute.isGLBufferAttribute) {
const cached = buffers.get(attribute);
const cached = this.buffers.get(attribute);

if (!cached || cached.version < attribute.version) {
buffers.set(attribute, {
this.buffers.set(attribute, {
buffer: attribute.buffer,
type: attribute.type,
bytesPerElement: attribute.elementSize,
Expand All @@ -110,22 +110,15 @@ function WebGLAttributes(gl, capabilities) {

if (attribute.isInterleavedBufferAttribute) attribute = attribute.data;

const data = buffers.get(attribute);
const data = this.buffers.get(attribute);

if (data === undefined) {
buffers.set(attribute, createBuffer(attribute, bufferType));
this.buffers.set(attribute, this._createBuffer(attribute, bufferType));
} else if (data.version < attribute.version) {
updateBuffer(data.buffer, attribute, bufferType);

this._updateBuffer(data.buffer, attribute, bufferType);
data.version = attribute.version;
}
}

return {
get,
remove,
update
};
}

export { WebGLAttributes };
2 changes: 1 addition & 1 deletion packages/webgl/tests/WebGLAttributes.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { WebGLAttributes } from '../src/WebGLAttributes.js';

describe('WebGL', () => {
describe('WebGLAttributes', () => {
it('should expose a function', () => {
it('should expose a class', () => {
expect(WebGLAttributes).toBeDefined();
});

Expand Down

0 comments on commit 8205548

Please sign in to comment.