169 lines
5.0 KiB
C#
169 lines
5.0 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
namespace PhxhSDK
|
||
{
|
||
public class RenderTexturePool : Singlenton<RenderTexturePool>, IInitable
|
||
{
|
||
private const int MAX_PER_POOL_COUNT = 2;
|
||
private const int MAX_USED_COUNT = 4;
|
||
private readonly Dictionary<string, Stack<RenderTexture>> _unusedRenderTexture = new Dictionary<string, Stack<RenderTexture>>(4);
|
||
private readonly List<RenderTexture> _usingRenderTexture = new List<RenderTexture>();
|
||
|
||
public static bool DISABLE_POOL = false;
|
||
|
||
public const int DEFAULT_DEPTH = 32;
|
||
|
||
public int AntiAliasing { get; private set; } = 2;
|
||
|
||
|
||
public void Init()
|
||
{
|
||
}
|
||
|
||
public void Release()
|
||
{
|
||
try
|
||
{
|
||
foreach (var rt in _usingRenderTexture) rt.Release();
|
||
_usingRenderTexture.Clear();
|
||
|
||
ClearUnUsed();
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
DebugUtil.LogError(e);
|
||
}
|
||
}
|
||
|
||
|
||
public RenderTexture Create(string rtName, int width, int height, int depth = DEFAULT_DEPTH, RenderTextureFormat format = RenderTextureFormat.ARGB32)
|
||
{
|
||
RenderTexture rt = null;
|
||
try
|
||
{
|
||
var key = _GenKey(width, height, depth, format);
|
||
Stack<RenderTexture> rts;
|
||
if (_unusedRenderTexture.TryGetValue(key, out rts))
|
||
{
|
||
if (rts.Count > 0)
|
||
{
|
||
rt = rts.Pop();
|
||
}
|
||
}
|
||
|
||
if (rt == null)
|
||
{
|
||
rt = new RenderTexture(width, height, depth)
|
||
{
|
||
format = format,
|
||
antiAliasing = AntiAliasing
|
||
};
|
||
DebugUtil.Log("创建新的RenderTexture: {0}", rtName);
|
||
}
|
||
|
||
rt.name = rtName;
|
||
|
||
_usingRenderTexture.Add(rt);
|
||
if (_usingRenderTexture.Count > MAX_USED_COUNT)
|
||
{
|
||
DebugUtil.LogError($"[RenderTexturePool] 当前正在使用的RenderTexture数量: {_usingRenderTexture.Count} 超过了最大值: {MAX_USED_COUNT},请检查是否有RenderTexture未释放");
|
||
for (int i = 0; i < _usingRenderTexture.Count; i++)
|
||
{
|
||
DebugUtil.LogError($"[RenderTexturePool] RenderTexture: {_usingRenderTexture[i].name}");
|
||
}
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
DebugUtil.LogError(e);
|
||
}
|
||
|
||
return rt;
|
||
}
|
||
|
||
|
||
public void Destroy(RenderTexture rt, bool forceDestory = false)
|
||
{
|
||
if (rt == null) return;
|
||
|
||
if (DISABLE_POOL || forceDestory)
|
||
{
|
||
_usingRenderTexture.Remove(rt);
|
||
rt.Release();
|
||
return;
|
||
}
|
||
|
||
try
|
||
{
|
||
|
||
if (_usingRenderTexture.Remove(rt))
|
||
{
|
||
var key = _GenKey(rt.width, rt.height, rt.depth, rt.format);
|
||
|
||
Stack<RenderTexture> rts;
|
||
if (_unusedRenderTexture.TryGetValue(key, out rts))
|
||
{
|
||
}
|
||
else
|
||
{
|
||
rts = new Stack<RenderTexture>(MAX_PER_POOL_COUNT);
|
||
_unusedRenderTexture.Add(key, rts);
|
||
}
|
||
|
||
if (rts.Count < MAX_PER_POOL_COUNT)
|
||
rts.Push(rt);
|
||
else
|
||
rt.Release();
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
DebugUtil.LogError(e);
|
||
}
|
||
}
|
||
|
||
public void ClearUnUsed()
|
||
{
|
||
try
|
||
{
|
||
foreach (var p in _unusedRenderTexture)
|
||
{
|
||
foreach (var rt in p.Value) rt.Release();
|
||
p.Value.Clear();
|
||
}
|
||
|
||
_unusedRenderTexture.Clear();
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
DebugUtil.LogError(e);
|
||
}
|
||
}
|
||
|
||
public void SetAntiAliasing(int level)
|
||
{
|
||
try
|
||
{
|
||
AntiAliasing = level;
|
||
foreach (var rt in _usingRenderTexture) rt.antiAliasing = level;
|
||
|
||
foreach (var p in _unusedRenderTexture)
|
||
foreach (var rt in p.Value)
|
||
rt.antiAliasing = level;
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
DebugUtil.LogError(e);
|
||
}
|
||
}
|
||
|
||
|
||
private string _GenKey(int width, int height, int depth, RenderTextureFormat format)
|
||
{
|
||
var hdrStr = format == RenderTextureFormat.ARGB32 ? "NoHDR" : "HDR";
|
||
return $"{width}_{height}_{depth}_{hdrStr}";
|
||
}
|
||
}
|
||
} |