OSDN Git Service

builtins: Add the mix(gentype, gentype, float) variant.
[android-x86/external-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 <cstdio>
25 #include <stdlib.h>
26 #include "glsl_symbol_table.h"
27 #include "glsl_parser_extras.h"
28 #include "glsl_types.h"
29 #include "builtin_types.h"
30 #include "hash_table.h"
31
32
33 hash_table *glsl_type::array_types = NULL;
34
35 static void
36 add_types_to_symbol_table(glsl_symbol_table *symtab,
37                           const struct glsl_type *types,
38                           unsigned num_types, bool warn)
39 {
40    (void) warn;
41
42    for (unsigned i = 0; i < num_types; i++) {
43       symtab->add_type(types[i].name, & types[i]);
44    }
45 }
46
47
48 static void
49 generate_110_types(glsl_symbol_table *symtab)
50 {
51    add_types_to_symbol_table(symtab, builtin_core_types,
52                              Elements(builtin_core_types),
53                              false);
54    add_types_to_symbol_table(symtab, builtin_structure_types,
55                              Elements(builtin_structure_types),
56                              false);
57    add_types_to_symbol_table(symtab, builtin_110_deprecated_structure_types,
58                              Elements(builtin_110_deprecated_structure_types),
59                              false);
60    add_types_to_symbol_table(symtab, & void_type, 1, false);
61 }
62
63
64 static void
65 generate_120_types(glsl_symbol_table *symtab)
66 {
67    generate_110_types(symtab);
68
69    add_types_to_symbol_table(symtab, builtin_120_types,
70                              Elements(builtin_120_types), false);
71 }
72
73
74 static void
75 generate_130_types(glsl_symbol_table *symtab)
76 {
77    generate_120_types(symtab);
78
79    add_types_to_symbol_table(symtab, builtin_130_types,
80                              Elements(builtin_130_types), false);
81 }
82
83
84 static void
85 generate_ARB_texture_rectangle_types(glsl_symbol_table *symtab, bool warn)
86 {
87    add_types_to_symbol_table(symtab, builtin_ARB_texture_rectangle_types,
88                              Elements(builtin_ARB_texture_rectangle_types),
89                              warn);
90 }
91
92
93 void
94 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state)
95 {
96    switch (state->language_version) {
97    case 110:
98       generate_110_types(state->symbols);
99       break;
100    case 120:
101       generate_120_types(state->symbols);
102       break;
103    case 130:
104       generate_130_types(state->symbols);
105       break;
106    default:
107       /* error */
108       break;
109    }
110
111    if (state->ARB_texture_rectangle_enable) {
112       generate_ARB_texture_rectangle_types(state->symbols,
113                                            state->ARB_texture_rectangle_warn);
114    }
115 }
116
117
118 const glsl_type *glsl_type::get_base_type() const
119 {
120    switch (base_type) {
121    case GLSL_TYPE_UINT:
122       return uint_type;
123    case GLSL_TYPE_INT:
124       return int_type;
125    case GLSL_TYPE_FLOAT:
126       return float_type;
127    case GLSL_TYPE_BOOL:
128       return bool_type;
129    default:
130       return error_type;
131    }
132 }
133
134
135 ir_function *
136 glsl_type::generate_constructor(glsl_symbol_table *symtab) const
137 {
138    /* Generate the function name and add it to the symbol table.
139     */
140    ir_function *const f = new ir_function(name);
141
142    bool added = symtab->add_function(name, f);
143    assert(added);
144
145    ir_function_signature *const sig = new ir_function_signature(this);
146    f->add_signature(sig);
147
148    ir_variable **declarations =
149       (ir_variable **) malloc(sizeof(ir_variable *) * this->length);
150    for (unsigned i = 0; i < length; i++) {
151       char *const param_name = (char *) malloc(10);
152
153       snprintf(param_name, 10, "p%08X", i);
154
155       ir_variable *var = (this->base_type == GLSL_TYPE_ARRAY)
156          ? new ir_variable(fields.array, param_name)
157          : new ir_variable(fields.structure[i].type, param_name);
158
159       var->mode = ir_var_in;
160       declarations[i] = var;
161       sig->parameters.push_tail(var);
162    }
163
164    /* Generate the body of the constructor.  The body assigns each of the
165     * parameters to a portion of a local variable called __retval that has
166     * the same type as the constructor.  After initializing __retval,
167     * __retval is returned.
168     */
169    ir_variable *retval = new ir_variable(this, "__retval");
170    sig->body.push_tail(retval);
171
172    for (unsigned i = 0; i < length; i++) {
173       ir_dereference *const lhs = (this->base_type == GLSL_TYPE_ARRAY)
174          ? (ir_dereference *) new ir_dereference_array(retval, new ir_constant(i))
175          : (ir_dereference *) new ir_dereference_record(retval, fields.structure[i].name);
176
177       ir_dereference *const rhs = new ir_dereference_variable(declarations[i]);
178       ir_instruction *const assign = new ir_assignment(lhs, rhs, NULL);
179
180       sig->body.push_tail(assign);
181    }
182
183    free(declarations);
184
185    ir_dereference *const retref = new ir_dereference_variable(retval);
186    ir_instruction *const inst = new ir_return(retref);
187    sig->body.push_tail(inst);
188
189    return f;
190 }
191
192
193 /**
194  * Generate the function intro for a constructor
195  *
196  * \param type         Data type to be constructed
197  * \param count        Number of parameters to this concrete constructor.  Most
198  *                     types have at least two constructors.  One will take a
199  *                     single scalar parameter and the other will take "N"
200  *                     scalar parameters.
201  * \param parameters   Storage for the list of parameters.  These are
202  *                     typically stored in an \c ir_function_signature.
203  * \param declarations Pointers to the variable declarations for the function
204  *                     parameters.  These are used later to avoid having to use
205  *                     the symbol table.
206  */
207 static ir_function_signature *
208 generate_constructor_intro(const glsl_type *type, unsigned parameter_count,
209                            ir_variable **declarations)
210 {
211    /* Names of parameters used in vector and matrix constructors
212     */
213    static const char *const names[] = {
214       "a", "b", "c", "d", "e", "f", "g", "h",
215       "i", "j", "k", "l", "m", "n", "o", "p",
216    };
217
218    assert(parameter_count <= Elements(names));
219
220    const glsl_type *const parameter_type = type->get_base_type();
221
222    ir_function_signature *const signature = new ir_function_signature(type);
223
224    for (unsigned i = 0; i < parameter_count; i++) {
225       ir_variable *var = new ir_variable(parameter_type, names[i]);
226
227       var->mode = ir_var_in;
228       signature->parameters.push_tail(var);
229
230       declarations[i] = var;
231    }
232
233    ir_variable *retval = new ir_variable(type, "__retval");
234    signature->body.push_tail(retval);
235
236    declarations[16] = retval;
237    return signature;
238 }
239
240
241 /**
242  * Generate the body of a vector constructor that takes a single scalar
243  */
244 static void
245 generate_vec_body_from_scalar(exec_list *instructions,
246                               ir_variable **declarations)
247 {
248    ir_instruction *inst;
249
250    /* Generate a single assignment of the parameter to __retval.x and return
251     * __retval.xxxx for however many vector components there are.
252     */
253    ir_dereference *const lhs_ref =
254       new ir_dereference_variable(declarations[16]);
255    ir_dereference *const rhs = new ir_dereference_variable(declarations[0]);
256
257    ir_swizzle *lhs = new ir_swizzle(lhs_ref, 0, 0, 0, 0, 1);
258
259    inst = new ir_assignment(lhs, rhs, NULL);
260    instructions->push_tail(inst);
261
262    ir_dereference *const retref = new ir_dereference_variable(declarations[16]);
263
264    ir_swizzle *retval = new ir_swizzle(retref, 0, 0, 0, 0,
265                                        declarations[16]->type->vector_elements);
266
267    inst = new ir_return(retval);
268    instructions->push_tail(inst);
269 }
270
271
272 /**
273  * Generate the body of a vector constructor that takes multiple scalars
274  */
275 static void
276 generate_vec_body_from_N_scalars(exec_list *instructions,
277                                  ir_variable **declarations)
278 {
279    ir_instruction *inst;
280    const glsl_type *const vec_type = declarations[16]->type;
281
282
283    /* Generate an assignment of each parameter to a single component of
284     * __retval.x and return __retval.
285     */
286    for (unsigned i = 0; i < vec_type->vector_elements; i++) {
287       ir_dereference *const lhs_ref =
288          new ir_dereference_variable(declarations[16]);
289       ir_dereference *const rhs = new ir_dereference_variable(declarations[i]);
290
291       ir_swizzle *lhs = new ir_swizzle(lhs_ref, i, 0, 0, 0, 1);
292
293       inst = new ir_assignment(lhs, rhs, NULL);
294       instructions->push_tail(inst);
295    }
296
297    ir_dereference *retval = new ir_dereference_variable(declarations[16]);
298
299    inst = new ir_return(retval);
300    instructions->push_tail(inst);
301 }
302
303
304 /**
305  * Generate the body of a matrix constructor that takes a single scalar
306  */
307 static void
308 generate_mat_body_from_scalar(exec_list *instructions,
309                               ir_variable **declarations)
310 {
311    ir_instruction *inst;
312
313    /* Generate an assignment of the parameter to the X component of a
314     * temporary vector.  Set the remaining fields of the vector to 0.  The
315     * size of the vector is equal to the number of rows of the matrix.
316     *
317     * Set each column of the matrix to a successive "rotation" of the
318     * temporary vector.  This fills the matrix with 0s, but writes the single
319     * scalar along the matrix's diagonal.
320     *
321     * For a mat4x3, this is equivalent to:
322     *
323     *   vec3 tmp;
324     *   mat4x3 __retval;
325     *   tmp.x = a;
326     *   tmp.y = 0.0;
327     *   tmp.z = 0.0;
328     *   __retval[0] = tmp.xyy;
329     *   __retval[1] = tmp.yxy;
330     *   __retval[2] = tmp.yyx;
331     *   __retval[3] = tmp.yyy;
332     */
333    const glsl_type *const column_type = declarations[16]->type->column_type();
334    const glsl_type *const row_type = declarations[16]->type->row_type();
335    ir_variable *const column = new ir_variable(column_type, "v");
336
337    instructions->push_tail(column);
338
339    ir_dereference *const lhs_ref = new ir_dereference_variable(column);
340    ir_dereference *const rhs = new ir_dereference_variable(declarations[0]);
341
342    ir_swizzle *lhs = new ir_swizzle(lhs_ref, 0, 0, 0, 0, 1);
343
344    inst = new ir_assignment(lhs, rhs, NULL);
345    instructions->push_tail(inst);
346
347    const float z = 0.0f;
348    ir_constant *const zero = new ir_constant(glsl_type::float_type, &z);
349
350    for (unsigned i = 1; i < column_type->vector_elements; i++) {
351       ir_dereference *const lhs_ref = new ir_dereference_variable(column);
352
353       ir_swizzle *lhs = new ir_swizzle(lhs_ref, i, 0, 0, 0, 1);
354
355       inst = new ir_assignment(lhs, zero, NULL);
356       instructions->push_tail(inst);
357    }
358
359
360    for (unsigned i = 0; i < row_type->vector_elements; i++) {
361       static const unsigned swiz[] = { 1, 1, 1, 0, 1, 1, 1 };
362       ir_dereference *const rhs_ref = new ir_dereference_variable(column);
363
364       /* This will be .xyyy when i=0, .yxyy when i=1, etc.
365        */
366       ir_swizzle *rhs = new ir_swizzle(rhs_ref, swiz[3 - i], swiz[4 - i],
367                                        swiz[5 - i], swiz[6 - i],
368                                        column_type->vector_elements);
369
370       ir_constant *const idx = new ir_constant(glsl_type::int_type, &i);
371       ir_dereference *const lhs =
372          new ir_dereference_array(declarations[16], idx);
373
374       inst = new ir_assignment(lhs, rhs, NULL);
375       instructions->push_tail(inst);
376    }
377
378    ir_dereference *const retval = new ir_dereference_variable(declarations[16]);
379    inst = new ir_return(retval);
380    instructions->push_tail(inst);
381 }
382
383
384 /**
385  * Generate the body of a vector constructor that takes multiple scalars
386  */
387 static void
388 generate_mat_body_from_N_scalars(exec_list *instructions,
389                                  ir_variable **declarations)
390 {
391    ir_instruction *inst;
392    const glsl_type *const row_type = declarations[16]->type->row_type();
393    const glsl_type *const column_type = declarations[16]->type->column_type();
394
395
396    /* Generate an assignment of each parameter to a single component of
397     * of a particular column of __retval and return __retval.
398     */
399    for (unsigned i = 0; i < column_type->vector_elements; i++) {
400       for (unsigned j = 0; j < row_type->vector_elements; j++) {
401          ir_constant *row_index = new ir_constant(glsl_type::int_type, &i);
402          ir_dereference *const row_access =
403             new ir_dereference_array(declarations[16], row_index);
404
405          ir_swizzle *component_access = new ir_swizzle(row_access,
406                                                        j, 0, 0, 0, 1);
407
408          const unsigned param = (i * row_type->vector_elements) + j;
409          ir_dereference *const rhs =
410             new ir_dereference_variable(declarations[param]);
411
412          inst = new ir_assignment(component_access, rhs, NULL);
413          instructions->push_tail(inst);
414       }
415    }
416
417    ir_dereference *retval = new ir_dereference_variable(declarations[16]);
418
419    inst = new ir_return(retval);
420    instructions->push_tail(inst);
421 }
422
423
424 /**
425  * Generate the constructors for a set of GLSL types
426  *
427  * Constructor implementations are added to \c instructions, and the symbols
428  * are added to \c symtab.
429  */
430 static void
431 generate_constructor(glsl_symbol_table *symtab, const struct glsl_type *types,
432                      unsigned num_types, exec_list *instructions)
433 {
434    ir_variable *declarations[17];
435
436    for (unsigned i = 0; i < num_types; i++) {
437       /* Only numeric and boolean vectors and matrices get constructors here.
438        * Structures need to be handled elsewhere.  It is expected that scalar
439        * constructors are never actually called, so they are not generated.
440        */
441       if (!types[i].is_numeric() && !types[i].is_boolean())
442          continue;
443
444       if (types[i].is_scalar())
445          continue;
446
447       /* Generate the function block, add it to the symbol table, and emit it.
448        */
449       ir_function *const f = new ir_function(types[i].name);
450
451       bool added = symtab->add_function(types[i].name, f);
452       assert(added);
453
454       instructions->push_tail(f);
455
456       /* Each type has several basic constructors.  The total number of forms
457        * depends on the derived type.
458        *
459        * Vectors:  1 scalar, N scalars
460        * Matrices: 1 scalar, NxM scalars
461        *
462        * Several possible types of constructors are not included in this list.
463        *
464        * Scalar constructors are not included.  The expectation is that the
465        * IR generator won't actually generate these as constructor calls.  The
466        * expectation is that it will just generate the necessary type
467        * conversion.
468        *
469        * Matrix contructors from matrices are also not included.  The
470        * expectation is that the IR generator will generate a call to the
471        * appropriate from-scalars constructor.
472        */
473       ir_function_signature *const sig =
474          generate_constructor_intro(&types[i], 1, declarations);
475       f->add_signature(sig);
476
477       if (types[i].is_vector()) {
478          generate_vec_body_from_scalar(&sig->body, declarations);
479
480          ir_function_signature *const vec_sig =
481             generate_constructor_intro(&types[i], types[i].vector_elements,
482                                        declarations);
483          f->add_signature(vec_sig);
484
485          generate_vec_body_from_N_scalars(&vec_sig->body, declarations);
486       } else {
487          assert(types[i].is_matrix());
488
489          generate_mat_body_from_scalar(&sig->body, declarations);
490
491          ir_function_signature *const mat_sig =
492             generate_constructor_intro(&types[i],
493                                        (types[i].vector_elements
494                                         * types[i].matrix_columns),
495                                        declarations);
496          f->add_signature(mat_sig);
497
498          generate_mat_body_from_N_scalars(&mat_sig->body, declarations);
499       }
500    }
501 }
502
503
504 void
505 generate_110_constructors(glsl_symbol_table *symtab, exec_list *instructions)
506 {
507    generate_constructor(symtab, builtin_core_types,
508                         Elements(builtin_core_types), instructions);
509 }
510
511
512 void
513 generate_120_constructors(glsl_symbol_table *symtab, exec_list *instructions)
514 {
515    generate_110_constructors(symtab, instructions);
516
517    generate_constructor(symtab, builtin_120_types,
518                         Elements(builtin_120_types), instructions);
519 }
520
521
522 void
523 generate_130_constructors(glsl_symbol_table *symtab, exec_list *instructions)
524 {
525    generate_120_constructors(symtab, instructions);
526
527    generate_constructor(symtab, builtin_130_types,
528                         Elements(builtin_130_types), instructions);
529 }
530
531
532 void
533 _mesa_glsl_initialize_constructors(exec_list *instructions,
534                                    struct _mesa_glsl_parse_state *state)
535 {
536    switch (state->language_version) {
537    case 110:
538       generate_110_constructors(state->symbols, instructions);
539       break;
540    case 120:
541       generate_120_constructors(state->symbols, instructions);
542       break;
543    case 130:
544       generate_130_constructors(state->symbols, instructions);
545       break;
546    default:
547       /* error */
548       break;
549    }
550 }
551
552
553 glsl_type::glsl_type(const glsl_type *array, unsigned length) :
554    base_type(GLSL_TYPE_ARRAY),
555    sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
556    sampler_type(0),
557    vector_elements(0), matrix_columns(0),
558    name(NULL), length(length)
559 {
560    this->fields.array = array;
561
562    /* Allow a maximum of 10 characters for the array size.  This is enough
563     * for 32-bits of ~0.  The extra 3 are for the '[', ']', and terminating
564     * NUL.
565     */
566    const unsigned name_length = strlen(array->name) + 10 + 3;
567    char *const n = (char *) malloc(name_length);
568
569    if (length == 0)
570       snprintf(n, name_length, "%s[]", array->name);
571    else
572       snprintf(n, name_length, "%s[%u]", array->name, length);
573
574    this->name = n;
575 }
576
577
578 const glsl_type *
579 glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
580 {
581    if (base_type == GLSL_TYPE_VOID)
582       return &void_type;
583
584    if ((rows < 1) || (rows > 4) || (columns < 1) || (columns > 4))
585       return error_type;
586
587    /* Treat GLSL vectors as Nx1 matrices.
588     */
589    if (columns == 1) {
590       switch (base_type) {
591       case GLSL_TYPE_UINT:
592          return uint_type + (rows - 1);
593       case GLSL_TYPE_INT:
594          return int_type + (rows - 1);
595       case GLSL_TYPE_FLOAT:
596          return float_type + (rows - 1);
597       case GLSL_TYPE_BOOL:
598          return bool_type + (rows - 1);
599       default:
600          return error_type;
601       }
602    } else {
603       if ((base_type != GLSL_TYPE_FLOAT) || (rows == 1))
604          return error_type;
605
606       /* GLSL matrix types are named mat{COLUMNS}x{ROWS}.  Only the following
607        * combinations are valid:
608        *
609        *   1 2 3 4
610        * 1
611        * 2   x x x
612        * 3   x x x
613        * 4   x x x
614        */
615 #define IDX(c,r) (((c-1)*3) + (r-1))
616
617       switch (IDX(columns, rows)) {
618       case IDX(2,2): return mat2_type;
619       case IDX(2,3): return mat2x3_type;
620       case IDX(2,4): return mat2x4_type;
621       case IDX(3,2): return mat3x2_type;
622       case IDX(3,3): return mat3_type;
623       case IDX(3,4): return mat3x4_type;
624       case IDX(4,2): return mat4x2_type;
625       case IDX(4,3): return mat4x3_type;
626       case IDX(4,4): return mat4_type;
627       default: return error_type;
628       }
629    }
630
631    assert(!"Should not get here.");
632    return error_type;
633 }
634
635
636 int
637 glsl_type::array_key_compare(const void *a, const void *b)
638 {
639    const glsl_type *const key1 = (glsl_type *) a;
640    const glsl_type *const key2 = (glsl_type *) b;
641
642    /* Return zero is the types match (there is zero difference) or non-zero
643     * otherwise.
644     */
645    return ((key1->fields.array == key2->fields.array)
646            && (key1->length == key2->length)) ? 0 : 1;
647 }
648
649
650 unsigned
651 glsl_type::array_key_hash(const void *a)
652 {
653    const glsl_type *const key = (glsl_type *) a;
654
655    const struct {
656       const glsl_type *t;
657       unsigned l;
658       char nul;
659    } hash_key = {
660       key->fields.array,
661       key->length,
662       '\0'
663    };
664
665    return hash_table_string_hash(& hash_key);
666 }
667
668
669 const glsl_type *
670 glsl_type::get_array_instance(const glsl_type *base, unsigned array_size)
671 {
672    const glsl_type key(base, array_size);
673
674    if (array_types == NULL) {
675       array_types = hash_table_ctor(64, array_key_hash, array_key_compare);
676    }
677
678    const glsl_type *t = (glsl_type *) hash_table_find(array_types, & key);
679    if (t == NULL) {
680       t = new glsl_type(base, array_size);
681
682       hash_table_insert(array_types, (void *) t, t);
683    }
684
685    assert(t->base_type == GLSL_TYPE_ARRAY);
686    assert(t->length == array_size);
687    assert(t->fields.array == base);
688
689    return t;
690 }
691
692
693 const glsl_type *
694 glsl_type::field_type(const char *name) const
695 {
696    if (this->base_type != GLSL_TYPE_STRUCT)
697       return error_type;
698
699    for (unsigned i = 0; i < this->length; i++) {
700       if (strcmp(name, this->fields.structure[i].name) == 0)
701          return this->fields.structure[i].type;
702    }
703
704    return error_type;
705 }