TextureView.cs 14 KB

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