X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=flow.cpp;h=5125d26b53a5d2f09bde21132136d2a6ec333b5c;hb=3795723be95f2fe82f3c8b8b45b1a905b2c811fd;hp=a38006ca9ca4ad7b25c50535b54c52e5977b10cf;hpb=728b20a90b39b96eb5ac5ecd17cd5c3cbaac7009;p=nageru diff --git a/flow.cpp b/flow.cpp index a38006c..5125d26 100644 --- a/flow.cpp +++ b/flow.cpp @@ -1,37 +1,28 @@ #define NO_SDL_GLEXT 1 -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - #include "flow.h" + +#include "embedded_files.h" #include "gpu_timers.h" #include "util.h" #include +#include #include -#include +#include +#include #include +#include #include +#include +#include +#include #include #define BUFFER_OFFSET(i) ((char *)nullptr + (i)) using namespace std; -SDL_Window *window; - // 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. @@ -39,29 +30,15 @@ SDL_Window *window; // although the error (EPE) seems to be fairly insensitive to the precise values. // Only the relative values matter, so we fix alpha (the smoothness constant) // at unity and tweak the others. -static float vr_alpha = 1.0f, vr_delta = 0.25f, vr_gamma = 0.25f; - -bool enable_timing = true; -bool detailed_timing = false; -bool enable_warmup = false; -bool in_warmup = false; -bool enable_variational_refinement = true; // Just for debugging. -bool enable_interpolation = false; +// +// TODO: Maybe this should not be global. +float vr_alpha = 1.0f, vr_delta = 0.25f, vr_gamma = 0.25f; // Some global OpenGL objects. // TODO: These should really be part of DISComputeFlow. GLuint nearest_sampler, linear_sampler, zero_border_sampler; GLuint vertex_vbo; -// Structures for asynchronous readback. We assume everything is the same size (and GL_RG16F). -struct ReadInProgress { - GLuint pbo; - string filename0, filename1; - string flow_filename, ppm_filename; // Either may be empty for no write. -}; -stack spare_pbos; -deque reads_in_progress; - int find_num_levels(int width, int height) { int levels = 1; @@ -73,10 +50,18 @@ int find_num_levels(int width, int height) return levels; } -string read_file(const string &filename) +string read_file(const string &filename, const unsigned char *start = nullptr, const size_t size = 0) { FILE *fp = fopen(filename.c_str(), "r"); if (fp == nullptr) { + // Fall back to the version we compiled in. (We prefer disk if we can, + // since that makes it possible to work on shaders without recompiling + // all the time.) + if (start != nullptr) { + return string(reinterpret_cast(start), + reinterpret_cast(start) + size); + } + perror(filename.c_str()); exit(1); } @@ -87,7 +72,7 @@ string read_file(const string &filename) exit(1); } - int size = ftell(fp); + int disk_size = ftell(fp); ret = fseek(fp, 0, SEEK_SET); if (ret == -1) { @@ -96,15 +81,15 @@ string read_file(const string &filename) } string str; - str.resize(size); - ret = fread(&str[0], size, 1, fp); + str.resize(disk_size); + ret = fread(&str[0], disk_size, 1, fp); if (ret == -1) { perror("fread"); exit(1); } if (ret == 0) { fprintf(stderr, "Short read when trying to read %d bytes from %s\n", - size, filename.c_str()); + disk_size, filename.c_str()); exit(1); } fclose(fp); @@ -112,11 +97,10 @@ string read_file(const string &filename) return str; } - GLuint compile_shader(const string &shader_src, GLenum type) { GLuint obj = glCreateShader(type); - const GLchar* source[] = { shader_src.data() }; + const GLchar *source[] = { shader_src.data() }; const GLint length[] = { (GLint)shader_src.size() }; glShaderSource(obj, 1, source, length); glCompileShader(obj); @@ -151,57 +135,6 @@ GLuint compile_shader(const string &shader_src, GLenum type) return obj; } -enum MipmapPolicy { - WITHOUT_MIPMAPS, - WITH_MIPMAPS -}; - -GLuint load_texture(const char *filename, unsigned *width_ret, unsigned *height_ret, MipmapPolicy mipmaps) -{ - SDL_Surface *surf = IMG_Load(filename); - if (surf == nullptr) { - fprintf(stderr, "IMG_Load(%s): %s\n", filename, IMG_GetError()); - exit(1); - } - - // For whatever reason, SDL doesn't support converting to YUV surfaces - // nor grayscale, so we'll do it ourselves. - SDL_Surface *rgb_surf = SDL_ConvertSurfaceFormat(surf, SDL_PIXELFORMAT_RGBA32, /*flags=*/0); - if (rgb_surf == nullptr) { - fprintf(stderr, "SDL_ConvertSurfaceFormat(%s): %s\n", filename, SDL_GetError()); - exit(1); - } - - SDL_FreeSurface(surf); - - unsigned width = rgb_surf->w, height = rgb_surf->h; - const uint8_t *sptr = (uint8_t *)rgb_surf->pixels; - unique_ptr pix(new uint8_t[width * height * 4]); - - // Extract the Y component, and convert to bottom-left origin. - for (unsigned y = 0; y < height; ++y) { - unsigned y2 = height - 1 - y; - memcpy(pix.get() + y * width * 4, sptr + y2 * rgb_surf->pitch, width * 4); - } - SDL_FreeSurface(rgb_surf); - - int num_levels = (mipmaps == WITH_MIPMAPS) ? find_num_levels(width, height) : 1; - - GLuint tex; - glCreateTextures(GL_TEXTURE_2D, 1, &tex); - glTextureStorage2D(tex, num_levels, GL_RGBA8, width, height); - glTextureSubImage2D(tex, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pix.get()); - - if (mipmaps == WITH_MIPMAPS) { - glGenerateTextureMipmap(tex); - } - - *width_ret = width; - *height_ret = height; - - return tex; -} - GLuint link_program(GLuint vs_obj, GLuint fs_obj) { GLuint program = glCreateProgram(); @@ -279,8 +212,8 @@ void PersistentFBOSetWithDepth::render_to(GLuint depth_rb, const a GrayscaleConversion::GrayscaleConversion() { - gray_vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER); - gray_fs_obj = compile_shader(read_file("gray.frag"), GL_FRAGMENT_SHADER); + gray_vs_obj = compile_shader(read_file("vs.vert", _binary_vs_vert_data, _binary_vs_vert_size), GL_VERTEX_SHADER); + gray_fs_obj = compile_shader(read_file("gray.frag", _binary_gray_frag_data, _binary_gray_frag_size), GL_FRAGMENT_SHADER); gray_program = link_program(gray_vs_obj, gray_fs_obj); // Set up the VAO containing all the required position/texcoord data. @@ -308,8 +241,8 @@ void GrayscaleConversion::exec(GLint tex, GLint gray_tex, int width, int height, Sobel::Sobel() { - sobel_vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER); - sobel_fs_obj = compile_shader(read_file("sobel.frag"), GL_FRAGMENT_SHADER); + sobel_vs_obj = compile_shader(read_file("vs.vert", _binary_vs_vert_data, _binary_vs_vert_size), GL_VERTEX_SHADER); + sobel_fs_obj = compile_shader(read_file("sobel.frag", _binary_sobel_frag_data, _binary_sobel_frag_size), GL_FRAGMENT_SHADER); sobel_program = link_program(sobel_vs_obj, sobel_fs_obj); uniform_tex = glGetUniformLocation(sobel_program, "tex"); @@ -329,8 +262,8 @@ void Sobel::exec(GLint tex_view, GLint grad_tex, int level_width, int level_heig MotionSearch::MotionSearch(const OperatingPoint &op) : op(op) { - motion_vs_obj = compile_shader(read_file("motion_search.vert"), GL_VERTEX_SHADER); - motion_fs_obj = compile_shader(read_file("motion_search.frag"), GL_FRAGMENT_SHADER); + motion_vs_obj = compile_shader(read_file("motion_search.vert", _binary_motion_search_vert_data, _binary_motion_search_vert_size), GL_VERTEX_SHADER); + motion_fs_obj = compile_shader(read_file("motion_search.frag", _binary_motion_search_frag_data, _binary_motion_search_frag_size), GL_FRAGMENT_SHADER); motion_search_program = link_program(motion_vs_obj, motion_fs_obj); uniform_inv_image_size = glGetUniformLocation(motion_search_program, "inv_image_size"); @@ -365,8 +298,8 @@ void MotionSearch::exec(GLuint tex_view, GLuint grad_tex, GLuint flow_tex, GLuin Densify::Densify(const OperatingPoint &op) : op(op) { - densify_vs_obj = compile_shader(read_file("densify.vert"), GL_VERTEX_SHADER); - densify_fs_obj = compile_shader(read_file("densify.frag"), GL_FRAGMENT_SHADER); + densify_vs_obj = compile_shader(read_file("densify.vert", _binary_densify_vert_data, _binary_densify_vert_size), GL_VERTEX_SHADER); + densify_fs_obj = compile_shader(read_file("densify.frag", _binary_densify_frag_data, _binary_densify_frag_size), GL_FRAGMENT_SHADER); densify_program = link_program(densify_vs_obj, densify_fs_obj); uniform_patch_size = glGetUniformLocation(densify_program, "patch_size"); @@ -396,8 +329,8 @@ void Densify::exec(GLuint tex_view, GLuint flow_tex, GLuint dense_flow_tex, int Prewarp::Prewarp() { - prewarp_vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER); - prewarp_fs_obj = compile_shader(read_file("prewarp.frag"), GL_FRAGMENT_SHADER); + prewarp_vs_obj = compile_shader(read_file("vs.vert", _binary_vs_vert_data, _binary_vs_vert_size), GL_VERTEX_SHADER); + prewarp_fs_obj = compile_shader(read_file("prewarp.frag", _binary_prewarp_frag_data, _binary_prewarp_frag_size), GL_FRAGMENT_SHADER); prewarp_program = link_program(prewarp_vs_obj, prewarp_fs_obj); uniform_image_tex = glGetUniformLocation(prewarp_program, "image_tex"); @@ -419,8 +352,8 @@ void Prewarp::exec(GLuint tex_view, GLuint flow_tex, GLuint I_tex, GLuint I_t_te Derivatives::Derivatives() { - derivatives_vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER); - derivatives_fs_obj = compile_shader(read_file("derivatives.frag"), GL_FRAGMENT_SHADER); + derivatives_vs_obj = compile_shader(read_file("vs.vert", _binary_vs_vert_data, _binary_vs_vert_size), GL_VERTEX_SHADER); + derivatives_fs_obj = compile_shader(read_file("derivatives.frag", _binary_derivatives_frag_data, _binary_derivatives_frag_size), GL_FRAGMENT_SHADER); derivatives_program = link_program(derivatives_vs_obj, derivatives_fs_obj); uniform_tex = glGetUniformLocation(derivatives_program, "tex"); @@ -440,8 +373,8 @@ void Derivatives::exec(GLuint input_tex, GLuint I_x_y_tex, GLuint beta_0_tex, in ComputeDiffusivity::ComputeDiffusivity() { - diffusivity_vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER); - diffusivity_fs_obj = compile_shader(read_file("diffusivity.frag"), GL_FRAGMENT_SHADER); + diffusivity_vs_obj = compile_shader(read_file("vs.vert", _binary_vs_vert_data, _binary_vs_vert_size), GL_VERTEX_SHADER); + diffusivity_fs_obj = compile_shader(read_file("diffusivity.frag", _binary_diffusivity_frag_data, _binary_diffusivity_frag_size), GL_FRAGMENT_SHADER); diffusivity_program = link_program(diffusivity_vs_obj, diffusivity_fs_obj); uniform_flow_tex = glGetUniformLocation(diffusivity_program, "flow_tex"); @@ -468,8 +401,8 @@ void ComputeDiffusivity::exec(GLuint flow_tex, GLuint diff_flow_tex, GLuint diff SetupEquations::SetupEquations() { - equations_vs_obj = compile_shader(read_file("equations.vert"), GL_VERTEX_SHADER); - equations_fs_obj = compile_shader(read_file("equations.frag"), GL_FRAGMENT_SHADER); + equations_vs_obj = compile_shader(read_file("equations.vert", _binary_equations_vert_data, _binary_equations_vert_size), GL_VERTEX_SHADER); + equations_fs_obj = compile_shader(read_file("equations.frag", _binary_equations_frag_data, _binary_equations_frag_size), GL_FRAGMENT_SHADER); equations_program = link_program(equations_vs_obj, equations_fs_obj); uniform_I_x_y_tex = glGetUniformLocation(equations_program, "I_x_y_tex"); @@ -505,8 +438,8 @@ void SetupEquations::exec(GLuint I_x_y_tex, GLuint I_t_tex, GLuint diff_flow_tex SOR::SOR() { - sor_vs_obj = compile_shader(read_file("sor.vert"), GL_VERTEX_SHADER); - sor_fs_obj = compile_shader(read_file("sor.frag"), GL_FRAGMENT_SHADER); + sor_vs_obj = compile_shader(read_file("sor.vert", _binary_sor_vert_data, _binary_sor_vert_size), GL_VERTEX_SHADER); + sor_fs_obj = compile_shader(read_file("sor.frag", _binary_sor_frag_data, _binary_sor_frag_size), GL_FRAGMENT_SHADER); sor_program = link_program(sor_vs_obj, sor_fs_obj); uniform_diff_flow_tex = glGetUniformLocation(sor_program, "diff_flow_tex"); @@ -567,8 +500,8 @@ void SOR::exec(GLuint diff_flow_tex, GLuint equation_red_tex, GLuint equation_bl AddBaseFlow::AddBaseFlow() { - add_flow_vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER); - add_flow_fs_obj = compile_shader(read_file("add_base_flow.frag"), GL_FRAGMENT_SHADER); + add_flow_vs_obj = compile_shader(read_file("vs.vert", _binary_vs_vert_data, _binary_vs_vert_size), GL_VERTEX_SHADER); + add_flow_fs_obj = compile_shader(read_file("add_base_flow.frag", _binary_add_base_flow_frag_data, _binary_add_base_flow_frag_size), GL_FRAGMENT_SHADER); add_flow_program = link_program(add_flow_vs_obj, add_flow_fs_obj); uniform_diff_flow_tex = glGetUniformLocation(add_flow_program, "diff_flow_tex"); @@ -590,8 +523,8 @@ void AddBaseFlow::exec(GLuint base_flow_tex, GLuint diff_flow_tex, int level_wid ResizeFlow::ResizeFlow() { - resize_flow_vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER); - resize_flow_fs_obj = compile_shader(read_file("resize_flow.frag"), GL_FRAGMENT_SHADER); + resize_flow_vs_obj = compile_shader(read_file("vs.vert", _binary_vs_vert_data, _binary_vs_vert_size), GL_VERTEX_SHADER); + resize_flow_fs_obj = compile_shader(read_file("resize_flow.frag", _binary_resize_flow_frag_data, _binary_resize_flow_frag_size), GL_FRAGMENT_SHADER); resize_flow_program = link_program(resize_flow_vs_obj, resize_flow_fs_obj); uniform_flow_tex = glGetUniformLocation(resize_flow_program, "flow_tex"); @@ -674,6 +607,7 @@ GLuint DISComputeFlow::exec(GLuint tex, FlowDirection flow_direction, ResizeStra GPUTimers timers; glBindVertexArray(vao); + glDisable(GL_DITHER); ScopedTimer total_timer("Compute flow", &timers); for (int level = op.coarsest_level; level >= int(op.finest_level); --level) { @@ -846,22 +780,22 @@ GLuint DISComputeFlow::exec(GLuint tex, FlowDirection flow_direction, ResizeStra Splat::Splat(const OperatingPoint &op) : op(op) { - splat_vs_obj = compile_shader(read_file("splat.vert"), GL_VERTEX_SHADER); - splat_fs_obj = compile_shader(read_file("splat.frag"), GL_FRAGMENT_SHADER); + splat_vs_obj = compile_shader(read_file("splat.vert", _binary_splat_vert_data, _binary_splat_vert_size), GL_VERTEX_SHADER); + splat_fs_obj = compile_shader(read_file("splat.frag", _binary_splat_frag_data, _binary_splat_frag_size), GL_FRAGMENT_SHADER); splat_program = link_program(splat_vs_obj, splat_fs_obj); uniform_splat_size = glGetUniformLocation(splat_program, "splat_size"); uniform_alpha = glGetUniformLocation(splat_program, "alpha"); - uniform_image_tex = glGetUniformLocation(splat_program, "image_tex"); + uniform_gray_tex = glGetUniformLocation(splat_program, "gray_tex"); uniform_flow_tex = glGetUniformLocation(splat_program, "flow_tex"); uniform_inv_flow_size = glGetUniformLocation(splat_program, "inv_flow_size"); } -void Splat::exec(GLuint image_tex, GLuint bidirectional_flow_tex, GLuint flow_tex, GLuint depth_rb, int width, int height, float alpha) +void Splat::exec(GLuint gray_tex, GLuint bidirectional_flow_tex, GLuint flow_tex, GLuint depth_rb, int width, int height, float alpha) { glUseProgram(splat_program); - bind_sampler(splat_program, uniform_image_tex, 0, image_tex, linear_sampler); + bind_sampler(splat_program, uniform_gray_tex, 0, gray_tex, linear_sampler); bind_sampler(splat_program, uniform_flow_tex, 1, bidirectional_flow_tex, nearest_sampler); glProgramUniform2f(splat_program, uniform_splat_size, op.splat_size / width, op.splat_size / height); @@ -871,6 +805,7 @@ void Splat::exec(GLuint image_tex, GLuint bidirectional_flow_tex, GLuint flow_te glViewport(0, 0, width, height); glDisable(GL_BLEND); glEnable(GL_DEPTH_TEST); + glDepthMask(GL_TRUE); glDepthFunc(GL_LESS); // We store the difference between I_0 and I_1, where less difference is good. (Default 1.0 is effectively +inf, which always loses.) fbos.render_to(depth_rb, flow_tex); @@ -888,8 +823,8 @@ void Splat::exec(GLuint image_tex, GLuint bidirectional_flow_tex, GLuint flow_te HoleFill::HoleFill() { - fill_vs_obj = compile_shader(read_file("hole_fill.vert"), GL_VERTEX_SHADER); - fill_fs_obj = compile_shader(read_file("hole_fill.frag"), GL_FRAGMENT_SHADER); + fill_vs_obj = compile_shader(read_file("hole_fill.vert", _binary_hole_fill_vert_data, _binary_hole_fill_vert_size), GL_VERTEX_SHADER); + fill_fs_obj = compile_shader(read_file("hole_fill.frag", _binary_hole_fill_frag_data, _binary_hole_fill_frag_size), GL_FRAGMENT_SHADER); fill_program = link_program(fill_vs_obj, fill_fs_obj); uniform_tex = glGetUniformLocation(fill_program, "tex"); @@ -952,8 +887,8 @@ void HoleFill::exec(GLuint flow_tex, GLuint depth_rb, GLuint temp_tex[3], int wi HoleBlend::HoleBlend() { - blend_vs_obj = compile_shader(read_file("hole_fill.vert"), GL_VERTEX_SHADER); // Reuse the vertex shader from the fill. - blend_fs_obj = compile_shader(read_file("hole_blend.frag"), GL_FRAGMENT_SHADER); + blend_vs_obj = compile_shader(read_file("hole_fill.vert", _binary_hole_fill_vert_data, _binary_hole_fill_vert_size), GL_VERTEX_SHADER); // Reuse the vertex shader from the fill. + blend_fs_obj = compile_shader(read_file("hole_blend.frag", _binary_hole_blend_frag_data, _binary_hole_blend_frag_size), GL_FRAGMENT_SHADER); blend_program = link_program(blend_vs_obj, blend_fs_obj); uniform_left_tex = glGetUniformLocation(blend_program, "left_tex"); @@ -988,10 +923,19 @@ void HoleBlend::exec(GLuint flow_tex, GLuint depth_rb, GLuint temp_tex[3], int w glDisable(GL_DEPTH_TEST); } -Blend::Blend() +Blend::Blend(bool split_ycbcr_output) + : split_ycbcr_output(split_ycbcr_output) { - blend_vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER); - blend_fs_obj = compile_shader(read_file("blend.frag"), GL_FRAGMENT_SHADER); + string frag_shader = read_file("blend.frag", _binary_blend_frag_data, _binary_blend_frag_size); + if (split_ycbcr_output) { + // Insert after the first #version line. + size_t offset = frag_shader.find('\n'); + assert(offset != string::npos); + frag_shader = frag_shader.substr(0, offset + 1) + "#define SPLIT_YCBCR_OUTPUT 1\n" + frag_shader.substr(offset + 1); + } + + blend_vs_obj = compile_shader(read_file("vs.vert", _binary_vs_vert_data, _binary_vs_vert_size), GL_VERTEX_SHADER); + blend_fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER); blend_program = link_program(blend_vs_obj, blend_fs_obj); uniform_image_tex = glGetUniformLocation(blend_program, "image_tex"); @@ -1000,7 +944,7 @@ Blend::Blend() uniform_flow_consistency_tolerance = glGetUniformLocation(blend_program, "flow_consistency_tolerance"); } -void Blend::exec(GLuint image_tex, GLuint flow_tex, GLuint output_tex, int level_width, int level_height, float alpha) +void Blend::exec(GLuint image_tex, GLuint flow_tex, GLuint output_tex, GLuint output2_tex, int level_width, int level_height, float alpha) { glUseProgram(blend_program); bind_sampler(blend_program, uniform_image_tex, 0, image_tex, linear_sampler); @@ -1008,13 +952,20 @@ void Blend::exec(GLuint image_tex, GLuint flow_tex, GLuint output_tex, int level glProgramUniform1f(blend_program, uniform_alpha, alpha); glViewport(0, 0, level_width, level_height); - fbos.render_to(output_tex); + if (split_ycbcr_output) { + fbos_split.render_to(output_tex, output2_tex); + } else { + fbos.render_to(output_tex); + } glDisable(GL_BLEND); // A bit ironic, perhaps. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); } -Interpolate::Interpolate(int width, int height, const OperatingPoint &op) - : width(width), height(height), flow_level(op.finest_level), op(op), splat(op) { +Interpolate::Interpolate(const OperatingPoint &op, bool split_ycbcr_output) + : flow_level(op.finest_level), + split_ycbcr_output(split_ycbcr_output), + splat(op), + blend(split_ycbcr_output) { // Set up the vertex data that will be shared between all passes. float vertices[] = { 0.0f, 1.0f, @@ -1034,18 +985,19 @@ Interpolate::Interpolate(int width, int height, const OperatingPoint &op) glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); } -GLuint Interpolate::exec(GLuint image_tex, GLuint bidirectional_flow_tex, GLuint width, GLuint height, float alpha) +pair Interpolate::exec(GLuint image_tex, GLuint gray_tex, GLuint bidirectional_flow_tex, GLuint width, GLuint height, float alpha) { GPUTimers timers; ScopedTimer total_timer("Interpolate", &timers); glBindVertexArray(vao); + glDisable(GL_DITHER); // Pick out the right level to test splatting results on. GLuint tex_view; glGenTextures(1, &tex_view); - glTextureView(tex_view, GL_TEXTURE_2D_ARRAY, image_tex, GL_RGBA8, flow_level, 1, 0, 2); + glTextureView(tex_view, GL_TEXTURE_2D_ARRAY, gray_tex, GL_R8, flow_level, 1, 0, 2); int flow_width = width >> flow_level; int flow_height = height >> flow_level; @@ -1075,10 +1027,20 @@ GLuint Interpolate::exec(GLuint image_tex, GLuint bidirectional_flow_tex, GLuint pool.release_texture(temp_tex[2]); pool.release_renderbuffer(depth_rb); - GLuint output_tex = pool.get_texture(GL_RGBA8, width, height); - { - ScopedTimer timer("Blend", &total_timer); - blend.exec(image_tex, flow_tex, output_tex, width, height, alpha); + GLuint output_tex, output2_tex = 0; + if (split_ycbcr_output) { + output_tex = pool.get_texture(GL_R8, width, height); + output2_tex = pool.get_texture(GL_RG8, width, height); + { + ScopedTimer timer("Blend", &total_timer); + blend.exec(image_tex, flow_tex, output_tex, output2_tex, width, height, alpha); + } + } else { + output_tex = pool.get_texture(GL_RGBA8, width, height); + { + ScopedTimer timer("Blend", &total_timer); + blend.exec(image_tex, flow_tex, output_tex, 0, width, height, alpha); + } } pool.release_texture(flow_tex); total_timer.end(); @@ -1086,16 +1048,19 @@ GLuint Interpolate::exec(GLuint image_tex, GLuint bidirectional_flow_tex, GLuint timers.print(); } - return output_tex; + return make_pair(output_tex, output2_tex); } GLuint TexturePool::get_texture(GLenum format, GLuint width, GLuint height, GLuint num_layers) { - for (Texture &tex : textures) { - if (!tex.in_use && !tex.is_renderbuffer && tex.format == format && - tex.width == width && tex.height == height && tex.num_layers == num_layers) { - tex.in_use = true; - return tex.tex_num; + { + lock_guard lock(mu); + for (Texture &tex : textures) { + if (!tex.in_use && !tex.is_renderbuffer && tex.format == format && + tex.width == width && tex.height == height && tex.num_layers == num_layers) { + tex.in_use = true; + return tex.tex_num; + } } } @@ -1113,17 +1078,23 @@ GLuint TexturePool::get_texture(GLenum format, GLuint width, GLuint height, GLui tex.num_layers = num_layers; tex.in_use = true; tex.is_renderbuffer = false; - textures.push_back(tex); + { + lock_guard lock(mu); + textures.push_back(tex); + } return tex.tex_num; } GLuint TexturePool::get_renderbuffer(GLenum format, GLuint width, GLuint height) { - for (Texture &tex : textures) { - if (!tex.in_use && tex.is_renderbuffer && tex.format == format && - tex.width == width && tex.height == height) { - tex.in_use = true; - return tex.tex_num; + { + lock_guard lock(mu); + for (Texture &tex : textures) { + if (!tex.in_use && tex.is_renderbuffer && tex.format == format && + tex.width == width && tex.height == height) { + tex.in_use = true; + return tex.tex_num; + } } } @@ -1136,12 +1107,16 @@ GLuint TexturePool::get_renderbuffer(GLenum format, GLuint width, GLuint height) tex.height = height; tex.in_use = true; tex.is_renderbuffer = true; - textures.push_back(tex); + { + lock_guard lock(mu); + textures.push_back(tex); + } return tex.tex_num; } void TexturePool::release_texture(GLuint tex_num) { + lock_guard lock(mu); for (Texture &tex : textures) { if (!tex.is_renderbuffer && tex.tex_num == tex_num) { assert(tex.in_use); @@ -1154,6 +1129,7 @@ void TexturePool::release_texture(GLuint tex_num) void TexturePool::release_renderbuffer(GLuint tex_num) { + lock_guard lock(mu); for (Texture &tex : textures) { if (tex.is_renderbuffer && tex.tex_num == tex_num) { assert(tex.in_use); @@ -1163,414 +1139,3 @@ void TexturePool::release_renderbuffer(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]; - } -} - -// Not relevant for RGB. -void flip_coordinate_system(uint8_t *dense_flow, unsigned width, unsigned height) -{ -} - -void write_flow(const char *filename, const float *dense_flow, unsigned width, unsigned height) -{ - FILE *flowfp = fopen(filename, "wb"); - fprintf(flowfp, "FEIH"); - fwrite(&width, 4, 1, flowfp); - fwrite(&height, 4, 1, flowfp); - for (unsigned y = 0; y < height; ++y) { - int yy = height - y - 1; - fwrite(&dense_flow[yy * width * 2], width * 2 * sizeof(float), 1, flowfp); - } - fclose(flowfp); -} - -// Not relevant for RGB. -void write_flow(const char *filename, const uint8_t *dense_flow, unsigned width, unsigned height) -{ - assert(false); -} - -void write_ppm(const char *filename, const float *dense_flow, unsigned width, unsigned height) -{ - FILE *fp = fopen(filename, "wb"); - fprintf(fp, "P6\n%d %d\n255\n", width, height); - for (unsigned y = 0; y < unsigned(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]; - - uint8_t r, g, b; - flow2rgb(du, dv, &r, &g, &b); - putc(r, fp); - putc(g, fp); - putc(b, fp); - } - } - fclose(fp); -} - -void write_ppm(const char *filename, const uint8_t *rgba, unsigned width, unsigned height) -{ - unique_ptr rgb_line(new uint8_t[width * 3 + 1]); - - FILE *fp = fopen(filename, "wb"); - fprintf(fp, "P6\n%d %d\n255\n", width, height); - for (unsigned y = 0; y < height; ++y) { - unsigned y2 = height - 1 - y; - for (size_t x = 0; x < width; ++x) { - memcpy(&rgb_line[x * 3], &rgba[(y2 * width + x) * 4], 4); - } - fwrite(rgb_line.get(), width * 3, 1, fp); - } - fclose(fp); -} - -struct FlowType { - using type = float; - static constexpr GLenum gl_format = GL_RG; - static constexpr GLenum gl_type = GL_FLOAT; - static constexpr int num_channels = 2; -}; - -struct RGBAType { - using type = uint8_t; - static constexpr GLenum gl_format = GL_RGBA; - static constexpr GLenum gl_type = GL_UNSIGNED_BYTE; - static constexpr int num_channels = 4; -}; - -template -void finish_one_read(GLuint width, GLuint height) -{ - using T = typename Type::type; - constexpr int bytes_per_pixel = Type::num_channels * sizeof(T); - - assert(!reads_in_progress.empty()); - ReadInProgress read = reads_in_progress.front(); - reads_in_progress.pop_front(); - - unique_ptr flow(new typename Type::type[width * height * Type::num_channels]); - void *buf = glMapNamedBufferRange(read.pbo, 0, width * height * bytes_per_pixel, GL_MAP_READ_BIT); // Blocks if the read isn't done yet. - memcpy(flow.get(), buf, width * height * bytes_per_pixel); // TODO: Unneeded for RGBType, since flip_coordinate_system() does nothing.: - glUnmapNamedBuffer(read.pbo); - spare_pbos.push(read.pbo); - - flip_coordinate_system(flow.get(), width, height); - if (!read.flow_filename.empty()) { - write_flow(read.flow_filename.c_str(), flow.get(), width, height); - fprintf(stderr, "%s %s -> %s\n", read.filename0.c_str(), read.filename1.c_str(), read.flow_filename.c_str()); - } - if (!read.ppm_filename.empty()) { - write_ppm(read.ppm_filename.c_str(), flow.get(), width, height); - } -} - -template -void schedule_read(GLuint tex, GLuint width, GLuint height, const char *filename0, const char *filename1, const char *flow_filename, const char *ppm_filename) -{ - using T = typename Type::type; - constexpr int bytes_per_pixel = Type::num_channels * sizeof(T); - - if (spare_pbos.empty()) { - finish_one_read(width, height); - } - assert(!spare_pbos.empty()); - reads_in_progress.emplace_back(ReadInProgress{ spare_pbos.top(), filename0, filename1, flow_filename, ppm_filename }); - glBindBuffer(GL_PIXEL_PACK_BUFFER, spare_pbos.top()); - spare_pbos.pop(); - glGetTextureImage(tex, 0, Type::gl_format, Type::gl_type, width * height * bytes_per_pixel, nullptr); - glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); -} - -void compute_flow_only(int argc, char **argv, int optind) -{ - 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"; - - // Load pictures. - unsigned width1, height1, width2, height2; - GLuint tex0 = load_texture(filename0, &width1, &height1, WITHOUT_MIPMAPS); - GLuint tex1 = load_texture(filename1, &width2, &height2, WITHOUT_MIPMAPS); - - if (width1 != width2 || height1 != height2) { - fprintf(stderr, "Image dimensions don't match (%dx%d versus %dx%d)\n", - width1, height1, width2, height2); - exit(1); - } - - // Move them into an array texture, since that's how the rest of the code - // would like them. - GLuint image_tex; - glCreateTextures(GL_TEXTURE_2D_ARRAY, 1, &image_tex); - glTextureStorage3D(image_tex, 1, GL_RGBA8, width1, height1, 2); - glCopyImageSubData(tex0, GL_TEXTURE_2D, 0, 0, 0, 0, image_tex, GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, width1, height1, 1); - glCopyImageSubData(tex1, GL_TEXTURE_2D, 0, 0, 0, 0, image_tex, GL_TEXTURE_2D_ARRAY, 0, 0, 0, 1, width1, height1, 1); - glDeleteTextures(1, &tex0); - glDeleteTextures(1, &tex1); - - // Set up some PBOs to do asynchronous readback. - GLuint pbos[5]; - glCreateBuffers(5, pbos); - for (int i = 0; i < 5; ++i) { - glNamedBufferData(pbos[i], width1 * height1 * 2 * 2 * sizeof(float), nullptr, GL_STREAM_READ); - spare_pbos.push(pbos[i]); - } - - int levels = find_num_levels(width1, height1); - - GLuint tex_gray; - glCreateTextures(GL_TEXTURE_2D_ARRAY, 1, &tex_gray); - glTextureStorage3D(tex_gray, levels, GL_R8, width1, height1, 2); - - GrayscaleConversion gray; - gray.exec(image_tex, tex_gray, width1, height1, /*num_layers=*/2); - glGenerateTextureMipmap(tex_gray); - - OperatingPoint op = operating_point3; - if (!enable_variational_refinement) { - op.variational_refinement = false; - } - DISComputeFlow compute_flow(width1, height1, op); - - if (enable_warmup) { - in_warmup = true; - for (int i = 0; i < 10; ++i) { - GLuint final_tex = compute_flow.exec(tex_gray, DISComputeFlow::FORWARD, DISComputeFlow::RESIZE_FLOW_TO_FULL_SIZE); - compute_flow.release_texture(final_tex); - } - in_warmup = false; - } - - GLuint final_tex = compute_flow.exec(tex_gray, DISComputeFlow::FORWARD, DISComputeFlow::RESIZE_FLOW_TO_FULL_SIZE); - //GLuint final_tex = compute_flow.exec(tex_gray, DISComputeFlow::FORWARD_AND_BACKWARD, DISComputeFlow::RESIZE_FLOW_TO_FULL_SIZE); - - schedule_read(final_tex, width1, height1, filename0, filename1, flow_filename, "flow.ppm"); - compute_flow.release_texture(final_tex); - - // 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]; - GLuint width, height; - GLuint tex0 = load_texture(filename0, &width, &height, WITHOUT_MIPMAPS); - 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); - } - glCopyImageSubData(tex0, GL_TEXTURE_2D, 0, 0, 0, 0, image_tex, GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, width1, height1, 1); - glDeleteTextures(1, &tex0); - - GLuint tex1 = load_texture(filename1, &width, &height, WITHOUT_MIPMAPS); - 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); - } - glCopyImageSubData(tex1, GL_TEXTURE_2D, 0, 0, 0, 0, image_tex, GL_TEXTURE_2D_ARRAY, 0, 0, 0, 1, width1, height1, 1); - glDeleteTextures(1, &tex1); - - gray.exec(image_tex, tex_gray, width1, height1, /*num_layers=*/2); - glGenerateTextureMipmap(tex_gray); - - GLuint final_tex = compute_flow.exec(tex_gray, DISComputeFlow::FORWARD, DISComputeFlow::RESIZE_FLOW_TO_FULL_SIZE); - - schedule_read(final_tex, width1, height1, filename0, filename1, flow_filename, ""); - compute_flow.release_texture(final_tex); - } - glDeleteTextures(1, &tex_gray); - - while (!reads_in_progress.empty()) { - finish_one_read(width1, height1); - } -} - -// Interpolate images based on -// -// Herbst, Seitz, Baker: “Occlusion Reasoning for Temporal Interpolation -// Using Optical Flow” -// -// or at least a reasonable subset thereof. Unfinished. -void interpolate_image(int argc, char **argv, int optind) -{ - const char *filename0 = argc >= (optind + 1) ? argv[optind] : "test1499.png"; - const char *filename1 = argc >= (optind + 2) ? argv[optind + 1] : "test1500.png"; - //const char *out_filename = argc >= (optind + 3) ? argv[optind + 2] : "interpolated.png"; - - // Load pictures. - unsigned width1, height1, width2, height2; - GLuint tex0 = load_texture(filename0, &width1, &height1, WITH_MIPMAPS); - GLuint tex1 = load_texture(filename1, &width2, &height2, WITH_MIPMAPS); - - if (width1 != width2 || height1 != height2) { - fprintf(stderr, "Image dimensions don't match (%dx%d versus %dx%d)\n", - width1, height1, width2, height2); - exit(1); - } - - // Move them into an array texture, since that's how the rest of the code - // would like them. - int levels = find_num_levels(width1, height1); - GLuint image_tex; - glCreateTextures(GL_TEXTURE_2D_ARRAY, 1, &image_tex); - glTextureStorage3D(image_tex, levels, GL_RGBA8, width1, height1, 2); - glCopyImageSubData(tex0, GL_TEXTURE_2D, 0, 0, 0, 0, image_tex, GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, width1, height1, 1); - glCopyImageSubData(tex1, GL_TEXTURE_2D, 0, 0, 0, 0, image_tex, GL_TEXTURE_2D_ARRAY, 0, 0, 0, 1, width1, height1, 1); - glDeleteTextures(1, &tex0); - glDeleteTextures(1, &tex1); - glGenerateTextureMipmap(image_tex); - - // Set up some PBOs to do asynchronous readback. - GLuint pbos[5]; - glCreateBuffers(5, pbos); - for (int i = 0; i < 5; ++i) { - glNamedBufferData(pbos[i], width1 * height1 * 4 * sizeof(uint8_t), nullptr, GL_STREAM_READ); - spare_pbos.push(pbos[i]); - } - - OperatingPoint op = operating_point3; - if (!enable_variational_refinement) { - op.variational_refinement = false; - } - DISComputeFlow compute_flow(width1, height1, op); - GrayscaleConversion gray; - Interpolate interpolate(width1, height1, op); - - GLuint tex_gray; - glCreateTextures(GL_TEXTURE_2D_ARRAY, 1, &tex_gray); - glTextureStorage3D(tex_gray, levels, GL_R8, width1, height1, 2); - gray.exec(image_tex, tex_gray, width1, height1, /*num_layers=*/2); - glGenerateTextureMipmap(tex_gray); - - if (enable_warmup) { - in_warmup = true; - for (int i = 0; i < 10; ++i) { - GLuint bidirectional_flow_tex = compute_flow.exec(tex_gray, DISComputeFlow::FORWARD_AND_BACKWARD, DISComputeFlow::DO_NOT_RESIZE_FLOW); - GLuint interpolated_tex = interpolate.exec(image_tex, bidirectional_flow_tex, width1, height1, 0.5f); - compute_flow.release_texture(bidirectional_flow_tex); - interpolate.release_texture(interpolated_tex); - } - in_warmup = false; - } - - GLuint bidirectional_flow_tex = compute_flow.exec(tex_gray, DISComputeFlow::FORWARD_AND_BACKWARD, DISComputeFlow::DO_NOT_RESIZE_FLOW); - - for (int frameno = 1; frameno < 60; ++frameno) { - char ppm_filename[256]; - snprintf(ppm_filename, sizeof(ppm_filename), "interp%04d.ppm", frameno); - - float alpha = frameno / 60.0f; - GLuint interpolated_tex = interpolate.exec(image_tex, bidirectional_flow_tex, width1, height1, alpha); - - schedule_read(interpolated_tex, width1, height1, filename0, filename1, "", ppm_filename); - interpolate.release_texture(interpolated_tex); - } - - while (!reads_in_progress.empty()) { - finish_one_read(width1, height1); - } -} - -int main(int argc, char **argv) -{ - static const option long_options[] = { - { "smoothness-relative-weight", required_argument, 0, 's' }, // alpha. - { "intensity-relative-weight", required_argument, 0, 'i' }, // delta. - { "gradient-relative-weight", required_argument, 0, 'g' }, // gamma. - { "disable-timing", no_argument, 0, 1000 }, - { "detailed-timing", no_argument, 0, 1003 }, - { "disable-variational-refinement", no_argument, 0, 1001 }, - { "interpolate", no_argument, 0, 1002 }, - { "warmup", no_argument, 0, 1004 } - }; - - for ( ;; ) { - int option_index = 0; - int c = getopt_long(argc, argv, "s:i:g:", long_options, &option_index); - - if (c == -1) { - break; - } - switch (c) { - case 's': - vr_alpha = atof(optarg); - break; - case 'i': - vr_delta = atof(optarg); - break; - case 'g': - vr_gamma = atof(optarg); - break; - case 1000: - enable_timing = false; - break; - case 1001: - enable_variational_refinement = false; - break; - case 1002: - enable_interpolation = true; - break; - case 1003: - detailed_timing = true; - break; - case 1004: - enable_warmup = true; - 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); - } - SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0); - SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0); - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - - SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5); - // SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); - window = SDL_CreateWindow("OpenGL window", - SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, - 64, 64, - SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN); - SDL_GLContext context = SDL_GL_CreateContext(window); - assert(context != nullptr); - - glDisable(GL_DITHER); - - // FIXME: Should be part of DISComputeFlow (but needs to be initialized - // before all the render passes). - float vertices[] = { - 0.0f, 1.0f, - 0.0f, 0.0f, - 1.0f, 1.0f, - 1.0f, 0.0f, - }; - glCreateBuffers(1, &vertex_vbo); - glNamedBufferData(vertex_vbo, sizeof(vertices), vertices, GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, vertex_vbo); - - if (enable_interpolation) { - interpolate_image(argc, argv, optind); - } else { - compute_flow_only(argc, argv, optind); - } -}