ir_variable: Add query to get number of slots used by a variable
[mesa.git] / ir_variable.cpp
1 /*
2 * Copyright © 2010 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 "ir.h"
25 #include "glsl_parser_extras.h"
26 #include "glsl_symbol_table.h"
27 #include "builtin_variables.h"
28
29 #ifndef Elements
30 #define Elements(x) (sizeof(x)/sizeof(*(x)))
31 #endif
32
33 static ir_variable *
34 add_variable(const char *name, enum ir_variable_mode mode, int slot,
35 const glsl_type *type, exec_list *instructions,
36 glsl_symbol_table *symtab)
37 {
38 ir_variable *var = new ir_variable(type, name);
39
40 var->mode = mode;
41 switch (var->mode) {
42 case ir_var_in:
43 var->shader_in = true;
44 var->read_only = true;
45 break;
46 case ir_var_inout:
47 var->shader_in = true;
48 var->shader_out = true;
49 break;
50 case ir_var_out:
51 var->shader_out = true;
52 break;
53 case ir_var_uniform:
54 var->shader_in = true;
55 var->read_only = true;
56 break;
57 default:
58 assert(0);
59 break;
60 }
61
62 var->location = slot;
63
64 /* Once the variable is created an initialized, add it to the symbol table
65 * and add the declaration to the IR stream.
66 */
67 instructions->push_tail(var);
68
69 symtab->add_variable(var->name, var);
70 return var;
71 }
72
73
74 static void
75 add_builtin_variable(const builtin_variable *proto, exec_list *instructions,
76 glsl_symbol_table *symtab)
77 {
78 /* Create a new variable declaration from the description supplied by
79 * the caller.
80 */
81 const glsl_type *const type = symtab->get_type(proto->type);
82
83 assert(type != NULL);
84
85 add_variable(proto->name, proto->mode, proto->slot, type, instructions,
86 symtab);
87 }
88
89
90 static void
91 generate_110_uniforms(exec_list *instructions,
92 glsl_symbol_table *symtab)
93 {
94 for (unsigned i = 0
95 ; i < Elements(builtin_110_deprecated_uniforms)
96 ; i++) {
97 add_builtin_variable(& builtin_110_deprecated_uniforms[i],
98 instructions, symtab);
99 }
100
101 /* FINISHME: The size of this array is implementation dependent based on the
102 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
103 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
104 * FINISHME: for now.
105 */
106 const glsl_type *const mat4_array_type =
107 glsl_type::get_array_instance(glsl_type::mat4_type, 4);
108
109 add_variable("gl_TextureMatrix", ir_var_uniform, -1, mat4_array_type,
110 instructions, symtab);
111
112 /* FINISHME: Add support for gl_DepthRangeParameters */
113 /* FINISHME: Add support for gl_ClipPlane[] */
114 /* FINISHME: Add support for gl_PointParameters */
115
116 /* FINISHME: Add support for gl_MaterialParameters
117 * FINISHME: (glFrontMaterial, glBackMaterial)
118 */
119
120 /* FINISHME: The size of this array is implementation dependent based on the
121 * FINISHME: value of GL_MAX_TEXTURE_LIGHTS. GL_MAX_TEXTURE_LIGHTS must be
122 * FINISHME: at least 8, so hard-code 8 for now.
123 */
124 const glsl_type *const light_source_array_type =
125 glsl_type::get_array_instance(symtab->get_type("gl_LightSourceParameters"), 8);
126
127 add_variable("gl_LightSource", ir_var_uniform, -1, light_source_array_type,
128 instructions, symtab);
129
130 /* FINISHME: Add support for gl_LightModel */
131 /* FINISHME: Add support for gl_FrontLightProduct[], gl_BackLightProduct[] */
132 /* FINISHME: Add support for gl_TextureEnvColor[] */
133 /* FINISHME: Add support for gl_ObjectPlane*[], gl_EyePlane*[] */
134 /* FINISHME: Add support for gl_Fog */
135 }
136
137 static void
138 generate_110_vs_variables(exec_list *instructions,
139 glsl_symbol_table *symtab)
140 {
141 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
142 add_builtin_variable(& builtin_core_vs_variables[i],
143 instructions, symtab);
144 }
145
146 for (unsigned i = 0
147 ; i < Elements(builtin_110_deprecated_vs_variables)
148 ; i++) {
149 add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
150 instructions, symtab);
151 }
152 generate_110_uniforms(instructions, symtab);
153
154 /* FINISHME: The size of this array is implementation dependent based on the
155 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
156 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
157 * FINISHME: for now.
158 */
159 const glsl_type *const vec4_array_type =
160 glsl_type::get_array_instance(glsl_type::vec4_type, 4);
161
162 add_variable("gl_TexCoord", ir_var_out, VERT_RESULT_TEX0, vec4_array_type,
163 instructions, symtab);
164 }
165
166
167 static void
168 generate_120_vs_variables(exec_list *instructions,
169 glsl_symbol_table *symtab)
170 {
171 /* GLSL version 1.20 did not add any built-in variables in the vertex
172 * shader.
173 */
174 generate_110_vs_variables(instructions, symtab);
175 }
176
177
178 static void
179 generate_130_vs_variables(exec_list *instructions,
180 glsl_symbol_table *symtab)
181 {
182 generate_120_vs_variables(instructions, symtab);
183
184 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
185 add_builtin_variable(& builtin_130_vs_variables[i],
186 instructions, symtab);
187 }
188
189 /* FINISHME: The size of this array is implementation dependent based on
190 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
191 */
192 const glsl_type *const clip_distance_array_type =
193 glsl_type::get_array_instance(glsl_type::float_type, 8);
194
195 /* FINISHME: gl_ClipDistance needs a real location assigned. */
196 add_variable("gl_ClipDistance", ir_var_out, -1, clip_distance_array_type,
197 instructions, symtab);
198
199 }
200
201
202 static void
203 initialize_vs_variables(exec_list *instructions,
204 struct _mesa_glsl_parse_state *state)
205 {
206
207 switch (state->language_version) {
208 case 110:
209 generate_110_vs_variables(instructions, state->symbols);
210 break;
211 case 120:
212 generate_120_vs_variables(instructions, state->symbols);
213 break;
214 case 130:
215 generate_130_vs_variables(instructions, state->symbols);
216 break;
217 }
218 }
219
220 static void
221 generate_110_fs_variables(exec_list *instructions,
222 glsl_symbol_table *symtab)
223 {
224 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
225 add_builtin_variable(& builtin_core_fs_variables[i],
226 instructions, symtab);
227 }
228
229 for (unsigned i = 0
230 ; i < Elements(builtin_110_deprecated_fs_variables)
231 ; i++) {
232 add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
233 instructions, symtab);
234 }
235 generate_110_uniforms(instructions, symtab);
236
237 /* FINISHME: The size of this array is implementation dependent based on the
238 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
239 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
240 * FINISHME: for now.
241 */
242 const glsl_type *const vec4_array_type =
243 glsl_type::get_array_instance(glsl_type::vec4_type, 4);
244
245 add_variable("gl_TexCoord", ir_var_in, FRAG_ATTRIB_TEX0, vec4_array_type,
246 instructions, symtab);
247 }
248
249
250 static void
251 generate_ARB_draw_buffers_fs_variables(exec_list *instructions,
252 glsl_symbol_table *symtab, bool warn)
253 {
254 /* FINISHME: The size of this array is implementation dependent based on the
255 * FINISHME: value of GL_MAX_DRAW_BUFFERS. GL_MAX_DRAW_BUFFERS must be
256 * FINISHME: at least 1, so hard-code 1 for now.
257 */
258 const glsl_type *const vec4_array_type =
259 glsl_type::get_array_instance(glsl_type::vec4_type, 1);
260
261 ir_variable *const fd =
262 add_variable("gl_FragData", ir_var_out, FRAG_RESULT_DATA0,
263 vec4_array_type, instructions, symtab);
264
265 if (warn)
266 fd->warn_extension = "GL_ARB_draw_buffers";
267 }
268
269
270 static void
271 generate_120_fs_variables(exec_list *instructions,
272 glsl_symbol_table *symtab)
273 {
274 generate_110_fs_variables(instructions, symtab);
275 generate_ARB_draw_buffers_fs_variables(instructions, symtab, false);
276 }
277
278 static void
279 generate_130_fs_variables(exec_list *instructions,
280 glsl_symbol_table *symtab)
281 {
282 generate_120_fs_variables(instructions, symtab);
283
284 /* FINISHME: The size of this array is implementation dependent based on
285 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
286 */
287 const glsl_type *const clip_distance_array_type =
288 glsl_type::get_array_instance(glsl_type::float_type, 8);
289
290 /* FINISHME: gl_ClipDistance needs a real location assigned. */
291 add_variable("gl_ClipDistance", ir_var_in, -1, clip_distance_array_type,
292 instructions, symtab);
293 }
294
295 static void
296 initialize_fs_variables(exec_list *instructions,
297 struct _mesa_glsl_parse_state *state)
298 {
299
300 switch (state->language_version) {
301 case 110:
302 generate_110_fs_variables(instructions, state->symbols);
303 break;
304 case 120:
305 generate_120_fs_variables(instructions, state->symbols);
306 break;
307 case 130:
308 generate_130_fs_variables(instructions, state->symbols);
309 break;
310 }
311
312
313 /* Since GL_ARB_draw_buffers is included in GLSL 1.20 and later, we
314 * can basically ignore any extension settings for it.
315 */
316 if (state->language_version < 120) {
317 if (state->ARB_draw_buffers_enable) {
318 generate_ARB_draw_buffers_fs_variables(instructions, state->symbols,
319 state->ARB_draw_buffers_warn);
320 }
321 }
322 }
323
324 void
325 _mesa_glsl_initialize_variables(exec_list *instructions,
326 struct _mesa_glsl_parse_state *state)
327 {
328 switch (state->target) {
329 case vertex_shader:
330 initialize_vs_variables(instructions, state);
331 break;
332 case geometry_shader:
333 break;
334 case fragment_shader:
335 initialize_fs_variables(instructions, state);
336 break;
337 case ir_shader:
338 fprintf(stderr, "ir reader has no builtin variables");
339 exit(1);
340 break;
341 }
342 }