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