2018-04-08 21:17:35 +02:00
|
|
|
using OpenTK.Graphics.OpenGL;
|
2018-09-08 19:51:50 +02:00
|
|
|
using Ryujinx.Graphics.Texture;
|
2018-06-02 05:50:56 +02:00
|
|
|
using System;
|
2018-04-08 21:17:35 +02:00
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gal.OpenGL
|
|
|
|
{
|
2018-08-20 03:25:26 +02:00
|
|
|
class OGLTexture : IGalTexture
|
2018-04-08 21:17:35 +02:00
|
|
|
{
|
2018-08-20 03:25:26 +02:00
|
|
|
private OGLCachedResource<ImageHandler> TextureCache;
|
2018-04-08 21:17:35 +02:00
|
|
|
|
|
|
|
public OGLTexture()
|
|
|
|
{
|
2018-08-20 03:25:26 +02:00
|
|
|
TextureCache = new OGLCachedResource<ImageHandler>(DeleteTexture);
|
2018-04-08 21:17:35 +02:00
|
|
|
}
|
|
|
|
|
2018-07-10 04:01:59 +02:00
|
|
|
public void LockCache()
|
|
|
|
{
|
|
|
|
TextureCache.Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UnlockCache()
|
|
|
|
{
|
|
|
|
TextureCache.Unlock();
|
|
|
|
}
|
|
|
|
|
2018-08-20 03:25:26 +02:00
|
|
|
private static void DeleteTexture(ImageHandler CachedImage)
|
2018-04-08 21:17:35 +02:00
|
|
|
{
|
2018-08-20 03:25:26 +02:00
|
|
|
GL.DeleteTexture(CachedImage.Handle);
|
2018-06-09 02:15:56 +02:00
|
|
|
}
|
2018-04-08 21:17:35 +02:00
|
|
|
|
2018-09-18 06:30:35 +02:00
|
|
|
public void Create(long Key, int Size, GalImage Image)
|
2018-06-09 02:15:56 +02:00
|
|
|
{
|
|
|
|
int Handle = GL.GenTexture();
|
|
|
|
|
2018-09-18 06:30:35 +02:00
|
|
|
GL.BindTexture(TextureTarget.Texture2D, Handle);
|
|
|
|
|
|
|
|
const int Level = 0; //TODO: Support mipmap textures.
|
|
|
|
const int Border = 0;
|
|
|
|
|
|
|
|
TextureCache.AddOrUpdate(Key, new ImageHandler(Handle, Image), (uint)Size);
|
|
|
|
|
|
|
|
GalImageFormat TypeLess = Image.Format & GalImageFormat.FormatMask;
|
|
|
|
|
|
|
|
bool IsASTC = TypeLess >= GalImageFormat.ASTC_BEGIN && TypeLess <= GalImageFormat.ASTC_END;
|
|
|
|
|
|
|
|
if (ImageUtils.IsCompressed(Image.Format) && !IsASTC)
|
|
|
|
{
|
|
|
|
InternalFormat InternalFmt = OGLEnumConverter.GetCompressedImageFormat(Image.Format);
|
|
|
|
|
|
|
|
GL.CompressedTexImage2D(
|
|
|
|
TextureTarget.Texture2D,
|
|
|
|
Level,
|
|
|
|
InternalFmt,
|
|
|
|
Image.Width,
|
|
|
|
Image.Height,
|
|
|
|
Border,
|
|
|
|
Size,
|
|
|
|
IntPtr.Zero);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
(PixelInternalFormat InternalFmt,
|
|
|
|
PixelFormat Format,
|
|
|
|
PixelType Type) = OGLEnumConverter.GetImageFormat(Image.Format);
|
|
|
|
|
|
|
|
GL.TexImage2D(
|
|
|
|
TextureTarget.Texture2D,
|
|
|
|
Level,
|
|
|
|
InternalFmt,
|
|
|
|
Image.Width,
|
|
|
|
Image.Height,
|
|
|
|
Border,
|
|
|
|
Format,
|
|
|
|
Type,
|
|
|
|
IntPtr.Zero);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Create(long Key, byte[] Data, GalImage Image)
|
|
|
|
{
|
|
|
|
int Handle = GL.GenTexture();
|
2018-06-09 02:15:56 +02:00
|
|
|
|
|
|
|
GL.BindTexture(TextureTarget.Texture2D, Handle);
|
2018-04-08 21:17:35 +02:00
|
|
|
|
2018-04-13 20:12:58 +02:00
|
|
|
const int Level = 0; //TODO: Support mipmap textures.
|
2018-04-10 21:50:32 +02:00
|
|
|
const int Border = 0;
|
2018-04-08 21:17:35 +02:00
|
|
|
|
2018-09-18 06:30:35 +02:00
|
|
|
TextureCache.AddOrUpdate(Key, new ImageHandler(Handle, Image), (uint)Data.Length);
|
|
|
|
|
2018-09-08 19:51:50 +02:00
|
|
|
GalImageFormat TypeLess = Image.Format & GalImageFormat.FormatMask;
|
|
|
|
|
|
|
|
bool IsASTC = TypeLess >= GalImageFormat.ASTC_BEGIN && TypeLess <= GalImageFormat.ASTC_END;
|
|
|
|
|
|
|
|
if (ImageUtils.IsCompressed(Image.Format) && !IsASTC)
|
2018-04-08 21:17:35 +02:00
|
|
|
{
|
2018-08-20 03:25:26 +02:00
|
|
|
InternalFormat InternalFmt = OGLEnumConverter.GetCompressedImageFormat(Image.Format);
|
2018-04-10 21:50:32 +02:00
|
|
|
|
|
|
|
GL.CompressedTexImage2D(
|
|
|
|
TextureTarget.Texture2D,
|
2018-04-13 20:12:58 +02:00
|
|
|
Level,
|
2018-04-10 21:50:32 +02:00
|
|
|
InternalFmt,
|
2018-08-20 03:25:26 +02:00
|
|
|
Image.Width,
|
|
|
|
Image.Height,
|
2018-04-10 21:50:32 +02:00
|
|
|
Border,
|
2018-06-09 02:15:56 +02:00
|
|
|
Data.Length,
|
|
|
|
Data);
|
2018-04-08 21:17:35 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-08 19:51:50 +02:00
|
|
|
//TODO: Use KHR_texture_compression_astc_hdr when available
|
|
|
|
if (IsASTC)
|
2018-06-02 05:50:56 +02:00
|
|
|
{
|
2018-09-18 06:30:35 +02:00
|
|
|
int TextureBlockWidth = ImageUtils.GetBlockWidth(Image.Format);
|
|
|
|
int TextureBlockHeight = ImageUtils.GetBlockHeight(Image.Format);
|
2018-06-09 02:15:56 +02:00
|
|
|
|
|
|
|
Data = ASTCDecoder.DecodeToRGBA8888(
|
|
|
|
Data,
|
|
|
|
TextureBlockWidth,
|
|
|
|
TextureBlockHeight, 1,
|
2018-08-20 03:25:26 +02:00
|
|
|
Image.Width,
|
|
|
|
Image.Height, 1);
|
2018-06-09 02:15:56 +02:00
|
|
|
|
2018-09-08 19:51:50 +02:00
|
|
|
Image.Format = GalImageFormat.A8B8G8R8 | GalImageFormat.Unorm;
|
|
|
|
}
|
|
|
|
else if (TypeLess == GalImageFormat.G8R8)
|
|
|
|
{
|
|
|
|
Data = ImageConverter.G8R8ToR8G8(
|
|
|
|
Data,
|
|
|
|
Image.Width,
|
|
|
|
Image.Height,
|
|
|
|
1);
|
|
|
|
|
2018-09-11 17:48:13 +02:00
|
|
|
Image.Format = GalImageFormat.R8G8 | (Image.Format & GalImageFormat.TypeMask);
|
2018-06-02 05:50:56 +02:00
|
|
|
}
|
|
|
|
|
2018-09-18 06:30:35 +02:00
|
|
|
(PixelInternalFormat InternalFmt,
|
|
|
|
PixelFormat Format,
|
|
|
|
PixelType Type) = OGLEnumConverter.GetImageFormat(Image.Format);
|
2018-04-10 21:50:32 +02:00
|
|
|
|
|
|
|
GL.TexImage2D(
|
|
|
|
TextureTarget.Texture2D,
|
2018-04-13 20:12:58 +02:00
|
|
|
Level,
|
2018-09-18 06:30:35 +02:00
|
|
|
InternalFmt,
|
2018-08-20 03:25:26 +02:00
|
|
|
Image.Width,
|
|
|
|
Image.Height,
|
2018-04-10 21:50:32 +02:00
|
|
|
Border,
|
2018-04-13 20:12:58 +02:00
|
|
|
Format,
|
|
|
|
Type,
|
2018-06-09 02:15:56 +02:00
|
|
|
Data);
|
2018-04-08 21:17:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 06:30:35 +02:00
|
|
|
public bool TryGetImage(long Key, out GalImage Image)
|
2018-08-20 03:25:26 +02:00
|
|
|
{
|
2018-09-18 06:30:35 +02:00
|
|
|
if (TextureCache.TryGetValue(Key, out ImageHandler CachedImage))
|
2018-08-20 03:25:26 +02:00
|
|
|
{
|
2018-09-18 06:30:35 +02:00
|
|
|
Image = CachedImage.Image;
|
2018-08-20 03:25:26 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-18 06:30:35 +02:00
|
|
|
Image = default(GalImage);
|
2018-08-20 03:25:26 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-18 06:30:35 +02:00
|
|
|
public bool TryGetImageHandler(long Key, out ImageHandler CachedImage)
|
2018-06-02 05:50:56 +02:00
|
|
|
{
|
2018-09-18 06:30:35 +02:00
|
|
|
if (TextureCache.TryGetValue(Key, out CachedImage))
|
2018-06-09 02:15:56 +02:00
|
|
|
{
|
2018-09-18 06:30:35 +02:00
|
|
|
return true;
|
2018-06-09 02:15:56 +02:00
|
|
|
}
|
|
|
|
|
2018-09-18 06:30:35 +02:00
|
|
|
CachedImage = null;
|
2018-06-09 02:15:56 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-18 06:30:35 +02:00
|
|
|
public void Bind(long Key, int Index, GalImage Image)
|
2018-04-08 21:17:35 +02:00
|
|
|
{
|
2018-08-20 03:25:26 +02:00
|
|
|
if (TextureCache.TryGetValue(Key, out ImageHandler CachedImage))
|
2018-06-09 02:15:56 +02:00
|
|
|
{
|
|
|
|
GL.ActiveTexture(TextureUnit.Texture0 + Index);
|
2018-04-08 21:17:35 +02:00
|
|
|
|
2018-08-20 03:25:26 +02:00
|
|
|
GL.BindTexture(TextureTarget.Texture2D, CachedImage.Handle);
|
2018-09-18 06:30:35 +02:00
|
|
|
|
|
|
|
int[] SwizzleRgba = new int[]
|
|
|
|
{
|
|
|
|
(int)OGLEnumConverter.GetTextureSwizzle(Image.XSource),
|
|
|
|
(int)OGLEnumConverter.GetTextureSwizzle(Image.YSource),
|
|
|
|
(int)OGLEnumConverter.GetTextureSwizzle(Image.ZSource),
|
|
|
|
(int)OGLEnumConverter.GetTextureSwizzle(Image.WSource)
|
|
|
|
};
|
|
|
|
|
|
|
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureSwizzleRgba, SwizzleRgba);
|
2018-06-09 02:15:56 +02:00
|
|
|
}
|
2018-04-13 20:12:58 +02:00
|
|
|
}
|
2018-04-08 21:17:35 +02:00
|
|
|
|
2018-06-24 02:39:25 +02:00
|
|
|
public void SetSampler(GalTextureSampler Sampler)
|
2018-04-13 20:12:58 +02:00
|
|
|
{
|
2018-04-08 21:17:35 +02:00
|
|
|
int WrapS = (int)OGLEnumConverter.GetTextureWrapMode(Sampler.AddressU);
|
|
|
|
int WrapT = (int)OGLEnumConverter.GetTextureWrapMode(Sampler.AddressV);
|
|
|
|
|
|
|
|
int MinFilter = (int)OGLEnumConverter.GetTextureMinFilter(Sampler.MinFilter, Sampler.MipFilter);
|
|
|
|
int MagFilter = (int)OGLEnumConverter.GetTextureMagFilter(Sampler.MagFilter);
|
|
|
|
|
|
|
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, WrapS);
|
|
|
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, WrapT);
|
|
|
|
|
|
|
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, MinFilter);
|
|
|
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, MagFilter);
|
|
|
|
|
|
|
|
float[] Color = new float[]
|
|
|
|
{
|
|
|
|
Sampler.BorderColor.Red,
|
|
|
|
Sampler.BorderColor.Green,
|
|
|
|
Sampler.BorderColor.Blue,
|
|
|
|
Sampler.BorderColor.Alpha
|
|
|
|
};
|
|
|
|
|
|
|
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBorderColor, Color);
|
|
|
|
}
|
|
|
|
}
|
2018-07-16 20:09:34 +02:00
|
|
|
}
|