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