glsl: add subpass image type (v2)
[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 case GLSL_SAMPLER_DIM_SUBPASS:
678 return error_type;
679 }
680 case GLSL_TYPE_INT:
681 if (shadow)
682 return error_type;
683 switch (dim) {
684 case GLSL_SAMPLER_DIM_1D:
685 return (array ? isampler1DArray_type : isampler1D_type);
686 case GLSL_SAMPLER_DIM_2D:
687 return (array ? isampler2DArray_type : isampler2D_type);
688 case GLSL_SAMPLER_DIM_3D:
689 if (array)
690 return error_type;
691 return isampler3D_type;
692 case GLSL_SAMPLER_DIM_CUBE:
693 return (array ? isamplerCubeArray_type : isamplerCube_type);
694 case GLSL_SAMPLER_DIM_RECT:
695 if (array)
696 return error_type;
697 return isampler2DRect_type;
698 case GLSL_SAMPLER_DIM_BUF:
699 if (array)
700 return error_type;
701 return isamplerBuffer_type;
702 case GLSL_SAMPLER_DIM_MS:
703 return (array ? isampler2DMSArray_type : isampler2DMS_type);
704 case GLSL_SAMPLER_DIM_EXTERNAL:
705 return error_type;
706 case GLSL_SAMPLER_DIM_SUBPASS:
707 return error_type;
708 }
709 case GLSL_TYPE_UINT:
710 if (shadow)
711 return error_type;
712 switch (dim) {
713 case GLSL_SAMPLER_DIM_1D:
714 return (array ? usampler1DArray_type : usampler1D_type);
715 case GLSL_SAMPLER_DIM_2D:
716 return (array ? usampler2DArray_type : usampler2D_type);
717 case GLSL_SAMPLER_DIM_3D:
718 if (array)
719 return error_type;
720 return usampler3D_type;
721 case GLSL_SAMPLER_DIM_CUBE:
722 return (array ? usamplerCubeArray_type : usamplerCube_type);
723 case GLSL_SAMPLER_DIM_RECT:
724 if (array)
725 return error_type;
726 return usampler2DRect_type;
727 case GLSL_SAMPLER_DIM_BUF:
728 if (array)
729 return error_type;
730 return usamplerBuffer_type;
731 case GLSL_SAMPLER_DIM_MS:
732 return (array ? usampler2DMSArray_type : usampler2DMS_type);
733 case GLSL_SAMPLER_DIM_EXTERNAL:
734 return error_type;
735 case GLSL_SAMPLER_DIM_SUBPASS:
736 return error_type;
737 }
738 default:
739 return error_type;
740 }
741
742 unreachable("switch statement above should be complete");
743 }
744
745 const glsl_type *
746 glsl_type::get_image_instance(enum glsl_sampler_dim dim,
747 bool array, glsl_base_type type)
748 {
749 if (dim == GLSL_SAMPLER_DIM_SUBPASS)
750 return subpassInput_type;
751 switch (type) {
752 case GLSL_TYPE_FLOAT:
753 switch (dim) {
754 case GLSL_SAMPLER_DIM_1D:
755 return (array ? image1DArray_type : image1D_type);
756 case GLSL_SAMPLER_DIM_2D:
757 return (array ? image2DArray_type : image2D_type);
758 case GLSL_SAMPLER_DIM_3D:
759 return image3D_type;
760 case GLSL_SAMPLER_DIM_CUBE:
761 return (array ? imageCubeArray_type : imageCube_type);
762 case GLSL_SAMPLER_DIM_RECT:
763 if (array)
764 return error_type;
765 else
766 return image2DRect_type;
767 case GLSL_SAMPLER_DIM_BUF:
768 if (array)
769 return error_type;
770 else
771 return imageBuffer_type;
772 case GLSL_SAMPLER_DIM_MS:
773 return (array ? image2DMSArray_type : image2DMS_type);
774 case GLSL_SAMPLER_DIM_EXTERNAL:
775 case GLSL_SAMPLER_DIM_SUBPASS:
776 return error_type;
777 }
778 case GLSL_TYPE_INT:
779 switch (dim) {
780 case GLSL_SAMPLER_DIM_1D:
781 return (array ? iimage1DArray_type : iimage1D_type);
782 case GLSL_SAMPLER_DIM_2D:
783 return (array ? iimage2DArray_type : iimage2D_type);
784 case GLSL_SAMPLER_DIM_3D:
785 if (array)
786 return error_type;
787 return iimage3D_type;
788 case GLSL_SAMPLER_DIM_CUBE:
789 return (array ? iimageCubeArray_type : iimageCube_type);
790 case GLSL_SAMPLER_DIM_RECT:
791 if (array)
792 return error_type;
793 return iimage2DRect_type;
794 case GLSL_SAMPLER_DIM_BUF:
795 if (array)
796 return error_type;
797 return iimageBuffer_type;
798 case GLSL_SAMPLER_DIM_MS:
799 return (array ? iimage2DMSArray_type : iimage2DMS_type);
800 case GLSL_SAMPLER_DIM_EXTERNAL:
801 case GLSL_SAMPLER_DIM_SUBPASS:
802 return error_type;
803 }
804 case GLSL_TYPE_UINT:
805 switch (dim) {
806 case GLSL_SAMPLER_DIM_1D:
807 return (array ? uimage1DArray_type : uimage1D_type);
808 case GLSL_SAMPLER_DIM_2D:
809 return (array ? uimage2DArray_type : uimage2D_type);
810 case GLSL_SAMPLER_DIM_3D:
811 if (array)
812 return error_type;
813 return uimage3D_type;
814 case GLSL_SAMPLER_DIM_CUBE:
815 return (array ? uimageCubeArray_type : uimageCube_type);
816 case GLSL_SAMPLER_DIM_RECT:
817 if (array)
818 return error_type;
819 return uimage2DRect_type;
820 case GLSL_SAMPLER_DIM_BUF:
821 if (array)
822 return error_type;
823 return uimageBuffer_type;
824 case GLSL_SAMPLER_DIM_MS:
825 return (array ? uimage2DMSArray_type : uimage2DMS_type);
826 case GLSL_SAMPLER_DIM_EXTERNAL:
827 case GLSL_SAMPLER_DIM_SUBPASS:
828 return error_type;
829 }
830 default:
831 return error_type;
832 }
833
834 unreachable("switch statement above should be complete");
835 }
836
837 const glsl_type *
838 glsl_type::get_array_instance(const glsl_type *base, unsigned array_size)
839 {
840 /* Generate a name using the base type pointer in the key. This is
841 * done because the name of the base type may not be unique across
842 * shaders. For example, two shaders may have different record types
843 * named 'foo'.
844 */
845 char key[128];
846 snprintf(key, sizeof(key), "%p[%u]", (void *) base, array_size);
847
848 mtx_lock(&glsl_type::mutex);
849
850 if (array_types == NULL) {
851 array_types = _mesa_hash_table_create(NULL, _mesa_key_hash_string,
852 _mesa_key_string_equal);
853 }
854
855 const struct hash_entry *entry = _mesa_hash_table_search(array_types, key);
856 if (entry == NULL) {
857 mtx_unlock(&glsl_type::mutex);
858 const glsl_type *t = new glsl_type(base, array_size);
859 mtx_lock(&glsl_type::mutex);
860
861 entry = _mesa_hash_table_insert(array_types,
862 ralloc_strdup(mem_ctx, key),
863 (void *) t);
864 }
865
866 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_ARRAY);
867 assert(((glsl_type *) entry->data)->length == array_size);
868 assert(((glsl_type *) entry->data)->fields.array == base);
869
870 mtx_unlock(&glsl_type::mutex);
871
872 return (glsl_type *) entry->data;
873 }
874
875
876 bool
877 glsl_type::record_compare(const glsl_type *b, bool match_locations) const
878 {
879 if (this->length != b->length)
880 return false;
881
882 if (this->interface_packing != b->interface_packing)
883 return false;
884
885 /* From the GLSL 4.20 specification (Sec 4.2):
886 *
887 * "Structures must have the same name, sequence of type names, and
888 * type definitions, and field names to be considered the same type."
889 *
890 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
891 *
892 * Note that we cannot force type name check when comparing unnamed
893 * structure types, these have a unique name assigned during parsing.
894 */
895 if (!this->is_anonymous() && !b->is_anonymous())
896 if (strcmp(this->name, b->name) != 0)
897 return false;
898
899 for (unsigned i = 0; i < this->length; i++) {
900 if (this->fields.structure[i].type != b->fields.structure[i].type)
901 return false;
902 if (strcmp(this->fields.structure[i].name,
903 b->fields.structure[i].name) != 0)
904 return false;
905 if (this->fields.structure[i].matrix_layout
906 != b->fields.structure[i].matrix_layout)
907 return false;
908 if (match_locations && this->fields.structure[i].location
909 != b->fields.structure[i].location)
910 return false;
911 if (this->fields.structure[i].offset
912 != b->fields.structure[i].offset)
913 return false;
914 if (this->fields.structure[i].interpolation
915 != b->fields.structure[i].interpolation)
916 return false;
917 if (this->fields.structure[i].centroid
918 != b->fields.structure[i].centroid)
919 return false;
920 if (this->fields.structure[i].sample
921 != b->fields.structure[i].sample)
922 return false;
923 if (this->fields.structure[i].patch
924 != b->fields.structure[i].patch)
925 return false;
926 if (this->fields.structure[i].image_read_only
927 != b->fields.structure[i].image_read_only)
928 return false;
929 if (this->fields.structure[i].image_write_only
930 != b->fields.structure[i].image_write_only)
931 return false;
932 if (this->fields.structure[i].image_coherent
933 != b->fields.structure[i].image_coherent)
934 return false;
935 if (this->fields.structure[i].image_volatile
936 != b->fields.structure[i].image_volatile)
937 return false;
938 if (this->fields.structure[i].image_restrict
939 != b->fields.structure[i].image_restrict)
940 return false;
941 if (this->fields.structure[i].precision
942 != b->fields.structure[i].precision)
943 return false;
944 if (this->fields.structure[i].explicit_xfb_buffer
945 != b->fields.structure[i].explicit_xfb_buffer)
946 return false;
947 if (this->fields.structure[i].xfb_buffer
948 != b->fields.structure[i].xfb_buffer)
949 return false;
950 if (this->fields.structure[i].xfb_stride
951 != b->fields.structure[i].xfb_stride)
952 return false;
953 }
954
955 return true;
956 }
957
958
959 bool
960 glsl_type::record_key_compare(const void *a, const void *b)
961 {
962 const glsl_type *const key1 = (glsl_type *) a;
963 const glsl_type *const key2 = (glsl_type *) b;
964
965 return strcmp(key1->name, key2->name) == 0 && key1->record_compare(key2);
966 }
967
968
969 /**
970 * Generate an integer hash value for a glsl_type structure type.
971 */
972 unsigned
973 glsl_type::record_key_hash(const void *a)
974 {
975 const glsl_type *const key = (glsl_type *) a;
976 uintptr_t hash = key->length;
977 unsigned retval;
978
979 for (unsigned i = 0; i < key->length; i++) {
980 /* casting pointer to uintptr_t */
981 hash = (hash * 13 ) + (uintptr_t) key->fields.structure[i].type;
982 }
983
984 if (sizeof(hash) == 8)
985 retval = (hash & 0xffffffff) ^ ((uint64_t) hash >> 32);
986 else
987 retval = hash;
988
989 return retval;
990 }
991
992
993 const glsl_type *
994 glsl_type::get_record_instance(const glsl_struct_field *fields,
995 unsigned num_fields,
996 const char *name)
997 {
998 const glsl_type key(fields, num_fields, name);
999
1000 mtx_lock(&glsl_type::mutex);
1001
1002 if (record_types == NULL) {
1003 record_types = _mesa_hash_table_create(NULL, record_key_hash,
1004 record_key_compare);
1005 }
1006
1007 const struct hash_entry *entry = _mesa_hash_table_search(record_types,
1008 &key);
1009 if (entry == NULL) {
1010 mtx_unlock(&glsl_type::mutex);
1011 const glsl_type *t = new glsl_type(fields, num_fields, name);
1012 mtx_lock(&glsl_type::mutex);
1013
1014 entry = _mesa_hash_table_insert(record_types, t, (void *) t);
1015 }
1016
1017 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_STRUCT);
1018 assert(((glsl_type *) entry->data)->length == num_fields);
1019 assert(strcmp(((glsl_type *) entry->data)->name, name) == 0);
1020
1021 mtx_unlock(&glsl_type::mutex);
1022
1023 return (glsl_type *) entry->data;
1024 }
1025
1026
1027 const glsl_type *
1028 glsl_type::get_interface_instance(const glsl_struct_field *fields,
1029 unsigned num_fields,
1030 enum glsl_interface_packing packing,
1031 const char *block_name)
1032 {
1033 const glsl_type key(fields, num_fields, packing, block_name);
1034
1035 mtx_lock(&glsl_type::mutex);
1036
1037 if (interface_types == NULL) {
1038 interface_types = _mesa_hash_table_create(NULL, record_key_hash,
1039 record_key_compare);
1040 }
1041
1042 const struct hash_entry *entry = _mesa_hash_table_search(interface_types,
1043 &key);
1044 if (entry == NULL) {
1045 mtx_unlock(&glsl_type::mutex);
1046 const glsl_type *t = new glsl_type(fields, num_fields,
1047 packing, block_name);
1048 mtx_lock(&glsl_type::mutex);
1049
1050 entry = _mesa_hash_table_insert(interface_types, t, (void *) t);
1051 }
1052
1053 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_INTERFACE);
1054 assert(((glsl_type *) entry->data)->length == num_fields);
1055 assert(strcmp(((glsl_type *) entry->data)->name, block_name) == 0);
1056
1057 mtx_unlock(&glsl_type::mutex);
1058
1059 return (glsl_type *) entry->data;
1060 }
1061
1062 const glsl_type *
1063 glsl_type::get_subroutine_instance(const char *subroutine_name)
1064 {
1065 const glsl_type key(subroutine_name);
1066
1067 mtx_lock(&glsl_type::mutex);
1068
1069 if (subroutine_types == NULL) {
1070 subroutine_types = _mesa_hash_table_create(NULL, record_key_hash,
1071 record_key_compare);
1072 }
1073
1074 const struct hash_entry *entry = _mesa_hash_table_search(subroutine_types,
1075 &key);
1076 if (entry == NULL) {
1077 mtx_unlock(&glsl_type::mutex);
1078 const glsl_type *t = new glsl_type(subroutine_name);
1079 mtx_lock(&glsl_type::mutex);
1080
1081 entry = _mesa_hash_table_insert(subroutine_types, t, (void *) t);
1082 }
1083
1084 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_SUBROUTINE);
1085 assert(strcmp(((glsl_type *) entry->data)->name, subroutine_name) == 0);
1086
1087 mtx_unlock(&glsl_type::mutex);
1088
1089 return (glsl_type *) entry->data;
1090 }
1091
1092
1093 static bool
1094 function_key_compare(const void *a, const void *b)
1095 {
1096 const glsl_type *const key1 = (glsl_type *) a;
1097 const glsl_type *const key2 = (glsl_type *) b;
1098
1099 if (key1->length != key2->length)
1100 return false;
1101
1102 return memcmp(key1->fields.parameters, key2->fields.parameters,
1103 (key1->length + 1) * sizeof(*key1->fields.parameters)) == 0;
1104 }
1105
1106
1107 static uint32_t
1108 function_key_hash(const void *a)
1109 {
1110 const glsl_type *const key = (glsl_type *) a;
1111 return _mesa_hash_data(key->fields.parameters,
1112 (key->length + 1) * sizeof(*key->fields.parameters));
1113 }
1114
1115 const glsl_type *
1116 glsl_type::get_function_instance(const glsl_type *return_type,
1117 const glsl_function_param *params,
1118 unsigned num_params)
1119 {
1120 const glsl_type key(return_type, params, num_params);
1121
1122 mtx_lock(&glsl_type::mutex);
1123
1124 if (function_types == NULL) {
1125 function_types = _mesa_hash_table_create(NULL, function_key_hash,
1126 function_key_compare);
1127 }
1128
1129 struct hash_entry *entry = _mesa_hash_table_search(function_types, &key);
1130 if (entry == NULL) {
1131 mtx_unlock(&glsl_type::mutex);
1132 const glsl_type *t = new glsl_type(return_type, params, num_params);
1133 mtx_lock(&glsl_type::mutex);
1134
1135 entry = _mesa_hash_table_insert(function_types, t, (void *) t);
1136 }
1137
1138 const glsl_type *t = (const glsl_type *)entry->data;
1139
1140 assert(t->base_type == GLSL_TYPE_FUNCTION);
1141 assert(t->length == num_params);
1142
1143 mtx_unlock(&glsl_type::mutex);
1144
1145 return t;
1146 }
1147
1148
1149 const glsl_type *
1150 glsl_type::get_mul_type(const glsl_type *type_a, const glsl_type *type_b)
1151 {
1152 if (type_a == type_b) {
1153 return type_a;
1154 } else if (type_a->is_matrix() && type_b->is_matrix()) {
1155 /* Matrix multiply. The columns of A must match the rows of B. Given
1156 * the other previously tested constraints, this means the vector type
1157 * of a row from A must be the same as the vector type of a column from
1158 * B.
1159 */
1160 if (type_a->row_type() == type_b->column_type()) {
1161 /* The resulting matrix has the number of columns of matrix B and
1162 * the number of rows of matrix A. We get the row count of A by
1163 * looking at the size of a vector that makes up a column. The
1164 * transpose (size of a row) is done for B.
1165 */
1166 const glsl_type *const type =
1167 get_instance(type_a->base_type,
1168 type_a->column_type()->vector_elements,
1169 type_b->row_type()->vector_elements);
1170 assert(type != error_type);
1171
1172 return type;
1173 }
1174 } else if (type_a->is_matrix()) {
1175 /* A is a matrix and B is a column vector. Columns of A must match
1176 * rows of B. Given the other previously tested constraints, this
1177 * means the vector type of a row from A must be the same as the
1178 * vector the type of B.
1179 */
1180 if (type_a->row_type() == type_b) {
1181 /* The resulting vector has a number of elements equal to
1182 * the number of rows of matrix A. */
1183 const glsl_type *const type =
1184 get_instance(type_a->base_type,
1185 type_a->column_type()->vector_elements,
1186 1);
1187 assert(type != error_type);
1188
1189 return type;
1190 }
1191 } else {
1192 assert(type_b->is_matrix());
1193
1194 /* A is a row vector and B is a matrix. Columns of A must match rows
1195 * of B. Given the other previously tested constraints, this means
1196 * the type of A must be the same as the vector type of a column from
1197 * B.
1198 */
1199 if (type_a == type_b->column_type()) {
1200 /* The resulting vector has a number of elements equal to
1201 * the number of columns of matrix B. */
1202 const glsl_type *const type =
1203 get_instance(type_a->base_type,
1204 type_b->row_type()->vector_elements,
1205 1);
1206 assert(type != error_type);
1207
1208 return type;
1209 }
1210 }
1211
1212 return error_type;
1213 }
1214
1215
1216 const glsl_type *
1217 glsl_type::field_type(const char *name) const
1218 {
1219 if (this->base_type != GLSL_TYPE_STRUCT
1220 && this->base_type != GLSL_TYPE_INTERFACE)
1221 return error_type;
1222
1223 for (unsigned i = 0; i < this->length; i++) {
1224 if (strcmp(name, this->fields.structure[i].name) == 0)
1225 return this->fields.structure[i].type;
1226 }
1227
1228 return error_type;
1229 }
1230
1231
1232 int
1233 glsl_type::field_index(const char *name) const
1234 {
1235 if (this->base_type != GLSL_TYPE_STRUCT
1236 && this->base_type != GLSL_TYPE_INTERFACE)
1237 return -1;
1238
1239 for (unsigned i = 0; i < this->length; i++) {
1240 if (strcmp(name, this->fields.structure[i].name) == 0)
1241 return i;
1242 }
1243
1244 return -1;
1245 }
1246
1247
1248 unsigned
1249 glsl_type::component_slots() const
1250 {
1251 switch (this->base_type) {
1252 case GLSL_TYPE_UINT:
1253 case GLSL_TYPE_INT:
1254 case GLSL_TYPE_FLOAT:
1255 case GLSL_TYPE_BOOL:
1256 return this->components();
1257
1258 case GLSL_TYPE_DOUBLE:
1259 return 2 * this->components();
1260
1261 case GLSL_TYPE_STRUCT:
1262 case GLSL_TYPE_INTERFACE: {
1263 unsigned size = 0;
1264
1265 for (unsigned i = 0; i < this->length; i++)
1266 size += this->fields.structure[i].type->component_slots();
1267
1268 return size;
1269 }
1270
1271 case GLSL_TYPE_ARRAY:
1272 return this->length * this->fields.array->component_slots();
1273
1274 case GLSL_TYPE_IMAGE:
1275 return 1;
1276 case GLSL_TYPE_SUBROUTINE:
1277 return 1;
1278
1279 case GLSL_TYPE_FUNCTION:
1280 case GLSL_TYPE_SAMPLER:
1281 case GLSL_TYPE_ATOMIC_UINT:
1282 case GLSL_TYPE_VOID:
1283 case GLSL_TYPE_ERROR:
1284 break;
1285 }
1286
1287 return 0;
1288 }
1289
1290 unsigned
1291 glsl_type::record_location_offset(unsigned length) const
1292 {
1293 unsigned offset = 0;
1294 const glsl_type *t = this->without_array();
1295 if (t->is_record()) {
1296 assert(length <= t->length);
1297
1298 for (unsigned i = 0; i < length; i++) {
1299 const glsl_type *st = t->fields.structure[i].type;
1300 const glsl_type *wa = st->without_array();
1301 if (wa->is_record()) {
1302 unsigned r_offset = wa->record_location_offset(wa->length);
1303 offset += st->is_array() ?
1304 st->arrays_of_arrays_size() * r_offset : r_offset;
1305 } else if (st->is_array() && st->fields.array->is_array()) {
1306 unsigned outer_array_size = st->length;
1307 const glsl_type *base_type = st->fields.array;
1308
1309 /* For arrays of arrays the outer arrays take up a uniform
1310 * slot for each element. The innermost array elements share a
1311 * single slot so we ignore the innermost array when calculating
1312 * the offset.
1313 */
1314 while (base_type->fields.array->is_array()) {
1315 outer_array_size = outer_array_size * base_type->length;
1316 base_type = base_type->fields.array;
1317 }
1318 offset += outer_array_size;
1319 } else {
1320 /* We dont worry about arrays here because unless the array
1321 * contains a structure or another array it only takes up a single
1322 * uniform slot.
1323 */
1324 offset += 1;
1325 }
1326 }
1327 }
1328 return offset;
1329 }
1330
1331 unsigned
1332 glsl_type::uniform_locations() const
1333 {
1334 unsigned size = 0;
1335
1336 switch (this->base_type) {
1337 case GLSL_TYPE_UINT:
1338 case GLSL_TYPE_INT:
1339 case GLSL_TYPE_FLOAT:
1340 case GLSL_TYPE_DOUBLE:
1341 case GLSL_TYPE_BOOL:
1342 case GLSL_TYPE_SAMPLER:
1343 case GLSL_TYPE_IMAGE:
1344 case GLSL_TYPE_SUBROUTINE:
1345 return 1;
1346
1347 case GLSL_TYPE_STRUCT:
1348 case GLSL_TYPE_INTERFACE:
1349 for (unsigned i = 0; i < this->length; i++)
1350 size += this->fields.structure[i].type->uniform_locations();
1351 return size;
1352 case GLSL_TYPE_ARRAY:
1353 return this->length * this->fields.array->uniform_locations();
1354 default:
1355 return 0;
1356 }
1357 }
1358
1359 unsigned
1360 glsl_type::varying_count() const
1361 {
1362 unsigned size = 0;
1363
1364 switch (this->base_type) {
1365 case GLSL_TYPE_UINT:
1366 case GLSL_TYPE_INT:
1367 case GLSL_TYPE_FLOAT:
1368 case GLSL_TYPE_DOUBLE:
1369 case GLSL_TYPE_BOOL:
1370 return 1;
1371
1372 case GLSL_TYPE_STRUCT:
1373 case GLSL_TYPE_INTERFACE:
1374 for (unsigned i = 0; i < this->length; i++)
1375 size += this->fields.structure[i].type->varying_count();
1376 return size;
1377 case GLSL_TYPE_ARRAY:
1378 /* Don't count innermost array elements */
1379 if (this->without_array()->is_record() ||
1380 this->without_array()->is_interface() ||
1381 this->fields.array->is_array())
1382 return this->length * this->fields.array->varying_count();
1383 else
1384 return this->fields.array->varying_count();
1385 default:
1386 assert(!"unsupported varying type");
1387 return 0;
1388 }
1389 }
1390
1391 bool
1392 glsl_type::can_implicitly_convert_to(const glsl_type *desired,
1393 _mesa_glsl_parse_state *state) const
1394 {
1395 if (this == desired)
1396 return true;
1397
1398 /* GLSL 1.10 and ESSL do not allow implicit conversions. If there is no
1399 * state, we're doing intra-stage function linking where these checks have
1400 * already been done.
1401 */
1402 if (state && (state->es_shader || !state->is_version(120, 0)))
1403 return false;
1404
1405 /* There is no conversion among matrix types. */
1406 if (this->matrix_columns > 1 || desired->matrix_columns > 1)
1407 return false;
1408
1409 /* Vector size must match. */
1410 if (this->vector_elements != desired->vector_elements)
1411 return false;
1412
1413 /* int and uint can be converted to float. */
1414 if (desired->is_float() && this->is_integer())
1415 return true;
1416
1417 /* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int
1418 * can be converted to uint. Note that state may be NULL here, when
1419 * resolving function calls in the linker. By this time, all the
1420 * state-dependent checks have already happened though, so allow anything
1421 * that's allowed in any shader version.
1422 */
1423 if ((!state || state->is_version(400, 0) || state->ARB_gpu_shader5_enable ||
1424 state->MESA_shader_integer_functions_enable) &&
1425 desired->base_type == GLSL_TYPE_UINT && this->base_type == GLSL_TYPE_INT)
1426 return true;
1427
1428 /* No implicit conversions from double. */
1429 if ((!state || state->has_double()) && this->is_double())
1430 return false;
1431
1432 /* Conversions from different types to double. */
1433 if ((!state || state->has_double()) && desired->is_double()) {
1434 if (this->is_float())
1435 return true;
1436 if (this->is_integer())
1437 return true;
1438 }
1439
1440 return false;
1441 }
1442
1443 unsigned
1444 glsl_type::std140_base_alignment(bool row_major) const
1445 {
1446 unsigned N = is_64bit() ? 8 : 4;
1447
1448 /* (1) If the member is a scalar consuming <N> basic machine units, the
1449 * base alignment is <N>.
1450 *
1451 * (2) If the member is a two- or four-component vector with components
1452 * consuming <N> basic machine units, the base alignment is 2<N> or
1453 * 4<N>, respectively.
1454 *
1455 * (3) If the member is a three-component vector with components consuming
1456 * <N> basic machine units, the base alignment is 4<N>.
1457 */
1458 if (this->is_scalar() || this->is_vector()) {
1459 switch (this->vector_elements) {
1460 case 1:
1461 return N;
1462 case 2:
1463 return 2 * N;
1464 case 3:
1465 case 4:
1466 return 4 * N;
1467 }
1468 }
1469
1470 /* (4) If the member is an array of scalars or vectors, the base alignment
1471 * and array stride are set to match the base alignment of a single
1472 * array element, according to rules (1), (2), and (3), and rounded up
1473 * to the base alignment of a vec4. The array may have padding at the
1474 * end; the base offset of the member following the array is rounded up
1475 * to the next multiple of the base alignment.
1476 *
1477 * (6) If the member is an array of <S> column-major matrices with <C>
1478 * columns and <R> rows, the matrix is stored identically to a row of
1479 * <S>*<C> column vectors with <R> components each, according to rule
1480 * (4).
1481 *
1482 * (8) If the member is an array of <S> row-major matrices with <C> columns
1483 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1484 * row vectors with <C> components each, according to rule (4).
1485 *
1486 * (10) If the member is an array of <S> structures, the <S> elements of
1487 * the array are laid out in order, according to rule (9).
1488 */
1489 if (this->is_array()) {
1490 if (this->fields.array->is_scalar() ||
1491 this->fields.array->is_vector() ||
1492 this->fields.array->is_matrix()) {
1493 return MAX2(this->fields.array->std140_base_alignment(row_major), 16);
1494 } else {
1495 assert(this->fields.array->is_record() ||
1496 this->fields.array->is_array());
1497 return this->fields.array->std140_base_alignment(row_major);
1498 }
1499 }
1500
1501 /* (5) If the member is a column-major matrix with <C> columns and
1502 * <R> rows, the matrix is stored identically to an array of
1503 * <C> column vectors with <R> components each, according to
1504 * rule (4).
1505 *
1506 * (7) If the member is a row-major matrix with <C> columns and <R>
1507 * rows, the matrix is stored identically to an array of <R>
1508 * row vectors with <C> components each, according to rule (4).
1509 */
1510 if (this->is_matrix()) {
1511 const struct glsl_type *vec_type, *array_type;
1512 int c = this->matrix_columns;
1513 int r = this->vector_elements;
1514
1515 if (row_major) {
1516 vec_type = get_instance(base_type, c, 1);
1517 array_type = glsl_type::get_array_instance(vec_type, r);
1518 } else {
1519 vec_type = get_instance(base_type, r, 1);
1520 array_type = glsl_type::get_array_instance(vec_type, c);
1521 }
1522
1523 return array_type->std140_base_alignment(false);
1524 }
1525
1526 /* (9) If the member is a structure, the base alignment of the
1527 * structure is <N>, where <N> is the largest base alignment
1528 * value of any of its members, and rounded up to the base
1529 * alignment of a vec4. The individual members of this
1530 * sub-structure are then assigned offsets by applying this set
1531 * of rules recursively, where the base offset of the first
1532 * member of the sub-structure is equal to the aligned offset
1533 * of the structure. The structure may have padding at the end;
1534 * the base offset of the member following the sub-structure is
1535 * rounded up to the next multiple of the base alignment of the
1536 * structure.
1537 */
1538 if (this->is_record()) {
1539 unsigned base_alignment = 16;
1540 for (unsigned i = 0; i < this->length; i++) {
1541 bool field_row_major = row_major;
1542 const enum glsl_matrix_layout matrix_layout =
1543 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1544 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1545 field_row_major = true;
1546 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1547 field_row_major = false;
1548 }
1549
1550 const struct glsl_type *field_type = this->fields.structure[i].type;
1551 base_alignment = MAX2(base_alignment,
1552 field_type->std140_base_alignment(field_row_major));
1553 }
1554 return base_alignment;
1555 }
1556
1557 assert(!"not reached");
1558 return -1;
1559 }
1560
1561 unsigned
1562 glsl_type::std140_size(bool row_major) const
1563 {
1564 unsigned N = is_64bit() ? 8 : 4;
1565
1566 /* (1) If the member is a scalar consuming <N> basic machine units, the
1567 * base alignment is <N>.
1568 *
1569 * (2) If the member is a two- or four-component vector with components
1570 * consuming <N> basic machine units, the base alignment is 2<N> or
1571 * 4<N>, respectively.
1572 *
1573 * (3) If the member is a three-component vector with components consuming
1574 * <N> basic machine units, the base alignment is 4<N>.
1575 */
1576 if (this->is_scalar() || this->is_vector()) {
1577 return this->vector_elements * N;
1578 }
1579
1580 /* (5) If the member is a column-major matrix with <C> columns and
1581 * <R> rows, the matrix is stored identically to an array of
1582 * <C> column vectors with <R> components each, according to
1583 * rule (4).
1584 *
1585 * (6) If the member is an array of <S> column-major matrices with <C>
1586 * columns and <R> rows, the matrix is stored identically to a row of
1587 * <S>*<C> column vectors with <R> components each, according to rule
1588 * (4).
1589 *
1590 * (7) If the member is a row-major matrix with <C> columns and <R>
1591 * rows, the matrix is stored identically to an array of <R>
1592 * row vectors with <C> components each, according to rule (4).
1593 *
1594 * (8) If the member is an array of <S> row-major matrices with <C> columns
1595 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1596 * row vectors with <C> components each, according to rule (4).
1597 */
1598 if (this->without_array()->is_matrix()) {
1599 const struct glsl_type *element_type;
1600 const struct glsl_type *vec_type;
1601 unsigned int array_len;
1602
1603 if (this->is_array()) {
1604 element_type = this->without_array();
1605 array_len = this->arrays_of_arrays_size();
1606 } else {
1607 element_type = this;
1608 array_len = 1;
1609 }
1610
1611 if (row_major) {
1612 vec_type = get_instance(element_type->base_type,
1613 element_type->matrix_columns, 1);
1614
1615 array_len *= element_type->vector_elements;
1616 } else {
1617 vec_type = get_instance(element_type->base_type,
1618 element_type->vector_elements, 1);
1619 array_len *= element_type->matrix_columns;
1620 }
1621 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
1622 array_len);
1623
1624 return array_type->std140_size(false);
1625 }
1626
1627 /* (4) If the member is an array of scalars or vectors, the base alignment
1628 * and array stride are set to match the base alignment of a single
1629 * array element, according to rules (1), (2), and (3), and rounded up
1630 * to the base alignment of a vec4. The array may have padding at the
1631 * end; the base offset of the member following the array is rounded up
1632 * to the next multiple of the base alignment.
1633 *
1634 * (10) If the member is an array of <S> structures, the <S> elements of
1635 * the array are laid out in order, according to rule (9).
1636 */
1637 if (this->is_array()) {
1638 if (this->without_array()->is_record()) {
1639 return this->arrays_of_arrays_size() *
1640 this->without_array()->std140_size(row_major);
1641 } else {
1642 unsigned element_base_align =
1643 this->without_array()->std140_base_alignment(row_major);
1644 return this->arrays_of_arrays_size() * MAX2(element_base_align, 16);
1645 }
1646 }
1647
1648 /* (9) If the member is a structure, the base alignment of the
1649 * structure is <N>, where <N> is the largest base alignment
1650 * value of any of its members, and rounded up to the base
1651 * alignment of a vec4. The individual members of this
1652 * sub-structure are then assigned offsets by applying this set
1653 * of rules recursively, where the base offset of the first
1654 * member of the sub-structure is equal to the aligned offset
1655 * of the structure. The structure may have padding at the end;
1656 * the base offset of the member following the sub-structure is
1657 * rounded up to the next multiple of the base alignment of the
1658 * structure.
1659 */
1660 if (this->is_record() || this->is_interface()) {
1661 unsigned size = 0;
1662 unsigned max_align = 0;
1663
1664 for (unsigned i = 0; i < this->length; i++) {
1665 bool field_row_major = row_major;
1666 const enum glsl_matrix_layout matrix_layout =
1667 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1668 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1669 field_row_major = true;
1670 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1671 field_row_major = false;
1672 }
1673
1674 const struct glsl_type *field_type = this->fields.structure[i].type;
1675 unsigned align = field_type->std140_base_alignment(field_row_major);
1676
1677 /* Ignore unsized arrays when calculating size */
1678 if (field_type->is_unsized_array())
1679 continue;
1680
1681 size = glsl_align(size, align);
1682 size += field_type->std140_size(field_row_major);
1683
1684 max_align = MAX2(align, max_align);
1685
1686 if (field_type->is_record() && (i + 1 < this->length))
1687 size = glsl_align(size, 16);
1688 }
1689 size = glsl_align(size, MAX2(max_align, 16));
1690 return size;
1691 }
1692
1693 assert(!"not reached");
1694 return -1;
1695 }
1696
1697 unsigned
1698 glsl_type::std430_base_alignment(bool row_major) const
1699 {
1700
1701 unsigned N = is_64bit() ? 8 : 4;
1702
1703 /* (1) If the member is a scalar consuming <N> basic machine units, the
1704 * base alignment is <N>.
1705 *
1706 * (2) If the member is a two- or four-component vector with components
1707 * consuming <N> basic machine units, the base alignment is 2<N> or
1708 * 4<N>, respectively.
1709 *
1710 * (3) If the member is a three-component vector with components consuming
1711 * <N> basic machine units, the base alignment is 4<N>.
1712 */
1713 if (this->is_scalar() || this->is_vector()) {
1714 switch (this->vector_elements) {
1715 case 1:
1716 return N;
1717 case 2:
1718 return 2 * N;
1719 case 3:
1720 case 4:
1721 return 4 * N;
1722 }
1723 }
1724
1725 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1726 *
1727 * "When using the std430 storage layout, shader storage blocks will be
1728 * laid out in buffer storage identically to uniform and shader storage
1729 * blocks using the std140 layout, except that the base alignment and
1730 * stride of arrays of scalars and vectors in rule 4 and of structures
1731 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1732 */
1733
1734 /* (1) If the member is a scalar consuming <N> basic machine units, the
1735 * base alignment is <N>.
1736 *
1737 * (2) If the member is a two- or four-component vector with components
1738 * consuming <N> basic machine units, the base alignment is 2<N> or
1739 * 4<N>, respectively.
1740 *
1741 * (3) If the member is a three-component vector with components consuming
1742 * <N> basic machine units, the base alignment is 4<N>.
1743 */
1744 if (this->is_array())
1745 return this->fields.array->std430_base_alignment(row_major);
1746
1747 /* (5) If the member is a column-major matrix with <C> columns and
1748 * <R> rows, the matrix is stored identically to an array of
1749 * <C> column vectors with <R> components each, according to
1750 * rule (4).
1751 *
1752 * (7) If the member is a row-major matrix with <C> columns and <R>
1753 * rows, the matrix is stored identically to an array of <R>
1754 * row vectors with <C> components each, according to rule (4).
1755 */
1756 if (this->is_matrix()) {
1757 const struct glsl_type *vec_type, *array_type;
1758 int c = this->matrix_columns;
1759 int r = this->vector_elements;
1760
1761 if (row_major) {
1762 vec_type = get_instance(base_type, c, 1);
1763 array_type = glsl_type::get_array_instance(vec_type, r);
1764 } else {
1765 vec_type = get_instance(base_type, r, 1);
1766 array_type = glsl_type::get_array_instance(vec_type, c);
1767 }
1768
1769 return array_type->std430_base_alignment(false);
1770 }
1771
1772 /* (9) If the member is a structure, the base alignment of the
1773 * structure is <N>, where <N> is the largest base alignment
1774 * value of any of its members, and rounded up to the base
1775 * alignment of a vec4. The individual members of this
1776 * sub-structure are then assigned offsets by applying this set
1777 * of rules recursively, where the base offset of the first
1778 * member of the sub-structure is equal to the aligned offset
1779 * of the structure. The structure may have padding at the end;
1780 * the base offset of the member following the sub-structure is
1781 * rounded up to the next multiple of the base alignment of the
1782 * structure.
1783 */
1784 if (this->is_record()) {
1785 unsigned base_alignment = 0;
1786 for (unsigned i = 0; i < this->length; i++) {
1787 bool field_row_major = row_major;
1788 const enum glsl_matrix_layout matrix_layout =
1789 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1790 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1791 field_row_major = true;
1792 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1793 field_row_major = false;
1794 }
1795
1796 const struct glsl_type *field_type = this->fields.structure[i].type;
1797 base_alignment = MAX2(base_alignment,
1798 field_type->std430_base_alignment(field_row_major));
1799 }
1800 assert(base_alignment > 0);
1801 return base_alignment;
1802 }
1803 assert(!"not reached");
1804 return -1;
1805 }
1806
1807 unsigned
1808 glsl_type::std430_array_stride(bool row_major) const
1809 {
1810 unsigned N = is_64bit() ? 8 : 4;
1811
1812 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1813 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1814 *
1815 * (3) If the member is a three-component vector with components consuming
1816 * <N> basic machine units, the base alignment is 4<N>.
1817 */
1818 if (this->is_vector() && this->vector_elements == 3)
1819 return 4 * N;
1820
1821 /* By default use std430_size(row_major) */
1822 return this->std430_size(row_major);
1823 }
1824
1825 unsigned
1826 glsl_type::std430_size(bool row_major) const
1827 {
1828 unsigned N = is_64bit() ? 8 : 4;
1829
1830 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1831 *
1832 * "When using the std430 storage layout, shader storage blocks will be
1833 * laid out in buffer storage identically to uniform and shader storage
1834 * blocks using the std140 layout, except that the base alignment and
1835 * stride of arrays of scalars and vectors in rule 4 and of structures
1836 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1837 */
1838 if (this->is_scalar() || this->is_vector())
1839 return this->vector_elements * N;
1840
1841 if (this->without_array()->is_matrix()) {
1842 const struct glsl_type *element_type;
1843 const struct glsl_type *vec_type;
1844 unsigned int array_len;
1845
1846 if (this->is_array()) {
1847 element_type = this->without_array();
1848 array_len = this->arrays_of_arrays_size();
1849 } else {
1850 element_type = this;
1851 array_len = 1;
1852 }
1853
1854 if (row_major) {
1855 vec_type = get_instance(element_type->base_type,
1856 element_type->matrix_columns, 1);
1857
1858 array_len *= element_type->vector_elements;
1859 } else {
1860 vec_type = get_instance(element_type->base_type,
1861 element_type->vector_elements, 1);
1862 array_len *= element_type->matrix_columns;
1863 }
1864 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
1865 array_len);
1866
1867 return array_type->std430_size(false);
1868 }
1869
1870 if (this->is_array()) {
1871 if (this->without_array()->is_record())
1872 return this->arrays_of_arrays_size() *
1873 this->without_array()->std430_size(row_major);
1874 else
1875 return this->arrays_of_arrays_size() *
1876 this->without_array()->std430_base_alignment(row_major);
1877 }
1878
1879 if (this->is_record() || this->is_interface()) {
1880 unsigned size = 0;
1881 unsigned max_align = 0;
1882
1883 for (unsigned i = 0; i < this->length; i++) {
1884 bool field_row_major = row_major;
1885 const enum glsl_matrix_layout matrix_layout =
1886 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1887 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1888 field_row_major = true;
1889 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1890 field_row_major = false;
1891 }
1892
1893 const struct glsl_type *field_type = this->fields.structure[i].type;
1894 unsigned align = field_type->std430_base_alignment(field_row_major);
1895 size = glsl_align(size, align);
1896 size += field_type->std430_size(field_row_major);
1897
1898 max_align = MAX2(align, max_align);
1899 }
1900 size = glsl_align(size, max_align);
1901 return size;
1902 }
1903
1904 assert(!"not reached");
1905 return -1;
1906 }
1907
1908 unsigned
1909 glsl_type::count_attribute_slots(bool is_vertex_input) const
1910 {
1911 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1912 *
1913 * "A scalar input counts the same amount against this limit as a vec4,
1914 * so applications may want to consider packing groups of four
1915 * unrelated float inputs together into a vector to better utilize the
1916 * capabilities of the underlying hardware. A matrix input will use up
1917 * multiple locations. The number of locations used will equal the
1918 * number of columns in the matrix."
1919 *
1920 * The spec does not explicitly say how arrays are counted. However, it
1921 * should be safe to assume the total number of slots consumed by an array
1922 * is the number of entries in the array multiplied by the number of slots
1923 * consumed by a single element of the array.
1924 *
1925 * The spec says nothing about how structs are counted, because vertex
1926 * attributes are not allowed to be (or contain) structs. However, Mesa
1927 * allows varying structs, the number of varying slots taken up by a
1928 * varying struct is simply equal to the sum of the number of slots taken
1929 * up by each element.
1930 *
1931 * Doubles are counted different depending on whether they are vertex
1932 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
1933 * take one location no matter what size they are, otherwise dvec3/4
1934 * take two locations.
1935 */
1936 switch (this->base_type) {
1937 case GLSL_TYPE_UINT:
1938 case GLSL_TYPE_INT:
1939 case GLSL_TYPE_FLOAT:
1940 case GLSL_TYPE_BOOL:
1941 return this->matrix_columns;
1942 case GLSL_TYPE_DOUBLE:
1943 if (this->vector_elements > 2 && !is_vertex_input)
1944 return this->matrix_columns * 2;
1945 else
1946 return this->matrix_columns;
1947 case GLSL_TYPE_STRUCT:
1948 case GLSL_TYPE_INTERFACE: {
1949 unsigned size = 0;
1950
1951 for (unsigned i = 0; i < this->length; i++)
1952 size += this->fields.structure[i].type->count_attribute_slots(is_vertex_input);
1953
1954 return size;
1955 }
1956
1957 case GLSL_TYPE_ARRAY:
1958 return this->length * this->fields.array->count_attribute_slots(is_vertex_input);
1959
1960 case GLSL_TYPE_FUNCTION:
1961 case GLSL_TYPE_SAMPLER:
1962 case GLSL_TYPE_IMAGE:
1963 case GLSL_TYPE_ATOMIC_UINT:
1964 case GLSL_TYPE_VOID:
1965 case GLSL_TYPE_SUBROUTINE:
1966 case GLSL_TYPE_ERROR:
1967 break;
1968 }
1969
1970 assert(!"Unexpected type in count_attribute_slots()");
1971
1972 return 0;
1973 }
1974
1975 int
1976 glsl_type::coordinate_components() const
1977 {
1978 int size;
1979
1980 switch (sampler_dimensionality) {
1981 case GLSL_SAMPLER_DIM_1D:
1982 case GLSL_SAMPLER_DIM_BUF:
1983 size = 1;
1984 break;
1985 case GLSL_SAMPLER_DIM_2D:
1986 case GLSL_SAMPLER_DIM_RECT:
1987 case GLSL_SAMPLER_DIM_MS:
1988 case GLSL_SAMPLER_DIM_EXTERNAL:
1989 case GLSL_SAMPLER_DIM_SUBPASS:
1990 size = 2;
1991 break;
1992 case GLSL_SAMPLER_DIM_3D:
1993 case GLSL_SAMPLER_DIM_CUBE:
1994 size = 3;
1995 break;
1996 default:
1997 assert(!"Should not get here.");
1998 size = 1;
1999 break;
2000 }
2001
2002 /* Array textures need an additional component for the array index, except
2003 * for cubemap array images that behave like a 2D array of interleaved
2004 * cubemap faces.
2005 */
2006 if (sampler_array &&
2007 !(base_type == GLSL_TYPE_IMAGE &&
2008 sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE))
2009 size += 1;
2010
2011 return size;
2012 }
2013
2014 /**
2015 * Declarations of type flyweights (glsl_type::_foo_type) and
2016 * convenience pointers (glsl_type::foo_type).
2017 * @{
2018 */
2019 #define DECL_TYPE(NAME, ...) \
2020 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
2021 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
2022
2023 #define STRUCT_TYPE(NAME)
2024
2025 #include "compiler/builtin_type_macros.h"
2026 /** @} */