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_get_array_instance(const glsl_type
*type
,
57 return glsl_type::get_array_instance(type
, array_size
);
61 glsl_get_struct_field(const glsl_type
*type
, unsigned index
)
63 return type
->fields
.structure
[index
].type
;
67 glsl_get_function_return_type(const glsl_type
*type
)
69 return type
->fields
.parameters
[0].type
;
72 const glsl_function_param
*
73 glsl_get_function_param(const glsl_type
*type
, unsigned index
)
75 return &type
->fields
.parameters
[index
+ 1];
78 const struct glsl_type
*
79 glsl_get_column_type(const struct glsl_type
*type
)
81 return type
->column_type();
85 glsl_get_base_type(const struct glsl_type
*type
)
87 return type
->base_type
;
91 glsl_get_vector_elements(const struct glsl_type
*type
)
93 return type
->vector_elements
;
97 glsl_get_components(const struct glsl_type
*type
)
99 return type
->components();
103 glsl_get_matrix_columns(const struct glsl_type
*type
)
105 return type
->matrix_columns
;
109 glsl_get_length(const struct glsl_type
*type
)
111 return type
->is_matrix() ? type
->matrix_columns
: type
->length
;
115 glsl_get_aoa_size(const struct glsl_type
*type
)
117 return type
->arrays_of_arrays_size();
121 glsl_count_attribute_slots(const struct glsl_type
*type
,
122 bool is_vertex_input
)
124 return type
->count_attribute_slots(is_vertex_input
);
128 glsl_get_component_slots(const struct glsl_type
*type
)
130 return type
->component_slots();
134 glsl_get_struct_elem_name(const struct glsl_type
*type
, unsigned index
)
136 return type
->fields
.structure
[index
].name
;
140 glsl_get_sampler_dim(const struct glsl_type
*type
)
142 assert(glsl_type_is_sampler(type
) || glsl_type_is_image(type
));
143 return (glsl_sampler_dim
)type
->sampler_dimensionality
;
147 glsl_get_sampler_result_type(const struct glsl_type
*type
)
149 assert(glsl_type_is_sampler(type
) || glsl_type_is_image(type
));
150 return (glsl_base_type
)type
->sampled_type
;
154 glsl_get_sampler_target(const struct glsl_type
*type
)
156 assert(glsl_type_is_sampler(type
));
157 return type
->sampler_index();
161 glsl_get_record_location_offset(const struct glsl_type
*type
,
164 return type
->record_location_offset(length
);
168 glsl_type_is_16bit(const glsl_type
*type
)
170 return type
->is_16bit();
174 glsl_type_is_64bit(const glsl_type
*type
)
176 return type
->is_64bit();
180 glsl_type_is_void(const glsl_type
*type
)
182 return type
->is_void();
186 glsl_type_is_error(const glsl_type
*type
)
188 return type
->is_error();
192 glsl_type_is_vector(const struct glsl_type
*type
)
194 return type
->is_vector();
198 glsl_type_is_scalar(const struct glsl_type
*type
)
200 return type
->is_scalar();
204 glsl_type_is_vector_or_scalar(const struct glsl_type
*type
)
206 return type
->is_vector() || type
->is_scalar();
210 glsl_type_is_matrix(const struct glsl_type
*type
)
212 return type
->is_matrix();
216 glsl_type_is_array(const struct glsl_type
*type
)
218 return type
->is_array();
222 glsl_type_is_array_of_arrays(const struct glsl_type
*type
)
224 return type
->is_array_of_arrays();
228 glsl_type_is_struct(const struct glsl_type
*type
)
230 return type
->is_record() || type
->is_interface();
234 glsl_type_is_sampler(const struct glsl_type
*type
)
236 return type
->is_sampler();
240 glsl_type_is_image(const struct glsl_type
*type
)
242 return type
->is_image();
246 glsl_sampler_type_is_shadow(const struct glsl_type
*type
)
248 assert(glsl_type_is_sampler(type
));
249 return type
->sampler_shadow
;
253 glsl_sampler_type_is_array(const struct glsl_type
*type
)
255 assert(glsl_type_is_sampler(type
) || glsl_type_is_image(type
));
256 return type
->sampler_array
;
260 glsl_type_is_dual_slot(const struct glsl_type
*type
)
262 return type
->is_dual_slot();
266 glsl_type_is_numeric(const struct glsl_type
*type
)
268 return type
->is_numeric();
272 glsl_type_is_boolean(const struct glsl_type
*type
)
274 return type
->is_boolean();
280 return glsl_type::void_type
;
284 glsl_float_type(void)
286 return glsl_type::float_type
;
290 glsl_double_type(void)
292 return glsl_type::double_type
;
296 glsl_float16_t_type(void)
298 return glsl_type::float16_t_type
;
302 glsl_vec_type(unsigned n
)
304 return glsl_type::vec(n
);
308 glsl_dvec_type(unsigned n
)
310 return glsl_type::dvec(n
);
316 return glsl_type::vec4_type
;
320 glsl_uvec4_type(void)
322 return glsl_type::uvec4_type
;
328 return glsl_type::int_type
;
334 return glsl_type::uint_type
;
338 glsl_int64_t_type(void)
340 return glsl_type::int64_t_type
;
344 glsl_uint64_t_type(void)
346 return glsl_type::uint64_t_type
;
350 glsl_int16_t_type(void)
352 return glsl_type::int16_t_type
;
356 glsl_uint16_t_type(void)
358 return glsl_type::uint16_t_type
;
362 glsl_int8_t_type(void)
364 return glsl_type::int8_t_type
;
368 glsl_uint8_t_type(void)
370 return glsl_type::uint8_t_type
;
376 return glsl_type::bool_type
;
380 glsl_scalar_type(enum glsl_base_type base_type
)
382 return glsl_type::get_instance(base_type
, 1, 1);
386 glsl_vector_type(enum glsl_base_type base_type
, unsigned components
)
388 const glsl_type
*t
= glsl_type::get_instance(base_type
, components
, 1);
389 assert(t
!= glsl_type::error_type
);
394 glsl_matrix_type(enum glsl_base_type base_type
, unsigned rows
, unsigned columns
)
396 const glsl_type
*t
= glsl_type::get_instance(base_type
, rows
, columns
);
397 assert(t
!= glsl_type::error_type
);
402 glsl_array_type(const glsl_type
*base
, unsigned elements
)
404 return glsl_type::get_array_instance(base
, elements
);
408 glsl_struct_type(const glsl_struct_field
*fields
,
409 unsigned num_fields
, const char *name
)
411 return glsl_type::get_record_instance(fields
, num_fields
, name
);
415 glsl_interface_type(const glsl_struct_field
*fields
,
417 enum glsl_interface_packing packing
,
419 const char *block_name
)
421 return glsl_type::get_interface_instance(fields
, num_fields
, packing
,
422 row_major
, block_name
);
425 const struct glsl_type
*
426 glsl_sampler_type(enum glsl_sampler_dim dim
, bool is_shadow
, bool is_array
,
427 enum glsl_base_type base_type
)
429 return glsl_type::get_sampler_instance(dim
, is_shadow
, is_array
, base_type
);
432 const struct glsl_type
*
433 glsl_bare_sampler_type()
435 return glsl_type::sampler_type
;
438 const struct glsl_type
*
439 glsl_image_type(enum glsl_sampler_dim dim
, bool is_array
,
440 enum glsl_base_type base_type
)
442 return glsl_type::get_image_instance(dim
, is_array
, base_type
);
446 glsl_function_type(const glsl_type
*return_type
,
447 const glsl_function_param
*params
, unsigned num_params
)
449 return glsl_type::get_function_instance(return_type
, params
, num_params
);
453 glsl_transposed_type(const struct glsl_type
*type
)
455 assert(glsl_type_is_matrix(type
));
456 return glsl_type::get_instance(type
->base_type
, type
->matrix_columns
,
457 type
->vector_elements
);
461 glsl_channel_type(const glsl_type
*t
)
463 switch (glsl_get_base_type(t
)) {
464 case GLSL_TYPE_ARRAY
: {
465 const glsl_type
*base
= glsl_channel_type(glsl_get_array_element(t
));
466 return glsl_array_type(base
, glsl_get_length(t
));
469 return glsl_uint_type();
471 return glsl_int_type();
472 case GLSL_TYPE_FLOAT
:
473 return glsl_float_type();
475 return glsl_bool_type();
476 case GLSL_TYPE_DOUBLE
:
477 return glsl_double_type();
478 case GLSL_TYPE_UINT64
:
479 return glsl_uint64_t_type();
480 case GLSL_TYPE_INT64
:
481 return glsl_int64_t_type();
482 case GLSL_TYPE_FLOAT16
:
483 return glsl_float16_t_type();
484 case GLSL_TYPE_UINT16
:
485 return glsl_uint16_t_type();
486 case GLSL_TYPE_INT16
:
487 return glsl_int16_t_type();
489 unreachable("Unhandled base type glsl_channel_type()");
494 glsl_get_natural_size_align_bytes(const struct glsl_type
*type
,
495 unsigned *size
, unsigned *align
)
497 switch (type
->base_type
) {
498 case GLSL_TYPE_UINT8
:
500 case GLSL_TYPE_UINT16
:
501 case GLSL_TYPE_INT16
:
502 case GLSL_TYPE_FLOAT16
:
505 case GLSL_TYPE_FLOAT
:
507 case GLSL_TYPE_DOUBLE
:
508 case GLSL_TYPE_UINT64
:
509 case GLSL_TYPE_INT64
: {
510 unsigned N
= glsl_get_bit_size(type
) / 8;
511 *size
= N
* type
->components();
516 case GLSL_TYPE_ARRAY
: {
517 unsigned elem_size
, elem_align
;
518 glsl_get_natural_size_align_bytes(type
->fields
.array
,
519 &elem_size
, &elem_align
);
521 *size
= type
->length
* ALIGN_POT(elem_size
, elem_align
);
525 case GLSL_TYPE_STRUCT
:
528 for (unsigned i
= 0; i
< type
->length
; i
++) {
529 unsigned elem_size
, elem_align
;
530 glsl_get_natural_size_align_bytes(type
->fields
.structure
[i
].type
,
531 &elem_size
, &elem_align
);
532 *align
= MAX2(*align
, elem_align
);
533 *size
= ALIGN_POT(*size
, elem_align
) + elem_size
;
537 case GLSL_TYPE_SAMPLER
:
538 case GLSL_TYPE_ATOMIC_UINT
:
539 case GLSL_TYPE_SUBROUTINE
:
540 case GLSL_TYPE_IMAGE
:
542 case GLSL_TYPE_ERROR
:
543 case GLSL_TYPE_INTERFACE
:
544 case GLSL_TYPE_FUNCTION
:
545 unreachable("type does not have a natural size");
550 glsl_atomic_uint_type(void)
552 return glsl_type::atomic_uint_type
;
556 glsl_atomic_size(const struct glsl_type
*type
)
558 return type
->atomic_size();
562 glsl_contains_atomic(const struct glsl_type
*type
)
564 return type
->contains_atomic();