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
);
81 add_builtin_variable(const builtin_variable
*proto
, exec_list
*instructions
,
82 glsl_symbol_table
*symtab
)
84 /* Create a new variable declaration from the description supplied by
87 const glsl_type
*const type
= symtab
->get_type(proto
->type
);
91 add_variable(proto
->name
, proto
->mode
, proto
->slot
, type
, instructions
,
96 add_builtin_constant(exec_list
*instructions
,
97 struct _mesa_glsl_parse_state
*state
,
98 const char *name
, int value
)
100 ir_variable
*const var
= add_variable(name
, ir_var_auto
,
101 -1, glsl_type::int_type
,
102 instructions
, state
->symbols
);
103 var
->constant_value
= new(var
) ir_constant(value
);
107 generate_110_uniforms(exec_list
*instructions
,
108 struct _mesa_glsl_parse_state
*state
)
111 ; i
< Elements(builtin_110_deprecated_uniforms
)
113 add_builtin_variable(& builtin_110_deprecated_uniforms
[i
],
114 instructions
, state
->symbols
);
117 add_builtin_constant(instructions
, state
, "gl_MaxLights",
118 state
->Const
.MaxLights
);
119 add_builtin_constant(instructions
, state
, "gl_MaxClipPlanes",
120 state
->Const
.MaxClipPlanes
);
121 add_builtin_constant(instructions
, state
, "gl_MaxTextureUnits",
122 state
->Const
.MaxTextureUnits
);
123 add_builtin_constant(instructions
, state
, "gl_MaxTextureCoords",
124 state
->Const
.MaxTextureCoords
);
125 add_builtin_constant(instructions
, state
, "gl_MaxVertexAttribs",
126 state
->Const
.MaxVertexAttribs
);
127 add_builtin_constant(instructions
, state
, "gl_MaxVertexUniformComponents",
128 state
->Const
.MaxVertexUniformComponents
);
129 add_builtin_constant(instructions
, state
, "gl_MaxVaryingFloats",
130 state
->Const
.MaxVaryingFloats
);
131 add_builtin_constant(instructions
, state
, "gl_MaxVertexTextureImageUnits",
132 state
->Const
.MaxVertexTextureImageUnits
);
133 add_builtin_constant(instructions
, state
, "gl_MaxCombinedTextureImageUnits",
134 state
->Const
.MaxCombinedTextureImageUnits
);
135 add_builtin_constant(instructions
, state
, "gl_MaxTextureImageUnits",
136 state
->Const
.MaxTextureImageUnits
);
137 add_builtin_constant(instructions
, state
, "gl_MaxFragmentUniformComponents",
138 state
->Const
.MaxFragmentUniformComponents
);
140 const glsl_type
*const mat4_array_type
=
141 glsl_type::get_array_instance(glsl_type::mat4_type
,
142 state
->Const
.MaxTextureCoords
);
144 add_variable("gl_TextureMatrix", ir_var_uniform
, -1, mat4_array_type
,
145 instructions
, state
->symbols
);
147 /* FINISHME: Add support for gl_DepthRangeParameters */
148 /* FINISHME: Add support for gl_ClipPlane[] */
149 /* FINISHME: Add support for gl_PointParameters */
151 /* FINISHME: Add support for gl_MaterialParameters
152 * FINISHME: (glFrontMaterial, glBackMaterial)
155 /* FINISHME: The size of this array is implementation dependent based on the
156 * FINISHME: value of GL_MAX_TEXTURE_LIGHTS. GL_MAX_TEXTURE_LIGHTS must be
157 * FINISHME: at least 8, so hard-code 8 for now.
159 const glsl_type
*const light_source_array_type
=
160 glsl_type::get_array_instance(state
->symbols
->get_type("gl_LightSourceParameters"), 8);
162 add_variable("gl_LightSource", ir_var_uniform
, -1, light_source_array_type
,
163 instructions
, state
->symbols
);
165 /* FINISHME: Add support for gl_LightModel */
166 /* FINISHME: Add support for gl_FrontLightProduct[], gl_BackLightProduct[] */
167 /* FINISHME: Add support for gl_TextureEnvColor[] */
168 /* FINISHME: Add support for gl_ObjectPlane*[], gl_EyePlane*[] */
169 /* FINISHME: Add support for gl_Fog */
173 generate_110_vs_variables(exec_list
*instructions
,
174 struct _mesa_glsl_parse_state
*state
)
176 for (unsigned i
= 0; i
< Elements(builtin_core_vs_variables
); i
++) {
177 add_builtin_variable(& builtin_core_vs_variables
[i
],
178 instructions
, state
->symbols
);
182 ; i
< Elements(builtin_110_deprecated_vs_variables
)
184 add_builtin_variable(& builtin_110_deprecated_vs_variables
[i
],
185 instructions
, state
->symbols
);
187 generate_110_uniforms(instructions
, state
);
189 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
191 * "As with all arrays, indices used to subscript gl_TexCoord must
192 * either be an integral constant expressions, or this array must be
193 * re-declared by the shader with a size. The size can be at most
194 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
195 * implementation in preserving varying resources."
197 const glsl_type
*const vec4_array_type
=
198 glsl_type::get_array_instance(glsl_type::vec4_type
, 0);
200 add_variable("gl_TexCoord", ir_var_out
, VERT_RESULT_TEX0
, vec4_array_type
,
201 instructions
, state
->symbols
);
203 generate_ARB_draw_buffers_variables(instructions
, state
, false,
209 generate_120_vs_variables(exec_list
*instructions
,
210 struct _mesa_glsl_parse_state
*state
)
212 /* GLSL version 1.20 did not add any built-in variables in the vertex
215 generate_110_vs_variables(instructions
, state
);
220 generate_130_vs_variables(exec_list
*instructions
,
221 struct _mesa_glsl_parse_state
*state
)
223 generate_120_vs_variables(instructions
, state
);
225 for (unsigned i
= 0; i
< Elements(builtin_130_vs_variables
); i
++) {
226 add_builtin_variable(& builtin_130_vs_variables
[i
],
227 instructions
, state
->symbols
);
230 /* FINISHME: The size of this array is implementation dependent based on
231 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
233 const glsl_type
*const clip_distance_array_type
=
234 glsl_type::get_array_instance(glsl_type::float_type
, 8);
236 /* FINISHME: gl_ClipDistance needs a real location assigned. */
237 add_variable("gl_ClipDistance", ir_var_out
, -1, clip_distance_array_type
,
238 instructions
, state
->symbols
);
244 initialize_vs_variables(exec_list
*instructions
,
245 struct _mesa_glsl_parse_state
*state
)
248 switch (state
->language_version
) {
250 generate_110_vs_variables(instructions
, state
);
253 generate_120_vs_variables(instructions
, state
);
256 generate_130_vs_variables(instructions
, state
);
262 generate_110_fs_variables(exec_list
*instructions
,
263 struct _mesa_glsl_parse_state
*state
)
265 for (unsigned i
= 0; i
< Elements(builtin_core_fs_variables
); i
++) {
266 add_builtin_variable(& builtin_core_fs_variables
[i
],
267 instructions
, state
->symbols
);
271 ; i
< Elements(builtin_110_deprecated_fs_variables
)
273 add_builtin_variable(& builtin_110_deprecated_fs_variables
[i
],
274 instructions
, state
->symbols
);
276 generate_110_uniforms(instructions
, state
);
278 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
280 * "As with all arrays, indices used to subscript gl_TexCoord must
281 * either be an integral constant expressions, or this array must be
282 * re-declared by the shader with a size. The size can be at most
283 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
284 * implementation in preserving varying resources."
286 const glsl_type
*const vec4_array_type
=
287 glsl_type::get_array_instance(glsl_type::vec4_type
, 0);
289 add_variable("gl_TexCoord", ir_var_in
, FRAG_ATTRIB_TEX0
, vec4_array_type
,
290 instructions
, state
->symbols
);
292 generate_ARB_draw_buffers_variables(instructions
, state
, false,
298 generate_ARB_draw_buffers_variables(exec_list
*instructions
,
299 struct _mesa_glsl_parse_state
*state
,
300 bool warn
, _mesa_glsl_parser_targets target
)
302 /* gl_MaxDrawBuffers is available in all shader stages.
304 ir_variable
*const mdb
=
305 add_variable("gl_MaxDrawBuffers", ir_var_auto
, -1,
306 glsl_type::int_type
, instructions
, state
->symbols
);
309 mdb
->warn_extension
= "GL_ARB_draw_buffers";
311 mdb
->constant_value
= new(mdb
)
312 ir_constant(int(state
->Const
.MaxDrawBuffers
));
315 /* gl_FragData is only available in the fragment shader.
317 if (target
== fragment_shader
) {
318 const glsl_type
*const vec4_array_type
=
319 glsl_type::get_array_instance(glsl_type::vec4_type
,
320 state
->Const
.MaxDrawBuffers
);
322 ir_variable
*const fd
=
323 add_variable("gl_FragData", ir_var_out
, FRAG_RESULT_DATA0
,
324 vec4_array_type
, instructions
, state
->symbols
);
327 fd
->warn_extension
= "GL_ARB_draw_buffers";
333 generate_120_fs_variables(exec_list
*instructions
,
334 struct _mesa_glsl_parse_state
*state
)
336 generate_110_fs_variables(instructions
, state
);
339 ; i
< Elements(builtin_120_fs_variables
)
341 add_builtin_variable(& builtin_120_fs_variables
[i
],
342 instructions
, state
->symbols
);
347 generate_130_fs_variables(exec_list
*instructions
,
348 struct _mesa_glsl_parse_state
*state
)
350 generate_120_fs_variables(instructions
, state
);
352 /* FINISHME: The size of this array is implementation dependent based on
353 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
355 const glsl_type
*const clip_distance_array_type
=
356 glsl_type::get_array_instance(glsl_type::float_type
, 8);
358 /* FINISHME: gl_ClipDistance needs a real location assigned. */
359 add_variable("gl_ClipDistance", ir_var_in
, -1, clip_distance_array_type
,
360 instructions
, state
->symbols
);
364 initialize_fs_variables(exec_list
*instructions
,
365 struct _mesa_glsl_parse_state
*state
)
368 switch (state
->language_version
) {
370 generate_110_fs_variables(instructions
, state
);
373 generate_120_fs_variables(instructions
, state
);
376 generate_130_fs_variables(instructions
, state
);
382 _mesa_glsl_initialize_variables(exec_list
*instructions
,
383 struct _mesa_glsl_parse_state
*state
)
385 switch (state
->target
) {
387 initialize_vs_variables(instructions
, state
);
389 case geometry_shader
:
391 case fragment_shader
:
392 initialize_fs_variables(instructions
, state
);
395 fprintf(stderr
, "ir reader has no builtin variables");