From e75c1a3b0afbbdc0e5e9c6dbbf2a185d1567c3c6 Mon Sep 17 00:00:00 2001
From: Stenzek <stenzek@gmail.com>
Date: Fri, 24 Nov 2023 21:35:54 +1000
Subject: [PATCH] OpenGLDevice: Use glClearBuffer() instead of glClear()

---
 src/util/opengl_device.cpp  | 11 ++++-------
 src/util/opengl_texture.cpp | 29 ++++++-----------------------
 2 files changed, 10 insertions(+), 30 deletions(-)

diff --git a/src/util/opengl_device.cpp b/src/util/opengl_device.cpp
index 4dd968df0..429801f27 100644
--- a/src/util/opengl_device.cpp
+++ b/src/util/opengl_device.cpp
@@ -20,6 +20,8 @@
 
 Log_SetChannel(OpenGLDevice);
 
+static constexpr std::array<float, 4> s_clear_color = {{0.0f, 0.0f, 0.0f, 1.0f}};
+
 OpenGLDevice::OpenGLDevice()
 {
   // Something which won't be matched..
@@ -598,12 +600,9 @@ void OpenGLDevice::SetSwapInterval()
 void OpenGLDevice::RenderBlankFrame()
 {
   glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
-  glDisable(GL_SCISSOR_TEST);
-  glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
-  glClear(GL_COLOR_BUFFER_BIT);
+  glClearBufferfv(GL_COLOR, 0, s_clear_color.data());
   m_gl_context->SwapBuffers();
   glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_current_framebuffer ? m_current_framebuffer->GetGLId() : 0);
-  glEnable(GL_SCISSOR_TEST);
 }
 
 GPUDevice::AdapterAndModeList OpenGLDevice::GetAdapterAndModeList()
@@ -697,6 +696,7 @@ bool OpenGLDevice::BeginPresent(bool skip_present)
   }
 
   glBindFramebuffer(GL_FRAMEBUFFER, 0);
+  glClearBufferfv(GL_COLOR, 0, s_clear_color.data());
 
   const Common::Rectangle<s32> window_rc =
     Common::Rectangle<s32>::FromExtents(0, 0, m_window_info.surface_width, m_window_info.surface_height);
@@ -705,9 +705,6 @@ bool OpenGLDevice::BeginPresent(bool skip_present)
   m_last_scissor = window_rc;
   UpdateViewport();
   UpdateScissor();
-
-  glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
-  glClear(GL_COLOR_BUFFER_BIT);
   return true;
 }
 
diff --git a/src/util/opengl_texture.cpp b/src/util/opengl_texture.cpp
index 5f308fe82..f3365298e 100644
--- a/src/util/opengl_texture.cpp
+++ b/src/util/opengl_texture.cpp
@@ -481,22 +481,16 @@ void OpenGLDevice::CommitClear(OpenGLTexture* tex)
         const GLenum attachment = tex->IsDepthStencil() ? GL_DEPTH_ATTACHMENT : GL_COLOR_ATTACHMENT0;
         glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, attachment, tex->GetGLTarget(), tex->GetGLId(), 0);
 
-        glDisable(GL_SCISSOR_TEST);
         if (tex->IsDepthStencil())
         {
-          if (glClearDepthf)
-            glClearDepthf(tex->GetClearDepth());
-          else
-            glClearDepth(tex->GetClearDepth());
-          glClear(GL_DEPTH_BUFFER_BIT);
+          const float depth = tex->GetClearDepth();
+          glClearBufferfv(GL_DEPTH, 0, &depth);
         }
         else
         {
           const auto color = tex->GetUNormClearColor();
-          glClearColor(color[0], color[1], color[2], color[3]);
-          glClear(GL_COLOR_BUFFER_BIT);
+          glClearBufferfv(GL_COLOR, 0, color.data());
         }
-        glEnable(GL_SCISSOR_TEST);
 
         glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, attachment, GL_TEXTURE_2D, 0, 0);
         glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_current_framebuffer ? m_current_framebuffer->GetGLId() : 0);
@@ -515,7 +509,6 @@ void OpenGLDevice::CommitClear(OpenGLTexture* tex)
 
 void OpenGLDevice::CommitClear(OpenGLFramebuffer* fb)
 {
-  GLenum clear_flags = 0;
   GLenum invalidate_attachments[2];
   GLuint num_invalidate_attachments = 0;
 
@@ -533,8 +526,7 @@ void OpenGLDevice::CommitClear(OpenGLFramebuffer* fb)
       case GPUTexture::State::Cleared:
       {
         const auto color = FB->GetUNormClearColor();
-        glClearColor(color[0], color[1], color[2], color[3]);
-        clear_flags |= GL_COLOR_BUFFER_BIT;
+        glClearBufferfv(GL_COLOR, 0, color.data());
         FB->SetState(GPUTexture::State::Dirty);
       }
 
@@ -559,11 +551,8 @@ void OpenGLDevice::CommitClear(OpenGLFramebuffer* fb)
 
       case GPUTexture::State::Cleared:
       {
-        if (glClearDepthf)
-          glClearDepthf(DS->GetClearDepth());
-        else
-          glClearDepth(DS->GetClearDepth());
-        clear_flags |= GL_DEPTH_BUFFER_BIT;
+        const float depth = DS->GetClearDepth();
+        glClearBufferfv(GL_DEPTH, 0, &depth);
         DS->SetState(GPUTexture::State::Dirty);
       }
       break;
@@ -577,12 +566,6 @@ void OpenGLDevice::CommitClear(OpenGLFramebuffer* fb)
     }
   }
 
-  if (clear_flags != 0)
-  {
-    glDisable(GL_SCISSOR_TEST);
-    glClear(clear_flags);
-    glEnable(GL_SCISSOR_TEST);
-  }
   if (num_invalidate_attachments > 0 && glInvalidateFramebuffer)
     glInvalidateFramebuffer(GL_DRAW_FRAMEBUFFER, num_invalidate_attachments, invalidate_attachments);
 }