Add definitions for 1.10 built-in uniforms for ff state.
[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 ir_label *
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 return label;
167 }
168
169
170 /**
171 * Generate the body of a vector constructor that takes a single scalar
172 */
173 static void
174 generate_vec_body_from_scalar(exec_list *instructions,
175 ir_variable **declarations)
176 {
177 ir_instruction *inst;
178
179 /* Generate a single assignment of the parameter to __retval.x and return
180 * __retval.xxxx for however many vector components there are.
181 */
182 ir_dereference *const lhs_ref = new ir_dereference(declarations[16]);
183 ir_dereference *const rhs = new ir_dereference(declarations[0]);
184
185 ir_swizzle *lhs = new ir_swizzle(lhs_ref, 0, 0, 0, 0, 1);
186
187 inst = new ir_assignment(lhs, rhs, NULL);
188 instructions->push_tail(inst);
189
190 ir_dereference *const retref = new ir_dereference(declarations[16]);
191
192 ir_swizzle *retval = new ir_swizzle(retref, 0, 0, 0, 0,
193 declarations[16]->type->vector_elements);
194
195 inst = new ir_return(retval);
196 instructions->push_tail(inst);
197 }
198
199
200 /**
201 * Generate the body of a vector constructor that takes multiple scalars
202 */
203 static void
204 generate_vec_body_from_N_scalars(exec_list *instructions,
205 ir_variable **declarations)
206 {
207 ir_instruction *inst;
208 const glsl_type *const vec_type = declarations[16]->type;
209
210
211 /* Generate an assignment of each parameter to a single component of
212 * __retval.x and return __retval.
213 */
214 for (unsigned i = 0; i < vec_type->vector_elements; i++) {
215 ir_dereference *const lhs_ref = new ir_dereference(declarations[16]);
216 ir_dereference *const rhs = new ir_dereference(declarations[i]);
217
218 ir_swizzle *lhs = new ir_swizzle(lhs_ref, 1, 0, 0, 0, 1);
219
220 inst = new ir_assignment(lhs, rhs, NULL);
221 instructions->push_tail(inst);
222 }
223
224 ir_dereference *retval = new ir_dereference(declarations[16]);
225
226 inst = new ir_return(retval);
227 instructions->push_tail(inst);
228 }
229
230
231 /**
232 * Generate the body of a matrix constructor that takes a single scalar
233 */
234 static void
235 generate_mat_body_from_scalar(exec_list *instructions,
236 ir_variable **declarations)
237 {
238 ir_instruction *inst;
239
240 /* Generate an assignment of the parameter to the X component of a
241 * temporary vector. Set the remaining fields of the vector to 0. The
242 * size of the vector is equal to the number of rows of the matrix.
243 *
244 * Set each column of the matrix to a successive "rotation" of the
245 * temporary vector. This fills the matrix with 0s, but writes the single
246 * scalar along the matrix's diagonal.
247 *
248 * For a mat4x3, this is equivalent to:
249 *
250 * vec3 tmp;
251 * mat4x3 __retval;
252 * tmp.x = a;
253 * tmp.y = 0.0;
254 * tmp.z = 0.0;
255 * __retval[0] = tmp.xyy;
256 * __retval[1] = tmp.yxy;
257 * __retval[2] = tmp.yyx;
258 * __retval[3] = tmp.yyy;
259 */
260 const glsl_type *const column_type = declarations[16]->type->column_type();
261 const glsl_type *const row_type = declarations[16]->type->row_type();
262 ir_variable *const column = new ir_variable(column_type, "v");
263
264 instructions->push_tail(column);
265
266 ir_dereference *const lhs_ref = new ir_dereference(column);
267 ir_dereference *const rhs = new ir_dereference(declarations[0]);
268
269 ir_swizzle *lhs = new ir_swizzle(lhs_ref, 0, 0, 0, 0, 1);
270
271 inst = new ir_assignment(lhs, rhs, NULL);
272 instructions->push_tail(inst);
273
274 const float z = 0.0f;
275 ir_constant *const zero = new ir_constant(glsl_type::float_type, &z);
276
277 for (unsigned i = 1; i < column_type->vector_elements; i++) {
278 ir_dereference *const lhs_ref = new ir_dereference(column);
279
280 ir_swizzle *lhs = new ir_swizzle(lhs_ref, i, 0, 0, 0, 1);
281
282 inst = new ir_assignment(lhs, zero, NULL);
283 instructions->push_tail(inst);
284 }
285
286
287 for (unsigned i = 0; i < row_type->vector_elements; i++) {
288 static const unsigned swiz[] = { 1, 1, 1, 0, 1, 1, 1 };
289 ir_dereference *const rhs_ref = new ir_dereference(column);
290
291 /* This will be .xyyy when i=0, .yxyy when i=1, etc.
292 */
293 ir_swizzle *rhs = new ir_swizzle(rhs_ref, swiz[3 - i], swiz[4 - i],
294 swiz[5 - i], swiz[6 - i],
295 column_type->vector_elements);
296
297 ir_constant *const idx = new ir_constant(glsl_type::int_type, &i);
298 ir_dereference *const lhs = new ir_dereference(declarations[16], idx);
299
300 inst = new ir_assignment(lhs, rhs, NULL);
301 instructions->push_tail(inst);
302 }
303
304 ir_dereference *const retval = new ir_dereference(declarations[16]);
305 inst = new ir_return(retval);
306 instructions->push_tail(inst);
307 }
308
309
310 /**
311 * Generate the body of a vector constructor that takes multiple scalars
312 */
313 static void
314 generate_mat_body_from_N_scalars(exec_list *instructions,
315 ir_variable **declarations)
316 {
317 ir_instruction *inst;
318 const glsl_type *const row_type = declarations[16]->type->row_type();
319 const glsl_type *const column_type = declarations[16]->type->column_type();
320
321
322 /* Generate an assignment of each parameter to a single component of
323 * of a particular column of __retval and return __retval.
324 */
325 for (unsigned i = 0; i < column_type->vector_elements; i++) {
326 for (unsigned j = 0; j < row_type->vector_elements; j++) {
327 ir_constant *row_index = new ir_constant(glsl_type::int_type, &i);
328 ir_dereference *const row_access =
329 new ir_dereference(declarations[16], row_index);
330
331 ir_dereference *const component_access_ref =
332 new ir_dereference(row_access);
333
334 ir_swizzle *component_access = new ir_swizzle(component_access_ref,
335 j, 0, 0, 0, 1);
336
337 const unsigned param = (i * row_type->vector_elements) + j;
338 ir_dereference *const rhs = new ir_dereference(declarations[param]);
339
340 inst = new ir_assignment(component_access, rhs, NULL);
341 instructions->push_tail(inst);
342 }
343 }
344
345 ir_dereference *retval = new ir_dereference(declarations[16]);
346
347 inst = new ir_return(retval);
348 instructions->push_tail(inst);
349 }
350
351
352 /**
353 * Generate the constructors for a set of GLSL types
354 *
355 * Constructor implementations are added to \c instructions, and the symbols
356 * are added to \c symtab.
357 */
358 static void
359 generate_constructor(glsl_symbol_table *symtab, const struct glsl_type *types,
360 unsigned num_types, exec_list *instructions)
361 {
362 ir_variable *declarations[17];
363
364 for (unsigned i = 0; i < num_types; i++) {
365 /* Only numeric and boolean vectors and matrices get constructors here.
366 * Structures need to be handled elsewhere. It is expected that scalar
367 * constructors are never actually called, so they are not generated.
368 */
369 if (!types[i].is_numeric() && !types[i].is_boolean())
370 continue;
371
372 if (types[i].is_scalar())
373 continue;
374
375 /* Generate the function name and add it to the symbol table.
376 */
377 ir_function *const f = new ir_function(types[i].name);
378
379 bool added = symtab->add_function(types[i].name, f);
380 assert(added);
381
382
383 /* Each type has several basic constructors. The total number of forms
384 * depends on the derived type.
385 *
386 * Vectors: 1 scalar, N scalars
387 * Matrices: 1 scalar, NxM scalars
388 *
389 * Several possible types of constructors are not included in this list.
390 *
391 * Scalar constructors are not included. The expectation is that the
392 * IR generator won't actually generate these as constructor calls. The
393 * expectation is that it will just generate the necessary type
394 * conversion.
395 *
396 * Matrix contructors from matrices are also not included. The
397 * expectation is that the IR generator will generate a call to the
398 * appropriate from-scalars constructor.
399 */
400 ir_function_signature *const sig = new ir_function_signature(& types[i]);
401 f->signatures.push_tail(sig);
402
403 sig->definition =
404 generate_constructor_intro(& types[i], 1, & sig->parameters,
405 instructions, declarations);
406
407 if (types[i].is_vector()) {
408 generate_vec_body_from_scalar(instructions, declarations);
409
410 ir_function_signature *const vec_sig =
411 new ir_function_signature(& types[i]);
412 f->signatures.push_tail(vec_sig);
413
414 vec_sig->definition =
415 generate_constructor_intro(& types[i], types[i].vector_elements,
416 & vec_sig->parameters, instructions,
417 declarations);
418 generate_vec_body_from_N_scalars(instructions, declarations);
419 } else {
420 assert(types[i].is_matrix());
421
422 generate_mat_body_from_scalar(instructions, declarations);
423
424 ir_function_signature *const mat_sig =
425 new ir_function_signature(& types[i]);
426 f->signatures.push_tail(mat_sig);
427
428 mat_sig->definition =
429 generate_constructor_intro(& types[i],
430 (types[i].vector_elements
431 * types[i].matrix_columns),
432 & mat_sig->parameters, instructions,
433 declarations);
434 generate_mat_body_from_N_scalars(instructions, declarations);
435 }
436 }
437 }
438
439
440 void
441 generate_110_constructors(glsl_symbol_table *symtab, exec_list *instructions)
442 {
443 generate_constructor(symtab, builtin_core_types,
444 Elements(builtin_core_types), instructions);
445 }
446
447
448 void
449 generate_120_constructors(glsl_symbol_table *symtab, exec_list *instructions)
450 {
451 generate_110_constructors(symtab, instructions);
452
453 generate_constructor(symtab, builtin_120_types,
454 Elements(builtin_120_types), instructions);
455 }
456
457
458 void
459 generate_130_constructors(glsl_symbol_table *symtab, exec_list *instructions)
460 {
461 generate_120_constructors(symtab, instructions);
462
463 generate_constructor(symtab, builtin_130_types,
464 Elements(builtin_130_types), instructions);
465 }
466
467
468 void
469 _mesa_glsl_initialize_constructors(exec_list *instructions,
470 struct _mesa_glsl_parse_state *state)
471 {
472 switch (state->language_version) {
473 case 110:
474 generate_110_constructors(state->symbols, instructions);
475 break;
476 case 120:
477 generate_120_constructors(state->symbols, instructions);
478 break;
479 case 130:
480 generate_130_constructors(state->symbols, instructions);
481 break;
482 default:
483 /* error */
484 break;
485 }
486 }
487
488
489 const glsl_type *
490 glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
491 {
492 if (base_type == GLSL_TYPE_VOID)
493 return &void_type;
494
495 if ((rows < 1) || (rows > 4) || (columns < 1) || (columns > 4))
496 return error_type;
497
498 /* Treat GLSL vectors as Nx1 matrices.
499 */
500 if (columns == 1) {
501 switch (base_type) {
502 case GLSL_TYPE_UINT:
503 return uint_type + (rows - 1);
504 case GLSL_TYPE_INT:
505 return int_type + (rows - 1);
506 case GLSL_TYPE_FLOAT:
507 return float_type + (rows - 1);
508 case GLSL_TYPE_BOOL:
509 return bool_type + (rows - 1);
510 default:
511 return error_type;
512 }
513 } else {
514 if ((base_type != GLSL_TYPE_FLOAT) || (rows == 1))
515 return error_type;
516
517 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
518 * combinations are valid:
519 *
520 * 1 2 3 4
521 * 1
522 * 2 x x x
523 * 3 x x x
524 * 4 x x x
525 */
526 #define IDX(c,r) (((c-1)*3) + (r-1))
527
528 switch (IDX(columns, rows)) {
529 case IDX(2,2): return mat2_type;
530 case IDX(2,3): return mat2x3_type;
531 case IDX(2,4): return mat2x4_type;
532 case IDX(3,2): return mat3x2_type;
533 case IDX(3,3): return mat3_type;
534 case IDX(3,4): return mat3x4_type;
535 case IDX(4,2): return mat4x2_type;
536 case IDX(4,3): return mat4x3_type;
537 case IDX(4,4): return mat4_type;
538 default: return error_type;
539 }
540 }
541
542 assert(!"Should not get here.");
543 return error_type;
544 }