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 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)705 glsl_size_align_handle_array_and_structs(const struct glsl_type *type,
706 glsl_type_size_align_func size_align,
707 unsigned *size, unsigned *align)
708 {
709 if (type->base_type == GLSL_TYPE_ARRAY) {
710 unsigned elem_size = 0, elem_align = 0;
711 size_align(type->fields.array, &elem_size, &elem_align);
712 *align = elem_align;
713 *size = type->length * ALIGN_POT(elem_size, elem_align);
714 } else {
715 assert(type->base_type == GLSL_TYPE_STRUCT ||
716 type->base_type == GLSL_TYPE_INTERFACE);
717
718 *size = 0;
719 *align = 0;
720 for (unsigned i = 0; i < type->length; i++) {
721 unsigned elem_size = 0, elem_align = 0;
722 size_align(type->fields.structure[i].type, &elem_size, &elem_align);
723 *align = MAX2(*align, elem_align);
724 *size = ALIGN_POT(*size, elem_align) + elem_size;
725 }
726 }
727 }
728
729 void
glsl_get_natural_size_align_bytes(const struct glsl_type * type,unsigned * size,unsigned * align)730 glsl_get_natural_size_align_bytes(const struct glsl_type *type,
731 unsigned *size, unsigned *align)
732 {
733 switch (type->base_type) {
734 case GLSL_TYPE_BOOL:
735 /* We special-case Booleans to 32 bits to not cause heartburn for
736 * drivers that suddenly get an 8-bit load.
737 */
738 *size = 4 * type->components();
739 *align = 4;
740 break;
741
742 case GLSL_TYPE_UINT8:
743 case GLSL_TYPE_INT8:
744 case GLSL_TYPE_UINT16:
745 case GLSL_TYPE_INT16:
746 case GLSL_TYPE_FLOAT16:
747 case GLSL_TYPE_UINT:
748 case GLSL_TYPE_INT:
749 case GLSL_TYPE_FLOAT:
750 case GLSL_TYPE_DOUBLE:
751 case GLSL_TYPE_UINT64:
752 case GLSL_TYPE_INT64: {
753 unsigned N = glsl_get_bit_size(type) / 8;
754 *size = N * type->components();
755 *align = N;
756 break;
757 }
758
759 case GLSL_TYPE_ARRAY:
760 case GLSL_TYPE_INTERFACE:
761 case GLSL_TYPE_STRUCT:
762 glsl_size_align_handle_array_and_structs(type,
763 glsl_get_natural_size_align_bytes,
764 size, align);
765 break;
766
767 case GLSL_TYPE_SAMPLER:
768 case GLSL_TYPE_IMAGE:
769 /* Bindless samplers and images. */
770 *size = 8;
771 *align = 8;
772 break;
773
774 case GLSL_TYPE_ATOMIC_UINT:
775 case GLSL_TYPE_SUBROUTINE:
776 case GLSL_TYPE_VOID:
777 case GLSL_TYPE_ERROR:
778 case GLSL_TYPE_FUNCTION:
779 unreachable("type does not have a natural size");
780 }
781 }
782
783 /**
784 * Returns a byte size/alignment for a type where each array element or struct
785 * field is aligned to 16 bytes.
786 */
787 void
glsl_get_vec4_size_align_bytes(const struct glsl_type * type,unsigned * size,unsigned * align)788 glsl_get_vec4_size_align_bytes(const struct glsl_type *type,
789 unsigned *size, unsigned *align)
790 {
791 switch (type->base_type) {
792 case GLSL_TYPE_BOOL:
793 /* We special-case Booleans to 32 bits to not cause heartburn for
794 * drivers that suddenly get an 8-bit load.
795 */
796 *size = 4 * type->components();
797 *align = 16;
798 break;
799
800 case GLSL_TYPE_UINT8:
801 case GLSL_TYPE_INT8:
802 case GLSL_TYPE_UINT16:
803 case GLSL_TYPE_INT16:
804 case GLSL_TYPE_FLOAT16:
805 case GLSL_TYPE_UINT:
806 case GLSL_TYPE_INT:
807 case GLSL_TYPE_FLOAT:
808 case GLSL_TYPE_DOUBLE:
809 case GLSL_TYPE_UINT64:
810 case GLSL_TYPE_INT64: {
811 unsigned N = glsl_get_bit_size(type) / 8;
812 *size = 16 * (type->matrix_columns - 1) + N * type->vector_elements;
813 *align = 16;
814 break;
815 }
816
817 case GLSL_TYPE_ARRAY:
818 case GLSL_TYPE_INTERFACE:
819 case GLSL_TYPE_STRUCT:
820 glsl_size_align_handle_array_and_structs(type,
821 glsl_get_vec4_size_align_bytes,
822 size, align);
823 break;
824
825 case GLSL_TYPE_SAMPLER:
826 case GLSL_TYPE_IMAGE:
827 case GLSL_TYPE_ATOMIC_UINT:
828 case GLSL_TYPE_SUBROUTINE:
829 case GLSL_TYPE_VOID:
830 case GLSL_TYPE_ERROR:
831 case GLSL_TYPE_FUNCTION:
832 unreachable("type does not make sense for glsl_get_vec4_size_align_bytes()");
833 }
834 }
835
836 const glsl_type *
glsl_atomic_uint_type(void)837 glsl_atomic_uint_type(void)
838 {
839 return glsl_type::atomic_uint_type;
840 }
841
842 unsigned
glsl_atomic_size(const struct glsl_type * type)843 glsl_atomic_size(const struct glsl_type *type)
844 {
845 return type->atomic_size();
846 }
847
848 bool
glsl_contains_atomic(const struct glsl_type * type)849 glsl_contains_atomic(const struct glsl_type *type)
850 {
851 return type->contains_atomic();
852 }
853
854 bool
glsl_contains_opaque(const struct glsl_type * type)855 glsl_contains_opaque(const struct glsl_type *type)
856 {
857 return type->contains_opaque();
858 }
859
860 int
glsl_get_cl_size(const struct glsl_type * type)861 glsl_get_cl_size(const struct glsl_type *type)
862 {
863 return type->cl_size();
864 }
865
866 int
glsl_get_cl_alignment(const struct glsl_type * type)867 glsl_get_cl_alignment(const struct glsl_type *type)
868 {
869 return type->cl_alignment();
870 }
871
872 void
glsl_get_cl_type_size_align(const struct glsl_type * type,unsigned * size,unsigned * align)873 glsl_get_cl_type_size_align(const struct glsl_type *type,
874 unsigned *size, unsigned *align)
875 {
876 *size = glsl_get_cl_size(type);
877 *align = glsl_get_cl_alignment(type);
878 }
879
880 unsigned
glsl_type_get_sampler_count(const struct glsl_type * type)881 glsl_type_get_sampler_count(const struct glsl_type *type)
882 {
883 if (glsl_type_is_array(type)) {
884 return (glsl_get_aoa_size(type) *
885 glsl_type_get_sampler_count(glsl_without_array(type)));
886 }
887
888 /* Ignore interface blocks - they can only contain bindless samplers,
889 * which we shouldn't count.
890 */
891 if (glsl_type_is_struct(type)) {
892 unsigned count = 0;
893 for (unsigned i = 0; i < glsl_get_length(type); i++)
894 count += glsl_type_get_sampler_count(glsl_get_struct_field(type, i));
895 return count;
896 }
897
898 if (glsl_type_is_sampler(type))
899 return 1;
900
901 return 0;
902 }
903
904 unsigned
glsl_type_get_image_count(const struct glsl_type * type)905 glsl_type_get_image_count(const struct glsl_type *type)
906 {
907 if (glsl_type_is_array(type)) {
908 return (glsl_get_aoa_size(type) *
909 glsl_type_get_image_count(glsl_without_array(type)));
910 }
911
912 /* Ignore interface blocks - they can only contain bindless images,
913 * which we shouldn't count.
914 */
915 if (glsl_type_is_struct(type)) {
916 unsigned count = 0;
917 for (unsigned i = 0; i < glsl_get_length(type); i++)
918 count += glsl_type_get_image_count(glsl_get_struct_field(type, i));
919 return count;
920 }
921
922 if (glsl_type_is_image(type))
923 return 1;
924
925 return 0;
926 }
927
928 enum glsl_interface_packing
glsl_get_internal_ifc_packing(const struct glsl_type * type,bool std430_supported)929 glsl_get_internal_ifc_packing(const struct glsl_type *type,
930 bool std430_supported)
931 {
932 return type->get_internal_ifc_packing(std430_supported);
933 }
934
935 enum glsl_interface_packing
glsl_get_ifc_packing(const struct glsl_type * type)936 glsl_get_ifc_packing(const struct glsl_type *type)
937 {
938 return type->get_interface_packing();
939 }
940
941 unsigned
glsl_get_std140_base_alignment(const struct glsl_type * type,bool row_major)942 glsl_get_std140_base_alignment(const struct glsl_type *type, bool row_major)
943 {
944 return type->std140_base_alignment(row_major);
945 }
946
947 unsigned
glsl_get_std140_size(const struct glsl_type * type,bool row_major)948 glsl_get_std140_size(const struct glsl_type *type, bool row_major)
949 {
950 return type->std140_size(row_major);
951 }
952
953 unsigned
glsl_get_std430_base_alignment(const struct glsl_type * type,bool row_major)954 glsl_get_std430_base_alignment(const struct glsl_type *type, bool row_major)
955 {
956 return type->std430_base_alignment(row_major);
957 }
958
959 unsigned
glsl_get_std430_size(const struct glsl_type * type,bool row_major)960 glsl_get_std430_size(const struct glsl_type *type, bool row_major)
961 {
962 return type->std430_size(row_major);
963 }
964
965 unsigned
glsl_get_explicit_size(const struct glsl_type * type,bool align_to_stride)966 glsl_get_explicit_size(const struct glsl_type *type, bool align_to_stride)
967 {
968 return type->explicit_size(align_to_stride);
969 }
970
971 unsigned
glsl_get_explicit_alignment(const struct glsl_type * type)972 glsl_get_explicit_alignment(const struct glsl_type *type)
973 {
974 return type->explicit_alignment;
975 }
976
977 bool
glsl_type_is_packed(const struct glsl_type * type)978 glsl_type_is_packed(const struct glsl_type *type)
979 {
980 return type->packed;
981 }
982
983 bool
glsl_type_is_leaf(const struct glsl_type * type)984 glsl_type_is_leaf(const struct glsl_type *type)
985 {
986 if (glsl_type_is_struct_or_ifc(type) ||
987 (glsl_type_is_array(type) &&
988 (glsl_type_is_array(glsl_get_array_element(type)) ||
989 glsl_type_is_struct_or_ifc(glsl_get_array_element(type))))) {
990 return false;
991 } else {
992 return true;
993 }
994 }
995
996 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)997 glsl_get_explicit_type_for_size_align(const struct glsl_type *type,
998 glsl_type_size_align_func type_info,
999 unsigned *size, unsigned *align)
1000 {
1001 return type->get_explicit_type_for_size_align(type_info, size, align);
1002 }
1003
1004 const struct glsl_type *
glsl_type_replace_vec3_with_vec4(const struct glsl_type * type)1005 glsl_type_replace_vec3_with_vec4(const struct glsl_type *type)
1006 {
1007 return type->replace_vec3_with_vec4();
1008 }
1009