TextureGroupHandle.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. using Ryujinx.Cpu.Tracking;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace Ryujinx.Graphics.Gpu.Image
  6. {
  7. /// <summary>
  8. /// A tracking handle for a texture group, which represents a range of views in a storage texture.
  9. /// Retains a list of overlapping texture views, a modified flag, and tracking for each
  10. /// CPU VA range that the views cover.
  11. /// Also tracks copy dependencies for the handle - references to other handles that must be kept
  12. /// in sync with this one before use.
  13. /// </summary>
  14. class TextureGroupHandle : IDisposable
  15. {
  16. private TextureGroup _group;
  17. private int _bindCount;
  18. private int _firstLevel;
  19. private int _firstLayer;
  20. /// <summary>
  21. /// The byte offset from the start of the storage of this handle.
  22. /// </summary>
  23. public int Offset { get; }
  24. /// <summary>
  25. /// The size in bytes covered by this handle.
  26. /// </summary>
  27. public int Size { get; }
  28. /// <summary>
  29. /// The textures which this handle overlaps with.
  30. /// </summary>
  31. public List<Texture> Overlaps { get; }
  32. /// <summary>
  33. /// The CPU memory tracking handles that cover this handle.
  34. /// </summary>
  35. public CpuRegionHandle[] Handles { get; }
  36. /// <summary>
  37. /// True if a texture overlapping this handle has been modified. Is set false when the flush action is called.
  38. /// </summary>
  39. public bool Modified { get; set; }
  40. /// <summary>
  41. /// Dependencies to handles from other texture groups.
  42. /// </summary>
  43. public List<TextureDependency> Dependencies { get; }
  44. /// <summary>
  45. /// A flag indicating that a copy is required from one of the dependencies.
  46. /// </summary>
  47. public bool NeedsCopy => DeferredCopy != null;
  48. /// <summary>
  49. /// A data copy that must be acknowledged the next time this handle is used.
  50. /// </summary>
  51. public TextureGroupHandle DeferredCopy { get; set; }
  52. /// <summary>
  53. /// Create a new texture group handle, representing a range of views in a storage texture.
  54. /// </summary>
  55. /// <param name="group">The TextureGroup that the handle belongs to</param>
  56. /// <param name="offset">The byte offset from the start of the storage of the handle</param>
  57. /// <param name="size">The size in bytes covered by the handle</param>
  58. /// <param name="views">All views of the storage texture, used to calculate overlaps</param>
  59. /// <param name="firstLayer">The first layer of this handle in the storage texture</param>
  60. /// <param name="firstLevel">The first level of this handle in the storage texture</param>
  61. /// <param name="handles">The memory tracking handles that cover this handle</param>
  62. public TextureGroupHandle(TextureGroup group, int offset, ulong size, List<Texture> views, int firstLayer, int firstLevel, CpuRegionHandle[] handles)
  63. {
  64. _group = group;
  65. _firstLayer = firstLayer;
  66. _firstLevel = firstLevel;
  67. Offset = offset;
  68. Size = (int)size;
  69. Overlaps = new List<Texture>();
  70. Dependencies = new List<TextureDependency>();
  71. if (views != null)
  72. {
  73. RecalculateOverlaps(group, views);
  74. }
  75. Handles = handles;
  76. }
  77. /// <summary>
  78. /// Calculate a list of which views overlap this handle.
  79. /// </summary>
  80. /// <param name="group">The parent texture group, used to find a view's base CPU VA offset</param>
  81. /// <param name="views">The list of views to search for overlaps</param>
  82. public void RecalculateOverlaps(TextureGroup group, List<Texture> views)
  83. {
  84. // Overlaps can be accessed from the memory tracking signal handler, so access must be atomic.
  85. lock (Overlaps)
  86. {
  87. int endOffset = Offset + Size;
  88. Overlaps.Clear();
  89. foreach (Texture view in views)
  90. {
  91. int viewOffset = group.FindOffset(view);
  92. if (viewOffset < endOffset && Offset < viewOffset + (int)view.Size)
  93. {
  94. Overlaps.Add(view);
  95. }
  96. }
  97. }
  98. }
  99. /// <summary>
  100. /// Signal that this handle has been modified to any existing dependencies, and set the modified flag.
  101. /// </summary>
  102. public void SignalModified()
  103. {
  104. Modified = true;
  105. // If this handle has any copy dependencies, notify the other handle that a copy needs to be performed.
  106. foreach (TextureDependency dependency in Dependencies)
  107. {
  108. dependency.SignalModified();
  109. }
  110. }
  111. /// <summary>
  112. /// Signal that this handle has either started or ended being modified.
  113. /// </summary>
  114. /// <param name="bound">True if this handle is being bound, false if unbound</param>
  115. public void SignalModifying(bool bound)
  116. {
  117. SignalModified();
  118. // Note: Bind count currently resets to 0 on inherit for safety, as the handle <-> view relationship can change.
  119. _bindCount = Math.Max(0, _bindCount + (bound ? 1 : -1));
  120. }
  121. /// <summary>
  122. /// Signal that a copy dependent texture has been modified, and must have its data copied to this one.
  123. /// </summary>
  124. /// <param name="copyFrom">The texture handle that must defer a copy to this one</param>
  125. public void DeferCopy(TextureGroupHandle copyFrom)
  126. {
  127. DeferredCopy = copyFrom;
  128. _group.Storage.SignalGroupDirty();
  129. foreach (Texture overlap in Overlaps)
  130. {
  131. overlap.SignalGroupDirty();
  132. }
  133. }
  134. /// <summary>
  135. /// Create a copy dependency between this handle, and another.
  136. /// </summary>
  137. /// <param name="other">The handle to create a copy dependency to</param>
  138. /// <param name="copyToOther">True if a copy should be deferred to all of the other handle's dependencies</param>
  139. public void CreateCopyDependency(TextureGroupHandle other, bool copyToOther = false)
  140. {
  141. // Does this dependency already exist?
  142. foreach (TextureDependency existing in Dependencies)
  143. {
  144. if (existing.Other.Handle == other)
  145. {
  146. // Do not need to create it again. May need to set the dirty flag.
  147. return;
  148. }
  149. }
  150. _group.HasCopyDependencies = true;
  151. other._group.HasCopyDependencies = true;
  152. TextureDependency dependency = new TextureDependency(this);
  153. TextureDependency otherDependency = new TextureDependency(other);
  154. dependency.Other = otherDependency;
  155. otherDependency.Other = dependency;
  156. Dependencies.Add(dependency);
  157. other.Dependencies.Add(otherDependency);
  158. // Recursively create dependency:
  159. // All of this handle's dependencies must depend on the other.
  160. foreach (TextureDependency existing in Dependencies.ToArray())
  161. {
  162. if (existing != dependency && existing.Other.Handle != other)
  163. {
  164. existing.Other.Handle.CreateCopyDependency(other);
  165. }
  166. }
  167. // All of the other handle's dependencies must depend on this.
  168. foreach (TextureDependency existing in other.Dependencies.ToArray())
  169. {
  170. if (existing != otherDependency && existing.Other.Handle != this)
  171. {
  172. existing.Other.Handle.CreateCopyDependency(this);
  173. if (copyToOther)
  174. {
  175. existing.Other.Handle.DeferCopy(this);
  176. }
  177. }
  178. }
  179. }
  180. /// <summary>
  181. /// Remove a dependency from this handle's dependency list.
  182. /// </summary>
  183. /// <param name="dependency">The dependency to remove</param>
  184. public void RemoveDependency(TextureDependency dependency)
  185. {
  186. Dependencies.Remove(dependency);
  187. }
  188. /// <summary>
  189. /// Check if any of this handle's memory tracking handles are dirty.
  190. /// </summary>
  191. /// <returns>True if at least one of the handles is dirty</returns>
  192. private bool CheckDirty()
  193. {
  194. return Handles.Any(handle => handle.Dirty);
  195. }
  196. /// <summary>
  197. /// Perform a copy from the provided handle to this one, or perform a deferred copy if none is provided.
  198. /// </summary>
  199. /// <param name="fromHandle">The handle to copy from. If not provided, this method will copy from and clear the deferred copy instead</param>
  200. /// <returns>True if the copy was performed, false otherwise</returns>
  201. public bool Copy(TextureGroupHandle fromHandle = null)
  202. {
  203. bool result = false;
  204. if (fromHandle == null)
  205. {
  206. fromHandle = DeferredCopy;
  207. if (fromHandle != null && fromHandle._bindCount == 0)
  208. {
  209. // Repeat the copy in future if the bind count is greater than 0.
  210. DeferredCopy = null;
  211. }
  212. }
  213. if (fromHandle != null)
  214. {
  215. // If the copy texture is dirty, do not copy. Its data no longer matters, and this handle should also be dirty.
  216. if (!fromHandle.CheckDirty())
  217. {
  218. Texture from = fromHandle._group.Storage;
  219. Texture to = _group.Storage;
  220. if (from.ScaleFactor != to.ScaleFactor)
  221. {
  222. to.PropagateScale(from);
  223. }
  224. from.HostTexture.CopyTo(
  225. to.HostTexture,
  226. fromHandle._firstLayer,
  227. _firstLayer,
  228. fromHandle._firstLevel,
  229. _firstLevel);
  230. Modified = true;
  231. _group.RegisterAction(this);
  232. result = true;
  233. }
  234. }
  235. return result;
  236. }
  237. /// <summary>
  238. /// Inherit modified flags and dependencies from another texture handle.
  239. /// </summary>
  240. /// <param name="old">The texture handle to inherit from</param>
  241. public void Inherit(TextureGroupHandle old)
  242. {
  243. Modified |= old.Modified;
  244. foreach (TextureDependency dependency in old.Dependencies.ToArray())
  245. {
  246. CreateCopyDependency(dependency.Other.Handle);
  247. if (dependency.Other.Handle.DeferredCopy == old)
  248. {
  249. dependency.Other.Handle.DeferredCopy = this;
  250. }
  251. }
  252. DeferredCopy = old.DeferredCopy;
  253. }
  254. /// <summary>
  255. /// Check if this region overlaps with another.
  256. /// </summary>
  257. /// <param name="address">Base address</param>
  258. /// <param name="size">Size of the region</param>
  259. /// <returns>True if overlapping, false otherwise</returns>
  260. public bool OverlapsWith(int offset, int size)
  261. {
  262. return Offset < offset + size && offset < Offset + Size;
  263. }
  264. public void Dispose()
  265. {
  266. foreach (CpuRegionHandle handle in Handles)
  267. {
  268. handle.Dispose();
  269. }
  270. foreach (TextureDependency dependency in Dependencies.ToArray())
  271. {
  272. dependency.Other.Handle.RemoveDependency(dependency.Other);
  273. }
  274. }
  275. }
  276. }