Texture.cs 22 KB

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