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"
28 #include "builtin_variables.h"
31 #define Elements(x) (sizeof(x)/sizeof(*(x)))
35 add_variable(const char *name
, enum ir_variable_mode mode
,
36 const glsl_type
*type
, exec_list
*instructions
,
37 glsl_symbol_table
*symtab
)
39 ir_variable
*var
= new ir_variable(type
, name
);
44 var
->shader_in
= true;
45 var
->read_only
= true;
48 var
->shader_in
= true;
49 var
->shader_out
= true;
52 var
->shader_out
= true;
55 var
->shader_in
= true;
56 var
->read_only
= true;
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
);
74 add_builtin_variable(const builtin_variable
*proto
, exec_list
*instructions
,
75 glsl_symbol_table
*symtab
)
77 /* Create a new variable declaration from the description supplied by
80 const glsl_type
*const type
= symtab
->get_type(proto
->type
);
84 add_variable(proto
->name
, proto
->mode
, type
, instructions
, symtab
);
89 generate_110_uniforms(exec_list
*instructions
,
90 glsl_symbol_table
*symtab
)
93 ; i
< Elements(builtin_110_deprecated_uniforms
)
95 add_builtin_variable(& builtin_110_deprecated_uniforms
[i
],
96 instructions
, symtab
);
99 /* FINISHME: The size of this array is implementation dependent based on the
100 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
101 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
104 const glsl_type
*const mat4_array_type
=
105 glsl_type::get_array_instance(glsl_type::mat4_type
, 4);
107 add_variable("gl_TextureMatrix", ir_var_uniform
, mat4_array_type
,
108 instructions
, symtab
);
110 /* FINISHME: Add support for gl_DepthRangeParameters */
111 /* FINISHME: Add support for gl_ClipPlane[] */
112 /* FINISHME: Add support for gl_PointParameters */
114 /* FINISHME: Add support for gl_MaterialParameters
115 * FINISHME: (glFrontMaterial, glBackMaterial)
118 /* FINISHME: The size of this array is implementation dependent based on the
119 * FINISHME: value of GL_MAX_TEXTURE_LIGHTS. GL_MAX_TEXTURE_LIGHTS must be
120 * FINISHME: at least 8, so hard-code 8 for now.
122 const glsl_type
*const light_source_array_type
=
123 glsl_type::get_array_instance(symtab
->get_type("gl_LightSourceParameters"), 8);
125 add_variable("gl_LightSource", ir_var_uniform
, light_source_array_type
,
126 instructions
, symtab
);
128 /* FINISHME: Add support for gl_LightModel */
129 /* FINISHME: Add support for gl_FrontLightProduct[], gl_BackLightProduct[] */
130 /* FINISHME: Add support for gl_TextureEnvColor[] */
131 /* FINISHME: Add support for gl_ObjectPlane*[], gl_EyePlane*[] */
132 /* FINISHME: Add support for gl_Fog */
136 generate_110_vs_variables(exec_list
*instructions
,
137 glsl_symbol_table
*symtab
)
139 for (unsigned i
= 0; i
< Elements(builtin_core_vs_variables
); i
++) {
140 add_builtin_variable(& builtin_core_vs_variables
[i
],
141 instructions
, symtab
);
145 ; i
< Elements(builtin_110_deprecated_vs_variables
)
147 add_builtin_variable(& builtin_110_deprecated_vs_variables
[i
],
148 instructions
, symtab
);
150 generate_110_uniforms(instructions
, symtab
);
152 /* FINISHME: The size of this array is implementation dependent based on the
153 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
154 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
157 const glsl_type
*const vec4_array_type
=
158 glsl_type::get_array_instance(glsl_type::vec4_type
, 4);
160 add_variable("gl_TexCoord", ir_var_out
, vec4_array_type
, instructions
,
166 generate_120_vs_variables(exec_list
*instructions
,
167 glsl_symbol_table
*symtab
)
169 /* GLSL version 1.20 did not add any built-in variables in the vertex
172 generate_110_vs_variables(instructions
, symtab
);
177 generate_130_vs_variables(exec_list
*instructions
,
178 glsl_symbol_table
*symtab
)
180 generate_120_vs_variables(instructions
, symtab
);
182 for (unsigned i
= 0; i
< Elements(builtin_130_vs_variables
); i
++) {
183 add_builtin_variable(& builtin_130_vs_variables
[i
],
184 instructions
, symtab
);
187 /* FINISHME: The size of this array is implementation dependent based on
188 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
190 const glsl_type
*const clip_distance_array_type
=
191 glsl_type::get_array_instance(glsl_type::float_type
, 8);
192 add_variable("gl_ClipDistance", ir_var_out
, clip_distance_array_type
,
193 instructions
, symtab
);
199 initialize_vs_variables(exec_list
*instructions
,
200 struct _mesa_glsl_parse_state
*state
)
203 switch (state
->language_version
) {
205 generate_110_vs_variables(instructions
, state
->symbols
);
208 generate_120_vs_variables(instructions
, state
->symbols
);
211 generate_130_vs_variables(instructions
, state
->symbols
);
217 generate_110_fs_variables(exec_list
*instructions
,
218 glsl_symbol_table
*symtab
)
220 for (unsigned i
= 0; i
< Elements(builtin_core_fs_variables
); i
++) {
221 add_builtin_variable(& builtin_core_fs_variables
[i
],
222 instructions
, symtab
);
226 ; i
< Elements(builtin_110_deprecated_fs_variables
)
228 add_builtin_variable(& builtin_110_deprecated_fs_variables
[i
],
229 instructions
, symtab
);
231 generate_110_uniforms(instructions
, symtab
);
233 /* FINISHME: The size of this array is implementation dependent based on the
234 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
235 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
238 const glsl_type
*const vec4_array_type
=
239 glsl_type::get_array_instance(glsl_type::vec4_type
, 4);
241 add_variable("gl_TexCoord", ir_var_in
, vec4_array_type
, instructions
,
247 generate_ARB_draw_buffers_fs_variables(exec_list
*instructions
,
248 glsl_symbol_table
*symtab
, bool warn
)
250 /* FINISHME: The size of this array is implementation dependent based on the
251 * FINISHME: value of GL_MAX_DRAW_BUFFERS. GL_MAX_DRAW_BUFFERS must be
252 * FINISHME: at least 1, so hard-code 1 for now.
254 const glsl_type
*const vec4_array_type
=
255 glsl_type::get_array_instance(glsl_type::vec4_type
, 1);
257 ir_variable
*const fd
=
258 add_variable("gl_FragData", ir_var_out
, vec4_array_type
, instructions
,
262 fd
->warn_extension
= "GL_ARB_draw_buffers";
267 generate_120_fs_variables(exec_list
*instructions
,
268 glsl_symbol_table
*symtab
)
270 generate_110_fs_variables(instructions
, symtab
);
271 generate_ARB_draw_buffers_fs_variables(instructions
, symtab
, false);
275 generate_130_fs_variables(exec_list
*instructions
,
276 glsl_symbol_table
*symtab
)
278 generate_120_fs_variables(instructions
, symtab
);
280 /* FINISHME: The size of this array is implementation dependent based on
281 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
283 const glsl_type
*const clip_distance_array_type
=
284 glsl_type::get_array_instance(glsl_type::float_type
, 8);
285 add_variable("gl_ClipDistance", ir_var_in
, clip_distance_array_type
,
286 instructions
, symtab
);
290 initialize_fs_variables(exec_list
*instructions
,
291 struct _mesa_glsl_parse_state
*state
)
294 switch (state
->language_version
) {
296 generate_110_fs_variables(instructions
, state
->symbols
);
299 generate_120_fs_variables(instructions
, state
->symbols
);
302 generate_130_fs_variables(instructions
, state
->symbols
);
307 /* Since GL_ARB_draw_buffers is included in GLSL 1.20 and later, we
308 * can basically ignore any extension settings for it.
310 if (state
->language_version
< 120) {
311 if (state
->ARB_draw_buffers_enable
) {
312 generate_ARB_draw_buffers_fs_variables(instructions
, state
->symbols
,
313 state
->ARB_draw_buffers_warn
);
319 _mesa_glsl_initialize_variables(exec_list
*instructions
,
320 struct _mesa_glsl_parse_state
*state
)
322 switch (state
->target
) {
324 initialize_vs_variables(instructions
, state
);
326 case geometry_shader
:
328 case fragment_shader
:
329 initialize_fs_variables(instructions
, state
);
332 fprintf(stderr
, "ir reader has no builtin variables");