diff options
| author | gdkchan <gab.dark.100@gmail.com> | 2018-06-08 21:15:56 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-06-08 21:15:56 -0300 |
| commit | 231fae1a4c97d7588655e9775f37c1dc9bd55fb0 (patch) | |
| tree | 1c0e7b298ec33d5bf5b6a5693dd69a8c7e0bd23b /Ryujinx.Graphics/Gal/OpenGL/OGLTexture.cs | |
| parent | 6fe51f970501fe732276c17ed0dacb564b92a73d (diff) | |
Texture/Vertex/Index data cache (#132)
* Initial implementation of the texture cache
* Cache vertex and index data aswell, some cleanup
* Improve handling of the cache by storing cached ranges on a list for each page
* Delete old data from the caches automatically, ensure that the cache is cleaned when the mapping/size changes, and some general cleanup
Diffstat (limited to 'Ryujinx.Graphics/Gal/OpenGL/OGLTexture.cs')
| -rw-r--r-- | Ryujinx.Graphics/Gal/OpenGL/OGLTexture.cs | 103 |
1 files changed, 62 insertions, 41 deletions
diff --git a/Ryujinx.Graphics/Gal/OpenGL/OGLTexture.cs b/Ryujinx.Graphics/Gal/OpenGL/OGLTexture.cs index 2cbe6913..540e4735 100644 --- a/Ryujinx.Graphics/Gal/OpenGL/OGLTexture.cs +++ b/Ryujinx.Graphics/Gal/OpenGL/OGLTexture.cs @@ -6,18 +6,38 @@ namespace Ryujinx.Graphics.Gal.OpenGL { class OGLTexture { - private int[] Textures; + 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; public OGLTexture() { - Textures = new int[80]; + TextureCache = new OGLCachedResource<TCE>(DeleteTexture); } - public void Set(int Index, GalTexture Texture) + private static void DeleteTexture(TCE CachedTexture) { - GL.ActiveTexture(TextureUnit.Texture0 + Index); + GL.DeleteTexture(CachedTexture.Handle); + } - Bind(Index); + 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); const int Level = 0; //TODO: Support mipmap textures. const int Border = 0; @@ -33,14 +53,24 @@ namespace Ryujinx.Graphics.Gal.OpenGL Texture.Width, Texture.Height, Border, - Texture.Data.Length, - Texture.Data); + Data.Length, + Data); } else { if (Texture.Format >= GalTextureFormat.Astc2D4x4) { - Texture = ConvertAstcTextureToRgba(Texture); + 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; } const PixelInternalFormat InternalFmt = PixelInternalFormat.Rgba; @@ -56,7 +86,7 @@ namespace Ryujinx.Graphics.Gal.OpenGL Border, Format, Type, - Texture.Data); + Data); } int SwizzleR = (int)OGLEnumConverter.GetTextureSwizzle(Texture.XSource); @@ -70,23 +100,6 @@ namespace Ryujinx.Graphics.Gal.OpenGL GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureSwizzleA, SwizzleA); } - private static GalTexture ConvertAstcTextureToRgba(GalTexture Texture) - { - int TextureBlockWidth = GetAstcBlockWidth(Texture.Format); - int TextureBlockHeight = GetAstcBlockHeight(Texture.Format); - - Texture.Data = ASTCDecoder.DecodeToRGBA8888( - Texture.Data, - TextureBlockWidth, - TextureBlockHeight, 1, - Texture.Width, - Texture.Height, 1); - - Texture.Format = GalTextureFormat.A8B8G8R8; - - return Texture; - } - private static int GetAstcBlockWidth(GalTextureFormat Format) { switch (Format) @@ -133,11 +146,31 @@ namespace Ryujinx.Graphics.Gal.OpenGL throw new ArgumentException(nameof(Format)); } - public void Bind(int Index) + 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) { - int Handle = EnsureTextureInitialized(Index); + if (TextureCache.TryGetValue(Tag, out TCE CachedTexture)) + { + GL.ActiveTexture(TextureUnit.Texture0 + Index); - GL.BindTexture(TextureTarget.Texture2D, Handle); + GL.BindTexture(TextureTarget.Texture2D, CachedTexture.Handle); + } } public static void Set(GalTextureSampler Sampler) @@ -179,17 +212,5 @@ namespace Ryujinx.Graphics.Gal.OpenGL return false; } - - private int EnsureTextureInitialized(int TexIndex) - { - int Handle = Textures[TexIndex]; - - if (Handle == 0) - { - Handle = Textures[TexIndex] = GL.GenTexture(); - } - - return Handle; - } } }
\ No newline at end of file |
