perf: 数组解构比分别赋值更快

This commit is contained in:
unanmed 2025-01-11 17:23:55 +08:00
parent babb478542
commit ba30d111a5

View File

@ -248,6 +248,7 @@ export abstract class GL2<E extends EGL2Event = EGL2Event> extends RenderItem<
case RenderMode.Arrays: { case RenderMode.Arrays: {
const { mode, first, count } = param as DrawArraysParam; const { mode, first, count } = param as DrawArraysParam;
gl.drawArrays(mode, first, count); gl.drawArrays(mode, first, count);
break;
} }
case RenderMode.Elements: { case RenderMode.Elements: {
if (!indices) return; if (!indices) return;
@ -255,11 +256,13 @@ export abstract class GL2<E extends EGL2Event = EGL2Event> extends RenderItem<
param as DrawElementsParam; param as DrawElementsParam;
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indices.data); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indices.data);
gl.drawElements(mode, count, type, offset); gl.drawElements(mode, count, type, offset);
break;
} }
case RenderMode.ArraysInstanced: { case RenderMode.ArraysInstanced: {
const { mode, first, count, instanceCount } = const { mode, first, count, instanceCount } =
param as DrawArraysInstancedParam; param as DrawArraysInstancedParam;
gl.drawArraysInstanced(mode, first, count, instanceCount); gl.drawArraysInstanced(mode, first, count, instanceCount);
break;
} }
case RenderMode.ElementsInstanced: { case RenderMode.ElementsInstanced: {
if (!indices) return; if (!indices) return;
@ -272,6 +275,7 @@ export abstract class GL2<E extends EGL2Event = EGL2Event> extends RenderItem<
} = param as DrawElementsInstancedParam; } = param as DrawElementsInstancedParam;
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indices.data); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indices.data);
gl.drawElementsInstanced(mode, count, type, offset, ins); gl.drawElementsInstanced(mode, count, type, offset, ins);
break;
} }
} }
} }
@ -714,12 +718,8 @@ class ShaderUniform<T extends UniformType> implements IShaderUniform<T> {
) {} ) {}
set(...params: UniformSetFn[T]): void { set(...params: UniformSetFn[T]): void {
this.gl.vertexAttribIPointer;
// 因为ts类型推导的限制类型肯定正确但是推导不出所以这里直接 as any 屏蔽掉类型推导 // 因为ts类型推导的限制类型肯定正确但是推导不出所以这里直接 as any 屏蔽掉类型推导
const x0 = params[0] as any; const [x0, x1, x2, x3] = params as any[];
const x1 = params[1] as any;
const x2 = params[2] as any;
const x3 = params[3] as any;
switch (this.type) { switch (this.type) {
case UniformType.Uniform1f: case UniformType.Uniform1f:
this.gl.uniform1f(this.location, x0); this.gl.uniform1f(this.location, x0);
@ -807,10 +807,7 @@ class ShaderAttrib<T extends AttribType> implements IShaderAttrib<T> {
set(...params: AttribSetFn[T]) { set(...params: AttribSetFn[T]) {
// 因为ts类型推导的限制类型肯定正确但是推导不出所以这里直接 as any 屏蔽掉类型推导 // 因为ts类型推导的限制类型肯定正确但是推导不出所以这里直接 as any 屏蔽掉类型推导
const x0 = params[0] as any; const [x0, x1, x2, x3] = params as any[];
const x1 = params[1] as any;
const x2 = params[2] as any;
const x3 = params[3] as any;
switch (this.type) { switch (this.type) {
case AttribType.Attrib1f: case AttribType.Attrib1f:
this.gl.vertexAttrib1f(this.location, x0); this.gl.vertexAttrib1f(this.location, x0);
@ -1035,10 +1032,10 @@ class ShaderUniformBlock implements IShaderUniformBlock {
const buffer = this.buffer; const buffer = this.buffer;
gl.bindBuffer(gl.UNIFORM_BUFFER, buffer); gl.bindBuffer(gl.UNIFORM_BUFFER, buffer);
if (srcOffset !== void 0) { if (srcOffset !== void 0) {
// @ts-ignore // @ts-expect-error 无法推断
gl.bufferSubData(gl.UNIFORM_BUFFER, 0, srcData, srcOffset, length); gl.bufferSubData(gl.UNIFORM_BUFFER, 0, srcData, srcOffset, length);
} else { } else {
// @ts-ignore // @ts-expect-error 无法推断
gl.bufferSubData(gl.UNIFORM_BUFFER, 0, srcData); gl.bufferSubData(gl.UNIFORM_BUFFER, 0, srcData);
} }
gl.bindBufferBase(gl.UNIFORM_BUFFER, this.binding, buffer); gl.bindBufferBase(gl.UNIFORM_BUFFER, this.binding, buffer);
@ -1478,7 +1475,7 @@ export class GL2Program extends EventEmitter<ShaderProgramEvent> {
/** /**
* uniform uniform * uniform uniform
* @param uniform uniform * @param uniform uniform
* @param type uniform {@link Shader.UNIFORM_1f} {@link Shader.UNIFORM_4uiv} * @param type uniform {@link GL2.UNIFORM_1f} {@link GL2.UNIFORM_4uiv}
* @returns uniform * @returns uniform
*/ */
defineUniform<T extends UniformType>( defineUniform<T extends UniformType>(
@ -1506,7 +1503,7 @@ export class GL2Program extends EventEmitter<ShaderProgramEvent> {
/** /**
* uniform uniform * uniform uniform
* @param uniform uniform * @param uniform uniform
* @param type uniform {@link Shader.U_MATRIX_2x2} {@link Shader.U_MATRIX_4x4} * @param type uniform {@link GL2.U_MATRIX_2x2} {@link GL2.U_MATRIX_4x4}
* @returns uniform * @returns uniform
*/ */
defineUniformMatrix( defineUniformMatrix(