gamma correction fallback for -nosrgb
authorJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 8 Dec 2020 00:49:11 +0000 (02:49 +0200)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 8 Dec 2020 00:49:11 +0000 (02:49 +0200)
libs/glew/Makefile
sdr/gamma.p.glsl [new file with mode: 0644]
sdr/gamma.v.glsl [new file with mode: 0644]
src/demo.c

index 21e9e4b..353b351 100644 (file)
@@ -1,7 +1,7 @@
 obj = glew.o
 alib = libglew_static.a
 
-CFLAGS = -DGLEW_STATIC
+CFLAGS = -DGLEW_STATIC -DGLEW_INCLUDE=\"glew.h\"
 
 $(alib): $(obj)
        $(AR) rcs $@ $(obj)
diff --git a/sdr/gamma.p.glsl b/sdr/gamma.p.glsl
new file mode 100644 (file)
index 0000000..a37f189
--- /dev/null
@@ -0,0 +1,8 @@
+uniform sampler2D tex;
+
+void main()
+{
+       vec3 texel = texture2D(tex, gl_TexCoord[0].st).rgb;
+       vec3 col = pow(texel, vec3(1.0 / 2.2));
+       gl_FragColor = vec4(col, 1.0);
+}
diff --git a/sdr/gamma.v.glsl b/sdr/gamma.v.glsl
new file mode 100644 (file)
index 0000000..4c3c8ef
--- /dev/null
@@ -0,0 +1,5 @@
+void main()
+{
+       gl_Position = gl_Vertex;
+       gl_TexCoord[0] = gl_MultiTexCoord0 * vec4(1.0, -1.0, 1.0, 1.0);
+}
index d0b1127..97da536 100644 (file)
@@ -2,6 +2,9 @@
 #include "opengl.h"
 #include "demo.h"
 #include "part.h"
+#include "post.h"
+#include "sdr.h"
+#include "opt.h"
 
 void reg_whitted(void);
 
@@ -9,6 +12,9 @@ int win_width, win_height;
 float win_aspect;
 long time_msec;
 
+static int reshape_pending;
+static unsigned int fbtex;
+static unsigned int sdr_gamma;
 
 int demo_init(void)
 {
@@ -23,6 +29,18 @@ int demo_init(void)
        glEnable(GL_FRAMEBUFFER_SRGB);
        glEnable(GL_MULTISAMPLE);
 
+       glGenTextures(1, &fbtex);
+       glBindTexture(GL_TEXTURE_2D, fbtex);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+       if(win_width) {
+               glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, win_width, win_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
+       }
+
+       if(!(sdr_gamma = create_program_load("sdr/gamma.v.glsl", "sdr/gamma.p.glsl"))) {
+               fprintf(stderr, "Warning: failed to load the gamma correction shader\n");
+       }
+
        reg_whitted();
 
        for(i=0; i<num_parts; i++) {
@@ -40,6 +58,13 @@ void demo_cleanup(void)
 {
        int i;
 
+       if(sdr_gamma) {
+               glDeleteProgram(sdr_gamma);
+       }
+       if(fbtex) {
+               glDeleteTextures(1, &fbtex);
+       }
+
        for(i=0; i<num_parts; i++) {
                parts[i]->destroy();
        }
@@ -49,6 +74,13 @@ void demo_display(void)
 {
        long part_time;
 
+       if(reshape_pending) {
+               /* reshape fbtex */
+               glBindTexture(GL_TEXTURE_2D, fbtex);
+               glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, win_width, win_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
+               reshape_pending = 1;
+       }
+
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
        if(!cur_part) return;
@@ -75,10 +107,21 @@ void demo_display(void)
                prev_part = 0;
                cur_part->draw(part_time);
        }
+
+       /* no-srgb gamma correction fallback */
+       if(!opt.srgb) {
+               glBindTexture(GL_TEXTURE_2D, fbtex);
+               glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, win_width, win_height);
+
+               glUseProgram(sdr_gamma);
+               overlay(fbtex, win_aspect, 1.0);
+       }
 }
 
 void demo_reshape(int x, int y)
 {
+       reshape_pending = 1;
+
        win_width = x;
        win_height = y;
        win_aspect = (float)x / (float)y;