Skip to content
Merged
13 changes: 10 additions & 3 deletions jme3-core/src/main/java/com/jme3/renderer/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,16 @@ public interface Renderer {
* the GL state was changed manually or through an external library.
* <p>
* The OpenGL renderer also immediately restores the states managed by
* {@link RenderState} to OpenGL defaults. Call this method on the rendering
* thread with the renderer's graphics context current, before resuming
* rendering.
* {@link RenderState} to OpenGL defaults, selects texture unit zero, and
* binds the platform's default framebuffer for reading and drawing with
* its configured sRGB write state. Previous external framebuffer bindings
* are not preserved. Texture bindings are forgotten and rebound on use.
* A main-framebuffer override remains configured, but is not selected or
* allocated by this reset; select the intended target with
* {@link #setFrameBuffer(FrameBuffer)} before resuming rendering.
* <p>
* Call this method on the rendering thread with the renderer's graphics
* context current.
*/
public void invalidateState();

Expand Down
36 changes: 28 additions & 8 deletions jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -1011,8 +1011,25 @@ public void invalidateState() {
gl2.glPolygonMode(GL.GL_FRONT_AND_BACK, GL2.GL_FILL);
}

// Establish native binding defaults without trusting the old cache.
gl.glActiveTexture(GL.GL_TEXTURE0);
if (caps.contains(Caps.FrameBuffer)) {
// Reset both read and draw bindings, including nonzero platform defaults.
// Do not select mainFbOverride here: cleanup/resetGLObjects also call
// invalidation, and must not allocate or recreate its resources.
glfbo.glBindFramebufferEXT(GLFbo.GL_FRAMEBUFFER_EXT, defaultFBO);
}
if (caps.contains(Caps.SrgbWriteControl) && caps.contains(Caps.Srgb)) {
if (mainFrameBufferSrgb) {
gl.glEnable(GLExt.GL_FRAMEBUFFER_SRGB_EXT);
} else {
gl.glDisable(GLExt.GL_FRAMEBUFFER_SRGB_EXT);
}
}

context.reset();
context.boundFBO = -1;
context.boundFBO = defaultFBO;
context.srgbWriteEnabled = mainFrameBufferSrgb;
Arrays.fill(vertexAttribDivisorsInvalid, true);
if (caps.contains(Caps.OpenGL20)) {
context.initialDrawBuf = getInteger(GL2.GL_DRAW_BUFFER);
Expand Down Expand Up @@ -2130,7 +2147,7 @@ public void copyFrameBuffer(FrameBuffer src, FrameBuffer dst, boolean copyColor,
int dstX1;
int dstY1;

final int previousFramebuffer = context.boundFBO < 0 ? defaultFBO : context.boundFBO;
final int previousFramebuffer = context.boundFBO;
FrameBuffer prevFB = context.boundFB;

if (mainFbOverride != null) {
Expand Down Expand Up @@ -2364,11 +2381,11 @@ public void updateFrameBufferAttachment(FrameBuffer fb, RenderBuffer rb) {
if (fb == null || rb == null || !isFrameBufferTarget(fb, rb)) {
throw new IllegalArgumentException("The target must belong to this framebuffer.");
}
if (!caps.contains(Caps.FrameBuffer)) {
throw new RendererException("Framebuffer objects are not supported by the video hardware");
}
int previousFbo = context.boundFBO;
FrameBuffer previousFb = context.boundFB;
// Invalidation forgets the binding without changing native GL state.
int restoreFbo = previousFbo < 0 && caps.contains(Caps.FrameBuffer)
? getInteger(GL.GL_FRAMEBUFFER_BINDING) : previousFbo;
try {
FrameBufferState state = prepareFrameBuffer(fb);
final boolean wasDirty = state.attachmentsDirty;
Expand All @@ -2382,7 +2399,7 @@ public void updateFrameBufferAttachment(FrameBuffer fb, RenderBuffer rb) {
state.attachmentsDirty = wasDirty;
} finally {
if (context.boundFBO != previousFbo) {
glfbo.glBindFramebufferEXT(GLFbo.GL_FRAMEBUFFER_EXT, restoreFbo);
glfbo.glBindFramebufferEXT(GLFbo.GL_FRAMEBUFFER_EXT, previousFbo);
context.boundFBO = previousFbo;
context.boundFB = previousFb;
}
Expand Down Expand Up @@ -2579,8 +2596,11 @@ public Vector2f[] getFrameBufferSamplePositions(FrameBuffer fb) {

@Override
public void setMainFrameBufferOverride(FrameBuffer fb) {
if (fb != null && !caps.contains(Caps.FrameBuffer)) {
throw new RendererException("Framebuffer objects are not supported by the video hardware");
}
mainFbOverride = null;
if (context.boundFB == null) {
if (context.boundFB == null && context.boundFBO == defaultFBO) {
// Main FB is now set to fb, make sure its bound
setFrameBuffer(fb);
}
Expand Down Expand Up @@ -4171,7 +4191,7 @@ public void setMainFrameBufferSrgb(boolean enableSrgb) {
}

mainFrameBufferSrgb = enableSrgb;
if (context.boundFB == null) {
if (context.boundFB == null && context.boundFBO == defaultFBO) {
toggleFramebufferSrgb(null);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,16 +232,18 @@ void lowLevelUpdateRestoresNonzeroPresentationFramebuffer() {
}

@Test
void lowLevelUpdateAfterInvalidationRestoresNativeBindingAndUnknownCache() {
void lowLevelUpdateAfterInvalidationRestoresDefaultBinding() {
FrameBuffer previous = framebuffer(texture());
final FrameBuffer other = framebuffer(texture());
renderer.setFrameBuffer(previous);
renderer.invalidateState();
assertEquals(0, driver.drawFramebuffer, "Invalidation must immediately select the screen");
assertEquals(0, driver.readFramebuffer);
renderer.updateRenderTexture(other, other.getColorTarget(0));
assertEquals(previous.getId(), driver.drawFramebuffer);
assertEquals(previous.getId(), driver.readFramebuffer);
assertEquals(0, driver.drawFramebuffer);
assertEquals(0, driver.readFramebuffer);
renderer.setFrameBuffer(null);
assertEquals(0, driver.drawFramebuffer, "The invalid cache must still force a real screen bind");
assertEquals(0, driver.drawFramebuffer);
renderer.cleanup();
}
}
Loading
Loading