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
);
49 case ir_var_system_value
:
50 var
->read_only
= true;
61 var
->explicit_location
= (slot
>= 0);
63 /* Once the variable is created an initialized, add it to the symbol table
64 * and add the declaration to the IR stream.
66 instructions
->push_tail(var
);
68 symtab
->add_variable(var
->name
, var
);
73 add_uniform(exec_list
*instructions
,
74 struct _mesa_glsl_parse_state
*state
,
75 const char *name
, const glsl_type
*type
)
77 return add_variable(name
, ir_var_uniform
, -1, type
, instructions
,
82 add_builtin_variable(const builtin_variable
*proto
, exec_list
*instructions
,
83 glsl_symbol_table
*symtab
)
85 /* Create a new variable declaration from the description supplied by
88 const glsl_type
*const type
= symtab
->get_type(proto
->type
);
92 add_variable(proto
->name
, proto
->mode
, proto
->slot
, type
, instructions
,
97 add_builtin_constant(exec_list
*instructions
,
98 struct _mesa_glsl_parse_state
*state
,
99 const char *name
, int value
)
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
);
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.
111 generate_100ES_uniforms(exec_list
*instructions
,
112 struct _mesa_glsl_parse_state
*state
)
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
);
129 add_uniform(instructions
, state
, "gl_DepthRange",
130 state
->symbols
->get_type("gl_DepthRangeParameters"));
134 generate_110_uniforms(exec_list
*instructions
,
135 struct _mesa_glsl_parse_state
*state
)
138 ; i
< Elements(builtin_110_deprecated_uniforms
)
140 add_builtin_variable(& builtin_110_deprecated_uniforms
[i
],
141 instructions
, state
->symbols
);
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
);
167 const glsl_type
*const mat4_array_type
=
168 glsl_type::get_array_instance(glsl_type::mat4_type
,
169 state
->Const
.MaxTextureCoords
);
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
);
176 add_uniform(instructions
, state
, "gl_DepthRange",
177 state
->symbols
->get_type("gl_DepthRangeParameters"));
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"));
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
);
190 const glsl_type
*const light_source_array_type
=
191 glsl_type::get_array_instance(state
->symbols
->get_type("gl_LightSourceParameters"), state
->Const
.MaxLights
);
193 add_uniform(instructions
, state
, "gl_LightSource", light_source_array_type
);
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
);
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
);
208 add_uniform(instructions
, state
, "gl_TextureEnvColor",
209 glsl_type::get_array_instance(glsl_type::vec4_type
,
210 state
->Const
.MaxTextureUnits
));
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
);
224 add_uniform(instructions
, state
, "gl_Fog",
225 state
->symbols
->get_type("gl_FogParameters"));
228 /* This function should only be called for ES, not desktop GL. */
230 generate_100ES_vs_variables(exec_list
*instructions
,
231 struct _mesa_glsl_parse_state
*state
)
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
);
238 generate_100ES_uniforms(instructions
, state
);
240 generate_ARB_draw_buffers_variables(instructions
, state
, false,
246 generate_110_vs_variables(exec_list
*instructions
,
247 struct _mesa_glsl_parse_state
*state
)
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
);
255 ; i
< Elements(builtin_110_deprecated_vs_variables
)
257 add_builtin_variable(& builtin_110_deprecated_vs_variables
[i
],
258 instructions
, state
->symbols
);
260 generate_110_uniforms(instructions
, state
);
262 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
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."
270 const glsl_type
*const vec4_array_type
=
271 glsl_type::get_array_instance(glsl_type::vec4_type
, 0);
273 add_variable("gl_TexCoord", ir_var_out
, VERT_RESULT_TEX0
, vec4_array_type
,
274 instructions
, state
->symbols
);
276 generate_ARB_draw_buffers_variables(instructions
, state
, false,
282 generate_120_vs_variables(exec_list
*instructions
,
283 struct _mesa_glsl_parse_state
*state
)
285 /* GLSL version 1.20 did not add any built-in variables in the vertex
288 generate_110_vs_variables(instructions
, state
);
293 generate_130_vs_variables(exec_list
*instructions
,
294 struct _mesa_glsl_parse_state
*state
)
296 generate_120_vs_variables(instructions
, state
);
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
);
303 const glsl_type
*const clip_distance_array_type
=
304 glsl_type::get_array_instance(glsl_type::float_type
,
305 state
->Const
.MaxClipPlanes
);
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
);
315 initialize_vs_variables(exec_list
*instructions
,
316 struct _mesa_glsl_parse_state
*state
)
319 switch (state
->language_version
) {
321 generate_100ES_vs_variables(instructions
, state
);
324 generate_110_vs_variables(instructions
, state
);
327 generate_120_vs_variables(instructions
, state
);
330 generate_130_vs_variables(instructions
, state
);
334 if (state
->ARB_draw_instanced_enable
)
335 generate_ARB_draw_instanced_variables(instructions
, state
, false,
340 /* This function should only be called for ES, not desktop GL. */
342 generate_100ES_fs_variables(exec_list
*instructions
,
343 struct _mesa_glsl_parse_state
*state
)
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
);
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
);
355 generate_100ES_uniforms(instructions
, state
);
357 generate_ARB_draw_buffers_variables(instructions
, state
, false,
362 generate_110_fs_variables(exec_list
*instructions
,
363 struct _mesa_glsl_parse_state
*state
)
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
);
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
);
376 ; i
< Elements(builtin_110_deprecated_fs_variables
)
378 add_builtin_variable(& builtin_110_deprecated_fs_variables
[i
],
379 instructions
, state
->symbols
);
381 generate_110_uniforms(instructions
, state
);
383 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
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."
391 const glsl_type
*const vec4_array_type
=
392 glsl_type::get_array_instance(glsl_type::vec4_type
, 0);
394 add_variable("gl_TexCoord", ir_var_in
, FRAG_ATTRIB_TEX0
, vec4_array_type
,
395 instructions
, state
->symbols
);
397 generate_ARB_draw_buffers_variables(instructions
, state
, false,
403 generate_ARB_draw_buffers_variables(exec_list
*instructions
,
404 struct _mesa_glsl_parse_state
*state
,
405 bool warn
, _mesa_glsl_parser_targets target
)
407 /* gl_MaxDrawBuffers is available in all shader stages.
409 ir_variable
*const mdb
=
410 add_variable("gl_MaxDrawBuffers", ir_var_auto
, -1,
411 glsl_type::int_type
, instructions
, state
->symbols
);
414 mdb
->warn_extension
= "GL_ARB_draw_buffers";
416 mdb
->constant_value
= new(mdb
)
417 ir_constant(int(state
->Const
.MaxDrawBuffers
));
420 /* gl_FragData is only available in the fragment shader.
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
);
427 ir_variable
*const fd
=
428 add_variable("gl_FragData", ir_var_out
, FRAG_RESULT_DATA0
,
429 vec4_array_type
, instructions
, state
->symbols
);
432 fd
->warn_extension
= "GL_ARB_draw_buffers";
438 generate_ARB_draw_instanced_variables(exec_list
*instructions
,
439 struct _mesa_glsl_parse_state
*state
,
441 _mesa_glsl_parser_targets target
)
443 /* gl_InstanceIDARB is only available in the vertex shader.
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
);
452 inst
->warn_extension
= "GL_ARB_draw_instanced";
458 generate_ARB_shader_stencil_export_variables(exec_list
*instructions
,
459 struct _mesa_glsl_parse_state
*state
,
462 /* gl_FragStencilRefARB is only available in the fragment shader.
464 ir_variable
*const fd
=
465 add_variable("gl_FragStencilRefARB", ir_var_out
, FRAG_RESULT_STENCIL
,
466 glsl_type::int_type
, instructions
, state
->symbols
);
469 fd
->warn_extension
= "GL_ARB_shader_stencil_export";
473 generate_120_fs_variables(exec_list
*instructions
,
474 struct _mesa_glsl_parse_state
*state
)
476 generate_110_fs_variables(instructions
, state
);
479 ; i
< Elements(builtin_120_fs_variables
)
481 add_builtin_variable(& builtin_120_fs_variables
[i
],
482 instructions
, state
->symbols
);
487 generate_130_fs_variables(exec_list
*instructions
,
488 struct _mesa_glsl_parse_state
*state
)
490 generate_120_fs_variables(instructions
, state
);
492 const glsl_type
*const clip_distance_array_type
=
493 glsl_type::get_array_instance(glsl_type::float_type
,
494 state
->Const
.MaxClipPlanes
);
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
);
502 initialize_fs_variables(exec_list
*instructions
,
503 struct _mesa_glsl_parse_state
*state
)
506 switch (state
->language_version
) {
508 generate_100ES_fs_variables(instructions
, state
);
511 generate_110_fs_variables(instructions
, state
);
514 generate_120_fs_variables(instructions
, state
);
517 generate_130_fs_variables(instructions
, state
);
521 if (state
->ARB_shader_stencil_export_enable
)
522 generate_ARB_shader_stencil_export_variables(instructions
, state
,
523 state
->ARB_shader_stencil_export_warn
);
527 _mesa_glsl_initialize_variables(exec_list
*instructions
,
528 struct _mesa_glsl_parse_state
*state
)
530 switch (state
->target
) {
532 initialize_vs_variables(instructions
, state
);
534 case geometry_shader
:
536 case fragment_shader
:
537 initialize_fs_variables(instructions
, state
);