浏览代码

[gc]Convert to pooling

/RenderPassXR_Sandbox
Tim Cooper 8 年前
当前提交
b1156959
共有 16 个文件被更改,包括 85 次插入72 次删除
  1. 1
      Assets/Editor/Tests/RenderloopTests/RenderPipelineTestFixture.cs
  2. 4
      Assets/ScriptableRenderPipeline/BasicRenderPipeline/BasicRenderPipeline.cs
  3. 17
      Assets/ScriptableRenderPipeline/Core/Shadow/Shadow.cs
  4. 24
      Assets/ScriptableRenderPipeline/Fptl/FptlLighting.cs
  5. 2
      Assets/ScriptableRenderPipeline/Fptl/SkyboxHelper.cs
  6. 25
      Assets/ScriptableRenderPipeline/HDRenderPipeline/HDRenderPipeline.cs
  7. 3
      Assets/ScriptableRenderPipeline/HDRenderPipeline/Lighting/AmbientOcclusion/ScreenSpaceAmbientOcclusion.cs
  8. 12
      Assets/ScriptableRenderPipeline/HDRenderPipeline/Lighting/TilePass/TilePass.cs
  9. 3
      Assets/ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/Lit.cs
  10. 2
      Assets/ScriptableRenderPipeline/HDRenderPipeline/Sky/HDRISky/HDRISkyRenderer.cs
  11. 4
      Assets/ScriptableRenderPipeline/HDRenderPipeline/Sky/ProceduralSky/ProceduralSkyRenderer.cs
  12. 6
      Assets/ScriptableRenderPipeline/HDRenderPipeline/Sky/RuntimeFilterIBL.cs
  13. 3
      Assets/ScriptableRenderPipeline/HDRenderPipeline/Sky/SkyManager.cs
  14. 10
      Assets/ScriptableRenderPipeline/HDRenderPipeline/Utilities.cs
  15. 9
      Assets/ScriptableRenderPipeline/LightweightPipeline/LightweightPipeline.cs
  16. 32
      Assets/ScriptableRenderPipeline/core/CommandBufferPool.cs

1
Assets/Editor/Tests/RenderloopTests/RenderPipelineTestFixture.cs


}
renderContext.Submit();
CommandBufferPool.EndOfFrame();
}
public static void Run(TestDelegate renderCallback)

4
Assets/ScriptableRenderPipeline/BasicRenderPipeline/BasicRenderPipeline.cs


{
base.Render(renderContext, cameras);
BasicRendering.Render(renderContext, cameras);
CommandBufferPool.EndOfFrame();
}
}

var cmd = CommandBufferPool.Get();
cmd.ClearRenderTarget(true, false, Color.black);
context.ExecuteCommandBuffer(cmd);
cmd.Release();
CommandBufferPool.Release(cmd);
// Setup global lighting shader variables
SetupLightShaderVariables(cull.visibleLights, context);

cmd.SetGlobalVector("globalLightCount", new Vector4(lightCount, 0, 0, 0));
cmd.SetGlobalVectorArray("globalSH", shConstants);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
// Prepare L2 spherical harmonics values for efficient evaluation in a shader

17
Assets/ScriptableRenderPipeline/Core/Shadow/Shadow.cs


cb.name = "Shadowmap.EnableShadowKeyword";
cb.EnableShaderKeyword(m_ShaderKeyword);
renderContext.ExecuteCommandBuffer( cb );
CommandBufferPool.Release(cb);
}
// loop for generating each individual shadowmap

cb.SetViewProjectionMatrices( m_EntryCache[i].current.view, m_EntryCache[i].current.proj );
cb.SetGlobalVector( "g_vLightDirWs", m_EntryCache[i].current.lightDir );
renderContext.ExecuteCommandBuffer( cb );
CommandBufferPool.Release(cb);
dss.lightIndex = m_EntryCache[i].key.visibleIdx;
dss.splitData = m_EntryCache[i].current.splitData;

cblast.DisableShaderKeyword( m_ShaderKeyword );
}
renderContext.ExecuteCommandBuffer( cblast );
CommandBufferPool.Release(cblast);
m_ActiveEntriesCount = 0;

debugCB.DrawProcedural(Matrix4x4.identity, m_DebugMaterial, m_DebugMaterial.FindPass("REGULARSHADOW"), MeshTopology.Triangles, 3, 1, propertyBlock);
renderContext.ExecuteCommandBuffer(debugCB);
CommandBufferPool.Release(debugCB);
}
}

debugCB.DrawProcedural(Matrix4x4.identity, m_DebugMaterial, m_DebugMaterial.FindPass("VARIANCESHADOW"), MeshTopology.Triangles, 3, 1, propertyBlock);
renderContext.ExecuteCommandBuffer(debugCB);
CommandBufferPool.Release(debugCB);
}
}
// -------------------------------------------------------------------------------------------------------------------------------------------------

m_ShadowCtxt.SyncData();
}
public override void BindResources( ScriptableRenderContext renderContext )
public override void BindResources(ScriptableRenderContext renderContext)
foreach( var sm in m_Shadowmaps )
foreach (var sm in m_Shadowmaps)
sm.Fill( m_ShadowCtxt );
sm.Fill(m_ShadowCtxt);
m_ShadowCtxt.BindResources( cb );
renderContext.ExecuteCommandBuffer( cb );
m_ShadowCtxt.BindResources(cb);
renderContext.ExecuteCommandBuffer(cb);
CommandBufferPool.Release(cb);
}
// resets the shadow slot counters and returns the sum of all slots

24
Assets/ScriptableRenderPipeline/Fptl/FptlLighting.cs


{
base.Render(renderContext, cameras);
m_Owner.Render(renderContext, cameras);
CommandBufferPool.EndOfFrame();
}
}

var cmd = CommandBufferPool.Get ("Create G-Buffer");
SetupGBuffer(camera.pixelWidth, camera.pixelHeight, cmd);
loop.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
// render opaque objects using Deferred pass
var settings = new DrawRendererSettings(cull, camera, new ShaderPassName("Deferred"))
{

cmd.SetGlobalBuffer("g_vLightListGlobal", useFptl ? s_LightList : s_PerVoxelLightLists);
loop.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
// render opaque objects using Deferred pass
var settings = new DrawRendererSettings(cull, camera, new ShaderPassName("ForwardSinglePass"))

var cmd = CommandBufferPool.Get("Forward Opaques - Depth Only" );
cmd.SetRenderTarget(new RenderTargetIdentifier(s_GBufferZ));
loop.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
// render opaque objects using Deferred pass
var settings = new DrawRendererSettings(cull, camera, new ShaderPassName("DepthOnly"))

var cmd = CommandBufferPool.Get("Copy depth");
cmd.CopyTexture(new RenderTargetIdentifier(s_GBufferZ), new RenderTargetIdentifier(s_CameraDepthTexture));
loop.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
void DoTiledDeferredLighting(Camera camera, ScriptableRenderContext loop, int numLights, int numDirLights)

cmd.SetRenderTarget(new RenderTargetIdentifier(s_CameraTarget), new RenderTargetIdentifier(s_CameraDepthTexture));
loop.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
private static void SetMatrixCS(CommandBuffer cmd, ComputeShader shadercs, string name, Matrix4x4 mat)

var cmd = CommandBufferPool.Get("FinalPass");
cmd.Blit(s_CameraTarget, BuiltinRenderTextureType.CameraTarget, m_BlitMaterial, 0);
loop.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
void ExecuteRenderLoop(Camera camera, CullResults cullResults, ScriptableRenderContext loop)

var cmd = CommandBufferPool.Get();
cmd.SetRenderTarget(BuiltinRenderTextureType.CameraTarget, new RenderTargetIdentifier(s_CameraDepthTexture));
loop.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
loop.Submit();
}

m_DebugLightBoundsMaterial.SetBuffer("g_data", s_ConvexBoundsBuffer);
cmd.DrawProcedural(Matrix4x4.identity, m_DebugLightBoundsMaterial, 0, MeshTopology.Triangles, 12 * 3 * numLights);
loop.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
void NewFrame()

}
loop.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
void PushGlobalParams(Camera camera, ScriptableRenderContext loop, Matrix4x4 viewToWorld, Matrix4x4 scrProj, Matrix4x4 incScrProj, int numDirLights)

2
Assets/ScriptableRenderPipeline/Fptl/SkyboxHelper.cs


}
loop.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
public static Matrix4x4 GetProjectionMatrix(Camera camera)

25
Assets/ScriptableRenderPipeline/HDRenderPipeline/HDRenderPipeline.cs


cmd.SetGlobalVectorArray("_TransmissionTints", sssParameters.transmissionTints);
renderContext.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
bool NeedDepthBufferCopy()

cmd.SetGlobalTexture("_MainDepthTexture", GetDepthTexture());
renderContext.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
public void UpdateCommonSettings()

if (camera == null)
{
renderContext.Submit();
CommandBufferPool.EndOfFrame();
return;
}

if (!CullResults.GetCullingParameters(camera, out cullingParams))
{
renderContext.Submit();
CommandBufferPool.EndOfFrame();
return;
}

var cmd = CommandBufferPool.Get("Blit to final RT" );
cmd.Blit(m_CameraColorBufferRT, BuiltinRenderTextureType.CameraTarget);
renderContext.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
else
{

var cmd = CommandBufferPool.Get();
cmd.SetRenderTarget(BuiltinRenderTextureType.CameraTarget, m_CameraDepthStencilBufferRT);
renderContext.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
CommandBufferPool.EndOfFrame();
}
void RenderOpaqueRenderList(CullResults cull, Camera camera, ScriptableRenderContext renderContext, string passName, RendererConfiguration rendererConfiguration = 0)

var cmd = CommandBufferPool.Get("DebugViewMaterialGBuffer" );
cmd.Blit(null, m_CameraColorBufferRT, m_DebugViewMaterialGBuffer, 0);
renderContext.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
// Render forward transparent

var cmd = CommandBufferPool.Get("Blit DebugView Material Debug");
cmd.Blit(m_CameraColorBufferRT, BuiltinRenderTextureType.CameraTarget);
renderContext.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
}

}
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
void UpdateSkyEnvironment(HDCamera hdCamera, ScriptableRenderContext renderContext)

cmd.SetRenderTarget(m_DistortionBufferRT, m_CameraDepthStencilBufferRT);
cmd.ClearRenderTarget(false, true, Color.black); // TODO: can we avoid this clear for performance ?
renderContext.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
// Only transparent object can render distortion vectors
RenderTransparentRenderList(cullResults, camera, renderContext, "DistortionVectors");

}
renderContext.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
}

}
renderContext.ExecuteCommandBuffer(debugCB);
CommandBufferPool.Release(debugCB);
m_LightLoop.RenderDebugOverlay(camera.camera, renderContext, m_DebugDisplaySettings, ref x, ref y, overlaySize, camera.camera.pixelWidth);
}

}
renderContext.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
Utilities.SetRenderTarget(renderContext, m_CameraColorBufferRT, m_CameraDepthStencilBufferRT, ClearFlag.ClearDepth);
}

3
Assets/ScriptableRenderPipeline/HDRenderPipeline/Lighting/AmbientOcclusion/ScreenSpaceAmbientOcclusion.cs


if (m_Command == null)
{
m_Command = CommandBufferPool.Get("Ambient Occlusion");
m_Command = new CommandBuffer();
m_Command.name = "Ambient Occlusion";
}
else
{

12
Assets/ScriptableRenderPipeline/HDRenderPipeline/Lighting/TilePass/TilePass.cs


cmd.SetComputeIntParam(buildDispatchIndirectShader, "g_NumTilesX", numTilesX);
cmd.DispatchCompute(buildDispatchIndirectShader, s_BuildDispatchIndirectKernel, (numTiles + 63) / 64, 1, 1);
}
loop.ExecuteCommandBuffer(cmd);
loop.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
// This is a workaround for global properties not being accessible from compute.

SetGlobalPropertyRedirect(null, 0, null);
loop.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
#if UNITY_EDITOR

SetGlobalPropertyRedirect(null, 0, null);
renderContext.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
public void RenderDeferredLighting( HDCamera hdCamera, ScriptableRenderContext renderContext,

SetGlobalPropertyRedirect(null, 0, null);
renderContext.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
} // TilePass - Deferred Lighting Pass
}

}
renderContext.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
public void RenderDebugOverlay(Camera camera, ScriptableRenderContext renderContext, DebugDisplaySettings debugDisplaySettings, ref float x, ref float y, float overlaySize, float width)

3
Assets/ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/Lit.cs


cmd.name = "Init PreFGD";
cmd.Blit(null, new RenderTargetIdentifier(m_PreIntegratedFGD), m_InitPreFGD, 0);
renderContext.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
m_isInit = true;
}

2
Assets/ScriptableRenderPipeline/HDRenderPipeline/Sky/HDRISky/HDRISkyRenderer.cs


var cmd = CommandBufferPool.Get("");
cmd.DrawMesh(builtinParams.skyMesh, Matrix4x4.identity, m_SkyHDRIMaterial, 0, renderForCubemap ? 0 : 1);
builtinParams.renderContext.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
public override bool IsSkyValid()

4
Assets/ScriptableRenderPipeline/HDRenderPipeline/Sky/ProceduralSky/ProceduralSkyRenderer.cs


SetUniforms(builtinParams, m_ProceduralSkySettings, renderForCubemap, ref properties);
var cmd = CommandBufferPool.Get("");
CommandBufferPool.Release(cmd);
}
}
}

6
Assets/ScriptableRenderPipeline/HDRenderPipeline/Sky/RuntimeFilterIBL.cs


var cmd = CommandBufferPool.Get("Compute GGX IBL Sample Data");
cmd.DispatchCompute(m_ComputeGgxIblSampleDataCS, m_ComputeGgxIblSampleDataKernel, 1, 1, 1);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
}

var cmd = CommandBufferPool.Get();
cmd.DrawMesh(cubemapFaceMesh[face], Matrix4x4.identity, m_GgxConvolveMaterial, 0, 0, props);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
}
}

cmd.DispatchCompute(m_BuildProbabilityTablesCS, m_ConditionalDensitiesKernel, numRows, 1, 1);
cmd.DispatchCompute(m_BuildProbabilityTablesCS, m_MarginalRowDensitiesKernel, 1, 1, 1);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
m_GgxConvolveMaterial.EnableKeyword("USE_MIS");
m_GgxConvolveMaterial.SetTexture("_ConditionalDensities", conditionalCdf);

3
Assets/ScriptableRenderPipeline/HDRenderPipeline/Sky/SkyManager.cs


propertyBlock.SetFloat("_faceIndex", (float)i);
cmd.DrawProcedural(Matrix4x4.identity, m_BlitCubemapMaterial, 0, MeshTopology.Triangles, 3, 1, propertyBlock);
renderContext.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
}

{
m_iblFilterGgx.FilterCubemap(renderContext, input, target, mipCount, m_CubemapFaceMesh);
}
CommandBufferPool.Release(cmd);
}
}

10
Assets/ScriptableRenderPipeline/HDRenderPipeline/Utilities.cs


if (clearFlag != ClearFlag.ClearNone)
cmd.ClearRenderTarget((clearFlag & ClearFlag.ClearDepth) != 0, (clearFlag & ClearFlag.ClearColor) != 0, clearColor);
renderContext.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
public static void SetRenderTarget(ScriptableRenderContext renderContext, RenderTargetIdentifier buffer, ClearFlag clearFlag = ClearFlag.ClearNone, int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown)

if (clearFlag != ClearFlag.ClearNone)
cmd.ClearRenderTarget((clearFlag & ClearFlag.ClearDepth) != 0, (clearFlag & ClearFlag.ClearColor) != 0, clearColor);
renderContext.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
public static void SetRenderTarget(ScriptableRenderContext renderContext, RenderTargetIdentifier[] colorBuffers, RenderTargetIdentifier depthBuffer)

}
renderContext.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
// Miscellanous

cmd.name = "";
cmd.BeginSample(name);
renderContext.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
public void Dispose()

cmd.name = "";
cmd.EndSample(name);
renderContext.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
disposed = true;

9
Assets/ScriptableRenderPipeline/LightweightPipeline/LightweightPipeline.cs


var cmd = CommandBufferPool.Get("Clear");
cmd.ClearRenderTarget(true, true, camera.backgroundColor);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
// Setup light and shadow shader constants
SetupLightShaderVariables(visibleLights, ref m_CullResults, ref context, ref lightData);

var discardRT = CommandBufferPool.Get();
discardRT.ReleaseTemporaryRT(m_ShadowMapProperty);
context.ExecuteCommandBuffer(discardRT);
CommandBufferPool.Release(cmd);
context.DrawRenderers(ref unlitSettings);

}
context.Submit();
CommandBufferPool.EndOfFrame();
}
private void BuildShadowSettings()

cmd.SetGlobalVector("globalLightData", new Vector4(lightData.pixelLightsCount, m_ShadowLightIndex, m_Asset.ShadowMinNormalBias, m_Asset.ShadowNormalBias));
SetShaderKeywords(cmd, lightData.isSingleDirectionalLight, lightData.vertexLightsCount > 0);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
private bool RenderShadows(ref CullResults cullResults, ref VisibleLight shadowLight, ref ScriptableRenderContext context)

setRenderTargetCommandBuffer.SetRenderTarget(m_ShadowMapRTID);
setRenderTargetCommandBuffer.ClearRenderTarget(true, true, Color.black);
context.ExecuteCommandBuffer(setRenderTargetCommandBuffer);
CommandBufferPool.Release(setRenderTargetCommandBuffer);
float shadowNearPlane = m_Asset.ShadowNearOffset;
Vector3 splitRatio = m_ShadowSettings.directionalLightCascades;

context.ExecuteCommandBuffer(buffer);
context.DrawShadows(ref settings);
CommandBufferPool.Release(buffer);
}
private int GetMaxTileResolutionInAtlas(int atlasWidth, int atlasHeight, int tileCount)

setupShadow.SetGlobalVectorArray("_DirShadowSplitSpheres", m_DirectionalShadowSplitDistances);
setupShadow.SetGlobalFloatArray("_PCFKernel", pcfKernel);
context.ExecuteCommandBuffer(setupShadow);
CommandBufferPool.Release(setupShadow);
}
private void SetShaderKeywords(CommandBuffer cmd, bool singleDirecitonal, bool vertexLightSupport)

32
Assets/ScriptableRenderPipeline/core/CommandBufferPool.cs


using System;
using System.Collections.Generic;
using System.Collections.Generic;
class ObjectPool<T> where T : new()
internal class ObjectPool<T> where T : new()
private readonly List<T> m_Loaned = new List<T>();
public int countAll { get; private set; }
public int countActive { get { return countAll - countInactive; } }
public int countInactive { get { return m_Stack.Count; } }
public ObjectPool(UnityAction<T> actionOnGet, UnityAction<T> actionOnRelease)
{
m_ActionOnGet = actionOnGet;

if (m_Stack.Count == 0)
{
element = new T();
countAll++;
m_Loaned.Add(element);
public void ReleaseAll()
public void Release(T element)
foreach (var element in m_Loaned)
{
if (m_ActionOnRelease != null)
m_ActionOnRelease(element);
m_Stack.Push(element);
}
m_Loaned.Clear();
if (m_Stack.Count > 0 && ReferenceEquals(m_Stack.Peek(), element))
Debug.LogError("Internal error. Trying to destroy object that is already released to pool.");
if (m_ActionOnRelease != null)
m_ActionOnRelease(element);
m_Stack.Push(element);
public class CommandBufferPool
public static class CommandBufferPool
{
private static ObjectPool<CommandBuffer> m_BufferPool = new ObjectPool<CommandBuffer>(null, x => x.Clear());

return cmd;
}
public static void EndOfFrame()
public static void Release(CommandBuffer buffer)
m_BufferPool.ReleaseAll();
m_BufferPool.Release(buffer);
}
}
}
正在加载...
取消
保存