spirv: add vtn_emit_make_{visible,available}_barrier helpers
[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_struct_type_is_packed(const struct glsl_type *type)
365 {
366 assert(glsl_type_is_struct(type));
367 return type->packed;
368 }
369
370 bool
371 glsl_type_is_dual_slot(const struct glsl_type *type)
372 {
373 return type->is_dual_slot();
374 }
375
376 bool
377 glsl_type_is_numeric(const struct glsl_type *type)
378 {
379 return type->is_numeric();
380 }
381
382 bool
383 glsl_type_is_boolean(const struct glsl_type *type)
384 {
385 return type->is_boolean();
386 }
387 bool
388 glsl_type_is_integer(const struct glsl_type *type)
389 {
390 return type->is_integer();
391 }
392
393 bool
394 glsl_type_contains_64bit(const struct glsl_type *type)
395 {
396 return type->contains_64bit();
397 }
398
399 const glsl_type *
400 glsl_void_type(void)
401 {
402 return glsl_type::void_type;
403 }
404
405 const glsl_type *
406 glsl_float_type(void)
407 {
408 return glsl_type::float_type;
409 }
410
411 const glsl_type *
412 glsl_double_type(void)
413 {
414 return glsl_type::double_type;
415 }
416
417 const glsl_type *
418 glsl_float16_t_type(void)
419 {
420 return glsl_type::float16_t_type;
421 }
422
423 const glsl_type *
424 glsl_vec_type(unsigned n)
425 {
426 return glsl_type::vec(n);
427 }
428
429 const glsl_type *
430 glsl_dvec_type(unsigned n)
431 {
432 return glsl_type::dvec(n);
433 }
434
435 const glsl_type *
436 glsl_vec4_type(void)
437 {
438 return glsl_type::vec4_type;
439 }
440
441 const glsl_type *
442 glsl_uvec4_type(void)
443 {
444 return glsl_type::uvec4_type;
445 }
446
447 const glsl_type *
448 glsl_int_type(void)
449 {
450 return glsl_type::int_type;
451 }
452
453 const glsl_type *
454 glsl_uint_type(void)
455 {
456 return glsl_type::uint_type;
457 }
458
459 const glsl_type *
460 glsl_int64_t_type(void)
461 {
462 return glsl_type::int64_t_type;
463 }
464
465 const glsl_type *
466 glsl_uint64_t_type(void)
467 {
468 return glsl_type::uint64_t_type;
469 }
470
471 const glsl_type *
472 glsl_int16_t_type(void)
473 {
474 return glsl_type::int16_t_type;
475 }
476
477 const glsl_type *
478 glsl_uint16_t_type(void)
479 {
480 return glsl_type::uint16_t_type;
481 }
482
483 const glsl_type *
484 glsl_int8_t_type(void)
485 {
486 return glsl_type::int8_t_type;
487 }
488
489 const glsl_type *
490 glsl_uint8_t_type(void)
491 {
492 return glsl_type::uint8_t_type;
493 }
494
495 const glsl_type *
496 glsl_bool_type(void)
497 {
498 return glsl_type::bool_type;
499 }
500
501 const glsl_type *
502 glsl_scalar_type(enum glsl_base_type base_type)
503 {
504 return glsl_type::get_instance(base_type, 1, 1);
505 }
506
507 const glsl_type *
508 glsl_vector_type(enum glsl_base_type base_type, unsigned components)
509 {
510 const glsl_type *t = glsl_type::get_instance(base_type, components, 1);
511 assert(t != glsl_type::error_type);
512 return t;
513 }
514
515 const glsl_type *
516 glsl_matrix_type(enum glsl_base_type base_type, unsigned rows, unsigned columns)
517 {
518 const glsl_type *t = glsl_type::get_instance(base_type, rows, columns);
519 assert(t != glsl_type::error_type);
520 return t;
521 }
522
523 const glsl_type *
524 glsl_explicit_matrix_type(const glsl_type *mat,
525 unsigned stride, bool row_major)
526 {
527 assert(stride > 0);
528 const glsl_type *t = glsl_type::get_instance(mat->base_type,
529 mat->vector_elements,
530 mat->matrix_columns,
531 stride, row_major);
532 assert(t != glsl_type::error_type);
533 return t;
534 }
535
536 const glsl_type *
537 glsl_array_type(const glsl_type *base, unsigned elements,
538 unsigned explicit_stride)
539 {
540 return glsl_type::get_array_instance(base, elements, explicit_stride);
541 }
542
543 const glsl_type *
544 glsl_replace_vector_type(const glsl_type *t, unsigned components)
545 {
546 if (glsl_type_is_array(t)) {
547 return glsl_array_type(
548 glsl_replace_vector_type(t->fields.array, components), t->length,
549 t->explicit_stride);
550 } else if (glsl_type_is_vector_or_scalar(t)) {
551 return glsl_vector_type(t->base_type, components);
552 } else {
553 unreachable("Unhandled base type glsl_replace_vector_type()");
554 }
555 }
556
557 const glsl_type *
558 glsl_struct_type(const glsl_struct_field *fields,
559 unsigned num_fields, const char *name,
560 bool packed)
561 {
562 return glsl_type::get_struct_instance(fields, num_fields, name, packed);
563 }
564
565 const glsl_type *
566 glsl_interface_type(const glsl_struct_field *fields,
567 unsigned num_fields,
568 enum glsl_interface_packing packing,
569 bool row_major,
570 const char *block_name)
571 {
572 return glsl_type::get_interface_instance(fields, num_fields, packing,
573 row_major, block_name);
574 }
575
576 const struct glsl_type *
577 glsl_sampler_type(enum glsl_sampler_dim dim, bool is_shadow, bool is_array,
578 enum glsl_base_type base_type)
579 {
580 return glsl_type::get_sampler_instance(dim, is_shadow, is_array, base_type);
581 }
582
583 const struct glsl_type *
584 glsl_bare_sampler_type()
585 {
586 return glsl_type::sampler_type;
587 }
588
589 const struct glsl_type *
590 glsl_bare_shadow_sampler_type()
591 {
592 return glsl_type::samplerShadow_type;
593 }
594
595 const struct glsl_type *
596 glsl_image_type(enum glsl_sampler_dim dim, bool is_array,
597 enum glsl_base_type base_type)
598 {
599 return glsl_type::get_image_instance(dim, is_array, base_type);
600 }
601
602 const glsl_type *
603 glsl_function_type(const glsl_type *return_type,
604 const glsl_function_param *params, unsigned num_params)
605 {
606 return glsl_type::get_function_instance(return_type, params, num_params);
607 }
608
609 const glsl_type *
610 glsl_transposed_type(const struct glsl_type *type)
611 {
612 assert(glsl_type_is_matrix(type));
613 return glsl_type::get_instance(type->base_type, type->matrix_columns,
614 type->vector_elements);
615 }
616
617 const glsl_type *
618 glsl_channel_type(const glsl_type *t)
619 {
620 switch (t->base_type) {
621 case GLSL_TYPE_ARRAY:
622 return glsl_array_type(glsl_channel_type(t->fields.array), t->length,
623 t->explicit_stride);
624 case GLSL_TYPE_UINT:
625 case GLSL_TYPE_INT:
626 case GLSL_TYPE_FLOAT:
627 case GLSL_TYPE_FLOAT16:
628 case GLSL_TYPE_DOUBLE:
629 case GLSL_TYPE_UINT8:
630 case GLSL_TYPE_INT8:
631 case GLSL_TYPE_UINT16:
632 case GLSL_TYPE_INT16:
633 case GLSL_TYPE_UINT64:
634 case GLSL_TYPE_INT64:
635 case GLSL_TYPE_BOOL:
636 return glsl_type::get_instance(t->base_type, 1, 1);
637 default:
638 unreachable("Unhandled base type glsl_channel_type()");
639 }
640 }
641
642 const glsl_type *
643 glsl_float16_type(const struct glsl_type *type)
644 {
645 return type->get_float16_type();
646 }
647
648 const glsl_type *
649 glsl_int16_type(const struct glsl_type *type)
650 {
651 return type->get_int16_type();
652 }
653
654 const glsl_type *
655 glsl_uint16_type(const struct glsl_type *type)
656 {
657 return type->get_uint16_type();
658 }
659
660 void
661 glsl_get_natural_size_align_bytes(const struct glsl_type *type,
662 unsigned *size, unsigned *align)
663 {
664 switch (type->base_type) {
665 case GLSL_TYPE_BOOL:
666 /* We special-case Booleans to 32 bits to not cause heartburn for
667 * drivers that suddenly get an 8-bit load.
668 */
669 *size = 4 * type->components();
670 *align = 4;
671 break;
672
673 case GLSL_TYPE_UINT8:
674 case GLSL_TYPE_INT8:
675 case GLSL_TYPE_UINT16:
676 case GLSL_TYPE_INT16:
677 case GLSL_TYPE_FLOAT16:
678 case GLSL_TYPE_UINT:
679 case GLSL_TYPE_INT:
680 case GLSL_TYPE_FLOAT:
681 case GLSL_TYPE_DOUBLE:
682 case GLSL_TYPE_UINT64:
683 case GLSL_TYPE_INT64: {
684 unsigned N = glsl_get_bit_size(type) / 8;
685 *size = N * type->components();
686 *align = N;
687 break;
688 }
689
690 case GLSL_TYPE_ARRAY: {
691 unsigned elem_size = 0, elem_align = 0;
692 glsl_get_natural_size_align_bytes(type->fields.array,
693 &elem_size, &elem_align);
694 *align = elem_align;
695 *size = type->length * ALIGN_POT(elem_size, elem_align);
696 break;
697 }
698
699 case GLSL_TYPE_STRUCT:
700 *size = 0;
701 *align = 0;
702 for (unsigned i = 0; i < type->length; i++) {
703 unsigned elem_size = 0, elem_align = 0;
704 glsl_get_natural_size_align_bytes(type->fields.structure[i].type,
705 &elem_size, &elem_align);
706 *align = MAX2(*align, elem_align);
707 *size = ALIGN_POT(*size, elem_align) + elem_size;
708 }
709 break;
710
711 case GLSL_TYPE_SAMPLER:
712 case GLSL_TYPE_IMAGE:
713 /* Bindless samplers and images. */
714 *size = 8;
715 *align = 8;
716 break;
717
718 case GLSL_TYPE_ATOMIC_UINT:
719 case GLSL_TYPE_SUBROUTINE:
720 case GLSL_TYPE_VOID:
721 case GLSL_TYPE_ERROR:
722 case GLSL_TYPE_INTERFACE:
723 case GLSL_TYPE_FUNCTION:
724 unreachable("type does not have a natural size");
725 }
726 }
727
728 const glsl_type *
729 glsl_atomic_uint_type(void)
730 {
731 return glsl_type::atomic_uint_type;
732 }
733
734 unsigned
735 glsl_atomic_size(const struct glsl_type *type)
736 {
737 return type->atomic_size();
738 }
739
740 bool
741 glsl_contains_atomic(const struct glsl_type *type)
742 {
743 return type->contains_atomic();
744 }
745
746 bool
747 glsl_contains_opaque(const struct glsl_type *type)
748 {
749 return type->contains_opaque();
750 }
751
752 int
753 glsl_get_cl_size(const struct glsl_type *type)
754 {
755 return type->cl_size();
756 }
757
758 int
759 glsl_get_cl_alignment(const struct glsl_type *type)
760 {
761 return type->cl_alignment();
762 }
763
764 void
765 glsl_get_cl_type_size_align(const struct glsl_type *type,
766 unsigned *size, unsigned *align)
767 {
768 *size = glsl_get_cl_size(type);
769 *align = glsl_get_cl_alignment(type);
770 }
771
772 unsigned
773 glsl_type_get_sampler_count(const struct glsl_type *type)
774 {
775 if (glsl_type_is_array(type)) {
776 return (glsl_get_aoa_size(type) *
777 glsl_type_get_sampler_count(glsl_without_array(type)));
778 }
779
780 /* Ignore interface blocks - they can only contain bindless samplers,
781 * which we shouldn't count.
782 */
783 if (glsl_type_is_struct(type)) {
784 unsigned count = 0;
785 for (unsigned i = 0; i < glsl_get_length(type); i++)
786 count += glsl_type_get_sampler_count(glsl_get_struct_field(type, i));
787 return count;
788 }
789
790 if (glsl_type_is_sampler(type))
791 return 1;
792
793 return 0;
794 }
795
796 unsigned
797 glsl_type_get_image_count(const struct glsl_type *type)
798 {
799 if (glsl_type_is_array(type)) {
800 return (glsl_get_aoa_size(type) *
801 glsl_type_get_image_count(glsl_without_array(type)));
802 }
803
804 /* Ignore interface blocks - they can only contain bindless images,
805 * which we shouldn't count.
806 */
807 if (glsl_type_is_struct(type)) {
808 unsigned count = 0;
809 for (unsigned i = 0; i < glsl_get_length(type); i++)
810 count += glsl_type_get_image_count(glsl_get_struct_field(type, i));
811 return count;
812 }
813
814 if (glsl_type_is_image(type))
815 return 1;
816
817 return 0;
818 }
819
820 enum glsl_interface_packing
821 glsl_get_internal_ifc_packing(const struct glsl_type *type,
822 bool std430_supported)
823 {
824 return type->get_internal_ifc_packing(std430_supported);
825 }
826
827 enum glsl_interface_packing
828 glsl_get_ifc_packing(const struct glsl_type *type)
829 {
830 return type->get_interface_packing();
831 }
832
833 unsigned
834 glsl_get_std140_base_alignment(const struct glsl_type *type, bool row_major)
835 {
836 return type->std140_base_alignment(row_major);
837 }
838
839 unsigned
840 glsl_get_std140_size(const struct glsl_type *type, bool row_major)
841 {
842 return type->std140_size(row_major);
843 }
844
845 unsigned
846 glsl_get_std430_base_alignment(const struct glsl_type *type, bool row_major)
847 {
848 return type->std430_base_alignment(row_major);
849 }
850
851 unsigned
852 glsl_get_std430_size(const struct glsl_type *type, bool row_major)
853 {
854 return type->std430_size(row_major);
855 }
856
857 unsigned
858 glsl_get_explicit_size(const struct glsl_type *type, bool align_to_stride)
859 {
860 return type->explicit_size(align_to_stride);
861 }
862
863 bool
864 glsl_type_is_leaf(const struct glsl_type *type)
865 {
866 if (glsl_type_is_struct_or_ifc(type) ||
867 (glsl_type_is_array(type) &&
868 (glsl_type_is_array(glsl_get_array_element(type)) ||
869 glsl_type_is_struct_or_ifc(glsl_get_array_element(type))))) {
870 return false;
871 } else {
872 return true;
873 }
874 }
875
876 const struct glsl_type *
877 glsl_get_explicit_type_for_size_align(const struct glsl_type *type,
878 glsl_type_size_align_func type_info,
879 unsigned *size, unsigned *align)
880 {
881 return type->get_explicit_type_for_size_align(type_info, size, align);
882 }