c8a2940496909682d4c277b5f5388b1cfe7562af
[mesa.git] / src / compiler / nir_types.cpp
1 /*
2 * Copyright © 2014 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Connor Abbott (cwabbott0@gmail.com)
25 *
26 */
27
28 #include "nir_types.h"
29 #include "compiler/glsl/ir.h"
30
31 const char *
32 glsl_get_type_name(const glsl_type *type)
33 {
34 return type->name;
35 }
36
37 const glsl_type *
38 glsl_get_array_element(const glsl_type* type)
39 {
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;
45 }
46
47 const glsl_type *
48 glsl_without_array(const glsl_type *type)
49 {
50 return type->without_array();
51 }
52
53 const glsl_type *
54 glsl_get_array_instance(const glsl_type *type,
55 unsigned array_size)
56 {
57 return glsl_type::get_array_instance(type, array_size);
58 }
59
60 const glsl_type *
61 glsl_get_struct_field(const glsl_type *type, unsigned index)
62 {
63 return type->fields.structure[index].type;
64 }
65
66 const glsl_type *
67 glsl_get_function_return_type(const glsl_type *type)
68 {
69 return type->fields.parameters[0].type;
70 }
71
72 const glsl_function_param *
73 glsl_get_function_param(const glsl_type *type, unsigned index)
74 {
75 return &type->fields.parameters[index + 1];
76 }
77
78 const struct glsl_type *
79 glsl_get_column_type(const struct glsl_type *type)
80 {
81 return type->column_type();
82 }
83
84 GLenum
85 glsl_get_gl_type(const struct glsl_type *type)
86 {
87 return type->gl_type;
88 }
89
90 enum glsl_base_type
91 glsl_get_base_type(const struct glsl_type *type)
92 {
93 return type->base_type;
94 }
95
96 unsigned
97 glsl_get_vector_elements(const struct glsl_type *type)
98 {
99 return type->vector_elements;
100 }
101
102 unsigned
103 glsl_get_components(const struct glsl_type *type)
104 {
105 return type->components();
106 }
107
108 unsigned
109 glsl_get_matrix_columns(const struct glsl_type *type)
110 {
111 return type->matrix_columns;
112 }
113
114 unsigned
115 glsl_get_length(const struct glsl_type *type)
116 {
117 return type->is_matrix() ? type->matrix_columns : type->length;
118 }
119
120 unsigned
121 glsl_get_aoa_size(const struct glsl_type *type)
122 {
123 return type->arrays_of_arrays_size();
124 }
125
126 unsigned
127 glsl_count_attribute_slots(const struct glsl_type *type,
128 bool is_vertex_input)
129 {
130 return type->count_attribute_slots(is_vertex_input);
131 }
132
133 unsigned
134 glsl_get_component_slots(const struct glsl_type *type)
135 {
136 return type->component_slots();
137 }
138
139 const char *
140 glsl_get_struct_elem_name(const struct glsl_type *type, unsigned index)
141 {
142 return type->fields.structure[index].name;
143 }
144
145 glsl_sampler_dim
146 glsl_get_sampler_dim(const struct glsl_type *type)
147 {
148 assert(glsl_type_is_sampler(type) || glsl_type_is_image(type));
149 return (glsl_sampler_dim)type->sampler_dimensionality;
150 }
151
152 glsl_base_type
153 glsl_get_sampler_result_type(const struct glsl_type *type)
154 {
155 assert(glsl_type_is_sampler(type) || glsl_type_is_image(type));
156 return (glsl_base_type)type->sampled_type;
157 }
158
159 unsigned
160 glsl_get_sampler_target(const struct glsl_type *type)
161 {
162 assert(glsl_type_is_sampler(type));
163 return type->sampler_index();
164 }
165
166 unsigned
167 glsl_get_record_location_offset(const struct glsl_type *type,
168 unsigned length)
169 {
170 return type->record_location_offset(length);
171 }
172
173 bool
174 glsl_type_is_16bit(const glsl_type *type)
175 {
176 return type->is_16bit();
177 }
178
179 bool
180 glsl_type_is_64bit(const glsl_type *type)
181 {
182 return type->is_64bit();
183 }
184
185 bool
186 glsl_type_is_void(const glsl_type *type)
187 {
188 return type->is_void();
189 }
190
191 bool
192 glsl_type_is_error(const glsl_type *type)
193 {
194 return type->is_error();
195 }
196
197 bool
198 glsl_type_is_vector(const struct glsl_type *type)
199 {
200 return type->is_vector();
201 }
202
203 bool
204 glsl_type_is_scalar(const struct glsl_type *type)
205 {
206 return type->is_scalar();
207 }
208
209 bool
210 glsl_type_is_vector_or_scalar(const struct glsl_type *type)
211 {
212 return type->is_vector() || type->is_scalar();
213 }
214
215 bool
216 glsl_type_is_matrix(const struct glsl_type *type)
217 {
218 return type->is_matrix();
219 }
220
221 bool
222 glsl_type_is_array(const struct glsl_type *type)
223 {
224 return type->is_array();
225 }
226
227 bool
228 glsl_type_is_array_of_arrays(const struct glsl_type *type)
229 {
230 return type->is_array_of_arrays();
231 }
232
233 bool
234 glsl_type_is_struct(const struct glsl_type *type)
235 {
236 return type->is_record() || type->is_interface();
237 }
238
239 bool
240 glsl_type_is_sampler(const struct glsl_type *type)
241 {
242 return type->is_sampler();
243 }
244
245 bool
246 glsl_type_is_image(const struct glsl_type *type)
247 {
248 return type->is_image();
249 }
250
251 bool
252 glsl_sampler_type_is_shadow(const struct glsl_type *type)
253 {
254 assert(glsl_type_is_sampler(type));
255 return type->sampler_shadow;
256 }
257
258 bool
259 glsl_sampler_type_is_array(const struct glsl_type *type)
260 {
261 assert(glsl_type_is_sampler(type) || glsl_type_is_image(type));
262 return type->sampler_array;
263 }
264
265 bool
266 glsl_type_is_dual_slot(const struct glsl_type *type)
267 {
268 return type->is_dual_slot();
269 }
270
271 bool
272 glsl_type_is_numeric(const struct glsl_type *type)
273 {
274 return type->is_numeric();
275 }
276
277 bool
278 glsl_type_is_boolean(const struct glsl_type *type)
279 {
280 return type->is_boolean();
281 }
282
283 const glsl_type *
284 glsl_void_type(void)
285 {
286 return glsl_type::void_type;
287 }
288
289 const glsl_type *
290 glsl_float_type(void)
291 {
292 return glsl_type::float_type;
293 }
294
295 const glsl_type *
296 glsl_double_type(void)
297 {
298 return glsl_type::double_type;
299 }
300
301 const glsl_type *
302 glsl_float16_t_type(void)
303 {
304 return glsl_type::float16_t_type;
305 }
306
307 const glsl_type *
308 glsl_vec_type(unsigned n)
309 {
310 return glsl_type::vec(n);
311 }
312
313 const glsl_type *
314 glsl_dvec_type(unsigned n)
315 {
316 return glsl_type::dvec(n);
317 }
318
319 const glsl_type *
320 glsl_vec4_type(void)
321 {
322 return glsl_type::vec4_type;
323 }
324
325 const glsl_type *
326 glsl_uvec4_type(void)
327 {
328 return glsl_type::uvec4_type;
329 }
330
331 const glsl_type *
332 glsl_int_type(void)
333 {
334 return glsl_type::int_type;
335 }
336
337 const glsl_type *
338 glsl_uint_type(void)
339 {
340 return glsl_type::uint_type;
341 }
342
343 const glsl_type *
344 glsl_int64_t_type(void)
345 {
346 return glsl_type::int64_t_type;
347 }
348
349 const glsl_type *
350 glsl_uint64_t_type(void)
351 {
352 return glsl_type::uint64_t_type;
353 }
354
355 const glsl_type *
356 glsl_int16_t_type(void)
357 {
358 return glsl_type::int16_t_type;
359 }
360
361 const glsl_type *
362 glsl_uint16_t_type(void)
363 {
364 return glsl_type::uint16_t_type;
365 }
366
367 const glsl_type *
368 glsl_int8_t_type(void)
369 {
370 return glsl_type::int8_t_type;
371 }
372
373 const glsl_type *
374 glsl_uint8_t_type(void)
375 {
376 return glsl_type::uint8_t_type;
377 }
378
379 const glsl_type *
380 glsl_bool_type(void)
381 {
382 return glsl_type::bool_type;
383 }
384
385 const glsl_type *
386 glsl_scalar_type(enum glsl_base_type base_type)
387 {
388 return glsl_type::get_instance(base_type, 1, 1);
389 }
390
391 const glsl_type *
392 glsl_vector_type(enum glsl_base_type base_type, unsigned components)
393 {
394 const glsl_type *t = glsl_type::get_instance(base_type, components, 1);
395 assert(t != glsl_type::error_type);
396 return t;
397 }
398
399 const glsl_type *
400 glsl_matrix_type(enum glsl_base_type base_type, unsigned rows, unsigned columns)
401 {
402 const glsl_type *t = glsl_type::get_instance(base_type, rows, columns);
403 assert(t != glsl_type::error_type);
404 return t;
405 }
406
407 const glsl_type *
408 glsl_array_type(const glsl_type *base, unsigned elements)
409 {
410 return glsl_type::get_array_instance(base, elements);
411 }
412
413 const glsl_type *
414 glsl_struct_type(const glsl_struct_field *fields,
415 unsigned num_fields, const char *name)
416 {
417 return glsl_type::get_record_instance(fields, num_fields, name);
418 }
419
420 const glsl_type *
421 glsl_interface_type(const glsl_struct_field *fields,
422 unsigned num_fields,
423 enum glsl_interface_packing packing,
424 bool row_major,
425 const char *block_name)
426 {
427 return glsl_type::get_interface_instance(fields, num_fields, packing,
428 row_major, block_name);
429 }
430
431 const struct glsl_type *
432 glsl_sampler_type(enum glsl_sampler_dim dim, bool is_shadow, bool is_array,
433 enum glsl_base_type base_type)
434 {
435 return glsl_type::get_sampler_instance(dim, is_shadow, is_array, base_type);
436 }
437
438 const struct glsl_type *
439 glsl_bare_sampler_type()
440 {
441 return glsl_type::sampler_type;
442 }
443
444 const struct glsl_type *
445 glsl_image_type(enum glsl_sampler_dim dim, bool is_array,
446 enum glsl_base_type base_type)
447 {
448 return glsl_type::get_image_instance(dim, is_array, base_type);
449 }
450
451 const glsl_type *
452 glsl_function_type(const glsl_type *return_type,
453 const glsl_function_param *params, unsigned num_params)
454 {
455 return glsl_type::get_function_instance(return_type, params, num_params);
456 }
457
458 const glsl_type *
459 glsl_transposed_type(const struct glsl_type *type)
460 {
461 assert(glsl_type_is_matrix(type));
462 return glsl_type::get_instance(type->base_type, type->matrix_columns,
463 type->vector_elements);
464 }
465
466 const glsl_type *
467 glsl_channel_type(const glsl_type *t)
468 {
469 switch (glsl_get_base_type(t)) {
470 case GLSL_TYPE_ARRAY: {
471 const glsl_type *base = glsl_channel_type(glsl_get_array_element(t));
472 return glsl_array_type(base, glsl_get_length(t));
473 }
474 case GLSL_TYPE_UINT:
475 return glsl_uint_type();
476 case GLSL_TYPE_INT:
477 return glsl_int_type();
478 case GLSL_TYPE_FLOAT:
479 return glsl_float_type();
480 case GLSL_TYPE_BOOL:
481 return glsl_bool_type();
482 case GLSL_TYPE_DOUBLE:
483 return glsl_double_type();
484 case GLSL_TYPE_UINT64:
485 return glsl_uint64_t_type();
486 case GLSL_TYPE_INT64:
487 return glsl_int64_t_type();
488 case GLSL_TYPE_FLOAT16:
489 return glsl_float16_t_type();
490 case GLSL_TYPE_UINT16:
491 return glsl_uint16_t_type();
492 case GLSL_TYPE_INT16:
493 return glsl_int16_t_type();
494 default:
495 unreachable("Unhandled base type glsl_channel_type()");
496 }
497 }
498
499 void
500 glsl_get_natural_size_align_bytes(const struct glsl_type *type,
501 unsigned *size, unsigned *align)
502 {
503 switch (type->base_type) {
504 case GLSL_TYPE_UINT8:
505 case GLSL_TYPE_INT8:
506 case GLSL_TYPE_UINT16:
507 case GLSL_TYPE_INT16:
508 case GLSL_TYPE_FLOAT16:
509 case GLSL_TYPE_UINT:
510 case GLSL_TYPE_INT:
511 case GLSL_TYPE_FLOAT:
512 case GLSL_TYPE_BOOL:
513 case GLSL_TYPE_DOUBLE:
514 case GLSL_TYPE_UINT64:
515 case GLSL_TYPE_INT64: {
516 unsigned N = glsl_get_bit_size(type) / 8;
517 *size = N * type->components();
518 *align = N;
519 break;
520 }
521
522 case GLSL_TYPE_ARRAY: {
523 unsigned elem_size, elem_align;
524 glsl_get_natural_size_align_bytes(type->fields.array,
525 &elem_size, &elem_align);
526 *align = elem_align;
527 *size = type->length * ALIGN_POT(elem_size, elem_align);
528 break;
529 }
530
531 case GLSL_TYPE_STRUCT:
532 *size = 0;
533 *align = 0;
534 for (unsigned i = 0; i < type->length; i++) {
535 unsigned elem_size, elem_align;
536 glsl_get_natural_size_align_bytes(type->fields.structure[i].type,
537 &elem_size, &elem_align);
538 *align = MAX2(*align, elem_align);
539 *size = ALIGN_POT(*size, elem_align) + elem_size;
540 }
541 break;
542
543 case GLSL_TYPE_SAMPLER:
544 case GLSL_TYPE_ATOMIC_UINT:
545 case GLSL_TYPE_SUBROUTINE:
546 case GLSL_TYPE_IMAGE:
547 case GLSL_TYPE_VOID:
548 case GLSL_TYPE_ERROR:
549 case GLSL_TYPE_INTERFACE:
550 case GLSL_TYPE_FUNCTION:
551 unreachable("type does not have a natural size");
552 }
553 }
554
555 const glsl_type *
556 glsl_atomic_uint_type(void)
557 {
558 return glsl_type::atomic_uint_type;
559 }
560
561 unsigned
562 glsl_atomic_size(const struct glsl_type *type)
563 {
564 return type->atomic_size();
565 }
566
567 bool
568 glsl_contains_atomic(const struct glsl_type *type)
569 {
570 return type->contains_atomic();
571 }