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