From 2cb9476d139699efc3519a2c4403a69bc11a565c Mon Sep 17 00:00:00 2001 From: Emanuel Rabina Date: Fri, 28 Jun 2024 19:09:49 +1200 Subject: [PATCH] Clear program used in clear method --- .../graphics/opengl/OpenGLRenderer.groovy | 21 +++++++++---------- .../graphics/opengl/OpenGLShader.groovy | 19 ++++++++++++++++- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/graphics/opengl/OpenGLRenderer.groovy b/redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/graphics/opengl/OpenGLRenderer.groovy index 3a051567..6275a286 100644 --- a/redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/graphics/opengl/OpenGLRenderer.groovy +++ b/redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/graphics/opengl/OpenGLRenderer.groovy @@ -55,7 +55,6 @@ import org.slf4j.Logger import org.slf4j.LoggerFactory import static org.lwjgl.opengl.GL11C.* import static org.lwjgl.opengl.GL20C.GL_MAX_FRAGMENT_UNIFORM_COMPONENTS -import static org.lwjgl.opengl.GL20C.glUseProgram import static org.lwjgl.opengl.GL30C.GL_FRAMEBUFFER import static org.lwjgl.opengl.GL30C.glBindFramebuffer import static org.lwjgl.opengl.GL31C.* @@ -169,6 +168,16 @@ class OpenGLRenderer implements GraphicsRenderer { @Override void clear() { + // For an error w/ nVidia on Windows, where the program state seems to + // linger when creating a shader with a different layout, eg: we last used + // the Sharp Upscaling shader which has all of colour, position, and + // texcoord attributes, then try to create the Primitives shader which has + // only colour and position. This then manifests as an error on the next + // frame when we call glClear() 🤔 + // "Program/shader state performance warning: Vertex shader in program X + // is being recompiled based on GL state" + OpenGLShader.useProgram(0) + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) } @@ -307,16 +316,6 @@ class OpenGLRenderer implements GraphicsRenderer { } trigger(new DrawEvent()) - - // For an error w/ nVidia on Windows, where the program state seems to - // linger when creating a shader with a different layout, eg: we last used - // the Sharp Upscaling shader which has all of colour, position, and - // texcoord attributes, then try to create the Primitives shader which has - // only colour and position. This then manifests as an error on the next - // frame when we call glClear() 🤔 - // "Program/shader state performance warning: Vertex shader in program X - // is being recompiled based on GL state" - glUseProgram(0) } } diff --git a/redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/graphics/opengl/OpenGLShader.groovy b/redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/graphics/opengl/OpenGLShader.groovy index a93d74f6..68d4fccd 100644 --- a/redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/graphics/opengl/OpenGLShader.groovy +++ b/redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/graphics/opengl/OpenGLShader.groovy @@ -29,6 +29,7 @@ import static org.lwjgl.opengl.GL20C.* import static org.lwjgl.system.MemoryStack.stackPush import groovy.transform.Memoized +import groovy.transform.PackageScope /** * OpenGL-specific shader implementation. @@ -38,6 +39,7 @@ import groovy.transform.Memoized class OpenGLShader extends Shader { private static final Logger logger = LoggerFactory.getLogger(OpenGLShader) + private static int lastProgramId = 0 final int programId @@ -158,6 +160,21 @@ class OpenGLShader extends Shader { @Override void use() { - glUseProgram(programId) + useProgram(programId) + } + + /** + * Set to use the OpenGL shader program with the given ID. + *

+ * This is only used by the {@link OpenGLRenderer} to reset the program used + * so that program state doesn't bleed into the next one. + */ + @PackageScope + static void useProgram(int programId) { + + if (programId != lastProgramId) { + glUseProgram(programId) + lastProgramId = programId + } } }