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_get_array_element(const glsl_type
* type
)
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
;
48 glsl_without_array(const glsl_type
*type
)
50 return type
->without_array();
54 glsl_without_array_or_matrix(const glsl_type
*type
)
56 type
= type
->without_array();
57 if (type
->is_matrix())
58 type
= type
->column_type();
63 glsl_get_bare_type(const glsl_type
*type
)
65 return type
->get_bare_type();
69 glsl_get_struct_field(const glsl_type
*type
, unsigned index
)
71 return type
->fields
.structure
[index
].type
;
75 glsl_get_struct_field_offset(const struct glsl_type
*type
,
78 return type
->fields
.structure
[index
].offset
;
81 const struct glsl_struct_field
*
82 glsl_get_struct_field_data(const struct glsl_type
*type
, unsigned index
)
84 assert(type
->is_struct() || type
->is_interface());
85 assert(index
< type
->length
);
86 return &type
->fields
.structure
[index
];
90 glsl_get_explicit_stride(const struct glsl_type
*type
)
92 return type
->explicit_stride
;
96 glsl_get_function_return_type(const glsl_type
*type
)
98 return type
->fields
.parameters
[0].type
;
101 const glsl_function_param
*
102 glsl_get_function_param(const glsl_type
*type
, unsigned index
)
104 return &type
->fields
.parameters
[index
+ 1];
107 const struct glsl_type
*
108 glsl_get_column_type(const struct glsl_type
*type
)
110 return type
->column_type();
114 glsl_get_gl_type(const struct glsl_type
*type
)
116 return type
->gl_type
;
120 glsl_get_base_type(const struct glsl_type
*type
)
122 return type
->base_type
;
126 glsl_get_vector_elements(const struct glsl_type
*type
)
128 return type
->vector_elements
;
132 glsl_get_components(const struct glsl_type
*type
)
134 return type
->components();
138 glsl_get_matrix_columns(const struct glsl_type
*type
)
140 return type
->matrix_columns
;
144 glsl_get_length(const struct glsl_type
*type
)
146 return type
->is_matrix() ? type
->matrix_columns
: type
->length
;
150 glsl_get_aoa_size(const struct glsl_type
*type
)
152 return type
->arrays_of_arrays_size();
156 glsl_count_attribute_slots(const struct glsl_type
*type
,
157 bool is_gl_vertex_input
)
159 return type
->count_attribute_slots(is_gl_vertex_input
);
163 glsl_get_component_slots(const struct glsl_type
*type
)
165 return type
->component_slots();
169 glsl_varying_count(const struct glsl_type
*type
)
171 return type
->varying_count();
175 glsl_get_struct_elem_name(const struct glsl_type
*type
, unsigned index
)
177 return type
->fields
.structure
[index
].name
;
181 glsl_get_sampler_dim(const struct glsl_type
*type
)
183 assert(glsl_type_is_sampler(type
) || glsl_type_is_image(type
));
184 return (glsl_sampler_dim
)type
->sampler_dimensionality
;
188 glsl_get_sampler_result_type(const struct glsl_type
*type
)
190 assert(glsl_type_is_sampler(type
) || glsl_type_is_image(type
));
191 return (glsl_base_type
)type
->sampled_type
;
195 glsl_get_sampler_target(const struct glsl_type
*type
)
197 assert(glsl_type_is_sampler(type
));
198 return type
->sampler_index();
202 glsl_get_sampler_coordinate_components(const struct glsl_type
*type
)
204 assert(glsl_type_is_sampler(type
) || glsl_type_is_image(type
));
205 return type
->coordinate_components();
209 glsl_get_struct_location_offset(const struct glsl_type
*type
,
212 return type
->struct_location_offset(length
);
216 glsl_type_is_16bit(const glsl_type
*type
)
218 return type
->is_16bit();
222 glsl_type_is_32bit(const glsl_type
*type
)
224 return type
->is_32bit();
228 glsl_type_is_64bit(const glsl_type
*type
)
230 return type
->is_64bit();
234 glsl_type_is_void(const glsl_type
*type
)
236 return type
->is_void();
240 glsl_type_is_error(const glsl_type
*type
)
242 return type
->is_error();
246 glsl_type_is_vector(const struct glsl_type
*type
)
248 return type
->is_vector();
252 glsl_type_is_scalar(const struct glsl_type
*type
)
254 return type
->is_scalar();
258 glsl_type_is_vector_or_scalar(const struct glsl_type
*type
)
260 return type
->is_vector() || type
->is_scalar();
264 glsl_type_is_matrix(const struct glsl_type
*type
)
266 return type
->is_matrix();
270 glsl_matrix_type_is_row_major(const struct glsl_type
*type
)
272 assert(type
->is_matrix() && type
->explicit_stride
);
273 return type
->interface_row_major
;
277 glsl_type_is_array(const struct glsl_type
*type
)
279 return type
->is_array();
283 glsl_type_is_array_of_arrays(const struct glsl_type
*type
)
285 return type
->is_array_of_arrays();
289 glsl_type_is_array_or_matrix(const struct glsl_type
*type
)
291 return type
->is_array() || type
->is_matrix();
295 glsl_type_is_struct(const struct glsl_type
*type
)
297 return type
->is_struct();
301 glsl_type_is_interface(const struct glsl_type
*type
)
303 return type
->is_interface();
307 glsl_type_is_struct_or_ifc(const struct glsl_type
*type
)
309 return type
->is_struct() || type
->is_interface();
313 glsl_type_is_sampler(const struct glsl_type
*type
)
315 return type
->is_sampler();
319 glsl_type_is_image(const struct glsl_type
*type
)
321 return type
->is_image();
325 glsl_sampler_type_is_shadow(const struct glsl_type
*type
)
327 assert(glsl_type_is_sampler(type
));
328 return type
->sampler_shadow
;
332 glsl_sampler_type_is_array(const struct glsl_type
*type
)
334 assert(glsl_type_is_sampler(type
) || glsl_type_is_image(type
));
335 return type
->sampler_array
;
339 glsl_type_is_dual_slot(const struct glsl_type
*type
)
341 return type
->is_dual_slot();
345 glsl_type_is_numeric(const struct glsl_type
*type
)
347 return type
->is_numeric();
351 glsl_type_is_boolean(const struct glsl_type
*type
)
353 return type
->is_boolean();
356 glsl_type_is_integer(const struct glsl_type
*type
)
358 return type
->is_integer();
362 glsl_type_contains_64bit(const struct glsl_type
*type
)
364 return type
->contains_64bit();
370 return glsl_type::void_type
;
374 glsl_float_type(void)
376 return glsl_type::float_type
;
380 glsl_double_type(void)
382 return glsl_type::double_type
;
386 glsl_float16_t_type(void)
388 return glsl_type::float16_t_type
;
392 glsl_vec_type(unsigned n
)
394 return glsl_type::vec(n
);
398 glsl_dvec_type(unsigned n
)
400 return glsl_type::dvec(n
);
406 return glsl_type::vec4_type
;
410 glsl_uvec4_type(void)
412 return glsl_type::uvec4_type
;
418 return glsl_type::int_type
;
424 return glsl_type::uint_type
;
428 glsl_int64_t_type(void)
430 return glsl_type::int64_t_type
;
434 glsl_uint64_t_type(void)
436 return glsl_type::uint64_t_type
;
440 glsl_int16_t_type(void)
442 return glsl_type::int16_t_type
;
446 glsl_uint16_t_type(void)
448 return glsl_type::uint16_t_type
;
452 glsl_int8_t_type(void)
454 return glsl_type::int8_t_type
;
458 glsl_uint8_t_type(void)
460 return glsl_type::uint8_t_type
;
466 return glsl_type::bool_type
;
470 glsl_scalar_type(enum glsl_base_type base_type
)
472 return glsl_type::get_instance(base_type
, 1, 1);
476 glsl_vector_type(enum glsl_base_type base_type
, unsigned components
)
478 const glsl_type
*t
= glsl_type::get_instance(base_type
, components
, 1);
479 assert(t
!= glsl_type::error_type
);
484 glsl_matrix_type(enum glsl_base_type base_type
, unsigned rows
, unsigned columns
)
486 const glsl_type
*t
= glsl_type::get_instance(base_type
, rows
, columns
);
487 assert(t
!= glsl_type::error_type
);
492 glsl_explicit_matrix_type(const glsl_type
*mat
,
493 unsigned stride
, bool row_major
)
496 const glsl_type
*t
= glsl_type::get_instance(mat
->base_type
,
497 mat
->vector_elements
,
500 assert(t
!= glsl_type::error_type
);
505 glsl_array_type(const glsl_type
*base
, unsigned elements
,
506 unsigned explicit_stride
)
508 return glsl_type::get_array_instance(base
, elements
, explicit_stride
);
512 glsl_struct_type(const glsl_struct_field
*fields
,
513 unsigned num_fields
, const char *name
,
516 return glsl_type::get_struct_instance(fields
, num_fields
, name
, packed
);
520 glsl_interface_type(const glsl_struct_field
*fields
,
522 enum glsl_interface_packing packing
,
524 const char *block_name
)
526 return glsl_type::get_interface_instance(fields
, num_fields
, packing
,
527 row_major
, block_name
);
530 const struct glsl_type
*
531 glsl_sampler_type(enum glsl_sampler_dim dim
, bool is_shadow
, bool is_array
,
532 enum glsl_base_type base_type
)
534 return glsl_type::get_sampler_instance(dim
, is_shadow
, is_array
, base_type
);
537 const struct glsl_type
*
538 glsl_bare_sampler_type()
540 return glsl_type::sampler_type
;
543 const struct glsl_type
*
544 glsl_image_type(enum glsl_sampler_dim dim
, bool is_array
,
545 enum glsl_base_type base_type
)
547 return glsl_type::get_image_instance(dim
, is_array
, base_type
);
551 glsl_function_type(const glsl_type
*return_type
,
552 const glsl_function_param
*params
, unsigned num_params
)
554 return glsl_type::get_function_instance(return_type
, params
, num_params
);
558 glsl_transposed_type(const struct glsl_type
*type
)
560 assert(glsl_type_is_matrix(type
));
561 return glsl_type::get_instance(type
->base_type
, type
->matrix_columns
,
562 type
->vector_elements
);
566 glsl_channel_type(const glsl_type
*t
)
568 switch (t
->base_type
) {
569 case GLSL_TYPE_ARRAY
:
570 return glsl_array_type(glsl_channel_type(t
->fields
.array
), t
->length
,
574 case GLSL_TYPE_FLOAT
:
575 case GLSL_TYPE_FLOAT16
:
576 case GLSL_TYPE_DOUBLE
:
577 case GLSL_TYPE_UINT8
:
579 case GLSL_TYPE_UINT16
:
580 case GLSL_TYPE_INT16
:
581 case GLSL_TYPE_UINT64
:
582 case GLSL_TYPE_INT64
:
584 return glsl_type::get_instance(t
->base_type
, 1, 1);
586 unreachable("Unhandled base type glsl_channel_type()");
591 glsl_get_natural_size_align_bytes(const struct glsl_type
*type
,
592 unsigned *size
, unsigned *align
)
594 switch (type
->base_type
) {
596 /* We special-case Booleans to 32 bits to not cause heartburn for
597 * drivers that suddenly get an 8-bit load.
599 *size
= 4 * type
->components();
603 case GLSL_TYPE_UINT8
:
605 case GLSL_TYPE_UINT16
:
606 case GLSL_TYPE_INT16
:
607 case GLSL_TYPE_FLOAT16
:
610 case GLSL_TYPE_FLOAT
:
611 case GLSL_TYPE_DOUBLE
:
612 case GLSL_TYPE_UINT64
:
613 case GLSL_TYPE_INT64
: {
614 unsigned N
= glsl_get_bit_size(type
) / 8;
615 *size
= N
* type
->components();
620 case GLSL_TYPE_ARRAY
: {
621 unsigned elem_size
, elem_align
;
622 glsl_get_natural_size_align_bytes(type
->fields
.array
,
623 &elem_size
, &elem_align
);
625 *size
= type
->length
* ALIGN_POT(elem_size
, elem_align
);
629 case GLSL_TYPE_STRUCT
:
632 for (unsigned i
= 0; i
< type
->length
; i
++) {
633 unsigned elem_size
, elem_align
;
634 glsl_get_natural_size_align_bytes(type
->fields
.structure
[i
].type
,
635 &elem_size
, &elem_align
);
636 *align
= MAX2(*align
, elem_align
);
637 *size
= ALIGN_POT(*size
, elem_align
) + elem_size
;
641 case GLSL_TYPE_SAMPLER
:
642 case GLSL_TYPE_ATOMIC_UINT
:
643 case GLSL_TYPE_SUBROUTINE
:
644 case GLSL_TYPE_IMAGE
:
646 case GLSL_TYPE_ERROR
:
647 case GLSL_TYPE_INTERFACE
:
648 case GLSL_TYPE_FUNCTION
:
649 unreachable("type does not have a natural size");
654 glsl_atomic_uint_type(void)
656 return glsl_type::atomic_uint_type
;
660 glsl_atomic_size(const struct glsl_type
*type
)
662 return type
->atomic_size();
666 glsl_contains_atomic(const struct glsl_type
*type
)
668 return type
->contains_atomic();
672 glsl_get_cl_size(const struct glsl_type
*type
)
674 return type
->cl_size();
678 glsl_get_cl_alignment(const struct glsl_type
*type
)
680 return type
->cl_alignment();
684 glsl_type_get_sampler_count(const struct glsl_type
*type
)
686 if (glsl_type_is_array(type
)) {
687 return (glsl_get_aoa_size(type
) *
688 glsl_type_get_sampler_count(glsl_without_array(type
)));
691 if (glsl_type_is_struct_or_ifc(type
)) {
693 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++)
694 count
+= glsl_type_get_sampler_count(glsl_get_struct_field(type
, i
));
698 if (glsl_type_is_sampler(type
))
705 glsl_type_get_image_count(const struct glsl_type
*type
)
707 if (glsl_type_is_array(type
)) {
708 return (glsl_get_aoa_size(type
) *
709 glsl_type_get_image_count(glsl_without_array(type
)));
712 if (glsl_type_is_struct_or_ifc(type
)) {
714 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++)
715 count
+= glsl_type_get_image_count(glsl_get_struct_field(type
, i
));
719 if (glsl_type_is_image(type
))