Skip to content

Commit

Permalink
Added support for hardware srgb output (gamma correction).
Browse files Browse the repository at this point in the history
It can be toggled at render time.
Works with lwjgl
Couldn't test with Jogl and might be not working
Not supported by mobile renderers
  • Loading branch information
Nehon committed May 26, 2014
1 parent a230384 commit 77a4002
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 10 deletions.
Expand Up @@ -2532,4 +2532,8 @@ public void invalidateState() {
boundShader = null;
lastFb = null;
}

public void setMainFrameBufferSrgb(boolean srgb) {
//TODO once opglES3.0 is supported maybe....
}
}
22 changes: 22 additions & 0 deletions jme3-core/src/main/java/com/jme3/renderer/Renderer.java
Expand Up @@ -313,4 +313,26 @@ public interface Renderer {
* </p>
*/
public void setAlphaToCoverage(boolean value);

/**
* If enabled, color values rendered to the main framebuffer undergo
* linear -> sRGB conversion.
*
* This is identical to {@link FrameBuffer#setSrgb(boolean)} except it is toggled
* for the main framebuffer instead of an offscreen buffer.
*
* This should be set together with {@link Renderer#setLinearizeSrgbImages(boolean)}
*
* As a shorthand, the user can set {@link AppSettings#setSrgbPipeline(boolean)} to true
* to toggle both {@link Renderer#setLinearizeSrgbImages(boolean)} and
* {@link Renderer#setMainFrameBufferSrgb(boolean)} if the
* {@link Caps#} is supported by the GPU.
*
* @throws RendererException If the GPU hardware does not support sRGB.
*
* @seealso FrameBuffer#setSrgb(boolean)
*
* @seealso Caps#Srgb
*/
public void setMainFrameBufferSrgb(boolean srgb);
}
3 changes: 3 additions & 0 deletions jme3-core/src/main/java/com/jme3/system/NullRenderer.java
Expand Up @@ -152,4 +152,7 @@ public void deleteImage(Image image) {
public void setAlphaToCoverage(boolean value) {
}

public void setMainFrameBufferSrgb(boolean srgb) {
}

}
Expand Up @@ -2572,4 +2572,8 @@ private int convertTestFunction(RenderState.TestFunction testFunc) {
throw new UnsupportedOperationException("Unrecognized test function: " + testFunc);
}
}

public void setMainFrameBufferSrgb(boolean srgb) {

}
}
Expand Up @@ -1258,4 +1258,7 @@ public void updateBufferData(VertexBuffer vb) {

public void deleteBuffer(VertexBuffer vb) {
}

public void setMainFrameBufferSrgb(boolean srgb) {
}
}
11 changes: 11 additions & 0 deletions jme3-jogl/src/main/java/com/jme3/renderer/jogl/JoglRenderer.java
Expand Up @@ -2589,4 +2589,15 @@ public void renderMesh(Mesh mesh, int lod, int count) {
renderMeshDefault(mesh, lod, count);
// }
}

public void setMainFrameBufferSrgb(boolean srgb) {
//Gamma correction
if(srgb && GLContext.getCurrent().isExtensionAvailable("GL_ARB_framebuffer_sRGB")){
GLContext.getCurrentGL().glEnable(GL3.GL_FRAMEBUFFER_SRGB);
logger.log(Level.FINER, "SRGB FrameBuffer enabled (Gamma Correction)");
}else{
GLContext.getCurrentGL().glDisable(GL3.GL_FRAMEBUFFER_SRGB);
}

}
}
Expand Up @@ -164,6 +164,8 @@ public boolean run(GLAutoDrawable glad) {
}

renderer = new JoglRenderer();

renderer.setMainFrameBufferSrgb(settings.getGammaCorrection());
}

protected void startGLCanvas() {
Expand Down
Expand Up @@ -144,6 +144,8 @@ public boolean run(GLAutoDrawable glad) {
}

renderer = new JoglRenderer();

renderer.setMainFrameBufferSrgb(settings.getGammaCorrection());
}

protected void startGLCanvas() {
Expand Down
Expand Up @@ -1198,4 +1198,8 @@ public void updateBufferData(VertexBuffer vb) {

public void deleteBuffer(VertexBuffer vb) {
}

public void setMainFrameBufferSrgb(boolean srgb) {

}
}
Expand Up @@ -55,9 +55,7 @@
import com.jme3.texture.Texture.WrapAxis;
import com.jme3.util.BufferUtils;
import com.jme3.util.ListMap;
import com.jme3.util.NativeObject;
import com.jme3.util.NativeObjectManager;
import com.jme3.util.SafeArrayList;
import java.nio.*;
import java.util.EnumSet;
import java.util.List;
Expand Down Expand Up @@ -371,7 +369,7 @@ public void initialize() {
caps.add(Caps.FrameBufferMRT);
logger.log(Level.FINER, "FBO Max MRT renderbuffers: {0}", maxMRTFBOAttachs);
}

// if (ctxCaps.GL_ARB_draw_buffers) {
// caps.add(Caps.FrameBufferMRT);
// glGetInteger(ARBDrawBuffers.GL_MAX_DRAW_BUFFERS_ARB, intBuf16);
Expand Down Expand Up @@ -2488,4 +2486,14 @@ public void renderMesh(Mesh mesh, int lod, int count) {
renderMeshDefault(mesh, lod, count);
// }
}

public void setMainFrameBufferSrgb(boolean srgb) {
//Gamma correction
if(srgb && GLContext.getCapabilities().GL_ARB_framebuffer_sRGB){
glEnable(GL30.GL_FRAMEBUFFER_SRGB);
logger.log(Level.FINER, "SRGB FrameBuffer enabled (Gamma Correction)");
}else{
glDisable(GL30.GL_FRAMEBUFFER_SRGB);
}
}
}
Expand Up @@ -308,7 +308,8 @@ protected PixelFormat acquirePixelFormat(boolean forPbuffer){
0,
0,
0,
settings.useStereo3D());
settings.useStereo3D())
.withSRGB(settings.getGammaCorrection());
}
return pbufferFormat;
}else{
Expand All @@ -322,7 +323,8 @@ protected PixelFormat acquirePixelFormat(boolean forPbuffer){
0,
0,
0,
settings.useStereo3D());
settings.useStereo3D())
.withSRGB(settings.getGammaCorrection());
}
return canvasFormat;
}
Expand Down Expand Up @@ -467,7 +469,7 @@ protected void createContext(AppSettings settings) {
}else{
Display.create(acquirePixelFormat(false));
}

renderer.invalidateState();
}else{
// First create the pbuffer, if it is needed.
Expand Down
Expand Up @@ -237,6 +237,7 @@ protected void initContextFirstTime(){
}else{
assert false;
}
renderer.setMainFrameBufferSrgb(settings.getGammaCorrection());

// Init input
if (keyInput != null) {
Expand Down
Expand Up @@ -91,8 +91,9 @@ protected void createContext(AppSettings settings) throws LWJGLException{
0,
0,
0,
settings.useStereo3D());

settings.useStereo3D())
.withSRGB(settings.getGammaCorrection());

frameRate = settings.getFrameRate();
logger.log(Level.FINE, "Selected display mode: {0}", displayMode);

Expand Down
Expand Up @@ -65,8 +65,9 @@ protected void initInThread(){
0,
settings.getDepthBits(),
settings.getStencilBits(),
samples);

samples)
.withSRGB(settings.getGammaCorrection());

width = settings.getWidth();
height = settings.getHeight();
try{
Expand Down

2 comments on commit 77a4002

@shadowislord
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way I don't think the withSRGB(settings.getGammaCorrection()) portion is needed at all, for me (Linux x64, Intel HD 4000) it crashes (see below). If I remove that part, gamma correction still works as intended.

SEVERE: Failed to create display
org.lwjgl.LWJGLException: sRGB specified but there's no support for GLX_ARB_framebuffer_sRGB
at org.lwjgl.opengl.LinuxDisplayPeerInfo.initDefaultPeerInfo(Native Method)
at org.lwjgl.opengl.LinuxDisplayPeerInfo.(LinuxDisplayPeerInfo.java:61)
at org.lwjgl.opengl.LinuxDisplay.createPeerInfo(LinuxDisplay.java:788)
at org.lwjgl.opengl.DrawableGL.setPixelFormat(DrawableGL.java:61)
at org.lwjgl.opengl.Display.create(Display.java:846)
at org.lwjgl.opengl.Display.create(Display.java:757)
at com.jme3.system.lwjgl.LwjglDisplay.createContext(LwjglDisplay.java:141)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:113)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:207)
at java.lang.Thread.run(Thread.java:745)

@Nehon
Copy link
Contributor Author

@Nehon Nehon commented on 77a4002 May 31, 2014 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.