TextureGroup.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. using Ryujinx.Cpu.Tracking;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.Gpu.Memory;
  4. using Ryujinx.Graphics.Texture;
  5. using Ryujinx.Memory.Range;
  6. using System;
  7. using System.Collections.Generic;
  8. namespace Ryujinx.Graphics.Gpu.Image
  9. {
  10. /// <summary>
  11. /// A texture group represents a group of textures that belong to the same storage.
  12. /// When views are created, this class will track memory accesses for them separately.
  13. /// The group iteratively adds more granular tracking as views of different kinds are added.
  14. /// Note that a texture group can be absorbed into another when it becomes a view parent.
  15. /// </summary>
  16. class TextureGroup : IDisposable
  17. {
  18. private delegate void HandlesCallbackDelegate(int baseHandle, int regionCount, bool split = false);
  19. /// <summary>
  20. /// The storage texture associated with this group.
  21. /// </summary>
  22. public Texture Storage { get; }
  23. /// <summary>
  24. /// Indicates if the texture has copy dependencies. If true, then all modifications
  25. /// must be signalled to the group, rather than skipping ones still to be flushed.
  26. /// </summary>
  27. public bool HasCopyDependencies { get; set; }
  28. private readonly GpuContext _context;
  29. private readonly PhysicalMemory _physicalMemory;
  30. private int[] _allOffsets;
  31. private int[] _sliceSizes;
  32. private bool _is3D;
  33. private bool _hasMipViews;
  34. private bool _hasLayerViews;
  35. private int _layers;
  36. private int _levels;
  37. private MultiRange TextureRange => Storage.Range;
  38. /// <summary>
  39. /// The views list from the storage texture.
  40. /// </summary>
  41. private List<Texture> _views;
  42. private TextureGroupHandle[] _handles;
  43. private bool[] _loadNeeded;
  44. /// <summary>
  45. /// Create a new texture group.
  46. /// </summary>
  47. /// <param name="context">GPU context that the texture group belongs to</param>
  48. /// <param name="physicalMemory">Physical memory where the <paramref name="storage"/> texture is mapped</param>
  49. /// <param name="storage">The storage texture for this group</param>
  50. public TextureGroup(GpuContext context, PhysicalMemory physicalMemory, Texture storage)
  51. {
  52. Storage = storage;
  53. _context = context;
  54. _physicalMemory = physicalMemory;
  55. _is3D = storage.Info.Target == Target.Texture3D;
  56. _layers = storage.Info.GetSlices();
  57. _levels = storage.Info.Levels;
  58. }
  59. /// <summary>
  60. /// Initialize a new texture group's dirty regions and offsets.
  61. /// </summary>
  62. /// <param name="size">Size info for the storage texture</param>
  63. /// <param name="hasLayerViews">True if the storage will have layer views</param>
  64. /// <param name="hasMipViews">True if the storage will have mip views</param>
  65. public void Initialize(ref SizeInfo size, bool hasLayerViews, bool hasMipViews)
  66. {
  67. _allOffsets = size.AllOffsets;
  68. _sliceSizes = size.SliceSizes;
  69. (_hasLayerViews, _hasMipViews) = PropagateGranularity(hasLayerViews, hasMipViews);
  70. RecalculateHandleRegions();
  71. }
  72. /// <summary>
  73. /// Consume the dirty flags for a given texture. The state is shared between views of the same layers and levels.
  74. /// </summary>
  75. /// <param name="texture">The texture being used</param>
  76. /// <returns>True if a flag was dirty, false otherwise</returns>
  77. public bool ConsumeDirty(Texture texture)
  78. {
  79. bool dirty = false;
  80. EvaluateRelevantHandles(texture, (baseHandle, regionCount, split) =>
  81. {
  82. for (int i = 0; i < regionCount; i++)
  83. {
  84. TextureGroupHandle group = _handles[baseHandle + i];
  85. foreach (CpuRegionHandle handle in group.Handles)
  86. {
  87. if (handle.Dirty)
  88. {
  89. handle.Reprotect();
  90. dirty = true;
  91. }
  92. }
  93. }
  94. });
  95. return dirty;
  96. }
  97. /// <summary>
  98. /// Synchronize memory for a given texture.
  99. /// If overlapping tracking handles are dirty, fully or partially synchronize the texture data.
  100. /// </summary>
  101. /// <param name="texture">The texture being used</param>
  102. public void SynchronizeMemory(Texture texture)
  103. {
  104. EvaluateRelevantHandles(texture, (baseHandle, regionCount, split) =>
  105. {
  106. bool dirty = false;
  107. bool anyModified = false;
  108. bool anyUnmapped = false;
  109. for (int i = 0; i < regionCount; i++)
  110. {
  111. TextureGroupHandle group = _handles[baseHandle + i];
  112. bool modified = group.Modified;
  113. bool handleDirty = false;
  114. bool handleModified = false;
  115. bool handleUnmapped = false;
  116. foreach (CpuRegionHandle handle in group.Handles)
  117. {
  118. if (handle.Dirty)
  119. {
  120. handle.Reprotect();
  121. handleDirty = true;
  122. }
  123. else
  124. {
  125. handleUnmapped |= handle.Unmapped;
  126. handleModified |= modified;
  127. }
  128. }
  129. // Evaluate if any copy dependencies need to be fulfilled. A few rules:
  130. // If the copy handle needs to be synchronized, prefer our own state.
  131. // If we need to be synchronized and there is a copy present, prefer the copy.
  132. if (group.NeedsCopy && group.Copy())
  133. {
  134. anyModified |= true; // The copy target has been modified.
  135. handleDirty = false;
  136. }
  137. else
  138. {
  139. anyModified |= handleModified;
  140. dirty |= handleDirty;
  141. }
  142. anyUnmapped |= handleUnmapped;
  143. if (group.NeedsCopy)
  144. {
  145. // The texture we copied from is still being written to. Copy from it again the next time this texture is used.
  146. texture.SignalGroupDirty();
  147. }
  148. _loadNeeded[baseHandle + i] = handleDirty && !handleUnmapped;
  149. }
  150. if (dirty)
  151. {
  152. if (anyUnmapped || (_handles.Length > 1 && (anyModified || split)))
  153. {
  154. // Partial texture invalidation. Only update the layers/levels with dirty flags of the storage.
  155. SynchronizePartial(baseHandle, regionCount);
  156. }
  157. else
  158. {
  159. // Full texture invalidation.
  160. texture.SynchronizeFull();
  161. }
  162. }
  163. });
  164. }
  165. /// <summary>
  166. /// Synchronize part of the storage texture, represented by a given range of handles.
  167. /// Only handles marked by the _loadNeeded array will be synchronized.
  168. /// </summary>
  169. /// <param name="baseHandle">The base index of the range of handles</param>
  170. /// <param name="regionCount">The number of handles to synchronize</param>
  171. private void SynchronizePartial(int baseHandle, int regionCount)
  172. {
  173. for (int i = 0; i < regionCount; i++)
  174. {
  175. if (_loadNeeded[baseHandle + i])
  176. {
  177. var info = GetHandleInformation(baseHandle + i);
  178. int offsetIndex = info.Index;
  179. // Only one of these will be greater than 1, as partial sync is only called when there are sub-image views.
  180. for (int layer = 0; layer < info.Layers; layer++)
  181. {
  182. for (int level = 0; level < info.Levels; level++)
  183. {
  184. int offset = _allOffsets[offsetIndex];
  185. int endOffset = (offsetIndex + 1 == _allOffsets.Length) ? (int)Storage.Size : _allOffsets[offsetIndex + 1];
  186. int size = endOffset - offset;
  187. ReadOnlySpan<byte> data = _physicalMemory.GetSpan(Storage.Range.GetSlice((ulong)offset, (ulong)size));
  188. data = Storage.ConvertToHostCompatibleFormat(data, info.BaseLevel, true);
  189. Storage.SetData(data, info.BaseLayer, info.BaseLevel);
  190. offsetIndex++;
  191. }
  192. }
  193. }
  194. }
  195. }
  196. /// <summary>
  197. /// Signal that a texture in the group has been modified by the GPU.
  198. /// </summary>
  199. /// <param name="texture">The texture that has been modified</param>
  200. /// <param name="registerAction">True if the flushing read action should be registered, false otherwise</param>
  201. public void SignalModified(Texture texture, bool registerAction)
  202. {
  203. EvaluateRelevantHandles(texture, (baseHandle, regionCount, split) =>
  204. {
  205. for (int i = 0; i < regionCount; i++)
  206. {
  207. TextureGroupHandle group = _handles[baseHandle + i];
  208. group.SignalModified();
  209. if (registerAction)
  210. {
  211. RegisterAction(group);
  212. }
  213. }
  214. });
  215. }
  216. /// <summary>
  217. /// Signal that a texture in the group is actively bound, or has been unbound by the GPU.
  218. /// </summary>
  219. /// <param name="texture">The texture that has been modified</param>
  220. /// <param name="bound">True if this texture is being bound, false if unbound</param>
  221. /// <param name="registerAction">True if the flushing read action should be registered, false otherwise</param>
  222. public void SignalModifying(Texture texture, bool bound, bool registerAction)
  223. {
  224. EvaluateRelevantHandles(texture, (baseHandle, regionCount, split) =>
  225. {
  226. for (int i = 0; i < regionCount; i++)
  227. {
  228. TextureGroupHandle group = _handles[baseHandle + i];
  229. group.SignalModifying(bound);
  230. if (registerAction)
  231. {
  232. RegisterAction(group);
  233. }
  234. }
  235. });
  236. }
  237. /// <summary>
  238. /// Register a read/write action to flush for a texture group.
  239. /// </summary>
  240. /// <param name="group">The group to register an action for</param>
  241. public void RegisterAction(TextureGroupHandle group)
  242. {
  243. foreach (CpuRegionHandle handle in group.Handles)
  244. {
  245. handle.RegisterAction((address, size) => FlushAction(group, address, size));
  246. }
  247. }
  248. /// <summary>
  249. /// Propagates the mip/layer view flags depending on the texture type.
  250. /// When the most granular type of subresource has views, the other type of subresource must be segmented granularly too.
  251. /// </summary>
  252. /// <param name="hasLayerViews">True if the storage has layer views</param>
  253. /// <param name="hasMipViews">True if the storage has mip views</param>
  254. /// <returns>The input values after propagation</returns>
  255. private (bool HasLayerViews, bool HasMipViews) PropagateGranularity(bool hasLayerViews, bool hasMipViews)
  256. {
  257. if (_is3D)
  258. {
  259. hasMipViews |= hasLayerViews;
  260. }
  261. else
  262. {
  263. hasLayerViews |= hasMipViews;
  264. }
  265. return (hasLayerViews, hasMipViews);
  266. }
  267. /// <summary>
  268. /// Evaluate the range of tracking handles which a view texture overlaps with.
  269. /// </summary>
  270. /// <param name="texture">The texture to get handles for</param>
  271. /// <param name="callback">
  272. /// A function to be called with the base index of the range of handles for the given texture, and the number of handles it covers.
  273. /// This can be called for multiple disjoint ranges, if required.
  274. /// </param>
  275. private void EvaluateRelevantHandles(Texture texture, HandlesCallbackDelegate callback)
  276. {
  277. if (texture == Storage || !(_hasMipViews || _hasLayerViews))
  278. {
  279. callback(0, _handles.Length);
  280. return;
  281. }
  282. EvaluateRelevantHandles(texture.FirstLayer, texture.FirstLevel, texture.Info.GetSlices(), texture.Info.Levels, callback);
  283. }
  284. /// <summary>
  285. /// Evaluate the range of tracking handles which a view texture overlaps with,
  286. /// using the view's position and slice/level counts.
  287. /// </summary>
  288. /// <param name="firstLayer">The first layer of the texture</param>
  289. /// <param name="firstLevel">The first level of the texture</param>
  290. /// <param name="slices">The slice count of the texture</param>
  291. /// <param name="levels">The level count of the texture</param>
  292. /// <param name="callback">
  293. /// A function to be called with the base index of the range of handles for the given texture, and the number of handles it covers.
  294. /// This can be called for multiple disjoint ranges, if required.
  295. /// </param>
  296. private void EvaluateRelevantHandles(int firstLayer, int firstLevel, int slices, int levels, HandlesCallbackDelegate callback)
  297. {
  298. int targetLayerHandles = _hasLayerViews ? slices : 1;
  299. int targetLevelHandles = _hasMipViews ? levels : 1;
  300. if (_is3D)
  301. {
  302. // Future mip levels come after all layers of the last mip level. Each mipmap has less layers (depth) than the last.
  303. if (!_hasLayerViews)
  304. {
  305. // When there are no layer views, the mips are at a consistent offset.
  306. callback(firstLevel, targetLevelHandles);
  307. }
  308. else
  309. {
  310. (int levelIndex, int layerCount) = Get3DLevelRange(firstLevel);
  311. if (levels > 1 && slices < _layers)
  312. {
  313. // The given texture only covers some of the depth of multiple mips. (a "depth slice")
  314. // Callback with each mip's range separately.
  315. // Can assume that the group is fully subdivided (both slices and levels > 1 for storage)
  316. while (levels-- > 1)
  317. {
  318. callback(firstLayer + levelIndex, slices);
  319. levelIndex += layerCount;
  320. layerCount = Math.Max(layerCount >> 1, 1);
  321. slices = Math.Max(layerCount >> 1, 1);
  322. }
  323. }
  324. else
  325. {
  326. int totalSize = Math.Min(layerCount, slices);
  327. while (levels-- > 1)
  328. {
  329. layerCount = Math.Max(layerCount >> 1, 1);
  330. totalSize += layerCount;
  331. }
  332. callback(firstLayer + levelIndex, totalSize);
  333. }
  334. }
  335. }
  336. else
  337. {
  338. // Future layers come after all mipmaps of the last.
  339. int levelHandles = _hasMipViews ? _levels : 1;
  340. if (slices > 1 && levels < _levels)
  341. {
  342. // The given texture only covers some of the mipmaps of multiple slices. (a "mip slice")
  343. // Callback with each layer's range separately.
  344. // Can assume that the group is fully subdivided (both slices and levels > 1 for storage)
  345. for (int i = 0; i < slices; i++)
  346. {
  347. callback(firstLevel + (firstLayer + i) * levelHandles, targetLevelHandles, true);
  348. }
  349. }
  350. else
  351. {
  352. callback(firstLevel + firstLayer * levelHandles, targetLevelHandles + (targetLayerHandles - 1) * levelHandles);
  353. }
  354. }
  355. }
  356. /// <summary>
  357. /// Get the range of offsets for a given mip level of a 3D texture.
  358. /// </summary>
  359. /// <param name="level">The level to return</param>
  360. /// <returns>Start index and count of offsets for the given level</returns>
  361. private (int Index, int Count) Get3DLevelRange(int level)
  362. {
  363. int index = 0;
  364. int count = _layers; // Depth. Halves with each mip level.
  365. while (level-- > 0)
  366. {
  367. index += count;
  368. count = Math.Max(count >> 1, 1);
  369. }
  370. return (index, count);
  371. }
  372. /// <summary>
  373. /// Get view information for a single tracking handle.
  374. /// </summary>
  375. /// <param name="handleIndex">The index of the handle</param>
  376. /// <returns>The layers and levels that the handle covers, and its index in the offsets array</returns>
  377. private (int BaseLayer, int BaseLevel, int Levels, int Layers, int Index) GetHandleInformation(int handleIndex)
  378. {
  379. int baseLayer;
  380. int baseLevel;
  381. int levels = _hasMipViews ? 1 : _levels;
  382. int layers = _hasLayerViews ? 1 : _layers;
  383. int index;
  384. if (_is3D)
  385. {
  386. if (_hasLayerViews)
  387. {
  388. // NOTE: Will also have mip views, or only one level in storage.
  389. index = handleIndex;
  390. baseLevel = 0;
  391. int layerLevels = _levels;
  392. while (handleIndex >= layerLevels)
  393. {
  394. handleIndex -= layerLevels;
  395. baseLevel++;
  396. layerLevels = Math.Max(layerLevels >> 1, 1);
  397. }
  398. baseLayer = handleIndex;
  399. }
  400. else
  401. {
  402. baseLayer = 0;
  403. baseLevel = handleIndex;
  404. (index, _) = Get3DLevelRange(baseLevel);
  405. }
  406. }
  407. else
  408. {
  409. baseLevel = _hasMipViews ? handleIndex % _levels : 0;
  410. baseLayer = _hasMipViews ? handleIndex / _levels : handleIndex;
  411. index = baseLevel + baseLayer * _levels;
  412. }
  413. return (baseLayer, baseLevel, levels, layers, index);
  414. }
  415. /// <summary>
  416. /// Gets the layer and level for a given view.
  417. /// </summary>
  418. /// <param name="index">The index of the view</param>
  419. /// <returns>The layer and level of the specified view</returns>
  420. private (int BaseLayer, int BaseLevel) GetLayerLevelForView(int index)
  421. {
  422. if (_is3D)
  423. {
  424. int baseLevel = 0;
  425. int layerLevels = _layers;
  426. while (index >= layerLevels)
  427. {
  428. index -= layerLevels;
  429. baseLevel++;
  430. layerLevels = Math.Max(layerLevels >> 1, 1);
  431. }
  432. return (index, baseLevel);
  433. }
  434. else
  435. {
  436. return (index / _levels, index % _levels);
  437. }
  438. }
  439. /// <summary>
  440. /// Find the byte offset of a given texture relative to the storage.
  441. /// </summary>
  442. /// <param name="texture">The texture to locate</param>
  443. /// <returns>The offset of the texture in bytes</returns>
  444. public int FindOffset(Texture texture)
  445. {
  446. return _allOffsets[GetOffsetIndex(texture.FirstLayer, texture.FirstLevel)];
  447. }
  448. /// <summary>
  449. /// Find the offset index of a given layer and level.
  450. /// </summary>
  451. /// <param name="layer">The view layer</param>
  452. /// <param name="level">The view level</param>
  453. /// <returns>The offset index of the given layer and level</returns>
  454. public int GetOffsetIndex(int layer, int level)
  455. {
  456. if (_is3D)
  457. {
  458. return layer + Get3DLevelRange(level).Index;
  459. }
  460. else
  461. {
  462. return level + layer * _levels;
  463. }
  464. }
  465. /// <summary>
  466. /// The action to perform when a memory tracking handle is flipped to dirty.
  467. /// This notifies overlapping textures that the memory needs to be synchronized.
  468. /// </summary>
  469. /// <param name="groupHandle">The handle that a dirty flag was set on</param>
  470. private void DirtyAction(TextureGroupHandle groupHandle)
  471. {
  472. // Notify all textures that belong to this handle.
  473. Storage.SignalGroupDirty();
  474. lock (groupHandle.Overlaps)
  475. {
  476. foreach (Texture overlap in groupHandle.Overlaps)
  477. {
  478. overlap.SignalGroupDirty();
  479. }
  480. }
  481. }
  482. /// <summary>
  483. /// Generate a CpuRegionHandle for a given address and size range in CPU VA.
  484. /// </summary>
  485. /// <param name="address">The start address of the tracked region</param>
  486. /// <param name="size">The size of the tracked region</param>
  487. /// <returns>A CpuRegionHandle covering the given range</returns>
  488. private CpuRegionHandle GenerateHandle(ulong address, ulong size)
  489. {
  490. return _physicalMemory.BeginTracking(address, size);
  491. }
  492. /// <summary>
  493. /// Generate a TextureGroupHandle covering a specified range of views.
  494. /// </summary>
  495. /// <param name="viewStart">The start view of the handle</param>
  496. /// <param name="views">The number of views to cover</param>
  497. /// <returns>A TextureGroupHandle covering the given views</returns>
  498. private TextureGroupHandle GenerateHandles(int viewStart, int views)
  499. {
  500. int offset = _allOffsets[viewStart];
  501. int endOffset = (viewStart + views == _allOffsets.Length) ? (int)Storage.Size : _allOffsets[viewStart + views];
  502. int size = endOffset - offset;
  503. var result = new List<CpuRegionHandle>();
  504. for (int i = 0; i < TextureRange.Count; i++)
  505. {
  506. MemoryRange item = TextureRange.GetSubRange(i);
  507. int subRangeSize = (int)item.Size;
  508. int sliceStart = Math.Clamp(offset, 0, subRangeSize);
  509. int sliceEnd = Math.Clamp(endOffset, 0, subRangeSize);
  510. if (sliceStart != sliceEnd)
  511. {
  512. result.Add(GenerateHandle(item.Address + (ulong)sliceStart, (ulong)(sliceEnd - sliceStart)));
  513. }
  514. offset -= subRangeSize;
  515. endOffset -= subRangeSize;
  516. if (endOffset <= 0)
  517. {
  518. break;
  519. }
  520. }
  521. (int firstLayer, int firstLevel) = GetLayerLevelForView(viewStart);
  522. if (_hasLayerViews && _hasMipViews)
  523. {
  524. size = _sliceSizes[firstLevel];
  525. }
  526. var groupHandle = new TextureGroupHandle(this, _allOffsets[viewStart], (ulong)size, _views, firstLayer, firstLevel, result.ToArray());
  527. foreach (CpuRegionHandle handle in result)
  528. {
  529. handle.RegisterDirtyEvent(() => DirtyAction(groupHandle));
  530. }
  531. return groupHandle;
  532. }
  533. /// <summary>
  534. /// Update the views in this texture group, rebuilding the memory tracking if required.
  535. /// </summary>
  536. /// <param name="views">The views list of the storage texture</param>
  537. public void UpdateViews(List<Texture> views)
  538. {
  539. // This is saved to calculate overlapping views for each handle.
  540. _views = views;
  541. bool layerViews = _hasLayerViews;
  542. bool mipViews = _hasMipViews;
  543. bool regionsRebuilt = false;
  544. if (!(layerViews && mipViews))
  545. {
  546. foreach (Texture view in views)
  547. {
  548. if (view.Info.GetSlices() < _layers)
  549. {
  550. layerViews = true;
  551. }
  552. if (view.Info.Levels < _levels)
  553. {
  554. mipViews = true;
  555. }
  556. }
  557. (layerViews, mipViews) = PropagateGranularity(layerViews, mipViews);
  558. if (layerViews != _hasLayerViews || mipViews != _hasMipViews)
  559. {
  560. _hasLayerViews = layerViews;
  561. _hasMipViews = mipViews;
  562. RecalculateHandleRegions();
  563. regionsRebuilt = true;
  564. }
  565. }
  566. if (!regionsRebuilt)
  567. {
  568. // Must update the overlapping views on all handles, but only if they were not just recreated.
  569. foreach (TextureGroupHandle handle in _handles)
  570. {
  571. handle.RecalculateOverlaps(this, views);
  572. }
  573. }
  574. Storage.SignalGroupDirty();
  575. foreach (Texture texture in views)
  576. {
  577. texture.SignalGroupDirty();
  578. }
  579. }
  580. /// <summary>
  581. /// Inherit handle state from an old set of handles, such as modified and dirty flags.
  582. /// </summary>
  583. /// <param name="oldHandles">The set of handles to inherit state from</param>
  584. /// <param name="handles">The set of handles inheriting the state</param>
  585. private void InheritHandles(TextureGroupHandle[] oldHandles, TextureGroupHandle[] handles)
  586. {
  587. foreach (var group in handles)
  588. {
  589. foreach (var handle in group.Handles)
  590. {
  591. bool dirty = false;
  592. foreach (var oldGroup in oldHandles)
  593. {
  594. if (group.OverlapsWith(oldGroup.Offset, oldGroup.Size))
  595. {
  596. foreach (var oldHandle in oldGroup.Handles)
  597. {
  598. if (handle.OverlapsWith(oldHandle.Address, oldHandle.Size))
  599. {
  600. dirty |= oldHandle.Dirty;
  601. }
  602. }
  603. group.Inherit(oldGroup);
  604. }
  605. }
  606. if (dirty && !handle.Dirty)
  607. {
  608. handle.Reprotect(true);
  609. }
  610. if (group.Modified)
  611. {
  612. handle.RegisterAction((address, size) => FlushAction(group, address, size));
  613. }
  614. }
  615. }
  616. }
  617. /// <summary>
  618. /// Inherit state from another texture group.
  619. /// </summary>
  620. /// <param name="other">The texture group to inherit from</param>
  621. public void Inherit(TextureGroup other)
  622. {
  623. bool layerViews = _hasLayerViews || other._hasLayerViews;
  624. bool mipViews = _hasMipViews || other._hasMipViews;
  625. if (layerViews != _hasLayerViews || mipViews != _hasMipViews)
  626. {
  627. _hasLayerViews = layerViews;
  628. _hasMipViews = mipViews;
  629. RecalculateHandleRegions();
  630. }
  631. InheritHandles(other._handles, _handles);
  632. }
  633. /// <summary>
  634. /// Replace the current handles with the new handles. It is assumed that the new handles start dirty.
  635. /// The dirty flags from the previous handles will be kept.
  636. /// </summary>
  637. /// <param name="handles">The handles to replace the current handles with</param>
  638. private void ReplaceHandles(TextureGroupHandle[] handles)
  639. {
  640. if (_handles != null)
  641. {
  642. // When replacing handles, they should start as non-dirty.
  643. foreach (TextureGroupHandle groupHandle in handles)
  644. {
  645. foreach (CpuRegionHandle handle in groupHandle.Handles)
  646. {
  647. handle.Reprotect();
  648. }
  649. }
  650. InheritHandles(_handles, handles);
  651. foreach (var oldGroup in _handles)
  652. {
  653. foreach (var oldHandle in oldGroup.Handles)
  654. {
  655. oldHandle.Dispose();
  656. }
  657. }
  658. }
  659. _handles = handles;
  660. _loadNeeded = new bool[_handles.Length];
  661. }
  662. /// <summary>
  663. /// Recalculate handle regions for this texture group, and inherit existing state into the new handles.
  664. /// </summary>
  665. private void RecalculateHandleRegions()
  666. {
  667. TextureGroupHandle[] handles;
  668. if (!(_hasMipViews || _hasLayerViews))
  669. {
  670. // Single dirty region.
  671. var cpuRegionHandles = new CpuRegionHandle[TextureRange.Count];
  672. for (int i = 0; i < TextureRange.Count; i++)
  673. {
  674. var currentRange = TextureRange.GetSubRange(i);
  675. cpuRegionHandles[i] = GenerateHandle(currentRange.Address, currentRange.Size);
  676. }
  677. var groupHandle = new TextureGroupHandle(this, 0, Storage.Size, _views, 0, 0, cpuRegionHandles);
  678. foreach (CpuRegionHandle handle in cpuRegionHandles)
  679. {
  680. handle.RegisterDirtyEvent(() => DirtyAction(groupHandle));
  681. }
  682. handles = new TextureGroupHandle[] { groupHandle };
  683. }
  684. else
  685. {
  686. // Get views for the host texture.
  687. // It's worth noting that either the texture has layer views or mip views when getting to this point, which simplifies the logic a little.
  688. // Depending on if the texture is 3d, either the mip views imply that layer views are present (2d) or the other way around (3d).
  689. // This is enforced by the way the texture matched as a view, so we don't need to check.
  690. int layerHandles = _hasLayerViews ? _layers : 1;
  691. int levelHandles = _hasMipViews ? _levels : 1;
  692. int handleIndex = 0;
  693. if (_is3D)
  694. {
  695. var handlesList = new List<TextureGroupHandle>();
  696. for (int i = 0; i < levelHandles; i++)
  697. {
  698. for (int j = 0; j < layerHandles; j++)
  699. {
  700. (int viewStart, int views) = Get3DLevelRange(i);
  701. viewStart += j;
  702. views = _hasLayerViews ? 1 : views; // A layer view is also a mip view.
  703. handlesList.Add(GenerateHandles(viewStart, views));
  704. }
  705. layerHandles = Math.Max(1, layerHandles >> 1);
  706. }
  707. handles = handlesList.ToArray();
  708. }
  709. else
  710. {
  711. handles = new TextureGroupHandle[layerHandles * levelHandles];
  712. for (int i = 0; i < layerHandles; i++)
  713. {
  714. for (int j = 0; j < levelHandles; j++)
  715. {
  716. int viewStart = j + i * _levels;
  717. int views = _hasMipViews ? 1 : _levels; // A mip view is also a layer view.
  718. handles[handleIndex++] = GenerateHandles(viewStart, views);
  719. }
  720. }
  721. }
  722. }
  723. ReplaceHandles(handles);
  724. }
  725. /// <summary>
  726. /// Ensure that there is a handle for each potential texture view. Required for copy dependencies to work.
  727. /// </summary>
  728. private void EnsureFullSubdivision()
  729. {
  730. if (!(_hasLayerViews && _hasMipViews))
  731. {
  732. _hasLayerViews = true;
  733. _hasMipViews = true;
  734. RecalculateHandleRegions();
  735. }
  736. }
  737. /// <summary>
  738. /// Create a copy dependency between this texture group, and a texture at a given layer/level offset.
  739. /// </summary>
  740. /// <param name="other">The view compatible texture to create a dependency to</param>
  741. /// <param name="firstLayer">The base layer of the given texture relative to the storage</param>
  742. /// <param name="firstLevel">The base level of the given texture relative to the storage</param>
  743. /// <param name="copyTo">True if this texture is first copied to the given one, false for the opposite direction</param>
  744. public void CreateCopyDependency(Texture other, int firstLayer, int firstLevel, bool copyTo)
  745. {
  746. TextureGroup otherGroup = other.Group;
  747. EnsureFullSubdivision();
  748. otherGroup.EnsureFullSubdivision();
  749. // Get the location of each texture within its storage, so we can find the handles to apply the dependency to.
  750. // This can consist of multiple disjoint regions, for example if this is a mip slice of an array texture.
  751. var targetRange = new List<(int BaseHandle, int RegionCount)>();
  752. var otherRange = new List<(int BaseHandle, int RegionCount)>();
  753. EvaluateRelevantHandles(firstLayer, firstLevel, other.Info.GetSlices(), other.Info.Levels, (baseHandle, regionCount, split) => targetRange.Add((baseHandle, regionCount)));
  754. otherGroup.EvaluateRelevantHandles(other, (baseHandle, regionCount, split) => otherRange.Add((baseHandle, regionCount)));
  755. int targetIndex = 0;
  756. int otherIndex = 0;
  757. (int Handle, int RegionCount) targetRegion = (0, 0);
  758. (int Handle, int RegionCount) otherRegion = (0, 0);
  759. while (true)
  760. {
  761. if (targetRegion.RegionCount == 0)
  762. {
  763. if (targetIndex >= targetRange.Count)
  764. {
  765. break;
  766. }
  767. targetRegion = targetRange[targetIndex++];
  768. }
  769. if (otherRegion.RegionCount == 0)
  770. {
  771. if (otherIndex >= otherRange.Count)
  772. {
  773. break;
  774. }
  775. otherRegion = otherRange[otherIndex++];
  776. }
  777. TextureGroupHandle handle = _handles[targetRegion.Handle++];
  778. TextureGroupHandle otherHandle = other.Group._handles[otherRegion.Handle++];
  779. targetRegion.RegionCount--;
  780. otherRegion.RegionCount--;
  781. handle.CreateCopyDependency(otherHandle, copyTo);
  782. // If "copyTo" is true, this texture must copy to the other.
  783. // Otherwise, it must copy to this texture.
  784. if (copyTo)
  785. {
  786. otherHandle.Copy(handle);
  787. }
  788. else
  789. {
  790. handle.Copy(otherHandle);
  791. }
  792. }
  793. }
  794. /// <summary>
  795. /// A flush has been requested on a tracked region. Find an appropriate view to flush.
  796. /// </summary>
  797. /// <param name="handle">The handle this flush action is for</param>
  798. /// <param name="address">The address of the flushing memory access</param>
  799. /// <param name="size">The size of the flushing memory access</param>
  800. public void FlushAction(TextureGroupHandle handle, ulong address, ulong size)
  801. {
  802. Storage.ExternalFlush(address, size);
  803. lock (handle.Overlaps)
  804. {
  805. foreach (Texture overlap in handle.Overlaps)
  806. {
  807. overlap.ExternalFlush(address, size);
  808. }
  809. }
  810. handle.Modified = false;
  811. }
  812. /// <summary>
  813. /// Dispose this texture group, disposing all related memory tracking handles.
  814. /// </summary>
  815. public void Dispose()
  816. {
  817. foreach (TextureGroupHandle group in _handles)
  818. {
  819. group.Dispose();
  820. }
  821. }
  822. }
  823. }