Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ir_function.cpp 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright © 2010 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "glsl_types.h"
  24. #include "ir.h"
  25. int
  26. type_compare(const glsl_type *a, const glsl_type *b)
  27. {
  28. /* If the types are the same, they trivially match.
  29. */
  30. if (a == b)
  31. return 0;
  32. switch (a->base_type) {
  33. case GLSL_TYPE_UINT:
  34. case GLSL_TYPE_INT:
  35. case GLSL_TYPE_BOOL:
  36. /* There is no implicit conversion to or from integer types or bool.
  37. */
  38. if ((a->is_integer() != b->is_integer())
  39. || (a->is_boolean() != b->is_boolean()))
  40. return -1;
  41. /* FALLTHROUGH */
  42. case GLSL_TYPE_FLOAT:
  43. if ((a->vector_elements != b->vector_elements)
  44. || (a->matrix_columns != b->matrix_columns))
  45. return -1;
  46. return 1;
  47. case GLSL_TYPE_SAMPLER:
  48. case GLSL_TYPE_STRUCT:
  49. /* Samplers and structures must match exactly.
  50. */
  51. return -1;
  52. case GLSL_TYPE_ARRAY:
  53. if ((b->base_type != GLSL_TYPE_ARRAY)
  54. || (a->length != b->length))
  55. return -1;
  56. /* From GLSL 1.50 spec, page 27 (page 33 of the PDF):
  57. * "There are no implicit array or structure conversions."
  58. *
  59. * If the comparison of the array element types detects that a conversion
  60. * would be required, the array types do not match.
  61. */
  62. return (type_compare(a->fields.array, b->fields.array) == 0) ? 0 : -1;
  63. case GLSL_TYPE_FUNCTION:
  64. case GLSL_TYPE_VOID:
  65. case GLSL_TYPE_ERROR:
  66. default:
  67. /* These are all error conditions. It is invalid for a parameter to
  68. * a function to be declared as error, void, or a function.
  69. */
  70. return -1;
  71. }
  72. /* This point should be unreachable.
  73. */
  74. assert(0);
  75. }
  76. static int
  77. parameter_lists_match(exec_list *list_a, exec_list *list_b)
  78. {
  79. exec_list_iterator iter_a = list_a->iterator();
  80. exec_list_iterator iter_b = list_b->iterator();
  81. int total_score = 0;
  82. for (/* empty */ ; iter_a.has_next(); iter_a.next(), iter_b.next()) {
  83. /* If all of the parameters from the other parameter list have been
  84. * exhausted, the lists have different length and, by definition,
  85. * do not match.
  86. */
  87. if (!iter_b.has_next())
  88. return -1;
  89. const ir_variable *const param = (ir_variable *) iter_a.get();
  90. const ir_instruction *const actual = (ir_instruction *) iter_b.get();
  91. /* Determine whether or not the types match. If the types are an
  92. * exact match, the match score is zero. If the types don't match
  93. * but the actual parameter can be coerced to the type of the declared
  94. * parameter, the match score is one.
  95. */
  96. int score;
  97. switch ((enum ir_variable_mode)(param->mode)) {
  98. case ir_var_auto:
  99. case ir_var_uniform:
  100. /* These are all error conditions. It is invalid for a parameter to
  101. * a function to be declared as auto (not in, out, or inout) or
  102. * as uniform.
  103. */
  104. assert(0);
  105. return -1;
  106. case ir_var_in:
  107. score = type_compare(param->type, actual->type);
  108. break;
  109. case ir_var_out:
  110. score = type_compare(actual->type, param->type);
  111. break;
  112. case ir_var_inout:
  113. /* Since there are no bi-directional automatic conversions (e.g.,
  114. * there is int -> float but no float -> int), inout parameters must
  115. * be exact matches.
  116. */
  117. score = (type_compare(actual->type, param->type) == 0) ? 0 : -1;
  118. break;
  119. }
  120. if (score < 0)
  121. return -1;
  122. total_score += score;
  123. }
  124. /* If all of the parameters from the other parameter list have been
  125. * exhausted, the lists have different length and, by definition, do not
  126. * match.
  127. */
  128. if (iter_b.has_next())
  129. return -1;
  130. return total_score;
  131. }
  132. const ir_function_signature *
  133. ir_function::matching_signature(exec_list *actual_parameters)
  134. {
  135. ir_function_signature *match = NULL;
  136. foreach_iter(exec_list_iterator, iter, signatures) {
  137. ir_function_signature *const sig =
  138. (ir_function_signature *) iter.get();
  139. const int score = parameter_lists_match(& sig->parameters,
  140. actual_parameters);
  141. if (score == 0)
  142. return sig;
  143. if (score > 0) {
  144. if (match != NULL)
  145. return NULL;
  146. match = sig;
  147. }
  148. }
  149. return match;
  150. }