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.

Signed-off-by: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
mesa: Add non-normalized formats support for ubyte packing functions

Signed-off-by: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
mesa/format_pack: Add _mesa_pack_int_rgba_row()

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

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.

Signed-off-by: Samuel Iglesias Gonsalvez <siglesias@igalia.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>
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>
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>
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>
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.
mesa: Set normalized=true for float array formats.

In order to check if a format is normalized Mesa does something like this:
normalized = !_mesa_is_enum_format_integer(srcFormat);

So all float types will set normalized to true. Since our mesa_array_format
includes a normalized flag for each type we want to make it consistent with
this.
mesa: Do not assert on integer<->non-integer direct pack/unpack fast paths

We can have conversions from non-integer types to integer types, so remove
the assertions for these in the pack/unpack fast paths. It could be that
we do not have all the necessary pack/unpack functions in these cases though,
so protect these paths with conditionals and let _mesa_format_convert use
other paths to resolve these kind of conversions if necessary.
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.
mesa: Consider internal base format in _mesa_format_convert

Add a dst_internal_format parameter to _mesa_format_convert, that represents
the base internal format for texture/pixel uploads, so we can do the right
thing when the driver has selected a different internal format for the target
dst format.
mesa: Avoid pack/unpack fast paths if base internal format != base format

In general, if the dst base internal format and the selected dst format are
different we can't be sure that directly packing or unpacking to the destination
format will produce correct results. In these cases we probably want to fall
back to other paths (like mesa_swizzle_and_convert) that can handle these
situations better.

An example of this includes a luminance internal format for which the driver
decided to use something like BGRA. In these case, unpacking to BGRA won't
match the requirements of the luminance internal format.

In the future we may want to allow these fast paths for specific cases
where we know the direct pack/unpack functions will do the right thing.
mesa: Add helper to convert a GL format and type to a mesa (array) format.
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>
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.
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.
mesa: Fix RGBA8888_UINT direct unpack path in _mesa_format_convert.

A direct unpack to this format from a signed format won't clamp so we should
not resolve converts of this kind via the unpack fast path. These scenarios
will be handled properly by mesa_swizzle_and_convert path.