]> git.sesse.net Git - nageru/blobdiff - flow.cpp
Small cleanup.
[nageru] / flow.cpp
index f16eda22ac82e5458a85955bbed8b276c976d21e..81d835b73a3e0b0eef8eab547c3e89e5b816d88b 100644 (file)
--- a/flow.cpp
+++ b/flow.cpp
@@ -11,6 +11,7 @@
 #include <SDL2/SDL_video.h>
 
 #include <assert.h>
+#include <getopt.h>
 #include <stdio.h>
 #include <unistd.h>
 
@@ -30,6 +31,11 @@ constexpr unsigned coarsest_level = 5;
 constexpr unsigned finest_level = 1;
 constexpr unsigned patch_size_pixels = 12;
 
+// Weighting constants for the different parts of the variational refinement.
+// These don't correspond 1:1 to the values given in the DIS paper,
+// since we have different normalizations and ranges in some cases.
+float vr_gamma = 10.0f, vr_delta = 5.0f, vr_alpha = 10.0f;
+
 // Some global OpenGL objects.
 GLuint nearest_sampler, linear_sampler, smoothness_sampler;
 GLuint vertex_vbo;
@@ -571,6 +577,7 @@ private:
        GLuint smoothness_vao;
 
        GLuint uniform_flow_tex, uniform_diff_flow_tex;
+       GLuint uniform_alpha;
 };
 
 ComputeSmoothness::ComputeSmoothness()
@@ -590,6 +597,7 @@ ComputeSmoothness::ComputeSmoothness()
 
        uniform_flow_tex = glGetUniformLocation(smoothness_program, "flow_tex");
        uniform_diff_flow_tex = glGetUniformLocation(smoothness_program, "diff_flow_tex");
+       uniform_alpha = glGetUniformLocation(smoothness_program, "alpha");
 }
 
 void ComputeSmoothness::exec(GLuint flow_tex, GLuint diff_flow_tex, GLuint smoothness_x_tex, GLuint smoothness_y_tex, int level_width, int level_height)
@@ -598,6 +606,7 @@ void ComputeSmoothness::exec(GLuint flow_tex, GLuint diff_flow_tex, GLuint smoot
 
        bind_sampler(smoothness_program, uniform_flow_tex, 0, flow_tex, nearest_sampler);
        bind_sampler(smoothness_program, uniform_diff_flow_tex, 1, diff_flow_tex, nearest_sampler);
+       glProgramUniform1f(smoothness_program, uniform_alpha, vr_alpha);
 
        GLuint smoothness_fbo;  // TODO: cleanup
        glCreateFramebuffers(1, &smoothness_fbo);
@@ -646,6 +655,7 @@ private:
        GLuint uniform_diff_flow_tex, uniform_base_flow_tex;
        GLuint uniform_beta_0_tex;
        GLuint uniform_smoothness_x_tex, uniform_smoothness_y_tex;
+       GLuint uniform_gamma, uniform_delta;
 };
 
 SetupEquations::SetupEquations()
@@ -670,6 +680,8 @@ SetupEquations::SetupEquations()
        uniform_beta_0_tex = glGetUniformLocation(equations_program, "beta_0_tex");
        uniform_smoothness_x_tex = glGetUniformLocation(equations_program, "smoothness_x_tex");
        uniform_smoothness_y_tex = glGetUniformLocation(equations_program, "smoothness_y_tex");
+       uniform_gamma = glGetUniformLocation(equations_program, "gamma");
+       uniform_delta = glGetUniformLocation(equations_program, "delta");
 }
 
 void SetupEquations::exec(GLuint I_x_y_tex, GLuint I_t_tex, GLuint diff_flow_tex, GLuint base_flow_tex, GLuint beta_0_tex, GLuint smoothness_x_tex, GLuint smoothness_y_tex, GLuint equation_tex, int level_width, int level_height)
@@ -683,6 +695,8 @@ void SetupEquations::exec(GLuint I_x_y_tex, GLuint I_t_tex, GLuint diff_flow_tex
        bind_sampler(equations_program, uniform_beta_0_tex, 4, beta_0_tex, nearest_sampler);
        bind_sampler(equations_program, uniform_smoothness_x_tex, 5, smoothness_x_tex, smoothness_sampler);
        bind_sampler(equations_program, uniform_smoothness_y_tex, 6, smoothness_y_tex, smoothness_sampler);
+       glProgramUniform1f(equations_program, uniform_delta, vr_delta);
+       glProgramUniform1f(equations_program, uniform_gamma, vr_gamma);
 
        GLuint equations_fbo;  // TODO: cleanup
        glCreateFramebuffers(1, &equations_fbo);
@@ -815,35 +829,6 @@ void AddBaseFlow::exec(GLuint base_flow_tex, GLuint diff_flow_tex, int level_wid
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
 }
 
-class GPUTimers {
-public:
-       void print();
-       pair<GLuint, GLuint> begin_timer(const string &name, int level);
-
-private:
-       struct Timer {
-               string name;
-               int level;
-               pair<GLuint, GLuint> query;
-       };
-       vector<Timer> timers;
-};
-
-pair<GLuint, GLuint> GPUTimers::begin_timer(const string &name, int level)
-{
-       GLuint queries[2];
-       glGenQueries(2, queries);
-       glQueryCounter(queries[0], GL_TIMESTAMP);
-
-       Timer timer;
-       timer.name = name;
-       timer.level = level;
-       timer.query.first = queries[0];
-       timer.query.second = queries[1];
-       timers.push_back(timer);
-       return timer.query;
-}
-
 // Take a copy of the flow, bilinearly interpolated and scaled up.
 class ResizeFlow {
 public:
@@ -899,6 +884,35 @@ void ResizeFlow::exec(GLuint flow_tex, GLuint out_tex, int input_width, int inpu
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
 }
 
+class GPUTimers {
+public:
+       void print();
+       pair<GLuint, GLuint> begin_timer(const string &name, int level);
+
+private:
+       struct Timer {
+               string name;
+               int level;
+               pair<GLuint, GLuint> query;
+       };
+       vector<Timer> timers;
+};
+
+pair<GLuint, GLuint> GPUTimers::begin_timer(const string &name, int level)
+{
+       GLuint queries[2];
+       glGenQueries(2, queries);
+       glQueryCounter(queries[0], GL_TIMESTAMP);
+
+       Timer timer;
+       timer.name = name;
+       timer.level = level;
+       timer.query.first = queries[0];
+       timer.query.second = queries[1];
+       timers.push_back(timer);
+       return timer.query;
+}
+
 void GPUTimers::print()
 {
        for (const Timer &timer : timers) {
@@ -952,6 +966,35 @@ private:
 
 int main(int argc, char **argv)
 {
+        static const option long_options[] = {
+                { "alpha", required_argument, 0, 'a' },
+                { "delta", required_argument, 0, 'd' },
+                { "gamma", required_argument, 0, 'g' }
+       };
+
+       for ( ;; ) {
+               int option_index = 0;
+               int c = getopt_long(argc, argv, "a:d:g:", long_options, &option_index);
+
+               if (c == -1) {
+                       break;
+               }
+               switch (c) {
+               case 'a':
+                       vr_alpha = atof(optarg);
+                       break;
+               case 'd':
+                       vr_delta = atof(optarg);
+                       break;
+               case 'g':
+                       vr_gamma = atof(optarg);
+                       break;
+               default:
+                       fprintf(stderr, "Unknown option '%s'\n", argv[option_index]);
+                       exit(1);
+               };
+       }
+
        if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
                fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
                exit(1);
@@ -975,8 +1018,8 @@ int main(int argc, char **argv)
 
        // Load pictures.
        unsigned width1, height1, width2, height2;
-       GLuint tex0 = load_texture(argc >= 2 ? argv[1] : "test1499.png", &width1, &height1);
-       GLuint tex1 = load_texture(argc >= 3 ? argv[2] : "test1500.png", &width2, &height2);
+       GLuint tex0 = load_texture(argc >= (optind + 1) ? argv[optind] : "test1499.png", &width1, &height1);
+       GLuint tex1 = load_texture(argc >= (optind + 2) ? argv[optind + 1] : "test1500.png", &width2, &height2);
 
        if (width1 != width2 || height1 != height2) {
                fprintf(stderr, "Image dimensions don't match (%dx%d versus %dx%d)\n",