blob: 68d9ab2055dc3d6d0bd075c9835b91065edb0fc8 [file] [log] [blame]
Ian Romanickadfb0cd2010-03-10 10:43:16 -08001/*
2 * Copyright © 2010 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
Eric Anholtac95f2f2010-06-22 10:38:52 -070024#include "ir.h"
Ian Romanickadfb0cd2010-03-10 10:43:16 -080025#include "glsl_parser_extras.h"
Ian Romanick8bde4ce2010-03-19 11:57:24 -070026#include "glsl_symbol_table.h"
Ian Romanickadfb0cd2010-03-10 10:43:16 -080027#include "builtin_variables.h"
28
Ian Romanick22971e92010-06-29 15:29:56 -070029static void generate_ARB_draw_buffers_variables(exec_list *,
30 struct _mesa_glsl_parse_state *,
31 bool, _mesa_glsl_parser_targets);
Ian Romanick9c4b1f22010-06-29 15:10:09 -070032
Brian Paul7ce18632010-12-08 18:25:38 -070033static void
34generate_ARB_draw_instanced_variables(exec_list *,
35 struct _mesa_glsl_parse_state *,
36 bool, _mesa_glsl_parser_targets);
37
Ian Romanickc77b2572010-04-07 16:59:46 -070038static ir_variable *
Ian Romanickdfdff942011-01-24 16:45:11 -080039add_variable(exec_list *instructions, glsl_symbol_table *symtab,
40 const char *name, const glsl_type *type,
41 enum ir_variable_mode mode, int slot)
Ian Romanickadfb0cd2010-03-10 10:43:16 -080042{
Ian Romanick7e2aa912010-07-19 17:12:42 -070043 ir_variable *var = new(symtab) ir_variable(type, name, mode);
Ian Romanickadfb0cd2010-03-10 10:43:16 -080044
Eric Anholt71df19f2010-04-19 11:10:37 -070045 switch (var->mode) {
Ian Romanicke2f84f02010-06-29 15:19:11 -070046 case ir_var_auto:
Eric Anholt71df19f2010-04-19 11:10:37 -070047 case ir_var_in:
Kenneth Graunke819d57f2011-01-12 15:37:37 -080048 case ir_var_const_in:
Eric Anholt046bef22010-08-04 20:33:57 -070049 case ir_var_uniform:
Brian Paul7ce18632010-12-08 18:25:38 -070050 case ir_var_system_value:
Ian Romanickadfb0cd2010-03-10 10:43:16 -080051 var->read_only = true;
Eric Anholt71df19f2010-04-19 11:10:37 -070052 break;
53 case ir_var_inout:
Eric Anholt71df19f2010-04-19 11:10:37 -070054 case ir_var_out:
Eric Anholt71df19f2010-04-19 11:10:37 -070055 break;
56 default:
57 assert(0);
58 break;
59 }
Ian Romanickadfb0cd2010-03-10 10:43:16 -080060
Ian Romanicked0626e2010-06-21 11:42:57 -070061 var->location = slot;
Ian Romanick68a4fc92010-10-07 17:21:22 -070062 var->explicit_location = (slot >= 0);
Ian Romanicked0626e2010-06-21 11:42:57 -070063
Ian Romanickadfb0cd2010-03-10 10:43:16 -080064 /* Once the variable is created an initialized, add it to the symbol table
65 * and add the declaration to the IR stream.
66 */
67 instructions->push_tail(var);
68
Eric Anholt001eee52010-11-05 06:11:24 -070069 symtab->add_variable(var);
Ian Romanickc77b2572010-04-07 16:59:46 -070070 return var;
Ian Romanickadfb0cd2010-03-10 10:43:16 -080071}
72
Eric Anholt85b5dba2010-07-28 12:23:51 -070073static ir_variable *
Ian Romanickdfdff942011-01-24 16:45:11 -080074add_uniform(exec_list *instructions, glsl_symbol_table *symtab,
Eric Anholt85b5dba2010-07-28 12:23:51 -070075 const char *name, const glsl_type *type)
76{
Ian Romanickdfdff942011-01-24 16:45:11 -080077 return add_variable(instructions, symtab, name, type, ir_var_uniform, -1);
Eric Anholt85b5dba2010-07-28 12:23:51 -070078}
Ian Romanick3f9a73d2010-04-02 11:59:57 -070079
80static void
Ian Romanickdfdff942011-01-24 16:45:11 -080081add_builtin_variable(exec_list *instructions, glsl_symbol_table *symtab,
82 const builtin_variable *proto)
Ian Romanick3f9a73d2010-04-02 11:59:57 -070083{
84 /* Create a new variable declaration from the description supplied by
85 * the caller.
86 */
87 const glsl_type *const type = symtab->get_type(proto->type);
88
89 assert(type != NULL);
90
Ian Romanickdfdff942011-01-24 16:45:11 -080091 add_variable(instructions, symtab, proto->name, type, proto->mode,
92 proto->slot);
Ian Romanick3f9a73d2010-04-02 11:59:57 -070093}
94
Eric Anholtf8946692010-07-20 14:03:35 -070095static void
Ian Romanickdfdff942011-01-24 16:45:11 -080096add_builtin_constant(exec_list *instructions, glsl_symbol_table *symtab,
Eric Anholtf8946692010-07-20 14:03:35 -070097 const char *name, int value)
98{
Ian Romanickdfdff942011-01-24 16:45:11 -080099 ir_variable *const var = add_variable(instructions, symtab,
100 name, glsl_type::int_type,
101 ir_var_auto, -1);
Eric Anholtf8946692010-07-20 14:03:35 -0700102 var->constant_value = new(var) ir_constant(value);
103}
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700104
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700105/* Several constants in GLSL ES have different names than normal desktop GLSL.
106 * Therefore, this function should only be called on the ES path.
107 */
108static void
109generate_100ES_uniforms(exec_list *instructions,
110 struct _mesa_glsl_parse_state *state)
111{
Ian Romanickdfdff942011-01-24 16:45:11 -0800112 glsl_symbol_table *const symtab = state->symbols;
113
114 add_builtin_constant(instructions, symtab, "gl_MaxVertexAttribs",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700115 state->Const.MaxVertexAttribs);
Ian Romanickdfdff942011-01-24 16:45:11 -0800116 add_builtin_constant(instructions, symtab, "gl_MaxVertexUniformVectors",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700117 state->Const.MaxVertexUniformComponents);
Ian Romanickdfdff942011-01-24 16:45:11 -0800118 add_builtin_constant(instructions, symtab, "gl_MaxVaryingVectors",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700119 state->Const.MaxVaryingFloats / 4);
Ian Romanickdfdff942011-01-24 16:45:11 -0800120 add_builtin_constant(instructions, symtab, "gl_MaxVertexTextureImageUnits",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700121 state->Const.MaxVertexTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800122 add_builtin_constant(instructions, symtab, "gl_MaxCombinedTextureImageUnits",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700123 state->Const.MaxCombinedTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800124 add_builtin_constant(instructions, symtab, "gl_MaxTextureImageUnits",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700125 state->Const.MaxTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800126 add_builtin_constant(instructions, symtab, "gl_MaxFragmentUniformVectors",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700127 state->Const.MaxFragmentUniformComponents);
128
Ian Romanickdfdff942011-01-24 16:45:11 -0800129 add_uniform(instructions, symtab, "gl_DepthRange",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700130 state->symbols->get_type("gl_DepthRangeParameters"));
131}
132
Eric Anholt78fe3c92010-03-28 01:46:48 -0700133static void
134generate_110_uniforms(exec_list *instructions,
Ian Romanick127308b2010-07-01 13:30:50 -0700135 struct _mesa_glsl_parse_state *state)
Eric Anholt78fe3c92010-03-28 01:46:48 -0700136{
Ian Romanickdfdff942011-01-24 16:45:11 -0800137 glsl_symbol_table *const symtab = state->symbols;
138
Eric Anholt78fe3c92010-03-28 01:46:48 -0700139 for (unsigned i = 0
140 ; i < Elements(builtin_110_deprecated_uniforms)
141 ; i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800142 add_builtin_variable(instructions, symtab,
143 & builtin_110_deprecated_uniforms[i]);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700144 }
145
Ian Romanickdfdff942011-01-24 16:45:11 -0800146 add_builtin_constant(instructions, symtab, "gl_MaxLights",
Eric Anholtf8946692010-07-20 14:03:35 -0700147 state->Const.MaxLights);
Ian Romanickdfdff942011-01-24 16:45:11 -0800148 add_builtin_constant(instructions, symtab, "gl_MaxClipPlanes",
Eric Anholtf8946692010-07-20 14:03:35 -0700149 state->Const.MaxClipPlanes);
Ian Romanickdfdff942011-01-24 16:45:11 -0800150 add_builtin_constant(instructions, symtab, "gl_MaxTextureUnits",
Eric Anholtf8946692010-07-20 14:03:35 -0700151 state->Const.MaxTextureUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800152 add_builtin_constant(instructions, symtab, "gl_MaxTextureCoords",
Eric Anholtf8946692010-07-20 14:03:35 -0700153 state->Const.MaxTextureCoords);
Ian Romanickdfdff942011-01-24 16:45:11 -0800154 add_builtin_constant(instructions, symtab, "gl_MaxVertexAttribs",
Eric Anholtf8946692010-07-20 14:03:35 -0700155 state->Const.MaxVertexAttribs);
Ian Romanickdfdff942011-01-24 16:45:11 -0800156 add_builtin_constant(instructions, symtab, "gl_MaxVertexUniformComponents",
Eric Anholtf8946692010-07-20 14:03:35 -0700157 state->Const.MaxVertexUniformComponents);
Ian Romanickdfdff942011-01-24 16:45:11 -0800158 add_builtin_constant(instructions, symtab, "gl_MaxVaryingFloats",
Eric Anholtf8946692010-07-20 14:03:35 -0700159 state->Const.MaxVaryingFloats);
Ian Romanickdfdff942011-01-24 16:45:11 -0800160 add_builtin_constant(instructions, symtab, "gl_MaxVertexTextureImageUnits",
Eric Anholtf8946692010-07-20 14:03:35 -0700161 state->Const.MaxVertexTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800162 add_builtin_constant(instructions, symtab, "gl_MaxCombinedTextureImageUnits",
Eric Anholtf8946692010-07-20 14:03:35 -0700163 state->Const.MaxCombinedTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800164 add_builtin_constant(instructions, symtab, "gl_MaxTextureImageUnits",
Eric Anholtf8946692010-07-20 14:03:35 -0700165 state->Const.MaxTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800166 add_builtin_constant(instructions, symtab, "gl_MaxFragmentUniformComponents",
Eric Anholtf8946692010-07-20 14:03:35 -0700167 state->Const.MaxFragmentUniformComponents);
Ian Romanick127308b2010-07-01 13:30:50 -0700168
Ian Romanick3eba5932010-04-26 14:59:32 -0700169 const glsl_type *const mat4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700170 glsl_type::get_array_instance(glsl_type::mat4_type,
Ian Romanick127308b2010-07-01 13:30:50 -0700171 state->Const.MaxTextureCoords);
Ian Romanick3eba5932010-04-26 14:59:32 -0700172
Ian Romanickdfdff942011-01-24 16:45:11 -0800173 add_uniform(instructions, symtab, "gl_TextureMatrix", mat4_array_type);
174 add_uniform(instructions, symtab, "gl_TextureMatrixInverse", mat4_array_type);
175 add_uniform(instructions, symtab, "gl_TextureMatrixTranspose", mat4_array_type);
176 add_uniform(instructions, symtab, "gl_TextureMatrixInverseTranspose", mat4_array_type);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700177
Ian Romanickdfdff942011-01-24 16:45:11 -0800178 add_uniform(instructions, symtab, "gl_DepthRange",
179 symtab->get_type("gl_DepthRangeParameters"));
Eric Anholt78fe3c92010-03-28 01:46:48 -0700180
Ian Romanickdfdff942011-01-24 16:45:11 -0800181 add_uniform(instructions, symtab, "gl_ClipPlane",
Eric Anholt85b5dba2010-07-28 12:23:51 -0700182 glsl_type::get_array_instance(glsl_type::vec4_type,
183 state->Const.MaxClipPlanes));
Ian Romanickdfdff942011-01-24 16:45:11 -0800184 add_uniform(instructions, symtab, "gl_Point",
185 symtab->get_type("gl_PointParameters"));
Eric Anholt85b5dba2010-07-28 12:23:51 -0700186
187 const glsl_type *const material_parameters_type =
Ian Romanickdfdff942011-01-24 16:45:11 -0800188 symtab->get_type("gl_MaterialParameters");
189 add_uniform(instructions, symtab, "gl_FrontMaterial", material_parameters_type);
190 add_uniform(instructions, symtab, "gl_BackMaterial", material_parameters_type);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700191
Eric Anholtaa579432010-05-19 14:09:04 -0700192 const glsl_type *const light_source_array_type =
Ian Romanickdfdff942011-01-24 16:45:11 -0800193 glsl_type::get_array_instance(symtab->get_type("gl_LightSourceParameters"), state->Const.MaxLights);
Eric Anholtaa579432010-05-19 14:09:04 -0700194
Ian Romanickdfdff942011-01-24 16:45:11 -0800195 add_uniform(instructions, symtab, "gl_LightSource", light_source_array_type);
Eric Anholtaa579432010-05-19 14:09:04 -0700196
Eric Anholt85b5dba2010-07-28 12:23:51 -0700197 const glsl_type *const light_model_products_type =
Ian Romanickdfdff942011-01-24 16:45:11 -0800198 symtab->get_type("gl_LightModelProducts");
199 add_uniform(instructions, symtab, "gl_FrontLightModelProduct",
Eric Anholt85b5dba2010-07-28 12:23:51 -0700200 light_model_products_type);
Ian Romanickdfdff942011-01-24 16:45:11 -0800201 add_uniform(instructions, symtab, "gl_BackLightModelProduct",
Eric Anholt85b5dba2010-07-28 12:23:51 -0700202 light_model_products_type);
203
204 const glsl_type *const light_products_type =
Ian Romanickdfdff942011-01-24 16:45:11 -0800205 glsl_type::get_array_instance(symtab->get_type("gl_LightProducts"),
Eric Anholt85b5dba2010-07-28 12:23:51 -0700206 state->Const.MaxLights);
Ian Romanickdfdff942011-01-24 16:45:11 -0800207 add_uniform(instructions, symtab, "gl_FrontLightProduct", light_products_type);
208 add_uniform(instructions, symtab, "gl_BackLightProduct", light_products_type);
Eric Anholt85b5dba2010-07-28 12:23:51 -0700209
Ian Romanickdfdff942011-01-24 16:45:11 -0800210 add_uniform(instructions, symtab, "gl_TextureEnvColor",
Eric Anholt85b5dba2010-07-28 12:23:51 -0700211 glsl_type::get_array_instance(glsl_type::vec4_type,
212 state->Const.MaxTextureUnits));
213
214 const glsl_type *const texcoords_vec4 =
215 glsl_type::get_array_instance(glsl_type::vec4_type,
216 state->Const.MaxTextureCoords);
Ian Romanickdfdff942011-01-24 16:45:11 -0800217 add_uniform(instructions, symtab, "gl_EyePlaneS", texcoords_vec4);
218 add_uniform(instructions, symtab, "gl_EyePlaneT", texcoords_vec4);
219 add_uniform(instructions, symtab, "gl_EyePlaneR", texcoords_vec4);
220 add_uniform(instructions, symtab, "gl_EyePlaneQ", texcoords_vec4);
221 add_uniform(instructions, symtab, "gl_ObjectPlaneS", texcoords_vec4);
222 add_uniform(instructions, symtab, "gl_ObjectPlaneT", texcoords_vec4);
223 add_uniform(instructions, symtab, "gl_ObjectPlaneR", texcoords_vec4);
224 add_uniform(instructions, symtab, "gl_ObjectPlaneQ", texcoords_vec4);
Eric Anholt85b5dba2010-07-28 12:23:51 -0700225
Ian Romanickdfdff942011-01-24 16:45:11 -0800226 add_uniform(instructions, symtab, "gl_Fog",
227 symtab->get_type("gl_FogParameters"));
Eric Anholt78fe3c92010-03-28 01:46:48 -0700228}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800229
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700230/* This function should only be called for ES, not desktop GL. */
231static void
232generate_100ES_vs_variables(exec_list *instructions,
233 struct _mesa_glsl_parse_state *state)
234{
235 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800236 add_builtin_variable(instructions, state->symbols,
237 & builtin_core_vs_variables[i]);
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700238 }
239
240 generate_100ES_uniforms(instructions, state);
241
242 generate_ARB_draw_buffers_variables(instructions, state, false,
243 vertex_shader);
244}
245
246
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800247static void
248generate_110_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700249 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800250{
251 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800252 add_builtin_variable(instructions, state->symbols,
253 & builtin_core_vs_variables[i]);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800254 }
255
256 for (unsigned i = 0
257 ; i < Elements(builtin_110_deprecated_vs_variables)
258 ; i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800259 add_builtin_variable(instructions, state->symbols,
260 & builtin_110_deprecated_vs_variables[i]);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800261 }
Ian Romanick127308b2010-07-01 13:30:50 -0700262 generate_110_uniforms(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800263
Ian Romanickcd00d5b2010-07-01 13:17:54 -0700264 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
265 *
266 * "As with all arrays, indices used to subscript gl_TexCoord must
267 * either be an integral constant expressions, or this array must be
268 * re-declared by the shader with a size. The size can be at most
269 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
270 * implementation in preserving varying resources."
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800271 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700272 const glsl_type *const vec4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700273 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700274
Ian Romanickdfdff942011-01-24 16:45:11 -0800275 add_variable(instructions, state->symbols,
276 "gl_TexCoord", vec4_array_type, ir_var_out, VERT_RESULT_TEX0);
Ian Romanick22971e92010-06-29 15:29:56 -0700277
278 generate_ARB_draw_buffers_variables(instructions, state, false,
279 vertex_shader);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800280}
281
282
283static void
284generate_120_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700285 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800286{
287 /* GLSL version 1.20 did not add any built-in variables in the vertex
288 * shader.
289 */
Ian Romanick22971e92010-06-29 15:29:56 -0700290 generate_110_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800291}
292
293
294static void
295generate_130_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700296 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800297{
Ian Romanick22971e92010-06-29 15:29:56 -0700298 generate_120_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800299
300 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800301 add_builtin_variable(instructions, state->symbols,
302 & builtin_130_vs_variables[i]);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800303 }
304
Eric Anholt271e1992010-04-02 23:47:06 -0700305 const glsl_type *const clip_distance_array_type =
Eric Anholt73df6362010-07-28 08:18:59 -0700306 glsl_type::get_array_instance(glsl_type::float_type,
307 state->Const.MaxClipPlanes);
Ian Romanicked0626e2010-06-21 11:42:57 -0700308
309 /* FINISHME: gl_ClipDistance needs a real location assigned. */
Ian Romanickdfdff942011-01-24 16:45:11 -0800310 add_variable(instructions, state->symbols,
311 "gl_ClipDistance", clip_distance_array_type, ir_var_out, -1);
Eric Anholt271e1992010-04-02 23:47:06 -0700312
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800313}
314
315
316static void
317initialize_vs_variables(exec_list *instructions,
318 struct _mesa_glsl_parse_state *state)
319{
320
321 switch (state->language_version) {
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700322 case 100:
323 generate_100ES_vs_variables(instructions, state);
324 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800325 case 110:
Ian Romanick22971e92010-06-29 15:29:56 -0700326 generate_110_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800327 break;
328 case 120:
Ian Romanick22971e92010-06-29 15:29:56 -0700329 generate_120_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800330 break;
331 case 130:
Ian Romanick22971e92010-06-29 15:29:56 -0700332 generate_130_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800333 break;
334 }
Brian Paul7ce18632010-12-08 18:25:38 -0700335
336 if (state->ARB_draw_instanced_enable)
337 generate_ARB_draw_instanced_variables(instructions, state, false,
338 vertex_shader);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800339}
340
Brian Paul7ce18632010-12-08 18:25:38 -0700341
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700342/* This function should only be called for ES, not desktop GL. */
343static void
344generate_100ES_fs_variables(exec_list *instructions,
345 struct _mesa_glsl_parse_state *state)
346{
347 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800348 add_builtin_variable(instructions, state->symbols,
349 & builtin_core_fs_variables[i]);
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700350 }
351
352 for (unsigned i = 0; i < Elements(builtin_100ES_fs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800353 add_builtin_variable(instructions, state->symbols,
354 & builtin_100ES_fs_variables[i]);
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700355 }
356
357 generate_100ES_uniforms(instructions, state);
358
359 generate_ARB_draw_buffers_variables(instructions, state, false,
360 fragment_shader);
361}
362
Eric Anholtb3f743a2010-03-25 14:48:25 -0700363static void
364generate_110_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700365 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700366{
367 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800368 add_builtin_variable(instructions, state->symbols,
369 & builtin_core_fs_variables[i]);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700370 }
371
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700372 for (unsigned i = 0; i < Elements(builtin_110_fs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800373 add_builtin_variable(instructions, state->symbols,
374 & builtin_110_fs_variables[i]);
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700375 }
376
Eric Anholt0f09aea2010-03-27 12:48:57 -0700377 for (unsigned i = 0
378 ; i < Elements(builtin_110_deprecated_fs_variables)
379 ; i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800380 add_builtin_variable(instructions, state->symbols,
381 & builtin_110_deprecated_fs_variables[i]);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700382 }
Ian Romanick127308b2010-07-01 13:30:50 -0700383 generate_110_uniforms(instructions, state);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700384
Ian Romanickcd00d5b2010-07-01 13:17:54 -0700385 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
386 *
387 * "As with all arrays, indices used to subscript gl_TexCoord must
388 * either be an integral constant expressions, or this array must be
389 * re-declared by the shader with a size. The size can be at most
390 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
391 * implementation in preserving varying resources."
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700392 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700393 const glsl_type *const vec4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700394 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700395
Ian Romanickdfdff942011-01-24 16:45:11 -0800396 add_variable(instructions, state->symbols,
397 "gl_TexCoord", vec4_array_type, ir_var_in, FRAG_ATTRIB_TEX0);
Ian Romanick9c4b1f22010-06-29 15:10:09 -0700398
Ian Romanick22971e92010-06-29 15:29:56 -0700399 generate_ARB_draw_buffers_variables(instructions, state, false,
400 fragment_shader);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700401}
402
Ian Romanickc77b2572010-04-07 16:59:46 -0700403
404static void
Ian Romanick22971e92010-06-29 15:29:56 -0700405generate_ARB_draw_buffers_variables(exec_list *instructions,
406 struct _mesa_glsl_parse_state *state,
407 bool warn, _mesa_glsl_parser_targets target)
Ian Romanickc77b2572010-04-07 16:59:46 -0700408{
Ian Romanick22971e92010-06-29 15:29:56 -0700409 /* gl_MaxDrawBuffers is available in all shader stages.
410 */
Ian Romanicke2f84f02010-06-29 15:19:11 -0700411 ir_variable *const mdb =
Ian Romanickdfdff942011-01-24 16:45:11 -0800412 add_variable(instructions, state->symbols,
413 "gl_MaxDrawBuffers", glsl_type::int_type, ir_var_auto, -1);
Ian Romanicke2f84f02010-06-29 15:19:11 -0700414
415 if (warn)
416 mdb->warn_extension = "GL_ARB_draw_buffers";
417
418 mdb->constant_value = new(mdb)
419 ir_constant(int(state->Const.MaxDrawBuffers));
420
Ian Romanickc77b2572010-04-07 16:59:46 -0700421
Ian Romanick22971e92010-06-29 15:29:56 -0700422 /* gl_FragData is only available in the fragment shader.
423 */
424 if (target == fragment_shader) {
425 const glsl_type *const vec4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700426 glsl_type::get_array_instance(glsl_type::vec4_type,
Ian Romanick22971e92010-06-29 15:29:56 -0700427 state->Const.MaxDrawBuffers);
Ian Romanickc77b2572010-04-07 16:59:46 -0700428
Ian Romanick22971e92010-06-29 15:29:56 -0700429 ir_variable *const fd =
Ian Romanickdfdff942011-01-24 16:45:11 -0800430 add_variable(instructions, state->symbols,
431 "gl_FragData", vec4_array_type,
432 ir_var_out, FRAG_RESULT_DATA0);
Ian Romanick22971e92010-06-29 15:29:56 -0700433
434 if (warn)
435 fd->warn_extension = "GL_ARB_draw_buffers";
436 }
Ian Romanickc77b2572010-04-07 16:59:46 -0700437}
438
Brian Paul7ce18632010-12-08 18:25:38 -0700439
440static void
441generate_ARB_draw_instanced_variables(exec_list *instructions,
442 struct _mesa_glsl_parse_state *state,
443 bool warn,
444 _mesa_glsl_parser_targets target)
445{
446 /* gl_InstanceIDARB is only available in the vertex shader.
447 */
448 if (target == vertex_shader) {
449 ir_variable *const inst =
Ian Romanickdfdff942011-01-24 16:45:11 -0800450 add_variable(instructions, state->symbols,
451 "gl_InstanceIDARB", glsl_type::int_type,
452 ir_var_system_value, SYSTEM_VALUE_INSTANCE_ID);
Brian Paul7ce18632010-12-08 18:25:38 -0700453
454 if (warn)
455 inst->warn_extension = "GL_ARB_draw_instanced";
456 }
457}
458
459
Dave Airlied9671862010-10-06 09:36:02 +1000460static void
461generate_ARB_shader_stencil_export_variables(exec_list *instructions,
462 struct _mesa_glsl_parse_state *state,
463 bool warn)
464{
465 /* gl_FragStencilRefARB is only available in the fragment shader.
466 */
467 ir_variable *const fd =
Ian Romanickdfdff942011-01-24 16:45:11 -0800468 add_variable(instructions, state->symbols,
469 "gl_FragStencilRefARB", glsl_type::int_type,
470 ir_var_out, FRAG_RESULT_STENCIL);
Dave Airlied9671862010-10-06 09:36:02 +1000471
472 if (warn)
473 fd->warn_extension = "GL_ARB_shader_stencil_export";
474}
Ian Romanickc77b2572010-04-07 16:59:46 -0700475
Eric Anholtb3f743a2010-03-25 14:48:25 -0700476static void
477generate_120_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700478 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700479{
Ian Romanick5e18b052010-06-29 14:21:05 -0700480 generate_110_fs_variables(instructions, state);
Eric Anholt152b55e2010-07-07 19:45:22 -0700481
482 for (unsigned i = 0
483 ; i < Elements(builtin_120_fs_variables)
484 ; i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800485 add_builtin_variable(instructions, state->symbols,
486 & builtin_120_fs_variables[i]);
Eric Anholt152b55e2010-07-07 19:45:22 -0700487 }
Eric Anholtb3f743a2010-03-25 14:48:25 -0700488}
489
490static void
491generate_130_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700492 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700493{
Ian Romanick5e18b052010-06-29 14:21:05 -0700494 generate_120_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700495
Ian Romanick8645a952010-04-07 16:47:44 -0700496 const glsl_type *const clip_distance_array_type =
Eric Anholt73df6362010-07-28 08:18:59 -0700497 glsl_type::get_array_instance(glsl_type::float_type,
498 state->Const.MaxClipPlanes);
Ian Romanicked0626e2010-06-21 11:42:57 -0700499
500 /* FINISHME: gl_ClipDistance needs a real location assigned. */
Ian Romanickdfdff942011-01-24 16:45:11 -0800501 add_variable(instructions, state->symbols,
502 "gl_ClipDistance", clip_distance_array_type, ir_var_in, -1);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700503}
504
505static void
506initialize_fs_variables(exec_list *instructions,
507 struct _mesa_glsl_parse_state *state)
508{
509
510 switch (state->language_version) {
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700511 case 100:
512 generate_100ES_fs_variables(instructions, state);
513 break;
Eric Anholtb3f743a2010-03-25 14:48:25 -0700514 case 110:
Ian Romanick5e18b052010-06-29 14:21:05 -0700515 generate_110_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700516 break;
517 case 120:
Ian Romanick5e18b052010-06-29 14:21:05 -0700518 generate_120_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700519 break;
520 case 130:
Ian Romanick5e18b052010-06-29 14:21:05 -0700521 generate_130_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700522 break;
523 }
Dave Airlied9671862010-10-06 09:36:02 +1000524
525 if (state->ARB_shader_stencil_export_enable)
526 generate_ARB_shader_stencil_export_variables(instructions, state,
527 state->ARB_shader_stencil_export_warn);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700528}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800529
530void
531_mesa_glsl_initialize_variables(exec_list *instructions,
532 struct _mesa_glsl_parse_state *state)
533{
534 switch (state->target) {
535 case vertex_shader:
536 initialize_vs_variables(instructions, state);
537 break;
538 case geometry_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700539 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800540 case fragment_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700541 initialize_fs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800542 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800543 }
544}