-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) {
- // NOTE: This makes the CPU wait for the GPU.
- GLuint64 time_start, time_end;
- glGetQueryObjectui64v(timer.query.first, GL_QUERY_RESULT, &time_start);
- glGetQueryObjectui64v(timer.query.second, GL_QUERY_RESULT, &time_end);
- //fprintf(stderr, "GPU time used = %.1f ms\n", time_elapsed / 1e6);
- for (int i = 0; i < timer.level * 2; ++i) {
- fprintf(stderr, " ");
- }
- fprintf(stderr, "%-30s %4.1f ms\n", timer.name.c_str(), GLint64(time_end - time_start) / 1e6);
- }
-}
-
-// A simple RAII class for timing until the end of the scope.
-class ScopedTimer {
-public:
- ScopedTimer(const string &name, GPUTimers *timers)
- : timers(timers), level(0)
- {
- query = timers->begin_timer(name, level);
- }
-
- ScopedTimer(const string &name, ScopedTimer *parent_timer)
- : timers(parent_timer->timers),
- level(parent_timer->level + 1)
- {
- query = timers->begin_timer(name, level);
- }
-
- ~ScopedTimer()
- {
- end();
- }
-
- void end()
- {
- if (!ended) {
- glQueryCounter(query.second, GL_TIMESTAMP);
- ended = true;
- }
- }
-
-private:
- GPUTimers *timers;
- int level;
- pair<GLuint, GLuint> query;
- bool ended = false;
-};
-
-class DISComputeFlow {
-public:
- DISComputeFlow(int width, int height);
-
- // Returns a texture that must be released with release_texture()
- // after use.
- GLuint exec(GLuint tex0, GLuint tex1);
- void release_texture(GLuint tex);
-
-private:
- int width, height;
- GLuint initial_flow_tex;
-
- // The various passes.
- Sobel sobel;
- MotionSearch motion_search;
- Densify densify;
- Prewarp prewarp;
- Derivatives derivatives;
- ComputeSmoothness compute_smoothness;
- SetupEquations setup_equations;
- SOR sor;
- AddBaseFlow add_base_flow;
- ResizeFlow resize_flow;
-
- struct Texture {
- GLuint tex_num;
- GLenum format;
- GLuint width, height;
- bool in_use = false;
- };
- vector<Texture> textures;
-
- GLuint get_texture(GLenum format, GLuint width, GLuint height);
-};
-
-DISComputeFlow::DISComputeFlow(int width, int height)
- : width(width), height(height)