glsl: move _mesa_glsl_can_implicitly_convert() to linker_util.cpp

Makes more sense here as its used by both the compiler and linker.

Acked-by: Marek Olšák <marek.olsak@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/31137>
This commit is contained in:
Timothy Arceri 2024-08-22 15:08:19 +10:00 committed by Marge Bot
parent 1cb115abd2
commit f3da074dc3
5 changed files with 89 additions and 89 deletions

View file

@ -1033,56 +1033,6 @@ _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
return true;
}
bool
_mesa_glsl_can_implicitly_convert(const glsl_type *from, const glsl_type *desired,
bool has_implicit_conversions,
bool has_implicit_int_to_uint_conversion)
{
if (from == desired)
return true;
/* GLSL 1.10 and ESSL do not allow implicit conversions. */
if (!has_implicit_conversions)
return false;
/* There is no conversion among matrix types. */
if (from->matrix_columns > 1 || desired->matrix_columns > 1)
return false;
/* Vector size must match. */
if (from->vector_elements != desired->vector_elements)
return false;
/* int and uint can be converted to float. */
if (glsl_type_is_float(desired) && (glsl_type_is_integer_32(from) ||
glsl_type_is_float_16(from)))
return true;
/* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int
* can be converted to uint. Note that state may be NULL here, when
* resolving function calls in the linker. By this time, all the
* state-dependent checks have already happened though, so allow anything
* that's allowed in any shader version.
*/
if (has_implicit_int_to_uint_conversion &&
desired->base_type == GLSL_TYPE_UINT && from->base_type == GLSL_TYPE_INT)
return true;
/* No implicit conversions from double. */
if (glsl_type_is_double(from))
return false;
/* Conversions from different types to double. */
if (glsl_type_is_double(desired)) {
if (glsl_type_is_float_16_32(from))
return true;
if (glsl_type_is_integer_32(from))
return true;
}
return false;
}
/**
* Recurses through <type> and <expr> if <expr> is an aggregate initializer
* and sets <expr>'s <constructor_type> field to <type>. Gives later functions

View file

@ -1071,45 +1071,6 @@ extern bool _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
YYLTYPE *behavior_locp,
_mesa_glsl_parse_state *state);
/**
* \brief Can \c from be implicitly converted to \c desired
*
* \return True if the types are identical or if \c from type can be converted
* to \c desired according to Section 4.1.10 of the GLSL spec.
*
* \verbatim
* From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
* Implicit Conversions:
*
* In some situations, an expression and its type will be implicitly
* converted to a different type. The following table shows all allowed
* implicit conversions:
*
* Type of expression | Can be implicitly converted to
* --------------------------------------------------
* int float
* uint
*
* ivec2 vec2
* uvec2
*
* ivec3 vec3
* uvec3
*
* ivec4 vec4
* uvec4
*
* There are no implicit array or structure conversions. For example,
* an array of int cannot be implicitly converted to an array of float.
* There are no implicit conversions between signed and unsigned
* integers.
* \endverbatim
*/
extern bool _mesa_glsl_can_implicitly_convert(const glsl_type *from, const glsl_type *desired,
bool has_implicit_conversions,
bool has_implicit_int_to_uint_conversion);
#endif /* __cplusplus */

View file

@ -24,6 +24,7 @@
#include "compiler/glsl_types.h"
#include "ir.h"
#include "glsl_parser_extras.h"
#include "linker_util.h"
#include "main/errors.h"
typedef enum {

View file

@ -459,3 +459,53 @@ interpolation_string(unsigned interpolation)
assert(!"Should not get here.");
return "";
}
bool
_mesa_glsl_can_implicitly_convert(const glsl_type *from, const glsl_type *desired,
bool has_implicit_conversions,
bool has_implicit_int_to_uint_conversion)
{
if (from == desired)
return true;
/* GLSL 1.10 and ESSL do not allow implicit conversions. */
if (!has_implicit_conversions)
return false;
/* There is no conversion among matrix types. */
if (from->matrix_columns > 1 || desired->matrix_columns > 1)
return false;
/* Vector size must match. */
if (from->vector_elements != desired->vector_elements)
return false;
/* int and uint can be converted to float. */
if (glsl_type_is_float(desired) && (glsl_type_is_integer_32(from) ||
glsl_type_is_float_16(from)))
return true;
/* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int
* can be converted to uint. Note that state may be NULL here, when
* resolving function calls in the linker. By this time, all the
* state-dependent checks have already happened though, so allow anything
* that's allowed in any shader version.
*/
if (has_implicit_int_to_uint_conversion &&
desired->base_type == GLSL_TYPE_UINT && from->base_type == GLSL_TYPE_INT)
return true;
/* No implicit conversions from double. */
if (glsl_type_is_double(from))
return false;
/* Conversions from different types to double. */
if (glsl_type_is_double(desired)) {
if (glsl_type_is_float_16_32(from))
return true;
if (glsl_type_is_integer_32(from))
return true;
}
return false;
}

View file

@ -142,6 +142,44 @@ link_util_mark_array_elements_referenced(const struct array_deref_range *dr,
*/
const char *interpolation_string(unsigned interpolation);
/**
* \brief Can \c from be implicitly converted to \c desired
*
* \return True if the types are identical or if \c from type can be converted
* to \c desired according to Section 4.1.10 of the GLSL spec.
*
* \verbatim
* From page 25 (31 of the pdf) of the GLSL 1.50 spec, Section 4.1.10
* Implicit Conversions:
*
* In some situations, an expression and its type will be implicitly
* converted to a different type. The following table shows all allowed
* implicit conversions:
*
* Type of expression | Can be implicitly converted to
* --------------------------------------------------
* int float
* uint
*
* ivec2 vec2
* uvec2
*
* ivec3 vec3
* uvec3
*
* ivec4 vec4
* uvec4
*
* There are no implicit array or structure conversions. For example,
* an array of int cannot be implicitly converted to an array of float.
* There are no implicit conversions between signed and unsigned
* integers.
* \endverbatim
*/
extern bool _mesa_glsl_can_implicitly_convert(const glsl_type *from, const glsl_type *desired,
bool has_implicit_conversions,
bool has_implicit_int_to_uint_conversion);
#ifdef __cplusplus
}
#endif