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)))
34 add_variable(const char *name
, enum ir_variable_mode mode
,
35 const glsl_type
*type
, exec_list
*instructions
,
36 glsl_symbol_table
*symtab
)
38 ir_variable
*var
= new ir_variable(type
, name
);
43 var
->shader_in
= true;
44 var
->read_only
= true;
47 var
->shader_in
= true;
48 var
->shader_out
= true;
51 var
->shader_out
= true;
54 var
->shader_in
= true;
55 var
->read_only
= true;
62 /* Once the variable is created an initialized, add it to the symbol table
63 * and add the declaration to the IR stream.
65 instructions
->push_tail(var
);
67 symtab
->add_variable(var
->name
, var
);
73 add_builtin_variable(const builtin_variable
*proto
, exec_list
*instructions
,
74 glsl_symbol_table
*symtab
)
76 /* Create a new variable declaration from the description supplied by
79 const glsl_type
*const type
= symtab
->get_type(proto
->type
);
83 add_variable(proto
->name
, proto
->mode
, type
, instructions
, symtab
);
88 generate_110_uniforms(exec_list
*instructions
,
89 glsl_symbol_table
*symtab
)
92 ; i
< Elements(builtin_110_deprecated_uniforms
)
94 add_builtin_variable(& builtin_110_deprecated_uniforms
[i
],
95 instructions
, symtab
);
98 /* FINISHME: The size of this array is implementation dependent based on the
99 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
100 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
103 const glsl_type
*const mat4_array_type
=
104 glsl_type::get_array_instance(glsl_type::mat4_type
, 4);
106 add_variable("gl_TextureMatrix", ir_var_uniform
, mat4_array_type
,
107 instructions
, symtab
);
109 /* FINISHME: Add support for gl_DepthRangeParameters */
110 /* FINISHME: Add support for gl_ClipPlane[] */
111 /* FINISHME: Add support for gl_PointParameters */
113 /* FINISHME: Add support for gl_MaterialParameters
114 * FINISHME: (glFrontMaterial, glBackMaterial)
117 /* FINISHME: The size of this array is implementation dependent based on the
118 * FINISHME: value of GL_MAX_TEXTURE_LIGHTS. GL_MAX_TEXTURE_LIGHTS must be
119 * FINISHME: at least 8, so hard-code 8 for now.
121 const glsl_type
*const light_source_array_type
=
122 glsl_type::get_array_instance(symtab
->get_type("gl_LightSourceParameters"), 8);
124 add_variable("gl_LightSource", ir_var_uniform
, light_source_array_type
,
125 instructions
, symtab
);
127 /* FINISHME: Add support for gl_LightModel */
128 /* FINISHME: Add support for gl_FrontLightProduct[], gl_BackLightProduct[] */
129 /* FINISHME: Add support for gl_TextureEnvColor[] */
130 /* FINISHME: Add support for gl_ObjectPlane*[], gl_EyePlane*[] */
131 /* FINISHME: Add support for gl_Fog */
135 generate_110_vs_variables(exec_list
*instructions
,
136 glsl_symbol_table
*symtab
)
138 for (unsigned i
= 0; i
< Elements(builtin_core_vs_variables
); i
++) {
139 add_builtin_variable(& builtin_core_vs_variables
[i
],
140 instructions
, symtab
);
144 ; i
< Elements(builtin_110_deprecated_vs_variables
)
146 add_builtin_variable(& builtin_110_deprecated_vs_variables
[i
],
147 instructions
, symtab
);
149 generate_110_uniforms(instructions
, symtab
);
151 /* FINISHME: The size of this array is implementation dependent based on the
152 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
153 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
156 const glsl_type
*const vec4_array_type
=
157 glsl_type::get_array_instance(glsl_type::vec4_type
, 4);
159 add_variable("gl_TexCoord", ir_var_out
, vec4_array_type
, instructions
,
165 generate_120_vs_variables(exec_list
*instructions
,
166 glsl_symbol_table
*symtab
)
168 /* GLSL version 1.20 did not add any built-in variables in the vertex
171 generate_110_vs_variables(instructions
, symtab
);
176 generate_130_vs_variables(exec_list
*instructions
,
177 glsl_symbol_table
*symtab
)
179 generate_120_vs_variables(instructions
, symtab
);
181 for (unsigned i
= 0; i
< Elements(builtin_130_vs_variables
); i
++) {
182 add_builtin_variable(& builtin_130_vs_variables
[i
],
183 instructions
, symtab
);
186 /* FINISHME: The size of this array is implementation dependent based on
187 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
189 const glsl_type
*const clip_distance_array_type
=
190 glsl_type::get_array_instance(glsl_type::float_type
, 8);
191 add_variable("gl_ClipDistance", ir_var_out
, clip_distance_array_type
,
192 instructions
, symtab
);
198 initialize_vs_variables(exec_list
*instructions
,
199 struct _mesa_glsl_parse_state
*state
)
202 switch (state
->language_version
) {
204 generate_110_vs_variables(instructions
, state
->symbols
);
207 generate_120_vs_variables(instructions
, state
->symbols
);
210 generate_130_vs_variables(instructions
, state
->symbols
);
216 generate_110_fs_variables(exec_list
*instructions
,
217 glsl_symbol_table
*symtab
)
219 for (unsigned i
= 0; i
< Elements(builtin_core_fs_variables
); i
++) {
220 add_builtin_variable(& builtin_core_fs_variables
[i
],
221 instructions
, symtab
);
225 ; i
< Elements(builtin_110_deprecated_fs_variables
)
227 add_builtin_variable(& builtin_110_deprecated_fs_variables
[i
],
228 instructions
, symtab
);
230 generate_110_uniforms(instructions
, symtab
);
232 /* FINISHME: The size of this array is implementation dependent based on the
233 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
234 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
237 const glsl_type
*const vec4_array_type
=
238 glsl_type::get_array_instance(glsl_type::vec4_type
, 4);
240 add_variable("gl_TexCoord", ir_var_in
, vec4_array_type
, instructions
,
246 generate_ARB_draw_buffers_fs_variables(exec_list
*instructions
,
247 glsl_symbol_table
*symtab
, bool warn
)
249 /* FINISHME: The size of this array is implementation dependent based on the
250 * FINISHME: value of GL_MAX_DRAW_BUFFERS. GL_MAX_DRAW_BUFFERS must be
251 * FINISHME: at least 1, so hard-code 1 for now.
253 const glsl_type
*const vec4_array_type
=
254 glsl_type::get_array_instance(glsl_type::vec4_type
, 1);
256 ir_variable
*const fd
=
257 add_variable("gl_FragData", ir_var_out
, vec4_array_type
, instructions
,
261 fd
->warn_extension
= "GL_ARB_draw_buffers";
266 generate_120_fs_variables(exec_list
*instructions
,
267 glsl_symbol_table
*symtab
)
269 generate_110_fs_variables(instructions
, symtab
);
270 generate_ARB_draw_buffers_fs_variables(instructions
, symtab
, false);
274 generate_130_fs_variables(exec_list
*instructions
,
275 glsl_symbol_table
*symtab
)
277 generate_120_fs_variables(instructions
, symtab
);
279 /* FINISHME: The size of this array is implementation dependent based on
280 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
282 const glsl_type
*const clip_distance_array_type
=
283 glsl_type::get_array_instance(glsl_type::float_type
, 8);
284 add_variable("gl_ClipDistance", ir_var_in
, clip_distance_array_type
,
285 instructions
, symtab
);
289 initialize_fs_variables(exec_list
*instructions
,
290 struct _mesa_glsl_parse_state
*state
)
293 switch (state
->language_version
) {
295 generate_110_fs_variables(instructions
, state
->symbols
);
298 generate_120_fs_variables(instructions
, state
->symbols
);
301 generate_130_fs_variables(instructions
, state
->symbols
);
306 /* Since GL_ARB_draw_buffers is included in GLSL 1.20 and later, we
307 * can basically ignore any extension settings for it.
309 if (state
->language_version
< 120) {
310 if (state
->ARB_draw_buffers_enable
) {
311 generate_ARB_draw_buffers_fs_variables(instructions
, state
->symbols
,
312 state
->ARB_draw_buffers_warn
);
318 _mesa_glsl_initialize_variables(exec_list
*instructions
,
319 struct _mesa_glsl_parse_state
*state
)
321 switch (state
->target
) {
323 initialize_vs_variables(instructions
, state
);
325 case geometry_shader
:
327 case fragment_shader
:
328 initialize_fs_variables(instructions
, state
);
331 fprintf(stderr
, "ir reader has no builtin variables");