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