From: Steinar H. Gunderson Date: Fri, 27 Jul 2018 14:07:47 +0000 (+0200) Subject: Use textureSize() instead of sending in uniforms manually. Same result, less code... X-Git-Tag: 1.8.0~76^2~168 X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=670611b5a707fb8dcebf60fcfcd0930545d14875;hp=a10be5d6a8a0652e434b725528265729aff19d27;p=nageru Use textureSize() instead of sending in uniforms manually. Same result, less code, less error-prone. --- diff --git a/densify.vert b/densify.vert index b7f78d7..1859dfc 100644 --- a/densify.vert +++ b/densify.vert @@ -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, diff --git a/flow.cpp b/flow.cpp index 8b09c88..d425fe1 100644 --- 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 diff --git a/motion_search.frag b/motion_search.frag index cd251f4..05e901c 100644 --- a/motion_search.frag +++ b/motion_search.frag @@ -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. diff --git a/motion_search.vert b/motion_search.vert index 9e25e7a..28b65c2 100644 --- a/motion_search.vert +++ b/motion_search.vert @@ -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; diff --git a/prewarp.frag b/prewarp.frag index b9645bf..4b2db44 100644 --- a/prewarp.frag +++ b/prewarp.frag @@ -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); } diff --git a/sobel.frag b/sobel.frag index 518b6f8..90c6d8a 100644 --- a/sobel.frag +++ b/sobel.frag @@ -4,7 +4,6 @@ in vec2 tc; out vec2 gradients; uniform sampler2D tex; -uniform vec2 inv_image_size; void main() { diff --git a/sor.frag b/sor.frag index fbcc2e4..ac5b83c 100644 --- 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. diff --git a/sor.vert b/sor.vert index a42ebc7..201578c 100644 --- 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; }