glsl_types: vec8/vec16 support
[mesa.git] / src / compiler / nir_types.cpp
1 /*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Connor Abbott (cwabbott0@gmail.com)
25 *
26 */
27
28 #include "nir_types.h"
29 #include "compiler/glsl/ir.h"
30
31 const char *
32 glsl_get_type_name(const glsl_type *type)
33 {
34 return type->name;
35 }
36
37 const glsl_type *
38 glsl_get_array_element(const glsl_type* type)
39 {
40 if (type->is_matrix())
41 return type->column_type();
42 else if (type->is_vector())
43 return type->get_scalar_type();
44 return type->fields.array;
45 }
46
47 const glsl_type *
48 glsl_without_array(const glsl_type *type)
49 {
50 return type->without_array();
51 }
52
53 const glsl_type *
54 glsl_get_array_instance(const glsl_type *type,
55 unsigned array_size)
56 {
57 return glsl_type::get_array_instance(type, array_size);
58 }
59
60 const glsl_type *
61 glsl_get_struct_field(const glsl_type *type, unsigned index)
62 {
63 return type->fields.structure[index].type;
64 }
65
66 const glsl_type *
67 glsl_get_function_return_type(const glsl_type *type)
68 {
69 return type->fields.parameters[0].type;
70 }
71
72 const glsl_function_param *
73 glsl_get_function_param(const glsl_type *type, unsigned index)
74 {
75 return &type->fields.parameters[index + 1];
76 }
77
78 const struct glsl_type *
79 glsl_get_column_type(const struct glsl_type *type)
80 {
81 return type->column_type();
82 }
83
84 enum glsl_base_type
85 glsl_get_base_type(const struct glsl_type *type)
86 {
87 return type->base_type;
88 }
89
90 unsigned
91 glsl_get_vector_elements(const struct glsl_type *type)
92 {
93 return type->vector_elements;
94 }
95
96 unsigned
97 glsl_get_components(const struct glsl_type *type)
98 {
99 return type->components();
100 }
101
102 unsigned
103 glsl_get_matrix_columns(const struct glsl_type *type)
104 {
105 return type->matrix_columns;
106 }
107
108 unsigned
109 glsl_get_length(const struct glsl_type *type)
110 {
111 return type->is_matrix() ? type->matrix_columns : type->length;
112 }
113
114 unsigned
115 glsl_get_aoa_size(const struct glsl_type *type)
116 {
117 return type->arrays_of_arrays_size();
118 }
119
120 unsigned
121 glsl_count_attribute_slots(const struct glsl_type *type,
122 bool is_vertex_input)
123 {
124 return type->count_attribute_slots(is_vertex_input);
125 }
126
127 const char *
128 glsl_get_struct_elem_name(const struct glsl_type *type, unsigned index)
129 {
130 return type->fields.structure[index].name;
131 }
132
133 glsl_sampler_dim
134 glsl_get_sampler_dim(const struct glsl_type *type)
135 {
136 assert(glsl_type_is_sampler(type) || glsl_type_is_image(type));
137 return (glsl_sampler_dim)type->sampler_dimensionality;
138 }
139
140 glsl_base_type
141 glsl_get_sampler_result_type(const struct glsl_type *type)
142 {
143 assert(glsl_type_is_sampler(type) || glsl_type_is_image(type));
144 return (glsl_base_type)type->sampled_type;
145 }
146
147 unsigned
148 glsl_get_record_location_offset(const struct glsl_type *type,
149 unsigned length)
150 {
151 return type->record_location_offset(length);
152 }
153
154 bool
155 glsl_type_is_64bit(const glsl_type *type)
156 {
157 return type->is_64bit();
158 }
159
160 bool
161 glsl_type_is_void(const glsl_type *type)
162 {
163 return type->is_void();
164 }
165
166 bool
167 glsl_type_is_error(const glsl_type *type)
168 {
169 return type->is_error();
170 }
171
172 bool
173 glsl_type_is_vector(const struct glsl_type *type)
174 {
175 return type->is_vector();
176 }
177
178 bool
179 glsl_type_is_scalar(const struct glsl_type *type)
180 {
181 return type->is_scalar();
182 }
183
184 bool
185 glsl_type_is_vector_or_scalar(const struct glsl_type *type)
186 {
187 return type->is_vector() || type->is_scalar();
188 }
189
190 bool
191 glsl_type_is_matrix(const struct glsl_type *type)
192 {
193 return type->is_matrix();
194 }
195
196 bool
197 glsl_type_is_array(const struct glsl_type *type)
198 {
199 return type->is_array();
200 }
201
202 bool
203 glsl_type_is_array_of_arrays(const struct glsl_type *type)
204 {
205 return type->is_array_of_arrays();
206 }
207
208 bool
209 glsl_type_is_struct(const struct glsl_type *type)
210 {
211 return type->is_record() || type->is_interface();
212 }
213
214 bool
215 glsl_type_is_sampler(const struct glsl_type *type)
216 {
217 return type->is_sampler();
218 }
219
220 bool
221 glsl_type_is_image(const struct glsl_type *type)
222 {
223 return type->is_image();
224 }
225
226 bool
227 glsl_sampler_type_is_shadow(const struct glsl_type *type)
228 {
229 assert(glsl_type_is_sampler(type));
230 return type->sampler_shadow;
231 }
232
233 bool
234 glsl_sampler_type_is_array(const struct glsl_type *type)
235 {
236 assert(glsl_type_is_sampler(type) || glsl_type_is_image(type));
237 return type->sampler_array;
238 }
239
240 bool
241 glsl_type_is_dual_slot(const struct glsl_type *type)
242 {
243 return type->is_dual_slot();
244 }
245
246 bool
247 glsl_type_is_numeric(const struct glsl_type *type)
248 {
249 return type->is_numeric();
250 }
251
252 bool
253 glsl_type_is_boolean(const struct glsl_type *type)
254 {
255 return type->is_boolean();
256 }
257
258 const glsl_type *
259 glsl_void_type(void)
260 {
261 return glsl_type::void_type;
262 }
263
264 const glsl_type *
265 glsl_float_type(void)
266 {
267 return glsl_type::float_type;
268 }
269
270 const glsl_type *
271 glsl_double_type(void)
272 {
273 return glsl_type::double_type;
274 }
275
276 const glsl_type *
277 glsl_float16_t_type(void)
278 {
279 return glsl_type::float16_t_type;
280 }
281
282 const glsl_type *
283 glsl_vec_type(unsigned n)
284 {
285 return glsl_type::vec(n);
286 }
287
288 const glsl_type *
289 glsl_dvec_type(unsigned n)
290 {
291 return glsl_type::dvec(n);
292 }
293
294 const glsl_type *
295 glsl_vec4_type(void)
296 {
297 return glsl_type::vec4_type;
298 }
299
300 const glsl_type *
301 glsl_uvec4_type(void)
302 {
303 return glsl_type::uvec4_type;
304 }
305
306 const glsl_type *
307 glsl_int_type(void)
308 {
309 return glsl_type::int_type;
310 }
311
312 const glsl_type *
313 glsl_uint_type(void)
314 {
315 return glsl_type::uint_type;
316 }
317
318 const glsl_type *
319 glsl_int64_t_type(void)
320 {
321 return glsl_type::int64_t_type;
322 }
323
324 const glsl_type *
325 glsl_uint64_t_type(void)
326 {
327 return glsl_type::uint64_t_type;
328 }
329
330 const glsl_type *
331 glsl_int16_t_type(void)
332 {
333 return glsl_type::int16_t_type;
334 }
335
336 const glsl_type *
337 glsl_uint16_t_type(void)
338 {
339 return glsl_type::uint16_t_type;
340 }
341
342 const glsl_type *
343 glsl_int8_t_type(void)
344 {
345 return glsl_type::int8_t_type;
346 }
347
348 const glsl_type *
349 glsl_uint8_t_type(void)
350 {
351 return glsl_type::uint8_t_type;
352 }
353
354 const glsl_type *
355 glsl_bool_type(void)
356 {
357 return glsl_type::bool_type;
358 }
359
360 const glsl_type *
361 glsl_scalar_type(enum glsl_base_type base_type)
362 {
363 return glsl_type::get_instance(base_type, 1, 1);
364 }
365
366 const glsl_type *
367 glsl_vector_type(enum glsl_base_type base_type, unsigned components)
368 {
369 const glsl_type *t = glsl_type::get_instance(base_type, components, 1);
370 assert(t != glsl_type::error_type);
371 return t;
372 }
373
374 const glsl_type *
375 glsl_matrix_type(enum glsl_base_type base_type, unsigned rows, unsigned columns)
376 {
377 const glsl_type *t = glsl_type::get_instance(base_type, rows, columns);
378 assert(t != glsl_type::error_type);
379 return t;
380 }
381
382 const glsl_type *
383 glsl_array_type(const glsl_type *base, unsigned elements)
384 {
385 return glsl_type::get_array_instance(base, elements);
386 }
387
388 const glsl_type *
389 glsl_struct_type(const glsl_struct_field *fields,
390 unsigned num_fields, const char *name)
391 {
392 return glsl_type::get_record_instance(fields, num_fields, name);
393 }
394
395 const glsl_type *
396 glsl_interface_type(const glsl_struct_field *fields,
397 unsigned num_fields,
398 enum glsl_interface_packing packing,
399 bool row_major,
400 const char *block_name)
401 {
402 return glsl_type::get_interface_instance(fields, num_fields, packing,
403 row_major, block_name);
404 }
405
406 const struct glsl_type *
407 glsl_sampler_type(enum glsl_sampler_dim dim, bool is_shadow, bool is_array,
408 enum glsl_base_type base_type)
409 {
410 return glsl_type::get_sampler_instance(dim, is_shadow, is_array, base_type);
411 }
412
413 const struct glsl_type *
414 glsl_bare_sampler_type()
415 {
416 return glsl_type::sampler_type;
417 }
418
419 const struct glsl_type *
420 glsl_image_type(enum glsl_sampler_dim dim, bool is_array,
421 enum glsl_base_type base_type)
422 {
423 return glsl_type::get_image_instance(dim, is_array, base_type);
424 }
425
426 const glsl_type *
427 glsl_function_type(const glsl_type *return_type,
428 const glsl_function_param *params, unsigned num_params)
429 {
430 return glsl_type::get_function_instance(return_type, params, num_params);
431 }
432
433 const glsl_type *
434 glsl_transposed_type(const struct glsl_type *type)
435 {
436 assert(glsl_type_is_matrix(type));
437 return glsl_type::get_instance(type->base_type, type->matrix_columns,
438 type->vector_elements);
439 }
440
441 const glsl_type *
442 glsl_channel_type(const glsl_type *t)
443 {
444 switch (glsl_get_base_type(t)) {
445 case GLSL_TYPE_ARRAY: {
446 const glsl_type *base = glsl_channel_type(glsl_get_array_element(t));
447 return glsl_array_type(base, glsl_get_length(t));
448 }
449 case GLSL_TYPE_UINT:
450 return glsl_uint_type();
451 case GLSL_TYPE_INT:
452 return glsl_int_type();
453 case GLSL_TYPE_FLOAT:
454 return glsl_float_type();
455 case GLSL_TYPE_BOOL:
456 return glsl_bool_type();
457 case GLSL_TYPE_DOUBLE:
458 return glsl_double_type();
459 case GLSL_TYPE_UINT64:
460 return glsl_uint64_t_type();
461 case GLSL_TYPE_INT64:
462 return glsl_int64_t_type();
463 default:
464 unreachable("Unhandled base type glsl_channel_type()");
465 }
466 }