Skip to content
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

Remove allocs from idle and selecting ray interactor #11344

Merged
merged 10 commits into from
Jan 24, 2023

Conversation

RogPodge
Copy link
Contributor

@RogPodge RogPodge commented Jan 5, 2023

Overview

Fixes an issue where ColorUtilities.GradientCompress was called every frame. The GradientCompress effect only needs to be applied when the ray interactor is registering a hit.

This investigation also revealed that the Gradient lerp function also allocs. It may be worthwhile to create in-place Gradient functions in the future to remove allocs which can occur while selecting or hovering over valid targets.

Changes

@@ -367,14 +367,14 @@ private void UpdateLineVisual()
{
lineRenderer.colorGradient = ColorUtilities.GradientLerp(ValidColorGradient, SelectActiveColorGradient, rayInteractor.hasSelection ? 1 : 0);
}

var compressionAmount = Mathf.Clamp(rayInteractor.maxRaycastDistance * MaxGradientLength / hitDistance, 0.0f, 1.0f);
lineRenderer.colorGradient = ColorUtilities.GradientCompress(lineRenderer.colorGradient, 0.0f, compressionAmount);
Copy link
Contributor

@keveleigh keveleigh Jan 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of only (or in addition to) calling this less frequently, can we make this method not alloc at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After much investigation, I learned a few things.

  1. calls to retrieve lineRender.colorGradient allocs a new instance of Gradient.
  2. Calls to retrieve gradient.colorKeys and gradient.alpha keys allocs new arrays.

With these considerations, we can't do much to the fundamental implementation of our utility functions to avoid these GC allocs. I've made other adjustments to the LineVisuals implementation to avoid calling these functions when not necessary, but fundamentally doing modifications of gradients will end up being costly due to the underlying native implemenation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's fundamentally unsolvable, we should be doing this in the shader.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though I agree, I think that the performance hit (a few allocs during inbetween time of selection) doesn't quite justify the engineering effort for a rework at this particular juncture. It's not regression from what we've had previously in MRTK2, so I don't think it should have much customer facing impact.

@RogPodge RogPodge changed the title Remove allocs from idle ray interactor Remove allocs from idle and selecting ray interactor Jan 5, 2023
@@ -267,6 +267,10 @@ protected void OnDisable()
#region LineVisual Updates

private static readonly ProfilerMarker UpdateLinePerfMarker = new ProfilerMarker("[MRTK] MRTKLineVisual.UpdateLineVisual");
private static readonly ProfilerMarker UpdateLinePerfMarker2 = new ProfilerMarker("[MRTK] MRTKLineVisual.UpdateLineVisual2");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this unused?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover while debugging, will remove

[Range(0.01f, 1)]
float maxGradientLength = 0.1f;
float maxGradientLength = 0.3f;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain a bit more about changing the default value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous value was a little too aggressive and would end up compressing the gradient in situations where it didn't really improve usability. Increasing the value makes it so we don't unnecessarily apply the compression effect (saving us on performance)

@Zee2 Zee2 enabled auto-merge (squash) January 24, 2023 00:59
@Zee2 Zee2 merged commit 4381709 into microsoft:mrtk3 Jan 24, 2023
drusk-unity pushed a commit to drusk-unity/MixedRealityToolkit-Unity that referenced this pull request Jun 26, 2023
* Line visual now only applies compression effect when the ray interactor has hit an object

* adjusted gradient compress algorithm to be more compact and efficient

* added deadzone parameter to the input simulator so axis values would reach 0 or 1

* reorganized line visuals script, made adjustments to minimize color utilities calls when not needed

* adjusted prefab values to be less aggressive with the compression effect

* removing unused perf marker

Co-authored-by: Finn Sinclair <finnnorth@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants