2018-04-08 20:17:35 +01:00
|
|
|
using OpenTK.Graphics.OpenGL;
|
2018-09-08 18:51:50 +01:00
|
|
|
using Ryujinx.Graphics.Texture;
|
2018-04-08 20:17:35 +01:00
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gal.OpenGL
|
|
|
|
{
|
2018-09-08 18:51:50 +01:00
|
|
|
class OGLRenderTarget : IGalRenderTarget
|
2018-04-08 20:17:35 +01:00
|
|
|
{
|
2018-10-23 21:59:52 +01:00
|
|
|
private const int NativeWidth = 1280;
|
|
|
|
private const int NativeHeight = 720;
|
|
|
|
|
|
|
|
private const int RenderTargetsCount = GalPipelineState.RenderTargetsCount;
|
|
|
|
|
2018-04-14 04:39:24 +01:00
|
|
|
private struct Rect
|
|
|
|
{
|
|
|
|
public int X { get; private set; }
|
|
|
|
public int Y { get; private set; }
|
|
|
|
public int Width { get; private set; }
|
|
|
|
public int Height { get; private set; }
|
|
|
|
|
|
|
|
public Rect(int X, int Y, int Width, int Height)
|
|
|
|
{
|
2018-09-18 05:30:35 +01:00
|
|
|
this.X = X;
|
|
|
|
this.Y = Y;
|
|
|
|
this.Width = Width;
|
2018-04-14 04:39:24 +01:00
|
|
|
this.Height = Height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-25 23:55:30 +01:00
|
|
|
private class FrameBufferAttachments
|
|
|
|
{
|
2018-10-23 21:59:52 +01:00
|
|
|
public int MapCount { get; set; }
|
|
|
|
|
|
|
|
public DrawBuffersEnum[] Map { get; private set; }
|
|
|
|
|
|
|
|
public long[] Colors { get; private set; }
|
2018-09-25 23:55:30 +01:00
|
|
|
|
2018-10-23 21:59:52 +01:00
|
|
|
public long Zeta { get; set; }
|
2018-09-25 23:55:30 +01:00
|
|
|
|
|
|
|
public FrameBufferAttachments()
|
|
|
|
{
|
|
|
|
Colors = new long[RenderTargetsCount];
|
|
|
|
|
|
|
|
Map = new DrawBuffersEnum[RenderTargetsCount];
|
|
|
|
}
|
|
|
|
|
2018-10-23 21:59:52 +01:00
|
|
|
public void Update(FrameBufferAttachments Source)
|
2018-09-25 23:55:30 +01:00
|
|
|
{
|
2018-10-23 21:59:52 +01:00
|
|
|
for (int Index = 0; Index < RenderTargetsCount; Index++)
|
2018-09-25 23:55:30 +01:00
|
|
|
{
|
2018-10-23 21:59:52 +01:00
|
|
|
Map[Index] = Source.Map[Index];
|
2018-09-25 23:55:30 +01:00
|
|
|
|
2018-10-23 21:59:52 +01:00
|
|
|
Colors[Index] = Source.Colors[Index];
|
2018-09-25 23:55:30 +01:00
|
|
|
}
|
2018-10-23 21:59:52 +01:00
|
|
|
|
|
|
|
MapCount = Source.MapCount;
|
|
|
|
Zeta = Source.Zeta;
|
2018-09-25 23:55:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 21:59:52 +01:00
|
|
|
private int[] ColorHandles;
|
|
|
|
private int ZetaHandle;
|
2018-04-08 20:17:35 +01:00
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
private OGLTexture Texture;
|
2018-04-14 04:39:24 +01:00
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
private ImageHandler ReadTex;
|
2018-04-08 20:17:35 +01:00
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
private Rect Window;
|
2018-04-13 19:12:58 +01:00
|
|
|
|
2018-10-23 21:59:52 +01:00
|
|
|
private float[] Viewports;
|
|
|
|
|
2018-07-23 15:21:05 +01:00
|
|
|
private bool FlipX;
|
|
|
|
private bool FlipY;
|
2018-04-13 19:12:58 +01:00
|
|
|
|
2018-07-23 15:21:05 +01:00
|
|
|
private int CropTop;
|
|
|
|
private int CropLeft;
|
|
|
|
private int CropRight;
|
|
|
|
private int CropBottom;
|
2018-04-08 20:17:35 +01:00
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
//This framebuffer is used to attach guest rendertargets,
|
|
|
|
//think of it as a dummy OpenGL VAO
|
|
|
|
private int DummyFrameBuffer;
|
2018-04-08 20:17:35 +01:00
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
//These framebuffers are used to blit images
|
|
|
|
private int SrcFb;
|
|
|
|
private int DstFb;
|
2018-04-08 20:17:35 +01:00
|
|
|
|
2018-09-25 23:55:30 +01:00
|
|
|
private FrameBufferAttachments Attachments;
|
|
|
|
private FrameBufferAttachments OldAttachments;
|
2018-04-08 20:17:35 +01:00
|
|
|
|
2018-09-19 22:26:49 +01:00
|
|
|
private int CopyPBO;
|
|
|
|
|
2018-11-28 23:09:44 +00:00
|
|
|
public bool FramebufferSrgb { get; set; }
|
|
|
|
|
2018-09-08 18:51:50 +01:00
|
|
|
public OGLRenderTarget(OGLTexture Texture)
|
2018-08-20 02:25:26 +01:00
|
|
|
{
|
2018-09-25 23:55:30 +01:00
|
|
|
Attachments = new FrameBufferAttachments();
|
|
|
|
|
|
|
|
OldAttachments = new FrameBufferAttachments();
|
|
|
|
|
2018-10-23 21:59:52 +01:00
|
|
|
ColorHandles = new int[RenderTargetsCount];
|
|
|
|
|
2018-09-25 23:55:30 +01:00
|
|
|
Viewports = new float[RenderTargetsCount * 4];
|
2018-04-08 20:17:35 +01:00
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
this.Texture = Texture;
|
2018-10-23 21:59:52 +01:00
|
|
|
|
|
|
|
Texture.TextureDeleted += TextureDeletionHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void TextureDeletionHandler(object Sender, int Handle)
|
|
|
|
{
|
|
|
|
//Texture was deleted, the handle is no longer valid, so
|
|
|
|
//reset all uses of this handle on a render target.
|
|
|
|
for (int Attachment = 0; Attachment < RenderTargetsCount; Attachment++)
|
|
|
|
{
|
|
|
|
if (ColorHandles[Attachment] == Handle)
|
|
|
|
{
|
|
|
|
ColorHandles[Attachment] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ZetaHandle == Handle)
|
|
|
|
{
|
|
|
|
ZetaHandle = 0;
|
|
|
|
}
|
2018-08-20 02:25:26 +01:00
|
|
|
}
|
2018-04-08 20:17:35 +01:00
|
|
|
|
2018-09-25 23:55:30 +01:00
|
|
|
public void Bind()
|
2018-08-20 02:25:26 +01:00
|
|
|
{
|
2018-09-25 23:55:30 +01:00
|
|
|
if (DummyFrameBuffer == 0)
|
2018-08-20 02:25:26 +01:00
|
|
|
{
|
2018-09-25 23:55:30 +01:00
|
|
|
DummyFrameBuffer = GL.GenFramebuffer();
|
2018-08-20 02:25:26 +01:00
|
|
|
}
|
2018-04-08 20:17:35 +01:00
|
|
|
|
2018-09-25 23:55:30 +01:00
|
|
|
GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, DummyFrameBuffer);
|
2018-04-08 20:17:35 +01:00
|
|
|
|
2018-09-25 23:55:30 +01:00
|
|
|
ImageHandler CachedImage;
|
2018-09-18 05:30:35 +01:00
|
|
|
|
2018-09-25 23:55:30 +01:00
|
|
|
for (int Attachment = 0; Attachment < RenderTargetsCount; Attachment++)
|
2018-08-20 02:25:26 +01:00
|
|
|
{
|
2018-10-17 22:02:23 +01:00
|
|
|
long Key = Attachments.Colors[Attachment];
|
|
|
|
|
2018-09-25 23:55:30 +01:00
|
|
|
int Handle = 0;
|
|
|
|
|
2018-10-17 22:02:23 +01:00
|
|
|
if (Key != 0 && Texture.TryGetImageHandler(Key, out CachedImage))
|
2018-09-25 23:55:30 +01:00
|
|
|
{
|
|
|
|
Handle = CachedImage.Handle;
|
|
|
|
}
|
|
|
|
|
2018-10-23 21:59:52 +01:00
|
|
|
if (Handle == ColorHandles[Attachment])
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-09-25 23:55:30 +01:00
|
|
|
GL.FramebufferTexture(
|
|
|
|
FramebufferTarget.DrawFramebuffer,
|
|
|
|
FramebufferAttachment.ColorAttachment0 + Attachment,
|
|
|
|
Handle,
|
|
|
|
0);
|
2018-10-23 21:59:52 +01:00
|
|
|
|
|
|
|
ColorHandles[Attachment] = Handle;
|
2018-09-25 23:55:30 +01:00
|
|
|
}
|
2018-04-08 20:17:35 +01:00
|
|
|
|
2018-10-23 21:59:52 +01:00
|
|
|
if (Attachments.Zeta != 0 && Texture.TryGetImageHandler(Attachments.Zeta, out CachedImage))
|
2018-09-25 23:55:30 +01:00
|
|
|
{
|
2018-10-23 21:59:52 +01:00
|
|
|
if (CachedImage.Handle != ZetaHandle)
|
2018-08-20 02:25:26 +01:00
|
|
|
{
|
2018-09-25 23:55:30 +01:00
|
|
|
if (CachedImage.HasDepth && CachedImage.HasStencil)
|
2018-08-20 02:25:26 +01:00
|
|
|
{
|
|
|
|
GL.FramebufferTexture(
|
|
|
|
FramebufferTarget.DrawFramebuffer,
|
|
|
|
FramebufferAttachment.DepthStencilAttachment,
|
2018-09-18 05:30:35 +01:00
|
|
|
CachedImage.Handle,
|
2018-08-20 02:25:26 +01:00
|
|
|
0);
|
|
|
|
}
|
2018-09-25 23:55:30 +01:00
|
|
|
else if (CachedImage.HasDepth)
|
|
|
|
{
|
|
|
|
GL.FramebufferTexture(
|
|
|
|
FramebufferTarget.DrawFramebuffer,
|
|
|
|
FramebufferAttachment.DepthAttachment,
|
|
|
|
CachedImage.Handle,
|
|
|
|
0);
|
2018-04-13 19:12:58 +01:00
|
|
|
|
2018-09-25 23:55:30 +01:00
|
|
|
GL.FramebufferTexture(
|
|
|
|
FramebufferTarget.DrawFramebuffer,
|
|
|
|
FramebufferAttachment.StencilAttachment,
|
|
|
|
0,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-23 21:59:52 +01:00
|
|
|
throw new InvalidOperationException("Invalid image format \"" + CachedImage.Format + "\" used as Zeta!");
|
2018-09-25 23:55:30 +01:00
|
|
|
}
|
2018-10-23 21:59:52 +01:00
|
|
|
|
|
|
|
ZetaHandle = CachedImage.Handle;
|
2018-08-20 02:25:26 +01:00
|
|
|
}
|
|
|
|
}
|
2018-10-23 21:59:52 +01:00
|
|
|
else if (ZetaHandle != 0)
|
|
|
|
{
|
|
|
|
GL.FramebufferTexture(
|
|
|
|
FramebufferTarget.DrawFramebuffer,
|
|
|
|
FramebufferAttachment.DepthStencilAttachment,
|
|
|
|
0,
|
|
|
|
0);
|
|
|
|
|
|
|
|
ZetaHandle = 0;
|
|
|
|
}
|
2018-09-25 23:55:30 +01:00
|
|
|
|
|
|
|
if (OGLExtension.ViewportArray)
|
|
|
|
{
|
2018-10-17 22:02:23 +01:00
|
|
|
GL.ViewportArray(0, RenderTargetsCount, Viewports);
|
2018-09-25 23:55:30 +01:00
|
|
|
}
|
2018-08-20 02:25:26 +01:00
|
|
|
else
|
|
|
|
{
|
2018-09-25 23:55:30 +01:00
|
|
|
GL.Viewport(
|
|
|
|
(int)Viewports[0],
|
|
|
|
(int)Viewports[1],
|
|
|
|
(int)Viewports[2],
|
|
|
|
(int)Viewports[3]);
|
2018-08-20 02:25:26 +01:00
|
|
|
}
|
2018-04-08 20:17:35 +01:00
|
|
|
|
2018-09-25 23:55:30 +01:00
|
|
|
if (Attachments.MapCount > 1)
|
2018-04-08 20:17:35 +01:00
|
|
|
{
|
2018-09-25 23:55:30 +01:00
|
|
|
GL.DrawBuffers(Attachments.MapCount, Attachments.Map);
|
2018-04-08 20:17:35 +01:00
|
|
|
}
|
2018-09-25 23:55:30 +01:00
|
|
|
else if (Attachments.MapCount == 1)
|
|
|
|
{
|
|
|
|
GL.DrawBuffer((DrawBufferMode)Attachments.Map[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GL.DrawBuffer(DrawBufferMode.None);
|
|
|
|
}
|
|
|
|
|
2018-10-23 21:59:52 +01:00
|
|
|
OldAttachments.Update(Attachments);
|
2018-04-13 19:12:58 +01:00
|
|
|
}
|
2018-04-08 20:17:35 +01:00
|
|
|
|
2018-09-25 23:55:30 +01:00
|
|
|
public void BindColor(long Key, int Attachment)
|
2018-04-13 19:12:58 +01:00
|
|
|
{
|
2018-09-25 23:55:30 +01:00
|
|
|
Attachments.Colors[Attachment] = Key;
|
|
|
|
}
|
2018-09-18 05:30:35 +01:00
|
|
|
|
2018-09-25 23:55:30 +01:00
|
|
|
public void UnbindColor(int Attachment)
|
|
|
|
{
|
|
|
|
Attachments.Colors[Attachment] = 0;
|
|
|
|
}
|
2018-04-13 19:12:58 +01:00
|
|
|
|
2018-09-25 23:55:30 +01:00
|
|
|
public void BindZeta(long Key)
|
|
|
|
{
|
|
|
|
Attachments.Zeta = Key;
|
|
|
|
}
|
2018-10-13 02:37:01 +01:00
|
|
|
|
2018-09-25 23:55:30 +01:00
|
|
|
public void UnbindZeta()
|
|
|
|
{
|
|
|
|
Attachments.Zeta = 0;
|
2018-04-08 20:17:35 +01:00
|
|
|
}
|
|
|
|
|
2018-09-25 23:55:30 +01:00
|
|
|
public void Present(long Key)
|
2018-04-08 20:17:35 +01:00
|
|
|
{
|
2018-09-18 05:30:35 +01:00
|
|
|
Texture.TryGetImageHandler(Key, out ReadTex);
|
2018-04-13 19:12:58 +01:00
|
|
|
}
|
|
|
|
|
2018-08-23 06:07:23 +01:00
|
|
|
public void SetMap(int[] Map)
|
|
|
|
{
|
2018-09-25 23:55:30 +01:00
|
|
|
if (Map != null)
|
2018-08-23 06:07:23 +01:00
|
|
|
{
|
2018-09-25 23:55:30 +01:00
|
|
|
Attachments.MapCount = Map.Length;
|
2018-08-23 06:07:23 +01:00
|
|
|
|
2018-09-25 23:55:30 +01:00
|
|
|
for (int Attachment = 0; Attachment < Attachments.MapCount; Attachment++)
|
2018-08-23 06:07:23 +01:00
|
|
|
{
|
2018-09-25 23:55:30 +01:00
|
|
|
Attachments.Map[Attachment] = DrawBuffersEnum.ColorAttachment0 + Map[Attachment];
|
2018-08-23 06:07:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-25 23:55:30 +01:00
|
|
|
Attachments.MapCount = 0;
|
2018-08-23 06:07:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-23 15:21:05 +01:00
|
|
|
public void SetTransform(bool FlipX, bool FlipY, int Top, int Left, int Right, int Bottom)
|
2018-04-13 19:12:58 +01:00
|
|
|
{
|
2018-07-23 15:21:05 +01:00
|
|
|
this.FlipX = FlipX;
|
|
|
|
this.FlipY = FlipY;
|
2018-06-24 01:39:25 +01:00
|
|
|
|
2018-07-23 15:21:05 +01:00
|
|
|
CropTop = Top;
|
|
|
|
CropLeft = Left;
|
|
|
|
CropRight = Right;
|
|
|
|
CropBottom = Bottom;
|
2018-04-13 19:12:58 +01:00
|
|
|
}
|
|
|
|
|
2018-04-14 05:14:42 +01:00
|
|
|
public void SetWindowSize(int Width, int Height)
|
|
|
|
{
|
2018-04-14 05:31:27 +01:00
|
|
|
Window = new Rect(0, 0, Width, Height);
|
2018-04-14 05:14:42 +01:00
|
|
|
}
|
|
|
|
|
2018-09-25 23:55:30 +01:00
|
|
|
public void SetViewport(int Attachment, int X, int Y, int Width, int Height)
|
2018-04-14 04:39:24 +01:00
|
|
|
{
|
2018-09-25 23:55:30 +01:00
|
|
|
int Offset = Attachment * 4;
|
2018-07-19 06:30:21 +01:00
|
|
|
|
2018-09-25 23:55:30 +01:00
|
|
|
Viewports[Offset + 0] = X;
|
|
|
|
Viewports[Offset + 1] = Y;
|
|
|
|
Viewports[Offset + 2] = Width;
|
|
|
|
Viewports[Offset + 3] = Height;
|
2018-04-14 04:39:24 +01:00
|
|
|
}
|
|
|
|
|
2018-04-13 19:12:58 +01:00
|
|
|
public void Render()
|
|
|
|
{
|
2018-08-20 02:25:26 +01:00
|
|
|
if (ReadTex == null)
|
2018-04-13 19:12:58 +01:00
|
|
|
{
|
2018-08-20 02:25:26 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-07-07 03:40:12 +01:00
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
int SrcX0, SrcX1, SrcY0, SrcY1;
|
2018-07-07 03:40:12 +01:00
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
if (CropLeft == 0 && CropRight == 0)
|
|
|
|
{
|
|
|
|
SrcX0 = 0;
|
|
|
|
SrcX1 = ReadTex.Width;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SrcX0 = CropLeft;
|
|
|
|
SrcX1 = CropRight;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CropTop == 0 && CropBottom == 0)
|
|
|
|
{
|
|
|
|
SrcY0 = 0;
|
|
|
|
SrcY1 = ReadTex.Height;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SrcY0 = CropTop;
|
|
|
|
SrcY1 = CropBottom;
|
|
|
|
}
|
2018-07-07 03:40:12 +01:00
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
float RatioX = MathF.Min(1f, (Window.Height * (float)NativeWidth) / ((float)NativeHeight * Window.Width));
|
|
|
|
float RatioY = MathF.Min(1f, (Window.Width * (float)NativeHeight) / ((float)NativeWidth * Window.Height));
|
2018-04-14 02:42:55 +01:00
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
int DstWidth = (int)(Window.Width * RatioX);
|
|
|
|
int DstHeight = (int)(Window.Height * RatioY);
|
2018-04-14 02:42:55 +01:00
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
int DstPaddingX = (Window.Width - DstWidth) / 2;
|
|
|
|
int DstPaddingY = (Window.Height - DstHeight) / 2;
|
2018-04-13 19:12:58 +01:00
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
int DstX0 = FlipX ? Window.Width - DstPaddingX : DstPaddingX;
|
|
|
|
int DstX1 = FlipX ? DstPaddingX : Window.Width - DstPaddingX;
|
2018-04-13 19:12:58 +01:00
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
int DstY0 = FlipY ? DstPaddingY : Window.Height - DstPaddingY;
|
|
|
|
int DstY1 = FlipY ? Window.Height - DstPaddingY : DstPaddingY;
|
2018-04-13 19:12:58 +01:00
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
GL.Viewport(0, 0, Window.Width, Window.Height);
|
2018-04-13 19:12:58 +01:00
|
|
|
|
2018-09-18 05:30:35 +01:00
|
|
|
if (SrcFb == 0)
|
|
|
|
{
|
|
|
|
SrcFb = GL.GenFramebuffer();
|
|
|
|
}
|
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, SrcFb);
|
2018-09-18 05:30:35 +01:00
|
|
|
GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, 0);
|
2018-04-13 19:12:58 +01:00
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
GL.FramebufferTexture(FramebufferTarget.ReadFramebuffer, FramebufferAttachment.ColorAttachment0, ReadTex.Handle, 0);
|
|
|
|
|
|
|
|
GL.ReadBuffer(ReadBufferMode.ColorAttachment0);
|
|
|
|
|
|
|
|
GL.Clear(ClearBufferMask.ColorBufferBit);
|
|
|
|
|
2018-11-28 23:09:44 +00:00
|
|
|
GL.Disable(EnableCap.FramebufferSrgb);
|
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
GL.BlitFramebuffer(
|
2018-11-28 23:09:44 +00:00
|
|
|
SrcX0,
|
|
|
|
SrcY0,
|
|
|
|
SrcX1,
|
|
|
|
SrcY1,
|
|
|
|
DstX0,
|
|
|
|
DstY0,
|
|
|
|
DstX1,
|
|
|
|
DstY1,
|
2018-09-18 05:30:35 +01:00
|
|
|
ClearBufferMask.ColorBufferBit,
|
|
|
|
BlitFramebufferFilter.Linear);
|
2018-11-28 23:09:44 +00:00
|
|
|
|
|
|
|
if (FramebufferSrgb)
|
|
|
|
{
|
|
|
|
GL.Enable(EnableCap.FramebufferSrgb);
|
|
|
|
}
|
2018-04-08 20:17:35 +01:00
|
|
|
}
|
|
|
|
|
2018-07-19 06:30:21 +01:00
|
|
|
public void Copy(
|
2019-02-28 01:12:24 +00:00
|
|
|
GalImage SrcImage,
|
|
|
|
GalImage DstImage,
|
|
|
|
long SrcKey,
|
|
|
|
long DstKey,
|
|
|
|
int SrcLayer,
|
|
|
|
int DstLayer,
|
|
|
|
int SrcX0,
|
|
|
|
int SrcY0,
|
|
|
|
int SrcX1,
|
|
|
|
int SrcY1,
|
|
|
|
int DstX0,
|
|
|
|
int DstY0,
|
|
|
|
int DstX1,
|
|
|
|
int DstY1)
|
2018-07-19 06:30:21 +01:00
|
|
|
{
|
2018-09-18 05:30:35 +01:00
|
|
|
if (Texture.TryGetImageHandler(SrcKey, out ImageHandler SrcTex) &&
|
|
|
|
Texture.TryGetImageHandler(DstKey, out ImageHandler DstTex))
|
2018-07-19 06:30:21 +01:00
|
|
|
{
|
2018-09-18 05:30:35 +01:00
|
|
|
if (SrcTex.HasColor != DstTex.HasColor ||
|
|
|
|
SrcTex.HasDepth != DstTex.HasDepth ||
|
2018-08-20 02:25:26 +01:00
|
|
|
SrcTex.HasStencil != DstTex.HasStencil)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
2018-07-19 06:30:21 +01:00
|
|
|
|
2018-09-18 05:30:35 +01:00
|
|
|
if (SrcFb == 0)
|
2018-08-20 02:25:26 +01:00
|
|
|
{
|
2018-09-18 05:30:35 +01:00
|
|
|
SrcFb = GL.GenFramebuffer();
|
2018-08-20 02:25:26 +01:00
|
|
|
}
|
2018-09-18 05:30:35 +01:00
|
|
|
|
|
|
|
if (DstFb == 0)
|
2018-08-20 02:25:26 +01:00
|
|
|
{
|
2018-09-18 05:30:35 +01:00
|
|
|
DstFb = GL.GenFramebuffer();
|
2018-08-20 02:25:26 +01:00
|
|
|
}
|
2018-09-18 05:30:35 +01:00
|
|
|
|
|
|
|
GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, SrcFb);
|
|
|
|
GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, DstFb);
|
|
|
|
|
|
|
|
FramebufferAttachment Attachment = GetAttachment(SrcTex);
|
|
|
|
|
2019-02-28 01:12:24 +00:00
|
|
|
if (ImageUtils.IsArray(SrcImage.TextureTarget) && SrcLayer > 0)
|
|
|
|
{
|
|
|
|
GL.FramebufferTextureLayer(FramebufferTarget.ReadFramebuffer, Attachment, SrcTex.Handle, 0, SrcLayer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GL.FramebufferTexture(FramebufferTarget.ReadFramebuffer, Attachment, SrcTex.Handle, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImageUtils.IsArray(DstImage.TextureTarget) && DstLayer > 0)
|
|
|
|
{
|
|
|
|
GL.FramebufferTextureLayer(FramebufferTarget.DrawFramebuffer, Attachment, DstTex.Handle, 0, DstLayer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GL.FramebufferTexture(FramebufferTarget.DrawFramebuffer, Attachment, DstTex.Handle, 0);
|
|
|
|
}
|
|
|
|
|
2018-09-18 05:30:35 +01:00
|
|
|
|
|
|
|
BlitFramebufferFilter Filter = BlitFramebufferFilter.Nearest;
|
|
|
|
|
|
|
|
if (SrcTex.HasColor)
|
2018-08-20 02:25:26 +01:00
|
|
|
{
|
2018-09-18 05:30:35 +01:00
|
|
|
GL.DrawBuffer(DrawBufferMode.ColorAttachment0);
|
|
|
|
|
|
|
|
Filter = BlitFramebufferFilter.Linear;
|
2018-08-20 02:25:26 +01:00
|
|
|
}
|
2018-07-19 06:30:21 +01:00
|
|
|
|
2018-09-18 05:30:35 +01:00
|
|
|
ClearBufferMask Mask = GetClearMask(SrcTex);
|
2018-04-26 03:11:26 +01:00
|
|
|
|
2018-09-18 05:30:35 +01:00
|
|
|
GL.BlitFramebuffer(SrcX0, SrcY0, SrcX1, SrcY1, DstX0, DstY0, DstX1, DstY1, Mask, Filter);
|
2018-04-26 03:11:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 05:30:35 +01:00
|
|
|
public void Reinterpret(long Key, GalImage NewImage)
|
2018-04-14 04:39:24 +01:00
|
|
|
{
|
2018-09-18 05:30:35 +01:00
|
|
|
if (!Texture.TryGetImage(Key, out GalImage OldImage))
|
2018-07-19 06:30:21 +01:00
|
|
|
{
|
2018-09-18 05:30:35 +01:00
|
|
|
return;
|
2018-07-19 06:30:21 +01:00
|
|
|
}
|
2018-04-14 04:39:24 +01:00
|
|
|
|
2018-11-28 23:09:44 +00:00
|
|
|
if (NewImage.Format == OldImage.Format &&
|
|
|
|
NewImage.Width == OldImage.Width &&
|
2019-02-28 01:12:24 +00:00
|
|
|
NewImage.Height == OldImage.Height &&
|
|
|
|
NewImage.Depth == OldImage.Depth &&
|
|
|
|
NewImage.LayerCount == OldImage.LayerCount &&
|
|
|
|
NewImage.TextureTarget == OldImage.TextureTarget)
|
2018-04-08 20:17:35 +01:00
|
|
|
{
|
2018-09-18 05:30:35 +01:00
|
|
|
return;
|
2018-08-20 02:25:26 +01:00
|
|
|
}
|
2018-04-08 20:17:35 +01:00
|
|
|
|
2018-09-19 22:26:49 +01:00
|
|
|
if (CopyPBO == 0)
|
|
|
|
{
|
|
|
|
CopyPBO = GL.GenBuffer();
|
|
|
|
}
|
2018-09-18 05:30:35 +01:00
|
|
|
|
2018-09-19 22:26:49 +01:00
|
|
|
GL.BindBuffer(BufferTarget.PixelPackBuffer, CopyPBO);
|
2018-09-18 05:30:35 +01:00
|
|
|
|
2018-11-28 23:09:44 +00:00
|
|
|
//The buffer should be large enough to hold the largest texture.
|
|
|
|
int BufferSize = Math.Max(ImageUtils.GetSize(OldImage),
|
|
|
|
ImageUtils.GetSize(NewImage));
|
|
|
|
|
|
|
|
GL.BufferData(BufferTarget.PixelPackBuffer, BufferSize, IntPtr.Zero, BufferUsageHint.StreamCopy);
|
2018-04-08 20:17:35 +01:00
|
|
|
|
2018-09-18 05:30:35 +01:00
|
|
|
if (!Texture.TryGetImageHandler(Key, out ImageHandler CachedImage))
|
2018-08-20 02:25:26 +01:00
|
|
|
{
|
2018-09-19 22:26:49 +01:00
|
|
|
throw new InvalidOperationException();
|
2018-09-18 05:30:35 +01:00
|
|
|
}
|
2018-04-13 19:12:58 +01:00
|
|
|
|
2018-09-19 22:26:49 +01:00
|
|
|
(_, PixelFormat Format, PixelType Type) = OGLEnumConverter.GetImageFormat(CachedImage.Format);
|
2018-09-18 05:30:35 +01:00
|
|
|
|
2019-02-28 01:12:24 +00:00
|
|
|
TextureTarget Target = ImageUtils.GetTextureTarget(NewImage.TextureTarget);
|
|
|
|
|
|
|
|
GL.BindTexture(Target, CachedImage.Handle);
|
2018-09-18 05:30:35 +01:00
|
|
|
|
2019-02-28 01:12:24 +00:00
|
|
|
GL.GetTexImage(Target, 0, Format, Type, IntPtr.Zero);
|
2018-04-13 19:12:58 +01:00
|
|
|
|
2018-09-19 22:26:49 +01:00
|
|
|
GL.BindBuffer(BufferTarget.PixelPackBuffer, 0);
|
|
|
|
GL.BindBuffer(BufferTarget.PixelUnpackBuffer, CopyPBO);
|
2018-09-18 05:30:35 +01:00
|
|
|
|
2018-11-28 23:09:44 +00:00
|
|
|
GL.PixelStore(PixelStoreParameter.UnpackRowLength, OldImage.Width);
|
|
|
|
|
2018-09-19 22:26:49 +01:00
|
|
|
Texture.Create(Key, ImageUtils.GetSize(NewImage), NewImage);
|
2018-09-18 05:30:35 +01:00
|
|
|
|
2018-11-28 23:09:44 +00:00
|
|
|
GL.PixelStore(PixelStoreParameter.UnpackRowLength, 0);
|
|
|
|
|
2018-09-19 22:26:49 +01:00
|
|
|
GL.BindBuffer(BufferTarget.PixelUnpackBuffer, 0);
|
2018-09-18 05:30:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static FramebufferAttachment GetAttachment(ImageHandler CachedImage)
|
|
|
|
{
|
|
|
|
if (CachedImage.HasColor)
|
2018-08-20 02:25:26 +01:00
|
|
|
{
|
2018-09-18 05:30:35 +01:00
|
|
|
return FramebufferAttachment.ColorAttachment0;
|
|
|
|
}
|
|
|
|
else if (CachedImage.HasDepth && CachedImage.HasStencil)
|
|
|
|
{
|
|
|
|
return FramebufferAttachment.DepthStencilAttachment;
|
|
|
|
}
|
|
|
|
else if (CachedImage.HasDepth)
|
|
|
|
{
|
|
|
|
return FramebufferAttachment.DepthAttachment;
|
|
|
|
}
|
|
|
|
else if (CachedImage.HasStencil)
|
|
|
|
{
|
|
|
|
return FramebufferAttachment.StencilAttachment;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException();
|
2018-08-20 02:25:26 +01:00
|
|
|
}
|
2018-09-18 05:30:35 +01:00
|
|
|
}
|
2018-04-13 19:12:58 +01:00
|
|
|
|
2018-09-18 05:30:35 +01:00
|
|
|
private static ClearBufferMask GetClearMask(ImageHandler CachedImage)
|
|
|
|
{
|
|
|
|
return (CachedImage.HasColor ? ClearBufferMask.ColorBufferBit : 0) |
|
|
|
|
(CachedImage.HasDepth ? ClearBufferMask.DepthBufferBit : 0) |
|
|
|
|
(CachedImage.HasStencil ? ClearBufferMask.StencilBufferBit : 0);
|
|
|
|
}
|
2018-04-08 20:17:35 +01:00
|
|
|
}
|
|
|
|
}
|