-
-
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
Open
xavierjs
wants to merge
15
commits into
maplibre:main
Choose a base branch
from
xavierjs:xavierjs/optimizeMatrixOps
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
15 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 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,6 @@ | ||
| const cache = new WeakMap(); | ||
| export function isWebGL2( | ||
| gl: WebGLRenderingContext | WebGL2RenderingContext | ||
| ): gl is WebGL2RenderingContext { | ||
| if (cache.has(gl)) { | ||
| return cache.get(gl); | ||
| } else { | ||
| const value = gl.getParameter(gl.VERSION)?.startsWith('WebGL 2.0'); | ||
| cache.set(gl, value); | ||
| return value; | ||
| } | ||
| // this method is really faster than fetching a cache: | ||
| return typeof WebGL2RenderingContext !== 'undefined' && gl instanceof WebGL2RenderingContext; | ||
| } |
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.
Show resolved
Hide 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,43 @@ | ||
| import {test, expect} from 'vitest'; | ||
| import {mat4, vec3, quat} from 'gl-matrix'; | ||
| import {fastInvertTransformMat4} from './fast_maths'; | ||
|
|
||
| test('fast_maths', () => { | ||
| // forge a transform matrix: | ||
| const m = mat4.create(); | ||
|
|
||
| // translation vector: | ||
| const v = vec3.create(); | ||
| v[0] = 1; | ||
| v[1] = 2; | ||
| v[2] = -3; | ||
|
|
||
| // scale vector. sx shoule be = sy: | ||
| const s = vec3.create(); | ||
| s[0] = 10; | ||
| s[1] = 10; | ||
| s[2] = 1; | ||
|
|
||
| // quaternion: | ||
| const q = quat.create(); | ||
| const axis = vec3.create(); | ||
| axis[0] = 5; | ||
| axis[1] = 6; | ||
| axis[2] = 7; | ||
| vec3.normalize(axis, axis); | ||
| quat.setAxisAngle(q, axis, Math.PI/4); | ||
|
|
||
| mat4.fromRotationTranslationScale(m, q, v, s); | ||
|
|
||
| // compute inv: | ||
| const mInv = mat4.create(); | ||
| fastInvertTransformMat4(mInv, m); | ||
|
|
||
| // compute ref: | ||
| const mInvRef = mat4.create(); | ||
| mat4.invert(mInvRef, m); | ||
|
|
||
| for (let i=0; i<16; ++i){ | ||
| expect(mInv[i]).toBeCloseTo(mInvRef[i], 6); | ||
| } | ||
| }); |
xavierjs marked this conversation as resolved.
Show resolved
Hide 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 @@ | ||
| /* | ||
| (xbourry): this class implements fast mathematical operations | ||
xavierjs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| 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 det = src[0]*src[5] - src[1]*src[4]; | ||
| dst[0] = src[5] / det; | ||
| dst[1] = -src[4] / det; | ||
| dst[2] = 0; | ||
| dst[3] = 0; | ||
|
|
||
| dst[4] = -src[1] / det; | ||
| dst[5] = src[0] / det; | ||
| 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; | ||
| } | ||
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.