Add unary operator to convert unsigned integer to float
[mesa.git] / glsl_types.cpp
1 /*
2 * Copyright © 2009 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
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include <stdlib.h>
25 #include "glsl_symbol_table.h"
26 #include "glsl_parser_extras.h"
27 #include "glsl_types.h"
28 #include "builtin_types.h"
29
30
31 static void
32 add_types_to_symbol_table(glsl_symbol_table *symtab,
33 const struct glsl_type *types,
34 unsigned num_types)
35 {
36 unsigned i;
37
38 for (i = 0; i < num_types; i++) {
39 symtab->add_type(types[i].name, & types[i]);
40 }
41 }
42
43
44 static void
45 generate_110_types(glsl_symbol_table *symtab)
46 {
47 add_types_to_symbol_table(symtab, builtin_core_types,
48 Elements(builtin_core_types));
49 add_types_to_symbol_table(symtab, builtin_structure_types,
50 Elements(builtin_structure_types));
51 add_types_to_symbol_table(symtab, builtin_110_deprecated_structure_types,
52 Elements(builtin_110_deprecated_structure_types));
53 add_types_to_symbol_table(symtab, & void_type, 1);
54 }
55
56
57 static void
58 generate_120_types(glsl_symbol_table *symtab)
59 {
60 generate_110_types(symtab);
61
62 add_types_to_symbol_table(symtab, builtin_120_types,
63 Elements(builtin_120_types));
64 }
65
66
67 static void
68 generate_130_types(glsl_symbol_table *symtab)
69 {
70 generate_120_types(symtab);
71
72 add_types_to_symbol_table(symtab, builtin_130_types,
73 Elements(builtin_130_types));
74 }
75
76
77 void
78 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state)
79 {
80 switch (state->language_version) {
81 case 110:
82 generate_110_types(state->symbols);
83 break;
84 case 120:
85 generate_120_types(state->symbols);
86 break;
87 case 130:
88 generate_130_types(state->symbols);
89 break;
90 default:
91 /* error */
92 break;
93 }
94 }
95
96
97 const glsl_type *glsl_type::get_base_type() const
98 {
99 switch (base_type) {
100 case GLSL_TYPE_UINT:
101 return uint_type;
102 case GLSL_TYPE_INT:
103 return int_type;
104 case GLSL_TYPE_FLOAT:
105 return float_type;
106 case GLSL_TYPE_BOOL:
107 return bool_type;
108 default:
109 return error_type;
110 }
111 }
112
113
114 /**
115 * Generate the function intro for a constructor
116 *
117 * \param type Data type to be constructed
118 * \param count Number of parameters to this concrete constructor. Most
119 * types have at least two constructors. One will take a
120 * single scalar parameter and the other will take "N"
121 * scalar parameters.
122 * \param parameters Storage for the list of parameters. These are
123 * typically stored in an \c ir_function_signature.
124 * \param instructions Storage for the preamble and body of the function.
125 * \param declarations Pointers to the variable declarations for the function
126 * parameters. These are used later to avoid having to use
127 * the symbol table.
128 */
129 static void
130 generate_constructor_intro(const glsl_type *type, unsigned parameter_count,
131 exec_list *parameters, exec_list *instructions,
132 ir_variable **declarations)
133 {
134 /* Names of parameters used in vector and matrix constructors
135 */
136 static const char *const names[] = {
137 "a", "b", "c", "d", "e", "f", "g", "h",
138 "i", "j", "k", "l", "m", "n", "o", "p",
139 };
140
141 assert(parameter_count <= Elements(names));
142
143 const glsl_type *const parameter_type = type->get_base_type();
144
145 ir_label *const label = new ir_label(type->name);
146 instructions->push_tail(label);
147
148 for (unsigned i = 0; i < parameter_count; i++) {
149 ir_variable *var = new ir_variable(parameter_type, names[i]);
150
151 var->mode = ir_var_in;
152 parameters->push_tail(var);
153
154 var = new ir_variable(parameter_type, names[i]);
155
156 var->mode = ir_var_in;
157 instructions->push_tail(var);
158
159 declarations[i] = var;
160 }
161
162 ir_variable *retval = new ir_variable(type, "__retval");
163 instructions->push_tail(retval);
164
165 declarations[16] = retval;
166 }
167
168
169 /**
170 * Generate the body of a vector constructor that takes a single scalar
171 */
172 static void
173 generate_vec_body_from_scalar(exec_list *instructions,
174 ir_variable **declarations)
175 {
176 ir_instruction *inst;
177
178 /* Generate a single assignment of the parameter to __retval.x and return
179 * __retval.xxxx for however many vector components there are.
180 */
181 ir_dereference *const lhs_ref = new ir_dereference(declarations[16]);
182 ir_dereference *const rhs = new ir_dereference(declarations[0]);
183
184 ir_swizzle *lhs = new ir_swizzle(lhs_ref, 0, 0, 0, 0, 1);
185
186 inst = new ir_assignment(lhs, rhs, NULL);
187 instructions->push_tail(inst);
188
189 ir_dereference *const retref = new ir_dereference(declarations[16]);
190
191 ir_swizzle *retval = new ir_swizzle(retref, 0, 0, 0, 0,
192 declarations[16]->type->vector_elements);
193
194 inst = new ir_return(retval);
195 instructions->push_tail(inst);
196 }
197
198
199 /**
200 * Generate the body of a vector constructor that takes multiple scalars
201 */
202 static void
203 generate_vec_body_from_N_scalars(exec_list *instructions,
204 ir_variable **declarations)
205 {
206 ir_instruction *inst;
207 const glsl_type *const vec_type = declarations[16]->type;
208
209
210 /* Generate an assignment of each parameter to a single component of
211 * __retval.x and return __retval.
212 */
213 for (unsigned i = 0; i < vec_type->vector_elements; i++) {
214 ir_dereference *const lhs_ref = new ir_dereference(declarations[16]);
215 ir_dereference *const rhs = new ir_dereference(declarations[i]);
216
217 ir_swizzle *lhs = new ir_swizzle(lhs_ref, 1, 0, 0, 0, 1);
218
219 inst = new ir_assignment(lhs, rhs, NULL);
220 instructions->push_tail(inst);
221 }
222
223 ir_dereference *retval = new ir_dereference(declarations[16]);
224
225 inst = new ir_return(retval);
226 instructions->push_tail(inst);
227 }
228
229
230 /**
231 * Generate the body of a matrix constructor that takes a single scalar
232 */
233 static void
234 generate_mat_body_from_scalar(exec_list *instructions,
235 ir_variable **declarations)
236 {
237 ir_instruction *inst;
238
239 /* Generate an assignment of the parameter to the X component of a
240 * temporary vector. Set the remaining fields of the vector to 0. The
241 * size of the vector is equal to the number of rows of the matrix.
242 *
243 * Set each column of the matrix to a successive "rotation" of the
244 * temporary vector. This fills the matrix with 0s, but writes the single
245 * scalar along the matrix's diagonal.
246 *
247 * For a mat4x3, this is equivalent to:
248 *
249 * vec3 tmp;
250 * mat4x3 __retval;
251 * tmp.x = a;
252 * tmp.y = 0.0;
253 * tmp.z = 0.0;
254 * __retval[0] = tmp.xyy;
255 * __retval[1] = tmp.yxy;
256 * __retval[2] = tmp.yyx;
257 * __retval[3] = tmp.yyy;
258 */
259 const glsl_type *const column_type = declarations[16]->type->column_type();
260 const glsl_type *const row_type = declarations[16]->type->row_type();
261 ir_variable *const column = new ir_variable(column_type, "v");
262
263 instructions->push_tail(column);
264
265 ir_dereference *const lhs_ref = new ir_dereference(column);
266 ir_dereference *const rhs = new ir_dereference(declarations[0]);
267
268 ir_swizzle *lhs = new ir_swizzle(lhs_ref, 0, 0, 0, 0, 1);
269
270 inst = new ir_assignment(lhs, rhs, NULL);
271 instructions->push_tail(inst);
272
273 const float z = 0.0f;
274 ir_constant *const zero = new ir_constant(glsl_type::float_type, &z);
275
276 for (unsigned i = 1; i < column_type->vector_elements; i++) {
277 ir_dereference *const lhs_ref = new ir_dereference(column);
278
279 ir_swizzle *lhs = new ir_swizzle(lhs_ref, i, 0, 0, 0, 1);
280
281 inst = new ir_assignment(lhs, zero, NULL);
282 instructions->push_tail(inst);
283 }
284
285
286 for (unsigned i = 0; i < row_type->vector_elements; i++) {
287 static const unsigned swiz[] = { 1, 1, 1, 0, 1, 1, 1 };
288 ir_dereference *const rhs_ref = new ir_dereference(column);
289
290 /* This will be .xyyy when i=0, .yxyy when i=1, etc.
291 */
292 ir_swizzle *rhs = new ir_swizzle(rhs_ref, swiz[3 - i], swiz[4 - i],
293 swiz[5 - i], swiz[6 - i],
294 column_type->vector_elements);
295
296 ir_constant *const idx = new ir_constant(glsl_type::int_type, &i);
297 ir_dereference *const lhs = new ir_dereference(declarations[16], idx);
298
299 inst = new ir_assignment(lhs, rhs, NULL);
300 instructions->push_tail(inst);
301 }
302
303 ir_dereference *const retval = new ir_dereference(declarations[16]);
304 inst = new ir_return(retval);
305 instructions->push_tail(inst);
306 }
307
308
309 /**
310 * Generate the body of a vector constructor that takes multiple scalars
311 */
312 static void
313 generate_mat_body_from_N_scalars(exec_list *instructions,
314 ir_variable **declarations)
315 {
316 ir_instruction *inst;
317 const glsl_type *const row_type = declarations[16]->type->row_type();
318 const glsl_type *const column_type = declarations[16]->type->column_type();
319
320
321 /* Generate an assignment of each parameter to a single component of
322 * of a particular column of __retval and return __retval.
323 */
324 for (unsigned i = 0; i < column_type->vector_elements; i++) {
325 for (unsigned j = 0; j < row_type->vector_elements; j++) {
326 ir_constant *row_index = new ir_constant(glsl_type::int_type, &i);
327 ir_dereference *const row_access =
328 new ir_dereference(declarations[16], row_index);
329
330 ir_dereference *const component_access_ref =
331 new ir_dereference(row_access);
332
333 ir_swizzle *component_access = new ir_swizzle(component_access_ref,
334 j, 0, 0, 0, 1);
335
336 const unsigned param = (i * row_type->vector_elements) + j;
337 ir_dereference *const rhs = new ir_dereference(declarations[param]);
338
339 inst = new ir_assignment(component_access, rhs, NULL);
340 instructions->push_tail(inst);
341 }
342 }
343
344 ir_dereference *retval = new ir_dereference(declarations[16]);
345
346 inst = new ir_return(retval);
347 instructions->push_tail(inst);
348 }
349
350
351 /**
352 * Generate the constructors for a set of GLSL types
353 *
354 * Constructor implementations are added to \c instructions, and the symbols
355 * are added to \c symtab.
356 */
357 static void
358 generate_constructor(glsl_symbol_table *symtab, const struct glsl_type *types,
359 unsigned num_types, exec_list *instructions)
360 {
361 ir_variable *declarations[17];
362
363 for (unsigned i = 0; i < num_types; i++) {
364 /* Only numeric and boolean vectors and matrices get constructors here.
365 * Structures need to be handled elsewhere. It is expected that scalar
366 * constructors are never actually called, so they are not generated.
367 */
368 if (!types[i].is_numeric() && !types[i].is_boolean())
369 continue;
370
371 if (types[i].is_scalar())
372 continue;
373
374 /* Generate the function name and add it to the symbol table.
375 */
376 ir_function *const f = new ir_function(types[i].name);
377
378 bool added = symtab->add_function(types[i].name, f);
379 assert(added);
380
381
382 /* Each type has several basic constructors. The total number of forms
383 * depends on the derived type.
384 *
385 * Vectors: 1 scalar, N scalars
386 * Matrices: 1 scalar, NxM scalars
387 *
388 * Several possible types of constructors are not included in this list.
389 *
390 * Scalar constructors are not included. The expectation is that the
391 * IR generator won't actually generate these as constructor calls. The
392 * expectation is that it will just generate the necessary type
393 * conversion.
394 *
395 * Matrix contructors from matrices are also not included. The
396 * expectation is that the IR generator will generate a call to the
397 * appropriate from-scalars constructor.
398 */
399 ir_function_signature *const sig = new ir_function_signature(& types[i]);
400 f->signatures.push_tail(sig);
401
402 generate_constructor_intro(& types[i], 1, & sig->parameters,
403 instructions, declarations);
404
405 if (types[i].is_vector()) {
406 generate_vec_body_from_scalar(instructions, declarations);
407
408 ir_function_signature *const vec_sig =
409 new ir_function_signature(& types[i]);
410 f->signatures.push_tail(vec_sig);
411
412 generate_constructor_intro(& types[i], types[i].vector_elements,
413 & vec_sig->parameters, instructions,
414 declarations);
415 generate_vec_body_from_N_scalars(instructions, declarations);
416 } else {
417 assert(types[i].is_matrix());
418
419 generate_mat_body_from_scalar(instructions, declarations);
420
421 ir_function_signature *const mat_sig =
422 new ir_function_signature(& types[i]);
423 f->signatures.push_tail(mat_sig);
424
425 generate_constructor_intro(& types[i],
426 (types[i].vector_elements
427 * types[i].matrix_columns),
428 & mat_sig->parameters, instructions,
429 declarations);
430 generate_mat_body_from_N_scalars(instructions, declarations);
431 }
432 }
433 }
434
435
436 void
437 generate_110_constructors(glsl_symbol_table *symtab, exec_list *instructions)
438 {
439 generate_constructor(symtab, builtin_core_types,
440 Elements(builtin_core_types), instructions);
441 }
442
443
444 void
445 generate_120_constructors(glsl_symbol_table *symtab, exec_list *instructions)
446 {
447 generate_110_constructors(symtab, instructions);
448
449 generate_constructor(symtab, builtin_120_types,
450 Elements(builtin_120_types), instructions);
451 }
452
453
454 void
455 generate_130_constructors(glsl_symbol_table *symtab, exec_list *instructions)
456 {
457 generate_120_constructors(symtab, instructions);
458
459 generate_constructor(symtab, builtin_130_types,
460 Elements(builtin_130_types), instructions);
461 }
462
463
464 void
465 _mesa_glsl_initialize_constructors(exec_list *instructions,
466 struct _mesa_glsl_parse_state *state)
467 {
468 switch (state->language_version) {
469 case 110:
470 generate_110_constructors(state->symbols, instructions);
471 break;
472 case 120:
473 generate_120_constructors(state->symbols, instructions);
474 break;
475 case 130:
476 generate_130_constructors(state->symbols, instructions);
477 break;
478 default:
479 /* error */
480 break;
481 }
482 }
483
484
485 const glsl_type *
486 glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
487 {
488 if ((rows < 1) || (rows > 4) || (columns < 1) || (columns > 4))
489 return error_type;
490
491
492 /* Treat GLSL vectors as Nx1 matrices.
493 */
494 if (columns == 1) {
495 switch (base_type) {
496 case GLSL_TYPE_UINT:
497 return uint_type + (rows - 1);
498 case GLSL_TYPE_INT:
499 return int_type + (rows - 1);
500 case GLSL_TYPE_FLOAT:
501 return float_type + (rows - 1);
502 case GLSL_TYPE_BOOL:
503 return bool_type + (rows - 1);
504 default:
505 return error_type;
506 }
507 } else {
508 if ((base_type != GLSL_TYPE_FLOAT) || (rows == 1))
509 return error_type;
510
511 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
512 * combinations are valid:
513 *
514 * 1 2 3 4
515 * 1
516 * 2 x x x
517 * 3 x x x
518 * 4 x x x
519 */
520 #define IDX(c,r) (((c-1)*3) + (r-1))
521
522 switch (IDX(columns, rows)) {
523 case IDX(2,2): return mat2_type;
524 case IDX(2,3): return mat2x3_type;
525 case IDX(2,4): return mat2x4_type;
526 case IDX(3,2): return mat3x2_type;
527 case IDX(3,3): return mat3_type;
528 case IDX(3,4): return mat3x4_type;
529 case IDX(4,2): return mat4x2_type;
530 case IDX(4,3): return mat4x3_type;
531 case IDX(4,4): return mat4_type;
532 default: return error_type;
533 }
534 }
535
536 assert(!"Should not get here.");
537 return error_type;
538 }