2 * Copyright © 2014 Intel Corporation
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:
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
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
24 * Connor Abbott (cwabbott0@gmail.com)
28 #include "nir_types.h"
29 #include "compiler/glsl/ir.h"
32 glsl_get_type_name(const glsl_type
*type
)
38 glsl_array_size(const struct glsl_type
*type
)
40 return type
->array_size();
44 glsl_get_array_element(const glsl_type
* type
)
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
;
54 glsl_without_array(const glsl_type
*type
)
56 return type
->without_array();
60 glsl_without_array_or_matrix(const glsl_type
*type
)
62 type
= type
->without_array();
63 if (type
->is_matrix())
64 type
= type
->column_type();
69 glsl_get_bare_type(const glsl_type
*type
)
71 return type
->get_bare_type();
75 glsl_get_struct_field(const glsl_type
*type
, unsigned index
)
77 return type
->fields
.structure
[index
].type
;
81 glsl_get_struct_field_offset(const struct glsl_type
*type
,
84 return type
->fields
.structure
[index
].offset
;
87 const struct glsl_struct_field
*
88 glsl_get_struct_field_data(const struct glsl_type
*type
, unsigned index
)
90 assert(type
->is_struct() || type
->is_interface());
91 assert(index
< type
->length
);
92 return &type
->fields
.structure
[index
];
96 glsl_get_explicit_stride(const struct glsl_type
*type
)
98 return type
->explicit_stride
;
102 glsl_get_function_return_type(const glsl_type
*type
)
104 return type
->fields
.parameters
[0].type
;
107 const glsl_function_param
*
108 glsl_get_function_param(const glsl_type
*type
, unsigned index
)
110 return &type
->fields
.parameters
[index
+ 1];
113 const struct glsl_type
*
114 glsl_get_column_type(const struct glsl_type
*type
)
116 return type
->column_type();
120 glsl_get_gl_type(const struct glsl_type
*type
)
122 return type
->gl_type
;
126 glsl_get_base_type(const struct glsl_type
*type
)
128 return type
->base_type
;
132 glsl_get_vector_elements(const struct glsl_type
*type
)
134 return type
->vector_elements
;
138 glsl_get_components(const struct glsl_type
*type
)
140 return type
->components();
144 glsl_get_matrix_columns(const struct glsl_type
*type
)
146 return type
->matrix_columns
;
150 glsl_get_length(const struct glsl_type
*type
)
152 return type
->is_matrix() ? type
->matrix_columns
: type
->length
;
156 glsl_get_aoa_size(const struct glsl_type
*type
)
158 return type
->arrays_of_arrays_size();
162 glsl_count_vec4_slots(const struct glsl_type
*type
,
163 bool is_gl_vertex_input
, bool is_bindless
)
165 return type
->count_vec4_slots(is_gl_vertex_input
, is_bindless
);
169 glsl_count_dword_slots(const struct glsl_type
*type
, bool is_bindless
)
171 return type
->count_dword_slots(is_bindless
);
175 glsl_count_attribute_slots(const struct glsl_type
*type
,
176 bool is_gl_vertex_input
)
178 return type
->count_attribute_slots(is_gl_vertex_input
);
182 glsl_get_component_slots(const struct glsl_type
*type
)
184 return type
->component_slots();
188 glsl_varying_count(const struct glsl_type
*type
)
190 return type
->varying_count();
194 glsl_get_struct_elem_name(const struct glsl_type
*type
, unsigned index
)
196 return type
->fields
.structure
[index
].name
;
200 glsl_get_sampler_dim(const struct glsl_type
*type
)
202 assert(glsl_type_is_sampler(type
) || glsl_type_is_image(type
));
203 return (glsl_sampler_dim
)type
->sampler_dimensionality
;
207 glsl_get_sampler_result_type(const struct glsl_type
*type
)
209 assert(glsl_type_is_sampler(type
) || glsl_type_is_image(type
));
210 return (glsl_base_type
)type
->sampled_type
;
214 glsl_get_sampler_target(const struct glsl_type
*type
)
216 assert(glsl_type_is_sampler(type
));
217 return type
->sampler_index();
221 glsl_get_sampler_coordinate_components(const struct glsl_type
*type
)
223 assert(glsl_type_is_sampler(type
) || glsl_type_is_image(type
));
224 return type
->coordinate_components();
228 glsl_get_struct_location_offset(const struct glsl_type
*type
,
231 return type
->struct_location_offset(length
);
235 glsl_type_is_16bit(const glsl_type
*type
)
237 return type
->is_16bit();
241 glsl_type_is_32bit(const glsl_type
*type
)
243 return type
->is_32bit();
247 glsl_type_is_64bit(const glsl_type
*type
)
249 return type
->is_64bit();
253 glsl_type_is_void(const glsl_type
*type
)
255 return type
->is_void();
259 glsl_type_is_error(const glsl_type
*type
)
261 return type
->is_error();
265 glsl_type_is_vector(const struct glsl_type
*type
)
267 return type
->is_vector();
271 glsl_type_is_scalar(const struct glsl_type
*type
)
273 return type
->is_scalar();
277 glsl_type_is_vector_or_scalar(const struct glsl_type
*type
)
279 return type
->is_vector() || type
->is_scalar();
283 glsl_type_is_matrix(const struct glsl_type
*type
)
285 return type
->is_matrix();
289 glsl_matrix_type_is_row_major(const struct glsl_type
*type
)
291 assert(type
->is_matrix() && type
->explicit_stride
);
292 return type
->interface_row_major
;
296 glsl_type_is_array(const struct glsl_type
*type
)
298 return type
->is_array();
302 glsl_type_is_unsized_array(const struct glsl_type
*type
)
304 return type
->is_unsized_array();
308 glsl_type_is_array_of_arrays(const struct glsl_type
*type
)
310 return type
->is_array_of_arrays();
314 glsl_type_is_array_or_matrix(const struct glsl_type
*type
)
316 return type
->is_array() || type
->is_matrix();
320 glsl_type_is_struct(const struct glsl_type
*type
)
322 return type
->is_struct();
326 glsl_type_is_interface(const struct glsl_type
*type
)
328 return type
->is_interface();
332 glsl_type_is_struct_or_ifc(const struct glsl_type
*type
)
334 return type
->is_struct() || type
->is_interface();
338 glsl_type_is_sampler(const struct glsl_type
*type
)
340 return type
->is_sampler();
344 glsl_type_is_image(const struct glsl_type
*type
)
346 return type
->is_image();
350 glsl_sampler_type_is_shadow(const struct glsl_type
*type
)
352 assert(glsl_type_is_sampler(type
));
353 return type
->sampler_shadow
;
357 glsl_sampler_type_is_array(const struct glsl_type
*type
)
359 assert(glsl_type_is_sampler(type
) || glsl_type_is_image(type
));
360 return type
->sampler_array
;
364 glsl_type_is_dual_slot(const struct glsl_type
*type
)
366 return type
->is_dual_slot();
370 glsl_type_is_numeric(const struct glsl_type
*type
)
372 return type
->is_numeric();
376 glsl_type_is_boolean(const struct glsl_type
*type
)
378 return type
->is_boolean();
381 glsl_type_is_integer(const struct glsl_type
*type
)
383 return type
->is_integer();
387 glsl_type_contains_64bit(const struct glsl_type
*type
)
389 return type
->contains_64bit();
395 return glsl_type::void_type
;
399 glsl_float_type(void)
401 return glsl_type::float_type
;
405 glsl_double_type(void)
407 return glsl_type::double_type
;
411 glsl_float16_t_type(void)
413 return glsl_type::float16_t_type
;
417 glsl_vec_type(unsigned n
)
419 return glsl_type::vec(n
);
423 glsl_dvec_type(unsigned n
)
425 return glsl_type::dvec(n
);
431 return glsl_type::vec4_type
;
435 glsl_uvec4_type(void)
437 return glsl_type::uvec4_type
;
443 return glsl_type::int_type
;
449 return glsl_type::uint_type
;
453 glsl_int64_t_type(void)
455 return glsl_type::int64_t_type
;
459 glsl_uint64_t_type(void)
461 return glsl_type::uint64_t_type
;
465 glsl_int16_t_type(void)
467 return glsl_type::int16_t_type
;
471 glsl_uint16_t_type(void)
473 return glsl_type::uint16_t_type
;
477 glsl_int8_t_type(void)
479 return glsl_type::int8_t_type
;
483 glsl_uint8_t_type(void)
485 return glsl_type::uint8_t_type
;
491 return glsl_type::bool_type
;
495 glsl_scalar_type(enum glsl_base_type base_type
)
497 return glsl_type::get_instance(base_type
, 1, 1);
501 glsl_vector_type(enum glsl_base_type base_type
, unsigned components
)
503 const glsl_type
*t
= glsl_type::get_instance(base_type
, components
, 1);
504 assert(t
!= glsl_type::error_type
);
509 glsl_matrix_type(enum glsl_base_type base_type
, unsigned rows
, unsigned columns
)
511 const glsl_type
*t
= glsl_type::get_instance(base_type
, rows
, columns
);
512 assert(t
!= glsl_type::error_type
);
517 glsl_explicit_matrix_type(const glsl_type
*mat
,
518 unsigned stride
, bool row_major
)
521 const glsl_type
*t
= glsl_type::get_instance(mat
->base_type
,
522 mat
->vector_elements
,
525 assert(t
!= glsl_type::error_type
);
530 glsl_array_type(const glsl_type
*base
, unsigned elements
,
531 unsigned explicit_stride
)
533 return glsl_type::get_array_instance(base
, elements
, explicit_stride
);
537 glsl_replace_vector_type(const glsl_type
*t
, unsigned components
)
539 if (glsl_type_is_array(t
)) {
540 return glsl_array_type(
541 glsl_replace_vector_type(t
->fields
.array
, components
), t
->length
,
543 } else if (glsl_type_is_vector_or_scalar(t
)) {
544 return glsl_vector_type(t
->base_type
, components
);
546 unreachable("Unhandled base type glsl_replace_vector_type()");
551 glsl_struct_type(const glsl_struct_field
*fields
,
552 unsigned num_fields
, const char *name
,
555 return glsl_type::get_struct_instance(fields
, num_fields
, name
, packed
);
559 glsl_interface_type(const glsl_struct_field
*fields
,
561 enum glsl_interface_packing packing
,
563 const char *block_name
)
565 return glsl_type::get_interface_instance(fields
, num_fields
, packing
,
566 row_major
, block_name
);
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
)
573 return glsl_type::get_sampler_instance(dim
, is_shadow
, is_array
, base_type
);
576 const struct glsl_type
*
577 glsl_bare_sampler_type()
579 return glsl_type::sampler_type
;
582 const struct glsl_type
*
583 glsl_image_type(enum glsl_sampler_dim dim
, bool is_array
,
584 enum glsl_base_type base_type
)
586 return glsl_type::get_image_instance(dim
, is_array
, base_type
);
590 glsl_function_type(const glsl_type
*return_type
,
591 const glsl_function_param
*params
, unsigned num_params
)
593 return glsl_type::get_function_instance(return_type
, params
, num_params
);
597 glsl_transposed_type(const struct glsl_type
*type
)
599 assert(glsl_type_is_matrix(type
));
600 return glsl_type::get_instance(type
->base_type
, type
->matrix_columns
,
601 type
->vector_elements
);
605 glsl_channel_type(const glsl_type
*t
)
607 switch (t
->base_type
) {
608 case GLSL_TYPE_ARRAY
:
609 return glsl_array_type(glsl_channel_type(t
->fields
.array
), t
->length
,
613 case GLSL_TYPE_FLOAT
:
614 case GLSL_TYPE_FLOAT16
:
615 case GLSL_TYPE_DOUBLE
:
616 case GLSL_TYPE_UINT8
:
618 case GLSL_TYPE_UINT16
:
619 case GLSL_TYPE_INT16
:
620 case GLSL_TYPE_UINT64
:
621 case GLSL_TYPE_INT64
:
623 return glsl_type::get_instance(t
->base_type
, 1, 1);
625 unreachable("Unhandled base type glsl_channel_type()");
630 glsl_float16_type(const struct glsl_type
*type
)
632 return type
->get_float16_type();
636 glsl_int16_type(const struct glsl_type
*type
)
638 return type
->get_int16_type();
642 glsl_uint16_type(const struct glsl_type
*type
)
644 return type
->get_uint16_type();
648 glsl_get_natural_size_align_bytes(const struct glsl_type
*type
,
649 unsigned *size
, unsigned *align
)
651 switch (type
->base_type
) {
653 /* We special-case Booleans to 32 bits to not cause heartburn for
654 * drivers that suddenly get an 8-bit load.
656 *size
= 4 * type
->components();
660 case GLSL_TYPE_UINT8
:
662 case GLSL_TYPE_UINT16
:
663 case GLSL_TYPE_INT16
:
664 case GLSL_TYPE_FLOAT16
:
667 case GLSL_TYPE_FLOAT
:
668 case GLSL_TYPE_DOUBLE
:
669 case GLSL_TYPE_UINT64
:
670 case GLSL_TYPE_INT64
: {
671 unsigned N
= glsl_get_bit_size(type
) / 8;
672 *size
= N
* type
->components();
677 case GLSL_TYPE_ARRAY
: {
678 unsigned elem_size
= 0, elem_align
= 0;
679 glsl_get_natural_size_align_bytes(type
->fields
.array
,
680 &elem_size
, &elem_align
);
682 *size
= type
->length
* ALIGN_POT(elem_size
, elem_align
);
686 case GLSL_TYPE_STRUCT
:
689 for (unsigned i
= 0; i
< type
->length
; i
++) {
690 unsigned elem_size
= 0, elem_align
= 0;
691 glsl_get_natural_size_align_bytes(type
->fields
.structure
[i
].type
,
692 &elem_size
, &elem_align
);
693 *align
= MAX2(*align
, elem_align
);
694 *size
= ALIGN_POT(*size
, elem_align
) + elem_size
;
698 case GLSL_TYPE_SAMPLER
:
699 case GLSL_TYPE_IMAGE
:
700 /* Bindless samplers and images. */
705 case GLSL_TYPE_ATOMIC_UINT
:
706 case GLSL_TYPE_SUBROUTINE
:
708 case GLSL_TYPE_ERROR
:
709 case GLSL_TYPE_INTERFACE
:
710 case GLSL_TYPE_FUNCTION
:
711 unreachable("type does not have a natural size");
716 glsl_atomic_uint_type(void)
718 return glsl_type::atomic_uint_type
;
722 glsl_atomic_size(const struct glsl_type
*type
)
724 return type
->atomic_size();
728 glsl_contains_atomic(const struct glsl_type
*type
)
730 return type
->contains_atomic();
734 glsl_contains_opaque(const struct glsl_type
*type
)
736 return type
->contains_opaque();
740 glsl_get_cl_size(const struct glsl_type
*type
)
742 return type
->cl_size();
746 glsl_get_cl_alignment(const struct glsl_type
*type
)
748 return type
->cl_alignment();
752 glsl_type_get_sampler_count(const struct glsl_type
*type
)
754 if (glsl_type_is_array(type
)) {
755 return (glsl_get_aoa_size(type
) *
756 glsl_type_get_sampler_count(glsl_without_array(type
)));
759 /* Ignore interface blocks - they can only contain bindless samplers,
760 * which we shouldn't count.
762 if (glsl_type_is_struct(type
)) {
764 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++)
765 count
+= glsl_type_get_sampler_count(glsl_get_struct_field(type
, i
));
769 if (glsl_type_is_sampler(type
))
776 glsl_type_get_image_count(const struct glsl_type
*type
)
778 if (glsl_type_is_array(type
)) {
779 return (glsl_get_aoa_size(type
) *
780 glsl_type_get_image_count(glsl_without_array(type
)));
783 /* Ignore interface blocks - they can only contain bindless images,
784 * which we shouldn't count.
786 if (glsl_type_is_struct(type
)) {
788 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++)
789 count
+= glsl_type_get_image_count(glsl_get_struct_field(type
, i
));
793 if (glsl_type_is_image(type
))
799 enum glsl_interface_packing
800 glsl_get_internal_ifc_packing(const struct glsl_type
*type
,
801 bool std430_supported
)
803 return type
->get_internal_ifc_packing(std430_supported
);
806 enum glsl_interface_packing
807 glsl_get_ifc_packing(const struct glsl_type
*type
)
809 return type
->get_interface_packing();
813 glsl_get_std140_base_alignment(const struct glsl_type
*type
, bool row_major
)
815 return type
->std140_base_alignment(row_major
);
819 glsl_get_std140_size(const struct glsl_type
*type
, bool row_major
)
821 return type
->std140_size(row_major
);
825 glsl_get_std430_base_alignment(const struct glsl_type
*type
, bool row_major
)
827 return type
->std430_base_alignment(row_major
);
831 glsl_get_std430_size(const struct glsl_type
*type
, bool row_major
)
833 return type
->std430_size(row_major
);
837 glsl_get_explicit_size(const struct glsl_type
*type
, bool align_to_stride
)
839 return type
->explicit_size(align_to_stride
);
843 glsl_type_is_leaf(const struct glsl_type
*type
)
845 if (glsl_type_is_struct_or_ifc(type
) ||
846 (glsl_type_is_array(type
) &&
847 (glsl_type_is_array(glsl_get_array_element(type
)) ||
848 glsl_type_is_struct_or_ifc(glsl_get_array_element(type
))))) {
855 const struct glsl_type
*
856 glsl_get_explicit_type_for_size_align(const struct glsl_type
*type
,
857 glsl_type_size_align_func type_info
,
858 unsigned *size
, unsigned *align
)
860 return type
->get_explicit_type_for_size_align(type_info
, size
, align
);