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
;
82 glsl_get_explicit_stride(const struct glsl_type
*type
)
84 return type
->explicit_stride
;
88 glsl_get_function_return_type(const glsl_type
*type
)
90 return type
->fields
.parameters
[0].type
;
93 const glsl_function_param
*
94 glsl_get_function_param(const glsl_type
*type
, unsigned index
)
96 return &type
->fields
.parameters
[index
+ 1];
99 const struct glsl_type
*
100 glsl_get_column_type(const struct glsl_type
*type
)
102 return type
->column_type();
106 glsl_get_gl_type(const struct glsl_type
*type
)
108 return type
->gl_type
;
112 glsl_get_base_type(const struct glsl_type
*type
)
114 return type
->base_type
;
118 glsl_get_vector_elements(const struct glsl_type
*type
)
120 return type
->vector_elements
;
124 glsl_get_components(const struct glsl_type
*type
)
126 return type
->components();
130 glsl_get_matrix_columns(const struct glsl_type
*type
)
132 return type
->matrix_columns
;
136 glsl_get_length(const struct glsl_type
*type
)
138 return type
->is_matrix() ? type
->matrix_columns
: type
->length
;
142 glsl_get_aoa_size(const struct glsl_type
*type
)
144 return type
->arrays_of_arrays_size();
148 glsl_count_attribute_slots(const struct glsl_type
*type
,
149 bool is_gl_vertex_input
)
151 return type
->count_attribute_slots(is_gl_vertex_input
);
155 glsl_get_component_slots(const struct glsl_type
*type
)
157 return type
->component_slots();
161 glsl_varying_count(const struct glsl_type
*type
)
163 return type
->varying_count();
167 glsl_get_struct_elem_name(const struct glsl_type
*type
, unsigned index
)
169 return type
->fields
.structure
[index
].name
;
173 glsl_get_sampler_dim(const struct glsl_type
*type
)
175 assert(glsl_type_is_sampler(type
) || glsl_type_is_image(type
));
176 return (glsl_sampler_dim
)type
->sampler_dimensionality
;
180 glsl_get_sampler_result_type(const struct glsl_type
*type
)
182 assert(glsl_type_is_sampler(type
) || glsl_type_is_image(type
));
183 return (glsl_base_type
)type
->sampled_type
;
187 glsl_get_sampler_target(const struct glsl_type
*type
)
189 assert(glsl_type_is_sampler(type
));
190 return type
->sampler_index();
194 glsl_get_sampler_coordinate_components(const struct glsl_type
*type
)
196 assert(glsl_type_is_sampler(type
) || glsl_type_is_image(type
));
197 return type
->coordinate_components();
201 glsl_get_struct_location_offset(const struct glsl_type
*type
,
204 return type
->struct_location_offset(length
);
208 glsl_type_is_16bit(const glsl_type
*type
)
210 return type
->is_16bit();
214 glsl_type_is_32bit(const glsl_type
*type
)
216 return type
->is_32bit();
220 glsl_type_is_64bit(const glsl_type
*type
)
222 return type
->is_64bit();
226 glsl_type_is_void(const glsl_type
*type
)
228 return type
->is_void();
232 glsl_type_is_error(const glsl_type
*type
)
234 return type
->is_error();
238 glsl_type_is_vector(const struct glsl_type
*type
)
240 return type
->is_vector();
244 glsl_type_is_scalar(const struct glsl_type
*type
)
246 return type
->is_scalar();
250 glsl_type_is_vector_or_scalar(const struct glsl_type
*type
)
252 return type
->is_vector() || type
->is_scalar();
256 glsl_type_is_matrix(const struct glsl_type
*type
)
258 return type
->is_matrix();
262 glsl_matrix_type_is_row_major(const struct glsl_type
*type
)
264 assert(type
->is_matrix() && type
->explicit_stride
);
265 return type
->interface_row_major
;
269 glsl_type_is_array(const struct glsl_type
*type
)
271 return type
->is_array();
275 glsl_type_is_array_of_arrays(const struct glsl_type
*type
)
277 return type
->is_array_of_arrays();
281 glsl_type_is_array_or_matrix(const struct glsl_type
*type
)
283 return type
->is_array() || type
->is_matrix();
287 glsl_type_is_struct(const struct glsl_type
*type
)
289 return type
->is_struct();
293 glsl_type_is_interface(const struct glsl_type
*type
)
295 return type
->is_interface();
299 glsl_type_is_struct_or_ifc(const struct glsl_type
*type
)
301 return type
->is_struct() || type
->is_interface();
305 glsl_type_is_sampler(const struct glsl_type
*type
)
307 return type
->is_sampler();
311 glsl_type_is_image(const struct glsl_type
*type
)
313 return type
->is_image();
317 glsl_sampler_type_is_shadow(const struct glsl_type
*type
)
319 assert(glsl_type_is_sampler(type
));
320 return type
->sampler_shadow
;
324 glsl_sampler_type_is_array(const struct glsl_type
*type
)
326 assert(glsl_type_is_sampler(type
) || glsl_type_is_image(type
));
327 return type
->sampler_array
;
331 glsl_type_is_dual_slot(const struct glsl_type
*type
)
333 return type
->is_dual_slot();
337 glsl_type_is_numeric(const struct glsl_type
*type
)
339 return type
->is_numeric();
343 glsl_type_is_boolean(const struct glsl_type
*type
)
345 return type
->is_boolean();
348 glsl_type_is_integer(const struct glsl_type
*type
)
350 return type
->is_integer();
354 glsl_type_contains_64bit(const struct glsl_type
*type
)
356 return type
->contains_64bit();
362 return glsl_type::void_type
;
366 glsl_float_type(void)
368 return glsl_type::float_type
;
372 glsl_double_type(void)
374 return glsl_type::double_type
;
378 glsl_float16_t_type(void)
380 return glsl_type::float16_t_type
;
384 glsl_vec_type(unsigned n
)
386 return glsl_type::vec(n
);
390 glsl_dvec_type(unsigned n
)
392 return glsl_type::dvec(n
);
398 return glsl_type::vec4_type
;
402 glsl_uvec4_type(void)
404 return glsl_type::uvec4_type
;
410 return glsl_type::int_type
;
416 return glsl_type::uint_type
;
420 glsl_int64_t_type(void)
422 return glsl_type::int64_t_type
;
426 glsl_uint64_t_type(void)
428 return glsl_type::uint64_t_type
;
432 glsl_int16_t_type(void)
434 return glsl_type::int16_t_type
;
438 glsl_uint16_t_type(void)
440 return glsl_type::uint16_t_type
;
444 glsl_int8_t_type(void)
446 return glsl_type::int8_t_type
;
450 glsl_uint8_t_type(void)
452 return glsl_type::uint8_t_type
;
458 return glsl_type::bool_type
;
462 glsl_scalar_type(enum glsl_base_type base_type
)
464 return glsl_type::get_instance(base_type
, 1, 1);
468 glsl_vector_type(enum glsl_base_type base_type
, unsigned components
)
470 const glsl_type
*t
= glsl_type::get_instance(base_type
, components
, 1);
471 assert(t
!= glsl_type::error_type
);
476 glsl_matrix_type(enum glsl_base_type base_type
, unsigned rows
, unsigned columns
)
478 const glsl_type
*t
= glsl_type::get_instance(base_type
, rows
, columns
);
479 assert(t
!= glsl_type::error_type
);
484 glsl_explicit_matrix_type(const glsl_type
*mat
,
485 unsigned stride
, bool row_major
)
488 const glsl_type
*t
= glsl_type::get_instance(mat
->base_type
,
489 mat
->vector_elements
,
492 assert(t
!= glsl_type::error_type
);
497 glsl_array_type(const glsl_type
*base
, unsigned elements
,
498 unsigned explicit_stride
)
500 return glsl_type::get_array_instance(base
, elements
, explicit_stride
);
504 glsl_struct_type(const glsl_struct_field
*fields
,
505 unsigned num_fields
, const char *name
)
507 return glsl_type::get_struct_instance(fields
, num_fields
, name
);
511 glsl_interface_type(const glsl_struct_field
*fields
,
513 enum glsl_interface_packing packing
,
515 const char *block_name
)
517 return glsl_type::get_interface_instance(fields
, num_fields
, packing
,
518 row_major
, block_name
);
521 const struct glsl_type
*
522 glsl_sampler_type(enum glsl_sampler_dim dim
, bool is_shadow
, bool is_array
,
523 enum glsl_base_type base_type
)
525 return glsl_type::get_sampler_instance(dim
, is_shadow
, is_array
, base_type
);
528 const struct glsl_type
*
529 glsl_bare_sampler_type()
531 return glsl_type::sampler_type
;
534 const struct glsl_type
*
535 glsl_image_type(enum glsl_sampler_dim dim
, bool is_array
,
536 enum glsl_base_type base_type
)
538 return glsl_type::get_image_instance(dim
, is_array
, base_type
);
542 glsl_function_type(const glsl_type
*return_type
,
543 const glsl_function_param
*params
, unsigned num_params
)
545 return glsl_type::get_function_instance(return_type
, params
, num_params
);
549 glsl_transposed_type(const struct glsl_type
*type
)
551 assert(glsl_type_is_matrix(type
));
552 return glsl_type::get_instance(type
->base_type
, type
->matrix_columns
,
553 type
->vector_elements
);
557 glsl_channel_type(const glsl_type
*t
)
559 switch (t
->base_type
) {
560 case GLSL_TYPE_ARRAY
:
561 return glsl_array_type(glsl_channel_type(t
->fields
.array
), t
->length
,
565 case GLSL_TYPE_FLOAT
:
566 case GLSL_TYPE_FLOAT16
:
567 case GLSL_TYPE_DOUBLE
:
568 case GLSL_TYPE_UINT8
:
570 case GLSL_TYPE_UINT16
:
571 case GLSL_TYPE_INT16
:
572 case GLSL_TYPE_UINT64
:
573 case GLSL_TYPE_INT64
:
575 return glsl_type::get_instance(t
->base_type
, 1, 1);
577 unreachable("Unhandled base type glsl_channel_type()");
582 glsl_get_natural_size_align_bytes(const struct glsl_type
*type
,
583 unsigned *size
, unsigned *align
)
585 switch (type
->base_type
) {
587 /* We special-case Booleans to 32 bits to not cause heartburn for
588 * drivers that suddenly get an 8-bit load.
590 *size
= 4 * type
->components();
594 case GLSL_TYPE_UINT8
:
596 case GLSL_TYPE_UINT16
:
597 case GLSL_TYPE_INT16
:
598 case GLSL_TYPE_FLOAT16
:
601 case GLSL_TYPE_FLOAT
:
602 case GLSL_TYPE_DOUBLE
:
603 case GLSL_TYPE_UINT64
:
604 case GLSL_TYPE_INT64
: {
605 unsigned N
= glsl_get_bit_size(type
) / 8;
606 *size
= N
* type
->components();
611 case GLSL_TYPE_ARRAY
: {
612 unsigned elem_size
, elem_align
;
613 glsl_get_natural_size_align_bytes(type
->fields
.array
,
614 &elem_size
, &elem_align
);
616 *size
= type
->length
* ALIGN_POT(elem_size
, elem_align
);
620 case GLSL_TYPE_STRUCT
:
623 for (unsigned i
= 0; i
< type
->length
; i
++) {
624 unsigned elem_size
, elem_align
;
625 glsl_get_natural_size_align_bytes(type
->fields
.structure
[i
].type
,
626 &elem_size
, &elem_align
);
627 *align
= MAX2(*align
, elem_align
);
628 *size
= ALIGN_POT(*size
, elem_align
) + elem_size
;
632 case GLSL_TYPE_SAMPLER
:
633 case GLSL_TYPE_ATOMIC_UINT
:
634 case GLSL_TYPE_SUBROUTINE
:
635 case GLSL_TYPE_IMAGE
:
637 case GLSL_TYPE_ERROR
:
638 case GLSL_TYPE_INTERFACE
:
639 case GLSL_TYPE_FUNCTION
:
640 unreachable("type does not have a natural size");
645 glsl_atomic_uint_type(void)
647 return glsl_type::atomic_uint_type
;
651 glsl_atomic_size(const struct glsl_type
*type
)
653 return type
->atomic_size();
657 glsl_contains_atomic(const struct glsl_type
*type
)
659 return type
->contains_atomic();