nir/types: Expose glsl_type::count_attribute_slots()
[mesa.git] / src / glsl / nir / 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 "ir.h"
30
31 void
32 glsl_print_type(const glsl_type *type, FILE *fp)
33 {
34 if (type->base_type == GLSL_TYPE_ARRAY) {
35 glsl_print_type(type->fields.array, fp);
36 fprintf(fp, "[%u]", type->length);
37 } else if ((type->base_type == GLSL_TYPE_STRUCT)
38 && !is_gl_identifier(type->name)) {
39 fprintf(fp, "%s@%p", type->name, (void *) type);
40 } else {
41 fprintf(fp, "%s", type->name);
42 }
43 }
44
45 void
46 glsl_print_struct(const glsl_type *type, FILE *fp)
47 {
48 assert(type->base_type == GLSL_TYPE_STRUCT);
49
50 fprintf(fp, "struct {\n");
51 for (unsigned i = 0; i < type->length; i++) {
52 fprintf(fp, "\t");
53 glsl_print_type(type->fields.structure[i].type, fp);
54 fprintf(fp, " %s;\n", type->fields.structure[i].name);
55 }
56 fprintf(fp, "}\n");
57 }
58
59 const glsl_type *
60 glsl_get_array_element(const glsl_type* type)
61 {
62 if (type->is_matrix())
63 return type->column_type();
64 return type->fields.array;
65 }
66
67 const glsl_type *
68 glsl_get_struct_field(const glsl_type *type, unsigned index)
69 {
70 return type->fields.structure[index].type;
71 }
72
73 const glsl_type *
74 glsl_get_function_return_type(const glsl_type *type)
75 {
76 return type->fields.parameters[0].type;
77 }
78
79 const glsl_function_param *
80 glsl_get_function_param(const glsl_type *type, unsigned index)
81 {
82 return &type->fields.parameters[index + 1];
83 }
84
85 const struct glsl_type *
86 glsl_get_column_type(const struct glsl_type *type)
87 {
88 return type->column_type();
89 }
90
91 enum glsl_base_type
92 glsl_get_base_type(const struct glsl_type *type)
93 {
94 return type->base_type;
95 }
96
97 unsigned
98 glsl_get_vector_elements(const struct glsl_type *type)
99 {
100 return type->vector_elements;
101 }
102
103 unsigned
104 glsl_get_components(const struct glsl_type *type)
105 {
106 return type->components();
107 }
108
109 unsigned
110 glsl_get_matrix_columns(const struct glsl_type *type)
111 {
112 return type->matrix_columns;
113 }
114
115 unsigned
116 glsl_get_length(const struct glsl_type *type)
117 {
118 return type->is_matrix() ? type->matrix_columns : type->length;
119 }
120
121 unsigned
122 glsl_get_aoa_size(const struct glsl_type *type)
123 {
124 return type->arrays_of_arrays_size();
125 }
126
127 unsigned
128 glsl_count_attribute_slots(const struct glsl_type *type)
129 {
130 return type->count_attribute_slots();
131 }
132
133 const char *
134 glsl_get_struct_elem_name(const struct glsl_type *type, unsigned index)
135 {
136 return type->fields.structure[index].name;
137 }
138
139 glsl_sampler_dim
140 glsl_get_sampler_dim(const struct glsl_type *type)
141 {
142 assert(glsl_type_is_sampler(type) || glsl_type_is_image(type));
143 return (glsl_sampler_dim)type->sampler_dimensionality;
144 }
145
146 glsl_base_type
147 glsl_get_sampler_result_type(const struct glsl_type *type)
148 {
149 assert(glsl_type_is_sampler(type) || glsl_type_is_image(type));
150 return (glsl_base_type)type->sampler_type;
151 }
152
153 unsigned
154 glsl_get_record_location_offset(const struct glsl_type *type,
155 unsigned length)
156 {
157 return type->record_location_offset(length);
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_vector(const struct glsl_type *type)
168 {
169 return type->is_vector();
170 }
171
172 bool
173 glsl_type_is_scalar(const struct glsl_type *type)
174 {
175 return type->is_scalar();
176 }
177
178 bool
179 glsl_type_is_vector_or_scalar(const struct glsl_type *type)
180 {
181 return type->is_vector() || type->is_scalar();
182 }
183
184 bool
185 glsl_type_is_matrix(const struct glsl_type *type)
186 {
187 return type->is_matrix();
188 }
189
190 bool
191 glsl_type_is_array(const struct glsl_type *type)
192 {
193 return type->is_array();
194 }
195
196 bool
197 glsl_type_is_struct(const struct glsl_type *type)
198 {
199 return type->is_record() || type->is_interface();
200 }
201
202 bool
203 glsl_type_is_sampler(const struct glsl_type *type)
204 {
205 return type->is_sampler();
206 }
207
208 bool
209 glsl_type_is_image(const struct glsl_type *type)
210 {
211 return type->is_image();
212 }
213
214 bool
215 glsl_sampler_type_is_shadow(const struct glsl_type *type)
216 {
217 assert(glsl_type_is_sampler(type));
218 return type->sampler_shadow;
219 }
220
221 bool
222 glsl_sampler_type_is_array(const struct glsl_type *type)
223 {
224 assert(glsl_type_is_sampler(type) || glsl_type_is_image(type));
225 return type->sampler_array;
226 }
227
228 const glsl_type *
229 glsl_void_type(void)
230 {
231 return glsl_type::void_type;
232 }
233
234 const glsl_type *
235 glsl_float_type(void)
236 {
237 return glsl_type::float_type;
238 }
239
240 const glsl_type *
241 glsl_int_type(void)
242 {
243 return glsl_type::int_type;
244 }
245
246 const glsl_type *
247 glsl_uint_type(void)
248 {
249 return glsl_type::uint_type;
250 }
251
252 const glsl_type *
253 glsl_bool_type(void)
254 {
255 return glsl_type::bool_type;
256 }
257
258 const glsl_type *
259 glsl_vec4_type(void)
260 {
261 return glsl_type::vec4_type;
262 }
263
264 const glsl_type *
265 glsl_scalar_type(enum glsl_base_type base_type)
266 {
267 return glsl_type::get_instance(base_type, 1, 1);
268 }
269
270 const glsl_type *
271 glsl_vector_type(enum glsl_base_type base_type, unsigned components)
272 {
273 assert(components > 1 && components <= 4);
274 return glsl_type::get_instance(base_type, components, 1);
275 }
276
277 const glsl_type *
278 glsl_matrix_type(enum glsl_base_type base_type, unsigned rows, unsigned columns)
279 {
280 assert(rows > 1 && rows <= 4 && columns >= 1 && columns <= 4);
281 return glsl_type::get_instance(base_type, rows, columns);
282 }
283
284 const glsl_type *
285 glsl_array_type(const glsl_type *base, unsigned elements)
286 {
287 return glsl_type::get_array_instance(base, elements);
288 }
289
290 const glsl_type *
291 glsl_struct_type(const glsl_struct_field *fields,
292 unsigned num_fields, const char *name)
293 {
294 return glsl_type::get_record_instance(fields, num_fields, name);
295 }
296
297 const struct glsl_type *
298 glsl_sampler_type(enum glsl_sampler_dim dim, bool is_shadow, bool is_array,
299 enum glsl_base_type base_type)
300 {
301 return glsl_type::get_sampler_instance(dim, is_shadow, is_array, base_type);
302 }
303
304 const struct glsl_type *
305 glsl_image_type(enum glsl_sampler_dim dim, bool is_array,
306 enum glsl_base_type base_type)
307 {
308 return glsl_type::get_image_instance(dim, is_array, base_type);
309 }
310
311 const glsl_type *
312 glsl_function_type(const glsl_type *return_type,
313 const glsl_function_param *params, unsigned num_params)
314 {
315 return glsl_type::get_function_instance(return_type, params, num_params);
316 }
317
318 const glsl_type *
319 glsl_transposed_type(const struct glsl_type *type)
320 {
321 return glsl_type::get_instance(type->base_type, type->matrix_columns,
322 type->vector_elements);
323 }