]> git.sesse.net Git - nageru/blobdiff - flow.cpp
Make flow writing a bit faster.
[nageru] / flow.cpp
index b5e77627f1826d1b59e98cbd131ac3d4472932d3..93ac447f0021f7dacec0bbd2c9265ee0e6a2c561 100644 (file)
--- a/flow.cpp
+++ b/flow.cpp
@@ -37,6 +37,8 @@ constexpr unsigned patch_size_pixels = 12;
 // since we have different normalizations and ranges in some cases.
 float vr_gamma = 10.0f, vr_delta = 5.0f, vr_alpha = 10.0f;
 
+bool enable_timing = true;
+
 // Some global OpenGL objects.
 // TODO: These should really be part of DISComputeFlow.
 GLuint nearest_sampler, linear_sampler, smoothness_sampler;
@@ -923,6 +925,10 @@ private:
 
 pair<GLuint, GLuint> GPUTimers::begin_timer(const string &name, int level)
 {
+       if (!enable_timing) {
+               return make_pair(0, 0);
+       }
+
        GLuint queries[2];
        glGenQueries(2, queries);
        glQueryCounter(queries[0], GL_TIMESTAMP);
@@ -974,7 +980,7 @@ public:
 
        void end()
        {
-               if (!ended) {
+               if (enable_timing && !ended) {
                        glQueryCounter(query.second, GL_TIMESTAMP);
                        ended = true;
                }
@@ -1263,6 +1269,14 @@ void DISComputeFlow::release_texture(GLuint tex_num)
        assert(false);
 }
 
+// OpenGL uses a bottom-left coordinate system, .flo files use a top-left coordinate system.
+void flip_coordinate_system(float *dense_flow, unsigned width, unsigned height)
+{
+       for (unsigned i = 0; i < width * height; ++i) {
+               dense_flow[i * 2 + 1] = -dense_flow[i * 2 + 1];
+       }
+}
+
 void write_flow(const char *filename, const float *dense_flow, unsigned width, unsigned height)
 {
        FILE *flowfp = fopen(filename, "wb");
@@ -1271,15 +1285,7 @@ void write_flow(const char *filename, const float *dense_flow, unsigned width, u
        fwrite(&height, 4, 1, flowfp);
        for (unsigned y = 0; y < height; ++y) {
                int yy = height - y - 1;
-               for (unsigned x = 0; x < unsigned(width); ++x) {
-                       float du = dense_flow[(yy * width + x) * 2 + 0];
-                       float dv = dense_flow[(yy * width + x) * 2 + 1];
-
-                       dv = -dv;
-
-                       fwrite(&du, 4, 1, flowfp);
-                       fwrite(&dv, 4, 1, flowfp);
-               }
+               fwrite(&dense_flow[yy * width * 2], width * 2 * sizeof(float), 1, flowfp);
        }
        fclose(flowfp);
 }
@@ -1294,8 +1300,6 @@ void write_ppm(const char *filename, const float *dense_flow, unsigned width, un
                        float du = dense_flow[(yy * width + x) * 2 + 0];
                        float dv = dense_flow[(yy * width + x) * 2 + 1];
 
-                       dv = -dv;
-
                        uint8_t r, g, b;
                        flow2rgb(du, dv, &r, &g, &b);
                        putc(r, fp);
@@ -1311,7 +1315,8 @@ 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' }
+                { "gamma", required_argument, 0, 'g' },
+               { "disable-timing", no_argument, 0, 1000 }
        };
 
        for ( ;; ) {
@@ -1331,6 +1336,9 @@ int main(int argc, char **argv)
                case 'g':
                        vr_gamma = atof(optarg);
                        break;
+               case 1000:
+                       enable_timing = false;
+                       break;
                default:
                        fprintf(stderr, "Unknown option '%s'\n", argv[option_index]);
                        exit(1);
@@ -1358,10 +1366,15 @@ int main(int argc, char **argv)
        SDL_GLContext context = SDL_GL_CreateContext(window);
        assert(context != nullptr);
 
+       const char *filename0 = argc >= (optind + 1) ? argv[optind] : "test1499.png";
+       const char *filename1 = argc >= (optind + 2) ? argv[optind + 1] : "test1500.png";
+       const char *flow_filename = argc >= (optind + 3) ? argv[optind + 2] : "flow.flo";
+       fprintf(stderr, "%s %s -> %s\n", filename0, filename1, flow_filename);
+
        // Load pictures.
        unsigned width1, height1, 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);
+       GLuint tex0 = load_texture(filename0, &width1, &height1);
+       GLuint tex1 = load_texture(filename1, &width2, &height2);
 
        if (width1 != width2 || height1 != height2) {
                fprintf(stderr, "Image dimensions don't match (%dx%d versus %dx%d)\n",
@@ -1389,8 +1402,46 @@ int main(int argc, char **argv)
 
        compute_flow.release_texture(final_tex);
 
-       write_flow("flow.flo", dense_flow.get(), width1, height1);
+       flip_coordinate_system(dense_flow.get(), width1, height1);
+       write_flow(flow_filename, dense_flow.get(), width1, height1);
        write_ppm("flow.ppm", dense_flow.get(), width1, height1);
 
+       dense_flow.reset();
+
+       // See if there are more flows on the command line (ie., more than three arguments),
+       // and if so, process them.
+       int num_flows = (argc - optind) / 3;
+       for (int i = 1; i < num_flows; ++i) {
+               const char *filename0 = argv[optind + i * 3 + 0];
+               const char *filename1 = argv[optind + i * 3 + 1];
+               const char *flow_filename = argv[optind + i * 3 + 2];
+               fprintf(stderr, "%s %s -> %s\n", filename0, filename1, flow_filename);
+
+               GLuint width, height;
+               GLuint tex0 = load_texture(filename0, &width, &height);
+               if (width != width1 || height != height1) {
+                       fprintf(stderr, "%s: Image dimensions don't match (%dx%d versus %dx%d)\n",
+                               filename0, width, height, width1, height1);
+                       exit(1);
+               }
+
+               GLuint tex1 = load_texture(filename1, &width, &height);
+               if (width != width1 || height != height1) {
+                       fprintf(stderr, "%s: Image dimensions don't match (%dx%d versus %dx%d)\n",
+                               filename1, width, height, width1, height1);
+                       exit(1);
+               }
+
+               GLuint final_tex = compute_flow.exec(tex0, tex1);
+
+               unique_ptr<float[]> dense_flow(new float[width * height * 2]);
+               glGetTextureImage(final_tex, 0, GL_RG, GL_FLOAT, width * height * 2 * sizeof(float), dense_flow.get());
+
+               compute_flow.release_texture(final_tex);
+
+               flip_coordinate_system(dense_flow.get(), width, height);
+               write_flow(flow_filename, dense_flow.get(), width, height);
+       }
+
        fprintf(stderr, "err = %d\n", glGetError());
 }