From cc3b7cdaf1f48b4a1b3c18cbd1cf97fc1bcfa352 Mon Sep 17 00:00:00 2001 From: Anis Date: Wed, 10 Jun 2020 16:59:40 +0200 Subject: [PATCH] Fixing the internsity being applied to RTAO too early leading to unexpected results (1254626). --- .../204_LocalAmbientOcclusionDenoised.png | 4 +-- .../CHANGELOG.md | 1 + .../HDRenderPipelineRayTracingResources.cs | 4 ++- .../HDRaytracingAmbientOcclusion.cs | 32 +++++++++++++------ .../RayTracingAmbientOcclusion.compute | 30 +++++++++++++++++ .../RayTracingAmbientOcclusion.compute.meta | 9 ++++++ .../RaytracingAmbientOcclusion.raytrace | 14 +++----- .../HDRenderPipelineRayTracingResources.asset | 4 +-- 8 files changed, 74 insertions(+), 24 deletions(-) create mode 100644 com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RayTracingAmbientOcclusion.compute create mode 100644 com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RayTracingAmbientOcclusion.compute.meta diff --git a/TestProjects/HDRP_DXR_Tests/Assets/ReferenceImages/Linear/WindowsEditor/Direct3D12/None/204_LocalAmbientOcclusionDenoised.png b/TestProjects/HDRP_DXR_Tests/Assets/ReferenceImages/Linear/WindowsEditor/Direct3D12/None/204_LocalAmbientOcclusionDenoised.png index 01f4e915152..00141814c3f 100644 --- a/TestProjects/HDRP_DXR_Tests/Assets/ReferenceImages/Linear/WindowsEditor/Direct3D12/None/204_LocalAmbientOcclusionDenoised.png +++ b/TestProjects/HDRP_DXR_Tests/Assets/ReferenceImages/Linear/WindowsEditor/Direct3D12/None/204_LocalAmbientOcclusionDenoised.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:c43cc43455a10d225759bfa8cc23884f8931d73b099c7f6ea9a182af925927c9 -size 514626 +oid sha256:43584696aff82c54a97062e1c192e64743257153567ed0da25796fe455e18bda +size 467284 diff --git a/com.unity.render-pipelines.high-definition/CHANGELOG.md b/com.unity.render-pipelines.high-definition/CHANGELOG.md index 08b3bc385dd..d9017f2cdad 100644 --- a/com.unity.render-pipelines.high-definition/CHANGELOG.md +++ b/com.unity.render-pipelines.high-definition/CHANGELOG.md @@ -662,6 +662,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Fix inconsistencies with transparent motion vectors and opaque by allowing camera only transparent motion vectors. - Fix reflection probe frame settings override - Fixed certain shadow bias artifacts present in volumetric lighting (case 1231885). +- Fixing the internsity being applied to RTAO too early leading to unexpected results (1254626). ### Changed - Improve MIP selection for decals on Transparents diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipelineRayTracingResources.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipelineRayTracingResources.cs index 5fa002e7482..e5ec0812a6f 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipelineRayTracingResources.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipelineRayTracingResources.cs @@ -43,7 +43,9 @@ partial class HDRenderPipelineRayTracingResources : ScriptableObject // Ambient Occlusion [Reload("Runtime/RenderPipeline/Raytracing/Shaders/RaytracingAmbientOcclusion.raytrace")] - public RayTracingShader aoRaytracing; + public RayTracingShader aoRaytracingRT; + [Reload("Runtime/RenderPipeline/Raytracing/Shaders/RaytracingAmbientOcclusion.compute")] + public ComputeShader aoRaytracingCS; // Sub-Surface Scattering [Reload("Runtime/RenderPipeline/Raytracing/Shaders/RayTracingSubSurface.raytrace")] diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/HDRaytracingAmbientOcclusion.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/HDRaytracingAmbientOcclusion.cs index e1f4b22aa01..9cc1dce3a09 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/HDRaytracingAmbientOcclusion.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/HDRaytracingAmbientOcclusion.cs @@ -12,6 +12,7 @@ internal class HDRaytracingAmbientOcclusion // The target denoising kernel static int m_KernelFilter; + static int m_RTAOApplyIntensityKernel; // Intermediate buffer that stores the ambient occlusion pre-denoising RTHandle m_AOIntermediateBuffer0 = null; @@ -36,6 +37,9 @@ public void Init(HDRenderPipeline renderPipeline) // keep track of the render pipeline m_RenderPipeline = renderPipeline; + // Grab the kernels we need + m_RTAOApplyIntensityKernel = m_PipelineRayTracingResources.aoRaytracingCS.FindKernel("RTAOApplyIntensity"); + // Allocate the intermediate textures m_AOIntermediateBuffer0 = RTHandles.Alloc(Vector2.one, TextureXR.slices, colorFormat: GraphicsFormat.R16G16B16A16_SFloat, dimension: TextureXR.dimension, enableRandomWrite: true, useDynamicScale: true, useMipMap: false, autoGenerateMips: false, name: "AOIntermediateBuffer0"); m_AOIntermediateBuffer1 = RTHandles.Alloc(Vector2.one, TextureXR.slices, colorFormat: GraphicsFormat.R16G16B16A16_SFloat, dimension: TextureXR.dimension, enableRandomWrite: true, useDynamicScale: true, useMipMap: false, autoGenerateMips: false, name: "AOIntermediateBuffer1"); @@ -69,7 +73,7 @@ public void RenderAO(HDCamera hdCamera, CommandBuffer cmd, RTHandle outputTextur return; } - RayTracingShader aoShader = m_PipelineRayTracingResources.aoRaytracing; + RayTracingShader aoShaderRT = m_PipelineRayTracingResources.aoRaytracingRT; var aoSettings = hdCamera.volumeStack.GetComponent(); RayCountManager rayCountManager = m_RenderPipeline.GetRayCountManager(); @@ -79,10 +83,10 @@ public void RenderAO(HDCamera hdCamera, CommandBuffer cmd, RTHandle outputTextur RayTracingAccelerationStructure accelerationStructure = m_RenderPipeline.RequestAccelerationStructure(); // Define the shader pass to use for the reflection pass - cmd.SetRayTracingShaderPass(aoShader, "VisibilityDXR"); + cmd.SetRayTracingShaderPass(aoShaderRT, "VisibilityDXR"); // Set the acceleration structure for the pass - cmd.SetRayTracingAccelerationStructure(aoShader, HDShaderIDs._RaytracingAccelerationStructureName, accelerationStructure); + cmd.SetRayTracingAccelerationStructure(aoShaderRT, HDShaderIDs._RaytracingAccelerationStructureName, accelerationStructure); // Inject the ray generation data (be careful of the global constant buffer limitation) globalCB._RaytracingRayMaxLength = aoSettings.rayLength; @@ -90,22 +94,20 @@ public void RenderAO(HDCamera hdCamera, CommandBuffer cmd, RTHandle outputTextur ConstantBuffer.PushGlobal(cmd, globalCB, HDShaderIDs._ShaderVariablesRaytracing); // Set the data for the ray generation - cmd.SetRayTracingTextureParam(aoShader, HDShaderIDs._DepthTexture, m_RenderPipeline.sharedRTManager.GetDepthStencilBuffer()); - cmd.SetRayTracingTextureParam(aoShader, HDShaderIDs._NormalBufferTexture, m_RenderPipeline.sharedRTManager.GetNormalBuffer()); + cmd.SetRayTracingTextureParam(aoShaderRT, HDShaderIDs._DepthTexture, m_RenderPipeline.sharedRTManager.GetDepthStencilBuffer()); + cmd.SetRayTracingTextureParam(aoShaderRT, HDShaderIDs._NormalBufferTexture, m_RenderPipeline.sharedRTManager.GetNormalBuffer()); // Inject the ray-tracing sampling data BlueNoise blueNoise = m_RenderPipeline.GetBlueNoiseManager(); blueNoise.BindDitheredRNGData8SPP(cmd); - // Value used to scale the ao intensity - cmd.SetRayTracingFloatParam(aoShader, HDShaderIDs._RaytracingAOIntensity, aoSettings.intensity.value); // Set the output textures - cmd.SetRayTracingTextureParam(aoShader, HDShaderIDs._RayCountTexture, rayCountManager.GetRayCountTexture()); - cmd.SetRayTracingTextureParam(aoShader, HDShaderIDs._AmbientOcclusionTextureRW, m_AOIntermediateBuffer0); + cmd.SetRayTracingTextureParam(aoShaderRT, HDShaderIDs._RayCountTexture, rayCountManager.GetRayCountTexture()); + cmd.SetRayTracingTextureParam(aoShaderRT, HDShaderIDs._AmbientOcclusionTextureRW, m_AOIntermediateBuffer0); // Run the computation - cmd.DispatchRays(aoShader, m_RayGenShaderName, (uint)hdCamera.actualWidth, (uint)hdCamera.actualHeight, (uint)hdCamera.viewCount); + cmd.DispatchRays(aoShaderRT, m_RayGenShaderName, (uint)hdCamera.actualWidth, (uint)hdCamera.actualHeight, (uint)hdCamera.viewCount); } using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.RaytracingFilterAmbientOcclusion))) @@ -137,6 +139,16 @@ public void RenderAO(HDCamera hdCamera, CommandBuffer cmd, RTHandle outputTextur { HDUtils.BlitCameraTexture(cmd, m_AOIntermediateBuffer0, outputTexture); } + + ComputeShader aoShaderCS = m_PipelineRayTracingResources.aoRaytracingCS; + cmd.SetComputeFloatParam(aoShaderCS, HDShaderIDs._RaytracingAOIntensity, aoSettings.intensity.value); + cmd.SetComputeTextureParam(aoShaderCS, m_RTAOApplyIntensityKernel, HDShaderIDs._AmbientOcclusionTextureRW, outputTexture); + int texWidth = hdCamera.actualWidth; + int texHeight = hdCamera.actualHeight; + int areaTileSize = 8; + int numTilesXHR = (texWidth + (areaTileSize - 1)) / areaTileSize; + int numTilesYHR = (texHeight + (areaTileSize - 1)) / areaTileSize; + cmd.DispatchCompute(aoShaderCS, m_RTAOApplyIntensityKernel, numTilesXHR, numTilesYHR, hdCamera.viewCount); } // Bind the textures and the params diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RayTracingAmbientOcclusion.compute b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RayTracingAmbientOcclusion.compute new file mode 100644 index 00000000000..39f232d6b37 --- /dev/null +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RayTracingAmbientOcclusion.compute @@ -0,0 +1,30 @@ +#pragma kernel RTAOApplyIntensity + +#pragma only_renderers d3d11 + +// HDRP generic includes +#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl" +#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl" +#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl" + +#pragma only_renderers d3d11 +// #pragma enable_d3d11_debug_symbols + +// Tile size of this compute +#define RAYTRACING_AMBIENT_OCCLUSION_TILE_SIZE 8 + +float4 _RaytracingAOIntensity; +RW_TEXTURE2D_X(float, _AmbientOcclusionTextureRW); + +[numthreads(RAYTRACING_AMBIENT_OCCLUSION_TILE_SIZE, RAYTRACING_AMBIENT_OCCLUSION_TILE_SIZE, 1)] +void RTAOApplyIntensity(uint3 dispatchThreadId : SV_DispatchThreadID, uint2 groupThreadId : SV_GroupThreadID, uint2 groupId : SV_GroupID) +{ + UNITY_XR_ASSIGN_VIEW_INDEX(dispatchThreadId.z); + + // Compute the pixel position to process + uint2 currentCoord = groupId * RAYTRACING_AMBIENT_OCCLUSION_TILE_SIZE + groupThreadId; + + // Grab the AO value without the intensity + float value = _AmbientOcclusionTextureRW[COORD_TEXTURE2D_X(currentCoord)]; + _AmbientOcclusionTextureRW[COORD_TEXTURE2D_X(currentCoord)] = 1.0 - pow(value, _RaytracingAOIntensity); +} diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RayTracingAmbientOcclusion.compute.meta b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RayTracingAmbientOcclusion.compute.meta new file mode 100644 index 00000000000..23c428125cf --- /dev/null +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RayTracingAmbientOcclusion.compute.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 10c05366baf9b0a44a827f3ef890b9e6 +ComputeShaderImporter: + externalObjects: {} + currentAPIMask: 262148 + preprocessorOverride: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RaytracingAmbientOcclusion.raytrace b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RaytracingAmbientOcclusion.raytrace index 6b9251ecba8..eb28657c786 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RaytracingAmbientOcclusion.raytrace +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RaytracingAmbientOcclusion.raytrace @@ -20,8 +20,7 @@ TEXTURE2D_X(_DepthTexture); // Output structure of the reflection raytrace shader -float _RaytracingAOIntensity; -RW_TEXTURE2D_X(float4, _AmbientOcclusionTextureRW); +RW_TEXTURE2D_X(float, _AmbientOcclusionTextureRW); [shader("miss")] void MissShaderAmbientOcclusion(inout RayIntersection rayIntersection : SV_RayPayload) @@ -41,7 +40,7 @@ void RayGenAmbientOcclusion() uint2 currentPixelCoord = uint2(LaunchIndex.x, LaunchIndex.y); // Reset the value of this pixel - _AmbientOcclusionTextureRW[COORD_TEXTURE2D_X(currentPixelCoord)] = float4(0.0f, 0.0f, 0.0f, 0.0f); + _AmbientOcclusionTextureRW[COORD_TEXTURE2D_X(currentPixelCoord)] = 0.0f; // Read the depth value float depthValue = LOAD_TEXTURE2D_X(_DepthTexture, currentPixelCoord).r; @@ -67,7 +66,7 @@ void RayGenAmbientOcclusion() } // Variable that accumulate the radiance - float3 finalColor = float3(0.0, 0.0, 0.0); + float finalColor = 0.0; // Let's loop through th e samples for (int i = 0; i < numSamples; ++i) @@ -102,17 +101,14 @@ void RayGenAmbientOcclusion() TraceRay(_RaytracingAccelerationStructure, RAY_FLAG_CULL_BACK_FACING_TRIANGLES, RAYTRACINGRENDERERFLAG_AMBIENT_OCCLUSION, 0, 1, 0, rayDescriptor, rayIntersection); // Accumulate this value - finalColor += rayIntersection.color; + finalColor += rayIntersection.color.x; } // Normalize the radiance finalColor /= (float)numSamples; - // Apply our intensity modifier - finalColor = pow(finalColor, _RaytracingAOIntensity); - // Alright we are done - _AmbientOcclusionTextureRW[COORD_TEXTURE2D_X(currentPixelCoord)] = float4(1.0 - finalColor, 1.0f); + _AmbientOcclusionTextureRW[COORD_TEXTURE2D_X(currentPixelCoord)] = finalColor; } // Fallback default any hit shader for this raytrace shader diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipelineResources/HDRenderPipelineRayTracingResources.asset b/com.unity.render-pipelines.high-definition/Runtime/RenderPipelineResources/HDRenderPipelineRayTracingResources.asset index 79825bec07c..6b1f4c98f31 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipelineResources/HDRenderPipelineRayTracingResources.asset +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipelineResources/HDRenderPipelineRayTracingResources.asset @@ -26,7 +26,6 @@ MonoBehaviour: shadowFilterCS: {fileID: 7200000, guid: f71fd853a538bf74e9e5a7228fc14dae, type: 3} forwardRaytracing: {fileID: 4807578003741378534, guid: d3a89a2d3f73b3e4da6f191e844fe68c, type: 3} - raytracingFlagMask: {fileID: 4800000, guid: 7822c8a69a1f21144a20cf6d84e5e706, type: 3} lightClusterBuildCS: {fileID: 7200000, guid: c0625ea908b52854bbf1d456e34026e4, type: 3} lightClusterDebugS: {fileID: 4800000, guid: c4d81c6e573560444bb1ea11ae4acfcb, type: 3} lightClusterDebugCS: {fileID: 7200000, guid: d48a3a5496d98a44c89f335934805d10, type: 3} @@ -34,8 +33,9 @@ MonoBehaviour: type: 3} indirectDiffuseRaytracingCS: {fileID: 7200000, guid: c5ad968b7cd39114d85dd860b3809087, type: 3} - aoRaytracing: {fileID: 4807578003741378534, guid: 82dc8cd069971d2488c502b0f32b94fb, + aoRaytracingRT: {fileID: 4807578003741378534, guid: 82dc8cd069971d2488c502b0f32b94fb, type: 3} + aoRaytracingCS: {fileID: 7200000, guid: 10c05366baf9b0a44a827f3ef890b9e6, type: 3} subSurfaceRayTracing: {fileID: 4807578003741378534, guid: b29a18f967c92364492508dddf78cff7, type: 3} subSurfaceRayTracingCS: {fileID: 7200000, guid: 4e5684a8dba46fe42a47642f9b0a6b89,