Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8aea89c
Fix near edges - remove old alpha normalization code
pmavridis Oct 21, 2020
33094ad
Fix alpha output
pmavridis Oct 21, 2020
c6d9d9f
Dof alpha output: preserve pixels with zero alpha
pmavridis Oct 21, 2020
1a24588
Physical DoF: generate color pyramid
pmavridis Oct 22, 2020
72cd729
Physical DoF: mip map selection based on radius
pmavridis Oct 22, 2020
d71fe33
Remove the preview indicator from the UI
pmavridis Oct 22, 2020
ecc26e7
Old (not rendergraph) pipeline implementation
pmavridis Oct 22, 2020
0c923d6
fix mem leak
pmavridis Oct 22, 2020
397a346
Support for much larger CoC radii
pmavridis Oct 22, 2020
88f90d7
Shader cleanup
pmavridis Oct 22, 2020
ed2d3c0
Tweak lod selection
pmavridis Oct 22, 2020
8fbea7d
Improve sampling noise
pmavridis Oct 22, 2020
82c6842
Fix post-dof TAA in rendergraph
pmavridis Oct 22, 2020
d755047
Shader cleanup
pmavridis Oct 23, 2020
31f309d
Bugfix for taa in DoF
pmavridis Oct 23, 2020
ccd8011
Bugfix for RTHandles: scale and clamp the UVs properly
pmavridis Oct 23, 2020
eaf9e1b
Temporary fix for Rendergraph post-dof TAA
pmavridis Oct 23, 2020
8aaf50f
Commit minor fixes before rebase
pmavridis Oct 23, 2020
4bbfc77
Revert "Remove the preview indicator from the UI"
pmavridis Oct 23, 2020
c29249b
Tweak parameter range to a more safe range
pmavridis Oct 23, 2020
a58f1c7
Remove unused code
pmavridis Oct 25, 2020
95f3b03
Remove unused code 2
pmavridis Oct 25, 2020
5a5bdbe
Dof shader: tweak sampling
pmavridis Oct 25, 2020
a98db85
Dof shader: tweak sampling
pmavridis Oct 25, 2020
0eeada8
Update changelog
pmavridis Oct 25, 2020
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 @@ -43,6 +43,7 @@ The version number for this package has increased due to a version update of a r
- Fixed issue where Default Volume Profile Asset change in project settings was not added to the undo stack (case 1285268).
- Fixed undo after enabling compositor.
- Fixed the ray tracing shadow UI being displayed while it shouldn't (case 1286391).
- Fixed issues with physically-based DoF, improved speed and robustness

### Changed
- Combined occlusion meshes into one to reduce draw calls and state changes with XR single-pass.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,19 +431,20 @@ TextureHandle DepthOfFieldPass(RenderGraph renderGraph, HDCamera hdCamera, Textu
// map rather than having to deal with all the implications of doing it before TAA
if (m_DepthOfField.IsActive() && !isSceneView && m_DepthOfFieldFS && !isDoFPathTraced)
{
// If we switch DoF modes and the old one was not using TAA, make sure we invalidate the history
// Note: for Rendergraph the m_IsDoFHisotoryValid perhaps should be moved to the "pass data" struct
if (taaEnabled && m_IsDoFHisotoryValid != m_DepthOfField.physicallyBased)
{
hdCamera.resetPostProcessingHistory = true;
}

var dofParameters = PrepareDoFParameters(hdCamera);

bool useHistoryMips = m_DepthOfField.physicallyBased;
GrabCoCHistory(hdCamera, out var prevCoC, out var nextCoC, useMips: useHistoryMips);
var prevCoCHandle = renderGraph.ImportTexture(prevCoC);
var nextCoCHandle = renderGraph.ImportTexture(nextCoC);

// If we switch DoF modes and the old one was not using TAA, make sure we invalidate the history
if (taaEnabled && m_IsDoFHisotoryValid != m_DepthOfField.physicallyBased)
{
hdCamera.resetPostProcessingHistory = true;
}

using (var builder = renderGraph.AddRenderPass<DepthofFieldData>("Depth of Field", out var passData, ProfilingSampler.Get(HDProfileId.DepthOfField)))
{
passData.source = builder.ReadTexture(source);
Expand All @@ -458,6 +459,7 @@ TextureHandle DepthOfFieldPass(RenderGraph renderGraph, HDCamera hdCamera, Textu
TextureHandle dest = GetPostprocessOutputHandle(renderGraph, "DoF Destination");
passData.destination = builder.WriteTexture(dest);
passData.motionVecTexture = builder.ReadTexture(motionVectors);
passData.taaEnabled = taaEnabled;

if (!m_DepthOfField.physicallyBased)
{
Expand Down Expand Up @@ -531,8 +533,6 @@ TextureHandle DepthOfFieldPass(RenderGraph renderGraph, HDCamera hdCamera, Textu
});
}

passData.taaEnabled = taaEnabled;

passData.bokehNearKernel = builder.CreateTransientComputeBuffer(new ComputeBufferDesc(dofParameters.nearSampleCount * dofParameters.nearSampleCount, sizeof(uint)) { name = "Bokeh Near Kernel" });
passData.bokehFarKernel = builder.CreateTransientComputeBuffer(new ComputeBufferDesc(dofParameters.farSampleCount * dofParameters.farSampleCount, sizeof(uint)) { name = "Bokeh Far Kernel" });
passData.bokehIndirectCmd = builder.CreateTransientComputeBuffer(new ComputeBufferDesc(3 * 2, sizeof(uint), ComputeBufferType.IndirectArguments) { name = "Bokeh Indirect Cmd" });
Expand Down Expand Up @@ -565,10 +565,13 @@ TextureHandle DepthOfFieldPass(RenderGraph renderGraph, HDCamera hdCamera, Textu
passData.fullresCoC = builder.CreateTransientTexture(new TextureDesc(Vector2.one, true, true)
{ colorFormat = k_CoCFormat, enableRandomWrite = true, useMipMap = true, name = "Full res CoC" });

passData.pingFarRGB = builder.CreateTransientTexture(new TextureDesc(Vector2.one, true, true)
{ colorFormat = m_ColorFormat, useMipMap = true, enableRandomWrite = true, name = "DoF Source Pyramid" });

builder.SetRenderFunc(
(DepthofFieldData data, RenderGraphContext ctx) =>
{
DoPhysicallyBasedDepthOfField(data.parameters, ctx.cmd, data.source, data.destination, data.fullresCoC, data.prevCoC, data.nextCoC, data.motionVecTexture, data.taaEnabled);
DoPhysicallyBasedDepthOfField(data.parameters, ctx.cmd, data.source, data.destination, data.fullresCoC, data.prevCoC, data.nextCoC, data.motionVecTexture, data.pingFarRGB, data.taaEnabled);
});

source = passData.destination;
Expand All @@ -580,15 +583,13 @@ TextureHandle DepthOfFieldPass(RenderGraph renderGraph, HDCamera hdCamera, Textu
if (taaEnabled && m_DepthOfField.physicallyBased)
{
bool postDof = true;
var taaParams = PrepareTAAParameters(hdCamera, postDof);


using (var builder = renderGraph.AddRenderPass<TemporalAntiAliasingData>("Temporal Anti-Aliasing", out var passData, ProfilingSampler.Get(HDProfileId.TemporalAntialiasing)))
{
GrabTemporalAntialiasingHistoryTextures(hdCamera, out var prevHistory, out var nextHistory, postDof);

passData.source = builder.ReadTexture(source);
passData.parameters = PrepareTAAParameters(hdCamera);
passData.parameters = PrepareTAAParameters(hdCamera, postDof);
passData.depthBuffer = builder.ReadTexture(depthBuffer);
passData.motionVecTexture = builder.ReadTexture(motionVectors);
passData.depthMipChain = builder.ReadTexture(depthBufferMipChain);
Expand Down Expand Up @@ -616,12 +617,21 @@ TextureHandle DepthOfFieldPass(RenderGraph renderGraph, HDCamera hdCamera, Textu
data.nextHistory,
data.prevMVLen,
data.nextMVLen);

// Temporary hack to make post-dof TAA work with rendergraph (still the first frame flashes black). We need a better solution.
m_IsDoFHisotoryValid = true;
});

source = passData.destination;
}

postDoFTAAEnabled = true;

}
else
{
// Temporary hack to make post-dof TAA work with rendergraph (still the first frame flashes black). We need a better solution.
m_IsDoFHisotoryValid = false;
}

if (!postDoFTAAEnabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -706,10 +706,11 @@ void PoolSource(ref RTHandle src, RTHandle dst)
GrabCoCHistory(camera, out prevCoC, out nextCoC, useMips: true);

var fullresCoC = m_Pool.Get(Vector2.one, k_CoCFormat, true);

DoPhysicallyBasedDepthOfField(dofParameters, cmd, source, destination, fullresCoC, prevCoC, nextCoC, motionVecTexture, taaEnabled);
var colorPyramid = m_Pool.Get(Vector2.one, m_ColorFormat, true);
DoPhysicallyBasedDepthOfField(dofParameters, cmd, source, destination, fullresCoC, prevCoC, nextCoC, motionVecTexture, colorPyramid, taaEnabled);

m_Pool.Recycle(fullresCoC);
m_Pool.Recycle(colorPyramid);
}
PoolSource(ref source, destination);

Expand Down Expand Up @@ -1825,6 +1826,7 @@ struct DepthOfFieldParameters
public bool farLayerActive;
public bool highQualityFiltering;
public bool useTiles;
public bool resetPostProcessingHistory;

public DepthOfFieldResolution resolution;
public DepthOfFieldMode focusMode;
Expand Down Expand Up @@ -1863,7 +1865,14 @@ DepthOfFieldParameters PrepareDoFParameters(HDCamera camera)
parameters.dofDilateCS = m_Resources.shaders.depthOfFieldDilateCS;
parameters.dofDilateKernel = parameters.dofDilateCS.FindKernel("KMain");
parameters.dofMipCS = m_Resources.shaders.depthOfFieldMipCS;
parameters.dofMipColorKernel = parameters.dofMipCS.FindKernel(m_EnableAlpha ? "KMainColorAlpha" : "KMainColor");
if (!m_DepthOfField.physicallyBased)
{
parameters.dofMipColorKernel = parameters.dofMipCS.FindKernel(m_EnableAlpha ? "KMainColorAlpha" : "KMainColor");
}
else
{
parameters.dofMipColorKernel = parameters.dofMipCS.FindKernel(m_EnableAlpha ? "KMainColorCopyAlpha" : "KMainColorCopy");
}
parameters.dofMipCoCKernel = parameters.dofMipCS.FindKernel("KMainCoC");
parameters.dofMipSafeCS = m_Resources.shaders.depthOfFieldMipSafeCS;
parameters.dofPrefilterCS = m_Resources.shaders.depthOfFieldPrefilterCS;
Expand All @@ -1886,6 +1895,7 @@ DepthOfFieldParameters PrepareDoFParameters(HDCamera camera)
parameters.pbDoFGatherKernel = parameters.pbDoFGatherCS.FindKernel("KMain");

parameters.camera = camera;
parameters.resetPostProcessingHistory = camera.resetPostProcessingHistory;

parameters.nearLayerActive = m_DepthOfField.IsNearLayerActive();
parameters.farLayerActive = m_DepthOfField.IsFarLayerActive();
Expand Down Expand Up @@ -2512,7 +2522,7 @@ static void ReprojectCoCHistory(in DepthOfFieldParameters parameters, CommandBuf
//Note: this reprojection creates some ghosting, we should replace it with something based on the new TAA
ComputeShader cs = parameters.dofCoCReprojectCS;
int kernel = parameters.dofCoCReprojectKernel;
cmd.SetComputeVectorParam(cs, HDShaderIDs._Params, new Vector4(camera.resetPostProcessingHistory ? 0f : 0.91f, cocHistoryScale.x, cocHistoryScale.y, 0f));
cmd.SetComputeVectorParam(cs, HDShaderIDs._Params, new Vector4(parameters.resetPostProcessingHistory ? 0f : 0.91f, cocHistoryScale.x, cocHistoryScale.y, 0f));
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._InputCoCTexture, fullresCoC);
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._InputHistoryCoCTexture, prevCoC);
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputCoCTexture, nextCoC);
Expand All @@ -2536,15 +2546,15 @@ static void GetMipMapDimensions(RTHandle texture, int lod, out int width, out in
}
}

static void DoPhysicallyBasedDepthOfField(in DepthOfFieldParameters dofParameters, CommandBuffer cmd, RTHandle source, RTHandle destination, RTHandle fullresCoC, RTHandle prevCoCHistory, RTHandle nextCoCHistory, RTHandle motionVecTexture, bool taaEnabled)
static void DoPhysicallyBasedDepthOfField(in DepthOfFieldParameters dofParameters, CommandBuffer cmd, RTHandle source, RTHandle destination, RTHandle fullresCoC, RTHandle prevCoCHistory, RTHandle nextCoCHistory, RTHandle motionVecTexture, RTHandle sourcePyramid, bool taaEnabled)
{
float scale = 1f / (float)dofParameters.resolution;
int targetWidth = Mathf.RoundToInt(dofParameters.camera.actualWidth * scale);
int targetHeight = Mathf.RoundToInt(dofParameters.camera.actualHeight * scale);

// Map the old "max radius" parameters to a bigger range, so we can work on more challenging scenes
float maxRadius = Mathf.Max(dofParameters.farMaxBlur, dofParameters.nearMaxBlur);
float cocLimit = Mathf.Clamp(4 * maxRadius, 1, 64);
float cocLimit = Mathf.Clamp(8 * maxRadius, 1, 128); //[1, 16] --> [1, 128]

ComputeShader cs;
int kernel;
Expand Down Expand Up @@ -2602,14 +2612,71 @@ static void DoPhysicallyBasedDepthOfField(in DepthOfFieldParameters dofParameter
cs = dofParameters.dofCoCPyramidCS;
kernel = dofParameters.dofCoCPyramidKernel;

float numMips = Mathf.Floor(Mathf.Log(Mathf.Max(dofParameters.camera.actualWidth, dofParameters.camera.actualHeight), 2));

cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._InputTexture, fullresCoC);
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputMip1, fullresCoC, 1);
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputMip2, fullresCoC, 2);
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputMip3, fullresCoC, 3);
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputMip4, fullresCoC, 4);
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputMip5, fullresCoC, 5);
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputMip6, fullresCoC, 6);
cmd.SetComputeVectorParam(cs, HDShaderIDs._Params, new Vector4(numMips, 0, 0, 0));
cmd.DispatchCompute(cs, kernel, (dofParameters.camera.actualWidth + 31) / 32, (dofParameters.camera.actualHeight + 31) / 32, dofParameters.camera.viewCount);

// do we need a second pass for the rest?
if (numMips > 6.0f && cocLimit > 32)
{
GetMipMapDimensions(fullresCoC, 6, out var mipMapWidth, out var mipMapHeight);
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._InputTexture, fullresCoC, 6);
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputMip1, fullresCoC, 7);

if (numMips > 7)
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputMip2, fullresCoC, 8);
else
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputMip2, fullresCoC, 1); // we will never write on this, but still need to bind something

if (numMips > 8)
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputMip3, fullresCoC, 9);
else
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputMip3, fullresCoC, 2); // we will never write on this, but still need to bind something

if (numMips > 9)
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputMip4, fullresCoC, 10);
else
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputMip4, fullresCoC, 3); // we will never write on this, but still need to bind something

if (numMips > 10)
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputMip5, fullresCoC, 11);
else
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputMip5, fullresCoC, 4); // we will never write on this, but still need to bind something

if (numMips > 11)
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputMip6, fullresCoC, 12);
else
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputMip6, fullresCoC, 5); // we will never write on this, but still need to bind something

cmd.SetComputeVectorParam(cs, HDShaderIDs._Params, new Vector4(numMips - 6.0f, 0, 0, 0));
cmd.DispatchCompute(cs, kernel, (mipMapWidth + 31) / 32, (mipMapHeight + 31) / 32, dofParameters.camera.viewCount);
}

// DoF color pyramid
if (sourcePyramid != null)
{
cs = dofParameters.dofMipCS;
kernel = dofParameters.dofMipColorKernel;

cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._InputTexture, source, 0);
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputTexture, sourcePyramid, 0);
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputMip1, sourcePyramid, 1);
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputMip2, sourcePyramid, 2);
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputMip3, sourcePyramid, 3);
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputMip4, sourcePyramid, 4);

int tx = ((dofParameters.camera.actualWidth >> 1) + 7) / 8;
int ty = ((dofParameters.camera.actualHeight >> 1) + 7) / 8;
cmd.DispatchCompute(cs, kernel, tx, ty, dofParameters.camera.viewCount);
}
}

using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.DepthOfFieldCombine)))
Expand All @@ -2618,12 +2685,11 @@ static void DoPhysicallyBasedDepthOfField(in DepthOfFieldParameters dofParameter
kernel = dofParameters.pbDoFGatherKernel;
float sampleCount = Mathf.Max(dofParameters.nearSampleCount, dofParameters.farSampleCount);

// We only have up to 6 mip levels
float mipLevel = Mathf.Min(6, 1 + Mathf.Ceil(Mathf.Log(cocLimit, 2)));
float mipLevel = 1 + Mathf.Ceil(Mathf.Log(cocLimit, 2));
GetMipMapDimensions(fullresCoC, (int)mipLevel, out var mipMapWidth, out var mipMapHeight);
cmd.SetComputeVectorParam(cs, HDShaderIDs._Params, new Vector4(sampleCount, cocLimit, 0.0f, 0.0f));
cmd.SetComputeVectorParam(cs, HDShaderIDs._Params2, new Vector4(mipLevel, mipMapWidth, mipMapHeight, 0.0f));
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._InputTexture, source);
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._InputTexture, sourcePyramid != null ? sourcePyramid : source);
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._InputCoCTexture, fullresCoC);
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._OutputTexture, destination);
cmd.DispatchCompute(cs, kernel, (dofParameters.camera.actualWidth + 7) / 8, (dofParameters.camera.actualHeight + 7) / 8, dofParameters.camera.viewCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@

#pragma only_renderers d3d11 playstation xboxone vulkan metal switch

#pragma kernel KMainColor MAIN=KMainColor CTYPE=float3
#pragma kernel KMainColorAlpha MAIN=KMainColorAlpha CTYPE=float4
#pragma kernel KMainCoC MAIN=KMainCoC CTYPE=float
#pragma kernel KMainColor MAIN=KMainColor CTYPE=float3
#pragma kernel KMainColorAlpha MAIN=KMainColorAlpha CTYPE=float4
#pragma kernel KMainCoC MAIN=KMainCoC CTYPE=float
#pragma kernel KMainColorCopy MAIN=KMainColorCopy CTYPE=float3 COPY_MIP0
#pragma kernel KMainColorCopyAlpha MAIN=KMainColorCopyAlpha CTYPE=float4 COPY_MIP0

RW_TEXTURE2D_X(CTYPE, _InputTexture);

#ifdef COPY_MIP0
RW_TEXTURE2D_X(CTYPE, _OutputTexture);
#endif

RW_TEXTURE2D_X(CTYPE, _OutputMip1);
RW_TEXTURE2D_X(CTYPE, _OutputMip2);
RW_TEXTURE2D_X(CTYPE, _OutputMip3);
Expand Down Expand Up @@ -38,6 +44,13 @@ void MAIN(uint3 dispatchThreadId : SV_DispatchThreadID, uint groupIndex : SV_Gro

// First mip
CTYPE color = _InputTexture[COORD_TEXTURE2D_X(ul)];
#ifdef COPY_MIP0
_OutputTexture[COORD_TEXTURE2D_X(ul)] = _InputTexture[COORD_TEXTURE2D_X(ul)];
_OutputTexture[COORD_TEXTURE2D_X(ul + uint2(1u, 0u))] = _InputTexture[COORD_TEXTURE2D_X(ul + uint2(1u, 0u))];
_OutputTexture[COORD_TEXTURE2D_X(ul + uint2(0u, 1u))] = _InputTexture[COORD_TEXTURE2D_X(ul + uint2(0u, 1u))];
_OutputTexture[COORD_TEXTURE2D_X(ul + uint2(1u, 1u))] = _InputTexture[COORD_TEXTURE2D_X(ul + uint2(1u, 1u))];
#endif

color += _InputTexture[COORD_TEXTURE2D_X(ul + uint2(1u, 0u))];
color += _InputTexture[COORD_TEXTURE2D_X(ul + uint2(0u, 1u))];
color += _InputTexture[COORD_TEXTURE2D_X(ul + uint2(1u, 1u))];
Expand Down
Loading