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
, int slot
,
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;
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
->name
, var
);
75 add_builtin_variable(const builtin_variable
*proto
, exec_list
*instructions
,
76 glsl_symbol_table
*symtab
)
78 /* Create a new variable declaration from the description supplied by
81 const glsl_type
*const type
= symtab
->get_type(proto
->type
);
85 add_variable(proto
->name
, proto
->mode
, proto
->slot
, type
, instructions
,
91 generate_110_uniforms(exec_list
*instructions
,
92 glsl_symbol_table
*symtab
)
95 ; i
< Elements(builtin_110_deprecated_uniforms
)
97 add_builtin_variable(& builtin_110_deprecated_uniforms
[i
],
98 instructions
, symtab
);
101 /* FINISHME: The size of this array is implementation dependent based on the
102 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
103 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
106 const glsl_type
*const mat4_array_type
=
107 glsl_type::get_array_instance(glsl_type::mat4_type
, 4);
109 add_variable("gl_TextureMatrix", ir_var_uniform
, -1, mat4_array_type
,
110 instructions
, symtab
);
112 /* FINISHME: Add support for gl_DepthRangeParameters */
113 /* FINISHME: Add support for gl_ClipPlane[] */
114 /* FINISHME: Add support for gl_PointParameters */
116 /* FINISHME: Add support for gl_MaterialParameters
117 * FINISHME: (glFrontMaterial, glBackMaterial)
120 /* FINISHME: The size of this array is implementation dependent based on the
121 * FINISHME: value of GL_MAX_TEXTURE_LIGHTS. GL_MAX_TEXTURE_LIGHTS must be
122 * FINISHME: at least 8, so hard-code 8 for now.
124 const glsl_type
*const light_source_array_type
=
125 glsl_type::get_array_instance(symtab
->get_type("gl_LightSourceParameters"), 8);
127 add_variable("gl_LightSource", ir_var_uniform
, -1, light_source_array_type
,
128 instructions
, symtab
);
130 /* FINISHME: Add support for gl_LightModel */
131 /* FINISHME: Add support for gl_FrontLightProduct[], gl_BackLightProduct[] */
132 /* FINISHME: Add support for gl_TextureEnvColor[] */
133 /* FINISHME: Add support for gl_ObjectPlane*[], gl_EyePlane*[] */
134 /* FINISHME: Add support for gl_Fog */
138 generate_110_vs_variables(exec_list
*instructions
,
139 glsl_symbol_table
*symtab
)
141 for (unsigned i
= 0; i
< Elements(builtin_core_vs_variables
); i
++) {
142 add_builtin_variable(& builtin_core_vs_variables
[i
],
143 instructions
, symtab
);
147 ; i
< Elements(builtin_110_deprecated_vs_variables
)
149 add_builtin_variable(& builtin_110_deprecated_vs_variables
[i
],
150 instructions
, symtab
);
152 generate_110_uniforms(instructions
, symtab
);
154 /* FINISHME: The size of this array is implementation dependent based on the
155 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
156 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
159 const glsl_type
*const vec4_array_type
=
160 glsl_type::get_array_instance(glsl_type::vec4_type
, 4);
162 add_variable("gl_TexCoord", ir_var_out
, VERT_RESULT_TEX0
, vec4_array_type
,
163 instructions
, symtab
);
168 generate_120_vs_variables(exec_list
*instructions
,
169 glsl_symbol_table
*symtab
)
171 /* GLSL version 1.20 did not add any built-in variables in the vertex
174 generate_110_vs_variables(instructions
, symtab
);
179 generate_130_vs_variables(exec_list
*instructions
,
180 glsl_symbol_table
*symtab
)
182 generate_120_vs_variables(instructions
, symtab
);
184 for (unsigned i
= 0; i
< Elements(builtin_130_vs_variables
); i
++) {
185 add_builtin_variable(& builtin_130_vs_variables
[i
],
186 instructions
, symtab
);
189 /* FINISHME: The size of this array is implementation dependent based on
190 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
192 const glsl_type
*const clip_distance_array_type
=
193 glsl_type::get_array_instance(glsl_type::float_type
, 8);
195 /* FINISHME: gl_ClipDistance needs a real location assigned. */
196 add_variable("gl_ClipDistance", ir_var_out
, -1, clip_distance_array_type
,
197 instructions
, symtab
);
203 initialize_vs_variables(exec_list
*instructions
,
204 struct _mesa_glsl_parse_state
*state
)
207 switch (state
->language_version
) {
209 generate_110_vs_variables(instructions
, state
->symbols
);
212 generate_120_vs_variables(instructions
, state
->symbols
);
215 generate_130_vs_variables(instructions
, state
->symbols
);
221 generate_110_fs_variables(exec_list
*instructions
,
222 glsl_symbol_table
*symtab
)
224 for (unsigned i
= 0; i
< Elements(builtin_core_fs_variables
); i
++) {
225 add_builtin_variable(& builtin_core_fs_variables
[i
],
226 instructions
, symtab
);
230 ; i
< Elements(builtin_110_deprecated_fs_variables
)
232 add_builtin_variable(& builtin_110_deprecated_fs_variables
[i
],
233 instructions
, symtab
);
235 generate_110_uniforms(instructions
, symtab
);
237 /* FINISHME: The size of this array is implementation dependent based on the
238 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
239 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
242 const glsl_type
*const vec4_array_type
=
243 glsl_type::get_array_instance(glsl_type::vec4_type
, 4);
245 add_variable("gl_TexCoord", ir_var_in
, FRAG_ATTRIB_TEX0
, vec4_array_type
,
246 instructions
, symtab
);
251 generate_ARB_draw_buffers_fs_variables(exec_list
*instructions
,
252 glsl_symbol_table
*symtab
, bool warn
)
254 /* FINISHME: The size of this array is implementation dependent based on the
255 * FINISHME: value of GL_MAX_DRAW_BUFFERS. GL_MAX_DRAW_BUFFERS must be
256 * FINISHME: at least 1, so hard-code 1 for now.
258 const glsl_type
*const vec4_array_type
=
259 glsl_type::get_array_instance(glsl_type::vec4_type
, 1);
261 ir_variable
*const fd
=
262 add_variable("gl_FragData", ir_var_out
, FRAG_RESULT_DATA0
,
263 vec4_array_type
, instructions
, symtab
);
266 fd
->warn_extension
= "GL_ARB_draw_buffers";
271 generate_120_fs_variables(exec_list
*instructions
,
272 glsl_symbol_table
*symtab
)
274 generate_110_fs_variables(instructions
, symtab
);
275 generate_ARB_draw_buffers_fs_variables(instructions
, symtab
, false);
279 generate_130_fs_variables(exec_list
*instructions
,
280 glsl_symbol_table
*symtab
)
282 generate_120_fs_variables(instructions
, symtab
);
284 /* FINISHME: The size of this array is implementation dependent based on
285 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
287 const glsl_type
*const clip_distance_array_type
=
288 glsl_type::get_array_instance(glsl_type::float_type
, 8);
290 /* FINISHME: gl_ClipDistance needs a real location assigned. */
291 add_variable("gl_ClipDistance", ir_var_in
, -1, clip_distance_array_type
,
292 instructions
, symtab
);
296 initialize_fs_variables(exec_list
*instructions
,
297 struct _mesa_glsl_parse_state
*state
)
300 switch (state
->language_version
) {
302 generate_110_fs_variables(instructions
, state
->symbols
);
305 generate_120_fs_variables(instructions
, state
->symbols
);
308 generate_130_fs_variables(instructions
, state
->symbols
);
313 /* Since GL_ARB_draw_buffers is included in GLSL 1.20 and later, we
314 * can basically ignore any extension settings for it.
316 if (state
->language_version
< 120) {
317 if (state
->ARB_draw_buffers_enable
) {
318 generate_ARB_draw_buffers_fs_variables(instructions
, state
->symbols
,
319 state
->ARB_draw_buffers_warn
);
325 _mesa_glsl_initialize_variables(exec_list
*instructions
,
326 struct _mesa_glsl_parse_state
*state
)
328 switch (state
->target
) {
330 initialize_vs_variables(instructions
, state
);
332 case geometry_shader
:
334 case fragment_shader
:
335 initialize_fs_variables(instructions
, state
);
338 fprintf(stderr
, "ir reader has no builtin variables");