2018-08-19 18:25:26 -07:00
|
|
|
|
using OpenTK.Graphics.OpenGL;
|
2018-09-08 10:51:50 -07:00
|
|
|
|
using Ryujinx.Graphics.Texture;
|
2018-08-19 18:25:26 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gal.OpenGL
|
|
|
|
|
{
|
|
|
|
|
class ImageHandler
|
|
|
|
|
{
|
|
|
|
|
private static int CopyBuffer = 0;
|
|
|
|
|
private static int CopyBufferSize = 0;
|
|
|
|
|
|
|
|
|
|
public GalImage Image { get; private set; }
|
|
|
|
|
|
|
|
|
|
public int Width => Image.Width;
|
|
|
|
|
public int Height => Image.Height;
|
|
|
|
|
|
|
|
|
|
public GalImageFormat Format => Image.Format;
|
|
|
|
|
|
|
|
|
|
public PixelInternalFormat InternalFormat { get; private set; }
|
|
|
|
|
public PixelFormat PixelFormat { get; private set; }
|
|
|
|
|
public PixelType PixelType { get; private set; }
|
|
|
|
|
|
|
|
|
|
public int Handle { get; private set; }
|
|
|
|
|
|
|
|
|
|
private bool Initialized;
|
|
|
|
|
|
|
|
|
|
public ImageHandler()
|
|
|
|
|
{
|
|
|
|
|
Handle = GL.GenTexture();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ImageHandler(int Handle, GalImage Image)
|
|
|
|
|
{
|
|
|
|
|
this.Handle = Handle;
|
|
|
|
|
|
|
|
|
|
this.Image = Image;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-08 10:51:50 -07:00
|
|
|
|
public void EnsureSetup(GalImage NewImage)
|
2018-08-19 18:25:26 -07:00
|
|
|
|
{
|
2018-09-08 10:51:50 -07:00
|
|
|
|
if (Width == NewImage.Width &&
|
|
|
|
|
Height == NewImage.Height &&
|
|
|
|
|
Format == NewImage.Format &&
|
|
|
|
|
Initialized)
|
2018-08-19 18:25:26 -07:00
|
|
|
|
{
|
2018-09-08 10:51:50 -07:00
|
|
|
|
return;
|
|
|
|
|
}
|
2018-08-19 18:25:26 -07:00
|
|
|
|
|
2018-09-08 10:51:50 -07:00
|
|
|
|
PixelInternalFormat InternalFmt;
|
|
|
|
|
PixelFormat PixelFormat;
|
|
|
|
|
PixelType PixelType;
|
2018-08-19 18:25:26 -07:00
|
|
|
|
|
2018-09-08 10:51:50 -07:00
|
|
|
|
if (ImageUtils.IsCompressed(NewImage.Format))
|
|
|
|
|
{
|
|
|
|
|
InternalFmt = (PixelInternalFormat)OGLEnumConverter.GetCompressedImageFormat(NewImage.Format);
|
|
|
|
|
|
|
|
|
|
PixelFormat = default(PixelFormat);
|
|
|
|
|
PixelType = default(PixelType);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
(InternalFmt, PixelFormat, PixelType) = OGLEnumConverter.GetImageFormat(NewImage.Format);
|
|
|
|
|
}
|
2018-08-19 18:25:26 -07:00
|
|
|
|
|
2018-09-08 10:51:50 -07:00
|
|
|
|
GL.BindTexture(TextureTarget.Texture2D, Handle);
|
2018-08-19 18:25:26 -07:00
|
|
|
|
|
2018-09-08 10:51:50 -07:00
|
|
|
|
if (Initialized)
|
|
|
|
|
{
|
|
|
|
|
if (CopyBuffer == 0)
|
|
|
|
|
{
|
|
|
|
|
CopyBuffer = GL.GenBuffer();
|
|
|
|
|
}
|
2018-08-19 18:25:26 -07:00
|
|
|
|
|
2018-09-08 10:51:50 -07:00
|
|
|
|
int CurrentSize = Math.Max(ImageUtils.GetSize(NewImage),
|
|
|
|
|
ImageUtils.GetSize(Image));
|
2018-08-19 18:25:26 -07:00
|
|
|
|
|
2018-09-08 10:51:50 -07:00
|
|
|
|
GL.BindBuffer(BufferTarget.PixelPackBuffer, CopyBuffer);
|
|
|
|
|
GL.BindBuffer(BufferTarget.PixelUnpackBuffer, CopyBuffer);
|
2018-08-19 18:25:26 -07:00
|
|
|
|
|
2018-09-08 10:51:50 -07:00
|
|
|
|
if (CopyBufferSize < CurrentSize)
|
|
|
|
|
{
|
|
|
|
|
CopyBufferSize = CurrentSize;
|
2018-08-19 18:25:26 -07:00
|
|
|
|
|
2018-09-08 10:51:50 -07:00
|
|
|
|
GL.BufferData(BufferTarget.PixelPackBuffer, CurrentSize, IntPtr.Zero, BufferUsageHint.StreamCopy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ImageUtils.IsCompressed(Image.Format))
|
|
|
|
|
{
|
|
|
|
|
GL.GetCompressedTexImage(TextureTarget.Texture2D, 0, IntPtr.Zero);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-08-19 18:25:26 -07:00
|
|
|
|
GL.GetTexImage(TextureTarget.Texture2D, 0, this.PixelFormat, this.PixelType, IntPtr.Zero);
|
2018-09-08 10:51:50 -07:00
|
|
|
|
}
|
2018-08-19 18:25:26 -07:00
|
|
|
|
|
2018-09-08 10:51:50 -07:00
|
|
|
|
GL.DeleteTexture(Handle);
|
2018-08-19 18:25:26 -07:00
|
|
|
|
|
2018-09-08 10:51:50 -07:00
|
|
|
|
Handle = GL.GenTexture();
|
2018-08-19 18:25:26 -07:00
|
|
|
|
|
2018-09-08 10:51:50 -07:00
|
|
|
|
GL.BindTexture(TextureTarget.Texture2D, Handle);
|
|
|
|
|
}
|
2018-08-19 18:25:26 -07:00
|
|
|
|
|
2018-09-08 10:51:50 -07:00
|
|
|
|
const int MinFilter = (int)TextureMinFilter.Linear;
|
|
|
|
|
const int MagFilter = (int)TextureMagFilter.Linear;
|
2018-08-19 18:25:26 -07:00
|
|
|
|
|
2018-09-08 10:51:50 -07:00
|
|
|
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, MinFilter);
|
|
|
|
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, MagFilter);
|
2018-08-19 18:25:26 -07:00
|
|
|
|
|
2018-09-08 10:51:50 -07:00
|
|
|
|
const int Level = 0;
|
|
|
|
|
const int Border = 0;
|
2018-08-19 18:25:26 -07:00
|
|
|
|
|
2018-09-08 10:51:50 -07:00
|
|
|
|
if (ImageUtils.IsCompressed(NewImage.Format))
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Hit");
|
|
|
|
|
|
|
|
|
|
GL.CompressedTexImage2D(
|
|
|
|
|
TextureTarget.Texture2D,
|
|
|
|
|
Level,
|
|
|
|
|
(InternalFormat)InternalFmt,
|
|
|
|
|
NewImage.Width,
|
|
|
|
|
NewImage.Height,
|
|
|
|
|
Border,
|
|
|
|
|
ImageUtils.GetSize(NewImage),
|
|
|
|
|
IntPtr.Zero);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-08-19 18:25:26 -07:00
|
|
|
|
GL.TexImage2D(
|
|
|
|
|
TextureTarget.Texture2D,
|
|
|
|
|
Level,
|
2018-09-08 10:51:50 -07:00
|
|
|
|
InternalFmt,
|
|
|
|
|
NewImage.Width,
|
|
|
|
|
NewImage.Height,
|
2018-08-19 18:25:26 -07:00
|
|
|
|
Border,
|
|
|
|
|
PixelFormat,
|
|
|
|
|
PixelType,
|
|
|
|
|
IntPtr.Zero);
|
2018-09-08 10:51:50 -07:00
|
|
|
|
}
|
2018-08-19 18:25:26 -07:00
|
|
|
|
|
2018-09-08 10:51:50 -07:00
|
|
|
|
if (Initialized)
|
|
|
|
|
{
|
|
|
|
|
GL.BindBuffer(BufferTarget.PixelPackBuffer, 0);
|
|
|
|
|
GL.BindBuffer(BufferTarget.PixelUnpackBuffer, 0);
|
|
|
|
|
}
|
2018-08-19 18:25:26 -07:00
|
|
|
|
|
2018-09-08 10:51:50 -07:00
|
|
|
|
Image = NewImage;
|
2018-08-19 18:25:26 -07:00
|
|
|
|
|
2018-09-08 10:51:50 -07:00
|
|
|
|
this.InternalFormat = InternalFmt;
|
|
|
|
|
this.PixelFormat = PixelFormat;
|
|
|
|
|
this.PixelType = PixelType;
|
2018-08-19 18:25:26 -07:00
|
|
|
|
|
2018-09-08 10:51:50 -07:00
|
|
|
|
Initialized = true;
|
2018-08-19 18:25:26 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-08 10:51:50 -07:00
|
|
|
|
public bool HasColor => ImageUtils.HasColor(Image.Format);
|
|
|
|
|
public bool HasDepth => ImageUtils.HasDepth(Image.Format);
|
|
|
|
|
public bool HasStencil => ImageUtils.HasStencil(Image.Format);
|
2018-08-19 18:25:26 -07:00
|
|
|
|
}
|
|
|
|
|
}
|