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 32KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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 <stdio.h>
  24. #include "main/core.h" /* for Elements, MAX2 */
  25. #include "glsl_parser_extras.h"
  26. #include "glsl_types.h"
  27. extern "C" {
  28. #include "program/hash_table.h"
  29. }
  30. hash_table *glsl_type::array_types = NULL;
  31. hash_table *glsl_type::record_types = NULL;
  32. hash_table *glsl_type::interface_types = NULL;
  33. void *glsl_type::mem_ctx = NULL;
  34. void
  35. glsl_type::init_ralloc_type_ctx(void)
  36. {
  37. if (glsl_type::mem_ctx == NULL) {
  38. glsl_type::mem_ctx = ralloc_autofree_context();
  39. assert(glsl_type::mem_ctx != NULL);
  40. }
  41. }
  42. glsl_type::glsl_type(GLenum gl_type,
  43. glsl_base_type base_type, unsigned vector_elements,
  44. unsigned matrix_columns, const char *name) :
  45. gl_type(gl_type),
  46. base_type(base_type),
  47. sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
  48. sampler_type(0), interface_packing(0),
  49. vector_elements(vector_elements), matrix_columns(matrix_columns),
  50. length(0)
  51. {
  52. init_ralloc_type_ctx();
  53. assert(name != NULL);
  54. this->name = ralloc_strdup(this->mem_ctx, name);
  55. /* Neither dimension is zero or both dimensions are zero.
  56. */
  57. assert((vector_elements == 0) == (matrix_columns == 0));
  58. memset(& fields, 0, sizeof(fields));
  59. }
  60. glsl_type::glsl_type(GLenum gl_type, glsl_base_type base_type,
  61. enum glsl_sampler_dim dim, bool shadow, bool array,
  62. unsigned type, const char *name) :
  63. gl_type(gl_type),
  64. base_type(base_type),
  65. sampler_dimensionality(dim), sampler_shadow(shadow),
  66. sampler_array(array), sampler_type(type), interface_packing(0),
  67. length(0)
  68. {
  69. init_ralloc_type_ctx();
  70. assert(name != NULL);
  71. this->name = ralloc_strdup(this->mem_ctx, name);
  72. memset(& fields, 0, sizeof(fields));
  73. if (base_type == GLSL_TYPE_SAMPLER) {
  74. /* Samplers take no storage whatsoever. */
  75. matrix_columns = vector_elements = 0;
  76. } else {
  77. matrix_columns = vector_elements = 1;
  78. }
  79. }
  80. glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
  81. const char *name) :
  82. gl_type(0),
  83. base_type(GLSL_TYPE_STRUCT),
  84. sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
  85. sampler_type(0), interface_packing(0),
  86. vector_elements(0), matrix_columns(0),
  87. length(num_fields)
  88. {
  89. unsigned int i;
  90. init_ralloc_type_ctx();
  91. assert(name != NULL);
  92. this->name = ralloc_strdup(this->mem_ctx, name);
  93. this->fields.structure = ralloc_array(this->mem_ctx,
  94. glsl_struct_field, length);
  95. for (i = 0; i < length; i++) {
  96. this->fields.structure[i].type = fields[i].type;
  97. this->fields.structure[i].name = ralloc_strdup(this->fields.structure,
  98. fields[i].name);
  99. this->fields.structure[i].location = fields[i].location;
  100. this->fields.structure[i].interpolation = fields[i].interpolation;
  101. this->fields.structure[i].centroid = fields[i].centroid;
  102. this->fields.structure[i].sample = fields[i].sample;
  103. this->fields.structure[i].matrix_layout = fields[i].matrix_layout;
  104. }
  105. }
  106. glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
  107. enum glsl_interface_packing packing, const char *name) :
  108. gl_type(0),
  109. base_type(GLSL_TYPE_INTERFACE),
  110. sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
  111. sampler_type(0), interface_packing((unsigned) packing),
  112. vector_elements(0), matrix_columns(0),
  113. length(num_fields)
  114. {
  115. unsigned int i;
  116. init_ralloc_type_ctx();
  117. assert(name != NULL);
  118. this->name = ralloc_strdup(this->mem_ctx, name);
  119. this->fields.structure = ralloc_array(this->mem_ctx,
  120. glsl_struct_field, length);
  121. for (i = 0; i < length; i++) {
  122. this->fields.structure[i].type = fields[i].type;
  123. this->fields.structure[i].name = ralloc_strdup(this->fields.structure,
  124. fields[i].name);
  125. this->fields.structure[i].location = fields[i].location;
  126. this->fields.structure[i].interpolation = fields[i].interpolation;
  127. this->fields.structure[i].centroid = fields[i].centroid;
  128. this->fields.structure[i].sample = fields[i].sample;
  129. this->fields.structure[i].matrix_layout = fields[i].matrix_layout;
  130. }
  131. }
  132. bool
  133. glsl_type::contains_sampler() const
  134. {
  135. if (this->is_array()) {
  136. return this->fields.array->contains_sampler();
  137. } else if (this->is_record()) {
  138. for (unsigned int i = 0; i < this->length; i++) {
  139. if (this->fields.structure[i].type->contains_sampler())
  140. return true;
  141. }
  142. return false;
  143. } else {
  144. return this->is_sampler();
  145. }
  146. }
  147. bool
  148. glsl_type::contains_integer() const
  149. {
  150. if (this->is_array()) {
  151. return this->fields.array->contains_integer();
  152. } else if (this->is_record()) {
  153. for (unsigned int i = 0; i < this->length; i++) {
  154. if (this->fields.structure[i].type->contains_integer())
  155. return true;
  156. }
  157. return false;
  158. } else {
  159. return this->is_integer();
  160. }
  161. }
  162. bool
  163. glsl_type::contains_opaque() const {
  164. switch (base_type) {
  165. case GLSL_TYPE_SAMPLER:
  166. case GLSL_TYPE_IMAGE:
  167. case GLSL_TYPE_ATOMIC_UINT:
  168. return true;
  169. case GLSL_TYPE_ARRAY:
  170. return element_type()->contains_opaque();
  171. case GLSL_TYPE_STRUCT:
  172. for (unsigned int i = 0; i < length; i++) {
  173. if (fields.structure[i].type->contains_opaque())
  174. return true;
  175. }
  176. return false;
  177. default:
  178. return false;
  179. }
  180. }
  181. gl_texture_index
  182. glsl_type::sampler_index() const
  183. {
  184. const glsl_type *const t = (this->is_array()) ? this->fields.array : this;
  185. assert(t->is_sampler());
  186. switch (t->sampler_dimensionality) {
  187. case GLSL_SAMPLER_DIM_1D:
  188. return (t->sampler_array) ? TEXTURE_1D_ARRAY_INDEX : TEXTURE_1D_INDEX;
  189. case GLSL_SAMPLER_DIM_2D:
  190. return (t->sampler_array) ? TEXTURE_2D_ARRAY_INDEX : TEXTURE_2D_INDEX;
  191. case GLSL_SAMPLER_DIM_3D:
  192. return TEXTURE_3D_INDEX;
  193. case GLSL_SAMPLER_DIM_CUBE:
  194. return (t->sampler_array) ? TEXTURE_CUBE_ARRAY_INDEX : TEXTURE_CUBE_INDEX;
  195. case GLSL_SAMPLER_DIM_RECT:
  196. return TEXTURE_RECT_INDEX;
  197. case GLSL_SAMPLER_DIM_BUF:
  198. return TEXTURE_BUFFER_INDEX;
  199. case GLSL_SAMPLER_DIM_EXTERNAL:
  200. return TEXTURE_EXTERNAL_INDEX;
  201. case GLSL_SAMPLER_DIM_MS:
  202. return (t->sampler_array) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX : TEXTURE_2D_MULTISAMPLE_INDEX;
  203. default:
  204. assert(!"Should not get here.");
  205. return TEXTURE_BUFFER_INDEX;
  206. }
  207. }
  208. bool
  209. glsl_type::contains_image() const
  210. {
  211. if (this->is_array()) {
  212. return this->fields.array->contains_image();
  213. } else if (this->is_record()) {
  214. for (unsigned int i = 0; i < this->length; i++) {
  215. if (this->fields.structure[i].type->contains_image())
  216. return true;
  217. }
  218. return false;
  219. } else {
  220. return this->is_image();
  221. }
  222. }
  223. const glsl_type *glsl_type::get_base_type() const
  224. {
  225. switch (base_type) {
  226. case GLSL_TYPE_UINT:
  227. return uint_type;
  228. case GLSL_TYPE_INT:
  229. return int_type;
  230. case GLSL_TYPE_FLOAT:
  231. return float_type;
  232. case GLSL_TYPE_BOOL:
  233. return bool_type;
  234. default:
  235. return error_type;
  236. }
  237. }
  238. const glsl_type *glsl_type::get_scalar_type() const
  239. {
  240. const glsl_type *type = this;
  241. /* Handle arrays */
  242. while (type->base_type == GLSL_TYPE_ARRAY)
  243. type = type->fields.array;
  244. /* Handle vectors and matrices */
  245. switch (type->base_type) {
  246. case GLSL_TYPE_UINT:
  247. return uint_type;
  248. case GLSL_TYPE_INT:
  249. return int_type;
  250. case GLSL_TYPE_FLOAT:
  251. return float_type;
  252. case GLSL_TYPE_BOOL:
  253. return bool_type;
  254. default:
  255. /* Handle everything else */
  256. return type;
  257. }
  258. }
  259. void
  260. _mesa_glsl_release_types(void)
  261. {
  262. if (glsl_type::array_types != NULL) {
  263. hash_table_dtor(glsl_type::array_types);
  264. glsl_type::array_types = NULL;
  265. }
  266. if (glsl_type::record_types != NULL) {
  267. hash_table_dtor(glsl_type::record_types);
  268. glsl_type::record_types = NULL;
  269. }
  270. }
  271. glsl_type::glsl_type(const glsl_type *array, unsigned length) :
  272. base_type(GLSL_TYPE_ARRAY),
  273. sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
  274. sampler_type(0), interface_packing(0),
  275. vector_elements(0), matrix_columns(0),
  276. name(NULL), length(length)
  277. {
  278. this->fields.array = array;
  279. /* Inherit the gl type of the base. The GL type is used for
  280. * uniform/statevar handling in Mesa and the arrayness of the type
  281. * is represented by the size rather than the type.
  282. */
  283. this->gl_type = array->gl_type;
  284. /* Allow a maximum of 10 characters for the array size. This is enough
  285. * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
  286. * NUL.
  287. */
  288. const unsigned name_length = strlen(array->name) + 10 + 3;
  289. char *const n = (char *) ralloc_size(this->mem_ctx, name_length);
  290. if (length == 0)
  291. snprintf(n, name_length, "%s[]", array->name);
  292. else {
  293. /* insert outermost dimensions in the correct spot
  294. * otherwise the dimension order will be backwards
  295. */
  296. const char *pos = strchr(array->name, '[');
  297. if (pos) {
  298. int idx = pos - array->name;
  299. snprintf(n, idx+1, "%s", array->name);
  300. snprintf(n + idx, name_length - idx, "[%u]%s",
  301. length, array->name + idx);
  302. } else {
  303. snprintf(n, name_length, "%s[%u]", array->name, length);
  304. }
  305. }
  306. this->name = n;
  307. }
  308. const glsl_type *
  309. glsl_type::vec(unsigned components)
  310. {
  311. if (components == 0 || components > 4)
  312. return error_type;
  313. static const glsl_type *const ts[] = {
  314. float_type, vec2_type, vec3_type, vec4_type
  315. };
  316. return ts[components - 1];
  317. }
  318. const glsl_type *
  319. glsl_type::ivec(unsigned components)
  320. {
  321. if (components == 0 || components > 4)
  322. return error_type;
  323. static const glsl_type *const ts[] = {
  324. int_type, ivec2_type, ivec3_type, ivec4_type
  325. };
  326. return ts[components - 1];
  327. }
  328. const glsl_type *
  329. glsl_type::uvec(unsigned components)
  330. {
  331. if (components == 0 || components > 4)
  332. return error_type;
  333. static const glsl_type *const ts[] = {
  334. uint_type, uvec2_type, uvec3_type, uvec4_type
  335. };
  336. return ts[components - 1];
  337. }
  338. const glsl_type *
  339. glsl_type::bvec(unsigned components)
  340. {
  341. if (components == 0 || components > 4)
  342. return error_type;
  343. static const glsl_type *const ts[] = {
  344. bool_type, bvec2_type, bvec3_type, bvec4_type
  345. };
  346. return ts[components - 1];
  347. }
  348. const glsl_type *
  349. glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
  350. {
  351. if (base_type == GLSL_TYPE_VOID)
  352. return void_type;
  353. if ((rows < 1) || (rows > 4) || (columns < 1) || (columns > 4))
  354. return error_type;
  355. /* Treat GLSL vectors as Nx1 matrices.
  356. */
  357. if (columns == 1) {
  358. switch (base_type) {
  359. case GLSL_TYPE_UINT:
  360. return uvec(rows);
  361. case GLSL_TYPE_INT:
  362. return ivec(rows);
  363. case GLSL_TYPE_FLOAT:
  364. return vec(rows);
  365. case GLSL_TYPE_BOOL:
  366. return bvec(rows);
  367. default:
  368. return error_type;
  369. }
  370. } else {
  371. if ((base_type != GLSL_TYPE_FLOAT) || (rows == 1))
  372. return error_type;
  373. /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
  374. * combinations are valid:
  375. *
  376. * 1 2 3 4
  377. * 1
  378. * 2 x x x
  379. * 3 x x x
  380. * 4 x x x
  381. */
  382. #define IDX(c,r) (((c-1)*3) + (r-1))
  383. switch (IDX(columns, rows)) {
  384. case IDX(2,2): return mat2_type;
  385. case IDX(2,3): return mat2x3_type;
  386. case IDX(2,4): return mat2x4_type;
  387. case IDX(3,2): return mat3x2_type;
  388. case IDX(3,3): return mat3_type;
  389. case IDX(3,4): return mat3x4_type;
  390. case IDX(4,2): return mat4x2_type;
  391. case IDX(4,3): return mat4x3_type;
  392. case IDX(4,4): return mat4_type;
  393. default: return error_type;
  394. }
  395. }
  396. assert(!"Should not get here.");
  397. return error_type;
  398. }
  399. const glsl_type *
  400. glsl_type::get_array_instance(const glsl_type *base, unsigned array_size)
  401. {
  402. if (array_types == NULL) {
  403. array_types = hash_table_ctor(64, hash_table_string_hash,
  404. hash_table_string_compare);
  405. }
  406. /* Generate a name using the base type pointer in the key. This is
  407. * done because the name of the base type may not be unique across
  408. * shaders. For example, two shaders may have different record types
  409. * named 'foo'.
  410. */
  411. char key[128];
  412. snprintf(key, sizeof(key), "%p[%u]", (void *) base, array_size);
  413. const glsl_type *t = (glsl_type *) hash_table_find(array_types, key);
  414. if (t == NULL) {
  415. t = new glsl_type(base, array_size);
  416. hash_table_insert(array_types, (void *) t, ralloc_strdup(mem_ctx, key));
  417. }
  418. assert(t->base_type == GLSL_TYPE_ARRAY);
  419. assert(t->length == array_size);
  420. assert(t->fields.array == base);
  421. return t;
  422. }
  423. bool
  424. glsl_type::record_compare(const glsl_type *b) const
  425. {
  426. if (this->length != b->length)
  427. return false;
  428. if (this->interface_packing != b->interface_packing)
  429. return false;
  430. /* From the GLSL 4.20 specification (Sec 4.2):
  431. *
  432. * "Structures must have the same name, sequence of type names, and
  433. * type definitions, and field names to be considered the same type."
  434. *
  435. * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
  436. *
  437. * Note that we cannot force type name check when comparing unnamed
  438. * structure types, these have a unique name assigned during parsing.
  439. */
  440. if (!this->is_anonymous() && !b->is_anonymous())
  441. if (strcmp(this->name, b->name) != 0)
  442. return false;
  443. for (unsigned i = 0; i < this->length; i++) {
  444. if (this->fields.structure[i].type != b->fields.structure[i].type)
  445. return false;
  446. if (strcmp(this->fields.structure[i].name,
  447. b->fields.structure[i].name) != 0)
  448. return false;
  449. if (this->fields.structure[i].matrix_layout
  450. != b->fields.structure[i].matrix_layout)
  451. return false;
  452. if (this->fields.structure[i].location
  453. != b->fields.structure[i].location)
  454. return false;
  455. if (this->fields.structure[i].interpolation
  456. != b->fields.structure[i].interpolation)
  457. return false;
  458. if (this->fields.structure[i].centroid
  459. != b->fields.structure[i].centroid)
  460. return false;
  461. if (this->fields.structure[i].sample
  462. != b->fields.structure[i].sample)
  463. return false;
  464. }
  465. return true;
  466. }
  467. int
  468. glsl_type::record_key_compare(const void *a, const void *b)
  469. {
  470. const glsl_type *const key1 = (glsl_type *) a;
  471. const glsl_type *const key2 = (glsl_type *) b;
  472. /* Return zero is the types match (there is zero difference) or non-zero
  473. * otherwise.
  474. */
  475. if (strcmp(key1->name, key2->name) != 0)
  476. return 1;
  477. return !key1->record_compare(key2);
  478. }
  479. unsigned
  480. glsl_type::record_key_hash(const void *a)
  481. {
  482. const glsl_type *const key = (glsl_type *) a;
  483. char hash_key[128];
  484. unsigned size = 0;
  485. size = snprintf(hash_key, sizeof(hash_key), "%08x", key->length);
  486. for (unsigned i = 0; i < key->length; i++) {
  487. if (size >= sizeof(hash_key))
  488. break;
  489. size += snprintf(& hash_key[size], sizeof(hash_key) - size,
  490. "%p", (void *) key->fields.structure[i].type);
  491. }
  492. return hash_table_string_hash(& hash_key);
  493. }
  494. const glsl_type *
  495. glsl_type::get_record_instance(const glsl_struct_field *fields,
  496. unsigned num_fields,
  497. const char *name)
  498. {
  499. const glsl_type key(fields, num_fields, name);
  500. if (record_types == NULL) {
  501. record_types = hash_table_ctor(64, record_key_hash, record_key_compare);
  502. }
  503. const glsl_type *t = (glsl_type *) hash_table_find(record_types, & key);
  504. if (t == NULL) {
  505. t = new glsl_type(fields, num_fields, name);
  506. hash_table_insert(record_types, (void *) t, t);
  507. }
  508. assert(t->base_type == GLSL_TYPE_STRUCT);
  509. assert(t->length == num_fields);
  510. assert(strcmp(t->name, name) == 0);
  511. return t;
  512. }
  513. const glsl_type *
  514. glsl_type::get_interface_instance(const glsl_struct_field *fields,
  515. unsigned num_fields,
  516. enum glsl_interface_packing packing,
  517. const char *block_name)
  518. {
  519. const glsl_type key(fields, num_fields, packing, block_name);
  520. if (interface_types == NULL) {
  521. interface_types = hash_table_ctor(64, record_key_hash, record_key_compare);
  522. }
  523. const glsl_type *t = (glsl_type *) hash_table_find(interface_types, & key);
  524. if (t == NULL) {
  525. t = new glsl_type(fields, num_fields, packing, block_name);
  526. hash_table_insert(interface_types, (void *) t, t);
  527. }
  528. assert(t->base_type == GLSL_TYPE_INTERFACE);
  529. assert(t->length == num_fields);
  530. assert(strcmp(t->name, block_name) == 0);
  531. return t;
  532. }
  533. const glsl_type *
  534. glsl_type::field_type(const char *name) const
  535. {
  536. if (this->base_type != GLSL_TYPE_STRUCT
  537. && this->base_type != GLSL_TYPE_INTERFACE)
  538. return error_type;
  539. for (unsigned i = 0; i < this->length; i++) {
  540. if (strcmp(name, this->fields.structure[i].name) == 0)
  541. return this->fields.structure[i].type;
  542. }
  543. return error_type;
  544. }
  545. int
  546. glsl_type::field_index(const char *name) const
  547. {
  548. if (this->base_type != GLSL_TYPE_STRUCT
  549. && this->base_type != GLSL_TYPE_INTERFACE)
  550. return -1;
  551. for (unsigned i = 0; i < this->length; i++) {
  552. if (strcmp(name, this->fields.structure[i].name) == 0)
  553. return i;
  554. }
  555. return -1;
  556. }
  557. unsigned
  558. glsl_type::component_slots() const
  559. {
  560. switch (this->base_type) {
  561. case GLSL_TYPE_UINT:
  562. case GLSL_TYPE_INT:
  563. case GLSL_TYPE_FLOAT:
  564. case GLSL_TYPE_BOOL:
  565. return this->components();
  566. case GLSL_TYPE_STRUCT:
  567. case GLSL_TYPE_INTERFACE: {
  568. unsigned size = 0;
  569. for (unsigned i = 0; i < this->length; i++)
  570. size += this->fields.structure[i].type->component_slots();
  571. return size;
  572. }
  573. case GLSL_TYPE_ARRAY:
  574. return this->length * this->fields.array->component_slots();
  575. case GLSL_TYPE_IMAGE:
  576. return 1;
  577. case GLSL_TYPE_SAMPLER:
  578. case GLSL_TYPE_ATOMIC_UINT:
  579. case GLSL_TYPE_VOID:
  580. case GLSL_TYPE_ERROR:
  581. break;
  582. }
  583. return 0;
  584. }
  585. unsigned
  586. glsl_type::uniform_locations() const
  587. {
  588. unsigned size = 0;
  589. switch (this->base_type) {
  590. case GLSL_TYPE_UINT:
  591. case GLSL_TYPE_INT:
  592. case GLSL_TYPE_FLOAT:
  593. case GLSL_TYPE_BOOL:
  594. case GLSL_TYPE_SAMPLER:
  595. case GLSL_TYPE_IMAGE:
  596. return 1;
  597. case GLSL_TYPE_STRUCT:
  598. case GLSL_TYPE_INTERFACE:
  599. for (unsigned i = 0; i < this->length; i++)
  600. size += this->fields.structure[i].type->uniform_locations();
  601. return size;
  602. case GLSL_TYPE_ARRAY:
  603. return this->length * this->fields.array->uniform_locations();
  604. default:
  605. return 0;
  606. }
  607. }
  608. bool
  609. glsl_type::can_implicitly_convert_to(const glsl_type *desired,
  610. _mesa_glsl_parse_state *state) const
  611. {
  612. if (this == desired)
  613. return true;
  614. /* There is no conversion among matrix types. */
  615. if (this->matrix_columns > 1 || desired->matrix_columns > 1)
  616. return false;
  617. /* Vector size must match. */
  618. if (this->vector_elements != desired->vector_elements)
  619. return false;
  620. /* int and uint can be converted to float. */
  621. if (desired->is_float() && this->is_integer())
  622. return true;
  623. /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
  624. * Note that state may be NULL here, when resolving function calls in the
  625. * linker. By this time, all the state-dependent checks have already
  626. * happened though, so allow anything that's allowed in any shader version. */
  627. if ((!state || state->is_version(400, 0) || state->ARB_gpu_shader5_enable) &&
  628. desired->base_type == GLSL_TYPE_UINT && this->base_type == GLSL_TYPE_INT)
  629. return true;
  630. return false;
  631. }
  632. unsigned
  633. glsl_type::std140_base_alignment(bool row_major) const
  634. {
  635. /* (1) If the member is a scalar consuming <N> basic machine units, the
  636. * base alignment is <N>.
  637. *
  638. * (2) If the member is a two- or four-component vector with components
  639. * consuming <N> basic machine units, the base alignment is 2<N> or
  640. * 4<N>, respectively.
  641. *
  642. * (3) If the member is a three-component vector with components consuming
  643. * <N> basic machine units, the base alignment is 4<N>.
  644. */
  645. if (this->is_scalar() || this->is_vector()) {
  646. switch (this->vector_elements) {
  647. case 1:
  648. return 4;
  649. case 2:
  650. return 8;
  651. case 3:
  652. case 4:
  653. return 16;
  654. }
  655. }
  656. /* (4) If the member is an array of scalars or vectors, the base alignment
  657. * and array stride are set to match the base alignment of a single
  658. * array element, according to rules (1), (2), and (3), and rounded up
  659. * to the base alignment of a vec4. The array may have padding at the
  660. * end; the base offset of the member following the array is rounded up
  661. * to the next multiple of the base alignment.
  662. *
  663. * (6) If the member is an array of <S> column-major matrices with <C>
  664. * columns and <R> rows, the matrix is stored identically to a row of
  665. * <S>*<C> column vectors with <R> components each, according to rule
  666. * (4).
  667. *
  668. * (8) If the member is an array of <S> row-major matrices with <C> columns
  669. * and <R> rows, the matrix is stored identically to a row of <S>*<R>
  670. * row vectors with <C> components each, according to rule (4).
  671. *
  672. * (10) If the member is an array of <S> structures, the <S> elements of
  673. * the array are laid out in order, according to rule (9).
  674. */
  675. if (this->is_array()) {
  676. if (this->fields.array->is_scalar() ||
  677. this->fields.array->is_vector() ||
  678. this->fields.array->is_matrix()) {
  679. return MAX2(this->fields.array->std140_base_alignment(row_major), 16);
  680. } else {
  681. assert(this->fields.array->is_record());
  682. return this->fields.array->std140_base_alignment(row_major);
  683. }
  684. }
  685. /* (5) If the member is a column-major matrix with <C> columns and
  686. * <R> rows, the matrix is stored identically to an array of
  687. * <C> column vectors with <R> components each, according to
  688. * rule (4).
  689. *
  690. * (7) If the member is a row-major matrix with <C> columns and <R>
  691. * rows, the matrix is stored identically to an array of <R>
  692. * row vectors with <C> components each, according to rule (4).
  693. */
  694. if (this->is_matrix()) {
  695. const struct glsl_type *vec_type, *array_type;
  696. int c = this->matrix_columns;
  697. int r = this->vector_elements;
  698. if (row_major) {
  699. vec_type = get_instance(GLSL_TYPE_FLOAT, c, 1);
  700. array_type = glsl_type::get_array_instance(vec_type, r);
  701. } else {
  702. vec_type = get_instance(GLSL_TYPE_FLOAT, r, 1);
  703. array_type = glsl_type::get_array_instance(vec_type, c);
  704. }
  705. return array_type->std140_base_alignment(false);
  706. }
  707. /* (9) If the member is a structure, the base alignment of the
  708. * structure is <N>, where <N> is the largest base alignment
  709. * value of any of its members, and rounded up to the base
  710. * alignment of a vec4. The individual members of this
  711. * sub-structure are then assigned offsets by applying this set
  712. * of rules recursively, where the base offset of the first
  713. * member of the sub-structure is equal to the aligned offset
  714. * of the structure. The structure may have padding at the end;
  715. * the base offset of the member following the sub-structure is
  716. * rounded up to the next multiple of the base alignment of the
  717. * structure.
  718. */
  719. if (this->is_record()) {
  720. unsigned base_alignment = 16;
  721. for (unsigned i = 0; i < this->length; i++) {
  722. bool field_row_major = row_major;
  723. const enum glsl_matrix_layout matrix_layout =
  724. glsl_matrix_layout(this->fields.structure[i].matrix_layout);
  725. if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
  726. field_row_major = true;
  727. } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
  728. field_row_major = false;
  729. }
  730. const struct glsl_type *field_type = this->fields.structure[i].type;
  731. base_alignment = MAX2(base_alignment,
  732. field_type->std140_base_alignment(field_row_major));
  733. }
  734. return base_alignment;
  735. }
  736. assert(!"not reached");
  737. return -1;
  738. }
  739. unsigned
  740. glsl_type::std140_size(bool row_major) const
  741. {
  742. /* (1) If the member is a scalar consuming <N> basic machine units, the
  743. * base alignment is <N>.
  744. *
  745. * (2) If the member is a two- or four-component vector with components
  746. * consuming <N> basic machine units, the base alignment is 2<N> or
  747. * 4<N>, respectively.
  748. *
  749. * (3) If the member is a three-component vector with components consuming
  750. * <N> basic machine units, the base alignment is 4<N>.
  751. */
  752. if (this->is_scalar() || this->is_vector()) {
  753. return this->vector_elements * 4;
  754. }
  755. /* (5) If the member is a column-major matrix with <C> columns and
  756. * <R> rows, the matrix is stored identically to an array of
  757. * <C> column vectors with <R> components each, according to
  758. * rule (4).
  759. *
  760. * (6) If the member is an array of <S> column-major matrices with <C>
  761. * columns and <R> rows, the matrix is stored identically to a row of
  762. * <S>*<C> column vectors with <R> components each, according to rule
  763. * (4).
  764. *
  765. * (7) If the member is a row-major matrix with <C> columns and <R>
  766. * rows, the matrix is stored identically to an array of <R>
  767. * row vectors with <C> components each, according to rule (4).
  768. *
  769. * (8) If the member is an array of <S> row-major matrices with <C> columns
  770. * and <R> rows, the matrix is stored identically to a row of <S>*<R>
  771. * row vectors with <C> components each, according to rule (4).
  772. */
  773. if (this->without_array()->is_matrix()) {
  774. const struct glsl_type *element_type;
  775. const struct glsl_type *vec_type;
  776. unsigned int array_len;
  777. if (this->is_array()) {
  778. element_type = this->fields.array;
  779. array_len = this->length;
  780. } else {
  781. element_type = this;
  782. array_len = 1;
  783. }
  784. if (row_major) {
  785. vec_type = get_instance(GLSL_TYPE_FLOAT,
  786. element_type->matrix_columns, 1);
  787. array_len *= element_type->vector_elements;
  788. } else {
  789. vec_type = get_instance(GLSL_TYPE_FLOAT,
  790. element_type->vector_elements, 1);
  791. array_len *= element_type->matrix_columns;
  792. }
  793. const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
  794. array_len);
  795. return array_type->std140_size(false);
  796. }
  797. /* (4) If the member is an array of scalars or vectors, the base alignment
  798. * and array stride are set to match the base alignment of a single
  799. * array element, according to rules (1), (2), and (3), and rounded up
  800. * to the base alignment of a vec4. The array may have padding at the
  801. * end; the base offset of the member following the array is rounded up
  802. * to the next multiple of the base alignment.
  803. *
  804. * (10) If the member is an array of <S> structures, the <S> elements of
  805. * the array are laid out in order, according to rule (9).
  806. */
  807. if (this->is_array()) {
  808. if (this->fields.array->is_record()) {
  809. return this->length * this->fields.array->std140_size(row_major);
  810. } else {
  811. unsigned element_base_align =
  812. this->fields.array->std140_base_alignment(row_major);
  813. return this->length * MAX2(element_base_align, 16);
  814. }
  815. }
  816. /* (9) If the member is a structure, the base alignment of the
  817. * structure is <N>, where <N> is the largest base alignment
  818. * value of any of its members, and rounded up to the base
  819. * alignment of a vec4. The individual members of this
  820. * sub-structure are then assigned offsets by applying this set
  821. * of rules recursively, where the base offset of the first
  822. * member of the sub-structure is equal to the aligned offset
  823. * of the structure. The structure may have padding at the end;
  824. * the base offset of the member following the sub-structure is
  825. * rounded up to the next multiple of the base alignment of the
  826. * structure.
  827. */
  828. if (this->is_record()) {
  829. unsigned size = 0;
  830. unsigned max_align = 0;
  831. for (unsigned i = 0; i < this->length; i++) {
  832. bool field_row_major = row_major;
  833. const enum glsl_matrix_layout matrix_layout =
  834. glsl_matrix_layout(this->fields.structure[i].matrix_layout);
  835. if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
  836. field_row_major = true;
  837. } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
  838. field_row_major = false;
  839. }
  840. const struct glsl_type *field_type = this->fields.structure[i].type;
  841. unsigned align = field_type->std140_base_alignment(field_row_major);
  842. size = glsl_align(size, align);
  843. size += field_type->std140_size(field_row_major);
  844. max_align = MAX2(align, max_align);
  845. if (field_type->is_record() && (i + 1 < this->length))
  846. size = glsl_align(size, 16);
  847. }
  848. size = glsl_align(size, MAX2(max_align, 16));
  849. return size;
  850. }
  851. assert(!"not reached");
  852. return -1;
  853. }
  854. unsigned
  855. glsl_type::count_attribute_slots() const
  856. {
  857. /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
  858. *
  859. * "A scalar input counts the same amount against this limit as a vec4,
  860. * so applications may want to consider packing groups of four
  861. * unrelated float inputs together into a vector to better utilize the
  862. * capabilities of the underlying hardware. A matrix input will use up
  863. * multiple locations. The number of locations used will equal the
  864. * number of columns in the matrix."
  865. *
  866. * The spec does not explicitly say how arrays are counted. However, it
  867. * should be safe to assume the total number of slots consumed by an array
  868. * is the number of entries in the array multiplied by the number of slots
  869. * consumed by a single element of the array.
  870. *
  871. * The spec says nothing about how structs are counted, because vertex
  872. * attributes are not allowed to be (or contain) structs. However, Mesa
  873. * allows varying structs, the number of varying slots taken up by a
  874. * varying struct is simply equal to the sum of the number of slots taken
  875. * up by each element.
  876. */
  877. switch (this->base_type) {
  878. case GLSL_TYPE_UINT:
  879. case GLSL_TYPE_INT:
  880. case GLSL_TYPE_FLOAT:
  881. case GLSL_TYPE_BOOL:
  882. return this->matrix_columns;
  883. case GLSL_TYPE_STRUCT:
  884. case GLSL_TYPE_INTERFACE: {
  885. unsigned size = 0;
  886. for (unsigned i = 0; i < this->length; i++)
  887. size += this->fields.structure[i].type->count_attribute_slots();
  888. return size;
  889. }
  890. case GLSL_TYPE_ARRAY:
  891. return this->length * this->fields.array->count_attribute_slots();
  892. case GLSL_TYPE_SAMPLER:
  893. case GLSL_TYPE_IMAGE:
  894. case GLSL_TYPE_ATOMIC_UINT:
  895. case GLSL_TYPE_VOID:
  896. case GLSL_TYPE_ERROR:
  897. break;
  898. }
  899. assert(!"Unexpected type in count_attribute_slots()");
  900. return 0;
  901. }
  902. int
  903. glsl_type::coordinate_components() const
  904. {
  905. int size;
  906. switch (sampler_dimensionality) {
  907. case GLSL_SAMPLER_DIM_1D:
  908. case GLSL_SAMPLER_DIM_BUF:
  909. size = 1;
  910. break;
  911. case GLSL_SAMPLER_DIM_2D:
  912. case GLSL_SAMPLER_DIM_RECT:
  913. case GLSL_SAMPLER_DIM_MS:
  914. case GLSL_SAMPLER_DIM_EXTERNAL:
  915. size = 2;
  916. break;
  917. case GLSL_SAMPLER_DIM_3D:
  918. case GLSL_SAMPLER_DIM_CUBE:
  919. size = 3;
  920. break;
  921. default:
  922. assert(!"Should not get here.");
  923. size = 1;
  924. break;
  925. }
  926. /* Array textures need an additional component for the array index. */
  927. if (sampler_array)
  928. size += 1;
  929. return size;
  930. }