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

Fix Asymmetric Projection Matrices and Fog / Pathtracing #4926

Merged
merged 6 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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 @@ -268,6 +268,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fixed custom pass volume not executed in scene view because of the volume culling mask.
- Fixed remapping of depth pyramid debug view
- Fix wobbling/tearing-like artifacts with SSAO.
- Fixed an issue with asymmetric projection matrices and fog / pathtracing. (case 1330290).

### Changed
- Changed Window/Render Pipeline/HD Render Pipeline Wizard to Window/Rendering/HDRP Wizard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1748,14 +1748,24 @@ Matrix4x4 GetJitteredProjectionMatrix(Matrix4x4 origProj)
Matrix4x4 ComputePixelCoordToWorldSpaceViewDirectionMatrix(ViewConstants viewConstants, Vector4 resolution, float aspect = -1)
{
// In XR mode, use a more generic matrix to account for asymmetry in the projection
if (xr.enabled)
var useGenericMatrix = xr.enabled;

// Asymmetry is also possible from a user-provided projection, so we must check for it too.
// Note however, that in case of physical camera, the lens shift term is the only source of
// asymmetry, and this is accounted for in the optimized path below. Additionally, Unity C++ will
// automatically disable physical camera when the projection is overridden by user.
useGenericMatrix |= HDUtils.IsProjectionMatrixAsymmetric(viewConstants.projMatrix) && !camera.usePhysicalProperties;

if (useGenericMatrix)
{
var transform = Matrix4x4.Scale(new Vector3(-1.0f, -1.0f, -1.0f)) * viewConstants.invViewProjMatrix;
transform = transform * Matrix4x4.Scale(new Vector3(1.0f, -1.0f, 1.0f));
transform = transform * Matrix4x4.Translate(new Vector3(-1.0f, -1.0f, 0.0f));
transform = transform * Matrix4x4.Scale(new Vector3(2.0f * resolution.z, 2.0f * resolution.w, 1.0f));
var viewSpaceRasterTransform = new Matrix4x4(
new Vector4(2.0f * resolution.z, 0.0f, 0.0f, -1.0f),
new Vector4(0.0f, -2.0f * resolution.w, 0.0f, 1.0f),
new Vector4(0.0f, 0.0f, 1.0f, 0.0f),
new Vector4(0.0f, 0.0f, 0.0f, 1.0f));

return transform.transpose;
var transformT = viewConstants.invViewProjMatrix.transpose * Matrix4x4.Scale(new Vector3(-1.0f, -1.0f, -1.0f));
return viewSpaceRasterTransform * transformT;
}

float verticalFoV = camera.GetGateFittedFieldOfView() * Mathf.Deg2Rad;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ void RayGen()
uint2 currentPixelCoord = uint2(LaunchIndex.x, LaunchIndex.y);

// Jitter them (we use 4x10 dimensions of our sequence during path tracing atm, so pick the next available ones)
float3 jitteredPixelCoord = float3(currentPixelCoord, 1.0);
float4 jitteredPixelCoord = float4(currentPixelCoord, 1.0, 1.0);
jitteredPixelCoord.x += GetSample(currentPixelCoord, _RaytracingSampleIndex, 40);
jitteredPixelCoord.y += GetSample(currentPixelCoord, _RaytracingSampleIndex, 41);

// Compute the ray direction from those coordinates (for zero aperture)
float3 directionWS = -normalize(mul(jitteredPixelCoord, (float3x3)_PixelCoordToViewDirWS));
// Compute the ray direction from those coordinates (for zero aperture
float3 directionWS = -normalize(mul(jitteredPixelCoord, _PixelCoordToViewDirWS).xyz);
float3 cameraPosWS = GetPrimaryCameraPosition();

float apertureRadius = _PathTracedDoFConstants.x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ internal static int GetRuntimeDebugPanelWidth(HDCamera hdCamera)
internal static float ProjectionMatrixAspect(in Matrix4x4 matrix)
=> - matrix.m11 / matrix.m00;

/// <summary>
/// Determine if a projection matrix is off-center (asymmetric).
/// </summary>
/// <param name="matrix"></param>
/// <returns></returns>
internal static bool IsProjectionMatrixAsymmetric(in Matrix4x4 matrix)
=> matrix.m02 != 0 || matrix.m12 != 0;

internal static Matrix4x4 ComputePixelCoordToWorldSpaceViewDirectionMatrix(float verticalFoV, Vector2 lensShift, Vector4 screenSize, Matrix4x4 worldToViewMatrix, bool renderToCubemap, float aspectRatio = -1, bool isOrthographic = false)
{
Matrix4x4 viewSpaceRasterTransform;
Expand Down