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