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-06-02 04:50:56 +01:00
|
|
|
using System;
|
2018-04-08 20:17:35 +01:00
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gal.OpenGL
|
|
|
|
{
|
2018-08-20 02:25:26 +01:00
|
|
|
class OGLTexture : IGalTexture
|
2018-04-08 20:17:35 +01:00
|
|
|
{
|
2018-08-20 02:25:26 +01:00
|
|
|
private OGLCachedResource<ImageHandler> TextureCache;
|
2018-04-08 20:17:35 +01:00
|
|
|
|
|
|
|
public OGLTexture()
|
|
|
|
{
|
2018-08-20 02:25:26 +01:00
|
|
|
TextureCache = new OGLCachedResource<ImageHandler>(DeleteTexture);
|
2018-04-08 20:17:35 +01:00
|
|
|
}
|
|
|
|
|
2018-07-10 03:01:59 +01:00
|
|
|
public void LockCache()
|
|
|
|
{
|
|
|
|
TextureCache.Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UnlockCache()
|
|
|
|
{
|
|
|
|
TextureCache.Unlock();
|
|
|
|
}
|
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
private static void DeleteTexture(ImageHandler CachedImage)
|
2018-04-08 20:17:35 +01:00
|
|
|
{
|
2018-08-20 02:25:26 +01:00
|
|
|
GL.DeleteTexture(CachedImage.Handle);
|
2018-06-09 01:15:56 +01:00
|
|
|
}
|
2018-04-08 20:17:35 +01:00
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
public void Create(long Key, byte[] Data, GalImage Image)
|
2018-06-09 01:15:56 +01:00
|
|
|
{
|
|
|
|
int Handle = GL.GenTexture();
|
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
TextureCache.AddOrUpdate(Key, new ImageHandler(Handle, Image), (uint)Data.Length);
|
2018-06-09 01:15:56 +01:00
|
|
|
|
|
|
|
GL.BindTexture(TextureTarget.Texture2D, Handle);
|
2018-04-08 20:17:35 +01:00
|
|
|
|
2018-04-13 19:12:58 +01:00
|
|
|
const int Level = 0; //TODO: Support mipmap textures.
|
2018-04-10 20:50:32 +01:00
|
|
|
const int Border = 0;
|
2018-04-08 20:17:35 +01:00
|
|
|
|
2018-09-08 18:51:50 +01: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 20:17:35 +01:00
|
|
|
{
|
2018-08-20 02:25:26 +01:00
|
|
|
InternalFormat InternalFmt = OGLEnumConverter.GetCompressedImageFormat(Image.Format);
|
2018-04-10 20:50:32 +01:00
|
|
|
|
|
|
|
GL.CompressedTexImage2D(
|
|
|
|
TextureTarget.Texture2D,
|
2018-04-13 19:12:58 +01:00
|
|
|
Level,
|
2018-04-10 20:50:32 +01:00
|
|
|
InternalFmt,
|
2018-08-20 02:25:26 +01:00
|
|
|
Image.Width,
|
|
|
|
Image.Height,
|
2018-04-10 20:50:32 +01:00
|
|
|
Border,
|
2018-06-09 01:15:56 +01:00
|
|
|
Data.Length,
|
|
|
|
Data);
|
2018-04-08 20:17:35 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-08 18:51:50 +01:00
|
|
|
//TODO: Use KHR_texture_compression_astc_hdr when available
|
|
|
|
if (IsASTC)
|
2018-06-02 04:50:56 +01:00
|
|
|
{
|
2018-08-20 02:25:26 +01:00
|
|
|
int TextureBlockWidth = GetAstcBlockWidth(Image.Format);
|
|
|
|
int TextureBlockHeight = GetAstcBlockHeight(Image.Format);
|
2018-06-09 01:15:56 +01:00
|
|
|
|
|
|
|
Data = ASTCDecoder.DecodeToRGBA8888(
|
|
|
|
Data,
|
|
|
|
TextureBlockWidth,
|
|
|
|
TextureBlockHeight, 1,
|
2018-08-20 02:25:26 +01:00
|
|
|
Image.Width,
|
|
|
|
Image.Height, 1);
|
2018-06-09 01:15:56 +01:00
|
|
|
|
2018-09-08 18:51:50 +01:00
|
|
|
Image.Format = GalImageFormat.A8B8G8R8 | GalImageFormat.Unorm;
|
|
|
|
}
|
|
|
|
else if (TypeLess == GalImageFormat.G8R8)
|
|
|
|
{
|
|
|
|
Data = ImageConverter.G8R8ToR8G8(
|
|
|
|
Data,
|
|
|
|
Image.Width,
|
|
|
|
Image.Height,
|
|
|
|
1);
|
|
|
|
|
2018-09-11 16:48:13 +01:00
|
|
|
Image.Format = GalImageFormat.R8G8 | (Image.Format & GalImageFormat.TypeMask);
|
2018-06-02 04:50:56 +01:00
|
|
|
}
|
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
(PixelInternalFormat InternalFormat, PixelFormat Format, PixelType Type) = OGLEnumConverter.GetImageFormat(Image.Format);
|
2018-04-10 20:50:32 +01:00
|
|
|
|
|
|
|
GL.TexImage2D(
|
|
|
|
TextureTarget.Texture2D,
|
2018-04-13 19:12:58 +01:00
|
|
|
Level,
|
2018-08-20 02:25:26 +01:00
|
|
|
InternalFormat,
|
|
|
|
Image.Width,
|
|
|
|
Image.Height,
|
2018-04-10 20:50:32 +01:00
|
|
|
Border,
|
2018-04-13 19:12:58 +01:00
|
|
|
Format,
|
|
|
|
Type,
|
2018-06-09 01:15:56 +01:00
|
|
|
Data);
|
2018-04-08 20:17:35 +01:00
|
|
|
}
|
2018-05-17 19:25:42 +01:00
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
int SwizzleR = (int)OGLEnumConverter.GetTextureSwizzle(Image.XSource);
|
|
|
|
int SwizzleG = (int)OGLEnumConverter.GetTextureSwizzle(Image.YSource);
|
|
|
|
int SwizzleB = (int)OGLEnumConverter.GetTextureSwizzle(Image.ZSource);
|
|
|
|
int SwizzleA = (int)OGLEnumConverter.GetTextureSwizzle(Image.WSource);
|
2018-05-17 19:25:42 +01:00
|
|
|
|
|
|
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureSwizzleR, SwizzleR);
|
|
|
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureSwizzleG, SwizzleG);
|
|
|
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureSwizzleB, SwizzleB);
|
|
|
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureSwizzleA, SwizzleA);
|
2018-04-08 20:17:35 +01:00
|
|
|
}
|
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
public void CreateFb(long Key, long Size, GalImage Image)
|
|
|
|
{
|
|
|
|
if (!TryGetImage(Key, out ImageHandler CachedImage))
|
|
|
|
{
|
|
|
|
CachedImage = new ImageHandler();
|
|
|
|
|
|
|
|
TextureCache.AddOrUpdate(Key, CachedImage, Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
CachedImage.EnsureSetup(Image);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool TryGetImage(long Key, out ImageHandler CachedImage)
|
|
|
|
{
|
|
|
|
if (TextureCache.TryGetValue(Key, out CachedImage))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CachedImage = null;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int GetAstcBlockWidth(GalImageFormat Format)
|
2018-06-02 04:50:56 +01:00
|
|
|
{
|
|
|
|
switch (Format)
|
|
|
|
{
|
2018-09-08 18:51:50 +01:00
|
|
|
case GalImageFormat.ASTC_4x4 | GalImageFormat.Unorm: return 4;
|
|
|
|
case GalImageFormat.ASTC_5x5 | GalImageFormat.Unorm: return 5;
|
|
|
|
case GalImageFormat.ASTC_6x6 | GalImageFormat.Unorm: return 6;
|
|
|
|
case GalImageFormat.ASTC_8x8 | GalImageFormat.Unorm: return 8;
|
|
|
|
case GalImageFormat.ASTC_10x10 | GalImageFormat.Unorm: return 10;
|
|
|
|
case GalImageFormat.ASTC_12x12 | GalImageFormat.Unorm: return 12;
|
|
|
|
case GalImageFormat.ASTC_5x4 | GalImageFormat.Unorm: return 5;
|
|
|
|
case GalImageFormat.ASTC_6x5 | GalImageFormat.Unorm: return 6;
|
|
|
|
case GalImageFormat.ASTC_8x6 | GalImageFormat.Unorm: return 8;
|
|
|
|
case GalImageFormat.ASTC_10x8 | GalImageFormat.Unorm: return 10;
|
|
|
|
case GalImageFormat.ASTC_12x10 | GalImageFormat.Unorm: return 12;
|
|
|
|
case GalImageFormat.ASTC_8x5 | GalImageFormat.Unorm: return 8;
|
|
|
|
case GalImageFormat.ASTC_10x5 | GalImageFormat.Unorm: return 10;
|
|
|
|
case GalImageFormat.ASTC_10x6 | GalImageFormat.Unorm: return 10;
|
2018-06-02 04:50:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new ArgumentException(nameof(Format));
|
|
|
|
}
|
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
private static int GetAstcBlockHeight(GalImageFormat Format)
|
2018-06-02 04:50:56 +01:00
|
|
|
{
|
|
|
|
switch (Format)
|
|
|
|
{
|
2018-09-08 18:51:50 +01:00
|
|
|
case GalImageFormat.ASTC_4x4 | GalImageFormat.Unorm: return 4;
|
|
|
|
case GalImageFormat.ASTC_5x5 | GalImageFormat.Unorm: return 5;
|
|
|
|
case GalImageFormat.ASTC_6x6 | GalImageFormat.Unorm: return 6;
|
|
|
|
case GalImageFormat.ASTC_8x8 | GalImageFormat.Unorm: return 8;
|
|
|
|
case GalImageFormat.ASTC_10x10 | GalImageFormat.Unorm: return 10;
|
|
|
|
case GalImageFormat.ASTC_12x12 | GalImageFormat.Unorm: return 12;
|
|
|
|
case GalImageFormat.ASTC_5x4 | GalImageFormat.Unorm: return 4;
|
|
|
|
case GalImageFormat.ASTC_6x5 | GalImageFormat.Unorm: return 5;
|
|
|
|
case GalImageFormat.ASTC_8x6 | GalImageFormat.Unorm: return 6;
|
|
|
|
case GalImageFormat.ASTC_10x8 | GalImageFormat.Unorm: return 8;
|
|
|
|
case GalImageFormat.ASTC_12x10 | GalImageFormat.Unorm: return 10;
|
|
|
|
case GalImageFormat.ASTC_8x5 | GalImageFormat.Unorm: return 5;
|
|
|
|
case GalImageFormat.ASTC_10x5 | GalImageFormat.Unorm: return 5;
|
|
|
|
case GalImageFormat.ASTC_10x6 | GalImageFormat.Unorm: return 6;
|
2018-06-02 04:50:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new ArgumentException(nameof(Format));
|
|
|
|
}
|
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
public bool TryGetCachedTexture(long Key, long DataSize, out GalImage Image)
|
2018-06-09 01:15:56 +01:00
|
|
|
{
|
2018-06-24 01:39:25 +01:00
|
|
|
if (TextureCache.TryGetSize(Key, out long Size) && Size == DataSize)
|
2018-06-09 01:15:56 +01:00
|
|
|
{
|
2018-08-20 02:25:26 +01:00
|
|
|
if (TextureCache.TryGetValue(Key, out ImageHandler CachedImage))
|
2018-06-09 01:15:56 +01:00
|
|
|
{
|
2018-08-20 02:25:26 +01:00
|
|
|
Image = CachedImage.Image;
|
2018-06-09 01:15:56 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
Image = default(GalImage);
|
2018-06-09 01:15:56 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-24 01:39:25 +01:00
|
|
|
public void Bind(long Key, int Index)
|
2018-04-08 20:17:35 +01:00
|
|
|
{
|
2018-08-20 02:25:26 +01:00
|
|
|
if (TextureCache.TryGetValue(Key, out ImageHandler CachedImage))
|
2018-06-09 01:15:56 +01:00
|
|
|
{
|
|
|
|
GL.ActiveTexture(TextureUnit.Texture0 + Index);
|
2018-04-08 20:17:35 +01:00
|
|
|
|
2018-08-20 02:25:26 +01:00
|
|
|
GL.BindTexture(TextureTarget.Texture2D, CachedImage.Handle);
|
2018-06-09 01:15:56 +01:00
|
|
|
}
|
2018-04-13 19:12:58 +01:00
|
|
|
}
|
2018-04-08 20:17:35 +01:00
|
|
|
|
2018-06-24 01:39:25 +01:00
|
|
|
public void SetSampler(GalTextureSampler Sampler)
|
2018-04-13 19:12:58 +01:00
|
|
|
{
|
2018-04-08 20:17:35 +01: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 19:09:34 +01:00
|
|
|
}
|