]> git.sesse.net Git - nageru/commitdiff
Use textureSize() instead of sending in uniforms manually. Same result, less code...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 27 Jul 2018 14:07:47 +0000 (16:07 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 27 Jul 2018 14:09:05 +0000 (16:09 +0200)
densify.vert
flow.cpp
motion_search.frag
motion_search.vert
prewarp.frag
sobel.frag
sor.frag
sor.vert

index b7f78d74edad1d6ca30e7304f221e9018c5cfeba..1859dfc2a0a33e0638ffd393263f7fa426116944 100644 (file)
@@ -5,21 +5,19 @@ out vec2 image_pos;
 flat out vec2 flow_du;
 flat out float mean_diff;
 
-uniform int width_patches;
 uniform vec2 patch_size;  // In 0..1 coordinates.
 uniform vec2 patch_spacing;  // In 0..1 coordinates.
 uniform sampler2D flow_tex;
-uniform vec2 flow_size;
 
 void main()
 {
-       int patch_x = gl_InstanceID % width_patches;
-       int patch_y = gl_InstanceID / width_patches;
+       int patch_x = gl_InstanceID % textureSize(flow_tex, 0).x;
+       int patch_y = gl_InstanceID / textureSize(flow_tex, 0).x;
 
        // Convert the patch index to being the full 0..1 range, to match where
        // the motion search puts the patches. We don't bother with the locking
        // to texel centers, though.
-       vec2 patch_center = ivec2(patch_x, patch_y) / (flow_size - 1.0);
+       vec2 patch_center = ivec2(patch_x, patch_y) / (textureSize(flow_tex, 0) - 1.0);
 
        // Increase the patch size a bit; since patch spacing is not necessarily
        // an integer number of pixels, and we don't use conservative rasterization,
index 8b09c8840999148ee3c0d3a0375e0af6c833da08..d425fe1d1baf12d4f57e48253eefd3c979631906 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()
@@ -368,7 +368,7 @@ private:
        GLuint motion_search_program;
        GLuint motion_search_vao;
 
-       GLuint uniform_image_size, uniform_inv_image_size, uniform_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 +387,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_flow_size = glGetUniformLocation(motion_search_program, "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 +404,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_flow_size, width_patches, 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,9 +435,8 @@ 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;
-       GLuint uniform_flow_size;
 };
 
 Densify::Densify()
@@ -459,13 +454,11 @@ 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");
        uniform_image1_tex = glGetUniformLocation(densify_program, "image1_tex");
        uniform_flow_tex = glGetUniformLocation(densify_program, "flow_tex");
-       uniform_flow_size = glGetUniformLocation(densify_program, "flow_size");
 }
 
 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)
@@ -476,13 +469,9 @@ 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);
-       glProgramUniform2f(densify_program, uniform_flow_size,
-               width_patches,
-               height_patches);
 
        float patch_spacing_x = float(level_width - patch_size_pixels) / (width_patches - 1);
        float patch_spacing_y = float(level_height - patch_size_pixels) / (height_patches - 1);
@@ -522,7 +511,6 @@ private:
        GLuint prewarp_vao;
 
        GLuint uniform_image0_tex, uniform_image1_tex, uniform_flow_tex;
-       GLuint uniform_image_size;
 };
 
 Prewarp::Prewarp()
@@ -543,8 +531,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)
@@ -555,8 +541,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);
@@ -783,7 +767,7 @@ private:
        GLuint uniform_diff_flow_tex;
        GLuint uniform_equation_tex;
        GLuint uniform_smoothness_x_tex, uniform_smoothness_y_tex;
-       GLuint uniform_image_size, uniform_phase;
+       GLuint uniform_phase;
 };
 
 SOR::SOR()
@@ -805,7 +789,6 @@ 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_image_size = glGetUniformLocation(sor_program, "image_size");
        uniform_phase = glGetUniformLocation(sor_program, "phase");
 }
 
@@ -818,8 +801,6 @@ 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);
 
-       glProgramUniform2f(sor_program, uniform_image_size, level_width, level_height);
-
        // 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
index cd251f4f15a7613487aa2d7b5fc23730c3eeded3..05e901c12844cfa3dd38753435c4b8e649fd0144 100644 (file)
@@ -43,10 +43,12 @@ in vec2 patch_center;
 out vec3 out_flow;
 
 uniform sampler2D flow_tex, grad0_tex, image0_tex, image1_tex;
-uniform vec2 image_size, inv_image_size, inv_prev_level_size;
+uniform vec2 inv_image_size, inv_prev_level_size;
 
 void main()
 {
+       vec2 image_size = textureSize(image0_tex, 0);
+
        // Lock the patch center to an integer, so that we never get
        // any bilinear artifacts for the gradient. (NOTE: This assumes an
        // even patch size.) Then calculate the bottom-left texel of the patch.
index 9e25e7a74c19356b448706aad0c7111ee4fc22f9..28b65c225bfafdb2ab0d5f0678175ab978472930 100644 (file)
@@ -4,7 +4,7 @@ in vec2 position;
 out vec2 flow_tc;
 out vec2 patch_center;
 
-uniform vec2 flow_size;
+uniform sampler2D flow_tex;
 
 void main()
 {
@@ -23,6 +23,7 @@ void main()
        //
        //   a = 1 / (w - 1)
        //   b = w / 2 (w - 1)
+       vec2 flow_size = textureSize(flow_tex, 0);
        vec2 a = flow_size / (flow_size - 1);
        vec2 b = -1.0 / (2 * (flow_size - 1.0));
        patch_center = a * position + b;
index b9645bf2c805de1a77593c977928164d409b03cb..4b2db4408ee8d9d35af8c4f72de10cafe5b05e3a 100644 (file)
@@ -7,7 +7,6 @@ out float I, I_t;
 out vec2 normalized_flow;
 
 uniform sampler2D image0_tex, image1_tex, flow_tex;
-uniform vec2 image_size;
 
 void main()
 {
@@ -19,5 +18,5 @@ void main()
 
        I = 0.5f * (I_0 + I_w);
        I_t = I_w - I_0;
-       normalized_flow = flow.xy * image_size;
+       normalized_flow = flow.xy * textureSize(image0_tex, 0);
 }
index 518b6f8abcdadb4491b8e1187f6cea3e696fa477..90c6d8a93c895368b45cbb18480148cd5583559c 100644 (file)
@@ -4,7 +4,6 @@ in vec2 tc;
 out vec2 gradients;
 
 uniform sampler2D tex;
-uniform vec2 inv_image_size;
 
 void main()
 {
index fbcc2e4ca8b8532b829a7c653b88757310448fba..ac5b83c09f7be1c8a6eb66267562b5fcf2f74d78 100644 (file)
--- a/sor.frag
+++ b/sor.frag
@@ -6,7 +6,6 @@ out vec2 diff_flow;
 
 uniform sampler2D diff_flow_tex, smoothness_x_tex, smoothness_y_tex;
 uniform usampler2D equation_tex;
-uniform vec2 image_size;
 uniform int phase;
 
 // See pack_floats_shared() in equations.frag.
index a42ebc7f8f83761d8014f73ecdfc07483e141652..201578c94401dd256ba5cc5c8a95931880b3121d 100644 (file)
--- a/sor.vert
+++ b/sor.vert
@@ -4,7 +4,7 @@ in vec2 position;
 out vec2 tc;
 out float element_sum_idx;
 
-uniform vec2 image_size;
+uniform sampler2D diff_flow_tex;
 
 void main()
 {
@@ -17,6 +17,6 @@ void main()
        gl_Position = vec4(2.0 * position.x - 1.0, 2.0 * position.y - 1.0, -1.0, 1.0);
        tc = position;
 
-       vec2 element_idx = position * image_size - 0.5;
+       vec2 element_idx = position * textureSize(diff_flow_tex, 0) - 0.5;
        element_sum_idx = element_idx.x + element_idx.y;
 }