TextureView.cs 13 KB

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