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