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