Clone of mesa.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

hir_field_selection.cpp 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 <stdio.h>
  24. #include "main/imports.h"
  25. #include "symbol_table.h"
  26. #include "glsl_parser_extras.h"
  27. #include "ast.h"
  28. #include "glsl_types.h"
  29. #include "ir.h"
  30. #define X 1
  31. #define R 5
  32. #define S 9
  33. #define I 13
  34. static bool
  35. generate_swizzle(const char *str, struct ir_swizzle_mask *swiz,
  36. unsigned vector_length)
  37. {
  38. /* For each possible swizzle character, this table encodes the value in
  39. * \c idx_map that represents the 0th element of the vector. For invalid
  40. * swizzle characters (e.g., 'k'), a special value is used that will allow
  41. * detection of errors.
  42. */
  43. unsigned char base_idx[26] = {
  44. /* a b c d e f g h i j k l m */
  45. R, R, I, I, I, I, R, I, I, I, I, I, I,
  46. /* n o p q r s t u v w x y z */
  47. I, I, S, S, R, S, S, I, I, X, X, X, X
  48. };
  49. /* Each valid swizzle character has an entry in the previous table. This
  50. * table encodes the base index encoded in the previous table plus the actual
  51. * index of the swizzle character. When processing swizzles, the first
  52. * character in the string is indexed in the previous table. Each character
  53. * in the string is indexed in this table, and the value found there has the
  54. * value form the first table subtracted. The result must be on the range
  55. * [0,3].
  56. *
  57. * For example, the string "wzyx" will get X from the first table. Each of
  58. * the charcaters will get X+3, X+2, X+1, and X+0 from this table. After
  59. * subtraction, the swizzle values are { 3, 2, 1, 0 }.
  60. *
  61. * The string "wzrg" will get X from the first table. Each of the characters
  62. * will get X+3, X+2, R+0, and R+1 from this table. After subtraction, the
  63. * swizzle values are { 3, 2, 4, 5 }. Since 4 and 5 are outside the range
  64. * [0,3], the error is detected.
  65. */
  66. unsigned char idx_map[26] = {
  67. /* a b c d e f g h i j k l m */
  68. R+3, R+2, 0, 0, 0, 0, R+1, 0, 0, 0, 0, 0, 0,
  69. /* n o p q r s t u v w x y z */
  70. 0, 0, S+2, S+3, R+0, S+0, S+1, 0, 0, X+3, X+0, X+1, X+2
  71. };
  72. int swiz_idx[4] = { 0, 0, 0, 0 };
  73. unsigned base;
  74. unsigned dup_mask = 0;
  75. unsigned seen_mask = 0;
  76. unsigned i;
  77. /* Validate the first character in the swizzle string and look up the base
  78. * index value as described above.
  79. */
  80. if ((str[0] < 'a') || (str[0] > 'z'))
  81. return false;
  82. base = base_idx[str[0] - 'a'];
  83. for (i = 0; (i < 4) && (str[i] != '\0'); i++) {
  84. unsigned bit;
  85. /* Validate the next character, and, as described above, convert it to a
  86. * swizzle index.
  87. */
  88. if ((str[i] < 'a') || (str[i] > 'z'))
  89. return false;
  90. swiz_idx[i] = idx_map[str[0] - 'a'] - base;
  91. if ((swiz_idx[i] < 0) || (swiz_idx[i] >= (int) vector_length))
  92. return false;
  93. /* Track a bit-mask of the swizzle index values that have been seen. If
  94. * a value is seen more than once, set the "duplicate" flag.
  95. */
  96. bit = (1U << swiz_idx[i]);
  97. dup_mask |= seen_mask & bit;
  98. seen_mask |= bit;
  99. }
  100. if (str[i] != '\0')
  101. return false;
  102. swiz->x = swiz_idx[0];
  103. swiz->y = swiz_idx[1];
  104. swiz->z = swiz_idx[2];
  105. swiz->w = swiz_idx[3];
  106. swiz->num_components = i;
  107. swiz->has_duplicates = (dup_mask != 0);
  108. return true;
  109. }
  110. struct ir_instruction *
  111. _mesa_ast_field_selection_to_hir(const ast_expression *expr,
  112. exec_list *instructions,
  113. struct _mesa_glsl_parse_state *state)
  114. {
  115. ir_instruction *op;
  116. ir_dereference *deref;
  117. YYLTYPE loc;
  118. op = expr->subexpressions[0]->hir(instructions, state);
  119. deref = new ir_dereference(op);
  120. /* Initially assume that the resulting type of the field selection is an
  121. * error. This make the error paths below a bit easier to follow.
  122. */
  123. deref->type = glsl_error_type;
  124. /* If processing the thing being dereferenced generated an error, bail out
  125. * now. Doing so prevents spurious error messages from being logged below.
  126. */
  127. if (is_error_type(op->type))
  128. return (struct ir_instruction *) deref;
  129. /* There are two kinds of field selection. There is the selection of a
  130. * specific field from a structure, and there is the selection of a
  131. * swizzle / mask from a vector. Which is which is determined entirely
  132. * by the base type of the thing to which the field selection operator is
  133. * being applied.
  134. */
  135. loc = expr->get_location();
  136. if (op->type->is_vector()) {
  137. if (generate_swizzle(expr->primary_expression.identifier,
  138. & deref->selector.swizzle,
  139. op->type->vector_elements)) {
  140. /* Based on the number of elements in the swizzle and the base type
  141. * (i.e., float, int, unsigned, or bool) of the vector being swizzled,
  142. * generate the type of the resulting value.
  143. */
  144. deref->type =
  145. _mesa_glsl_get_vector_type(op->type->base_type,
  146. deref->selector.swizzle.num_components);
  147. } else {
  148. /* FINISHME: Logging of error messages should be moved into
  149. * FINISHME: generate_swizzle. This allows the generation of more
  150. * FINISHME: specific error messages.
  151. */
  152. _mesa_glsl_error(& loc, state, "Invalid swizzle / mask `%s'",
  153. expr->primary_expression.identifier);
  154. }
  155. } else if (op->type->base_type == GLSL_TYPE_STRUCT) {
  156. /* FINISHME: Handle field selection from structures. */
  157. } else {
  158. _mesa_glsl_error(& loc, state, "Cannot access field `%s' of "
  159. "non-structure / non-vector.",
  160. expr->primary_expression.identifier);
  161. }
  162. return (struct ir_instruction *) deref;
  163. }