Skip to content
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

Introduce concept of falling back textures to black in rendergraph + ReadWrite tag #2762

Merged
merged 6 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using UnityEngine.Rendering;

namespace UnityEngine.Experimental.Rendering.RenderGraphModule
{
Expand All @@ -24,6 +25,7 @@ public struct RenderGraphBuilder : IDisposable
public TextureHandle UseColorBuffer(in TextureHandle input, int index)
{
CheckResource(input.handle);
m_Resources.IncrementWriteCount(input.handle);
m_RenderPass.SetColorBuffer(input, index);
return input;
}
Expand All @@ -37,6 +39,7 @@ public TextureHandle UseColorBuffer(in TextureHandle input, int index)
public TextureHandle UseDepthBuffer(in TextureHandle input, DepthAccess flags)
{
CheckResource(input.handle);
m_Resources.IncrementWriteCount(input.handle);
m_RenderPass.SetDepthBuffer(input, flags);
return input;
}
Expand All @@ -49,6 +52,20 @@ public TextureHandle UseDepthBuffer(in TextureHandle input, DepthAccess flags)
public TextureHandle ReadTexture(in TextureHandle input)
{
CheckResource(input.handle);

if (!m_Resources.IsResourceImported(input.handle) && m_Resources.TextureNeedsFallback(input))
{
var texDimension = m_Resources.GetTextureResourceDesc(input.handle).dimension;
if (texDimension == TextureXR.dimension)
{
return m_RenderGraph.defaultResources.blackTextureXR;
}
else if (texDimension == TextureDimension.Tex3D)
{
return m_RenderGraph.defaultResources.blackTexture3DXR;
}
}
JulienIgnace-Unity marked this conversation as resolved.
Show resolved Hide resolved

m_RenderPass.AddResourceRead(input.handle);
return input;
}
Expand All @@ -61,11 +78,25 @@ public TextureHandle ReadTexture(in TextureHandle input)
public TextureHandle WriteTexture(in TextureHandle input)
{
CheckResource(input.handle);
m_Resources.IncrementWriteCount(input.handle);
// TODO RENDERGRAPH: Manage resource "version" for debugging purpose
m_RenderPass.AddResourceWrite(input.handle);
return input;
}

/// <summary>
/// Specify a Texture resource to read and write to during the pass.
/// </summary>
/// <param name="input">The Texture resource to read and write to during the pass.</param>
/// <returns>An updated resource handle to the input resource.</returns>
public TextureHandle ReadWriteTexture(in TextureHandle input)
{
CheckResource(input.handle);
JulienIgnace-Unity marked this conversation as resolved.
Show resolved Hide resolved
m_RenderPass.AddResourceWrite(input.handle);
m_RenderPass.AddResourceRead(input.handle);
return input;
}

/// <summary>
/// Create a new Render Graph Texture resource.
/// This texture will only be available for the current pass and will be assumed to be both written and read so users don't need to add explicit read/write declarations.
Expand Down Expand Up @@ -125,6 +156,7 @@ public ComputeBufferHandle WriteComputeBuffer(in ComputeBufferHandle input)
{
CheckResource(input.handle);
m_RenderPass.AddResourceWrite(input.handle);
m_Resources.IncrementWriteCount(input.handle);
return input;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,20 @@ internal static RenderGraphResourceRegistry current
class IRenderGraphResource
{
public bool imported;
public int cachedHash;
public int transientPassIndex;
public int cachedHash;
public int transientPassIndex;
public uint writeCount;
public bool wasReleased;
public bool requestFallBack;

public virtual void Reset()
{
imported = false;
cachedHash = -1;
transientPassIndex = -1;
wasReleased = false;
requestFallBack = false;
writeCount = 0;
}

public virtual string GetName()
Expand All @@ -53,6 +57,16 @@ public virtual bool IsCreated()
{
return false;
}

public void IncrementWriteCount()
{
writeCount++;
}

public bool NeedsFallBack()
{
return requestFallBack && writeCount == 0;
}
}

#region Resources
Expand All @@ -63,7 +77,7 @@ class RenderGraphResource<DescType, ResType>
where ResType : class
{
public DescType desc;
public ResType resource;
public ResType resource;

protected RenderGraphResource()
{
Expand Down Expand Up @@ -108,7 +122,7 @@ public override string GetName()
internal struct RendererListResource
{
public RendererListDesc desc;
public RendererList rendererList;
public RendererList rendererList;

internal RendererListResource(in RendererListDesc desc)
{
Expand All @@ -121,15 +135,15 @@ internal RendererListResource(in RendererListDesc desc)

DynamicArray<IRenderGraphResource>[] m_Resources = new DynamicArray<IRenderGraphResource>[(int)RenderGraphResourceType.Count];

TexturePool m_TexturePool = new TexturePool();
int m_TextureCreationIndex;
ComputeBufferPool m_ComputeBufferPool = new ComputeBufferPool();
DynamicArray<RendererListResource> m_RendererListResources = new DynamicArray<RendererListResource>();
RenderGraphDebugParams m_RenderGraphDebug;
RenderGraphLogger m_Logger;
int m_CurrentFrameIndex;
TexturePool m_TexturePool = new TexturePool();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yikes visual studio, gonna revert this spacing change

int m_TextureCreationIndex;
ComputeBufferPool m_ComputeBufferPool = new ComputeBufferPool();
DynamicArray<RendererListResource> m_RendererListResources = new DynamicArray<RendererListResource>();
RenderGraphDebugParams m_RenderGraphDebug;
RenderGraphLogger m_Logger;
int m_CurrentFrameIndex;

RTHandle m_CurrentBackbuffer;
RTHandle m_CurrentBackbuffer;

internal RTHandle GetTexture(in TextureHandle handle)
{
Expand All @@ -139,6 +153,14 @@ internal RTHandle GetTexture(in TextureHandle handle)
return GetTextureResource(handle.handle).resource;
}

internal bool TextureNeedsFallback(in TextureHandle handle)
{
if (!handle.IsValid())
return false;

return GetTextureResource(handle.handle).NeedsFallBack();
}

internal RendererList GetRendererList(in RendererListHandle handle)
{
if (!handle.IsValid() || handle >= m_RendererListResources.size)
Expand Down Expand Up @@ -192,6 +214,11 @@ void CheckHandleValidity(RenderGraphResourceType type, int index)
if (index >= resources.size)
throw new ArgumentException($"Trying to access resource of type {type} with an invalid resource index {index}");
}
internal void IncrementWriteCount(in ResourceHandle res)
{
CheckHandleValidity(res);
m_Resources[res.iType][res.index].IncrementWriteCount();
}

internal string GetResourceName(in ResourceHandle res)
{
Expand Down Expand Up @@ -276,6 +303,7 @@ internal TextureHandle CreateTexture(in TextureDesc desc, int transientPassIndex
ValidateTextureDesc(desc);

int newHandle = AddNewResource(m_Resources[(int)RenderGraphResourceType.Texture], out TextureResource texResource);
texResource.requestFallBack = desc.fallBackToBlackTexture;
texResource.desc = desc;
texResource.transientPassIndex = transientPassIndex;
return new TextureHandle(newHandle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ public struct TextureDesc
///<summary>Descriptor to determine how the texture will be in fast memory on platform that supports it.</summary>
public FastMemoryDesc fastMemoryDesc;
#endif
///<summary>Determines whether the texture will fallback to a black texture if it is read without ever writing to it.</summary>
public bool fallBackToBlackTexture;

// Initial state. Those should not be used in the hash
///<summary>Texture needs to be cleared on first use.</summary>
Expand Down
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 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fixed the clear coat not being handled properly for SSR and RTR (case 1291654).
- Fixed ghosting in RTGI and RTAO when denoising is enabled and the RTHandle size is not equal to the Viewport size (case 1291654).
- Fixed alpha output when atmospheric scattering is enabled.
- Fixed issue with normal buffer being read without ever being written to.

### Changed
- Volume Manager now always tests scene culling masks. This was required to fix hybrid workflow.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,17 +320,17 @@ public struct SSGIDenoiserOutput
// History buffer
bool historyRequireClear = false;
RTHandle indirectDiffuseHistory0 = RequestIndirectDiffuseHistory0(hdCamera, out historyRequireClear);
passData.indirectDiffuseHistory0 = builder.ReadTexture(builder.WriteTexture(renderGraph.ImportTexture(indirectDiffuseHistory0)));
passData.indirectDiffuseHistory0 = builder.ReadWriteTexture(renderGraph.ImportTexture(indirectDiffuseHistory0));
RTHandle indirectDiffuseHistory1 = RequestIndirectDiffuseHistory1(hdCamera, out historyRequireClear);
passData.indirectDiffuseHistory1 = builder.ReadTexture(builder.WriteTexture(renderGraph.ImportTexture(indirectDiffuseHistory1)));
passData.indirectDiffuseHistory1 = builder.ReadWriteTexture(renderGraph.ImportTexture(indirectDiffuseHistory1));
var historyDepthBuffer = halfResolution ? hdCamera.GetCurrentFrameRT((int)HDCameraFrameHistoryType.Depth1) : hdCamera.GetCurrentFrameRT((int)HDCameraFrameHistoryType.Depth);
passData.historyDepthBuffer = historyDepthBuffer != null ? builder.ReadTexture(renderGraph.ImportTexture(historyDepthBuffer)) : renderGraph.defaultResources.blackTextureXR;
passData.intermediateBuffer0 = builder.CreateTransientTexture(new TextureDesc(Vector2.one, true, true)
{ colorFormat = GraphicsFormat.R16G16B16A16_SFloat, enableRandomWrite = true, name = "SSGI Denoiser Intermediate0" });
passData.intermediateBuffer1 = builder.CreateTransientTexture(new TextureDesc(Vector2.one, true, true)
{ colorFormat = GraphicsFormat.R16G16B16A16_SFloat, enableRandomWrite = true, name = "SSGI Denoiser Intermediate1" });
passData.inputOutputBuffer0 = builder.WriteTexture(builder.ReadTexture(inputOutputBuffer0));
passData.inputOutputBuffer1 = builder.WriteTexture(builder.ReadTexture(inputOutputBuffer1));
passData.inputOutputBuffer0 = builder.ReadWriteTexture(inputOutputBuffer0);
passData.inputOutputBuffer1 = builder.ReadWriteTexture(inputOutputBuffer1);

passData.parameters = PrepareSSGIDenoiserParameters(hdCamera, halfResolution, historyValidity, historyRequireClear, depthMipInfo);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ TextureHandle ConvertSSGI(RenderGraph renderGraph, HDCamera hdCamera, bool halfR
passData.depthTexture = builder.ReadTexture(depthPyramid);
passData.stencilBuffer = builder.ReadTexture(stencilBuffer);
passData.normalBuffer = builder.ReadTexture(normalBuffer);
passData.inoutputBuffer0 = builder.WriteTexture(builder.ReadTexture(inoutputBuffer0));
passData.inoutputBuffer1 = builder.WriteTexture(builder.ReadTexture(inoutputBuffer1));
passData.inoutputBuffer0 = builder.ReadWriteTexture(inoutputBuffer0);
passData.inoutputBuffer1 = builder.ReadWriteTexture(inoutputBuffer1);

builder.SetRenderFunc(
(ConvertSSGIPassData data, RenderGraphContext ctx) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void WriteScreenSpaceShadow(RenderGraph renderGraph, HDCamera hdCamera, TextureH
{
passData.parameters = PrepareWriteScreenSpaceShadowParameters(hdCamera, shadowIndex, shadowType);
passData.inputShadowBuffer = builder.ReadTexture(shadowTexture);
passData.outputShadowArrayBuffer = builder.WriteTexture(builder.ReadTexture(screenSpaceShadowArray));
passData.outputShadowArrayBuffer = builder.ReadWriteTexture(screenSpaceShadowArray);

builder.SetRenderFunc(
(WriteScreenSpaceShadowPassData data, RenderGraphContext context) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,8 @@ class RTShadowAreaPassData
passData.gbuffer3 = builder.ReadTexture(renderGraph.defaultResources.blackTextureXR);
}

passData.shadowHistoryArray = builder.ReadTexture(builder.WriteTexture(renderGraph.ImportTexture(shadowHistoryArray)));
passData.analyticHistoryArray = builder.ReadTexture(builder.WriteTexture(renderGraph.ImportTexture(analyticHistoryArray)));
passData.shadowHistoryArray = builder.ReadWriteTexture(renderGraph.ImportTexture(shadowHistoryArray));
passData.analyticHistoryArray = builder.ReadWriteTexture(renderGraph.ImportTexture(analyticHistoryArray));

// Intermediate buffers
passData.directionBuffer = builder.CreateTransientTexture(new TextureDesc(Vector2.one, true, true) { colorFormat = GraphicsFormat.R16G16B16A16_SFloat, enableRandomWrite = true, name = "Direction Buffer" });
Expand All @@ -448,10 +448,10 @@ class RTShadowAreaPassData
passData.intermediateBufferRG0 = builder.CreateTransientTexture(new TextureDesc(Vector2.one, true, true) { colorFormat = GraphicsFormat.R16G16B16A16_SFloat, enableRandomWrite = true, name = "Intermediate Buffer RG0" });

// Debug textures
passData.rayCountTexture = builder.ReadTexture(builder.WriteTexture(rayCountTexture));
passData.rayCountTexture = builder.ReadWriteTexture(rayCountTexture);

// Output buffers
passData.outputShadowTexture = builder.ReadTexture(builder.WriteTexture(renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) { colorFormat = GraphicsFormat.R16G16B16A16_SFloat, enableRandomWrite = true, name = "Shadow Buffer" })));
passData.outputShadowTexture = builder.ReadWriteTexture(renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true) { colorFormat = GraphicsFormat.R16G16B16A16_SFloat, enableRandomWrite = true, name = "Shadow Buffer" }));
builder.SetRenderFunc(
(RTShadowAreaPassData data, RenderGraphContext context) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ void RenderRayTracedDirectionalScreenSpaceShadow(RenderGraph renderGraph, HDCame
passData.directionBuffer = builder.CreateTransientTexture(new TextureDesc(Vector2.one, true, true) { colorFormat = GraphicsFormat.R16G16B16A16_SFloat, enableRandomWrite = true, name = "Direction Buffer" });

// Debug buffers
passData.rayCountTexture = builder.ReadTexture(builder.WriteTexture(rayCountTexture));
passData.rayCountTexture = builder.ReadWriteTexture(rayCountTexture);

// Output Buffers
passData.velocityBuffer = builder.ReadTexture(builder.WriteTexture(renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true)
{ colorFormat = GraphicsFormat.R8_SNorm, enableRandomWrite = true, clearBuffer = true, name = "Velocity Buffer" })));
passData.distanceBuffer = builder.ReadTexture(builder.WriteTexture(renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true)
{ colorFormat = GraphicsFormat.R32_SFloat, enableRandomWrite = true, clearBuffer = true, name = "Distance Buffer" })));
passData.outputShadowBuffer = builder.ReadTexture(builder.WriteTexture(renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true)
{ colorFormat = GraphicsFormat.R16G16B16A16_SFloat, enableRandomWrite = true, clearBuffer = true, name = "RT Directional Shadow" })));
passData.velocityBuffer = builder.ReadWriteTexture(renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true)
{ colorFormat = GraphicsFormat.R8_SNorm, enableRandomWrite = true, clearBuffer = true, name = "Velocity Buffer" }));
passData.distanceBuffer = builder.ReadWriteTexture(renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true)
{ colorFormat = GraphicsFormat.R32_SFloat, enableRandomWrite = true, clearBuffer = true, name = "Distance Buffer" }));
passData.outputShadowBuffer = builder.ReadWriteTexture(renderGraph.CreateTexture(new TextureDesc(Vector2.one, true, true)
{ colorFormat = GraphicsFormat.R16G16B16A16_SFloat, enableRandomWrite = true, clearBuffer = true, name = "RT Directional Shadow" }));

builder.SetRenderFunc(
(RTSDirectionalTracePassData data, RenderGraphContext context) =>
Expand Down Expand Up @@ -143,7 +143,7 @@ void RenderDirectionalLightScreenSpaceShadow(RenderGraph renderGraph, HDCamera h
{
passData.parameters = PrepareSSShadowDirectionalParameters();
passData.normalBuffer = builder.ReadTexture(normalBuffer);
passData.screenSpaceShadowArray = builder.ReadTexture(builder.WriteTexture(screenSpaceShadowArray));
passData.screenSpaceShadowArray = builder.ReadWriteTexture(screenSpaceShadowArray);

builder.SetRenderFunc(
(SSSDirectionalTracePassData data, RenderGraphContext context) =>
Expand Down