2018-04-08 21:17:35 +02:00
|
|
|
using OpenTK.Graphics.OpenGL;
|
2018-06-02 05:50:56 +02:00
|
|
|
using Ryujinx.Graphics.Gal.Texture;
|
|
|
|
using System;
|
2018-04-08 21:17:35 +02:00
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gal.OpenGL
|
|
|
|
{
|
|
|
|
class OGLTexture
|
|
|
|
{
|
2018-06-09 02:15:56 +02:00
|
|
|
private class TCE
|
|
|
|
{
|
|
|
|
public int Handle;
|
|
|
|
|
|
|
|
public GalTexture Texture;
|
|
|
|
|
|
|
|
public TCE(int Handle, GalTexture Texture)
|
|
|
|
{
|
|
|
|
this.Handle = Handle;
|
|
|
|
this.Texture = Texture;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private OGLCachedResource<TCE> TextureCache;
|
2018-04-08 21:17:35 +02:00
|
|
|
|
|
|
|
public OGLTexture()
|
|
|
|
{
|
2018-06-09 02:15:56 +02:00
|
|
|
TextureCache = new OGLCachedResource<TCE>(DeleteTexture);
|
2018-04-08 21:17:35 +02:00
|
|
|
}
|
|
|
|
|
2018-06-09 02:15:56 +02:00
|
|
|
private static void DeleteTexture(TCE CachedTexture)
|
2018-04-08 21:17:35 +02:00
|
|
|
{
|
2018-06-09 02:15:56 +02:00
|
|
|
GL.DeleteTexture(CachedTexture.Handle);
|
|
|
|
}
|
2018-04-08 21:17:35 +02:00
|
|
|
|
2018-06-09 02:15:56 +02:00
|
|
|
public void Create(long Tag, byte[] Data, GalTexture Texture)
|
|
|
|
{
|
|
|
|
int Handle = GL.GenTexture();
|
|
|
|
|
|
|
|
TextureCache.AddOrUpdate(Tag, new TCE(Handle, Texture), (uint)Data.Length);
|
|
|
|
|
|
|
|
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-04-10 21:50:32 +02:00
|
|
|
if (IsCompressedTextureFormat(Texture.Format))
|
2018-04-08 21:17:35 +02:00
|
|
|
{
|
2018-04-10 21:50:32 +02:00
|
|
|
PixelInternalFormat InternalFmt = OGLEnumConverter.GetCompressedTextureFormat(Texture.Format);
|
|
|
|
|
|
|
|
GL.CompressedTexImage2D(
|
|
|
|
TextureTarget.Texture2D,
|
2018-04-13 20:12:58 +02:00
|
|
|
Level,
|
2018-04-10 21:50:32 +02:00
|
|
|
InternalFmt,
|
|
|
|
Texture.Width,
|
|
|
|
Texture.Height,
|
|
|
|
Border,
|
2018-06-09 02:15:56 +02:00
|
|
|
Data.Length,
|
|
|
|
Data);
|
2018-04-08 21:17:35 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-02 05:50:56 +02:00
|
|
|
if (Texture.Format >= GalTextureFormat.Astc2D4x4)
|
|
|
|
{
|
2018-06-09 02:15:56 +02:00
|
|
|
int TextureBlockWidth = GetAstcBlockWidth(Texture.Format);
|
|
|
|
int TextureBlockHeight = GetAstcBlockHeight(Texture.Format);
|
|
|
|
|
|
|
|
Data = ASTCDecoder.DecodeToRGBA8888(
|
|
|
|
Data,
|
|
|
|
TextureBlockWidth,
|
|
|
|
TextureBlockHeight, 1,
|
|
|
|
Texture.Width,
|
|
|
|
Texture.Height, 1);
|
|
|
|
|
|
|
|
Texture.Format = GalTextureFormat.A8B8G8R8;
|
2018-06-02 05:50:56 +02:00
|
|
|
}
|
|
|
|
|
2018-04-10 21:50:32 +02:00
|
|
|
const PixelInternalFormat InternalFmt = PixelInternalFormat.Rgba;
|
|
|
|
|
2018-04-13 20:12:58 +02:00
|
|
|
(PixelFormat Format, PixelType Type) = OGLEnumConverter.GetTextureFormat(Texture.Format);
|
2018-04-10 21:50:32 +02:00
|
|
|
|
|
|
|
GL.TexImage2D(
|
|
|
|
TextureTarget.Texture2D,
|
2018-04-13 20:12:58 +02:00
|
|
|
Level,
|
2018-04-10 21:50:32 +02:00
|
|
|
InternalFmt,
|
|
|
|
Texture.Width,
|
|
|
|
Texture.Height,
|
|
|
|
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-05-17 20:25:42 +02:00
|
|
|
|
|
|
|
int SwizzleR = (int)OGLEnumConverter.GetTextureSwizzle(Texture.XSource);
|
|
|
|
int SwizzleG = (int)OGLEnumConverter.GetTextureSwizzle(Texture.YSource);
|
|
|
|
int SwizzleB = (int)OGLEnumConverter.GetTextureSwizzle(Texture.ZSource);
|
|
|
|
int SwizzleA = (int)OGLEnumConverter.GetTextureSwizzle(Texture.WSource);
|
|
|
|
|
|
|
|
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 21:17:35 +02:00
|
|
|
}
|
|
|
|
|
2018-06-02 18:39:52 +02:00
|
|
|
private static int GetAstcBlockWidth(GalTextureFormat Format)
|
2018-06-02 05:50:56 +02:00
|
|
|
{
|
|
|
|
switch (Format)
|
|
|
|
{
|
|
|
|
case GalTextureFormat.Astc2D4x4: return 4;
|
|
|
|
case GalTextureFormat.Astc2D5x5: return 5;
|
|
|
|
case GalTextureFormat.Astc2D6x6: return 6;
|
|
|
|
case GalTextureFormat.Astc2D8x8: return 8;
|
|
|
|
case GalTextureFormat.Astc2D10x10: return 10;
|
|
|
|
case GalTextureFormat.Astc2D12x12: return 12;
|
|
|
|
case GalTextureFormat.Astc2D5x4: return 5;
|
|
|
|
case GalTextureFormat.Astc2D6x5: return 6;
|
|
|
|
case GalTextureFormat.Astc2D8x6: return 8;
|
|
|
|
case GalTextureFormat.Astc2D10x8: return 10;
|
|
|
|
case GalTextureFormat.Astc2D12x10: return 12;
|
|
|
|
case GalTextureFormat.Astc2D8x5: return 8;
|
|
|
|
case GalTextureFormat.Astc2D10x5: return 10;
|
|
|
|
case GalTextureFormat.Astc2D10x6: return 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new ArgumentException(nameof(Format));
|
|
|
|
}
|
|
|
|
|
2018-06-02 18:39:52 +02:00
|
|
|
private static int GetAstcBlockHeight(GalTextureFormat Format)
|
2018-06-02 05:50:56 +02:00
|
|
|
{
|
|
|
|
switch (Format)
|
|
|
|
{
|
|
|
|
case GalTextureFormat.Astc2D4x4: return 4;
|
|
|
|
case GalTextureFormat.Astc2D5x5: return 5;
|
|
|
|
case GalTextureFormat.Astc2D6x6: return 6;
|
|
|
|
case GalTextureFormat.Astc2D8x8: return 8;
|
|
|
|
case GalTextureFormat.Astc2D10x10: return 10;
|
|
|
|
case GalTextureFormat.Astc2D12x12: return 12;
|
|
|
|
case GalTextureFormat.Astc2D5x4: return 4;
|
|
|
|
case GalTextureFormat.Astc2D6x5: return 5;
|
|
|
|
case GalTextureFormat.Astc2D8x6: return 6;
|
|
|
|
case GalTextureFormat.Astc2D10x8: return 8;
|
|
|
|
case GalTextureFormat.Astc2D12x10: return 10;
|
|
|
|
case GalTextureFormat.Astc2D8x5: return 5;
|
|
|
|
case GalTextureFormat.Astc2D10x5: return 5;
|
|
|
|
case GalTextureFormat.Astc2D10x6: return 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new ArgumentException(nameof(Format));
|
|
|
|
}
|
|
|
|
|
2018-06-09 02:15:56 +02:00
|
|
|
public bool TryGetCachedTexture(long Tag, long DataSize, out GalTexture Texture)
|
|
|
|
{
|
|
|
|
if (TextureCache.TryGetSize(Tag, out long Size) && Size == DataSize)
|
|
|
|
{
|
|
|
|
if (TextureCache.TryGetValue(Tag, out TCE CachedTexture))
|
|
|
|
{
|
|
|
|
Texture = CachedTexture.Texture;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Texture = default(GalTexture);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Bind(long Tag, int Index)
|
2018-04-08 21:17:35 +02:00
|
|
|
{
|
2018-06-09 02:15:56 +02:00
|
|
|
if (TextureCache.TryGetValue(Tag, out TCE CachedTexture))
|
|
|
|
{
|
|
|
|
GL.ActiveTexture(TextureUnit.Texture0 + Index);
|
2018-04-08 21:17:35 +02:00
|
|
|
|
2018-06-09 02:15:56 +02:00
|
|
|
GL.BindTexture(TextureTarget.Texture2D, CachedTexture.Handle);
|
|
|
|
}
|
2018-04-13 20:12:58 +02:00
|
|
|
}
|
2018-04-08 21:17:35 +02:00
|
|
|
|
2018-04-13 20:12:58 +02:00
|
|
|
public static void Set(GalTextureSampler Sampler)
|
|
|
|
{
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool IsCompressedTextureFormat(GalTextureFormat Format)
|
|
|
|
{
|
2018-04-10 23:54:50 +02:00
|
|
|
switch (Format)
|
|
|
|
{
|
2018-06-10 06:00:43 +02:00
|
|
|
case GalTextureFormat.BC7U:
|
2018-04-10 23:54:50 +02:00
|
|
|
case GalTextureFormat.BC1:
|
|
|
|
case GalTextureFormat.BC2:
|
|
|
|
case GalTextureFormat.BC3:
|
|
|
|
case GalTextureFormat.BC4:
|
|
|
|
case GalTextureFormat.BC5:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2018-04-08 21:17:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|