-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexturePool.cs
More file actions
48 lines (39 loc) · 1.41 KB
/
TexturePool.cs
File metadata and controls
48 lines (39 loc) · 1.41 KB
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
using System.Collections.Generic;
using UnityEngine;
namespace TextureFlow
{
class TexturePool
{
Dictionary<uint, Queue<RenderTexture>> _textures = new Dictionary<uint, Queue<RenderTexture>>();
public RenderTexture GetOrAdd(int width, int height)
{
uint hash = ToHash(width, height);
Queue<RenderTexture> list;
if (!_textures.TryGetValue(hash, out list))
_textures.Add(hash, list = new Queue<RenderTexture>());
if (list.Count > 0)
return list.Dequeue();
return new RenderTexture(width, height, 0);
}
public void Release(RenderTexture texture)
{
uint hash = ToHash(texture.width, texture.height);
Queue<RenderTexture> list;
if (!_textures.TryGetValue(hash, out list))
_textures.Add(hash, list = new Queue<RenderTexture>());
list.Enqueue(texture);
}
public void Add(RenderTexture texture)
{
uint hash = ToHash(texture.width, texture.height);
Queue<RenderTexture> list;
if (!_textures.TryGetValue(hash, out list))
_textures.Add(hash, list = new Queue<RenderTexture>());
list.Enqueue(texture);
}
private uint ToHash(int width, int height)
{
return ((uint)width) | ((uint)height) << 16;
}
}
}