From 8dbcae1ff88927dc0734d5f0e24fbf8781d68590 Mon Sep 17 00:00:00 2001
From: gdkchan <gab.dark.100@gmail.com>
Date: Sun, 26 Jul 2020 00:03:40 -0300
Subject: Implement BGRA texture support (#1418)

* Implement BGRA texture support

* Missing AppendLine

* Remove empty lines

* Address PR feedback
---
 Ryujinx.Graphics.OpenGL/Image/TextureView.cs | 44 +++++++++++++++++++++-------
 1 file changed, 33 insertions(+), 11 deletions(-)

(limited to 'Ryujinx.Graphics.OpenGL/Image/TextureView.cs')

diff --git a/Ryujinx.Graphics.OpenGL/Image/TextureView.cs b/Ryujinx.Graphics.OpenGL/Image/TextureView.cs
index 02353ffa..2d50eba5 100644
--- a/Ryujinx.Graphics.OpenGL/Image/TextureView.cs
+++ b/Ryujinx.Graphics.OpenGL/Image/TextureView.cs
@@ -72,6 +72,15 @@ namespace Ryujinx.Graphics.OpenGL.Image
                 (int)Info.SwizzleA.Convert()
             };
 
+            if (Info.Format.IsBgra8())
+            {
+                // 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.
+                int temp = swizzleRgba[0];
+                swizzleRgba[0] = swizzleRgba[2];
+                swizzleRgba[2] = temp;
+            }
+
             GL.TexParameter(target, TextureParameterName.TextureSwizzleRgba, swizzleRgba);
 
             int maxLevel = Info.Levels - 1;
@@ -189,7 +198,12 @@ namespace Ryujinx.Graphics.OpenGL.Image
             return data;
         }
 
-        private void WriteTo(IntPtr ptr)
+        public void WriteToPbo(int offset, bool forceBgra)
+        {
+            WriteTo(IntPtr.Zero + offset, forceBgra);
+        }
+
+        private void WriteTo(IntPtr data, bool forceBgra = false)
         {
             TextureTarget target = Target.Convert();
 
@@ -197,6 +211,14 @@ namespace Ryujinx.Graphics.OpenGL.Image
 
             FormatInfo format = FormatTable.GetFormatInfo(Info.Format);
 
+            PixelFormat pixelFormat = format.PixelFormat;
+            PixelType   pixelType   = format.PixelType;
+
+            if (forceBgra)
+            {
+                pixelFormat = PixelFormat.Bgra;
+            }
+
             int faces = 1;
 
             if (target == TextureTarget.TextureCubeMap)
@@ -214,20 +236,15 @@ namespace Ryujinx.Graphics.OpenGL.Image
 
                     if (format.IsCompressed)
                     {
-                        GL.GetCompressedTexImage(target + face, level, ptr + faceOffset);
+                        GL.GetCompressedTexImage(target + face, level, data + faceOffset);
                     }
                     else
                     {
-                        GL.GetTexImage(
-                            target + face,
-                            level,
-                            format.PixelFormat,
-                            format.PixelType,
-                            ptr + faceOffset);
+                        GL.GetTexImage(target + face, level, pixelFormat, pixelType, data + faceOffset);
                     }
                 }
 
-                ptr += Info.GetMipSize(level);
+                data += Info.GetMipSize(level);
             }
         }
 
@@ -237,12 +254,17 @@ namespace Ryujinx.Graphics.OpenGL.Image
             {
                 fixed (byte* ptr = data)
                 {
-                    SetData((IntPtr)ptr, data.Length);
+                    ReadFrom((IntPtr)ptr, data.Length);
                 }
             }
         }
 
-        private void SetData(IntPtr data, int size)
+        public void ReadFromPbo(int offset, int size)
+        {
+            ReadFrom(IntPtr.Zero + offset, size);
+        }
+
+        private void ReadFrom(IntPtr data, int size)
         {
             TextureTarget target = Target.Convert();
 
-- 
cgit v1.2.3-70-g09d2