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