giawa / opengl4csharp

OpenGL 4 Bindings (partially based on OpenTK) for C#
Other
232 stars 61 forks source link

Optimizations for Matrix4, BoundingBox and Frustum #54

Closed TheAIBot closed 4 years ago

TheAIBot commented 4 years ago

Took a stab at optimizing Matrix4, Frustum and BoundingBox. This PR is mainly focused on Matrix4 optimizations.

Added tests for the Matrix4 operators that were missing tests that i optimized.

No uint tests for both Frustum and BoundingBox but that's mainly because it's difficult to make unit tests for them. I've instead tested them manually. The optimizations for these two are also very small and it should be easy to see that the old and new code are equivalent.

The optimizations in Matrix4 are mainly done by replacing uses of the index operator with the row it would return and by vectorizing math operations.

Matrix4 benchmarks using System.Numerics


BenchmarkDotNet=v0.12.1, OS=Windows 10.0.18362.1016 (1903/May2019Update/19H1)
.NET Core SDK=5.0.100-preview.6.20318.15
  [Host]     : .NET Core 3.1.7 (CoreCLR 4.700.20.36602, CoreFX 4.700.20.37001), X64 RyuJIT
  DefaultJob : .NET Core 3.1.7 (CoreCLR 4.700.20.36602, CoreFX 4.700.20.37001), X64 RyuJIT
Method Mean Error StdDev
MultMatMatOld 403.704 ns 1.2245 ns 1.1454 ns
MultMatMatNew 36.361 ns 0.3194 ns 0.2988 ns
MultMatVec3Old 31.207 ns 0.1240 ns 0.1160 ns
MultMatVec3New 6.711 ns 0.1152 ns 0.1078 ns
MultMatVec4Old 50.112 ns 0.1868 ns 0.1656 ns
MultMatVec4New 6.695 ns 0.0609 ns 0.0509 ns
MultVec4MatOld 49.080 ns 0.1196 ns 0.1060 ns
MultVec4MatNew 49.138 ns 0.1861 ns 0.1554 ns
MultTransOld 47.183 ns 0.2741 ns 0.2430 ns
MultTransNew 6.657 ns 0.0552 ns 0.0516 ns
MultToFloatOld 53.761 ns 0.2290 ns 0.1912 ns
MultToFloatNew 11.199 ns 0.1325 ns 0.1107 ns
MatEqualityOpMatOld 11.473 ns 0.0778 ns 0.0689 ns
MatEqualityOpMatNew 2.968 ns 0.0377 ns 0.0352 ns
MatInEqualityOpMatOld 11.524 ns 0.0564 ns 0.0500 ns
MatInEqualityOpMatNew 3.589 ns 0.0232 ns 0.0194 ns
MatEqualsMatOld 11.190 ns 0.0954 ns 0.0846 ns
MatEqualsMatNew 3.937 ns 0.0247 ns 0.0206 ns
MatEqualsObjOld 13.710 ns 0.1509 ns 0.1411 ns
MatEqualsObjNew 6.896 ns 0.0563 ns 0.0499 ns
giawa commented 4 years ago

Very cool! This looks much nicer. I support merging this if all tests are looking good.