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