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