-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Optimize matrix inversions and reduce GPU stalls #7367
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3fbc9a2
[QUAL] Optimize matrix computation, avoid some matrix allocations, sm…
xavierjs 5e969ca
[MINOR]
xavierjs e059aa8
[DOC] Update changelog
xavierjs 1efdf3d
[QUAL] Make unit test pass - if WebGL2RenderingContext is not defined
xavierjs b652602
[QUAL] Add unit tests for matrix inversion [FIX] Fix a skew inversion…
xavierjs 15715ca
[QUAL] Add more tests for fast_maths
xavierjs 49c1867
[QUAL] Implement feedback
xavierjs 9cf2e0d
[MERGE]
xavierjs c6a2cc1
[MINOR]
xavierjs f48891e
[MINOR]
xavierjs 02f4e78
[MERGE]
xavierjs 265af94
[QUAL] Resolve https://github.com/maplibre/maplibre-gl-js/pull/7367#d…
xavierjs 1bf0984
[MERGE]
xavierjs d0d3466
[TEST] Update min bundle size test result
xavierjs c7a6fb9
[MINOR]
xavierjs 6cf753a
Merge branch 'main' into xavierjs/optimizeMatrixOps
HarelM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
xavierjs marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| import {describe, test, expect} from 'vitest'; | ||
| import {mat4, vec3, quat} from 'gl-matrix'; | ||
| import {fastInvertTransformMat4, fastInvertProjMat4, fastInvertSkewMat4} from './fast_maths'; | ||
|
|
||
| function compare_matrix(mat: mat4, matRef: mat4){ | ||
|
xavierjs marked this conversation as resolved.
|
||
| for (let i=0; i<16; ++i){ | ||
| expect(mat[i]).toBeCloseTo(matRef[i], 6); | ||
| } | ||
| } | ||
|
|
||
| describe('fast_maths', () => { | ||
| test('invert a transform matrix', () => { | ||
| const testParams = [{ | ||
| translate: [1, 2, -3], | ||
| scaleXY: 10, | ||
| rotAxis: [5, 6, 7], | ||
| rotAngle: Math.PI / 4 | ||
| },{ | ||
| translate: [0, 0, 0], | ||
| scaleXY: 1, | ||
| rotAxis: [0, 0, 1], | ||
| rotAngle: 0 | ||
| },{ | ||
| translate: [-50, 100, 0.5], | ||
| scaleXY: 0.01, | ||
| rotAxis: [1, 0, 0], | ||
| rotAngle: Math.PI | ||
| },{ | ||
| translate: [1000, -2000, 500], | ||
| scaleXY: 200, | ||
| rotAxis: [0, 1, 0], | ||
| rotAngle: -Math.PI / 6 | ||
| },{ | ||
| translate: [-0.1, 0.2, -0.3], | ||
| scaleXY: 0.5, | ||
| rotAxis: [1, 1, 0], | ||
| rotAngle: Math.PI / 2 | ||
| },{ | ||
| translate: [0, 0, -9999], | ||
| scaleXY: 50, | ||
| rotAxis: [0.3, 0.4, 0.5], | ||
| rotAngle: -0.01 | ||
| },{ | ||
| translate: [42, -7, 13], | ||
| scaleXY: 4, | ||
| rotAxis: [1, -2, 3], | ||
| rotAngle: 2.1 | ||
| }]; | ||
| const m = mat4.create(); | ||
| const mInv = mat4.create(); | ||
| const mInvRef = mat4.create(); | ||
| for (const {translate, scaleXY, rotAxis, rotAngle} of testParams) { | ||
| // translation vector: | ||
| const v = vec3.create(); | ||
| vec3.copy(v, translate); | ||
|
|
||
| // scale vector. sx shoule be = sy: | ||
| const s = vec3.create(); | ||
| s[0] = scaleXY; | ||
| s[1] = scaleXY; | ||
| s[2] = 1; | ||
|
|
||
| // quaternion: | ||
| const q = quat.create(); | ||
| const axis = vec3.create(); | ||
| vec3.copy(axis, rotAxis); | ||
| vec3.normalize(axis, axis); | ||
| quat.setAxisAngle(q, axis, rotAngle); | ||
| mat4.fromRotationTranslationScale(m, q, v, s); | ||
|
|
||
| // compute inv: | ||
| fastInvertTransformMat4(mInv, m); | ||
|
|
||
| // compute ref: | ||
| mat4.invert(mInvRef, m); | ||
|
|
||
| compare_matrix(mInv, mInvRef); | ||
| }; | ||
| }); | ||
|
|
||
| test('invert a projection matrix', () => { | ||
| const testParams = [ | ||
| { | ||
| fov: Math.PI/4, | ||
| aspect: 16/9, | ||
| zNear: 1, | ||
| zFar: 1000 | ||
| }, | ||
| { | ||
| fov: Math.PI/3, | ||
| aspect: 4/3, | ||
| zNear: 10, | ||
| zFar: 10000 | ||
| }, | ||
| { | ||
| fov: Math.PI/8, | ||
| aspect: 1, | ||
| zNear: 1, | ||
| zFar: 10 | ||
| }, | ||
| { | ||
| fov: Math.PI/4, | ||
| aspect: 2, | ||
| zNear: 1, | ||
| zFar: 1000 | ||
| } | ||
| ]; | ||
| const m = mat4.create(); | ||
| const mInv = mat4.create(); | ||
| const mInvRef = mat4.create(); | ||
| for (const {fov, aspect, zNear, zFar} of testParams) { | ||
| mat4.perspective(m, fov, aspect, zNear, zFar); | ||
| // compute inv: | ||
| fastInvertProjMat4(mInv, m); | ||
| // compute ref: | ||
| mat4.invert(mInvRef, m); | ||
| compare_matrix(mInv, mInvRef); | ||
| }; | ||
| }); | ||
|
|
||
| test('invert a skew matrix', () => { | ||
| const testParams = [{ | ||
| diag: [1, 1, 1, 1], | ||
| counterDiagXY: [0, 0] | ||
| },{ | ||
| diag: [2, 3, 4, 5], | ||
| counterDiagXY: [0.5, -0.5] | ||
| },{ | ||
| diag: [0.1, 0.2, 0.3, 0.4], | ||
| counterDiagXY: [0.01, -0.01] | ||
| },{ | ||
| diag: [10, 10, 10, 1], | ||
| counterDiagXY: [3, -3] | ||
| },{ | ||
| diag: [1, 1, 1, 1], | ||
| counterDiagXY: [0.9, -0.9] | ||
| },{ | ||
| diag: [100, 200, 50, 1], | ||
| counterDiagXY: [10, 20] | ||
| },{ | ||
| diag: [0.5, 0.5, 0.5, 2], | ||
| counterDiagXY: [0, 0] | ||
| },{ | ||
| diag: [7, 3, 5, 1], | ||
| counterDiagXY: [-2, 1] | ||
| },{ | ||
| diag: [1, 1, 0.01, 1], | ||
| counterDiagXY: [0.5, 0.3] | ||
| },{ | ||
| diag: [4, 8, 2, 1], | ||
| counterDiagXY: [-5, 3] | ||
| },{ | ||
| diag: [0.3, 0.7, 1.5, 1], | ||
| counterDiagXY: [0.1, -0.2] | ||
| }]; | ||
| const m = mat4.create(); | ||
| const mInv = mat4.create(); | ||
| const mInvRef = mat4.create(); | ||
| for(const {diag, counterDiagXY} of testParams) { | ||
| // forge a skew matrix: | ||
| m[0] = diag[0]; | ||
| m[5] = diag[1]; | ||
| m[10] = diag[2]; | ||
| m[15] = diag[3]; | ||
| m[4] = counterDiagXY[0]; | ||
| m[1] = counterDiagXY[1]; | ||
|
|
||
| // compute inv: | ||
| fastInvertSkewMat4(mInv, m); | ||
|
|
||
| // compute ref: | ||
| mat4.invert(mInvRef, m); | ||
|
|
||
| compare_matrix(mInv, mInvRef); | ||
| }; | ||
| }); | ||
| }); | ||
|
xavierjs marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /* | ||
| This class implements fast mathematical operations to optimize or complement gl-matrix | ||
| */ | ||
| import type {mat4} from 'gl-matrix'; | ||
|
|
||
| /* | ||
| Invert a transform matrix | ||
| dst and src are in column-major flat format | ||
| */ | ||
| export function fastInvertTransformMat4(dst: mat4, src: mat4): mat4{ | ||
| // extract scale. hyp: scale X = scale Y: | ||
| const sXYSqInv = 1.0 / (src[0] * src[0] + src[1] * src[1] + src[2] * src[2]); | ||
| const sZSqInv = 1.0 / (src[8] * src[8] + src[9] * src[9] + src[10] * src[10]); | ||
|
|
||
| // inv rotation and scaling part: | ||
| const s0 = src[0] * sXYSqInv; | ||
| const s4 = src[4] * sXYSqInv; | ||
| const s8 = src[8] * sZSqInv; | ||
| const s1 = src[1] * sXYSqInv; | ||
| const s5 = src[5] * sXYSqInv; | ||
| const s9 = src[9] * sZSqInv; | ||
| const s2 = src[2] * sXYSqInv; | ||
| const s6 = src[6] * sXYSqInv; | ||
| const s10 = src[10] * sZSqInv; | ||
|
|
||
| // rotation part: | ||
| dst[0] = s0; | ||
| dst[1] = s4; | ||
| dst[2] = s8; | ||
| dst[4] = s1; | ||
| dst[5] = s5; | ||
| dst[6] = s9; | ||
| dst[8] = s2; | ||
| dst[9] = s6; | ||
| dst[10] = s10; | ||
|
|
||
| // translation part: | ||
| const t0 = src[12]; | ||
| const t1 = src[13]; | ||
| const t2 = src[14]; | ||
| dst[12] = -s0 * t0 - s1 * t1 - s2 * t2; | ||
| dst[13] = -s4 * t0 - s5 * t1 - s6 * t2; | ||
| dst[14] = -s8 * t0 - s9 * t1 - s10 * t2; | ||
|
|
||
| // constant part: | ||
| dst[3] = 0; | ||
| dst[7] = 0; | ||
| dst[11] = 0; | ||
| dst[15] = 1; | ||
|
|
||
| return dst; | ||
| } | ||
|
|
||
| /* | ||
| Invert a perspective projection matrix | ||
| dst and src are in column-major flat format | ||
| */ | ||
| export function fastInvertProjMat4(dst: mat4, src: mat4): mat4{ | ||
| dst[0] = 1 / src[0]; | ||
| dst[1] = 0; | ||
| dst[2] = 0; | ||
| dst[3] = 0; | ||
|
|
||
| dst[4] = 0; | ||
| dst[5] = 1 / src[5]; | ||
| dst[6] = 0; | ||
| dst[7] = 0; | ||
|
|
||
| dst[8] = 0; | ||
| dst[9] = 0; | ||
| dst[10] = 0; | ||
| dst[11] = 1 / src[14]; | ||
|
|
||
| dst[12] = 0; | ||
| dst[13] = 0; | ||
| dst[14] = -1; | ||
| dst[15] = src[10] / src[14]; | ||
|
|
||
| return dst; | ||
| } | ||
|
|
||
| /* | ||
| Invert a skew matrix | ||
| with diag coefficients > 0 and XY counterdiag | ||
| dst and src are in column-major flat format | ||
| */ | ||
|
|
||
| export function fastInvertSkewMat4(dst: mat4, src: mat4): mat4{ | ||
| const invDetXY = 1.0 / (src[0]*src[5] - src[1]*src[4]); | ||
| dst[0] = src[5] * invDetXY; | ||
| dst[1] = -src[1] * invDetXY; | ||
| dst[2] = 0; | ||
| dst[3] = 0; | ||
|
|
||
| dst[4] = -src[4] * invDetXY; | ||
| dst[5] = src[0] * invDetXY; | ||
| dst[6] = 0; | ||
| dst[7] = 0; | ||
|
|
||
| dst[8] = 0; | ||
| dst[9] = 0; | ||
| dst[10] = 1 / src[10]; | ||
| dst[11] = 0; | ||
|
|
||
| dst[12] = 0; | ||
| dst[13] = 0; | ||
| dst[14] = 0; | ||
| dst[15] = 1 / src[15]; | ||
|
|
||
| return dst; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.