Texture.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. using Ryujinx.Common;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.GAL.Texture;
  4. using Ryujinx.Graphics.Gpu.Memory;
  5. using Ryujinx.Graphics.Texture;
  6. using Ryujinx.Graphics.Texture.Astc;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Diagnostics;
  10. using Ryujinx.Common.Logging;
  11. namespace Ryujinx.Graphics.Gpu.Image
  12. {
  13. class Texture : IRange<Texture>
  14. {
  15. private GpuContext _context;
  16. private TextureInfo _info;
  17. private SizeInfo _sizeInfo;
  18. public Format Format => _info.FormatInfo.Format;
  19. public TextureInfo Info => _info;
  20. private int _depth;
  21. private int _layers;
  22. private int _firstLayer;
  23. private int _firstLevel;
  24. private bool _hasData;
  25. private ITexture _arrayViewTexture;
  26. private Target _arrayViewTarget;
  27. private Texture _viewStorage;
  28. private List<Texture> _views;
  29. public ITexture HostTexture { get; private set; }
  30. public LinkedListNode<Texture> CacheNode { get; set; }
  31. public bool Modified { get; set; }
  32. public ulong Address => _info.Address;
  33. public ulong EndAddress => _info.Address + Size;
  34. public ulong Size => (ulong)_sizeInfo.TotalSize;
  35. private int _referenceCount;
  36. private int _sequenceNumber;
  37. private Texture(
  38. GpuContext context,
  39. TextureInfo info,
  40. SizeInfo sizeInfo,
  41. int firstLayer,
  42. int firstLevel)
  43. {
  44. InitializeTexture(context, info, sizeInfo);
  45. _firstLayer = firstLayer;
  46. _firstLevel = firstLevel;
  47. _hasData = true;
  48. }
  49. public Texture(GpuContext context, TextureInfo info, SizeInfo sizeInfo)
  50. {
  51. InitializeTexture(context, info, sizeInfo);
  52. TextureCreateInfo createInfo = TextureManager.GetCreateInfo(info, context.Capabilities);
  53. HostTexture = _context.Renderer.CreateTexture(createInfo);
  54. }
  55. private void InitializeTexture(GpuContext context, TextureInfo info, SizeInfo sizeInfo)
  56. {
  57. _context = context;
  58. _sizeInfo = sizeInfo;
  59. SetInfo(info);
  60. _viewStorage = this;
  61. _views = new List<Texture>();
  62. }
  63. public Texture CreateView(TextureInfo info, SizeInfo sizeInfo, int firstLayer, int firstLevel)
  64. {
  65. Texture texture = new Texture(
  66. _context,
  67. info,
  68. sizeInfo,
  69. _firstLayer + firstLayer,
  70. _firstLevel + firstLevel);
  71. TextureCreateInfo createInfo = TextureManager.GetCreateInfo(info, _context.Capabilities);
  72. texture.HostTexture = HostTexture.CreateView(createInfo, firstLayer, firstLevel);
  73. _viewStorage.AddView(texture);
  74. return texture;
  75. }
  76. private void AddView(Texture texture)
  77. {
  78. _views.Add(texture);
  79. texture._viewStorage = this;
  80. }
  81. private void RemoveView(Texture texture)
  82. {
  83. _views.Remove(texture);
  84. texture._viewStorage = null;
  85. DeleteIfNotUsed();
  86. }
  87. public void ChangeSize(int width, int height, int depthOrLayers)
  88. {
  89. width <<= _firstLevel;
  90. height <<= _firstLevel;
  91. if (_info.Target == Target.Texture3D)
  92. {
  93. depthOrLayers <<= _firstLevel;
  94. }
  95. else
  96. {
  97. depthOrLayers = _viewStorage._info.DepthOrLayers;
  98. }
  99. _viewStorage.RecreateStorageOrView(width, height, depthOrLayers);
  100. foreach (Texture view in _viewStorage._views)
  101. {
  102. int viewWidth = Math.Max(1, width >> view._firstLevel);
  103. int viewHeight = Math.Max(1, height >> view._firstLevel);
  104. int viewDepthOrLayers;
  105. if (view._info.Target == Target.Texture3D)
  106. {
  107. viewDepthOrLayers = Math.Max(1, depthOrLayers >> view._firstLevel);
  108. }
  109. else
  110. {
  111. viewDepthOrLayers = view._info.DepthOrLayers;
  112. }
  113. view.RecreateStorageOrView(viewWidth, viewHeight, viewDepthOrLayers);
  114. }
  115. }
  116. private void RecreateStorageOrView(int width, int height, int depthOrLayers)
  117. {
  118. SetInfo(new TextureInfo(
  119. _info.Address,
  120. width,
  121. height,
  122. depthOrLayers,
  123. _info.Levels,
  124. _info.SamplesInX,
  125. _info.SamplesInY,
  126. _info.Stride,
  127. _info.IsLinear,
  128. _info.GobBlocksInY,
  129. _info.GobBlocksInZ,
  130. _info.GobBlocksInTileX,
  131. _info.Target,
  132. _info.FormatInfo,
  133. _info.DepthStencilMode,
  134. _info.SwizzleR,
  135. _info.SwizzleG,
  136. _info.SwizzleB,
  137. _info.SwizzleA));
  138. TextureCreateInfo createInfo = TextureManager.GetCreateInfo(_info, _context.Capabilities);
  139. if (_viewStorage != this)
  140. {
  141. ReplaceStorage(_viewStorage.HostTexture.CreateView(createInfo, _firstLayer, _firstLevel));
  142. }
  143. else
  144. {
  145. ITexture newStorage = _context.Renderer.CreateTexture(createInfo);
  146. HostTexture.CopyTo(newStorage, 0, 0);
  147. ReplaceStorage(newStorage);
  148. }
  149. }
  150. public void SynchronizeMemory()
  151. {
  152. if (_sequenceNumber == _context.SequenceNumber && _hasData)
  153. {
  154. return;
  155. }
  156. _sequenceNumber = _context.SequenceNumber;
  157. bool modified = _context.PhysicalMemory.GetModifiedRanges(Address, Size, ResourceName.Texture).Length != 0;
  158. if (!modified && _hasData)
  159. {
  160. return;
  161. }
  162. Span<byte> data = _context.PhysicalMemory.Read(Address, Size);
  163. if (_info.IsLinear)
  164. {
  165. data = LayoutConverter.ConvertLinearStridedToLinear(
  166. _info.Width,
  167. _info.Height,
  168. _info.FormatInfo.BlockWidth,
  169. _info.FormatInfo.BlockHeight,
  170. _info.Stride,
  171. _info.FormatInfo.BytesPerPixel,
  172. data);
  173. }
  174. else
  175. {
  176. data = LayoutConverter.ConvertBlockLinearToLinear(
  177. _info.Width,
  178. _info.Height,
  179. _depth,
  180. _info.Levels,
  181. _layers,
  182. _info.FormatInfo.BlockWidth,
  183. _info.FormatInfo.BlockHeight,
  184. _info.FormatInfo.BytesPerPixel,
  185. _info.GobBlocksInY,
  186. _info.GobBlocksInZ,
  187. _info.GobBlocksInTileX,
  188. _sizeInfo,
  189. data);
  190. }
  191. if (!_context.Capabilities.SupportsAstcCompression && _info.FormatInfo.Format.IsAstc())
  192. {
  193. if (!AstcDecoder.TryDecodeToRgba8(
  194. data.ToArray(),
  195. _info.FormatInfo.BlockWidth,
  196. _info.FormatInfo.BlockHeight,
  197. _info.Width,
  198. _info.Height,
  199. _depth,
  200. _info.Levels,
  201. out Span<byte> decoded))
  202. {
  203. // TODO: Error.
  204. }
  205. data = decoded;
  206. }
  207. HostTexture.SetData(data);
  208. _hasData = true;
  209. }
  210. public void Flush()
  211. {
  212. Span<byte> data = HostTexture.GetData();
  213. if (_info.IsLinear)
  214. {
  215. data = LayoutConverter.ConvertLinearToLinearStrided(
  216. _info.Width,
  217. _info.Height,
  218. _info.FormatInfo.BlockWidth,
  219. _info.FormatInfo.BlockHeight,
  220. _info.Stride,
  221. _info.FormatInfo.BytesPerPixel,
  222. data);
  223. }
  224. else
  225. {
  226. data = LayoutConverter.ConvertLinearToBlockLinear(
  227. _info.Width,
  228. _info.Height,
  229. _depth,
  230. _info.Levels,
  231. _layers,
  232. _info.FormatInfo.BlockWidth,
  233. _info.FormatInfo.BlockHeight,
  234. _info.FormatInfo.BytesPerPixel,
  235. _info.GobBlocksInY,
  236. _info.GobBlocksInZ,
  237. _info.GobBlocksInTileX,
  238. _sizeInfo,
  239. data);
  240. }
  241. _context.PhysicalMemory.Write(Address, data);
  242. }
  243. public bool IsPerfectMatch(TextureInfo info, TextureSearchFlags flags)
  244. {
  245. if (!FormatMatches(info, (flags & TextureSearchFlags.Strict) != 0))
  246. {
  247. return false;
  248. }
  249. if (!LayoutMatches(info))
  250. {
  251. return false;
  252. }
  253. if (!SizeMatches(info, (flags & TextureSearchFlags.Strict) == 0))
  254. {
  255. return false;
  256. }
  257. if ((flags & TextureSearchFlags.Sampler) != 0)
  258. {
  259. if (!SamplerParamsMatches(info))
  260. {
  261. return false;
  262. }
  263. }
  264. if ((flags & TextureSearchFlags.IgnoreMs) != 0)
  265. {
  266. bool msTargetCompatible = _info.Target == Target.Texture2DMultisample &&
  267. info.Target == Target.Texture2D;
  268. if (!msTargetCompatible && !TargetAndSamplesCompatible(info))
  269. {
  270. return false;
  271. }
  272. }
  273. else if (!TargetAndSamplesCompatible(info))
  274. {
  275. return false;
  276. }
  277. return _info.Address == info.Address && _info.Levels == info.Levels;
  278. }
  279. private bool FormatMatches(TextureInfo info, bool strict)
  280. {
  281. // D32F and R32F texture have the same representation internally,
  282. // however the R32F format is used to sample from depth textures.
  283. if (_info.FormatInfo.Format == Format.D32Float &&
  284. info.FormatInfo.Format == Format.R32Float && !strict)
  285. {
  286. return true;
  287. }
  288. return _info.FormatInfo.Format == info.FormatInfo.Format;
  289. }
  290. private bool LayoutMatches(TextureInfo info)
  291. {
  292. if (_info.IsLinear != info.IsLinear)
  293. {
  294. return false;
  295. }
  296. // For linear textures, gob block sizes are ignored.
  297. // For block linear textures, the stride is ignored.
  298. if (info.IsLinear)
  299. {
  300. return _info.Stride == info.Stride;
  301. }
  302. else
  303. {
  304. return _info.GobBlocksInY == info.GobBlocksInY &&
  305. _info.GobBlocksInZ == info.GobBlocksInZ;
  306. }
  307. }
  308. public bool SizeMatches(TextureInfo info)
  309. {
  310. return SizeMatches(info, alignSizes: false);
  311. }
  312. public bool SizeMatches(TextureInfo info, int level)
  313. {
  314. return Math.Max(1, _info.Width >> level) == info.Width &&
  315. Math.Max(1, _info.Height >> level) == info.Height &&
  316. Math.Max(1, _info.GetDepth() >> level) == info.GetDepth();
  317. }
  318. private bool SizeMatches(TextureInfo info, bool alignSizes)
  319. {
  320. if (_info.GetLayers() != info.GetLayers())
  321. {
  322. return false;
  323. }
  324. if (alignSizes)
  325. {
  326. Size size0 = GetAlignedSize(_info);
  327. Size size1 = GetAlignedSize(info);
  328. return size0.Width == size1.Width &&
  329. size0.Height == size1.Height &&
  330. size0.Depth == size1.Depth;
  331. }
  332. else
  333. {
  334. return _info.Width == info.Width &&
  335. _info.Height == info.Height &&
  336. _info.GetDepth() == info.GetDepth();
  337. }
  338. }
  339. private bool SamplerParamsMatches(TextureInfo info)
  340. {
  341. return _info.DepthStencilMode == info.DepthStencilMode &&
  342. _info.SwizzleR == info.SwizzleR &&
  343. _info.SwizzleG == info.SwizzleG &&
  344. _info.SwizzleB == info.SwizzleB &&
  345. _info.SwizzleA == info.SwizzleA;
  346. }
  347. private bool TargetAndSamplesCompatible(TextureInfo info)
  348. {
  349. return _info.Target == info.Target &&
  350. _info.SamplesInX == info.SamplesInX &&
  351. _info.SamplesInY == info.SamplesInY;
  352. }
  353. public bool IsViewCompatible(
  354. TextureInfo info,
  355. ulong size,
  356. out int firstLayer,
  357. out int firstLevel)
  358. {
  359. return IsViewCompatible(info, size, isCopy: false, out firstLayer, out firstLevel);
  360. }
  361. public bool IsViewCompatible(
  362. TextureInfo info,
  363. ulong size,
  364. bool isCopy,
  365. out int firstLayer,
  366. out int firstLevel)
  367. {
  368. // Out of range.
  369. if (info.Address < Address || info.Address + size > EndAddress)
  370. {
  371. firstLayer = 0;
  372. firstLevel = 0;
  373. return false;
  374. }
  375. int offset = (int)(info.Address - Address);
  376. if (!_sizeInfo.FindView(offset, (int)size, out firstLayer, out firstLevel))
  377. {
  378. return false;
  379. }
  380. if (!ViewLayoutCompatible(info, firstLevel))
  381. {
  382. return false;
  383. }
  384. if (!ViewFormatCompatible(info))
  385. {
  386. return false;
  387. }
  388. if (!ViewSizeMatches(info, firstLevel, isCopy))
  389. {
  390. return false;
  391. }
  392. if (!ViewTargetCompatible(info, isCopy))
  393. {
  394. return false;
  395. }
  396. return _info.SamplesInX == info.SamplesInX &&
  397. _info.SamplesInY == info.SamplesInY;
  398. }
  399. private bool ViewLayoutCompatible(TextureInfo info, int level)
  400. {
  401. if (_info.IsLinear != info.IsLinear)
  402. {
  403. return false;
  404. }
  405. // For linear textures, gob block sizes are ignored.
  406. // For block linear textures, the stride is ignored.
  407. if (info.IsLinear)
  408. {
  409. int width = Math.Max(1, _info.Width >> level);
  410. int stride = width * _info.FormatInfo.BytesPerPixel;
  411. stride = BitUtils.AlignUp(stride, 32);
  412. return stride == info.Stride;
  413. }
  414. else
  415. {
  416. int height = Math.Max(1, _info.Height >> level);
  417. int depth = Math.Max(1, _info.GetDepth() >> level);
  418. (int gobBlocksInY, int gobBlocksInZ) = SizeCalculator.GetMipGobBlockSizes(
  419. height,
  420. depth,
  421. _info.FormatInfo.BlockHeight,
  422. _info.GobBlocksInY,
  423. _info.GobBlocksInZ);
  424. return gobBlocksInY == info.GobBlocksInY &&
  425. gobBlocksInZ == info.GobBlocksInZ;
  426. }
  427. }
  428. private bool ViewFormatCompatible(TextureInfo info)
  429. {
  430. return TextureCompatibility.FormatCompatible(_info.FormatInfo, info.FormatInfo);
  431. }
  432. private bool ViewSizeMatches(TextureInfo info, int level, bool isCopy)
  433. {
  434. Size size = GetAlignedSize(_info, level);
  435. Size otherSize = GetAlignedSize(info);
  436. // For copies, we can copy a subset of the 3D texture slices,
  437. // so the depth may be different in this case.
  438. if (!isCopy && info.Target == Target.Texture3D && size.Depth != otherSize.Depth)
  439. {
  440. return false;
  441. }
  442. return size.Width == otherSize.Width &&
  443. size.Height == otherSize.Height;
  444. }
  445. private bool ViewTargetCompatible(TextureInfo info, bool isCopy)
  446. {
  447. switch (_info.Target)
  448. {
  449. case Target.Texture1D:
  450. case Target.Texture1DArray:
  451. return info.Target == Target.Texture1D ||
  452. info.Target == Target.Texture1DArray;
  453. case Target.Texture2D:
  454. return info.Target == Target.Texture2D ||
  455. info.Target == Target.Texture2DArray;
  456. case Target.Texture2DArray:
  457. case Target.Cubemap:
  458. case Target.CubemapArray:
  459. return info.Target == Target.Texture2D ||
  460. info.Target == Target.Texture2DArray ||
  461. info.Target == Target.Cubemap ||
  462. info.Target == Target.CubemapArray;
  463. case Target.Texture2DMultisample:
  464. case Target.Texture2DMultisampleArray:
  465. return info.Target == Target.Texture2DMultisample ||
  466. info.Target == Target.Texture2DMultisampleArray;
  467. case Target.Texture3D:
  468. return info.Target == Target.Texture3D ||
  469. (info.Target == Target.Texture2D && isCopy);
  470. }
  471. return false;
  472. }
  473. private static Size GetAlignedSize(TextureInfo info, int level = 0)
  474. {
  475. int width = Math.Max(1, info.Width >> level);
  476. int height = Math.Max(1, info.Height >> level);
  477. if (info.IsLinear)
  478. {
  479. return SizeCalculator.GetLinearAlignedSize(
  480. width,
  481. height,
  482. info.FormatInfo.BlockWidth,
  483. info.FormatInfo.BlockHeight,
  484. info.FormatInfo.BytesPerPixel);
  485. }
  486. else
  487. {
  488. int depth = Math.Max(1, info.GetDepth() >> level);
  489. (int gobBlocksInY, int gobBlocksInZ) = SizeCalculator.GetMipGobBlockSizes(
  490. height,
  491. depth,
  492. info.FormatInfo.BlockHeight,
  493. info.GobBlocksInY,
  494. info.GobBlocksInZ);
  495. return SizeCalculator.GetBlockLinearAlignedSize(
  496. width,
  497. height,
  498. depth,
  499. info.FormatInfo.BlockWidth,
  500. info.FormatInfo.BlockHeight,
  501. info.FormatInfo.BytesPerPixel,
  502. gobBlocksInY,
  503. gobBlocksInZ,
  504. info.GobBlocksInTileX);
  505. }
  506. }
  507. public ITexture GetTargetTexture(Target target)
  508. {
  509. if (target == _info.Target)
  510. {
  511. return HostTexture;
  512. }
  513. if (_arrayViewTexture == null && IsSameDimensionsTarget(target))
  514. {
  515. TextureCreateInfo createInfo = new TextureCreateInfo(
  516. _info.Width,
  517. _info.Height,
  518. target == Target.CubemapArray ? 6 : 1,
  519. _info.Levels,
  520. _info.Samples,
  521. _info.FormatInfo.BlockWidth,
  522. _info.FormatInfo.BlockHeight,
  523. _info.FormatInfo.BytesPerPixel,
  524. _info.FormatInfo.Format,
  525. _info.DepthStencilMode,
  526. target,
  527. _info.SwizzleR,
  528. _info.SwizzleG,
  529. _info.SwizzleB,
  530. _info.SwizzleA);
  531. ITexture viewTexture = HostTexture.CreateView(createInfo, 0, 0);
  532. _arrayViewTexture = viewTexture;
  533. _arrayViewTarget = target;
  534. return viewTexture;
  535. }
  536. else if (_arrayViewTarget == target)
  537. {
  538. return _arrayViewTexture;
  539. }
  540. return null;
  541. }
  542. private bool IsSameDimensionsTarget(Target target)
  543. {
  544. switch (_info.Target)
  545. {
  546. case Target.Texture1D:
  547. case Target.Texture1DArray:
  548. return target == Target.Texture1D ||
  549. target == Target.Texture1DArray;
  550. case Target.Texture2D:
  551. case Target.Texture2DArray:
  552. return target == Target.Texture2D ||
  553. target == Target.Texture2DArray;
  554. case Target.Cubemap:
  555. case Target.CubemapArray:
  556. return target == Target.Cubemap ||
  557. target == Target.CubemapArray;
  558. case Target.Texture2DMultisample:
  559. case Target.Texture2DMultisampleArray:
  560. return target == Target.Texture2DMultisample ||
  561. target == Target.Texture2DMultisampleArray;
  562. case Target.Texture3D:
  563. return target == Target.Texture3D;
  564. }
  565. return false;
  566. }
  567. public void ReplaceView(Texture parent, TextureInfo info, ITexture hostTexture)
  568. {
  569. ReplaceStorage(hostTexture);
  570. parent._viewStorage.AddView(this);
  571. SetInfo(info);
  572. }
  573. private void SetInfo(TextureInfo info)
  574. {
  575. _info = info;
  576. _depth = info.GetDepth();
  577. _layers = info.GetLayers();
  578. }
  579. private void ReplaceStorage(ITexture hostTexture)
  580. {
  581. DisposeTextures();
  582. HostTexture = hostTexture;
  583. }
  584. public bool OverlapsWith(ulong address, ulong size)
  585. {
  586. return Address < address + size && address < EndAddress;
  587. }
  588. public void IncrementReferenceCount()
  589. {
  590. _referenceCount++;
  591. }
  592. public void DecrementReferenceCount()
  593. {
  594. int newRefCount = --_referenceCount;
  595. if (newRefCount == 0)
  596. {
  597. if (_viewStorage != this)
  598. {
  599. _viewStorage.RemoveView(this);
  600. }
  601. _context.Methods.TextureManager.RemoveTextureFromCache(this);
  602. }
  603. Debug.Assert(newRefCount >= 0);
  604. DeleteIfNotUsed();
  605. }
  606. private void DeleteIfNotUsed()
  607. {
  608. // We can delete the texture as long it is not being used
  609. // in any cache (the reference count is 0 in this case), and
  610. // also all views that may be created from this texture were
  611. // already deleted (views count is 0).
  612. if (_referenceCount == 0 && _views.Count == 0)
  613. {
  614. DisposeTextures();
  615. }
  616. }
  617. private void DisposeTextures()
  618. {
  619. HostTexture.Dispose();
  620. _arrayViewTexture?.Dispose();
  621. _arrayViewTexture = null;
  622. }
  623. }
  624. }