<div dir="ltr"><p dir="ltr"><br>
On Jun 7, 2016 5:32 PM, "Dylan Baker" <<a href="mailto:dylan@pnwbakers.com" target="_blank">dylan@pnwbakers.com</a>> wrote:<br>
><br>
> This replaces the current bash generator with a python based generator<br>
> using mako. It's quite fast and works with both python 2.7 and python<br>
> 3.5, and should work with 3.3+ and maybe even 3.2.<br>
><br>
> It produces an almost identical file except for a minor layout changes,<br>
> and the addition of a "generated file, do not edit" warning.<br>
><br>
> Signed-off-by: Dylan Baker <<a href="mailto:dylanx.c.baker@intel.com" target="_blank">dylanx.c.baker@intel.com</a>><br>
> ---<br>
> src/intel/isl/Makefile.am | 7 +-<br>
> src/intel/isl/gen_format_layout.c.mako | 77 +++++++++++++++++<br>
> src/intel/isl/gen_format_layout.py | 143 +++++++++++++++++++++++++++++++<br>
> src/intel/isl/isl_format_layout_gen.bash | 129 ----------------------------<br>
> 4 files changed, 224 insertions(+), 132 deletions(-)<br>
> create mode 100644 src/intel/isl/gen_format_layout.c.mako<br>
> create mode 100644 src/intel/isl/gen_format_layout.py<br>
> delete mode 100755 src/intel/isl/isl_format_layout_gen.bash<br>
><br>
> diff --git a/src/intel/isl/Makefile.am b/src/intel/isl/Makefile.am<br>
> index 74f863a..ee75bbd 100644<br>
> --- a/src/intel/isl/Makefile.am<br>
> +++ b/src/intel/isl/Makefile.am<br>
> @@ -66,10 +66,11 @@ libisl_gen9_la_CFLAGS = $(libisl_la_CFLAGS) -DGEN_VERSIONx10=90<br>
><br>
> BUILT_SOURCES = $(ISL_GENERATED_FILES)<br>
><br>
> -isl_format_layout.c: isl_format_layout_gen.bash \<br>
> +isl_format_layout.c: gen_format_layout.py \<br>
> + gen_format_layout.c.mako \<br>
> isl_format_layout.csv<br>
> - $(AM_V_GEN)$(srcdir)/isl_format_layout_gen.bash \<br>
> - <$(srcdir)/isl_format_layout.csv >$@<br>
> + $(PYTHON_GEN) $(AM_V_GEN)$(srcdir)/gen_format_layout.py \<br>
> + <$(srcdir)/isl_format_layout.csv<br>
><br>
> # ----------------------------------------------------------------------------<br>
> # Tests<br>
> diff --git a/src/intel/isl/gen_format_layout.c.mako b/src/intel/isl/gen_format_layout.c.mako</p>
<p dir="ltr">I made this comment in the office today but I think the mako here is simple enough that we might be better off just putting it all in one file.</p>
<p dir="ltr">> new file mode 100644<br>
> index 0000000..241a923<br>
> --- /dev/null<br>
> +++ b/src/intel/isl/gen_format_layout.c.mako<br>
> @@ -0,0 +1,77 @@<br>
> +## encoding=utf-8<br>
> +## Copyright © 2016 Intel Corporation<br>
> +##<br>
> +## Permission is hereby granted, free of charge, to any person obtaining a copy<br>
> +## of this software and associated documentation files (the "Software"), to deal<br>
> +## in the Software without restriction, including without limitation the rights<br>
> +## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell<br>
> +## copies of the Software, and to permit persons to whom the Software is<br>
> +## furnished to do so, subject to the following conditions:<br>
> +##<br>
> +## The above copyright notice and this permission notice shall be included in<br>
> +## all copies or substantial portions of the Software.<br>
> +##<br>
> +## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
> +## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
> +## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE<br>
> +## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
> +## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,<br>
> +## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE<br>
> +## SOFTWARE.<br>
> +##<br>
> +## This is the mako template companion to gen_format_layout.py<br>
> +##<br>
> +/* This file is autogenerated by gen_format_layout.{c.mako,py}. DO NOT EDIT! */<br>
> +<br>
> +/*<br>
> + * Copyright 2015 Intel Corporation<br>
> + *<br>
> + * Permission is hereby granted, free of charge, to any person obtaining a<br>
> + * copy of this software and associated documentation files (the "Software"),<br>
> + * to deal in the Software without restriction, including without limitation<br>
> + * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
> + * and/or sell copies of the Software, and to permit persons to whom the<br>
> + * Software is furnished to do so, subject to the following conditions:<br>
> + *<br>
> + * The above copyright notice and this permission notice (including the next<br>
> + * paragraph) shall be included in all copies or substantial portions of the<br>
> + * Software.<br>
> + *<br>
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL<br>
> + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
> + * IN THE SOFTWARE.<br>
> + */<br>
> +<br>
> +#include "isl.h"<br>
> +<br>
> +const struct isl_format_layout<br>
> +isl_format_layouts[] = {<br>
> +% for format in formats:<br>
> + [ISL_FORMAT_${<a href="http://format.name" target="_blank">format.name</a>}] = {<br>
> + .format = ISL_FORMAT_${<a href="http://format.name" target="_blank">format.name</a>},<br>
> + .name = "ISL_FORMAT_${<a href="http://format.name" target="_blank">format.name</a>}",<br>
> + .bs = ${<a href="http://format.bs" target="_blank">format.bs</a>},<br>
> + .bw = ${<a href="http://format.bw" target="_blank">format.bw</a>},<br>
> + .bh = ${<a href="http://format.bh" target="_blank">format.bh</a>},<br>
> + .bd = ${<a href="http://format.bd" target="_blank">format.bd</a>},<br>
> + .channels = {<br>
> + % for mask in ['r', 'g', 'b', 'a', 'l', 'i', 'p']:<br>
> + <% channel = getattr(format, mask, None) %>\<br>
> + % if channel.type is not None:<br>
> + .${mask} = { ISL_${channel.type}, ${channel.size} },<br>
> + % else:<br>
> + .${mask} = {},<br>
> + % endif<br>
> + % endfor<br>
> + },<br>
> + .colorspace = ISL_COLORSPACE_${format.colorspace},<br>
> + .txc = ISL_TXC_${format.txc},<br>
> + },<br>
> +<br>
> +% endfor<br>
> +};<br>
> +<br>
> diff --git a/src/intel/isl/gen_format_layout.py b/src/intel/isl/gen_format_layout.py<br>
> new file mode 100644<br>
> index 0000000..314e4ca<br>
> --- /dev/null<br>
> +++ b/src/intel/isl/gen_format_layout.py<br>
> @@ -0,0 +1,143 @@<br>
> +# encoding=utf-8<br>
> +# Copyright © 2016 Intel Corporation<br>
> +<br>
> +# Permission is hereby granted, free of charge, to any person obtaining a copy<br>
> +# of this software and associated documentation files (the "Software"), to deal<br>
> +# in the Software without restriction, including without limitation the rights<br>
> +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell<br>
> +# copies of the Software, and to permit persons to whom the Software is<br>
> +# furnished to do so, subject to the following conditions:<br>
> +<br>
> +# The above copyright notice and this permission notice shall be included in<br>
> +# all copies or substantial portions of the Software.<br>
> +<br>
> +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
> +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
> +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE<br>
> +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
> +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,<br>
> +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE<br>
> +# SOFTWARE.<br>
> +<br>
> +"""Generates isl_format_layout.c."""<br>
> +<br>
> +from __future__ import (<br>
> + absolute_import, division, print_function, unicode_literals<br>
> +)<br>
> +import csv<br>
> +import re<br>
> +<br>
> +from mako import template<br>
> +<br>
> +# Load the template, ensure that __future__.division is imported, and set the<br>
> +# bytes encoding to be utf-8. This last bit is important to getting simple<br>
> +# consistent behavior for python 3 when we get there.<br>
> +TEMPLATE = template.Template(filename="gen_format_layout.c.mako",<br>
> + future_imports=['division'],<br>
> + output_encoding='utf-8')<br>
> +<br>
> +<br>
> +class Channel(object):<br>
> + """Class representing a Channel.<br>
> +<br>
> + Converts the csv encoded data into the format that the template (and thus<br>
> + the consuming C code) expects.<br>
> +<br>
> + """<br>
> + # If the csv file grew very large this class could be put behind a factory<br>
> + # to increase efficiency. Right now though it's fast enough that It didn't<br>
> + # seem worthwhile to add all of the boilerplate<br>
> + _types = {<br>
> + 'x': 'void',<br>
> + 'r': 'raw',<br>
> + 'un': 'unorm',<br>
> + 'sn': 'snorm',<br>
> + 'uf': 'ufloat',<br>
> + 'sf': 'sfloat',<br>
> + 'ux': 'ufixed',<br>
> + 'sx': 'sfixed',<br>
> + 'ui': 'uint',<br>
> + 'si': 'sint',<br>
> + 'us': 'uscaled',<br>
> + 'ss': 'sscaled',<br>
> + }<br>
> + _splitter = re.compile(r'\s*(?P<type>[a-z]+)(?P<size>[0-9]+)')<br>
> +<br>
> + def __init__(self, line):<br>
> + # If the line is just whitespace then just set everything to None to<br>
> + # save on the regex cost and let the template skip on None.<br>
> + if line.isspace():<br>
> + self.size = None<br>
> + self.type = None<br>
> + else:<br>
> + grouped = self._splitter.match(line)<br>
> + self.type = self._types[grouped.group('type')].upper()<br>
> + self.size = grouped.group('size')<br>
> +<br>
> +<br>
> +class Format(object):<br>
> + """Class taht contains all values needed by the template."""<br>
> + def __init__(self, line):<br>
> + # pylint: disable=invalid-name<br>
> + <a href="http://self.name" target="_blank">self.name</a> = line[0].strip()<br>
> +<br>
> + # Future division makes this work in python 2.<br>
> + <a href="http://self.bs" target="_blank">self.bs</a> = int(line[1]) // 8<br>
> + <a href="http://self.bw" target="_blank">self.bw</a> = line[2].strip()<br>
> + <a href="http://self.bh" target="_blank">self.bh</a> = line[3].strip()<br>
> + <a href="http://self.bd" target="_blank">self.bd</a> = line[4].strip()<br>
> + self.r = Channel(line[5])<br>
> + self.g = Channel(line[6])<br>
> + self.b = Channel(line[7])<br>
> + self.a = Channel(line[8])<br>
> + self.l = Channel(line[9])<br>
> + self.i = Channel(line[10])<br>
> + self.p = Channel(line[11])<br>
> +<br>
> + # alpha doesn't have a colorspace of it's own.<br>
> + self.colorspace = line[12].strip().upper()<br>
> + if self.colorspace in ['', 'ALPHA']:<br>
> + self.colorspace = 'NONE'<br>
> +<br>
> + # This sets it to the line value, or if it's an empty string 'NONE'<br>
> + self.txc = line[13].strip().upper() or 'NONE'<br>
> +<br>
> +<br>
> +def reader():<br>
> + """Wrapper around csv.reader that skips comments and blanks."""<br>
> + # csv.reader actually reads the file one line at a time (it was designed to<br>
> + # open excel generated sheets), so hold the file until all of the lines are<br>
> + # read.<br>
> + with open('isl_format_layout.csv', 'r') as f:</p>
<p dir="ltr">I'm not so sure this works. It's probably better to pass the file name in. Maybe this is safe but I'm skeptical.</p><p>Other than those two comments, this seems perfectly reasonable.<br></p><p dir="ltr">> + for line in csv.reader(f):<br>
> + if line and not line[0].startswith('#'):<br>
> + yield line<br>
> +<br>
> +<br>
> +def main():<br>
> + """Main function."""<br>
> + # This generator opens and writes the file itself, and it does so in bytes<br>
> + # mode. This solves both python 2 vs 3 problems and solves the locale<br>
> + # problem: Unicode can be rendered even if the shell calling this script<br>
> + # doesn't.<br>
> + with open('isl_format_layout.c', 'wb') as f:<br>
> + try:<br>
> + # This basically does lazy evaluation and initialization, which<br>
> + # saves on memory and startup overhead.<br>
> + f.write(TEMPLATE.render(formats=(Format(l) for l in reader())))<br>
> + except Exception:<br>
> + # In the even there's an error this imports some helpers from mako<br>
> + # to print a useful stack trace and prints it, then exits with<br>
> + # status 1, if python is run with debug; otherwise it just raises<br>
> + # the exception<br>
> + if __debug__:<br>
> + import sys<br>
> + from mako import exceptions<br>
> + print(exceptions.text_error_template().render(),<br>
> + file=sys.stderr)<br>
> + sys.exit(1)<br>
> + raise<br>
> +<br>
> +<br>
> +if __name__ == '__main__':<br>
> + main()<br>
> diff --git a/src/intel/isl/isl_format_layout_gen.bash b/src/intel/isl/isl_format_layout_gen.bash<br>
> deleted file mode 100755<br>
> index e20da55..0000000<br>
> --- a/src/intel/isl/isl_format_layout_gen.bash<br>
> +++ /dev/null<br>
> @@ -1,129 +0,0 @@<br>
> -#!/usr/bin/env bash<br>
> -#<br>
> -# Copyright 2015 Intel Corporation<br>
> -#<br>
> -# Permission is hereby granted, free of charge, to any person obtaining a<br>
> -# copy of this software and associated documentation files (the "Software"),<br>
> -# to deal in the Software without restriction, including without limitation<br>
> -# the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
> -# and/or sell copies of the Software, and to permit persons to whom the<br>
> -# Software is furnished to do so, subject to the following conditions:<br>
> -#<br>
> -# The above copyright notice and this permission notice (including the next<br>
> -# paragraph) shall be included in all copies or substantial portions of the<br>
> -# Software.<br>
> -#<br>
> -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
> -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
> -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL<br>
> -# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
> -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
> -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
> -# IN THE SOFTWARE.<br>
> -<br>
> -set -eu<br>
> -set -o pipefail<br>
> -<br>
> -cat <<'EOF'<br>
> -/*<br>
> - * Copyright 2015 Intel Corporation<br>
> - *<br>
> - * Permission is hereby granted, free of charge, to any person obtaining a<br>
> - * copy of this software and associated documentation files (the "Software"),<br>
> - * to deal in the Software without restriction, including without limitation<br>
> - * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
> - * and/or sell copies of the Software, and to permit persons to whom the<br>
> - * Software is furnished to do so, subject to the following conditions:<br>
> - *<br>
> - * The above copyright notice and this permission notice (including the next<br>
> - * paragraph) shall be included in all copies or substantial portions of the<br>
> - * Software.<br>
> - *<br>
> - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
> - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
> - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL<br>
> - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
> - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
> - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
> - * IN THE SOFTWARE.<br>
> - */<br>
> -<br>
> -#include "isl.h"<br>
> -<br>
> -const struct isl_format_layout<br>
> -isl_format_layouts[] = {<br>
> -EOF<br>
> -<br>
> -sed -r '<br>
> -# Delete comment lines and empty lines<br>
> -/^[[:space:]]*#/d<br>
> -/^[[:space:]]*$/d<br>
> -<br>
> -# Delete spaces<br>
> -s/[[:space:]]//g<br>
> -<br>
> -# Translate formats<br>
> -s/^([A-Za-z0-9_]+),*/ISL_FORMAT_\1,/<br>
> -<br>
> -# Translate data type of channels<br>
> -s/\<x([0-9]+),/ISL_VOID@\1,/g<br>
> -s/\<r([0-9]+),/ISL_RAW@\1,/g<br>
> -s/\<un([0-9]+),/ISL_UNORM@\1,/g<br>
> -s/\<sn([0-9]+),/ISL_SNORM@\1,/g<br>
> -s/\<uf([0-9]+),/ISL_UFLOAT@\1,/g<br>
> -s/\<sf([0-9]+),/ISL_SFLOAT@\1,/g<br>
> -s/\<ux([0-9]+),/ISL_UFIXED@\1,/g<br>
> -s/\<sx([0-9]+),/ISL_SFIXED@\1,/g<br>
> -s/\<ui([0-9]+),/ISL_UINT@\1,/g<br>
> -s/\<si([0-9]+),/ISL_SINT@\1,/g<br>
> -s/\<us([0-9]+),/ISL_USCALED@\1,/g<br>
> -s/\<ss([0-9]+),/ISL_SSCALED@\1,/g<br>
> -<br>
> -# Translate colorspaces<br>
> -# Interpret alpha-only formats as having no colorspace.<br>
> -s/\<(linear|srgb|yuv)\>/ISL_COLORSPACE_\1/<br>
> -s/\<alpha\>//<br>
> -<br>
> -# Translate texture compression<br>
> -s/\<(dxt|fxt|rgtc|bptc|etc|astc)([0-9]*)\>/ISL_TXC_\1\2/<br>
> -' |<br>
> -tr 'a-z' 'A-Z' | # Convert to uppersace<br>
> -while IFS=, read -r format bpb bw bh bd \<br>
> - red green blue alpha \<br>
> - luminance intensity palette \<br>
> - colorspace txc<br>
> -do<br>
> - : ${colorspace:=ISL_COLORSPACE_NONE}<br>
> - : ${txc:=ISL_TXC_NONE}<br>
> -<br>
> - cat <<EOF<br>
> - [$format] = {<br>
> - .format = $format,<br>
> - .name = "$format",<br>
> - .bs = $((bpb/8)),<br>
> - .bw = $bw, .bh = $bh, .bd = $bd,<br>
> - .channels = {<br>
> - .r = { $red },<br>
> - .g = { $green },<br>
> - .b = { $blue },<br>
> - .a = { $alpha },<br>
> - .l = { $luminance },<br>
> - .i = { $intensity },<br>
> - .p = { $palette },<br>
> - },<br>
> - .colorspace = $colorspace,<br>
> - .txc = $txc,<br>
> - },<br>
> -<br>
> -EOF<br>
> -done |<br>
> -sed -r '<br>
> -# Collapse empty channels<br>
> -s/\{ \}/{}/<br>
> -<br>
> -# Split non-empty channels into two members: base type and bit size<br>
> -s/@/, /<br>
> -'<br>
> -<br>
> -# Terminate the table<br>
> -printf '};\n'<br>
> --<br>
> 2.8.3<br>
><br>
><br>
> _______________________________________________<br>
> mesa-dev mailing list<br>
> <a href="mailto:mesa-dev@lists.freedesktop.org" target="_blank">mesa-dev@lists.freedesktop.org</a><br>
> <a href="https://lists.freedesktop.org/mailman/listinfo/mesa-dev" target="_blank">https://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
><br>
</p>
</div>