RenderTextureCache.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class RenderTextureCache
  5. {
  6. public static bool CheckSupportsRenderTextureFormat(RenderTextureFormat tex_format)
  7. {
  8. if (!RenderTextureCache.supports_format_dic_.ContainsKey(tex_format))
  9. {
  10. RenderTextureCache.supports_format_dic_.Add(tex_format, SystemInfo.SupportsRenderTextureFormat(tex_format));
  11. }
  12. return RenderTextureCache.supports_format_dic_[tex_format];
  13. }
  14. public RenderTexture GetTexture(int width, int height, RenderTextureFormat tex_format, KeyValuePair<int, Action<RenderTexture>> recreate_func)
  15. {
  16. if (!this.render_tex_dic_.ContainsKey(width))
  17. {
  18. this.render_tex_dic_.Add(width, new Dictionary<int, List<RenderTextureCache.Data>>());
  19. }
  20. Dictionary<int, List<RenderTextureCache.Data>> dictionary = this.render_tex_dic_[width];
  21. if (!dictionary.ContainsKey(height))
  22. {
  23. dictionary.Add(height, new List<RenderTextureCache.Data>());
  24. }
  25. List<RenderTextureCache.Data> list = dictionary[height];
  26. RenderTextureCache.Data data = null;
  27. for (int i = 0; i < list.Count; i++)
  28. {
  29. if (list[i].status == RenderTextureCache.Data.Status.Free && list[i].tex.format == tex_format)
  30. {
  31. data = list[i];
  32. break;
  33. }
  34. }
  35. if (data == null)
  36. {
  37. data = new RenderTextureCache.Data();
  38. list.Add(data);
  39. data.size = new Vector2((float)width, (float)height);
  40. data.tex = this.CreateTex(width, height, tex_format);
  41. this.render_tex_list_.Add(data);
  42. }
  43. data.recreate_func = recreate_func;
  44. RenderTexture active = RenderTexture.active;
  45. data.tex.Create();
  46. RenderTexture.active = active;
  47. data.status = RenderTextureCache.Data.Status.Use;
  48. return data.tex;
  49. }
  50. public void RemoveTexture(RenderTexture texture)
  51. {
  52. if (texture == null)
  53. {
  54. return;
  55. }
  56. foreach (KeyValuePair<int, Dictionary<int, List<RenderTextureCache.Data>>> keyValuePair in this.render_tex_dic_)
  57. {
  58. Dictionary<int, List<RenderTextureCache.Data>> value = keyValuePair.Value;
  59. foreach (KeyValuePair<int, List<RenderTextureCache.Data>> keyValuePair2 in value)
  60. {
  61. List<RenderTextureCache.Data> value2 = keyValuePair2.Value;
  62. for (int i = 0; i < value2.Count; i++)
  63. {
  64. if (value2[i].tex == texture)
  65. {
  66. value2[i].tex.Release();
  67. value2[i].recreate_func = new KeyValuePair<int, Action<RenderTexture>>(0, null);
  68. value2[i].status = RenderTextureCache.Data.Status.Free;
  69. return;
  70. }
  71. }
  72. }
  73. }
  74. }
  75. public void Update()
  76. {
  77. List<RenderTextureCache.Data> list = null;
  78. for (int i = 0; i < this.render_tex_list_.Count; i++)
  79. {
  80. if (this.render_tex_list_[i].status == RenderTextureCache.Data.Status.Use && !this.render_tex_list_[i].tex.IsCreated())
  81. {
  82. if (list == null)
  83. {
  84. list = new List<RenderTextureCache.Data>();
  85. }
  86. list.Add(this.render_tex_list_[i]);
  87. }
  88. }
  89. if (list != null)
  90. {
  91. SortedDictionary<int, List<RenderTextureCache.Data>> sortedDictionary = new SortedDictionary<int, List<RenderTextureCache.Data>>();
  92. for (int j = 0; j < list.Count; j++)
  93. {
  94. int key = list[j].recreate_func.Key;
  95. if (!sortedDictionary.ContainsKey(key))
  96. {
  97. sortedDictionary.Add(key, new List<RenderTextureCache.Data>());
  98. }
  99. sortedDictionary[key].Add(list[j]);
  100. }
  101. foreach (KeyValuePair<int, List<RenderTextureCache.Data>> keyValuePair in sortedDictionary)
  102. {
  103. List<RenderTextureCache.Data> value = keyValuePair.Value;
  104. for (int k = 0; k < value.Count; k++)
  105. {
  106. RenderTexture active = RenderTexture.active;
  107. value[k].tex.Create();
  108. RenderTexture.active = active;
  109. value[k].recreate_func.Value(value[k].tex);
  110. }
  111. }
  112. }
  113. }
  114. private RenderTexture CreateTex(int width, int height, RenderTextureFormat tex_format)
  115. {
  116. RenderTexture active = RenderTexture.active;
  117. RenderTexture renderTexture = new RenderTexture(width, height, 0, tex_format);
  118. renderTexture.Create();
  119. RenderTexture.active = active;
  120. return renderTexture;
  121. }
  122. private static Dictionary<RenderTextureFormat, bool> supports_format_dic_ = new Dictionary<RenderTextureFormat, bool>();
  123. private Dictionary<int, Dictionary<int, List<RenderTextureCache.Data>>> render_tex_dic_ = new Dictionary<int, Dictionary<int, List<RenderTextureCache.Data>>>();
  124. private List<RenderTextureCache.Data> render_tex_list_ = new List<RenderTextureCache.Data>();
  125. private class Data
  126. {
  127. public RenderTexture tex;
  128. public Vector2 size;
  129. public RenderTextureCache.Data.Status status;
  130. public KeyValuePair<int, Action<RenderTexture>> recreate_func;
  131. public enum Status
  132. {
  133. Free,
  134. Use
  135. }
  136. }
  137. }