TextureView.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Graphics.GAL;
  3. using System;
  4. namespace Ryujinx.Graphics.OpenGL
  5. {
  6. class TextureView : ITexture
  7. {
  8. public int Handle { get; private set; }
  9. private readonly Renderer _renderer;
  10. private readonly TextureStorage _parent;
  11. private TextureView _emulatedViewParent;
  12. private readonly TextureCreateInfo _info;
  13. private int _firstLayer;
  14. private int _firstLevel;
  15. private bool _acquired;
  16. private bool _pendingDelete;
  17. public int Width => _info.Width;
  18. public int Height => _info.Height;
  19. public int DepthOrLayers => _info.GetDepthOrLayers();
  20. public int Levels => _info.Levels;
  21. public Target Target => _info.Target;
  22. public Format Format => _info.Format;
  23. public int BlockWidth => _info.BlockWidth;
  24. public int BlockHeight => _info.BlockHeight;
  25. public bool IsCompressed => _info.IsCompressed;
  26. public TextureView(
  27. Renderer renderer,
  28. TextureStorage parent,
  29. TextureCreateInfo info,
  30. int firstLayer,
  31. int firstLevel)
  32. {
  33. _renderer = renderer;
  34. _parent = parent;
  35. _info = info;
  36. _firstLayer = firstLayer;
  37. _firstLevel = firstLevel;
  38. Handle = GL.GenTexture();
  39. CreateView();
  40. }
  41. private void CreateView()
  42. {
  43. TextureTarget target = Target.Convert();
  44. FormatInfo format = FormatTable.GetFormatInfo(_info.Format);
  45. PixelInternalFormat pixelInternalFormat;
  46. if (format.IsCompressed)
  47. {
  48. pixelInternalFormat = (PixelInternalFormat)format.PixelFormat;
  49. }
  50. else
  51. {
  52. pixelInternalFormat = format.PixelInternalFormat;
  53. }
  54. GL.TextureView(
  55. Handle,
  56. target,
  57. _parent.Handle,
  58. pixelInternalFormat,
  59. _firstLevel,
  60. _info.Levels,
  61. _firstLayer,
  62. _info.GetLayers());
  63. GL.ActiveTexture(TextureUnit.Texture0);
  64. GL.BindTexture(target, Handle);
  65. int[] swizzleRgba = new int[]
  66. {
  67. (int)_info.SwizzleR.Convert(),
  68. (int)_info.SwizzleG.Convert(),
  69. (int)_info.SwizzleB.Convert(),
  70. (int)_info.SwizzleA.Convert()
  71. };
  72. GL.TexParameter(target, TextureParameterName.TextureSwizzleRgba, swizzleRgba);
  73. int maxLevel = _info.Levels - 1;
  74. if (maxLevel < 0)
  75. {
  76. maxLevel = 0;
  77. }
  78. GL.TexParameter(target, TextureParameterName.TextureMaxLevel, maxLevel);
  79. // TODO: This requires ARB_stencil_texturing, we should uncomment and test this.
  80. // GL.TexParameter(target, TextureParameterName.DepthStencilTextureMode, (int)_info.DepthStencilMode.Convert());
  81. }
  82. public ITexture CreateView(TextureCreateInfo info, int firstLayer, int firstLevel)
  83. {
  84. if (_info.IsCompressed == info.IsCompressed)
  85. {
  86. firstLayer += _firstLayer;
  87. firstLevel += _firstLevel;
  88. return _parent.CreateView(info, firstLayer, firstLevel);
  89. }
  90. else
  91. {
  92. // TODO: Most graphics APIs doesn't support creating a texture view from a compressed format
  93. // with a non-compressed format (or vice-versa), however NVN seems to support it.
  94. // So we emulate that here with a texture copy (see the first CopyTo overload).
  95. // However right now it only does a single copy right after the view is created,
  96. // so it doesn't work for all cases.
  97. TextureView emulatedView = (TextureView)_renderer.CreateTexture(info);
  98. emulatedView._emulatedViewParent = this;
  99. emulatedView._firstLayer = firstLayer;
  100. emulatedView._firstLevel = firstLevel;
  101. return emulatedView;
  102. }
  103. }
  104. public void CopyTo(ITexture destination, int firstLayer, int firstLevel)
  105. {
  106. TextureView destinationView = (TextureView)destination;
  107. TextureCopyUnscaled.Copy(this, destinationView, firstLayer, firstLevel);
  108. if (destinationView._emulatedViewParent != null)
  109. {
  110. TextureCopyUnscaled.Copy(
  111. this,
  112. destinationView._emulatedViewParent,
  113. destinationView._firstLayer,
  114. destinationView._firstLevel);
  115. }
  116. }
  117. public void CopyTo(ITexture destination, Extents2D srcRegion, Extents2D dstRegion, bool linearFilter)
  118. {
  119. _renderer.TextureCopy.Copy(this, (TextureView)destination, srcRegion, dstRegion, linearFilter);
  120. }
  121. public byte[] GetData()
  122. {
  123. int size = 0;
  124. for (int level = 0; level < _info.Levels; level++)
  125. {
  126. size += _info.GetMipSize(level);
  127. }
  128. byte[] data = new byte[size];
  129. unsafe
  130. {
  131. fixed (byte* ptr = data)
  132. {
  133. WriteTo((IntPtr)ptr);
  134. }
  135. }
  136. return data;
  137. }
  138. private void WriteTo(IntPtr ptr)
  139. {
  140. TextureTarget target = Target.Convert();
  141. Bind(target, 0);
  142. FormatInfo format = FormatTable.GetFormatInfo(_info.Format);
  143. int faces = 1;
  144. if (target == TextureTarget.TextureCubeMap)
  145. {
  146. target = TextureTarget.TextureCubeMapPositiveX;
  147. faces = 6;
  148. }
  149. for (int level = 0; level < _info.Levels; level++)
  150. {
  151. for (int face = 0; face < faces; face++)
  152. {
  153. int faceOffset = face * _info.GetMipSize2D(level);
  154. if (format.IsCompressed)
  155. {
  156. GL.GetCompressedTexImage(target + face, level, ptr + faceOffset);
  157. }
  158. else
  159. {
  160. GL.GetTexImage(
  161. target + face,
  162. level,
  163. format.PixelFormat,
  164. format.PixelType,
  165. ptr + faceOffset);
  166. }
  167. }
  168. ptr += _info.GetMipSize(level);
  169. }
  170. }
  171. public void SetData(Span<byte> data)
  172. {
  173. unsafe
  174. {
  175. fixed (byte* ptr = data)
  176. {
  177. SetData((IntPtr)ptr, data.Length);
  178. }
  179. }
  180. }
  181. private void SetData(IntPtr data, int size)
  182. {
  183. TextureTarget target = Target.Convert();
  184. Bind(target, 0);
  185. FormatInfo format = FormatTable.GetFormatInfo(_info.Format);
  186. int width = _info.Width;
  187. int height = _info.Height;
  188. int depth = _info.Depth;
  189. int offset = 0;
  190. for (int level = 0; level < _info.Levels; level++)
  191. {
  192. int mipSize = _info.GetMipSize(level);
  193. int endOffset = offset + mipSize;
  194. if ((uint)endOffset > (uint)size)
  195. {
  196. return;
  197. }
  198. switch (_info.Target)
  199. {
  200. case Target.Texture1D:
  201. if (format.IsCompressed)
  202. {
  203. GL.CompressedTexSubImage1D(
  204. target,
  205. level,
  206. 0,
  207. width,
  208. format.PixelFormat,
  209. mipSize,
  210. data);
  211. }
  212. else
  213. {
  214. GL.TexSubImage1D(
  215. target,
  216. level,
  217. 0,
  218. width,
  219. format.PixelFormat,
  220. format.PixelType,
  221. data);
  222. }
  223. break;
  224. case Target.Texture1DArray:
  225. case Target.Texture2D:
  226. if (format.IsCompressed)
  227. {
  228. GL.CompressedTexSubImage2D(
  229. target,
  230. level,
  231. 0,
  232. 0,
  233. width,
  234. height,
  235. format.PixelFormat,
  236. mipSize,
  237. data);
  238. }
  239. else
  240. {
  241. GL.TexSubImage2D(
  242. target,
  243. level,
  244. 0,
  245. 0,
  246. width,
  247. height,
  248. format.PixelFormat,
  249. format.PixelType,
  250. data);
  251. }
  252. break;
  253. case Target.Texture2DArray:
  254. case Target.Texture3D:
  255. case Target.CubemapArray:
  256. if (format.IsCompressed)
  257. {
  258. GL.CompressedTexSubImage3D(
  259. target,
  260. level,
  261. 0,
  262. 0,
  263. 0,
  264. width,
  265. height,
  266. depth,
  267. format.PixelFormat,
  268. mipSize,
  269. data);
  270. }
  271. else
  272. {
  273. GL.TexSubImage3D(
  274. target,
  275. level,
  276. 0,
  277. 0,
  278. 0,
  279. width,
  280. height,
  281. depth,
  282. format.PixelFormat,
  283. format.PixelType,
  284. data);
  285. }
  286. break;
  287. case Target.Cubemap:
  288. int faceOffset = 0;
  289. for (int face = 0; face < 6; face++, faceOffset += mipSize / 6)
  290. {
  291. if (format.IsCompressed)
  292. {
  293. GL.CompressedTexSubImage2D(
  294. TextureTarget.TextureCubeMapPositiveX + face,
  295. level,
  296. 0,
  297. 0,
  298. width,
  299. height,
  300. format.PixelFormat,
  301. mipSize / 6,
  302. data + faceOffset);
  303. }
  304. else
  305. {
  306. GL.TexSubImage2D(
  307. TextureTarget.TextureCubeMapPositiveX + face,
  308. level,
  309. 0,
  310. 0,
  311. width,
  312. height,
  313. format.PixelFormat,
  314. format.PixelType,
  315. data + faceOffset);
  316. }
  317. }
  318. break;
  319. }
  320. data += mipSize;
  321. offset += mipSize;
  322. width = Math.Max(1, width >> 1);
  323. height = Math.Max(1, height >> 1);
  324. if (Target == Target.Texture3D)
  325. {
  326. depth = Math.Max(1, depth >> 1);
  327. }
  328. }
  329. }
  330. public void Bind(int unit)
  331. {
  332. Bind(Target.Convert(), unit);
  333. }
  334. private void Bind(TextureTarget target, int unit)
  335. {
  336. GL.ActiveTexture(TextureUnit.Texture0 + unit);
  337. GL.BindTexture(target, Handle);
  338. }
  339. public void Dispose()
  340. {
  341. if (Handle != 0)
  342. {
  343. GL.DeleteTexture(Handle);
  344. _parent.DecrementViewsCount();
  345. Handle = 0;
  346. }
  347. }
  348. }
  349. }