Texture.cs 21 KB

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