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