shithub: cstory

Download patch

ref: b1a706c31266a752fa2a3e84c11eb46e312fbed5
parent: ac273d2d7c2f7169851a2055740059220ad72171
author: Clownacy <Clownacy@users.noreply.github.com>
date: Fri Aug 9 11:55:42 EDT 2019

Call glFramebufferTexture2D as rarely as possible

Turns out performance is absolutely abysmal on my laptop's copy of
Windows 10 (AMD A9 APU).

This is only one of the weird bottleecks: glFramebufferTexture2D
is a CPU sinkhole, so don't call it often.

--- a/src/Backends/Rendering/OpenGL3.cpp
+++ b/src/Backends/Rendering/OpenGL3.cpp
@@ -205,6 +205,17 @@
 	return program_id;
 }
 
+static void SetFramebufferTarget(GLuint texture_id)
+{
+	static GLuint last_framebuffer_target;
+
+	if (texture_id != last_framebuffer_target)
+	{
+		last_framebuffer_target = texture_id;
+		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture_id, 0);
+	}
+}
+
 SDL_Window* Backend_CreateWindow(const char *title, int width, int height)
 {
 	SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
@@ -410,7 +421,7 @@
 		return;
 
 	// Point our framebuffer to the destination texture
-	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, destination_surface->texture_id, 0);
+	SetFramebufferTarget(destination_surface->texture_id);
 	glViewport(0, 0, destination_surface->width, destination_surface->height);
 
 	// Switch to colour-key shader if we have to
@@ -474,7 +485,7 @@
 		return;
 
 	// Point our framebuffer to the destination texture
-	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, surface->texture_id, 0);
+	SetFramebufferTarget(surface->texture_id);
 	glViewport(0, 0, surface->width, surface->height);
 
 	glUseProgram(program_colour_fill);
@@ -597,7 +608,7 @@
 		return;
 
 	// Point our framebuffer to the destination texture
-	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, surface->texture_id, 0);
+	SetFramebufferTarget(surface->texture_id);
 	glViewport(0, 0, surface->width, surface->height);
 
 	glEnable(GL_BLEND);