Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 56 additions & 3 deletions jme3-core/src/main/java/com/jme3/renderer/RenderContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,20 @@ private static <T> WeakReference<T>[] newWeakReferenceArray(int size) {
public Shader boundShader;

/**
* ID of the bound FrameBuffer.
* ID of the bound FrameBuffer. This value is untrusted while
* {@link #isFrameBufferBindingValid()} is false.
*
* @see Renderer#setFrameBuffer(com.jme3.texture.FrameBuffer)
*/
public int boundFBO;

private boolean frameBufferBindingValid = true;

/**
* Currently bound FrameBuffer.
* Java object associated with the known framebuffer binding, or null when
* there is no associated object or the binding is unknown. A null value
* does not imply a native framebuffer name of zero; presentation and
* externally managed framebuffers can have nonzero names.
*
* @see Renderer#setFrameBuffer(com.jme3.texture.FrameBuffer)
*/
Expand Down Expand Up @@ -289,7 +295,7 @@ private static <T> WeakReference<T>[] newWeakReferenceArray(int size) {
public final IDList textureIndexList = new IDList();

/**
* Currently bound texture unit.
* Currently bound texture unit, or -1 when the active unit is unknown.
*
* @see Renderer#setTexture(int, com.jme3.texture.Texture)
*/
Expand Down Expand Up @@ -374,6 +380,52 @@ public RenderContext() {
init();
}

/**
* Tests whether the cached framebuffer binding can suppress a GL bind.
*
* @return true if the binding is known
*/
public boolean isFrameBufferBindingValid() {
return frameBufferBindingValid;
}

/**
* Records the object and native name of a known framebuffer binding.
* Call after establishing native state or restoring a known binding.
*
* @param fb the associated Java framebuffer, or null if there is none
* @param fbo the known native framebuffer name
*/
public void setFrameBufferBinding(FrameBuffer fb, int fbo) {
boundFB = fb;
boundFBO = fbo;
frameBufferBindingValid = true;
}

/**
* Invalidates only the framebuffer binding, leaving texture bindings alone.
* The numeric name is retained as an untrusted value, and the associated
* Java object is cleared. This does not modify native state.
*/
public void invalidateFrameBufferBinding() {
frameBufferBindingValid = false;
boundFB = null;
}

/**
* Invalidates texture and framebuffer bindings after external GL changes.
* This does not modify native state. Unlike reset(), it does not assume
* that texture unit zero or the default framebuffer is bound.
*/
public void invalidateBindings() {
boundTextureUnit = -1;
invalidateFrameBufferBinding();
for (int i = 0; i < boundTextures.length; i++) {
boundTextures[i] = null;
}
textureIndexList.reset();
}


private void init() {
cullMode = RenderState.FaceCullMode.Off;
Expand All @@ -399,6 +451,7 @@ private void init() {
boundShader = null;
boundFBO = 0;
boundFB = null;
frameBufferBindingValid = true;
boundRB = 0;

boundElementArrayVBO = 0;
Expand Down
54 changes: 38 additions & 16 deletions jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,7 @@ public void initialize() {
@Override
public void invalidateState() {
context.reset();
context.invalidateBindings();
if (gl2 != null) {
context.initialDrawBuf = getInteger(GL2.GL_DRAW_BUFFER);
context.initialReadBuf = getInteger(GL2.GL_READ_BUFFER);
Expand Down Expand Up @@ -2004,6 +2005,13 @@ public void copyFrameBuffer(FrameBuffer src, FrameBuffer dst, boolean copyColor,

int prevFBO = context.boundFBO;
FrameBuffer prevFB = context.boundFB;
boolean prevBindingValid = context.isFrameBufferBindingValid();
// Attachment updates below can bind another framebuffer. An unknown
// binding may also have different native read and draw targets.
final int prevReadFramebuffer = prevBindingValid ? prevFBO
: getInteger(GLFbo.GL_READ_FRAMEBUFFER_BINDING_EXT);
final int prevDrawFramebuffer = prevBindingValid ? prevFBO
: getInteger(GLFbo.GL_DRAW_FRAMEBUFFER_BINDING_EXT);

if (mainFbOverride != null) {
if (src == null) {
Expand Down Expand Up @@ -2062,10 +2070,18 @@ public void copyFrameBuffer(FrameBuffer src, FrameBuffer dst, boolean copyColor,
GL.GL_NEAREST);


glfbo.glBindFramebufferEXT(GLFbo.GL_FRAMEBUFFER_EXT, prevFBO);
context.boundFBO = prevFBO;
context.boundFB = prevFB;
toggleFramebufferSrgb(prevFB);
if (prevBindingValid) {
glfbo.glBindFramebufferEXT(GLFbo.GL_FRAMEBUFFER_EXT, prevFBO);
context.setFrameBufferBinding(prevFB, prevFBO);
if (prevFB != null || prevFBO == defaultFBO) {
toggleFramebufferSrgb(prevFB);
}
} else {
glfbo.glBindFramebufferEXT(GLFbo.GL_READ_FRAMEBUFFER_EXT, prevReadFramebuffer);
glfbo.glBindFramebufferEXT(GLFbo.GL_DRAW_FRAMEBUFFER_EXT, prevDrawFramebuffer);
context.invalidateFrameBufferBinding();
context.boundFBO = prevFBO;
}
} else {
throw new RendererException("Framebuffer blitting not supported by the video hardware");
}
Expand Down Expand Up @@ -2220,18 +2236,16 @@ public void updateFrameBufferAttachment(FrameBuffer fb, RenderBuffer rb) {

private void bindFrameBuffer(FrameBuffer fb) {
if (fb == null) {
if (context.boundFBO != defaultFBO) {
if (!context.isFrameBufferBindingValid() || context.boundFBO != defaultFBO) {
glfbo.glBindFramebufferEXT(GLFbo.GL_FRAMEBUFFER_EXT, defaultFBO);
statistics.onFrameBufferUse(null, true);
context.boundFBO = defaultFBO;
context.boundFB = null;
context.setFrameBufferBinding(null, defaultFBO);
}
} else {
assert fb.getId() != -1 && fb.getId() != 0;
if (context.boundFBO != fb.getId()) {
if (!context.isFrameBufferBindingValid() || context.boundFBO != fb.getId()) {
glfbo.glBindFramebufferEXT(GLFbo.GL_FRAMEBUFFER_EXT, fb.getId());
context.boundFBO = fb.getId();
context.boundFB = fb;
context.setFrameBufferBinding(fb, fb.getId());
statistics.onFrameBufferUse(fb, true);
} else {
statistics.onFrameBufferUse(fb, false);
Expand Down Expand Up @@ -2311,7 +2325,7 @@ public Vector2f[] getFrameBufferSamplePositions(FrameBuffer fb) {
@Override
public void setMainFrameBufferOverride(FrameBuffer fb) {
mainFbOverride = null;
if (context.boundFBO == 0) {
if (context.isFrameBufferBindingValid() && context.boundFBO == 0) {
// Main FB is now set to fb, make sure its bound
setFrameBuffer(fb);
}
Expand Down Expand Up @@ -2385,7 +2399,14 @@ public void setFrameBuffer(FrameBuffer fb) {
fb = mainFbOverride;
}

if (context.boundFB == fb) {
if (fb == null && !caps.contains(Caps.FrameBuffer)) {
// Selecting the screen does not require framebuffer-object support.
toggleFramebufferSrgb(null);
return;
}

if (context.isFrameBufferBindingValid() && context.boundFB == fb
&& context.boundFBO == (fb == null ? defaultFBO : fb.getId())) {
if (fb == null || !fb.isUpdateNeeded()) {
toggleFramebufferSrgb(fb);
return;
Expand Down Expand Up @@ -2443,7 +2464,7 @@ && isMipmapGenerationSupported(tex.getImage().getFormat(),
assert fb.getId() > 0;
assert context.boundFBO == fb.getId();

context.boundFB = fb;
context.setFrameBufferBinding(fb, fb.getId());
if (debug && caps.contains(Caps.GLDebug)) {
if (fb.getName() != null) glext.glObjectLabel(GL3.GL_FRAMEBUFFER, fb.getId(), fb.getName());
}
Expand Down Expand Up @@ -2488,9 +2509,9 @@ private void deleteRenderBuffer(FrameBuffer fb, RenderBuffer rb) {
@Override
public void deleteFrameBuffer(FrameBuffer fb) {
if (fb.getId() != -1) {
if (context.boundFBO == fb.getId()) {
if (context.isFrameBufferBindingValid() && context.boundFBO == fb.getId()) {
glfbo.glBindFramebufferEXT(GLFbo.GL_FRAMEBUFFER_EXT, 0);
context.boundFBO = 0;
context.setFrameBufferBinding(null, 0);
}

if (fb.getDepthTarget() != null) {
Expand Down Expand Up @@ -3838,7 +3859,8 @@ public void setMainFrameBufferSrgb(boolean enableSrgb) {
}

mainFrameBufferSrgb = enableSrgb;
if (context.boundFB == null) {
if (context.isFrameBufferBindingValid() && context.boundFB == null
&& context.boundFBO == defaultFBO) {
toggleFramebufferSrgb(null);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright (c) 2009-2026 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme3.renderer;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.jme3.texture.FrameBuffer;
import com.jme3.texture.Image;
import org.junit.jupiter.api.Test;

/**
* Verifies the distinction between fresh defaults and externally changed bindings.
*/
public class RenderContextBindingTest {

@Test
public void bindingInvalidationClearsTextureReferencesWithoutAssumingNativeDefaults() {
RenderContext context = new RenderContext();
Image image = new Image();
context.boundTextures[0] = image.getWeakRef();
context.boundTextures[3] = image.getWeakRef();
context.textureIndexList.moveToNew(3);
context.textureIndexList.copyNewToOld();
context.boundTextureUnit = 3;
context.boundFBO = 12;
context.boundFB = new FrameBuffer(4, 4, 1);

context.invalidateBindings();

assertEquals(-1, context.boundTextureUnit);
assertEquals(12, context.boundFBO);
assertNull(context.boundFB);
assertFalse(context.isFrameBufferBindingValid());
for (int unit = 0; unit < context.boundTextures.length; unit++) {
assertNull(context.boundTextures[unit]);
}
assertEquals(0, context.textureIndexList.oldLen);
assertEquals(0, context.textureIndexList.newLen);
}

@Test
public void resetReturnsBindingsToKnownFreshDefaults() {
RenderContext context = new RenderContext();
context.boundFBO = 12;
context.boundTextureUnit = 3;
context.invalidateBindings();
context.reset();
assertDefaults(context);
}

@Test
public void framebufferOnlyInvalidationPreservesTextureKnowledgeAndUntrustedName() {
RenderContext context = new RenderContext();
FrameBuffer framebuffer = new FrameBuffer(4, 4, 1);
context.setFrameBufferBinding(framebuffer, 12);
Image image = new Image();
context.boundTextures[3] = image.getWeakRef();
context.boundTextureUnit = 3;
context.textureIndexList.moveToNew(3);
context.textureIndexList.copyNewToOld();
context.textureIndexList.moveToNew(0);

context.invalidateFrameBufferBinding();

assertNull(context.boundFB);
assertFalse(context.isFrameBufferBindingValid());
assertEquals(12, context.boundFBO);
assertEquals(3, context.boundTextureUnit);
assertSame(image.getWeakRef(), context.boundTextures[3]);
assertEquals(1, context.textureIndexList.oldLen);
assertEquals(1, context.textureIndexList.newLen);
}

private static void assertDefaults(RenderContext context) {
assertEquals(0, context.boundTextureUnit);
assertEquals(0, context.boundFBO);
assertNull(context.boundFB);
assertTrue(context.isFrameBufferBindingValid());
}
}
Loading