TextureView.cs 13 KB

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