-
-
Notifications
You must be signed in to change notification settings - Fork 97
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
Optimize Projection::determinant()
#11821
Comments
How often is this method actually called? I don't think that it shouldn't be implemented but would users actually see a real performance gain? |
I doubt it'll benefit performance much in most projects, but this kind of optimization generally saves on CPU cycles and therefore reduce power consumption on mobile devices. |
Projection::determinant()
One can think of the Projection class in more wide viewing angle. It's just a regular 4x4 matrix of float (or double) with handy set of built-in functions by its nature. So, from mathematical point of view, its usage limited only by developer fantasy. So, for example, just by 4 calls of determinant() method and Projection class constructor user can find:
In 3d game it can be an archer summoning cloud of hundreds of arrow from the sky (like in Path of Exile) or hundreds of archers shoots with one arrow each (like in Total War series). To calculate a position (Vector3) array where all of the arrows hits the 3d-mesh (landscape, or boss, strucrures, e.t.c.) determinant() function can be used hundreds of times per second per mesh triangle (it's normal if to be clear). |
Describe the project you are working on
Research in Godot / Raylib / blender 3d matrix math performance differences.
Describe the problem or limitation you are having in your project
Projection determinant calculation can be found in module https://github.com/godotengine/godot/blob/master/core/math/projection.cpp LINE 40 and looks like:
It takes 72 multiplication to calculate 4x4 matrix determinant as one can see.
Meanwhile, accordinly to this Wikipedia's article https://en.wikipedia.org/wiki/Laplace_expansion, this calculation can be reduced to (4 + 4 * (3 + 3 * 2)) = 40 multiplication by decreasing matrix size from 4x4 to 2x2 using minors.
Such algorithm is used in blender 3d application source code as for example: https://github.com/blender/blender/blob/main/source/blender/blenlib/intern/math_matrix_c.cc from LINE 1837
So we can replace those existing code fragment from above by something like this (slower variant):
or this (faster variant):
Describe the feature / enhancement and how it helps to overcome the problem or limitation
We can achieve 1.7x to 1.8x faster determinant calculation.
I wrote some simple test application to check perfomance boost. Look on green and dark green results on screenshot:
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
One should just replace old Projection::determinant() implementation with faster one from above.
If this enhancement will not be used often, can it be worked around with a few lines of script?
No.
Is there a reason why this should be core and not an add-on in the asset library?
This enhancement will be used often because its in the core math module.
The text was updated successfully, but these errors were encountered: