TextureGroup.cs 38 KB

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