glsl: add several EmitNo* options, and MaxUnrollIterations
[mesa.git] / src / glsl / 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 static void generate_ARB_draw_buffers_variables(exec_list *,
30 struct _mesa_glsl_parse_state *,
31 bool, _mesa_glsl_parser_targets);
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(symtab) ir_variable(type, name, mode);
39
40 switch (var->mode) {
41 case ir_var_auto:
42 case ir_var_in:
43 case ir_var_uniform:
44 var->read_only = true;
45 break;
46 case ir_var_inout:
47 case ir_var_out:
48 break;
49 default:
50 assert(0);
51 break;
52 }
53
54 var->location = slot;
55
56 /* Once the variable is created an initialized, add it to the symbol table
57 * and add the declaration to the IR stream.
58 */
59 instructions->push_tail(var);
60
61 symtab->add_variable(var->name, var);
62 return var;
63 }
64
65 static ir_variable *
66 add_uniform(exec_list *instructions,
67 struct _mesa_glsl_parse_state *state,
68 const char *name, const glsl_type *type)
69 {
70 return add_variable(name, ir_var_uniform, -1, type, instructions,
71 state->symbols);
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 static void
90 add_builtin_constant(exec_list *instructions,
91 struct _mesa_glsl_parse_state *state,
92 const char *name, int value)
93 {
94 ir_variable *const var = add_variable(name, ir_var_auto,
95 -1, glsl_type::int_type,
96 instructions, state->symbols);
97 var->constant_value = new(var) ir_constant(value);
98 }
99
100 /* Several constants in GLSL ES have different names than normal desktop GLSL.
101 * Therefore, this function should only be called on the ES path.
102 */
103 static void
104 generate_100ES_uniforms(exec_list *instructions,
105 struct _mesa_glsl_parse_state *state)
106 {
107 add_builtin_constant(instructions, state, "gl_MaxVertexAttribs",
108 state->Const.MaxVertexAttribs);
109 add_builtin_constant(instructions, state, "gl_MaxVertexUniformVectors",
110 state->Const.MaxVertexUniformComponents);
111 add_builtin_constant(instructions, state, "gl_MaxVaryingVectors",
112 state->Const.MaxVaryingFloats / 4);
113 add_builtin_constant(instructions, state, "gl_MaxVertexTextureImageUnits",
114 state->Const.MaxVertexTextureImageUnits);
115 add_builtin_constant(instructions, state, "gl_MaxCombinedTextureImageUnits",
116 state->Const.MaxCombinedTextureImageUnits);
117 add_builtin_constant(instructions, state, "gl_MaxTextureImageUnits",
118 state->Const.MaxTextureImageUnits);
119 add_builtin_constant(instructions, state, "gl_MaxFragmentUniformVectors",
120 state->Const.MaxFragmentUniformComponents);
121
122 add_uniform(instructions, state, "gl_DepthRange",
123 state->symbols->get_type("gl_DepthRangeParameters"));
124 }
125
126 static void
127 generate_110_uniforms(exec_list *instructions,
128 struct _mesa_glsl_parse_state *state)
129 {
130 for (unsigned i = 0
131 ; i < Elements(builtin_110_deprecated_uniforms)
132 ; i++) {
133 add_builtin_variable(& builtin_110_deprecated_uniforms[i],
134 instructions, state->symbols);
135 }
136
137 add_builtin_constant(instructions, state, "gl_MaxLights",
138 state->Const.MaxLights);
139 add_builtin_constant(instructions, state, "gl_MaxClipPlanes",
140 state->Const.MaxClipPlanes);
141 add_builtin_constant(instructions, state, "gl_MaxTextureUnits",
142 state->Const.MaxTextureUnits);
143 add_builtin_constant(instructions, state, "gl_MaxTextureCoords",
144 state->Const.MaxTextureCoords);
145 add_builtin_constant(instructions, state, "gl_MaxVertexAttribs",
146 state->Const.MaxVertexAttribs);
147 add_builtin_constant(instructions, state, "gl_MaxVertexUniformComponents",
148 state->Const.MaxVertexUniformComponents);
149 add_builtin_constant(instructions, state, "gl_MaxVaryingFloats",
150 state->Const.MaxVaryingFloats);
151 add_builtin_constant(instructions, state, "gl_MaxVertexTextureImageUnits",
152 state->Const.MaxVertexTextureImageUnits);
153 add_builtin_constant(instructions, state, "gl_MaxCombinedTextureImageUnits",
154 state->Const.MaxCombinedTextureImageUnits);
155 add_builtin_constant(instructions, state, "gl_MaxTextureImageUnits",
156 state->Const.MaxTextureImageUnits);
157 add_builtin_constant(instructions, state, "gl_MaxFragmentUniformComponents",
158 state->Const.MaxFragmentUniformComponents);
159
160 const glsl_type *const mat4_array_type =
161 glsl_type::get_array_instance(glsl_type::mat4_type,
162 state->Const.MaxTextureCoords);
163
164 add_uniform(instructions, state, "gl_TextureMatrix", mat4_array_type);
165
166 add_uniform(instructions, state, "gl_DepthRange",
167 state->symbols->get_type("gl_DepthRangeParameters"));
168
169 add_uniform(instructions, state, "gl_ClipPlane",
170 glsl_type::get_array_instance(glsl_type::vec4_type,
171 state->Const.MaxClipPlanes));
172 add_uniform(instructions, state, "gl_Point",
173 state->symbols->get_type("gl_PointParameters"));
174
175 const glsl_type *const material_parameters_type =
176 state->symbols->get_type("gl_MaterialParameters");
177 add_uniform(instructions, state, "gl_FrontMaterial", material_parameters_type);
178 add_uniform(instructions, state, "gl_BackMaterial", material_parameters_type);
179
180 const glsl_type *const light_source_array_type =
181 glsl_type::get_array_instance(state->symbols->get_type("gl_LightSourceParameters"), state->Const.MaxLights);
182
183 add_uniform(instructions, state, "gl_LightSource", light_source_array_type);
184
185 const glsl_type *const light_model_products_type =
186 state->symbols->get_type("gl_LightModelProducts");
187 add_uniform(instructions, state, "gl_FrontLightModelProduct",
188 light_model_products_type);
189 add_uniform(instructions, state, "gl_BackLightModelProduct",
190 light_model_products_type);
191
192 const glsl_type *const light_products_type =
193 glsl_type::get_array_instance(state->symbols->get_type("gl_LightProducts"),
194 state->Const.MaxLights);
195 add_uniform(instructions, state, "gl_FrontLightProduct", light_products_type);
196 add_uniform(instructions, state, "gl_BackLightProduct", light_products_type);
197
198 add_uniform(instructions, state, "gl_TextureEnvColor",
199 glsl_type::get_array_instance(glsl_type::vec4_type,
200 state->Const.MaxTextureUnits));
201
202 const glsl_type *const texcoords_vec4 =
203 glsl_type::get_array_instance(glsl_type::vec4_type,
204 state->Const.MaxTextureCoords);
205 add_uniform(instructions, state, "gl_EyePlaneS", texcoords_vec4);
206 add_uniform(instructions, state, "gl_EyePlaneT", texcoords_vec4);
207 add_uniform(instructions, state, "gl_EyePlaneR", texcoords_vec4);
208 add_uniform(instructions, state, "gl_EyePlaneQ", texcoords_vec4);
209 add_uniform(instructions, state, "gl_ObjectPlaneS", texcoords_vec4);
210 add_uniform(instructions, state, "gl_ObjectPlaneT", texcoords_vec4);
211 add_uniform(instructions, state, "gl_ObjectPlaneR", texcoords_vec4);
212 add_uniform(instructions, state, "gl_ObjectPlaneQ", texcoords_vec4);
213
214 add_uniform(instructions, state, "gl_Fog",
215 state->symbols->get_type("gl_FogParameters"));
216 }
217
218 /* This function should only be called for ES, not desktop GL. */
219 static void
220 generate_100ES_vs_variables(exec_list *instructions,
221 struct _mesa_glsl_parse_state *state)
222 {
223 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
224 add_builtin_variable(& builtin_core_vs_variables[i],
225 instructions, state->symbols);
226 }
227
228 generate_100ES_uniforms(instructions, state);
229
230 generate_ARB_draw_buffers_variables(instructions, state, false,
231 vertex_shader);
232 }
233
234
235 static void
236 generate_110_vs_variables(exec_list *instructions,
237 struct _mesa_glsl_parse_state *state)
238 {
239 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
240 add_builtin_variable(& builtin_core_vs_variables[i],
241 instructions, state->symbols);
242 }
243
244 for (unsigned i = 0
245 ; i < Elements(builtin_110_deprecated_vs_variables)
246 ; i++) {
247 add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
248 instructions, state->symbols);
249 }
250 generate_110_uniforms(instructions, state);
251
252 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
253 *
254 * "As with all arrays, indices used to subscript gl_TexCoord must
255 * either be an integral constant expressions, or this array must be
256 * re-declared by the shader with a size. The size can be at most
257 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
258 * implementation in preserving varying resources."
259 */
260 const glsl_type *const vec4_array_type =
261 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
262
263 add_variable("gl_TexCoord", ir_var_out, VERT_RESULT_TEX0, vec4_array_type,
264 instructions, state->symbols);
265
266 generate_ARB_draw_buffers_variables(instructions, state, false,
267 vertex_shader);
268 }
269
270
271 static void
272 generate_120_vs_variables(exec_list *instructions,
273 struct _mesa_glsl_parse_state *state)
274 {
275 /* GLSL version 1.20 did not add any built-in variables in the vertex
276 * shader.
277 */
278 generate_110_vs_variables(instructions, state);
279 }
280
281
282 static void
283 generate_130_vs_variables(exec_list *instructions,
284 struct _mesa_glsl_parse_state *state)
285 {
286 generate_120_vs_variables(instructions, state);
287
288 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
289 add_builtin_variable(& builtin_130_vs_variables[i],
290 instructions, state->symbols);
291 }
292
293 const glsl_type *const clip_distance_array_type =
294 glsl_type::get_array_instance(glsl_type::float_type,
295 state->Const.MaxClipPlanes);
296
297 /* FINISHME: gl_ClipDistance needs a real location assigned. */
298 add_variable("gl_ClipDistance", ir_var_out, -1, clip_distance_array_type,
299 instructions, state->symbols);
300
301 }
302
303
304 static void
305 initialize_vs_variables(exec_list *instructions,
306 struct _mesa_glsl_parse_state *state)
307 {
308
309 switch (state->language_version) {
310 case 100:
311 generate_100ES_vs_variables(instructions, state);
312 break;
313 case 110:
314 generate_110_vs_variables(instructions, state);
315 break;
316 case 120:
317 generate_120_vs_variables(instructions, state);
318 break;
319 case 130:
320 generate_130_vs_variables(instructions, state);
321 break;
322 }
323 }
324
325 /* This function should only be called for ES, not desktop GL. */
326 static void
327 generate_100ES_fs_variables(exec_list *instructions,
328 struct _mesa_glsl_parse_state *state)
329 {
330 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
331 add_builtin_variable(& builtin_core_fs_variables[i],
332 instructions, state->symbols);
333 }
334
335 for (unsigned i = 0; i < Elements(builtin_100ES_fs_variables); i++) {
336 add_builtin_variable(& builtin_100ES_fs_variables[i],
337 instructions, state->symbols);
338 }
339
340 generate_100ES_uniforms(instructions, state);
341
342 generate_ARB_draw_buffers_variables(instructions, state, false,
343 fragment_shader);
344 }
345
346 static void
347 generate_110_fs_variables(exec_list *instructions,
348 struct _mesa_glsl_parse_state *state)
349 {
350 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
351 add_builtin_variable(& builtin_core_fs_variables[i],
352 instructions, state->symbols);
353 }
354
355 for (unsigned i = 0; i < Elements(builtin_110_fs_variables); i++) {
356 add_builtin_variable(& builtin_110_fs_variables[i],
357 instructions, state->symbols);
358 }
359
360 for (unsigned i = 0
361 ; i < Elements(builtin_110_deprecated_fs_variables)
362 ; i++) {
363 add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
364 instructions, state->symbols);
365 }
366 generate_110_uniforms(instructions, state);
367
368 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
369 *
370 * "As with all arrays, indices used to subscript gl_TexCoord must
371 * either be an integral constant expressions, or this array must be
372 * re-declared by the shader with a size. The size can be at most
373 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
374 * implementation in preserving varying resources."
375 */
376 const glsl_type *const vec4_array_type =
377 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
378
379 add_variable("gl_TexCoord", ir_var_in, FRAG_ATTRIB_TEX0, vec4_array_type,
380 instructions, state->symbols);
381
382 generate_ARB_draw_buffers_variables(instructions, state, false,
383 fragment_shader);
384 }
385
386
387 static void
388 generate_ARB_draw_buffers_variables(exec_list *instructions,
389 struct _mesa_glsl_parse_state *state,
390 bool warn, _mesa_glsl_parser_targets target)
391 {
392 /* gl_MaxDrawBuffers is available in all shader stages.
393 */
394 ir_variable *const mdb =
395 add_variable("gl_MaxDrawBuffers", ir_var_auto, -1,
396 glsl_type::int_type, instructions, state->symbols);
397
398 if (warn)
399 mdb->warn_extension = "GL_ARB_draw_buffers";
400
401 mdb->constant_value = new(mdb)
402 ir_constant(int(state->Const.MaxDrawBuffers));
403
404
405 /* gl_FragData is only available in the fragment shader.
406 */
407 if (target == fragment_shader) {
408 const glsl_type *const vec4_array_type =
409 glsl_type::get_array_instance(glsl_type::vec4_type,
410 state->Const.MaxDrawBuffers);
411
412 ir_variable *const fd =
413 add_variable("gl_FragData", ir_var_out, FRAG_RESULT_DATA0,
414 vec4_array_type, instructions, state->symbols);
415
416 if (warn)
417 fd->warn_extension = "GL_ARB_draw_buffers";
418 }
419 }
420
421
422 static void
423 generate_120_fs_variables(exec_list *instructions,
424 struct _mesa_glsl_parse_state *state)
425 {
426 generate_110_fs_variables(instructions, state);
427
428 for (unsigned i = 0
429 ; i < Elements(builtin_120_fs_variables)
430 ; i++) {
431 add_builtin_variable(& builtin_120_fs_variables[i],
432 instructions, state->symbols);
433 }
434 }
435
436 static void
437 generate_130_fs_variables(exec_list *instructions,
438 struct _mesa_glsl_parse_state *state)
439 {
440 generate_120_fs_variables(instructions, state);
441
442 const glsl_type *const clip_distance_array_type =
443 glsl_type::get_array_instance(glsl_type::float_type,
444 state->Const.MaxClipPlanes);
445
446 /* FINISHME: gl_ClipDistance needs a real location assigned. */
447 add_variable("gl_ClipDistance", ir_var_in, -1, clip_distance_array_type,
448 instructions, state->symbols);
449 }
450
451 static void
452 initialize_fs_variables(exec_list *instructions,
453 struct _mesa_glsl_parse_state *state)
454 {
455
456 switch (state->language_version) {
457 case 100:
458 generate_100ES_fs_variables(instructions, state);
459 break;
460 case 110:
461 generate_110_fs_variables(instructions, state);
462 break;
463 case 120:
464 generate_120_fs_variables(instructions, state);
465 break;
466 case 130:
467 generate_130_fs_variables(instructions, state);
468 break;
469 }
470 }
471
472 void
473 _mesa_glsl_initialize_variables(exec_list *instructions,
474 struct _mesa_glsl_parse_state *state)
475 {
476 switch (state->target) {
477 case vertex_shader:
478 initialize_vs_variables(instructions, state);
479 break;
480 case geometry_shader:
481 break;
482 case fragment_shader:
483 initialize_fs_variables(instructions, state);
484 break;
485 case ir_shader:
486 fprintf(stderr, "ir reader has no builtin variables");
487 exit(1);
488 break;
489 }
490 }