Initial commit. lol
[mesa.git] / glsl_types.c
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 "symbol_table.h"
26 #include "glsl_parser_extras.h"
27 #include "glsl_types.h"
28 #include "builtin_types.h"
29
30
31 struct glsl_type *
32 _mesa_glsl_array_type_ctor(struct glsl_type *base, unsigned length,
33 const char *name)
34 {
35 struct glsl_type *type = calloc(1, sizeof(*type));
36
37 type->base_type = GLSL_TYPE_ARRAY;
38 type->name = name;
39 type->length = length;
40 type->fields.array = base;
41
42 return type;
43 }
44
45
46 static void
47 add_types_to_symbol_table(struct _mesa_symbol_table *symtab,
48 const struct glsl_type *types,
49 unsigned num_types)
50 {
51 unsigned i;
52
53 for (i = 0; i < num_types; i++) {
54 _mesa_symbol_table_add_symbol(symtab, 0, types[i].name,
55 (void *) & types[i]);
56 }
57 }
58
59
60 static void
61 generate_110_types(struct _mesa_symbol_table *symtab)
62 {
63 add_types_to_symbol_table(symtab, builtin_core_types,
64 Elements(builtin_core_types));
65 add_types_to_symbol_table(symtab, builtin_structure_types,
66 Elements(builtin_structure_types));
67 add_types_to_symbol_table(symtab, builtin_110_deprecated_structure_types,
68 Elements(builtin_110_deprecated_structure_types));
69 }
70
71
72 static void
73 generate_120_types(struct _mesa_symbol_table *symtab)
74 {
75 generate_110_types(symtab);
76
77 add_types_to_symbol_table(symtab, builtin_120_types,
78 Elements(builtin_120_types));
79 }
80
81
82 static void
83 generate_130_types(struct _mesa_symbol_table *symtab)
84 {
85 generate_120_types(symtab);
86
87 add_types_to_symbol_table(symtab, builtin_130_types,
88 Elements(builtin_130_types));
89 }
90
91
92 void
93 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state)
94 {
95 switch (state->language_version) {
96 case 110:
97 generate_110_types(state->symbols);
98 break;
99 case 120:
100 generate_120_types(state->symbols);
101 break;
102 case 130:
103 generate_130_types(state->symbols);
104 break;
105 default:
106 /* error */
107 break;
108 }
109 }
110
111
112 const struct glsl_type *
113 _mesa_glsl_get_vector_type(unsigned base_type, unsigned vector_length)
114 {
115 switch (base_type) {
116 case GLSL_TYPE_UINT:
117 switch (vector_length) {
118 case 1:
119 case 2:
120 case 3:
121 case 4:
122 return glsl_uint_type + (vector_length - 1);
123 default:
124 return glsl_error_type;
125 }
126 case GLSL_TYPE_INT:
127 switch (vector_length) {
128 case 1:
129 case 2:
130 case 3:
131 case 4:
132 return glsl_int_type + (vector_length - 1);
133 default:
134 return glsl_error_type;
135 }
136 case GLSL_TYPE_FLOAT:
137 switch (vector_length) {
138 case 1:
139 case 2:
140 case 3:
141 case 4:
142 return glsl_float_type + (vector_length - 1);
143 default:
144 return glsl_error_type;
145 }
146 case GLSL_TYPE_BOOL:
147 switch (vector_length) {
148 case 1:
149 case 2:
150 case 3:
151 case 4:
152 return glsl_bool_type + (vector_length - 1);
153 default:
154 return glsl_error_type;
155 }
156 default:
157 return glsl_error_type;
158 }
159 }