2 * Copyright © 2010 Intel Corporation
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:
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
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.
25 #include "glsl_parser_extras.h"
26 #include "glsl_symbol_table.h"
27 #include "builtin_variables.h"
29 static void generate_ARB_draw_buffers_variables(exec_list
*,
30 struct _mesa_glsl_parse_state
*,
31 bool, _mesa_glsl_parser_targets
);
34 generate_ARB_draw_instanced_variables(exec_list
*,
35 struct _mesa_glsl_parse_state
*,
36 bool, _mesa_glsl_parser_targets
);
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
)
43 ir_variable
*var
= new(symtab
) ir_variable(type
, name
, mode
);
50 case ir_var_system_value
:
51 var
->read_only
= true;
62 var
->explicit_location
= (slot
>= 0);
64 /* Once the variable is created an initialized, add it to the symbol table
65 * and add the declaration to the IR stream.
67 instructions
->push_tail(var
);
69 symtab
->add_variable(var
);
74 add_uniform(exec_list
*instructions
, glsl_symbol_table
*symtab
,
75 const char *name
, const glsl_type
*type
)
77 return add_variable(instructions
, symtab
, name
, type
, ir_var_uniform
, -1);
81 add_builtin_variable(exec_list
*instructions
, glsl_symbol_table
*symtab
,
82 const builtin_variable
*proto
)
84 /* Create a new variable declaration from the description supplied by
87 const glsl_type
*const type
= symtab
->get_type(proto
->type
);
91 add_variable(instructions
, symtab
, proto
->name
, type
, proto
->mode
,
96 add_builtin_constant(exec_list
*instructions
, glsl_symbol_table
*symtab
,
97 const char *name
, int value
)
99 ir_variable
*const var
= add_variable(instructions
, symtab
,
100 name
, glsl_type::int_type
,
102 var
->constant_value
= new(var
) ir_constant(value
);
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.
109 generate_100ES_uniforms(exec_list
*instructions
,
110 struct _mesa_glsl_parse_state
*state
)
112 glsl_symbol_table
*const symtab
= state
->symbols
;
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
);
129 add_uniform(instructions
, symtab
, "gl_DepthRange",
130 state
->symbols
->get_type("gl_DepthRangeParameters"));
134 generate_110_uniforms(exec_list
*instructions
,
135 struct _mesa_glsl_parse_state
*state
)
137 glsl_symbol_table
*const symtab
= state
->symbols
;
140 ; i
< Elements(builtin_110_deprecated_uniforms
)
142 add_builtin_variable(instructions
, symtab
,
143 & builtin_110_deprecated_uniforms
[i
]);
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
);
169 const glsl_type
*const mat4_array_type
=
170 glsl_type::get_array_instance(glsl_type::mat4_type
,
171 state
->Const
.MaxTextureCoords
);
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
);
178 add_uniform(instructions
, symtab
, "gl_DepthRange",
179 symtab
->get_type("gl_DepthRangeParameters"));
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"));
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
);
192 const glsl_type
*const light_source_array_type
=
193 glsl_type::get_array_instance(symtab
->get_type("gl_LightSourceParameters"), state
->Const
.MaxLights
);
195 add_uniform(instructions
, symtab
, "gl_LightSource", light_source_array_type
);
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
);
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
);
210 add_uniform(instructions
, symtab
, "gl_TextureEnvColor",
211 glsl_type::get_array_instance(glsl_type::vec4_type
,
212 state
->Const
.MaxTextureUnits
));
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
);
226 add_uniform(instructions
, symtab
, "gl_Fog",
227 symtab
->get_type("gl_FogParameters"));
230 /* This function should only be called for ES, not desktop GL. */
232 generate_100ES_vs_variables(exec_list
*instructions
,
233 struct _mesa_glsl_parse_state
*state
)
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
]);
240 generate_100ES_uniforms(instructions
, state
);
242 generate_ARB_draw_buffers_variables(instructions
, state
, false,
248 generate_110_vs_variables(exec_list
*instructions
,
249 struct _mesa_glsl_parse_state
*state
)
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
]);
257 ; i
< Elements(builtin_110_deprecated_vs_variables
)
259 add_builtin_variable(instructions
, state
->symbols
,
260 & builtin_110_deprecated_vs_variables
[i
]);
262 generate_110_uniforms(instructions
, state
);
264 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
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."
272 const glsl_type
*const vec4_array_type
=
273 glsl_type::get_array_instance(glsl_type::vec4_type
, 0);
275 add_variable(instructions
, state
->symbols
,
276 "gl_TexCoord", vec4_array_type
, ir_var_out
, VERT_RESULT_TEX0
);
278 generate_ARB_draw_buffers_variables(instructions
, state
, false,
284 generate_120_vs_variables(exec_list
*instructions
,
285 struct _mesa_glsl_parse_state
*state
)
287 /* GLSL version 1.20 did not add any built-in variables in the vertex
290 generate_110_vs_variables(instructions
, state
);
295 generate_130_vs_variables(exec_list
*instructions
,
296 struct _mesa_glsl_parse_state
*state
)
298 generate_120_vs_variables(instructions
, state
);
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
]);
305 const glsl_type
*const clip_distance_array_type
=
306 glsl_type::get_array_instance(glsl_type::float_type
,
307 state
->Const
.MaxClipPlanes
);
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);
317 initialize_vs_variables(exec_list
*instructions
,
318 struct _mesa_glsl_parse_state
*state
)
321 switch (state
->language_version
) {
323 generate_100ES_vs_variables(instructions
, state
);
326 generate_110_vs_variables(instructions
, state
);
329 generate_120_vs_variables(instructions
, state
);
332 generate_130_vs_variables(instructions
, state
);
336 if (state
->ARB_draw_instanced_enable
)
337 generate_ARB_draw_instanced_variables(instructions
, state
, false,
342 /* This function should only be called for ES, not desktop GL. */
344 generate_100ES_fs_variables(exec_list
*instructions
,
345 struct _mesa_glsl_parse_state
*state
)
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
]);
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
]);
357 generate_100ES_uniforms(instructions
, state
);
359 generate_ARB_draw_buffers_variables(instructions
, state
, false,
364 generate_110_fs_variables(exec_list
*instructions
,
365 struct _mesa_glsl_parse_state
*state
)
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
]);
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
]);
378 ; i
< Elements(builtin_110_deprecated_fs_variables
)
380 add_builtin_variable(instructions
, state
->symbols
,
381 & builtin_110_deprecated_fs_variables
[i
]);
383 generate_110_uniforms(instructions
, state
);
385 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
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."
393 const glsl_type
*const vec4_array_type
=
394 glsl_type::get_array_instance(glsl_type::vec4_type
, 0);
396 add_variable(instructions
, state
->symbols
,
397 "gl_TexCoord", vec4_array_type
, ir_var_in
, FRAG_ATTRIB_TEX0
);
399 generate_ARB_draw_buffers_variables(instructions
, state
, false,
405 generate_ARB_draw_buffers_variables(exec_list
*instructions
,
406 struct _mesa_glsl_parse_state
*state
,
407 bool warn
, _mesa_glsl_parser_targets target
)
409 /* gl_MaxDrawBuffers is available in all shader stages.
411 ir_variable
*const mdb
=
412 add_variable(instructions
, state
->symbols
,
413 "gl_MaxDrawBuffers", glsl_type::int_type
, ir_var_auto
, -1);
416 mdb
->warn_extension
= "GL_ARB_draw_buffers";
418 mdb
->constant_value
= new(mdb
)
419 ir_constant(int(state
->Const
.MaxDrawBuffers
));
422 /* gl_FragData is only available in the fragment shader.
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
);
429 ir_variable
*const fd
=
430 add_variable(instructions
, state
->symbols
,
431 "gl_FragData", vec4_array_type
,
432 ir_var_out
, FRAG_RESULT_DATA0
);
435 fd
->warn_extension
= "GL_ARB_draw_buffers";
441 generate_ARB_draw_instanced_variables(exec_list
*instructions
,
442 struct _mesa_glsl_parse_state
*state
,
444 _mesa_glsl_parser_targets target
)
446 /* gl_InstanceIDARB is only available in the vertex shader.
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
);
455 inst
->warn_extension
= "GL_ARB_draw_instanced";
461 generate_ARB_shader_stencil_export_variables(exec_list
*instructions
,
462 struct _mesa_glsl_parse_state
*state
,
465 /* gl_FragStencilRefARB is only available in the fragment shader.
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
);
473 fd
->warn_extension
= "GL_ARB_shader_stencil_export";
477 generate_120_fs_variables(exec_list
*instructions
,
478 struct _mesa_glsl_parse_state
*state
)
480 generate_110_fs_variables(instructions
, state
);
483 ; i
< Elements(builtin_120_fs_variables
)
485 add_builtin_variable(instructions
, state
->symbols
,
486 & builtin_120_fs_variables
[i
]);
491 generate_130_fs_variables(exec_list
*instructions
,
492 struct _mesa_glsl_parse_state
*state
)
494 generate_120_fs_variables(instructions
, state
);
496 const glsl_type
*const clip_distance_array_type
=
497 glsl_type::get_array_instance(glsl_type::float_type
,
498 state
->Const
.MaxClipPlanes
);
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);
506 initialize_fs_variables(exec_list
*instructions
,
507 struct _mesa_glsl_parse_state
*state
)
510 switch (state
->language_version
) {
512 generate_100ES_fs_variables(instructions
, state
);
515 generate_110_fs_variables(instructions
, state
);
518 generate_120_fs_variables(instructions
, state
);
521 generate_130_fs_variables(instructions
, state
);
525 if (state
->ARB_shader_stencil_export_enable
)
526 generate_ARB_shader_stencil_export_variables(instructions
, state
,
527 state
->ARB_shader_stencil_export_warn
);
531 _mesa_glsl_initialize_variables(exec_list
*instructions
,
532 struct _mesa_glsl_parse_state
*state
)
534 switch (state
->target
) {
536 initialize_vs_variables(instructions
, state
);
538 case geometry_shader
:
540 case fragment_shader
:
541 initialize_fs_variables(instructions
, state
);