TextureGroupHandle.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. /// Synchronize dependent textures, if any of them have deferred a copy from this texture.
  123. /// </summary>
  124. public void SynchronizeDependents()
  125. {
  126. foreach (TextureDependency dependency in Dependencies)
  127. {
  128. TextureGroupHandle otherHandle = dependency.Other.Handle;
  129. if (otherHandle.DeferredCopy == this)
  130. {
  131. otherHandle._group.Storage.SynchronizeMemory();
  132. }
  133. }
  134. }
  135. /// <summary>
  136. /// Signal that a copy dependent texture has been modified, and must have its data copied to this one.
  137. /// </summary>
  138. /// <param name="copyFrom">The texture handle that must defer a copy to this one</param>
  139. public void DeferCopy(TextureGroupHandle copyFrom)
  140. {
  141. DeferredCopy = copyFrom;
  142. _group.Storage.SignalGroupDirty();
  143. foreach (Texture overlap in Overlaps)
  144. {
  145. overlap.SignalGroupDirty();
  146. }
  147. }
  148. /// <summary>
  149. /// Create a copy dependency between this handle, and another.
  150. /// </summary>
  151. /// <param name="other">The handle to create a copy dependency to</param>
  152. /// <param name="copyToOther">True if a copy should be deferred to all of the other handle's dependencies</param>
  153. public void CreateCopyDependency(TextureGroupHandle other, bool copyToOther = false)
  154. {
  155. // Does this dependency already exist?
  156. foreach (TextureDependency existing in Dependencies)
  157. {
  158. if (existing.Other.Handle == other)
  159. {
  160. // Do not need to create it again. May need to set the dirty flag.
  161. return;
  162. }
  163. }
  164. _group.HasCopyDependencies = true;
  165. other._group.HasCopyDependencies = true;
  166. TextureDependency dependency = new TextureDependency(this);
  167. TextureDependency otherDependency = new TextureDependency(other);
  168. dependency.Other = otherDependency;
  169. otherDependency.Other = dependency;
  170. Dependencies.Add(dependency);
  171. other.Dependencies.Add(otherDependency);
  172. // Recursively create dependency:
  173. // All of this handle's dependencies must depend on the other.
  174. foreach (TextureDependency existing in Dependencies.ToArray())
  175. {
  176. if (existing != dependency && existing.Other.Handle != other)
  177. {
  178. existing.Other.Handle.CreateCopyDependency(other);
  179. }
  180. }
  181. // All of the other handle's dependencies must depend on this.
  182. foreach (TextureDependency existing in other.Dependencies.ToArray())
  183. {
  184. if (existing != otherDependency && existing.Other.Handle != this)
  185. {
  186. existing.Other.Handle.CreateCopyDependency(this);
  187. if (copyToOther)
  188. {
  189. existing.Other.Handle.DeferCopy(this);
  190. }
  191. }
  192. }
  193. }
  194. /// <summary>
  195. /// Remove a dependency from this handle's dependency list.
  196. /// </summary>
  197. /// <param name="dependency">The dependency to remove</param>
  198. public void RemoveDependency(TextureDependency dependency)
  199. {
  200. Dependencies.Remove(dependency);
  201. }
  202. /// <summary>
  203. /// Check if any of this handle's memory tracking handles are dirty.
  204. /// </summary>
  205. /// <returns>True if at least one of the handles is dirty</returns>
  206. private bool CheckDirty()
  207. {
  208. return Handles.Any(handle => handle.Dirty);
  209. }
  210. /// <summary>
  211. /// Perform a copy from the provided handle to this one, or perform a deferred copy if none is provided.
  212. /// </summary>
  213. /// <param name="fromHandle">The handle to copy from. If not provided, this method will copy from and clear the deferred copy instead</param>
  214. /// <returns>True if the copy was performed, false otherwise</returns>
  215. public bool Copy(TextureGroupHandle fromHandle = null)
  216. {
  217. bool result = false;
  218. if (fromHandle == null)
  219. {
  220. fromHandle = DeferredCopy;
  221. if (fromHandle != null && fromHandle._bindCount == 0)
  222. {
  223. // Repeat the copy in future if the bind count is greater than 0.
  224. DeferredCopy = null;
  225. }
  226. }
  227. if (fromHandle != null)
  228. {
  229. // If the copy texture is dirty, do not copy. Its data no longer matters, and this handle should also be dirty.
  230. if (!fromHandle.CheckDirty())
  231. {
  232. Texture from = fromHandle._group.Storage;
  233. Texture to = _group.Storage;
  234. if (from.ScaleFactor != to.ScaleFactor)
  235. {
  236. to.PropagateScale(from);
  237. }
  238. from.HostTexture.CopyTo(
  239. to.HostTexture,
  240. fromHandle._firstLayer,
  241. _firstLayer,
  242. fromHandle._firstLevel,
  243. _firstLevel);
  244. Modified = true;
  245. _group.RegisterAction(this);
  246. result = true;
  247. }
  248. }
  249. return result;
  250. }
  251. /// <summary>
  252. /// Inherit modified flags and dependencies from another texture handle.
  253. /// </summary>
  254. /// <param name="old">The texture handle to inherit from</param>
  255. public void Inherit(TextureGroupHandle old)
  256. {
  257. Modified |= old.Modified;
  258. foreach (TextureDependency dependency in old.Dependencies.ToArray())
  259. {
  260. CreateCopyDependency(dependency.Other.Handle);
  261. if (dependency.Other.Handle.DeferredCopy == old)
  262. {
  263. dependency.Other.Handle.DeferredCopy = this;
  264. }
  265. }
  266. DeferredCopy = old.DeferredCopy;
  267. }
  268. /// <summary>
  269. /// Check if this region overlaps with another.
  270. /// </summary>
  271. /// <param name="address">Base address</param>
  272. /// <param name="size">Size of the region</param>
  273. /// <returns>True if overlapping, false otherwise</returns>
  274. public bool OverlapsWith(int offset, int size)
  275. {
  276. return Offset < offset + size && offset < Offset + Size;
  277. }
  278. public void Dispose()
  279. {
  280. foreach (CpuRegionHandle handle in Handles)
  281. {
  282. handle.Dispose();
  283. }
  284. foreach (TextureDependency dependency in Dependencies.ToArray())
  285. {
  286. dependency.Other.Handle.RemoveDependency(dependency.Other);
  287. }
  288. }
  289. }
  290. }