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