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 "main/compiler.h"
26 #include "glsl_parser_extras.h"
27 #include "glsl_symbol_table.h"
28 #include "builtin_variables.h"
30 static void generate_ARB_draw_buffers_variables(exec_list
*,
31 struct _mesa_glsl_parse_state
*,
32 bool, _mesa_glsl_parser_targets
);
35 add_variable(const char *name
, enum ir_variable_mode mode
, int slot
,
36 const glsl_type
*type
, exec_list
*instructions
,
37 glsl_symbol_table
*symtab
)
39 ir_variable
*var
= new(symtab
) ir_variable(type
, name
, mode
);
45 var
->read_only
= true;
57 /* Once the variable is created an initialized, add it to the symbol table
58 * and add the declaration to the IR stream.
60 instructions
->push_tail(var
);
62 symtab
->add_variable(var
->name
, var
);
67 add_uniform(exec_list
*instructions
,
68 struct _mesa_glsl_parse_state
*state
,
69 const char *name
, const glsl_type
*type
)
71 return add_variable(name
, ir_var_uniform
, -1, type
, instructions
,
76 add_builtin_variable(const builtin_variable
*proto
, exec_list
*instructions
,
77 glsl_symbol_table
*symtab
)
79 /* Create a new variable declaration from the description supplied by
82 const glsl_type
*const type
= symtab
->get_type(proto
->type
);
86 add_variable(proto
->name
, proto
->mode
, proto
->slot
, type
, instructions
,
91 add_builtin_constant(exec_list
*instructions
,
92 struct _mesa_glsl_parse_state
*state
,
93 const char *name
, int value
)
95 ir_variable
*const var
= add_variable(name
, ir_var_auto
,
96 -1, glsl_type::int_type
,
97 instructions
, state
->symbols
);
98 var
->constant_value
= new(var
) ir_constant(value
);
102 generate_110_uniforms(exec_list
*instructions
,
103 struct _mesa_glsl_parse_state
*state
)
106 ; i
< Elements(builtin_110_deprecated_uniforms
)
108 add_builtin_variable(& builtin_110_deprecated_uniforms
[i
],
109 instructions
, state
->symbols
);
112 add_builtin_constant(instructions
, state
, "gl_MaxLights",
113 state
->Const
.MaxLights
);
114 add_builtin_constant(instructions
, state
, "gl_MaxClipPlanes",
115 state
->Const
.MaxClipPlanes
);
116 add_builtin_constant(instructions
, state
, "gl_MaxTextureUnits",
117 state
->Const
.MaxTextureUnits
);
118 add_builtin_constant(instructions
, state
, "gl_MaxTextureCoords",
119 state
->Const
.MaxTextureCoords
);
120 add_builtin_constant(instructions
, state
, "gl_MaxVertexAttribs",
121 state
->Const
.MaxVertexAttribs
);
122 add_builtin_constant(instructions
, state
, "gl_MaxVertexUniformComponents",
123 state
->Const
.MaxVertexUniformComponents
);
124 add_builtin_constant(instructions
, state
, "gl_MaxVaryingFloats",
125 state
->Const
.MaxVaryingFloats
);
126 add_builtin_constant(instructions
, state
, "gl_MaxVertexTextureImageUnits",
127 state
->Const
.MaxVertexTextureImageUnits
);
128 add_builtin_constant(instructions
, state
, "gl_MaxCombinedTextureImageUnits",
129 state
->Const
.MaxCombinedTextureImageUnits
);
130 add_builtin_constant(instructions
, state
, "gl_MaxTextureImageUnits",
131 state
->Const
.MaxTextureImageUnits
);
132 add_builtin_constant(instructions
, state
, "gl_MaxFragmentUniformComponents",
133 state
->Const
.MaxFragmentUniformComponents
);
135 const glsl_type
*const mat4_array_type
=
136 glsl_type::get_array_instance(glsl_type::mat4_type
,
137 state
->Const
.MaxTextureCoords
);
139 add_uniform(instructions
, state
, "gl_TextureMatrix", mat4_array_type
);
141 add_uniform(instructions
, state
, "gl_DepthRange",
142 state
->symbols
->get_type("gl_DepthRangeParameters"));
144 add_uniform(instructions
, state
, "gl_ClipPlane",
145 glsl_type::get_array_instance(glsl_type::vec4_type
,
146 state
->Const
.MaxClipPlanes
));
147 add_uniform(instructions
, state
, "gl_Point",
148 state
->symbols
->get_type("gl_PointParameters"));
150 const glsl_type
*const material_parameters_type
=
151 state
->symbols
->get_type("gl_MaterialParameters");
152 add_uniform(instructions
, state
, "gl_FrontMaterial", material_parameters_type
);
153 add_uniform(instructions
, state
, "gl_BackMaterial", material_parameters_type
);
155 const glsl_type
*const light_source_array_type
=
156 glsl_type::get_array_instance(state
->symbols
->get_type("gl_LightSourceParameters"), state
->Const
.MaxLights
);
158 add_uniform(instructions
, state
, "gl_LightSource", light_source_array_type
);
160 const glsl_type
*const light_model_products_type
=
161 state
->symbols
->get_type("gl_LightModelProducts");
162 add_uniform(instructions
, state
, "gl_FrontLightModelProduct",
163 light_model_products_type
);
164 add_uniform(instructions
, state
, "gl_BackLightModelProduct",
165 light_model_products_type
);
167 const glsl_type
*const light_products_type
=
168 glsl_type::get_array_instance(state
->symbols
->get_type("gl_LightProducts"),
169 state
->Const
.MaxLights
);
170 add_uniform(instructions
, state
, "gl_FrontLightProduct", light_products_type
);
171 add_uniform(instructions
, state
, "gl_BackLightProduct", light_products_type
);
173 add_uniform(instructions
, state
, "gl_TextureEnvColor",
174 glsl_type::get_array_instance(glsl_type::vec4_type
,
175 state
->Const
.MaxTextureUnits
));
177 const glsl_type
*const texcoords_vec4
=
178 glsl_type::get_array_instance(glsl_type::vec4_type
,
179 state
->Const
.MaxTextureCoords
);
180 add_uniform(instructions
, state
, "gl_EyePlaneS", texcoords_vec4
);
181 add_uniform(instructions
, state
, "gl_EyePlaneT", texcoords_vec4
);
182 add_uniform(instructions
, state
, "gl_EyePlaneR", texcoords_vec4
);
183 add_uniform(instructions
, state
, "gl_EyePlaneQ", texcoords_vec4
);
184 add_uniform(instructions
, state
, "gl_ObjectPlaneS", texcoords_vec4
);
185 add_uniform(instructions
, state
, "gl_ObjectPlaneT", texcoords_vec4
);
186 add_uniform(instructions
, state
, "gl_ObjectPlaneR", texcoords_vec4
);
187 add_uniform(instructions
, state
, "gl_ObjectPlaneQ", texcoords_vec4
);
189 add_uniform(instructions
, state
, "gl_Fog",
190 state
->symbols
->get_type("gl_FogParameters"));
194 generate_110_vs_variables(exec_list
*instructions
,
195 struct _mesa_glsl_parse_state
*state
)
197 for (unsigned i
= 0; i
< Elements(builtin_core_vs_variables
); i
++) {
198 add_builtin_variable(& builtin_core_vs_variables
[i
],
199 instructions
, state
->symbols
);
203 ; i
< Elements(builtin_110_deprecated_vs_variables
)
205 add_builtin_variable(& builtin_110_deprecated_vs_variables
[i
],
206 instructions
, state
->symbols
);
208 generate_110_uniforms(instructions
, state
);
210 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
212 * "As with all arrays, indices used to subscript gl_TexCoord must
213 * either be an integral constant expressions, or this array must be
214 * re-declared by the shader with a size. The size can be at most
215 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
216 * implementation in preserving varying resources."
218 const glsl_type
*const vec4_array_type
=
219 glsl_type::get_array_instance(glsl_type::vec4_type
, 0);
221 add_variable("gl_TexCoord", ir_var_out
, VERT_RESULT_TEX0
, vec4_array_type
,
222 instructions
, state
->symbols
);
224 generate_ARB_draw_buffers_variables(instructions
, state
, false,
230 generate_120_vs_variables(exec_list
*instructions
,
231 struct _mesa_glsl_parse_state
*state
)
233 /* GLSL version 1.20 did not add any built-in variables in the vertex
236 generate_110_vs_variables(instructions
, state
);
241 generate_130_vs_variables(exec_list
*instructions
,
242 struct _mesa_glsl_parse_state
*state
)
244 generate_120_vs_variables(instructions
, state
);
246 for (unsigned i
= 0; i
< Elements(builtin_130_vs_variables
); i
++) {
247 add_builtin_variable(& builtin_130_vs_variables
[i
],
248 instructions
, state
->symbols
);
251 const glsl_type
*const clip_distance_array_type
=
252 glsl_type::get_array_instance(glsl_type::float_type
,
253 state
->Const
.MaxClipPlanes
);
255 /* FINISHME: gl_ClipDistance needs a real location assigned. */
256 add_variable("gl_ClipDistance", ir_var_out
, -1, clip_distance_array_type
,
257 instructions
, state
->symbols
);
263 initialize_vs_variables(exec_list
*instructions
,
264 struct _mesa_glsl_parse_state
*state
)
267 switch (state
->language_version
) {
269 generate_110_vs_variables(instructions
, state
);
272 generate_120_vs_variables(instructions
, state
);
275 generate_130_vs_variables(instructions
, state
);
281 generate_110_fs_variables(exec_list
*instructions
,
282 struct _mesa_glsl_parse_state
*state
)
284 for (unsigned i
= 0; i
< Elements(builtin_core_fs_variables
); i
++) {
285 add_builtin_variable(& builtin_core_fs_variables
[i
],
286 instructions
, state
->symbols
);
290 ; i
< Elements(builtin_110_deprecated_fs_variables
)
292 add_builtin_variable(& builtin_110_deprecated_fs_variables
[i
],
293 instructions
, state
->symbols
);
295 generate_110_uniforms(instructions
, state
);
297 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
299 * "As with all arrays, indices used to subscript gl_TexCoord must
300 * either be an integral constant expressions, or this array must be
301 * re-declared by the shader with a size. The size can be at most
302 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
303 * implementation in preserving varying resources."
305 const glsl_type
*const vec4_array_type
=
306 glsl_type::get_array_instance(glsl_type::vec4_type
, 0);
308 add_variable("gl_TexCoord", ir_var_in
, FRAG_ATTRIB_TEX0
, vec4_array_type
,
309 instructions
, state
->symbols
);
311 generate_ARB_draw_buffers_variables(instructions
, state
, false,
317 generate_ARB_draw_buffers_variables(exec_list
*instructions
,
318 struct _mesa_glsl_parse_state
*state
,
319 bool warn
, _mesa_glsl_parser_targets target
)
321 /* gl_MaxDrawBuffers is available in all shader stages.
323 ir_variable
*const mdb
=
324 add_variable("gl_MaxDrawBuffers", ir_var_auto
, -1,
325 glsl_type::int_type
, instructions
, state
->symbols
);
328 mdb
->warn_extension
= "GL_ARB_draw_buffers";
330 mdb
->constant_value
= new(mdb
)
331 ir_constant(int(state
->Const
.MaxDrawBuffers
));
334 /* gl_FragData is only available in the fragment shader.
336 if (target
== fragment_shader
) {
337 const glsl_type
*const vec4_array_type
=
338 glsl_type::get_array_instance(glsl_type::vec4_type
,
339 state
->Const
.MaxDrawBuffers
);
341 ir_variable
*const fd
=
342 add_variable("gl_FragData", ir_var_out
, FRAG_RESULT_DATA0
,
343 vec4_array_type
, instructions
, state
->symbols
);
346 fd
->warn_extension
= "GL_ARB_draw_buffers";
352 generate_120_fs_variables(exec_list
*instructions
,
353 struct _mesa_glsl_parse_state
*state
)
355 generate_110_fs_variables(instructions
, state
);
358 ; i
< Elements(builtin_120_fs_variables
)
360 add_builtin_variable(& builtin_120_fs_variables
[i
],
361 instructions
, state
->symbols
);
366 generate_130_fs_variables(exec_list
*instructions
,
367 struct _mesa_glsl_parse_state
*state
)
369 generate_120_fs_variables(instructions
, state
);
371 const glsl_type
*const clip_distance_array_type
=
372 glsl_type::get_array_instance(glsl_type::float_type
,
373 state
->Const
.MaxClipPlanes
);
375 /* FINISHME: gl_ClipDistance needs a real location assigned. */
376 add_variable("gl_ClipDistance", ir_var_in
, -1, clip_distance_array_type
,
377 instructions
, state
->symbols
);
381 initialize_fs_variables(exec_list
*instructions
,
382 struct _mesa_glsl_parse_state
*state
)
385 switch (state
->language_version
) {
387 generate_110_fs_variables(instructions
, state
);
390 generate_120_fs_variables(instructions
, state
);
393 generate_130_fs_variables(instructions
, state
);
399 _mesa_glsl_initialize_variables(exec_list
*instructions
,
400 struct _mesa_glsl_parse_state
*state
)
402 switch (state
->target
) {
404 initialize_vs_variables(instructions
, state
);
406 case geometry_shader
:
408 case fragment_shader
:
409 initialize_fs_variables(instructions
, state
);
412 fprintf(stderr
, "ir reader has no builtin variables");