Texture.cs 23 KB

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