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

Roland Scheidegger sroland at vmware.com
Wed Dec 11 07:45:01 PST 2013


Wouldn't something like max(min(x, max(y,z)), min(y, z)) work as well,
saving one instruction?

Roland

Am 11.12.2013 08:52, schrieb Mario Rugiero:
> Why not computing the max3(min(x,y),min(x,z),min(y,z))?
> 
> For the six possible cases:
> x < y < z --> max(x, x, y) -> y ==> works
> x < z < y --> max(x, z, z) -> z ==> works
> y < x < z --> max(y, x, y) -> x ==> works
> y < z < x --> max(y, z, y) -> z ==> works
> z < x < y --> max(x, z, z) -> x ==> works
> z < y < x --> max(y, z, z) -> y ==> works
> 
> 
> 2013/12/11 Kenneth Graunke <kenneth at whitecape.org
> <mailto:kenneth at whitecape.org>>
> 
>     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;
>     > +}
>     > +
>     >  /** @} */
>     >
>     >
>      /******************************************************************************/
>     >
> 
>     _______________________________________________
>     mesa-dev mailing list
>     mesa-dev at lists.freedesktop.org <mailto:mesa-dev at lists.freedesktop.org>
>     http://lists.freedesktop.org/mailman/listinfo/mesa-dev
>     <https://urldefense.proofpoint.com/v1/url?u=http://lists.freedesktop.org/mailman/listinfo/mesa-dev&k=oIvRg1%2BdGAgOoM1BIlLLqw%3D%3D%0A&r=F4msKE2WxRzA%2BwN%2B25muztFm5TSPwE8HKJfWfR2NgfY%3D%0A&m=P0UPWHkjwggjyMzQsGbIJge0JmImxQOJ8abTwIlVgak%3D%0A&s=e5fb02199165025279af0affc1dac271265febe087f6ec38fb650691002164d2>
> 
> 
> 
> 
> _______________________________________________
> mesa-dev mailing list
> mesa-dev at lists.freedesktop.org
> https://urldefense.proofpoint.com/v1/url?u=http://lists.freedesktop.org/mailman/listinfo/mesa-dev&k=oIvRg1%2BdGAgOoM1BIlLLqw%3D%3D%0A&r=F4msKE2WxRzA%2BwN%2B25muztFm5TSPwE8HKJfWfR2NgfY%3D%0A&m=P0UPWHkjwggjyMzQsGbIJge0JmImxQOJ8abTwIlVgak%3D%0A&s=e5fb02199165025279af0affc1dac271265febe087f6ec38fb650691002164d2
> 


More information about the mesa-dev mailing list