浏览代码

Updated beffered RTHandle system

/main
Frédéric Vauchelles 6 年前
当前提交
c65c8258
共有 6 个文件被更改,包括 207 次插入51 次删除
  1. 47
      ScriptableRenderPipeline/Core/CoreRP/Textures/BufferedRTHandleSystem.cs
  2. 102
      ScriptableRenderPipeline/Core/CoreRP/Textures/RTHandleSystem.cs
  3. 1
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Camera/HDAdditionalCameraData.cs
  4. 53
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Camera/HDCamera.cs
  5. 43
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipeline.cs
  6. 12
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/RenderPipelineResources/BufferPyramid.cs

47
ScriptableRenderPipeline/Core/CoreRP/Textures/BufferedRTHandleSystem.cs


public void AllocBuffer(
int id,
Func<RTHandleSystem, RTHandleSystem.RTHandle> allocator,
Func<RTHandleSystem, int, RTHandleSystem.RTHandle> allocator,
m_RTHandles.Add(id, new RTHandleSystem.RTHandle[bufferSize]);
var buffer = new RTHandleSystem.RTHandle[bufferSize];
m_RTHandles.Add(id, buffer);
// First is autoresized
buffer[0] = allocator(m_RTHandleSystem, 0);
// Other are resized on demand
for (int i = 1, c = buffer.Length; i < c; ++i)
{
buffer[i] = allocator(m_RTHandleSystem, i);
m_RTHandleSystem.SwitchResizeMode(buffer[i], RTHandleSystem.ResizeMode.OnDemand);
}
}
public void SetReferenceSize(int width, int height, bool msaa, MSAASamples msaaSamples)
{
m_RTHandleSystem.SetReferenceSize(width, height, msaa, msaaSamples);
}
public void Swap()
{
foreach (var item in m_RTHandles)
{
var nextFirst = item.Value[item.Value.Length - 1];
for (int i = 0, c = item.Value.Length - 1; i < c; ++i)
item.Value[i + 1] = item.Value[i];
item.Value[0] = nextFirst;
// First is autoresize, other are on demand
m_RTHandleSystem.SwitchResizeMode(item.Value[0], RTHandleSystem.ResizeMode.Auto);
m_RTHandleSystem.SwitchResizeMode(item.Value[1], RTHandleSystem.ResizeMode.OnDemand);
}
}
void Dispose(bool disposing)

public void Dispose()
{
Dispose(true);
}
public void ReleaseAll()
{
foreach (var item in m_RTHandles)
{
for (int i = 0, c = item.Value.Length; i < c; ++i)
{
m_RTHandleSystem.Release(item.Value[i]);
}
}
m_RTHandles.Clear();
}
}
}

102
ScriptableRenderPipeline/Core/CoreRP/Textures/RTHandleSystem.cs


public partial class RTHandleSystem : IDisposable
{
public enum ResizeMode
{
Auto,
OnDemand
}
internal enum RTCategory
{
Regular = 0,

// Parameters for auto-scaled Render Textures
bool m_ScaledRTSupportsMSAA = false;
MSAASamples m_ScaledRTCurrentMSAASamples = MSAASamples.None;
List<RTHandle> m_AutoSizedRTs;
RTCategory m_ScaledRTCurrentCategory = RTCategory.Regular;
bool m_ScaledRTSupportsMSAA = false;
MSAASamples m_ScaledRTCurrentMSAASamples = MSAASamples.None;
HashSet<RTHandle> m_AutoSizedRTs;
RTHandle[] m_AutoSizedRTsArray; // For fast iteration
HashSet<RTHandle> m_ResizeOnDemandRTs;
RTCategory m_ScaledRTCurrentCategory = RTCategory.Regular;
int[] m_MaxWidths = new int[(int)RTCategory.Count];
int[] m_MaxHeights = new int[(int)RTCategory.Count];

public RTHandleSystem()
{
m_AutoSizedRTs = new List<RTHandle>();
m_AutoSizedRTs = new HashSet<RTHandle>();
m_ResizeOnDemandRTs = new HashSet<RTHandle>();
for (int i = 0; i < (int)RTCategory.Count; ++i)
{
m_MaxWidths[i] = 1;

Resize(width, height, category, msaaSamples);
}
public void SwitchResizeMode(RTHandle rth, ResizeMode mode)
{
switch (mode)
{
case ResizeMode.OnDemand:
m_AutoSizedRTs.Remove(rth);
m_ResizeOnDemandRTs.Add(rth);
break;
case ResizeMode.Auto:
// Resize now so it is consistent with other auto resize RTHs
if (m_ResizeOnDemandRTs.Contains(rth))
DemandResize(rth);
m_ResizeOnDemandRTs.Remove(rth);
m_AutoSizedRTs.Add(rth);
break;
}
}
public void DemandResize(RTHandle rth)
{
Assert.IsTrue(m_ResizeOnDemandRTs.Contains(rth), string.Format("The RTHandle {0} is not an resize on demand handle in this RTHandleSystem. Please call SwitchToResizeOnDemand(rth, true) before resizing on demand.", rth));
for (int i = 0, c = (int)RTCategory.Count; i < c; ++i)
{
if (rth.m_RTs[i] == null)
continue;
var rt = rth.m_RTs[i];
var scaledSize = rth.GetScaledSize(new Vector2Int(m_MaxWidths[i], m_MaxHeights[i]));
scaledSize = Vector2Int.Max(Vector2Int.one, scaledSize);
var enableMSAA = i == (int)RTCategory.MSAA;
var sizeChanged = rt.width != scaledSize.x || rt.height != scaledSize.y;
var msaaSampleChanged = enableMSAA && rt.antiAliasing != (int)m_ScaledRTCurrentMSAASamples;
if (sizeChanged || msaaSampleChanged)
{
rt.Release();
if (enableMSAA)
rt.antiAliasing = (int)m_ScaledRTCurrentMSAASamples;
rt.name = CoreUtils.GetRenderTargetAutoName(
rt.width,
rt.height,
rt.format,
rth.m_Name,
mips: rt.useMipMap,
enableMSAA : enableMSAA,
msaaSamples: m_ScaledRTCurrentMSAASamples
);
rt.Create();
}
}
}
int GetMaxWidth(RTCategory category) { return m_MaxWidths[(int)category]; }
int GetMaxHeight(RTCategory category) { return m_MaxHeights[(int)category]; }

{
for (int i = 0, c = m_AutoSizedRTs.Count; i < c; ++i)
Array.Resize(ref m_AutoSizedRTsArray, m_AutoSizedRTs.Count);
m_AutoSizedRTs.CopyTo(m_AutoSizedRTsArray);
for (int i = 0, c = m_AutoSizedRTsArray.Length; i < c; ++i)
var rt = m_AutoSizedRTs[i];
var rt = m_AutoSizedRTsArray[i];
Array.Resize(ref m_AutoSizedRTsArray, m_ResizeOnDemandRTs.Count);
m_ResizeOnDemandRTs.CopyTo(m_AutoSizedRTsArray);
for (int i = 0, c = m_AutoSizedRTsArray.Length; i < c; ++i)
{
var rt = m_AutoSizedRTsArray[i];
Release(rt);
}
m_ResizeOnDemandRTs.Clear();
m_AutoSizedRTsArray = null;
}
}

var maxSize = new Vector2Int(width, height);
m_ScaledRTCurrentCategory = category;
foreach (var rth in m_AutoSizedRTs)
Array.Resize(ref m_AutoSizedRTsArray, m_AutoSizedRTs.Count);
m_AutoSizedRTs.CopyTo(m_AutoSizedRTsArray);
for (int i = 0, c = m_AutoSizedRTsArray.Length; i < c; ++i)
var rth = m_AutoSizedRTsArray[i];
var rt = rth.m_RTs[(int)category];
// This can happen if you create a RTH for MSAA. By default we only create the MSAA version of the target.

public string DumpRTInfo()
{
string result = "";
for (int i = 0; i < m_AutoSizedRTs.Count; ++i)
Array.Resize(ref m_AutoSizedRTsArray, m_AutoSizedRTs.Count);
m_AutoSizedRTs.CopyTo(m_AutoSizedRTsArray);
for (int i = 0, c = m_AutoSizedRTsArray.Length; i < c; ++i)
RenderTexture rt = m_AutoSizedRTs[i].rt;
var rt = m_AutoSizedRTsArray[i].rt;
result = string.Format("{0}\nRT ({1})\t Format: {2} W: {3} H {4}\n", result, i, rt.format, rt.width, rt.height );
}

1
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Camera/HDAdditionalCameraData.cs


using System;
using UnityEngine.Serialization;
namespace UnityEngine.Experimental.Rendering.HDPipeline

53
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Camera/HDCamera.cs


HDAdditionalCameraData m_AdditionalCameraData;
// Fallback used when the HDAdditionalCameraData is missing
BufferedRTHandleSystem m_HistoryRTSystemFallback = null;
get { return m_AdditionalCameraData != null ? m_AdditionalCameraData.historyRTSystem : null; }
get
{
if (m_AdditionalCameraData == null)
{
if (m_HistoryRTSystemFallback == null)
m_HistoryRTSystemFallback = new BufferedRTHandleSystem();
return m_HistoryRTSystemFallback;
}
return m_AdditionalCameraData.historyRTSystem;
}
}
public HDCamera(Camera cam)

RTHandles.SetReferenceSize(m_ActualWidth, m_ActualHeight, frameSettings.enableMSAA, m_msaaSamples);
historyRTSystem.SetReferenceSize(m_ActualWidth, m_ActualHeight, frameSettings.enableMSAA, m_msaaSamples);
historyRTSystem.Swap();
int maxWidth = RTHandles.maxWidth;
int maxHeight = RTHandles.maxHeight;

return hdcam;
}
public static void ClearAll()
{
int frameCheck = Time.frameCount - 1;
foreach (var cam in s_Cameras)
cam.Value.historyRTSystem.ReleaseAll();
s_Cameras.Clear();
s_Cleanup.Clear();
}
// Look for any camera that hasn't been used in the last frame and remove them for the pool.
public static void CleanUnused()
{

}
foreach (var cam in s_Cleanup)
{
var hdCam = s_Cameras[cam];
if (hdCam.m_HistoryRTSystemFallback != null)
{
hdCam.m_HistoryRTSystemFallback.Dispose();
hdCam.m_HistoryRTSystemFallback = null;
}
}
s_Cleanup.Clear();
}

public RTHandleSystem.RTHandle GetPreviousFrameRT(int id)
{
if (m_AdditionalCameraData == null)
return null;
return m_AdditionalCameraData.historyRTSystem.GetFrameRT(id, 1);
return historyRTSystem.GetFrameRT(id, 1);
if (m_AdditionalCameraData == null)
return null;
return m_AdditionalCameraData.historyRTSystem.GetFrameRT(id, 0);
return historyRTSystem.GetFrameRT(id, 0);
public RTHandleSystem.RTHandle AllocHistoryFrameRT(int id, Func<RTHandleSystem, RTHandleSystem.RTHandle> allocator)
public RTHandleSystem.RTHandle AllocHistoryFrameRT(int id, Func<string, int, RTHandleSystem, RTHandleSystem.RTHandle> allocator)
if (m_AdditionalCameraData == null)
return null;
m_AdditionalCameraData.historyRTSystem.AllocBuffer(id, allocator, 2);
return m_AdditionalCameraData.historyRTSystem.GetFrameRT(id, 0);
historyRTSystem.AllocBuffer(id, (rts, i) => allocator(camera.name, i, rts), 2);
return historyRTSystem.GetFrameRT(id, 0);
}
}
}

43
ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipeline.cs


RTHandles.Release(m_DebugFullScreenTempBuffer);
m_DebugScreenSpaceTracingData.Release();
HDCamera.CleanUnused();
}

RenderForwardError(m_CullResults, hdCamera, renderContext, cmd, ForwardPass.PreRefraction);
RenderColorPyramid(hdCamera, cmd, renderContext, true);
RenderForward(m_CullResults, hdCamera, renderContext, cmd, ForwardPass.OpaqueSSReflection);
// Render all type of transparent forward (unlit, lit, complex (hair...)) to keep the sorting between transparent objects.
RenderForward(m_CullResults, hdCamera, renderContext, cmd, ForwardPass.Transparent);

var camera = hdCamera.camera;
m_LightLoop.RenderForward(camera, cmd, pass == ForwardPass.Opaque);
var debugScreenSpaceTracing = m_CurrentDebugDisplaySettings.fullScreenDebugMode == FullScreenDebugMode.ScreenSpaceTracing;
if (pass == ForwardPass.Opaque)
switch (pass)
var bindIndex = 0;
// In case of forward SSS we will bind all the required target. It is up to the shader to write into it or not.
if (m_FrameSettings.enableSubsurfaceScattering)
{

}
HDUtils.SetRenderTarget(cmd, hdCamera, m_MRTWithSSS, m_CameraDepthStencilBuffer);
bindIndex += m_MRTWithSSS.Length;
++bindIndex;
}
m_ForwardAndForwardOnlyPassNames[0] = m_ForwardOnlyPassNames[0] =

var passNames = m_FrameSettings.enableForwardRenderingOnly
? m_ForwardAndForwardOnlyPassNames
: m_ForwardOnlyPassNames;
var debugSSTThisPass = debugScreenSpaceTracing && (m_CurrentDebugDisplaySettings.lightingDebugSettings.debugLightingMode == DebugLightingMode.ScreenSpaceTracingReflection);
if (debugSSTThisPass)
{
cmd.SetGlobalBuffer(HDShaderIDs._DebugScreenSpaceTracingData, m_DebugScreenSpaceTracingData);
cmd.SetRandomWriteTarget(bindIndex, m_DebugScreenSpaceTracingData);
}
}
else
if (debugSSTThisPass)
cmd.ClearRandomWriteTargets();
break;
// Assign debug data
if (m_CurrentDebugDisplaySettings.fullScreenDebugMode == FullScreenDebugMode.ScreenSpaceTracing
&& pass == ForwardPass.Transparent)
default:
cmd.SetGlobalBuffer(HDShaderIDs._DebugScreenSpaceTracingData, m_DebugScreenSpaceTracingData);
cmd.SetRandomWriteTarget(1, m_DebugScreenSpaceTracingData);
}
if ((m_FrameSettings.enableDBuffer) && (DecalSystem.m_DecalsVisibleThisFrame > 0)) // enable d-buffer flag value is being interpreted more like enable decals in general now that we have clustered
{
if ((m_FrameSettings.enableDBuffer) && (DecalSystem.m_DecalsVisibleThisFrame > 0)) // enable d-buffer flag value is being interpreted more like enable decals in general now that we have clustered
var debugSSTThisPass = debugScreenSpaceTracing && (m_CurrentDebugDisplaySettings.lightingDebugSettings.debugLightingMode == DebugLightingMode.ScreenSpaceTracingRefraction);
if (debugSSTThisPass)
{
cmd.SetGlobalBuffer(HDShaderIDs._DebugScreenSpaceTracingData, m_DebugScreenSpaceTracingData);
cmd.SetRandomWriteTarget(1, m_DebugScreenSpaceTracingData);
if (m_CurrentDebugDisplaySettings.fullScreenDebugMode == FullScreenDebugMode.ScreenSpaceTracing
&& pass == ForwardPass.Transparent)
{
cmd.ClearRandomWriteTargets();
if (debugSSTThisPass)
cmd.ClearRandomWriteTargets();
}
}
}

12
ScriptableRenderPipeline/HDRenderPipeline/HDRP/RenderPipelineResources/BufferPyramid.cs


cmd.SetGlobalTexture(HDShaderIDs._ColorPyramidTexture, targetColorTexture);
}
public RTHandleSystem.RTHandle AllocColorRT(RTHandleSystem rtHandleSystem)
public RTHandleSystem.RTHandle AllocColorRT(string id, int frameIndex, RTHandleSystem rtHandleSystem)
{
return rtHandleSystem.Alloc(
size => CalculatePyramidSize(size),

useMipMap: true,
autoGenerateMips: false,
enableRandomWrite: true,
name: "ColorPyramid"
name: string.Format("ColorPyramid-{0}-{1}", id, frameIndex)
public RTHandleSystem.RTHandle AllocDepthRT(RTHandleSystem rtHandleSystem)
public RTHandleSystem.RTHandle AllocDepthRT(string id, int frameIndex, RTHandleSystem rtHandleSystem)
{
return rtHandleSystem.Alloc(
size => CalculatePyramidSize(size),

useMipMap: true,
autoGenerateMips: false,
enableRandomWrite: true,
name: "DepthPyramid"
); // Need randomReadWrite because we downsample the first mip with a compute shader.
enableRandomWrite: true, // Need randomReadWrite because we downsample the first mip with a compute shader.
name: string.Format("DepthPyramid-{0}-{1}", id, frameIndex)
);
}
}
}
正在加载...
取消
保存