From fb0c22aeeeb532f6ff69698560a4d94598e5afe3 Mon Sep 17 00:00:00 2001 From: Emmanuel Turquin Date: Fri, 15 Jan 2021 11:32:09 +0100 Subject: [PATCH 1/2] Added alpha support changes for path tracing in bugfix branch. --- .../Accumulation/Shaders/Accumulation.compute | 12 +++++++----- .../PathTracing/Shaders/PathTracingIntersection.hlsl | 5 ++++- .../PathTracing/Shaders/PathTracingMain.raytrace | 11 ++++++----- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Accumulation/Shaders/Accumulation.compute b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Accumulation/Shaders/Accumulation.compute index 2b9c8159d3f..be5d5af81ea 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Accumulation/Shaders/Accumulation.compute +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Accumulation/Shaders/Accumulation.compute @@ -51,13 +51,15 @@ void KMain(uint3 dispatchThreadId : SV_DispatchThreadID) UNITY_XR_ASSIGN_VIEW_INDEX(dispatchThreadId.z); uint2 currentPixelCoord = dispatchThreadId.xy; - float exposureMultiplier = (_AccumulationNeedsExposure != 0) ? GetCurrentExposureMultiplier() : 1.0; + float4 exposureMultiplier; + exposureMultiplier.xyz = (_AccumulationNeedsExposure != 0) ? GetCurrentExposureMultiplier() : 1.0; + exposureMultiplier.w = 1.0; // Have we reached max sampling? uint sampleCount = _AccumulationFrameIndex; if (sampleCount >= _AccumulationNumSamples) { - _CameraColorTextureRW[COORD_TEXTURE2D_X(currentPixelCoord)] = float4(_AccumulatedFrameTexture[COORD_TEXTURE2D_X(currentPixelCoord)].xyz * exposureMultiplier, 1.0); + _CameraColorTextureRW[COORD_TEXTURE2D_X(currentPixelCoord)] = _AccumulatedFrameTexture[COORD_TEXTURE2D_X(currentPixelCoord)] * exposureMultiplier; } else { @@ -68,9 +70,9 @@ void KMain(uint3 dispatchThreadId : SV_DispatchThreadID) #endif if (sampleCount++) - color.xyz = (_AccumulatedFrameTexture[COORD_TEXTURE2D_X(currentPixelCoord)].xyz * _AccumulationWeights.y + _AccumulationWeights.x * color.xyz) * _AccumulationWeights.z; + color = (_AccumulatedFrameTexture[COORD_TEXTURE2D_X(currentPixelCoord)] * _AccumulationWeights.y + _AccumulationWeights.x * color) * _AccumulationWeights.z; - _AccumulatedFrameTexture[COORD_TEXTURE2D_X(currentPixelCoord)] = float4(color.xyz, 1.0); + _AccumulatedFrameTexture[COORD_TEXTURE2D_X(currentPixelCoord)] = color; // Apply exposure modifier color *= exposureMultiplier; @@ -78,6 +80,6 @@ void KMain(uint3 dispatchThreadId : SV_DispatchThreadID) // Add a little convergence cue to our result AddConvergenceCue(currentPixelCoord, sampleCount, color.xyz); - _CameraColorTextureRW[COORD_TEXTURE2D_X(currentPixelCoord)] = float4(color.xyz, 1.0); + _CameraColorTextureRW[COORD_TEXTURE2D_X(currentPixelCoord)] = color; } } diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl index 42fe7f6a366..b803427b1b7 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl @@ -6,12 +6,15 @@ // Structure that defines the current state of the intersection, for path tracing struct PathIntersection { + // t as in: O + t*D = H (i.e. distance between O and H, if D is normalized) float t; // Resulting value (often color) of the ray float3 value; + // Resulting alpha (camera rays only) + float alpha; // Cone representation of the ray RayCone cone; - // The remaining available depth for the current Ray + // The remaining available depth for the current ray uint remainingDepth; // Pixel coordinate from which the initial ray was launched uint2 pixelCoord; diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingMain.raytrace b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingMain.raytrace index 04b8b7e47dc..3e6aae936be 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingMain.raytrace +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingMain.raytrace @@ -25,7 +25,7 @@ // Input(s) float4x4 _PixelCoordToViewDirWS; int _RaytracingCameraSkyEnabled; -float3 _RaytracingCameraClearColor; +float4 _RaytracingCameraClearColor; // DoF related parameters float4 _PathTracedDoFConstants; // x: aperture radius, y: focus distance, zw: unused @@ -36,9 +36,9 @@ RWTexture2D _RadianceTexture; [shader("miss")] void MissCamera(inout PathIntersection pathIntersection : SV_RayPayload) { - // If the _RaytracingCameraClearColor content is positive, we override the camera sky display with a bg color - pathIntersection.value = _EnvLightSkyEnabled && _RaytracingCameraSkyEnabled ? - SampleSkyTexture(WorldRayDirection(), 0.0, 0).xyz : _RaytracingCameraClearColor * GetInverseCurrentExposureMultiplier(); + bool skyEnabled = _EnvLightSkyEnabled && _RaytracingCameraSkyEnabled; + pathIntersection.value = skyEnabled ? SampleSkyTexture(WorldRayDirection(), 0.0, 0).xyz : _RaytracingCameraClearColor.xyz * GetInverseCurrentExposureMultiplier(); + pathIntersection.alpha = skyEnabled ? 1.0 : _RaytracingCameraClearColor.w; ApplyFogAttenuation(WorldRayOrigin(), WorldRayDirection(), pathIntersection.value); @@ -137,6 +137,7 @@ void RayGen() // Create and init the PathIntersection structure for this PathIntersection pathIntersection; pathIntersection.value = 1.0; + pathIntersection.alpha = 1.0; pathIntersection.remainingDepth = _RaytracingMaxRecursion; pathIntersection.pixelCoord = currentPixelCoord; pathIntersection.maxRoughness = 0.0; @@ -148,7 +149,7 @@ void RayGen() // Evaluate the ray intersection TraceRay(_RaytracingAccelerationStructure, RAY_FLAG_CULL_BACK_FACING_TRIANGLES, RAYTRACINGRENDERERFLAG_PATH_TRACING, 0, 1, 0, rayDescriptor, pathIntersection); - _RadianceTexture[currentPixelCoord] = float4(pathIntersection.value, 1.0); + _RadianceTexture[currentPixelCoord] = float4(pathIntersection.value, pathIntersection.alpha); } // This should never be called, return magenta just in case From 09d62d5370284bbf876d288df30571f0071aceec Mon Sep 17 00:00:00 2001 From: Emmanuel Turquin Date: Fri, 15 Jan 2021 12:31:24 +0100 Subject: [PATCH 2/2] Updated changelog and added doc. --- com.unity.render-pipelines.high-definition/CHANGELOG.md | 1 + .../Documentation~/Alpha-Output.md | 2 ++ 2 files changed, 3 insertions(+) diff --git a/com.unity.render-pipelines.high-definition/CHANGELOG.md b/com.unity.render-pipelines.high-definition/CHANGELOG.md index 5fd18cb6302..692d8b7bef5 100644 --- a/com.unity.render-pipelines.high-definition/CHANGELOG.md +++ b/com.unity.render-pipelines.high-definition/CHANGELOG.md @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Fixed spacing of UI widgets in the Graphics Compositor (case 1305638). - Fixed undo-redo on layered lit editor. - Fixed tesselation culling, big triangles using lit tesselation shader would dissapear when camera is too close to them (case 1299116) +- Fixed path tracing alpha channel support (case 1304187). ### Changed - Change the source value for the ray tracing frame index iterator from m_FrameCount to the camera frame count (case 1301356). diff --git a/com.unity.render-pipelines.high-definition/Documentation~/Alpha-Output.md b/com.unity.render-pipelines.high-definition/Documentation~/Alpha-Output.md index 745072311fd..9e06b0f9ac5 100644 --- a/com.unity.render-pipelines.high-definition/Documentation~/Alpha-Output.md +++ b/com.unity.render-pipelines.high-definition/Documentation~/Alpha-Output.md @@ -14,6 +14,8 @@ Rendering Buffer Format | Post-processing Buffer Format | Alpha Output **R16G16B16A16** | **R11G11B10** | Alpha channel without post-processing (AlphaCopy) **R16G16B16A16** | **R16G16B16A16** | Alpha channel with post-processing +Note that alpha output is also supported in [Path Tracing](Ray-Tracing-Path-Tracing.md). + ## DoF and Alpha Output Another case which might require post-processing of the alpha channel is for scenes that use Depth Of Field. In this case, if the alpha is not processed, compositing will result in a sharp cut-off of an object that should appear blurred. This is better is illustrated in the images below: