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