Clone of mesa.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ir_function.cpp 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_FLOAT:
  36. case GLSL_TYPE_BOOL:
  37. if ((a->vector_elements != b->vector_elements)
  38. || (a->matrix_columns != b->matrix_columns))
  39. return -1;
  40. /* There is no implicit conversion to or from bool.
  41. */
  42. if ((a->base_type == GLSL_TYPE_BOOL)
  43. || (b->base_type == GLSL_TYPE_BOOL))
  44. return -1;
  45. return 1;
  46. case GLSL_TYPE_SAMPLER:
  47. case GLSL_TYPE_STRUCT:
  48. /* Samplers and structures must match exactly.
  49. */
  50. return -1;
  51. case GLSL_TYPE_ARRAY:
  52. if ((b->base_type != GLSL_TYPE_ARRAY)
  53. || (a->length != b->length))
  54. return -1;
  55. /* From GLSL 1.50 spec, page 27 (page 33 of the PDF):
  56. * "There are no implicit array or structure conversions."
  57. *
  58. * If the comparison of the array element types detects that a conversion
  59. * would be required, the array types do not match.
  60. */
  61. return (type_compare(a->fields.array, b->fields.array) == 0) ? 0 : -1;
  62. case GLSL_TYPE_FUNCTION:
  63. case GLSL_TYPE_VOID:
  64. case GLSL_TYPE_ERROR:
  65. default:
  66. /* These are all error conditions. It is invalid for a parameter to
  67. * a function to be declared as error, void, or a function.
  68. */
  69. return -1;
  70. }
  71. /* This point should be unreachable.
  72. */
  73. assert(0);
  74. }
  75. static int
  76. parameter_lists_match(exec_list *list_a, exec_list *list_b)
  77. {
  78. exec_list_iterator iter_a = list_a->iterator();
  79. exec_list_iterator iter_b = list_b->iterator();
  80. int total_score = 0;
  81. for (/* empty */ ; iter_a.has_next(); iter_a.next(), iter_b.next()) {
  82. /* If all of the parameters from the other parameter list have been
  83. * exhausted, the lists have different length and, by definition,
  84. * do not match.
  85. */
  86. if (!iter_b.has_next())
  87. return -1;
  88. const ir_variable *const param = (ir_variable *) iter_a.get();
  89. const ir_instruction *const actual = (ir_instruction *) iter_b.get();
  90. /* Determine whether or not the types match. If the types are an
  91. * exact match, the match score is zero. If the types don't match
  92. * but the actual parameter can be coerced to the type of the declared
  93. * parameter, the match score is one.
  94. */
  95. int score;
  96. switch ((enum ir_variable_mode)(param->mode)) {
  97. case ir_var_auto:
  98. case ir_var_uniform:
  99. /* These are all error conditions. It is invalid for a parameter to
  100. * a function to be declared as auto (not in, out, or inout) or
  101. * as uniform.
  102. */
  103. assert(0);
  104. return -1;
  105. case ir_var_in:
  106. score = type_compare(param->type, actual->type);
  107. break;
  108. case ir_var_out:
  109. /* FINISHME: Make sure that actual is a valid lvalue. */
  110. score = type_compare(actual->type, param->type);
  111. break;
  112. case ir_var_inout:
  113. /* FINISHME: Make sure that actual is a valid lvalue. */
  114. /* Since there are no bi-directional automatic conversions (e.g.,
  115. * there is int -> float but no float -> int), inout parameters must
  116. * be exact matches.
  117. */
  118. score = (type_compare(actual->type, param->type) == 0) ? 0 : -1;
  119. break;
  120. }
  121. if (score < 0)
  122. return -1;
  123. total_score += score;
  124. }
  125. /* If all of the parameters from the other parameter list have been
  126. * exhausted, the lists have different length and, by definition, do not
  127. * match.
  128. */
  129. if (iter_b.has_next())
  130. return -1;
  131. return total_score;
  132. }
  133. const ir_function_signature *
  134. ir_function::matching_signature(exec_list *actual_parameters)
  135. {
  136. ir_function_signature *match = NULL;
  137. foreach_iter(exec_list_iterator, iter, signatures) {
  138. ir_function_signature *const sig =
  139. (ir_function_signature *) iter.get();
  140. const int score = parameter_lists_match(& sig->parameters,
  141. actual_parameters);
  142. if (score == 0)
  143. return sig;
  144. if (score > 0) {
  145. if (match != NULL)
  146. return NULL;
  147. match = sig;
  148. }
  149. }
  150. return match;
  151. }