Skip to content

Compute Buffer management for render graph #194

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 13 commits into from
Apr 21, 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
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ public ProfilingScope(CommandBuffer cmd, ProfilingSampler sampler)

if (cmd != null)
#if UNITY_USE_RECORDER
cmd.BeginSample(m_Sampler);
if (m_Sampler != null)
cmd.BeginSample(m_Sampler);
else
cmd.BeginSample(m_Name);
#else
cmd.BeginSample(m_Name);
#endif
Expand Down Expand Up @@ -245,7 +248,10 @@ void Dispose(bool disposing)
{
if (m_Cmd != null)
#if UNITY_USE_RECORDER
m_Cmd.EndSample(m_Sampler);
if (m_Sampler != null)
m_Cmd.EndSample(m_Sampler);
else
m_Cmd.EndSample(m_Name);
#else
m_Cmd.EndSample(m_Name);
#endif
Expand Down
62 changes: 39 additions & 23 deletions com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraph.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Collections.Generic;
using UnityEngine.Rendering;
using UnityEngine.Profiling;
Expand Down Expand Up @@ -94,6 +95,7 @@ public class RenderGraph
///<summary>Maximum number of MRTs supported by Render Graph.</summary>
public static readonly int kMaxMRTCount = 8;

[DebuggerDisplay("RenderPass ({name})")]
internal abstract class RenderPass
{
internal RenderFunc<PassData> GetExecuteDelegate<PassData>()
Expand All @@ -103,28 +105,32 @@ internal RenderFunc<PassData> GetExecuteDelegate<PassData>()
internal abstract void Release(RenderGraphContext renderGraphContext);
internal abstract bool HasRenderFunc();

internal string name;
internal int index;
internal ProfilingSampler customSampler;
internal List<TextureHandle> resourceReadList = new List<TextureHandle>();
internal List<TextureHandle> resourceWriteList = new List<TextureHandle>();
internal List<RendererListHandle> usedRendererListList = new List<RendererListHandle>();
internal bool enableAsyncCompute;
internal TextureHandle depthBuffer { get { return m_DepthBuffer; } }
internal TextureHandle[] colorBuffers { get { return m_ColorBuffers; } }
internal int colorBufferMaxIndex { get { return m_MaxColorBufferIndex; } }

protected TextureHandle[] m_ColorBuffers = new TextureHandle[kMaxMRTCount];
protected TextureHandle m_DepthBuffer;
protected int m_MaxColorBufferIndex = -1;
internal string name;
internal int index;
internal ProfilingSampler customSampler;
internal List<TextureHandle> textureReadList = new List<TextureHandle>();
internal List<TextureHandle> textureWriteList = new List<TextureHandle>();
internal List<ComputeBufferHandle> bufferReadList = new List<ComputeBufferHandle>();
internal List<ComputeBufferHandle> bufferWriteList = new List<ComputeBufferHandle>();
internal List<RendererListHandle> usedRendererListList = new List<RendererListHandle>();
internal bool enableAsyncCompute;
internal TextureHandle depthBuffer { get { return m_DepthBuffer; } }
internal TextureHandle[] colorBuffers { get { return m_ColorBuffers; } }
internal int colorBufferMaxIndex { get { return m_MaxColorBufferIndex; } }

protected TextureHandle[] m_ColorBuffers = new TextureHandle[kMaxMRTCount];
protected TextureHandle m_DepthBuffer;
protected int m_MaxColorBufferIndex = -1;

internal void Clear()
{
name = "";
index = -1;
customSampler = null;
resourceReadList.Clear();
resourceWriteList.Clear();
textureReadList.Clear();
textureWriteList.Clear();
bufferReadList.Clear();
bufferWriteList.Clear();
usedRendererListList.Clear();
enableAsyncCompute = false;

Expand All @@ -142,16 +148,16 @@ internal void SetColorBuffer(TextureHandle resource, int index)
Debug.Assert(index < RenderGraph.kMaxMRTCount && index >= 0);
m_MaxColorBufferIndex = Math.Max(m_MaxColorBufferIndex, index);
m_ColorBuffers[index] = resource;
resourceWriteList.Add(resource);
textureWriteList.Add(resource);
}

internal void SetDepthBuffer(TextureHandle resource, DepthAccess flags)
{
m_DepthBuffer = resource;
if ((flags | DepthAccess.Read) != 0)
resourceReadList.Add(resource);
textureReadList.Add(resource);
if ((flags | DepthAccess.Write) != 0)
resourceWriteList.Add(resource);
textureWriteList.Add(resource);

}
}
Expand Down Expand Up @@ -304,6 +310,16 @@ public RendererListHandle CreateRendererList(in RendererListDesc desc)
return m_Resources.CreateRendererList(desc);
}

/// <summary>
/// Import an external Compute Buffer to the Render Graph
/// </summary>
/// <param name="computeBuffer">External Compute Buffer that needs to be imported.</param>
/// <returns>A new ComputeBufferHandle.</returns>
public ComputeBufferHandle ImportComputeBuffer(ComputeBuffer computeBuffer)
{
return m_Resources.ImportComputeBuffer(computeBuffer);
}

/// <summary>
/// Add a new Render Pass to the current Render Graph.
/// </summary>
Expand Down Expand Up @@ -460,18 +476,18 @@ void PreRenderPassSetRenderTargets(in RenderPass pass, RenderGraphContext rgCont
void PreRenderPassExecute(int passIndex, in RenderPass pass, RenderGraphContext rgContext)
{
// TODO merge clear and setup here if possible
m_Resources.CreateAndClearTexturesForPass(rgContext, pass.index, pass.resourceWriteList);
m_Resources.CreateAndClearTexturesForPass(rgContext, pass.index, pass.textureWriteList);
PreRenderPassSetRenderTargets(pass, rgContext);
m_Resources.PreRenderPassSetGlobalTextures(rgContext, pass.resourceReadList);
m_Resources.PreRenderPassSetGlobalTextures(rgContext, pass.textureReadList);
}

void PostRenderPassExecute(int passIndex, in RenderPass pass, RenderGraphContext rgContext)
{
if (m_DebugParameters.unbindGlobalTextures)
m_Resources.PostRenderPassUnbindGlobalTextures(rgContext, pass.resourceReadList);
m_Resources.PostRenderPassUnbindGlobalTextures(rgContext, pass.textureReadList);

m_RenderGraphPool.ReleaseAllTempAlloc();
m_Resources.ReleaseTexturesForPass(rgContext, pass.index, pass.resourceReadList, pass.resourceWriteList);
m_Resources.ReleaseTexturesForPass(rgContext, pass.index, pass.textureReadList, pass.textureWriteList);
pass.Release(rgContext);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public TextureHandle UseDepthBuffer(TextureHandle input, DepthAccess flags)
/// <returns>An updated resource handle to the input resource.</returns>
public TextureHandle ReadTexture(TextureHandle input)
{
m_RenderPass.resourceReadList.Add(input);
m_RenderPass.textureReadList.Add(input);
m_Resources.UpdateTextureLastRead(input, m_RenderPass.index);
return input;
}
Expand All @@ -63,7 +63,7 @@ public TextureHandle ReadTexture(TextureHandle input)
public TextureHandle WriteTexture(TextureHandle input)
{
// TODO: Manage resource "version" for debugging purpose
m_RenderPass.resourceWriteList.Add(input);
m_RenderPass.textureWriteList.Add(input);
m_Resources.UpdateTextureFirstWrite(input, m_RenderPass.index);
return input;
}
Expand All @@ -79,6 +79,28 @@ public RendererListHandle UseRendererList(RendererListHandle input)
return input;
}

/// <summary>
/// Specify a Compute Buffer resource to read from during the pass.
/// </summary>
/// <param name="input">The Compute Buffer resource to read from during the pass.</param>
/// <returns>An updated resource handle to the input resource.</returns>
public ComputeBufferHandle ReadComputeBuffer(ComputeBufferHandle input)
{
m_RenderPass.bufferReadList.Add(input);
return input;
}

/// <summary>
/// Specify a Compute Buffer resource to write to during the pass.
/// </summary>
/// <param name="input">The Compute Buffer resource to write to during the pass.</param>
/// <returns>An updated resource handle to the input resource.</returns>
public ComputeBufferHandle WriteComputeBuffer(ComputeBufferHandle input)
{
m_RenderPass.bufferWriteList.Add(input);
return input;
}

/// <summary>
/// Specify the render function to use for this pass.
/// A call to this is mandatory for the pass to be valid.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@ public struct RendererListHandle
public bool IsValid() => m_IsValid;
}

/// <summary>
/// Compute Buffer resource handle.
/// </summary>
[DebuggerDisplay("ComputeBuffer ({handle})")]
public struct ComputeBufferHandle
{
bool m_IsValid;
internal int handle { get; private set; }
internal ComputeBufferHandle(int handle) { this.handle = handle; m_IsValid = true; }
/// <summary>
/// Conversion to int.
/// </summary>
/// <param name="handle">Compute Buffer handle to convert.</param>
/// <returns>The integer representation of the handle.</returns>
public static implicit operator int(ComputeBufferHandle handle) { return handle.handle; }
/// <summary>
/// Return true if the handle is valid.
/// </summary>
/// <returns>True if the handle is valid.</returns>
public bool IsValid() => m_IsValid;
}

/// <summary>
/// The mode that determines the size of a Texture.
/// </summary>
Expand Down Expand Up @@ -316,11 +338,22 @@ internal RendererListResource(in RendererListDesc desc)
this.rendererList = new RendererList(); // Invalid by default
}
}

internal struct ComputeBufferResource
{
public ComputeBuffer computeBuffer;

internal ComputeBufferResource(ComputeBuffer computeBuffer)
{
this.computeBuffer = computeBuffer;
}
}
#endregion

DynamicArray<TextureResource> m_TextureResources = new DynamicArray<TextureResource>();
Dictionary<int, Stack<RTHandle>> m_TexturePool = new Dictionary<int, Stack<RTHandle>>();
DynamicArray<RendererListResource> m_RendererListResources = new DynamicArray<RendererListResource>();
DynamicArray<ComputeBufferResource> m_ComputeBufferResources = new DynamicArray<ComputeBufferResource>();
RTHandleSystem m_RTHandleSystem = new RTHandleSystem();
RenderGraphDebugParams m_RenderGraphDebug;
RenderGraphLogger m_Logger;
Expand Down Expand Up @@ -358,14 +391,27 @@ public RTHandle GetTexture(in TextureHandle handle)
/// Returns the RendererList associated with the provided resource handle.
/// </summary>
/// <param name="handle">Handle to a Renderer List resource.</param>
/// <returns>The Renderer List associated with the provided resource handle or a null renderer list if the handle is invalid.</returns>
/// <returns>The Renderer List associated with the provided resource handle or an invalid renderer list if the handle is invalid.</returns>
public RendererList GetRendererList(in RendererListHandle handle)
{
if (!handle.IsValid())
return RendererList.nullRendererList;

return m_RendererListResources[handle].rendererList;
}

/// <summary>
/// Returns the Compute Buffer associated with the provided resource handle.
/// </summary>
/// <param name="handle">Handle to a Compute Buffer resource.</param>
/// <returns>The Compute Buffer associated with the provided resource handle or a null reference if the handle is invalid.</returns>
public ComputeBuffer GetComputeBuffer(in ComputeBufferHandle handle)
{
if (!handle.IsValid())
return null;

return m_ComputeBufferResources[handle].computeBuffer;
}
#endregion

#region Internal Interface
Expand Down Expand Up @@ -456,6 +502,12 @@ internal RendererListHandle CreateRendererList(in RendererListDesc desc)
return new RendererListHandle(newHandle);
}

internal ComputeBufferHandle ImportComputeBuffer(ComputeBuffer computeBuffer)
{
int newHandle = m_ComputeBufferResources.Add(new ComputeBufferResource(computeBuffer));
return new ComputeBufferHandle(newHandle);
}

internal void CreateAndClearTexturesForPass(RenderGraphContext rgContext, int passIndex, List<TextureHandle> textures)
{
foreach (var rgResource in textures)
Expand Down Expand Up @@ -704,6 +756,7 @@ internal void Clear()

m_TextureResources.Clear();
m_RendererListResources.Clear();
m_ComputeBufferResources.Clear();

#if DEVELOPMENT_BUILD || UNITY_EDITOR
if (m_AllocatedTextures.Count != 0)
Expand Down
Loading