Skip to content
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
7 changes: 7 additions & 0 deletions jme3-core/src/main/java/com/jme3/renderer/RenderContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ public class RenderContext {
*/
public FrameBuffer boundFB;

/**
* Convert writes to srgb target from linear space to srgb
*/
public boolean srgbWriteEnabled;
Comment thread
riccardobl marked this conversation as resolved.

/**
* Currently bound Renderbuffer.
*
Expand Down Expand Up @@ -412,6 +417,8 @@ private void init() {
alphaFunc = RenderState.TestFunction.Greater;
cullMode = RenderState.FaceCullMode.Off;

srgbWriteEnabled = false;

clearColor.set(0, 0, 0, 0);
}

Expand Down
38 changes: 29 additions & 9 deletions jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public final class GLRenderer implements Renderer {
private int clipX, clipY, clipW, clipH;
private int defaultAnisotropicFilter = 1;
private boolean linearizeSrgbImages;
private boolean mainFrameBufferSrgb;
private HashSet<String> extensions;
private boolean generateMipmapsForFramebuffers = true;

Expand Down Expand Up @@ -1829,6 +1830,7 @@ public void copyFrameBuffer(FrameBuffer src, FrameBuffer dst, boolean copyColor,
int dstY1;

int prevFBO = context.boundFBO;
FrameBuffer prevFB = context.boundFB;

if (mainFbOverride != null) {
if (src == null) {
Expand Down Expand Up @@ -1870,6 +1872,8 @@ public void copyFrameBuffer(FrameBuffer src, FrameBuffer dst, boolean copyColor,
dstY1 = dst.getHeight();
}

toggleFramebufferSrgb(dst);

int mask = 0;

if(copyColor){
Expand All @@ -1886,6 +1890,9 @@ public void copyFrameBuffer(FrameBuffer src, FrameBuffer dst, boolean copyColor,


glfbo.glBindFramebufferEXT(GLFbo.GL_FRAMEBUFFER_EXT, prevFBO);
context.boundFBO = prevFBO;
context.boundFB = prevFB;
Comment thread
riccardobl marked this conversation as resolved.
toggleFramebufferSrgb(prevFB);
} else {
throw new RendererException("Framebuffer blitting not supported by the video hardware");
}
Expand Down Expand Up @@ -2046,6 +2053,21 @@ private void bindFrameBuffer(FrameBuffer fb) {
}
}

private void toggleFramebufferSrgb(FrameBuffer fb) {
boolean isSrgb = fb == null ? mainFrameBufferSrgb : fb.isSrgb();

if (isSrgb != context.srgbWriteEnabled) {
if (caps.contains(Caps.Srgb)) {
Comment thread
riccardobl marked this conversation as resolved.
if (isSrgb) {
gl.glEnable(GLExt.GL_FRAMEBUFFER_SRGB_EXT);
} else {
gl.glDisable(GLExt.GL_FRAMEBUFFER_SRGB_EXT);
}
context.srgbWriteEnabled = isSrgb;
}
}
}
Comment thread
riccardobl marked this conversation as resolved.
Comment thread
riccardobl marked this conversation as resolved.

public void updateFrameBuffer(FrameBuffer fb) {
if (fb.getNumColorBuffers() == 0 && fb.getDepthBuffer() == null) {
throw new IllegalArgumentException("The framebuffer: " + fb
Expand Down Expand Up @@ -2179,6 +2201,7 @@ public void setFrameBuffer(FrameBuffer fb) {

if (context.boundFB == fb) {
if (fb == null || !fb.isUpdateNeeded()) {
toggleFramebufferSrgb(fb);
return;
}
}
Expand Down Expand Up @@ -2230,6 +2253,7 @@ public void setFrameBuffer(FrameBuffer fb) {
if (fb.getName() != null) glext.glObjectLabel(GL3.GL_FRAMEBUFFER, fb.getId(), fb.getName());
}
}
toggleFramebufferSrgb(fb);
}

@Override
Expand Down Expand Up @@ -3487,17 +3511,13 @@ public void setMainFrameBufferSrgb(boolean enableSrgb) {
logger.warning("sRGB framebuffer is not supported " +
"by video hardware, but was requested.");

mainFrameBufferSrgb = false;
return;
}

setFrameBuffer(null);

if (enableSrgb) {
gl.glEnable(GLExt.GL_FRAMEBUFFER_SRGB_EXT);
logger.log(Level.FINER, "sRGB FrameBuffer enabled (Gamma Correction)");
} else {
gl.glDisable(GLExt.GL_FRAMEBUFFER_SRGB_EXT);
logger.log(Level.FINER, "sRGB FrameBuffer disabled (Gamma Correction)");
mainFrameBufferSrgb = enableSrgb;
if (context.boundFB == null) {
toggleFramebufferSrgb(null);
}
}

Expand Down Expand Up @@ -3595,7 +3615,7 @@ public boolean isMainFrameBufferSrgb() {
if (!caps.contains(Caps.Srgb)) {
return false;
} else {
return gl.glIsEnabled(GLExt.GL_FRAMEBUFFER_SRGB_EXT);
return mainFrameBufferSrgb;
}
}

Expand Down