]> git.sesse.net Git - nageru/blobdiff - flow.cpp
Split texture pooling out from DISComputeFlow into its own class.
[nageru] / flow.cpp
index 3cbbb7174266842ba4ee9145fdec7fa5b65b7ce8..e6347dc8b7978f2907a58a9fe548ce7d1a320f69 100644 (file)
--- a/flow.cpp
+++ b/flow.cpp
@@ -319,7 +319,7 @@ private:
        GLuint sobel_program;
        GLuint sobel_vao;
 
-       GLuint uniform_tex, uniform_image_size;
+       GLuint uniform_tex;
 };
 
 Sobel::Sobel()
@@ -342,9 +342,7 @@ Sobel::Sobel()
 void Sobel::exec(GLint tex0_view, GLint grad0_tex, int level_width, int level_height)
 {
        glUseProgram(sobel_program);
-       glBindTextureUnit(0, tex0_view);
-       glBindSampler(0, nearest_sampler);
-       glProgramUniform1i(sobel_program, uniform_tex, 0);
+       bind_sampler(sobel_program, uniform_tex, 0, tex0_view, nearest_sampler);
 
        glViewport(0, 0, level_width, level_height);
        fbos.render_to(grad0_tex);
@@ -368,7 +366,7 @@ private:
        GLuint motion_search_program;
        GLuint motion_search_vao;
 
-       GLuint uniform_image_size, uniform_inv_image_size, uniform_inv_flow_size, uniform_inv_prev_level_size;
+       GLuint uniform_inv_image_size, uniform_inv_prev_level_size;
        GLuint uniform_image0_tex, uniform_image1_tex, uniform_grad0_tex, uniform_flow_tex;
 };
 
@@ -387,9 +385,7 @@ MotionSearch::MotionSearch()
        glEnableVertexArrayAttrib(motion_search_vao, position_attrib);
        glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
 
-       uniform_image_size = glGetUniformLocation(motion_search_program, "image_size");
        uniform_inv_image_size = glGetUniformLocation(motion_search_program, "inv_image_size");
-       uniform_inv_flow_size = glGetUniformLocation(motion_search_program, "inv_flow_size");
        uniform_inv_prev_level_size = glGetUniformLocation(motion_search_program, "inv_prev_level_size");
        uniform_image0_tex = glGetUniformLocation(motion_search_program, "image0_tex");
        uniform_image1_tex = glGetUniformLocation(motion_search_program, "image1_tex");
@@ -406,9 +402,7 @@ void MotionSearch::exec(GLuint tex0_view, GLuint tex1_view, GLuint grad0_tex, GL
        bind_sampler(motion_search_program, uniform_grad0_tex, 2, grad0_tex, zero_border_sampler);
        bind_sampler(motion_search_program, uniform_flow_tex, 3, flow_tex, linear_sampler);
 
-       glProgramUniform2f(motion_search_program, uniform_image_size, level_width, level_height);
        glProgramUniform2f(motion_search_program, uniform_inv_image_size, 1.0f / level_width, 1.0f / level_height);
-       glProgramUniform2f(motion_search_program, uniform_inv_flow_size, 1.0f / width_patches, 1.0f / height_patches);
        glProgramUniform2f(motion_search_program, uniform_inv_prev_level_size, 1.0f / prev_level_width, 1.0f / prev_level_height);
 
        glViewport(0, 0, width_patches, height_patches);
@@ -439,7 +433,7 @@ private:
        GLuint densify_program;
        GLuint densify_vao;
 
-       GLuint uniform_width_patches, uniform_patch_size, uniform_patch_spacing;
+       GLuint uniform_patch_size, uniform_patch_spacing;
        GLuint uniform_image0_tex, uniform_image1_tex, uniform_flow_tex;
 };
 
@@ -458,7 +452,6 @@ Densify::Densify()
        glEnableVertexArrayAttrib(densify_vao, position_attrib);
        glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
 
-       uniform_width_patches = glGetUniformLocation(densify_program, "width_patches");
        uniform_patch_size = glGetUniformLocation(densify_program, "patch_size");
        uniform_patch_spacing = glGetUniformLocation(densify_program, "patch_spacing");
        uniform_image0_tex = glGetUniformLocation(densify_program, "image0_tex");
@@ -474,7 +467,6 @@ void Densify::exec(GLuint tex0_view, GLuint tex1_view, GLuint flow_tex, GLuint d
        bind_sampler(densify_program, uniform_image1_tex, 1, tex1_view, linear_sampler);
        bind_sampler(densify_program, uniform_flow_tex, 2, flow_tex, nearest_sampler);
 
-       glProgramUniform1i(densify_program, uniform_width_patches, width_patches);
        glProgramUniform2f(densify_program, uniform_patch_size,
                float(patch_size_pixels) / level_width,
                float(patch_size_pixels) / level_height);
@@ -517,7 +509,6 @@ private:
        GLuint prewarp_vao;
 
        GLuint uniform_image0_tex, uniform_image1_tex, uniform_flow_tex;
-       GLuint uniform_image_size;
 };
 
 Prewarp::Prewarp()
@@ -538,8 +529,6 @@ Prewarp::Prewarp()
        uniform_image0_tex = glGetUniformLocation(prewarp_program, "image0_tex");
        uniform_image1_tex = glGetUniformLocation(prewarp_program, "image1_tex");
        uniform_flow_tex = glGetUniformLocation(prewarp_program, "flow_tex");
-
-       uniform_image_size = glGetUniformLocation(prewarp_program, "image_size");
 }
 
 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)
@@ -550,8 +539,6 @@ void Prewarp::exec(GLuint tex0_view, GLuint tex1_view, GLuint flow_tex, GLuint I
        bind_sampler(prewarp_program, uniform_image1_tex, 1, tex1_view, linear_sampler);
        bind_sampler(prewarp_program, uniform_flow_tex, 2, flow_tex, nearest_sampler);
 
-       glProgramUniform2f(prewarp_program, uniform_image_size, level_width, level_height);
-
        glViewport(0, 0, level_width, level_height);
        glDisable(GL_BLEND);
        glBindVertexArray(prewarp_vao);
@@ -778,11 +765,12 @@ private:
        GLuint uniform_diff_flow_tex;
        GLuint uniform_equation_tex;
        GLuint uniform_smoothness_x_tex, uniform_smoothness_y_tex;
+       GLuint uniform_phase;
 };
 
 SOR::SOR()
 {
-       sor_vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER);
+       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_program = link_program(sor_vs_obj, sor_fs_obj);
 
@@ -799,6 +787,7 @@ SOR::SOR()
        uniform_equation_tex = glGetUniformLocation(sor_program, "equation_tex");
        uniform_smoothness_x_tex = glGetUniformLocation(sor_program, "smoothness_x_tex");
        uniform_smoothness_y_tex = glGetUniformLocation(sor_program, "smoothness_y_tex");
+       uniform_phase = glGetUniformLocation(sor_program, "phase");
 }
 
 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)
@@ -810,12 +799,20 @@ void SOR::exec(GLuint diff_flow_tex, GLuint equation_tex, GLuint smoothness_x_te
        bind_sampler(sor_program, uniform_smoothness_y_tex, 2, smoothness_y_tex, zero_border_sampler);
        bind_sampler(sor_program, uniform_equation_tex, 3, equation_tex, nearest_sampler);
 
+       // NOTE: We bind to the texture we are rendering from, but we never write any value
+       // that we read in the same shader pass (we call discard for red values when we compute
+       // black, and vice versa), and we have barriers between the passes, so we're fine
+       // as per the spec.
        glViewport(0, 0, level_width, level_height);
        glDisable(GL_BLEND);
        glBindVertexArray(sor_vao);
-       fbos.render_to(diff_flow_tex);  // NOTE: Bind to same as we render from!
+       fbos.render_to(diff_flow_tex);
 
        for (int i = 0; i < num_iterations; ++i) {
+               glProgramUniform1i(sor_program, uniform_phase, 0);
+               glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+               glTextureBarrier();
+               glProgramUniform1i(sor_program, uniform_phase, 1);
                glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
                if (i != num_iterations - 1) {
                        glTextureBarrier();
@@ -1011,6 +1008,21 @@ private:
        bool ended = false;
 };
 
+class TexturePool {
+public:
+       GLuint get_texture(GLenum format, GLuint width, GLuint height);
+       void release_texture(GLuint tex_num);
+
+private:
+       struct Texture {
+               GLuint tex_num;
+               GLenum format;
+               GLuint width, height;
+               bool in_use = false;
+       };
+       vector<Texture> textures;
+};
+
 class DISComputeFlow {
 public:
        DISComputeFlow(int width, int height);
@@ -1018,11 +1030,15 @@ public:
        // Returns a texture that must be released with release_texture()
        // after use.
        GLuint exec(GLuint tex0, GLuint tex1);
-       void release_texture(GLuint tex);
+
+       void release_texture(GLuint tex) {
+               pool.release_texture(tex);
+       }
 
 private:
        int width, height;
        GLuint initial_flow_tex;
+       TexturePool pool;
 
        // The various passes.
        Sobel sobel;
@@ -1035,16 +1051,6 @@ private:
        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)
@@ -1083,10 +1089,6 @@ DISComputeFlow::DISComputeFlow(int width, int height)
 
 GLuint DISComputeFlow::exec(GLuint tex0, GLuint tex1)
 {
-       for (const Texture &tex : textures) {
-               assert(!tex.in_use);
-       }
-
        int prev_level_width = 1, prev_level_height = 1;
        GLuint prev_level_flow_tex = initial_flow_tex;
 
@@ -1101,13 +1103,17 @@ GLuint DISComputeFlow::exec(GLuint tex0, GLuint tex1)
                int level_width = width >> level;
                int level_height = height >> level;
                float patch_spacing_pixels = patch_size_pixels * (1.0f - patch_overlap_ratio);
-               int width_patches = 1 + lrintf((level_width - patch_size_pixels) / patch_spacing_pixels);
-               int height_patches = 1 + lrintf((level_height - patch_size_pixels) / patch_spacing_pixels);
+
+               // Make sure we have patches at least every Nth pixel, e.g. for width=9
+               // and patch_spacing=3 (the default), we put out patch centers in
+               // x=0, x=3, x=6, x=9, which is four patches. The fragment shader will
+               // lock all the centers to integer coordinates if needed.
+               int width_patches = 1 + ceil(level_width / patch_spacing_pixels);
+               int height_patches = 1 + ceil(level_height / patch_spacing_pixels);
 
                // Make sure we always read from the correct level; the chosen
                // mipmapping could otherwise be rather unpredictable, especially
                // during motion search.
-               // TODO: create these beforehand, and stop leaking them.
                GLuint tex0_view, tex1_view;
                glGenTextures(1, &tex0_view);
                glTextureView(tex0_view, GL_TEXTURE_2D, tex0, GL_R8, level, 1, 0, 1);
@@ -1116,7 +1122,7 @@ GLuint DISComputeFlow::exec(GLuint tex0, GLuint tex1)
 
                // Create a new texture; we could be fancy and render use a multi-level
                // texture, but meh.
-               GLuint grad0_tex = get_texture(GL_RG16F, level_width, level_height);
+               GLuint grad0_tex = pool.get_texture(GL_RG16F, level_width, level_height);
 
                // Find the derivative.
                {
@@ -1128,19 +1134,19 @@ GLuint DISComputeFlow::exec(GLuint tex0, GLuint tex1)
                // level (sampled bilinearly; no fancy tricks) as a guide, then search from there.
 
                // Create an output flow texture.
-               GLuint flow_out_tex = get_texture(GL_RGB16F, width_patches, height_patches);
+               GLuint flow_out_tex = pool.get_texture(GL_RGB16F, width_patches, height_patches);
 
                // And draw.
                {
                        ScopedTimer timer("Motion search", &level_timer);
                        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);
                }
-               release_texture(grad0_tex);
+               pool.release_texture(grad0_tex);
 
                // Densification.
 
                // Set up an output texture (initially zero).
-               GLuint dense_flow_tex = get_texture(GL_RGB16F, level_width, level_height);
+               GLuint dense_flow_tex = pool.get_texture(GL_RGB16F, level_width, level_height);
                glClearTexImage(dense_flow_tex, 0, GL_RGB, GL_FLOAT, nullptr);
 
                // And draw.
@@ -1148,7 +1154,7 @@ GLuint DISComputeFlow::exec(GLuint tex0, GLuint tex1)
                        ScopedTimer timer("Densification", &level_timer);
                        densify.exec(tex0_view, tex1_view, flow_out_tex, dense_flow_tex, level_width, level_height, width_patches, height_patches);
                }
-               release_texture(flow_out_tex);
+               pool.release_texture(flow_out_tex);
 
                // Everything below here in the loop belongs to variational refinement.
                ScopedTimer varref_timer("Variational refinement", &level_timer);
@@ -1160,40 +1166,42 @@ GLuint DISComputeFlow::exec(GLuint tex0, GLuint tex1)
                // in pixels, not 0..1 normalized OpenGL texture coordinates.
                // This is because variational refinement depends so heavily on derivatives,
                // which are measured in intensity levels per pixel.
-               GLuint I_tex = get_texture(GL_R16F, level_width, level_height);
-               GLuint I_t_tex = get_texture(GL_R16F, level_width, level_height);
-               GLuint base_flow_tex = get_texture(GL_RG16F, level_width, level_height);
+               GLuint I_tex = pool.get_texture(GL_R16F, level_width, level_height);
+               GLuint I_t_tex = pool.get_texture(GL_R16F, level_width, level_height);
+               GLuint base_flow_tex = pool.get_texture(GL_RG16F, level_width, level_height);
                {
                        ScopedTimer timer("Prewarping", &varref_timer);
                        prewarp.exec(tex0_view, tex1_view, dense_flow_tex, I_tex, I_t_tex, base_flow_tex, level_width, level_height);
                }
-               release_texture(dense_flow_tex);
+               pool.release_texture(dense_flow_tex);
+               glDeleteTextures(1, &tex0_view);
+               glDeleteTextures(1, &tex1_view);
 
                // Calculate I_x and I_y. We're only calculating first derivatives;
                // the others will be taken on-the-fly in order to sample from fewer
                // textures overall, since sampling from the L1 cache is cheap.
                // (TODO: Verify that this is indeed faster than making separate
                // double-derivative textures.)
-               GLuint I_x_y_tex = get_texture(GL_RG16F, level_width, level_height);
-               GLuint beta_0_tex = get_texture(GL_R16F, level_width, level_height);
+               GLuint I_x_y_tex = pool.get_texture(GL_RG16F, level_width, level_height);
+               GLuint beta_0_tex = pool.get_texture(GL_R16F, level_width, level_height);
                {
                        ScopedTimer timer("First derivatives", &varref_timer);
                        derivatives.exec(I_tex, I_x_y_tex, beta_0_tex, level_width, level_height);
                }
-               release_texture(I_tex);
+               pool.release_texture(I_tex);
 
                // We need somewhere to store du and dv (the flow increment, relative
                // to the non-refined base flow u0 and v0). It starts at zero.
-               GLuint du_dv_tex = get_texture(GL_RG16F, level_width, level_height);
+               GLuint du_dv_tex = pool.get_texture(GL_RG16F, level_width, level_height);
                glClearTexImage(du_dv_tex, 0, GL_RG, GL_FLOAT, nullptr);
 
                // And for smoothness.
-               GLuint smoothness_x_tex = get_texture(GL_R16F, level_width, level_height);
-               GLuint smoothness_y_tex = get_texture(GL_R16F, level_width, level_height);
+               GLuint smoothness_x_tex = pool.get_texture(GL_R16F, level_width, level_height);
+               GLuint smoothness_y_tex = pool.get_texture(GL_R16F, level_width, level_height);
 
                // And finally for the equation set. See SetupEquations for
                // the storage format.
-               GLuint equation_tex = get_texture(GL_RGBA32UI, level_width, level_height);
+               GLuint equation_tex = pool.get_texture(GL_RGBA32UI, level_width, level_height);
 
                for (int outer_idx = 0; outer_idx < level + 1; ++outer_idx) {
                        // Calculate the smoothness terms between the neighboring pixels,
@@ -1217,12 +1225,12 @@ GLuint DISComputeFlow::exec(GLuint tex0, GLuint tex1)
                        }
                }
 
-               release_texture(I_t_tex);
-               release_texture(I_x_y_tex);
-               release_texture(beta_0_tex);
-               release_texture(smoothness_x_tex);
-               release_texture(smoothness_y_tex);
-               release_texture(equation_tex);
+               pool.release_texture(I_t_tex);
+               pool.release_texture(I_x_y_tex);
+               pool.release_texture(beta_0_tex);
+               pool.release_texture(smoothness_x_tex);
+               pool.release_texture(smoothness_y_tex);
+               pool.release_texture(equation_tex);
 
                // Add the differential flow found by the variational refinement to the base flow,
                // giving the final flow estimate for this level.
@@ -1234,10 +1242,10 @@ GLuint DISComputeFlow::exec(GLuint tex0, GLuint tex1)
                        ScopedTimer timer("Add differential flow", &varref_timer);
                        add_base_flow.exec(base_flow_tex, du_dv_tex, level_width, level_height);
                }
-               release_texture(du_dv_tex);
+               pool.release_texture(du_dv_tex);
 
                if (prev_level_flow_tex != initial_flow_tex) {
-                       release_texture(prev_level_flow_tex);
+                       pool.release_texture(prev_level_flow_tex);
                }
                prev_level_flow_tex = base_flow_tex;
                prev_level_width = level_width;
@@ -1251,14 +1259,14 @@ GLuint DISComputeFlow::exec(GLuint tex0, GLuint tex1)
        if (finest_level == 0) {
                return prev_level_flow_tex;
        } else {
-               GLuint final_tex = get_texture(GL_RG16F, width, height);
+               GLuint final_tex = pool.get_texture(GL_RG16F, width, height);
                resize_flow.exec(prev_level_flow_tex, final_tex, prev_level_width, prev_level_height, width, height);
-               release_texture(prev_level_flow_tex);
+               pool.release_texture(prev_level_flow_tex);
                return final_tex;
        }
 }
 
-GLuint DISComputeFlow::get_texture(GLenum format, GLuint width, GLuint height)
+GLuint TexturePool::get_texture(GLenum format, GLuint width, GLuint height)
 {
        for (Texture &tex : textures) {
                if (!tex.in_use && tex.format == format &&
@@ -1279,7 +1287,7 @@ GLuint DISComputeFlow::get_texture(GLenum format, GLuint width, GLuint height)
        return tex.tex_num;
 }
 
-void DISComputeFlow::release_texture(GLuint tex_num)
+void TexturePool::release_texture(GLuint tex_num)
 {
        for (Texture &tex : textures) {
                if (tex.tex_num == tex_num) {
@@ -1420,10 +1428,10 @@ int main(int argc, char **argv)
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
        // SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
        SDL_Window *window = SDL_CreateWindow("OpenGL window",
-                       SDL_WINDOWPOS_UNDEFINED,
-                       SDL_WINDOWPOS_UNDEFINED,
-                       64, 64,
-                       SDL_WINDOW_OPENGL);
+               SDL_WINDOWPOS_UNDEFINED,
+               SDL_WINDOWPOS_UNDEFINED,
+               64, 64,
+               SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN);
        SDL_GLContext context = SDL_GL_CreateContext(window);
        assert(context != nullptr);