浏览代码

Added Forward renderer. Changes ScriptableRenderPass interface.

/main
Felipe Lira 7 年前
当前提交
9a5ecd7b
共有 6 个文件被更改,包括 172 次插入68 次删除
  1. 64
      ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipeline.cs
  2. 36
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Passes/DepthOnlyPass.cs
  3. 26
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Passes/ScriptableRenderPass.cs
  4. 42
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Passes/ShadowPass.cs
  5. 61
      ScriptableRenderPipeline/LightweightPipeline/LWRP/ForwardRenderer.cs
  6. 11
      ScriptableRenderPipeline/LightweightPipeline/LWRP/ForwardRenderer.cs.meta

64
ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipeline.cs


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
#if UNITY_EDITOR
using UnityEditor.Experimental.Rendering.LightweightPipeline;
#endif

public List<int> localLightIndices;
}
public struct CameraData
{
public Camera camera;
public bool isSceneViewCamera;
public bool stereoEnabled;
public bool shadowsEnabled;
}
public enum MixedLightingSetup
{
None = 0,

private readonly LightweightPipelineAsset m_Asset;
DepthOnlyPass m_DepthOnlyPass;
private ShadowPass m_ShadowPass;
ShadowPass m_ShadowPass;
// Maximum amount of visible lights the shader can process. This controls the constant global light buffer size.
// It must match the MAX_VISIBLE_LIGHTS in LightweightInput.cginc

private CopyTextureSupport m_CopyTextureSupport;
ForwardRenderer m_Renderer;
public LightweightPipeline(LightweightPipelineAsset asset)
{
m_Asset = asset;

m_MaxLocalLightsShadedPerPass = m_UseComputeBuffer ? kMaxVisibleLocalLights : kMaxNonIndexedLocalLights;
m_LocalLightIndices = new List<int>(m_MaxLocalLightsShadedPerPass);
m_DepthOnlyPass = new DepthOnlyPass(null, RenderTextureFormat.Depth);
RenderTextureFormat shadowmapFormat =
(SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.Shadowmap))
? RenderTextureFormat.Shadowmap
: RenderTextureFormat.Depth;
m_ShadowPass = new ShadowPass(m_Asset, kMaxVisibleLocalLights, null, shadowmapFormat);
m_Renderer = new ForwardRenderer();
m_DepthOnlyPass = new DepthOnlyPass(m_Renderer, null, new[] {RenderTargetHandle.Depth});
m_ShadowPass = new ShadowPass(m_Renderer, null,
new[] {RenderTargetHandle.DirectionalShadowmap, RenderTargetHandle.LocalShadowmap}, m_Asset,
kMaxVisibleLocalLights);
// Let engine know we have MSAA on for cases where we support MSAA backbuffer
if (QualitySettings.antiAliasing != m_Asset.MSAASampleCount)

public override void Render(ScriptableRenderContext context, Camera[] cameras)
{
base.Render(context, cameras);
RenderPipeline.BeginFrameRendering(cameras);
BeginFrameRendering(cameras);
GraphicsSettings.lightsUseLinearIntensity = true;
SetupPerFrameShaderConstants();

CameraData cameraData;
InitializeCameraData(camera, out cameraData);
m_CurrCamera = camera;
bool sceneViewCamera = m_CurrCamera.cameraType == CameraType.SceneView;
bool stereoEnabled = IsStereoEnabled(m_CurrCamera);

m_IsOffscreenCamera = m_CurrCamera.targetTexture != null && m_CurrCamera.cameraType != CameraType.SceneView;
SetupPerCameraShaderConstants();
RenderPipeline.BeginCameraRendering(m_CurrCamera);
BeginCameraRendering(m_CurrCamera);
cullingParameters.shadowDistance = Mathf.Min(m_ShadowPass.RenderingDistance, m_CurrCamera.farClipPlane);
cullingParameters.shadowDistance = Mathf.Min(m_ShadowPass.renderingDistance, m_CurrCamera.farClipPlane);
#if UNITY_EDITOR
// Emit scene view UI

LightData lightData;
InitializeLightData(visibleLights, out lightData);
m_ShadowPass.Execute(ref context, ref m_CullResults, ref lightData, camera, false);
PassData passData;
passData.lightData = lightData;
passData.cameraData = cameraData;
m_ShadowPass.Execute(ref context, ref m_CullResults, ref passData);
bool screenspaceShadows = m_ShadowPass.requireScreenSpaceResolve;
FrameRenderingConfiguration frameRenderingConfiguration;

context.SetupCameraProperties(m_CurrCamera, stereoEnabled);
if (CoreUtils.HasFlag(frameRenderingConfiguration, FrameRenderingConfiguration.DepthPrePass))
m_DepthOnlyPass.Execute(ref context, ref m_CullResults, ref lightData, camera, stereoEnabled);
m_DepthOnlyPass.Execute(ref context, ref m_CullResults, ref passData);
if (screenspaceShadows)
m_ShadowPass.CollectShadows(m_CurrCamera, frameRenderingConfiguration, ref context);

{
cmd = CommandBufferPool.Get("Copy Depth to Camera");
cmd.DisableShaderKeyword(kMSAADepthKeyword);
CopyTexture(cmd, m_DepthOnlyPass.depthTextureID, BuiltinRenderTextureType.CameraTarget, m_CopyDepthMaterial, true);
CopyTexture(cmd, RenderTargetHandle.Depth, BuiltinRenderTextureType.CameraTarget, m_CopyDepthMaterial, true);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}

CopyTexture(cmd, m_DepthRT, m_CopyDepth, m_CopyDepthMaterial, forceBlit);
depthRT = m_CopyDepth;
setRenderTarget = true;
cmd.SetGlobalTexture(m_DepthOnlyPass.depthTextureID, m_CopyDepth);
cmd.SetGlobalTexture(RenderTargetHandle.Depth, m_CopyDepth);
}
if (setRenderTarget)

CommandBufferPool.Release(cmd);
}
void InitializeCameraData(Camera camera, out CameraData cameraData)
{
cameraData.camera = camera;
cameraData.isSceneViewCamera = camera.cameraType == CameraType.SceneView;
cameraData.stereoEnabled = IsStereoEnabled(camera);
cameraData.shadowsEnabled = true;
}
private void InitializeLightData(List<VisibleLight> visibleLights, out LightData lightData)
{
m_LocalLightIndices.Clear();

// TODO: We have to discuss cookie approach on LWRP.
// CoreUtils.SetKeyword(cmd, LightweightKeywords.MainLightCookieText, mainLightIndex != -1 && LightweightUtils.IsSupportedCookieType(visibleLights[mainLightIndex].lightType) && visibleLights[mainLightIndex].light.cookie != null);
bool anyShadowsEnabled = m_ShadowPass.IsDirectionalShadowsEnabled || m_ShadowPass.IsLocalShadowsEnabled;
CoreUtils.SetKeyword(cmd, LightweightKeywords.DirectionalShadowsText, m_ShadowPass.DirectionalShadowsRendered);
CoreUtils.SetKeyword(cmd, LightweightKeywords.LocalShadowsText, m_ShadowPass.LocalShadowsRendered);
CoreUtils.SetKeyword(cmd, LightweightKeywords.SoftShadowsText, m_ShadowPass.IsSoftShadowsEnabled && anyShadowsEnabled);
bool anyShadowsEnabled = m_ShadowPass.isDirectionalShadowsEnabled || m_ShadowPass.isLocalShadowsEnabled;
CoreUtils.SetKeyword(cmd, LightweightKeywords.DirectionalShadowsText, m_ShadowPass.directionalShadowsRendered);
CoreUtils.SetKeyword(cmd, LightweightKeywords.LocalShadowsText, m_ShadowPass.localShadowsRendered);
CoreUtils.SetKeyword(cmd, LightweightKeywords.SoftShadowsText, m_ShadowPass.isSoftShadowsEnabled && anyShadowsEnabled);
// TODO: Remove this. legacy particles support will be removed from Unity in 2018.3. This should be a shader_feature instead with prop exposed in the Standard particles shader.
CoreUtils.SetKeyword(cmd, "SOFTPARTICLES_ON", m_RequireDepthTexture && m_Asset.RequireSoftParticles);

36
ScriptableRenderPipeline/LightweightPipeline/LWRP/Passes/DepthOnlyPass.cs


{
public class DepthOnlyPass : ScriptableRenderPass
{
public int depthTextureID { get; private set; }
public RenderTargetIdentifier depthTexture { get; private set; }
const string kProfilerTag = "Depth Prepass Setup";
const string kCommandBufferTag = "Depth Prepass";
int kDepthBufferBits = 32;

public DepthOnlyPass(RenderTextureFormat[] colorAttachments, RenderTextureFormat depthAttachment) :
base(colorAttachments, depthAttachment)
public DepthOnlyPass(ForwardRenderer renderer, int[] inputs, int[] targets) :
base(renderer, inputs, targets)
depthTextureID = Shader.PropertyToID("_CameraDepthTexture");
depthTexture = new RenderTargetIdentifier(depthTextureID);
m_Disposed = true;
m_Disposed = true;
attachmentDescriptor.colorFormat = depthAttachment;
attachmentDescriptor.colorFormat = RenderTextureFormat.Depth;
attachmentDescriptor.depthBufferBits = kDepthBufferBits;
if (samples > 1)

}
cmd.GetTemporaryRT(depthTextureID, attachmentDescriptor, FilterMode.Point);
cmd.GetTemporaryRT(targetHandles[0], attachmentDescriptor, FilterMode.Point);
public override void Execute(ref ScriptableRenderContext context, ref CullResults cullResults, ref LightData lightData,
Camera camera, bool stereoRendering)
public override void Execute(ref ScriptableRenderContext context, ref CullResults cullResults, ref PassData passData)
CameraData cameraData = passData.cameraData;
int depthSlice = LightweightPipeline.GetRenderTargetDepthSlice(stereoRendering);
CoreUtils.SetRenderTarget(cmd, depthTexture, ClearFlag.Depth, 0, CubemapFace.Unknown, depthSlice);
int depthSlice = LightweightPipeline.GetRenderTargetDepthSlice(cameraData.stereoEnabled);
CoreUtils.SetRenderTarget(cmd, attachments[0], ClearFlag.Depth, 0, CubemapFace.Unknown, depthSlice);
var opaqueDrawSettings = new DrawRendererSettings(camera, m_ShaderPassNames[0]);
var opaqueDrawSettings = new DrawRendererSettings(cameraData.camera, m_ShaderPassNames[0]);
if (stereoRendering)
if (cameraData.stereoEnabled)
context.StartMultiEye(camera);
context.StartMultiEye(cameraData.camera);
context.StopMultiEye(camera);
context.StopMultiEye(cameraData.camera);
cmd.SetGlobalTexture(depthTextureID, depthTexture);
//cmd.SetGlobalTexture(depthTextureID, depthTexture);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}

if (!m_Disposed)
cmd.ReleaseTemporaryRT(depthTextureID);
cmd.ReleaseTemporaryRT(targetHandles[0]);
}
}
}

26
ScriptableRenderPipeline/LightweightPipeline/LWRP/Passes/ScriptableRenderPass.cs


namespace UnityEngine.Experimental.Rendering.LightweightPipeline
{
public struct PassData
{
public LightData lightData;
public CameraData cameraData;
}
public ScriptableRenderPass(RenderTextureFormat[] colorAttachments, RenderTextureFormat depthAttachment)
public ScriptableRenderPass(ForwardRenderer renderer, int[] inputs, int[] targets)
this.colorAttachments = colorAttachments;
this.depthAttachment = depthAttachment;
inputHandles = inputs;
targetHandles = targets;
attachments = new RenderTargetIdentifier[targets.Length];
for (int i = 0; i < targets.Length; ++i)
attachments[i] = renderer.GetSurface(targets[i]);
public abstract void Execute(ref ScriptableRenderContext context, ref CullResults cullResults, ref LightData lightData,
Camera camera, bool stereoRendering);
public abstract void Execute(ref ScriptableRenderContext context, ref CullResults cullResults, ref PassData passData);
public RenderTextureFormat[] colorAttachments { get; set; }
public RenderTextureFormat depthAttachment { get; set; }
public int[] inputHandles { get; private set; }
public int[] targetHandles { get; private set; }
public RenderTargetIdentifier[] attachments { get; private set; }
protected List<ShaderPassName> m_ShaderPassNames = new List<ShaderPassName>();
public void RegisterShaderPassName(string passName)

42
ScriptableRenderPipeline/LightweightPipeline/LWRP/Passes/ShadowPass.cs


public class ShadowPass : ScriptableRenderPass
{
public bool IsDirectionalShadowsEnabled { get { return m_ShadowSettings.supportsDirectionalShadows; } }
public bool IsLocalShadowsEnabled { get { return m_ShadowSettings.supportsLocalShadows; } }
public bool RequireScreenSpaceShadowmap { get { return IsDirectionalShadowsEnabled && m_ShadowSettings.screenSpace; } }
public bool DirectionalShadowsRendered { get { return m_DirectionalShadowmapQuality != LightShadows.None; } }
public bool LocalShadowsRendered { get { return m_LocalShadowmapQuality != LightShadows.None; } }
public bool IsSoftShadowsEnabled { get { return m_ShadowSettings.supportsSoftShadows; } }
public bool isDirectionalShadowsEnabled { get { return m_ShadowSettings.supportsDirectionalShadows; } }
public bool isLocalShadowsEnabled { get { return m_ShadowSettings.supportsLocalShadows; } }
public bool requireScreenSpaceShadowmap { get { return isDirectionalShadowsEnabled && m_ShadowSettings.screenSpace; } }
public bool directionalShadowsRendered { get { return m_DirectionalShadowmapQuality != LightShadows.None; } }
public bool localShadowsRendered { get { return m_LocalShadowmapQuality != LightShadows.None; } }
public bool isSoftShadowsEnabled { get { return m_ShadowSettings.supportsSoftShadows; } }
public float RenderingDistance { get { return m_ShadowSettings.maxShadowDistance; } }
public float renderingDistance { get { return m_ShadowSettings.maxShadowDistance; } }
private const int kMaxCascades = 4;

private ShadowSliceData[] m_LocalLightSlices;
private float[] m_LocalShadowStrength;
public ShadowPass(LightweightPipelineAsset pipelineAsset, int maxLocalLightsCount,
RenderTextureFormat[] colorAttachments, RenderTextureFormat depthAttachment) :
base(colorAttachments, depthAttachment)
public ShadowPass(ForwardRenderer renderer, int[] inputs, int[] targets,
LightweightPipelineAsset pipelineAsset, int maxLocalLightsCount) :
base(renderer, inputs, targets)
{
BuildShadowSettings(pipelineAsset);
RegisterShaderPassName("ShadowCaster");

public override void BindSurface(CommandBuffer cmd, RenderTextureDescriptor attachmentDescriptor, int samples)
{
if (RequireScreenSpaceShadowmap)
if (requireScreenSpaceShadowmap)
{
attachmentDescriptor.depthBufferBits = 0;
attachmentDescriptor.colorFormat = m_ShadowSettings.screenspaceShadowmapTextureFormat;

public override void Execute(ref ScriptableRenderContext context, ref CullResults cullResults, ref LightData lightData,
Camera camera, bool stereoRendering)
public override void Execute(ref ScriptableRenderContext context, ref CullResults cullResults, ref PassData passData)
if (IsDirectionalShadowsEnabled)
directionalShadowmapRendered = RenderDirectionalCascadeShadowmap(ref cullResults, ref lightData, ref context);
if (isDirectionalShadowsEnabled)
directionalShadowmapRendered = RenderDirectionalCascadeShadowmap(ref context, ref cullResults, ref passData.lightData);
if (IsLocalShadowsEnabled)
RenderLocalShadowmapAtlas(ref cullResults, ref lightData, ref context);
if (isLocalShadowsEnabled)
RenderLocalShadowmapAtlas(ref context, ref cullResults, ref passData.lightData);
requireScreenSpaceResolve = directionalShadowmapRendered && m_ShadowSettings.screenSpace;
}

CoreUtils.SetKeyword(cmd, LightweightKeywords.CascadeShadowsText, m_ShadowSettings.directionalLightCascadeCount > 1);
}
private bool RenderDirectionalCascadeShadowmap(ref CullResults cullResults, ref LightData lightData, ref ScriptableRenderContext context)
private bool RenderDirectionalCascadeShadowmap(ref ScriptableRenderContext context, ref CullResults cullResults, ref LightData lightData)
{
int shadowLightIndex = lightData.mainLightIndex;
if (shadowLightIndex == -1)

if (success)
{
m_DirectionalShadowmapQuality = (IsSoftShadowsEnabled) ? light.shadows : LightShadows.Hard;
m_DirectionalShadowmapQuality = (isSoftShadowsEnabled) ? light.shadows : LightShadows.Hard;
// In order to avoid shader variants explosion we only do hard shadows when sampling shadowmap in the lit pass.
// GLES2 platform is forced to hard single cascade shadows.

return success;
}
private void RenderLocalShadowmapAtlas(ref CullResults cullResults, ref LightData lightData, ref ScriptableRenderContext context)
private void RenderLocalShadowmapAtlas(ref ScriptableRenderContext context, ref CullResults cullResults, ref LightData lightData)
{
List<int> localLightIndices = lightData.localLightIndices;
List<VisibleLight> visibleLights = lightData.visibleLights;

SetupLocalLightsShadowReceiverConstants(cmd, ref context);
m_LocalShadowmapQuality = (IsSoftShadowsEnabled) ? (LightShadows)shadowSampling : LightShadows.Hard;
m_LocalShadowmapQuality = (isSoftShadowsEnabled) ? (LightShadows)shadowSampling : LightShadows.Hard;
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}

61
ScriptableRenderPipeline/LightweightPipeline/LWRP/ForwardRenderer.cs


using System.Collections.Generic;
using UnityEngine.Rendering;
namespace UnityEngine.Experimental.Rendering.LightweightPipeline
{
public static class RenderTargetHandle
{
public static int Color;
public static int Depth;
public static int DepthMS;
public static int OpaqueColor;
public static int DirectionalShadowmap;
public static int LocalShadowmap;
public static int ScreenSpaceOcclusion;
}
public class ForwardRenderer
{
private Dictionary<int, RenderTargetIdentifier> m_ResourceMap = new Dictionary<int, RenderTargetIdentifier>();
private List<ScriptableRenderPass> m_Passes = new List<ScriptableRenderPass>();
private List<ScriptableRenderPass> m_Graph = new List<ScriptableRenderPass>();
public ForwardRenderer()
{
// RenderTexture format depends on camera and pipeline (HDR, non HDR, etc)
// Samples (MSAA) depend on camera and pipeline
RenderTargetHandle.Color = Shader.PropertyToID("_CameraColorTexture");
RenderTargetHandle.Depth = Shader.PropertyToID("_CameraDepthTexture");
RenderTargetHandle.DepthMS = Shader.PropertyToID("_CameraDepthMSTexture");
RenderTargetHandle.OpaqueColor = Shader.PropertyToID("_CameraOpaqueTexture");
RenderTargetHandle.DirectionalShadowmap = Shader.PropertyToID("_DirectionalShadowmapTexture");
RenderTargetHandle.LocalShadowmap = Shader.PropertyToID("_LocalShadowmapTexture");
RenderTargetHandle.ScreenSpaceOcclusion = Shader.PropertyToID("_ScreenSpaceShadowMapTexture");
m_ResourceMap.Add(RenderTargetHandle.Color, new RenderTargetIdentifier(RenderTargetHandle.Color));
m_ResourceMap.Add(RenderTargetHandle.Depth, new RenderTargetIdentifier(RenderTargetHandle.Depth));
m_ResourceMap.Add(RenderTargetHandle.DepthMS, new RenderTargetIdentifier(RenderTargetHandle.DepthMS));
m_ResourceMap.Add(RenderTargetHandle.OpaqueColor, new RenderTargetIdentifier(RenderTargetHandle.OpaqueColor));
m_ResourceMap.Add(RenderTargetHandle.DirectionalShadowmap, new RenderTargetIdentifier(RenderTargetHandle.DirectionalShadowmap));
m_ResourceMap.Add(RenderTargetHandle.LocalShadowmap, new RenderTargetIdentifier(RenderTargetHandle.LocalShadowmap));
m_ResourceMap.Add(RenderTargetHandle.ScreenSpaceOcclusion, new RenderTargetIdentifier(RenderTargetHandle.ScreenSpaceOcclusion));
}
public void AddPass(ScriptableRenderPass pass)
{
m_Passes.Add(pass);
}
public List<ScriptableRenderPass> BuildRenderGraph()
{
m_Graph.Clear();
return m_Graph;
}
public RenderTargetIdentifier GetSurface(int handle)
{
return m_ResourceMap[handle];
}
}
}

11
ScriptableRenderPipeline/LightweightPipeline/LWRP/ForwardRenderer.cs.meta


fileFormatVersion: 2
guid: 83797c942458af1439f0f02f681ee8c9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存