199 lines
9.1 KiB
C#
199 lines
9.1 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine.Experimental.Rendering;
|
|
using UnityEngine.Experimental.Rendering.RenderGraphModule;
|
|
using UnityEngine.Rendering.Universal.Internal;
|
|
|
|
namespace UnityEngine.Rendering.Universal
|
|
{
|
|
internal class DecalDrawGBufferSystem : DecalDrawSystem
|
|
{
|
|
public DecalDrawGBufferSystem(DecalEntityManager entityManager) : base("DecalDrawGBufferSystem.Execute", entityManager) { }
|
|
protected override int GetPassIndex(DecalCachedChunk decalCachedChunk) => decalCachedChunk.passIndexGBuffer;
|
|
}
|
|
|
|
internal class DecalGBufferRenderPass : ScriptableRenderPass
|
|
{
|
|
private FilteringSettings m_FilteringSettings;
|
|
private ProfilingSampler m_ProfilingSampler;
|
|
private List<ShaderTagId> m_ShaderTagIdList;
|
|
private DecalDrawGBufferSystem m_DrawSystem;
|
|
private DecalScreenSpaceSettings m_Settings;
|
|
private DeferredLights m_DeferredLights;
|
|
private RTHandle[] m_GbufferAttachments;
|
|
private bool m_DecalLayers;
|
|
private PassData m_PassData;
|
|
|
|
public DecalGBufferRenderPass(DecalScreenSpaceSettings settings, DecalDrawGBufferSystem drawSystem, bool decalLayers)
|
|
{
|
|
renderPassEvent = RenderPassEvent.AfterRenderingGbuffer;
|
|
|
|
m_DrawSystem = drawSystem;
|
|
m_Settings = settings;
|
|
m_ProfilingSampler = new ProfilingSampler("Decal GBuffer Render");
|
|
m_FilteringSettings = new FilteringSettings(RenderQueueRange.opaque, -1);
|
|
m_DecalLayers = decalLayers;
|
|
|
|
m_ShaderTagIdList = new List<ShaderTagId>();
|
|
if (drawSystem == null)
|
|
m_ShaderTagIdList.Add(new ShaderTagId(DecalShaderPassNames.DecalGBufferProjector));
|
|
else
|
|
m_ShaderTagIdList.Add(new ShaderTagId(DecalShaderPassNames.DecalGBufferMesh));
|
|
|
|
m_PassData = new PassData();
|
|
}
|
|
|
|
internal void Setup(DeferredLights deferredLights)
|
|
{
|
|
m_DeferredLights = deferredLights;
|
|
}
|
|
|
|
public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
|
|
{
|
|
if (m_DeferredLights.UseRenderPass)
|
|
{
|
|
m_GbufferAttachments = new RTHandle[]
|
|
{
|
|
m_DeferredLights.GbufferAttachments[0], m_DeferredLights.GbufferAttachments[1],
|
|
m_DeferredLights.GbufferAttachments[2], m_DeferredLights.GbufferAttachments[3]
|
|
};
|
|
|
|
if (m_DecalLayers)
|
|
{
|
|
var deferredInputAttachments = new RTHandle[]
|
|
{
|
|
m_DeferredLights.GbufferAttachments[m_DeferredLights.GbufferDepthIndex],
|
|
m_DeferredLights.GbufferAttachments[m_DeferredLights.GBufferRenderingLayers],
|
|
};
|
|
|
|
var deferredInputIsTransient = new bool[]
|
|
{
|
|
true, false, // TODO: Make rendering layers transient
|
|
};
|
|
|
|
ConfigureInputAttachments(deferredInputAttachments, deferredInputIsTransient);
|
|
}
|
|
else
|
|
{
|
|
var deferredInputAttachments = new RTHandle[]
|
|
{
|
|
m_DeferredLights.GbufferAttachments[m_DeferredLights.GbufferDepthIndex],
|
|
};
|
|
|
|
var deferredInputIsTransient = new bool[]
|
|
{
|
|
true,
|
|
};
|
|
|
|
ConfigureInputAttachments(deferredInputAttachments, deferredInputIsTransient);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
m_GbufferAttachments = new RTHandle[]
|
|
{
|
|
m_DeferredLights.GbufferAttachments[0], m_DeferredLights.GbufferAttachments[1],
|
|
m_DeferredLights.GbufferAttachments[2], m_DeferredLights.GbufferAttachments[3]
|
|
};
|
|
}
|
|
|
|
ConfigureTarget(m_GbufferAttachments, m_DeferredLights.DepthAttachmentHandle);
|
|
}
|
|
|
|
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
|
|
{
|
|
InitPassData(ref m_PassData);
|
|
|
|
SortingCriteria sortingCriteria = renderingData.cameraData.defaultOpaqueSortFlags;
|
|
DrawingSettings drawingSettings = RenderingUtils.CreateDrawingSettings(m_ShaderTagIdList, ref renderingData, sortingCriteria);
|
|
var param = new RendererListParams(renderingData.cullResults, drawingSettings, m_FilteringSettings);
|
|
var rendererList = context.CreateRendererList(ref param);
|
|
using (new ProfilingScope(renderingData.commandBuffer, m_ProfilingSampler))
|
|
{
|
|
ExecutePass(CommandBufferHelpers.GetRasterCommandBuffer(renderingData.commandBuffer), m_PassData, rendererList, ref renderingData);
|
|
}
|
|
}
|
|
|
|
private class PassData
|
|
{
|
|
internal DecalDrawGBufferSystem drawSystem;
|
|
internal DecalScreenSpaceSettings settings;
|
|
internal bool decalLayers;
|
|
|
|
internal RenderingData renderingData;
|
|
internal RendererListHandle rendererList;
|
|
}
|
|
|
|
private void InitPassData(ref PassData passData)
|
|
{
|
|
passData.drawSystem = m_DrawSystem;
|
|
passData.settings = m_Settings;
|
|
passData.decalLayers = m_DecalLayers;
|
|
}
|
|
|
|
private static void ExecutePass(RasterCommandBuffer cmd, PassData passData, RendererList rendererList, ref RenderingData renderingData)
|
|
{
|
|
NormalReconstruction.SetupProperties(cmd, renderingData.cameraData);
|
|
|
|
CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.DecalNormalBlendLow, passData.settings.normalBlend == DecalNormalBlend.Low);
|
|
CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.DecalNormalBlendMedium, passData.settings.normalBlend == DecalNormalBlend.Medium);
|
|
CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.DecalNormalBlendHigh, passData.settings.normalBlend == DecalNormalBlend.High);
|
|
|
|
CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.DecalLayers, passData.decalLayers);
|
|
|
|
passData.drawSystem?.Execute(cmd);
|
|
cmd.DrawRendererList(rendererList);
|
|
}
|
|
|
|
public override void RecordRenderGraph(RenderGraph renderGraph, FrameResources frameResources, ref RenderingData renderingData)
|
|
{
|
|
UniversalRenderer renderer = (UniversalRenderer)renderingData.cameraData.renderer;
|
|
|
|
TextureHandle cameraDepthTexture = frameResources.GetTexture(UniversalResource.CameraDepthTexture);
|
|
|
|
RenderGraphUtils.SetGlobalTexture(renderGraph, Shader.PropertyToID("_CameraDepthTexture"), cameraDepthTexture);
|
|
|
|
using (var builder = renderGraph.AddRasterRenderPass<PassData>("Decal GBuffer Pass", out var passData, m_ProfilingSampler))
|
|
{
|
|
InitPassData(ref passData);
|
|
passData.renderingData = renderingData;
|
|
|
|
builder.UseTextureFragment(frameResources.GetTexture(UniversalResource.GBuffer0), 0, IBaseRenderGraphBuilder.AccessFlags.Write);
|
|
builder.UseTextureFragment(frameResources.GetTexture(UniversalResource.GBuffer1), 1, IBaseRenderGraphBuilder.AccessFlags.Write);
|
|
builder.UseTextureFragment(frameResources.GetTexture(UniversalResource.GBuffer2), 2, IBaseRenderGraphBuilder.AccessFlags.Write);
|
|
builder.UseTextureFragment(frameResources.GetTexture(UniversalResource.GBuffer3), 3, IBaseRenderGraphBuilder.AccessFlags.Write);
|
|
builder.UseTextureFragmentDepth(renderer.activeDepthTexture, IBaseRenderGraphBuilder.AccessFlags.Read);
|
|
|
|
if (cameraDepthTexture.IsValid())
|
|
builder.UseTexture(cameraDepthTexture, IBaseRenderGraphBuilder.AccessFlags.Read);
|
|
|
|
|
|
SortingCriteria sortingCriteria = renderingData.cameraData.defaultOpaqueSortFlags;
|
|
DrawingSettings drawingSettings = RenderingUtils.CreateDrawingSettings(m_ShaderTagIdList, ref renderingData, sortingCriteria);
|
|
var param = new RendererListParams(renderingData.cullResults, drawingSettings, m_FilteringSettings);
|
|
passData.rendererList = renderGraph.CreateRendererList(param);
|
|
builder.UseRendererList(passData.rendererList);
|
|
|
|
builder.AllowGlobalStateModification(true);
|
|
|
|
builder.SetRenderFunc((PassData data, RasterGraphContext rgContext) =>
|
|
{
|
|
ExecutePass(rgContext.cmd, data, data.rendererList, ref data.renderingData);
|
|
});
|
|
}
|
|
}
|
|
|
|
public override void OnCameraCleanup(CommandBuffer cmd)
|
|
{
|
|
if (cmd == null)
|
|
{
|
|
throw new System.ArgumentNullException("cmd");
|
|
}
|
|
|
|
CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.DecalNormalBlendLow, false);
|
|
CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.DecalNormalBlendMedium, false);
|
|
CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.DecalNormalBlendHigh, false);
|
|
CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.DecalLayers, false);
|
|
}
|
|
}
|
|
}
|