From c702943af3c7e9396b8fa86e3c1be3cb9339addc Mon Sep 17 00:00:00 2001 From: gdkchan Date: Fri, 20 Aug 2021 18:26:25 -0300 Subject: [PATCH] Swap BGR components for 16-bit BGR texture formats (#2567) --- Ryujinx.Graphics.GAL/Format.cs | 9 ++++++--- Ryujinx.Graphics.OpenGL/FormatTable.cs | 6 +++--- Ryujinx.Graphics.OpenGL/Image/TextureCopy.cs | 2 +- Ryujinx.Graphics.OpenGL/Image/TextureView.cs | 2 +- Ryujinx.Graphics.OpenGL/Pipeline.cs | 2 +- Ryujinx.Graphics.OpenGL/Window.cs | 6 +++--- 6 files changed, 15 insertions(+), 12 deletions(-) diff --git a/Ryujinx.Graphics.GAL/Format.cs b/Ryujinx.Graphics.GAL/Format.cs index 1d034921f5..98b6f5068f 100644 --- a/Ryujinx.Graphics.GAL/Format.cs +++ b/Ryujinx.Graphics.GAL/Format.cs @@ -235,14 +235,17 @@ namespace Ryujinx.Graphics.GAL } /// - /// Checks if the texture format is a BGRA format with 8-bit components. + /// Checks if the texture format is a BGR format. /// /// Texture format - /// True if the texture format is a BGRA format with 8-bit components, false otherwise - public static bool IsBgra8(this Format format) + /// True if the texture format is a BGR format, false otherwise + public static bool IsBgr(this Format format) { switch (format) { + case Format.B5G6R5Unorm: + case Format.B5G5R5X1Unorm: + case Format.B5G5R5A1Unorm: case Format.B8G8R8X8Unorm: case Format.B8G8R8A8Unorm: case Format.B8G8R8X8Srgb: diff --git a/Ryujinx.Graphics.OpenGL/FormatTable.cs b/Ryujinx.Graphics.OpenGL/FormatTable.cs index 8c926fc28c..b673eb48ee 100644 --- a/Ryujinx.Graphics.OpenGL/FormatTable.cs +++ b/Ryujinx.Graphics.OpenGL/FormatTable.cs @@ -168,9 +168,9 @@ namespace Ryujinx.Graphics.OpenGL Add(Format.Astc10x10Srgb, new FormatInfo(4, false, false, All.CompressedSrgb8Alpha8Astc10X10Khr)); Add(Format.Astc12x10Srgb, new FormatInfo(4, false, false, All.CompressedSrgb8Alpha8Astc12X10Khr)); Add(Format.Astc12x12Srgb, new FormatInfo(4, false, false, All.CompressedSrgb8Alpha8Astc12X12Khr)); - Add(Format.B5G6R5Unorm, new FormatInfo(3, true, false, All.Rgb565, PixelFormat.Rgb, PixelType.UnsignedShort565)); - Add(Format.B5G5R5X1Unorm, new FormatInfo(4, true, false, All.Rgb5, PixelFormat.Bgra, PixelType.UnsignedShort5551)); - Add(Format.B5G5R5A1Unorm, new FormatInfo(4, true, false, All.Rgb5A1, PixelFormat.Bgra, PixelType.UnsignedShort5551)); + Add(Format.B5G6R5Unorm, new FormatInfo(3, true, false, All.Rgb565, PixelFormat.Rgb, PixelType.UnsignedShort565Reversed)); + Add(Format.B5G5R5X1Unorm, new FormatInfo(4, true, false, All.Rgb5, PixelFormat.Rgba, PixelType.UnsignedShort1555Reversed)); + Add(Format.B5G5R5A1Unorm, new FormatInfo(4, true, false, All.Rgb5A1, PixelFormat.Rgba, PixelType.UnsignedShort1555Reversed)); Add(Format.A1B5G5R5Unorm, new FormatInfo(4, true, false, All.Rgb5A1, PixelFormat.Bgra, PixelType.UnsignedShort1555Reversed)); Add(Format.B8G8R8X8Unorm, new FormatInfo(4, true, false, All.Rgba8, PixelFormat.Rgba, PixelType.UnsignedByte)); Add(Format.B8G8R8A8Unorm, new FormatInfo(4, true, false, All.Rgba8, PixelFormat.Rgba, PixelType.UnsignedByte)); diff --git a/Ryujinx.Graphics.OpenGL/Image/TextureCopy.cs b/Ryujinx.Graphics.OpenGL/Image/TextureCopy.cs index e8dfcc4684..7811d02156 100644 --- a/Ryujinx.Graphics.OpenGL/Image/TextureCopy.cs +++ b/Ryujinx.Graphics.OpenGL/Image/TextureCopy.cs @@ -27,7 +27,7 @@ namespace Ryujinx.Graphics.OpenGL.Image Extents2D dstRegion, bool linearFilter) { - TextureView srcConverted = src.Format.IsBgra8() != dst.Format.IsBgra8() ? BgraSwap(src) : src; + TextureView srcConverted = src.Format.IsBgr() != dst.Format.IsBgr() ? BgraSwap(src) : src; (int oldDrawFramebufferHandle, int oldReadFramebufferHandle) = ((Pipeline)_renderer.Pipeline).GetBoundFramebuffers(); diff --git a/Ryujinx.Graphics.OpenGL/Image/TextureView.cs b/Ryujinx.Graphics.OpenGL/Image/TextureView.cs index ce0582ce93..d4d490754b 100644 --- a/Ryujinx.Graphics.OpenGL/Image/TextureView.cs +++ b/Ryujinx.Graphics.OpenGL/Image/TextureView.cs @@ -70,7 +70,7 @@ namespace Ryujinx.Graphics.OpenGL.Image (int)Info.SwizzleA.Convert() }; - if (Info.Format.IsBgra8()) + if (Info.Format.IsBgr()) { // Swap B <-> R for BGRA formats, as OpenGL has no support for them // and we need to manually swap the components on read/write on the GPU. diff --git a/Ryujinx.Graphics.OpenGL/Pipeline.cs b/Ryujinx.Graphics.OpenGL/Pipeline.cs index 7c626f54a0..be526fa99a 100644 --- a/Ryujinx.Graphics.OpenGL/Pipeline.cs +++ b/Ryujinx.Graphics.OpenGL/Pipeline.cs @@ -879,7 +879,7 @@ namespace Ryujinx.Graphics.OpenGL _framebuffer.AttachColor(index, color); - int isBgra = color != null && color.Format.IsBgra8() ? 1 : 0; + int isBgra = color != null && color.Format.IsBgr() ? 1 : 0; if (_fpIsBgra[index].X != isBgra) { diff --git a/Ryujinx.Graphics.OpenGL/Window.cs b/Ryujinx.Graphics.OpenGL/Window.cs index 35b04d6d9d..b80348cdb0 100644 --- a/Ryujinx.Graphics.OpenGL/Window.cs +++ b/Ryujinx.Graphics.OpenGL/Window.cs @@ -45,7 +45,7 @@ namespace Ryujinx.Graphics.OpenGL GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, drawFramebuffer); GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, readFramebuffer); - TextureView viewConverted = view.Format.IsBgra8() ? _renderer.TextureCopy.BgraSwap(view) : view; + TextureView viewConverted = view.Format.IsBgr() ? _renderer.TextureCopy.BgraSwap(view) : view; GL.FramebufferTexture( FramebufferTarget.ReadFramebuffer, @@ -110,7 +110,7 @@ namespace Ryujinx.Graphics.OpenGL if (ScreenCaptureRequested) { - CaptureFrame(srcX0, srcY0, srcX1, srcY1, view.Format.IsBgra8(), crop.FlipX, crop.FlipY); + CaptureFrame(srcX0, srcY0, srcX1, srcY1, view.Format.IsBgr(), crop.FlipX, crop.FlipY); ScreenCaptureRequested = false; } @@ -174,7 +174,7 @@ namespace Ryujinx.Graphics.OpenGL byte[] bitmap = new byte[size]; GL.ReadPixels(x, y, width, height, isBgra ? PixelFormat.Bgra : PixelFormat.Rgba, PixelType.UnsignedByte, bitmap); - + _renderer.OnScreenCaptured(new ScreenCaptureImageInfo(width, height, isBgra, bitmap, flipX, flipY)); }