Skip to content

Commit 5af1548

Browse files
authored
toggle srgb on per framebuffer basis (#2729)
1 parent ffbb13f commit 5af1548

2 files changed

Lines changed: 36 additions & 9 deletions

File tree

jme3-core/src/main/java/com/jme3/renderer/RenderContext.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ public class RenderContext {
215215
*/
216216
public FrameBuffer boundFB;
217217

218+
/**
219+
* Convert writes to srgb target from linear space to srgb
220+
*/
221+
public boolean srgbWriteEnabled;
222+
218223
/**
219224
* Currently bound Renderbuffer.
220225
*
@@ -412,6 +417,8 @@ private void init() {
412417
alphaFunc = RenderState.TestFunction.Greater;
413418
cullMode = RenderState.FaceCullMode.Off;
414419

420+
srgbWriteEnabled = false;
421+
415422
clearColor.set(0, 0, 0, 0);
416423
}
417424

jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public final class GLRenderer implements Renderer {
106106
private int clipX, clipY, clipW, clipH;
107107
private int defaultAnisotropicFilter = 1;
108108
private boolean linearizeSrgbImages;
109+
private boolean mainFrameBufferSrgb;
109110
private HashSet<String> extensions;
110111
private boolean generateMipmapsForFramebuffers = true;
111112

@@ -1916,6 +1917,7 @@ public void copyFrameBuffer(FrameBuffer src, FrameBuffer dst, boolean copyColor,
19161917
int dstY1;
19171918

19181919
int prevFBO = context.boundFBO;
1920+
FrameBuffer prevFB = context.boundFB;
19191921

19201922
if (mainFbOverride != null) {
19211923
if (src == null) {
@@ -1957,6 +1959,8 @@ public void copyFrameBuffer(FrameBuffer src, FrameBuffer dst, boolean copyColor,
19571959
dstY1 = dst.getHeight();
19581960
}
19591961

1962+
toggleFramebufferSrgb(dst);
1963+
19601964
int mask = 0;
19611965

19621966
if(copyColor){
@@ -1973,6 +1977,9 @@ public void copyFrameBuffer(FrameBuffer src, FrameBuffer dst, boolean copyColor,
19731977

19741978

19751979
glfbo.glBindFramebufferEXT(GLFbo.GL_FRAMEBUFFER_EXT, prevFBO);
1980+
context.boundFBO = prevFBO;
1981+
context.boundFB = prevFB;
1982+
toggleFramebufferSrgb(prevFB);
19761983
} else {
19771984
throw new RendererException("Framebuffer blitting not supported by the video hardware");
19781985
}
@@ -2133,6 +2140,21 @@ private void bindFrameBuffer(FrameBuffer fb) {
21332140
}
21342141
}
21352142

2143+
private void toggleFramebufferSrgb(FrameBuffer fb) {
2144+
boolean isSrgb = fb == null ? mainFrameBufferSrgb : fb.isSrgb();
2145+
2146+
if (isSrgb != context.srgbWriteEnabled) {
2147+
if (caps.contains(Caps.Srgb)) {
2148+
if (isSrgb) {
2149+
gl.glEnable(GLExt.GL_FRAMEBUFFER_SRGB_EXT);
2150+
} else {
2151+
gl.glDisable(GLExt.GL_FRAMEBUFFER_SRGB_EXT);
2152+
}
2153+
context.srgbWriteEnabled = isSrgb;
2154+
}
2155+
}
2156+
}
2157+
21362158
public void updateFrameBuffer(FrameBuffer fb) {
21372159
if (fb.getNumColorBuffers() == 0 && fb.getDepthBuffer() == null) {
21382160
throw new IllegalArgumentException("The framebuffer: " + fb
@@ -2266,6 +2288,7 @@ public void setFrameBuffer(FrameBuffer fb) {
22662288

22672289
if (context.boundFB == fb) {
22682290
if (fb == null || !fb.isUpdateNeeded()) {
2291+
toggleFramebufferSrgb(fb);
22692292
return;
22702293
}
22712294
}
@@ -2317,6 +2340,7 @@ public void setFrameBuffer(FrameBuffer fb) {
23172340
if (fb.getName() != null) glext.glObjectLabel(GL3.GL_FRAMEBUFFER, fb.getId(), fb.getName());
23182341
}
23192342
}
2343+
toggleFramebufferSrgb(fb);
23202344
}
23212345

23222346
@Override
@@ -3574,17 +3598,13 @@ public void setMainFrameBufferSrgb(boolean enableSrgb) {
35743598
logger.warning("sRGB framebuffer is not supported " +
35753599
"by video hardware, but was requested.");
35763600

3601+
mainFrameBufferSrgb = false;
35773602
return;
35783603
}
35793604

3580-
setFrameBuffer(null);
3581-
3582-
if (enableSrgb) {
3583-
gl.glEnable(GLExt.GL_FRAMEBUFFER_SRGB_EXT);
3584-
logger.log(Level.FINER, "sRGB FrameBuffer enabled (Gamma Correction)");
3585-
} else {
3586-
gl.glDisable(GLExt.GL_FRAMEBUFFER_SRGB_EXT);
3587-
logger.log(Level.FINER, "sRGB FrameBuffer disabled (Gamma Correction)");
3605+
mainFrameBufferSrgb = enableSrgb;
3606+
if (context.boundFB == null) {
3607+
toggleFramebufferSrgb(null);
35883608
}
35893609
}
35903610

@@ -3682,7 +3702,7 @@ public boolean isMainFrameBufferSrgb() {
36823702
if (!caps.contains(Caps.Srgb)) {
36833703
return false;
36843704
} else {
3685-
return gl.glIsEnabled(GLExt.GL_FRAMEBUFFER_SRGB_EXT);
3705+
return mainFrameBufferSrgb;
36863706
}
36873707
}
36883708

0 commit comments

Comments
 (0)