2019-10-13 08:02:07 +02:00
|
|
|
using Ryujinx.Graphics.GAL;
|
|
|
|
using OpenTK.Graphics.OpenGL;
|
2019-12-31 23:09:49 +01:00
|
|
|
using System;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-05-23 11:46:09 +02:00
|
|
|
namespace Ryujinx.Graphics.OpenGL.Image
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-12-31 23:09:49 +01:00
|
|
|
class TextureCopy : IDisposable
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2020-03-29 05:02:58 +02:00
|
|
|
private readonly Renderer _renderer;
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
private int _srcFramebuffer;
|
|
|
|
private int _dstFramebuffer;
|
|
|
|
|
2020-07-26 05:03:40 +02:00
|
|
|
private int _copyPboHandle;
|
|
|
|
private int _copyPboSize;
|
|
|
|
|
2020-03-29 05:02:58 +02:00
|
|
|
public TextureCopy(Renderer renderer)
|
|
|
|
{
|
|
|
|
_renderer = renderer;
|
|
|
|
}
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
public void Copy(
|
|
|
|
TextureView src,
|
|
|
|
TextureView dst,
|
|
|
|
Extents2D srcRegion,
|
|
|
|
Extents2D dstRegion,
|
|
|
|
bool linearFilter)
|
|
|
|
{
|
2020-07-26 05:03:40 +02:00
|
|
|
TextureView srcConverted = src.Format.IsBgra8() != dst.Format.IsBgra8() ? BgraSwap(src) : src;
|
|
|
|
|
2020-05-24 15:44:12 +02:00
|
|
|
(int oldDrawFramebufferHandle, int oldReadFramebufferHandle) = ((Pipeline)_renderer.Pipeline).GetBoundFramebuffers();
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, GetSrcFramebufferLazy());
|
|
|
|
GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, GetDstFramebufferLazy());
|
|
|
|
|
2020-07-26 05:03:40 +02:00
|
|
|
Attach(FramebufferTarget.ReadFramebuffer, src.Format, srcConverted.Handle);
|
2019-10-13 08:02:07 +02:00
|
|
|
Attach(FramebufferTarget.DrawFramebuffer, dst.Format, dst.Handle);
|
|
|
|
|
|
|
|
ClearBufferMask mask = GetMask(src.Format);
|
|
|
|
|
2020-07-07 04:41:07 +02:00
|
|
|
if ((mask & (ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit)) != 0 || src.Format.IsInteger())
|
|
|
|
{
|
|
|
|
linearFilter = false;
|
|
|
|
}
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
BlitFramebufferFilter filter = linearFilter
|
|
|
|
? BlitFramebufferFilter.Linear
|
|
|
|
: BlitFramebufferFilter.Nearest;
|
|
|
|
|
|
|
|
GL.ReadBuffer(ReadBufferMode.ColorAttachment0);
|
|
|
|
GL.DrawBuffer(DrawBufferMode.ColorAttachment0);
|
|
|
|
|
2020-04-07 11:19:45 +02:00
|
|
|
GL.Disable(EnableCap.RasterizerDiscard);
|
|
|
|
GL.Disable(IndexedEnableCap.ScissorTest, 0);
|
2020-03-29 05:02:58 +02:00
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
GL.BlitFramebuffer(
|
|
|
|
srcRegion.X1,
|
|
|
|
srcRegion.Y1,
|
|
|
|
srcRegion.X2,
|
|
|
|
srcRegion.Y2,
|
|
|
|
dstRegion.X1,
|
|
|
|
dstRegion.Y1,
|
|
|
|
dstRegion.X2,
|
|
|
|
dstRegion.Y2,
|
|
|
|
mask,
|
|
|
|
filter);
|
|
|
|
|
2020-07-07 04:41:07 +02:00
|
|
|
Attach(FramebufferTarget.ReadFramebuffer, src.Format, 0);
|
|
|
|
Attach(FramebufferTarget.DrawFramebuffer, dst.Format, 0);
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, oldReadFramebufferHandle);
|
|
|
|
GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, oldDrawFramebufferHandle);
|
2020-03-29 05:02:58 +02:00
|
|
|
|
2020-04-07 11:19:45 +02:00
|
|
|
((Pipeline)_renderer.Pipeline).RestoreScissor0Enable();
|
|
|
|
((Pipeline)_renderer.Pipeline).RestoreRasterizerDiscard();
|
2020-07-26 05:03:40 +02:00
|
|
|
|
|
|
|
if (srcConverted != src)
|
|
|
|
{
|
|
|
|
srcConverted.Dispose();
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void Attach(FramebufferTarget target, Format format, int handle)
|
|
|
|
{
|
|
|
|
if (format == Format.D24UnormS8Uint || format == Format.D32FloatS8Uint)
|
|
|
|
{
|
|
|
|
GL.FramebufferTexture(target, FramebufferAttachment.DepthStencilAttachment, handle, 0);
|
|
|
|
}
|
|
|
|
else if (IsDepthOnly(format))
|
|
|
|
{
|
|
|
|
GL.FramebufferTexture(target, FramebufferAttachment.DepthAttachment, handle, 0);
|
|
|
|
}
|
|
|
|
else if (format == Format.S8Uint)
|
|
|
|
{
|
|
|
|
GL.FramebufferTexture(target, FramebufferAttachment.StencilAttachment, handle, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GL.FramebufferTexture(target, FramebufferAttachment.ColorAttachment0, handle, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static ClearBufferMask GetMask(Format format)
|
|
|
|
{
|
|
|
|
if (format == Format.D24UnormS8Uint || format == Format.D32FloatS8Uint)
|
|
|
|
{
|
|
|
|
return ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit;
|
|
|
|
}
|
|
|
|
else if (IsDepthOnly(format))
|
|
|
|
{
|
|
|
|
return ClearBufferMask.DepthBufferBit;
|
|
|
|
}
|
|
|
|
else if (format == Format.S8Uint)
|
|
|
|
{
|
|
|
|
return ClearBufferMask.StencilBufferBit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return ClearBufferMask.ColorBufferBit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool IsDepthOnly(Format format)
|
|
|
|
{
|
2020-01-06 23:27:50 +01:00
|
|
|
return format == Format.D16Unorm ||
|
2019-10-13 08:02:07 +02:00
|
|
|
format == Format.D24X8Unorm ||
|
|
|
|
format == Format.D32Float;
|
|
|
|
}
|
|
|
|
|
2020-07-26 05:03:40 +02:00
|
|
|
public TextureView BgraSwap(TextureView from)
|
|
|
|
{
|
2020-10-29 22:57:34 +01:00
|
|
|
TextureView to = (TextureView)_renderer.CreateTexture(from.Info, from.ScaleFactor);
|
2020-07-26 05:03:40 +02:00
|
|
|
|
|
|
|
EnsurePbo(from);
|
|
|
|
|
|
|
|
GL.BindBuffer(BufferTarget.PixelPackBuffer, _copyPboHandle);
|
|
|
|
|
|
|
|
from.WriteToPbo(0, forceBgra: true);
|
|
|
|
|
|
|
|
GL.BindBuffer(BufferTarget.PixelPackBuffer, 0);
|
|
|
|
GL.BindBuffer(BufferTarget.PixelUnpackBuffer, _copyPboHandle);
|
|
|
|
|
|
|
|
to.ReadFromPbo(0, _copyPboSize);
|
|
|
|
|
|
|
|
GL.BindBuffer(BufferTarget.PixelUnpackBuffer, 0);
|
|
|
|
|
|
|
|
return to;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void EnsurePbo(TextureView view)
|
|
|
|
{
|
|
|
|
int requiredSize = 0;
|
|
|
|
|
|
|
|
for (int level = 0; level < view.Info.Levels; level++)
|
|
|
|
{
|
|
|
|
requiredSize += view.Info.GetMipSize(level);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_copyPboSize < requiredSize && _copyPboHandle != 0)
|
|
|
|
{
|
|
|
|
GL.DeleteBuffer(_copyPboHandle);
|
|
|
|
|
|
|
|
_copyPboHandle = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_copyPboHandle == 0)
|
|
|
|
{
|
|
|
|
_copyPboHandle = GL.GenBuffer();
|
|
|
|
_copyPboSize = requiredSize;
|
|
|
|
|
|
|
|
GL.BindBuffer(BufferTarget.PixelPackBuffer, _copyPboHandle);
|
|
|
|
GL.BufferData(BufferTarget.PixelPackBuffer, requiredSize, IntPtr.Zero, BufferUsageHint.DynamicCopy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
private int GetSrcFramebufferLazy()
|
|
|
|
{
|
|
|
|
if (_srcFramebuffer == 0)
|
|
|
|
{
|
|
|
|
_srcFramebuffer = GL.GenFramebuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
return _srcFramebuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int GetDstFramebufferLazy()
|
|
|
|
{
|
|
|
|
if (_dstFramebuffer == 0)
|
|
|
|
{
|
|
|
|
_dstFramebuffer = GL.GenFramebuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
return _dstFramebuffer;
|
|
|
|
}
|
2019-12-31 23:09:49 +01:00
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
if (_srcFramebuffer != 0)
|
|
|
|
{
|
|
|
|
GL.DeleteFramebuffer(_srcFramebuffer);
|
|
|
|
|
|
|
|
_srcFramebuffer = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_dstFramebuffer != 0)
|
|
|
|
{
|
|
|
|
GL.DeleteFramebuffer(_dstFramebuffer);
|
|
|
|
|
|
|
|
_dstFramebuffer = 0;
|
|
|
|
}
|
2020-07-26 05:03:40 +02:00
|
|
|
|
|
|
|
if (_copyPboHandle != 0)
|
|
|
|
{
|
|
|
|
GL.DeleteBuffer(_copyPboHandle);
|
|
|
|
|
|
|
|
_copyPboHandle = 0;
|
|
|
|
}
|
2019-12-31 23:09:49 +01:00
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
}
|