From 6eb915c129fc90c6f4c82ae097dd6ffad5239efc Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 25 Jan 2021 14:28:30 +0800 Subject: +scripts --- .../Scripts/PostEffect/RenderTextureManager.cs | 232 +++++++++++++++++++++ 1 file changed, 232 insertions(+) create mode 100644 Client/Assets/Scripts/PostEffect/RenderTextureManager.cs (limited to 'Client/Assets/Scripts/PostEffect/RenderTextureManager.cs') diff --git a/Client/Assets/Scripts/PostEffect/RenderTextureManager.cs b/Client/Assets/Scripts/PostEffect/RenderTextureManager.cs new file mode 100644 index 00000000..24291e29 --- /dev/null +++ b/Client/Assets/Scripts/PostEffect/RenderTextureManager.cs @@ -0,0 +1,232 @@ +#define FXPRO_EFFECT +//#define BLOOMPRO_EFFECT +//#define DOFPRO_EFFECT + +using System; +using UnityEngine; +using System.Collections; +using System.Collections.Generic; + +#if FXPRO_EFFECT +namespace FxProNS { +#elif BLOOMPRO_EFFECT +namespace BloomProNS { +#elif DOFPRO_EFFECT +namespace DOFProNS { +#endif + public enum EffectsQuality + { + High, + Normal, + Fast, + Fastest + } + + public abstract class Singleton + where T : class, new() { + + private static bool Compare( T x, T y ) + { + return x == y; + } + + #region Singleton + + private static T instance = default( T ); + + public static T Instance { + get { + if (Compare( default( T ), instance )) { + instance = new T(); + } + + return instance; + } + } + + #endregion + } + + public class RenderTextureManager : IDisposable + { + private static RenderTextureManager instance; + public static RenderTextureManager Instance + { + get + { + return instance ?? (instance = new RenderTextureManager()); + } + } + + private List allRenderTextures = null; + private List availableRenderTextures = null; + + // public RenderTexture RequestRenderTexture(int _width, int _height, int _depth, RenderTextureFormat _format) { + // return RenderTexture.GetTemporary( _width, _height, _depth, _format ); + // } + // + // public RenderTexture ReleaseRenderTexture( RenderTexture _tex ) { + // RenderTexture.ReleaseTemporary( _tex ); + // + // return null; + // } + + public RenderTexture RequestRenderTexture(int _width, int _height, int _depth, RenderTextureFormat _format) { + if (null == allRenderTextures) + allRenderTextures = new List(); + + if (null == availableRenderTextures) + availableRenderTextures = new List(); + + //First look for an available RenderTexture + RenderTexture tempTex = null; + for (int i = 0, imax = availableRenderTextures.Count; i < imax; ++i) + { + RenderTexture rt = availableRenderTextures[i]; + if (null == rt) + continue; + + if (rt.width == _width && rt.height == _height && rt.depth == _depth && rt.format == _format) + { + tempTex = rt; + } + } + + if (null != tempTex) + { + MakeRenderTextureNonAvailable(tempTex); + + // PrintRenderTextureStats(); + tempTex.DiscardContents(true, true); + return tempTex; + } + + //Create a new texture if it was not found. + tempTex = CreateNewTexture( _width, _height, _depth, _format ); + MakeRenderTextureNonAvailable( tempTex ); + // PrintRenderTextureStats(); + + return tempTex; + } + + public RenderTexture ReleaseRenderTexture( RenderTexture _tex ) { + // Debug.Log( "ReleaseRenderTexture: " + RenderTexToString(_tex) + "" ); + + if (null == _tex || null == availableRenderTextures) + return null; + + if ( availableRenderTextures.Contains( _tex ) ) { + // Debug.Log( "Already available" ); + return null; + } + + availableRenderTextures.Add( _tex ); + + return null; + } + + /// + /// Releases a, and then assigns b to a (a = b). + /// + /// + /// + public void SafeAssign( ref RenderTexture a, RenderTexture b ) + { + if ( a == b ) + return; + + ReleaseRenderTexture( a ); + a = b; + + //return b; + } + + public void MakeRenderTextureNonAvailable ( RenderTexture _tex ) { + // Debug.Log("MakeRenderTextureNonAvailable: " + RenderTexToString( _tex ) ); + if ( availableRenderTextures.Contains (_tex ) ) + availableRenderTextures.Remove( _tex ); + } + + + private RenderTexture CreateNewTexture( int _width, int _height, int _depth, RenderTextureFormat _format ) { + RenderTexture newTexture = new RenderTexture( _width, _height, _depth, _format ); + newTexture.Create(); + newTexture.DiscardContents(true, true); + allRenderTextures.Add( newTexture ); + availableRenderTextures.Add( newTexture ); + + // Debug.Log("CreateNewTexture: " + RenderTexToString(newTexture) + ""); + + return newTexture; + } + + public void PrintRenderTextureStats() { + string resString = "availableRenderTextures: " + availableRenderTextures.Count + "\n"; + for (int i = 0, imax = availableRenderTextures.Count; i < imax; ++i) + { + RenderTexture rt = availableRenderTextures[i]; + resString += "\t" + RenderTexToString(rt) + "\n"; + } + + Debug.Log(resString); + + resString = "allRenderTextures:" + allRenderTextures.Count + "\n"; + for (int i = 0, imax = allRenderTextures.Count; i < imax; ++i) + { + RenderTexture rt = allRenderTextures[i]; + resString += "\t" + RenderTexToString(rt) + "\n"; + } + + Debug.Log(resString); + } + + private string RenderTexToString( RenderTexture _rt ) { + if (null == _rt) + return "null"; + + return _rt.width + " x " + _rt.height + "\t" + _rt.depth + "\t" + _rt.format; + } + + private void PrintRenderTexturesCount(string _prefix = "") { + Debug.Log(_prefix + ": " + (allRenderTextures.Count - availableRenderTextures.Count) + "/" + allRenderTextures.Count); + } + + //Should be called every frame to make sure that we don't hold on to render textures that are no longer used. + public void ReleaseAllRenderTextures() { + if (null == allRenderTextures) + return; + for (int i = 0, imax = allRenderTextures.Count; i < imax; ++i) + { + RenderTexture rt = allRenderTextures[i]; + if (!availableRenderTextures.Contains(rt)) + { + //Debug.Log("RT not released: " + RenderTexToString(rt) + "" ); + ReleaseRenderTexture(rt); + } + } + } + + public void PrintBalance() + { + Debug.Log( "RenderTextures balance: " + (allRenderTextures.Count - availableRenderTextures.Count) + "/" + allRenderTextures.Count ); + } + + public void Dispose() { + // Debug.Log("Dispose"); + + if (null != allRenderTextures) { + for (int i = 0, imax = allRenderTextures.Count; i < imax; ++i) + { + RenderTexture rt = allRenderTextures[i]; + rt.Release(); + } + + allRenderTextures.Clear(); + } + + if (null != availableRenderTextures) { + availableRenderTextures.Clear(); + } + } + } +} \ No newline at end of file -- cgit v1.1-26-g67d0