132 lines
5.8 KiB
C#
132 lines
5.8 KiB
C#
//pipelinedefine
|
|
#define H_URP
|
|
|
|
using HTraceWSGI.Scripts.Data.Private;
|
|
using HTraceWSGI.Scripts.Extensions;
|
|
using HTraceWSGI.Scripts.Globals;
|
|
using HTraceWSGI.Scripts.Passes.Shared;
|
|
using UnityEngine;
|
|
using UnityEngine.Experimental.Rendering;
|
|
using UnityEngine.Rendering;
|
|
using UnityEngine.Rendering.Universal;
|
|
|
|
#if UNITY_2023_3_OR_NEWER
|
|
using HTraceWSGI.Scripts.Services.LightsCluster;
|
|
using UnityEngine.Rendering.RenderGraphModule;
|
|
#endif
|
|
|
|
namespace HTraceWSGI.Scripts.Passes.URP
|
|
{
|
|
internal class LightClusterPassURP : ScriptableRenderPass
|
|
{
|
|
const string _OutputTarget = "_OutputTarget";
|
|
private static string s_motionVectorsKeyword = "MOTION_VECTORS";
|
|
|
|
// Shader properties
|
|
private static readonly int AmbientOcclusionParam = Shader.PropertyToID("_AmbientOcclusionParam");
|
|
private static readonly int DebugSwitch = Shader.PropertyToID("_DebugSwitch");
|
|
private static readonly int BuffersSwitch = Shader.PropertyToID("_BuffersSwitch");
|
|
private static readonly int Debug_Output = Shader.PropertyToID("_Debug_Output");
|
|
|
|
internal static ComputeShader VoxelVisualization;
|
|
internal static Material VoxelVisualizationMaterial;
|
|
|
|
// Buffers & etc
|
|
internal static ComputeShader HDebug = null;
|
|
|
|
// Textures
|
|
internal static RTHandle OutputTarget;
|
|
|
|
#region --------------------------- Render Graph ---------------------------
|
|
|
|
#if UNITY_2023_3_OR_NEWER
|
|
private class PassData
|
|
{
|
|
public UniversalCameraData UniversalCameraData;
|
|
}
|
|
|
|
public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
|
|
{
|
|
using (var builder = renderGraph.AddUnsafePass<PassData>(HNames.HTRACE_LIGHT_CLUSTER_PASS_NAME, out var passData, new ProfilingSampler(HNames.HTRACE_LIGHT_CLUSTER_PASS_NAME)))
|
|
{
|
|
UniversalResourceData resourceData = frameData.Get<UniversalResourceData>();
|
|
UniversalCameraData universalCameraData = frameData.Get<UniversalCameraData>();
|
|
UniversalRenderingData universalRenderingData = frameData.Get<UniversalRenderingData>();
|
|
UniversalLightData lightData = frameData.Get<UniversalLightData>();
|
|
|
|
builder.AllowGlobalStateModification(true);
|
|
builder.AllowPassCulling(false);
|
|
|
|
passData.UniversalCameraData = universalCameraData;
|
|
|
|
if (Shared.LightClusterShared.HLightCluster == null) Shared.LightClusterShared.HLightCluster = HExtensions.LoadComputeShader($"HLightClusterURP", HRenderPipeline.URP);
|
|
|
|
int width = (int)(universalCameraData.camera.scaledPixelWidth * universalCameraData.renderScale);
|
|
int height = (int)(universalCameraData.camera.scaledPixelHeight * universalCameraData.renderScale);
|
|
Allocation(width, height, universalCameraData.cameraTargetDescriptor); //resourceData.activeColorTexture.GetDescriptor(renderGraph)
|
|
|
|
builder.SetRenderFunc((PassData data, UnsafeGraphContext context) => ExecutePass(data, context));
|
|
}
|
|
}
|
|
|
|
internal static void Allocation(int width, int height, RenderTextureDescriptor desc, bool onlyRelease = false)
|
|
{
|
|
void ReleaseTextures()
|
|
{
|
|
Shared.LightClusterShared.SceneLightsBuffer.HRelease();
|
|
Shared.LightClusterShared.LightDatasCompactedBuffer.HRelease();
|
|
Shared.LightClusterShared.LightClusterDebugBuffer.HRelease();
|
|
Shared.LightClusterShared.LightClusterIndexesBuffer.HRelease();
|
|
Shared.LightClusterShared.LightClusterCounterBuffer.HRelease();
|
|
Shared.LightClusterShared.SceneLightsBuffer = null;
|
|
Shared.LightClusterShared.LightDatasCompactedBuffer = null;
|
|
Shared.LightClusterShared.LightClusterDebugBuffer = null;
|
|
Shared.LightClusterShared.LightClusterIndexesBuffer = null;
|
|
Shared.LightClusterShared.LightClusterCounterBuffer = null;
|
|
}
|
|
|
|
if (onlyRelease)
|
|
{
|
|
ReleaseTextures();
|
|
return;
|
|
}
|
|
|
|
Shared.LightClusterShared.MaxPunctualLightOnScreen = 100;
|
|
|
|
if (Shared.LightClusterShared.SceneLightsBuffer == null) Shared.LightClusterShared.SceneLightsBuffer = new ComputeBuffer(Shared.LightClusterShared.MaxPunctualLightOnScreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(LightService.LightInfo)));
|
|
if (Shared.LightClusterShared.LightDatasCompactedBuffer == null) Shared.LightClusterShared.LightDatasCompactedBuffer = new ComputeBuffer(Shared.LightClusterShared.MaxPunctualLightOnScreen, sizeof(int));
|
|
if (Shared.LightClusterShared.LightClusterDebugBuffer == null) Shared.LightClusterShared.LightClusterDebugBuffer = new ComputeBuffer(64 * 64 * 64, sizeof(int));
|
|
if (Shared.LightClusterShared.LightClusterIndexesBuffer == null) Shared.LightClusterShared.LightClusterIndexesBuffer = new ComputeBuffer(64 * 64 * 64 * 16, sizeof(int));
|
|
if (Shared.LightClusterShared.LightClusterCounterBuffer == null) Shared.LightClusterShared.LightClusterCounterBuffer = new ComputeBuffer(64 * 64 * 64, sizeof(int));
|
|
}
|
|
|
|
private static void ExecutePass(PassData data, UnsafeGraphContext rgContext)
|
|
{
|
|
if (HSettings.LightingSettings.EvaluatePunctualLights == false)
|
|
return;
|
|
|
|
var cmd = CommandBufferHelpers.GetNativeCommandBuffer(rgContext.cmd);
|
|
|
|
int width = (int)(data.UniversalCameraData.camera.scaledPixelWidth * data.UniversalCameraData.renderScale);
|
|
int height = (int)(data.UniversalCameraData.camera.scaledPixelHeight * data.UniversalCameraData.renderScale);
|
|
|
|
Shared.LightClusterShared.Execute(cmd, data.UniversalCameraData.camera, width, height);
|
|
|
|
Shared.LightClusterShared.History.Update();
|
|
|
|
}
|
|
#endif
|
|
#endregion --------------------------- Render Graph ---------------------------
|
|
|
|
#region --------------------------- Shared ---------------------------
|
|
|
|
|
|
protected internal void Dispose()
|
|
{
|
|
Allocation(0,0, new RenderTextureDescriptor(), true);
|
|
}
|
|
|
|
#endregion --------------------------- Shared ---------------------------
|
|
}
|
|
}
|