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 "nir_gl_types.h"
30 #include "compiler/glsl/ir.h"
31
32 const char *
glsl_get_type_name(const glsl_type * type)33 glsl_get_type_name(const glsl_type *type)
34 {
35 return type->name;
36 }
37
38 int
glsl_array_size(const struct glsl_type * type)39 glsl_array_size(const struct glsl_type *type)
40 {
41 return type->array_size();
42 }
43
44 const glsl_type *
glsl_get_array_element(const glsl_type * type)45 glsl_get_array_element(const glsl_type* type)
46 {
47 if (type->is_matrix())
48 return type->column_type();
49 else if (type->is_vector())
50 return type->get_scalar_type();
51 return type->fields.array;
52 }
53
54 const glsl_type *
glsl_without_array(const glsl_type * type)55 glsl_without_array(const glsl_type *type)
56 {
57 return type->without_array();
58 }
59
60 const glsl_type *
glsl_without_array_or_matrix(const glsl_type * type)61 glsl_without_array_or_matrix(const glsl_type *type)
62 {
63 type = type->without_array();
64 if (type->is_matrix())
65 type = type->column_type();
66 return type;
67 }
68
69 const glsl_type *
glsl_get_bare_type(const glsl_type * type)70 glsl_get_bare_type(const glsl_type *type)
71 {
72 return type->get_bare_type();
73 }
74
75 const glsl_type *
glsl_get_struct_field(const glsl_type * type,unsigned index)76 glsl_get_struct_field(const glsl_type *type, unsigned index)
77 {
78 return type->fields.structure[index].type;
79 }
80
81 int
glsl_get_struct_field_offset(const struct glsl_type * type,unsigned index)82 glsl_get_struct_field_offset(const struct glsl_type *type,
83 unsigned index)
84 {
85 return type->fields.structure[index].offset;
86 }
87
88 const struct glsl_struct_field *
glsl_get_struct_field_data(const struct glsl_type * type,unsigned index)89 glsl_get_struct_field_data(const struct glsl_type *type, unsigned index)
90 {
91 assert(type->is_struct() || type->is_interface());
92 assert(index < type->length);
93 return &type->fields.structure[index];
94 }
95
96 unsigned
glsl_get_explicit_stride(const struct glsl_type * type)97 glsl_get_explicit_stride(const struct glsl_type *type)
98 {
99 return type->explicit_stride;
100 }
101
102 const glsl_type *
glsl_get_function_return_type(const glsl_type * type)103 glsl_get_function_return_type(const glsl_type *type)
104 {
105 return type->fields.parameters[0].type;
106 }
107
108 const glsl_function_param *
glsl_get_function_param(const glsl_type * type,unsigned index)109 glsl_get_function_param(const glsl_type *type, unsigned index)
110 {
111 return &type->fields.parameters[index + 1];
112 }
113
114 const glsl_type *
glsl_texture_type_to_sampler(const glsl_type * type,bool is_shadow)115 glsl_texture_type_to_sampler(const glsl_type *type, bool is_shadow)
116 {
117 assert(glsl_type_is_texture(type));
118 return glsl_sampler_type((glsl_sampler_dim)type->sampler_dimensionality,
119 is_shadow, type->sampler_array,
120 (glsl_base_type)type->sampled_type);
121 }
122
123 const glsl_type *
glsl_sampler_type_to_texture(const glsl_type * type)124 glsl_sampler_type_to_texture(const glsl_type *type)
125 {
126 assert(glsl_type_is_sampler(type) && !glsl_type_is_bare_sampler(type));
127 return glsl_texture_type((glsl_sampler_dim)type->sampler_dimensionality,
128 type->sampler_array,
129 (glsl_base_type)type->sampled_type);
130 }
131
132 const struct glsl_type *
glsl_get_column_type(const struct glsl_type * type)133 glsl_get_column_type(const struct glsl_type *type)
134 {
135 return type->column_type();
136 }
137
138 GLenum
glsl_get_gl_type(const struct glsl_type * type)139 glsl_get_gl_type(const struct glsl_type *type)
140 {
141 return type->gl_type;
142 }
143
144 enum glsl_base_type
glsl_get_base_type(const struct glsl_type * type)145 glsl_get_base_type(const struct glsl_type *type)
146 {
147 return type->base_type;
148 }
149
150 unsigned
glsl_get_vector_elements(const struct glsl_type * type)151 glsl_get_vector_elements(const struct glsl_type *type)
152 {
153 return type->vector_elements;
154 }
155
156 unsigned
glsl_get_components(const struct glsl_type * type)157 glsl_get_components(const struct glsl_type *type)
158 {
159 return type->components();
160 }
161
162 unsigned
glsl_get_matrix_columns(const struct glsl_type * type)163 glsl_get_matrix_columns(const struct glsl_type *type)
164 {
165 return type->matrix_columns;
166 }
167
168 unsigned
glsl_get_length(const struct glsl_type * type)169 glsl_get_length(const struct glsl_type *type)
170 {
171 return type->is_matrix() ? type->matrix_columns : type->length;
172 }
173
174 unsigned
glsl_get_aoa_size(const struct glsl_type * type)175 glsl_get_aoa_size(const struct glsl_type *type)
176 {
177 return type->arrays_of_arrays_size();
178 }
179
180 unsigned
glsl_count_vec4_slots(const struct glsl_type * type,bool is_gl_vertex_input,bool is_bindless)181 glsl_count_vec4_slots(const struct glsl_type *type,
182 bool is_gl_vertex_input, bool is_bindless)
183 {
184 return type->count_vec4_slots(is_gl_vertex_input, is_bindless);
185 }
186
187 unsigned
glsl_count_dword_slots(const struct glsl_type * type,bool is_bindless)188 glsl_count_dword_slots(const struct glsl_type *type, bool is_bindless)
189 {
190 return type->count_dword_slots(is_bindless);
191 }
192
193 unsigned
glsl_count_attribute_slots(const struct glsl_type * type,bool is_gl_vertex_input)194 glsl_count_attribute_slots(const struct glsl_type *type,
195 bool is_gl_vertex_input)
196 {
197 return type->count_attribute_slots(is_gl_vertex_input);
198 }
199
200 unsigned
glsl_get_component_slots(const struct glsl_type * type)201 glsl_get_component_slots(const struct glsl_type *type)
202 {
203 return type->component_slots();
204 }
205
206 unsigned
glsl_get_component_slots_aligned(const struct glsl_type * type,unsigned offset)207 glsl_get_component_slots_aligned(const struct glsl_type *type, unsigned offset)
208 {
209 return type->component_slots_aligned(offset);
210 }
211
212 unsigned
glsl_varying_count(const struct glsl_type * type)213 glsl_varying_count(const struct glsl_type *type)
214 {
215 return type->varying_count();
216 }
217
218 const char *
glsl_get_struct_elem_name(const struct glsl_type * type,unsigned index)219 glsl_get_struct_elem_name(const struct glsl_type *type, unsigned index)
220 {
221 return type->fields.structure[index].name;
222 }
223
224 glsl_sampler_dim
glsl_get_sampler_dim(const struct glsl_type * type)225 glsl_get_sampler_dim(const struct glsl_type *type)
226 {
227 assert(glsl_type_is_sampler(type) ||
228 glsl_type_is_texture(type) ||
229 glsl_type_is_image(type));
230 return (glsl_sampler_dim)type->sampler_dimensionality;
231 }
232
233 glsl_base_type
glsl_get_sampler_result_type(const struct glsl_type * type)234 glsl_get_sampler_result_type(const struct glsl_type *type)
235 {
236 assert(glsl_type_is_sampler(type) ||
237 glsl_type_is_texture(type) ||
238 glsl_type_is_image(type));
239 return (glsl_base_type)type->sampled_type;
240 }
241
242 unsigned
glsl_get_sampler_target(const struct glsl_type * type)243 glsl_get_sampler_target(const struct glsl_type *type)
244 {
245 assert(glsl_type_is_sampler(type));
246 return type->sampler_index();
247 }
248
249 int
glsl_get_sampler_coordinate_components(const struct glsl_type * type)250 glsl_get_sampler_coordinate_components(const struct glsl_type *type)
251 {
252 assert(glsl_type_is_sampler(type) ||
253 glsl_type_is_texture(type) ||
254 glsl_type_is_image(type));
255 return type->coordinate_components();
256 }
257
258 unsigned
glsl_get_struct_location_offset(const struct glsl_type * type,unsigned length)259 glsl_get_struct_location_offset(const struct glsl_type *type,
260 unsigned length)
261 {
262 return type->struct_location_offset(length);
263 }
264
265 bool
glsl_type_is_16bit(const glsl_type * type)266 glsl_type_is_16bit(const glsl_type *type)
267 {
268 return type->is_16bit();
269 }
270
271 bool
glsl_type_is_32bit(const glsl_type * type)272 glsl_type_is_32bit(const glsl_type *type)
273 {
274 return type->is_32bit();
275 }
276
277 bool
glsl_type_is_64bit(const glsl_type * type)278 glsl_type_is_64bit(const glsl_type *type)
279 {
280 return type->is_64bit();
281 }
282
283 bool
glsl_type_is_void(const glsl_type * type)284 glsl_type_is_void(const glsl_type *type)
285 {
286 return type->is_void();
287 }
288
289 bool
glsl_type_is_error(const glsl_type * type)290 glsl_type_is_error(const glsl_type *type)
291 {
292 return type->is_error();
293 }
294
295 bool
glsl_type_is_vector(const struct glsl_type * type)296 glsl_type_is_vector(const struct glsl_type *type)
297 {
298 return type->is_vector();
299 }
300
301 bool
glsl_type_is_scalar(const struct glsl_type * type)302 glsl_type_is_scalar(const struct glsl_type *type)
303 {
304 return type->is_scalar();
305 }
306
307 bool
glsl_type_is_vector_or_scalar(const struct glsl_type * type)308 glsl_type_is_vector_or_scalar(const struct glsl_type *type)
309 {
310 return type->is_vector() || type->is_scalar();
311 }
312
313 bool
glsl_type_is_matrix(const struct glsl_type * type)314 glsl_type_is_matrix(const struct glsl_type *type)
315 {
316 return type->is_matrix();
317 }
318
319 bool
glsl_matrix_type_is_row_major(const struct glsl_type * type)320 glsl_matrix_type_is_row_major(const struct glsl_type *type)
321 {
322 assert((type->is_matrix() && type->explicit_stride) || type->is_interface());
323 return type->interface_row_major;
324 }
325
326 bool
glsl_type_is_array(const struct glsl_type * type)327 glsl_type_is_array(const struct glsl_type *type)
328 {
329 return type->is_array();
330 }
331
332 bool
glsl_type_is_unsized_array(const struct glsl_type * type)333 glsl_type_is_unsized_array(const struct glsl_type *type)
334 {
335 return type->is_unsized_array();
336 }
337
338 bool
glsl_type_is_array_of_arrays(const struct glsl_type * type)339 glsl_type_is_array_of_arrays(const struct glsl_type *type)
340 {
341 return type->is_array_of_arrays();
342 }
343
344 bool
glsl_type_is_array_or_matrix(const struct glsl_type * type)345 glsl_type_is_array_or_matrix(const struct glsl_type *type)
346 {
347 return type->is_array() || type->is_matrix();
348 }
349
350 bool
glsl_type_is_struct(const struct glsl_type * type)351 glsl_type_is_struct(const struct glsl_type *type)
352 {
353 return type->is_struct();
354 }
355
356 bool
glsl_type_is_interface(const struct glsl_type * type)357 glsl_type_is_interface(const struct glsl_type *type)
358 {
359 return type->is_interface();
360 }
361
362 bool
glsl_type_is_struct_or_ifc(const struct glsl_type * type)363 glsl_type_is_struct_or_ifc(const struct glsl_type *type)
364 {
365 return type->is_struct() || type->is_interface();
366 }
367
368 bool
glsl_type_is_sampler(const struct glsl_type * type)369 glsl_type_is_sampler(const struct glsl_type *type)
370 {
371 return type->is_sampler();
372 }
373
374 bool
glsl_type_is_bare_sampler(const struct glsl_type * type)375 glsl_type_is_bare_sampler(const struct glsl_type *type)
376 {
377 return type->is_sampler() && type->sampled_type == GLSL_TYPE_VOID;
378 }
379
380 bool
glsl_type_is_texture(const struct glsl_type * type)381 glsl_type_is_texture(const struct glsl_type *type)
382 {
383 return type->is_texture();
384 }
385
386 bool
glsl_type_is_image(const struct glsl_type * type)387 glsl_type_is_image(const struct glsl_type *type)
388 {
389 return type->is_image();
390 }
391
392 bool
glsl_sampler_type_is_shadow(const struct glsl_type * type)393 glsl_sampler_type_is_shadow(const struct glsl_type *type)
394 {
395 assert(glsl_type_is_sampler(type));
396 return type->sampler_shadow;
397 }
398
399 bool
glsl_sampler_type_is_array(const struct glsl_type * type)400 glsl_sampler_type_is_array(const struct glsl_type *type)
401 {
402 assert(glsl_type_is_sampler(type) ||
403 glsl_type_is_texture(type) ||
404 glsl_type_is_image(type));
405 return type->sampler_array;
406 }
407
408 bool
glsl_struct_type_is_packed(const struct glsl_type * type)409 glsl_struct_type_is_packed(const struct glsl_type *type)
410 {
411 assert(glsl_type_is_struct(type));
412 return type->packed;
413 }
414
415 bool
glsl_type_is_dual_slot(const struct glsl_type * type)416 glsl_type_is_dual_slot(const struct glsl_type *type)
417 {
418 return type->is_dual_slot();
419 }
420
421 bool
glsl_type_is_numeric(const struct glsl_type * type)422 glsl_type_is_numeric(const struct glsl_type *type)
423 {
424 return type->is_numeric();
425 }
426
427 bool
glsl_type_is_boolean(const struct glsl_type * type)428 glsl_type_is_boolean(const struct glsl_type *type)
429 {
430 return type->is_boolean();
431 }
432 bool
glsl_type_is_integer(const struct glsl_type * type)433 glsl_type_is_integer(const struct glsl_type *type)
434 {
435 return type->is_integer();
436 }
437
438 bool
glsl_type_contains_64bit(const struct glsl_type * type)439 glsl_type_contains_64bit(const struct glsl_type *type)
440 {
441 return type->contains_64bit();
442 }
443
444 bool
glsl_type_contains_image(const struct glsl_type * type)445 glsl_type_contains_image(const struct glsl_type *type)
446 {
447 return type->contains_image();
448 }
449
450 bool
glsl_contains_double(const struct glsl_type * type)451 glsl_contains_double(const struct glsl_type *type)
452 {
453 return type->contains_double();
454 }
455
456 bool
glsl_contains_integer(const struct glsl_type * type)457 glsl_contains_integer(const struct glsl_type *type)
458 {
459 return type->contains_integer();
460 }
461
462 bool
glsl_record_compare(const struct glsl_type * a,const struct glsl_type * b,bool match_name,bool match_locations,bool match_precision)463 glsl_record_compare(const struct glsl_type *a, const struct glsl_type *b,
464 bool match_name, bool match_locations, bool match_precision)
465 {
466 return a->record_compare(b, match_name, match_locations, match_precision);
467 }
468
469 const glsl_type *
glsl_void_type(void)470 glsl_void_type(void)
471 {
472 return glsl_type::void_type;
473 }
474
475 const glsl_type *
glsl_float_type(void)476 glsl_float_type(void)
477 {
478 return glsl_type::float_type;
479 }
480
481 const glsl_type *
glsl_double_type(void)482 glsl_double_type(void)
483 {
484 return glsl_type::double_type;
485 }
486
487 const glsl_type *
glsl_float16_t_type(void)488 glsl_float16_t_type(void)
489 {
490 return glsl_type::float16_t_type;
491 }
492
493 const glsl_type *
glsl_floatN_t_type(unsigned bit_size)494 glsl_floatN_t_type(unsigned bit_size)
495 {
496 switch (bit_size) {
497 case 16: return glsl_type::float16_t_type;
498 case 32: return glsl_type::float_type;
499 case 64: return glsl_type::double_type;
500 default:
501 unreachable("Unsupported bit size");
502 }
503 }
504
505 const glsl_type *
glsl_vec_type(unsigned n)506 glsl_vec_type(unsigned n)
507 {
508 return glsl_type::vec(n);
509 }
510
511 const glsl_type *
glsl_dvec_type(unsigned n)512 glsl_dvec_type(unsigned n)
513 {
514 return glsl_type::dvec(n);
515 }
516
517 const glsl_type *
glsl_vec4_type(void)518 glsl_vec4_type(void)
519 {
520 return glsl_type::vec4_type;
521 }
522
523 const glsl_type *
glsl_uvec4_type(void)524 glsl_uvec4_type(void)
525 {
526 return glsl_type::uvec4_type;
527 }
528
529 const glsl_type *
glsl_ivec4_type(void)530 glsl_ivec4_type(void)
531 {
532 return glsl_type::ivec4_type;
533 }
534
535 const glsl_type *
glsl_int_type(void)536 glsl_int_type(void)
537 {
538 return glsl_type::int_type;
539 }
540
541 const glsl_type *
glsl_uint_type(void)542 glsl_uint_type(void)
543 {
544 return glsl_type::uint_type;
545 }
546
547 const glsl_type *
glsl_int64_t_type(void)548 glsl_int64_t_type(void)
549 {
550 return glsl_type::int64_t_type;
551 }
552
553 const glsl_type *
glsl_uint64_t_type(void)554 glsl_uint64_t_type(void)
555 {
556 return glsl_type::uint64_t_type;
557 }
558
559 const glsl_type *
glsl_int16_t_type(void)560 glsl_int16_t_type(void)
561 {
562 return glsl_type::int16_t_type;
563 }
564
565 const glsl_type *
glsl_uint16_t_type(void)566 glsl_uint16_t_type(void)
567 {
568 return glsl_type::uint16_t_type;
569 }
570
571 const glsl_type *
glsl_int8_t_type(void)572 glsl_int8_t_type(void)
573 {
574 return glsl_type::int8_t_type;
575 }
576
577 const glsl_type *
glsl_uint8_t_type(void)578 glsl_uint8_t_type(void)
579 {
580 return glsl_type::uint8_t_type;
581 }
582
583 const glsl_type *
glsl_intN_t_type(unsigned bit_size)584 glsl_intN_t_type(unsigned bit_size)
585 {
586 switch (bit_size) {
587 case 8: return glsl_type::int8_t_type;
588 case 16: return glsl_type::int16_t_type;
589 case 32: return glsl_type::int_type;
590 case 64: return glsl_type::int64_t_type;
591 default:
592 unreachable("Unsupported bit size");
593 }
594 }
595
596 const glsl_type *
glsl_uintN_t_type(unsigned bit_size)597 glsl_uintN_t_type(unsigned bit_size)
598 {
599 switch (bit_size) {
600 case 8: return glsl_type::uint8_t_type;
601 case 16: return glsl_type::uint16_t_type;
602 case 32: return glsl_type::uint_type;
603 case 64: return glsl_type::uint64_t_type;
604 default:
605 unreachable("Unsupported bit size");
606 }
607 }
608
609 const glsl_type *
glsl_bool_type(void)610 glsl_bool_type(void)
611 {
612 return glsl_type::bool_type;
613 }
614
615 const glsl_type *
glsl_scalar_type(enum glsl_base_type base_type)616 glsl_scalar_type(enum glsl_base_type base_type)
617 {
618 return glsl_type::get_instance(base_type, 1, 1);
619 }
620
621 const glsl_type *
glsl_vector_type(enum glsl_base_type base_type,unsigned components)622 glsl_vector_type(enum glsl_base_type base_type, unsigned components)
623 {
624 const glsl_type *t = glsl_type::get_instance(base_type, components, 1);
625 assert(t != glsl_type::error_type);
626 return t;
627 }
628
629 const glsl_type *
glsl_matrix_type(enum glsl_base_type base_type,unsigned rows,unsigned columns)630 glsl_matrix_type(enum glsl_base_type base_type, unsigned rows, unsigned columns)
631 {
632 const glsl_type *t = glsl_type::get_instance(base_type, rows, columns);
633 assert(t != glsl_type::error_type);
634 return t;
635 }
636
637 const glsl_type *
glsl_explicit_matrix_type(const glsl_type * mat,unsigned stride,bool row_major)638 glsl_explicit_matrix_type(const glsl_type *mat,
639 unsigned stride, bool row_major)
640 {
641 assert(stride > 0);
642 const glsl_type *t = glsl_type::get_instance(mat->base_type,
643 mat->vector_elements,
644 mat->matrix_columns,
645 stride, row_major);
646 assert(t != glsl_type::error_type);
647 return t;
648 }
649
650 const glsl_type *
glsl_array_type(const glsl_type * base,unsigned elements,unsigned explicit_stride)651 glsl_array_type(const glsl_type *base, unsigned elements,
652 unsigned explicit_stride)
653 {
654 return glsl_type::get_array_instance(base, elements, explicit_stride);
655 }
656
657 const glsl_type *
glsl_replace_vector_type(const glsl_type * t,unsigned components)658 glsl_replace_vector_type(const glsl_type *t, unsigned components)
659 {
660 if (glsl_type_is_array(t)) {
661 return glsl_array_type(
662 glsl_replace_vector_type(t->fields.array, components), t->length,
663 t->explicit_stride);
664 } else if (glsl_type_is_vector_or_scalar(t)) {
665 return glsl_vector_type(t->base_type, components);
666 } else {
667 unreachable("Unhandled base type glsl_replace_vector_type()");
668 }
669 }
670
671 const glsl_type *
glsl_struct_type(const glsl_struct_field * fields,unsigned num_fields,const char * name,bool packed)672 glsl_struct_type(const glsl_struct_field *fields,
673 unsigned num_fields, const char *name,
674 bool packed)
675 {
676 return glsl_type::get_struct_instance(fields, num_fields, name, packed);
677 }
678
679 const glsl_type *
glsl_interface_type(const glsl_struct_field * fields,unsigned num_fields,enum glsl_interface_packing packing,bool row_major,const char * block_name)680 glsl_interface_type(const glsl_struct_field *fields,
681 unsigned num_fields,
682 enum glsl_interface_packing packing,
683 bool row_major,
684 const char *block_name)
685 {
686 return glsl_type::get_interface_instance(fields, num_fields, packing,
687 row_major, block_name);
688 }
689
690 const struct glsl_type *
glsl_sampler_type(enum glsl_sampler_dim dim,bool is_shadow,bool is_array,enum glsl_base_type base_type)691 glsl_sampler_type(enum glsl_sampler_dim dim, bool is_shadow, bool is_array,
692 enum glsl_base_type base_type)
693 {
694 return glsl_type::get_sampler_instance(dim, is_shadow, is_array, base_type);
695 }
696
697 const struct glsl_type *
glsl_bare_sampler_type()698 glsl_bare_sampler_type()
699 {
700 return glsl_type::sampler_type;
701 }
702
703 const struct glsl_type *
glsl_bare_shadow_sampler_type()704 glsl_bare_shadow_sampler_type()
705 {
706 return glsl_type::samplerShadow_type;
707 }
708
709 const struct glsl_type *
glsl_texture_type(enum glsl_sampler_dim dim,bool is_array,enum glsl_base_type base_type)710 glsl_texture_type(enum glsl_sampler_dim dim, bool is_array,
711 enum glsl_base_type base_type)
712 {
713 return glsl_type::get_texture_instance(dim, is_array, base_type);
714 }
715
716 const struct glsl_type *
glsl_image_type(enum glsl_sampler_dim dim,bool is_array,enum glsl_base_type base_type)717 glsl_image_type(enum glsl_sampler_dim dim, bool is_array,
718 enum glsl_base_type base_type)
719 {
720 return glsl_type::get_image_instance(dim, is_array, base_type);
721 }
722
723 const glsl_type *
glsl_function_type(const glsl_type * return_type,const glsl_function_param * params,unsigned num_params)724 glsl_function_type(const glsl_type *return_type,
725 const glsl_function_param *params, unsigned num_params)
726 {
727 return glsl_type::get_function_instance(return_type, params, num_params);
728 }
729
730 const glsl_type *
glsl_transposed_type(const struct glsl_type * type)731 glsl_transposed_type(const struct glsl_type *type)
732 {
733 assert(glsl_type_is_matrix(type));
734 return glsl_type::get_instance(type->base_type, type->matrix_columns,
735 type->vector_elements);
736 }
737
738 const glsl_type *
glsl_channel_type(const glsl_type * t)739 glsl_channel_type(const glsl_type *t)
740 {
741 switch (t->base_type) {
742 case GLSL_TYPE_ARRAY:
743 return glsl_array_type(glsl_channel_type(t->fields.array), t->length,
744 t->explicit_stride);
745 case GLSL_TYPE_UINT:
746 case GLSL_TYPE_INT:
747 case GLSL_TYPE_FLOAT:
748 case GLSL_TYPE_FLOAT16:
749 case GLSL_TYPE_DOUBLE:
750 case GLSL_TYPE_UINT8:
751 case GLSL_TYPE_INT8:
752 case GLSL_TYPE_UINT16:
753 case GLSL_TYPE_INT16:
754 case GLSL_TYPE_UINT64:
755 case GLSL_TYPE_INT64:
756 case GLSL_TYPE_BOOL:
757 return glsl_type::get_instance(t->base_type, 1, 1);
758 default:
759 unreachable("Unhandled base type glsl_channel_type()");
760 }
761 }
762
763 const glsl_type *
glsl_float16_type(const struct glsl_type * type)764 glsl_float16_type(const struct glsl_type *type)
765 {
766 return type->get_float16_type();
767 }
768
769 const glsl_type *
glsl_int16_type(const struct glsl_type * type)770 glsl_int16_type(const struct glsl_type *type)
771 {
772 return type->get_int16_type();
773 }
774
775 const glsl_type *
glsl_uint16_type(const struct glsl_type * type)776 glsl_uint16_type(const struct glsl_type *type)
777 {
778 return type->get_uint16_type();
779 }
780
781 static void
glsl_size_align_handle_array_and_structs(const struct glsl_type * type,glsl_type_size_align_func size_align,unsigned * size,unsigned * align)782 glsl_size_align_handle_array_and_structs(const struct glsl_type *type,
783 glsl_type_size_align_func size_align,
784 unsigned *size, unsigned *align)
785 {
786 if (type->base_type == GLSL_TYPE_ARRAY) {
787 unsigned elem_size = 0, elem_align = 0;
788 size_align(type->fields.array, &elem_size, &elem_align);
789 *align = elem_align;
790 *size = type->length * ALIGN_POT(elem_size, elem_align);
791 } else {
792 assert(type->base_type == GLSL_TYPE_STRUCT ||
793 type->base_type == GLSL_TYPE_INTERFACE);
794
795 *size = 0;
796 *align = 0;
797 for (unsigned i = 0; i < type->length; i++) {
798 unsigned elem_size = 0, elem_align = 0;
799 size_align(type->fields.structure[i].type, &elem_size, &elem_align);
800 *align = MAX2(*align, elem_align);
801 *size = ALIGN_POT(*size, elem_align) + elem_size;
802 }
803 }
804 }
805
806 void
glsl_get_natural_size_align_bytes(const struct glsl_type * type,unsigned * size,unsigned * align)807 glsl_get_natural_size_align_bytes(const struct glsl_type *type,
808 unsigned *size, unsigned *align)
809 {
810 switch (type->base_type) {
811 case GLSL_TYPE_BOOL:
812 /* We special-case Booleans to 32 bits to not cause heartburn for
813 * drivers that suddenly get an 8-bit load.
814 */
815 *size = 4 * type->components();
816 *align = 4;
817 break;
818
819 case GLSL_TYPE_UINT8:
820 case GLSL_TYPE_INT8:
821 case GLSL_TYPE_UINT16:
822 case GLSL_TYPE_INT16:
823 case GLSL_TYPE_FLOAT16:
824 case GLSL_TYPE_UINT:
825 case GLSL_TYPE_INT:
826 case GLSL_TYPE_FLOAT:
827 case GLSL_TYPE_DOUBLE:
828 case GLSL_TYPE_UINT64:
829 case GLSL_TYPE_INT64: {
830 unsigned N = glsl_get_bit_size(type) / 8;
831 *size = N * type->components();
832 *align = N;
833 break;
834 }
835
836 case GLSL_TYPE_ARRAY:
837 case GLSL_TYPE_INTERFACE:
838 case GLSL_TYPE_STRUCT:
839 glsl_size_align_handle_array_and_structs(type,
840 glsl_get_natural_size_align_bytes,
841 size, align);
842 break;
843
844 case GLSL_TYPE_SAMPLER:
845 case GLSL_TYPE_TEXTURE:
846 case GLSL_TYPE_IMAGE:
847 /* Bindless samplers and images. */
848 *size = 8;
849 *align = 8;
850 break;
851
852 case GLSL_TYPE_ATOMIC_UINT:
853 case GLSL_TYPE_SUBROUTINE:
854 case GLSL_TYPE_VOID:
855 case GLSL_TYPE_ERROR:
856 case GLSL_TYPE_FUNCTION:
857 unreachable("type does not have a natural size");
858 }
859 }
860
861 /**
862 * Returns a byte size/alignment for a type where each array element or struct
863 * field is aligned to 16 bytes.
864 */
865 void
glsl_get_vec4_size_align_bytes(const struct glsl_type * type,unsigned * size,unsigned * align)866 glsl_get_vec4_size_align_bytes(const struct glsl_type *type,
867 unsigned *size, unsigned *align)
868 {
869 switch (type->base_type) {
870 case GLSL_TYPE_BOOL:
871 /* We special-case Booleans to 32 bits to not cause heartburn for
872 * drivers that suddenly get an 8-bit load.
873 */
874 *size = 4 * type->components();
875 *align = 16;
876 break;
877
878 case GLSL_TYPE_UINT8:
879 case GLSL_TYPE_INT8:
880 case GLSL_TYPE_UINT16:
881 case GLSL_TYPE_INT16:
882 case GLSL_TYPE_FLOAT16:
883 case GLSL_TYPE_UINT:
884 case GLSL_TYPE_INT:
885 case GLSL_TYPE_FLOAT:
886 case GLSL_TYPE_DOUBLE:
887 case GLSL_TYPE_UINT64:
888 case GLSL_TYPE_INT64: {
889 unsigned N = glsl_get_bit_size(type) / 8;
890 *size = 16 * (type->matrix_columns - 1) + N * type->vector_elements;
891 *align = 16;
892 break;
893 }
894
895 case GLSL_TYPE_ARRAY:
896 case GLSL_TYPE_INTERFACE:
897 case GLSL_TYPE_STRUCT:
898 glsl_size_align_handle_array_and_structs(type,
899 glsl_get_vec4_size_align_bytes,
900 size, align);
901 break;
902
903 case GLSL_TYPE_SAMPLER:
904 case GLSL_TYPE_TEXTURE:
905 case GLSL_TYPE_IMAGE:
906 case GLSL_TYPE_ATOMIC_UINT:
907 case GLSL_TYPE_SUBROUTINE:
908 case GLSL_TYPE_VOID:
909 case GLSL_TYPE_ERROR:
910 case GLSL_TYPE_FUNCTION:
911 unreachable("type does not make sense for glsl_get_vec4_size_align_bytes()");
912 }
913 }
914
915 const glsl_type *
glsl_atomic_uint_type(void)916 glsl_atomic_uint_type(void)
917 {
918 return glsl_type::atomic_uint_type;
919 }
920
921 unsigned
glsl_atomic_size(const struct glsl_type * type)922 glsl_atomic_size(const struct glsl_type *type)
923 {
924 return type->atomic_size();
925 }
926
927 bool
glsl_contains_atomic(const struct glsl_type * type)928 glsl_contains_atomic(const struct glsl_type *type)
929 {
930 return type->contains_atomic();
931 }
932
933 bool
glsl_contains_opaque(const struct glsl_type * type)934 glsl_contains_opaque(const struct glsl_type *type)
935 {
936 return type->contains_opaque();
937 }
938
939 int
glsl_get_cl_size(const struct glsl_type * type)940 glsl_get_cl_size(const struct glsl_type *type)
941 {
942 return type->cl_size();
943 }
944
945 int
glsl_get_cl_alignment(const struct glsl_type * type)946 glsl_get_cl_alignment(const struct glsl_type *type)
947 {
948 return type->cl_alignment();
949 }
950
951 void
glsl_get_cl_type_size_align(const struct glsl_type * type,unsigned * size,unsigned * align)952 glsl_get_cl_type_size_align(const struct glsl_type *type,
953 unsigned *size, unsigned *align)
954 {
955 *size = glsl_get_cl_size(type);
956 *align = glsl_get_cl_alignment(type);
957 }
958
959 static unsigned
glsl_type_count(const glsl_type * type,glsl_base_type base_type)960 glsl_type_count(const glsl_type *type, glsl_base_type base_type)
961 {
962 if (glsl_type_is_array(type)) {
963 return glsl_get_length(type) *
964 glsl_type_count(glsl_get_array_element(type), base_type);
965 }
966
967 /* Ignore interface blocks - they can only contain bindless samplers,
968 * which we shouldn't count.
969 */
970 if (glsl_type_is_struct(type)) {
971 unsigned count = 0;
972 for (unsigned i = 0; i < glsl_get_length(type); i++)
973 count += glsl_type_count(glsl_get_struct_field(type, i), base_type);
974 return count;
975 }
976
977 if (glsl_get_base_type(type) == base_type)
978 return 1;
979
980 return 0;
981 }
982
983 unsigned
glsl_type_get_sampler_count(const struct glsl_type * type)984 glsl_type_get_sampler_count(const struct glsl_type *type)
985 {
986 return glsl_type_count(type, GLSL_TYPE_SAMPLER);
987 }
988
989 unsigned
glsl_type_get_texture_count(const struct glsl_type * type)990 glsl_type_get_texture_count(const struct glsl_type *type)
991 {
992 return glsl_type_count(type, GLSL_TYPE_TEXTURE);
993 }
994
995 unsigned
glsl_type_get_image_count(const struct glsl_type * type)996 glsl_type_get_image_count(const struct glsl_type *type)
997 {
998 return glsl_type_count(type, GLSL_TYPE_IMAGE);
999 }
1000
1001 int
glsl_get_field_index(const struct glsl_type * type,const char * name)1002 glsl_get_field_index(const struct glsl_type *type, const char *name)
1003 {
1004 return type->field_index(name);
1005 }
1006
1007 enum glsl_interface_packing
glsl_get_internal_ifc_packing(const struct glsl_type * type,bool std430_supported)1008 glsl_get_internal_ifc_packing(const struct glsl_type *type,
1009 bool std430_supported)
1010 {
1011 return type->get_internal_ifc_packing(std430_supported);
1012 }
1013
1014 enum glsl_interface_packing
glsl_get_ifc_packing(const struct glsl_type * type)1015 glsl_get_ifc_packing(const struct glsl_type *type)
1016 {
1017 return type->get_interface_packing();
1018 }
1019
1020 unsigned
glsl_get_std140_base_alignment(const struct glsl_type * type,bool row_major)1021 glsl_get_std140_base_alignment(const struct glsl_type *type, bool row_major)
1022 {
1023 return type->std140_base_alignment(row_major);
1024 }
1025
1026 unsigned
glsl_get_std140_size(const struct glsl_type * type,bool row_major)1027 glsl_get_std140_size(const struct glsl_type *type, bool row_major)
1028 {
1029 return type->std140_size(row_major);
1030 }
1031
1032 unsigned
glsl_get_std430_base_alignment(const struct glsl_type * type,bool row_major)1033 glsl_get_std430_base_alignment(const struct glsl_type *type, bool row_major)
1034 {
1035 return type->std430_base_alignment(row_major);
1036 }
1037
1038 unsigned
glsl_get_std430_size(const struct glsl_type * type,bool row_major)1039 glsl_get_std430_size(const struct glsl_type *type, bool row_major)
1040 {
1041 return type->std430_size(row_major);
1042 }
1043
1044 unsigned
glsl_get_explicit_size(const struct glsl_type * type,bool align_to_stride)1045 glsl_get_explicit_size(const struct glsl_type *type, bool align_to_stride)
1046 {
1047 return type->explicit_size(align_to_stride);
1048 }
1049
1050 unsigned
glsl_get_explicit_alignment(const struct glsl_type * type)1051 glsl_get_explicit_alignment(const struct glsl_type *type)
1052 {
1053 return type->explicit_alignment;
1054 }
1055
1056 bool
glsl_type_is_packed(const struct glsl_type * type)1057 glsl_type_is_packed(const struct glsl_type *type)
1058 {
1059 return type->packed;
1060 }
1061
1062 bool
glsl_type_is_leaf(const struct glsl_type * type)1063 glsl_type_is_leaf(const struct glsl_type *type)
1064 {
1065 if (glsl_type_is_struct_or_ifc(type) ||
1066 (glsl_type_is_array(type) &&
1067 (glsl_type_is_array(glsl_get_array_element(type)) ||
1068 glsl_type_is_struct_or_ifc(glsl_get_array_element(type))))) {
1069 return false;
1070 } else {
1071 return true;
1072 }
1073 }
1074
1075 const struct glsl_type *
glsl_get_explicit_type_for_size_align(const struct glsl_type * type,glsl_type_size_align_func type_info,unsigned * size,unsigned * align)1076 glsl_get_explicit_type_for_size_align(const struct glsl_type *type,
1077 glsl_type_size_align_func type_info,
1078 unsigned *size, unsigned *align)
1079 {
1080 return type->get_explicit_type_for_size_align(type_info, size, align);
1081 }
1082
1083 const struct glsl_type *
glsl_type_wrap_in_arrays(const struct glsl_type * type,const struct glsl_type * arrays)1084 glsl_type_wrap_in_arrays(const struct glsl_type *type,
1085 const struct glsl_type *arrays)
1086 {
1087 if (!glsl_type_is_array(arrays))
1088 return type;
1089
1090 const glsl_type *elem_type =
1091 glsl_type_wrap_in_arrays(type, glsl_get_array_element(arrays));
1092 return glsl_array_type(elem_type, glsl_get_length(arrays),
1093 glsl_get_explicit_stride(arrays));
1094 }
1095
1096 const struct glsl_type *
glsl_type_replace_vec3_with_vec4(const struct glsl_type * type)1097 glsl_type_replace_vec3_with_vec4(const struct glsl_type *type)
1098 {
1099 return type->replace_vec3_with_vec4();
1100 }
1101