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.
24 #include "glsl_parser_extras.h"
25 #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
);
41 if (var
->mode
!= ir_var_out
)
42 var
->read_only
= true;
45 /* Once the variable is created an initialized, add it to the symbol table
46 * and add the declaration to the IR stream.
48 instructions
->push_tail(var
);
50 symtab
->add_variable(var
->name
, var
);
55 add_builtin_variable(const builtin_variable
*proto
, exec_list
*instructions
,
56 glsl_symbol_table
*symtab
)
58 /* Create a new variable declaration from the description supplied by
61 const glsl_type
*const type
= symtab
->get_type(proto
->type
);
65 add_variable(proto
->name
, proto
->mode
, type
, instructions
, symtab
);
70 generate_110_uniforms(exec_list
*instructions
,
71 glsl_symbol_table
*symtab
)
74 ; i
< Elements(builtin_110_deprecated_uniforms
)
76 add_builtin_variable(& builtin_110_deprecated_uniforms
[i
],
77 instructions
, symtab
);
80 /* FINISHME: Add support for gl_TextureMatrix[]. The size of this array is
81 * FINISHME: implementation dependent based on the value of
82 * FINISHME: GL_MAX_TEXTURE_COORDS.
85 /* FINISHME: Add support for gl_DepthRangeParameters */
86 /* FINISHME: Add support for gl_ClipPlane[] */
87 /* FINISHME: Add support for gl_PointParameters */
89 /* FINISHME: Add support for gl_MaterialParameters
90 * FINISHME: (glFrontMaterial, glBackMaterial)
93 /* FINISHME: Add support for gl_LightSource[] */
94 /* FINISHME: Add support for gl_LightModel */
95 /* FINISHME: Add support for gl_FrontLightProduct[], gl_BackLightProduct[] */
96 /* FINISHME: Add support for gl_TextureEnvColor[] */
97 /* FINISHME: Add support for gl_ObjectPlane*[], gl_EyePlane*[] */
98 /* FINISHME: Add support for gl_Fog */
102 generate_110_vs_variables(exec_list
*instructions
,
103 glsl_symbol_table
*symtab
)
105 for (unsigned i
= 0; i
< Elements(builtin_core_vs_variables
); i
++) {
106 add_builtin_variable(& builtin_core_vs_variables
[i
],
107 instructions
, symtab
);
111 ; i
< Elements(builtin_110_deprecated_vs_variables
)
113 add_builtin_variable(& builtin_110_deprecated_vs_variables
[i
],
114 instructions
, symtab
);
116 generate_110_uniforms(instructions
, symtab
);
118 /* FINISHME: The size of this array is implementation dependent based on the
119 * FINISHME: value of GL_MAX_TEXTURE_COORDS. GL_MAX_TEXTURE_COORDS must be
120 * FINISHME: at least 2, so hard-code 2 for now.
122 const glsl_type
*const vec4_type
=
123 glsl_type::get_instance(GLSL_TYPE_FLOAT
, 4, 0);
124 const glsl_type
*const vec4_array_type
=
125 glsl_type::get_array_instance(vec4_type
, 2);
127 add_variable("gl_TexCoord", ir_var_out
, vec4_array_type
, instructions
,
133 generate_120_vs_variables(exec_list
*instructions
,
134 glsl_symbol_table
*symtab
)
136 /* GLSL version 1.20 did not add any built-in variables in the vertex
139 generate_110_vs_variables(instructions
, symtab
);
144 generate_130_vs_variables(exec_list
*instructions
,
145 glsl_symbol_table
*symtab
)
147 generate_120_vs_variables(instructions
, symtab
);
149 for (unsigned i
= 0; i
< Elements(builtin_130_vs_variables
); i
++) {
150 add_builtin_variable(& builtin_130_vs_variables
[i
],
151 instructions
, symtab
);
154 /* FINISHME: The size of this array is implementation dependent based on
155 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
157 const glsl_type
*const clip_distance_array_type
=
158 glsl_type::get_array_instance(glsl_type::float_type
, 8);
159 add_variable("gl_ClipDistance", ir_var_out
, clip_distance_array_type
,
160 instructions
, symtab
);
166 initialize_vs_variables(exec_list
*instructions
,
167 struct _mesa_glsl_parse_state
*state
)
170 switch (state
->language_version
) {
172 generate_110_vs_variables(instructions
, state
->symbols
);
175 generate_120_vs_variables(instructions
, state
->symbols
);
178 generate_130_vs_variables(instructions
, state
->symbols
);
184 generate_110_fs_variables(exec_list
*instructions
,
185 glsl_symbol_table
*symtab
)
187 for (unsigned i
= 0; i
< Elements(builtin_core_fs_variables
); i
++) {
188 add_builtin_variable(& builtin_core_fs_variables
[i
],
189 instructions
, symtab
);
193 ; i
< Elements(builtin_110_deprecated_fs_variables
)
195 add_builtin_variable(& builtin_110_deprecated_fs_variables
[i
],
196 instructions
, symtab
);
198 generate_110_uniforms(instructions
, symtab
);
200 /* FINISHME: Add support for gl_FragData[GL_MAX_DRAW_BUFFERS]. */
202 /* FINISHME: The size of this array is implementation dependent based on the
203 * FINISHME: value of GL_MAX_TEXTURE_COORDS. GL_MAX_TEXTURE_COORDS must be
204 * FINISHME: at least 2, so hard-code 2 for now.
206 const glsl_type
*const vec4_type
=
207 glsl_type::get_instance(GLSL_TYPE_FLOAT
, 4, 0);
208 const glsl_type
*const vec4_array_type
=
209 glsl_type::get_array_instance(vec4_type
, 2);
211 add_variable("gl_TexCoord", ir_var_in
, vec4_array_type
, instructions
,
216 generate_120_fs_variables(exec_list
*instructions
,
217 glsl_symbol_table
*symtab
)
219 /* GLSL version 1.20 did not add any built-in variables in the fragment
222 generate_110_fs_variables(instructions
, symtab
);
226 generate_130_fs_variables(exec_list
*instructions
,
227 glsl_symbol_table
*symtab
)
229 generate_120_fs_variables(instructions
, symtab
);
231 /* FINISHME: Add support fo gl_ClipDistance. The size of this array is
232 * FINISHME: implementation dependent based on the value of
233 * FINISHME: GL_MAX_CLIP_DISTANCES.
238 initialize_fs_variables(exec_list
*instructions
,
239 struct _mesa_glsl_parse_state
*state
)
242 switch (state
->language_version
) {
244 generate_110_fs_variables(instructions
, state
->symbols
);
247 generate_120_fs_variables(instructions
, state
->symbols
);
250 generate_130_fs_variables(instructions
, state
->symbols
);
256 _mesa_glsl_initialize_variables(exec_list
*instructions
,
257 struct _mesa_glsl_parse_state
*state
)
259 switch (state
->target
) {
261 initialize_vs_variables(instructions
, state
);
263 case geometry_shader
:
265 case fragment_shader
:
266 initialize_fs_variables(instructions
, state
);