Texture.cs 23 KB

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