浏览代码

Lw/fix custom renderer lifetime 1061353 (#1814)

*  Fixed custom renderer lifetime issue. Materials are not cached anymore in the passes, instead the renderer is passed in execution and resources are accessed from it.

*  updated release notes
/main
GitHub 6 年前
当前提交
633c0bcc
共有 24 个文件被更改,包括 63 次插入87 次删除
  1. 6
      TestProjects/LWGraphicsTest/Assets/Scenes/045_CustomLWPipe/CustomLWPipe.cs
  2. 1
      com.unity.render-pipelines.lightweight/CHANGELOG.md
  3. 18
      com.unity.render-pipelines.lightweight/LWRP/DefaultRendererSetup.cs
  4. 4
      com.unity.render-pipelines.lightweight/LWRP/LightweightForwardRenderer.cs
  5. 3
      com.unity.render-pipelines.lightweight/LWRP/Passes/BeginXRRenderingPass.cs
  6. 10
      com.unity.render-pipelines.lightweight/LWRP/Passes/CopyColorPass.cs
  7. 12
      com.unity.render-pipelines.lightweight/LWRP/Passes/CopyDepthPass.cs
  8. 2
      com.unity.render-pipelines.lightweight/LWRP/Passes/CreateLightweightRenderTexturesPass.cs
  9. 2
      com.unity.render-pipelines.lightweight/LWRP/Passes/DepthOnlyPass.cs
  10. 2
      com.unity.render-pipelines.lightweight/LWRP/Passes/DirectionalShadowsPass.cs
  11. 2
      com.unity.render-pipelines.lightweight/LWRP/Passes/DrawSkyboxPass.cs
  12. 2
      com.unity.render-pipelines.lightweight/LWRP/Passes/EndXRRenderingPass.cs
  13. 16
      com.unity.render-pipelines.lightweight/LWRP/Passes/FinalBlitPass.cs
  14. 11
      com.unity.render-pipelines.lightweight/LWRP/Passes/LightweightForwardPass.cs
  15. 4
      com.unity.render-pipelines.lightweight/LWRP/Passes/LocalShadowsPass.cs
  16. 3
      com.unity.render-pipelines.lightweight/LWRP/Passes/OpaquePostProcessPass.cs
  17. 9
      com.unity.render-pipelines.lightweight/LWRP/Passes/RenderOpaqueForwardPass.cs
  18. 7
      com.unity.render-pipelines.lightweight/LWRP/Passes/RenderTransparentForwardPass.cs
  19. 16
      com.unity.render-pipelines.lightweight/LWRP/Passes/SceneViewDepthCopy.cs
  20. 11
      com.unity.render-pipelines.lightweight/LWRP/Passes/ScreenSpaceShadowResolvePass.cs
  21. 2
      com.unity.render-pipelines.lightweight/LWRP/Passes/ScriptableRenderPass.cs
  22. 3
      com.unity.render-pipelines.lightweight/LWRP/Passes/SetupForwardRenderingPass.cs
  23. 2
      com.unity.render-pipelines.lightweight/LWRP/Passes/SetupLightweightConstanstPass.cs
  24. 2
      com.unity.render-pipelines.lightweight/LWRP/Passes/TransparentPostProcessPass.cs

6
TestProjects/LWGraphicsTest/Assets/Scenes/045_CustomLWPipe/CustomLWPipe.cs


[NonSerialized]
private bool m_Initialized = false;
private void Init(LightweightForwardRenderer renderer)
private void Init()
{
if (m_Initialized)
return;

m_SetupLightweightConstants = new SetupLightweightConstanstPass();
m_RenderOpaqueForwardPass = new RenderOpaqueForwardPass(renderer.GetMaterial(MaterialHandles.Error));
m_RenderOpaqueForwardPass = new RenderOpaqueForwardPass();
m_Initialized = true;
}

{
Init(renderer);
Init();
renderer.Clear();

1
com.unity.render-pipelines.lightweight/CHANGELOG.md


## [3.2.0-preview]
### Fixed
- Scriptable passes no longer have missing material references. Now they access cached materials in the renderer.(case 1061353)
- When you change a Shadow Cascade option in the Pipeline Asset, this no longer warns you that you've exceeded the array size for the _WorldToShadow property.
## [3.1.0-preview]

18
com.unity.render-pipelines.lightweight/LWRP/DefaultRendererSetup.cs


[NonSerialized]
private bool m_Initialized = false;
private void Init(LightweightForwardRenderer renderer)
private void Init()
{
if (m_Initialized)
return;

m_LocalShadowPass = new LocalShadowsPass();
m_SetupForwardRenderingPass = new SetupForwardRenderingPass();
m_ScreenSpaceShadowResovePass = new ScreenSpaceShadowResolvePass(renderer.GetMaterial(MaterialHandles.ScrenSpaceShadow));
m_ScreenSpaceShadowResovePass = new ScreenSpaceShadowResolvePass();
m_RenderOpaqueForwardPass = new RenderOpaqueForwardPass(renderer.GetMaterial(MaterialHandles.Error));
m_RenderOpaqueForwardPass = new RenderOpaqueForwardPass();
m_CopyDepthPass = new CopyDepthPass(renderer.GetMaterial(MaterialHandles.DepthCopy));
m_CopyColorPass = new CopyColorPass(renderer.GetMaterial(MaterialHandles.Sampling));
m_RenderTransparentForwardPass = new RenderTransparentForwardPass(renderer.GetMaterial(MaterialHandles.Error));
m_CopyDepthPass = new CopyDepthPass();
m_CopyColorPass = new CopyColorPass();
m_RenderTransparentForwardPass = new RenderTransparentForwardPass();
m_FinalBlitPass = new FinalBlitPass(renderer.GetMaterial(MaterialHandles.Blit));
m_FinalBlitPass = new FinalBlitPass();
m_SceneViewDepthCopyPass = new SceneViewDepthCopyPass(renderer.GetMaterial(MaterialHandles.DepthCopy));
m_SceneViewDepthCopyPass = new SceneViewDepthCopyPass();
#endif
// RenderTexture format depends on camera and pipeline (HDR, non HDR, etc)

public void Setup(LightweightForwardRenderer renderer, ref ScriptableRenderContext context,
ref CullResults cullResults, ref RenderingData renderingData)
{
Init(renderer);
Init();
renderer.Clear();

4
com.unity.render-pipelines.lightweight/LWRP/LightweightForwardRenderer.cs


using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine.Rendering;
using UnityEngine.Rendering.PostProcessing;

public void Execute(ref ScriptableRenderContext context, ref CullResults cullResults, ref RenderingData renderingData)
{
for (int i = 0; i < m_ActiveRenderPassQueue.Count; ++i)
m_ActiveRenderPassQueue[i].Execute(ref context, ref cullResults, ref renderingData);
m_ActiveRenderPassQueue[i].Execute(this, ref context, ref cullResults, ref renderingData);
public Material GetMaterial(MaterialHandles handle)
{
int handleID = (int)handle;

3
com.unity.render-pipelines.lightweight/LWRP/Passes/BeginXRRenderingPass.cs


{
public class BeginXRRenderingPass : ScriptableRenderPass
{
public override void Execute(ref ScriptableRenderContext context, ref CullResults cullResults,
public override void Execute(LightweightForwardRenderer renderer, ref ScriptableRenderContext context,
ref CullResults cullResults,
ref RenderingData renderingData)
{
Camera camera = renderingData.cameraData.camera;

10
com.unity.render-pipelines.lightweight/LWRP/Passes/CopyColorPass.cs


private RenderTargetHandle source { get; set; }
private RenderTargetHandle destination { get; set; }
private Material samplingMaterial { get; set; }
public CopyColorPass(Material samplingMaterial)
public CopyColorPass()
this.samplingMaterial = samplingMaterial;
m_SampleOffsetShaderHandle = Shader.PropertyToID("_SampleOffset");
}

this.destination = destination;
}
public override void Execute(ref ScriptableRenderContext context,
public override void Execute(LightweightForwardRenderer renderer, ref ScriptableRenderContext context,
ref CullResults cullResults,
ref RenderingData renderingData)
{

cmd.Blit(colorRT, opaqueColorRT);
break;
case Downsampling._4xBox:
Material samplingMaterial = renderer.GetMaterial(MaterialHandles.Sampling);
samplingMaterial.SetFloat(m_SampleOffsetShaderHandle, 2);
cmd.Blit(colorRT, opaqueColorRT, samplingMaterial, 0);
break;

}
}
}
}

12
com.unity.render-pipelines.lightweight/LWRP/Passes/CopyDepthPass.cs


{
private RenderTargetHandle source { get; set; }
private RenderTargetHandle destination { get; set; }
private Material depthCopyMaterial { get; set; }
public CopyDepthPass(Material depthCopyMaterial)
{
this.depthCopyMaterial = depthCopyMaterial;
}
const string k_DepthCopyTag = "Depth Copy";
public void Setup(RenderTargetHandle source, RenderTargetHandle destination)
{

public override void Execute(ref ScriptableRenderContext context,
public override void Execute(LightweightForwardRenderer renderer, ref ScriptableRenderContext context,
CommandBuffer cmd = CommandBufferPool.Get("Depth Copy");
CommandBuffer cmd = CommandBufferPool.Get(k_DepthCopyTag);
Material depthCopyMaterial = renderer.GetMaterial(MaterialHandles.DepthCopy);
RenderTextureDescriptor descriptor = LightweightForwardRenderer.CreateRTDesc(ref renderingData.cameraData);
descriptor.colorFormat = RenderTextureFormat.Depth;

2
com.unity.render-pipelines.lightweight/LWRP/Passes/CreateLightweightRenderTexturesPass.cs


descriptor = baseDescriptor;
}
public override void Execute(ref ScriptableRenderContext context,
public override void Execute(LightweightForwardRenderer renderer, ref ScriptableRenderContext context,
ref CullResults cullResults,
ref RenderingData renderingData)
{

2
com.unity.render-pipelines.lightweight/LWRP/Passes/DepthOnlyPass.cs


};
}
public override void Execute(ref ScriptableRenderContext context,
public override void Execute(LightweightForwardRenderer renderer, ref ScriptableRenderContext context,
ref CullResults cullResults,
ref RenderingData renderingData)
{

2
com.unity.render-pipelines.lightweight/LWRP/Passes/DirectionalShadowsPass.cs


this.destination = destination;
}
public override void Execute(ref ScriptableRenderContext context,
public override void Execute(LightweightForwardRenderer renderer, ref ScriptableRenderContext context,
ref CullResults cullResults,
ref RenderingData renderingData)
{

2
com.unity.render-pipelines.lightweight/LWRP/Passes/DrawSkyboxPass.cs


{
public class DrawSkyboxPass : ScriptableRenderPass
{
public override void Execute(ref ScriptableRenderContext context,
public override void Execute(LightweightForwardRenderer renderer, ref ScriptableRenderContext context,
ref CullResults cullResults,
ref RenderingData renderingData)
{

2
com.unity.render-pipelines.lightweight/LWRP/Passes/EndXRRenderingPass.cs


{
public class EndXRRenderingPass : ScriptableRenderPass
{
public override void Execute(ref ScriptableRenderContext context,
public override void Execute(LightweightForwardRenderer renderer, ref ScriptableRenderContext context,
ref CullResults cullResults,
ref RenderingData renderingData)
{

16
com.unity.render-pipelines.lightweight/LWRP/Passes/FinalBlitPass.cs


{
public class FinalBlitPass : ScriptableRenderPass
{
const string k_FinalBlitTag = "Final Blit Pass";
private Material blitMaterial { get; set; }
public FinalBlitPass(Material blitMaterial)
{
this.blitMaterial = blitMaterial;
}
public void Setup(RenderTextureDescriptor baseDescriptor, RenderTargetHandle colorAttachmentHandle)
{
this.colorAttachmentHandle = colorAttachmentHandle;

public override void Execute(ref ScriptableRenderContext context,
public override void Execute(LightweightForwardRenderer renderer, ref ScriptableRenderContext context,
Material material = renderingData.cameraData.isStereoEnabled ? null : blitMaterial;
Material material = renderingData.cameraData.isStereoEnabled ? null : renderer.GetMaterial(MaterialHandles.Blit);
CommandBuffer cmd = CommandBufferPool.Get("Final Blit Pass");
CommandBuffer cmd = CommandBufferPool.Get(k_FinalBlitTag);
cmd.SetGlobalTexture("_BlitTex", sourceRT);
// We need to handle viewport on a RT. We do it by rendering a fullscreen quad + viewport

11
com.unity.render-pipelines.lightweight/LWRP/Passes/LightweightForwardPass.cs


private RenderTargetHandle colorAttachmentHandle { get; set; }
private RenderTargetHandle depthAttachmentHandle { get; set; }
private RenderTextureDescriptor descriptor { get; set; }
private Material errorMaterial { get; set; }
const string k_SwitchRTs = "Switch RT";
protected LightweightForwardPass(Material errorMaterial)
protected LightweightForwardPass()
{
m_LegacyShaderPassNames = new List<ShaderPassName>();
m_LegacyShaderPassNames.Add(new ShaderPassName("Always"));

m_LegacyShaderPassNames.Add(new ShaderPassName("VertexLMRGBM"));
m_LegacyShaderPassNames.Add(new ShaderPassName("VertexLM"));
this.errorMaterial = errorMaterial;
RegisterShaderPassName("LightweightForward");
RegisterShaderPassName("SRPDefaultUnlit");

}
[Conditional("DEVELOPMENT_BUILD"), Conditional("UNITY_EDITOR")]
protected void RenderObjectsWithError(ref ScriptableRenderContext context, ref CullResults cullResults, Camera camera, FilterRenderersSettings filterSettings, SortFlags sortFlags)
protected void RenderObjectsWithError(LightweightForwardRenderer renderer, ref ScriptableRenderContext context, ref CullResults cullResults, Camera camera, FilterRenderersSettings filterSettings, SortFlags sortFlags)
Material errorMaterial = renderer.GetMaterial(MaterialHandles.Error);
if (errorMaterial != null)
{
DrawRendererSettings errorSettings = new DrawRendererSettings(camera, m_LegacyShaderPassNames[0]);

4
com.unity.render-pipelines.lightweight/LWRP/Passes/LocalShadowsPass.cs


using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine.Rendering;
namespace UnityEngine.Experimental.Rendering.LightweightPipeline

}
}
public override void Execute(ref ScriptableRenderContext context, ref CullResults cullResults,
public override void Execute(LightweightForwardRenderer renderer, ref ScriptableRenderContext context,
ref CullResults cullResults,
ref RenderingData renderingData)
{
if (renderingData.shadowData.renderLocalShadows)

3
com.unity.render-pipelines.lightweight/LWRP/Passes/OpaquePostProcessPass.cs


descriptor = baseDescriptor;
}
public override void Execute(ref ScriptableRenderContext context, ref CullResults cullResults,
public override void Execute(LightweightForwardRenderer renderer, ref ScriptableRenderContext context,
ref CullResults cullResults,
ref RenderingData renderingData)
{
CommandBuffer cmd = CommandBufferPool.Get("Render Opaque PostProcess Effects");

9
com.unity.render-pipelines.lightweight/LWRP/Passes/RenderOpaqueForwardPass.cs


const string k_RenderOpaquesTag = "Render Opaques";
public FilterRenderersSettings opaqueFilterSettings { get; private set; }
public RenderOpaqueForwardPass(Material errorMaterial) : base(errorMaterial)
public RenderOpaqueForwardPass()
{
opaqueFilterSettings = new FilterRenderersSettings(true)
{

public override void Execute(ref ScriptableRenderContext context,
public override void Execute(LightweightForwardRenderer renderer, ref ScriptableRenderContext context,
ref CullResults cullResults,
ref RenderingData renderingData)
{

SetRenderTarget(cmd, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store, clearFlag, CoreUtils.ConvertSRGBToActiveColorSpace(clearColor));
// TODO: We need a proper way to handle multiple camera/ camera stack. Issue is: multiple cameras can share a same RT

// cmd.SetViewport(camera.pixelRect);
Camera camera = renderingData.cameraData.camera;
var drawSettings = CreateDrawRendererSettings(camera, SortFlags.CommonOpaque, rendererConfiguration, dynamicBatching);

RenderObjectsWithError(ref context, ref cullResults, camera, opaqueFilterSettings, SortFlags.None);
RenderObjectsWithError(renderer, ref context, ref cullResults, camera, opaqueFilterSettings, SortFlags.None);
}
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);

7
com.unity.render-pipelines.lightweight/LWRP/Passes/RenderTransparentForwardPass.cs


private FilterRenderersSettings transparentFilterSettings { get; set; }
public RenderTransparentForwardPass(Material errorMaterial) : base(errorMaterial)
public RenderTransparentForwardPass()
public override void Execute(ref ScriptableRenderContext context,
public override void Execute(LightweightForwardRenderer renderer, ref ScriptableRenderContext context,
ref CullResults cullResults,
ref RenderingData renderingData)
{

context.DrawRenderers(cullResults.visibleRenderers, ref drawSettings, transparentFilterSettings);
// Render objects that did not match any shader pass with error shader
RenderObjectsWithError(ref context, ref cullResults, camera, transparentFilterSettings, SortFlags.None);
RenderObjectsWithError(renderer, ref context, ref cullResults, camera, transparentFilterSettings, SortFlags.None);
}
context.ExecuteCommandBuffer(cmd);

16
com.unity.render-pipelines.lightweight/LWRP/Passes/SceneViewDepthCopy.cs


{
public class SceneViewDepthCopyPass : ScriptableRenderPass
{
private RenderTargetHandle source { get; set; }
private Material depthCopyMaterial { get; set; }
const string k_CopyDepthToCameraTag = "Copy Depth to Camera";
public SceneViewDepthCopyPass(Material depthCopyMaterial)
{
this.depthCopyMaterial = depthCopyMaterial;
}
private RenderTargetHandle source { get; set; }
public void Setup(RenderTargetHandle source)
{

public override void Execute(ref ScriptableRenderContext context,
public override void Execute(LightweightForwardRenderer renderer, ref ScriptableRenderContext context,
CommandBuffer cmd = CommandBufferPool.Get("Copy Depth to Camera");
CommandBuffer cmd = CommandBufferPool.Get(k_CopyDepthToCameraTag);
cmd.Blit(source.Identifier(), BuiltinRenderTextureType.CameraTarget, depthCopyMaterial);
cmd.Blit(source.Identifier(), BuiltinRenderTextureType.CameraTarget, renderer.GetMaterial(MaterialHandles.DepthCopy));
}
}

11
com.unity.render-pipelines.lightweight/LWRP/Passes/ScreenSpaceShadowResolvePass.cs


public class ScreenSpaceShadowResolvePass : ScriptableRenderPass
{
RenderTextureFormat m_ColorFormat;
private Material screenSpaceShadowsMaterial { get; set; }
public ScreenSpaceShadowResolvePass(Material screenSpaceShadowsMaterial)
public ScreenSpaceShadowResolvePass()
this.screenSpaceShadowsMaterial = screenSpaceShadowsMaterial;
}
private RenderTargetHandle colorAttachmentHandle { get; set; }

descriptor = baseDescriptor;
}
public override void Execute(ref ScriptableRenderContext context, ref CullResults cullResults,
public override void Execute(LightweightForwardRenderer renderer, ref ScriptableRenderContext context,
ref CullResults cullResults,
ref RenderingData renderingData)
{
if (renderingData.shadowData.renderedDirectionalShadowQuality == LightShadows.None)

RenderTargetIdentifier screenSpaceOcclusionTexture = colorAttachmentHandle.Identifier();
SetRenderTarget(cmd, screenSpaceOcclusionTexture, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store,
ClearFlag.Color | ClearFlag.Depth, Color.white, descriptor.dimension);
cmd.Blit(screenSpaceOcclusionTexture, screenSpaceOcclusionTexture, screenSpaceShadowsMaterial);
cmd.Blit(screenSpaceOcclusionTexture, screenSpaceOcclusionTexture, renderer.GetMaterial(MaterialHandles.ScrenSpaceShadow));
if (renderingData.cameraData.isStereoEnabled)
{

2
com.unity.render-pipelines.lightweight/LWRP/Passes/ScriptableRenderPass.cs


public virtual void FrameCleanup(CommandBuffer cmd)
{}
public abstract void Execute(ref ScriptableRenderContext context,
public abstract void Execute(LightweightForwardRenderer renderer, ref ScriptableRenderContext context,
ref CullResults cullResults,
ref RenderingData renderingData);

3
com.unity.render-pipelines.lightweight/LWRP/Passes/SetupForwardRenderingPass.cs


{
public class SetupForwardRenderingPass : ScriptableRenderPass
{
public override void Execute(ref ScriptableRenderContext context, ref CullResults cullResults,
public override void Execute(LightweightForwardRenderer renderer, ref ScriptableRenderContext context,
ref CullResults cullResults,
ref RenderingData renderingData)
{
// SetupCameraProperties does the following:

2
com.unity.render-pipelines.lightweight/LWRP/Passes/SetupLightweightConstanstPass.cs


CoreUtils.SetKeyword(cmd, "SOFTPARTICLES_ON", cameraData.requiresSoftParticles);
}
public override void Execute(ref ScriptableRenderContext context,
public override void Execute(LightweightForwardRenderer renderer, ref ScriptableRenderContext context,
ref CullResults cullResults,
ref RenderingData renderingData)
{

2
com.unity.render-pipelines.lightweight/LWRP/Passes/TransparentPostProcessPass.cs


descriptor = baseDescriptor;
}
public override void Execute(ref ScriptableRenderContext context,
public override void Execute(LightweightForwardRenderer renderer, ref ScriptableRenderContext context,
ref CullResults cullResults,
ref RenderingData renderingData)
{

正在加载...
取消
保存