[Mesa-dev] [PATCH 28/32] intel/isl/format: Add an srgb_to_linear helper

Pohjolainen, Topi topi.pohjolainen at gmail.com
Sat Jul 22 09:21:48 UTC 2017


On Wed, Jul 19, 2017 at 02:01:54PM -0700, Jason Ekstrand wrote:
> ---
>  src/intel/isl/gen_format_layout.py | 46 +++++++++++++++++++++++++++++++++++++-
>  src/intel/isl/isl.h                |  8 +++++++
>  2 files changed, 53 insertions(+), 1 deletion(-)
> 
> diff --git a/src/intel/isl/gen_format_layout.py b/src/intel/isl/gen_format_layout.py
> index aa4e2d8..0ca42db 100644
> --- a/src/intel/isl/gen_format_layout.py
> +++ b/src/intel/isl/gen_format_layout.py
> @@ -88,6 +88,19 @@ isl_format_layouts[] = {
>  
>  % endfor
>  };
> +
> +enum isl_format
> +isl_format_srgb_to_linear(enum isl_format format)
> +{
> +    switch (format) {
> +% for srgb, rgb in srgb_to_linear_map:
> +    case ISL_FORMAT_${srgb}:
> +        return ISL_FORMAT_${rgb};
> +%endfor
> +    default:
> +        return format;
> +    }
> +}
>  """)
>  
>  
> @@ -167,6 +180,34 @@ def reader(csvfile):
>              if line and not line[0].startswith('#'):
>                  yield line
>  
> +def get_srgb_to_linear_map(formats):
> +    """Compute a map from sRGB to linear formats.
> +
> +    This function uses some probably somewhat fragile string munging to do
> +    the conversion.  However, we do assert that, if it's SRGB, the munging
> +    succeeded so that gives some safety.
> +    """
> +    names = {f.name for f in formats}
> +    for fmt in formats:
> +        if fmt.colorspace != 'SRGB':
> +            continue
> +
> +        replacements = [
> +            ('_SRGB',   ''),
> +            ('SRGB',    'RGB'),
> +            ('U8SRGB',  'FLT16'),
> +        ]
> +
> +        found = False;
> +        for rep in replacements:
> +            rgb_name = fmt.name.replace(rep[0], rep[1])
> +            if rgb_name in names:
> +                found = True
> +                yield fmt.name, rgb_name
> +                break;
> +
> +        # We should have found a format name
> +        assert found
>  
>  def main():
>      """Main function."""
> @@ -183,11 +224,14 @@ def main():
>      # problem: Unicode can be rendered even if the shell calling this script
>      # doesn't.
>      with open(args.out, 'wb') as f:
> +        formats = [Format(l) for l in reader(args.csv)]
>          try:
>              # This basically does lazy evaluation and initialization, which
>              # saves on memory and startup overhead.
>              f.write(TEMPLATE.render(
> -                formats=(Format(l) for l in reader(args.csv))))
> +                formats             = formats,
> +                srgb_to_linear_map  = list(get_srgb_to_linear_map(formats)),
> +            ))
>          except Exception:
>              # In the even there's an error this imports some helpers from mako
>              # to print a useful stack trace and prints it, then exits with
> diff --git a/src/intel/isl/isl.h b/src/intel/isl/isl.h
> index 68bfcee..bc68e58 100644
> --- a/src/intel/isl/isl.h
> +++ b/src/intel/isl/isl.h
> @@ -1495,6 +1495,14 @@ isl_format_block_is_1x1x1(enum isl_format fmt)
>  }
>  
>  static inline bool
> +isl_format_is_srgb(enum isl_format fmt)
> +{
> +   return isl_format_layouts[fmt].colorspace == ISL_COLORSPACE_SRGB;
> +}

This put me off for a little while, it is in addition to what subject says but
really needed in the last patch.

> +
> +enum isl_format isl_format_srgb_to_linear(enum isl_format fmt);
> +
> +static inline bool
>  isl_format_is_rgb(enum isl_format fmt)
>  {
>     return isl_format_layouts[fmt].channels.r.bits > 0 &&
> -- 
> 2.5.0.400.gff86faf
> 
> _______________________________________________
> mesa-dev mailing list
> mesa-dev at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/mesa-dev


More information about the mesa-dev mailing list