Skip to content

Fixed the point distribution for the diffuse denoiser sometimes not being properly intialized. #6194

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
Nov 4, 2021
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
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 @@ -49,6 +49,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fixed Z axis orientation when sampling 3D textures in local volumetric fog.
- Fixed geometry scale issue with the Eye Shader.
- Fixed motion vector buffer not accessible from custom passes in the BeforeTransparent, BeforePreRefraction and AfterDepthAndNormal injection points.
- Fixed the point distribution for the diffuse denoiser sometimes not being properly intialized.

### Changed
- Use RayTracingAccelerationStructure.CullInstances to filter Renderers and populate the acceleration structure with ray tracing instances for improved CPU performance on the main thread.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,32 @@ class HDDiffuseDenoiser
// Resources used for the de-noiser
ComputeShader m_DiffuseDenoiser;

HDRenderPipeline m_RenderPipeline;
// Runtime Initialization data
bool m_DenoiserInitialized;
Texture2D m_OwnenScrambledTexture;
ComputeBuffer m_PointDistribution;

// Kernels that may be required
int m_BilateralFilterSingleKernel;
int m_BilateralFilterColorKernel;
int m_GatherSingleKernel;
int m_GatherColorKernel;
ComputeBuffer m_PointDistribution;

public void Init(HDRenderPipelineRuntimeResources rpResources, HDRenderPipeline renderPipeline)
{
// Keep track of the resources
m_DiffuseDenoiser = rpResources.shaders.diffuseDenoiserCS;

m_RenderPipeline = renderPipeline;

// Grab all the kernels we'll eventually need
m_BilateralFilterSingleKernel = m_DiffuseDenoiser.FindKernel("BilateralFilterSingle");
m_BilateralFilterColorKernel = m_DiffuseDenoiser.FindKernel("BilateralFilterColor");
m_GatherSingleKernel = m_DiffuseDenoiser.FindKernel("GatherSingle");
m_GatherColorKernel = m_DiffuseDenoiser.FindKernel("GatherColor");

// Generate the point distribution
int m_GeneratePointDistributionKernel = m_DiffuseDenoiser.FindKernel("GeneratePointDistribution");
// Data required for the online initialization
m_DenoiserInitialized = false;
m_OwnenScrambledTexture = rpResources.textures.owenScrambledRGBATex;
m_PointDistribution = new ComputeBuffer(16 * 2 * 4, sizeof(float));
m_DiffuseDenoiser.SetTexture(m_GeneratePointDistributionKernel, HDShaderIDs._OwenScrambledRGTexture, rpResources.textures.owenScrambledRGBATex);
m_DiffuseDenoiser.SetBuffer(m_GeneratePointDistributionKernel, "_PointDistributionRW", m_PointDistribution);
m_DiffuseDenoiser.Dispatch(m_GeneratePointDistributionKernel, 1, 1, 1);
}

public void Release()
Expand All @@ -52,6 +50,7 @@ class DiffuseDenoiserPassData
public int viewCount;

// Denoising parameters
public bool needInit;
public float pixelSpreadTangent;
public float kernelSize;
public bool halfResolutionFilter;
Expand All @@ -67,6 +66,7 @@ class DiffuseDenoiserPassData
public ComputeBufferHandle pointDistribution;
public ComputeShader diffuseDenoiserCS;

public Texture2D owenScrambledTexture;
public TextureHandle depthStencilBuffer;
public TextureHandle normalBuffer;
public TextureHandle noisyBuffer;
Expand All @@ -91,7 +91,11 @@ public TextureHandle Denoise(RenderGraph renderGraph, HDCamera hdCamera, Diffuse
// Cannot run in async
builder.EnableAsyncCompute(false);

// Fetch all the resources
// Initialization data
passData.needInit = !m_DenoiserInitialized;
m_DenoiserInitialized = true;
passData.owenScrambledTexture = m_OwnenScrambledTexture;

// Camera parameters
if (denoiserParams.fullResolutionInput)
{
Expand All @@ -110,7 +114,7 @@ public TextureHandle Denoise(RenderGraph renderGraph, HDCamera hdCamera, Diffuse
passData.kernelSize = denoiserParams.kernelSize;
passData.halfResolutionFilter = denoiserParams.halfResolutionFilter;
passData.jitterFilter = denoiserParams.jitterFilter;
passData.frameIndex = m_RenderPipeline.RayTracingFrameIndex(hdCamera);
passData.frameIndex = HDRenderPipeline.RayTracingFrameIndex(hdCamera);
passData.fullResolutionInput = denoiserParams.fullResolutionInput;

// Kernels
Expand All @@ -130,6 +134,15 @@ public TextureHandle Denoise(RenderGraph renderGraph, HDCamera hdCamera, Diffuse
builder.SetRenderFunc(
(DiffuseDenoiserPassData data, RenderGraphContext ctx) =>
{
// Generate the point distribution if needed (this is only ran once)
if (passData.needInit)
{
int m_GeneratePointDistributionKernel = data.diffuseDenoiserCS.FindKernel("GeneratePointDistribution");
ctx.cmd.SetComputeTextureParam(data.diffuseDenoiserCS, m_GeneratePointDistributionKernel, HDShaderIDs._OwenScrambledRGTexture, data.owenScrambledTexture);
ctx.cmd.SetComputeBufferParam(data.diffuseDenoiserCS, m_GeneratePointDistributionKernel, "_PointDistributionRW", data.pointDistribution);
ctx.cmd.DispatchCompute(data.diffuseDenoiserCS, m_GeneratePointDistributionKernel, 1, 1, 1);
}

// Evaluate the dispatch parameters
int areaTileSize = 8;
int numTilesX = (data.texWidth + (areaTileSize - 1)) / areaTileSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ static internal bool ValidRayTracingHistory(HDCamera hdCamera)
&& hdCamera.historyRTHandleProperties.previousViewportSize.y == hdCamera.actualHeight;
}

internal int RayTracingFrameIndex(HDCamera hdCamera)
internal static int RayTracingFrameIndex(HDCamera hdCamera)
{
#if UNITY_HDRP_DXR_TESTS_DEFINE
if (Application.isPlaying)
Expand Down