Skip to content

Commit

Permalink
Fix path traced DoF focusing issue (#732)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmavridis committed Jun 5, 2020
1 parent d573175 commit 37e93e7
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 42 deletions.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

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 @@ -637,6 +637,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fixed a vulkan and metal warning in the SSGI compute shader.
- Fixed an exception due to the color pyramid not allocated when SSGI is enabled.
- Fixed an issue with the first Depth history was incorrectly copied.
- Fixed path traced DoF focusing issue

### Changed
- Improve MIP selection for decals on Transparents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ void RenderPathTracing(HDCamera hdCamera, CommandBuffer cmd, RTHandle outputText
cmd.SetRayTracingTextureParam(pathTracingShader, HDShaderIDs._RadianceTexture, m_RadianceTexture);
cmd.SetRayTracingMatrixParam(pathTracingShader, HDShaderIDs._PixelCoordToViewDirWS, hdCamera.mainViewConstants.pixelCoordToViewDirWS);
cmd.SetRayTracingVectorParam(pathTracingShader, HDShaderIDs._PathTracedDoFConstants, ComputeDoFConstants(hdCamera, m_PathTracingSettings));
cmd.SetRayTracingVectorParam(pathTracingShader, HDShaderIDs._InvViewportScaleBias, HDUtils.ComputeInverseViewportScaleBias(hdCamera));

// Run the computation
cmd.DispatchRays(pathTracingShader, "RayGen", (uint)hdCamera.actualWidth, (uint)hdCamera.actualHeight, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ float3 _RaytracingCameraClearColor;

// DoF related parameters
float4 _PathTracedDoFConstants; // x: aperture radius, y: focus distance, zw: unused
float4 _InvViewportScaleBias;

// Output(s)
RWTexture2D<float4> _RadianceTexture;
Expand Down Expand Up @@ -71,39 +70,30 @@ void RayGen()
jitteredPixelCoord.x += GetSample(currentPixelCoord, _RaytracingSampleIndex, 40);
jitteredPixelCoord.y += GetSample(currentPixelCoord, _RaytracingSampleIndex, 41);

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

float apertureRadius = _PathTracedDoFConstants.x;
if (apertureRadius == 0.0)
{
// Compute the ray direction from those coordinates (fast path for zero aperture)
directionWS = -normalize(mul(jitteredPixelCoord, (float3x3)_PixelCoordToViewDirWS));
cameraPosWS = _WorldSpaceCameraPos;
}
else
if (apertureRadius > 0.0)
{
// Compute the ray origin and direction for a lens with non-zero aperture

// Apply the inverse viewport transform to go from viewport coordinates to NDC
jitteredPixelCoord.xy = jitteredPixelCoord.xy * _InvViewportScaleBias.xy + _InvViewportScaleBias.zw;

// Sample the lens apperture using the next available dimensions (we use 40 for path tracing, 2 for sub-pixel jittering, 64 for SSS -> 106, 107)
float r1 = GetSample(currentPixelCoord, _RaytracingSampleIndex, 106);
float r2 = GetSample(currentPixelCoord, _RaytracingSampleIndex, 107);
float2 uv = apertureRadius * SampleDiskUniform(r1, r2);

// Compute the new ray origin ( _ViewMatrix[0] = right, _ViewMatrix[1] = up, _ViewMatrix[2] = forward )
float focusDistance = _PathTracedDoFConstants.y;
float3 focusPoint = _WorldSpaceCameraPos - _ViewMatrix[2] * focusDistance;
cameraPosWS = _WorldSpaceCameraPos + _ViewMatrix[0] * uv.x + _ViewMatrix[1] * uv.y;
cameraPosWS += _ViewMatrix[0] * uv.x + _ViewMatrix[1] * uv.y;

// Create the new view matrix
float3 newForward = normalize(focusPoint - cameraPosWS);
float3 newRight = cross(newForward, _ViewMatrix[1]);
float3x3 newViewMatrix = GetLocalFrame(newForward, newRight);
// Compute the focus point by intersecting the pinhole ray with the focus plane
float focusDistance = _PathTracedDoFConstants.y;
float t = focusDistance / dot(directionWS, _ViewMatrix[2]);
float3 focusPointWS = _WorldSpaceCameraPos - t * directionWS;

directionWS = normalize(mul(jitteredPixelCoord, newViewMatrix));
// The new ray direction should pass through the focus point
directionWS = normalize(focusPointWS - cameraPosWS);
}

// Create the ray descriptor for this pixel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,23 +196,6 @@ internal static Matrix4x4 ComputePixelCoordToWorldSpaceViewDirectionMatrix(float
return Matrix4x4.Transpose(worldToViewMatrix.transpose * viewSpaceRasterTransform);
}

// Scale and bias to transform unnormalized viewport/pixel coordinates to normalized device coordinates
internal static Vector4 ComputeInverseViewportScaleBias(HDCamera hdCamera)
{
float verticalFoV = hdCamera.camera.GetGateFittedFieldOfView() * Mathf.Deg2Rad;
Vector2 lensShift = hdCamera.camera.GetGateFittedLensShift();

float aspectRatio = hdCamera.camera.aspect < 0 ? hdCamera.screenSize.x * hdCamera.screenSize.w : hdCamera.camera.aspect;
float tanHalfVertFoV = Mathf.Tan(0.5f * verticalFoV);

// See the comment in ComputePixelCoordToWorldSpaceViewDirectionMatrix for the derivation
return new Vector4(
-2.0f * hdCamera.screenSize.z * tanHalfVertFoV * aspectRatio,
-2.0f * hdCamera.screenSize.w * tanHalfVertFoV,
(1.0f - 2.0f * lensShift.x) * tanHalfVertFoV * aspectRatio,
(1.0f - 2.0f * lensShift.y) * tanHalfVertFoV);
}

internal static float ComputZPlaneTexelSpacing(float planeDepth, float verticalFoV, float resolutionY)
{
float tanHalfVertFoV = Mathf.Tan(0.5f * verticalFoV);
Expand Down

0 comments on commit 37e93e7

Please sign in to comment.