Skip to content

Optimizing light loop part 2 #2

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

Merged
merged 30 commits into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
83dd52a
Saving 10% off the PrepareLightForGPU
FrancescoC-unity Mar 24, 2020
09444e6
18% decrease in cost with this
FrancescoC-unity Mar 24, 2020
22f85bf
Around 12% cost shaving off GetLightData
FrancescoC-unity Mar 24, 2020
9520b38
Around 6.5% win here
FrancescoC-unity Mar 24, 2020
6af3741
Faster View matrix flip
FrancescoC-unity Mar 25, 2020
8f573d1
Missing *=-1
FrancescoC-unity Mar 25, 2020
d1546d6
About 7% win in preprocess light data
FrancescoC-unity Mar 25, 2020
340540a
Another small batch
FrancescoC-unity Mar 25, 2020
6337d37
Small cleanup for first optimization pass
FrancescoC-unity Mar 25, 2020
04409af
Tiny bit more cleanup
FrancescoC-unity Mar 25, 2020
18f5ce0
Make UpdateShadowRequests 12% cheaper
FrancescoC-unity Mar 25, 2020
6fab90f
Address review points
FrancescoC-unity Mar 25, 2020
c7d2f8f
A bit less than 20% win in ExtractPointLightData
FrancescoC-unity Mar 25, 2020
50140cf
Add comment
FrancescoC-unity Mar 25, 2020
e6a852b
changelog
FrancescoC-unity Mar 25, 2020
9734944
25% reduction of ExtractPointLightData
FrancescoC-unity Mar 25, 2020
a032bd3
18% win in UpdateShadowRequest
FrancescoC-unity Mar 25, 2020
b89293d
Another around 11% win for UpdateShadowRequest
FrancescoC-unity Mar 26, 2020
530d2e3
Merge branch 'HDRP/optimizing-light-loop' into HDRP/optimizing-light-…
FrancescoC-unity Mar 26, 2020
bc1a292
Move matrix multiply utils to core
FrancescoC-unity Mar 26, 2020
1134251
less lighttype computation
FrancescoC-unity Mar 26, 2020
6a625d2
Ooops
FrancescoC-unity Mar 27, 2020
815e969
Merge branch 'HDRP/staging' into HDRP/optimizing-light-loop-when-shadows
FrancescoC-unity Mar 27, 2020
63766fb
fixup changelog
FrancescoC-unity Mar 27, 2020
ddde84e
TMP merge staging, will need to fix more stuff on this.
FrancescoC-unity Apr 1, 2020
cb47788
Proper merge
FrancescoC-unity Apr 1, 2020
cf4f7c6
Missing leftovers .
FrancescoC-unity Apr 1, 2020
7556895
I'll manage to submit it all one day...
FrancescoC-unity Apr 1, 2020
f6aafb9
Merge branch 'HDRP/staging' into HDRP/optimizing-light-loop-when-shadows
sebastienlagarde Apr 8, 2020
e12c64d
Merge branch 'HDRP/staging' into HDRP/optimizing-light-loop-when-shadows
sebastienlagarde Apr 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 164 additions & 0 deletions com.unity.render-pipelines.core/Runtime/Utilities/CoreMatrixUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Experimental.Rendering;

namespace UnityEngine.Rendering
{
using UnityObject = UnityEngine.Object;

/// <summary>
/// Set of utility functions for the Core Scriptable Render Pipeline Library related to Matrix operations
/// </summary>
public static class CoreMatrixUtils
{
/// <summary>
/// This function provides the equivalent of multiplying matrix parameter inOutMatrix with a translation matrix defined by the parameter translation.
/// The order of the equivalent multiplication is inOutMatrix * translation.
/// </summary>
/// <param name="inOutMatrix">Matrix to multiply with translation.</param>
/// <param name="translation">Translation component to multiply to the matrix.</param>
public static void MatrixTimesTranslation(ref Matrix4x4 inOutMatrix, Vector3 translation)
{
inOutMatrix.m03 += (inOutMatrix.m00 * translation.x + inOutMatrix.m01 * translation.y + inOutMatrix.m02 * translation.z);
inOutMatrix.m13 += (inOutMatrix.m10 * translation.x + inOutMatrix.m11 * translation.y + inOutMatrix.m12 * translation.z);
inOutMatrix.m23 += (inOutMatrix.m20 * translation.x + inOutMatrix.m21 * translation.y + inOutMatrix.m22 * translation.z);
}

/// <summary>
/// This function provides the equivalent of multiplying a translation matrix defined by the parameter translation with the matrix specified by the parameter inOutMatrix.
/// The order of the equivalent multiplication is translation * inOutMatrix.
/// </summary>
/// <param name="inOutMatrix">Matrix to multiply with translation.</param>
/// <param name="translation">Translation component to multiply to the matrix.</param>
public static void TranslationTimesMatrix(ref Matrix4x4 inOutMatrix, Vector3 translation)
{
inOutMatrix.m00 += translation.x * inOutMatrix.m30;
inOutMatrix.m01 += translation.x * inOutMatrix.m31;
inOutMatrix.m02 += translation.x * inOutMatrix.m32;
inOutMatrix.m03 += translation.x * inOutMatrix.m33;

inOutMatrix.m10 += translation.y * inOutMatrix.m30;
inOutMatrix.m11 += translation.y * inOutMatrix.m31;
inOutMatrix.m12 += translation.y * inOutMatrix.m32;
inOutMatrix.m13 += translation.y * inOutMatrix.m33;

inOutMatrix.m20 += translation.z * inOutMatrix.m30;
inOutMatrix.m21 += translation.z * inOutMatrix.m31;
inOutMatrix.m22 += translation.z * inOutMatrix.m32;
inOutMatrix.m23 += translation.z * inOutMatrix.m33;
}

/// <summary>
/// Multiplies a matrix with a perspective matrix. This function is faster than performing the full matrix multiplication.
/// The operation order is perspective * rhs.
/// </summary>
/// <param name="perspective">The perspective matrix to multiply with rhs.</param>
/// <param name="rhs">A matrix to be multiply to perspective.</param>
public static Matrix4x4 MultiplyPerspectiveMatrix(Matrix4x4 perspective, Matrix4x4 rhs)
{
Matrix4x4 outMat;
outMat.m00 = perspective.m00 * rhs.m00;
outMat.m01 = perspective.m00 * rhs.m01;
outMat.m02 = perspective.m00 * rhs.m02;
outMat.m03 = perspective.m00 * rhs.m03;

outMat.m10 = perspective.m11 * rhs.m10;
outMat.m11 = perspective.m11 * rhs.m11;
outMat.m12 = perspective.m11 * rhs.m12;
outMat.m13 = perspective.m11 * rhs.m13;

outMat.m20 = perspective.m22 * rhs.m20 + perspective.m23 * rhs.m30;
outMat.m21 = perspective.m22 * rhs.m21 + perspective.m23 * rhs.m31;
outMat.m22 = perspective.m22 * rhs.m22 + perspective.m23 * rhs.m32;
outMat.m23 = perspective.m22 * rhs.m23 + perspective.m23 * rhs.m33;

outMat.m30 = -rhs.m20;
outMat.m31 = -rhs.m21;
outMat.m32 = -rhs.m22;
outMat.m33 = -rhs.m23;

return outMat;
}

// An orthographic projection is centered if (right+left) == 0 and (top+bottom) == 0
private static Matrix4x4 MultiplyOrthoMatrixCentered(Matrix4x4 ortho, Matrix4x4 rhs)
{
Matrix4x4 outMat;
outMat.m00 = ortho.m00 * rhs.m00;
outMat.m01 = ortho.m00 * rhs.m01;
outMat.m02 = ortho.m00 * rhs.m02;
outMat.m03 = ortho.m00 * rhs.m03;

outMat.m10 = ortho.m11 * rhs.m10;
outMat.m11 = ortho.m11 * rhs.m11;
outMat.m12 = ortho.m11 * rhs.m12;
outMat.m13 = ortho.m11 * rhs.m13;

outMat.m20 = ortho.m22 * rhs.m20 + ortho.m23 * rhs.m30;
outMat.m21 = ortho.m22 * rhs.m21 + ortho.m23 * rhs.m31;
outMat.m22 = ortho.m22 * rhs.m22 + ortho.m23 * rhs.m32;
outMat.m23 = ortho.m22 * rhs.m23 + ortho.m23 * rhs.m33;

outMat.m30 = rhs.m20;
outMat.m31 = rhs.m21;
outMat.m32 = rhs.m22;
outMat.m33 = rhs.m23;

return outMat;
}

// General case has m03 and m13 != 0
private static Matrix4x4 MultiplyGenericOrthoMatrix(Matrix4x4 ortho, Matrix4x4 rhs)
{
Matrix4x4 outMat;
outMat.m00 = ortho.m00 * rhs.m00 + ortho.m03 * rhs.m30;
outMat.m01 = ortho.m00 * rhs.m01 + ortho.m03 * rhs.m31;
outMat.m02 = ortho.m00 * rhs.m02 + ortho.m03 * rhs.m32;
outMat.m03 = ortho.m00 * rhs.m03 + ortho.m03 * rhs.m33;

outMat.m10 = ortho.m11 * rhs.m10 + ortho.m13 * rhs.m30;
outMat.m11 = ortho.m11 * rhs.m11 + ortho.m13 * rhs.m31;
outMat.m12 = ortho.m11 * rhs.m12 + ortho.m13 * rhs.m32;
outMat.m13 = ortho.m11 * rhs.m13 + ortho.m13 * rhs.m33;

outMat.m20 = ortho.m22 * rhs.m20 + ortho.m23 * rhs.m30;
outMat.m21 = ortho.m22 * rhs.m21 + ortho.m23 * rhs.m31;
outMat.m22 = ortho.m22 * rhs.m22 + ortho.m23 * rhs.m32;
outMat.m23 = ortho.m22 * rhs.m23 + ortho.m23 * rhs.m33;

outMat.m30 = rhs.m20;
outMat.m31 = rhs.m21;
outMat.m32 = rhs.m22;
outMat.m33 = rhs.m23;
return outMat;
}

/// <summary>
/// Multiplies a matrix with an orthographic matrix. This function is faster than performing the full matrix multiplication.
/// The operation order is ortho * rhs.
/// </summary>
/// <param name="ortho">The ortho matrix to multiply with rhs.</param>
/// <param name="rhs">A matrix to be multiply to perspective.</param>
/// <param name="centered">If true, it means that right and left are equivalently distant from center and similarly top/bottom are equivalently distant from center.</param>
public static Matrix4x4 MultiplyOrthoMatrix(Matrix4x4 ortho, Matrix4x4 rhs, bool centered)
{
return centered ? MultiplyGenericOrthoMatrix(ortho, rhs) : MultiplyOrthoMatrixCentered(ortho, rhs);
}


/// <summary>
/// Multiplies a matrix with a projection matrix. This function is faster than performing the full matrix multiplication.
/// The operation order is projMatrix * rhs.
/// </summary>
/// <param name="projMatrix">The projection matrix to multiply with rhs.</param>
/// <param name="rhs">A matrix to be multiply to perspective.</param>
/// <param name="orthoCentered">If true, the projection matrix is a centered ( right+left == top+bottom == 0) orthographic projection, otherwise it is a perspective matrix..</param>
public static Matrix4x4 MultiplyProjectionMatrix(Matrix4x4 projMatrix, Matrix4x4 rhs, bool orthoCentered)
{
return orthoCentered
? MultiplyOrthoMatrixCentered(projMatrix, rhs)
: MultiplyPerspectiveMatrix(projMatrix, rhs);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions com.unity.render-pipelines.high-definition/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Moved scene view camera settings for HDRP from the preferences window to the scene view camera settings window.
- Updated shaders to be compatible with Microsoft's DXC.
- Debug exposure in debug menu have been replace to debug exposure compensation in EV100 space and is always visible.
- Further optimized PrepareLightsForGPU (3x faster with few shadows, 1.4x faster with a lot of shadows or equivalently cost reduced by 68% to 37%).

## [7.1.1] - 2019-09-05

Expand Down
Loading