nir/lower_amul: fix slot calculation
[mesa.git] / src / compiler / nir_types.cpp
1 /*
2 * Copyright © 2014 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 DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Connor Abbott (cwabbott0@gmail.com)
25 *
26 */
27
28 #include "nir_types.h"
29 #include "compiler/glsl/ir.h"
30
31 const char *
32 glsl_get_type_name(const glsl_type *type)
33 {
34 return type->name;
35 }
36
37 int
38 glsl_array_size(const struct glsl_type *type)
39 {
40 return type->array_size();
41 }
42
43 const glsl_type *
44 glsl_get_array_element(const glsl_type* type)
45 {
46 if (type->is_matrix())
47 return type->column_type();
48 else if (type->is_vector())
49 return type->get_scalar_type();
50 return type->fields.array;
51 }
52
53 const glsl_type *
54 glsl_without_array(const glsl_type *type)
55 {
56 return type->without_array();
57 }
58
59 const glsl_type *
60 glsl_without_array_or_matrix(const glsl_type *type)
61 {
62 type = type->without_array();
63 if (type->is_matrix())
64 type = type->column_type();
65 return type;
66 }
67
68 const glsl_type *
69 glsl_get_bare_type(const glsl_type *type)
70 {
71 return type->get_bare_type();
72 }
73
74 const glsl_type *
75 glsl_get_struct_field(const glsl_type *type, unsigned index)
76 {
77 return type->fields.structure[index].type;
78 }
79
80 int
81 glsl_get_struct_field_offset(const struct glsl_type *type,
82 unsigned index)
83 {
84 return type->fields.structure[index].offset;
85 }
86
87 const struct glsl_struct_field *
88 glsl_get_struct_field_data(const struct glsl_type *type, unsigned index)
89 {
90 assert(type->is_struct() || type->is_interface());
91 assert(index < type->length);
92 return &type->fields.structure[index];
93 }
94
95 unsigned
96 glsl_get_explicit_stride(const struct glsl_type *type)
97 {
98 return type->explicit_stride;
99 }
100
101 const glsl_type *
102 glsl_get_function_return_type(const glsl_type *type)
103 {
104 return type->fields.parameters[0].type;
105 }
106
107 const glsl_function_param *
108 glsl_get_function_param(const glsl_type *type, unsigned index)
109 {
110 return &type->fields.parameters[index + 1];
111 }
112
113 const struct glsl_type *
114 glsl_get_column_type(const struct glsl_type *type)
115 {
116 return type->column_type();
117 }
118
119 GLenum
120 glsl_get_gl_type(const struct glsl_type *type)
121 {
122 return type->gl_type;
123 }
124
125 enum glsl_base_type
126 glsl_get_base_type(const struct glsl_type *type)
127 {
128 return type->base_type;
129 }
130
131 unsigned
132 glsl_get_vector_elements(const struct glsl_type *type)
133 {
134 return type->vector_elements;
135 }
136
137 unsigned
138 glsl_get_components(const struct glsl_type *type)
139 {
140 return type->components();
141 }
142
143 unsigned
144 glsl_get_matrix_columns(const struct glsl_type *type)
145 {
146 return type->matrix_columns;
147 }
148
149 unsigned
150 glsl_get_length(const struct glsl_type *type)
151 {
152 return type->is_matrix() ? type->matrix_columns : type->length;
153 }
154
155 unsigned
156 glsl_get_aoa_size(const struct glsl_type *type)
157 {
158 return type->arrays_of_arrays_size();
159 }
160
161 unsigned
162 glsl_count_vec4_slots(const struct glsl_type *type,
163 bool is_gl_vertex_input, bool is_bindless)
164 {
165 return type->count_vec4_slots(is_gl_vertex_input, is_bindless);
166 }
167
168 unsigned
169 glsl_count_dword_slots(const struct glsl_type *type, bool is_bindless)
170 {
171 return type->count_dword_slots(is_bindless);
172 }
173
174 unsigned
175 glsl_count_attribute_slots(const struct glsl_type *type,
176 bool is_gl_vertex_input)
177 {
178 return type->count_attribute_slots(is_gl_vertex_input);
179 }
180
181 unsigned
182 glsl_get_component_slots(const struct glsl_type *type)
183 {
184 return type->component_slots();
185 }
186
187 unsigned
188 glsl_varying_count(const struct glsl_type *type)
189 {
190 return type->varying_count();
191 }
192
193 const char *
194 glsl_get_struct_elem_name(const struct glsl_type *type, unsigned index)
195 {
196 return type->fields.structure[index].name;
197 }
198
199 glsl_sampler_dim
200 glsl_get_sampler_dim(const struct glsl_type *type)
201 {
202 assert(glsl_type_is_sampler(type) || glsl_type_is_image(type));
203 return (glsl_sampler_dim)type->sampler_dimensionality;
204 }
205
206 glsl_base_type
207 glsl_get_sampler_result_type(const struct glsl_type *type)
208 {
209 assert(glsl_type_is_sampler(type) || glsl_type_is_image(type));
210 return (glsl_base_type)type->sampled_type;
211 }
212
213 unsigned
214 glsl_get_sampler_target(const struct glsl_type *type)
215 {
216 assert(glsl_type_is_sampler(type));
217 return type->sampler_index();
218 }
219
220 int
221 glsl_get_sampler_coordinate_components(const struct glsl_type *type)
222 {
223 assert(glsl_type_is_sampler(type) || glsl_type_is_image(type));
224 return type->coordinate_components();
225 }
226
227 unsigned
228 glsl_get_struct_location_offset(const struct glsl_type *type,
229 unsigned length)
230 {
231 return type->struct_location_offset(length);
232 }
233
234 bool
235 glsl_type_is_16bit(const glsl_type *type)
236 {
237 return type->is_16bit();
238 }
239
240 bool
241 glsl_type_is_32bit(const glsl_type *type)
242 {
243 return type->is_32bit();
244 }
245
246 bool
247 glsl_type_is_64bit(const glsl_type *type)
248 {
249 return type->is_64bit();
250 }
251
252 bool
253 glsl_type_is_void(const glsl_type *type)
254 {
255 return type->is_void();
256 }
257
258 bool
259 glsl_type_is_error(const glsl_type *type)
260 {
261 return type->is_error();
262 }
263
264 bool
265 glsl_type_is_vector(const struct glsl_type *type)
266 {
267 return type->is_vector();
268 }
269
270 bool
271 glsl_type_is_scalar(const struct glsl_type *type)
272 {
273 return type->is_scalar();
274 }
275
276 bool
277 glsl_type_is_vector_or_scalar(const struct glsl_type *type)
278 {
279 return type->is_vector() || type->is_scalar();
280 }
281
282 bool
283 glsl_type_is_matrix(const struct glsl_type *type)
284 {
285 return type->is_matrix();
286 }
287
288 bool
289 glsl_matrix_type_is_row_major(const struct glsl_type *type)
290 {
291 assert(type->is_matrix() && type->explicit_stride);
292 return type->interface_row_major;
293 }
294
295 bool
296 glsl_type_is_array(const struct glsl_type *type)
297 {
298 return type->is_array();
299 }
300
301 bool
302 glsl_type_is_unsized_array(const struct glsl_type *type)
303 {
304 return type->is_unsized_array();
305 }
306
307 bool
308 glsl_type_is_array_of_arrays(const struct glsl_type *type)
309 {
310 return type->is_array_of_arrays();
311 }
312
313 bool
314 glsl_type_is_array_or_matrix(const struct glsl_type *type)
315 {
316 return type->is_array() || type->is_matrix();
317 }
318
319 bool
320 glsl_type_is_struct(const struct glsl_type *type)
321 {
322 return type->is_struct();
323 }
324
325 bool
326 glsl_type_is_interface(const struct glsl_type *type)
327 {
328 return type->is_interface();
329 }
330
331 bool
332 glsl_type_is_struct_or_ifc(const struct glsl_type *type)
333 {
334 return type->is_struct() || type->is_interface();
335 }
336
337 bool
338 glsl_type_is_sampler(const struct glsl_type *type)
339 {
340 return type->is_sampler();
341 }
342
343 bool
344 glsl_type_is_image(const struct glsl_type *type)
345 {
346 return type->is_image();
347 }
348
349 bool
350 glsl_sampler_type_is_shadow(const struct glsl_type *type)
351 {
352 assert(glsl_type_is_sampler(type));
353 return type->sampler_shadow;
354 }
355
356 bool
357 glsl_sampler_type_is_array(const struct glsl_type *type)
358 {
359 assert(glsl_type_is_sampler(type) || glsl_type_is_image(type));
360 return type->sampler_array;
361 }
362
363 bool
364 glsl_type_is_dual_slot(const struct glsl_type *type)
365 {
366 return type->is_dual_slot();
367 }
368
369 bool
370 glsl_type_is_numeric(const struct glsl_type *type)
371 {
372 return type->is_numeric();
373 }
374
375 bool
376 glsl_type_is_boolean(const struct glsl_type *type)
377 {
378 return type->is_boolean();
379 }
380 bool
381 glsl_type_is_integer(const struct glsl_type *type)
382 {
383 return type->is_integer();
384 }
385
386 bool
387 glsl_type_contains_64bit(const struct glsl_type *type)
388 {
389 return type->contains_64bit();
390 }
391
392 const glsl_type *
393 glsl_void_type(void)
394 {
395 return glsl_type::void_type;
396 }
397
398 const glsl_type *
399 glsl_float_type(void)
400 {
401 return glsl_type::float_type;
402 }
403
404 const glsl_type *
405 glsl_double_type(void)
406 {
407 return glsl_type::double_type;
408 }
409
410 const glsl_type *
411 glsl_float16_t_type(void)
412 {
413 return glsl_type::float16_t_type;
414 }
415
416 const glsl_type *
417 glsl_vec_type(unsigned n)
418 {
419 return glsl_type::vec(n);
420 }
421
422 const glsl_type *
423 glsl_dvec_type(unsigned n)
424 {
425 return glsl_type::dvec(n);
426 }
427
428 const glsl_type *
429 glsl_vec4_type(void)
430 {
431 return glsl_type::vec4_type;
432 }
433
434 const glsl_type *
435 glsl_uvec4_type(void)
436 {
437 return glsl_type::uvec4_type;
438 }
439
440 const glsl_type *
441 glsl_int_type(void)
442 {
443 return glsl_type::int_type;
444 }
445
446 const glsl_type *
447 glsl_uint_type(void)
448 {
449 return glsl_type::uint_type;
450 }
451
452 const glsl_type *
453 glsl_int64_t_type(void)
454 {
455 return glsl_type::int64_t_type;
456 }
457
458 const glsl_type *
459 glsl_uint64_t_type(void)
460 {
461 return glsl_type::uint64_t_type;
462 }
463
464 const glsl_type *
465 glsl_int16_t_type(void)
466 {
467 return glsl_type::int16_t_type;
468 }
469
470 const glsl_type *
471 glsl_uint16_t_type(void)
472 {
473 return glsl_type::uint16_t_type;
474 }
475
476 const glsl_type *
477 glsl_int8_t_type(void)
478 {
479 return glsl_type::int8_t_type;
480 }
481
482 const glsl_type *
483 glsl_uint8_t_type(void)
484 {
485 return glsl_type::uint8_t_type;
486 }
487
488 const glsl_type *
489 glsl_bool_type(void)
490 {
491 return glsl_type::bool_type;
492 }
493
494 const glsl_type *
495 glsl_scalar_type(enum glsl_base_type base_type)
496 {
497 return glsl_type::get_instance(base_type, 1, 1);
498 }
499
500 const glsl_type *
501 glsl_vector_type(enum glsl_base_type base_type, unsigned components)
502 {
503 const glsl_type *t = glsl_type::get_instance(base_type, components, 1);
504 assert(t != glsl_type::error_type);
505 return t;
506 }
507
508 const glsl_type *
509 glsl_matrix_type(enum glsl_base_type base_type, unsigned rows, unsigned columns)
510 {
511 const glsl_type *t = glsl_type::get_instance(base_type, rows, columns);
512 assert(t != glsl_type::error_type);
513 return t;
514 }
515
516 const glsl_type *
517 glsl_explicit_matrix_type(const glsl_type *mat,
518 unsigned stride, bool row_major)
519 {
520 assert(stride > 0);
521 const glsl_type *t = glsl_type::get_instance(mat->base_type,
522 mat->vector_elements,
523 mat->matrix_columns,
524 stride, row_major);
525 assert(t != glsl_type::error_type);
526 return t;
527 }
528
529 const glsl_type *
530 glsl_array_type(const glsl_type *base, unsigned elements,
531 unsigned explicit_stride)
532 {
533 return glsl_type::get_array_instance(base, elements, explicit_stride);
534 }
535
536 const glsl_type *
537 glsl_replace_vector_type(const glsl_type *t, unsigned components)
538 {
539 if (glsl_type_is_array(t)) {
540 return glsl_array_type(
541 glsl_replace_vector_type(t->fields.array, components), t->length,
542 t->explicit_stride);
543 } else if (glsl_type_is_vector_or_scalar(t)) {
544 return glsl_vector_type(t->base_type, components);
545 } else {
546 unreachable("Unhandled base type glsl_replace_vector_type()");
547 }
548 }
549
550 const glsl_type *
551 glsl_struct_type(const glsl_struct_field *fields,
552 unsigned num_fields, const char *name,
553 bool packed)
554 {
555 return glsl_type::get_struct_instance(fields, num_fields, name, packed);
556 }
557
558 const glsl_type *
559 glsl_interface_type(const glsl_struct_field *fields,
560 unsigned num_fields,
561 enum glsl_interface_packing packing,
562 bool row_major,
563 const char *block_name)
564 {
565 return glsl_type::get_interface_instance(fields, num_fields, packing,
566 row_major, block_name);
567 }
568
569 const struct glsl_type *
570 glsl_sampler_type(enum glsl_sampler_dim dim, bool is_shadow, bool is_array,
571 enum glsl_base_type base_type)
572 {
573 return glsl_type::get_sampler_instance(dim, is_shadow, is_array, base_type);
574 }
575
576 const struct glsl_type *
577 glsl_bare_sampler_type()
578 {
579 return glsl_type::sampler_type;
580 }
581
582 const struct glsl_type *
583 glsl_image_type(enum glsl_sampler_dim dim, bool is_array,
584 enum glsl_base_type base_type)
585 {
586 return glsl_type::get_image_instance(dim, is_array, base_type);
587 }
588
589 const glsl_type *
590 glsl_function_type(const glsl_type *return_type,
591 const glsl_function_param *params, unsigned num_params)
592 {
593 return glsl_type::get_function_instance(return_type, params, num_params);
594 }
595
596 const glsl_type *
597 glsl_transposed_type(const struct glsl_type *type)
598 {
599 assert(glsl_type_is_matrix(type));
600 return glsl_type::get_instance(type->base_type, type->matrix_columns,
601 type->vector_elements);
602 }
603
604 const glsl_type *
605 glsl_channel_type(const glsl_type *t)
606 {
607 switch (t->base_type) {
608 case GLSL_TYPE_ARRAY:
609 return glsl_array_type(glsl_channel_type(t->fields.array), t->length,
610 t->explicit_stride);
611 case GLSL_TYPE_UINT:
612 case GLSL_TYPE_INT:
613 case GLSL_TYPE_FLOAT:
614 case GLSL_TYPE_FLOAT16:
615 case GLSL_TYPE_DOUBLE:
616 case GLSL_TYPE_UINT8:
617 case GLSL_TYPE_INT8:
618 case GLSL_TYPE_UINT16:
619 case GLSL_TYPE_INT16:
620 case GLSL_TYPE_UINT64:
621 case GLSL_TYPE_INT64:
622 case GLSL_TYPE_BOOL:
623 return glsl_type::get_instance(t->base_type, 1, 1);
624 default:
625 unreachable("Unhandled base type glsl_channel_type()");
626 }
627 }
628
629 const glsl_type *
630 glsl_float16_type(const struct glsl_type *type)
631 {
632 return type->get_float16_type();
633 }
634
635 void
636 glsl_get_natural_size_align_bytes(const struct glsl_type *type,
637 unsigned *size, unsigned *align)
638 {
639 switch (type->base_type) {
640 case GLSL_TYPE_BOOL:
641 /* We special-case Booleans to 32 bits to not cause heartburn for
642 * drivers that suddenly get an 8-bit load.
643 */
644 *size = 4 * type->components();
645 *align = 4;
646 break;
647
648 case GLSL_TYPE_UINT8:
649 case GLSL_TYPE_INT8:
650 case GLSL_TYPE_UINT16:
651 case GLSL_TYPE_INT16:
652 case GLSL_TYPE_FLOAT16:
653 case GLSL_TYPE_UINT:
654 case GLSL_TYPE_INT:
655 case GLSL_TYPE_FLOAT:
656 case GLSL_TYPE_DOUBLE:
657 case GLSL_TYPE_UINT64:
658 case GLSL_TYPE_INT64: {
659 unsigned N = glsl_get_bit_size(type) / 8;
660 *size = N * type->components();
661 *align = N;
662 break;
663 }
664
665 case GLSL_TYPE_ARRAY: {
666 unsigned elem_size = 0, elem_align = 0;
667 glsl_get_natural_size_align_bytes(type->fields.array,
668 &elem_size, &elem_align);
669 *align = elem_align;
670 *size = type->length * ALIGN_POT(elem_size, elem_align);
671 break;
672 }
673
674 case GLSL_TYPE_STRUCT:
675 *size = 0;
676 *align = 0;
677 for (unsigned i = 0; i < type->length; i++) {
678 unsigned elem_size = 0, elem_align = 0;
679 glsl_get_natural_size_align_bytes(type->fields.structure[i].type,
680 &elem_size, &elem_align);
681 *align = MAX2(*align, elem_align);
682 *size = ALIGN_POT(*size, elem_align) + elem_size;
683 }
684 break;
685
686 case GLSL_TYPE_SAMPLER:
687 case GLSL_TYPE_IMAGE:
688 /* Bindless samplers and images. */
689 *size = 8;
690 *align = 8;
691 break;
692
693 case GLSL_TYPE_ATOMIC_UINT:
694 case GLSL_TYPE_SUBROUTINE:
695 case GLSL_TYPE_VOID:
696 case GLSL_TYPE_ERROR:
697 case GLSL_TYPE_INTERFACE:
698 case GLSL_TYPE_FUNCTION:
699 unreachable("type does not have a natural size");
700 }
701 }
702
703 const glsl_type *
704 glsl_atomic_uint_type(void)
705 {
706 return glsl_type::atomic_uint_type;
707 }
708
709 unsigned
710 glsl_atomic_size(const struct glsl_type *type)
711 {
712 return type->atomic_size();
713 }
714
715 bool
716 glsl_contains_atomic(const struct glsl_type *type)
717 {
718 return type->contains_atomic();
719 }
720
721 bool
722 glsl_contains_opaque(const struct glsl_type *type)
723 {
724 return type->contains_opaque();
725 }
726
727 int
728 glsl_get_cl_size(const struct glsl_type *type)
729 {
730 return type->cl_size();
731 }
732
733 int
734 glsl_get_cl_alignment(const struct glsl_type *type)
735 {
736 return type->cl_alignment();
737 }
738
739 unsigned
740 glsl_type_get_sampler_count(const struct glsl_type *type)
741 {
742 if (glsl_type_is_array(type)) {
743 return (glsl_get_aoa_size(type) *
744 glsl_type_get_sampler_count(glsl_without_array(type)));
745 }
746
747 if (glsl_type_is_struct_or_ifc(type)) {
748 unsigned count = 0;
749 for (unsigned i = 0; i < glsl_get_length(type); i++)
750 count += glsl_type_get_sampler_count(glsl_get_struct_field(type, i));
751 return count;
752 }
753
754 if (glsl_type_is_sampler(type))
755 return 1;
756
757 return 0;
758 }
759
760 unsigned
761 glsl_type_get_image_count(const struct glsl_type *type)
762 {
763 if (glsl_type_is_array(type)) {
764 return (glsl_get_aoa_size(type) *
765 glsl_type_get_image_count(glsl_without_array(type)));
766 }
767
768 if (glsl_type_is_struct_or_ifc(type)) {
769 unsigned count = 0;
770 for (unsigned i = 0; i < glsl_get_length(type); i++)
771 count += glsl_type_get_image_count(glsl_get_struct_field(type, i));
772 return count;
773 }
774
775 if (glsl_type_is_image(type))
776 return 1;
777
778 return 0;
779 }
780
781 enum glsl_interface_packing
782 glsl_get_internal_ifc_packing(const struct glsl_type *type,
783 bool std430_supported)
784 {
785 return type->get_internal_ifc_packing(std430_supported);
786 }
787
788 unsigned
789 glsl_get_std140_base_alignment(const struct glsl_type *type, bool row_major)
790 {
791 return type->std140_base_alignment(row_major);
792 }
793
794 unsigned
795 glsl_get_std140_size(const struct glsl_type *type, bool row_major)
796 {
797 return type->std140_size(row_major);
798 }
799
800 unsigned
801 glsl_get_std430_base_alignment(const struct glsl_type *type, bool row_major)
802 {
803 return type->std430_base_alignment(row_major);
804 }
805
806 unsigned
807 glsl_get_std430_size(const struct glsl_type *type, bool row_major)
808 {
809 return type->std430_size(row_major);
810 }
811
812 unsigned
813 glsl_get_explicit_size(const struct glsl_type *type, bool align_to_stride)
814 {
815 return type->explicit_size(align_to_stride);
816 }
817
818 bool
819 glsl_type_is_leaf(const struct glsl_type *type)
820 {
821 if (glsl_type_is_struct_or_ifc(type) ||
822 (glsl_type_is_array(type) &&
823 (glsl_type_is_array(glsl_get_array_element(type)) ||
824 glsl_type_is_struct_or_ifc(glsl_get_array_element(type))))) {
825 return false;
826 } else {
827 return true;
828 }
829 }
830
831 const struct glsl_type *
832 glsl_get_explicit_type_for_size_align(const struct glsl_type *type,
833 glsl_type_size_align_func type_info,
834 unsigned *size, unsigned *align)
835 {
836 return type->get_explicit_type_for_size_align(type_info, size, align);
837 }