summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/PostEffect/RenderTextureManager.cs
blob: 24291e2976256ea5afb5deab20e3fd31c4d174e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
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<T>
        				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<RenderTexture> allRenderTextures = null;
		private List<RenderTexture> 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<RenderTexture>();
		
			if (null == availableRenderTextures)
				availableRenderTextures = new List<RenderTexture>();

            //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( "<color=cyan>ReleaseRenderTexture: " + RenderTexToString(_tex) + "</color>" );
		
			if (null == _tex || null == availableRenderTextures)
				return null;
		
			if ( availableRenderTextures.Contains( _tex ) ) {
	//			Debug.Log( "<color=red>Already available</color>" );
				return null;
			}
			
			availableRenderTextures.Add( _tex );
			
			return null;
		}
		
	    /// <summary>
	    /// Releases a, and then assigns b to a (a = b).
	    /// </summary>
	    /// <param name="a"></param>
	    /// <param name="b"></param>
	    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("<color=blue>MakeRenderTextureNonAvailable</color>: " + 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("<color=green>CreateNewTexture: " + RenderTexToString(newTexture) + "</color>");
			
			return newTexture;
		}
		
		public void PrintRenderTextureStats() {
		    string resString = "<color=blue>availableRenderTextures: </color>" + 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 = "<color=green>allRenderTextures:</color>" + 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("<color=red>RT not released: " + RenderTexToString(rt) + "</color>" );
                    ReleaseRenderTexture(rt);
                }            
			}
		}
	
	    public void PrintBalance()
	    {
	        Debug.Log( "RenderTextures balance: " + (allRenderTextures.Count - availableRenderTextures.Count) + "/" + allRenderTextures.Count );
	    }
		
		public void Dispose() {
	//        Debug.Log("<color=red>Dispose</color>");
			
			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();
			}
		}
	}
}