mesa: Autogenerate most of format_pack.c

We were auto-generating it before.  The problem was that the autogeneration
tool we were using was called "copy, paste, and edit".  Let's use a more
sensible solution.

Signed-off-by: Jason Ekstrand <jason.ekstrand@intel.com>

v2 by Samuel Iglesias <siglesias@igalia.com>
- Remove format_pack.c as it is now autogenerated
- Add usage of INDENT_FLAGS in Makefile.am
- Remove trailing blank line

v3 by Samuel Iglesias <siglesias@igalia.com>
- Merge format_convert.py into format_parser.py
   - Adapt pack_*_* function generations
- Fix out-of-tree build

v4 by Samuel Iglesias <siglesias@igalia.com>
- _get_datatype() is now a helper function

v5 by Samuel Iglesias <siglesias@igalia.com>
- format_pack.c.mako is now format_pack.py, with the template code
  inlined. It now auto-generates format_pack.c
- Simplify Makefile.am change.
- Modify SConscript to build format_pack.c with scons.
- Remove run_mako.py
- Add format_pack.c to gitignore

v6 by Samuel Iglesias <siglesias@igalia.com>:
- Don't allow float to non-normalized integer format conversions.
- Add non-normalized formats support for ubyte packing functions. Merge
the previously separated patch.
- Add clamping for non-normalized integer formats in pack_ubyte*()

Signed-off-by: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
mesa: Autogenerate format_unpack.c

Signed-off-by: Jason Ekstrand <jason.ekstrand@intel.com>

v2 by Samuel Iglesias <siglesias@igalia.com>:
- Add usage of INDENT_FLAGS in Makefile.am

v3 by Samuel Iglesias <siglesias@igalia.com>:
- Modify unpack_float_*() and unpack_ubyte_*() function generation
to use c.datatype() and f.datatype()
- Fix out-of-tree build

v4 by Samuel Iglesias <siglesias@igalia.com>:
- format_unpack.c.mako is now format_unpack.py, with the template code
  inlined. It now auto-generates format_unpack.c
- Add format_unpack.c to gitignore.
- Simplify Makefile.am change
- Modify SConscript to build format_unpack.c with scons

Reviewed-by: Jason Ekstrand <jason.ekstrand@intel.com>
mesa: Add _mesa_pack_uint_rgba_row() format conversion function

We will use this later on to handle uint conversion scenarios in a master
convert function.

v2:
- Modify pack_uint_*() function generation to use c.datatype() and
  f.datatype().
- Remove UINT_TO_FLOAT() macro usage from pack_uint*()
- Remove "if not f.is_normalized()" conditional as pack_uint*()
  functions are only autogenerated for non normalized formats.

v3:
- Add clamping for non-normalized integer formats in pack_uint*()

Signed-off-by: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
Reviewed-by: Jason Ekstrand <jason.ekstrand@intel.com>
mesa/format_pack: Add _mesa_pack_int_rgba_row()

This will be used to unify code in pack.c.

v2:
- Modify pack_int_*() function generator to use c.datatype() and
  f.datatype()

Signed-off-by: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
mesa/formats: add new mesa formats and their pack/unpack functions.

This  will be used to refactor code in pack.c and support conversion
to/from these types in a master convert function that will be added
later.

v2:
- Fix autogeneration of MESA_FORMAT_A2R10G10B10_UNORM pack/unpack
  functions

Signed-off-by: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
Reviewed-by: Jason Ekstrand <jason.ekstrand@intel.com>
mesa: use format conversion functions in swrast

This commit adds a macro to facilitate the task of using
format conversions functions but keeps the same API.

Signed-off-by: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
Reviewed-by: Jason Ekstrand <jason.ekstrand@intel.com>
mesa/pack: use autogenerated format_pack functions

Take advantage of new mesa formats and new format_pack functions to
reduce source code in _mesa_pack_rgba_span_from_ints() and
_mesa_pack_rgba_span_from_uints().

Signed-off-by: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
Reviewed-by: Jason Ekstrand <jason.ekstrand@intel.com>
mesa/main/pack_tmp.h: Add float conversion support

We will use this in a later patch to refactor _mesa_pack_rgba_span_float.

Signed-off-by: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
Reviewed-by: Jason Ekstrand <jason.ekstrand@intel.com>
mesa/pack: refactor _mesa_pack_rgba_span_float()

Use autogenerated format pack functions and take advantage of some
macros to reduce source code, facilitating its maintenance.

Unfortunately, dstType == GL_UNSIGNED_SHORT cannot simplified like
the others, so keep it as it is.

Signed-off-by: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
Reviewed-by: Jason Ekstrand <jason.ekstrand@intel.com>
mesa: Add an implementation of a master convert function.

v2 by Iago Toral <itoral@igalia.com>:

- When testing if we can directly pack we should use the src format to check
  if we are packing from an RGBA format. The original code used the dst format
  for the ubyte case by mistake.
- Fixed incorrect number of bits for dst, it was computed using the src format
  instead of the dst format.
- If the dst format is an array format, check if it is signed. We were only
  checking this for the case where it was not an array format, but we need
  to know this in both scenarios.
- Fixed incorrect swizzle transform for the cases where we convert between
  array formats.
- Compute is_signed and bits only once and for the dst format. We were
  computing these for the src format too but they were overwritten by the
  dst values immediately after.
- Be more careful when selecting the integer path. Specifically, check that
  both src and dst are integer types. Checking only one of them should suffice
  since OpenGL does not allow conversions between normalized and integer types,
  but putting extra care here makes sense and also makes the actual requirements
  for this path more clear.
- The format argument for pack functions is the destination format we are
  packing to, not the source format (which has to be RGBA).
- Expose RGBA8888_* to other files. These will come in handy when in need to
  test if a given array format is RGBA or in need to pass RGBA formats to
  mesa_format_convert.

v3 by Samuel Iglesias <siglesias@igalia.com>:

- Add an RGBA8888_INT definition.

v4 by Iago Toral <itoral@igalia.com> after review by Jason Ekstrand:

- Added documentation for _mesa_format_convert.
- Added additional explanatory comments for integer conversions.
- Ensure that we use _messa_swizzle_and_convert for all signed source formats.
- Squashed: do not directly (un)pack to RGBA UINT if the source is not unsigned.

v5 by Iago Toral <itoral@igalia.com>:

- Adapted to the new implementation of mesa_array_format as a plain uint32_t
  bitfield.

Reviewed-by: Jason Ekstrand <jason.ekstrand@intel.com>
mesa: Expose compute_component_mapping as _mesa_compute_component_mapping

This is necessary to handle conversions between array types where
the driver does not support the dst format requested by the client and
chooses a different format instead.

We will need this in _mesa_format_convert, so move it to format_utils.c,
prefix it with '_mesa_' and make it available to other files.

v2:
  - Move _mesa_compute_component_mapping to glformats

Reviewed-by: Jason Ekstrand <jason.ekstrand@intel.com>
mesa: Add a rebase_swizzle parameter to _mesa_format_convert

The new parameter allows callers to provide a rebase swizzle that
the function needs to use to match the requirements of the base
internal format involved. This is necessary when the source or
destination internal formats (depending on whether we are doing
the conversion for a pixel download or a pixel upload respectively)
do not match the base formats of the source or destination
formats of the conversion. This can happen when the driver does not
support the internal formats and uses a different format to store
pixel data internally.

For example, a texture upload from RGB to Luminance in a driver
that does not support textures with a Luminance format may decide
to store the Luminance data as RGBA. In this case we want to store
the RGBA values as (R,R,R,1). Following the same example, when we
download from that texture to RGBA we want to read (R,0,0,1). The
rebase_swizzle parameter allows these transforms to happen.

Reviewed-by: Jason Ekstrand <jason.ekstrand@intel.com>
mesa: Add a helper _mesa_compute_rgba2base2rgba_component_mapping

This will come in handy when callers of _mesa_format_convert need
to compute the rebase swizzle parameter to use.

Reviewed-by: Jason Ekstrand <jason.ekstrand@intel.com>
mesa: Add helper to convert a GL format and type to a mesa (array) format.

v2 after review by Jason Ekstrand:
- Move _mesa_format_from_format_and_type to glformats
- Return a mesa_format for GL_UNSIGNED_INT_8_8_8_8(_REV)

v3:
- Adapted to the new implementation of mesa_array_format as a plain uint32_t
  bitfield.

Reviewed-by: Jason Ekstrand <jason.ekstrand@intel.com>
mesa/pack: use _mesa_format_from_format_and_type in _mesa_pack_rgba_span_from_*

We had previously added the needed mesa formats, so we can simplify
the code further.

Signed-off-by: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
Reviewed-by: Jason Ekstrand <jason.ekstrand@intel.com>
mesa: Add _mesa_swap2_copy and _mesa_swap4_copy

We have _mesa_swap{2,4} but these do in-place byte-swapping only. The new
functions receive an extra parameter so we can swap bytes on a source
input array and store the results in a (possibly different) destination
array.

This is useful to implement byte-swapping in pixel uploads, since in this
case we need to swap bytes on the src data which is owned by the
application so we can't do an in-place byte swap.

Reviewed-by: Jason Ekstrand <jason.ekstrand@intel.com>
mesa: Add RGBA to Luminance conversion helpers

For glReadPixels with a Luminance destination format we compute luminance
values from RGBA as L=R+G+B. This, however, requires ad-hoc implementation,
since pack/unpack functions or _mesa_swizzle_and_convert won't do this
(and thus, neither will _mesa_format_convert). This patch adds helpers
to do this computation so they can be used to support conversion to luminance
formats.

The current implementation of glReadPixels does this computation as part
of the span functions in pack.c (see _mesa_pack_rgba_span_float), that do
this together with other things like type conversion, etc. We do not want
to use these functions but use _mesa_format_convert instead (later patches
will remove the color span functions), so we need to extract this functionality
as helpers.
mesa: Add helpers to extract GL_COLOR_INDEX to RGBA float/ubyte

We only use _mesa_make_temp_ubyte_image in texstore.c to convert
GL_COLOR_INDEX to RGBA, but this helper does more stuff than this.
All uses of this helper can be replaced with calls to
_mesa_format_convert except for this GL_COLOR_INDEX conversion.

This patch extracts the GL_COLOR_INDEX to RGBA logic to a separate
helper so we can use that instead from texstore.c.

In future patches we will replace all remaining calls to
_mesa_make_temp_ubyte_image in the repository (related to compressed
formats) with calls to _mesa_format_convert so we can remove
_mesa_make_temp_ubyte_image and related functions.

v2:
- Remove ‘for’ loop initial declaration. They are only allowed in C99 or C11
mode.

Reviewed-by: Jason Ekstrand <jason.ekstrand@intel.com>
mesa: Use _mesa_format_convert to implement texstore_rgba.

Notice that _mesa_format_convert does not handle byte-swapping scenarios,
GL_COLOR_INDEX or MESA_FORMAT_YCBCR(_REV), so these must be handled
separately.

Also, remove all the code that goes unused after using _mesa_format_convert.

Reviewed-by: Jason Ekstrand <jason.ekstrand@intel.com>
mesa: use _mesa_format_convert to implement glReadPixels.