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