Texture.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. using Ryujinx.Common;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Graphics.GAL;
  4. using Ryujinx.Graphics.GAL.Texture;
  5. using Ryujinx.Graphics.Gpu.Memory;
  6. using Ryujinx.Graphics.Texture;
  7. using Ryujinx.Graphics.Texture.Astc;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Diagnostics;
  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. string texInfo = $"{_info.Target} {_info.FormatInfo.Format} {_info.Width}x{_info.Height}x{_info.DepthOrLayers} levels {_info.Levels}";
  204. Logger.PrintError(LogClass.Gpu, $"Invalid ASTC texture at 0x{_info.Address:X} ({texInfo}).");
  205. }
  206. data = decoded;
  207. }
  208. HostTexture.SetData(data);
  209. _hasData = true;
  210. }
  211. public void Flush()
  212. {
  213. Span<byte> data = HostTexture.GetData();
  214. if (_info.IsLinear)
  215. {
  216. data = LayoutConverter.ConvertLinearToLinearStrided(
  217. _info.Width,
  218. _info.Height,
  219. _info.FormatInfo.BlockWidth,
  220. _info.FormatInfo.BlockHeight,
  221. _info.Stride,
  222. _info.FormatInfo.BytesPerPixel,
  223. data);
  224. }
  225. else
  226. {
  227. data = LayoutConverter.ConvertLinearToBlockLinear(
  228. _info.Width,
  229. _info.Height,
  230. _depth,
  231. _info.Levels,
  232. _layers,
  233. _info.FormatInfo.BlockWidth,
  234. _info.FormatInfo.BlockHeight,
  235. _info.FormatInfo.BytesPerPixel,
  236. _info.GobBlocksInY,
  237. _info.GobBlocksInZ,
  238. _info.GobBlocksInTileX,
  239. _sizeInfo,
  240. data);
  241. }
  242. _context.PhysicalMemory.Write(Address, data);
  243. }
  244. public bool IsPerfectMatch(TextureInfo info, TextureSearchFlags flags)
  245. {
  246. if (!FormatMatches(info, (flags & TextureSearchFlags.Strict) != 0))
  247. {
  248. return false;
  249. }
  250. if (!LayoutMatches(info))
  251. {
  252. return false;
  253. }
  254. if (!SizeMatches(info, (flags & TextureSearchFlags.Strict) == 0))
  255. {
  256. return false;
  257. }
  258. if ((flags & TextureSearchFlags.Sampler) != 0)
  259. {
  260. if (!SamplerParamsMatches(info))
  261. {
  262. return false;
  263. }
  264. }
  265. if ((flags & TextureSearchFlags.IgnoreMs) != 0)
  266. {
  267. bool msTargetCompatible = _info.Target == Target.Texture2DMultisample &&
  268. info.Target == Target.Texture2D;
  269. if (!msTargetCompatible && !TargetAndSamplesCompatible(info))
  270. {
  271. return false;
  272. }
  273. }
  274. else if (!TargetAndSamplesCompatible(info))
  275. {
  276. return false;
  277. }
  278. return _info.Address == info.Address && _info.Levels == info.Levels;
  279. }
  280. private bool FormatMatches(TextureInfo info, bool strict)
  281. {
  282. // D32F and R32F texture have the same representation internally,
  283. // however the R32F format is used to sample from depth textures.
  284. if (_info.FormatInfo.Format == Format.D32Float &&
  285. info.FormatInfo.Format == Format.R32Float && !strict)
  286. {
  287. return true;
  288. }
  289. return _info.FormatInfo.Format == info.FormatInfo.Format;
  290. }
  291. private bool LayoutMatches(TextureInfo info)
  292. {
  293. if (_info.IsLinear != info.IsLinear)
  294. {
  295. return false;
  296. }
  297. // For linear textures, gob block sizes are ignored.
  298. // For block linear textures, the stride is ignored.
  299. if (info.IsLinear)
  300. {
  301. return _info.Stride == info.Stride;
  302. }
  303. else
  304. {
  305. return _info.GobBlocksInY == info.GobBlocksInY &&
  306. _info.GobBlocksInZ == info.GobBlocksInZ;
  307. }
  308. }
  309. public bool SizeMatches(TextureInfo info)
  310. {
  311. return SizeMatches(info, alignSizes: false);
  312. }
  313. public bool SizeMatches(TextureInfo info, int level)
  314. {
  315. return Math.Max(1, _info.Width >> level) == info.Width &&
  316. Math.Max(1, _info.Height >> level) == info.Height &&
  317. Math.Max(1, _info.GetDepth() >> level) == info.GetDepth();
  318. }
  319. private bool SizeMatches(TextureInfo info, bool alignSizes)
  320. {
  321. if (_info.GetLayers() != info.GetLayers())
  322. {
  323. return false;
  324. }
  325. if (alignSizes)
  326. {
  327. Size size0 = GetAlignedSize(_info);
  328. Size size1 = GetAlignedSize(info);
  329. return size0.Width == size1.Width &&
  330. size0.Height == size1.Height &&
  331. size0.Depth == size1.Depth;
  332. }
  333. else
  334. {
  335. return _info.Width == info.Width &&
  336. _info.Height == info.Height &&
  337. _info.GetDepth() == info.GetDepth();
  338. }
  339. }
  340. private bool SamplerParamsMatches(TextureInfo info)
  341. {
  342. return _info.DepthStencilMode == info.DepthStencilMode &&
  343. _info.SwizzleR == info.SwizzleR &&
  344. _info.SwizzleG == info.SwizzleG &&
  345. _info.SwizzleB == info.SwizzleB &&
  346. _info.SwizzleA == info.SwizzleA;
  347. }
  348. private bool TargetAndSamplesCompatible(TextureInfo info)
  349. {
  350. return _info.Target == info.Target &&
  351. _info.SamplesInX == info.SamplesInX &&
  352. _info.SamplesInY == info.SamplesInY;
  353. }
  354. public bool IsViewCompatible(
  355. TextureInfo info,
  356. ulong size,
  357. out int firstLayer,
  358. out int firstLevel)
  359. {
  360. return IsViewCompatible(info, size, isCopy: false, out firstLayer, out firstLevel);
  361. }
  362. public bool IsViewCompatible(
  363. TextureInfo info,
  364. ulong size,
  365. bool isCopy,
  366. out int firstLayer,
  367. out int firstLevel)
  368. {
  369. // Out of range.
  370. if (info.Address < Address || info.Address + size > EndAddress)
  371. {
  372. firstLayer = 0;
  373. firstLevel = 0;
  374. return false;
  375. }
  376. int offset = (int)(info.Address - Address);
  377. if (!_sizeInfo.FindView(offset, (int)size, out firstLayer, out firstLevel))
  378. {
  379. return false;
  380. }
  381. if (!ViewLayoutCompatible(info, firstLevel))
  382. {
  383. return false;
  384. }
  385. if (!ViewFormatCompatible(info))
  386. {
  387. return false;
  388. }
  389. if (!ViewSizeMatches(info, firstLevel, isCopy))
  390. {
  391. return false;
  392. }
  393. if (!ViewTargetCompatible(info, isCopy))
  394. {
  395. return false;
  396. }
  397. return _info.SamplesInX == info.SamplesInX &&
  398. _info.SamplesInY == info.SamplesInY;
  399. }
  400. private bool ViewLayoutCompatible(TextureInfo info, int level)
  401. {
  402. if (_info.IsLinear != info.IsLinear)
  403. {
  404. return false;
  405. }
  406. // For linear textures, gob block sizes are ignored.
  407. // For block linear textures, the stride is ignored.
  408. if (info.IsLinear)
  409. {
  410. int width = Math.Max(1, _info.Width >> level);
  411. int stride = width * _info.FormatInfo.BytesPerPixel;
  412. stride = BitUtils.AlignUp(stride, 32);
  413. return stride == info.Stride;
  414. }
  415. else
  416. {
  417. int height = Math.Max(1, _info.Height >> level);
  418. int depth = Math.Max(1, _info.GetDepth() >> level);
  419. (int gobBlocksInY, int gobBlocksInZ) = SizeCalculator.GetMipGobBlockSizes(
  420. height,
  421. depth,
  422. _info.FormatInfo.BlockHeight,
  423. _info.GobBlocksInY,
  424. _info.GobBlocksInZ);
  425. return gobBlocksInY == info.GobBlocksInY &&
  426. gobBlocksInZ == info.GobBlocksInZ;
  427. }
  428. }
  429. private bool ViewFormatCompatible(TextureInfo info)
  430. {
  431. return TextureCompatibility.FormatCompatible(_info.FormatInfo, info.FormatInfo);
  432. }
  433. private bool ViewSizeMatches(TextureInfo info, int level, bool isCopy)
  434. {
  435. Size size = GetAlignedSize(_info, level);
  436. Size otherSize = GetAlignedSize(info);
  437. // For copies, we can copy a subset of the 3D texture slices,
  438. // so the depth may be different in this case.
  439. if (!isCopy && info.Target == Target.Texture3D && size.Depth != otherSize.Depth)
  440. {
  441. return false;
  442. }
  443. return size.Width == otherSize.Width &&
  444. size.Height == otherSize.Height;
  445. }
  446. private bool ViewTargetCompatible(TextureInfo info, bool isCopy)
  447. {
  448. switch (_info.Target)
  449. {
  450. case Target.Texture1D:
  451. case Target.Texture1DArray:
  452. return info.Target == Target.Texture1D ||
  453. info.Target == Target.Texture1DArray;
  454. case Target.Texture2D:
  455. return info.Target == Target.Texture2D ||
  456. info.Target == Target.Texture2DArray;
  457. case Target.Texture2DArray:
  458. case Target.Cubemap:
  459. case Target.CubemapArray:
  460. return info.Target == Target.Texture2D ||
  461. info.Target == Target.Texture2DArray ||
  462. info.Target == Target.Cubemap ||
  463. info.Target == Target.CubemapArray;
  464. case Target.Texture2DMultisample:
  465. case Target.Texture2DMultisampleArray:
  466. return info.Target == Target.Texture2DMultisample ||
  467. info.Target == Target.Texture2DMultisampleArray;
  468. case Target.Texture3D:
  469. return info.Target == Target.Texture3D ||
  470. (info.Target == Target.Texture2D && isCopy);
  471. }
  472. return false;
  473. }
  474. private static Size GetAlignedSize(TextureInfo info, int level = 0)
  475. {
  476. int width = Math.Max(1, info.Width >> level);
  477. int height = Math.Max(1, info.Height >> level);
  478. if (info.IsLinear)
  479. {
  480. return SizeCalculator.GetLinearAlignedSize(
  481. width,
  482. height,
  483. info.FormatInfo.BlockWidth,
  484. info.FormatInfo.BlockHeight,
  485. info.FormatInfo.BytesPerPixel);
  486. }
  487. else
  488. {
  489. int depth = Math.Max(1, info.GetDepth() >> level);
  490. (int gobBlocksInY, int gobBlocksInZ) = SizeCalculator.GetMipGobBlockSizes(
  491. height,
  492. depth,
  493. info.FormatInfo.BlockHeight,
  494. info.GobBlocksInY,
  495. info.GobBlocksInZ);
  496. return SizeCalculator.GetBlockLinearAlignedSize(
  497. width,
  498. height,
  499. depth,
  500. info.FormatInfo.BlockWidth,
  501. info.FormatInfo.BlockHeight,
  502. info.FormatInfo.BytesPerPixel,
  503. gobBlocksInY,
  504. gobBlocksInZ,
  505. info.GobBlocksInTileX);
  506. }
  507. }
  508. public ITexture GetTargetTexture(Target target)
  509. {
  510. if (target == _info.Target)
  511. {
  512. return HostTexture;
  513. }
  514. if (_arrayViewTexture == null && IsSameDimensionsTarget(target))
  515. {
  516. TextureCreateInfo createInfo = new TextureCreateInfo(
  517. _info.Width,
  518. _info.Height,
  519. target == Target.CubemapArray ? 6 : 1,
  520. _info.Levels,
  521. _info.Samples,
  522. _info.FormatInfo.BlockWidth,
  523. _info.FormatInfo.BlockHeight,
  524. _info.FormatInfo.BytesPerPixel,
  525. _info.FormatInfo.Format,
  526. _info.DepthStencilMode,
  527. target,
  528. _info.SwizzleR,
  529. _info.SwizzleG,
  530. _info.SwizzleB,
  531. _info.SwizzleA);
  532. ITexture viewTexture = HostTexture.CreateView(createInfo, 0, 0);
  533. _arrayViewTexture = viewTexture;
  534. _arrayViewTarget = target;
  535. return viewTexture;
  536. }
  537. else if (_arrayViewTarget == target)
  538. {
  539. return _arrayViewTexture;
  540. }
  541. return null;
  542. }
  543. private bool IsSameDimensionsTarget(Target target)
  544. {
  545. switch (_info.Target)
  546. {
  547. case Target.Texture1D:
  548. case Target.Texture1DArray:
  549. return target == Target.Texture1D ||
  550. target == Target.Texture1DArray;
  551. case Target.Texture2D:
  552. case Target.Texture2DArray:
  553. return target == Target.Texture2D ||
  554. target == Target.Texture2DArray;
  555. case Target.Cubemap:
  556. case Target.CubemapArray:
  557. return target == Target.Cubemap ||
  558. target == Target.CubemapArray;
  559. case Target.Texture2DMultisample:
  560. case Target.Texture2DMultisampleArray:
  561. return target == Target.Texture2DMultisample ||
  562. target == Target.Texture2DMultisampleArray;
  563. case Target.Texture3D:
  564. return target == Target.Texture3D;
  565. }
  566. return false;
  567. }
  568. public void ReplaceView(Texture parent, TextureInfo info, ITexture hostTexture)
  569. {
  570. ReplaceStorage(hostTexture);
  571. parent._viewStorage.AddView(this);
  572. SetInfo(info);
  573. }
  574. private void SetInfo(TextureInfo info)
  575. {
  576. _info = info;
  577. _depth = info.GetDepth();
  578. _layers = info.GetLayers();
  579. }
  580. private void ReplaceStorage(ITexture hostTexture)
  581. {
  582. DisposeTextures();
  583. HostTexture = hostTexture;
  584. }
  585. public bool OverlapsWith(ulong address, ulong size)
  586. {
  587. return Address < address + size && address < EndAddress;
  588. }
  589. public void IncrementReferenceCount()
  590. {
  591. _referenceCount++;
  592. }
  593. public void DecrementReferenceCount()
  594. {
  595. int newRefCount = --_referenceCount;
  596. if (newRefCount == 0)
  597. {
  598. if (_viewStorage != this)
  599. {
  600. _viewStorage.RemoveView(this);
  601. }
  602. _context.Methods.TextureManager.RemoveTextureFromCache(this);
  603. }
  604. Debug.Assert(newRefCount >= 0);
  605. DeleteIfNotUsed();
  606. }
  607. private void DeleteIfNotUsed()
  608. {
  609. // We can delete the texture as long it is not being used
  610. // in any cache (the reference count is 0 in this case), and
  611. // also all views that may be created from this texture were
  612. // already deleted (views count is 0).
  613. if (_referenceCount == 0 && _views.Count == 0)
  614. {
  615. DisposeTextures();
  616. }
  617. }
  618. private void DisposeTextures()
  619. {
  620. HostTexture.Dispose();
  621. _arrayViewTexture?.Dispose();
  622. _arrayViewTexture = null;
  623. }
  624. }
  625. }