6 #include <SDL2/SDL_error.h>
7 #include <SDL2/SDL_events.h>
8 #include <SDL2/SDL_image.h>
9 #include <SDL2/SDL_keyboard.h>
10 #include <SDL2/SDL_mouse.h>
11 #include <SDL2/SDL_video.h>
25 #define BUFFER_OFFSET(i) ((char *)nullptr + (i))
29 // Operating point 3 (10 Hz on CPU, excluding preprocessing).
30 constexpr float patch_overlap_ratio = 0.75f;
31 constexpr unsigned coarsest_level = 5;
32 constexpr unsigned finest_level = 1;
33 constexpr unsigned patch_size_pixels = 12;
35 // Weighting constants for the different parts of the variational refinement.
36 // These don't correspond 1:1 to the values given in the DIS paper,
37 // since we have different normalizations and ranges in some cases.
38 float vr_gamma = 10.0f, vr_delta = 5.0f, vr_alpha = 10.0f;
40 // Some global OpenGL objects.
41 GLuint nearest_sampler, linear_sampler, smoothness_sampler;
44 string read_file(const string &filename)
46 FILE *fp = fopen(filename.c_str(), "r");
48 perror(filename.c_str());
52 int ret = fseek(fp, 0, SEEK_END);
54 perror("fseek(SEEK_END)");
60 ret = fseek(fp, 0, SEEK_SET);
62 perror("fseek(SEEK_SET)");
68 ret = fread(&str[0], size, 1, fp);
74 fprintf(stderr, "Short read when trying to read %d bytes from %s\n",
75 size, filename.c_str());
84 GLuint compile_shader(const string &shader_src, GLenum type)
86 GLuint obj = glCreateShader(type);
87 const GLchar* source[] = { shader_src.data() };
88 const GLint length[] = { (GLint)shader_src.size() };
89 glShaderSource(obj, 1, source, length);
92 GLchar info_log[4096];
93 GLsizei log_length = sizeof(info_log) - 1;
94 glGetShaderInfoLog(obj, log_length, &log_length, info_log);
95 info_log[log_length] = 0;
96 if (strlen(info_log) > 0) {
97 fprintf(stderr, "Shader compile log: %s\n", info_log);
101 glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
102 if (status == GL_FALSE) {
103 // Add some line numbers to easier identify compile errors.
104 string src_with_lines = "/* 1 */ ";
106 for (char ch : shader_src) {
107 src_with_lines.push_back(ch);
110 snprintf(buf, sizeof(buf), "/* %3zu */ ", ++lineno);
111 src_with_lines += buf;
115 fprintf(stderr, "Failed to compile shader:\n%s\n", src_with_lines.c_str());
122 GLuint load_texture(const char *filename, unsigned *width_ret, unsigned *height_ret)
124 SDL_Surface *surf = IMG_Load(filename);
125 if (surf == nullptr) {
126 fprintf(stderr, "IMG_Load(%s): %s\n", filename, IMG_GetError());
130 // For whatever reason, SDL doesn't support converting to YUV surfaces
131 // nor grayscale, so we'll do it (slowly) ourselves.
132 SDL_Surface *rgb_surf = SDL_ConvertSurfaceFormat(surf, SDL_PIXELFORMAT_RGBA8888, /*flags=*/0);
133 if (rgb_surf == nullptr) {
134 fprintf(stderr, "SDL_ConvertSurfaceFormat(%s): %s\n", filename, SDL_GetError());
138 SDL_FreeSurface(surf);
140 unsigned width = rgb_surf->w, height = rgb_surf->h;
141 const uint8_t *sptr = (uint8_t *)rgb_surf->pixels;
142 unique_ptr<uint8_t[]> pix(new uint8_t[width * height]);
144 // Extract the Y component, and convert to bottom-left origin.
145 for (unsigned y = 0; y < height; ++y) {
146 unsigned y2 = height - 1 - y;
147 for (unsigned x = 0; x < width; ++x) {
148 uint8_t r = sptr[(y2 * width + x) * 4 + 3];
149 uint8_t g = sptr[(y2 * width + x) * 4 + 2];
150 uint8_t b = sptr[(y2 * width + x) * 4 + 1];
153 pix[y * width + x] = lrintf(r * 0.2126f + g * 0.7152f + b * 0.0722f);
156 SDL_FreeSurface(rgb_surf);
159 for (int w = width, h = height; w > 1 || h > 1; ) {
166 glCreateTextures(GL_TEXTURE_2D, 1, &tex);
167 glTextureStorage2D(tex, levels, GL_R8, width, height);
168 glTextureSubImage2D(tex, 0, 0, 0, width, height, GL_RED, GL_UNSIGNED_BYTE, pix.get());
169 glGenerateTextureMipmap(tex);
172 *height_ret = height;
177 GLuint link_program(GLuint vs_obj, GLuint fs_obj)
179 GLuint program = glCreateProgram();
180 glAttachShader(program, vs_obj);
181 glAttachShader(program, fs_obj);
182 glLinkProgram(program);
184 glGetProgramiv(program, GL_LINK_STATUS, &success);
185 if (success == GL_FALSE) {
186 GLchar error_log[1024] = {0};
187 glGetProgramInfoLog(program, 1024, nullptr, error_log);
188 fprintf(stderr, "Error linking program: %s\n", error_log);
194 GLuint generate_vbo(GLint size, GLsizeiptr data_size, const GLvoid *data)
197 glCreateBuffers(1, &vbo);
198 glBufferData(GL_ARRAY_BUFFER, data_size, data, GL_STATIC_DRAW);
199 glNamedBufferData(vbo, data_size, data, GL_STATIC_DRAW);
203 GLuint fill_vertex_attribute(GLuint vao, GLuint glsl_program_num, const string &attribute_name, GLint size, GLenum type, GLsizeiptr data_size, const GLvoid *data)
205 int attrib = glGetAttribLocation(glsl_program_num, attribute_name.c_str());
210 GLuint vbo = generate_vbo(size, data_size, data);
212 glBindBuffer(GL_ARRAY_BUFFER, vbo);
213 glEnableVertexArrayAttrib(vao, attrib);
214 glVertexAttribPointer(attrib, size, type, GL_FALSE, 0, BUFFER_OFFSET(0));
215 glBindBuffer(GL_ARRAY_BUFFER, 0);
220 void bind_sampler(GLuint program, GLint location, GLuint texture_unit, GLuint tex, GLuint sampler)
222 if (location == -1) {
226 glBindTextureUnit(texture_unit, tex);
227 glBindSampler(texture_unit, sampler);
228 glProgramUniform1i(program, location, texture_unit);
231 // A class that caches FBOs that render to a given set of textures.
232 // It never frees anything, so it is only suitable for rendering to
233 // the same (small) set of textures over and over again.
234 template<size_t num_elements>
235 class PersistentFBOSet {
237 void render_to(const array<GLuint, num_elements> &textures);
239 // Convenience wrappers.
240 void render_to(GLuint texture0, enable_if<num_elements == 1> * = nullptr) {
241 render_to({{texture0}});
244 void render_to(GLuint texture0, GLuint texture1, enable_if<num_elements == 2> * = nullptr) {
245 render_to({{texture0, texture1}});
248 void render_to(GLuint texture0, GLuint texture1, GLuint texture2, enable_if<num_elements == 3> * = nullptr) {
249 render_to({{texture0, texture1, texture2}});
252 void render_to(GLuint texture0, GLuint texture1, GLuint texture2, GLuint texture3, enable_if<num_elements == 4> * = nullptr) {
253 render_to({{texture0, texture1, texture2, texture3}});
257 // TODO: Delete these on destruction.
258 map<array<GLuint, num_elements>, GLuint> fbos;
261 template<size_t num_elements>
262 void PersistentFBOSet<num_elements>::render_to(const array<GLuint, num_elements> &textures)
264 auto it = fbos.find(textures);
265 if (it != fbos.end()) {
266 glBindFramebuffer(GL_FRAMEBUFFER, it->second);
271 glCreateFramebuffers(1, &fbo);
272 GLenum bufs[num_elements];
273 for (size_t i = 0; i < num_elements; ++i) {
274 glNamedFramebufferTexture(fbo, GL_COLOR_ATTACHMENT0 + i, textures[i], 0);
275 bufs[i] = GL_COLOR_ATTACHMENT0 + i;
277 glNamedFramebufferDrawBuffers(fbo, num_elements, bufs);
279 fbos[textures] = fbo;
280 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
283 // Compute gradients in every point, used for the motion search.
284 // The DIS paper doesn't actually mention how these are computed,
285 // but seemingly, a 3x3 Sobel operator is used here (at least in
286 // later versions of the code), while a [1 -8 0 8 -1] kernel is
287 // used for all the derivatives in the variational refinement part
288 // (which borrows code from DeepFlow). This is inconsistent,
289 // but I guess we're better off with staying with the original
290 // decisions until we actually know having different ones would be better.
294 void exec(GLint tex0_view, GLint grad0_tex, int level_width, int level_height);
297 PersistentFBOSet<1> fbos;
300 GLuint sobel_program;
303 GLuint uniform_tex, uniform_image_size;
308 sobel_vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER);
309 sobel_fs_obj = compile_shader(read_file("sobel.frag"), GL_FRAGMENT_SHADER);
310 sobel_program = link_program(sobel_vs_obj, sobel_fs_obj);
312 // Set up the VAO containing all the required position/texcoord data.
313 glCreateVertexArrays(1, &sobel_vao);
314 glBindVertexArray(sobel_vao);
316 GLint position_attrib = glGetAttribLocation(sobel_program, "position");
317 glEnableVertexArrayAttrib(sobel_vao, position_attrib);
318 glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
320 uniform_tex = glGetUniformLocation(sobel_program, "tex");
323 void Sobel::exec(GLint tex0_view, GLint grad0_tex, int level_width, int level_height)
325 glUseProgram(sobel_program);
326 glBindTextureUnit(0, tex0_view);
327 glBindSampler(0, nearest_sampler);
328 glProgramUniform1i(sobel_program, uniform_tex, 0);
330 glViewport(0, 0, level_width, level_height);
331 fbos.render_to(grad0_tex);
332 glBindVertexArray(sobel_vao);
333 glUseProgram(sobel_program);
335 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
338 // Motion search to find the initial flow. See motion_search.frag for documentation.
342 void exec(GLuint tex0_view, GLuint tex1_view, GLuint grad0_tex, GLuint flow_tex, GLuint flow_out_tex, int level_width, int level_height, int prev_level_width, int prev_level_height, int width_patches, int height_patches);
345 PersistentFBOSet<1> fbos;
347 GLuint motion_vs_obj;
348 GLuint motion_fs_obj;
349 GLuint motion_search_program;
350 GLuint motion_search_vao;
352 GLuint uniform_image_size, uniform_inv_image_size, uniform_inv_prev_level_size;
353 GLuint uniform_image0_tex, uniform_image1_tex, uniform_grad0_tex, uniform_flow_tex;
356 MotionSearch::MotionSearch()
358 motion_vs_obj = compile_shader(read_file("motion_search.vert"), GL_VERTEX_SHADER);
359 motion_fs_obj = compile_shader(read_file("motion_search.frag"), GL_FRAGMENT_SHADER);
360 motion_search_program = link_program(motion_vs_obj, motion_fs_obj);
362 // Set up the VAO containing all the required position/texcoord data.
363 glCreateVertexArrays(1, &motion_search_vao);
364 glBindVertexArray(motion_search_vao);
365 glBindBuffer(GL_ARRAY_BUFFER, vertex_vbo);
367 GLint position_attrib = glGetAttribLocation(motion_search_program, "position");
368 glEnableVertexArrayAttrib(motion_search_vao, position_attrib);
369 glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
371 uniform_image_size = glGetUniformLocation(motion_search_program, "image_size");
372 uniform_inv_image_size = glGetUniformLocation(motion_search_program, "inv_image_size");
373 uniform_inv_prev_level_size = glGetUniformLocation(motion_search_program, "inv_prev_level_size");
374 uniform_image0_tex = glGetUniformLocation(motion_search_program, "image0_tex");
375 uniform_image1_tex = glGetUniformLocation(motion_search_program, "image1_tex");
376 uniform_grad0_tex = glGetUniformLocation(motion_search_program, "grad0_tex");
377 uniform_flow_tex = glGetUniformLocation(motion_search_program, "flow_tex");
380 void MotionSearch::exec(GLuint tex0_view, GLuint tex1_view, GLuint grad0_tex, GLuint flow_tex, GLuint flow_out_tex, int level_width, int level_height, int prev_level_width, int prev_level_height, int width_patches, int height_patches)
382 glUseProgram(motion_search_program);
384 bind_sampler(motion_search_program, uniform_image0_tex, 0, tex0_view, nearest_sampler);
385 bind_sampler(motion_search_program, uniform_image1_tex, 1, tex1_view, linear_sampler);
386 bind_sampler(motion_search_program, uniform_grad0_tex, 2, grad0_tex, nearest_sampler);
387 bind_sampler(motion_search_program, uniform_flow_tex, 3, flow_tex, linear_sampler);
389 glProgramUniform2f(motion_search_program, uniform_image_size, level_width, level_height);
390 glProgramUniform2f(motion_search_program, uniform_inv_image_size, 1.0f / level_width, 1.0f / level_height);
391 glProgramUniform2f(motion_search_program, uniform_inv_prev_level_size, 1.0f / prev_level_width, 1.0f / prev_level_height);
393 glViewport(0, 0, width_patches, height_patches);
394 fbos.render_to(flow_out_tex);
395 glBindVertexArray(motion_search_vao);
396 glUseProgram(motion_search_program);
397 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
400 // Do “densification”, ie., upsampling of the flow patches to the flow field
401 // (the same size as the image at this level). We draw one quad per patch
402 // over its entire covered area (using instancing in the vertex shader),
403 // and then weight the contributions in the pixel shader by post-warp difference.
404 // This is equation (3) in the paper.
406 // We accumulate the flow vectors in the R/G channels (for u/v) and the total
407 // weight in the B channel. Dividing R and G by B gives the normalized values.
411 void exec(GLuint tex0_view, GLuint tex1_view, GLuint flow_tex, GLuint dense_flow_tex, int level_width, int level_height, int width_patches, int height_patches);
414 PersistentFBOSet<1> fbos;
416 GLuint densify_vs_obj;
417 GLuint densify_fs_obj;
418 GLuint densify_program;
421 GLuint uniform_width_patches, uniform_patch_size, uniform_patch_spacing;
422 GLuint uniform_image0_tex, uniform_image1_tex, uniform_flow_tex;
427 densify_vs_obj = compile_shader(read_file("densify.vert"), GL_VERTEX_SHADER);
428 densify_fs_obj = compile_shader(read_file("densify.frag"), GL_FRAGMENT_SHADER);
429 densify_program = link_program(densify_vs_obj, densify_fs_obj);
431 // Set up the VAO containing all the required position/texcoord data.
432 glCreateVertexArrays(1, &densify_vao);
433 glBindVertexArray(densify_vao);
434 glBindBuffer(GL_ARRAY_BUFFER, vertex_vbo);
436 GLint position_attrib = glGetAttribLocation(densify_program, "position");
437 glEnableVertexArrayAttrib(densify_vao, position_attrib);
438 glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
440 uniform_width_patches = glGetUniformLocation(densify_program, "width_patches");
441 uniform_patch_size = glGetUniformLocation(densify_program, "patch_size");
442 uniform_patch_spacing = glGetUniformLocation(densify_program, "patch_spacing");
443 uniform_image0_tex = glGetUniformLocation(densify_program, "image0_tex");
444 uniform_image1_tex = glGetUniformLocation(densify_program, "image1_tex");
445 uniform_flow_tex = glGetUniformLocation(densify_program, "flow_tex");
448 void Densify::exec(GLuint tex0_view, GLuint tex1_view, GLuint flow_tex, GLuint dense_flow_tex, int level_width, int level_height, int width_patches, int height_patches)
450 glUseProgram(densify_program);
452 bind_sampler(densify_program, uniform_image0_tex, 0, tex0_view, nearest_sampler);
453 bind_sampler(densify_program, uniform_image1_tex, 1, tex1_view, linear_sampler);
454 bind_sampler(densify_program, uniform_flow_tex, 2, flow_tex, nearest_sampler);
456 glProgramUniform1i(densify_program, uniform_width_patches, width_patches);
457 glProgramUniform2f(densify_program, uniform_patch_size,
458 float(patch_size_pixels) / level_width,
459 float(patch_size_pixels) / level_height);
461 float patch_spacing_x = float(level_width - patch_size_pixels) / (width_patches - 1);
462 float patch_spacing_y = float(level_height - patch_size_pixels) / (height_patches - 1);
463 if (width_patches == 1) patch_spacing_x = 0.0f; // Avoid infinities.
464 if (height_patches == 1) patch_spacing_y = 0.0f;
465 glProgramUniform2f(densify_program, uniform_patch_spacing,
466 patch_spacing_x / level_width,
467 patch_spacing_y / level_height);
469 glViewport(0, 0, level_width, level_height);
471 glBlendFunc(GL_ONE, GL_ONE);
472 glBindVertexArray(densify_vao);
473 fbos.render_to(dense_flow_tex);
474 glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, width_patches * height_patches);
477 // Warp I_1 to I_w, and then compute the mean (I) and difference (I_t) of
478 // I_0 and I_w. The prewarping is what enables us to solve the variational
479 // flow for du,dv instead of u,v.
481 // Also calculates the normalized flow, ie. divides by z (this is needed because
482 // Densify works by additive blending) and multiplies by the image size.
484 // See variational_refinement.txt for more information.
488 void exec(GLuint tex0_view, GLuint tex1_view, GLuint flow_tex, GLuint normalized_flow_tex, GLuint I_tex, GLuint I_t_tex, int level_width, int level_height);
491 PersistentFBOSet<3> fbos;
493 GLuint prewarp_vs_obj;
494 GLuint prewarp_fs_obj;
495 GLuint prewarp_program;
498 GLuint uniform_image0_tex, uniform_image1_tex, uniform_flow_tex;
499 GLuint uniform_image_size;
504 prewarp_vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER);
505 prewarp_fs_obj = compile_shader(read_file("prewarp.frag"), GL_FRAGMENT_SHADER);
506 prewarp_program = link_program(prewarp_vs_obj, prewarp_fs_obj);
508 // Set up the VAO containing all the required position/texcoord data.
509 glCreateVertexArrays(1, &prewarp_vao);
510 glBindVertexArray(prewarp_vao);
511 glBindBuffer(GL_ARRAY_BUFFER, vertex_vbo);
513 GLint position_attrib = glGetAttribLocation(prewarp_program, "position");
514 glEnableVertexArrayAttrib(prewarp_vao, position_attrib);
515 glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
517 uniform_image0_tex = glGetUniformLocation(prewarp_program, "image0_tex");
518 uniform_image1_tex = glGetUniformLocation(prewarp_program, "image1_tex");
519 uniform_flow_tex = glGetUniformLocation(prewarp_program, "flow_tex");
521 uniform_image_size = glGetUniformLocation(prewarp_program, "image_size");
524 void Prewarp::exec(GLuint tex0_view, GLuint tex1_view, GLuint flow_tex, GLuint I_tex, GLuint I_t_tex, GLuint normalized_flow_tex, int level_width, int level_height)
526 glUseProgram(prewarp_program);
528 bind_sampler(prewarp_program, uniform_image0_tex, 0, tex0_view, nearest_sampler);
529 bind_sampler(prewarp_program, uniform_image1_tex, 1, tex1_view, linear_sampler);
530 bind_sampler(prewarp_program, uniform_flow_tex, 2, flow_tex, nearest_sampler);
532 glProgramUniform2f(prewarp_program, uniform_image_size, level_width, level_height);
534 glViewport(0, 0, level_width, level_height);
536 glBindVertexArray(prewarp_vao);
537 fbos.render_to(I_tex, I_t_tex, normalized_flow_tex);
538 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
541 // From I, calculate the partial derivatives I_x and I_y. We use a four-tap
542 // central difference filter, since apparently, that's tradition (I haven't
543 // measured quality versus a more normal 0.5 (I[x+1] - I[x-1]).)
544 // The coefficients come from
546 // https://en.wikipedia.org/wiki/Finite_difference_coefficient
548 // Also computes β_0, since it depends only on I_x and I_y.
552 void exec(GLuint input_tex, GLuint I_x_y_tex, GLuint beta_0_tex, int level_width, int level_height);
555 PersistentFBOSet<2> fbos;
557 GLuint derivatives_vs_obj;
558 GLuint derivatives_fs_obj;
559 GLuint derivatives_program;
560 GLuint derivatives_vao;
565 Derivatives::Derivatives()
567 derivatives_vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER);
568 derivatives_fs_obj = compile_shader(read_file("derivatives.frag"), GL_FRAGMENT_SHADER);
569 derivatives_program = link_program(derivatives_vs_obj, derivatives_fs_obj);
571 // Set up the VAO containing all the required position/texcoord data.
572 glCreateVertexArrays(1, &derivatives_vao);
573 glBindVertexArray(derivatives_vao);
574 glBindBuffer(GL_ARRAY_BUFFER, vertex_vbo);
576 GLint position_attrib = glGetAttribLocation(derivatives_program, "position");
577 glEnableVertexArrayAttrib(derivatives_vao, position_attrib);
578 glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
580 uniform_tex = glGetUniformLocation(derivatives_program, "tex");
583 void Derivatives::exec(GLuint input_tex, GLuint I_x_y_tex, GLuint beta_0_tex, int level_width, int level_height)
585 glUseProgram(derivatives_program);
587 bind_sampler(derivatives_program, uniform_tex, 0, input_tex, nearest_sampler);
589 glViewport(0, 0, level_width, level_height);
591 glBindVertexArray(derivatives_vao);
592 fbos.render_to(I_x_y_tex, beta_0_tex);
593 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
596 // Calculate the smoothness constraints between neighboring pixels;
597 // s_x(x,y) stores smoothness between pixel (x,y) and (x+1,y),
598 // and s_y(x,y) stores between (x,y) and (x,y+1). We'll sample with
599 // border color (0,0) later, so that there's zero diffusion out of
602 // See variational_refinement.txt for more information.
603 class ComputeSmoothness {
606 void exec(GLuint flow_tex, GLuint diff_flow_tex, GLuint smoothness_x_tex, GLuint smoothness_y_tex, int level_width, int level_height);
609 PersistentFBOSet<2> fbos;
611 GLuint smoothness_vs_obj;
612 GLuint smoothness_fs_obj;
613 GLuint smoothness_program;
614 GLuint smoothness_vao;
616 GLuint uniform_flow_tex, uniform_diff_flow_tex;
617 GLuint uniform_alpha;
620 ComputeSmoothness::ComputeSmoothness()
622 smoothness_vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER);
623 smoothness_fs_obj = compile_shader(read_file("smoothness.frag"), GL_FRAGMENT_SHADER);
624 smoothness_program = link_program(smoothness_vs_obj, smoothness_fs_obj);
626 // Set up the VAO containing all the required position/texcoord data.
627 glCreateVertexArrays(1, &smoothness_vao);
628 glBindVertexArray(smoothness_vao);
629 glBindBuffer(GL_ARRAY_BUFFER, vertex_vbo);
631 GLint position_attrib = glGetAttribLocation(smoothness_program, "position");
632 glEnableVertexArrayAttrib(smoothness_vao, position_attrib);
633 glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
635 uniform_flow_tex = glGetUniformLocation(smoothness_program, "flow_tex");
636 uniform_diff_flow_tex = glGetUniformLocation(smoothness_program, "diff_flow_tex");
637 uniform_alpha = glGetUniformLocation(smoothness_program, "alpha");
640 void ComputeSmoothness::exec(GLuint flow_tex, GLuint diff_flow_tex, GLuint smoothness_x_tex, GLuint smoothness_y_tex, int level_width, int level_height)
642 glUseProgram(smoothness_program);
644 bind_sampler(smoothness_program, uniform_flow_tex, 0, flow_tex, nearest_sampler);
645 bind_sampler(smoothness_program, uniform_diff_flow_tex, 1, diff_flow_tex, nearest_sampler);
646 glProgramUniform1f(smoothness_program, uniform_alpha, vr_alpha);
648 glViewport(0, 0, level_width, level_height);
651 glBindVertexArray(smoothness_vao);
652 fbos.render_to(smoothness_x_tex, smoothness_y_tex);
653 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
655 // Make sure the smoothness on the right and upper borders is zero.
656 // We could have done this by making (W-1)xH and Wx(H-1) textures instead
657 // (we're sampling smoothness with all-zero border color), but we'd
658 // have to adjust the sampling coordinates, which is annoying.
659 glClearTexSubImage(smoothness_x_tex, 0, level_width - 1, 0, 0, 1, level_height, 1, GL_RED, GL_FLOAT, nullptr);
660 glClearTexSubImage(smoothness_y_tex, 0, 0, level_height - 1, 0, level_width, 1, 1, GL_RED, GL_FLOAT, nullptr);
663 // Set up the equations set (two equations in two unknowns, per pixel).
664 // We store five floats; the three non-redundant elements of the 2x2 matrix (A)
665 // as 32-bit floats, and the two elements on the right-hand side (b) as 16-bit
666 // floats. (Actually, we store the inverse of the diagonal elements, because
667 // we only ever need to divide by them.) This fits into four u32 values;
668 // R, G, B for the matrix (the last element is symmetric) and A for the two b values.
669 // All the values of the energy term (E_I, E_G, E_S), except the smoothness
670 // terms that depend on other pixels, are calculated in one pass.
672 // See variational_refinement.txt for more information.
673 class SetupEquations {
676 void exec(GLuint I_x_y_tex, GLuint I_t_tex, GLuint diff_flow_tex, GLuint flow_tex, GLuint beta_0_tex, GLuint smoothness_x_tex, GLuint smoothness_y_tex, GLuint equation_tex, int level_width, int level_height);
679 PersistentFBOSet<1> fbos;
681 GLuint equations_vs_obj;
682 GLuint equations_fs_obj;
683 GLuint equations_program;
684 GLuint equations_vao;
686 GLuint uniform_I_x_y_tex, uniform_I_t_tex;
687 GLuint uniform_diff_flow_tex, uniform_base_flow_tex;
688 GLuint uniform_beta_0_tex;
689 GLuint uniform_smoothness_x_tex, uniform_smoothness_y_tex;
690 GLuint uniform_gamma, uniform_delta;
693 SetupEquations::SetupEquations()
695 equations_vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER);
696 equations_fs_obj = compile_shader(read_file("equations.frag"), GL_FRAGMENT_SHADER);
697 equations_program = link_program(equations_vs_obj, equations_fs_obj);
699 // Set up the VAO containing all the required position/texcoord data.
700 glCreateVertexArrays(1, &equations_vao);
701 glBindVertexArray(equations_vao);
702 glBindBuffer(GL_ARRAY_BUFFER, vertex_vbo);
704 GLint position_attrib = glGetAttribLocation(equations_program, "position");
705 glEnableVertexArrayAttrib(equations_vao, position_attrib);
706 glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
708 uniform_I_x_y_tex = glGetUniformLocation(equations_program, "I_x_y_tex");
709 uniform_I_t_tex = glGetUniformLocation(equations_program, "I_t_tex");
710 uniform_diff_flow_tex = glGetUniformLocation(equations_program, "diff_flow_tex");
711 uniform_base_flow_tex = glGetUniformLocation(equations_program, "base_flow_tex");
712 uniform_beta_0_tex = glGetUniformLocation(equations_program, "beta_0_tex");
713 uniform_smoothness_x_tex = glGetUniformLocation(equations_program, "smoothness_x_tex");
714 uniform_smoothness_y_tex = glGetUniformLocation(equations_program, "smoothness_y_tex");
715 uniform_gamma = glGetUniformLocation(equations_program, "gamma");
716 uniform_delta = glGetUniformLocation(equations_program, "delta");
719 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)
721 glUseProgram(equations_program);
723 bind_sampler(equations_program, uniform_I_x_y_tex, 0, I_x_y_tex, nearest_sampler);
724 bind_sampler(equations_program, uniform_I_t_tex, 1, I_t_tex, nearest_sampler);
725 bind_sampler(equations_program, uniform_diff_flow_tex, 2, diff_flow_tex, nearest_sampler);
726 bind_sampler(equations_program, uniform_base_flow_tex, 3, base_flow_tex, nearest_sampler);
727 bind_sampler(equations_program, uniform_beta_0_tex, 4, beta_0_tex, nearest_sampler);
728 bind_sampler(equations_program, uniform_smoothness_x_tex, 5, smoothness_x_tex, smoothness_sampler);
729 bind_sampler(equations_program, uniform_smoothness_y_tex, 6, smoothness_y_tex, smoothness_sampler);
730 glProgramUniform1f(equations_program, uniform_delta, vr_delta);
731 glProgramUniform1f(equations_program, uniform_gamma, vr_gamma);
733 glViewport(0, 0, level_width, level_height);
735 glBindVertexArray(equations_vao);
736 fbos.render_to(equation_tex);
737 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
740 // Actually solve the equation sets made by SetupEquations, by means of
741 // successive over-relaxation (SOR).
743 // See variational_refinement.txt for more information.
747 void exec(GLuint diff_flow_tex, GLuint equation_tex, GLuint smoothness_x_tex, GLuint smoothness_y_tex, int level_width, int level_height, int num_iterations);
750 PersistentFBOSet<1> fbos;
757 GLuint uniform_diff_flow_tex;
758 GLuint uniform_equation_tex;
759 GLuint uniform_smoothness_x_tex, uniform_smoothness_y_tex;
764 sor_vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER);
765 sor_fs_obj = compile_shader(read_file("sor.frag"), GL_FRAGMENT_SHADER);
766 sor_program = link_program(sor_vs_obj, sor_fs_obj);
768 // Set up the VAO containing all the required position/texcoord data.
769 glCreateVertexArrays(1, &sor_vao);
770 glBindVertexArray(sor_vao);
771 glBindBuffer(GL_ARRAY_BUFFER, vertex_vbo);
773 GLint position_attrib = glGetAttribLocation(sor_program, "position");
774 glEnableVertexArrayAttrib(sor_vao, position_attrib);
775 glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
777 uniform_diff_flow_tex = glGetUniformLocation(sor_program, "diff_flow_tex");
778 uniform_equation_tex = glGetUniformLocation(sor_program, "equation_tex");
779 uniform_smoothness_x_tex = glGetUniformLocation(sor_program, "smoothness_x_tex");
780 uniform_smoothness_y_tex = glGetUniformLocation(sor_program, "smoothness_y_tex");
783 void SOR::exec(GLuint diff_flow_tex, GLuint equation_tex, GLuint smoothness_x_tex, GLuint smoothness_y_tex, int level_width, int level_height, int num_iterations)
785 glUseProgram(sor_program);
787 bind_sampler(sor_program, uniform_diff_flow_tex, 0, diff_flow_tex, nearest_sampler);
788 bind_sampler(sor_program, uniform_smoothness_x_tex, 1, smoothness_x_tex, smoothness_sampler);
789 bind_sampler(sor_program, uniform_smoothness_y_tex, 2, smoothness_y_tex, smoothness_sampler);
790 bind_sampler(sor_program, uniform_equation_tex, 3, equation_tex, nearest_sampler);
792 glViewport(0, 0, level_width, level_height);
794 glBindVertexArray(sor_vao);
795 fbos.render_to(diff_flow_tex); // NOTE: Bind to same as we render from!
797 for (int i = 0; i < num_iterations; ++i) {
798 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
799 if (i != num_iterations - 1) {
805 // Simply add the differential flow found by the variational refinement to the base flow.
806 // The output is in base_flow_tex; we don't need to make a new texture.
810 void exec(GLuint base_flow_tex, GLuint diff_flow_tex, int level_width, int level_height);
813 PersistentFBOSet<1> fbos;
815 GLuint add_flow_vs_obj;
816 GLuint add_flow_fs_obj;
817 GLuint add_flow_program;
820 GLuint uniform_diff_flow_tex;
823 AddBaseFlow::AddBaseFlow()
825 add_flow_vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER);
826 add_flow_fs_obj = compile_shader(read_file("add_base_flow.frag"), GL_FRAGMENT_SHADER);
827 add_flow_program = link_program(add_flow_vs_obj, add_flow_fs_obj);
829 // Set up the VAO containing all the required position/texcoord data.
830 glCreateVertexArrays(1, &add_flow_vao);
831 glBindVertexArray(add_flow_vao);
832 glBindBuffer(GL_ARRAY_BUFFER, vertex_vbo);
834 GLint position_attrib = glGetAttribLocation(add_flow_program, "position");
835 glEnableVertexArrayAttrib(add_flow_vao, position_attrib);
836 glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
838 uniform_diff_flow_tex = glGetUniformLocation(add_flow_program, "diff_flow_tex");
841 void AddBaseFlow::exec(GLuint base_flow_tex, GLuint diff_flow_tex, int level_width, int level_height)
843 glUseProgram(add_flow_program);
845 bind_sampler(add_flow_program, uniform_diff_flow_tex, 0, diff_flow_tex, nearest_sampler);
847 glViewport(0, 0, level_width, level_height);
849 glBlendFunc(GL_ONE, GL_ONE);
850 glBindVertexArray(add_flow_vao);
851 fbos.render_to(base_flow_tex);
853 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
856 // Take a copy of the flow, bilinearly interpolated and scaled up.
860 void exec(GLuint in_tex, GLuint out_tex, int input_width, int input_height, int output_width, int output_height);
863 PersistentFBOSet<1> fbos;
865 GLuint resize_flow_vs_obj;
866 GLuint resize_flow_fs_obj;
867 GLuint resize_flow_program;
868 GLuint resize_flow_vao;
870 GLuint uniform_flow_tex;
871 GLuint uniform_scale_factor;
874 ResizeFlow::ResizeFlow()
876 resize_flow_vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER);
877 resize_flow_fs_obj = compile_shader(read_file("resize_flow.frag"), GL_FRAGMENT_SHADER);
878 resize_flow_program = link_program(resize_flow_vs_obj, resize_flow_fs_obj);
880 // Set up the VAO containing all the required position/texcoord data.
881 glCreateVertexArrays(1, &resize_flow_vao);
882 glBindVertexArray(resize_flow_vao);
883 glBindBuffer(GL_ARRAY_BUFFER, vertex_vbo);
885 GLint position_attrib = glGetAttribLocation(resize_flow_program, "position");
886 glEnableVertexArrayAttrib(resize_flow_vao, position_attrib);
887 glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
889 uniform_flow_tex = glGetUniformLocation(resize_flow_program, "flow_tex");
890 uniform_scale_factor = glGetUniformLocation(resize_flow_program, "scale_factor");
893 void ResizeFlow::exec(GLuint flow_tex, GLuint out_tex, int input_width, int input_height, int output_width, int output_height)
895 glUseProgram(resize_flow_program);
897 bind_sampler(resize_flow_program, uniform_flow_tex, 0, flow_tex, nearest_sampler);
899 glProgramUniform2f(resize_flow_program, uniform_scale_factor, float(output_width) / input_width, float(output_height) / input_height);
901 glViewport(0, 0, output_width, output_height);
903 glBindVertexArray(resize_flow_vao);
904 fbos.render_to(out_tex);
906 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
912 pair<GLuint, GLuint> begin_timer(const string &name, int level);
918 pair<GLuint, GLuint> query;
920 vector<Timer> timers;
923 pair<GLuint, GLuint> GPUTimers::begin_timer(const string &name, int level)
926 glGenQueries(2, queries);
927 glQueryCounter(queries[0], GL_TIMESTAMP);
932 timer.query.first = queries[0];
933 timer.query.second = queries[1];
934 timers.push_back(timer);
938 void GPUTimers::print()
940 for (const Timer &timer : timers) {
941 // NOTE: This makes the CPU wait for the GPU.
942 GLuint64 time_start, time_end;
943 glGetQueryObjectui64v(timer.query.first, GL_QUERY_RESULT, &time_start);
944 glGetQueryObjectui64v(timer.query.second, GL_QUERY_RESULT, &time_end);
945 //fprintf(stderr, "GPU time used = %.1f ms\n", time_elapsed / 1e6);
946 for (int i = 0; i < timer.level * 2; ++i) {
947 fprintf(stderr, " ");
949 fprintf(stderr, "%-30s %4.1f ms\n", timer.name.c_str(), GLint64(time_end - time_start) / 1e6);
953 // A simple RAII class for timing until the end of the scope.
956 ScopedTimer(const string &name, GPUTimers *timers)
957 : timers(timers), level(0)
959 query = timers->begin_timer(name, level);
962 ScopedTimer(const string &name, ScopedTimer *parent_timer)
963 : timers(parent_timer->timers),
964 level(parent_timer->level + 1)
966 query = timers->begin_timer(name, level);
977 glQueryCounter(query.second, GL_TIMESTAMP);
985 pair<GLuint, GLuint> query;
989 int main(int argc, char **argv)
991 static const option long_options[] = {
992 { "alpha", required_argument, 0, 'a' },
993 { "delta", required_argument, 0, 'd' },
994 { "gamma", required_argument, 0, 'g' }
998 int option_index = 0;
999 int c = getopt_long(argc, argv, "a:d:g:", long_options, &option_index);
1006 vr_alpha = atof(optarg);
1009 vr_delta = atof(optarg);
1012 vr_gamma = atof(optarg);
1015 fprintf(stderr, "Unknown option '%s'\n", argv[option_index]);
1020 if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
1021 fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
1024 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
1025 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
1026 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
1027 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
1029 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
1030 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
1031 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
1032 // SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
1033 SDL_Window *window = SDL_CreateWindow("OpenGL window",
1034 SDL_WINDOWPOS_UNDEFINED,
1035 SDL_WINDOWPOS_UNDEFINED,
1038 SDL_GLContext context = SDL_GL_CreateContext(window);
1039 assert(context != nullptr);
1042 unsigned width1, height1, width2, height2;
1043 GLuint tex0 = load_texture(argc >= (optind + 1) ? argv[optind] : "test1499.png", &width1, &height1);
1044 GLuint tex1 = load_texture(argc >= (optind + 2) ? argv[optind + 1] : "test1500.png", &width2, &height2);
1046 if (width1 != width2 || height1 != height2) {
1047 fprintf(stderr, "Image dimensions don't match (%dx%d versus %dx%d)\n",
1048 width1, height1, width2, height2);
1052 // Make some samplers.
1053 glCreateSamplers(1, &nearest_sampler);
1054 glSamplerParameteri(nearest_sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1055 glSamplerParameteri(nearest_sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1056 glSamplerParameteri(nearest_sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1057 glSamplerParameteri(nearest_sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1059 glCreateSamplers(1, &linear_sampler);
1060 glSamplerParameteri(linear_sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1061 glSamplerParameteri(linear_sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1062 glSamplerParameteri(linear_sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1063 glSamplerParameteri(linear_sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1065 // The smoothness is sampled so that once we get to a smoothness involving
1066 // a value outside the border, the diffusivity between the two becomes zero.
1067 glCreateSamplers(1, &smoothness_sampler);
1068 glSamplerParameteri(smoothness_sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1069 glSamplerParameteri(smoothness_sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1070 glSamplerParameteri(smoothness_sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
1071 glSamplerParameteri(smoothness_sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
1072 float zero[] = { 0.0f, 0.0f, 0.0f, 0.0f };
1073 glSamplerParameterfv(smoothness_sampler, GL_TEXTURE_BORDER_COLOR, zero);
1075 float vertices[] = {
1081 glCreateBuffers(1, &vertex_vbo);
1082 glNamedBufferData(vertex_vbo, sizeof(vertices), vertices, GL_STATIC_DRAW);
1083 glBindBuffer(GL_ARRAY_BUFFER, vertex_vbo);
1085 // Initial flow is zero, 1x1.
1086 GLuint initial_flow_tex;
1087 glCreateTextures(GL_TEXTURE_2D, 1, &initial_flow_tex);
1088 glTextureStorage2D(initial_flow_tex, 1, GL_RG16F, 1, 1);
1089 glClearTexImage(initial_flow_tex, 0, GL_RG, GL_FLOAT, nullptr);
1090 int prev_level_width = 1, prev_level_height = 1;
1092 GLuint prev_level_flow_tex = initial_flow_tex;
1095 MotionSearch motion_search;
1098 Derivatives derivatives;
1099 ComputeSmoothness compute_smoothness;
1100 SetupEquations setup_equations;
1102 AddBaseFlow add_base_flow;
1103 ResizeFlow resize_flow;
1106 glGenQueries(1, &query);
1107 glBeginQuery(GL_TIME_ELAPSED, query);
1111 ScopedTimer total_timer("Total", &timers);
1112 for (int level = coarsest_level; level >= int(finest_level); --level) {
1113 char timer_name[256];
1114 snprintf(timer_name, sizeof(timer_name), "Level %d", level);
1115 ScopedTimer level_timer(timer_name, &total_timer);
1117 int level_width = width1 >> level;
1118 int level_height = height1 >> level;
1119 float patch_spacing_pixels = patch_size_pixels * (1.0f - patch_overlap_ratio);
1120 int width_patches = 1 + lrintf((level_width - patch_size_pixels) / patch_spacing_pixels);
1121 int height_patches = 1 + lrintf((level_height - patch_size_pixels) / patch_spacing_pixels);
1123 // Make sure we always read from the correct level; the chosen
1124 // mipmapping could otherwise be rather unpredictable, especially
1125 // during motion search.
1126 // TODO: create these beforehand, and stop leaking them.
1127 GLuint tex0_view, tex1_view;
1128 glGenTextures(1, &tex0_view);
1129 glTextureView(tex0_view, GL_TEXTURE_2D, tex0, GL_R8, level, 1, 0, 1);
1130 glGenTextures(1, &tex1_view);
1131 glTextureView(tex1_view, GL_TEXTURE_2D, tex1, GL_R8, level, 1, 0, 1);
1133 // Create a new texture; we could be fancy and render use a multi-level
1134 // texture, but meh.
1136 glCreateTextures(GL_TEXTURE_2D, 1, &grad0_tex);
1137 glTextureStorage2D(grad0_tex, 1, GL_RG16F, level_width, level_height);
1139 // Find the derivative.
1141 ScopedTimer timer("Sobel", &level_timer);
1142 sobel.exec(tex0_view, grad0_tex, level_width, level_height);
1145 // Motion search to find the initial flow. We use the flow from the previous
1146 // level (sampled bilinearly; no fancy tricks) as a guide, then search from there.
1148 // Create an output flow texture.
1149 GLuint flow_out_tex;
1150 glCreateTextures(GL_TEXTURE_2D, 1, &flow_out_tex);
1151 glTextureStorage2D(flow_out_tex, 1, GL_RGB16F, width_patches, height_patches);
1155 ScopedTimer timer("Motion search", &level_timer);
1156 motion_search.exec(tex0_view, tex1_view, grad0_tex, prev_level_flow_tex, flow_out_tex, level_width, level_height, prev_level_width, prev_level_height, width_patches, height_patches);
1161 // Set up an output texture (initially zero).
1162 GLuint dense_flow_tex;
1163 glCreateTextures(GL_TEXTURE_2D, 1, &dense_flow_tex);
1164 glTextureStorage2D(dense_flow_tex, 1, GL_RGB16F, level_width, level_height);
1165 glClearTexImage(dense_flow_tex, 0, GL_RGB, GL_FLOAT, nullptr);
1169 ScopedTimer timer("Densification", &level_timer);
1170 densify.exec(tex0_view, tex1_view, flow_out_tex, dense_flow_tex, level_width, level_height, width_patches, height_patches);
1173 // Everything below here in the loop belongs to variational refinement.
1174 ScopedTimer varref_timer("Variational refinement", &level_timer);
1176 // Prewarping; create I and I_t, and a normalized base flow (so we don't
1177 // have to normalize it over and over again, and also save some bandwidth).
1179 // During the entire rest of the variational refinement, flow will be measured
1180 // in pixels, not 0..1 normalized OpenGL texture coordinates.
1181 // This is because variational refinement depends so heavily on derivatives,
1182 // which are measured in intensity levels per pixel.
1183 GLuint I_tex, I_t_tex, base_flow_tex;
1184 glCreateTextures(GL_TEXTURE_2D, 1, &I_tex);
1185 glCreateTextures(GL_TEXTURE_2D, 1, &I_t_tex);
1186 glCreateTextures(GL_TEXTURE_2D, 1, &base_flow_tex);
1187 glTextureStorage2D(I_tex, 1, GL_R16F, level_width, level_height);
1188 glTextureStorage2D(I_t_tex, 1, GL_R16F, level_width, level_height);
1189 glTextureStorage2D(base_flow_tex, 1, GL_RG16F, level_width, level_height);
1191 ScopedTimer timer("Prewarping", &varref_timer);
1192 prewarp.exec(tex0_view, tex1_view, dense_flow_tex, I_tex, I_t_tex, base_flow_tex, level_width, level_height);
1195 // Calculate I_x and I_y. We're only calculating first derivatives;
1196 // the others will be taken on-the-fly in order to sample from fewer
1197 // textures overall, since sampling from the L1 cache is cheap.
1198 // (TODO: Verify that this is indeed faster than making separate
1199 // double-derivative textures.)
1200 GLuint I_x_y_tex, beta_0_tex;
1201 glCreateTextures(GL_TEXTURE_2D, 1, &I_x_y_tex);
1202 glCreateTextures(GL_TEXTURE_2D, 1, &beta_0_tex);
1203 glTextureStorage2D(I_x_y_tex, 1, GL_RG16F, level_width, level_height);
1204 glTextureStorage2D(beta_0_tex, 1, GL_R16F, level_width, level_height);
1206 ScopedTimer timer("First derivatives", &varref_timer);
1207 derivatives.exec(I_tex, I_x_y_tex, beta_0_tex, level_width, level_height);
1210 // We need somewhere to store du and dv (the flow increment, relative
1211 // to the non-refined base flow u0 and v0). It starts at zero.
1213 glCreateTextures(GL_TEXTURE_2D, 1, &du_dv_tex);
1214 glTextureStorage2D(du_dv_tex, 1, GL_RG16F, level_width, level_height);
1215 glClearTexImage(du_dv_tex, 0, GL_RG, GL_FLOAT, nullptr);
1217 // And for smoothness.
1218 GLuint smoothness_x_tex, smoothness_y_tex;
1219 glCreateTextures(GL_TEXTURE_2D, 1, &smoothness_x_tex);
1220 glCreateTextures(GL_TEXTURE_2D, 1, &smoothness_y_tex);
1221 glTextureStorage2D(smoothness_x_tex, 1, GL_R16F, level_width, level_height);
1222 glTextureStorage2D(smoothness_y_tex, 1, GL_R16F, level_width, level_height);
1224 // And finally for the equation set. See SetupEquations for
1225 // the storage format.
1226 GLuint equation_tex;
1227 glCreateTextures(GL_TEXTURE_2D, 1, &equation_tex);
1228 glTextureStorage2D(equation_tex, 1, GL_RGBA32UI, level_width, level_height);
1230 for (int outer_idx = 0; outer_idx < level + 1; ++outer_idx) {
1231 // Calculate the smoothness terms between the neighboring pixels,
1232 // both in x and y direction.
1234 ScopedTimer timer("Compute smoothness", &varref_timer);
1235 compute_smoothness.exec(base_flow_tex, du_dv_tex, smoothness_x_tex, smoothness_y_tex, level_width, level_height);
1238 // Set up the 2x2 equation system for each pixel.
1240 ScopedTimer timer("Set up equations", &varref_timer);
1241 setup_equations.exec(I_x_y_tex, I_t_tex, du_dv_tex, base_flow_tex, beta_0_tex, smoothness_x_tex, smoothness_y_tex, equation_tex, level_width, level_height);
1244 // Run a few SOR (or quasi-SOR, since we're not really Jacobi) iterations.
1245 // Note that these are to/from the same texture.
1247 ScopedTimer timer("SOR", &varref_timer);
1248 sor.exec(du_dv_tex, equation_tex, smoothness_x_tex, smoothness_y_tex, level_width, level_height, 5);
1252 // Add the differential flow found by the variational refinement to the base flow,
1253 // giving the final flow estimate for this level.
1254 // The output is in diff_flow_tex; we don't need to make a new texture.
1255 // You can comment out this part if you wish to test disabling of the variational refinement.
1257 ScopedTimer timer("Add differential flow", &varref_timer);
1258 add_base_flow.exec(base_flow_tex, du_dv_tex, level_width, level_height);
1261 prev_level_flow_tex = base_flow_tex;
1262 prev_level_width = level_width;
1263 prev_level_height = level_height;
1269 // Scale up the flow to the final size (if needed).
1271 if (finest_level == 0) {
1272 final_tex = prev_level_flow_tex;
1274 glCreateTextures(GL_TEXTURE_2D, 1, &final_tex);
1275 glTextureStorage2D(final_tex, 1, GL_RG16F, width1, height1);
1276 resize_flow.exec(prev_level_flow_tex, final_tex, prev_level_width, prev_level_height, width1, height1);
1279 unique_ptr<float[]> dense_flow(new float[width1 * height1 * 2]);
1280 glGetTextureImage(final_tex, 0, GL_RG, GL_FLOAT, width1 * height1 * 2 * sizeof(float), dense_flow.get());
1282 FILE *fp = fopen("flow.ppm", "wb");
1283 FILE *flowfp = fopen("flow.flo", "wb");
1284 fprintf(fp, "P6\n%d %d\n255\n", width1, height1);
1285 fprintf(flowfp, "FEIH");
1286 fwrite(&width1, 4, 1, flowfp);
1287 fwrite(&height1, 4, 1, flowfp);
1288 for (unsigned y = 0; y < unsigned(height1); ++y) {
1289 int yy = height1 - y - 1;
1290 for (unsigned x = 0; x < unsigned(width1); ++x) {
1291 float du = dense_flow[(yy * width1 + x) * 2 + 0];
1292 float dv = dense_flow[(yy * width1 + x) * 2 + 1];
1296 fwrite(&du, 4, 1, flowfp);
1297 fwrite(&dv, 4, 1, flowfp);
1300 flow2rgb(du, dv, &r, &g, &b);
1309 fprintf(stderr, "err = %d\n", glGetError());