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"
29 static void generate_ARB_draw_buffers_variables(exec_list
*,
30 struct _mesa_glsl_parse_state
*,
31 bool, _mesa_glsl_parser_targets
);
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(symtab
) ir_variable(type
, name
, mode
);
44 var
->read_only
= true;
56 /* Once the variable is created an initialized, add it to the symbol table
57 * and add the declaration to the IR stream.
59 instructions
->push_tail(var
);
61 symtab
->add_variable(var
->name
, var
);
66 add_uniform(exec_list
*instructions
,
67 struct _mesa_glsl_parse_state
*state
,
68 const char *name
, const glsl_type
*type
)
70 return add_variable(name
, ir_var_uniform
, -1, type
, instructions
,
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
,
90 add_builtin_constant(exec_list
*instructions
,
91 struct _mesa_glsl_parse_state
*state
,
92 const char *name
, int value
)
94 ir_variable
*const var
= add_variable(name
, ir_var_auto
,
95 -1, glsl_type::int_type
,
96 instructions
, state
->symbols
);
97 var
->constant_value
= new(var
) ir_constant(value
);
101 generate_110_uniforms(exec_list
*instructions
,
102 struct _mesa_glsl_parse_state
*state
)
105 ; i
< Elements(builtin_110_deprecated_uniforms
)
107 add_builtin_variable(& builtin_110_deprecated_uniforms
[i
],
108 instructions
, state
->symbols
);
111 add_builtin_constant(instructions
, state
, "gl_MaxLights",
112 state
->Const
.MaxLights
);
113 add_builtin_constant(instructions
, state
, "gl_MaxClipPlanes",
114 state
->Const
.MaxClipPlanes
);
115 add_builtin_constant(instructions
, state
, "gl_MaxTextureUnits",
116 state
->Const
.MaxTextureUnits
);
117 add_builtin_constant(instructions
, state
, "gl_MaxTextureCoords",
118 state
->Const
.MaxTextureCoords
);
119 add_builtin_constant(instructions
, state
, "gl_MaxVertexAttribs",
120 state
->Const
.MaxVertexAttribs
);
121 add_builtin_constant(instructions
, state
, "gl_MaxVertexUniformComponents",
122 state
->Const
.MaxVertexUniformComponents
);
123 add_builtin_constant(instructions
, state
, "gl_MaxVaryingFloats",
124 state
->Const
.MaxVaryingFloats
);
125 add_builtin_constant(instructions
, state
, "gl_MaxVertexTextureImageUnits",
126 state
->Const
.MaxVertexTextureImageUnits
);
127 add_builtin_constant(instructions
, state
, "gl_MaxCombinedTextureImageUnits",
128 state
->Const
.MaxCombinedTextureImageUnits
);
129 add_builtin_constant(instructions
, state
, "gl_MaxTextureImageUnits",
130 state
->Const
.MaxTextureImageUnits
);
131 add_builtin_constant(instructions
, state
, "gl_MaxFragmentUniformComponents",
132 state
->Const
.MaxFragmentUniformComponents
);
134 const glsl_type
*const mat4_array_type
=
135 glsl_type::get_array_instance(glsl_type::mat4_type
,
136 state
->Const
.MaxTextureCoords
);
138 add_uniform(instructions
, state
, "gl_TextureMatrix", mat4_array_type
);
140 add_uniform(instructions
, state
, "gl_DepthRange",
141 state
->symbols
->get_type("gl_DepthRangeParameters"));
143 add_uniform(instructions
, state
, "gl_ClipPlane",
144 glsl_type::get_array_instance(glsl_type::vec4_type
,
145 state
->Const
.MaxClipPlanes
));
146 add_uniform(instructions
, state
, "gl_Point",
147 state
->symbols
->get_type("gl_PointParameters"));
149 const glsl_type
*const material_parameters_type
=
150 state
->symbols
->get_type("gl_MaterialParameters");
151 add_uniform(instructions
, state
, "gl_FrontMaterial", material_parameters_type
);
152 add_uniform(instructions
, state
, "gl_BackMaterial", material_parameters_type
);
154 const glsl_type
*const light_source_array_type
=
155 glsl_type::get_array_instance(state
->symbols
->get_type("gl_LightSourceParameters"), state
->Const
.MaxLights
);
157 add_uniform(instructions
, state
, "gl_LightSource", light_source_array_type
);
159 const glsl_type
*const light_model_products_type
=
160 state
->symbols
->get_type("gl_LightModelProducts");
161 add_uniform(instructions
, state
, "gl_FrontLightModelProduct",
162 light_model_products_type
);
163 add_uniform(instructions
, state
, "gl_BackLightModelProduct",
164 light_model_products_type
);
166 const glsl_type
*const light_products_type
=
167 glsl_type::get_array_instance(state
->symbols
->get_type("gl_LightProducts"),
168 state
->Const
.MaxLights
);
169 add_uniform(instructions
, state
, "gl_FrontLightProduct", light_products_type
);
170 add_uniform(instructions
, state
, "gl_BackLightProduct", light_products_type
);
172 add_uniform(instructions
, state
, "gl_TextureEnvColor",
173 glsl_type::get_array_instance(glsl_type::vec4_type
,
174 state
->Const
.MaxTextureUnits
));
176 const glsl_type
*const texcoords_vec4
=
177 glsl_type::get_array_instance(glsl_type::vec4_type
,
178 state
->Const
.MaxTextureCoords
);
179 add_uniform(instructions
, state
, "gl_EyePlaneS", texcoords_vec4
);
180 add_uniform(instructions
, state
, "gl_EyePlaneT", texcoords_vec4
);
181 add_uniform(instructions
, state
, "gl_EyePlaneR", texcoords_vec4
);
182 add_uniform(instructions
, state
, "gl_EyePlaneQ", texcoords_vec4
);
183 add_uniform(instructions
, state
, "gl_ObjectPlaneS", texcoords_vec4
);
184 add_uniform(instructions
, state
, "gl_ObjectPlaneT", texcoords_vec4
);
185 add_uniform(instructions
, state
, "gl_ObjectPlaneR", texcoords_vec4
);
186 add_uniform(instructions
, state
, "gl_ObjectPlaneQ", texcoords_vec4
);
188 add_uniform(instructions
, state
, "gl_Fog",
189 state
->symbols
->get_type("gl_FogParameters"));
193 generate_110_vs_variables(exec_list
*instructions
,
194 struct _mesa_glsl_parse_state
*state
)
196 for (unsigned i
= 0; i
< Elements(builtin_core_vs_variables
); i
++) {
197 add_builtin_variable(& builtin_core_vs_variables
[i
],
198 instructions
, state
->symbols
);
202 ; i
< Elements(builtin_110_deprecated_vs_variables
)
204 add_builtin_variable(& builtin_110_deprecated_vs_variables
[i
],
205 instructions
, state
->symbols
);
207 generate_110_uniforms(instructions
, state
);
209 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
211 * "As with all arrays, indices used to subscript gl_TexCoord must
212 * either be an integral constant expressions, or this array must be
213 * re-declared by the shader with a size. The size can be at most
214 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
215 * implementation in preserving varying resources."
217 const glsl_type
*const vec4_array_type
=
218 glsl_type::get_array_instance(glsl_type::vec4_type
, 0);
220 add_variable("gl_TexCoord", ir_var_out
, VERT_RESULT_TEX0
, vec4_array_type
,
221 instructions
, state
->symbols
);
223 generate_ARB_draw_buffers_variables(instructions
, state
, false,
229 generate_120_vs_variables(exec_list
*instructions
,
230 struct _mesa_glsl_parse_state
*state
)
232 /* GLSL version 1.20 did not add any built-in variables in the vertex
235 generate_110_vs_variables(instructions
, state
);
240 generate_130_vs_variables(exec_list
*instructions
,
241 struct _mesa_glsl_parse_state
*state
)
243 generate_120_vs_variables(instructions
, state
);
245 for (unsigned i
= 0; i
< Elements(builtin_130_vs_variables
); i
++) {
246 add_builtin_variable(& builtin_130_vs_variables
[i
],
247 instructions
, state
->symbols
);
250 const glsl_type
*const clip_distance_array_type
=
251 glsl_type::get_array_instance(glsl_type::float_type
,
252 state
->Const
.MaxClipPlanes
);
254 /* FINISHME: gl_ClipDistance needs a real location assigned. */
255 add_variable("gl_ClipDistance", ir_var_out
, -1, clip_distance_array_type
,
256 instructions
, state
->symbols
);
262 initialize_vs_variables(exec_list
*instructions
,
263 struct _mesa_glsl_parse_state
*state
)
266 switch (state
->language_version
) {
268 generate_110_vs_variables(instructions
, state
);
271 generate_120_vs_variables(instructions
, state
);
274 generate_130_vs_variables(instructions
, state
);
280 generate_110_fs_variables(exec_list
*instructions
,
281 struct _mesa_glsl_parse_state
*state
)
283 for (unsigned i
= 0; i
< Elements(builtin_core_fs_variables
); i
++) {
284 add_builtin_variable(& builtin_core_fs_variables
[i
],
285 instructions
, state
->symbols
);
289 ; i
< Elements(builtin_110_deprecated_fs_variables
)
291 add_builtin_variable(& builtin_110_deprecated_fs_variables
[i
],
292 instructions
, state
->symbols
);
294 generate_110_uniforms(instructions
, state
);
296 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
298 * "As with all arrays, indices used to subscript gl_TexCoord must
299 * either be an integral constant expressions, or this array must be
300 * re-declared by the shader with a size. The size can be at most
301 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
302 * implementation in preserving varying resources."
304 const glsl_type
*const vec4_array_type
=
305 glsl_type::get_array_instance(glsl_type::vec4_type
, 0);
307 add_variable("gl_TexCoord", ir_var_in
, FRAG_ATTRIB_TEX0
, vec4_array_type
,
308 instructions
, state
->symbols
);
310 generate_ARB_draw_buffers_variables(instructions
, state
, false,
316 generate_ARB_draw_buffers_variables(exec_list
*instructions
,
317 struct _mesa_glsl_parse_state
*state
,
318 bool warn
, _mesa_glsl_parser_targets target
)
320 /* gl_MaxDrawBuffers is available in all shader stages.
322 ir_variable
*const mdb
=
323 add_variable("gl_MaxDrawBuffers", ir_var_auto
, -1,
324 glsl_type::int_type
, instructions
, state
->symbols
);
327 mdb
->warn_extension
= "GL_ARB_draw_buffers";
329 mdb
->constant_value
= new(mdb
)
330 ir_constant(int(state
->Const
.MaxDrawBuffers
));
333 /* gl_FragData is only available in the fragment shader.
335 if (target
== fragment_shader
) {
336 const glsl_type
*const vec4_array_type
=
337 glsl_type::get_array_instance(glsl_type::vec4_type
,
338 state
->Const
.MaxDrawBuffers
);
340 ir_variable
*const fd
=
341 add_variable("gl_FragData", ir_var_out
, FRAG_RESULT_DATA0
,
342 vec4_array_type
, instructions
, state
->symbols
);
345 fd
->warn_extension
= "GL_ARB_draw_buffers";
351 generate_120_fs_variables(exec_list
*instructions
,
352 struct _mesa_glsl_parse_state
*state
)
354 generate_110_fs_variables(instructions
, state
);
357 ; i
< Elements(builtin_120_fs_variables
)
359 add_builtin_variable(& builtin_120_fs_variables
[i
],
360 instructions
, state
->symbols
);
365 generate_130_fs_variables(exec_list
*instructions
,
366 struct _mesa_glsl_parse_state
*state
)
368 generate_120_fs_variables(instructions
, state
);
370 const glsl_type
*const clip_distance_array_type
=
371 glsl_type::get_array_instance(glsl_type::float_type
,
372 state
->Const
.MaxClipPlanes
);
374 /* FINISHME: gl_ClipDistance needs a real location assigned. */
375 add_variable("gl_ClipDistance", ir_var_in
, -1, clip_distance_array_type
,
376 instructions
, state
->symbols
);
380 initialize_fs_variables(exec_list
*instructions
,
381 struct _mesa_glsl_parse_state
*state
)
384 switch (state
->language_version
) {
386 generate_110_fs_variables(instructions
, state
);
389 generate_120_fs_variables(instructions
, state
);
392 generate_130_fs_variables(instructions
, state
);
398 _mesa_glsl_initialize_variables(exec_list
*instructions
,
399 struct _mesa_glsl_parse_state
*state
)
401 switch (state
->target
) {
403 initialize_vs_variables(instructions
, state
);
405 case geometry_shader
:
407 case fragment_shader
:
408 initialize_fs_variables(instructions
, state
);
411 fprintf(stderr
, "ir reader has no builtin variables");