TextureCopy.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 srcDepth = srcInfo.GetDepthOrLayers();
  97. int srcLevels = srcInfo.Levels;
  98. int dstDepth = dstInfo.GetDepthOrLayers();
  99. int dstLevels = dstInfo.Levels;
  100. if (dstInfo.Target == Target.Texture3D)
  101. {
  102. dstDepth = Math.Max(1, dstDepth >> dstLevel);
  103. }
  104. int depth = Math.Min(srcDepth, dstDepth);
  105. int levels = Math.Min(srcLevels, dstLevels);
  106. CopyUnscaled(src, dst, srcLayer, dstLayer, srcLevel, dstLevel, depth, levels);
  107. }
  108. public void CopyUnscaled(
  109. ITextureInfo src,
  110. ITextureInfo dst,
  111. int srcLayer,
  112. int dstLayer,
  113. int srcLevel,
  114. int dstLevel,
  115. int depth,
  116. int levels)
  117. {
  118. TextureCreateInfo srcInfo = src.Info;
  119. TextureCreateInfo dstInfo = dst.Info;
  120. int srcHandle = src.Handle;
  121. int dstHandle = dst.Handle;
  122. int srcWidth = srcInfo.Width;
  123. int srcHeight = srcInfo.Height;
  124. int dstWidth = dstInfo.Width;
  125. int dstHeight = dstInfo.Height;
  126. srcWidth = Math.Max(1, srcWidth >> srcLevel);
  127. srcHeight = Math.Max(1, srcHeight >> srcLevel);
  128. dstWidth = Math.Max(1, dstWidth >> dstLevel);
  129. dstHeight = Math.Max(1, dstHeight >> dstLevel);
  130. int blockWidth = 1;
  131. int blockHeight = 1;
  132. bool sizeInBlocks = false;
  133. // When copying from a compressed to a non-compressed format,
  134. // the non-compressed texture will have the size of the texture
  135. // in blocks (not in texels), so we must adjust that size to
  136. // match the size in texels of the compressed texture.
  137. if (!srcInfo.IsCompressed && dstInfo.IsCompressed)
  138. {
  139. srcWidth *= dstInfo.BlockWidth;
  140. srcHeight *= dstInfo.BlockHeight;
  141. blockWidth = dstInfo.BlockWidth;
  142. blockHeight = dstInfo.BlockHeight;
  143. sizeInBlocks = true;
  144. }
  145. else if (srcInfo.IsCompressed && !dstInfo.IsCompressed)
  146. {
  147. dstWidth *= srcInfo.BlockWidth;
  148. dstHeight *= srcInfo.BlockHeight;
  149. blockWidth = srcInfo.BlockWidth;
  150. blockHeight = srcInfo.BlockHeight;
  151. }
  152. int width = Math.Min(srcWidth, dstWidth);
  153. int height = Math.Min(srcHeight, dstHeight);
  154. for (int level = 0; level < levels; level++)
  155. {
  156. // Stop copy if we are already out of the levels range.
  157. if (level >= srcInfo.Levels || dstLevel + level >= dstInfo.Levels)
  158. {
  159. break;
  160. }
  161. if ((width % blockWidth != 0 || height % blockHeight != 0) && src is TextureView srcView && dst is TextureView dstView)
  162. {
  163. PboCopy(srcView, dstView, srcLayer, dstLayer, srcLevel + level, dstLevel + level, width, height);
  164. }
  165. else
  166. {
  167. int copyWidth = sizeInBlocks ? BitUtils.DivRoundUp(width, blockWidth) : width;
  168. int copyHeight = sizeInBlocks ? BitUtils.DivRoundUp(height, blockHeight) : height;
  169. GL.CopyImageSubData(
  170. srcHandle,
  171. srcInfo.Target.ConvertToImageTarget(),
  172. srcLevel + level,
  173. 0,
  174. 0,
  175. srcLayer,
  176. dstHandle,
  177. dstInfo.Target.ConvertToImageTarget(),
  178. dstLevel + level,
  179. 0,
  180. 0,
  181. dstLayer,
  182. copyWidth,
  183. copyHeight,
  184. depth);
  185. }
  186. width = Math.Max(1, width >> 1);
  187. height = Math.Max(1, height >> 1);
  188. if (srcInfo.Target == Target.Texture3D)
  189. {
  190. depth = Math.Max(1, depth >> 1);
  191. }
  192. }
  193. }
  194. private static FramebufferAttachment AttachmentForFormat(Format format)
  195. {
  196. if (format == Format.D24UnormS8Uint || format == Format.D32FloatS8Uint)
  197. {
  198. return FramebufferAttachment.DepthStencilAttachment;
  199. }
  200. else if (IsDepthOnly(format))
  201. {
  202. return FramebufferAttachment.DepthAttachment;
  203. }
  204. else if (format == Format.S8Uint)
  205. {
  206. return FramebufferAttachment.StencilAttachment;
  207. }
  208. else
  209. {
  210. return FramebufferAttachment.ColorAttachment0;
  211. }
  212. }
  213. private static void Attach(FramebufferTarget target, Format format, int handle, int level = 0)
  214. {
  215. FramebufferAttachment attachment = AttachmentForFormat(format);
  216. GL.FramebufferTexture(target, attachment, handle, level);
  217. }
  218. private static void Attach(FramebufferTarget target, Format format, int handle, int level, int layer)
  219. {
  220. FramebufferAttachment attachment = AttachmentForFormat(format);
  221. GL.FramebufferTextureLayer(target, attachment, handle, level, layer);
  222. }
  223. private static ClearBufferMask GetMask(Format format)
  224. {
  225. if (format == Format.D24UnormS8Uint || format == Format.D32FloatS8Uint)
  226. {
  227. return ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit;
  228. }
  229. else if (IsDepthOnly(format))
  230. {
  231. return ClearBufferMask.DepthBufferBit;
  232. }
  233. else if (format == Format.S8Uint)
  234. {
  235. return ClearBufferMask.StencilBufferBit;
  236. }
  237. else
  238. {
  239. return ClearBufferMask.ColorBufferBit;
  240. }
  241. }
  242. private static bool IsDepthOnly(Format format)
  243. {
  244. return format == Format.D16Unorm ||
  245. format == Format.D24X8Unorm ||
  246. format == Format.D32Float;
  247. }
  248. public TextureView BgraSwap(TextureView from)
  249. {
  250. TextureView to = (TextureView)_renderer.CreateTexture(from.Info, from.ScaleFactor);
  251. EnsurePbo(from);
  252. GL.BindBuffer(BufferTarget.PixelPackBuffer, _copyPboHandle);
  253. from.WriteToPbo(0, forceBgra: true);
  254. GL.BindBuffer(BufferTarget.PixelPackBuffer, 0);
  255. GL.BindBuffer(BufferTarget.PixelUnpackBuffer, _copyPboHandle);
  256. to.ReadFromPbo(0, _copyPboSize);
  257. GL.BindBuffer(BufferTarget.PixelUnpackBuffer, 0);
  258. return to;
  259. }
  260. private TextureView PboCopy(TextureView from, TextureView to, int srcLayer, int dstLayer, int srcLevel, int dstLevel, int width, int height)
  261. {
  262. int dstWidth = width;
  263. int dstHeight = height;
  264. // The size of the source texture.
  265. int unpackWidth = from.Width;
  266. int unpackHeight = from.Height;
  267. if (from.Info.IsCompressed != to.Info.IsCompressed)
  268. {
  269. if (from.Info.IsCompressed)
  270. {
  271. // Dest size is in pixels, but should be in blocks
  272. dstWidth = BitUtils.DivRoundUp(width, from.Info.BlockWidth);
  273. dstHeight = BitUtils.DivRoundUp(height, from.Info.BlockHeight);
  274. // When copying from a compressed texture, the source size must be taken in blocks for unpacking to the uncompressed block texture.
  275. unpackWidth = BitUtils.DivRoundUp(from.Info.Width, from.Info.BlockWidth);
  276. unpackHeight = BitUtils.DivRoundUp(from.Info.Height, from.Info.BlockHeight);
  277. }
  278. else
  279. {
  280. // When copying to a compressed texture, the source size must be scaled by the block width for unpacking on the compressed target.
  281. unpackWidth = from.Info.Width * to.Info.BlockWidth;
  282. unpackHeight = from.Info.Height * to.Info.BlockHeight;
  283. }
  284. }
  285. EnsurePbo(from);
  286. GL.BindBuffer(BufferTarget.PixelPackBuffer, _copyPboHandle);
  287. // The source texture is written out in full, then the destination is taken as a slice from the data using unpack params.
  288. // The offset points to the base at which the requested layer is at.
  289. int offset = from.WriteToPbo2D(0, srcLayer, srcLevel);
  290. // If the destination size is not an exact match for the source unpack parameters, we need to set them to slice the data correctly.
  291. bool slice = (unpackWidth != dstWidth || unpackHeight != dstHeight);
  292. if (slice)
  293. {
  294. // Set unpack parameters to take a slice of width/height:
  295. GL.PixelStore(PixelStoreParameter.UnpackRowLength, unpackWidth);
  296. GL.PixelStore(PixelStoreParameter.UnpackImageHeight, unpackHeight);
  297. if (to.Info.IsCompressed)
  298. {
  299. GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockWidth, to.Info.BlockWidth);
  300. GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockHeight, to.Info.BlockHeight);
  301. GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockDepth, 1);
  302. GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockSize, to.Info.BytesPerPixel);
  303. }
  304. }
  305. GL.BindBuffer(BufferTarget.PixelPackBuffer, 0);
  306. GL.BindBuffer(BufferTarget.PixelUnpackBuffer, _copyPboHandle);
  307. to.ReadFromPbo2D(offset, dstLayer, dstLevel, dstWidth, dstHeight);
  308. if (slice)
  309. {
  310. // Reset unpack parameters
  311. GL.PixelStore(PixelStoreParameter.UnpackRowLength, 0);
  312. GL.PixelStore(PixelStoreParameter.UnpackImageHeight, 0);
  313. if (to.Info.IsCompressed)
  314. {
  315. GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockWidth, 0);
  316. GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockHeight, 0);
  317. GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockDepth, 0);
  318. GL.PixelStore(PixelStoreParameter.UnpackCompressedBlockSize, 0);
  319. }
  320. }
  321. GL.BindBuffer(BufferTarget.PixelUnpackBuffer, 0);
  322. return to;
  323. }
  324. private void EnsurePbo(TextureView view)
  325. {
  326. int requiredSize = 0;
  327. for (int level = 0; level < view.Info.Levels; level++)
  328. {
  329. requiredSize += view.Info.GetMipSize(level);
  330. }
  331. if (_copyPboSize < requiredSize && _copyPboHandle != 0)
  332. {
  333. GL.DeleteBuffer(_copyPboHandle);
  334. _copyPboHandle = 0;
  335. }
  336. if (_copyPboHandle == 0)
  337. {
  338. _copyPboHandle = GL.GenBuffer();
  339. _copyPboSize = requiredSize;
  340. GL.BindBuffer(BufferTarget.PixelPackBuffer, _copyPboHandle);
  341. GL.BufferData(BufferTarget.PixelPackBuffer, requiredSize, IntPtr.Zero, BufferUsageHint.DynamicCopy);
  342. }
  343. }
  344. private int GetSrcFramebufferLazy()
  345. {
  346. if (_srcFramebuffer == 0)
  347. {
  348. _srcFramebuffer = GL.GenFramebuffer();
  349. }
  350. return _srcFramebuffer;
  351. }
  352. private int GetDstFramebufferLazy()
  353. {
  354. if (_dstFramebuffer == 0)
  355. {
  356. _dstFramebuffer = GL.GenFramebuffer();
  357. }
  358. return _dstFramebuffer;
  359. }
  360. public void Dispose()
  361. {
  362. if (_srcFramebuffer != 0)
  363. {
  364. GL.DeleteFramebuffer(_srcFramebuffer);
  365. _srcFramebuffer = 0;
  366. }
  367. if (_dstFramebuffer != 0)
  368. {
  369. GL.DeleteFramebuffer(_dstFramebuffer);
  370. _dstFramebuffer = 0;
  371. }
  372. if (_copyPboHandle != 0)
  373. {
  374. GL.DeleteBuffer(_copyPboHandle);
  375. _copyPboHandle = 0;
  376. }
  377. }
  378. }
  379. }