624 lines
36 KiB
C#
624 lines
36 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 HTraceWSGI.Scripts.Wrappers;
|
|
using UnityEngine;
|
|
using UnityEngine.Experimental.Rendering;
|
|
using UnityEngine.Rendering;
|
|
using UnityEngine.Rendering.Universal;
|
|
|
|
#if UNITY_2023_3_OR_NEWER
|
|
using UnityEngine.Rendering.RenderGraphModule;
|
|
#endif
|
|
|
|
namespace HTraceWSGI.Scripts.Passes.URP
|
|
{
|
|
internal class SoftwareTracingPassURP : ScriptableRenderPass
|
|
{
|
|
private static readonly int _CameraNormalsTexture = Shader.PropertyToID("_CameraNormalsTexture");
|
|
public static readonly int _IndirectLightingIntensity = Shader.PropertyToID("_IndirectLightingIntensity");
|
|
public static readonly int _MetallicIndirectFallback = Shader.PropertyToID("_MetallicIndirectFallback");
|
|
public static readonly int _ColorCopy = Shader.PropertyToID("_ColorCopy");
|
|
public static readonly int _SampleCountWSGI = Shader.PropertyToID("_SampleCountSSGI");
|
|
public static readonly string USE_RECEIVE_LAYER_MASK = "USE_RECEIVE_LAYER_MASK";
|
|
|
|
public static Material ColorCompose;
|
|
private static bool _forceHashBufferRecreateOnNextAllocation = true;
|
|
|
|
public static ProfilingSamplerHTrace AmbientLightingOverrideSampler = new ProfilingSamplerHTrace("Ambient Lighting Override");
|
|
public static ProfilingSamplerHTrace IndirectLightingInjectionSampler = new ProfilingSamplerHTrace("Indirect Lighting Injection");
|
|
|
|
#region --------------------------- Non Render Graph ---------------------------
|
|
|
|
#if !UNITY_6000_4_OR_NEWER
|
|
private ScriptableRenderer _renderer;
|
|
|
|
protected internal void Initialize(ScriptableRenderer renderer)
|
|
{
|
|
_renderer = renderer;
|
|
}
|
|
|
|
#if UNITY_2023_3_OR_NEWER
|
|
[System.Obsolete]
|
|
#endif
|
|
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
|
|
{
|
|
//SetupShared(renderingData.cameraData.camera, renderingData.cameraData.renderScale, renderingData.cameraData.cameraTargetDescriptor);
|
|
}
|
|
|
|
#if UNITY_2023_3_OR_NEWER
|
|
[System.Obsolete]
|
|
#endif
|
|
public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
|
|
{
|
|
}
|
|
|
|
#if UNITY_2023_3_OR_NEWER
|
|
[System.Obsolete]
|
|
#endif
|
|
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
|
|
{
|
|
|
|
Camera camera = renderingData.cameraData.camera;
|
|
|
|
var cmd = CommandBufferPool.Get(HNames.HTRACE_FINAL_PASS_NAME);
|
|
|
|
int width = (int)(camera.scaledPixelWidth * renderingData.cameraData.renderScale);
|
|
int height = (int)(camera.scaledPixelHeight * renderingData.cameraData.renderScale);
|
|
|
|
context.ExecuteCommandBuffer(cmd);
|
|
cmd.Clear();
|
|
CommandBufferPool.Release(cmd);
|
|
}
|
|
#endif
|
|
|
|
#endregion --------------------------- Non Render Graph ---------------------------
|
|
|
|
#region --------------------------- Render Graph ---------------------------
|
|
|
|
#if UNITY_2023_3_OR_NEWER
|
|
private class PassData
|
|
{
|
|
public UniversalCameraData UniversalCameraData;
|
|
public TextureHandle ColorTexture;
|
|
public TextureHandle ColorCopy;
|
|
}
|
|
|
|
public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
|
|
{
|
|
using (var builder = renderGraph.AddUnsafePass<PassData>(HNames.HTRACE_SOFTWARE_TRACING_PASS_NAME, out var passData, new ProfilingSampler(HNames.HTRACE_SOFTWARE_TRACING_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);
|
|
|
|
TextureHandle colorTexture = universalRenderingData.renderingMode == RenderingMode.Deferred
|
|
#if UNITY_6000_1_OR_NEWER
|
|
|| universalRenderingData.renderingMode == RenderingMode.DeferredPlus
|
|
#endif
|
|
? resourceData.activeColorTexture : resourceData.cameraColor;
|
|
builder.UseTexture(colorTexture, AccessFlags.Write);
|
|
|
|
passData.UniversalCameraData = universalCameraData;
|
|
passData.ColorTexture = colorTexture;
|
|
|
|
if (ColorCompose == null) ColorCompose = new Material(Shader.Find($"Hidden/{HNames.ASSET_NAME}/ColorComposeURP"));
|
|
if (SoftwareTracingShared.HReservoirValidation == null) SoftwareTracingShared.HReservoirValidation = HExtensions.LoadComputeShader("HReservoirValidation");
|
|
if (SoftwareTracingShared.HTracingScreenSpace == null) SoftwareTracingShared.HTracingScreenSpace = HExtensions.LoadComputeShader("HTracingScreenSpace");
|
|
if (SoftwareTracingShared.HTracingWorldSpace == null) SoftwareTracingShared.HTracingWorldSpace = HExtensions.LoadComputeShader("HTracingWorldSpace");
|
|
if (SoftwareTracingShared.HRadianceCache == null) SoftwareTracingShared.HRadianceCache = HExtensions.LoadComputeShader("HRadianceCache");
|
|
if (SoftwareTracingShared.HTemporalReprojection == null) SoftwareTracingShared.HTemporalReprojection = HExtensions.LoadComputeShader("HTemporalReprojection");
|
|
if (SoftwareTracingShared.HSpatialPrepass == null) SoftwareTracingShared.HSpatialPrepass = HExtensions.LoadComputeShader("HSpatialPrepass");
|
|
if (SoftwareTracingShared.HProbeAmbientOcclusion == null) SoftwareTracingShared.HProbeAmbientOcclusion = HExtensions.LoadComputeShader("HProbeAmbientOcclusion");
|
|
if (SoftwareTracingShared.HReSTIR == null) SoftwareTracingShared.HReSTIR = HExtensions.LoadComputeShader("HReSTIR");
|
|
if (SoftwareTracingShared.HCopy == null) SoftwareTracingShared.HCopy = HExtensions.LoadComputeShader("HCopy");
|
|
if (SoftwareTracingShared.HRayGeneration == null) SoftwareTracingShared.HRayGeneration = HExtensions.LoadComputeShader("HRayGeneration");
|
|
if (SoftwareTracingShared.HRenderAO == null) SoftwareTracingShared.HRenderAO = HExtensions.LoadComputeShader("HRenderAO");
|
|
if (SoftwareTracingShared.HDebugPassthrough == null) SoftwareTracingShared.HDebugPassthrough = HExtensions.LoadComputeShader("HDebugPassthrough");
|
|
if (SoftwareTracingShared.HInterpolation == null) SoftwareTracingShared.HInterpolation = HExtensions.LoadComputeShader("HInterpolation");
|
|
if (SoftwareTracingShared.HTemporalDenoiser == null) SoftwareTracingShared.HTemporalDenoiser = HExtensions.LoadComputeShader("HTemporalDenoiser");
|
|
|
|
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)
|
|
|
|
TextureHandle colorTextureHandle = colorTexture;
|
|
TextureDesc textureDesc = colorTextureHandle.GetDescriptor(renderGraph);
|
|
textureDesc.clearBuffer = false;
|
|
textureDesc.colorFormat = GraphicsFormat.R16G16B16A16_SFloat;
|
|
textureDesc.name = "_ColorCopy";
|
|
passData.ColorCopy = renderGraph.CreateTexture(textureDesc);
|
|
builder.UseTexture(passData.ColorCopy, AccessFlags.ReadWrite);
|
|
|
|
//SoftwareTracingShared.SkipFirstFrame = true;
|
|
|
|
builder.SetRenderFunc((PassData data, UnsafeGraphContext context) => ExecutePass(data, context));
|
|
}
|
|
}
|
|
|
|
private static void ExecutePass(PassData data, UnsafeGraphContext rgContext)
|
|
{
|
|
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);
|
|
|
|
if (Shader.GetGlobalTexture(_CameraNormalsTexture) == null) //TODO: do we need it?
|
|
return;
|
|
|
|
// ---------------------------------------- AMBIENT LIGHTING OVERRIDE ---------------------------------------- //
|
|
using (new HTraceProfilingScope(cmd, AmbientLightingOverrideSampler))
|
|
{
|
|
cmd.SetGlobalFloat(_IndirectLightingIntensity, HSettings.GeneralSettings.Intensity);
|
|
|
|
if (HSettings.GeneralSettings.AmbientOverride)
|
|
{
|
|
// Copy Color buffer
|
|
CoreUtils.SetRenderTarget(cmd, data.ColorCopy);
|
|
cmd.DrawProcedural(Matrix4x4.identity, ColorCompose, 0, MeshTopology.Triangles, 3, 1);
|
|
|
|
// Subtract indirect lighting from Color buffer
|
|
CoreUtils.SetRenderTarget(cmd, data.ColorTexture);
|
|
ColorCompose.SetTexture(_ColorCopy, data.ColorCopy);
|
|
cmd.DrawProcedural(Matrix4x4.identity, ColorCompose, 1, MeshTopology.Triangles, 3, 1);
|
|
}
|
|
|
|
// Early out if we want to preview direct lighting only
|
|
if (HSettings.GeneralSettings.DebugModeWS == DebugModeWS.DirectLighting)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
SoftwareTracingShared.Execute(cmd, data.UniversalCameraData.camera, width, height, data.ColorTexture, SoftwareTracingShared.ColorPreviousFrame.rt, Shader.GetGlobalTexture("g_HTraceGBuffer0"));
|
|
|
|
// ---------------------------------------- INDIRECT LIGHTING INJECTION ---------------------------------------- //
|
|
|
|
using (new HTraceProfilingScope(cmd, IndirectLightingInjectionSampler))
|
|
{
|
|
// Copy color buffer + indirect lighting (without intensity multiplication) for multibounce
|
|
cmd.SetComputeTextureParam(SoftwareTracingShared.HTemporalReprojection, (int)SoftwareTracingShared.HTemporalReprojectionKernel.CopyHistoryURP, HShaderParams._Radiance_Output, SoftwareTracingShared.ColorPreviousFrame.rt);
|
|
cmd.DispatchCompute(SoftwareTracingShared.HTemporalReprojection, (int)SoftwareTracingShared.HTemporalReprojectionKernel.CopyHistoryURP, Mathf.CeilToInt(width / 8.0f), Mathf.CeilToInt(height / 8.0f), HRenderer.TextureXrSlices);
|
|
|
|
// Inject final indirect lighting (with intensity multiplication) into color buffer via additive blending
|
|
CoreUtils.SetRenderTarget(cmd, data.ColorTexture);
|
|
ColorCompose.SetInt(_MetallicIndirectFallback, HSettings.GeneralSettings.MetallicIndirectFallback ? 1 : 0);
|
|
cmd.DrawProcedural(Matrix4x4.identity, ColorCompose, 2, MeshTopology.Triangles, 3, 1);
|
|
}
|
|
|
|
SoftwareTracingShared.History.Update();
|
|
}
|
|
|
|
internal static void Allocation(int width, int height, RenderTextureDescriptor desc, bool onlyRelease = false)
|
|
{
|
|
AllocateMainRT(width, height, desc, onlyRelease);
|
|
AllocateSSAO_RT(width, height, desc, onlyRelease);
|
|
AllocationHashBuffers(onlyRelease, _forceHashBufferRecreateOnNextAllocation);
|
|
AllocateDebugRT(width, height, desc, onlyRelease);
|
|
AllocateIndirectionBuffers(width, height, onlyRelease);
|
|
|
|
_forceHashBufferRecreateOnNextAllocation = onlyRelease;
|
|
}
|
|
|
|
internal static void RequestHashBufferRecreate()
|
|
{
|
|
_forceHashBufferRecreateOnNextAllocation = true;
|
|
}
|
|
|
|
internal static void AllocateMainRT(int width, int height, RenderTextureDescriptor desc, bool onlyRelease = false)
|
|
{
|
|
void ReleaseTextures()
|
|
{
|
|
SoftwareTracingShared.ColorPreviousFrame.HRelease();
|
|
|
|
SoftwareTracingShared.VoxelPayload.HRelease();
|
|
SoftwareTracingShared.RayDirections.HRelease();
|
|
SoftwareTracingShared.HitRadiance.HRelease();
|
|
SoftwareTracingShared.HitDistanceScreenSpace.HRelease();
|
|
SoftwareTracingShared.HitDistanceWorldSpace.HRelease();
|
|
SoftwareTracingShared.HitCoordScreenSpace.HRelease();
|
|
|
|
SoftwareTracingShared.ProbeAmbientOcclusion.HRelease();
|
|
SoftwareTracingShared.ProbeAmbientOcclusion_History.HRelease();
|
|
SoftwareTracingShared.ProbeAmbientOcclusion_Filtered.HRelease();
|
|
|
|
SoftwareTracingShared.GeometryNormal.HRelease();
|
|
SoftwareTracingShared.NormalDepth_History.HRelease();
|
|
SoftwareTracingShared.ProbeNormalDepth.HRelease();
|
|
SoftwareTracingShared.ProbeNormalDepth_History.HRelease();
|
|
SoftwareTracingShared.ProbeWorldPosNormal_History.HRelease();
|
|
SoftwareTracingShared.ProbeNormalDepth_Intermediate.HRelease();
|
|
SoftwareTracingShared.ProbeDiffuse.HRelease();
|
|
|
|
SoftwareTracingShared.HistoryIndirection.HRelease();
|
|
SoftwareTracingShared.ReprojectionWeights.HRelease();
|
|
SoftwareTracingShared.PersistentReprojectionWeights.HRelease();
|
|
SoftwareTracingShared.ReprojectionCoord.HRelease();
|
|
SoftwareTracingShared.PersistentReprojectionCoord.HRelease();
|
|
|
|
SoftwareTracingShared.SpatialOffsetsPacked.HRelease();
|
|
SoftwareTracingShared.SpatialWeightsPacked.HRelease();
|
|
|
|
SoftwareTracingShared.ReservoirAtlas.HRelease();
|
|
SoftwareTracingShared.ReservoirAtlas_History.HRelease();
|
|
SoftwareTracingShared.ReservoirAtlasRadianceData_A.HRelease();
|
|
SoftwareTracingShared.ReservoirAtlasRadianceData_B.HRelease();
|
|
SoftwareTracingShared.ReservoirAtlasRadianceData_C.HRelease();
|
|
SoftwareTracingShared.ReservoirAtlasRayData_A.HRelease();
|
|
SoftwareTracingShared.ReservoirAtlasRayData_B.HRelease();
|
|
SoftwareTracingShared.ReservoirAtlasRayData_C.HRelease();
|
|
|
|
SoftwareTracingShared.ShadowGuidanceMask.HRelease();
|
|
SoftwareTracingShared.ShadowGuidanceMask_Accumulated.HRelease();
|
|
SoftwareTracingShared.ShadowGuidanceMask_Filtered.HRelease();
|
|
SoftwareTracingShared.ShadowGuidanceMask_History.HRelease();
|
|
SoftwareTracingShared.ShadowGuidanceMask_CheckerboardHistory.HRelease();
|
|
SoftwareTracingShared.ShadowGuidanceMask_Samplecount.HRelease();
|
|
SoftwareTracingShared.ShadowGuidanceMask_SamplecountHistory.HRelease();
|
|
|
|
SoftwareTracingShared.PackedSH_A.HRelease();
|
|
SoftwareTracingShared.PackedSH_B.HRelease();
|
|
SoftwareTracingShared.Radiance_Interpolated.HRelease();
|
|
|
|
SoftwareTracingShared.RadianceAccumulated.HRelease();
|
|
SoftwareTracingShared.RadianceAccumulated_History.HRelease();
|
|
SoftwareTracingShared.LuminanceDelta.HRelease();
|
|
SoftwareTracingShared.LuminanceDelta_History.HRelease();
|
|
|
|
SoftwareTracingShared.RadianceCacheFiltered.HRelease();
|
|
|
|
SoftwareTracingShared.RayCounter.HRelease();
|
|
SoftwareTracingShared.RayCounterWS.HRelease();
|
|
SoftwareTracingShared.IndirectArgumentsSS.HRelease();
|
|
SoftwareTracingShared.IndirectArgumentsWS.HRelease();
|
|
SoftwareTracingShared.IndirectArgumentsOV.HRelease();
|
|
SoftwareTracingShared.IndirectArgumentsSF.HRelease();
|
|
|
|
SoftwareTracingShared.RayCounter = null;
|
|
SoftwareTracingShared.RayCounterWS = null;
|
|
SoftwareTracingShared.IndirectArgumentsSS = null;
|
|
SoftwareTracingShared.IndirectArgumentsWS = null;
|
|
SoftwareTracingShared.IndirectArgumentsOV = null;
|
|
SoftwareTracingShared.IndirectArgumentsSF = null;
|
|
|
|
SoftwareTracingShared.PointDistributionBuffer.HRelease();
|
|
SoftwareTracingShared.SpatialOffsetsBuffer.HRelease();
|
|
|
|
SoftwareTracingShared.PointDistributionBuffer = null;
|
|
SoftwareTracingShared.SpatialOffsetsBuffer = null;
|
|
|
|
SoftwareTracingShared.HashBuffer_Key.HRelease();
|
|
SoftwareTracingShared.HashBuffer_Payload.HRelease();
|
|
SoftwareTracingShared.HashBuffer_Counter.HRelease();
|
|
SoftwareTracingShared.HashBuffer_Radiance.HRelease();
|
|
SoftwareTracingShared.HashBuffer_Position.HRelease();
|
|
|
|
SoftwareTracingShared.HashBuffer_Key = null;
|
|
SoftwareTracingShared.HashBuffer_Payload = null;
|
|
SoftwareTracingShared.HashBuffer_Counter = null;
|
|
SoftwareTracingShared.HashBuffer_Radiance = null;
|
|
SoftwareTracingShared.HashBuffer_Position = null;
|
|
}
|
|
|
|
if (onlyRelease)
|
|
{
|
|
ReleaseTextures();
|
|
return;
|
|
}
|
|
|
|
Vector2Int fullRes = new Vector2Int(width, height);
|
|
float probeSize = (float)HSettings.GeneralSettings.RayCountMode.ParseToProbeSize();
|
|
Vector2Int probeRes = new Vector2Int(Mathf.RoundToInt(width / probeSize), Mathf.RoundToInt(height / probeSize));
|
|
Vector2Int probeAtlasRes = new Vector2Int(Mathf.RoundToInt(width / probeSize * (float)HConstants.OCTAHEDRAL_SIZE), Mathf.RoundToInt(height / probeSize * (float)HConstants.OCTAHEDRAL_SIZE));
|
|
|
|
TextureDimension originalTextureDimension = desc.dimension;
|
|
RenderTextureDescriptor fullResDesc = desc;
|
|
fullResDesc.enableRandomWrite = true;
|
|
fullResDesc.depthStencilFormat = GraphicsFormat.None;
|
|
|
|
RenderTextureDescriptor probeDesc = desc;
|
|
probeDesc.width = probeRes.x;
|
|
probeDesc.height = probeRes.y;
|
|
probeDesc.enableRandomWrite = true;
|
|
probeDesc.depthStencilFormat = GraphicsFormat.None;
|
|
|
|
RenderTextureDescriptor probeAtlasDesc = desc;
|
|
probeAtlasDesc.width = probeAtlasRes.x;
|
|
probeAtlasDesc.height = probeAtlasRes.y;
|
|
probeAtlasDesc.enableRandomWrite = true;
|
|
probeAtlasDesc.depthStencilFormat = GraphicsFormat.None;
|
|
|
|
// -------------------------------------- BUFFERS -------------------------------------- //
|
|
|
|
// Indirection dispatch buffers
|
|
if (SoftwareTracingShared.RayCounter == null) SoftwareTracingShared.RayCounter = new ComputeBuffer(10 * HRenderer.TextureXrSlices, sizeof(uint));
|
|
if (SoftwareTracingShared.RayCounterWS == null) SoftwareTracingShared.RayCounterWS = new ComputeBuffer(10 * HRenderer.TextureXrSlices, sizeof(uint));
|
|
if (SoftwareTracingShared.IndirectArgumentsSS == null) SoftwareTracingShared.IndirectArgumentsSS = new ComputeBuffer(3 * HRenderer.TextureXrSlices, sizeof(uint), ComputeBufferType.IndirectArguments);
|
|
if (SoftwareTracingShared.IndirectArgumentsWS == null) SoftwareTracingShared.IndirectArgumentsWS = new ComputeBuffer(3 * HRenderer.TextureXrSlices, sizeof(uint), ComputeBufferType.IndirectArguments);
|
|
if (SoftwareTracingShared.IndirectArgumentsOV == null) SoftwareTracingShared.IndirectArgumentsOV = new ComputeBuffer(3 * HRenderer.TextureXrSlices, sizeof(uint), ComputeBufferType.IndirectArguments);
|
|
if (SoftwareTracingShared.IndirectArgumentsSF == null) SoftwareTracingShared.IndirectArgumentsSF = new ComputeBuffer(3 * HRenderer.TextureXrSlices, sizeof(uint), ComputeBufferType.IndirectArguments);
|
|
|
|
// Spatial offsets buffers
|
|
if (SoftwareTracingShared.PointDistributionBuffer == null) SoftwareTracingShared.PointDistributionBuffer = new ComputeBuffer(HRenderer.TextureXrSlices * 32 * 4, 2 * sizeof(float));
|
|
if (SoftwareTracingShared.SpatialOffsetsBuffer == null) SoftwareTracingShared.SpatialOffsetsBuffer = new ComputeBuffer(9 * 9, 2 * sizeof(int));
|
|
|
|
// Hash buffers
|
|
if (SoftwareTracingShared.HashBuffer_Key == null) SoftwareTracingShared.HashBuffer_Key = new ComputeBuffer(HConstants.HASH_STORAGE_SIZE, 1 * sizeof(uint));
|
|
if (SoftwareTracingShared.HashBuffer_Payload == null) SoftwareTracingShared.HashBuffer_Payload = new ComputeBuffer(HConstants.HASH_STORAGE_SIZE / HConstants.HASH_UPDATE_FRACTION, 2 * sizeof(uint));
|
|
if (SoftwareTracingShared.HashBuffer_Counter == null) SoftwareTracingShared.HashBuffer_Counter = new ComputeBuffer(HConstants.HASH_STORAGE_SIZE, 1 * sizeof(uint));
|
|
if (SoftwareTracingShared.HashBuffer_Radiance == null) SoftwareTracingShared.HashBuffer_Radiance = new ComputeBuffer(HConstants.HASH_STORAGE_SIZE, 4 * sizeof(uint));
|
|
if (SoftwareTracingShared.HashBuffer_Position == null) SoftwareTracingShared.HashBuffer_Position = new ComputeBuffer(HConstants.HASH_STORAGE_SIZE, 4 * sizeof(uint));
|
|
|
|
SoftwareTracingShared.ColorPreviousFrame.ReAllocateIfNeeded(name: "_ColorPreviousFrame", ref fullResDesc, GraphicsFormat.B10G11R11_UFloatPack32);
|
|
|
|
// -------------------------------------- TRACING RT -------------------------------------- //
|
|
SoftwareTracingShared.VoxelPayload.ReAllocateIfNeeded("_VoxelPayload", ref probeAtlasDesc, GraphicsFormat.R32G32_UInt);
|
|
SoftwareTracingShared.RayDirections.ReAllocateIfNeeded("_RayDirections", ref probeAtlasDesc, GraphicsFormat.R8G8B8A8_UNorm);
|
|
SoftwareTracingShared.HitDistanceScreenSpace.ReAllocateIfNeeded("_HitDistanceScreenSpace", ref probeAtlasDesc, GraphicsFormat.R16_UInt);
|
|
SoftwareTracingShared.HitDistanceWorldSpace.ReAllocateIfNeeded("_HitDistanceWorldSpace", ref probeAtlasDesc, GraphicsFormat.R16_SFloat);
|
|
SoftwareTracingShared.HitRadiance.ReAllocateIfNeeded("_HitRadiance", ref probeAtlasDesc, GraphicsFormat.R16G16B16A16_SFloat);
|
|
SoftwareTracingShared.HitCoordScreenSpace.ReAllocateIfNeeded("_HitCoordScreenSpace", ref fullResDesc, GraphicsFormat.R16G16_UInt);
|
|
|
|
|
|
// -------------------------------------- PROBE AO RT -------------------------------------- //
|
|
SoftwareTracingShared.ProbeAmbientOcclusion.ReAllocateIfNeeded("_ProbeAmbientOcclusion", ref probeDesc, GraphicsFormat.R16_UInt);
|
|
SoftwareTracingShared.ProbeAmbientOcclusion_History.ReAllocateIfNeeded("_ProbeAmbientOcclusion_History", ref probeDesc, GraphicsFormat.R16_UInt, volumeDepth: HRenderer.TextureXrSlices * HConstants.PERSISTENT_HISTORY_SAMPLES, dimension: TextureDimension.Tex2DArray);
|
|
SoftwareTracingShared.ProbeAmbientOcclusion_Filtered.ReAllocateIfNeeded("_ProbeAmbientOcclusion_Filtered", ref probeDesc, GraphicsFormat.R8_UNorm);
|
|
|
|
|
|
// -------------------------------------- GBUFFER RT -------------------------------------- //
|
|
SoftwareTracingShared.GeometryNormal.ReAllocateIfNeeded("_GeometryNormal", ref fullResDesc, GraphicsFormat.R16G16B16A16_SFloat);
|
|
SoftwareTracingShared.NormalDepth_History.ReAllocateIfNeeded("_NormalDepth_History", ref fullResDesc, GraphicsFormat.R32G32_UInt);
|
|
SoftwareTracingShared.ProbeNormalDepth.ReAllocateIfNeeded("_ProbeNormalDepth", ref probeDesc, GraphicsFormat.R32G32_UInt);
|
|
SoftwareTracingShared.ProbeNormalDepth_History.ReAllocateIfNeeded("_ProbeNormalDepth_History", ref probeDesc, GraphicsFormat.R32G32_UInt);
|
|
SoftwareTracingShared.ProbeWorldPosNormal_History.ReAllocateIfNeeded("_ProbeWorldPosNormal_History", ref probeDesc, GraphicsFormat.R32G32B32A32_UInt, volumeDepth: HRenderer.TextureXrSlices * HConstants.PERSISTENT_HISTORY_SAMPLES, dimension: TextureDimension.Tex2DArray);
|
|
SoftwareTracingShared.ProbeNormalDepth_Intermediate.ReAllocateIfNeeded("_ProbeNormalDepth_Intermediate", ref probeDesc, GraphicsFormat.R32G32_UInt);
|
|
SoftwareTracingShared.ProbeDiffuse.ReAllocateIfNeeded("_ProbeDiffuse", ref probeDesc, GraphicsFormat.R8G8B8A8_UNorm);
|
|
|
|
|
|
// -------------------------------------- REPROJECTION RT -------------------------------------- //
|
|
SoftwareTracingShared.HistoryIndirection.ReAllocateIfNeeded("_HistoryIndirection", ref probeDesc, GraphicsFormat.R16G16_UInt, volumeDepth: HRenderer.TextureXrSlices * HConstants.PERSISTENT_HISTORY_SAMPLES, dimension: TextureDimension.Tex2DArray);
|
|
SoftwareTracingShared.ReprojectionWeights.ReAllocateIfNeeded("_ReprojectionWeights", ref probeDesc, GraphicsFormat.R8G8B8A8_UNorm);
|
|
SoftwareTracingShared.PersistentReprojectionWeights.ReAllocateIfNeeded("_PersistentReprojectionWeights", ref probeDesc, GraphicsFormat.R8G8B8A8_UNorm);
|
|
SoftwareTracingShared.ReprojectionCoord.ReAllocateIfNeeded("_ReprojectionCoord", ref probeDesc, GraphicsFormat.R16G16_UInt);
|
|
SoftwareTracingShared.PersistentReprojectionCoord.ReAllocateIfNeeded("_PersistentReprojectionCoord", ref probeDesc, GraphicsFormat.R16G16_UInt);
|
|
|
|
|
|
// -------------------------------------- SPATIAL PREPASS RT -------------------------------------- //
|
|
SoftwareTracingShared.SpatialOffsetsPacked.ReAllocateIfNeeded("_SpatialOffsetsPacked", ref probeDesc, GraphicsFormat.R32G32B32A32_UInt, volumeDepth: HRenderer.TextureXrSlices * 4, dimension: TextureDimension.Tex2DArray);
|
|
SoftwareTracingShared.SpatialWeightsPacked.ReAllocateIfNeeded("_SpatialWeightsPacked", ref probeDesc, GraphicsFormat.R16G16B16A16_UInt, volumeDepth: HRenderer.TextureXrSlices * 4, dimension: TextureDimension.Tex2DArray);
|
|
|
|
|
|
// -------------------------------------- RESERVOIR RT -------------------------------------- //
|
|
SoftwareTracingShared.ReservoirAtlas.ReAllocateIfNeeded("_ReservoirAtlas", ref probeAtlasDesc, GraphicsFormat.R32G32B32A32_UInt);
|
|
SoftwareTracingShared.ReservoirAtlas_History.ReAllocateIfNeeded("_ReservoirAtlas_History", ref probeAtlasDesc, GraphicsFormat.R32G32B32A32_UInt, volumeDepth: HRenderer.TextureXrSlices * HConstants.PERSISTENT_HISTORY_SAMPLES, dimension: TextureDimension.Tex2DArray);
|
|
SoftwareTracingShared.ReservoirAtlasRadianceData_A.ReAllocateIfNeeded("_ReservoirAtlasRadianceData_A" , ref probeAtlasDesc, GraphicsFormat.R32G32_UInt);
|
|
SoftwareTracingShared.ReservoirAtlasRadianceData_B.ReAllocateIfNeeded("_ReservoirAtlasRadianceData_B", ref probeAtlasDesc, GraphicsFormat.R32G32_UInt);
|
|
SoftwareTracingShared.ReservoirAtlasRadianceData_C.ReAllocateIfNeeded("_ReservoirAtlasRadianceData_C", ref probeAtlasDesc, GraphicsFormat.R32G32_UInt);
|
|
|
|
SoftwareTracingShared.ReservoirAtlasRayData_A.ReAllocateIfNeeded("_ReservoirAtlasRayData_A", ref probeAtlasDesc, GraphicsFormat.R32_UInt);
|
|
SoftwareTracingShared.ReservoirAtlasRayData_B.ReAllocateIfNeeded("_ReservoirAtlasRayData_B", ref probeAtlasDesc, GraphicsFormat.R32_UInt);
|
|
SoftwareTracingShared.ReservoirAtlasRayData_C.ReAllocateIfNeeded("_ReservoirAtlasRayData_C", ref probeAtlasDesc, GraphicsFormat.R32_UInt);
|
|
|
|
|
|
// -------------------------------------- SHADOW GUIDANCE MASK RT -------------------------------------- //
|
|
SoftwareTracingShared.ShadowGuidanceMask.ReAllocateIfNeeded("_ShadowGuidanceMask", ref probeAtlasDesc, GraphicsFormat.R8_UNorm);
|
|
SoftwareTracingShared.ShadowGuidanceMask_Accumulated.ReAllocateIfNeeded("_ShadowGuidanceMask_Accumulated", ref probeAtlasDesc, GraphicsFormat.R8_UNorm);
|
|
SoftwareTracingShared.ShadowGuidanceMask_Filtered .ReAllocateIfNeeded("_ShadowGuidanceMask_Filtered", ref probeAtlasDesc, GraphicsFormat.R8_UNorm);
|
|
SoftwareTracingShared.ShadowGuidanceMask_History.ReAllocateIfNeeded("_ShadowGuidanceMask_History", ref probeAtlasDesc, GraphicsFormat.R8_UNorm);
|
|
SoftwareTracingShared.ShadowGuidanceMask_CheckerboardHistory.ReAllocateIfNeeded("_ShadowGuidanceMask_CheckerboardHistory", ref probeAtlasDesc, GraphicsFormat.R8_UNorm);
|
|
SoftwareTracingShared.ShadowGuidanceMask_Samplecount.ReAllocateIfNeeded("_ShadowGuidanceMask_Samplecount", ref probeDesc, GraphicsFormat.R16_SFloat);
|
|
SoftwareTracingShared.ShadowGuidanceMask_SamplecountHistory.ReAllocateIfNeeded("_ShadowGuidanceMask_SamplecountHistory", ref probeDesc, GraphicsFormat.R16_SFloat);
|
|
|
|
|
|
// -------------------------------------- INTERPOLATION RT -------------------------------------- //
|
|
SoftwareTracingShared.PackedSH_A.ReAllocateIfNeeded("_PackedSH_A", ref probeDesc, GraphicsFormat.R32G32B32A32_UInt);
|
|
SoftwareTracingShared.PackedSH_B.ReAllocateIfNeeded("_PackedSH_B", ref probeDesc, GraphicsFormat.R32G32B32A32_UInt);
|
|
SoftwareTracingShared.Radiance_Interpolated.ReAllocateIfNeeded("_Radiance_Interpolated", ref fullResDesc, GraphicsFormat.R32_UInt);
|
|
|
|
|
|
// -------------------------------------- TEMPORAL DENOISER RT -------------------------------------- //
|
|
SoftwareTracingShared.RadianceAccumulated.ReAllocateIfNeeded("_RadianceAccumulated", ref fullResDesc, GraphicsFormat.R16G16B16A16_SFloat);
|
|
SoftwareTracingShared.RadianceAccumulated_History.ReAllocateIfNeeded("_RadianceAccumulated_History", ref fullResDesc, GraphicsFormat.R16G16B16A16_SFloat);
|
|
SoftwareTracingShared.LuminanceDelta.ReAllocateIfNeeded("_RadianceLumaDelta", ref fullResDesc, GraphicsFormat.R16_SFloat);
|
|
SoftwareTracingShared.LuminanceDelta_History.ReAllocateIfNeeded("_RadianceLumaDelta_History", ref fullResDesc, GraphicsFormat.R16_SFloat);
|
|
|
|
// TODO: figure out if we need this, do not delete and do not allocate for now
|
|
// RadianceCacheFiltered.ReAllocateIfNeeded(600, 100, 100, TextureDimension.Tex3D,
|
|
// GraphicsFormat.B10G11R11_UFloatPack32, "_RadianceCacheFiltered");
|
|
}
|
|
|
|
internal static void AllocateSSAO_RT(int width, int height, RenderTextureDescriptor desc, bool onlyRelease = false)
|
|
{
|
|
void ReleaseTextures()
|
|
{
|
|
SoftwareTracingShared.ProbeSSAO.HRelease();
|
|
SoftwareTracingShared.NormalDepthHalf.HRelease();
|
|
SoftwareTracingShared.BentNormalsAO.HRelease();
|
|
SoftwareTracingShared.BentNormalsAO_Interpolated.HRelease();
|
|
SoftwareTracingShared.BentNormalsAO_History.HRelease();
|
|
SoftwareTracingShared.BentNormalsAO_Accumulated.HRelease();
|
|
SoftwareTracingShared.BentNormalsAO_Samplecount.HRelease();
|
|
SoftwareTracingShared.BentNormalsAO_SamplecountHistory.HRelease();
|
|
}
|
|
|
|
if (onlyRelease)
|
|
{
|
|
ReleaseTextures();
|
|
return;
|
|
}
|
|
|
|
if (SoftwareTracingShared.UseDirectionalOcclusion == false)
|
|
return;
|
|
|
|
Vector2Int fullRes = new Vector2Int(width, height);
|
|
float probeSize = (float)HSettings.GeneralSettings.RayCountMode.ParseToProbeSize();
|
|
Vector2Int probeRes = new Vector2Int(Mathf.RoundToInt(width / probeSize), Mathf.RoundToInt(height / probeSize));
|
|
|
|
RenderTextureDescriptor fullResDesc = desc;
|
|
TextureDimension originalTextureDimension = desc.dimension;
|
|
fullResDesc.depthStencilFormat = GraphicsFormat.None;
|
|
fullResDesc.enableRandomWrite = true;
|
|
|
|
RenderTextureDescriptor probeDesc = desc;
|
|
probeDesc.width = probeRes.x;
|
|
probeDesc.height = probeRes.y;
|
|
probeDesc.depthStencilFormat = GraphicsFormat.None;
|
|
probeDesc.enableRandomWrite = true;
|
|
|
|
// -------------------------------------- SSAO RT -------------------------------------- //
|
|
|
|
if (HSettings.ScreenSpaceLightingSettings.DirectionalOcclusion)
|
|
{
|
|
SoftwareTracingShared.ProbeSSAO.ReAllocateIfNeeded("_ProbeSSAO", ref probeDesc, GraphicsFormat.R8_UNorm);
|
|
SoftwareTracingShared.BentNormalsAO.ReAllocateIfNeeded("_BentNormalsAO", ref fullResDesc, GraphicsFormat.R16G16B16A16_SFloat);
|
|
SoftwareTracingShared.BentNormalsAO_Interpolated.ReAllocateIfNeeded("_BentNormalsAO_Interpolated", ref fullResDesc, GraphicsFormat.R16G16B16A16_SFloat);
|
|
fullResDesc.width = Mathf.RoundToInt(fullResDesc.width / 2.0f);
|
|
fullResDesc.height = Mathf.RoundToInt(fullResDesc.height / 2.0f);
|
|
SoftwareTracingShared.NormalDepthHalf.ReAllocateIfNeeded("_NormalDepthHalf", ref fullResDesc, GraphicsFormat.R16G16B16A16_SFloat);
|
|
fullResDesc.width = width;
|
|
fullResDesc.height = height;
|
|
|
|
// TODO: porbably we will no need these, leave them for now, but don't allocate.
|
|
// BentNormalsAO_History.ReAllocateIfNeeded(FullRes, HRenderer.TextureXrSlices, textureDimension: HConstants.TextureDimension,
|
|
// GraphicsFormat.R16G16B16A16_SFloat, "_BentNormalsAO_History");
|
|
//
|
|
// BentNormalsAO_Accumulated.ReAllocateIfNeeded(FullRes, HRenderer.TextureXrSlices, textureDimension: HConstants.TextureDimension,
|
|
// GraphicsFormat.R16G16B16A16_SFloat, "_BentNormalsAO_Accumulated");
|
|
//
|
|
// BentNormalsAO_SamplecountHistory.ReAllocateIfNeeded(FullRes, HRenderer.TextureXrSlices, textureDimension: HConstants.TextureDimension,
|
|
// GraphicsFormat.R8_UInt, "_BentNormalsAO_SamplecountHistory");
|
|
//
|
|
// BentNormalsAO_Samplecount.ReAllocateIfNeeded(FullRes, HRenderer.TextureXrSlices, textureDimension: HConstants.TextureDimension,
|
|
// GraphicsFormat.R8_UInt, "_BentNormalsAO_Samplecount");
|
|
}
|
|
else //Warnings suppression
|
|
{
|
|
fullResDesc.width = fullResDesc.height = 1;
|
|
SoftwareTracingShared.ProbeSSAO.ReAllocateIfNeeded("_ProbeSSAO", ref fullResDesc, GraphicsFormat.R8_UNorm);
|
|
SoftwareTracingShared.BentNormalsAO.ReAllocateIfNeeded("_BentNormalsAO", ref fullResDesc, GraphicsFormat.R16G16B16A16_SFloat);
|
|
SoftwareTracingShared.BentNormalsAO_Interpolated.ReAllocateIfNeeded("_BentNormalsAO_Interpolated", ref fullResDesc, GraphicsFormat.R16G16B16A16_SFloat);
|
|
SoftwareTracingShared.NormalDepthHalf.ReAllocateIfNeeded("_NormalDepthHalf", ref fullResDesc, GraphicsFormat.R16G16B16A16_SFloat);
|
|
}
|
|
}
|
|
|
|
internal static void AllocateDebugRT(int width, int height, RenderTextureDescriptor desc, bool onlyRelease = false)
|
|
{
|
|
void ReleaseTextures()
|
|
{
|
|
SoftwareTracingShared.DebugOutput.HRelease();
|
|
}
|
|
|
|
if (onlyRelease)
|
|
{
|
|
ReleaseTextures();
|
|
return;
|
|
}
|
|
|
|
if (HSettings.GeneralSettings.DebugModeWS == DebugModeWS.None)
|
|
return;
|
|
|
|
Vector2 fullRes = Vector2.one;
|
|
|
|
// -------------------------------------- DEBUG RT -------------------------------------- //
|
|
if (HSettings.GeneralSettings.DebugModeWS != DebugModeWS.None)
|
|
{
|
|
desc.depthStencilFormat = GraphicsFormat.None;
|
|
desc.enableRandomWrite = true;
|
|
SoftwareTracingShared.DebugOutput.ReAllocateIfNeeded("_DebugOutput", ref desc, GraphicsFormat.B10G11R11_UFloatPack32);
|
|
}
|
|
}
|
|
|
|
internal static void AllocateIndirectionBuffers(int width, int height, bool onlyRelease = false)
|
|
{
|
|
void ReleaseTextures()
|
|
{
|
|
SoftwareTracingShared.IndirectCoordsSS.HRelease();
|
|
SoftwareTracingShared.IndirectCoordsWS.HRelease();
|
|
SoftwareTracingShared.IndirectCoordsOV.HRelease();
|
|
SoftwareTracingShared.IndirectCoordsSF.HRelease();
|
|
SoftwareTracingShared.IndirectCoordsSS = null;
|
|
SoftwareTracingShared.IndirectCoordsWS = null;
|
|
SoftwareTracingShared.IndirectCoordsOV = null;
|
|
SoftwareTracingShared.IndirectCoordsSF = null;
|
|
}
|
|
|
|
if (onlyRelease)
|
|
{
|
|
ReleaseTextures();
|
|
return;
|
|
}
|
|
|
|
// -------------------------------------- BUFFERS -------------------------------------- //
|
|
|
|
if (SoftwareTracingShared.IndirectCoordsSS == null) SoftwareTracingShared.IndirectCoordsSS = new HDynamicBuffer(BufferType.ComputeBuffer, 2 * sizeof(uint), HRenderer.TextureXrSlices, avoidDownscale: true);
|
|
if (SoftwareTracingShared.IndirectCoordsWS == null) SoftwareTracingShared.IndirectCoordsWS = new HDynamicBuffer(BufferType.ComputeBuffer, 2 * sizeof(uint), HRenderer.TextureXrSlices, avoidDownscale: true);
|
|
if (SoftwareTracingShared.IndirectCoordsOV == null) SoftwareTracingShared.IndirectCoordsOV = new HDynamicBuffer(BufferType.ComputeBuffer, 2 * sizeof(uint), HRenderer.TextureXrSlices, avoidDownscale: true);
|
|
if (SoftwareTracingShared.IndirectCoordsSF == null) SoftwareTracingShared.IndirectCoordsSF = new HDynamicBuffer(BufferType.ComputeBuffer, 2 * sizeof(uint), HRenderer.TextureXrSlices, avoidDownscale: true);
|
|
|
|
SoftwareTracingShared.IndirectCoordsSS.ReAllocIfNeeded(new Vector2Int(width, height));
|
|
SoftwareTracingShared.IndirectCoordsWS.ReAllocIfNeeded(new Vector2Int(width, height));
|
|
SoftwareTracingShared.IndirectCoordsOV.ReAllocIfNeeded(new Vector2Int(width, height));
|
|
SoftwareTracingShared.IndirectCoordsSF.ReAllocIfNeeded(new Vector2Int(width, height));
|
|
}
|
|
|
|
internal static void AllocationHashBuffers(bool onlyRelease = false, bool forceRecreate = false)
|
|
{
|
|
void ReleaseTextures()
|
|
{
|
|
SoftwareTracingShared.HashBuffer_Key.HRelease();
|
|
SoftwareTracingShared.HashBuffer_Payload.HRelease();
|
|
SoftwareTracingShared.HashBuffer_Counter.HRelease();
|
|
SoftwareTracingShared.HashBuffer_Radiance.HRelease();
|
|
SoftwareTracingShared.HashBuffer_Position.HRelease();
|
|
SoftwareTracingShared.HashBuffer_Key = null;
|
|
SoftwareTracingShared.HashBuffer_Payload = null;
|
|
SoftwareTracingShared.HashBuffer_Counter = null;
|
|
SoftwareTracingShared.HashBuffer_Radiance = null;
|
|
SoftwareTracingShared.HashBuffer_Position = null;
|
|
}
|
|
|
|
if (onlyRelease)
|
|
{
|
|
ReleaseTextures();
|
|
return;
|
|
}
|
|
|
|
if (forceRecreate)
|
|
{
|
|
ReleaseTextures();
|
|
}
|
|
|
|
if (SoftwareTracingShared.HashBuffer_Key == null) SoftwareTracingShared.HashBuffer_Key = new ComputeBuffer(HConstants.HASH_STORAGE_SIZE, 1 * sizeof(uint));
|
|
if (SoftwareTracingShared.HashBuffer_Payload == null) SoftwareTracingShared.HashBuffer_Payload = new ComputeBuffer(HConstants.HASH_STORAGE_SIZE / HConstants.HASH_UPDATE_FRACTION, 2 * sizeof(uint));
|
|
if (SoftwareTracingShared.HashBuffer_Counter == null) SoftwareTracingShared.HashBuffer_Counter = new ComputeBuffer(HConstants.HASH_STORAGE_SIZE, 1 * sizeof(uint));
|
|
if (SoftwareTracingShared.HashBuffer_Position == null) SoftwareTracingShared.HashBuffer_Position = new ComputeBuffer(HConstants.HASH_STORAGE_SIZE, 4 * sizeof(uint));
|
|
if (SoftwareTracingShared.HashBuffer_Radiance == null)
|
|
{
|
|
SoftwareTracingShared.HashBuffer_Radiance = new ComputeBuffer(HConstants.HASH_STORAGE_SIZE, 4 * sizeof(uint));
|
|
uint[] zeroArray = new uint[HConstants.HASH_STORAGE_SIZE * 4];
|
|
SoftwareTracingShared.HashBuffer_Radiance.SetData(zeroArray);
|
|
}
|
|
}
|
|
#endif
|
|
#endregion --------------------------- Render Graph ---------------------------
|
|
|
|
#region --------------------------- Shared ---------------------------
|
|
|
|
protected internal void Dispose()
|
|
{
|
|
Allocation(0,0, new RenderTextureDescriptor(), true);
|
|
}
|
|
|
|
#endregion --------------------------- Shared ---------------------------
|
|
}
|
|
}
|