TextureView.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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()
  127. {
  128. int size = 0;
  129. for (int level = 0; level < _info.Levels; level++)
  130. {
  131. size += _info.GetMipSize(level);
  132. }
  133. byte[] data = new byte[size];
  134. unsafe
  135. {
  136. fixed (byte* ptr = data)
  137. {
  138. WriteTo((IntPtr)ptr);
  139. }
  140. }
  141. return data;
  142. }
  143. private void WriteTo(IntPtr ptr)
  144. {
  145. TextureTarget target = Target.Convert();
  146. Bind(target, 0);
  147. FormatInfo format = FormatTable.GetFormatInfo(_info.Format);
  148. int faces = 1;
  149. if (target == TextureTarget.TextureCubeMap)
  150. {
  151. target = TextureTarget.TextureCubeMapPositiveX;
  152. faces = 6;
  153. }
  154. for (int level = 0; level < _info.Levels; level++)
  155. {
  156. for (int face = 0; face < faces; face++)
  157. {
  158. int faceOffset = face * _info.GetMipSize2D(level);
  159. if (format.IsCompressed)
  160. {
  161. GL.GetCompressedTexImage(target + face, level, ptr + faceOffset);
  162. }
  163. else
  164. {
  165. GL.GetTexImage(
  166. target + face,
  167. level,
  168. format.PixelFormat,
  169. format.PixelType,
  170. ptr + faceOffset);
  171. }
  172. }
  173. ptr += _info.GetMipSize(level);
  174. }
  175. }
  176. public void SetData(Span<byte> data)
  177. {
  178. unsafe
  179. {
  180. fixed (byte* ptr = data)
  181. {
  182. SetData((IntPtr)ptr, data.Length);
  183. }
  184. }
  185. }
  186. private void SetData(IntPtr data, int size)
  187. {
  188. TextureTarget target = Target.Convert();
  189. Bind(target, 0);
  190. FormatInfo format = FormatTable.GetFormatInfo(_info.Format);
  191. int width = _info.Width;
  192. int height = _info.Height;
  193. int depth = _info.Depth;
  194. int offset = 0;
  195. for (int level = 0; level < _info.Levels; level++)
  196. {
  197. int mipSize = _info.GetMipSize(level);
  198. int endOffset = offset + mipSize;
  199. if ((uint)endOffset > (uint)size)
  200. {
  201. return;
  202. }
  203. switch (_info.Target)
  204. {
  205. case Target.Texture1D:
  206. if (format.IsCompressed)
  207. {
  208. GL.CompressedTexSubImage1D(
  209. target,
  210. level,
  211. 0,
  212. width,
  213. format.PixelFormat,
  214. mipSize,
  215. data);
  216. }
  217. else
  218. {
  219. GL.TexSubImage1D(
  220. target,
  221. level,
  222. 0,
  223. width,
  224. format.PixelFormat,
  225. format.PixelType,
  226. data);
  227. }
  228. break;
  229. case Target.Texture1DArray:
  230. case Target.Texture2D:
  231. if (format.IsCompressed)
  232. {
  233. GL.CompressedTexSubImage2D(
  234. target,
  235. level,
  236. 0,
  237. 0,
  238. width,
  239. height,
  240. format.PixelFormat,
  241. mipSize,
  242. data);
  243. }
  244. else
  245. {
  246. GL.TexSubImage2D(
  247. target,
  248. level,
  249. 0,
  250. 0,
  251. width,
  252. height,
  253. format.PixelFormat,
  254. format.PixelType,
  255. data);
  256. }
  257. break;
  258. case Target.Texture2DArray:
  259. case Target.Texture3D:
  260. case Target.CubemapArray:
  261. if (format.IsCompressed)
  262. {
  263. GL.CompressedTexSubImage3D(
  264. target,
  265. level,
  266. 0,
  267. 0,
  268. 0,
  269. width,
  270. height,
  271. depth,
  272. format.PixelFormat,
  273. mipSize,
  274. data);
  275. }
  276. else
  277. {
  278. GL.TexSubImage3D(
  279. target,
  280. level,
  281. 0,
  282. 0,
  283. 0,
  284. width,
  285. height,
  286. depth,
  287. format.PixelFormat,
  288. format.PixelType,
  289. data);
  290. }
  291. break;
  292. case Target.Cubemap:
  293. int faceOffset = 0;
  294. for (int face = 0; face < 6; face++, faceOffset += mipSize / 6)
  295. {
  296. if (format.IsCompressed)
  297. {
  298. GL.CompressedTexSubImage2D(
  299. TextureTarget.TextureCubeMapPositiveX + face,
  300. level,
  301. 0,
  302. 0,
  303. width,
  304. height,
  305. format.PixelFormat,
  306. mipSize / 6,
  307. data + faceOffset);
  308. }
  309. else
  310. {
  311. GL.TexSubImage2D(
  312. TextureTarget.TextureCubeMapPositiveX + face,
  313. level,
  314. 0,
  315. 0,
  316. width,
  317. height,
  318. format.PixelFormat,
  319. format.PixelType,
  320. data + faceOffset);
  321. }
  322. }
  323. break;
  324. }
  325. data += mipSize;
  326. offset += mipSize;
  327. width = Math.Max(1, width >> 1);
  328. height = Math.Max(1, height >> 1);
  329. if (Target == Target.Texture3D)
  330. {
  331. depth = Math.Max(1, depth >> 1);
  332. }
  333. }
  334. }
  335. public void Bind(int unit)
  336. {
  337. Bind(Target.Convert(), unit);
  338. }
  339. private void Bind(TextureTarget target, int unit)
  340. {
  341. GL.ActiveTexture(TextureUnit.Texture0 + unit);
  342. GL.BindTexture(target, Handle);
  343. }
  344. public void Acquire()
  345. {
  346. _acquired = true;
  347. }
  348. public void Release()
  349. {
  350. _acquired = false;
  351. if (_pendingDelete)
  352. {
  353. _pendingDelete = false;
  354. Dispose();
  355. }
  356. }
  357. public void Dispose()
  358. {
  359. if (_acquired)
  360. {
  361. _pendingDelete = true;
  362. return;
  363. }
  364. if (Handle != 0)
  365. {
  366. GL.DeleteTexture(Handle);
  367. _parent.DecrementViewsCount();
  368. Handle = 0;
  369. }
  370. }
  371. }
  372. }