TextureView.cs 21 KB

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