Android: clean-up and fix DRI module path handling
[mesa.git] / src / compiler / 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/macros.h"
26 #include "compiler/glsl/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 sampled_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), sampled_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 sampled_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 this->fields.structure[i].image_read_only = fields[i].image_read_only;
129 this->fields.structure[i].image_write_only = fields[i].image_write_only;
130 this->fields.structure[i].image_coherent = fields[i].image_coherent;
131 this->fields.structure[i].image_volatile = fields[i].image_volatile;
132 this->fields.structure[i].image_restrict = fields[i].image_restrict;
133 this->fields.structure[i].precision = fields[i].precision;
134 }
135
136 mtx_unlock(&glsl_type::mutex);
137 }
138
139 glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
140 enum glsl_interface_packing packing, const char *name) :
141 gl_type(0),
142 base_type(GLSL_TYPE_INTERFACE),
143 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
144 sampled_type(0), interface_packing((unsigned) packing),
145 vector_elements(0), matrix_columns(0),
146 length(num_fields)
147 {
148 unsigned int i;
149
150 mtx_lock(&glsl_type::mutex);
151
152 init_ralloc_type_ctx();
153 assert(name != NULL);
154 this->name = ralloc_strdup(this->mem_ctx, name);
155 this->fields.structure = ralloc_array(this->mem_ctx,
156 glsl_struct_field, length);
157 for (i = 0; i < length; i++) {
158 this->fields.structure[i].type = fields[i].type;
159 this->fields.structure[i].name = ralloc_strdup(this->fields.structure,
160 fields[i].name);
161 this->fields.structure[i].location = fields[i].location;
162 this->fields.structure[i].interpolation = fields[i].interpolation;
163 this->fields.structure[i].centroid = fields[i].centroid;
164 this->fields.structure[i].sample = fields[i].sample;
165 this->fields.structure[i].matrix_layout = fields[i].matrix_layout;
166 this->fields.structure[i].patch = fields[i].patch;
167 this->fields.structure[i].image_read_only = fields[i].image_read_only;
168 this->fields.structure[i].image_write_only = fields[i].image_write_only;
169 this->fields.structure[i].image_coherent = fields[i].image_coherent;
170 this->fields.structure[i].image_volatile = fields[i].image_volatile;
171 this->fields.structure[i].image_restrict = fields[i].image_restrict;
172 this->fields.structure[i].precision = fields[i].precision;
173 }
174
175 mtx_unlock(&glsl_type::mutex);
176 }
177
178 glsl_type::glsl_type(const glsl_type *return_type,
179 const glsl_function_param *params, unsigned num_params) :
180 gl_type(0),
181 base_type(GLSL_TYPE_FUNCTION),
182 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
183 sampled_type(0), interface_packing(0),
184 vector_elements(0), matrix_columns(0),
185 length(num_params)
186 {
187 unsigned int i;
188
189 mtx_lock(&glsl_type::mutex);
190
191 init_ralloc_type_ctx();
192
193 this->fields.parameters = rzalloc_array(this->mem_ctx,
194 glsl_function_param, num_params + 1);
195
196 /* We store the return type as the first parameter */
197 this->fields.parameters[0].type = return_type;
198 this->fields.parameters[0].in = false;
199 this->fields.parameters[0].out = true;
200
201 /* We store the i'th parameter in slot i+1 */
202 for (i = 0; i < length; i++) {
203 this->fields.parameters[i + 1].type = params[i].type;
204 this->fields.parameters[i + 1].in = params[i].in;
205 this->fields.parameters[i + 1].out = params[i].out;
206 }
207
208 mtx_unlock(&glsl_type::mutex);
209 }
210
211 glsl_type::glsl_type(const char *subroutine_name) :
212 gl_type(0),
213 base_type(GLSL_TYPE_SUBROUTINE),
214 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
215 sampled_type(0), interface_packing(0),
216 vector_elements(1), matrix_columns(1),
217 length(0)
218 {
219 mtx_lock(&glsl_type::mutex);
220
221 init_ralloc_type_ctx();
222 assert(subroutine_name != NULL);
223 this->name = ralloc_strdup(this->mem_ctx, subroutine_name);
224 mtx_unlock(&glsl_type::mutex);
225 }
226
227 bool
228 glsl_type::contains_sampler() const
229 {
230 if (this->is_array()) {
231 return this->fields.array->contains_sampler();
232 } else if (this->is_record()) {
233 for (unsigned int i = 0; i < this->length; i++) {
234 if (this->fields.structure[i].type->contains_sampler())
235 return true;
236 }
237 return false;
238 } else {
239 return this->is_sampler();
240 }
241 }
242
243
244 bool
245 glsl_type::contains_integer() const
246 {
247 if (this->is_array()) {
248 return this->fields.array->contains_integer();
249 } else if (this->is_record()) {
250 for (unsigned int i = 0; i < this->length; i++) {
251 if (this->fields.structure[i].type->contains_integer())
252 return true;
253 }
254 return false;
255 } else {
256 return this->is_integer();
257 }
258 }
259
260 bool
261 glsl_type::contains_double() const
262 {
263 if (this->is_array()) {
264 return this->fields.array->contains_double();
265 } else if (this->is_record()) {
266 for (unsigned int i = 0; i < this->length; i++) {
267 if (this->fields.structure[i].type->contains_double())
268 return true;
269 }
270 return false;
271 } else {
272 return this->is_double();
273 }
274 }
275
276 bool
277 glsl_type::contains_opaque() const {
278 switch (base_type) {
279 case GLSL_TYPE_SAMPLER:
280 case GLSL_TYPE_IMAGE:
281 case GLSL_TYPE_ATOMIC_UINT:
282 return true;
283 case GLSL_TYPE_ARRAY:
284 return fields.array->contains_opaque();
285 case GLSL_TYPE_STRUCT:
286 for (unsigned int i = 0; i < length; i++) {
287 if (fields.structure[i].type->contains_opaque())
288 return true;
289 }
290 return false;
291 default:
292 return false;
293 }
294 }
295
296 bool
297 glsl_type::contains_subroutine() const
298 {
299 if (this->is_array()) {
300 return this->fields.array->contains_subroutine();
301 } else if (this->is_record()) {
302 for (unsigned int i = 0; i < this->length; i++) {
303 if (this->fields.structure[i].type->contains_subroutine())
304 return true;
305 }
306 return false;
307 } else {
308 return this->is_subroutine();
309 }
310 }
311
312 gl_texture_index
313 glsl_type::sampler_index() const
314 {
315 const glsl_type *const t = (this->is_array()) ? this->fields.array : this;
316
317 assert(t->is_sampler());
318
319 switch (t->sampler_dimensionality) {
320 case GLSL_SAMPLER_DIM_1D:
321 return (t->sampler_array) ? TEXTURE_1D_ARRAY_INDEX : TEXTURE_1D_INDEX;
322 case GLSL_SAMPLER_DIM_2D:
323 return (t->sampler_array) ? TEXTURE_2D_ARRAY_INDEX : TEXTURE_2D_INDEX;
324 case GLSL_SAMPLER_DIM_3D:
325 return TEXTURE_3D_INDEX;
326 case GLSL_SAMPLER_DIM_CUBE:
327 return (t->sampler_array) ? TEXTURE_CUBE_ARRAY_INDEX : TEXTURE_CUBE_INDEX;
328 case GLSL_SAMPLER_DIM_RECT:
329 return TEXTURE_RECT_INDEX;
330 case GLSL_SAMPLER_DIM_BUF:
331 return TEXTURE_BUFFER_INDEX;
332 case GLSL_SAMPLER_DIM_EXTERNAL:
333 return TEXTURE_EXTERNAL_INDEX;
334 case GLSL_SAMPLER_DIM_MS:
335 return (t->sampler_array) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX : TEXTURE_2D_MULTISAMPLE_INDEX;
336 default:
337 assert(!"Should not get here.");
338 return TEXTURE_BUFFER_INDEX;
339 }
340 }
341
342 bool
343 glsl_type::contains_image() const
344 {
345 if (this->is_array()) {
346 return this->fields.array->contains_image();
347 } else if (this->is_record()) {
348 for (unsigned int i = 0; i < this->length; i++) {
349 if (this->fields.structure[i].type->contains_image())
350 return true;
351 }
352 return false;
353 } else {
354 return this->is_image();
355 }
356 }
357
358 const glsl_type *glsl_type::get_base_type() const
359 {
360 switch (base_type) {
361 case GLSL_TYPE_UINT:
362 return uint_type;
363 case GLSL_TYPE_INT:
364 return int_type;
365 case GLSL_TYPE_FLOAT:
366 return float_type;
367 case GLSL_TYPE_DOUBLE:
368 return double_type;
369 case GLSL_TYPE_BOOL:
370 return bool_type;
371 default:
372 return error_type;
373 }
374 }
375
376
377 const glsl_type *glsl_type::get_scalar_type() const
378 {
379 const glsl_type *type = this;
380
381 /* Handle arrays */
382 while (type->base_type == GLSL_TYPE_ARRAY)
383 type = type->fields.array;
384
385 /* Handle vectors and matrices */
386 switch (type->base_type) {
387 case GLSL_TYPE_UINT:
388 return uint_type;
389 case GLSL_TYPE_INT:
390 return int_type;
391 case GLSL_TYPE_FLOAT:
392 return float_type;
393 case GLSL_TYPE_DOUBLE:
394 return double_type;
395 case GLSL_TYPE_BOOL:
396 return bool_type;
397 default:
398 /* Handle everything else */
399 return type;
400 }
401 }
402
403
404 void
405 _mesa_glsl_release_types(void)
406 {
407 /* Should only be called during atexit (either when unloading shared
408 * object, or if process terminates), so no mutex-locking should be
409 * necessary.
410 */
411 if (glsl_type::array_types != NULL) {
412 _mesa_hash_table_destroy(glsl_type::array_types, NULL);
413 glsl_type::array_types = NULL;
414 }
415
416 if (glsl_type::record_types != NULL) {
417 _mesa_hash_table_destroy(glsl_type::record_types, NULL);
418 glsl_type::record_types = NULL;
419 }
420
421 if (glsl_type::interface_types != NULL) {
422 _mesa_hash_table_destroy(glsl_type::interface_types, NULL);
423 glsl_type::interface_types = NULL;
424 }
425 }
426
427
428 glsl_type::glsl_type(const glsl_type *array, unsigned length) :
429 base_type(GLSL_TYPE_ARRAY),
430 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
431 sampled_type(0), interface_packing(0),
432 vector_elements(0), matrix_columns(0),
433 length(length), name(NULL)
434 {
435 this->fields.array = array;
436 /* Inherit the gl type of the base. The GL type is used for
437 * uniform/statevar handling in Mesa and the arrayness of the type
438 * is represented by the size rather than the type.
439 */
440 this->gl_type = array->gl_type;
441
442 /* Allow a maximum of 10 characters for the array size. This is enough
443 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
444 * NUL.
445 */
446 const unsigned name_length = strlen(array->name) + 10 + 3;
447
448 mtx_lock(&glsl_type::mutex);
449 char *const n = (char *) ralloc_size(this->mem_ctx, name_length);
450 mtx_unlock(&glsl_type::mutex);
451
452 if (length == 0)
453 snprintf(n, name_length, "%s[]", array->name);
454 else {
455 /* insert outermost dimensions in the correct spot
456 * otherwise the dimension order will be backwards
457 */
458 const char *pos = strchr(array->name, '[');
459 if (pos) {
460 int idx = pos - array->name;
461 snprintf(n, idx+1, "%s", array->name);
462 snprintf(n + idx, name_length - idx, "[%u]%s",
463 length, array->name + idx);
464 } else {
465 snprintf(n, name_length, "%s[%u]", array->name, length);
466 }
467 }
468
469 this->name = n;
470 }
471
472
473 const glsl_type *
474 glsl_type::vec(unsigned components)
475 {
476 if (components == 0 || components > 4)
477 return error_type;
478
479 static const glsl_type *const ts[] = {
480 float_type, vec2_type, vec3_type, vec4_type
481 };
482 return ts[components - 1];
483 }
484
485 const glsl_type *
486 glsl_type::dvec(unsigned components)
487 {
488 if (components == 0 || components > 4)
489 return error_type;
490
491 static const glsl_type *const ts[] = {
492 double_type, dvec2_type, dvec3_type, dvec4_type
493 };
494 return ts[components - 1];
495 }
496
497 const glsl_type *
498 glsl_type::ivec(unsigned components)
499 {
500 if (components == 0 || components > 4)
501 return error_type;
502
503 static const glsl_type *const ts[] = {
504 int_type, ivec2_type, ivec3_type, ivec4_type
505 };
506 return ts[components - 1];
507 }
508
509
510 const glsl_type *
511 glsl_type::uvec(unsigned components)
512 {
513 if (components == 0 || components > 4)
514 return error_type;
515
516 static const glsl_type *const ts[] = {
517 uint_type, uvec2_type, uvec3_type, uvec4_type
518 };
519 return ts[components - 1];
520 }
521
522
523 const glsl_type *
524 glsl_type::bvec(unsigned components)
525 {
526 if (components == 0 || components > 4)
527 return error_type;
528
529 static const glsl_type *const ts[] = {
530 bool_type, bvec2_type, bvec3_type, bvec4_type
531 };
532 return ts[components - 1];
533 }
534
535
536 const glsl_type *
537 glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
538 {
539 if (base_type == GLSL_TYPE_VOID)
540 return void_type;
541
542 if ((rows < 1) || (rows > 4) || (columns < 1) || (columns > 4))
543 return error_type;
544
545 /* Treat GLSL vectors as Nx1 matrices.
546 */
547 if (columns == 1) {
548 switch (base_type) {
549 case GLSL_TYPE_UINT:
550 return uvec(rows);
551 case GLSL_TYPE_INT:
552 return ivec(rows);
553 case GLSL_TYPE_FLOAT:
554 return vec(rows);
555 case GLSL_TYPE_DOUBLE:
556 return dvec(rows);
557 case GLSL_TYPE_BOOL:
558 return bvec(rows);
559 default:
560 return error_type;
561 }
562 } else {
563 if ((base_type != GLSL_TYPE_FLOAT && base_type != GLSL_TYPE_DOUBLE) || (rows == 1))
564 return error_type;
565
566 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
567 * combinations are valid:
568 *
569 * 1 2 3 4
570 * 1
571 * 2 x x x
572 * 3 x x x
573 * 4 x x x
574 */
575 #define IDX(c,r) (((c-1)*3) + (r-1))
576
577 if (base_type == GLSL_TYPE_DOUBLE) {
578 switch (IDX(columns, rows)) {
579 case IDX(2,2): return dmat2_type;
580 case IDX(2,3): return dmat2x3_type;
581 case IDX(2,4): return dmat2x4_type;
582 case IDX(3,2): return dmat3x2_type;
583 case IDX(3,3): return dmat3_type;
584 case IDX(3,4): return dmat3x4_type;
585 case IDX(4,2): return dmat4x2_type;
586 case IDX(4,3): return dmat4x3_type;
587 case IDX(4,4): return dmat4_type;
588 default: return error_type;
589 }
590 } else {
591 switch (IDX(columns, rows)) {
592 case IDX(2,2): return mat2_type;
593 case IDX(2,3): return mat2x3_type;
594 case IDX(2,4): return mat2x4_type;
595 case IDX(3,2): return mat3x2_type;
596 case IDX(3,3): return mat3_type;
597 case IDX(3,4): return mat3x4_type;
598 case IDX(4,2): return mat4x2_type;
599 case IDX(4,3): return mat4x3_type;
600 case IDX(4,4): return mat4_type;
601 default: return error_type;
602 }
603 }
604 }
605
606 assert(!"Should not get here.");
607 return error_type;
608 }
609
610 const glsl_type *
611 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim,
612 bool shadow,
613 bool array,
614 glsl_base_type type)
615 {
616 switch (type) {
617 case GLSL_TYPE_FLOAT:
618 switch (dim) {
619 case GLSL_SAMPLER_DIM_1D:
620 if (shadow)
621 return (array ? sampler1DArrayShadow_type : sampler1DShadow_type);
622 else
623 return (array ? sampler1DArray_type : sampler1D_type);
624 case GLSL_SAMPLER_DIM_2D:
625 if (shadow)
626 return (array ? sampler2DArrayShadow_type : sampler2DShadow_type);
627 else
628 return (array ? sampler2DArray_type : sampler2D_type);
629 case GLSL_SAMPLER_DIM_3D:
630 if (shadow || array)
631 return error_type;
632 else
633 return sampler3D_type;
634 case GLSL_SAMPLER_DIM_CUBE:
635 if (shadow)
636 return (array ? samplerCubeArrayShadow_type : samplerCubeShadow_type);
637 else
638 return (array ? samplerCubeArray_type : samplerCube_type);
639 case GLSL_SAMPLER_DIM_RECT:
640 if (array)
641 return error_type;
642 if (shadow)
643 return sampler2DRectShadow_type;
644 else
645 return sampler2DRect_type;
646 case GLSL_SAMPLER_DIM_BUF:
647 if (shadow || array)
648 return error_type;
649 else
650 return samplerBuffer_type;
651 case GLSL_SAMPLER_DIM_MS:
652 if (shadow)
653 return error_type;
654 return (array ? sampler2DMSArray_type : sampler2DMS_type);
655 case GLSL_SAMPLER_DIM_EXTERNAL:
656 if (shadow || array)
657 return error_type;
658 else
659 return samplerExternalOES_type;
660 }
661 case GLSL_TYPE_INT:
662 if (shadow)
663 return error_type;
664 switch (dim) {
665 case GLSL_SAMPLER_DIM_1D:
666 return (array ? isampler1DArray_type : isampler1D_type);
667 case GLSL_SAMPLER_DIM_2D:
668 return (array ? isampler2DArray_type : isampler2D_type);
669 case GLSL_SAMPLER_DIM_3D:
670 if (array)
671 return error_type;
672 return isampler3D_type;
673 case GLSL_SAMPLER_DIM_CUBE:
674 return (array ? isamplerCubeArray_type : isamplerCube_type);
675 case GLSL_SAMPLER_DIM_RECT:
676 if (array)
677 return error_type;
678 return isampler2DRect_type;
679 case GLSL_SAMPLER_DIM_BUF:
680 if (array)
681 return error_type;
682 return isamplerBuffer_type;
683 case GLSL_SAMPLER_DIM_MS:
684 return (array ? isampler2DMSArray_type : isampler2DMS_type);
685 case GLSL_SAMPLER_DIM_EXTERNAL:
686 return error_type;
687 }
688 case GLSL_TYPE_UINT:
689 if (shadow)
690 return error_type;
691 switch (dim) {
692 case GLSL_SAMPLER_DIM_1D:
693 return (array ? usampler1DArray_type : usampler1D_type);
694 case GLSL_SAMPLER_DIM_2D:
695 return (array ? usampler2DArray_type : usampler2D_type);
696 case GLSL_SAMPLER_DIM_3D:
697 if (array)
698 return error_type;
699 return usampler3D_type;
700 case GLSL_SAMPLER_DIM_CUBE:
701 return (array ? usamplerCubeArray_type : usamplerCube_type);
702 case GLSL_SAMPLER_DIM_RECT:
703 if (array)
704 return error_type;
705 return usampler2DRect_type;
706 case GLSL_SAMPLER_DIM_BUF:
707 if (array)
708 return error_type;
709 return usamplerBuffer_type;
710 case GLSL_SAMPLER_DIM_MS:
711 return (array ? usampler2DMSArray_type : usampler2DMS_type);
712 case GLSL_SAMPLER_DIM_EXTERNAL:
713 return error_type;
714 }
715 default:
716 return error_type;
717 }
718
719 unreachable("switch statement above should be complete");
720 }
721
722 const glsl_type *
723 glsl_type::get_image_instance(enum glsl_sampler_dim dim,
724 bool array, glsl_base_type type)
725 {
726 switch (type) {
727 case GLSL_TYPE_FLOAT:
728 switch (dim) {
729 case GLSL_SAMPLER_DIM_1D:
730 return (array ? image1DArray_type : image1D_type);
731 case GLSL_SAMPLER_DIM_2D:
732 return (array ? image2DArray_type : image2D_type);
733 case GLSL_SAMPLER_DIM_3D:
734 return image3D_type;
735 case GLSL_SAMPLER_DIM_CUBE:
736 return (array ? imageCubeArray_type : imageCube_type);
737 case GLSL_SAMPLER_DIM_RECT:
738 if (array)
739 return error_type;
740 else
741 return image2DRect_type;
742 case GLSL_SAMPLER_DIM_BUF:
743 if (array)
744 return error_type;
745 else
746 return imageBuffer_type;
747 case GLSL_SAMPLER_DIM_MS:
748 return (array ? image2DMSArray_type : image2DMS_type);
749 case GLSL_SAMPLER_DIM_EXTERNAL:
750 return error_type;
751 }
752 case GLSL_TYPE_INT:
753 switch (dim) {
754 case GLSL_SAMPLER_DIM_1D:
755 return (array ? iimage1DArray_type : iimage1D_type);
756 case GLSL_SAMPLER_DIM_2D:
757 return (array ? iimage2DArray_type : iimage2D_type);
758 case GLSL_SAMPLER_DIM_3D:
759 if (array)
760 return error_type;
761 return iimage3D_type;
762 case GLSL_SAMPLER_DIM_CUBE:
763 return (array ? iimageCubeArray_type : iimageCube_type);
764 case GLSL_SAMPLER_DIM_RECT:
765 if (array)
766 return error_type;
767 return iimage2DRect_type;
768 case GLSL_SAMPLER_DIM_BUF:
769 if (array)
770 return error_type;
771 return iimageBuffer_type;
772 case GLSL_SAMPLER_DIM_MS:
773 return (array ? iimage2DMSArray_type : iimage2DMS_type);
774 case GLSL_SAMPLER_DIM_EXTERNAL:
775 return error_type;
776 }
777 case GLSL_TYPE_UINT:
778 switch (dim) {
779 case GLSL_SAMPLER_DIM_1D:
780 return (array ? uimage1DArray_type : uimage1D_type);
781 case GLSL_SAMPLER_DIM_2D:
782 return (array ? uimage2DArray_type : uimage2D_type);
783 case GLSL_SAMPLER_DIM_3D:
784 if (array)
785 return error_type;
786 return uimage3D_type;
787 case GLSL_SAMPLER_DIM_CUBE:
788 return (array ? uimageCubeArray_type : uimageCube_type);
789 case GLSL_SAMPLER_DIM_RECT:
790 if (array)
791 return error_type;
792 return uimage2DRect_type;
793 case GLSL_SAMPLER_DIM_BUF:
794 if (array)
795 return error_type;
796 return uimageBuffer_type;
797 case GLSL_SAMPLER_DIM_MS:
798 return (array ? uimage2DMSArray_type : uimage2DMS_type);
799 case GLSL_SAMPLER_DIM_EXTERNAL:
800 return error_type;
801 }
802 default:
803 return error_type;
804 }
805
806 unreachable("switch statement above should be complete");
807 }
808
809 const glsl_type *
810 glsl_type::get_array_instance(const glsl_type *base, unsigned array_size)
811 {
812 /* Generate a name using the base type pointer in the key. This is
813 * done because the name of the base type may not be unique across
814 * shaders. For example, two shaders may have different record types
815 * named 'foo'.
816 */
817 char key[128];
818 snprintf(key, sizeof(key), "%p[%u]", (void *) base, array_size);
819
820 mtx_lock(&glsl_type::mutex);
821
822 if (array_types == NULL) {
823 array_types = _mesa_hash_table_create(NULL, _mesa_key_hash_string,
824 _mesa_key_string_equal);
825 }
826
827 const struct hash_entry *entry = _mesa_hash_table_search(array_types, key);
828 if (entry == NULL) {
829 mtx_unlock(&glsl_type::mutex);
830 const glsl_type *t = new glsl_type(base, array_size);
831 mtx_lock(&glsl_type::mutex);
832
833 entry = _mesa_hash_table_insert(array_types,
834 ralloc_strdup(mem_ctx, key),
835 (void *) t);
836 }
837
838 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_ARRAY);
839 assert(((glsl_type *) entry->data)->length == array_size);
840 assert(((glsl_type *) entry->data)->fields.array == base);
841
842 mtx_unlock(&glsl_type::mutex);
843
844 return (glsl_type *) entry->data;
845 }
846
847
848 bool
849 glsl_type::record_compare(const glsl_type *b) const
850 {
851 if (this->length != b->length)
852 return false;
853
854 if (this->interface_packing != b->interface_packing)
855 return false;
856
857 /* From the GLSL 4.20 specification (Sec 4.2):
858 *
859 * "Structures must have the same name, sequence of type names, and
860 * type definitions, and field names to be considered the same type."
861 *
862 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
863 *
864 * Note that we cannot force type name check when comparing unnamed
865 * structure types, these have a unique name assigned during parsing.
866 */
867 if (!this->is_anonymous() && !b->is_anonymous())
868 if (strcmp(this->name, b->name) != 0)
869 return false;
870
871 for (unsigned i = 0; i < this->length; i++) {
872 if (this->fields.structure[i].type != b->fields.structure[i].type)
873 return false;
874 if (strcmp(this->fields.structure[i].name,
875 b->fields.structure[i].name) != 0)
876 return false;
877 if (this->fields.structure[i].matrix_layout
878 != b->fields.structure[i].matrix_layout)
879 return false;
880 if (this->fields.structure[i].location
881 != b->fields.structure[i].location)
882 return false;
883 if (this->fields.structure[i].interpolation
884 != b->fields.structure[i].interpolation)
885 return false;
886 if (this->fields.structure[i].centroid
887 != b->fields.structure[i].centroid)
888 return false;
889 if (this->fields.structure[i].sample
890 != b->fields.structure[i].sample)
891 return false;
892 if (this->fields.structure[i].patch
893 != b->fields.structure[i].patch)
894 return false;
895 if (this->fields.structure[i].image_read_only
896 != b->fields.structure[i].image_read_only)
897 return false;
898 if (this->fields.structure[i].image_write_only
899 != b->fields.structure[i].image_write_only)
900 return false;
901 if (this->fields.structure[i].image_coherent
902 != b->fields.structure[i].image_coherent)
903 return false;
904 if (this->fields.structure[i].image_volatile
905 != b->fields.structure[i].image_volatile)
906 return false;
907 if (this->fields.structure[i].image_restrict
908 != b->fields.structure[i].image_restrict)
909 return false;
910 if (this->fields.structure[i].precision
911 != b->fields.structure[i].precision)
912 return false;
913 }
914
915 return true;
916 }
917
918
919 bool
920 glsl_type::record_key_compare(const void *a, const void *b)
921 {
922 const glsl_type *const key1 = (glsl_type *) a;
923 const glsl_type *const key2 = (glsl_type *) b;
924
925 return strcmp(key1->name, key2->name) == 0 && key1->record_compare(key2);
926 }
927
928
929 /**
930 * Generate an integer hash value for a glsl_type structure type.
931 */
932 unsigned
933 glsl_type::record_key_hash(const void *a)
934 {
935 const glsl_type *const key = (glsl_type *) a;
936 uintptr_t hash = key->length;
937 unsigned retval;
938
939 for (unsigned i = 0; i < key->length; i++) {
940 /* casting pointer to uintptr_t */
941 hash = (hash * 13 ) + (uintptr_t) key->fields.structure[i].type;
942 }
943
944 if (sizeof(hash) == 8)
945 retval = (hash & 0xffffffff) ^ ((uint64_t) hash >> 32);
946 else
947 retval = hash;
948
949 return retval;
950 }
951
952
953 const glsl_type *
954 glsl_type::get_record_instance(const glsl_struct_field *fields,
955 unsigned num_fields,
956 const char *name)
957 {
958 const glsl_type key(fields, num_fields, name);
959
960 mtx_lock(&glsl_type::mutex);
961
962 if (record_types == NULL) {
963 record_types = _mesa_hash_table_create(NULL, record_key_hash,
964 record_key_compare);
965 }
966
967 const struct hash_entry *entry = _mesa_hash_table_search(record_types,
968 &key);
969 if (entry == NULL) {
970 mtx_unlock(&glsl_type::mutex);
971 const glsl_type *t = new glsl_type(fields, num_fields, name);
972 mtx_lock(&glsl_type::mutex);
973
974 entry = _mesa_hash_table_insert(record_types, t, (void *) t);
975 }
976
977 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_STRUCT);
978 assert(((glsl_type *) entry->data)->length == num_fields);
979 assert(strcmp(((glsl_type *) entry->data)->name, name) == 0);
980
981 mtx_unlock(&glsl_type::mutex);
982
983 return (glsl_type *) entry->data;
984 }
985
986
987 const glsl_type *
988 glsl_type::get_interface_instance(const glsl_struct_field *fields,
989 unsigned num_fields,
990 enum glsl_interface_packing packing,
991 const char *block_name)
992 {
993 const glsl_type key(fields, num_fields, packing, block_name);
994
995 mtx_lock(&glsl_type::mutex);
996
997 if (interface_types == NULL) {
998 interface_types = _mesa_hash_table_create(NULL, record_key_hash,
999 record_key_compare);
1000 }
1001
1002 const struct hash_entry *entry = _mesa_hash_table_search(interface_types,
1003 &key);
1004 if (entry == NULL) {
1005 mtx_unlock(&glsl_type::mutex);
1006 const glsl_type *t = new glsl_type(fields, num_fields,
1007 packing, block_name);
1008 mtx_lock(&glsl_type::mutex);
1009
1010 entry = _mesa_hash_table_insert(interface_types, t, (void *) t);
1011 }
1012
1013 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_INTERFACE);
1014 assert(((glsl_type *) entry->data)->length == num_fields);
1015 assert(strcmp(((glsl_type *) entry->data)->name, block_name) == 0);
1016
1017 mtx_unlock(&glsl_type::mutex);
1018
1019 return (glsl_type *) entry->data;
1020 }
1021
1022 const glsl_type *
1023 glsl_type::get_subroutine_instance(const char *subroutine_name)
1024 {
1025 const glsl_type key(subroutine_name);
1026
1027 mtx_lock(&glsl_type::mutex);
1028
1029 if (subroutine_types == NULL) {
1030 subroutine_types = _mesa_hash_table_create(NULL, record_key_hash,
1031 record_key_compare);
1032 }
1033
1034 const struct hash_entry *entry = _mesa_hash_table_search(subroutine_types,
1035 &key);
1036 if (entry == NULL) {
1037 mtx_unlock(&glsl_type::mutex);
1038 const glsl_type *t = new glsl_type(subroutine_name);
1039 mtx_lock(&glsl_type::mutex);
1040
1041 entry = _mesa_hash_table_insert(subroutine_types, t, (void *) t);
1042 }
1043
1044 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_SUBROUTINE);
1045 assert(strcmp(((glsl_type *) entry->data)->name, subroutine_name) == 0);
1046
1047 mtx_unlock(&glsl_type::mutex);
1048
1049 return (glsl_type *) entry->data;
1050 }
1051
1052
1053 static bool
1054 function_key_compare(const void *a, const void *b)
1055 {
1056 const glsl_type *const key1 = (glsl_type *) a;
1057 const glsl_type *const key2 = (glsl_type *) b;
1058
1059 if (key1->length != key2->length)
1060 return 1;
1061
1062 return memcmp(key1->fields.parameters, key2->fields.parameters,
1063 (key1->length + 1) * sizeof(*key1->fields.parameters)) == 0;
1064 }
1065
1066
1067 static uint32_t
1068 function_key_hash(const void *a)
1069 {
1070 const glsl_type *const key = (glsl_type *) a;
1071 char hash_key[128];
1072 unsigned size = 0;
1073
1074 size = snprintf(hash_key, sizeof(hash_key), "%08x", key->length);
1075
1076 for (unsigned i = 0; i < key->length; i++) {
1077 if (size >= sizeof(hash_key))
1078 break;
1079
1080 size += snprintf(& hash_key[size], sizeof(hash_key) - size,
1081 "%p", (void *) key->fields.structure[i].type);
1082 }
1083
1084 return _mesa_hash_string(hash_key);
1085 }
1086
1087 const glsl_type *
1088 glsl_type::get_function_instance(const glsl_type *return_type,
1089 const glsl_function_param *params,
1090 unsigned num_params)
1091 {
1092 const glsl_type key(return_type, params, num_params);
1093
1094 mtx_lock(&glsl_type::mutex);
1095
1096 if (function_types == NULL) {
1097 function_types = _mesa_hash_table_create(NULL, function_key_hash,
1098 function_key_compare);
1099 }
1100
1101 struct hash_entry *entry = _mesa_hash_table_search(function_types, &key);
1102 if (entry == NULL) {
1103 mtx_unlock(&glsl_type::mutex);
1104 const glsl_type *t = new glsl_type(return_type, params, num_params);
1105 mtx_lock(&glsl_type::mutex);
1106
1107 entry = _mesa_hash_table_insert(function_types, t, (void *) t);
1108 }
1109
1110 const glsl_type *t = (const glsl_type *)entry->data;
1111
1112 assert(t->base_type == GLSL_TYPE_FUNCTION);
1113 assert(t->length == num_params);
1114
1115 mtx_unlock(&glsl_type::mutex);
1116
1117 return t;
1118 }
1119
1120
1121 const glsl_type *
1122 glsl_type::get_mul_type(const glsl_type *type_a, const glsl_type *type_b)
1123 {
1124 if (type_a == type_b) {
1125 return type_a;
1126 } else if (type_a->is_matrix() && type_b->is_matrix()) {
1127 /* Matrix multiply. The columns of A must match the rows of B. Given
1128 * the other previously tested constraints, this means the vector type
1129 * of a row from A must be the same as the vector type of a column from
1130 * B.
1131 */
1132 if (type_a->row_type() == type_b->column_type()) {
1133 /* The resulting matrix has the number of columns of matrix B and
1134 * the number of rows of matrix A. We get the row count of A by
1135 * looking at the size of a vector that makes up a column. The
1136 * transpose (size of a row) is done for B.
1137 */
1138 const glsl_type *const type =
1139 get_instance(type_a->base_type,
1140 type_a->column_type()->vector_elements,
1141 type_b->row_type()->vector_elements);
1142 assert(type != error_type);
1143
1144 return type;
1145 }
1146 } else if (type_a->is_matrix()) {
1147 /* A is a matrix and B is a column vector. Columns of A must match
1148 * rows of B. Given the other previously tested constraints, this
1149 * means the vector type of a row from A must be the same as the
1150 * vector the type of B.
1151 */
1152 if (type_a->row_type() == type_b) {
1153 /* The resulting vector has a number of elements equal to
1154 * the number of rows of matrix A. */
1155 const glsl_type *const type =
1156 get_instance(type_a->base_type,
1157 type_a->column_type()->vector_elements,
1158 1);
1159 assert(type != error_type);
1160
1161 return type;
1162 }
1163 } else {
1164 assert(type_b->is_matrix());
1165
1166 /* A is a row vector and B is a matrix. Columns of A must match rows
1167 * of B. Given the other previously tested constraints, this means
1168 * the type of A must be the same as the vector type of a column from
1169 * B.
1170 */
1171 if (type_a == type_b->column_type()) {
1172 /* The resulting vector has a number of elements equal to
1173 * the number of columns of matrix B. */
1174 const glsl_type *const type =
1175 get_instance(type_a->base_type,
1176 type_b->row_type()->vector_elements,
1177 1);
1178 assert(type != error_type);
1179
1180 return type;
1181 }
1182 }
1183
1184 return error_type;
1185 }
1186
1187
1188 const glsl_type *
1189 glsl_type::field_type(const char *name) const
1190 {
1191 if (this->base_type != GLSL_TYPE_STRUCT
1192 && this->base_type != GLSL_TYPE_INTERFACE)
1193 return error_type;
1194
1195 for (unsigned i = 0; i < this->length; i++) {
1196 if (strcmp(name, this->fields.structure[i].name) == 0)
1197 return this->fields.structure[i].type;
1198 }
1199
1200 return error_type;
1201 }
1202
1203
1204 int
1205 glsl_type::field_index(const char *name) const
1206 {
1207 if (this->base_type != GLSL_TYPE_STRUCT
1208 && this->base_type != GLSL_TYPE_INTERFACE)
1209 return -1;
1210
1211 for (unsigned i = 0; i < this->length; i++) {
1212 if (strcmp(name, this->fields.structure[i].name) == 0)
1213 return i;
1214 }
1215
1216 return -1;
1217 }
1218
1219
1220 unsigned
1221 glsl_type::component_slots() const
1222 {
1223 switch (this->base_type) {
1224 case GLSL_TYPE_UINT:
1225 case GLSL_TYPE_INT:
1226 case GLSL_TYPE_FLOAT:
1227 case GLSL_TYPE_BOOL:
1228 return this->components();
1229
1230 case GLSL_TYPE_DOUBLE:
1231 return 2 * this->components();
1232
1233 case GLSL_TYPE_STRUCT:
1234 case GLSL_TYPE_INTERFACE: {
1235 unsigned size = 0;
1236
1237 for (unsigned i = 0; i < this->length; i++)
1238 size += this->fields.structure[i].type->component_slots();
1239
1240 return size;
1241 }
1242
1243 case GLSL_TYPE_ARRAY:
1244 return this->length * this->fields.array->component_slots();
1245
1246 case GLSL_TYPE_IMAGE:
1247 return 1;
1248 case GLSL_TYPE_SUBROUTINE:
1249 return 1;
1250
1251 case GLSL_TYPE_FUNCTION:
1252 case GLSL_TYPE_SAMPLER:
1253 case GLSL_TYPE_ATOMIC_UINT:
1254 case GLSL_TYPE_VOID:
1255 case GLSL_TYPE_ERROR:
1256 break;
1257 }
1258
1259 return 0;
1260 }
1261
1262 unsigned
1263 glsl_type::record_location_offset(unsigned length) const
1264 {
1265 unsigned offset = 0;
1266 const glsl_type *t = this->without_array();
1267 if (t->is_record()) {
1268 assert(length <= t->length);
1269
1270 for (unsigned i = 0; i < length; i++) {
1271 const glsl_type *st = t->fields.structure[i].type;
1272 const glsl_type *wa = st->without_array();
1273 if (wa->is_record()) {
1274 unsigned r_offset = wa->record_location_offset(wa->length);
1275 offset += st->is_array() ?
1276 st->arrays_of_arrays_size() * r_offset : r_offset;
1277 } else if (st->is_array() && st->fields.array->is_array()) {
1278 unsigned outer_array_size = st->length;
1279 const glsl_type *base_type = st->fields.array;
1280
1281 /* For arrays of arrays the outer arrays take up a uniform
1282 * slot for each element. The innermost array elements share a
1283 * single slot so we ignore the innermost array when calculating
1284 * the offset.
1285 */
1286 while (base_type->fields.array->is_array()) {
1287 outer_array_size = outer_array_size * base_type->length;
1288 base_type = base_type->fields.array;
1289 }
1290 offset += outer_array_size;
1291 } else {
1292 /* We dont worry about arrays here because unless the array
1293 * contains a structure or another array it only takes up a single
1294 * uniform slot.
1295 */
1296 offset += 1;
1297 }
1298 }
1299 }
1300 return offset;
1301 }
1302
1303 unsigned
1304 glsl_type::uniform_locations() const
1305 {
1306 unsigned size = 0;
1307
1308 switch (this->base_type) {
1309 case GLSL_TYPE_UINT:
1310 case GLSL_TYPE_INT:
1311 case GLSL_TYPE_FLOAT:
1312 case GLSL_TYPE_DOUBLE:
1313 case GLSL_TYPE_BOOL:
1314 case GLSL_TYPE_SAMPLER:
1315 case GLSL_TYPE_IMAGE:
1316 case GLSL_TYPE_SUBROUTINE:
1317 return 1;
1318
1319 case GLSL_TYPE_STRUCT:
1320 case GLSL_TYPE_INTERFACE:
1321 for (unsigned i = 0; i < this->length; i++)
1322 size += this->fields.structure[i].type->uniform_locations();
1323 return size;
1324 case GLSL_TYPE_ARRAY:
1325 return this->length * this->fields.array->uniform_locations();
1326 default:
1327 return 0;
1328 }
1329 }
1330
1331 bool
1332 glsl_type::can_implicitly_convert_to(const glsl_type *desired,
1333 _mesa_glsl_parse_state *state) const
1334 {
1335 if (this == desired)
1336 return true;
1337
1338 /* ESSL does not allow implicit conversions. If there is no state, we're
1339 * doing intra-stage function linking where these checks have already been
1340 * done.
1341 */
1342 if (state && state->es_shader)
1343 return false;
1344
1345 /* There is no conversion among matrix types. */
1346 if (this->matrix_columns > 1 || desired->matrix_columns > 1)
1347 return false;
1348
1349 /* Vector size must match. */
1350 if (this->vector_elements != desired->vector_elements)
1351 return false;
1352
1353 /* int and uint can be converted to float. */
1354 if (desired->is_float() && this->is_integer())
1355 return true;
1356
1357 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
1358 * Note that state may be NULL here, when resolving function calls in the
1359 * linker. By this time, all the state-dependent checks have already
1360 * happened though, so allow anything that's allowed in any shader version. */
1361 if ((!state || state->is_version(400, 0) || state->ARB_gpu_shader5_enable) &&
1362 desired->base_type == GLSL_TYPE_UINT && this->base_type == GLSL_TYPE_INT)
1363 return true;
1364
1365 /* No implicit conversions from double. */
1366 if ((!state || state->has_double()) && this->is_double())
1367 return false;
1368
1369 /* Conversions from different types to double. */
1370 if ((!state || state->has_double()) && desired->is_double()) {
1371 if (this->is_float())
1372 return true;
1373 if (this->is_integer())
1374 return true;
1375 }
1376
1377 return false;
1378 }
1379
1380 unsigned
1381 glsl_type::std140_base_alignment(bool row_major) const
1382 {
1383 unsigned N = is_double() ? 8 : 4;
1384
1385 /* (1) If the member is a scalar consuming <N> basic machine units, the
1386 * base alignment is <N>.
1387 *
1388 * (2) If the member is a two- or four-component vector with components
1389 * consuming <N> basic machine units, the base alignment is 2<N> or
1390 * 4<N>, respectively.
1391 *
1392 * (3) If the member is a three-component vector with components consuming
1393 * <N> basic machine units, the base alignment is 4<N>.
1394 */
1395 if (this->is_scalar() || this->is_vector()) {
1396 switch (this->vector_elements) {
1397 case 1:
1398 return N;
1399 case 2:
1400 return 2 * N;
1401 case 3:
1402 case 4:
1403 return 4 * N;
1404 }
1405 }
1406
1407 /* (4) If the member is an array of scalars or vectors, the base alignment
1408 * and array stride are set to match the base alignment of a single
1409 * array element, according to rules (1), (2), and (3), and rounded up
1410 * to the base alignment of a vec4. The array may have padding at the
1411 * end; the base offset of the member following the array is rounded up
1412 * to the next multiple of the base alignment.
1413 *
1414 * (6) If the member is an array of <S> column-major matrices with <C>
1415 * columns and <R> rows, the matrix is stored identically to a row of
1416 * <S>*<C> column vectors with <R> components each, according to rule
1417 * (4).
1418 *
1419 * (8) If the member is an array of <S> row-major matrices with <C> columns
1420 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1421 * row vectors with <C> components each, according to rule (4).
1422 *
1423 * (10) If the member is an array of <S> structures, the <S> elements of
1424 * the array are laid out in order, according to rule (9).
1425 */
1426 if (this->is_array()) {
1427 if (this->fields.array->is_scalar() ||
1428 this->fields.array->is_vector() ||
1429 this->fields.array->is_matrix()) {
1430 return MAX2(this->fields.array->std140_base_alignment(row_major), 16);
1431 } else {
1432 assert(this->fields.array->is_record() ||
1433 this->fields.array->is_array());
1434 return this->fields.array->std140_base_alignment(row_major);
1435 }
1436 }
1437
1438 /* (5) If the member is a column-major matrix with <C> columns and
1439 * <R> rows, the matrix is stored identically to an array of
1440 * <C> column vectors with <R> components each, according to
1441 * rule (4).
1442 *
1443 * (7) If the member is a row-major matrix with <C> columns and <R>
1444 * rows, the matrix is stored identically to an array of <R>
1445 * row vectors with <C> components each, according to rule (4).
1446 */
1447 if (this->is_matrix()) {
1448 const struct glsl_type *vec_type, *array_type;
1449 int c = this->matrix_columns;
1450 int r = this->vector_elements;
1451
1452 if (row_major) {
1453 vec_type = get_instance(base_type, c, 1);
1454 array_type = glsl_type::get_array_instance(vec_type, r);
1455 } else {
1456 vec_type = get_instance(base_type, r, 1);
1457 array_type = glsl_type::get_array_instance(vec_type, c);
1458 }
1459
1460 return array_type->std140_base_alignment(false);
1461 }
1462
1463 /* (9) If the member is a structure, the base alignment of the
1464 * structure is <N>, where <N> is the largest base alignment
1465 * value of any of its members, and rounded up to the base
1466 * alignment of a vec4. The individual members of this
1467 * sub-structure are then assigned offsets by applying this set
1468 * of rules recursively, where the base offset of the first
1469 * member of the sub-structure is equal to the aligned offset
1470 * of the structure. The structure may have padding at the end;
1471 * the base offset of the member following the sub-structure is
1472 * rounded up to the next multiple of the base alignment of the
1473 * structure.
1474 */
1475 if (this->is_record()) {
1476 unsigned base_alignment = 16;
1477 for (unsigned i = 0; i < this->length; i++) {
1478 bool field_row_major = row_major;
1479 const enum glsl_matrix_layout matrix_layout =
1480 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1481 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1482 field_row_major = true;
1483 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1484 field_row_major = false;
1485 }
1486
1487 const struct glsl_type *field_type = this->fields.structure[i].type;
1488 base_alignment = MAX2(base_alignment,
1489 field_type->std140_base_alignment(field_row_major));
1490 }
1491 return base_alignment;
1492 }
1493
1494 assert(!"not reached");
1495 return -1;
1496 }
1497
1498 unsigned
1499 glsl_type::std140_size(bool row_major) const
1500 {
1501 unsigned N = is_double() ? 8 : 4;
1502
1503 /* (1) If the member is a scalar consuming <N> basic machine units, the
1504 * base alignment is <N>.
1505 *
1506 * (2) If the member is a two- or four-component vector with components
1507 * consuming <N> basic machine units, the base alignment is 2<N> or
1508 * 4<N>, respectively.
1509 *
1510 * (3) If the member is a three-component vector with components consuming
1511 * <N> basic machine units, the base alignment is 4<N>.
1512 */
1513 if (this->is_scalar() || this->is_vector()) {
1514 return this->vector_elements * N;
1515 }
1516
1517 /* (5) If the member is a column-major matrix with <C> columns and
1518 * <R> rows, the matrix is stored identically to an array of
1519 * <C> column vectors with <R> components each, according to
1520 * rule (4).
1521 *
1522 * (6) If the member is an array of <S> column-major matrices with <C>
1523 * columns and <R> rows, the matrix is stored identically to a row of
1524 * <S>*<C> column vectors with <R> components each, according to rule
1525 * (4).
1526 *
1527 * (7) If the member is a row-major matrix with <C> columns and <R>
1528 * rows, the matrix is stored identically to an array of <R>
1529 * row vectors with <C> components each, according to rule (4).
1530 *
1531 * (8) If the member is an array of <S> row-major matrices with <C> columns
1532 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1533 * row vectors with <C> components each, according to rule (4).
1534 */
1535 if (this->without_array()->is_matrix()) {
1536 const struct glsl_type *element_type;
1537 const struct glsl_type *vec_type;
1538 unsigned int array_len;
1539
1540 if (this->is_array()) {
1541 element_type = this->without_array();
1542 array_len = this->arrays_of_arrays_size();
1543 } else {
1544 element_type = this;
1545 array_len = 1;
1546 }
1547
1548 if (row_major) {
1549 vec_type = get_instance(element_type->base_type,
1550 element_type->matrix_columns, 1);
1551
1552 array_len *= element_type->vector_elements;
1553 } else {
1554 vec_type = get_instance(element_type->base_type,
1555 element_type->vector_elements, 1);
1556 array_len *= element_type->matrix_columns;
1557 }
1558 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
1559 array_len);
1560
1561 return array_type->std140_size(false);
1562 }
1563
1564 /* (4) If the member is an array of scalars or vectors, the base alignment
1565 * and array stride are set to match the base alignment of a single
1566 * array element, according to rules (1), (2), and (3), and rounded up
1567 * to the base alignment of a vec4. The array may have padding at the
1568 * end; the base offset of the member following the array is rounded up
1569 * to the next multiple of the base alignment.
1570 *
1571 * (10) If the member is an array of <S> structures, the <S> elements of
1572 * the array are laid out in order, according to rule (9).
1573 */
1574 if (this->is_array()) {
1575 if (this->without_array()->is_record()) {
1576 return this->arrays_of_arrays_size() *
1577 this->without_array()->std140_size(row_major);
1578 } else {
1579 unsigned element_base_align =
1580 this->without_array()->std140_base_alignment(row_major);
1581 return this->arrays_of_arrays_size() * MAX2(element_base_align, 16);
1582 }
1583 }
1584
1585 /* (9) If the member is a structure, the base alignment of the
1586 * structure is <N>, where <N> is the largest base alignment
1587 * value of any of its members, and rounded up to the base
1588 * alignment of a vec4. The individual members of this
1589 * sub-structure are then assigned offsets by applying this set
1590 * of rules recursively, where the base offset of the first
1591 * member of the sub-structure is equal to the aligned offset
1592 * of the structure. The structure may have padding at the end;
1593 * the base offset of the member following the sub-structure is
1594 * rounded up to the next multiple of the base alignment of the
1595 * structure.
1596 */
1597 if (this->is_record() || this->is_interface()) {
1598 unsigned size = 0;
1599 unsigned max_align = 0;
1600
1601 for (unsigned i = 0; i < this->length; i++) {
1602 bool field_row_major = row_major;
1603 const enum glsl_matrix_layout matrix_layout =
1604 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1605 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1606 field_row_major = true;
1607 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1608 field_row_major = false;
1609 }
1610
1611 const struct glsl_type *field_type = this->fields.structure[i].type;
1612 unsigned align = field_type->std140_base_alignment(field_row_major);
1613
1614 /* Ignore unsized arrays when calculating size */
1615 if (field_type->is_unsized_array())
1616 continue;
1617
1618 size = glsl_align(size, align);
1619 size += field_type->std140_size(field_row_major);
1620
1621 max_align = MAX2(align, max_align);
1622
1623 if (field_type->is_record() && (i + 1 < this->length))
1624 size = glsl_align(size, 16);
1625 }
1626 size = glsl_align(size, MAX2(max_align, 16));
1627 return size;
1628 }
1629
1630 assert(!"not reached");
1631 return -1;
1632 }
1633
1634 unsigned
1635 glsl_type::std430_base_alignment(bool row_major) const
1636 {
1637
1638 unsigned N = is_double() ? 8 : 4;
1639
1640 /* (1) If the member is a scalar consuming <N> basic machine units, the
1641 * base alignment is <N>.
1642 *
1643 * (2) If the member is a two- or four-component vector with components
1644 * consuming <N> basic machine units, the base alignment is 2<N> or
1645 * 4<N>, respectively.
1646 *
1647 * (3) If the member is a three-component vector with components consuming
1648 * <N> basic machine units, the base alignment is 4<N>.
1649 */
1650 if (this->is_scalar() || this->is_vector()) {
1651 switch (this->vector_elements) {
1652 case 1:
1653 return N;
1654 case 2:
1655 return 2 * N;
1656 case 3:
1657 case 4:
1658 return 4 * N;
1659 }
1660 }
1661
1662 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1663 *
1664 * "When using the std430 storage layout, shader storage blocks will be
1665 * laid out in buffer storage identically to uniform and shader storage
1666 * blocks using the std140 layout, except that the base alignment and
1667 * stride of arrays of scalars and vectors in rule 4 and of structures
1668 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1669 */
1670
1671 /* (1) If the member is a scalar consuming <N> basic machine units, the
1672 * base alignment is <N>.
1673 *
1674 * (2) If the member is a two- or four-component vector with components
1675 * consuming <N> basic machine units, the base alignment is 2<N> or
1676 * 4<N>, respectively.
1677 *
1678 * (3) If the member is a three-component vector with components consuming
1679 * <N> basic machine units, the base alignment is 4<N>.
1680 */
1681 if (this->is_array())
1682 return this->fields.array->std430_base_alignment(row_major);
1683
1684 /* (5) If the member is a column-major matrix with <C> columns and
1685 * <R> rows, the matrix is stored identically to an array of
1686 * <C> column vectors with <R> components each, according to
1687 * rule (4).
1688 *
1689 * (7) If the member is a row-major matrix with <C> columns and <R>
1690 * rows, the matrix is stored identically to an array of <R>
1691 * row vectors with <C> components each, according to rule (4).
1692 */
1693 if (this->is_matrix()) {
1694 const struct glsl_type *vec_type, *array_type;
1695 int c = this->matrix_columns;
1696 int r = this->vector_elements;
1697
1698 if (row_major) {
1699 vec_type = get_instance(base_type, c, 1);
1700 array_type = glsl_type::get_array_instance(vec_type, r);
1701 } else {
1702 vec_type = get_instance(base_type, r, 1);
1703 array_type = glsl_type::get_array_instance(vec_type, c);
1704 }
1705
1706 return array_type->std430_base_alignment(false);
1707 }
1708
1709 /* (9) If the member is a structure, the base alignment of the
1710 * structure is <N>, where <N> is the largest base alignment
1711 * value of any of its members, and rounded up to the base
1712 * alignment of a vec4. The individual members of this
1713 * sub-structure are then assigned offsets by applying this set
1714 * of rules recursively, where the base offset of the first
1715 * member of the sub-structure is equal to the aligned offset
1716 * of the structure. The structure may have padding at the end;
1717 * the base offset of the member following the sub-structure is
1718 * rounded up to the next multiple of the base alignment of the
1719 * structure.
1720 */
1721 if (this->is_record()) {
1722 unsigned base_alignment = 0;
1723 for (unsigned i = 0; i < this->length; i++) {
1724 bool field_row_major = row_major;
1725 const enum glsl_matrix_layout matrix_layout =
1726 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1727 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1728 field_row_major = true;
1729 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1730 field_row_major = false;
1731 }
1732
1733 const struct glsl_type *field_type = this->fields.structure[i].type;
1734 base_alignment = MAX2(base_alignment,
1735 field_type->std430_base_alignment(field_row_major));
1736 }
1737 assert(base_alignment > 0);
1738 return base_alignment;
1739 }
1740 assert(!"not reached");
1741 return -1;
1742 }
1743
1744 unsigned
1745 glsl_type::std430_array_stride(bool row_major) const
1746 {
1747 unsigned N = is_double() ? 8 : 4;
1748
1749 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1750 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1751 *
1752 * (3) If the member is a three-component vector with components consuming
1753 * <N> basic machine units, the base alignment is 4<N>.
1754 */
1755 if (this->is_vector() && this->vector_elements == 3)
1756 return 4 * N;
1757
1758 /* By default use std430_size(row_major) */
1759 return this->std430_size(row_major);
1760 }
1761
1762 unsigned
1763 glsl_type::std430_size(bool row_major) const
1764 {
1765 unsigned N = is_double() ? 8 : 4;
1766
1767 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1768 *
1769 * "When using the std430 storage layout, shader storage blocks will be
1770 * laid out in buffer storage identically to uniform and shader storage
1771 * blocks using the std140 layout, except that the base alignment and
1772 * stride of arrays of scalars and vectors in rule 4 and of structures
1773 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1774 */
1775 if (this->is_scalar() || this->is_vector())
1776 return this->vector_elements * N;
1777
1778 if (this->without_array()->is_matrix()) {
1779 const struct glsl_type *element_type;
1780 const struct glsl_type *vec_type;
1781 unsigned int array_len;
1782
1783 if (this->is_array()) {
1784 element_type = this->without_array();
1785 array_len = this->arrays_of_arrays_size();
1786 } else {
1787 element_type = this;
1788 array_len = 1;
1789 }
1790
1791 if (row_major) {
1792 vec_type = get_instance(element_type->base_type,
1793 element_type->matrix_columns, 1);
1794
1795 array_len *= element_type->vector_elements;
1796 } else {
1797 vec_type = get_instance(element_type->base_type,
1798 element_type->vector_elements, 1);
1799 array_len *= element_type->matrix_columns;
1800 }
1801 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
1802 array_len);
1803
1804 return array_type->std430_size(false);
1805 }
1806
1807 if (this->is_array()) {
1808 if (this->without_array()->is_record())
1809 return this->arrays_of_arrays_size() *
1810 this->without_array()->std430_size(row_major);
1811 else
1812 return this->arrays_of_arrays_size() *
1813 this->without_array()->std430_base_alignment(row_major);
1814 }
1815
1816 if (this->is_record() || this->is_interface()) {
1817 unsigned size = 0;
1818 unsigned max_align = 0;
1819
1820 for (unsigned i = 0; i < this->length; i++) {
1821 bool field_row_major = row_major;
1822 const enum glsl_matrix_layout matrix_layout =
1823 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1824 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1825 field_row_major = true;
1826 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1827 field_row_major = false;
1828 }
1829
1830 const struct glsl_type *field_type = this->fields.structure[i].type;
1831 unsigned align = field_type->std430_base_alignment(field_row_major);
1832 size = glsl_align(size, align);
1833 size += field_type->std430_size(field_row_major);
1834
1835 max_align = MAX2(align, max_align);
1836 }
1837 size = glsl_align(size, max_align);
1838 return size;
1839 }
1840
1841 assert(!"not reached");
1842 return -1;
1843 }
1844
1845 unsigned
1846 glsl_type::count_attribute_slots(bool vertex_input_slots) const
1847 {
1848 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1849 *
1850 * "A scalar input counts the same amount against this limit as a vec4,
1851 * so applications may want to consider packing groups of four
1852 * unrelated float inputs together into a vector to better utilize the
1853 * capabilities of the underlying hardware. A matrix input will use up
1854 * multiple locations. The number of locations used will equal the
1855 * number of columns in the matrix."
1856 *
1857 * The spec does not explicitly say how arrays are counted. However, it
1858 * should be safe to assume the total number of slots consumed by an array
1859 * is the number of entries in the array multiplied by the number of slots
1860 * consumed by a single element of the array.
1861 *
1862 * The spec says nothing about how structs are counted, because vertex
1863 * attributes are not allowed to be (or contain) structs. However, Mesa
1864 * allows varying structs, the number of varying slots taken up by a
1865 * varying struct is simply equal to the sum of the number of slots taken
1866 * up by each element.
1867 *
1868 * Doubles are counted different depending on whether they are vertex
1869 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
1870 * take one location no matter what size they are, otherwise dvec3/4
1871 * take two locations.
1872 */
1873 switch (this->base_type) {
1874 case GLSL_TYPE_UINT:
1875 case GLSL_TYPE_INT:
1876 case GLSL_TYPE_FLOAT:
1877 case GLSL_TYPE_BOOL:
1878 return this->matrix_columns;
1879 case GLSL_TYPE_DOUBLE:
1880 if (this->vector_elements > 2 && !vertex_input_slots)
1881 return this->matrix_columns * 2;
1882 else
1883 return this->matrix_columns;
1884 case GLSL_TYPE_STRUCT:
1885 case GLSL_TYPE_INTERFACE: {
1886 unsigned size = 0;
1887
1888 for (unsigned i = 0; i < this->length; i++)
1889 size += this->fields.structure[i].type->count_attribute_slots(vertex_input_slots);
1890
1891 return size;
1892 }
1893
1894 case GLSL_TYPE_ARRAY:
1895 return this->length * this->fields.array->count_attribute_slots(vertex_input_slots);
1896
1897 case GLSL_TYPE_FUNCTION:
1898 case GLSL_TYPE_SAMPLER:
1899 case GLSL_TYPE_IMAGE:
1900 case GLSL_TYPE_ATOMIC_UINT:
1901 case GLSL_TYPE_VOID:
1902 case GLSL_TYPE_SUBROUTINE:
1903 case GLSL_TYPE_ERROR:
1904 break;
1905 }
1906
1907 assert(!"Unexpected type in count_attribute_slots()");
1908
1909 return 0;
1910 }
1911
1912 int
1913 glsl_type::coordinate_components() const
1914 {
1915 int size;
1916
1917 switch (sampler_dimensionality) {
1918 case GLSL_SAMPLER_DIM_1D:
1919 case GLSL_SAMPLER_DIM_BUF:
1920 size = 1;
1921 break;
1922 case GLSL_SAMPLER_DIM_2D:
1923 case GLSL_SAMPLER_DIM_RECT:
1924 case GLSL_SAMPLER_DIM_MS:
1925 case GLSL_SAMPLER_DIM_EXTERNAL:
1926 size = 2;
1927 break;
1928 case GLSL_SAMPLER_DIM_3D:
1929 case GLSL_SAMPLER_DIM_CUBE:
1930 size = 3;
1931 break;
1932 default:
1933 assert(!"Should not get here.");
1934 size = 1;
1935 break;
1936 }
1937
1938 /* Array textures need an additional component for the array index, except
1939 * for cubemap array images that behave like a 2D array of interleaved
1940 * cubemap faces.
1941 */
1942 if (sampler_array &&
1943 !(base_type == GLSL_TYPE_IMAGE &&
1944 sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE))
1945 size += 1;
1946
1947 return size;
1948 }
1949
1950 /**
1951 * Declarations of type flyweights (glsl_type::_foo_type) and
1952 * convenience pointers (glsl_type::foo_type).
1953 * @{
1954 */
1955 #define DECL_TYPE(NAME, ...) \
1956 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
1957 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
1958
1959 #define STRUCT_TYPE(NAME)
1960
1961 #include "compiler/builtin_type_macros.h"
1962 /** @} */