[Mesa-dev] [PATCH 6/9] glsl: implement mid3 built-in function

Kenneth Graunke kenneth at whitecape.org
Tue Dec 10 23:47:50 PST 2013


On 12/10/2013 02:43 PM, =?UTF-8?q?Maxence=20Le=20Dor=C3=A9?= <Maxence Le
Doré> wrote:
> ---
>  src/glsl/builtin_functions.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 44 insertions(+)
> 
> diff --git a/src/glsl/builtin_functions.cpp b/src/glsl/builtin_functions.cpp
> index 1f21a37..b9beffd 100644
> --- a/src/glsl/builtin_functions.cpp
> +++ b/src/glsl/builtin_functions.cpp
> @@ -404,6 +404,7 @@ private:
>  
>     ir_expression *min3_expr(ir_variable *x, ir_variable *y, ir_variable *z);
>     ir_expression *max3_expr(ir_variable *x, ir_variable *y, ir_variable *z);
> +   ir_expression *mid3_expr(ir_variable *x, ir_variable *y, ir_variable *z);
>  
>     /**
>      * Call function \param f with parameters specified as the linked
> @@ -589,6 +590,11 @@ private:
>                                  const glsl_type *y_type,
>                                  const glsl_type *z_type);
>  
> +   ir_function_signature *_mid3(builtin_available_predicate avail,
> +                                const glsl_type *x_type,
> +                                const glsl_type *y_type,
> +                                const glsl_type *z_type);
> +
>  #undef B0
>  #undef B1
>  #undef B2
> @@ -2159,6 +2165,23 @@ builtin_builder::create_builtins()
>                  _max3(shader_trinary_mimax, glsl_type::uvec4_type, glsl_type::uvec4_type, glsl_type::uvec4_type),
>                  NULL);
>  
> +   add_function("mid3",
> +                _mid3(shader_trinary_mimax, glsl_type::float_type, glsl_type::float_type, glsl_type::float_type),
> +                _mid3(shader_trinary_mimax, glsl_type::vec2_type, glsl_type::vec2_type, glsl_type::vec2_type),
> +                _mid3(shader_trinary_mimax, glsl_type::vec3_type, glsl_type::vec3_type, glsl_type::vec3_type),
> +                _mid3(shader_trinary_mimax, glsl_type::vec4_type, glsl_type::vec4_type, glsl_type::vec4_type),
> +
> +                _mid3(shader_trinary_mimax, glsl_type::int_type, glsl_type::int_type, glsl_type::int_type),
> +                _mid3(shader_trinary_mimax, glsl_type::ivec2_type, glsl_type::ivec2_type, glsl_type::ivec2_type),
> +                _mid3(shader_trinary_mimax, glsl_type::ivec3_type, glsl_type::ivec3_type, glsl_type::ivec3_type),
> +                _mid3(shader_trinary_mimax, glsl_type::ivec4_type, glsl_type::ivec4_type, glsl_type::ivec4_type),
> +
> +                _mid3(shader_trinary_mimax, glsl_type::uint_type, glsl_type::uint_type, glsl_type::uint_type),
> +                _mid3(shader_trinary_mimax, glsl_type::uvec2_type, glsl_type::uvec2_type, glsl_type::uvec2_type),
> +                _mid3(shader_trinary_mimax, glsl_type::uvec3_type, glsl_type::uvec3_type, glsl_type::uvec3_type),
> +                _mid3(shader_trinary_mimax, glsl_type::uvec4_type, glsl_type::uvec4_type, glsl_type::uvec4_type),
> +                NULL);
> +
>  #undef F
>  #undef FI
>  #undef FIU
> @@ -2386,6 +2409,12 @@ builtin_builder::max3_expr(ir_variable *x, ir_variable *y, ir_variable *z)
>     return max(x, max(y,z));
>  }
>  
> +ir_expression *
> +builtin_builder::mid3_expr(ir_variable *x, ir_variable *y, ir_variable *z)
> +{
> +   return max(min(x, y), min(y, z));

This is broken if y is the median.  Consider the following cases:

x < y < z --> max(x, y) -> y ==> works
x < z < y --> max(x, z) -> z ==> works
y < x < z --> max(y, y) -> y ==> broken (selects least, not median)
y < z < x --> max(y, y) -> y ==> broken (selects least, not median)
z < x < y --> max(x, z) -> x ==> works
z < y < x --> max(y, z) -> y ==> works

> +}
> +
>  ir_call *
>  builtin_builder::call(ir_function *f, ir_variable *ret, exec_list params)
>  {
> @@ -4086,6 +4115,21 @@ builtin_builder::_max3(builtin_available_predicate avail,
>     return sig;
>  }
>  
> +ir_function_signature *
> +builtin_builder::_mid3(builtin_available_predicate avail,
> +                       const glsl_type *x_type, const glsl_type *y_type,
> +                       const glsl_type *z_type)
> +{
> +   ir_variable *x = in_var(x_type, "x");
> +   ir_variable *y = in_var(y_type, "y");
> +   ir_variable *z = in_var(z_type, "z");
> +   MAKE_SIG(x_type, avail, 3, x, y, z);
> +
> +   body.emit(ret(mid3_expr(x, y, z)));
> +
> +   return sig;
> +}
> +
>  /** @} */
>  
>  /******************************************************************************/
> 



More information about the mesa-dev mailing list