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_array_instance(const glsl_type
*type
,
66 return glsl_type::get_array_instance(type
, array_size
);
70 glsl_get_struct_field(const glsl_type
*type
, unsigned index
)
72 return type
->fields
.structure
[index
].type
;
76 glsl_get_function_return_type(const glsl_type
*type
)
78 return type
->fields
.parameters
[0].type
;
81 const glsl_function_param
*
82 glsl_get_function_param(const glsl_type
*type
, unsigned index
)
84 return &type
->fields
.parameters
[index
+ 1];
87 const struct glsl_type
*
88 glsl_get_column_type(const struct glsl_type
*type
)
90 return type
->column_type();
94 glsl_get_gl_type(const struct glsl_type
*type
)
100 glsl_get_base_type(const struct glsl_type
*type
)
102 return type
->base_type
;
106 glsl_get_vector_elements(const struct glsl_type
*type
)
108 return type
->vector_elements
;
112 glsl_get_components(const struct glsl_type
*type
)
114 return type
->components();
118 glsl_get_matrix_columns(const struct glsl_type
*type
)
120 return type
->matrix_columns
;
124 glsl_get_length(const struct glsl_type
*type
)
126 return type
->is_matrix() ? type
->matrix_columns
: type
->length
;
130 glsl_get_aoa_size(const struct glsl_type
*type
)
132 return type
->arrays_of_arrays_size();
136 glsl_count_attribute_slots(const struct glsl_type
*type
,
137 bool is_vertex_input
)
139 return type
->count_attribute_slots(is_vertex_input
);
143 glsl_get_component_slots(const struct glsl_type
*type
)
145 return type
->component_slots();
149 glsl_get_struct_elem_name(const struct glsl_type
*type
, unsigned index
)
151 return type
->fields
.structure
[index
].name
;
155 glsl_get_sampler_dim(const struct glsl_type
*type
)
157 assert(glsl_type_is_sampler(type
) || glsl_type_is_image(type
));
158 return (glsl_sampler_dim
)type
->sampler_dimensionality
;
162 glsl_get_sampler_result_type(const struct glsl_type
*type
)
164 assert(glsl_type_is_sampler(type
) || glsl_type_is_image(type
));
165 return (glsl_base_type
)type
->sampled_type
;
169 glsl_get_sampler_target(const struct glsl_type
*type
)
171 assert(glsl_type_is_sampler(type
));
172 return type
->sampler_index();
176 glsl_get_sampler_coordinate_components(const struct glsl_type
*type
)
178 assert(glsl_type_is_sampler(type
) || glsl_type_is_image(type
));
179 return type
->coordinate_components();
183 glsl_get_record_location_offset(const struct glsl_type
*type
,
186 return type
->record_location_offset(length
);
190 glsl_type_is_16bit(const glsl_type
*type
)
192 return type
->is_16bit();
196 glsl_type_is_64bit(const glsl_type
*type
)
198 return type
->is_64bit();
202 glsl_type_is_void(const glsl_type
*type
)
204 return type
->is_void();
208 glsl_type_is_error(const glsl_type
*type
)
210 return type
->is_error();
214 glsl_type_is_vector(const struct glsl_type
*type
)
216 return type
->is_vector();
220 glsl_type_is_scalar(const struct glsl_type
*type
)
222 return type
->is_scalar();
226 glsl_type_is_vector_or_scalar(const struct glsl_type
*type
)
228 return type
->is_vector() || type
->is_scalar();
232 glsl_type_is_matrix(const struct glsl_type
*type
)
234 return type
->is_matrix();
238 glsl_type_is_array(const struct glsl_type
*type
)
240 return type
->is_array();
244 glsl_type_is_array_of_arrays(const struct glsl_type
*type
)
246 return type
->is_array_of_arrays();
250 glsl_type_is_array_or_matrix(const struct glsl_type
*type
)
252 return type
->is_array() || type
->is_matrix();
256 glsl_type_is_struct(const struct glsl_type
*type
)
258 return type
->is_record() || type
->is_interface();
262 glsl_type_is_sampler(const struct glsl_type
*type
)
264 return type
->is_sampler();
268 glsl_type_is_image(const struct glsl_type
*type
)
270 return type
->is_image();
274 glsl_sampler_type_is_shadow(const struct glsl_type
*type
)
276 assert(glsl_type_is_sampler(type
));
277 return type
->sampler_shadow
;
281 glsl_sampler_type_is_array(const struct glsl_type
*type
)
283 assert(glsl_type_is_sampler(type
) || glsl_type_is_image(type
));
284 return type
->sampler_array
;
288 glsl_type_is_dual_slot(const struct glsl_type
*type
)
290 return type
->is_dual_slot();
294 glsl_type_is_numeric(const struct glsl_type
*type
)
296 return type
->is_numeric();
300 glsl_type_is_boolean(const struct glsl_type
*type
)
302 return type
->is_boolean();
305 glsl_type_is_integer(const struct glsl_type
*type
)
307 return type
->is_integer();
313 return glsl_type::void_type
;
317 glsl_float_type(void)
319 return glsl_type::float_type
;
323 glsl_double_type(void)
325 return glsl_type::double_type
;
329 glsl_float16_t_type(void)
331 return glsl_type::float16_t_type
;
335 glsl_vec_type(unsigned n
)
337 return glsl_type::vec(n
);
341 glsl_dvec_type(unsigned n
)
343 return glsl_type::dvec(n
);
349 return glsl_type::vec4_type
;
353 glsl_uvec4_type(void)
355 return glsl_type::uvec4_type
;
361 return glsl_type::int_type
;
367 return glsl_type::uint_type
;
371 glsl_int64_t_type(void)
373 return glsl_type::int64_t_type
;
377 glsl_uint64_t_type(void)
379 return glsl_type::uint64_t_type
;
383 glsl_int16_t_type(void)
385 return glsl_type::int16_t_type
;
389 glsl_uint16_t_type(void)
391 return glsl_type::uint16_t_type
;
395 glsl_int8_t_type(void)
397 return glsl_type::int8_t_type
;
401 glsl_uint8_t_type(void)
403 return glsl_type::uint8_t_type
;
409 return glsl_type::bool_type
;
413 glsl_scalar_type(enum glsl_base_type base_type
)
415 return glsl_type::get_instance(base_type
, 1, 1);
419 glsl_vector_type(enum glsl_base_type base_type
, unsigned components
)
421 const glsl_type
*t
= glsl_type::get_instance(base_type
, components
, 1);
422 assert(t
!= glsl_type::error_type
);
427 glsl_matrix_type(enum glsl_base_type base_type
, unsigned rows
, unsigned columns
)
429 const glsl_type
*t
= glsl_type::get_instance(base_type
, rows
, columns
);
430 assert(t
!= glsl_type::error_type
);
435 glsl_array_type(const glsl_type
*base
, unsigned elements
)
437 return glsl_type::get_array_instance(base
, elements
);
441 glsl_struct_type(const glsl_struct_field
*fields
,
442 unsigned num_fields
, const char *name
)
444 return glsl_type::get_record_instance(fields
, num_fields
, name
);
448 glsl_interface_type(const glsl_struct_field
*fields
,
450 enum glsl_interface_packing packing
,
452 const char *block_name
)
454 return glsl_type::get_interface_instance(fields
, num_fields
, packing
,
455 row_major
, block_name
);
458 const struct glsl_type
*
459 glsl_sampler_type(enum glsl_sampler_dim dim
, bool is_shadow
, bool is_array
,
460 enum glsl_base_type base_type
)
462 return glsl_type::get_sampler_instance(dim
, is_shadow
, is_array
, base_type
);
465 const struct glsl_type
*
466 glsl_bare_sampler_type()
468 return glsl_type::sampler_type
;
471 const struct glsl_type
*
472 glsl_image_type(enum glsl_sampler_dim dim
, bool is_array
,
473 enum glsl_base_type base_type
)
475 return glsl_type::get_image_instance(dim
, is_array
, base_type
);
479 glsl_function_type(const glsl_type
*return_type
,
480 const glsl_function_param
*params
, unsigned num_params
)
482 return glsl_type::get_function_instance(return_type
, params
, num_params
);
486 glsl_transposed_type(const struct glsl_type
*type
)
488 assert(glsl_type_is_matrix(type
));
489 return glsl_type::get_instance(type
->base_type
, type
->matrix_columns
,
490 type
->vector_elements
);
494 glsl_channel_type(const glsl_type
*t
)
496 switch (glsl_get_base_type(t
)) {
497 case GLSL_TYPE_ARRAY
: {
498 const glsl_type
*base
= glsl_channel_type(glsl_get_array_element(t
));
499 return glsl_array_type(base
, glsl_get_length(t
));
502 return glsl_uint_type();
504 return glsl_int_type();
505 case GLSL_TYPE_FLOAT
:
506 return glsl_float_type();
508 return glsl_bool_type();
509 case GLSL_TYPE_DOUBLE
:
510 return glsl_double_type();
511 case GLSL_TYPE_UINT64
:
512 return glsl_uint64_t_type();
513 case GLSL_TYPE_INT64
:
514 return glsl_int64_t_type();
515 case GLSL_TYPE_FLOAT16
:
516 return glsl_float16_t_type();
517 case GLSL_TYPE_UINT16
:
518 return glsl_uint16_t_type();
519 case GLSL_TYPE_INT16
:
520 return glsl_int16_t_type();
522 unreachable("Unhandled base type glsl_channel_type()");
527 glsl_get_natural_size_align_bytes(const struct glsl_type
*type
,
528 unsigned *size
, unsigned *align
)
530 switch (type
->base_type
) {
531 case GLSL_TYPE_UINT8
:
533 case GLSL_TYPE_UINT16
:
534 case GLSL_TYPE_INT16
:
535 case GLSL_TYPE_FLOAT16
:
538 case GLSL_TYPE_FLOAT
:
540 case GLSL_TYPE_DOUBLE
:
541 case GLSL_TYPE_UINT64
:
542 case GLSL_TYPE_INT64
: {
543 unsigned N
= glsl_get_bit_size(type
) / 8;
544 *size
= N
* type
->components();
549 case GLSL_TYPE_ARRAY
: {
550 unsigned elem_size
, elem_align
;
551 glsl_get_natural_size_align_bytes(type
->fields
.array
,
552 &elem_size
, &elem_align
);
554 *size
= type
->length
* ALIGN_POT(elem_size
, elem_align
);
558 case GLSL_TYPE_STRUCT
:
561 for (unsigned i
= 0; i
< type
->length
; i
++) {
562 unsigned elem_size
, elem_align
;
563 glsl_get_natural_size_align_bytes(type
->fields
.structure
[i
].type
,
564 &elem_size
, &elem_align
);
565 *align
= MAX2(*align
, elem_align
);
566 *size
= ALIGN_POT(*size
, elem_align
) + elem_size
;
570 case GLSL_TYPE_SAMPLER
:
571 case GLSL_TYPE_ATOMIC_UINT
:
572 case GLSL_TYPE_SUBROUTINE
:
573 case GLSL_TYPE_IMAGE
:
575 case GLSL_TYPE_ERROR
:
576 case GLSL_TYPE_INTERFACE
:
577 case GLSL_TYPE_FUNCTION
:
578 unreachable("type does not have a natural size");
583 glsl_atomic_uint_type(void)
585 return glsl_type::atomic_uint_type
;
589 glsl_atomic_size(const struct glsl_type
*type
)
591 return type
->atomic_size();
595 glsl_contains_atomic(const struct glsl_type
*type
)
597 return type
->contains_atomic();