2018-04-08 15:17:35 -04:00
|
|
|
using OpenTK.Graphics.OpenGL;
|
2018-09-08 13:51:50 -04:00
|
|
|
using Ryujinx.Graphics.Texture;
|
2018-06-01 23:50:56 -04:00
|
|
|
using System;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gal.OpenGL
|
|
|
|
{
|
2018-08-19 21:25:26 -04:00
|
|
|
class OGLTexture : IGalTexture
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2018-08-19 21:25:26 -04:00
|
|
|
private OGLCachedResource<ImageHandler> TextureCache;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
|
|
|
public OGLTexture()
|
|
|
|
{
|
2018-08-19 21:25:26 -04:00
|
|
|
TextureCache = new OGLCachedResource<ImageHandler>(DeleteTexture);
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
2018-07-09 22:01:59 -04:00
|
|
|
public void LockCache()
|
|
|
|
{
|
|
|
|
TextureCache.Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UnlockCache()
|
|
|
|
{
|
|
|
|
TextureCache.Unlock();
|
|
|
|
}
|
|
|
|
|
2018-08-19 21:25:26 -04:00
|
|
|
private static void DeleteTexture(ImageHandler CachedImage)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2018-08-19 21:25:26 -04:00
|
|
|
GL.DeleteTexture(CachedImage.Handle);
|
2018-06-08 20:15:56 -04:00
|
|
|
}
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2018-09-18 00:30:35 -04:00
|
|
|
public void Create(long Key, int Size, GalImage Image)
|
2018-06-08 20:15:56 -04:00
|
|
|
{
|
|
|
|
int Handle = GL.GenTexture();
|
|
|
|
|
2018-09-18 00:30:35 -04: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);
|
|
|
|
|
2018-10-17 17:02:23 -04:00
|
|
|
if (ImageUtils.IsCompressed(Image.Format))
|
2018-09-18 00:30:35 -04:00
|
|
|
{
|
2018-10-17 17:02:23 -04:00
|
|
|
throw new InvalidOperationException("Surfaces with compressed formats are not supported!");
|
2018-09-18 00:30:35 -04:00
|
|
|
}
|
|
|
|
|
2018-10-17 17:02:23 -04:00
|
|
|
(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);
|
2018-09-18 00:30:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Create(long Key, byte[] Data, GalImage Image)
|
|
|
|
{
|
|
|
|
int Handle = GL.GenTexture();
|
2018-06-08 20:15:56 -04:00
|
|
|
|
|
|
|
GL.BindTexture(TextureTarget.Texture2D, Handle);
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2018-04-13 14:12:58 -04:00
|
|
|
const int Level = 0; //TODO: Support mipmap textures.
|
2018-04-10 15:50:32 -04:00
|
|
|
const int Border = 0;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2018-09-18 00:30:35 -04:00
|
|
|
TextureCache.AddOrUpdate(Key, new ImageHandler(Handle, Image), (uint)Data.Length);
|
|
|
|
|
2018-10-17 17:02:23 -04:00
|
|
|
if (ImageUtils.IsCompressed(Image.Format) && !IsAstc(Image.Format))
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2018-08-19 21:25:26 -04:00
|
|
|
InternalFormat InternalFmt = OGLEnumConverter.GetCompressedImageFormat(Image.Format);
|
2018-04-10 15:50:32 -04:00
|
|
|
|
|
|
|
GL.CompressedTexImage2D(
|
|
|
|
TextureTarget.Texture2D,
|
2018-04-13 14:12:58 -04:00
|
|
|
Level,
|
2018-04-10 15:50:32 -04:00
|
|
|
InternalFmt,
|
2018-08-19 21:25:26 -04:00
|
|
|
Image.Width,
|
|
|
|
Image.Height,
|
2018-04-10 15:50:32 -04:00
|
|
|
Border,
|
2018-06-08 20:15:56 -04:00
|
|
|
Data.Length,
|
|
|
|
Data);
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-08 13:51:50 -04:00
|
|
|
//TODO: Use KHR_texture_compression_astc_hdr when available
|
2018-10-17 17:02:23 -04:00
|
|
|
if (IsAstc(Image.Format))
|
2018-06-01 23:50:56 -04:00
|
|
|
{
|
2018-09-18 00:30:35 -04:00
|
|
|
int TextureBlockWidth = ImageUtils.GetBlockWidth(Image.Format);
|
|
|
|
int TextureBlockHeight = ImageUtils.GetBlockHeight(Image.Format);
|
2018-06-08 20:15:56 -04:00
|
|
|
|
|
|
|
Data = ASTCDecoder.DecodeToRGBA8888(
|
|
|
|
Data,
|
|
|
|
TextureBlockWidth,
|
|
|
|
TextureBlockHeight, 1,
|
2018-08-19 21:25:26 -04:00
|
|
|
Image.Width,
|
|
|
|
Image.Height, 1);
|
2018-06-08 20:15:56 -04:00
|
|
|
|
2018-10-17 17:02:23 -04:00
|
|
|
Image.Format = GalImageFormat.RGBA8 | GalImageFormat.Unorm;
|
2018-06-01 23:50:56 -04:00
|
|
|
}
|
|
|
|
|
2018-09-18 00:30:35 -04:00
|
|
|
(PixelInternalFormat InternalFmt,
|
|
|
|
PixelFormat Format,
|
|
|
|
PixelType Type) = OGLEnumConverter.GetImageFormat(Image.Format);
|
2018-04-10 15:50:32 -04:00
|
|
|
|
|
|
|
GL.TexImage2D(
|
|
|
|
TextureTarget.Texture2D,
|
2018-04-13 14:12:58 -04:00
|
|
|
Level,
|
2018-09-18 00:30:35 -04:00
|
|
|
InternalFmt,
|
2018-08-19 21:25:26 -04:00
|
|
|
Image.Width,
|
|
|
|
Image.Height,
|
2018-04-10 15:50:32 -04:00
|
|
|
Border,
|
2018-04-13 14:12:58 -04:00
|
|
|
Format,
|
|
|
|
Type,
|
2018-06-08 20:15:56 -04:00
|
|
|
Data);
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 17:02:23 -04:00
|
|
|
private static bool IsAstc(GalImageFormat Format)
|
|
|
|
{
|
|
|
|
Format &= GalImageFormat.FormatMask;
|
|
|
|
|
|
|
|
return Format > GalImageFormat.Astc2DStart && Format < GalImageFormat.Astc2DEnd;
|
|
|
|
}
|
|
|
|
|
2018-09-18 00:30:35 -04:00
|
|
|
public bool TryGetImage(long Key, out GalImage Image)
|
2018-08-19 21:25:26 -04:00
|
|
|
{
|
2018-09-18 00:30:35 -04:00
|
|
|
if (TextureCache.TryGetValue(Key, out ImageHandler CachedImage))
|
2018-08-19 21:25:26 -04:00
|
|
|
{
|
2018-09-18 00:30:35 -04:00
|
|
|
Image = CachedImage.Image;
|
2018-08-19 21:25:26 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-18 00:30:35 -04:00
|
|
|
Image = default(GalImage);
|
2018-08-19 21:25:26 -04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-18 00:30:35 -04:00
|
|
|
public bool TryGetImageHandler(long Key, out ImageHandler CachedImage)
|
2018-06-01 23:50:56 -04:00
|
|
|
{
|
2018-09-18 00:30:35 -04:00
|
|
|
if (TextureCache.TryGetValue(Key, out CachedImage))
|
2018-06-08 20:15:56 -04:00
|
|
|
{
|
2018-09-18 00:30:35 -04:00
|
|
|
return true;
|
2018-06-08 20:15:56 -04:00
|
|
|
}
|
|
|
|
|
2018-09-18 00:30:35 -04:00
|
|
|
CachedImage = null;
|
2018-06-08 20:15:56 -04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-18 00:30:35 -04:00
|
|
|
public void Bind(long Key, int Index, GalImage Image)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2018-08-19 21:25:26 -04:00
|
|
|
if (TextureCache.TryGetValue(Key, out ImageHandler CachedImage))
|
2018-06-08 20:15:56 -04:00
|
|
|
{
|
|
|
|
GL.ActiveTexture(TextureUnit.Texture0 + Index);
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2018-08-19 21:25:26 -04:00
|
|
|
GL.BindTexture(TextureTarget.Texture2D, CachedImage.Handle);
|
2018-09-18 00:30:35 -04: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-08 20:15:56 -04:00
|
|
|
}
|
2018-04-13 14:12:58 -04:00
|
|
|
}
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2018-06-23 20:39:25 -04:00
|
|
|
public void SetSampler(GalTextureSampler Sampler)
|
2018-04-13 14:12:58 -04:00
|
|
|
{
|
2018-04-08 15:17:35 -04: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 14:09:34 -04:00
|
|
|
}
|