From 24c21cf229d7e273ecc0e7ad0dc93bcb9fe466e3 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Wed, 18 Jul 2018 10:51:58 +0200 Subject: [PATCH] Use textureOffset() wherever possible to save some tedious arithmetic. --- derivatives.frag | 17 ++++++++--------- flow.cpp | 7 +------ sobel.frag | 28 ++++++++++------------------ 3 files changed, 19 insertions(+), 33 deletions(-) diff --git a/derivatives.frag b/derivatives.frag index f5394c3..e5e3557 100644 --- a/derivatives.frag +++ b/derivatives.frag @@ -4,19 +4,18 @@ in vec2 tc; out vec2 derivatives; uniform sampler2D tex; -uniform vec2 inv_image_size; void main() { - float x_m2 = texture(tex, vec2(tc.x - 2.0 * inv_image_size.x), tc.y).x; - float x_m1 = texture(tex, vec2(tc.x - inv_image_size.x), tc.y).x; - float x_p1 = texture(tex, vec2(tc.x + inv_image_size.x), tc.y).x; - float x_p2 = texture(tex, vec2(tc.x + 2.0 * inv_image_size.x), tc.y).x; + float x_m2 = textureOffset(tex, tc, ivec2(-2, 0)).x; + float x_m1 = textureOffset(tex, tc, ivec2(-1, 0)).x; + float x_p1 = textureOffset(tex, tc, ivec2( 1, 0)).x; + float x_p2 = textureOffset(tex, tc, ivec2( 2, 0)).x; - float y_m2 = texture(tex, vec2(tc.x, tc.y - 2.0 * inv_image_size.y)).x; - float y_m1 = texture(tex, vec2(tc.x, tc.y - inv_image_size.y)).x; - float y_p1 = texture(tex, vec2(tc.x, tc.y + inv_image_size.y)).x; - float y_p2 = texture(tex, vec2(tc.x, tc.y + 2.0 * inv_image_size.y)).x; + float y_m2 = textureOffset(tex, tc, ivec2( 0, -2)).x; + float y_m1 = textureOffset(tex, tc, ivec2( 0, -1)).x; + float y_p1 = textureOffset(tex, tc, ivec2( 0, 1)).x; + float y_p2 = textureOffset(tex, tc, ivec2( 0, 2)).x; derivatives.x = (x_p1 - x_m1) * (2.0/3.0) + (x_m2 - x_p2) * (1.0/12.0); derivatives.y = (y_p1 - y_m1) * (2.0/3.0) + (y_m2 - y_p2) * (1.0/12.0); diff --git a/flow.cpp b/flow.cpp index d5e73ec..9bae425 100644 --- a/flow.cpp +++ b/flow.cpp @@ -224,7 +224,7 @@ private: GLuint sobel_program; GLuint sobel_vao; - GLuint uniform_tex, uniform_image_size, uniform_inv_image_size; + GLuint uniform_tex, uniform_image_size; }; Sobel::Sobel() @@ -242,7 +242,6 @@ Sobel::Sobel() glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); uniform_tex = glGetUniformLocation(sobel_program, "tex"); - uniform_inv_image_size = glGetUniformLocation(sobel_program, "inv_image_size"); } void Sobel::exec(GLint tex0_view, GLint grad0_tex, int level_width, int level_height) @@ -251,7 +250,6 @@ void Sobel::exec(GLint tex0_view, GLint grad0_tex, int level_width, int level_he glBindTextureUnit(0, tex0_view); glBindSampler(0, nearest_sampler); glProgramUniform1i(sobel_program, uniform_tex, 0); - glProgramUniform2f(sobel_program, uniform_inv_image_size, 1.0f / level_width, 1.0f / level_height); GLuint grad0_fbo; // TODO: cleanup glCreateFramebuffers(1, &grad0_fbo); @@ -483,7 +481,6 @@ private: GLuint derivatives_vao; GLuint uniform_tex; - GLuint uniform_inv_image_size; }; Derivatives::Derivatives() @@ -502,7 +499,6 @@ Derivatives::Derivatives() glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); uniform_tex = glGetUniformLocation(derivatives_program, "tex"); - uniform_inv_image_size = glGetUniformLocation(derivatives_program, "inv_image_size"); } void Derivatives::exec(GLuint input_tex, GLuint output_tex, int level_width, int level_height) @@ -510,7 +506,6 @@ void Derivatives::exec(GLuint input_tex, GLuint output_tex, int level_width, int glUseProgram(derivatives_program); bind_sampler(derivatives_program, uniform_tex, 0, input_tex, nearest_sampler); - glProgramUniform2f(derivatives_program, uniform_inv_image_size, 1.0f / level_width, 1.0f / level_height); GLuint derivatives_fbo; // TODO: cleanup glCreateFramebuffers(1, &derivatives_fbo); diff --git a/sobel.frag b/sobel.frag index c4dabd5..518b6f8 100644 --- a/sobel.frag +++ b/sobel.frag @@ -26,24 +26,16 @@ void main() // Computing both directions at once allows us to get away with eight // texture samples instead of twelve. - float x_left = tc.x - inv_image_size.x; - float x_mid = tc.x; - float x_right = tc.x + inv_image_size.x; - - float y_top = tc.y + inv_image_size.y; // Note the bottom-left coordinate system. - float y_mid = tc.y; - float y_bottom = tc.y - inv_image_size.y; - - float top_left = texture(tex, vec2(x_left, y_top)).x; - float left = texture(tex, vec2(x_left, y_mid)).x; - float bottom_left = texture(tex, vec2(x_left, y_bottom)).x; - - float top = texture(tex, vec2(x_mid, y_top)).x; - float bottom = texture(tex, vec2(x_mid, y_bottom)).x; - - float top_right = texture(tex, vec2(x_right, y_top)).x; - float right = texture(tex, vec2(x_right, y_mid)).x; - float bottom_right = texture(tex, vec2(x_right, y_bottom)).x; + float top_left = textureOffset(tex, tc, ivec2(-1, 1)).x; // Note the bottom-left coordinate system. + float left = textureOffset(tex, tc, ivec2(-1, 0)).x; + float bottom_left = textureOffset(tex, tc, ivec2(-1, -1)).x; + + float top = textureOffset(tex, tc, ivec2( 0, 1)).x; + float bottom = textureOffset(tex, tc, ivec2( 0, -1)).x; + + float top_right = textureOffset(tex, tc, ivec2( 1, 1)).x; + float right = textureOffset(tex, tc, ivec2( 1, 0)).x; + float bottom_right = textureOffset(tex, tc, ivec2( 1, -1)).x; gradients.x = (top_right + 2.0f * right + bottom_right) - (top_left + 2.0f * left + bottom_left); gradients.y = (top_left + 2.0 * top + top_right) - (bottom_left + 2.0f * bottom + bottom_right); -- 2.39.2