Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batcher buffers merging optimization #7109

Open
wants to merge 2 commits into
base: main_v1
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions src/scene/batching/batch-manager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Debug } from '../../core/debug.js';
import { now } from '../../core/time.js';
import { Mat3 } from '../../core/math/mat3.js';
import { Vec3 } from '../../core/math/vec3.js';

Check failure on line 4 in src/scene/batching/batch-manager.js

View workflow job for this annotation

GitHub Actions / Lint

'Vec3' is defined but never used
import { BoundingBox } from '../../core/shape/bounding-box.js';

import {
Expand Down Expand Up @@ -666,7 +666,6 @@
let verticesOffset = 0;
let indexOffset = 0;
let transform;
const vec = new Vec3();

// allocate indices
const indexArrayType = batchNumVerts <= 0xffff ? Uint16Array : Uint32Array;
Expand Down Expand Up @@ -706,24 +705,35 @@
// transform position, normal and tangent to world space
if (!dynamic && stream.numComponents >= 3) {
if (semantic === SEMANTIC_POSITION) {
const m = transform.data;
let x, y, z;

for (let j = 0; j < totalComponents; j += stream.numComponents) {
vec.set(subarray[j], subarray[j + 1], subarray[j + 2]);
transform.transformPoint(vec, vec);
subarray[j] = vec.x;
subarray[j + 1] = vec.y;
subarray[j + 2] = vec.z;
x = subarray[j];
y = subarray[j + 1];
z = subarray[j + 2];

// mat4.transformVector
subarray[j] = x * m[0] + y * m[4] + z * m[8] + m[12];
Maksims marked this conversation as resolved.
Show resolved Hide resolved
subarray[j + 1] = x * m[1] + y * m[5] + z * m[9] + m[13];
subarray[j + 2] = x * m[2] + y * m[6] + z * m[10] + m[14];
}
} else if (semantic === SEMANTIC_NORMAL || semantic === SEMANTIC_TANGENT) {

// handle non-uniform scale by using transposed inverse matrix to transform vectors
mat3.invertMat4(transform).transpose();

const m = mat3.data;
let x, y, z;

for (let j = 0; j < totalComponents; j += stream.numComponents) {
vec.set(subarray[j], subarray[j + 1], subarray[j + 2]);
mat3.transformVector(vec, vec);
subarray[j] = vec.x;
subarray[j + 1] = vec.y;
subarray[j + 2] = vec.z;
x = subarray[j];
y = subarray[j + 1];
z = subarray[j + 2];

// mat3.transformVector
subarray[j] = x * m[0] + y * m[3] + z * m[6];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would make some further improvement to copy matrix data to local variables outside of the loop

const m0 = m[0];
const m1 = m[1];

subarray[j + 1] = x * m[1] + y * m[4] + z * m[7];
subarray[j + 2] = x * m[2] + y * m[5] + z * m[8];
}
}
}
Expand Down
Loading