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
);
46 var
->read_only
= true;
49 var
->shader_in
= true;
50 var
->read_only
= true;
53 var
->shader_in
= true;
54 var
->shader_out
= true;
57 var
->shader_out
= true;
60 var
->shader_in
= true;
61 var
->read_only
= true;
70 /* Once the variable is created an initialized, add it to the symbol table
71 * and add the declaration to the IR stream.
73 instructions
->push_tail(var
);
75 symtab
->add_variable(var
->name
, var
);
80 add_uniform(exec_list
*instructions
,
81 struct _mesa_glsl_parse_state
*state
,
82 const char *name
, const glsl_type
*type
)
84 return add_variable(name
, ir_var_uniform
, -1, type
, instructions
,
89 add_builtin_variable(const builtin_variable
*proto
, exec_list
*instructions
,
90 glsl_symbol_table
*symtab
)
92 /* Create a new variable declaration from the description supplied by
95 const glsl_type
*const type
= symtab
->get_type(proto
->type
);
99 add_variable(proto
->name
, proto
->mode
, proto
->slot
, type
, instructions
,
104 add_builtin_constant(exec_list
*instructions
,
105 struct _mesa_glsl_parse_state
*state
,
106 const char *name
, int value
)
108 ir_variable
*const var
= add_variable(name
, ir_var_auto
,
109 -1, glsl_type::int_type
,
110 instructions
, state
->symbols
);
111 var
->constant_value
= new(var
) ir_constant(value
);
115 generate_110_uniforms(exec_list
*instructions
,
116 struct _mesa_glsl_parse_state
*state
)
119 ; i
< Elements(builtin_110_deprecated_uniforms
)
121 add_builtin_variable(& builtin_110_deprecated_uniforms
[i
],
122 instructions
, state
->symbols
);
125 add_builtin_constant(instructions
, state
, "gl_MaxLights",
126 state
->Const
.MaxLights
);
127 add_builtin_constant(instructions
, state
, "gl_MaxClipPlanes",
128 state
->Const
.MaxClipPlanes
);
129 add_builtin_constant(instructions
, state
, "gl_MaxTextureUnits",
130 state
->Const
.MaxTextureUnits
);
131 add_builtin_constant(instructions
, state
, "gl_MaxTextureCoords",
132 state
->Const
.MaxTextureCoords
);
133 add_builtin_constant(instructions
, state
, "gl_MaxVertexAttribs",
134 state
->Const
.MaxVertexAttribs
);
135 add_builtin_constant(instructions
, state
, "gl_MaxVertexUniformComponents",
136 state
->Const
.MaxVertexUniformComponents
);
137 add_builtin_constant(instructions
, state
, "gl_MaxVaryingFloats",
138 state
->Const
.MaxVaryingFloats
);
139 add_builtin_constant(instructions
, state
, "gl_MaxVertexTextureImageUnits",
140 state
->Const
.MaxVertexTextureImageUnits
);
141 add_builtin_constant(instructions
, state
, "gl_MaxCombinedTextureImageUnits",
142 state
->Const
.MaxCombinedTextureImageUnits
);
143 add_builtin_constant(instructions
, state
, "gl_MaxTextureImageUnits",
144 state
->Const
.MaxTextureImageUnits
);
145 add_builtin_constant(instructions
, state
, "gl_MaxFragmentUniformComponents",
146 state
->Const
.MaxFragmentUniformComponents
);
148 const glsl_type
*const mat4_array_type
=
149 glsl_type::get_array_instance(glsl_type::mat4_type
,
150 state
->Const
.MaxTextureCoords
);
152 add_uniform(instructions
, state
, "gl_TextureMatrix", mat4_array_type
);
154 add_uniform(instructions
, state
, "gl_DepthRangeParameters",
155 state
->symbols
->get_type("gl_DepthRangeParameters"));
157 add_uniform(instructions
, state
, "gl_ClipPlane",
158 glsl_type::get_array_instance(glsl_type::vec4_type
,
159 state
->Const
.MaxClipPlanes
));
160 add_uniform(instructions
, state
, "gl_Point",
161 state
->symbols
->get_type("gl_PointParameters"));
163 const glsl_type
*const material_parameters_type
=
164 state
->symbols
->get_type("gl_MaterialParameters");
165 add_uniform(instructions
, state
, "gl_FrontMaterial", material_parameters_type
);
166 add_uniform(instructions
, state
, "gl_BackMaterial", material_parameters_type
);
168 const glsl_type
*const light_source_array_type
=
169 glsl_type::get_array_instance(state
->symbols
->get_type("gl_LightSourceParameters"), state
->Const
.MaxLights
);
171 add_uniform(instructions
, state
, "gl_LightSource", light_source_array_type
);
173 const glsl_type
*const light_model_products_type
=
174 state
->symbols
->get_type("gl_LightModelProducts");
175 add_uniform(instructions
, state
, "gl_FrontLightModelProduct",
176 light_model_products_type
);
177 add_uniform(instructions
, state
, "gl_BackLightModelProduct",
178 light_model_products_type
);
180 const glsl_type
*const light_products_type
=
181 glsl_type::get_array_instance(state
->symbols
->get_type("gl_LightProducts"),
182 state
->Const
.MaxLights
);
183 add_uniform(instructions
, state
, "gl_FrontLightProduct", light_products_type
);
184 add_uniform(instructions
, state
, "gl_BackLightProduct", light_products_type
);
186 add_uniform(instructions
, state
, "gl_TextureEnvColor",
187 glsl_type::get_array_instance(glsl_type::vec4_type
,
188 state
->Const
.MaxTextureUnits
));
190 const glsl_type
*const texcoords_vec4
=
191 glsl_type::get_array_instance(glsl_type::vec4_type
,
192 state
->Const
.MaxTextureCoords
);
193 add_uniform(instructions
, state
, "gl_EyePlaneS", texcoords_vec4
);
194 add_uniform(instructions
, state
, "gl_EyePlaneT", texcoords_vec4
);
195 add_uniform(instructions
, state
, "gl_EyePlaneR", texcoords_vec4
);
196 add_uniform(instructions
, state
, "gl_EyePlaneQ", texcoords_vec4
);
197 add_uniform(instructions
, state
, "gl_ObjectPlaneS", texcoords_vec4
);
198 add_uniform(instructions
, state
, "gl_ObjectPlaneT", texcoords_vec4
);
199 add_uniform(instructions
, state
, "gl_ObjectPlaneR", texcoords_vec4
);
200 add_uniform(instructions
, state
, "gl_ObjectPlaneQ", texcoords_vec4
);
202 add_uniform(instructions
, state
, "gl_Fog",
203 state
->symbols
->get_type("gl_FogParameters"));
207 generate_110_vs_variables(exec_list
*instructions
,
208 struct _mesa_glsl_parse_state
*state
)
210 for (unsigned i
= 0; i
< Elements(builtin_core_vs_variables
); i
++) {
211 add_builtin_variable(& builtin_core_vs_variables
[i
],
212 instructions
, state
->symbols
);
216 ; i
< Elements(builtin_110_deprecated_vs_variables
)
218 add_builtin_variable(& builtin_110_deprecated_vs_variables
[i
],
219 instructions
, state
->symbols
);
221 generate_110_uniforms(instructions
, state
);
223 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
225 * "As with all arrays, indices used to subscript gl_TexCoord must
226 * either be an integral constant expressions, or this array must be
227 * re-declared by the shader with a size. The size can be at most
228 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
229 * implementation in preserving varying resources."
231 const glsl_type
*const vec4_array_type
=
232 glsl_type::get_array_instance(glsl_type::vec4_type
, 0);
234 add_variable("gl_TexCoord", ir_var_out
, VERT_RESULT_TEX0
, vec4_array_type
,
235 instructions
, state
->symbols
);
237 generate_ARB_draw_buffers_variables(instructions
, state
, false,
243 generate_120_vs_variables(exec_list
*instructions
,
244 struct _mesa_glsl_parse_state
*state
)
246 /* GLSL version 1.20 did not add any built-in variables in the vertex
249 generate_110_vs_variables(instructions
, state
);
254 generate_130_vs_variables(exec_list
*instructions
,
255 struct _mesa_glsl_parse_state
*state
)
257 generate_120_vs_variables(instructions
, state
);
259 for (unsigned i
= 0; i
< Elements(builtin_130_vs_variables
); i
++) {
260 add_builtin_variable(& builtin_130_vs_variables
[i
],
261 instructions
, state
->symbols
);
264 const glsl_type
*const clip_distance_array_type
=
265 glsl_type::get_array_instance(glsl_type::float_type
,
266 state
->Const
.MaxClipPlanes
);
268 /* FINISHME: gl_ClipDistance needs a real location assigned. */
269 add_variable("gl_ClipDistance", ir_var_out
, -1, clip_distance_array_type
,
270 instructions
, state
->symbols
);
276 initialize_vs_variables(exec_list
*instructions
,
277 struct _mesa_glsl_parse_state
*state
)
280 switch (state
->language_version
) {
282 generate_110_vs_variables(instructions
, state
);
285 generate_120_vs_variables(instructions
, state
);
288 generate_130_vs_variables(instructions
, state
);
294 generate_110_fs_variables(exec_list
*instructions
,
295 struct _mesa_glsl_parse_state
*state
)
297 for (unsigned i
= 0; i
< Elements(builtin_core_fs_variables
); i
++) {
298 add_builtin_variable(& builtin_core_fs_variables
[i
],
299 instructions
, state
->symbols
);
303 ; i
< Elements(builtin_110_deprecated_fs_variables
)
305 add_builtin_variable(& builtin_110_deprecated_fs_variables
[i
],
306 instructions
, state
->symbols
);
308 generate_110_uniforms(instructions
, state
);
310 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
312 * "As with all arrays, indices used to subscript gl_TexCoord must
313 * either be an integral constant expressions, or this array must be
314 * re-declared by the shader with a size. The size can be at most
315 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
316 * implementation in preserving varying resources."
318 const glsl_type
*const vec4_array_type
=
319 glsl_type::get_array_instance(glsl_type::vec4_type
, 0);
321 add_variable("gl_TexCoord", ir_var_in
, FRAG_ATTRIB_TEX0
, vec4_array_type
,
322 instructions
, state
->symbols
);
324 generate_ARB_draw_buffers_variables(instructions
, state
, false,
330 generate_ARB_draw_buffers_variables(exec_list
*instructions
,
331 struct _mesa_glsl_parse_state
*state
,
332 bool warn
, _mesa_glsl_parser_targets target
)
334 /* gl_MaxDrawBuffers is available in all shader stages.
336 ir_variable
*const mdb
=
337 add_variable("gl_MaxDrawBuffers", ir_var_auto
, -1,
338 glsl_type::int_type
, instructions
, state
->symbols
);
341 mdb
->warn_extension
= "GL_ARB_draw_buffers";
343 mdb
->constant_value
= new(mdb
)
344 ir_constant(int(state
->Const
.MaxDrawBuffers
));
347 /* gl_FragData is only available in the fragment shader.
349 if (target
== fragment_shader
) {
350 const glsl_type
*const vec4_array_type
=
351 glsl_type::get_array_instance(glsl_type::vec4_type
,
352 state
->Const
.MaxDrawBuffers
);
354 ir_variable
*const fd
=
355 add_variable("gl_FragData", ir_var_out
, FRAG_RESULT_DATA0
,
356 vec4_array_type
, instructions
, state
->symbols
);
359 fd
->warn_extension
= "GL_ARB_draw_buffers";
365 generate_120_fs_variables(exec_list
*instructions
,
366 struct _mesa_glsl_parse_state
*state
)
368 generate_110_fs_variables(instructions
, state
);
371 ; i
< Elements(builtin_120_fs_variables
)
373 add_builtin_variable(& builtin_120_fs_variables
[i
],
374 instructions
, state
->symbols
);
379 generate_130_fs_variables(exec_list
*instructions
,
380 struct _mesa_glsl_parse_state
*state
)
382 generate_120_fs_variables(instructions
, state
);
384 const glsl_type
*const clip_distance_array_type
=
385 glsl_type::get_array_instance(glsl_type::float_type
,
386 state
->Const
.MaxClipPlanes
);
388 /* FINISHME: gl_ClipDistance needs a real location assigned. */
389 add_variable("gl_ClipDistance", ir_var_in
, -1, clip_distance_array_type
,
390 instructions
, state
->symbols
);
394 initialize_fs_variables(exec_list
*instructions
,
395 struct _mesa_glsl_parse_state
*state
)
398 switch (state
->language_version
) {
400 generate_110_fs_variables(instructions
, state
);
403 generate_120_fs_variables(instructions
, state
);
406 generate_130_fs_variables(instructions
, state
);
412 _mesa_glsl_initialize_variables(exec_list
*instructions
,
413 struct _mesa_glsl_parse_state
*state
)
415 switch (state
->target
) {
417 initialize_vs_variables(instructions
, state
);
419 case geometry_shader
:
421 case fragment_shader
:
422 initialize_fs_variables(instructions
, state
);
425 fprintf(stderr
, "ir reader has no builtin variables");