Skip to content

Commit 85aeef7

Browse files
authored
perf(webgl): look up tracked buffer targets in a constant table (#3423)
1 parent 72e71e8 commit 85aeef7

2 files changed

Lines changed: 54 additions & 7 deletions

File tree

‎modules/webgl/src/context/parameters/webgl-parameter-tables.ts‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,15 @@ export const GL_COMPOSITE_PARAMETER_SETTERS = {
398398

399399
type UpdateFunc = (params: Record<string, any>) => void;
400400

401+
/** Binding parameter tracked for each buffer target that is cached */
402+
const BUFFER_BINDING_PARAMETERS: Record<number, GL> = {
403+
[GL.ARRAY_BUFFER]: GL.ARRAY_BUFFER_BINDING,
404+
[GL.COPY_READ_BUFFER]: GL.COPY_READ_BUFFER_BINDING,
405+
[GL.COPY_WRITE_BUFFER]: GL.COPY_WRITE_BUFFER_BINDING,
406+
[GL.PIXEL_PACK_BUFFER]: GL.PIXEL_PACK_BUFFER_BINDING,
407+
[GL.PIXEL_UNPACK_BUFFER]: GL.PIXEL_UNPACK_BUFFER_BINDING
408+
};
409+
401410
// Setter functions intercepted for cache updates
402411
export const GL_HOOKED_SETTERS = {
403412
// GENERIC SETTERS
@@ -453,13 +462,7 @@ export const GL_HOOKED_SETTERS = {
453462
}
454463
},
455464
bindBuffer: (update: UpdateFunc, target, buffer) => {
456-
const pname = {
457-
[GL.ARRAY_BUFFER]: [GL.ARRAY_BUFFER_BINDING],
458-
[GL.COPY_READ_BUFFER]: [GL.COPY_READ_BUFFER_BINDING],
459-
[GL.COPY_WRITE_BUFFER]: [GL.COPY_WRITE_BUFFER_BINDING],
460-
[GL.PIXEL_PACK_BUFFER]: [GL.PIXEL_PACK_BUFFER_BINDING],
461-
[GL.PIXEL_UNPACK_BUFFER]: [GL.PIXEL_UNPACK_BUFFER_BINDING]
462-
}[target];
465+
const pname = BUFFER_BINDING_PARAMETERS[target];
463466

464467
if (pname) {
465468
return update({[pname]: buffer});

‎modules/webgl/test/context/state-tracker/webgl-state-tracker.spec.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,50 @@ it('WebGLStateTracker#intercept gl calls', async () => {
208208
void 0;
209209
});
210210

211+
it('WebGLStateTracker#tracks buffer bindings by target', async () => {
212+
const device = await devicePromise;
213+
const {gl} = device;
214+
const state = WebGLStateTracker.get(gl);
215+
216+
resetGLParameters(gl);
217+
218+
const bindingParameters = [
219+
[gl.ARRAY_BUFFER, gl.ARRAY_BUFFER_BINDING],
220+
[gl.COPY_READ_BUFFER, gl.COPY_READ_BUFFER_BINDING],
221+
[gl.COPY_WRITE_BUFFER, gl.COPY_WRITE_BUFFER_BINDING],
222+
[gl.PIXEL_PACK_BUFFER, gl.PIXEL_PACK_BUFFER_BINDING],
223+
[gl.PIXEL_UNPACK_BUFFER, gl.PIXEL_UNPACK_BUFFER_BINDING]
224+
];
225+
const buffers = bindingParameters.map(() => gl.createBuffer());
226+
227+
device.pushState();
228+
for (const [index, [target, bindingParameter]] of bindingParameters.entries()) {
229+
gl.bindBuffer(target, buffers[index]);
230+
expect(state.cache[bindingParameter], `binding of target ${target} is tracked`).toBe(
231+
buffers[index]
232+
);
233+
}
234+
for (const [index, [, bindingParameter]] of bindingParameters.entries()) {
235+
expect(gl.getParameter(bindingParameter), `binding ${bindingParameter} is current`).toBe(
236+
buffers[index]
237+
);
238+
}
239+
240+
const uniformBuffer = gl.createBuffer();
241+
gl.bindBuffer(gl.UNIFORM_BUFFER, uniformBuffer);
242+
expect(state.cache[gl.UNIFORM_BUFFER_BINDING], 'uniform buffer binding is not tracked').toBe(
243+
undefined
244+
);
245+
gl.bindBuffer(gl.UNIFORM_BUFFER, null);
246+
device.popState();
247+
248+
for (const [, bindingParameter] of bindingParameters) {
249+
expect(gl.getParameter(bindingParameter), `binding ${bindingParameter} is restored`).toBe(null);
250+
}
251+
252+
for (const buffer of [...buffers, uniformBuffer]) gl.deleteBuffer(buffer);
253+
});
254+
211255
it('WebGLStateTracker#not cached parameters', async () => {
212256
const device = await devicePromise;
213257
const {gl} = device;

0 commit comments

Comments
 (0)