Skip to content

Fixing the intensity being applied to RTAO too early leading to unexpected results (1254626). #825

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

Merged
merged 2 commits into from
Jun 11, 2020
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 @@ -672,6 +672,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fixed an issue where rendering preview with MSAA might generate render graph errors.
- Fixed compile error in PS4 for planar reflection filtering.
- Fixed issue with blue line in prefabs for volume mode.
- Fixing the internsity being applied to RTAO too early leading to unexpected results (1254626).

### Changed
- Improve MIP selection for decals on Transparents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand Down Expand Up @@ -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<AmbientOcclusion>();
RayCountManager rayCountManager = m_RenderPipeline.GetRayCountManager();

Expand All @@ -79,33 +83,31 @@ 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;
globalCB._RaytracingNumSamples = aoSettings.sampleCount;
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)))
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ 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}
indirectDiffuseRaytracingRT: {fileID: 4807578003741378534, guid: f9b74dc8a89fc7c489e244f0be35baa8,
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,
Expand Down