Skip to content

Commit

Permalink
OpenGLDevice: Use glClearBuffer() instead of glClear()
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Nov 24, 2023
1 parent 603531f commit e75c1a3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 30 deletions.
11 changes: 4 additions & 7 deletions src/util/opengl_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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..
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down
29 changes: 6 additions & 23 deletions src/util/opengl_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;

Expand All @@ -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);
}

Expand All @@ -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;
Expand All @@ -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);
}
Expand Down

0 comments on commit e75c1a3

Please sign in to comment.