Skip to content

Commit

Permalink
Merge branch 'performance/program-switching' into performance/quadtree
Browse files Browse the repository at this point in the history
  • Loading branch information
ultraq committed Jun 28, 2024
2 parents db22c27 + 2cb9476 commit 25dcdf4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -306,16 +315,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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down Expand Up @@ -151,6 +153,21 @@ class OpenGLShader extends Shader {
@Override
void use() {

glUseProgram(programId)
useProgram(programId)
}

/**
* Set to use the OpenGL shader program with the given ID.
* <p>
* 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
}
}
}

0 comments on commit 25dcdf4

Please sign in to comment.