Files
Fishing2/Packages/com.waveharmonic.crest/Runtime/Scripts/Utility/Shared/Rendering/RenderPipelineCompatibilityHelper.cs
2026-01-31 00:32:49 +08:00

150 lines
7.4 KiB
C#

// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
using UnityEngine;
using UnityEngine.Rendering;
namespace WaveHarmonic.Crest
{
static class RenderPipelineCompatibilityHelper
{
// Taken from:
// https://github.com/Unity-Technologies/Graphics/blob/19ec161f3f752db865597374b3ad1b3eaf110097/Packages/com.unity.render-pipelines.universal/Runtime/RenderingUtils.cs#L588-L634
/// <summary>
/// Return true if handle does not match descriptor
/// </summary>
/// <param name="handle">RTHandle to check (can be null)</param>
/// <param name="descriptor">Descriptor for the RTHandle to match</param>
/// <param name="filterMode">Filtering mode of the RTHandle.</param>
/// <param name="wrapMode">Addressing mode of the RTHandle.</param>
/// <param name="isShadowMap">Set to true if the depth buffer should be used as a shadow map.</param>
/// <param name="anisoLevel">Anisotropic filtering level.</param>
/// <param name="mipMapBias">Bias applied to mipmaps during filtering.</param>
/// <param name="name">Name of the RTHandle.</param>
/// <param name="scaled">Check if the RTHandle has auto scaling enabled if not, check the widths and heights</param>
/// <returns></returns>
internal static bool RTHandleNeedsReAlloc(
RTHandle handle,
in RenderTextureDescriptor descriptor,
FilterMode filterMode,
TextureWrapMode wrapMode,
bool isShadowMap,
int anisoLevel,
float mipMapBias,
string name,
bool scaled)
{
if (handle == null || handle.rt == null)
return true;
if (handle.useScaling != scaled)
return true;
if (!scaled && (handle.rt.width != descriptor.width || handle.rt.height != descriptor.height))
return true;
return
handle.rt.descriptor.depthBufferBits != descriptor.depthBufferBits ||
(handle.rt.descriptor.depthBufferBits == (int)DepthBits.None && !isShadowMap && handle.rt.descriptor.graphicsFormat != descriptor.graphicsFormat) ||
handle.rt.descriptor.dimension != descriptor.dimension ||
handle.rt.descriptor.enableRandomWrite != descriptor.enableRandomWrite ||
handle.rt.descriptor.useMipMap != descriptor.useMipMap ||
handle.rt.descriptor.autoGenerateMips != descriptor.autoGenerateMips ||
handle.rt.descriptor.msaaSamples != descriptor.msaaSamples ||
handle.rt.descriptor.bindMS != descriptor.bindMS ||
handle.rt.descriptor.useDynamicScale != descriptor.useDynamicScale ||
handle.rt.descriptor.memoryless != descriptor.memoryless ||
handle.rt.filterMode != filterMode ||
handle.rt.wrapMode != wrapMode ||
handle.rt.anisoLevel != anisoLevel ||
handle.rt.mipMapBias != mipMapBias ||
handle.name != name;
}
// https://github.com/Unity-Technologies/Graphics/blob/19ec161f3f752db865597374b3ad1b3eaf110097/Packages/com.unity.render-pipelines.universal/Runtime/RenderingUtils.cs#L666-L695
/// <summary>
/// Re-allocate fixed-size RTHandle if it is not allocated or doesn't match the descriptor
/// </summary>
/// <param name="handle">RTHandle to check (can be null)</param>
/// <param name="descriptor">Descriptor for the RTHandle to match</param>
/// <param name="filterMode">Filtering mode of the RTHandle.</param>
/// <param name="wrapMode">Addressing mode of the RTHandle.</param>
/// <param name="isShadowMap">Set to true if the depth buffer should be used as a shadow map.</param>
/// <param name="anisoLevel">Anisotropic filtering level.</param>
/// <param name="mipMapBias">Bias applied to mipmaps during filtering.</param>
/// <param name="name">Name of the RTHandle.</param>
/// <returns></returns>
public static bool ReAllocateIfNeeded(
ref RTHandle handle,
in RenderTextureDescriptor descriptor,
FilterMode filterMode = FilterMode.Point,
TextureWrapMode wrapMode = TextureWrapMode.Repeat,
bool isShadowMap = false,
int anisoLevel = 1,
float mipMapBias = 0,
string name = "")
{
if (RTHandleNeedsReAlloc(handle, descriptor, filterMode, wrapMode, isShadowMap, anisoLevel, mipMapBias, name, false))
{
handle?.Release();
handle = RTHandles.Alloc(descriptor, filterMode, wrapMode, isShadowMap, anisoLevel, mipMapBias, name);
return true;
}
return false;
}
// Modified from:
// https://github.com/Unity-Technologies/Graphics/blob/19ec161f3f752db865597374b3ad1b3eaf110097/Packages/com.unity.render-pipelines.universal/Runtime/RenderingUtils.cs#L697-L729
/// <summary>
/// Re-allocate dynamically resized RTHandle if it is not allocated or doesn't match the descriptor
/// </summary>
/// <param name="handle">RTHandle to check (can be null)</param>
/// <param name="scaleFactor">Constant scale for the RTHandle size computation.</param>
/// <param name="descriptor">Descriptor for the RTHandle to match</param>
/// <param name="filterMode">Filtering mode of the RTHandle.</param>
/// <param name="wrapMode">Addressing mode of the RTHandle.</param>
/// <param name="isShadowMap">Set to true if the depth buffer should be used as a shadow map.</param>
/// <param name="anisoLevel">Anisotropic filtering level.</param>
/// <param name="mipMapBias">Bias applied to mipmaps during filtering.</param>
/// <param name="name">Name of the RTHandle.</param>
/// <returns>If the RTHandle should be re-allocated</returns>
public static bool ReAllocateIfNeeded
(
ref RTHandle handle,
Vector2 scaleFactor,
in RenderTextureDescriptor descriptor,
FilterMode filterMode = FilterMode.Point,
TextureWrapMode wrapMode = TextureWrapMode.Repeat,
bool isShadowMap = false,
int anisoLevel = 1,
float mipMapBias = 0,
string name = ""
)
{
// Only HDRP seems to use this?
if (RenderPipelineHelper.IsHighDefinition)
{
var usingConstantScale = handle != null && handle.useScaling && handle.scaleFactor == scaleFactor;
if (!usingConstantScale || RTHandleNeedsReAlloc(handle, descriptor, filterMode, wrapMode, isShadowMap, anisoLevel, mipMapBias, name, true))
{
handle?.Release();
handle = RTHandles.Alloc(scaleFactor, descriptor, filterMode, wrapMode, isShadowMap, anisoLevel, mipMapBias, name);
return true;
}
}
else
{
if (RTHandleNeedsReAlloc(handle, descriptor, filterMode, wrapMode, isShadowMap, anisoLevel, mipMapBias, name, false))
{
handle?.Release();
handle = RTHandles.Alloc(descriptor, filterMode, wrapMode, isShadowMap, anisoLevel, mipMapBias, name);
return true;
}
}
return false;
}
}
}