Skip to content

Fix draw/read buffer selection for framebuffers (FrameBuffer.setTargetIndex(int)) #1786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2022
Merged
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
26 changes: 2 additions & 24 deletions jme3-core/src/main/java/com/jme3/renderer/RenderContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,28 +216,7 @@ public class RenderContext {
*/
public int boundRB;

/**
* Currently bound draw buffer.
* -2 = GL_NONE
* -1 = GL_BACK
* 0 = GL_COLOR_ATTACHMENT0
* n = GL_COLOR_ATTACHMENTn
* where n is an integer greater than 1
*
* @see Renderer#setFrameBuffer(com.jme3.texture.FrameBuffer)
* @see FrameBuffer#setTargetIndex(int)
*/
public int boundDrawBuf;

/**
* Currently bound read buffer.
*
* @see RenderContext#boundDrawBuf
* @see Renderer#setFrameBuffer(com.jme3.texture.FrameBuffer)
* @see FrameBuffer#setTargetIndex(int)
*/
public int boundReadBuf;


/**
* Currently bound element array vertex buffer.
*
Expand Down Expand Up @@ -396,8 +375,7 @@ private void init() {
boundFBO = 0;
boundFB = null;
boundRB = 0;
boundDrawBuf = -1;
boundReadBuf = -1;

boundElementArrayVBO = 0;
boundVertexArray = 0;
boundArrayVBO = 0;
Expand Down
43 changes: 9 additions & 34 deletions jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -1996,28 +1996,13 @@ public void setReadDrawBuffers(FrameBuffer fb) {
final int INITIAL = -1;
final int MRT_OFF = 100;

if (fb == null) {
// Set Read/Draw buffers to initial value.
if (context.boundDrawBuf != INITIAL) {
gl2.glDrawBuffer(context.initialDrawBuf);
context.boundDrawBuf = INITIAL;
}
if (context.boundReadBuf != INITIAL) {
gl2.glReadBuffer(context.initialReadBuf);
context.boundReadBuf = INITIAL;
}
} else {
if (fb != null) {

if (fb.getNumColorBuffers() == 0) {
// make sure to select NONE as draw buf
// no color buffer attached.
if (context.boundDrawBuf != NONE) {
gl2.glDrawBuffer(GL.GL_NONE);
context.boundDrawBuf = NONE;
}
if (context.boundReadBuf != NONE) {
gl2.glReadBuffer(GL.GL_NONE);
context.boundReadBuf = NONE;
}
// no color buffer attached.
gl2.glDrawBuffer(GL.GL_NONE);
gl2.glReadBuffer(GL.GL_NONE);
} else {
if (fb.getNumColorBuffers() > limits.get(Limits.FrameBufferAttachments)) {
throw new RendererException("Framebuffer has more color "
Expand All @@ -2042,15 +2027,12 @@ public void setReadDrawBuffers(FrameBuffer fb) {

intBuf16.flip();
glext.glDrawBuffers(intBuf16);
context.boundDrawBuf = MRT_OFF + fb.getNumColorBuffers();

} else {
RenderBuffer rb = fb.getColorBuffer(fb.getTargetIndex());
// select this draw buffer
if (context.boundDrawBuf != rb.getSlot()) {
gl2.glDrawBuffer(GLFbo.GL_COLOR_ATTACHMENT0_EXT + rb.getSlot());
context.boundDrawBuf = rb.getSlot();
}
gl2.glDrawBuffer(GLFbo.GL_COLOR_ATTACHMENT0_EXT + rb.getSlot());
// select this read buffer
gl2.glReadBuffer(GLFbo.GL_COLOR_ATTACHMENT0_EXT + rb.getSlot());
}
}
}
Expand Down Expand Up @@ -2098,13 +2080,11 @@ public void setFrameBuffer(FrameBuffer fb) {

if (fb == null) {
bindFrameBuffer(null);
setReadDrawBuffers(null);
} else {
if (fb.isUpdateNeeded()) {
updateFrameBuffer(fb);
} else {
bindFrameBuffer(fb);
setReadDrawBuffers(fb);
}

// update viewport to reflect framebuffer's resolution
Expand All @@ -2131,12 +2111,7 @@ private void readFrameBufferWithGLFormat(FrameBuffer fb, ByteBuffer byteBuf, int
}

setFrameBuffer(fb);
if (gl2 != null) {
if (context.boundReadBuf != rb.getSlot()) {
gl2.glReadBuffer(GLFbo.GL_COLOR_ATTACHMENT0_EXT + rb.getSlot());
context.boundReadBuf = rb.getSlot();
}
}


} else {
setFrameBuffer(null);
Expand Down