Skip to content

[Fogbugz # 1345143] DLSS: Aleviating some of the ghosting artifacts for partciles by using the color bias mask #5081

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 1 commit into from
Jul 28, 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 @@ -332,6 +332,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fixed custom post process template not working with Blit method.
- Fixed support for instanced motion vector rendering
- Fixed an issue that made Custom Pass buffers inaccessible in ShaderGraph.
- Fixed some of the extreme ghosting in DLSS by using a bit mask to bias the color of particles. VFX tagged as Exclude from TAA will be on this pass.

### Changed
- Changed Window/Render Pipeline/HD Render Pipeline Wizard to Window/Rendering/HDRP Wizard
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
Shader "Hidden/HDRP/DLSSBiasColorMask"
{
Properties
{
[HideInInspector] _StencilRef("_StencilRef", Int) = 2
[HideInInspector] _StencilMask("_StencilMask", Int) = 2
}

HLSLINCLUDE
#pragma target 4.5
#pragma only_renderers d3d11 playstation xboxone xboxseries vulkan metal switch
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"

struct Attributes
{
uint vertexID : SV_VertexID;
UNITY_VERTEX_INPUT_INSTANCE_ID
};

struct Varyings
{
float4 positionCS : SV_POSITION;
float2 texcoord : TEXCOORD0;
UNITY_VERTEX_OUTPUT_STEREO
};

Varyings Vert(Attributes input)
{
Varyings output;
UNITY_SETUP_INSTANCE_ID(input);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
output.positionCS = GetFullScreenTriangleVertexPosition(input.vertexID);
output.texcoord = GetFullScreenTriangleTexCoord(input.vertexID);
return output;
}

void DLSSBiasColorMaskPS(Varyings input, out float4 outColor : SV_Target0)
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
outColor = float4(1,1,1,1);
}

ENDHLSL

SubShader
{
Tags{ "RenderPipeline" = "HDRenderPipeline" }

Pass
{
Stencil
{
ReadMask [_StencilMask]
Ref [_StencilRef]
Comp Equal
Pass Keep
}

ZWrite Off ZTest Always Blend Off Cull Off

HLSLPROGRAM
#pragma vertex Vert
#pragma fragment DLSSBiasColorMaskPS
ENDHLSL
}
}

Fallback Off
}

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 @@ -191,6 +191,7 @@ internal enum HDProfileId
DynamicExposure,
ApplyExposure,
TemporalAntialiasing,
DeepLearningSuperSamplingColorMask,
DeepLearningSuperSampling,
DepthOfField,
DepthOfFieldKernel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,15 @@ public PostProcessTextureAllocator()
System.Random m_Random;

bool m_DLSSPassEnabled = false;
Material m_DLSSBiasColorMaskMaterial;
DLSSPass m_DLSSPass = null;
void InitializePostProcess()
{
m_FinalPassMaterial = CoreUtils.CreateEngineMaterial(defaultResources.shaders.finalPassPS);
m_ClearBlackMaterial = CoreUtils.CreateEngineMaterial(defaultResources.shaders.clearBlackPS);
m_SMAAMaterial = CoreUtils.CreateEngineMaterial(defaultResources.shaders.SMAAPS);
m_TemporalAAMaterial = CoreUtils.CreateEngineMaterial(defaultResources.shaders.temporalAntialiasingPS);
m_DLSSBiasColorMaskMaterial = CoreUtils.CreateEngineMaterial(defaultResources.shaders.DLSSBiasColorMaskPS);

// Lens Flare
m_LensFlareDataDrivenShader = CoreUtils.CreateEngineMaterial(defaultResources.shaders.lensFlareDataDrivenPS);
Expand Down Expand Up @@ -470,7 +472,8 @@ TextureHandle RenderPostProcess(RenderGraph renderGraph,

if (m_DLSSPassEnabled && DynamicResolutionHandler.instance.upsamplerSchedule == DynamicResolutionHandler.UpsamplerScheduleType.BeforePost)
{
source = DoDLSSPass(renderGraph, hdCamera, inputColor, depthBuffer, motionVectors);
TextureHandle colorBiasMask = DoDLSSColorMaskPass(renderGraph, hdCamera, depthBuffer);
source = DoDLSSPass(renderGraph, hdCamera, inputColor, depthBuffer, motionVectors, colorBiasMask);
SetCurrentResolutionGroup(renderGraph, hdCamera, ResolutionGroup.AfterDynamicResUpscale);
}

Expand Down Expand Up @@ -635,6 +638,46 @@ TextureHandle RenderAfterPostProcessObjects(RenderGraph renderGraph, HDCamera hd
#endregion

#region DLSS
class DLSSColorMaskPassData
{
public Material colorMaskMaterial;
public int destWidth;
public int destHeight;
}

TextureHandle DoDLSSColorMaskPass(RenderGraph renderGraph, HDCamera hdCamera, TextureHandle inputDepth)
{
TextureHandle output = TextureHandle.nullHandle;
using (var builder = renderGraph.AddRenderPass<DLSSColorMaskPassData>("DLSS Color Mask", out var passData, ProfilingSampler.Get(HDProfileId.DeepLearningSuperSamplingColorMask)))
{
output = builder.UseColorBuffer(renderGraph.CreateTexture(
new TextureDesc(Vector2.one, true, true)
{
colorFormat = GraphicsFormat.R8G8B8A8_UNorm,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume the format is required by DLSS? Sounds a bit of a shame to use 32 bit for a binary mask

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation says that DLSS can take an R8_Unorm, however DLSS crashes hard (on the CPU) when we pass anything that doesnt have an RGBA swizzle.
Im prepping something to ask nvidia why is this the case. For now this will have to do.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ouch, yeah whatever works for now

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should add a comment in the code about it

clearBuffer = true,
clearColor = Color.black, name = "DLSS Color Mask"
}), 0);
builder.UseDepthBuffer(inputDepth, DepthAccess.Read);

passData.colorMaskMaterial = m_DLSSBiasColorMaskMaterial;

passData.destWidth = hdCamera.actualWidth;
passData.destHeight = hdCamera.actualHeight;

builder.SetRenderFunc(
(DLSSColorMaskPassData data, RenderGraphContext ctx) =>
{
Rect targetViewport = new Rect(0.0f, 0.0f, data.destWidth, data.destHeight);
data.colorMaskMaterial.SetInt(HDShaderIDs._StencilMask, (int)StencilUsage.ExcludeFromTAA);
data.colorMaskMaterial.SetInt(HDShaderIDs._StencilRef, (int)StencilUsage.ExcludeFromTAA);
ctx.cmd.SetViewport(targetViewport);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be setting the stencil bits from here too instead of relying on the fact that the hardcoded one will remain the same, i.e.

data.colorMaskMaterial.SetInt(HDShaderIDs._StencilMask, (int)StencilUsage.ExcludeFromTAA); data.colorMaskMaterial.SetInt(HDShaderIDs._StencilRef, (int)StencilUsage.ExcludeFromTAA);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

ctx.cmd.DrawProcedural(Matrix4x4.identity, data.colorMaskMaterial, 0, MeshTopology.Triangles, 3, 1, null);
});
}

return output;
}

class DLSSData
{
public DLSSPass.Parameters parameters;
Expand All @@ -643,7 +686,7 @@ class DLSSData

TextureHandle DoDLSSPass(
RenderGraph renderGraph, HDCamera hdCamera,
TextureHandle source, TextureHandle depthBuffer, TextureHandle motionVectors)
TextureHandle source, TextureHandle depthBuffer, TextureHandle motionVectors, TextureHandle biasColorMask)
{
using (var builder = renderGraph.AddRenderPass<DLSSData>("Deep Learning Super Sampling", out var passData, ProfilingSampler.Get(HDProfileId.DeepLearningSuperSampling)))
{
Expand All @@ -656,6 +699,12 @@ TextureHandle DoDLSSPass(
viewHandles.output = builder.WriteTexture(GetPostprocessUpsampledOutputHandle(renderGraph, "DLSS destination"));
viewHandles.depth = builder.ReadTexture(depthBuffer);
viewHandles.motionVectors = builder.ReadTexture(motionVectors);

if (biasColorMask.IsValid())
viewHandles.biasColorMask = builder.ReadTexture(biasColorMask);
else
viewHandles.biasColorMask = TextureHandle.nullHandle;

passData.resourceHandles = DLSSPass.CreateCameraResources(hdCamera, renderGraph, builder, viewHandles);

source = viewHandles.output;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,8 @@ public sealed class ShaderResources
public ComputeShader upsampleSceneCS;
[Reload("Runtime/PostProcessing/Shaders/LensFlareDataDriven.shader")]
public Shader lensFlareDataDrivenPS;
[Reload("Runtime/PostProcessing/Shaders/DLSSBiasColorMask.shader")]
public Shader DLSSBiasColorMaskPS;

// Physically based DoF
[Reload("Runtime/PostProcessing/Shaders/DoFCircleOfConfusion.compute")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ public struct ViewResourceHandles
public TextureHandle output;
public TextureHandle depth;
public TextureHandle motionVectors;
public TextureHandle biasColorMask;
public void WriteResources(RenderGraphBuilder builder)
{
source = builder.WriteTexture(source);
output = builder.WriteTexture(output);
depth = builder.WriteTexture(depth);
motionVectors = builder.WriteTexture(motionVectors);

if (biasColorMask.IsValid())
biasColorMask = builder.WriteTexture(biasColorMask);
}
}

Expand All @@ -41,6 +45,9 @@ public static ViewResources GetViewResources(in ViewResourceHandles handles)
depth = (RenderTexture)handles.depth,
motionVectors = (RenderTexture)handles.motionVectors
};

resources.biasColorMask = (handles.biasColorMask.IsValid()) ? (RenderTexture)handles.biasColorMask : (RenderTexture)null;

return resources;
}

Expand All @@ -54,6 +61,9 @@ public static CameraResourcesHandles CreateCameraResources(HDCamera camera, Rend
{
TextureHandle GetTmpViewXrTex(in TextureHandle handle)
{
if (!handle.IsValid())
return TextureHandle.nullHandle;

var newTexDesc = renderGraph.GetTextureDesc(handle);
newTexDesc.slices = 1;
newTexDesc.dimension = TextureDimension.Tex2D;
Expand All @@ -66,6 +76,7 @@ void CreateCopyNoXR(in ViewResourceHandles input, out ViewResourceHandles newRes
newResources.output = GetTmpViewXrTex(input.output);
newResources.depth = GetTmpViewXrTex(input.depth);
newResources.motionVectors = GetTmpViewXrTex(input.motionVectors);
newResources.biasColorMask = GetTmpViewXrTex(input.biasColorMask);
newResources.WriteResources(builder);
}

Expand Down Expand Up @@ -108,6 +119,7 @@ public struct ViewResources
public RenderTexture output;
public RenderTexture depth;
public RenderTexture motionVectors;
public RenderTexture biasColorMask;
}

public struct CameraResources
Expand Down Expand Up @@ -374,6 +386,7 @@ public void SubmitDlssCommands(
RenderTexture source,
RenderTexture depth,
RenderTexture motionVectors,
RenderTexture biasColorMask,
RenderTexture output,
CommandBuffer cmdBuffer)
{
Expand All @@ -399,7 +412,8 @@ public void SubmitDlssCommands(
colorInput = source,
colorOutput = output,
depth = depth,
motionVectors = motionVectors
motionVectors = motionVectors,
biasColorMask = biasColorMask
};

m_Device.ExecuteDLSS(cmdBuffer, m_DlssContext, textureTable);
Expand Down Expand Up @@ -474,6 +488,7 @@ void RunPass(ViewState viewState, CommandBuffer cmdBuffer, in DlssViewData viewD
viewResources.source,
viewResources.depth,
viewResources.motionVectors,
viewResources.biasColorMask,
viewResources.output, cmdBuffer);
}

Expand All @@ -490,6 +505,9 @@ void RunPass(ViewState viewState, CommandBuffer cmdBuffer, in DlssViewData viewD
cmdBuffer.CopyTexture(camResources.resources.source, viewId, tmpResources.source, 0);
cmdBuffer.CopyTexture(camResources.resources.depth, viewId, tmpResources.depth, 0);
cmdBuffer.CopyTexture(camResources.resources.motionVectors, viewId, tmpResources.motionVectors, 0);

if (camResources.resources.biasColorMask != null)
cmdBuffer.CopyTexture(camResources.resources.biasColorMask, viewId, tmpResources.biasColorMask, 0);
}

for (int viewId = 0; viewId < m_Views.Length; ++viewId)
Expand Down
Loading