TextureView.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Graphics.GAL;
  3. using System;
  4. namespace Ryujinx.Graphics.OpenGL.Image
  5. {
  6. class TextureView : TextureBase, ITexture
  7. {
  8. private readonly Renderer _renderer;
  9. private readonly TextureStorage _parent;
  10. private TextureView _emulatedViewParent;
  11. private TextureView _incompatibleFormatView;
  12. public int FirstLayer { get; private set; }
  13. public int FirstLevel { get; private set; }
  14. public TextureView(
  15. Renderer renderer,
  16. TextureStorage parent,
  17. TextureCreateInfo info,
  18. int firstLayer,
  19. int firstLevel) : base(info, parent.ScaleFactor)
  20. {
  21. _renderer = renderer;
  22. _parent = parent;
  23. FirstLayer = firstLayer;
  24. FirstLevel = firstLevel;
  25. CreateView();
  26. }
  27. private void CreateView()
  28. {
  29. TextureTarget target = Target.Convert();
  30. FormatInfo format = FormatTable.GetFormatInfo(Info.Format);
  31. PixelInternalFormat pixelInternalFormat;
  32. if (format.IsCompressed)
  33. {
  34. pixelInternalFormat = (PixelInternalFormat)format.PixelFormat;
  35. }
  36. else
  37. {
  38. pixelInternalFormat = format.PixelInternalFormat;
  39. }
  40. GL.TextureView(
  41. Handle,
  42. target,
  43. _parent.Handle,
  44. pixelInternalFormat,
  45. FirstLevel,
  46. Info.Levels,
  47. FirstLayer,
  48. Info.GetLayers());
  49. GL.ActiveTexture(TextureUnit.Texture0);
  50. GL.BindTexture(target, Handle);
  51. int[] swizzleRgba = new int[]
  52. {
  53. (int)Info.SwizzleR.Convert(),
  54. (int)Info.SwizzleG.Convert(),
  55. (int)Info.SwizzleB.Convert(),
  56. (int)Info.SwizzleA.Convert()
  57. };
  58. if (Info.Format.IsBgra8())
  59. {
  60. // Swap B <-> R for BGRA formats, as OpenGL has no support for them
  61. // and we need to manually swap the components on read/write on the GPU.
  62. int temp = swizzleRgba[0];
  63. swizzleRgba[0] = swizzleRgba[2];
  64. swizzleRgba[2] = temp;
  65. }
  66. GL.TexParameter(target, TextureParameterName.TextureSwizzleRgba, swizzleRgba);
  67. int maxLevel = Info.Levels - 1;
  68. if (maxLevel < 0)
  69. {
  70. maxLevel = 0;
  71. }
  72. GL.TexParameter(target, TextureParameterName.TextureMaxLevel, maxLevel);
  73. GL.TexParameter(target, TextureParameterName.DepthStencilTextureMode, (int)Info.DepthStencilMode.Convert());
  74. }
  75. public ITexture CreateView(TextureCreateInfo info, int firstLayer, int firstLevel)
  76. {
  77. if (Info.IsCompressed == info.IsCompressed)
  78. {
  79. firstLayer += FirstLayer;
  80. firstLevel += FirstLevel;
  81. return _parent.CreateView(info, firstLayer, firstLevel);
  82. }
  83. else
  84. {
  85. // TODO: Most graphics APIs doesn't support creating a texture view from a compressed format
  86. // with a non-compressed format (or vice-versa), however NVN seems to support it.
  87. // So we emulate that here with a texture copy (see the first CopyTo overload).
  88. // However right now it only does a single copy right after the view is created,
  89. // so it doesn't work for all cases.
  90. TextureView emulatedView = (TextureView)_renderer.CreateTexture(info, ScaleFactor);
  91. _renderer.TextureCopy.CopyUnscaled(
  92. this,
  93. emulatedView,
  94. 0,
  95. firstLayer,
  96. 0,
  97. firstLevel);
  98. emulatedView._emulatedViewParent = this;
  99. emulatedView.FirstLayer = firstLayer;
  100. emulatedView.FirstLevel = firstLevel;
  101. return emulatedView;
  102. }
  103. }
  104. public int GetIncompatibleFormatViewHandle()
  105. {
  106. // AMD and Intel has a bug where the view format is always ignored,
  107. // it uses the parent format instead.
  108. // As workaround we create a new texture with the correct
  109. // format, and then do a copy after the draw.
  110. if (_parent.Info.Format != Format)
  111. {
  112. if (_incompatibleFormatView == null)
  113. {
  114. _incompatibleFormatView = (TextureView)_renderer.CreateTexture(Info, ScaleFactor);
  115. }
  116. _renderer.TextureCopy.CopyUnscaled(_parent, _incompatibleFormatView, FirstLayer, 0, FirstLevel, 0);
  117. return _incompatibleFormatView.Handle;
  118. }
  119. return Handle;
  120. }
  121. public void SignalModified()
  122. {
  123. if (_incompatibleFormatView != null)
  124. {
  125. _renderer.TextureCopy.CopyUnscaled(_incompatibleFormatView, _parent, 0, FirstLayer, 0, FirstLevel);
  126. }
  127. }
  128. public void CopyTo(ITexture destination, int firstLayer, int firstLevel)
  129. {
  130. TextureView destinationView = (TextureView)destination;
  131. _renderer.TextureCopy.CopyUnscaled(this, destinationView, 0, firstLayer, 0, firstLevel);
  132. if (destinationView._emulatedViewParent != null)
  133. {
  134. _renderer.TextureCopy.CopyUnscaled(
  135. this,
  136. destinationView._emulatedViewParent,
  137. 0,
  138. destinationView.FirstLayer,
  139. 0,
  140. destinationView.FirstLevel);
  141. }
  142. }
  143. public void CopyTo(ITexture destination, Extents2D srcRegion, Extents2D dstRegion, bool linearFilter)
  144. {
  145. _renderer.TextureCopy.Copy(this, (TextureView)destination, srcRegion, dstRegion, linearFilter);
  146. }
  147. public byte[] GetData()
  148. {
  149. int size = 0;
  150. for (int level = 0; level < Info.Levels; level++)
  151. {
  152. size += Info.GetMipSize(level);
  153. }
  154. byte[] data = new byte[size];
  155. unsafe
  156. {
  157. fixed (byte* ptr = data)
  158. {
  159. WriteTo((IntPtr)ptr);
  160. }
  161. }
  162. return data;
  163. }
  164. public void WriteToPbo(int offset, bool forceBgra)
  165. {
  166. WriteTo(IntPtr.Zero + offset, forceBgra);
  167. }
  168. public int WriteToPbo2D(int offset, int layer, int level)
  169. {
  170. return WriteTo2D(IntPtr.Zero + offset, layer, level);
  171. }
  172. private int WriteTo2D(IntPtr data, int layer, int level)
  173. {
  174. TextureTarget target = Target.Convert();
  175. Bind(target, 0);
  176. FormatInfo format = FormatTable.GetFormatInfo(Info.Format);
  177. PixelFormat pixelFormat = format.PixelFormat;
  178. PixelType pixelType = format.PixelType;
  179. if (target == TextureTarget.TextureCubeMap || target == TextureTarget.TextureCubeMapArray)
  180. {
  181. target = TextureTarget.TextureCubeMapPositiveX + (layer % 6);
  182. }
  183. int mipSize = Info.GetMipSize2D(level);
  184. // The GL function returns all layers. Must return the offset of the layer we're interested in.
  185. int resultOffset = target switch
  186. {
  187. TextureTarget.TextureCubeMapArray => (layer / 6) * mipSize,
  188. TextureTarget.Texture1DArray => layer * mipSize,
  189. TextureTarget.Texture2DArray => layer * mipSize,
  190. _ => 0
  191. };
  192. if (format.IsCompressed)
  193. {
  194. GL.GetCompressedTexImage(target, level, data);
  195. }
  196. else
  197. {
  198. GL.GetTexImage(target, level, pixelFormat, pixelType, data);
  199. }
  200. return resultOffset;
  201. }
  202. private void WriteTo(IntPtr data, bool forceBgra = false)
  203. {
  204. TextureTarget target = Target.Convert();
  205. Bind(target, 0);
  206. FormatInfo format = FormatTable.GetFormatInfo(Info.Format);
  207. PixelFormat pixelFormat = format.PixelFormat;
  208. PixelType pixelType = format.PixelType;
  209. if (forceBgra)
  210. {
  211. pixelFormat = PixelFormat.Bgra;
  212. }
  213. int faces = 1;
  214. if (target == TextureTarget.TextureCubeMap)
  215. {
  216. target = TextureTarget.TextureCubeMapPositiveX;
  217. faces = 6;
  218. }
  219. for (int level = 0; level < Info.Levels; level++)
  220. {
  221. for (int face = 0; face < faces; face++)
  222. {
  223. int faceOffset = face * Info.GetMipSize2D(level);
  224. if (format.IsCompressed)
  225. {
  226. GL.GetCompressedTexImage(target + face, level, data + faceOffset);
  227. }
  228. else
  229. {
  230. GL.GetTexImage(target + face, level, pixelFormat, pixelType, data + faceOffset);
  231. }
  232. }
  233. data += Info.GetMipSize(level);
  234. }
  235. }
  236. public void SetData(ReadOnlySpan<byte> data)
  237. {
  238. unsafe
  239. {
  240. fixed (byte* ptr = data)
  241. {
  242. ReadFrom((IntPtr)ptr, data.Length);
  243. }
  244. }
  245. }
  246. public void ReadFromPbo(int offset, int size)
  247. {
  248. ReadFrom(IntPtr.Zero + offset, size);
  249. }
  250. public void ReadFromPbo2D(int offset, int layer, int level, int width, int height)
  251. {
  252. ReadFrom2D(IntPtr.Zero + offset, layer, level, width, height);
  253. }
  254. private void ReadFrom2D(IntPtr data, int layer, int level, int width, int height)
  255. {
  256. TextureTarget target = Target.Convert();
  257. int mipSize = Info.GetMipSize2D(level);
  258. Bind(target, 0);
  259. FormatInfo format = FormatTable.GetFormatInfo(Info.Format);
  260. switch (Target)
  261. {
  262. case Target.Texture1D:
  263. if (format.IsCompressed)
  264. {
  265. GL.CompressedTexSubImage1D(
  266. target,
  267. level,
  268. 0,
  269. width,
  270. format.PixelFormat,
  271. mipSize,
  272. data);
  273. }
  274. else
  275. {
  276. GL.TexSubImage1D(
  277. target,
  278. level,
  279. 0,
  280. width,
  281. format.PixelFormat,
  282. format.PixelType,
  283. data);
  284. }
  285. break;
  286. case Target.Texture1DArray:
  287. if (format.IsCompressed)
  288. {
  289. GL.CompressedTexSubImage2D(
  290. target,
  291. level,
  292. 0,
  293. layer,
  294. width,
  295. 1,
  296. format.PixelFormat,
  297. mipSize,
  298. data);
  299. }
  300. else
  301. {
  302. GL.TexSubImage2D(
  303. target,
  304. level,
  305. 0,
  306. layer,
  307. width,
  308. 1,
  309. format.PixelFormat,
  310. format.PixelType,
  311. data);
  312. }
  313. break;
  314. case Target.Texture2D:
  315. if (format.IsCompressed)
  316. {
  317. GL.CompressedTexSubImage2D(
  318. target,
  319. level,
  320. 0,
  321. 0,
  322. width,
  323. height,
  324. format.PixelFormat,
  325. mipSize,
  326. data);
  327. }
  328. else
  329. {
  330. GL.TexSubImage2D(
  331. target,
  332. level,
  333. 0,
  334. 0,
  335. width,
  336. height,
  337. format.PixelFormat,
  338. format.PixelType,
  339. data);
  340. }
  341. break;
  342. case Target.Texture2DArray:
  343. case Target.Texture3D:
  344. case Target.CubemapArray:
  345. if (format.IsCompressed)
  346. {
  347. GL.CompressedTexSubImage3D(
  348. target,
  349. level,
  350. 0,
  351. 0,
  352. layer,
  353. width,
  354. height,
  355. 1,
  356. format.PixelFormat,
  357. mipSize,
  358. data);
  359. }
  360. else
  361. {
  362. GL.TexSubImage3D(
  363. target,
  364. level,
  365. 0,
  366. 0,
  367. layer,
  368. width,
  369. height,
  370. 1,
  371. format.PixelFormat,
  372. format.PixelType,
  373. data);
  374. }
  375. break;
  376. case Target.Cubemap:
  377. if (format.IsCompressed)
  378. {
  379. GL.CompressedTexSubImage2D(
  380. TextureTarget.TextureCubeMapPositiveX + layer,
  381. level,
  382. 0,
  383. 0,
  384. width,
  385. height,
  386. format.PixelFormat,
  387. mipSize,
  388. data);
  389. }
  390. else
  391. {
  392. GL.TexSubImage2D(
  393. TextureTarget.TextureCubeMapPositiveX + layer,
  394. level,
  395. 0,
  396. 0,
  397. width,
  398. height,
  399. format.PixelFormat,
  400. format.PixelType,
  401. data);
  402. }
  403. break;
  404. }
  405. }
  406. private void ReadFrom(IntPtr data, int size)
  407. {
  408. TextureTarget target = Target.Convert();
  409. Bind(target, 0);
  410. FormatInfo format = FormatTable.GetFormatInfo(Info.Format);
  411. int width = Info.Width;
  412. int height = Info.Height;
  413. int depth = Info.Depth;
  414. int offset = 0;
  415. for (int level = 0; level < Info.Levels; level++)
  416. {
  417. int mipSize = Info.GetMipSize(level);
  418. int endOffset = offset + mipSize;
  419. if ((uint)endOffset > (uint)size)
  420. {
  421. return;
  422. }
  423. switch (Info.Target)
  424. {
  425. case Target.Texture1D:
  426. if (format.IsCompressed)
  427. {
  428. GL.CompressedTexSubImage1D(
  429. target,
  430. level,
  431. 0,
  432. width,
  433. format.PixelFormat,
  434. mipSize,
  435. data);
  436. }
  437. else
  438. {
  439. GL.TexSubImage1D(
  440. target,
  441. level,
  442. 0,
  443. width,
  444. format.PixelFormat,
  445. format.PixelType,
  446. data);
  447. }
  448. break;
  449. case Target.Texture1DArray:
  450. case Target.Texture2D:
  451. if (format.IsCompressed)
  452. {
  453. GL.CompressedTexSubImage2D(
  454. target,
  455. level,
  456. 0,
  457. 0,
  458. width,
  459. height,
  460. format.PixelFormat,
  461. mipSize,
  462. data);
  463. }
  464. else
  465. {
  466. GL.TexSubImage2D(
  467. target,
  468. level,
  469. 0,
  470. 0,
  471. width,
  472. height,
  473. format.PixelFormat,
  474. format.PixelType,
  475. data);
  476. }
  477. break;
  478. case Target.Texture2DArray:
  479. case Target.Texture3D:
  480. case Target.CubemapArray:
  481. if (format.IsCompressed)
  482. {
  483. GL.CompressedTexSubImage3D(
  484. target,
  485. level,
  486. 0,
  487. 0,
  488. 0,
  489. width,
  490. height,
  491. depth,
  492. format.PixelFormat,
  493. mipSize,
  494. data);
  495. }
  496. else
  497. {
  498. GL.TexSubImage3D(
  499. target,
  500. level,
  501. 0,
  502. 0,
  503. 0,
  504. width,
  505. height,
  506. depth,
  507. format.PixelFormat,
  508. format.PixelType,
  509. data);
  510. }
  511. break;
  512. case Target.Cubemap:
  513. int faceOffset = 0;
  514. for (int face = 0; face < 6; face++, faceOffset += mipSize / 6)
  515. {
  516. if (format.IsCompressed)
  517. {
  518. GL.CompressedTexSubImage2D(
  519. TextureTarget.TextureCubeMapPositiveX + face,
  520. level,
  521. 0,
  522. 0,
  523. width,
  524. height,
  525. format.PixelFormat,
  526. mipSize / 6,
  527. data + faceOffset);
  528. }
  529. else
  530. {
  531. GL.TexSubImage2D(
  532. TextureTarget.TextureCubeMapPositiveX + face,
  533. level,
  534. 0,
  535. 0,
  536. width,
  537. height,
  538. format.PixelFormat,
  539. format.PixelType,
  540. data + faceOffset);
  541. }
  542. }
  543. break;
  544. }
  545. data += mipSize;
  546. offset += mipSize;
  547. width = Math.Max(1, width >> 1);
  548. height = Math.Max(1, height >> 1);
  549. if (Target == Target.Texture3D)
  550. {
  551. depth = Math.Max(1, depth >> 1);
  552. }
  553. }
  554. }
  555. public void SetStorage(BufferRange buffer)
  556. {
  557. throw new NotSupportedException();
  558. }
  559. private void DisposeHandles()
  560. {
  561. if (_incompatibleFormatView != null)
  562. {
  563. _incompatibleFormatView.Dispose();
  564. _incompatibleFormatView = null;
  565. }
  566. if (Handle != 0)
  567. {
  568. GL.DeleteTexture(Handle);
  569. Handle = 0;
  570. }
  571. }
  572. /// <summary>
  573. /// Release the view without necessarily disposing the parent if we are the default view.
  574. /// This allows it to be added to the resource pool and reused later.
  575. /// </summary>
  576. public void Release()
  577. {
  578. bool hadHandle = Handle != 0;
  579. if (_parent.DefaultView != this)
  580. {
  581. DisposeHandles();
  582. }
  583. if (hadHandle)
  584. {
  585. _parent.DecrementViewsCount();
  586. }
  587. }
  588. public void Dispose()
  589. {
  590. if (_parent.DefaultView == this)
  591. {
  592. // Remove the default view (us), so that the texture cannot be released to the cache.
  593. _parent.DeleteDefault();
  594. }
  595. Release();
  596. }
  597. }
  598. }