755618ac28b10b51f4a727a8f22503200cedcbec
[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::uniform_locations() const
1044 {
1045 unsigned size = 0;
1046
1047 switch (this->base_type) {
1048 case GLSL_TYPE_UINT:
1049 case GLSL_TYPE_INT:
1050 case GLSL_TYPE_FLOAT:
1051 case GLSL_TYPE_DOUBLE:
1052 case GLSL_TYPE_BOOL:
1053 case GLSL_TYPE_SAMPLER:
1054 case GLSL_TYPE_IMAGE:
1055 case GLSL_TYPE_SUBROUTINE:
1056 return 1;
1057
1058 case GLSL_TYPE_STRUCT:
1059 case GLSL_TYPE_INTERFACE:
1060 for (unsigned i = 0; i < this->length; i++)
1061 size += this->fields.structure[i].type->uniform_locations();
1062 return size;
1063 case GLSL_TYPE_ARRAY:
1064 return this->length * this->fields.array->uniform_locations();
1065 default:
1066 return 0;
1067 }
1068 }
1069
1070 bool
1071 glsl_type::can_implicitly_convert_to(const glsl_type *desired,
1072 _mesa_glsl_parse_state *state) const
1073 {
1074 if (this == desired)
1075 return true;
1076
1077 /* There is no conversion among matrix types. */
1078 if (this->matrix_columns > 1 || desired->matrix_columns > 1)
1079 return false;
1080
1081 /* Vector size must match. */
1082 if (this->vector_elements != desired->vector_elements)
1083 return false;
1084
1085 /* int and uint can be converted to float. */
1086 if (desired->is_float() && this->is_integer())
1087 return true;
1088
1089 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
1090 * Note that state may be NULL here, when resolving function calls in the
1091 * linker. By this time, all the state-dependent checks have already
1092 * happened though, so allow anything that's allowed in any shader version. */
1093 if ((!state || state->is_version(400, 0) || state->ARB_gpu_shader5_enable) &&
1094 desired->base_type == GLSL_TYPE_UINT && this->base_type == GLSL_TYPE_INT)
1095 return true;
1096
1097 /* No implicit conversions from double. */
1098 if ((!state || state->has_double()) && this->is_double())
1099 return false;
1100
1101 /* Conversions from different types to double. */
1102 if ((!state || state->has_double()) && desired->is_double()) {
1103 if (this->is_float())
1104 return true;
1105 if (this->is_integer())
1106 return true;
1107 }
1108
1109 return false;
1110 }
1111
1112 unsigned
1113 glsl_type::std140_base_alignment(bool row_major) const
1114 {
1115 unsigned N = is_double() ? 8 : 4;
1116
1117 /* (1) If the member is a scalar consuming <N> basic machine units, the
1118 * base alignment is <N>.
1119 *
1120 * (2) If the member is a two- or four-component vector with components
1121 * consuming <N> basic machine units, the base alignment is 2<N> or
1122 * 4<N>, respectively.
1123 *
1124 * (3) If the member is a three-component vector with components consuming
1125 * <N> basic machine units, the base alignment is 4<N>.
1126 */
1127 if (this->is_scalar() || this->is_vector()) {
1128 switch (this->vector_elements) {
1129 case 1:
1130 return N;
1131 case 2:
1132 return 2 * N;
1133 case 3:
1134 case 4:
1135 return 4 * N;
1136 }
1137 }
1138
1139 /* (4) If the member is an array of scalars or vectors, the base alignment
1140 * and array stride are set to match the base alignment of a single
1141 * array element, according to rules (1), (2), and (3), and rounded up
1142 * to the base alignment of a vec4. The array may have padding at the
1143 * end; the base offset of the member following the array is rounded up
1144 * to the next multiple of the base alignment.
1145 *
1146 * (6) If the member is an array of <S> column-major matrices with <C>
1147 * columns and <R> rows, the matrix is stored identically to a row of
1148 * <S>*<C> column vectors with <R> components each, according to rule
1149 * (4).
1150 *
1151 * (8) If the member is an array of <S> row-major matrices with <C> columns
1152 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1153 * row vectors with <C> components each, according to rule (4).
1154 *
1155 * (10) If the member is an array of <S> structures, the <S> elements of
1156 * the array are laid out in order, according to rule (9).
1157 */
1158 if (this->is_array()) {
1159 if (this->fields.array->is_scalar() ||
1160 this->fields.array->is_vector() ||
1161 this->fields.array->is_matrix()) {
1162 return MAX2(this->fields.array->std140_base_alignment(row_major), 16);
1163 } else {
1164 assert(this->fields.array->is_record() ||
1165 this->fields.array->is_array());
1166 return this->fields.array->std140_base_alignment(row_major);
1167 }
1168 }
1169
1170 /* (5) If the member is a column-major matrix with <C> columns and
1171 * <R> rows, the matrix is stored identically to an array of
1172 * <C> column vectors with <R> components each, according to
1173 * rule (4).
1174 *
1175 * (7) If the member is a row-major matrix with <C> columns and <R>
1176 * rows, the matrix is stored identically to an array of <R>
1177 * row vectors with <C> components each, according to rule (4).
1178 */
1179 if (this->is_matrix()) {
1180 const struct glsl_type *vec_type, *array_type;
1181 int c = this->matrix_columns;
1182 int r = this->vector_elements;
1183
1184 if (row_major) {
1185 vec_type = get_instance(base_type, c, 1);
1186 array_type = glsl_type::get_array_instance(vec_type, r);
1187 } else {
1188 vec_type = get_instance(base_type, r, 1);
1189 array_type = glsl_type::get_array_instance(vec_type, c);
1190 }
1191
1192 return array_type->std140_base_alignment(false);
1193 }
1194
1195 /* (9) If the member is a structure, the base alignment of the
1196 * structure is <N>, where <N> is the largest base alignment
1197 * value of any of its members, and rounded up to the base
1198 * alignment of a vec4. The individual members of this
1199 * sub-structure are then assigned offsets by applying this set
1200 * of rules recursively, where the base offset of the first
1201 * member of the sub-structure is equal to the aligned offset
1202 * of the structure. The structure may have padding at the end;
1203 * the base offset of the member following the sub-structure is
1204 * rounded up to the next multiple of the base alignment of the
1205 * structure.
1206 */
1207 if (this->is_record()) {
1208 unsigned base_alignment = 16;
1209 for (unsigned i = 0; i < this->length; i++) {
1210 bool field_row_major = row_major;
1211 const enum glsl_matrix_layout matrix_layout =
1212 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1213 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1214 field_row_major = true;
1215 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1216 field_row_major = false;
1217 }
1218
1219 const struct glsl_type *field_type = this->fields.structure[i].type;
1220 base_alignment = MAX2(base_alignment,
1221 field_type->std140_base_alignment(field_row_major));
1222 }
1223 return base_alignment;
1224 }
1225
1226 assert(!"not reached");
1227 return -1;
1228 }
1229
1230 unsigned
1231 glsl_type::std140_size(bool row_major) const
1232 {
1233 unsigned N = is_double() ? 8 : 4;
1234
1235 /* (1) If the member is a scalar consuming <N> basic machine units, the
1236 * base alignment is <N>.
1237 *
1238 * (2) If the member is a two- or four-component vector with components
1239 * consuming <N> basic machine units, the base alignment is 2<N> or
1240 * 4<N>, respectively.
1241 *
1242 * (3) If the member is a three-component vector with components consuming
1243 * <N> basic machine units, the base alignment is 4<N>.
1244 */
1245 if (this->is_scalar() || this->is_vector()) {
1246 return this->vector_elements * N;
1247 }
1248
1249 /* (5) If the member is a column-major matrix with <C> columns and
1250 * <R> rows, the matrix is stored identically to an array of
1251 * <C> column vectors with <R> components each, according to
1252 * rule (4).
1253 *
1254 * (6) If the member is an array of <S> column-major matrices with <C>
1255 * columns and <R> rows, the matrix is stored identically to a row of
1256 * <S>*<C> column vectors with <R> components each, according to rule
1257 * (4).
1258 *
1259 * (7) If the member is a row-major matrix with <C> columns and <R>
1260 * rows, the matrix is stored identically to an array of <R>
1261 * row vectors with <C> components each, according to rule (4).
1262 *
1263 * (8) If the member is an array of <S> row-major matrices with <C> columns
1264 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1265 * row vectors with <C> components each, according to rule (4).
1266 */
1267 if (this->without_array()->is_matrix()) {
1268 const struct glsl_type *element_type;
1269 const struct glsl_type *vec_type;
1270 unsigned int array_len;
1271
1272 if (this->is_array()) {
1273 element_type = this->fields.array;
1274 array_len = this->length;
1275 } else {
1276 element_type = this;
1277 array_len = 1;
1278 }
1279
1280 if (row_major) {
1281 vec_type = get_instance(element_type->base_type,
1282 element_type->matrix_columns, 1);
1283
1284 array_len *= element_type->vector_elements;
1285 } else {
1286 vec_type = get_instance(element_type->base_type,
1287 element_type->vector_elements, 1);
1288 array_len *= element_type->matrix_columns;
1289 }
1290 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
1291 array_len);
1292
1293 return array_type->std140_size(false);
1294 }
1295
1296 /* (4) If the member is an array of scalars or vectors, the base alignment
1297 * and array stride are set to match the base alignment of a single
1298 * array element, according to rules (1), (2), and (3), and rounded up
1299 * to the base alignment of a vec4. The array may have padding at the
1300 * end; the base offset of the member following the array is rounded up
1301 * to the next multiple of the base alignment.
1302 *
1303 * (10) If the member is an array of <S> structures, the <S> elements of
1304 * the array are laid out in order, according to rule (9).
1305 */
1306 if (this->is_array()) {
1307 if (this->fields.array->is_record()) {
1308 return this->length * this->fields.array->std140_size(row_major);
1309 } else {
1310 unsigned element_base_align =
1311 this->fields.array->std140_base_alignment(row_major);
1312 return this->length * MAX2(element_base_align, 16);
1313 }
1314 }
1315
1316 /* (9) If the member is a structure, the base alignment of the
1317 * structure is <N>, where <N> is the largest base alignment
1318 * value of any of its members, and rounded up to the base
1319 * alignment of a vec4. The individual members of this
1320 * sub-structure are then assigned offsets by applying this set
1321 * of rules recursively, where the base offset of the first
1322 * member of the sub-structure is equal to the aligned offset
1323 * of the structure. The structure may have padding at the end;
1324 * the base offset of the member following the sub-structure is
1325 * rounded up to the next multiple of the base alignment of the
1326 * structure.
1327 */
1328 if (this->is_record()) {
1329 unsigned size = 0;
1330 unsigned max_align = 0;
1331
1332 for (unsigned i = 0; i < this->length; i++) {
1333 bool field_row_major = row_major;
1334 const enum glsl_matrix_layout matrix_layout =
1335 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1336 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1337 field_row_major = true;
1338 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1339 field_row_major = false;
1340 }
1341
1342 const struct glsl_type *field_type = this->fields.structure[i].type;
1343 unsigned align = field_type->std140_base_alignment(field_row_major);
1344 size = glsl_align(size, align);
1345 size += field_type->std140_size(field_row_major);
1346
1347 max_align = MAX2(align, max_align);
1348
1349 if (field_type->is_record() && (i + 1 < this->length))
1350 size = glsl_align(size, 16);
1351 }
1352 size = glsl_align(size, MAX2(max_align, 16));
1353 return size;
1354 }
1355
1356 assert(!"not reached");
1357 return -1;
1358 }
1359
1360
1361 unsigned
1362 glsl_type::count_attribute_slots() const
1363 {
1364 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1365 *
1366 * "A scalar input counts the same amount against this limit as a vec4,
1367 * so applications may want to consider packing groups of four
1368 * unrelated float inputs together into a vector to better utilize the
1369 * capabilities of the underlying hardware. A matrix input will use up
1370 * multiple locations. The number of locations used will equal the
1371 * number of columns in the matrix."
1372 *
1373 * The spec does not explicitly say how arrays are counted. However, it
1374 * should be safe to assume the total number of slots consumed by an array
1375 * is the number of entries in the array multiplied by the number of slots
1376 * consumed by a single element of the array.
1377 *
1378 * The spec says nothing about how structs are counted, because vertex
1379 * attributes are not allowed to be (or contain) structs. However, Mesa
1380 * allows varying structs, the number of varying slots taken up by a
1381 * varying struct is simply equal to the sum of the number of slots taken
1382 * up by each element.
1383 */
1384 switch (this->base_type) {
1385 case GLSL_TYPE_UINT:
1386 case GLSL_TYPE_INT:
1387 case GLSL_TYPE_FLOAT:
1388 case GLSL_TYPE_BOOL:
1389 case GLSL_TYPE_DOUBLE:
1390 return this->matrix_columns;
1391
1392 case GLSL_TYPE_STRUCT:
1393 case GLSL_TYPE_INTERFACE: {
1394 unsigned size = 0;
1395
1396 for (unsigned i = 0; i < this->length; i++)
1397 size += this->fields.structure[i].type->count_attribute_slots();
1398
1399 return size;
1400 }
1401
1402 case GLSL_TYPE_ARRAY:
1403 return this->length * this->fields.array->count_attribute_slots();
1404
1405 case GLSL_TYPE_SAMPLER:
1406 case GLSL_TYPE_IMAGE:
1407 case GLSL_TYPE_ATOMIC_UINT:
1408 case GLSL_TYPE_VOID:
1409 case GLSL_TYPE_SUBROUTINE:
1410 case GLSL_TYPE_ERROR:
1411 break;
1412 }
1413
1414 assert(!"Unexpected type in count_attribute_slots()");
1415
1416 return 0;
1417 }
1418
1419 int
1420 glsl_type::coordinate_components() const
1421 {
1422 int size;
1423
1424 switch (sampler_dimensionality) {
1425 case GLSL_SAMPLER_DIM_1D:
1426 case GLSL_SAMPLER_DIM_BUF:
1427 size = 1;
1428 break;
1429 case GLSL_SAMPLER_DIM_2D:
1430 case GLSL_SAMPLER_DIM_RECT:
1431 case GLSL_SAMPLER_DIM_MS:
1432 case GLSL_SAMPLER_DIM_EXTERNAL:
1433 size = 2;
1434 break;
1435 case GLSL_SAMPLER_DIM_3D:
1436 case GLSL_SAMPLER_DIM_CUBE:
1437 size = 3;
1438 break;
1439 default:
1440 assert(!"Should not get here.");
1441 size = 1;
1442 break;
1443 }
1444
1445 /* Array textures need an additional component for the array index, except
1446 * for cubemap array images that behave like a 2D array of interleaved
1447 * cubemap faces.
1448 */
1449 if (sampler_array &&
1450 !(base_type == GLSL_TYPE_IMAGE &&
1451 sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE))
1452 size += 1;
1453
1454 return size;
1455 }