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