Texture.cs 21 KB

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