glsl: fix indention in glsl_types.cpp
[mesa.git] / src / glsl / glsl_types.cpp
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
24 #include <stdio.h>
25 #include "main/core.h" /* for Elements, MAX2 */
26 #include "glsl_parser_extras.h"
27 #include "glsl_types.h"
28 #include "util/hash_table.h"
29
30
31 mtx_t glsl_type::mutex = _MTX_INITIALIZER_NP;
32 hash_table *glsl_type::array_types = NULL;
33 hash_table *glsl_type::record_types = NULL;
34 hash_table *glsl_type::interface_types = NULL;
35 hash_table *glsl_type::subroutine_types = NULL;
36 void *glsl_type::mem_ctx = NULL;
37
38 void
39 glsl_type::init_ralloc_type_ctx(void)
40 {
41 if (glsl_type::mem_ctx == NULL) {
42 glsl_type::mem_ctx = ralloc_autofree_context();
43 assert(glsl_type::mem_ctx != NULL);
44 }
45 }
46
47 glsl_type::glsl_type(GLenum gl_type,
48 glsl_base_type base_type, unsigned vector_elements,
49 unsigned matrix_columns, const char *name) :
50 gl_type(gl_type),
51 base_type(base_type),
52 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
53 sampler_type(0), interface_packing(0),
54 vector_elements(vector_elements), matrix_columns(matrix_columns),
55 length(0)
56 {
57 mtx_lock(&glsl_type::mutex);
58
59 init_ralloc_type_ctx();
60 assert(name != NULL);
61 this->name = ralloc_strdup(this->mem_ctx, name);
62
63 mtx_unlock(&glsl_type::mutex);
64
65 /* Neither dimension is zero or both dimensions are zero.
66 */
67 assert((vector_elements == 0) == (matrix_columns == 0));
68 memset(& fields, 0, sizeof(fields));
69 }
70
71 glsl_type::glsl_type(GLenum gl_type, glsl_base_type base_type,
72 enum glsl_sampler_dim dim, bool shadow, bool array,
73 unsigned type, const char *name) :
74 gl_type(gl_type),
75 base_type(base_type),
76 sampler_dimensionality(dim), sampler_shadow(shadow),
77 sampler_array(array), sampler_type(type), interface_packing(0),
78 length(0)
79 {
80 mtx_lock(&glsl_type::mutex);
81
82 init_ralloc_type_ctx();
83 assert(name != NULL);
84 this->name = ralloc_strdup(this->mem_ctx, name);
85
86 mtx_unlock(&glsl_type::mutex);
87
88 memset(& fields, 0, sizeof(fields));
89
90 if (base_type == GLSL_TYPE_SAMPLER) {
91 /* Samplers take no storage whatsoever. */
92 matrix_columns = vector_elements = 0;
93 } else {
94 matrix_columns = vector_elements = 1;
95 }
96 }
97
98 glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
99 const char *name) :
100 gl_type(0),
101 base_type(GLSL_TYPE_STRUCT),
102 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
103 sampler_type(0), interface_packing(0),
104 vector_elements(0), matrix_columns(0),
105 length(num_fields)
106 {
107 unsigned int i;
108
109 mtx_lock(&glsl_type::mutex);
110
111 init_ralloc_type_ctx();
112 assert(name != NULL);
113 this->name = ralloc_strdup(this->mem_ctx, name);
114 this->fields.structure = ralloc_array(this->mem_ctx,
115 glsl_struct_field, length);
116
117 for (i = 0; i < length; i++) {
118 this->fields.structure[i].type = fields[i].type;
119 this->fields.structure[i].name = ralloc_strdup(this->fields.structure,
120 fields[i].name);
121 this->fields.structure[i].location = fields[i].location;
122 this->fields.structure[i].interpolation = fields[i].interpolation;
123 this->fields.structure[i].centroid = fields[i].centroid;
124 this->fields.structure[i].sample = fields[i].sample;
125 this->fields.structure[i].matrix_layout = fields[i].matrix_layout;
126 this->fields.structure[i].patch = fields[i].patch;
127 }
128
129 mtx_unlock(&glsl_type::mutex);
130 }
131
132 glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
133 enum glsl_interface_packing packing, const char *name) :
134 gl_type(0),
135 base_type(GLSL_TYPE_INTERFACE),
136 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
137 sampler_type(0), interface_packing((unsigned) packing),
138 vector_elements(0), matrix_columns(0),
139 length(num_fields)
140 {
141 unsigned int i;
142
143 mtx_lock(&glsl_type::mutex);
144
145 init_ralloc_type_ctx();
146 assert(name != NULL);
147 this->name = ralloc_strdup(this->mem_ctx, name);
148 this->fields.structure = ralloc_array(this->mem_ctx,
149 glsl_struct_field, length);
150 for (i = 0; i < length; i++) {
151 this->fields.structure[i].type = fields[i].type;
152 this->fields.structure[i].name = ralloc_strdup(this->fields.structure,
153 fields[i].name);
154 this->fields.structure[i].location = fields[i].location;
155 this->fields.structure[i].interpolation = fields[i].interpolation;
156 this->fields.structure[i].centroid = fields[i].centroid;
157 this->fields.structure[i].sample = fields[i].sample;
158 this->fields.structure[i].matrix_layout = fields[i].matrix_layout;
159 this->fields.structure[i].patch = fields[i].patch;
160 }
161
162 mtx_unlock(&glsl_type::mutex);
163 }
164
165 glsl_type::glsl_type(const char *subroutine_name) :
166 gl_type(0),
167 base_type(GLSL_TYPE_SUBROUTINE),
168 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
169 sampler_type(0), interface_packing(0),
170 vector_elements(0), matrix_columns(0),
171 length(0)
172 {
173 mtx_lock(&glsl_type::mutex);
174
175 init_ralloc_type_ctx();
176 assert(subroutine_name != NULL);
177 this->name = ralloc_strdup(this->mem_ctx, subroutine_name);
178 this->vector_elements = 1;
179 mtx_unlock(&glsl_type::mutex);
180 }
181
182 bool
183 glsl_type::contains_sampler() const
184 {
185 if (this->is_array()) {
186 return this->fields.array->contains_sampler();
187 } else if (this->is_record()) {
188 for (unsigned int i = 0; i < this->length; i++) {
189 if (this->fields.structure[i].type->contains_sampler())
190 return true;
191 }
192 return false;
193 } else {
194 return this->is_sampler();
195 }
196 }
197
198
199 bool
200 glsl_type::contains_integer() const
201 {
202 if (this->is_array()) {
203 return this->fields.array->contains_integer();
204 } else if (this->is_record()) {
205 for (unsigned int i = 0; i < this->length; i++) {
206 if (this->fields.structure[i].type->contains_integer())
207 return true;
208 }
209 return false;
210 } else {
211 return this->is_integer();
212 }
213 }
214
215 bool
216 glsl_type::contains_double() const
217 {
218 if (this->is_array()) {
219 return this->fields.array->contains_double();
220 } else if (this->is_record()) {
221 for (unsigned int i = 0; i < this->length; i++) {
222 if (this->fields.structure[i].type->contains_double())
223 return true;
224 }
225 return false;
226 } else {
227 return this->is_double();
228 }
229 }
230
231 bool
232 glsl_type::contains_opaque() const {
233 switch (base_type) {
234 case GLSL_TYPE_SAMPLER:
235 case GLSL_TYPE_IMAGE:
236 case GLSL_TYPE_ATOMIC_UINT:
237 return true;
238 case GLSL_TYPE_ARRAY:
239 return fields.array->contains_opaque();
240 case GLSL_TYPE_STRUCT:
241 for (unsigned int i = 0; i < length; i++) {
242 if (fields.structure[i].type->contains_opaque())
243 return true;
244 }
245 return false;
246 default:
247 return false;
248 }
249 }
250
251 bool
252 glsl_type::contains_subroutine() const
253 {
254 if (this->is_array()) {
255 return this->fields.array->contains_subroutine();
256 } else if (this->is_record()) {
257 for (unsigned int i = 0; i < this->length; i++) {
258 if (this->fields.structure[i].type->contains_subroutine())
259 return true;
260 }
261 return false;
262 } else {
263 return this->is_subroutine();
264 }
265 }
266
267 gl_texture_index
268 glsl_type::sampler_index() const
269 {
270 const glsl_type *const t = (this->is_array()) ? this->fields.array : this;
271
272 assert(t->is_sampler());
273
274 switch (t->sampler_dimensionality) {
275 case GLSL_SAMPLER_DIM_1D:
276 return (t->sampler_array) ? TEXTURE_1D_ARRAY_INDEX : TEXTURE_1D_INDEX;
277 case GLSL_SAMPLER_DIM_2D:
278 return (t->sampler_array) ? TEXTURE_2D_ARRAY_INDEX : TEXTURE_2D_INDEX;
279 case GLSL_SAMPLER_DIM_3D:
280 return TEXTURE_3D_INDEX;
281 case GLSL_SAMPLER_DIM_CUBE:
282 return (t->sampler_array) ? TEXTURE_CUBE_ARRAY_INDEX : TEXTURE_CUBE_INDEX;
283 case GLSL_SAMPLER_DIM_RECT:
284 return TEXTURE_RECT_INDEX;
285 case GLSL_SAMPLER_DIM_BUF:
286 return TEXTURE_BUFFER_INDEX;
287 case GLSL_SAMPLER_DIM_EXTERNAL:
288 return TEXTURE_EXTERNAL_INDEX;
289 case GLSL_SAMPLER_DIM_MS:
290 return (t->sampler_array) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX : TEXTURE_2D_MULTISAMPLE_INDEX;
291 default:
292 assert(!"Should not get here.");
293 return TEXTURE_BUFFER_INDEX;
294 }
295 }
296
297 bool
298 glsl_type::contains_image() const
299 {
300 if (this->is_array()) {
301 return this->fields.array->contains_image();
302 } else if (this->is_record()) {
303 for (unsigned int i = 0; i < this->length; i++) {
304 if (this->fields.structure[i].type->contains_image())
305 return true;
306 }
307 return false;
308 } else {
309 return this->is_image();
310 }
311 }
312
313 const glsl_type *glsl_type::get_base_type() const
314 {
315 switch (base_type) {
316 case GLSL_TYPE_UINT:
317 return uint_type;
318 case GLSL_TYPE_INT:
319 return int_type;
320 case GLSL_TYPE_FLOAT:
321 return float_type;
322 case GLSL_TYPE_DOUBLE:
323 return double_type;
324 case GLSL_TYPE_BOOL:
325 return bool_type;
326 default:
327 return error_type;
328 }
329 }
330
331
332 const glsl_type *glsl_type::get_scalar_type() const
333 {
334 const glsl_type *type = this;
335
336 /* Handle arrays */
337 while (type->base_type == GLSL_TYPE_ARRAY)
338 type = type->fields.array;
339
340 /* Handle vectors and matrices */
341 switch (type->base_type) {
342 case GLSL_TYPE_UINT:
343 return uint_type;
344 case GLSL_TYPE_INT:
345 return int_type;
346 case GLSL_TYPE_FLOAT:
347 return float_type;
348 case GLSL_TYPE_DOUBLE:
349 return double_type;
350 case GLSL_TYPE_BOOL:
351 return bool_type;
352 default:
353 /* Handle everything else */
354 return type;
355 }
356 }
357
358
359 void
360 _mesa_glsl_release_types(void)
361 {
362 /* Should only be called during atexit (either when unloading shared
363 * object, or if process terminates), so no mutex-locking should be
364 * necessary.
365 */
366 if (glsl_type::array_types != NULL) {
367 _mesa_hash_table_destroy(glsl_type::array_types, NULL);
368 glsl_type::array_types = NULL;
369 }
370
371 if (glsl_type::record_types != NULL) {
372 _mesa_hash_table_destroy(glsl_type::record_types, NULL);
373 glsl_type::record_types = NULL;
374 }
375
376 if (glsl_type::interface_types != NULL) {
377 _mesa_hash_table_destroy(glsl_type::interface_types, NULL);
378 glsl_type::interface_types = NULL;
379 }
380 }
381
382
383 glsl_type::glsl_type(const glsl_type *array, unsigned length) :
384 base_type(GLSL_TYPE_ARRAY),
385 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
386 sampler_type(0), interface_packing(0),
387 vector_elements(0), matrix_columns(0),
388 length(length), name(NULL)
389 {
390 this->fields.array = array;
391 /* Inherit the gl type of the base. The GL type is used for
392 * uniform/statevar handling in Mesa and the arrayness of the type
393 * is represented by the size rather than the type.
394 */
395 this->gl_type = array->gl_type;
396
397 /* Allow a maximum of 10 characters for the array size. This is enough
398 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
399 * NUL.
400 */
401 const unsigned name_length = strlen(array->name) + 10 + 3;
402
403 mtx_lock(&glsl_type::mutex);
404 char *const n = (char *) ralloc_size(this->mem_ctx, name_length);
405 mtx_unlock(&glsl_type::mutex);
406
407 if (length == 0)
408 snprintf(n, name_length, "%s[]", array->name);
409 else {
410 /* insert outermost dimensions in the correct spot
411 * otherwise the dimension order will be backwards
412 */
413 const char *pos = strchr(array->name, '[');
414 if (pos) {
415 int idx = pos - array->name;
416 snprintf(n, idx+1, "%s", array->name);
417 snprintf(n + idx, name_length - idx, "[%u]%s",
418 length, array->name + idx);
419 } else {
420 snprintf(n, name_length, "%s[%u]", array->name, length);
421 }
422 }
423
424 this->name = n;
425 }
426
427
428 const glsl_type *
429 glsl_type::vec(unsigned components)
430 {
431 if (components == 0 || components > 4)
432 return error_type;
433
434 static const glsl_type *const ts[] = {
435 float_type, vec2_type, vec3_type, vec4_type
436 };
437 return ts[components - 1];
438 }
439
440 const glsl_type *
441 glsl_type::dvec(unsigned components)
442 {
443 if (components == 0 || components > 4)
444 return error_type;
445
446 static const glsl_type *const ts[] = {
447 double_type, dvec2_type, dvec3_type, dvec4_type
448 };
449 return ts[components - 1];
450 }
451
452 const glsl_type *
453 glsl_type::ivec(unsigned components)
454 {
455 if (components == 0 || components > 4)
456 return error_type;
457
458 static const glsl_type *const ts[] = {
459 int_type, ivec2_type, ivec3_type, ivec4_type
460 };
461 return ts[components - 1];
462 }
463
464
465 const glsl_type *
466 glsl_type::uvec(unsigned components)
467 {
468 if (components == 0 || components > 4)
469 return error_type;
470
471 static const glsl_type *const ts[] = {
472 uint_type, uvec2_type, uvec3_type, uvec4_type
473 };
474 return ts[components - 1];
475 }
476
477
478 const glsl_type *
479 glsl_type::bvec(unsigned components)
480 {
481 if (components == 0 || components > 4)
482 return error_type;
483
484 static const glsl_type *const ts[] = {
485 bool_type, bvec2_type, bvec3_type, bvec4_type
486 };
487 return ts[components - 1];
488 }
489
490
491 const glsl_type *
492 glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
493 {
494 if (base_type == GLSL_TYPE_VOID)
495 return void_type;
496
497 if ((rows < 1) || (rows > 4) || (columns < 1) || (columns > 4))
498 return error_type;
499
500 /* Treat GLSL vectors as Nx1 matrices.
501 */
502 if (columns == 1) {
503 switch (base_type) {
504 case GLSL_TYPE_UINT:
505 return uvec(rows);
506 case GLSL_TYPE_INT:
507 return ivec(rows);
508 case GLSL_TYPE_FLOAT:
509 return vec(rows);
510 case GLSL_TYPE_DOUBLE:
511 return dvec(rows);
512 case GLSL_TYPE_BOOL:
513 return bvec(rows);
514 default:
515 return error_type;
516 }
517 } else {
518 if ((base_type != GLSL_TYPE_FLOAT && base_type != GLSL_TYPE_DOUBLE) || (rows == 1))
519 return error_type;
520
521 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
522 * combinations are valid:
523 *
524 * 1 2 3 4
525 * 1
526 * 2 x x x
527 * 3 x x x
528 * 4 x x x
529 */
530 #define IDX(c,r) (((c-1)*3) + (r-1))
531
532 if (base_type == GLSL_TYPE_DOUBLE) {
533 switch (IDX(columns, rows)) {
534 case IDX(2,2): return dmat2_type;
535 case IDX(2,3): return dmat2x3_type;
536 case IDX(2,4): return dmat2x4_type;
537 case IDX(3,2): return dmat3x2_type;
538 case IDX(3,3): return dmat3_type;
539 case IDX(3,4): return dmat3x4_type;
540 case IDX(4,2): return dmat4x2_type;
541 case IDX(4,3): return dmat4x3_type;
542 case IDX(4,4): return dmat4_type;
543 default: return error_type;
544 }
545 } else {
546 switch (IDX(columns, rows)) {
547 case IDX(2,2): return mat2_type;
548 case IDX(2,3): return mat2x3_type;
549 case IDX(2,4): return mat2x4_type;
550 case IDX(3,2): return mat3x2_type;
551 case IDX(3,3): return mat3_type;
552 case IDX(3,4): return mat3x4_type;
553 case IDX(4,2): return mat4x2_type;
554 case IDX(4,3): return mat4x3_type;
555 case IDX(4,4): return mat4_type;
556 default: return error_type;
557 }
558 }
559 }
560
561 assert(!"Should not get here.");
562 return error_type;
563 }
564
565 const glsl_type *
566 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim,
567 bool shadow,
568 bool array,
569 glsl_base_type type)
570 {
571 switch (type) {
572 case GLSL_TYPE_FLOAT:
573 switch (dim) {
574 case GLSL_SAMPLER_DIM_1D:
575 if (shadow)
576 return (array ? sampler1DArrayShadow_type : sampler1DShadow_type);
577 else
578 return (array ? sampler1DArray_type : sampler1D_type);
579 case GLSL_SAMPLER_DIM_2D:
580 if (shadow)
581 return (array ? sampler2DArrayShadow_type : sampler2DShadow_type);
582 else
583 return (array ? sampler2DArray_type : sampler2D_type);
584 case GLSL_SAMPLER_DIM_3D:
585 if (shadow || array)
586 return error_type;
587 else
588 return sampler3D_type;
589 case GLSL_SAMPLER_DIM_CUBE:
590 if (shadow)
591 return (array ? samplerCubeArrayShadow_type : samplerCubeShadow_type);
592 else
593 return (array ? samplerCubeArray_type : samplerCube_type);
594 case GLSL_SAMPLER_DIM_RECT:
595 if (array)
596 return error_type;
597 if (shadow)
598 return sampler2DRectShadow_type;
599 else
600 return sampler2DRect_type;
601 case GLSL_SAMPLER_DIM_BUF:
602 if (shadow || array)
603 return error_type;
604 else
605 return samplerBuffer_type;
606 case GLSL_SAMPLER_DIM_MS:
607 if (shadow)
608 return error_type;
609 return (array ? sampler2DMSArray_type : sampler2DMS_type);
610 case GLSL_SAMPLER_DIM_EXTERNAL:
611 if (shadow || array)
612 return error_type;
613 else
614 return samplerExternalOES_type;
615 }
616 case GLSL_TYPE_INT:
617 if (shadow)
618 return error_type;
619 switch (dim) {
620 case GLSL_SAMPLER_DIM_1D:
621 return (array ? isampler1DArray_type : isampler1D_type);
622 case GLSL_SAMPLER_DIM_2D:
623 return (array ? isampler2DArray_type : isampler2D_type);
624 case GLSL_SAMPLER_DIM_3D:
625 if (array)
626 return error_type;
627 return isampler3D_type;
628 case GLSL_SAMPLER_DIM_CUBE:
629 return (array ? isamplerCubeArray_type : isamplerCube_type);
630 case GLSL_SAMPLER_DIM_RECT:
631 if (array)
632 return error_type;
633 return isampler2DRect_type;
634 case GLSL_SAMPLER_DIM_BUF:
635 if (array)
636 return error_type;
637 return isamplerBuffer_type;
638 case GLSL_SAMPLER_DIM_MS:
639 return (array ? isampler2DMSArray_type : isampler2DMS_type);
640 case GLSL_SAMPLER_DIM_EXTERNAL:
641 return error_type;
642 }
643 case GLSL_TYPE_UINT:
644 if (shadow)
645 return error_type;
646 switch (dim) {
647 case GLSL_SAMPLER_DIM_1D:
648 return (array ? usampler1DArray_type : usampler1D_type);
649 case GLSL_SAMPLER_DIM_2D:
650 return (array ? usampler2DArray_type : usampler2D_type);
651 case GLSL_SAMPLER_DIM_3D:
652 if (array)
653 return error_type;
654 return usampler3D_type;
655 case GLSL_SAMPLER_DIM_CUBE:
656 return (array ? usamplerCubeArray_type : usamplerCube_type);
657 case GLSL_SAMPLER_DIM_RECT:
658 if (array)
659 return error_type;
660 return usampler2DRect_type;
661 case GLSL_SAMPLER_DIM_BUF:
662 if (array)
663 return error_type;
664 return usamplerBuffer_type;
665 case GLSL_SAMPLER_DIM_MS:
666 return (array ? usampler2DMSArray_type : usampler2DMS_type);
667 case GLSL_SAMPLER_DIM_EXTERNAL:
668 return error_type;
669 }
670 default:
671 return error_type;
672 }
673
674 unreachable("switch statement above should be complete");
675 }
676
677 const glsl_type *
678 glsl_type::get_array_instance(const glsl_type *base, unsigned array_size)
679 {
680 /* Generate a name using the base type pointer in the key. This is
681 * done because the name of the base type may not be unique across
682 * shaders. For example, two shaders may have different record types
683 * named 'foo'.
684 */
685 char key[128];
686 snprintf(key, sizeof(key), "%p[%u]", (void *) base, array_size);
687
688 mtx_lock(&glsl_type::mutex);
689
690 if (array_types == NULL) {
691 array_types = _mesa_hash_table_create(NULL, _mesa_key_hash_string,
692 _mesa_key_string_equal);
693 }
694
695 const struct hash_entry *entry = _mesa_hash_table_search(array_types, key);
696 if (entry == NULL) {
697 mtx_unlock(&glsl_type::mutex);
698 const glsl_type *t = new glsl_type(base, array_size);
699 mtx_lock(&glsl_type::mutex);
700
701 entry = _mesa_hash_table_insert(array_types,
702 ralloc_strdup(mem_ctx, key),
703 (void *) t);
704 }
705
706 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_ARRAY);
707 assert(((glsl_type *) entry->data)->length == array_size);
708 assert(((glsl_type *) entry->data)->fields.array == base);
709
710 mtx_unlock(&glsl_type::mutex);
711
712 return (glsl_type *) entry->data;
713 }
714
715
716 bool
717 glsl_type::record_compare(const glsl_type *b) const
718 {
719 if (this->length != b->length)
720 return false;
721
722 if (this->interface_packing != b->interface_packing)
723 return false;
724
725 /* From the GLSL 4.20 specification (Sec 4.2):
726 *
727 * "Structures must have the same name, sequence of type names, and
728 * type definitions, and field names to be considered the same type."
729 *
730 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
731 *
732 * Note that we cannot force type name check when comparing unnamed
733 * structure types, these have a unique name assigned during parsing.
734 */
735 if (!this->is_anonymous() && !b->is_anonymous())
736 if (strcmp(this->name, b->name) != 0)
737 return false;
738
739 for (unsigned i = 0; i < this->length; i++) {
740 if (this->fields.structure[i].type != b->fields.structure[i].type)
741 return false;
742 if (strcmp(this->fields.structure[i].name,
743 b->fields.structure[i].name) != 0)
744 return false;
745 if (this->fields.structure[i].matrix_layout
746 != b->fields.structure[i].matrix_layout)
747 return false;
748 if (this->fields.structure[i].location
749 != b->fields.structure[i].location)
750 return false;
751 if (this->fields.structure[i].interpolation
752 != b->fields.structure[i].interpolation)
753 return false;
754 if (this->fields.structure[i].centroid
755 != b->fields.structure[i].centroid)
756 return false;
757 if (this->fields.structure[i].sample
758 != b->fields.structure[i].sample)
759 return false;
760 if (this->fields.structure[i].patch
761 != b->fields.structure[i].patch)
762 return false;
763 }
764
765 return true;
766 }
767
768
769 bool
770 glsl_type::record_key_compare(const void *a, const void *b)
771 {
772 const glsl_type *const key1 = (glsl_type *) a;
773 const glsl_type *const key2 = (glsl_type *) b;
774
775 return strcmp(key1->name, key2->name) == 0 && key1->record_compare(key2);
776 }
777
778
779 /**
780 * Generate an integer hash value for a glsl_type structure type.
781 */
782 unsigned
783 glsl_type::record_key_hash(const void *a)
784 {
785 const glsl_type *const key = (glsl_type *) a;
786 uintptr_t hash = key->length;
787 unsigned retval;
788
789 for (unsigned i = 0; i < key->length; i++) {
790 /* casting pointer to uintptr_t */
791 hash = (hash * 13 ) + (uintptr_t) key->fields.structure[i].type;
792 }
793
794 if (sizeof(hash) == 8)
795 retval = (hash & 0xffffffff) ^ ((uint64_t) hash >> 32);
796 else
797 retval = hash;
798
799 return retval;
800 }
801
802
803 const glsl_type *
804 glsl_type::get_record_instance(const glsl_struct_field *fields,
805 unsigned num_fields,
806 const char *name)
807 {
808 const glsl_type key(fields, num_fields, name);
809
810 mtx_lock(&glsl_type::mutex);
811
812 if (record_types == NULL) {
813 record_types = _mesa_hash_table_create(NULL, record_key_hash,
814 record_key_compare);
815 }
816
817 const struct hash_entry *entry = _mesa_hash_table_search(record_types,
818 &key);
819 if (entry == NULL) {
820 mtx_unlock(&glsl_type::mutex);
821 const glsl_type *t = new glsl_type(fields, num_fields, name);
822 mtx_lock(&glsl_type::mutex);
823
824 entry = _mesa_hash_table_insert(record_types, t, (void *) t);
825 }
826
827 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_STRUCT);
828 assert(((glsl_type *) entry->data)->length == num_fields);
829 assert(strcmp(((glsl_type *) entry->data)->name, name) == 0);
830
831 mtx_unlock(&glsl_type::mutex);
832
833 return (glsl_type *) entry->data;
834 }
835
836
837 const glsl_type *
838 glsl_type::get_interface_instance(const glsl_struct_field *fields,
839 unsigned num_fields,
840 enum glsl_interface_packing packing,
841 const char *block_name)
842 {
843 const glsl_type key(fields, num_fields, packing, block_name);
844
845 mtx_lock(&glsl_type::mutex);
846
847 if (interface_types == NULL) {
848 interface_types = _mesa_hash_table_create(NULL, record_key_hash,
849 record_key_compare);
850 }
851
852 const struct hash_entry *entry = _mesa_hash_table_search(interface_types,
853 &key);
854 if (entry == NULL) {
855 mtx_unlock(&glsl_type::mutex);
856 const glsl_type *t = new glsl_type(fields, num_fields,
857 packing, block_name);
858 mtx_lock(&glsl_type::mutex);
859
860 entry = _mesa_hash_table_insert(interface_types, t, (void *) t);
861 }
862
863 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_INTERFACE);
864 assert(((glsl_type *) entry->data)->length == num_fields);
865 assert(strcmp(((glsl_type *) entry->data)->name, block_name) == 0);
866
867 mtx_unlock(&glsl_type::mutex);
868
869 return (glsl_type *) entry->data;
870 }
871
872 const glsl_type *
873 glsl_type::get_subroutine_instance(const char *subroutine_name)
874 {
875 const glsl_type key(subroutine_name);
876
877 mtx_lock(&glsl_type::mutex);
878
879 if (subroutine_types == NULL) {
880 subroutine_types = _mesa_hash_table_create(NULL, record_key_hash,
881 record_key_compare);
882 }
883
884 const struct hash_entry *entry = _mesa_hash_table_search(subroutine_types,
885 &key);
886 if (entry == NULL) {
887 mtx_unlock(&glsl_type::mutex);
888 const glsl_type *t = new glsl_type(subroutine_name);
889 mtx_lock(&glsl_type::mutex);
890
891 entry = _mesa_hash_table_insert(subroutine_types, t, (void *) t);
892 }
893
894 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_SUBROUTINE);
895 assert(strcmp(((glsl_type *) entry->data)->name, subroutine_name) == 0);
896
897 mtx_unlock(&glsl_type::mutex);
898
899 return (glsl_type *) entry->data;
900 }
901
902
903 const glsl_type *
904 glsl_type::get_mul_type(const glsl_type *type_a, const glsl_type *type_b)
905 {
906 if (type_a == type_b) {
907 return type_a;
908 } else if (type_a->is_matrix() && type_b->is_matrix()) {
909 /* Matrix multiply. The columns of A must match the rows of B. Given
910 * the other previously tested constraints, this means the vector type
911 * of a row from A must be the same as the vector type of a column from
912 * B.
913 */
914 if (type_a->row_type() == type_b->column_type()) {
915 /* The resulting matrix has the number of columns of matrix B and
916 * the number of rows of matrix A. We get the row count of A by
917 * looking at the size of a vector that makes up a column. The
918 * transpose (size of a row) is done for B.
919 */
920 const glsl_type *const type =
921 get_instance(type_a->base_type,
922 type_a->column_type()->vector_elements,
923 type_b->row_type()->vector_elements);
924 assert(type != error_type);
925
926 return type;
927 }
928 } else if (type_a->is_matrix()) {
929 /* A is a matrix and B is a column vector. Columns of A must match
930 * rows of B. Given the other previously tested constraints, this
931 * means the vector type of a row from A must be the same as the
932 * vector the type of B.
933 */
934 if (type_a->row_type() == type_b) {
935 /* The resulting vector has a number of elements equal to
936 * the number of rows of matrix A. */
937 const glsl_type *const type =
938 get_instance(type_a->base_type,
939 type_a->column_type()->vector_elements,
940 1);
941 assert(type != error_type);
942
943 return type;
944 }
945 } else {
946 assert(type_b->is_matrix());
947
948 /* A is a row vector and B is a matrix. Columns of A must match rows
949 * of B. Given the other previously tested constraints, this means
950 * the type of A must be the same as the vector type of a column from
951 * B.
952 */
953 if (type_a == type_b->column_type()) {
954 /* The resulting vector has a number of elements equal to
955 * the number of columns of matrix B. */
956 const glsl_type *const type =
957 get_instance(type_a->base_type,
958 type_b->row_type()->vector_elements,
959 1);
960 assert(type != error_type);
961
962 return type;
963 }
964 }
965
966 return error_type;
967 }
968
969
970 const glsl_type *
971 glsl_type::field_type(const char *name) const
972 {
973 if (this->base_type != GLSL_TYPE_STRUCT
974 && this->base_type != GLSL_TYPE_INTERFACE)
975 return error_type;
976
977 for (unsigned i = 0; i < this->length; i++) {
978 if (strcmp(name, this->fields.structure[i].name) == 0)
979 return this->fields.structure[i].type;
980 }
981
982 return error_type;
983 }
984
985
986 int
987 glsl_type::field_index(const char *name) const
988 {
989 if (this->base_type != GLSL_TYPE_STRUCT
990 && this->base_type != GLSL_TYPE_INTERFACE)
991 return -1;
992
993 for (unsigned i = 0; i < this->length; i++) {
994 if (strcmp(name, this->fields.structure[i].name) == 0)
995 return i;
996 }
997
998 return -1;
999 }
1000
1001
1002 unsigned
1003 glsl_type::component_slots() const
1004 {
1005 switch (this->base_type) {
1006 case GLSL_TYPE_UINT:
1007 case GLSL_TYPE_INT:
1008 case GLSL_TYPE_FLOAT:
1009 case GLSL_TYPE_BOOL:
1010 return this->components();
1011
1012 case GLSL_TYPE_DOUBLE:
1013 return 2 * this->components();
1014
1015 case GLSL_TYPE_STRUCT:
1016 case GLSL_TYPE_INTERFACE: {
1017 unsigned size = 0;
1018
1019 for (unsigned i = 0; i < this->length; i++)
1020 size += this->fields.structure[i].type->component_slots();
1021
1022 return size;
1023 }
1024
1025 case GLSL_TYPE_ARRAY:
1026 return this->length * this->fields.array->component_slots();
1027
1028 case GLSL_TYPE_IMAGE:
1029 return 1;
1030 case GLSL_TYPE_SUBROUTINE:
1031 return 1;
1032 case GLSL_TYPE_SAMPLER:
1033 case GLSL_TYPE_ATOMIC_UINT:
1034 case GLSL_TYPE_VOID:
1035 case GLSL_TYPE_ERROR:
1036 break;
1037 }
1038
1039 return 0;
1040 }
1041
1042 unsigned
1043 glsl_type::record_location_offset(unsigned length) const
1044 {
1045 unsigned offset = 0;
1046 const glsl_type *t = this->without_array();
1047 if (t->is_record()) {
1048 assert(length <= t->length);
1049
1050 for (unsigned i = 0; i < length; i++) {
1051 const glsl_type *st = t->fields.structure[i].type;
1052 const glsl_type *wa = st->without_array();
1053 if (wa->is_record()) {
1054 unsigned r_offset = wa->record_location_offset(wa->length);
1055 offset += st->is_array() ? st->length * r_offset : r_offset;
1056 } else {
1057 /* We dont worry about arrays here because unless the array
1058 * contains a structure or another array it only takes up a single
1059 * uniform slot.
1060 */
1061 offset += 1;
1062 }
1063 }
1064 }
1065 return offset;
1066 }
1067
1068 unsigned
1069 glsl_type::uniform_locations() const
1070 {
1071 unsigned size = 0;
1072
1073 switch (this->base_type) {
1074 case GLSL_TYPE_UINT:
1075 case GLSL_TYPE_INT:
1076 case GLSL_TYPE_FLOAT:
1077 case GLSL_TYPE_DOUBLE:
1078 case GLSL_TYPE_BOOL:
1079 case GLSL_TYPE_SAMPLER:
1080 case GLSL_TYPE_IMAGE:
1081 case GLSL_TYPE_SUBROUTINE:
1082 return 1;
1083
1084 case GLSL_TYPE_STRUCT:
1085 case GLSL_TYPE_INTERFACE:
1086 for (unsigned i = 0; i < this->length; i++)
1087 size += this->fields.structure[i].type->uniform_locations();
1088 return size;
1089 case GLSL_TYPE_ARRAY:
1090 return this->length * this->fields.array->uniform_locations();
1091 default:
1092 return 0;
1093 }
1094 }
1095
1096 bool
1097 glsl_type::can_implicitly_convert_to(const glsl_type *desired,
1098 _mesa_glsl_parse_state *state) const
1099 {
1100 if (this == desired)
1101 return true;
1102
1103 /* There is no conversion among matrix types. */
1104 if (this->matrix_columns > 1 || desired->matrix_columns > 1)
1105 return false;
1106
1107 /* Vector size must match. */
1108 if (this->vector_elements != desired->vector_elements)
1109 return false;
1110
1111 /* int and uint can be converted to float. */
1112 if (desired->is_float() && this->is_integer())
1113 return true;
1114
1115 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
1116 * Note that state may be NULL here, when resolving function calls in the
1117 * linker. By this time, all the state-dependent checks have already
1118 * happened though, so allow anything that's allowed in any shader version. */
1119 if ((!state || state->is_version(400, 0) || state->ARB_gpu_shader5_enable) &&
1120 desired->base_type == GLSL_TYPE_UINT && this->base_type == GLSL_TYPE_INT)
1121 return true;
1122
1123 /* No implicit conversions from double. */
1124 if ((!state || state->has_double()) && this->is_double())
1125 return false;
1126
1127 /* Conversions from different types to double. */
1128 if ((!state || state->has_double()) && desired->is_double()) {
1129 if (this->is_float())
1130 return true;
1131 if (this->is_integer())
1132 return true;
1133 }
1134
1135 return false;
1136 }
1137
1138 unsigned
1139 glsl_type::std140_base_alignment(bool row_major) const
1140 {
1141 unsigned N = is_double() ? 8 : 4;
1142
1143 /* (1) If the member is a scalar consuming <N> basic machine units, the
1144 * base alignment is <N>.
1145 *
1146 * (2) If the member is a two- or four-component vector with components
1147 * consuming <N> basic machine units, the base alignment is 2<N> or
1148 * 4<N>, respectively.
1149 *
1150 * (3) If the member is a three-component vector with components consuming
1151 * <N> basic machine units, the base alignment is 4<N>.
1152 */
1153 if (this->is_scalar() || this->is_vector()) {
1154 switch (this->vector_elements) {
1155 case 1:
1156 return N;
1157 case 2:
1158 return 2 * N;
1159 case 3:
1160 case 4:
1161 return 4 * N;
1162 }
1163 }
1164
1165 /* (4) If the member is an array of scalars or vectors, the base alignment
1166 * and array stride are set to match the base alignment of a single
1167 * array element, according to rules (1), (2), and (3), and rounded up
1168 * to the base alignment of a vec4. The array may have padding at the
1169 * end; the base offset of the member following the array is rounded up
1170 * to the next multiple of the base alignment.
1171 *
1172 * (6) If the member is an array of <S> column-major matrices with <C>
1173 * columns and <R> rows, the matrix is stored identically to a row of
1174 * <S>*<C> column vectors with <R> components each, according to rule
1175 * (4).
1176 *
1177 * (8) If the member is an array of <S> row-major matrices with <C> columns
1178 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1179 * row vectors with <C> components each, according to rule (4).
1180 *
1181 * (10) If the member is an array of <S> structures, the <S> elements of
1182 * the array are laid out in order, according to rule (9).
1183 */
1184 if (this->is_array()) {
1185 if (this->fields.array->is_scalar() ||
1186 this->fields.array->is_vector() ||
1187 this->fields.array->is_matrix()) {
1188 return MAX2(this->fields.array->std140_base_alignment(row_major), 16);
1189 } else {
1190 assert(this->fields.array->is_record() ||
1191 this->fields.array->is_array());
1192 return this->fields.array->std140_base_alignment(row_major);
1193 }
1194 }
1195
1196 /* (5) If the member is a column-major matrix with <C> columns and
1197 * <R> rows, the matrix is stored identically to an array of
1198 * <C> column vectors with <R> components each, according to
1199 * rule (4).
1200 *
1201 * (7) If the member is a row-major matrix with <C> columns and <R>
1202 * rows, the matrix is stored identically to an array of <R>
1203 * row vectors with <C> components each, according to rule (4).
1204 */
1205 if (this->is_matrix()) {
1206 const struct glsl_type *vec_type, *array_type;
1207 int c = this->matrix_columns;
1208 int r = this->vector_elements;
1209
1210 if (row_major) {
1211 vec_type = get_instance(base_type, c, 1);
1212 array_type = glsl_type::get_array_instance(vec_type, r);
1213 } else {
1214 vec_type = get_instance(base_type, r, 1);
1215 array_type = glsl_type::get_array_instance(vec_type, c);
1216 }
1217
1218 return array_type->std140_base_alignment(false);
1219 }
1220
1221 /* (9) If the member is a structure, the base alignment of the
1222 * structure is <N>, where <N> is the largest base alignment
1223 * value of any of its members, and rounded up to the base
1224 * alignment of a vec4. The individual members of this
1225 * sub-structure are then assigned offsets by applying this set
1226 * of rules recursively, where the base offset of the first
1227 * member of the sub-structure is equal to the aligned offset
1228 * of the structure. The structure may have padding at the end;
1229 * the base offset of the member following the sub-structure is
1230 * rounded up to the next multiple of the base alignment of the
1231 * structure.
1232 */
1233 if (this->is_record()) {
1234 unsigned base_alignment = 16;
1235 for (unsigned i = 0; i < this->length; i++) {
1236 bool field_row_major = row_major;
1237 const enum glsl_matrix_layout matrix_layout =
1238 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1239 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1240 field_row_major = true;
1241 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1242 field_row_major = false;
1243 }
1244
1245 const struct glsl_type *field_type = this->fields.structure[i].type;
1246 base_alignment = MAX2(base_alignment,
1247 field_type->std140_base_alignment(field_row_major));
1248 }
1249 return base_alignment;
1250 }
1251
1252 assert(!"not reached");
1253 return -1;
1254 }
1255
1256 unsigned
1257 glsl_type::std140_size(bool row_major) const
1258 {
1259 unsigned N = is_double() ? 8 : 4;
1260
1261 /* (1) If the member is a scalar consuming <N> basic machine units, the
1262 * base alignment is <N>.
1263 *
1264 * (2) If the member is a two- or four-component vector with components
1265 * consuming <N> basic machine units, the base alignment is 2<N> or
1266 * 4<N>, respectively.
1267 *
1268 * (3) If the member is a three-component vector with components consuming
1269 * <N> basic machine units, the base alignment is 4<N>.
1270 */
1271 if (this->is_scalar() || this->is_vector()) {
1272 return this->vector_elements * N;
1273 }
1274
1275 /* (5) If the member is a column-major matrix with <C> columns and
1276 * <R> rows, the matrix is stored identically to an array of
1277 * <C> column vectors with <R> components each, according to
1278 * rule (4).
1279 *
1280 * (6) If the member is an array of <S> column-major matrices with <C>
1281 * columns and <R> rows, the matrix is stored identically to a row of
1282 * <S>*<C> column vectors with <R> components each, according to rule
1283 * (4).
1284 *
1285 * (7) If the member is a row-major matrix with <C> columns and <R>
1286 * rows, the matrix is stored identically to an array of <R>
1287 * row vectors with <C> components each, according to rule (4).
1288 *
1289 * (8) If the member is an array of <S> row-major matrices with <C> columns
1290 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1291 * row vectors with <C> components each, according to rule (4).
1292 */
1293 if (this->without_array()->is_matrix()) {
1294 const struct glsl_type *element_type;
1295 const struct glsl_type *vec_type;
1296 unsigned int array_len;
1297
1298 if (this->is_array()) {
1299 element_type = this->fields.array;
1300 array_len = this->length;
1301 } else {
1302 element_type = this;
1303 array_len = 1;
1304 }
1305
1306 if (row_major) {
1307 vec_type = get_instance(element_type->base_type,
1308 element_type->matrix_columns, 1);
1309
1310 array_len *= element_type->vector_elements;
1311 } else {
1312 vec_type = get_instance(element_type->base_type,
1313 element_type->vector_elements, 1);
1314 array_len *= element_type->matrix_columns;
1315 }
1316 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
1317 array_len);
1318
1319 return array_type->std140_size(false);
1320 }
1321
1322 /* (4) If the member is an array of scalars or vectors, the base alignment
1323 * and array stride are set to match the base alignment of a single
1324 * array element, according to rules (1), (2), and (3), and rounded up
1325 * to the base alignment of a vec4. The array may have padding at the
1326 * end; the base offset of the member following the array is rounded up
1327 * to the next multiple of the base alignment.
1328 *
1329 * (10) If the member is an array of <S> structures, the <S> elements of
1330 * the array are laid out in order, according to rule (9).
1331 */
1332 if (this->is_array()) {
1333 if (this->fields.array->is_record()) {
1334 return this->length * this->fields.array->std140_size(row_major);
1335 } else {
1336 unsigned element_base_align =
1337 this->fields.array->std140_base_alignment(row_major);
1338 return this->length * MAX2(element_base_align, 16);
1339 }
1340 }
1341
1342 /* (9) If the member is a structure, the base alignment of the
1343 * structure is <N>, where <N> is the largest base alignment
1344 * value of any of its members, and rounded up to the base
1345 * alignment of a vec4. The individual members of this
1346 * sub-structure are then assigned offsets by applying this set
1347 * of rules recursively, where the base offset of the first
1348 * member of the sub-structure is equal to the aligned offset
1349 * of the structure. The structure may have padding at the end;
1350 * the base offset of the member following the sub-structure is
1351 * rounded up to the next multiple of the base alignment of the
1352 * structure.
1353 */
1354 if (this->is_record()) {
1355 unsigned size = 0;
1356 unsigned max_align = 0;
1357
1358 for (unsigned i = 0; i < this->length; i++) {
1359 bool field_row_major = row_major;
1360 const enum glsl_matrix_layout matrix_layout =
1361 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1362 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1363 field_row_major = true;
1364 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1365 field_row_major = false;
1366 }
1367
1368 const struct glsl_type *field_type = this->fields.structure[i].type;
1369 unsigned align = field_type->std140_base_alignment(field_row_major);
1370 size = glsl_align(size, align);
1371 size += field_type->std140_size(field_row_major);
1372
1373 max_align = MAX2(align, max_align);
1374
1375 if (field_type->is_record() && (i + 1 < this->length))
1376 size = glsl_align(size, 16);
1377 }
1378 size = glsl_align(size, MAX2(max_align, 16));
1379 return size;
1380 }
1381
1382 assert(!"not reached");
1383 return -1;
1384 }
1385
1386
1387 unsigned
1388 glsl_type::count_attribute_slots() const
1389 {
1390 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1391 *
1392 * "A scalar input counts the same amount against this limit as a vec4,
1393 * so applications may want to consider packing groups of four
1394 * unrelated float inputs together into a vector to better utilize the
1395 * capabilities of the underlying hardware. A matrix input will use up
1396 * multiple locations. The number of locations used will equal the
1397 * number of columns in the matrix."
1398 *
1399 * The spec does not explicitly say how arrays are counted. However, it
1400 * should be safe to assume the total number of slots consumed by an array
1401 * is the number of entries in the array multiplied by the number of slots
1402 * consumed by a single element of the array.
1403 *
1404 * The spec says nothing about how structs are counted, because vertex
1405 * attributes are not allowed to be (or contain) structs. However, Mesa
1406 * allows varying structs, the number of varying slots taken up by a
1407 * varying struct is simply equal to the sum of the number of slots taken
1408 * up by each element.
1409 */
1410 switch (this->base_type) {
1411 case GLSL_TYPE_UINT:
1412 case GLSL_TYPE_INT:
1413 case GLSL_TYPE_FLOAT:
1414 case GLSL_TYPE_BOOL:
1415 case GLSL_TYPE_DOUBLE:
1416 return this->matrix_columns;
1417
1418 case GLSL_TYPE_STRUCT:
1419 case GLSL_TYPE_INTERFACE: {
1420 unsigned size = 0;
1421
1422 for (unsigned i = 0; i < this->length; i++)
1423 size += this->fields.structure[i].type->count_attribute_slots();
1424
1425 return size;
1426 }
1427
1428 case GLSL_TYPE_ARRAY:
1429 return this->length * this->fields.array->count_attribute_slots();
1430
1431 case GLSL_TYPE_SAMPLER:
1432 case GLSL_TYPE_IMAGE:
1433 case GLSL_TYPE_ATOMIC_UINT:
1434 case GLSL_TYPE_VOID:
1435 case GLSL_TYPE_SUBROUTINE:
1436 case GLSL_TYPE_ERROR:
1437 break;
1438 }
1439
1440 assert(!"Unexpected type in count_attribute_slots()");
1441
1442 return 0;
1443 }
1444
1445 int
1446 glsl_type::coordinate_components() const
1447 {
1448 int size;
1449
1450 switch (sampler_dimensionality) {
1451 case GLSL_SAMPLER_DIM_1D:
1452 case GLSL_SAMPLER_DIM_BUF:
1453 size = 1;
1454 break;
1455 case GLSL_SAMPLER_DIM_2D:
1456 case GLSL_SAMPLER_DIM_RECT:
1457 case GLSL_SAMPLER_DIM_MS:
1458 case GLSL_SAMPLER_DIM_EXTERNAL:
1459 size = 2;
1460 break;
1461 case GLSL_SAMPLER_DIM_3D:
1462 case GLSL_SAMPLER_DIM_CUBE:
1463 size = 3;
1464 break;
1465 default:
1466 assert(!"Should not get here.");
1467 size = 1;
1468 break;
1469 }
1470
1471 /* Array textures need an additional component for the array index, except
1472 * for cubemap array images that behave like a 2D array of interleaved
1473 * cubemap faces.
1474 */
1475 if (sampler_array &&
1476 !(base_type == GLSL_TYPE_IMAGE &&
1477 sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE))
1478 size += 1;
1479
1480 return size;
1481 }