TextureCopy.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Common;
  3. using Ryujinx.Graphics.GAL;
  4. using System;
  5. namespace Ryujinx.Graphics.OpenGL.Image
  6. {
  7. class TextureCopy : IDisposable
  8. {
  9. private readonly Renderer _renderer;
  10. private int _srcFramebuffer;
  11. private int _dstFramebuffer;
  12. private int _copyPboHandle;
  13. private int _copyPboSize;
  14. public TextureCopy(Renderer renderer)
  15. {
  16. _renderer = renderer;
  17. }
  18. public void Copy(
  19. TextureView src,
  20. TextureView dst,
  21. Extents2D srcRegion,
  22. Extents2D dstRegion,
  23. bool linearFilter)
  24. {
  25. TextureView srcConverted = src.Format.IsBgra8() != dst.Format.IsBgra8() ? BgraSwap(src) : src;
  26. (int oldDrawFramebufferHandle, int oldReadFramebufferHandle) = ((Pipeline)_renderer.Pipeline).GetBoundFramebuffers();
  27. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, GetSrcFramebufferLazy());
  28. GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, GetDstFramebufferLazy());
  29. int levels = Math.Min(src.Info.Levels, dst.Info.Levels);
  30. int layers = Math.Min(src.Info.GetLayers(), dst.Info.GetLayers());
  31. for (int level = 0; level < levels; level++)
  32. {
  33. for (int layer = 0; layer < layers; layer++)
  34. {
  35. if (layers > 1)
  36. {
  37. Attach(FramebufferTarget.ReadFramebuffer, src.Format, srcConverted.Handle, level, layer);
  38. Attach(FramebufferTarget.DrawFramebuffer, dst.Format, dst.Handle, level, layer);
  39. }
  40. else
  41. {
  42. Attach(FramebufferTarget.ReadFramebuffer, src.Format, srcConverted.Handle, level);
  43. Attach(FramebufferTarget.DrawFramebuffer, dst.Format, dst.Handle, level);
  44. }
  45. ClearBufferMask mask = GetMask(src.Format);
  46. if ((mask & (ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit)) != 0 || src.Format.IsInteger())
  47. {
  48. linearFilter = false;
  49. }
  50. BlitFramebufferFilter filter = linearFilter
  51. ? BlitFramebufferFilter.Linear
  52. : BlitFramebufferFilter.Nearest;
  53. GL.ReadBuffer(ReadBufferMode.ColorAttachment0);
  54. GL.DrawBuffer(DrawBufferMode.ColorAttachment0);
  55. GL.Disable(EnableCap.RasterizerDiscard);
  56. GL.Disable(IndexedEnableCap.ScissorTest, 0);
  57. GL.BlitFramebuffer(
  58. srcRegion.X1,
  59. srcRegion.Y1,
  60. srcRegion.X2,
  61. srcRegion.Y2,
  62. dstRegion.X1,
  63. dstRegion.Y1,
  64. dstRegion.X2,
  65. dstRegion.Y2,
  66. mask,
  67. filter);
  68. }
  69. if (level < levels - 1)
  70. {
  71. srcRegion = srcRegion.Reduce(1);
  72. dstRegion = dstRegion.Reduce(1);
  73. }
  74. }
  75. Attach(FramebufferTarget.ReadFramebuffer, src.Format, 0);
  76. Attach(FramebufferTarget.DrawFramebuffer, dst.Format, 0);
  77. GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, oldReadFramebufferHandle);
  78. GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, oldDrawFramebufferHandle);
  79. ((Pipeline)_renderer.Pipeline).RestoreScissor0Enable();
  80. ((Pipeline)_renderer.Pipeline).RestoreRasterizerDiscard();
  81. if (srcConverted != src)
  82. {
  83. srcConverted.Dispose();
  84. }
  85. }
  86. public void CopyUnscaled(
  87. ITextureInfo src,
  88. ITextureInfo dst,
  89. int srcLayer,
  90. int dstLayer,
  91. int srcLevel,
  92. int dstLevel)
  93. {
  94. TextureCreateInfo srcInfo = src.Info;
  95. TextureCreateInfo dstInfo = dst.Info;
  96. int srcHandle = src.Handle;
  97. int dstHandle = dst.Handle;
  98. int srcWidth = srcInfo.Width;
  99. int srcHeight = srcInfo.Height;
  100. int srcDepth = srcInfo.GetDepthOrLayers();
  101. int srcLevels = srcInfo.Levels;
  102. int dstWidth = dstInfo.Width;
  103. int dstHeight = dstInfo.Height;
  104. int dstDepth = dstInfo.GetDepthOrLayers();
  105. int dstLevels = dstInfo.Levels;
  106. srcWidth = Math.Max(1, srcWidth >> srcLevel);
  107. srcHeight = Math.Max(1, srcHeight >> srcLevel);
  108. dstWidth = Math.Max(1, dstWidth >> dstLevel);
  109. dstHeight = Math.Max(1, dstHeight >> dstLevel);
  110. if (dstInfo.Target == Target.Texture3D)
  111. {
  112. dstDepth = Math.Max(1, dstDepth >> dstLevel);
  113. }
  114. int blockWidth = 1;
  115. int blockHeight = 1;
  116. bool sizeInBlocks = false;
  117. // When copying from a compressed to a non-compressed format,
  118. // the non-compressed texture will have the size of the texture
  119. // in blocks (not in texels), so we must adjust that size to
  120. // match the size in texels of the compressed texture.
  121. if (!srcInfo.IsCompressed && dstInfo.IsCompressed)
  122. {
  123. srcWidth *= dstInfo.BlockWidth;
  124. srcHeight *= dstInfo.BlockHeight;
  125. blockWidth = dstInfo.BlockWidth;
  126. blockHeight = dstInfo.BlockHeight;
  127. sizeInBlocks = true;
  128. }
  129. else if (srcInfo.IsCompressed && !dstInfo.IsCompressed)
  130. {
  131. dstWidth *= srcInfo.BlockWidth;
  132. dstHeight *= srcInfo.BlockHeight;
  133. blockWidth = srcInfo.BlockWidth;
  134. blockHeight = srcInfo.BlockHeight;
  135. }
  136. int width = Math.Min(srcWidth, dstWidth);
  137. int height = Math.Min(srcHeight, dstHeight);
  138. int depth = Math.Min(srcDepth, dstDepth);
  139. int levels = Math.Min(srcLevels, dstLevels);
  140. for (int level = 0; level < levels; level++)
  141. {
  142. // Stop copy if we are already out of the levels range.
  143. if (level >= srcInfo.Levels || dstLevel + level >= dstInfo.Levels)
  144. {
  145. break;
  146. }
  147. if ((width % blockWidth != 0 || height % blockHeight != 0) && src is TextureView srcView && dst is TextureView dstView)
  148. {
  149. PboCopy(srcView, dstView, srcLayer, dstLayer, srcLevel + level, dstLevel + level, width, height);
  150. }
  151. else
  152. {
  153. int copyWidth = sizeInBlocks ? BitUtils.DivRoundUp(width, blockWidth) : width;
  154. int copyHeight = sizeInBlocks ? BitUtils.DivRoundUp(height, blockHeight) : height;
  155. GL.CopyImageSubData(
  156. srcHandle,
  157. srcInfo.Target.ConvertToImageTarget(),
  158. srcLevel + level,
  159. 0,
  160. 0,
  161. srcLayer,
  162. dstHandle,
  163. dstInfo.Target.ConvertToImageTarget(),
  164. dstLevel + level,
  165. 0,
  166. 0,
  167. dstLayer,
  168. copyWidth,
  169. copyHeight,
  170. depth);
  171. }
  172. width = Math.Max(1, width >> 1);
  173. height = Math.Max(1, height >> 1);
  174. if (srcInfo.Target == Target.Texture3D)
  175. {
  176. depth = Math.Max(1, depth >> 1);
  177. }
  178. }
  179. }
  180. private static FramebufferAttachment AttachmentForFormat(Format format)
  181. {
  182. if (format == Format.D24UnormS8Uint || format == Format.D32FloatS8Uint)
  183. {
  184. return FramebufferAttachment.DepthStencilAttachment;
  185. }
  186. else if (IsDepthOnly(format))
  187. {
  188. return FramebufferAttachment.DepthAttachment;
  189. }
  190. else if (format == Format.S8Uint)
  191. {
  192. return FramebufferAttachment.StencilAttachment;
  193. }
  194. else
  195. {
  196. return FramebufferAttachment.ColorAttachment0;
  197. }
  198. }
  199. private static void Attach(FramebufferTarget target, Format format, int handle, int level = 0)
  200. {
  201. FramebufferAttachment attachment = AttachmentForFormat(format);
  202. GL.FramebufferTexture(target, attachment, handle, level);
  203. }
  204. private static void Attach(FramebufferTarget target, Format format, int handle, int level, int layer)
  205. {
  206. FramebufferAttachment attachment = AttachmentForFormat(format);
  207. GL.FramebufferTextureLayer(target, attachment, handle, level, layer);
  208. }
  209. private static ClearBufferMask GetMask(Format format)
  210. {
  211. if (format == Format.D24UnormS8Uint || format == Format.D32FloatS8Uint)
  212. {
  213. return ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit;
  214. }
  215. else if (IsDepthOnly(format))
  216. {
  217. return ClearBufferMask.DepthBufferBit;
  218. }
  219. else if (format == Format.S8Uint)
  220. {
  221. return ClearBufferMask.StencilBufferBit;
  222. }
  223. else
  224. {
  225. return ClearBufferMask.ColorBufferBit;
  226. }
  227. }
  228. private static bool IsDepthOnly(Format format)
  229. {
  230. return format == Format.D16Unorm ||
  231. format == Format.D24X8Unorm ||
  232. format == Format.D32Float;
  233. }
  234. public TextureView BgraSwap(TextureView from)
  235. {
  236. TextureView to = (TextureView)_renderer.CreateTexture(from.Info, from.ScaleFactor);
  237. EnsurePbo(from);
  238. GL.BindBuffer(BufferTarget.PixelPackBuffer, _copyPboHandle);
  239. from.WriteToPbo(0, forceBgra: true);
  240. GL.BindBuffer(BufferTarget.PixelPackBuffer, 0);
  241. GL.BindBuffer(BufferTarget.PixelUnpackBuffer, _copyPboHandle);
  242. to.ReadFromPbo(0, _copyPboSize);
  243. GL.BindBuffer(BufferTarget.PixelUnpackBuffer, 0);
  244. return to;
  245. }
  246. private TextureView PboCopy(TextureView from, TextureView to, int srcLayer, int dstLayer, int srcLevel, int dstLevel, int width, int height)
  247. {
  248. int dstWidth = width;
  249. int dstHeight = height;
  250. // The size of the source texture.
  251. int unpackWidth = from.Width;
  252. int unpackHeight = from.Height;
  253. if (from.Info.IsCompressed != to.Info.IsCompressed)
  254. {
  255. if (from.Info.IsCompressed)
  256. {
  257. // Dest size is in pixels, but should be in blocks
  258. dstWidth = BitUtils.DivRoundUp(width, from.Info.BlockWidth);
  259. dstHeight = BitUtils.DivRoundUp(height, from.Info.BlockHeight);
  260. // When copying from a compressed texture, the source size must be taken in blocks for unpacking to the uncompressed block texture.
  261. unpackWidth = BitUtils.DivRoundUp(from.Info.Width, from.Info.BlockWidth);
  262. unpackHeight = BitUtils.DivRoundUp(from.Info.Height, from.Info.BlockHeight);
  263. }
  264. else
  265. {
  266. // When copying to a compressed texture, the source size must be scaled by the block width for unpacking on the compressed target.
  267. unpackWidth = from.Info.Width * to.Info.BlockWidth;
  268. unpackHeight = from.Info.Height * to.Info.BlockHeight;
  269. }
  270. }
  271. EnsurePbo(from);
  272. GL.BindBuffer(BufferTarget.PixelPackBuffer, _copyPboHandle);
  273. // The source texture is written out in full, then the destination is taken as a slice from the data using unpack params.
  274. // The offset points to the base at which the requested layer is at.
  275. int offset = from.WriteToPbo2D(0, srcLayer, srcLevel);
  276. // If the destination size is not an exact match for the source unpack parameters, we need to set them to slice the data correctly.
  277. bool slice = (unpackWidth != dstWidth || unpackHeight != dstHeight);
  278. if (slice)
  279. {
  280. // Set unpack parameters to take a slice of width/height:
  281. GL.PixelStore(PixelStoreParameter.UnpackRowLength, unpackWidth);
  282. GL.PixelStore(PixelStoreParameter.UnpackImageHeight, unpackHeight);
  283. if (to.Info.IsCompressed)
  284. {
  285. GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockWidth, to.Info.BlockWidth);
  286. GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockHeight, to.Info.BlockHeight);
  287. GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockDepth, 1);
  288. GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockSize, to.Info.BytesPerPixel);
  289. }
  290. }
  291. GL.BindBuffer(BufferTarget.PixelPackBuffer, 0);
  292. GL.BindBuffer(BufferTarget.PixelUnpackBuffer, _copyPboHandle);
  293. to.ReadFromPbo2D(offset, dstLayer, dstLevel, dstWidth, dstHeight);
  294. if (slice)
  295. {
  296. // Reset unpack parameters
  297. GL.PixelStore(PixelStoreParameter.UnpackRowLength, 0);
  298. GL.PixelStore(PixelStoreParameter.UnpackImageHeight, 0);
  299. if (to.Info.IsCompressed)
  300. {
  301. GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockWidth, 0);
  302. GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockHeight, 0);
  303. GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockDepth, 0);
  304. GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockSize, 0);
  305. }
  306. }
  307. GL.BindBuffer(BufferTarget.PixelUnpackBuffer, 0);
  308. return to;
  309. }
  310. private void EnsurePbo(TextureView view)
  311. {
  312. int requiredSize = 0;
  313. for (int level = 0; level < view.Info.Levels; level++)
  314. {
  315. requiredSize += view.Info.GetMipSize(level);
  316. }
  317. if (_copyPboSize < requiredSize && _copyPboHandle != 0)
  318. {
  319. GL.DeleteBuffer(_copyPboHandle);
  320. _copyPboHandle = 0;
  321. }
  322. if (_copyPboHandle == 0)
  323. {
  324. _copyPboHandle = GL.GenBuffer();
  325. _copyPboSize = requiredSize;
  326. GL.BindBuffer(BufferTarget.PixelPackBuffer, _copyPboHandle);
  327. GL.BufferData(BufferTarget.PixelPackBuffer, requiredSize, IntPtr.Zero, BufferUsageHint.DynamicCopy);
  328. }
  329. }
  330. private int GetSrcFramebufferLazy()
  331. {
  332. if (_srcFramebuffer == 0)
  333. {
  334. _srcFramebuffer = GL.GenFramebuffer();
  335. }
  336. return _srcFramebuffer;
  337. }
  338. private int GetDstFramebufferLazy()
  339. {
  340. if (_dstFramebuffer == 0)
  341. {
  342. _dstFramebuffer = GL.GenFramebuffer();
  343. }
  344. return _dstFramebuffer;
  345. }
  346. public void Dispose()
  347. {
  348. if (_srcFramebuffer != 0)
  349. {
  350. GL.DeleteFramebuffer(_srcFramebuffer);
  351. _srcFramebuffer = 0;
  352. }
  353. if (_dstFramebuffer != 0)
  354. {
  355. GL.DeleteFramebuffer(_dstFramebuffer);
  356. _dstFramebuffer = 0;
  357. }
  358. if (_copyPboHandle != 0)
  359. {
  360. GL.DeleteBuffer(_copyPboHandle);
  361. _copyPboHandle = 0;
  362. }
  363. }
  364. }
  365. }