Skip to content

Optimise VectorLine camera projection for improved Map View performance#281

Merged
gotmachine merged 11 commits into
KSPModdingLibs:devfrom
Halbann:OptimisedVectorLines
Mar 9, 2025
Merged

Optimise VectorLine camera projection for improved Map View performance#281
gotmachine merged 11 commits into
KSPModdingLibs:devfrom
Halbann:OptimisedVectorLines

Conversation

@Halbann
Copy link
Copy Markdown
Contributor

@Halbann Halbann commented Oct 31, 2024

Orbit rendering takes up a significant majority of the frame time with a large number of orbit lines visible in the Map View, which is mainly caused by camera projection done by VectorLine.

This patch improves performance by caching the camera's projection matrix multiplied with its view matrix once per frame, and replacing calls to different camera projection functions in VectorLine with our cached versions.

Will write a full description with some benchmarking results when the patch is more complete.

Comment thread KSPCommunityFixes/KSPCommunityFixes.csproj Outdated
Comment thread KSPCommunityFixes/Performance/OptimisedVectorLines.cs Outdated
Comment thread KSPCommunityFixes/Performance/OptimisedVectorLines.cs Outdated
Comment thread KSPCommunityFixes/Performance/OptimisedVectorLines.cs Outdated
Comment thread KSPCommunityFixes/Performance/OptimisedVectorLines.cs Outdated
@gotmachine
Copy link
Copy Markdown
Contributor

I would recommend validating your results by comparing them to the original method results.
You can gate that validation code behind a file level #if def, and use ULP based equality comparisons (Numerics.AlmostEqual(), with maybe an ULP of 5 in this this case), see an example here :

#if DEBUG_UPDATEAERO
private static float kspcf_rb_AngularDrag;
private static float kspcf_dynamicPressurekPa;
private static Vector3 kspcf_dragVectorDir;
private static float kspcf_dragScalar;
private static Vector3 kspcf_liftForce;
private static float kspcf_liftScalar;
private static void FlightIntegrator_UpdateAerodynamics_DebugPrefix(FlightIntegrator __instance, Part part)
{
FlightIntegrator_UpdateAerodynamics_Override(__instance, part);
kspcf_dynamicPressurekPa = (float)part.dynamicPressurekPa; // comparing as float since this is computed from floats
kspcf_dragVectorDir = part.dragVectorDir;
kspcf_dragScalar = part.dragScalar;
kspcf_liftForce = part.DragCubes.LiftForce;
kspcf_liftScalar = part.bodyLiftScalar;
}
private static void FlightIntegrator_UpdateAerodynamics_DebugPostfix(Part part)
{
float stock_rb_angularDrag = part.rb.IsNotNullOrDestroyed() ? part.rb.angularDrag : 0f;
float stock_dynamicPressurekPa = (float)part.dynamicPressurekPa; // comparing as float since this is computed from floats
Vector3 stock_dragVectorDir = part.dragVectorDir;
float stock_dragScalar = part.dragScalar;
Vector3 stock_liftForce = part.DragCubes.LiftForce;
float stock_liftScalar = part.bodyLiftScalar;
if (!Numerics.AlmostEqual(stock_rb_angularDrag, kspcf_rb_AngularDrag, 20))
Debug.Log($"[FIAeroDebug] Mismatching RB angularDrag : {Math.Abs(stock_rb_angularDrag - kspcf_rb_AngularDrag)}");
if (!Numerics.AlmostEqual(stock_dynamicPressurekPa, kspcf_dynamicPressurekPa, 20))
Debug.Log($"[FIAeroDebug] Mismatching dynamicPressurekPa : {Math.Abs(stock_dynamicPressurekPa - kspcf_dynamicPressurekPa)}");
if (stock_dragVectorDir != kspcf_dragVectorDir)
Debug.Log($"[FIAeroDebug] Mismatching dragVectorDir : {(stock_dragVectorDir - kspcf_dragVectorDir).magnitude}");
if (!Numerics.AlmostEqual(stock_dragScalar, kspcf_dragScalar, 20))
Debug.Log($"[FIAeroDebug] Mismatching dragScalar : {Math.Abs(stock_dragScalar - kspcf_dragScalar)}");
if (stock_liftForce != kspcf_liftForce)
Debug.Log($"[FIAeroDebug] Mismatching liftForce : {(stock_liftForce - kspcf_liftForce).magnitude}");
if (!Numerics.AlmostEqual(stock_liftScalar, kspcf_liftScalar, 20))
Debug.Log($"[FIAeroDebug] Mismatching liftScalar : {Math.Abs(stock_liftScalar - kspcf_liftScalar)}");
}
#endif

@gotmachine gotmachine added the kspPerformance Possible performance improvement in KSP label Nov 1, 2024
Comment thread KSPCommunityFixes/KSPCommunityFixes.cs Outdated
@Halbann Halbann marked this pull request as ready for review November 3, 2024 21:47
@gotmachine gotmachine merged commit 5367575 into KSPModdingLibs:dev Mar 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kspPerformance Possible performance improvement in KSP

Development

Successfully merging this pull request may close these issues.

3 participants