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(const char *name
, enum ir_variable_mode mode
, int slot
,
40 const glsl_type
*type
, exec_list
*instructions
,
41 glsl_symbol_table
*symtab
)
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
,
75 struct _mesa_glsl_parse_state
*state
,
76 const char *name
, const glsl_type
*type
)
78 return add_variable(name
, ir_var_uniform
, -1, type
, instructions
,
83 add_builtin_variable(const builtin_variable
*proto
, exec_list
*instructions
,
84 glsl_symbol_table
*symtab
)
86 /* Create a new variable declaration from the description supplied by
89 const glsl_type
*const type
= symtab
->get_type(proto
->type
);
93 add_variable(proto
->name
, proto
->mode
, proto
->slot
, type
, instructions
,
98 add_builtin_constant(exec_list
*instructions
,
99 struct _mesa_glsl_parse_state
*state
,
100 const char *name
, int value
)
102 ir_variable
*const var
= add_variable(name
, ir_var_auto
,
103 -1, glsl_type::int_type
,
104 instructions
, state
->symbols
);
105 var
->constant_value
= new(var
) ir_constant(value
);
108 /* Several constants in GLSL ES have different names than normal desktop GLSL.
109 * Therefore, this function should only be called on the ES path.
112 generate_100ES_uniforms(exec_list
*instructions
,
113 struct _mesa_glsl_parse_state
*state
)
115 add_builtin_constant(instructions
, state
, "gl_MaxVertexAttribs",
116 state
->Const
.MaxVertexAttribs
);
117 add_builtin_constant(instructions
, state
, "gl_MaxVertexUniformVectors",
118 state
->Const
.MaxVertexUniformComponents
);
119 add_builtin_constant(instructions
, state
, "gl_MaxVaryingVectors",
120 state
->Const
.MaxVaryingFloats
/ 4);
121 add_builtin_constant(instructions
, state
, "gl_MaxVertexTextureImageUnits",
122 state
->Const
.MaxVertexTextureImageUnits
);
123 add_builtin_constant(instructions
, state
, "gl_MaxCombinedTextureImageUnits",
124 state
->Const
.MaxCombinedTextureImageUnits
);
125 add_builtin_constant(instructions
, state
, "gl_MaxTextureImageUnits",
126 state
->Const
.MaxTextureImageUnits
);
127 add_builtin_constant(instructions
, state
, "gl_MaxFragmentUniformVectors",
128 state
->Const
.MaxFragmentUniformComponents
);
130 add_uniform(instructions
, state
, "gl_DepthRange",
131 state
->symbols
->get_type("gl_DepthRangeParameters"));
135 generate_110_uniforms(exec_list
*instructions
,
136 struct _mesa_glsl_parse_state
*state
)
139 ; i
< Elements(builtin_110_deprecated_uniforms
)
141 add_builtin_variable(& builtin_110_deprecated_uniforms
[i
],
142 instructions
, state
->symbols
);
145 add_builtin_constant(instructions
, state
, "gl_MaxLights",
146 state
->Const
.MaxLights
);
147 add_builtin_constant(instructions
, state
, "gl_MaxClipPlanes",
148 state
->Const
.MaxClipPlanes
);
149 add_builtin_constant(instructions
, state
, "gl_MaxTextureUnits",
150 state
->Const
.MaxTextureUnits
);
151 add_builtin_constant(instructions
, state
, "gl_MaxTextureCoords",
152 state
->Const
.MaxTextureCoords
);
153 add_builtin_constant(instructions
, state
, "gl_MaxVertexAttribs",
154 state
->Const
.MaxVertexAttribs
);
155 add_builtin_constant(instructions
, state
, "gl_MaxVertexUniformComponents",
156 state
->Const
.MaxVertexUniformComponents
);
157 add_builtin_constant(instructions
, state
, "gl_MaxVaryingFloats",
158 state
->Const
.MaxVaryingFloats
);
159 add_builtin_constant(instructions
, state
, "gl_MaxVertexTextureImageUnits",
160 state
->Const
.MaxVertexTextureImageUnits
);
161 add_builtin_constant(instructions
, state
, "gl_MaxCombinedTextureImageUnits",
162 state
->Const
.MaxCombinedTextureImageUnits
);
163 add_builtin_constant(instructions
, state
, "gl_MaxTextureImageUnits",
164 state
->Const
.MaxTextureImageUnits
);
165 add_builtin_constant(instructions
, state
, "gl_MaxFragmentUniformComponents",
166 state
->Const
.MaxFragmentUniformComponents
);
168 const glsl_type
*const mat4_array_type
=
169 glsl_type::get_array_instance(glsl_type::mat4_type
,
170 state
->Const
.MaxTextureCoords
);
172 add_uniform(instructions
, state
, "gl_TextureMatrix", mat4_array_type
);
173 add_uniform(instructions
, state
, "gl_TextureMatrixInverse", mat4_array_type
);
174 add_uniform(instructions
, state
, "gl_TextureMatrixTranspose", mat4_array_type
);
175 add_uniform(instructions
, state
, "gl_TextureMatrixInverseTranspose", mat4_array_type
);
177 add_uniform(instructions
, state
, "gl_DepthRange",
178 state
->symbols
->get_type("gl_DepthRangeParameters"));
180 add_uniform(instructions
, state
, "gl_ClipPlane",
181 glsl_type::get_array_instance(glsl_type::vec4_type
,
182 state
->Const
.MaxClipPlanes
));
183 add_uniform(instructions
, state
, "gl_Point",
184 state
->symbols
->get_type("gl_PointParameters"));
186 const glsl_type
*const material_parameters_type
=
187 state
->symbols
->get_type("gl_MaterialParameters");
188 add_uniform(instructions
, state
, "gl_FrontMaterial", material_parameters_type
);
189 add_uniform(instructions
, state
, "gl_BackMaterial", material_parameters_type
);
191 const glsl_type
*const light_source_array_type
=
192 glsl_type::get_array_instance(state
->symbols
->get_type("gl_LightSourceParameters"), state
->Const
.MaxLights
);
194 add_uniform(instructions
, state
, "gl_LightSource", light_source_array_type
);
196 const glsl_type
*const light_model_products_type
=
197 state
->symbols
->get_type("gl_LightModelProducts");
198 add_uniform(instructions
, state
, "gl_FrontLightModelProduct",
199 light_model_products_type
);
200 add_uniform(instructions
, state
, "gl_BackLightModelProduct",
201 light_model_products_type
);
203 const glsl_type
*const light_products_type
=
204 glsl_type::get_array_instance(state
->symbols
->get_type("gl_LightProducts"),
205 state
->Const
.MaxLights
);
206 add_uniform(instructions
, state
, "gl_FrontLightProduct", light_products_type
);
207 add_uniform(instructions
, state
, "gl_BackLightProduct", light_products_type
);
209 add_uniform(instructions
, state
, "gl_TextureEnvColor",
210 glsl_type::get_array_instance(glsl_type::vec4_type
,
211 state
->Const
.MaxTextureUnits
));
213 const glsl_type
*const texcoords_vec4
=
214 glsl_type::get_array_instance(glsl_type::vec4_type
,
215 state
->Const
.MaxTextureCoords
);
216 add_uniform(instructions
, state
, "gl_EyePlaneS", texcoords_vec4
);
217 add_uniform(instructions
, state
, "gl_EyePlaneT", texcoords_vec4
);
218 add_uniform(instructions
, state
, "gl_EyePlaneR", texcoords_vec4
);
219 add_uniform(instructions
, state
, "gl_EyePlaneQ", texcoords_vec4
);
220 add_uniform(instructions
, state
, "gl_ObjectPlaneS", texcoords_vec4
);
221 add_uniform(instructions
, state
, "gl_ObjectPlaneT", texcoords_vec4
);
222 add_uniform(instructions
, state
, "gl_ObjectPlaneR", texcoords_vec4
);
223 add_uniform(instructions
, state
, "gl_ObjectPlaneQ", texcoords_vec4
);
225 add_uniform(instructions
, state
, "gl_Fog",
226 state
->symbols
->get_type("gl_FogParameters"));
229 /* This function should only be called for ES, not desktop GL. */
231 generate_100ES_vs_variables(exec_list
*instructions
,
232 struct _mesa_glsl_parse_state
*state
)
234 for (unsigned i
= 0; i
< Elements(builtin_core_vs_variables
); i
++) {
235 add_builtin_variable(& builtin_core_vs_variables
[i
],
236 instructions
, state
->symbols
);
239 generate_100ES_uniforms(instructions
, state
);
241 generate_ARB_draw_buffers_variables(instructions
, state
, false,
247 generate_110_vs_variables(exec_list
*instructions
,
248 struct _mesa_glsl_parse_state
*state
)
250 for (unsigned i
= 0; i
< Elements(builtin_core_vs_variables
); i
++) {
251 add_builtin_variable(& builtin_core_vs_variables
[i
],
252 instructions
, state
->symbols
);
256 ; i
< Elements(builtin_110_deprecated_vs_variables
)
258 add_builtin_variable(& builtin_110_deprecated_vs_variables
[i
],
259 instructions
, state
->symbols
);
261 generate_110_uniforms(instructions
, state
);
263 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
265 * "As with all arrays, indices used to subscript gl_TexCoord must
266 * either be an integral constant expressions, or this array must be
267 * re-declared by the shader with a size. The size can be at most
268 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
269 * implementation in preserving varying resources."
271 const glsl_type
*const vec4_array_type
=
272 glsl_type::get_array_instance(glsl_type::vec4_type
, 0);
274 add_variable("gl_TexCoord", ir_var_out
, VERT_RESULT_TEX0
, vec4_array_type
,
275 instructions
, state
->symbols
);
277 generate_ARB_draw_buffers_variables(instructions
, state
, false,
283 generate_120_vs_variables(exec_list
*instructions
,
284 struct _mesa_glsl_parse_state
*state
)
286 /* GLSL version 1.20 did not add any built-in variables in the vertex
289 generate_110_vs_variables(instructions
, state
);
294 generate_130_vs_variables(exec_list
*instructions
,
295 struct _mesa_glsl_parse_state
*state
)
297 generate_120_vs_variables(instructions
, state
);
299 for (unsigned i
= 0; i
< Elements(builtin_130_vs_variables
); i
++) {
300 add_builtin_variable(& builtin_130_vs_variables
[i
],
301 instructions
, state
->symbols
);
304 const glsl_type
*const clip_distance_array_type
=
305 glsl_type::get_array_instance(glsl_type::float_type
,
306 state
->Const
.MaxClipPlanes
);
308 /* FINISHME: gl_ClipDistance needs a real location assigned. */
309 add_variable("gl_ClipDistance", ir_var_out
, -1, clip_distance_array_type
,
310 instructions
, state
->symbols
);
316 initialize_vs_variables(exec_list
*instructions
,
317 struct _mesa_glsl_parse_state
*state
)
320 switch (state
->language_version
) {
322 generate_100ES_vs_variables(instructions
, state
);
325 generate_110_vs_variables(instructions
, state
);
328 generate_120_vs_variables(instructions
, state
);
331 generate_130_vs_variables(instructions
, state
);
335 if (state
->ARB_draw_instanced_enable
)
336 generate_ARB_draw_instanced_variables(instructions
, state
, false,
341 /* This function should only be called for ES, not desktop GL. */
343 generate_100ES_fs_variables(exec_list
*instructions
,
344 struct _mesa_glsl_parse_state
*state
)
346 for (unsigned i
= 0; i
< Elements(builtin_core_fs_variables
); i
++) {
347 add_builtin_variable(& builtin_core_fs_variables
[i
],
348 instructions
, state
->symbols
);
351 for (unsigned i
= 0; i
< Elements(builtin_100ES_fs_variables
); i
++) {
352 add_builtin_variable(& builtin_100ES_fs_variables
[i
],
353 instructions
, state
->symbols
);
356 generate_100ES_uniforms(instructions
, state
);
358 generate_ARB_draw_buffers_variables(instructions
, state
, false,
363 generate_110_fs_variables(exec_list
*instructions
,
364 struct _mesa_glsl_parse_state
*state
)
366 for (unsigned i
= 0; i
< Elements(builtin_core_fs_variables
); i
++) {
367 add_builtin_variable(& builtin_core_fs_variables
[i
],
368 instructions
, state
->symbols
);
371 for (unsigned i
= 0; i
< Elements(builtin_110_fs_variables
); i
++) {
372 add_builtin_variable(& builtin_110_fs_variables
[i
],
373 instructions
, state
->symbols
);
377 ; i
< Elements(builtin_110_deprecated_fs_variables
)
379 add_builtin_variable(& builtin_110_deprecated_fs_variables
[i
],
380 instructions
, state
->symbols
);
382 generate_110_uniforms(instructions
, state
);
384 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
386 * "As with all arrays, indices used to subscript gl_TexCoord must
387 * either be an integral constant expressions, or this array must be
388 * re-declared by the shader with a size. The size can be at most
389 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
390 * implementation in preserving varying resources."
392 const glsl_type
*const vec4_array_type
=
393 glsl_type::get_array_instance(glsl_type::vec4_type
, 0);
395 add_variable("gl_TexCoord", ir_var_in
, FRAG_ATTRIB_TEX0
, vec4_array_type
,
396 instructions
, state
->symbols
);
398 generate_ARB_draw_buffers_variables(instructions
, state
, false,
404 generate_ARB_draw_buffers_variables(exec_list
*instructions
,
405 struct _mesa_glsl_parse_state
*state
,
406 bool warn
, _mesa_glsl_parser_targets target
)
408 /* gl_MaxDrawBuffers is available in all shader stages.
410 ir_variable
*const mdb
=
411 add_variable("gl_MaxDrawBuffers", ir_var_auto
, -1,
412 glsl_type::int_type
, instructions
, state
->symbols
);
415 mdb
->warn_extension
= "GL_ARB_draw_buffers";
417 mdb
->constant_value
= new(mdb
)
418 ir_constant(int(state
->Const
.MaxDrawBuffers
));
421 /* gl_FragData is only available in the fragment shader.
423 if (target
== fragment_shader
) {
424 const glsl_type
*const vec4_array_type
=
425 glsl_type::get_array_instance(glsl_type::vec4_type
,
426 state
->Const
.MaxDrawBuffers
);
428 ir_variable
*const fd
=
429 add_variable("gl_FragData", ir_var_out
, FRAG_RESULT_DATA0
,
430 vec4_array_type
, instructions
, state
->symbols
);
433 fd
->warn_extension
= "GL_ARB_draw_buffers";
439 generate_ARB_draw_instanced_variables(exec_list
*instructions
,
440 struct _mesa_glsl_parse_state
*state
,
442 _mesa_glsl_parser_targets target
)
444 /* gl_InstanceIDARB is only available in the vertex shader.
446 if (target
== vertex_shader
) {
447 ir_variable
*const inst
=
448 add_variable("gl_InstanceIDARB", ir_var_system_value
,
449 SYSTEM_VALUE_INSTANCE_ID
,
450 glsl_type::int_type
, instructions
, state
->symbols
);
453 inst
->warn_extension
= "GL_ARB_draw_instanced";
459 generate_ARB_shader_stencil_export_variables(exec_list
*instructions
,
460 struct _mesa_glsl_parse_state
*state
,
463 /* gl_FragStencilRefARB is only available in the fragment shader.
465 ir_variable
*const fd
=
466 add_variable("gl_FragStencilRefARB", ir_var_out
, FRAG_RESULT_STENCIL
,
467 glsl_type::int_type
, instructions
, state
->symbols
);
470 fd
->warn_extension
= "GL_ARB_shader_stencil_export";
474 generate_120_fs_variables(exec_list
*instructions
,
475 struct _mesa_glsl_parse_state
*state
)
477 generate_110_fs_variables(instructions
, state
);
480 ; i
< Elements(builtin_120_fs_variables
)
482 add_builtin_variable(& builtin_120_fs_variables
[i
],
483 instructions
, state
->symbols
);
488 generate_130_fs_variables(exec_list
*instructions
,
489 struct _mesa_glsl_parse_state
*state
)
491 generate_120_fs_variables(instructions
, state
);
493 const glsl_type
*const clip_distance_array_type
=
494 glsl_type::get_array_instance(glsl_type::float_type
,
495 state
->Const
.MaxClipPlanes
);
497 /* FINISHME: gl_ClipDistance needs a real location assigned. */
498 add_variable("gl_ClipDistance", ir_var_in
, -1, clip_distance_array_type
,
499 instructions
, state
->symbols
);
503 initialize_fs_variables(exec_list
*instructions
,
504 struct _mesa_glsl_parse_state
*state
)
507 switch (state
->language_version
) {
509 generate_100ES_fs_variables(instructions
, state
);
512 generate_110_fs_variables(instructions
, state
);
515 generate_120_fs_variables(instructions
, state
);
518 generate_130_fs_variables(instructions
, state
);
522 if (state
->ARB_shader_stencil_export_enable
)
523 generate_ARB_shader_stencil_export_variables(instructions
, state
,
524 state
->ARB_shader_stencil_export_warn
);
528 _mesa_glsl_initialize_variables(exec_list
*instructions
,
529 struct _mesa_glsl_parse_state
*state
)
531 switch (state
->target
) {
533 initialize_vs_variables(instructions
, state
);
535 case geometry_shader
:
537 case fragment_shader
:
538 initialize_fs_variables(instructions
, state
);