- moved fbtex to post.c, made it a ping-pong buffer, added fbos for it.
authorJohn Tsiombikas <nuclear@member.fsf.org>
Thu, 10 Dec 2020 06:38:57 +0000 (08:38 +0200)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Thu, 10 Dec 2020 06:38:57 +0000 (08:38 +0200)
- fixed: sRGB and MSAA should be enabled only if their respective
  options are true.

sdr/whitted.p.glsl
src/demo.c
src/post.c
src/post.h
src/texture.c

index 3fa4308..44bb180 100644 (file)
@@ -169,6 +169,7 @@ vec3 backdrop(in vec3 dir)
 #define FLOOR_OFFS     vec3(3.0, 0.0, 0.0)
 #define FLOOR_SIZE     vec2(5.5, 15.0)
 
+#define REFL_POS       vec3(1.36, -0.5, 0.0)
 #define GLASS_POS      vec3(0.0, 0.2, 1.2)
 
 bool isect_scene(in vec3 ro, in vec3 rd, out HitPoint hit_res)
@@ -178,7 +179,7 @@ bool isect_scene(in vec3 ro, in vec3 rd, out HitPoint hit_res)
 
        nearest.dist = 10000.0;
 
-       if(isect_sphere(ro, rd, vec3(1.5, -0.5, 0.0), 0.85, hit)) {
+       if(isect_sphere(ro, rd, REFL_POS, 0.85, hit)) {
                nearest = hit;
                nearest.mtl = mtl_sph;
                if(hit.dist < 1.0) {
index 97da536..3841066 100644 (file)
@@ -13,7 +13,6 @@ float win_aspect;
 long time_msec;
 
 static int reshape_pending;
-static unsigned int fbtex;
 static unsigned int sdr_gamma;
 
 int demo_init(void)
@@ -26,16 +25,14 @@ int demo_init(void)
 
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_CULL_FACE);
-       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(opt.srgb) {
+               glEnable(GL_FRAMEBUFFER_SRGB);
        }
+       if(opt.msaa) {
+               glEnable(GL_MULTISAMPLE);
+       }
+
+       post_init();
 
        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");
@@ -61,9 +58,7 @@ void demo_cleanup(void)
        if(sdr_gamma) {
                glDeleteProgram(sdr_gamma);
        }
-       if(fbtex) {
-               glDeleteTextures(1, &fbtex);
-       }
+       post_cleanup();
 
        for(i=0; i<num_parts; i++) {
                parts[i]->destroy();
@@ -75,10 +70,8 @@ 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;
+               post_reshape(win_width, win_height);
+               reshape_pending = 0;
        }
 
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -110,11 +103,11 @@ void demo_display(void)
 
        /* no-srgb gamma correction fallback */
        if(!opt.srgb) {
-               glBindTexture(GL_TEXTURE_2D, fbtex);
+               glBindTexture(GL_TEXTURE_2D, fbtex[0].id);
                glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, win_width, win_height);
 
                glUseProgram(sdr_gamma);
-               overlay(fbtex, win_aspect, 1.0);
+               overlay_tex(fbtex, 1.0);
        }
 }
 
index c1a2a23..b78284a 100644 (file)
@@ -1,7 +1,76 @@
+#include <assert.h>
 #include "opengl.h"
 #include "texture.h"
 #include "post.h"
 #include "demo.h"
+#include "opt.h"
+
+static unsigned int fbtex_gltexid[2];
+static unsigned int fbo[2], rbuf_depth[2];
+struct texture fbtex[2];
+int fbtex_cur;
+
+int post_init(void)
+{
+       int i;
+
+       glGenTextures(2, fbtex_gltexid);
+       glGenRenderbuffers(2, rbuf_depth);
+
+       for(i=0; i<2; i++) {
+               fbtex[i].id = fbtex_gltexid[i];
+               fbtex[i].width = 0;
+               fbtex[i].height = 0;
+               fbtex[i].pixels = 0;
+
+               glBindTexture(GL_TEXTURE_2D, fbtex[i].id);
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+       }
+       return 0;
+}
+
+void post_cleanup(void)
+{
+       if(fbtex_gltexid[0]) {
+               glDeleteTextures(2, fbtex_gltexid);
+       }
+       if(rbuf_depth[0]) {
+               glDeleteRenderbuffers(2, rbuf_depth);
+       }
+       if(fbo[0]) {
+               glDeleteFramebuffers(2, fbo);
+       }
+}
+
+void post_reshape(int x, int y)
+{
+       int i, ifmt;
+
+       if(x != fbtex[0].width || y != fbtex[0].height) {
+               ifmt = opt.srgb ? GL_SRGB_ALPHA : GL_RGBA;
+
+               for(i=0; i<2; i++) {
+                       glBindTexture(GL_TEXTURE_2D, fbtex[i].id);
+                       glTexImage2D(GL_TEXTURE_2D, 0, ifmt, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
+                       fbtex[i].width = x;
+                       fbtex[i].height = y;
+
+                       glBindRenderbuffer(GL_RENDERBUFFER, rbuf_depth[i]);
+                       glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, x, y);
+               }
+
+               if(!fbo[0]) {
+                       glGenFramebuffers(2, fbo);
+                       for(i=0; i<2; i++) {
+                               glBindFramebuffer(GL_FRAMEBUFFER, fbo[i]);
+                               glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbtex_gltexid[i], 0);
+                               glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth[i]);
+                       }
+                       glBindFramebuffer(GL_FRAMEBUFFER, 0);
+               }
+       }
+}
 
 void overlay(unsigned int tex, float aspect, float alpha)
 {
index d61dc5d..c942630 100644 (file)
@@ -1,7 +1,14 @@
 #ifndef POST_H_
 #define POST_H_
 
-struct texture;
+#include "texture.h"
+
+extern struct texture fbtex[2];
+extern int fbtex_cur;
+
+int post_init(void);
+void post_cleanup(void);
+void post_reshape(int x, int y);
 
 void overlay(unsigned int tex, float aspect, float alpha);
 void overlay_tex(struct texture *tex, float alpha);
index c0a80b2..32e5c2b 100644 (file)
@@ -18,6 +18,7 @@ static unsigned int gl_type[] = {
        GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE, GL_FLOAT, GL_FLOAT, GL_FLOAT, GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT_5_6_5
 };
 
+
 struct texture *load_texture(const char *fname)
 {
        struct img_pixmap img;