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"
30 #define Elements(x) (sizeof(x)/sizeof(*(x)))
33 static void generate_ARB_draw_buffers_variables(exec_list
*,
34 struct _mesa_glsl_parse_state
*,
35 bool, _mesa_glsl_parser_targets
);
38 add_variable(const char *name
, enum ir_variable_mode mode
, int slot
,
39 const glsl_type
*type
, exec_list
*instructions
,
40 glsl_symbol_table
*symtab
)
42 ir_variable
*var
= new(symtab
) ir_variable(type
, name
, mode
);
48 var
->read_only
= true;
60 /* Once the variable is created an initialized, add it to the symbol table
61 * and add the declaration to the IR stream.
63 instructions
->push_tail(var
);
65 symtab
->add_variable(var
->name
, var
);
70 add_uniform(exec_list
*instructions
,
71 struct _mesa_glsl_parse_state
*state
,
72 const char *name
, const glsl_type
*type
)
74 return add_variable(name
, ir_var_uniform
, -1, type
, instructions
,
79 add_builtin_variable(const builtin_variable
*proto
, exec_list
*instructions
,
80 glsl_symbol_table
*symtab
)
82 /* Create a new variable declaration from the description supplied by
85 const glsl_type
*const type
= symtab
->get_type(proto
->type
);
89 add_variable(proto
->name
, proto
->mode
, proto
->slot
, type
, instructions
,
94 add_builtin_constant(exec_list
*instructions
,
95 struct _mesa_glsl_parse_state
*state
,
96 const char *name
, int value
)
98 ir_variable
*const var
= add_variable(name
, ir_var_auto
,
99 -1, glsl_type::int_type
,
100 instructions
, state
->symbols
);
101 var
->constant_value
= new(var
) ir_constant(value
);
105 generate_110_uniforms(exec_list
*instructions
,
106 struct _mesa_glsl_parse_state
*state
)
109 ; i
< Elements(builtin_110_deprecated_uniforms
)
111 add_builtin_variable(& builtin_110_deprecated_uniforms
[i
],
112 instructions
, state
->symbols
);
115 add_builtin_constant(instructions
, state
, "gl_MaxLights",
116 state
->Const
.MaxLights
);
117 add_builtin_constant(instructions
, state
, "gl_MaxClipPlanes",
118 state
->Const
.MaxClipPlanes
);
119 add_builtin_constant(instructions
, state
, "gl_MaxTextureUnits",
120 state
->Const
.MaxTextureUnits
);
121 add_builtin_constant(instructions
, state
, "gl_MaxTextureCoords",
122 state
->Const
.MaxTextureCoords
);
123 add_builtin_constant(instructions
, state
, "gl_MaxVertexAttribs",
124 state
->Const
.MaxVertexAttribs
);
125 add_builtin_constant(instructions
, state
, "gl_MaxVertexUniformComponents",
126 state
->Const
.MaxVertexUniformComponents
);
127 add_builtin_constant(instructions
, state
, "gl_MaxVaryingFloats",
128 state
->Const
.MaxVaryingFloats
);
129 add_builtin_constant(instructions
, state
, "gl_MaxVertexTextureImageUnits",
130 state
->Const
.MaxVertexTextureImageUnits
);
131 add_builtin_constant(instructions
, state
, "gl_MaxCombinedTextureImageUnits",
132 state
->Const
.MaxCombinedTextureImageUnits
);
133 add_builtin_constant(instructions
, state
, "gl_MaxTextureImageUnits",
134 state
->Const
.MaxTextureImageUnits
);
135 add_builtin_constant(instructions
, state
, "gl_MaxFragmentUniformComponents",
136 state
->Const
.MaxFragmentUniformComponents
);
138 const glsl_type
*const mat4_array_type
=
139 glsl_type::get_array_instance(glsl_type::mat4_type
,
140 state
->Const
.MaxTextureCoords
);
142 add_uniform(instructions
, state
, "gl_TextureMatrix", mat4_array_type
);
144 add_uniform(instructions
, state
, "gl_DepthRange",
145 state
->symbols
->get_type("gl_DepthRangeParameters"));
147 add_uniform(instructions
, state
, "gl_ClipPlane",
148 glsl_type::get_array_instance(glsl_type::vec4_type
,
149 state
->Const
.MaxClipPlanes
));
150 add_uniform(instructions
, state
, "gl_Point",
151 state
->symbols
->get_type("gl_PointParameters"));
153 const glsl_type
*const material_parameters_type
=
154 state
->symbols
->get_type("gl_MaterialParameters");
155 add_uniform(instructions
, state
, "gl_FrontMaterial", material_parameters_type
);
156 add_uniform(instructions
, state
, "gl_BackMaterial", material_parameters_type
);
158 const glsl_type
*const light_source_array_type
=
159 glsl_type::get_array_instance(state
->symbols
->get_type("gl_LightSourceParameters"), state
->Const
.MaxLights
);
161 add_uniform(instructions
, state
, "gl_LightSource", light_source_array_type
);
163 const glsl_type
*const light_model_products_type
=
164 state
->symbols
->get_type("gl_LightModelProducts");
165 add_uniform(instructions
, state
, "gl_FrontLightModelProduct",
166 light_model_products_type
);
167 add_uniform(instructions
, state
, "gl_BackLightModelProduct",
168 light_model_products_type
);
170 const glsl_type
*const light_products_type
=
171 glsl_type::get_array_instance(state
->symbols
->get_type("gl_LightProducts"),
172 state
->Const
.MaxLights
);
173 add_uniform(instructions
, state
, "gl_FrontLightProduct", light_products_type
);
174 add_uniform(instructions
, state
, "gl_BackLightProduct", light_products_type
);
176 add_uniform(instructions
, state
, "gl_TextureEnvColor",
177 glsl_type::get_array_instance(glsl_type::vec4_type
,
178 state
->Const
.MaxTextureUnits
));
180 const glsl_type
*const texcoords_vec4
=
181 glsl_type::get_array_instance(glsl_type::vec4_type
,
182 state
->Const
.MaxTextureCoords
);
183 add_uniform(instructions
, state
, "gl_EyePlaneS", texcoords_vec4
);
184 add_uniform(instructions
, state
, "gl_EyePlaneT", texcoords_vec4
);
185 add_uniform(instructions
, state
, "gl_EyePlaneR", texcoords_vec4
);
186 add_uniform(instructions
, state
, "gl_EyePlaneQ", texcoords_vec4
);
187 add_uniform(instructions
, state
, "gl_ObjectPlaneS", texcoords_vec4
);
188 add_uniform(instructions
, state
, "gl_ObjectPlaneT", texcoords_vec4
);
189 add_uniform(instructions
, state
, "gl_ObjectPlaneR", texcoords_vec4
);
190 add_uniform(instructions
, state
, "gl_ObjectPlaneQ", texcoords_vec4
);
192 add_uniform(instructions
, state
, "gl_Fog",
193 state
->symbols
->get_type("gl_FogParameters"));
197 generate_110_vs_variables(exec_list
*instructions
,
198 struct _mesa_glsl_parse_state
*state
)
200 for (unsigned i
= 0; i
< Elements(builtin_core_vs_variables
); i
++) {
201 add_builtin_variable(& builtin_core_vs_variables
[i
],
202 instructions
, state
->symbols
);
206 ; i
< Elements(builtin_110_deprecated_vs_variables
)
208 add_builtin_variable(& builtin_110_deprecated_vs_variables
[i
],
209 instructions
, state
->symbols
);
211 generate_110_uniforms(instructions
, state
);
213 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
215 * "As with all arrays, indices used to subscript gl_TexCoord must
216 * either be an integral constant expressions, or this array must be
217 * re-declared by the shader with a size. The size can be at most
218 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
219 * implementation in preserving varying resources."
221 const glsl_type
*const vec4_array_type
=
222 glsl_type::get_array_instance(glsl_type::vec4_type
, 0);
224 add_variable("gl_TexCoord", ir_var_out
, VERT_RESULT_TEX0
, vec4_array_type
,
225 instructions
, state
->symbols
);
227 generate_ARB_draw_buffers_variables(instructions
, state
, false,
233 generate_120_vs_variables(exec_list
*instructions
,
234 struct _mesa_glsl_parse_state
*state
)
236 /* GLSL version 1.20 did not add any built-in variables in the vertex
239 generate_110_vs_variables(instructions
, state
);
244 generate_130_vs_variables(exec_list
*instructions
,
245 struct _mesa_glsl_parse_state
*state
)
247 generate_120_vs_variables(instructions
, state
);
249 for (unsigned i
= 0; i
< Elements(builtin_130_vs_variables
); i
++) {
250 add_builtin_variable(& builtin_130_vs_variables
[i
],
251 instructions
, state
->symbols
);
254 const glsl_type
*const clip_distance_array_type
=
255 glsl_type::get_array_instance(glsl_type::float_type
,
256 state
->Const
.MaxClipPlanes
);
258 /* FINISHME: gl_ClipDistance needs a real location assigned. */
259 add_variable("gl_ClipDistance", ir_var_out
, -1, clip_distance_array_type
,
260 instructions
, state
->symbols
);
266 initialize_vs_variables(exec_list
*instructions
,
267 struct _mesa_glsl_parse_state
*state
)
270 switch (state
->language_version
) {
272 generate_110_vs_variables(instructions
, state
);
275 generate_120_vs_variables(instructions
, state
);
278 generate_130_vs_variables(instructions
, state
);
284 generate_110_fs_variables(exec_list
*instructions
,
285 struct _mesa_glsl_parse_state
*state
)
287 for (unsigned i
= 0; i
< Elements(builtin_core_fs_variables
); i
++) {
288 add_builtin_variable(& builtin_core_fs_variables
[i
],
289 instructions
, state
->symbols
);
293 ; i
< Elements(builtin_110_deprecated_fs_variables
)
295 add_builtin_variable(& builtin_110_deprecated_fs_variables
[i
],
296 instructions
, state
->symbols
);
298 generate_110_uniforms(instructions
, state
);
300 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
302 * "As with all arrays, indices used to subscript gl_TexCoord must
303 * either be an integral constant expressions, or this array must be
304 * re-declared by the shader with a size. The size can be at most
305 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
306 * implementation in preserving varying resources."
308 const glsl_type
*const vec4_array_type
=
309 glsl_type::get_array_instance(glsl_type::vec4_type
, 0);
311 add_variable("gl_TexCoord", ir_var_in
, FRAG_ATTRIB_TEX0
, vec4_array_type
,
312 instructions
, state
->symbols
);
314 generate_ARB_draw_buffers_variables(instructions
, state
, false,
320 generate_ARB_draw_buffers_variables(exec_list
*instructions
,
321 struct _mesa_glsl_parse_state
*state
,
322 bool warn
, _mesa_glsl_parser_targets target
)
324 /* gl_MaxDrawBuffers is available in all shader stages.
326 ir_variable
*const mdb
=
327 add_variable("gl_MaxDrawBuffers", ir_var_auto
, -1,
328 glsl_type::int_type
, instructions
, state
->symbols
);
331 mdb
->warn_extension
= "GL_ARB_draw_buffers";
333 mdb
->constant_value
= new(mdb
)
334 ir_constant(int(state
->Const
.MaxDrawBuffers
));
337 /* gl_FragData is only available in the fragment shader.
339 if (target
== fragment_shader
) {
340 const glsl_type
*const vec4_array_type
=
341 glsl_type::get_array_instance(glsl_type::vec4_type
,
342 state
->Const
.MaxDrawBuffers
);
344 ir_variable
*const fd
=
345 add_variable("gl_FragData", ir_var_out
, FRAG_RESULT_DATA0
,
346 vec4_array_type
, instructions
, state
->symbols
);
349 fd
->warn_extension
= "GL_ARB_draw_buffers";
355 generate_120_fs_variables(exec_list
*instructions
,
356 struct _mesa_glsl_parse_state
*state
)
358 generate_110_fs_variables(instructions
, state
);
361 ; i
< Elements(builtin_120_fs_variables
)
363 add_builtin_variable(& builtin_120_fs_variables
[i
],
364 instructions
, state
->symbols
);
369 generate_130_fs_variables(exec_list
*instructions
,
370 struct _mesa_glsl_parse_state
*state
)
372 generate_120_fs_variables(instructions
, state
);
374 const glsl_type
*const clip_distance_array_type
=
375 glsl_type::get_array_instance(glsl_type::float_type
,
376 state
->Const
.MaxClipPlanes
);
378 /* FINISHME: gl_ClipDistance needs a real location assigned. */
379 add_variable("gl_ClipDistance", ir_var_in
, -1, clip_distance_array_type
,
380 instructions
, state
->symbols
);
384 initialize_fs_variables(exec_list
*instructions
,
385 struct _mesa_glsl_parse_state
*state
)
388 switch (state
->language_version
) {
390 generate_110_fs_variables(instructions
, state
);
393 generate_120_fs_variables(instructions
, state
);
396 generate_130_fs_variables(instructions
, state
);
402 _mesa_glsl_initialize_variables(exec_list
*instructions
,
403 struct _mesa_glsl_parse_state
*state
)
405 switch (state
->target
) {
407 initialize_vs_variables(instructions
, state
);
409 case geometry_shader
:
411 case fragment_shader
:
412 initialize_fs_variables(instructions
, state
);
415 fprintf(stderr
, "ir reader has no builtin variables");