Skip to content

Commit

Permalink
Update WebGLBindingStates.js
Browse files Browse the repository at this point in the history
extract binding states class
  • Loading branch information
epreston committed Dec 1, 2023
1 parent 68789a9 commit 40b46ff
Showing 1 changed file with 24 additions and 28 deletions.
52 changes: 24 additions & 28 deletions packages/webgl/src/WebGLBindingStates.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
import { IntType } from '@renderlayer/shared';

class BindingState {
constructor(maxVertexAttributes, vao) {
this.newAttributes = [];
this.enabledAttributes = [];
this.attributeDivisors = [];

for (let i = 0; i < maxVertexAttributes; i++) {
this.newAttributes[i] = 0;
this.enabledAttributes[i] = 0;
this.attributeDivisors[i] = 0;
}

this.geometry = null;
this.program = null;
this.wireframe = false;
this.object = vao;
this.attributes = {};
this.attributesNum = 0;
this.index = null;
}
}

/** @param { WebGL2RenderingContext} gl */
function WebGLBindingStates(gl, extensions, attributes, capabilities) {
const maxVertexAttributes = gl.getParameter(gl.MAX_VERTEX_ATTRIBS);

const bindingStates = {};

const defaultState = createBindingState(null);
const defaultState = new BindingState(maxVertexAttributes, null);
let currentState = defaultState;
let forceUpdate = false;

Expand Down Expand Up @@ -71,39 +93,13 @@ function WebGLBindingStates(gl, extensions, attributes, capabilities) {
let state = stateMap[wireframe];

if (state === undefined) {
state = createBindingState(createVertexArrayObject());
state = new BindingState(maxVertexAttributes, createVertexArrayObject());
stateMap[wireframe] = state;
}

return state;
}

function createBindingState(vao) {
const newAttributes = [];
const enabledAttributes = [];
const attributeDivisors = [];

for (let i = 0; i < maxVertexAttributes; i++) {
newAttributes[i] = 0;
enabledAttributes[i] = 0;
attributeDivisors[i] = 0;
}

return {
// for backward compatibility on non-VAO support browser
geometry: null,
program: null,
wireframe: false,

newAttributes: newAttributes,
enabledAttributes: enabledAttributes,
attributeDivisors: attributeDivisors,
object: vao,
attributes: {},
index: null
};
}

function needsUpdate(object, geometry, program, index) {
const cachedAttributes = currentState.attributes;
const geometryAttributes = geometry.attributes;
Expand Down

0 comments on commit 40b46ff

Please sign in to comment.