2018-04-08 12:17:35 -07:00
|
|
|
using OpenTK.Graphics.OpenGL;
|
2018-09-08 10:51:50 -07:00
|
|
|
using Ryujinx.Graphics.Texture;
|
2018-06-01 20:50:56 -07:00
|
|
|
using System;
|
2018-04-08 12:17:35 -07:00
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gal.OpenGL
|
|
|
|
{
|
2018-08-19 18:25:26 -07:00
|
|
|
class OGLTexture : IGalTexture
|
2018-04-08 12:17:35 -07:00
|
|
|
{
|
2018-08-19 18:25:26 -07:00
|
|
|
private OGLCachedResource<ImageHandler> TextureCache;
|
2018-04-08 12:17:35 -07:00
|
|
|
|
|
|
|
public OGLTexture()
|
|
|
|
{
|
2018-08-19 18:25:26 -07:00
|
|
|
TextureCache = new OGLCachedResource<ImageHandler>(DeleteTexture);
|
2018-04-08 12:17:35 -07:00
|
|
|
}
|
|
|
|
|
2018-07-09 19:01:59 -07:00
|
|
|
public void LockCache()
|
|
|
|
{
|
|
|
|
TextureCache.Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UnlockCache()
|
|
|
|
{
|
|
|
|
TextureCache.Unlock();
|
|
|
|
}
|
|
|
|
|
2018-08-19 18:25:26 -07:00
|
|
|
private static void DeleteTexture(ImageHandler CachedImage)
|
2018-04-08 12:17:35 -07:00
|
|
|
{
|
2018-08-19 18:25:26 -07:00
|
|
|
GL.DeleteTexture(CachedImage.Handle);
|
2018-06-08 17:15:56 -07:00
|
|
|
}
|
2018-04-08 12:17:35 -07:00
|
|
|
|
2018-08-19 18:25:26 -07:00
|
|
|
public void Create(long Key, byte[] Data, GalImage Image)
|
2018-06-08 17:15:56 -07:00
|
|
|
{
|
|
|
|
int Handle = GL.GenTexture();
|
|
|
|
|
2018-08-19 18:25:26 -07:00
|
|
|
TextureCache.AddOrUpdate(Key, new ImageHandler(Handle, Image), (uint)Data.Length);
|
2018-06-08 17:15:56 -07:00
|
|
|
|
|
|
|
GL.BindTexture(TextureTarget.Texture2D, Handle);
|
2018-04-08 12:17:35 -07:00
|
|
|
|
2018-04-13 11:12:58 -07:00
|
|
|
const int Level = 0; //TODO: Support mipmap textures.
|
2018-04-10 12:50:32 -07:00
|
|
|
const int Border = 0;
|
2018-04-08 12:17:35 -07:00
|
|
|
|
2018-09-08 10:51:50 -07: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 12:17:35 -07:00
|
|
|
{
|
2018-08-19 18:25:26 -07:00
|
|
|
InternalFormat InternalFmt = OGLEnumConverter.GetCompressedImageFormat(Image.Format);
|
2018-04-10 12:50:32 -07:00
|
|
|
|
|
|
|
GL.CompressedTexImage2D(
|
|
|
|
TextureTarget.Texture2D,
|
2018-04-13 11:12:58 -07:00
|
|
|
Level,
|
2018-04-10 12:50:32 -07:00
|
|
|
InternalFmt,
|
2018-08-19 18:25:26 -07:00
|
|
|
Image.Width,
|
|
|
|
Image.Height,
|
2018-04-10 12:50:32 -07:00
|
|
|
Border,
|
2018-06-08 17:15:56 -07:00
|
|
|
Data.Length,
|
|
|
|
Data);
|
2018-04-08 12:17:35 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-08 10:51:50 -07:00
|
|
|
//TODO: Use KHR_texture_compression_astc_hdr when available
|
|
|
|
if (IsASTC)
|
2018-06-01 20:50:56 -07:00
|
|
|
{
|
2018-08-19 18:25:26 -07:00
|
|
|
int TextureBlockWidth = GetAstcBlockWidth(Image.Format);
|
|
|
|
int TextureBlockHeight = GetAstcBlockHeight(Image.Format);
|
2018-06-08 17:15:56 -07:00
|
|
|
|
|
|
|
Data = ASTCDecoder.DecodeToRGBA8888(
|
|
|
|
Data,
|
|
|
|
TextureBlockWidth,
|
|
|
|
TextureBlockHeight, 1,
|
2018-08-19 18:25:26 -07:00
|
|
|
Image.Width,
|
|
|
|
Image.Height, 1);
|
2018-06-08 17:15:56 -07:00
|
|
|
|
2018-09-08 10:51:50 -07:00
|
|
|
Image.Format = GalImageFormat.A8B8G8R8 | GalImageFormat.Unorm;
|
|
|
|
}
|
|
|
|
else if (TypeLess == GalImageFormat.G8R8)
|
|
|
|
{
|
|
|
|
Data = ImageConverter.G8R8ToR8G8(
|
|
|
|
Data,
|
|
|
|
Image.Width,
|
|
|
|
Image.Height,
|
|
|
|
1);
|
|
|
|
|
|
|
|
Image.Format = GalImageFormat.R8G8 | (Image.Format & GalImageFormat.FormatMask);
|
2018-06-01 20:50:56 -07:00
|
|
|
}
|
|
|
|
|
2018-08-19 18:25:26 -07:00
|
|
|
(PixelInternalFormat InternalFormat, PixelFormat Format, PixelType Type) = OGLEnumConverter.GetImageFormat(Image.Format);
|
2018-04-10 12:50:32 -07:00
|
|
|
|
|
|
|
GL.TexImage2D(
|
|
|
|
TextureTarget.Texture2D,
|
2018-04-13 11:12:58 -07:00
|
|
|
Level,
|
2018-08-19 18:25:26 -07:00
|
|
|
InternalFormat,
|
|
|
|
Image.Width,
|
|
|
|
Image.Height,
|
2018-04-10 12:50:32 -07:00
|
|
|
Border,
|
2018-04-13 11:12:58 -07:00
|
|
|
Format,
|
|
|
|
Type,
|
2018-06-08 17:15:56 -07:00
|
|
|
Data);
|
2018-04-08 12:17:35 -07:00
|
|
|
}
|
2018-05-17 11:25:42 -07:00
|
|
|
|
2018-08-19 18:25:26 -07: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 11:25:42 -07: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 12:17:35 -07:00
|
|
|
}
|
|
|
|
|
2018-08-19 18:25:26 -07: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-01 20:50:56 -07:00
|
|
|
{
|
|
|
|
switch (Format)
|
|
|
|
{
|
2018-09-08 10:51:50 -07: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-01 20:50:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new ArgumentException(nameof(Format));
|
|
|
|
}
|
|
|
|
|
2018-08-19 18:25:26 -07:00
|
|
|
private static int GetAstcBlockHeight(GalImageFormat Format)
|
2018-06-01 20:50:56 -07:00
|
|
|
{
|
|
|
|
switch (Format)
|
|
|
|
{
|
2018-09-08 10:51:50 -07: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-01 20:50:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new ArgumentException(nameof(Format));
|
|
|
|
}
|
|
|
|
|
2018-08-19 18:25:26 -07:00
|
|
|
public bool TryGetCachedTexture(long Key, long DataSize, out GalImage Image)
|
2018-06-08 17:15:56 -07:00
|
|
|
{
|
2018-06-23 17:39:25 -07:00
|
|
|
if (TextureCache.TryGetSize(Key, out long Size) && Size == DataSize)
|
2018-06-08 17:15:56 -07:00
|
|
|
{
|
2018-08-19 18:25:26 -07:00
|
|
|
if (TextureCache.TryGetValue(Key, out ImageHandler CachedImage))
|
2018-06-08 17:15:56 -07:00
|
|
|
{
|
2018-08-19 18:25:26 -07:00
|
|
|
Image = CachedImage.Image;
|
2018-06-08 17:15:56 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-19 18:25:26 -07:00
|
|
|
Image = default(GalImage);
|
2018-06-08 17:15:56 -07:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-23 17:39:25 -07:00
|
|
|
public void Bind(long Key, int Index)
|
2018-04-08 12:17:35 -07:00
|
|
|
{
|
2018-08-19 18:25:26 -07:00
|
|
|
if (TextureCache.TryGetValue(Key, out ImageHandler CachedImage))
|
2018-06-08 17:15:56 -07:00
|
|
|
{
|
|
|
|
GL.ActiveTexture(TextureUnit.Texture0 + Index);
|
2018-04-08 12:17:35 -07:00
|
|
|
|
2018-08-19 18:25:26 -07:00
|
|
|
GL.BindTexture(TextureTarget.Texture2D, CachedImage.Handle);
|
2018-06-08 17:15:56 -07:00
|
|
|
}
|
2018-04-13 11:12:58 -07:00
|
|
|
}
|
2018-04-08 12:17:35 -07:00
|
|
|
|
2018-06-23 17:39:25 -07:00
|
|
|
public void SetSampler(GalTextureSampler Sampler)
|
2018-04-13 11:12:58 -07:00
|
|
|
{
|
2018-04-08 12:17:35 -07: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 11:09:34 -07:00
|
|
|
}
|