TextureView.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. emulatedView._emulatedViewParent = this;
  92. emulatedView.FirstLayer = firstLayer;
  93. emulatedView.FirstLevel = firstLevel;
  94. return emulatedView;
  95. }
  96. }
  97. public int GetIncompatibleFormatViewHandle()
  98. {
  99. // AMD and Intel has a bug where the view format is always ignored,
  100. // it uses the parent format instead.
  101. // As workaround we create a new texture with the correct
  102. // format, and then do a copy after the draw.
  103. if (_parent.Info.Format != Format)
  104. {
  105. if (_incompatibleFormatView == null)
  106. {
  107. _incompatibleFormatView = (TextureView)_renderer.CreateTexture(Info, ScaleFactor);
  108. }
  109. TextureCopyUnscaled.Copy(_parent.Info, _incompatibleFormatView.Info, _parent.Handle, _incompatibleFormatView.Handle, FirstLayer, 0, FirstLevel, 0);
  110. return _incompatibleFormatView.Handle;
  111. }
  112. return Handle;
  113. }
  114. public void SignalModified()
  115. {
  116. if (_incompatibleFormatView != null)
  117. {
  118. TextureCopyUnscaled.Copy(_incompatibleFormatView.Info, _parent.Info, _incompatibleFormatView.Handle, _parent.Handle, 0, FirstLayer, 0, FirstLevel);
  119. }
  120. }
  121. public void CopyTo(ITexture destination, int firstLayer, int firstLevel)
  122. {
  123. TextureView destinationView = (TextureView)destination;
  124. TextureCopyUnscaled.Copy(Info, destinationView.Info, Handle, destinationView.Handle, 0, firstLayer, 0, firstLevel);
  125. if (destinationView._emulatedViewParent != null)
  126. {
  127. TextureCopyUnscaled.Copy(
  128. Info,
  129. destinationView._emulatedViewParent.Info,
  130. Handle,
  131. destinationView._emulatedViewParent.Handle,
  132. 0,
  133. destinationView.FirstLayer,
  134. 0,
  135. destinationView.FirstLevel);
  136. }
  137. }
  138. public void CopyTo(ITexture destination, Extents2D srcRegion, Extents2D dstRegion, bool linearFilter)
  139. {
  140. _renderer.TextureCopy.Copy(this, (TextureView)destination, srcRegion, dstRegion, linearFilter);
  141. }
  142. public byte[] GetData()
  143. {
  144. int size = 0;
  145. for (int level = 0; level < Info.Levels; level++)
  146. {
  147. size += Info.GetMipSize(level);
  148. }
  149. byte[] data = new byte[size];
  150. unsafe
  151. {
  152. fixed (byte* ptr = data)
  153. {
  154. WriteTo((IntPtr)ptr);
  155. }
  156. }
  157. return data;
  158. }
  159. public void WriteToPbo(int offset, bool forceBgra)
  160. {
  161. WriteTo(IntPtr.Zero + offset, forceBgra);
  162. }
  163. private void WriteTo(IntPtr data, bool forceBgra = false)
  164. {
  165. TextureTarget target = Target.Convert();
  166. Bind(target, 0);
  167. FormatInfo format = FormatTable.GetFormatInfo(Info.Format);
  168. PixelFormat pixelFormat = format.PixelFormat;
  169. PixelType pixelType = format.PixelType;
  170. if (forceBgra)
  171. {
  172. pixelFormat = PixelFormat.Bgra;
  173. }
  174. int faces = 1;
  175. if (target == TextureTarget.TextureCubeMap)
  176. {
  177. target = TextureTarget.TextureCubeMapPositiveX;
  178. faces = 6;
  179. }
  180. for (int level = 0; level < Info.Levels; level++)
  181. {
  182. for (int face = 0; face < faces; face++)
  183. {
  184. int faceOffset = face * Info.GetMipSize2D(level);
  185. if (format.IsCompressed)
  186. {
  187. GL.GetCompressedTexImage(target + face, level, data + faceOffset);
  188. }
  189. else
  190. {
  191. GL.GetTexImage(target + face, level, pixelFormat, pixelType, data + faceOffset);
  192. }
  193. }
  194. data += Info.GetMipSize(level);
  195. }
  196. }
  197. public void SetData(ReadOnlySpan<byte> data)
  198. {
  199. unsafe
  200. {
  201. fixed (byte* ptr = data)
  202. {
  203. ReadFrom((IntPtr)ptr, data.Length);
  204. }
  205. }
  206. }
  207. public void ReadFromPbo(int offset, int size)
  208. {
  209. ReadFrom(IntPtr.Zero + offset, size);
  210. }
  211. private void ReadFrom(IntPtr data, int size)
  212. {
  213. TextureTarget target = Target.Convert();
  214. Bind(target, 0);
  215. FormatInfo format = FormatTable.GetFormatInfo(Info.Format);
  216. int width = Info.Width;
  217. int height = Info.Height;
  218. int depth = Info.Depth;
  219. int offset = 0;
  220. for (int level = 0; level < Info.Levels; level++)
  221. {
  222. int mipSize = Info.GetMipSize(level);
  223. int endOffset = offset + mipSize;
  224. if ((uint)endOffset > (uint)size)
  225. {
  226. return;
  227. }
  228. switch (Info.Target)
  229. {
  230. case Target.Texture1D:
  231. if (format.IsCompressed)
  232. {
  233. GL.CompressedTexSubImage1D(
  234. target,
  235. level,
  236. 0,
  237. width,
  238. format.PixelFormat,
  239. mipSize,
  240. data);
  241. }
  242. else
  243. {
  244. GL.TexSubImage1D(
  245. target,
  246. level,
  247. 0,
  248. width,
  249. format.PixelFormat,
  250. format.PixelType,
  251. data);
  252. }
  253. break;
  254. case Target.Texture1DArray:
  255. case Target.Texture2D:
  256. if (format.IsCompressed)
  257. {
  258. GL.CompressedTexSubImage2D(
  259. target,
  260. level,
  261. 0,
  262. 0,
  263. width,
  264. height,
  265. format.PixelFormat,
  266. mipSize,
  267. data);
  268. }
  269. else
  270. {
  271. GL.TexSubImage2D(
  272. target,
  273. level,
  274. 0,
  275. 0,
  276. width,
  277. height,
  278. format.PixelFormat,
  279. format.PixelType,
  280. data);
  281. }
  282. break;
  283. case Target.Texture2DArray:
  284. case Target.Texture3D:
  285. case Target.CubemapArray:
  286. if (format.IsCompressed)
  287. {
  288. GL.CompressedTexSubImage3D(
  289. target,
  290. level,
  291. 0,
  292. 0,
  293. 0,
  294. width,
  295. height,
  296. depth,
  297. format.PixelFormat,
  298. mipSize,
  299. data);
  300. }
  301. else
  302. {
  303. GL.TexSubImage3D(
  304. target,
  305. level,
  306. 0,
  307. 0,
  308. 0,
  309. width,
  310. height,
  311. depth,
  312. format.PixelFormat,
  313. format.PixelType,
  314. data);
  315. }
  316. break;
  317. case Target.Cubemap:
  318. int faceOffset = 0;
  319. for (int face = 0; face < 6; face++, faceOffset += mipSize / 6)
  320. {
  321. if (format.IsCompressed)
  322. {
  323. GL.CompressedTexSubImage2D(
  324. TextureTarget.TextureCubeMapPositiveX + face,
  325. level,
  326. 0,
  327. 0,
  328. width,
  329. height,
  330. format.PixelFormat,
  331. mipSize / 6,
  332. data + faceOffset);
  333. }
  334. else
  335. {
  336. GL.TexSubImage2D(
  337. TextureTarget.TextureCubeMapPositiveX + face,
  338. level,
  339. 0,
  340. 0,
  341. width,
  342. height,
  343. format.PixelFormat,
  344. format.PixelType,
  345. data + faceOffset);
  346. }
  347. }
  348. break;
  349. }
  350. data += mipSize;
  351. offset += mipSize;
  352. width = Math.Max(1, width >> 1);
  353. height = Math.Max(1, height >> 1);
  354. if (Target == Target.Texture3D)
  355. {
  356. depth = Math.Max(1, depth >> 1);
  357. }
  358. }
  359. }
  360. public void SetStorage(BufferRange buffer)
  361. {
  362. throw new NotSupportedException();
  363. }
  364. private void DisposeHandles()
  365. {
  366. if (_incompatibleFormatView != null)
  367. {
  368. _incompatibleFormatView.Dispose();
  369. _incompatibleFormatView = null;
  370. }
  371. if (Handle != 0)
  372. {
  373. GL.DeleteTexture(Handle);
  374. Handle = 0;
  375. }
  376. }
  377. /// <summary>
  378. /// Release the view without necessarily disposing the parent if we are the default view.
  379. /// This allows it to be added to the resource pool and reused later.
  380. /// </summary>
  381. public void Release()
  382. {
  383. bool hadHandle = Handle != 0;
  384. if (_parent.DefaultView != this)
  385. {
  386. DisposeHandles();
  387. }
  388. if (hadHandle)
  389. {
  390. _parent.DecrementViewsCount();
  391. }
  392. }
  393. public void Dispose()
  394. {
  395. if (_parent.DefaultView == this)
  396. {
  397. // Remove the default view (us), so that the texture cannot be released to the cache.
  398. _parent.DeleteDefault();
  399. }
  400. Release();
  401. }
  402. }
  403. }