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. 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. // GL.TexParameter(target, TextureParameterName.DepthStencilTextureMode, (int)_info.DepthStencilMode.Convert());
  80. }
  81. public ITexture CreateView(TextureCreateInfo info, int firstLayer, int firstLevel)
  82. {
  83. if (_info.IsCompressed == info.IsCompressed)
  84. {
  85. firstLayer += _firstLayer;
  86. firstLevel += _firstLevel;
  87. return _parent.CreateView(info, firstLayer, firstLevel);
  88. }
  89. else
  90. {
  91. // TODO: Improve
  92. TextureView emulatedView = (TextureView)_renderer.CreateTexture(info);
  93. emulatedView._emulatedViewParent = this;
  94. emulatedView._firstLayer = firstLayer;
  95. emulatedView._firstLevel = firstLevel;
  96. return emulatedView;
  97. }
  98. }
  99. public void CopyTo(ITexture destination, int firstLayer, int firstLevel)
  100. {
  101. TextureView destinationView = (TextureView)destination;
  102. TextureCopyUnscaled.Copy(this, destinationView, firstLayer, firstLevel);
  103. if (destinationView._emulatedViewParent != null)
  104. {
  105. TextureCopyUnscaled.Copy(
  106. this,
  107. destinationView._emulatedViewParent,
  108. destinationView._firstLayer,
  109. destinationView._firstLevel);
  110. }
  111. }
  112. public void CopyTo(ITexture destination, Extents2D srcRegion, Extents2D dstRegion, bool linearFilter)
  113. {
  114. _renderer.TextureCopy.Copy(this, (TextureView)destination, srcRegion, dstRegion, linearFilter);
  115. }
  116. public byte[] GetData()
  117. {
  118. int size = 0;
  119. for (int level = 0; level < _info.Levels; level++)
  120. {
  121. size += _info.GetMipSize(level);
  122. }
  123. byte[] data = new byte[size];
  124. unsafe
  125. {
  126. fixed (byte* ptr = data)
  127. {
  128. WriteTo((IntPtr)ptr);
  129. }
  130. }
  131. return data;
  132. }
  133. private void WriteTo(IntPtr ptr)
  134. {
  135. TextureTarget target = Target.Convert();
  136. Bind(target, 0);
  137. FormatInfo format = FormatTable.GetFormatInfo(_info.Format);
  138. int faces = 1;
  139. if (target == TextureTarget.TextureCubeMap)
  140. {
  141. target = TextureTarget.TextureCubeMapPositiveX;
  142. faces = 6;
  143. }
  144. for (int level = 0; level < _info.Levels; level++)
  145. {
  146. for (int face = 0; face < faces; face++)
  147. {
  148. int faceOffset = face * _info.GetMipSize2D(level);
  149. if (format.IsCompressed)
  150. {
  151. GL.GetCompressedTexImage(target + face, level, ptr + faceOffset);
  152. }
  153. else
  154. {
  155. GL.GetTexImage(
  156. target + face,
  157. level,
  158. format.PixelFormat,
  159. format.PixelType,
  160. ptr + faceOffset);
  161. }
  162. }
  163. ptr += _info.GetMipSize(level);
  164. }
  165. }
  166. public void SetData(Span<byte> data)
  167. {
  168. unsafe
  169. {
  170. fixed (byte* ptr = data)
  171. {
  172. SetData((IntPtr)ptr, data.Length);
  173. }
  174. }
  175. }
  176. private void SetData(IntPtr data, int size)
  177. {
  178. TextureTarget target = Target.Convert();
  179. Bind(target, 0);
  180. FormatInfo format = FormatTable.GetFormatInfo(_info.Format);
  181. int width = _info.Width;
  182. int height = _info.Height;
  183. int depth = _info.Depth;
  184. int offset = 0;
  185. for (int level = 0; level < _info.Levels; level++)
  186. {
  187. int mipSize = _info.GetMipSize(level);
  188. int endOffset = offset + mipSize;
  189. if ((uint)endOffset > (uint)size)
  190. {
  191. return;
  192. }
  193. switch (_info.Target)
  194. {
  195. case Target.Texture1D:
  196. if (format.IsCompressed)
  197. {
  198. GL.CompressedTexSubImage1D(
  199. target,
  200. level,
  201. 0,
  202. width,
  203. format.PixelFormat,
  204. mipSize,
  205. data);
  206. }
  207. else
  208. {
  209. GL.TexSubImage1D(
  210. target,
  211. level,
  212. 0,
  213. width,
  214. format.PixelFormat,
  215. format.PixelType,
  216. data);
  217. }
  218. break;
  219. case Target.Texture1DArray:
  220. case Target.Texture2D:
  221. if (format.IsCompressed)
  222. {
  223. GL.CompressedTexSubImage2D(
  224. target,
  225. level,
  226. 0,
  227. 0,
  228. width,
  229. height,
  230. format.PixelFormat,
  231. mipSize,
  232. data);
  233. }
  234. else
  235. {
  236. GL.TexSubImage2D(
  237. target,
  238. level,
  239. 0,
  240. 0,
  241. width,
  242. height,
  243. format.PixelFormat,
  244. format.PixelType,
  245. data);
  246. }
  247. break;
  248. case Target.Texture2DArray:
  249. case Target.Texture3D:
  250. case Target.CubemapArray:
  251. if (format.IsCompressed)
  252. {
  253. GL.CompressedTexSubImage3D(
  254. target,
  255. level,
  256. 0,
  257. 0,
  258. 0,
  259. width,
  260. height,
  261. depth,
  262. format.PixelFormat,
  263. mipSize,
  264. data);
  265. }
  266. else
  267. {
  268. GL.TexSubImage3D(
  269. target,
  270. level,
  271. 0,
  272. 0,
  273. 0,
  274. width,
  275. height,
  276. depth,
  277. format.PixelFormat,
  278. format.PixelType,
  279. data);
  280. }
  281. break;
  282. case Target.Cubemap:
  283. int faceOffset = 0;
  284. for (int face = 0; face < 6; face++, faceOffset += mipSize / 6)
  285. {
  286. if (format.IsCompressed)
  287. {
  288. GL.CompressedTexSubImage2D(
  289. TextureTarget.TextureCubeMapPositiveX + face,
  290. level,
  291. 0,
  292. 0,
  293. width,
  294. height,
  295. format.PixelFormat,
  296. mipSize / 6,
  297. data + faceOffset);
  298. }
  299. else
  300. {
  301. GL.TexSubImage2D(
  302. TextureTarget.TextureCubeMapPositiveX + face,
  303. level,
  304. 0,
  305. 0,
  306. width,
  307. height,
  308. format.PixelFormat,
  309. format.PixelType,
  310. data + faceOffset);
  311. }
  312. }
  313. break;
  314. }
  315. data += mipSize;
  316. offset += mipSize;
  317. width = Math.Max(1, width >> 1);
  318. height = Math.Max(1, height >> 1);
  319. if (Target == Target.Texture3D)
  320. {
  321. depth = Math.Max(1, depth >> 1);
  322. }
  323. }
  324. }
  325. public void Bind(int unit)
  326. {
  327. Bind(Target.Convert(), unit);
  328. }
  329. private void Bind(TextureTarget target, int unit)
  330. {
  331. GL.ActiveTexture(TextureUnit.Texture0 + unit);
  332. GL.BindTexture(target, Handle);
  333. }
  334. public void Dispose()
  335. {
  336. if (Handle != 0)
  337. {
  338. GL.DeleteTexture(Handle);
  339. _parent.DecrementViewsCount();
  340. Handle = 0;
  341. }
  342. }
  343. }
  344. }