Add glsl_type::get_instance method
[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 struct glsl_type *
98 _mesa_glsl_get_vector_type(unsigned base_type, unsigned vector_length)
99 {
100 switch (base_type) {
101 case GLSL_TYPE_UINT:
102 switch (vector_length) {
103 case 1:
104 case 2:
105 case 3:
106 case 4:
107 return glsl_uint_type + (vector_length - 1);
108 default:
109 return glsl_error_type;
110 }
111 case GLSL_TYPE_INT:
112 switch (vector_length) {
113 case 1:
114 case 2:
115 case 3:
116 case 4:
117 return glsl_int_type + (vector_length - 1);
118 default:
119 return glsl_error_type;
120 }
121 case GLSL_TYPE_FLOAT:
122 switch (vector_length) {
123 case 1:
124 case 2:
125 case 3:
126 case 4:
127 return glsl_float_type + (vector_length - 1);
128 default:
129 return glsl_error_type;
130 }
131 case GLSL_TYPE_BOOL:
132 switch (vector_length) {
133 case 1:
134 case 2:
135 case 3:
136 case 4:
137 return glsl_bool_type + (vector_length - 1);
138 default:
139 return glsl_error_type;
140 }
141 default:
142 return glsl_error_type;
143 }
144 }
145
146
147 const glsl_type *glsl_type::get_base_type() const
148 {
149 switch (base_type) {
150 case GLSL_TYPE_UINT:
151 return glsl_uint_type;
152 case GLSL_TYPE_INT:
153 return glsl_int_type;
154 case GLSL_TYPE_FLOAT:
155 return glsl_float_type;
156 case GLSL_TYPE_BOOL:
157 return glsl_bool_type;
158 default:
159 return glsl_error_type;
160 }
161 }
162
163
164 const glsl_type *
165 glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
166 {
167 if ((rows < 1) || (rows > 4) || (columns < 1) || (columns > 4))
168 return glsl_error_type;
169
170
171 /* Treat GLSL vectors as Nx1 matrices.
172 */
173 if (columns == 1) {
174 switch (base_type) {
175 case GLSL_TYPE_UINT:
176 return glsl_uint_type + (rows - 1);
177 case GLSL_TYPE_INT:
178 return glsl_int_type + (rows - 1);
179 case GLSL_TYPE_FLOAT:
180 return glsl_float_type + (rows - 1);
181 case GLSL_TYPE_BOOL:
182 return glsl_bool_type + (rows - 1);
183 default:
184 return glsl_error_type;
185 }
186 } else {
187 if ((base_type != GLSL_TYPE_FLOAT) || (rows == 1))
188 return glsl_error_type;
189
190 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
191 * combinations are valid:
192 *
193 * 1 2 3 4
194 * 1
195 * 2 x x x
196 * 3 x x x
197 * 4 x x x
198 */
199 #define IDX(c,r) (((c-1)*3) + (r-1))
200
201 switch (IDX(columns, rows)) {
202 case IDX(2,2): return mat2_type;
203 case IDX(2,3): return mat2x3_type;
204 case IDX(2,4): return mat2x4_type;
205 case IDX(3,2): return mat3x2_type;
206 case IDX(3,3): return mat3_type;
207 case IDX(3,4): return mat3x4_type;
208 case IDX(4,2): return mat4x2_type;
209 case IDX(4,3): return mat4x3_type;
210 case IDX(4,4): return mat4_type;
211 default: return glsl_error_type;
212 }
213 }
214
215 assert(!"Should not get here.");
216 return glsl_error_type;
217 }