From 66e7fdb8710ff24bfb80c9c638ce2e6fa9563068 Mon Sep 17 00:00:00 2001
From: gdkchan <gab.dark.100@gmail.com>
Date: Mon, 8 Aug 2022 14:39:22 -0300
Subject: [PATCH] OpenGL: Fix clears of unbound color targets (#3564)

---
 Ryujinx.Graphics.OpenGL/Framebuffer.cs | 2 +-
 Ryujinx.Graphics.OpenGL/Pipeline.cs    | 4 ++++
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/Ryujinx.Graphics.OpenGL/Framebuffer.cs b/Ryujinx.Graphics.OpenGL/Framebuffer.cs
index d132e9c49..b180b8578 100644
--- a/Ryujinx.Graphics.OpenGL/Framebuffer.cs
+++ b/Ryujinx.Graphics.OpenGL/Framebuffer.cs
@@ -147,7 +147,7 @@ namespace Ryujinx.Graphics.OpenGL
 
         public int GetColorLayerCount(int index)
         {
-            return _colors[index].Info.GetDepthOrLayers();
+            return _colors[index]?.Info.GetDepthOrLayers() ?? 0;
         }
 
         public int GetDepthStencilLayerCount()
diff --git a/Ryujinx.Graphics.OpenGL/Pipeline.cs b/Ryujinx.Graphics.OpenGL/Pipeline.cs
index 58d165d93..fc28c86e8 100644
--- a/Ryujinx.Graphics.OpenGL/Pipeline.cs
+++ b/Ryujinx.Graphics.OpenGL/Pipeline.cs
@@ -112,6 +112,8 @@ namespace Ryujinx.Graphics.OpenGL
 
         public void ClearRenderTargetColor(int index, int layer, int layerCount, uint componentMask, ColorF color)
         {
+            EnsureFramebuffer();
+
             GL.ColorMask(
                 index,
                 (componentMask & 1) != 0,
@@ -142,6 +144,8 @@ namespace Ryujinx.Graphics.OpenGL
 
         public void ClearRenderTargetDepthStencil(int layer, int layerCount, float depthValue, bool depthMask, int stencilValue, int stencilMask)
         {
+            EnsureFramebuffer();
+
             bool stencilMaskChanged =
                 stencilMask != 0 &&
                 stencilMask != _stencilFrontMask;