From 04e4baf8f89267b142f1d5ffacb3148733fd5756 Mon Sep 17 00:00:00 2001 From: LunaMoo Date: Thu, 21 Sep 2017 07:00:13 +0200 Subject: [PATCH] Revert "Merge pull request #9505 from hrydgard/remove-gl-bgra-path" Caused regression in FFIV on some gpu's --- GPU/GLES/DepalettizeShaderGLES.cpp | 5 ++++ GPU/GLES/FramebufferManagerGLES.cpp | 41 ++++++++++++++++++++++++----- GPU/GLES/TextureCacheGLES.cpp | 26 ++++++++++++------ GPU/GLES/TextureCacheGLES.h | 8 ++++++ 4 files changed, 65 insertions(+), 15 deletions(-) diff --git a/GPU/GLES/DepalettizeShaderGLES.cpp b/GPU/GLES/DepalettizeShaderGLES.cpp index f18513753a..299f0eb915 100644 --- a/GPU/GLES/DepalettizeShaderGLES.cpp +++ b/GPU/GLES/DepalettizeShaderGLES.cpp @@ -138,12 +138,17 @@ GLuint DepalShaderCacheGLES::GetClutTexture(GEPaletteFormat clutFormat, const u3 GLuint dstFmt = getClutDestFormat(clutFormat); int texturePixels = clutFormat == GE_CMODE_32BIT_ABGR8888 ? 256 : 512; + bool useBGRA = UseBGRA8888() && dstFmt == GL_UNSIGNED_BYTE; + DepalTexture *tex = new DepalTexture(); glGenTextures(1, &tex->texture); glBindTexture(GL_TEXTURE_2D, tex->texture); GLuint components = dstFmt == GL_UNSIGNED_SHORT_5_6_5 ? GL_RGB : GL_RGBA; GLuint components2 = components; + if (useBGRA) { + components2 = GL_BGRA_EXT; + } glTexImage2D(GL_TEXTURE_2D, 0, components, texturePixels, 1, 0, components2, dstFmt, (void *)rawClut); diff --git a/GPU/GLES/FramebufferManagerGLES.cpp b/GPU/GLES/FramebufferManagerGLES.cpp index 6fad8a9ee9..0aa21e22e9 100644 --- a/GPU/GLES/FramebufferManagerGLES.cpp +++ b/GPU/GLES/FramebufferManagerGLES.cpp @@ -747,6 +747,12 @@ void ConvertFromRGBA8888(u8 *dst, const u8 *src, u32 dstStride, u32 srcStride, u u32 *dst32 = (u32 *)dst; if (src == dst) { return; + } else if (UseBGRA8888()) { + for (u32 y = 0; y < height; ++y) { + ConvertBGRA8888ToRGBA8888(dst32, src32, width); + src32 += srcStride; + dst32 += dstStride; + } } else { // Here let's assume they don't intersect for (u32 y = 0; y < height; ++y) { @@ -760,7 +766,13 @@ void ConvertFromRGBA8888(u8 *dst, const u8 *src, u32 dstStride, u32 srcStride, u u16 *dst16 = (u16 *)dst; switch (format) { case GE_FORMAT_565: // BGR 565 - { + if (UseBGRA8888()) { + for (u32 y = 0; y < height; ++y) { + ConvertBGRA8888ToRGB565(dst16, src32, width); + src32 += srcStride; + dst16 += dstStride; + } + } else { for (u32 y = 0; y < height; ++y) { ConvertRGBA8888ToRGB565(dst16, src32, width); src32 += srcStride; @@ -769,7 +781,13 @@ void ConvertFromRGBA8888(u8 *dst, const u8 *src, u32 dstStride, u32 srcStride, u } break; case GE_FORMAT_5551: // ABGR 1555 - { + if (UseBGRA8888()) { + for (u32 y = 0; y < height; ++y) { + ConvertBGRA8888ToRGBA5551(dst16, src32, width); + src32 += srcStride; + dst16 += dstStride; + } + } else { for (u32 y = 0; y < height; ++y) { ConvertRGBA8888ToRGBA5551(dst16, src32, width); src32 += srcStride; @@ -778,7 +796,13 @@ void ConvertFromRGBA8888(u8 *dst, const u8 *src, u32 dstStride, u32 srcStride, u } break; case GE_FORMAT_4444: // ABGR 4444 - { + if (UseBGRA8888()) { + for (u32 y = 0; y < height; ++y) { + ConvertBGRA8888ToRGBA4444(dst16, src32, width); + src32 += srcStride; + dst16 += dstStride; + } + } else { for (u32 y = 0; y < height; ++y) { ConvertRGBA8888ToRGBA4444(dst16, src32, width); src32 += srcStride; @@ -890,7 +914,7 @@ void FramebufferManagerGLES::PackFramebufferAsync_(VirtualFramebuffer *vfb) { DEBUG_LOG(FRAMEBUF, "Reading PBO to memory , bufSize = %u, packed = %p, fb_address = %08x, stride = %u, pbo = %u", pbo.size, packed, pbo.fb_address, pbo.stride, nextPBO); - if (useCPU) { + if (useCPU || (UseBGRA8888() && pbo.format == GE_FORMAT_8888)) { u8 *dst = Memory::GetPointer(pbo.fb_address); ConvertFromRGBA8888(dst, packed, pbo.stride, pbo.stride, pbo.stride, pbo.height, pbo.format); } else { @@ -947,7 +971,7 @@ void FramebufferManagerGLES::PackFramebufferAsync_(VirtualFramebuffer *vfb) { case GE_FORMAT_8888: // 32 bit RGBA default: pixelType = GL_UNSIGNED_BYTE; - pixelFormat = GL_RGBA; + pixelFormat = UseBGRA8888() ? GL_BGRA_EXT : GL_RGBA; pixelSize = 4; align = 4; break; @@ -975,7 +999,7 @@ void FramebufferManagerGLES::PackFramebufferAsync_(VirtualFramebuffer *vfb) { if (useCPU) { // If converting pixel formats on the CPU we'll always request RGBA8888 glPixelStorei(GL_PACK_ALIGNMENT, 4); - SafeGLReadPixels(0, 0, vfb->fb_stride, vfb->height, GL_RGBA, GL_UNSIGNED_BYTE, 0); + SafeGLReadPixels(0, 0, vfb->fb_stride, vfb->height, UseBGRA8888() ? GL_BGRA_EXT : GL_RGBA, GL_UNSIGNED_BYTE, 0); } else { // Otherwise we'll directly request the format we need and let the GPU sort it out glPixelStorei(GL_PACK_ALIGNMENT, align); @@ -1017,7 +1041,7 @@ void FramebufferManagerGLES::PackFramebufferSync_(VirtualFramebuffer *vfb, int x u32 bufSize = vfb->fb_stride * h * 4; u32 fb_address = 0x04000000 | vfb->fb_address; - bool convert = vfb->format != GE_FORMAT_8888; + bool convert = vfb->format != GE_FORMAT_8888 || UseBGRA8888(); const int dstBpp = vfb->format == GE_FORMAT_8888 ? 4 : 2; const int packWidth = std::min(vfb->fb_stride, std::min(x + w, (int)vfb->width)); @@ -1042,6 +1066,9 @@ void FramebufferManagerGLES::PackFramebufferSync_(VirtualFramebuffer *vfb, int x glPixelStorei(GL_PACK_ALIGNMENT, 4); GLenum glfmt = GL_RGBA; + if (UseBGRA8888()) { + glfmt = GL_BGRA_EXT; + } CHECK_GL_ERROR_IF_DEBUG(); SafeGLReadPixels(0, y, h == 1 ? packWidth : vfb->fb_stride, h, glfmt, GL_UNSIGNED_BYTE, packed); diff --git a/GPU/GLES/TextureCacheGLES.cpp b/GPU/GLES/TextureCacheGLES.cpp index 48b115c517..bcd14c7099 100644 --- a/GPU/GLES/TextureCacheGLES.cpp +++ b/GPU/GLES/TextureCacheGLES.cpp @@ -230,9 +230,13 @@ static void ConvertColors(void *dstBuf, const void *srcBuf, GLuint dstFmt, int n ConvertRGB565ToBGR565((u16 *)dst, (const u16 *)src, numPixels); break; default: - // No need to convert RGBA8888, right order already - if (dst != src) - memcpy(dst, src, numPixels * sizeof(u32)); + if (UseBGRA8888()) { + ConvertRGBA8888ToBGRA8888(dst, src, numPixels); + } else { + // No need to convert RGBA8888, right order already + if (dst != src) + memcpy(dst, src, numPixels * sizeof(u32)); + } break; } } @@ -267,7 +271,7 @@ void TextureCacheGLES::UpdateCurrentClut(GEPaletteFormat clutFormat, u32 clutBas clutHash_ = DoReliableHash32((const char *)clutBufRaw_, clutExtendedBytes, 0xC0108888); // Avoid a copy when we don't need to convert colors. - if (clutFormat != GE_CMODE_32BIT_ABGR8888) { + if (UseBGRA8888() || clutFormat != GE_CMODE_32BIT_ABGR8888) { const int numColors = clutFormat == GE_CMODE_32BIT_ABGR8888 ? (clutMaxBytes_ / sizeof(u32)) : (clutMaxBytes_ / sizeof(u16)); ConvertColors(clutBufConverted_, clutBufRaw_, getClutDestFormat(clutFormat), numColors); clutBuf_ = clutBufConverted_; @@ -536,13 +540,13 @@ void TextureCacheGLES::ApplyTextureFramebuffer(TexCacheEntry *entry, VirtualFram InvalidateLastTexture(); } -ReplacedTextureFormat FromGLESFormat(GLenum fmt) { +ReplacedTextureFormat FromGLESFormat(GLenum fmt, bool useBGRA = false) { // TODO: 16-bit formats are incorrect, since swizzled. switch (fmt) { case GL_UNSIGNED_SHORT_5_6_5: return ReplacedTextureFormat::F_0565_ABGR; case GL_UNSIGNED_SHORT_5_5_5_1: return ReplacedTextureFormat::F_1555_ABGR; case GL_UNSIGNED_SHORT_4_4_4_4: return ReplacedTextureFormat::F_4444_ABGR; - case GL_UNSIGNED_BYTE: default: return ReplacedTextureFormat::F_8888; + case GL_UNSIGNED_BYTE: default: return useBGRA ? ReplacedTextureFormat::F_8888_BGRA : ReplacedTextureFormat::F_8888; } } @@ -812,7 +816,7 @@ void *TextureCacheGLES::DecodeTextureLevelOld(GETextureFormat format, GEPaletteF } tmpTexBufRearrange_.resize(std::max(w, bufw) * h); - DecodeTextureLevel((u8 *)tmpTexBufRearrange_.data(), decPitch, format, clutformat, texaddr, level, bufw, true, false, false); + DecodeTextureLevel((u8 *)tmpTexBufRearrange_.data(), decPitch, format, clutformat, texaddr, level, bufw, true, UseBGRA8888(), false); return tmpTexBufRearrange_.data(); } @@ -841,6 +845,7 @@ void TextureCacheGLES::LoadTextureLevel(TexCacheEntry &entry, ReplacedTexture &r int w = gstate.getTextureWidth(level); int h = gstate.getTextureHeight(level); bool useUnpack = false; + bool useBGRA; u32 *pixelData; CHECK_GL_ERROR_IF_DEBUG(); @@ -861,6 +866,7 @@ void TextureCacheGLES::LoadTextureLevel(TexCacheEntry &entry, ReplacedTexture &r dstFmt = ToGLESFormat(replaced.Format(level)); texByteAlign = bpp; + useBGRA = false; } else { PROFILE_THIS_SCOPE("decodetex"); @@ -879,6 +885,7 @@ void TextureCacheGLES::LoadTextureLevel(TexCacheEntry &entry, ReplacedTexture &r // Textures are always aligned to 16 bytes bufw, so this could safely be 4 always. texByteAlign = dstFmt == GL_UNSIGNED_BYTE ? 4 : 2; + useBGRA = UseBGRA8888() && dstFmt == GL_UNSIGNED_BYTE; pixelData = (u32 *)finalBuf; if (scaleFactor > 1) @@ -899,7 +906,7 @@ void TextureCacheGLES::LoadTextureLevel(TexCacheEntry &entry, ReplacedTexture &r replacedInfo.isVideo = videos_.find(entry.addr & 0x3FFFFFFF) != videos_.end(); replacedInfo.isFinal = (entry.status & TexCacheEntry::STATUS_TO_SCALE) == 0; replacedInfo.scaleFactor = scaleFactor; - replacedInfo.fmt = FromGLESFormat(dstFmt); + replacedInfo.fmt = FromGLESFormat(dstFmt, useBGRA); int bpp = dstFmt == GL_UNSIGNED_BYTE ? 4 : 2; replacer_.NotifyTextureDecoded(replacedInfo, pixelData, (useUnpack ? bufw : w) * bpp, level, w, h); @@ -913,6 +920,9 @@ void TextureCacheGLES::LoadTextureLevel(TexCacheEntry &entry, ReplacedTexture &r GLuint components = dstFmt == GL_UNSIGNED_SHORT_5_6_5 ? GL_RGB : GL_RGBA; GLuint components2 = components; + if (useBGRA) { + components2 = GL_BGRA_EXT; + } if (replaceImages) { PROFILE_THIS_SCOPE("repltex"); diff --git a/GPU/GLES/TextureCacheGLES.h b/GPU/GLES/TextureCacheGLES.h index b7c1eeaeb3..0d8ec3ea9f 100644 --- a/GPU/GLES/TextureCacheGLES.h +++ b/GPU/GLES/TextureCacheGLES.h @@ -32,6 +32,14 @@ class DepalShaderCacheGLES; class ShaderManagerGLES; class DrawEngineGLES; +inline bool UseBGRA8888() { + // TODO: Other platforms? May depend on vendor which is faster? +#ifdef _WIN32 + return gl_extensions.EXT_bgra; +#endif + return false; +} + class TextureCacheGLES : public TextureCacheCommon { public: TextureCacheGLES(Draw::DrawContext *draw);