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