Skip to content

Commit 3199087

Browse files
[HDRP][Path Tracing] Added proper support for interleaved tiling (#5953)
* Added ortho cam support, plus raygen refactor. * Added support for interleaved tiling. * Added spread angle adjustment. * Offset tile sub-pixels, instead of relying on proj matrix modifications. * Undoed last commit. * Use tiled pixel coords for all things sampling-related (incl. lens). * Update CHANGELOG.md Co-authored-by: sebastienlagarde <sebastien@unity3d.com>
1 parent 9880950 commit 3199087

File tree

4 files changed

+31
-18
lines changed

4 files changed

+31
-18
lines changed

com.unity.render-pipelines.high-definition/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2222
- Fix API warnings in Matcap mode on Metal.
2323
- Fix D3D validation layer errors w.r.t shadow textures when an atlas is not used.
2424
- Fixed compatibility message not displayed correctly when switching platforms.
25+
- Fixed support for interleaved tiling in path tracing.
2526

2627
### Changed
2728
- Changed the max distance for Light Anchors to avoid unstability with high values (case 1362802).

com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDStringConstants.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,9 @@ static class HDShaderIDs
589589
public static readonly string _RaytracingAccelerationStructureName = "_RaytracingAccelerationStructure";
590590

591591
// Path tracing variables
592-
public static readonly int _PathTracedDoFConstants = Shader.PropertyToID("_PathTracedDoFConstants");
593592
public static readonly int _InvViewportScaleBias = Shader.PropertyToID("_InvViewportScaleBias");
593+
public static readonly int _PathTracingDoFParameters = Shader.PropertyToID("_PathTracingDoFParameters");
594+
public static readonly int _PathTracingTilingParameters = Shader.PropertyToID("_PathTracingTilingParameters");
594595

595596
// Light Cluster
596597
public static readonly int _LightDatasRT = Shader.PropertyToID("_LightDatasRT");

com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ public sealed class PathTracing : VolumeComponent
5151
[Tooltip("Defines the maximum, post-exposed luminance computed for indirect path segments. Lower values help against noise and fireflies (very bright pixels), but introduce bias by darkening the overall result. Increase this value if your image looks too dark.")]
5252
public MinFloatParameter maximumIntensity = new MinFloatParameter(10f, 0f);
5353

54+
/// <summary>
55+
/// Defines the number of tiles (X: width, Y: height) and the indices of the current tile (Z: i in [0, width[, W: j in [0, height[) for interleaved tiled rendering.
56+
/// </summary>
57+
[Tooltip("Defines the number of tiles (X: width, Y: height) and the indices of the current tile (Z: i in [0, width[, W: j in [0, height[) for interleaved tiled rendering.")]
58+
public Vector4Parameter tilingParameters = new Vector4Parameter(new Vector4(1, 1, 0, 0));
59+
5460
/// <summary>
5561
/// Default constructor for the path tracing volume component.
5662
/// </summary>
@@ -253,6 +259,7 @@ class RenderPathTracingData
253259
public Texture skyReflection;
254260
public Matrix4x4 pixelCoordToViewDirWS;
255261
public Vector4 dofParameters;
262+
public Vector4 tilingParameters;
256263
public int width, height;
257264
public RayTracingAccelerationStructure accelerationStructure;
258265
public HDRaytracingLightCluster lightCluster;
@@ -272,6 +279,7 @@ TextureHandle RenderPathTracing(RenderGraph renderGraph, HDCamera hdCamera, in C
272279
passData.skyReflection = m_SkyManager.GetSkyReflection(hdCamera);
273280
passData.pixelCoordToViewDirWS = hdCamera.mainViewConstants.pixelCoordToViewDirWS;
274281
passData.dofParameters = ComputeDoFConstants(hdCamera, m_PathTracingSettings);
282+
passData.tilingParameters = m_PathTracingSettings.tilingParameters.value;
275283
passData.width = hdCamera.actualWidth;
276284
passData.height = hdCamera.actualHeight;
277285
passData.accelerationStructure = RequestAccelerationStructure();
@@ -319,7 +327,8 @@ TextureHandle RenderPathTracing(RenderGraph renderGraph, HDCamera hdCamera, in C
319327
// Additional data for path tracing
320328
ctx.cmd.SetRayTracingTextureParam(data.pathTracingShader, HDShaderIDs._FrameTexture, data.output);
321329
ctx.cmd.SetRayTracingMatrixParam(data.pathTracingShader, HDShaderIDs._PixelCoordToViewDirWS, data.pixelCoordToViewDirWS);
322-
ctx.cmd.SetRayTracingVectorParam(data.pathTracingShader, HDShaderIDs._PathTracedDoFConstants, data.dofParameters);
330+
ctx.cmd.SetRayTracingVectorParam(data.pathTracingShader, HDShaderIDs._PathTracingDoFParameters, data.dofParameters);
331+
ctx.cmd.SetRayTracingVectorParam(data.pathTracingShader, HDShaderIDs._PathTracingTilingParameters, data.tilingParameters);
323332

324333
// Run the computation
325334
ctx.cmd.DispatchRays(data.pathTracingShader, "RayGen", (uint)data.width, (uint)data.height, 1);

com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingMain.raytrace

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ float4x4 _PixelCoordToViewDirWS;
2727
int _RaytracingCameraSkyEnabled;
2828
float4 _RaytracingCameraClearColor;
2929
TEXTURE2D_X(_SkyCameraTexture);
30+
float4 _PathTracingDoFParameters; // x: aperture radius, y: focus distance, zw: unused
31+
float4 _PathTracingTilingParameters; // xy: tile count, zw: current tile index
3032

3133
// Output(s)
3234
RW_TEXTURE2D_X(float4, _FrameTexture);
3335

34-
// DoF related parameters
35-
float4 _PathTracedDoFConstants; // x: aperture radius, y: focus distance, zw: unused
36-
3736
[shader("miss")]
3837
void MissCamera(inout PathIntersection pathIntersection : SV_RayPayload)
3938
{
@@ -92,19 +91,17 @@ void MissMaterial(inout PathIntersection pathIntersection : SV_RayPayload)
9291

9392
void ApplyDepthOfField(uint2 pixelCoord, float dotDirection, inout float3 origin, inout float3 direction)
9493
{
95-
float apertureRadius = _PathTracedDoFConstants.x;
96-
97-
if (apertureRadius <= 0.0)
94+
// Check aperture radius
95+
if (_PathTracingDoFParameters.x <= 0.0)
9896
return;
9997

10098
// Sample the lens aperture using the next available dimensions
10199
// (we use 40 for path tracing, 2 for sub-pixel jittering, 64 for SSS -> 106, 107)
102-
float2 uv = apertureRadius * SampleDiskUniform(GetSample(pixelCoord, _RaytracingSampleIndex, 106),
103-
GetSample(pixelCoord, _RaytracingSampleIndex, 107));
100+
float2 uv = _PathTracingDoFParameters.x * SampleDiskUniform(GetSample(pixelCoord, _RaytracingSampleIndex, 106),
101+
GetSample(pixelCoord, _RaytracingSampleIndex, 107));
104102

105103
// Compute the focus point by intersecting the pinhole ray with the focus plane
106-
float focusDistance = _PathTracedDoFConstants.y;
107-
float t = focusDistance / dotDirection;
104+
float t = _PathTracingDoFParameters.y / dotDirection;
108105
float3 focusPoint = origin + t * direction;
109106

110107
// Compute the new ray origin (_ViewMatrix[0] = right, _ViewMatrix[1] = up)
@@ -120,10 +117,15 @@ void RayGen()
120117
// Get the current pixel coordinates
121118
uint2 pixelCoord = DispatchRaysIndex().xy;
122119

120+
// Get the current tile coordinates (for interleaved tiling) and update pixel coordinates accordingly
121+
uint2 tileCount = uint2(_PathTracingTilingParameters.xy);
122+
uint2 tileIndex = uint2(_PathTracingTilingParameters.zw);
123+
uint2 tiledPixelCoord = pixelCoord * tileCount + tileIndex;
124+
123125
// Jitter them (we use 4x10 dimensions of our sequence during path tracing atm, so pick the next available ones)
124126
float4 jitteredPixelCoord = float4(pixelCoord, 1.0, 1.0);
125-
jitteredPixelCoord.x += GetSample(pixelCoord, _RaytracingSampleIndex, 40);
126-
jitteredPixelCoord.y += GetSample(pixelCoord, _RaytracingSampleIndex, 41);
127+
jitteredPixelCoord.x += GetSample(tiledPixelCoord, _RaytracingSampleIndex, 40) / tileCount.x;
128+
jitteredPixelCoord.y += GetSample(tiledPixelCoord, _RaytracingSampleIndex, 41) / tileCount.y;
127129

128130
// Create the ray descriptor for this pixel
129131
RayDesc ray;
@@ -143,7 +145,7 @@ void RayGen()
143145
float dotDirection = dot(cameraDirection, ray.Direction);
144146
ray.TMin /= dotDirection;
145147

146-
ApplyDepthOfField(pixelCoord, dotDirection, ray.Origin, ray.Direction);
148+
ApplyDepthOfField(tiledPixelCoord, dotDirection, ray.Origin, ray.Direction);
147149
}
148150
else // Orthographic projection
149151
{
@@ -156,16 +158,16 @@ void RayGen()
156158
ray.Direction = cameraDirection;
157159
}
158160

159-
// Create and init the PathIntersection structure for this
161+
// Create and init the PathIntersection structure for this pixel
160162
PathIntersection pathIntersection;
161163
pathIntersection.value = 1.0;
162164
pathIntersection.alpha = 1.0;
163165
pathIntersection.remainingDepth = _RaytracingMaxRecursion;
164-
pathIntersection.pixelCoord = pixelCoord;
166+
pathIntersection.pixelCoord = tiledPixelCoord;
165167
pathIntersection.maxRoughness = 0.0;
166168

167169
// In order to achieve filtering for the textures, we need to compute the spread angle of the pixel
168-
pathIntersection.cone.spreadAngle = _RaytracingPixelSpreadAngle;
170+
pathIntersection.cone.spreadAngle = _RaytracingPixelSpreadAngle / min(tileCount.x, tileCount.y);
169171
pathIntersection.cone.width = 0.0;
170172

171173
// Evaluate the ray intersection

0 commit comments

Comments
 (0)