+#include "opengl.h"
+#include "app.h"
+
+static void update_fbtex();
+
+static unsigned int fb_tex;
+static int fb_tex_width, fb_tex_height;
+
+void slow_post(unsigned int sdr)
+{
+ update_fbtex();
+
+ glBindTexture(GL_TEXTURE_2D, fb_tex);
+ glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, win_width, win_height);
+
+ glPushAttrib(GL_ENABLE_BIT);
+ glDisable(GL_DEPTH_TEST);
+ glDisable(GL_LIGHTING);
+
+ glUseProgram(sdr);
+
+ float umax = (float)win_width / (float)fb_tex_width;
+ float vmax = (float)win_height / (float)fb_tex_height;
+
+ glBegin(GL_QUADS);
+ glTexCoord2f(0, 0);
+ glVertex2f(-1, -1);
+ glTexCoord2f(umax, 0);
+ glVertex2f(1, -1);
+ glTexCoord2f(umax, vmax);
+ glVertex2f(1, 1);
+ glTexCoord2f(0, vmax);
+ glVertex2f(-1, 1);
+ glEnd();
+
+ glPopAttrib();
+}
+
+static int next_pow2(int x)
+{
+ x--;
+ x = (x >> 1) | x;
+ x = (x >> 2) | x;
+ x = (x >> 4) | x;
+ x = (x >> 8) | x;
+ x = (x >> 16) | x;
+ return x + 1;
+}
+
+static void update_fbtex()
+{
+ if(win_width <= fb_tex_width && win_height <= fb_tex_height) {
+ return; // nothing to do
+ }
+ if(!fb_tex) {
+ glGenTextures(1, &fb_tex);
+ glBindTexture(GL_TEXTURE_2D, fb_tex);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ } else {
+ glBindTexture(GL_TEXTURE_2D, fb_tex);
+ }
+
+ fb_tex_width = next_pow2(win_width);
+ fb_tex_height = next_pow2(win_height);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_width, fb_tex_height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
+}