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.

glsl_types.cpp 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /*
  2. * Copyright © 2009 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 <cstdio>
  24. #include <stdlib.h>
  25. #include "glsl_symbol_table.h"
  26. #include "glsl_parser_extras.h"
  27. #include "glsl_types.h"
  28. #include "builtin_types.h"
  29. #include "hash_table.h"
  30. hash_table *glsl_type::array_types = NULL;
  31. static void
  32. add_types_to_symbol_table(glsl_symbol_table *symtab,
  33. const struct glsl_type *types,
  34. unsigned num_types, bool warn)
  35. {
  36. (void) warn;
  37. for (unsigned i = 0; i < num_types; i++) {
  38. symtab->add_type(types[i].name, & types[i]);
  39. }
  40. }
  41. static void
  42. generate_110_types(glsl_symbol_table *symtab)
  43. {
  44. add_types_to_symbol_table(symtab, builtin_core_types,
  45. Elements(builtin_core_types),
  46. false);
  47. add_types_to_symbol_table(symtab, builtin_structure_types,
  48. Elements(builtin_structure_types),
  49. false);
  50. add_types_to_symbol_table(symtab, builtin_110_deprecated_structure_types,
  51. Elements(builtin_110_deprecated_structure_types),
  52. false);
  53. add_types_to_symbol_table(symtab, & void_type, 1, false);
  54. }
  55. static void
  56. generate_120_types(glsl_symbol_table *symtab)
  57. {
  58. generate_110_types(symtab);
  59. add_types_to_symbol_table(symtab, builtin_120_types,
  60. Elements(builtin_120_types), false);
  61. }
  62. static void
  63. generate_130_types(glsl_symbol_table *symtab)
  64. {
  65. generate_120_types(symtab);
  66. add_types_to_symbol_table(symtab, builtin_130_types,
  67. Elements(builtin_130_types), false);
  68. }
  69. static void
  70. generate_ARB_texture_rectangle_types(glsl_symbol_table *symtab, bool warn)
  71. {
  72. add_types_to_symbol_table(symtab, builtin_ARB_texture_rectangle_types,
  73. Elements(builtin_ARB_texture_rectangle_types),
  74. warn);
  75. }
  76. void
  77. _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state)
  78. {
  79. switch (state->language_version) {
  80. case 110:
  81. generate_110_types(state->symbols);
  82. break;
  83. case 120:
  84. generate_120_types(state->symbols);
  85. break;
  86. case 130:
  87. generate_130_types(state->symbols);
  88. break;
  89. default:
  90. /* error */
  91. break;
  92. }
  93. if (state->ARB_texture_rectangle_enable) {
  94. generate_ARB_texture_rectangle_types(state->symbols,
  95. state->ARB_texture_rectangle_warn);
  96. }
  97. }
  98. const glsl_type *glsl_type::get_base_type() const
  99. {
  100. switch (base_type) {
  101. case GLSL_TYPE_UINT:
  102. return uint_type;
  103. case GLSL_TYPE_INT:
  104. return int_type;
  105. case GLSL_TYPE_FLOAT:
  106. return float_type;
  107. case GLSL_TYPE_BOOL:
  108. return bool_type;
  109. default:
  110. return error_type;
  111. }
  112. }
  113. ir_function *
  114. glsl_type::generate_constructor_prototype(glsl_symbol_table *symtab) const
  115. {
  116. /* FINISHME: Add support for non-array types. */
  117. assert(base_type == GLSL_TYPE_ARRAY);
  118. /* Generate the function name and add it to the symbol table.
  119. */
  120. ir_function *const f = new ir_function(name);
  121. bool added = symtab->add_function(name, f);
  122. assert(added);
  123. ir_function_signature *const sig = new ir_function_signature(this);
  124. f->add_signature(sig);
  125. for (unsigned i = 0; i < length; i++) {
  126. char *const param_name = (char *) malloc(10);
  127. snprintf(param_name, 10, "p%08X", i);
  128. ir_variable *var = new ir_variable(fields.array, param_name);
  129. var->mode = ir_var_in;
  130. sig->parameters.push_tail(var);
  131. }
  132. return f;
  133. }
  134. /**
  135. * Generate the function intro for a constructor
  136. *
  137. * \param type Data type to be constructed
  138. * \param count Number of parameters to this concrete constructor. Most
  139. * types have at least two constructors. One will take a
  140. * single scalar parameter and the other will take "N"
  141. * scalar parameters.
  142. * \param parameters Storage for the list of parameters. These are
  143. * typically stored in an \c ir_function_signature.
  144. * \param instructions Storage for the preamble and body of the function.
  145. * \param declarations Pointers to the variable declarations for the function
  146. * parameters. These are used later to avoid having to use
  147. * the symbol table.
  148. */
  149. static ir_label *
  150. generate_constructor_intro(const glsl_type *type, unsigned parameter_count,
  151. ir_function_signature *const signature,
  152. exec_list *instructions,
  153. ir_variable **declarations)
  154. {
  155. /* Names of parameters used in vector and matrix constructors
  156. */
  157. static const char *const names[] = {
  158. "a", "b", "c", "d", "e", "f", "g", "h",
  159. "i", "j", "k", "l", "m", "n", "o", "p",
  160. };
  161. assert(parameter_count <= Elements(names));
  162. const glsl_type *const parameter_type = type->get_base_type();
  163. ir_label *const label = new ir_label(type->name, signature);
  164. instructions->push_tail(label);
  165. for (unsigned i = 0; i < parameter_count; i++) {
  166. ir_variable *var = new ir_variable(parameter_type, names[i]);
  167. var->mode = ir_var_in;
  168. signature->parameters.push_tail(var);
  169. declarations[i] = var;
  170. }
  171. ir_variable *retval = new ir_variable(type, "__retval");
  172. signature->body.push_tail(retval);
  173. declarations[16] = retval;
  174. return label;
  175. }
  176. /**
  177. * Generate the body of a vector constructor that takes a single scalar
  178. */
  179. static void
  180. generate_vec_body_from_scalar(exec_list *instructions,
  181. ir_variable **declarations)
  182. {
  183. ir_instruction *inst;
  184. /* Generate a single assignment of the parameter to __retval.x and return
  185. * __retval.xxxx for however many vector components there are.
  186. */
  187. ir_dereference *const lhs_ref = new ir_dereference(declarations[16]);
  188. ir_dereference *const rhs = new ir_dereference(declarations[0]);
  189. ir_swizzle *lhs = new ir_swizzle(lhs_ref, 0, 0, 0, 0, 1);
  190. inst = new ir_assignment(lhs, rhs, NULL);
  191. instructions->push_tail(inst);
  192. ir_dereference *const retref = new ir_dereference(declarations[16]);
  193. ir_swizzle *retval = new ir_swizzle(retref, 0, 0, 0, 0,
  194. declarations[16]->type->vector_elements);
  195. inst = new ir_return(retval);
  196. instructions->push_tail(inst);
  197. }
  198. /**
  199. * Generate the body of a vector constructor that takes multiple scalars
  200. */
  201. static void
  202. generate_vec_body_from_N_scalars(exec_list *instructions,
  203. ir_variable **declarations)
  204. {
  205. ir_instruction *inst;
  206. const glsl_type *const vec_type = declarations[16]->type;
  207. /* Generate an assignment of each parameter to a single component of
  208. * __retval.x and return __retval.
  209. */
  210. for (unsigned i = 0; i < vec_type->vector_elements; i++) {
  211. ir_dereference *const lhs_ref = new ir_dereference(declarations[16]);
  212. ir_dereference *const rhs = new ir_dereference(declarations[i]);
  213. ir_swizzle *lhs = new ir_swizzle(lhs_ref, 1, 0, 0, 0, 1);
  214. inst = new ir_assignment(lhs, rhs, NULL);
  215. instructions->push_tail(inst);
  216. }
  217. ir_dereference *retval = new ir_dereference(declarations[16]);
  218. inst = new ir_return(retval);
  219. instructions->push_tail(inst);
  220. }
  221. /**
  222. * Generate the body of a matrix constructor that takes a single scalar
  223. */
  224. static void
  225. generate_mat_body_from_scalar(exec_list *instructions,
  226. ir_variable **declarations)
  227. {
  228. ir_instruction *inst;
  229. /* Generate an assignment of the parameter to the X component of a
  230. * temporary vector. Set the remaining fields of the vector to 0. The
  231. * size of the vector is equal to the number of rows of the matrix.
  232. *
  233. * Set each column of the matrix to a successive "rotation" of the
  234. * temporary vector. This fills the matrix with 0s, but writes the single
  235. * scalar along the matrix's diagonal.
  236. *
  237. * For a mat4x3, this is equivalent to:
  238. *
  239. * vec3 tmp;
  240. * mat4x3 __retval;
  241. * tmp.x = a;
  242. * tmp.y = 0.0;
  243. * tmp.z = 0.0;
  244. * __retval[0] = tmp.xyy;
  245. * __retval[1] = tmp.yxy;
  246. * __retval[2] = tmp.yyx;
  247. * __retval[3] = tmp.yyy;
  248. */
  249. const glsl_type *const column_type = declarations[16]->type->column_type();
  250. const glsl_type *const row_type = declarations[16]->type->row_type();
  251. ir_variable *const column = new ir_variable(column_type, "v");
  252. instructions->push_tail(column);
  253. ir_dereference *const lhs_ref = new ir_dereference(column);
  254. ir_dereference *const rhs = new ir_dereference(declarations[0]);
  255. ir_swizzle *lhs = new ir_swizzle(lhs_ref, 0, 0, 0, 0, 1);
  256. inst = new ir_assignment(lhs, rhs, NULL);
  257. instructions->push_tail(inst);
  258. const float z = 0.0f;
  259. ir_constant *const zero = new ir_constant(glsl_type::float_type, &z);
  260. for (unsigned i = 1; i < column_type->vector_elements; i++) {
  261. ir_dereference *const lhs_ref = new ir_dereference(column);
  262. ir_swizzle *lhs = new ir_swizzle(lhs_ref, i, 0, 0, 0, 1);
  263. inst = new ir_assignment(lhs, zero, NULL);
  264. instructions->push_tail(inst);
  265. }
  266. for (unsigned i = 0; i < row_type->vector_elements; i++) {
  267. static const unsigned swiz[] = { 1, 1, 1, 0, 1, 1, 1 };
  268. ir_dereference *const rhs_ref = new ir_dereference(column);
  269. /* This will be .xyyy when i=0, .yxyy when i=1, etc.
  270. */
  271. ir_swizzle *rhs = new ir_swizzle(rhs_ref, swiz[3 - i], swiz[4 - i],
  272. swiz[5 - i], swiz[6 - i],
  273. column_type->vector_elements);
  274. ir_constant *const idx = new ir_constant(glsl_type::int_type, &i);
  275. ir_dereference *const lhs = new ir_dereference(declarations[16], idx);
  276. inst = new ir_assignment(lhs, rhs, NULL);
  277. instructions->push_tail(inst);
  278. }
  279. ir_dereference *const retval = new ir_dereference(declarations[16]);
  280. inst = new ir_return(retval);
  281. instructions->push_tail(inst);
  282. }
  283. /**
  284. * Generate the body of a vector constructor that takes multiple scalars
  285. */
  286. static void
  287. generate_mat_body_from_N_scalars(exec_list *instructions,
  288. ir_variable **declarations)
  289. {
  290. ir_instruction *inst;
  291. const glsl_type *const row_type = declarations[16]->type->row_type();
  292. const glsl_type *const column_type = declarations[16]->type->column_type();
  293. /* Generate an assignment of each parameter to a single component of
  294. * of a particular column of __retval and return __retval.
  295. */
  296. for (unsigned i = 0; i < column_type->vector_elements; i++) {
  297. for (unsigned j = 0; j < row_type->vector_elements; j++) {
  298. ir_constant *row_index = new ir_constant(glsl_type::int_type, &i);
  299. ir_dereference *const row_access =
  300. new ir_dereference(declarations[16], row_index);
  301. ir_dereference *const component_access_ref =
  302. new ir_dereference(row_access);
  303. ir_swizzle *component_access = new ir_swizzle(component_access_ref,
  304. j, 0, 0, 0, 1);
  305. const unsigned param = (i * row_type->vector_elements) + j;
  306. ir_dereference *const rhs = new ir_dereference(declarations[param]);
  307. inst = new ir_assignment(component_access, rhs, NULL);
  308. instructions->push_tail(inst);
  309. }
  310. }
  311. ir_dereference *retval = new ir_dereference(declarations[16]);
  312. inst = new ir_return(retval);
  313. instructions->push_tail(inst);
  314. }
  315. /**
  316. * Generate the constructors for a set of GLSL types
  317. *
  318. * Constructor implementations are added to \c instructions, and the symbols
  319. * are added to \c symtab.
  320. */
  321. static void
  322. generate_constructor(glsl_symbol_table *symtab, const struct glsl_type *types,
  323. unsigned num_types, exec_list *instructions)
  324. {
  325. ir_variable *declarations[17];
  326. for (unsigned i = 0; i < num_types; i++) {
  327. /* Only numeric and boolean vectors and matrices get constructors here.
  328. * Structures need to be handled elsewhere. It is expected that scalar
  329. * constructors are never actually called, so they are not generated.
  330. */
  331. if (!types[i].is_numeric() && !types[i].is_boolean())
  332. continue;
  333. if (types[i].is_scalar())
  334. continue;
  335. /* Generate the function name and add it to the symbol table.
  336. */
  337. ir_function *const f = new ir_function(types[i].name);
  338. bool added = symtab->add_function(types[i].name, f);
  339. assert(added);
  340. /* Each type has several basic constructors. The total number of forms
  341. * depends on the derived type.
  342. *
  343. * Vectors: 1 scalar, N scalars
  344. * Matrices: 1 scalar, NxM scalars
  345. *
  346. * Several possible types of constructors are not included in this list.
  347. *
  348. * Scalar constructors are not included. The expectation is that the
  349. * IR generator won't actually generate these as constructor calls. The
  350. * expectation is that it will just generate the necessary type
  351. * conversion.
  352. *
  353. * Matrix contructors from matrices are also not included. The
  354. * expectation is that the IR generator will generate a call to the
  355. * appropriate from-scalars constructor.
  356. */
  357. ir_function_signature *const sig = new ir_function_signature(& types[i]);
  358. f->add_signature(sig);
  359. sig->definition =
  360. generate_constructor_intro(& types[i], 1, sig,
  361. instructions, declarations);
  362. if (types[i].is_vector()) {
  363. generate_vec_body_from_scalar(&sig->body, declarations);
  364. ir_function_signature *const vec_sig =
  365. new ir_function_signature(& types[i]);
  366. f->add_signature(vec_sig);
  367. vec_sig->definition =
  368. generate_constructor_intro(& types[i], types[i].vector_elements,
  369. vec_sig, instructions,
  370. declarations);
  371. generate_vec_body_from_N_scalars(&sig->body, declarations);
  372. } else {
  373. assert(types[i].is_matrix());
  374. generate_mat_body_from_scalar(&sig->body, declarations);
  375. ir_function_signature *const mat_sig =
  376. new ir_function_signature(& types[i]);
  377. f->add_signature(mat_sig);
  378. mat_sig->definition =
  379. generate_constructor_intro(& types[i],
  380. (types[i].vector_elements
  381. * types[i].matrix_columns),
  382. mat_sig, instructions,
  383. declarations);
  384. generate_mat_body_from_N_scalars(instructions, declarations);
  385. }
  386. }
  387. }
  388. void
  389. generate_110_constructors(glsl_symbol_table *symtab, exec_list *instructions)
  390. {
  391. generate_constructor(symtab, builtin_core_types,
  392. Elements(builtin_core_types), instructions);
  393. }
  394. void
  395. generate_120_constructors(glsl_symbol_table *symtab, exec_list *instructions)
  396. {
  397. generate_110_constructors(symtab, instructions);
  398. generate_constructor(symtab, builtin_120_types,
  399. Elements(builtin_120_types), instructions);
  400. }
  401. void
  402. generate_130_constructors(glsl_symbol_table *symtab, exec_list *instructions)
  403. {
  404. generate_120_constructors(symtab, instructions);
  405. generate_constructor(symtab, builtin_130_types,
  406. Elements(builtin_130_types), instructions);
  407. }
  408. void
  409. _mesa_glsl_initialize_constructors(exec_list *instructions,
  410. struct _mesa_glsl_parse_state *state)
  411. {
  412. switch (state->language_version) {
  413. case 110:
  414. generate_110_constructors(state->symbols, instructions);
  415. break;
  416. case 120:
  417. generate_120_constructors(state->symbols, instructions);
  418. break;
  419. case 130:
  420. generate_130_constructors(state->symbols, instructions);
  421. break;
  422. default:
  423. /* error */
  424. break;
  425. }
  426. }
  427. glsl_type::glsl_type(const glsl_type *array, unsigned length) :
  428. base_type(GLSL_TYPE_ARRAY),
  429. sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
  430. sampler_type(0),
  431. vector_elements(0), matrix_columns(0),
  432. name(NULL), length(length)
  433. {
  434. this->fields.array = array;
  435. /* Allow a maximum of 10 characters for the array size. This is enough
  436. * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
  437. * NUL.
  438. */
  439. const unsigned name_length = strlen(array->name) + 10 + 3;
  440. char *const n = (char *) malloc(name_length);
  441. if (length == 0)
  442. snprintf(n, name_length, "%s[]", array->name);
  443. else
  444. snprintf(n, name_length, "%s[%u]", array->name, length);
  445. this->name = n;
  446. }
  447. const glsl_type *
  448. glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
  449. {
  450. if (base_type == GLSL_TYPE_VOID)
  451. return &void_type;
  452. if ((rows < 1) || (rows > 4) || (columns < 1) || (columns > 4))
  453. return error_type;
  454. /* Treat GLSL vectors as Nx1 matrices.
  455. */
  456. if (columns == 1) {
  457. switch (base_type) {
  458. case GLSL_TYPE_UINT:
  459. return uint_type + (rows - 1);
  460. case GLSL_TYPE_INT:
  461. return int_type + (rows - 1);
  462. case GLSL_TYPE_FLOAT:
  463. return float_type + (rows - 1);
  464. case GLSL_TYPE_BOOL:
  465. return bool_type + (rows - 1);
  466. default:
  467. return error_type;
  468. }
  469. } else {
  470. if ((base_type != GLSL_TYPE_FLOAT) || (rows == 1))
  471. return error_type;
  472. /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
  473. * combinations are valid:
  474. *
  475. * 1 2 3 4
  476. * 1
  477. * 2 x x x
  478. * 3 x x x
  479. * 4 x x x
  480. */
  481. #define IDX(c,r) (((c-1)*3) + (r-1))
  482. switch (IDX(columns, rows)) {
  483. case IDX(2,2): return mat2_type;
  484. case IDX(2,3): return mat2x3_type;
  485. case IDX(2,4): return mat2x4_type;
  486. case IDX(3,2): return mat3x2_type;
  487. case IDX(3,3): return mat3_type;
  488. case IDX(3,4): return mat3x4_type;
  489. case IDX(4,2): return mat4x2_type;
  490. case IDX(4,3): return mat4x3_type;
  491. case IDX(4,4): return mat4_type;
  492. default: return error_type;
  493. }
  494. }
  495. assert(!"Should not get here.");
  496. return error_type;
  497. }
  498. int
  499. glsl_type::array_key_compare(const void *a, const void *b)
  500. {
  501. const glsl_type *const key1 = (glsl_type *) a;
  502. const glsl_type *const key2 = (glsl_type *) b;
  503. /* Return zero is the types match (there is zero difference) or non-zero
  504. * otherwise.
  505. */
  506. return ((key1->fields.array == key2->fields.array)
  507. && (key1->length == key2->length)) ? 0 : 1;
  508. }
  509. unsigned
  510. glsl_type::array_key_hash(const void *a)
  511. {
  512. const glsl_type *const key = (glsl_type *) a;
  513. const struct {
  514. const glsl_type *t;
  515. unsigned l;
  516. char nul;
  517. } hash_key = {
  518. key->fields.array,
  519. key->length,
  520. '\0'
  521. };
  522. return hash_table_string_hash(& hash_key);
  523. }
  524. const glsl_type *
  525. glsl_type::get_array_instance(const glsl_type *base, unsigned array_size)
  526. {
  527. const glsl_type key(base, array_size);
  528. if (array_types == NULL) {
  529. array_types = hash_table_ctor(64, array_key_hash, array_key_compare);
  530. }
  531. const glsl_type *t = (glsl_type *) hash_table_find(array_types, & key);
  532. if (t == NULL) {
  533. t = new glsl_type(base, array_size);
  534. hash_table_insert(array_types, (void *) t, t);
  535. }
  536. assert(t->base_type == GLSL_TYPE_ARRAY);
  537. assert(t->length == array_size);
  538. assert(t->fields.array == base);
  539. return t;
  540. }