blob: ddc3bb0b9f391e3f516843ccd385f0074882d26b [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
Ian Romanickc77b2572010-04-07 16:59:46 -070033static ir_variable *
Ian Romanicked0626e2010-06-21 11:42:57 -070034add_variable(const char *name, enum ir_variable_mode mode, int slot,
Ian Romanick3f9a73d2010-04-02 11:59:57 -070035 const glsl_type *type, exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -070036 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -080037{
Ian Romanick7e2aa912010-07-19 17:12:42 -070038 ir_variable *var = new(symtab) ir_variable(type, name, mode);
Ian Romanickadfb0cd2010-03-10 10:43:16 -080039
Eric Anholt71df19f2010-04-19 11:10:37 -070040 switch (var->mode) {
Ian Romanicke2f84f02010-06-29 15:19:11 -070041 case ir_var_auto:
Eric Anholt71df19f2010-04-19 11:10:37 -070042 case ir_var_in:
Eric Anholt046bef22010-08-04 20:33:57 -070043 case ir_var_uniform:
Ian Romanickadfb0cd2010-03-10 10:43:16 -080044 var->read_only = true;
Eric Anholt71df19f2010-04-19 11:10:37 -070045 break;
46 case ir_var_inout:
Eric Anholt71df19f2010-04-19 11:10:37 -070047 case ir_var_out:
Eric Anholt71df19f2010-04-19 11:10:37 -070048 break;
49 default:
50 assert(0);
51 break;
52 }
Ian Romanickadfb0cd2010-03-10 10:43:16 -080053
Ian Romanicked0626e2010-06-21 11:42:57 -070054 var->location = slot;
Ian Romanick68a4fc92010-10-07 17:21:22 -070055 var->explicit_location = (slot >= 0);
Ian Romanicked0626e2010-06-21 11:42:57 -070056
Ian Romanickadfb0cd2010-03-10 10:43:16 -080057 /* Once the variable is created an initialized, add it to the symbol table
58 * and add the declaration to the IR stream.
59 */
60 instructions->push_tail(var);
61
Ian Romanick8bde4ce2010-03-19 11:57:24 -070062 symtab->add_variable(var->name, var);
Ian Romanickc77b2572010-04-07 16:59:46 -070063 return var;
Ian Romanickadfb0cd2010-03-10 10:43:16 -080064}
65
Eric Anholt85b5dba2010-07-28 12:23:51 -070066static ir_variable *
67add_uniform(exec_list *instructions,
68 struct _mesa_glsl_parse_state *state,
69 const char *name, const glsl_type *type)
70{
71 return add_variable(name, ir_var_uniform, -1, type, instructions,
72 state->symbols);
73}
Ian Romanick3f9a73d2010-04-02 11:59:57 -070074
75static void
76add_builtin_variable(const builtin_variable *proto, exec_list *instructions,
77 glsl_symbol_table *symtab)
78{
79 /* Create a new variable declaration from the description supplied by
80 * the caller.
81 */
82 const glsl_type *const type = symtab->get_type(proto->type);
83
84 assert(type != NULL);
85
Ian Romanicked0626e2010-06-21 11:42:57 -070086 add_variable(proto->name, proto->mode, proto->slot, type, instructions,
87 symtab);
Ian Romanick3f9a73d2010-04-02 11:59:57 -070088}
89
Eric Anholtf8946692010-07-20 14:03:35 -070090static void
91add_builtin_constant(exec_list *instructions,
92 struct _mesa_glsl_parse_state *state,
93 const char *name, int value)
94{
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);
99}
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700100
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700101/* Several constants in GLSL ES have different names than normal desktop GLSL.
102 * Therefore, this function should only be called on the ES path.
103 */
104static void
105generate_100ES_uniforms(exec_list *instructions,
106 struct _mesa_glsl_parse_state *state)
107{
108 add_builtin_constant(instructions, state, "gl_MaxVertexAttribs",
109 state->Const.MaxVertexAttribs);
110 add_builtin_constant(instructions, state, "gl_MaxVertexUniformVectors",
111 state->Const.MaxVertexUniformComponents);
112 add_builtin_constant(instructions, state, "gl_MaxVaryingVectors",
113 state->Const.MaxVaryingFloats / 4);
114 add_builtin_constant(instructions, state, "gl_MaxVertexTextureImageUnits",
115 state->Const.MaxVertexTextureImageUnits);
116 add_builtin_constant(instructions, state, "gl_MaxCombinedTextureImageUnits",
117 state->Const.MaxCombinedTextureImageUnits);
118 add_builtin_constant(instructions, state, "gl_MaxTextureImageUnits",
119 state->Const.MaxTextureImageUnits);
120 add_builtin_constant(instructions, state, "gl_MaxFragmentUniformVectors",
121 state->Const.MaxFragmentUniformComponents);
122
123 add_uniform(instructions, state, "gl_DepthRange",
124 state->symbols->get_type("gl_DepthRangeParameters"));
125}
126
Eric Anholt78fe3c92010-03-28 01:46:48 -0700127static void
128generate_110_uniforms(exec_list *instructions,
Ian Romanick127308b2010-07-01 13:30:50 -0700129 struct _mesa_glsl_parse_state *state)
Eric Anholt78fe3c92010-03-28 01:46:48 -0700130{
131 for (unsigned i = 0
132 ; i < Elements(builtin_110_deprecated_uniforms)
133 ; i++) {
134 add_builtin_variable(& builtin_110_deprecated_uniforms[i],
Ian Romanick127308b2010-07-01 13:30:50 -0700135 instructions, state->symbols);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700136 }
137
Eric Anholtf8946692010-07-20 14:03:35 -0700138 add_builtin_constant(instructions, state, "gl_MaxLights",
139 state->Const.MaxLights);
140 add_builtin_constant(instructions, state, "gl_MaxClipPlanes",
141 state->Const.MaxClipPlanes);
142 add_builtin_constant(instructions, state, "gl_MaxTextureUnits",
143 state->Const.MaxTextureUnits);
144 add_builtin_constant(instructions, state, "gl_MaxTextureCoords",
145 state->Const.MaxTextureCoords);
146 add_builtin_constant(instructions, state, "gl_MaxVertexAttribs",
147 state->Const.MaxVertexAttribs);
148 add_builtin_constant(instructions, state, "gl_MaxVertexUniformComponents",
149 state->Const.MaxVertexUniformComponents);
150 add_builtin_constant(instructions, state, "gl_MaxVaryingFloats",
151 state->Const.MaxVaryingFloats);
152 add_builtin_constant(instructions, state, "gl_MaxVertexTextureImageUnits",
153 state->Const.MaxVertexTextureImageUnits);
154 add_builtin_constant(instructions, state, "gl_MaxCombinedTextureImageUnits",
155 state->Const.MaxCombinedTextureImageUnits);
156 add_builtin_constant(instructions, state, "gl_MaxTextureImageUnits",
157 state->Const.MaxTextureImageUnits);
158 add_builtin_constant(instructions, state, "gl_MaxFragmentUniformComponents",
159 state->Const.MaxFragmentUniformComponents);
Ian Romanick127308b2010-07-01 13:30:50 -0700160
Ian Romanick3eba5932010-04-26 14:59:32 -0700161 const glsl_type *const mat4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700162 glsl_type::get_array_instance(glsl_type::mat4_type,
Ian Romanick127308b2010-07-01 13:30:50 -0700163 state->Const.MaxTextureCoords);
Ian Romanick3eba5932010-04-26 14:59:32 -0700164
Eric Anholt85b5dba2010-07-28 12:23:51 -0700165 add_uniform(instructions, state, "gl_TextureMatrix", mat4_array_type);
Eric Anholtb5bb2152010-09-21 10:08:38 -0700166 add_uniform(instructions, state, "gl_TextureMatrixInverse", mat4_array_type);
167 add_uniform(instructions, state, "gl_TextureMatrixTranspose", mat4_array_type);
168 add_uniform(instructions, state, "gl_TextureMatrixInverseTranspose", mat4_array_type);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700169
Kenneth Graunkedbff7b52010-08-07 02:28:40 -0700170 add_uniform(instructions, state, "gl_DepthRange",
Eric Anholt85b5dba2010-07-28 12:23:51 -0700171 state->symbols->get_type("gl_DepthRangeParameters"));
Eric Anholt78fe3c92010-03-28 01:46:48 -0700172
Eric Anholt85b5dba2010-07-28 12:23:51 -0700173 add_uniform(instructions, state, "gl_ClipPlane",
174 glsl_type::get_array_instance(glsl_type::vec4_type,
175 state->Const.MaxClipPlanes));
176 add_uniform(instructions, state, "gl_Point",
177 state->symbols->get_type("gl_PointParameters"));
178
179 const glsl_type *const material_parameters_type =
180 state->symbols->get_type("gl_MaterialParameters");
181 add_uniform(instructions, state, "gl_FrontMaterial", material_parameters_type);
182 add_uniform(instructions, state, "gl_BackMaterial", material_parameters_type);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700183
Eric Anholtaa579432010-05-19 14:09:04 -0700184 const glsl_type *const light_source_array_type =
Eric Anholt73df6362010-07-28 08:18:59 -0700185 glsl_type::get_array_instance(state->symbols->get_type("gl_LightSourceParameters"), state->Const.MaxLights);
Eric Anholtaa579432010-05-19 14:09:04 -0700186
Eric Anholt85b5dba2010-07-28 12:23:51 -0700187 add_uniform(instructions, state, "gl_LightSource", light_source_array_type);
Eric Anholtaa579432010-05-19 14:09:04 -0700188
Eric Anholt85b5dba2010-07-28 12:23:51 -0700189 const glsl_type *const light_model_products_type =
190 state->symbols->get_type("gl_LightModelProducts");
191 add_uniform(instructions, state, "gl_FrontLightModelProduct",
192 light_model_products_type);
193 add_uniform(instructions, state, "gl_BackLightModelProduct",
194 light_model_products_type);
195
196 const glsl_type *const light_products_type =
197 glsl_type::get_array_instance(state->symbols->get_type("gl_LightProducts"),
198 state->Const.MaxLights);
199 add_uniform(instructions, state, "gl_FrontLightProduct", light_products_type);
200 add_uniform(instructions, state, "gl_BackLightProduct", light_products_type);
201
202 add_uniform(instructions, state, "gl_TextureEnvColor",
203 glsl_type::get_array_instance(glsl_type::vec4_type,
204 state->Const.MaxTextureUnits));
205
206 const glsl_type *const texcoords_vec4 =
207 glsl_type::get_array_instance(glsl_type::vec4_type,
208 state->Const.MaxTextureCoords);
209 add_uniform(instructions, state, "gl_EyePlaneS", texcoords_vec4);
210 add_uniform(instructions, state, "gl_EyePlaneT", texcoords_vec4);
211 add_uniform(instructions, state, "gl_EyePlaneR", texcoords_vec4);
212 add_uniform(instructions, state, "gl_EyePlaneQ", texcoords_vec4);
213 add_uniform(instructions, state, "gl_ObjectPlaneS", texcoords_vec4);
214 add_uniform(instructions, state, "gl_ObjectPlaneT", texcoords_vec4);
215 add_uniform(instructions, state, "gl_ObjectPlaneR", texcoords_vec4);
216 add_uniform(instructions, state, "gl_ObjectPlaneQ", texcoords_vec4);
217
218 add_uniform(instructions, state, "gl_Fog",
219 state->symbols->get_type("gl_FogParameters"));
Eric Anholt78fe3c92010-03-28 01:46:48 -0700220}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800221
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700222/* This function should only be called for ES, not desktop GL. */
223static void
224generate_100ES_vs_variables(exec_list *instructions,
225 struct _mesa_glsl_parse_state *state)
226{
227 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
228 add_builtin_variable(& builtin_core_vs_variables[i],
229 instructions, state->symbols);
230 }
231
232 generate_100ES_uniforms(instructions, state);
233
234 generate_ARB_draw_buffers_variables(instructions, state, false,
235 vertex_shader);
236}
237
238
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800239static void
240generate_110_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700241 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800242{
243 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
244 add_builtin_variable(& builtin_core_vs_variables[i],
Ian Romanick22971e92010-06-29 15:29:56 -0700245 instructions, state->symbols);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800246 }
247
248 for (unsigned i = 0
249 ; i < Elements(builtin_110_deprecated_vs_variables)
250 ; i++) {
251 add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
Ian Romanick22971e92010-06-29 15:29:56 -0700252 instructions, state->symbols);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800253 }
Ian Romanick127308b2010-07-01 13:30:50 -0700254 generate_110_uniforms(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800255
Ian Romanickcd00d5b2010-07-01 13:17:54 -0700256 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
257 *
258 * "As with all arrays, indices used to subscript gl_TexCoord must
259 * either be an integral constant expressions, or this array must be
260 * re-declared by the shader with a size. The size can be at most
261 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
262 * implementation in preserving varying resources."
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800263 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700264 const glsl_type *const vec4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700265 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700266
Ian Romanicked0626e2010-06-21 11:42:57 -0700267 add_variable("gl_TexCoord", ir_var_out, VERT_RESULT_TEX0, vec4_array_type,
Ian Romanick22971e92010-06-29 15:29:56 -0700268 instructions, state->symbols);
269
270 generate_ARB_draw_buffers_variables(instructions, state, false,
271 vertex_shader);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800272}
273
274
275static void
276generate_120_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700277 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800278{
279 /* GLSL version 1.20 did not add any built-in variables in the vertex
280 * shader.
281 */
Ian Romanick22971e92010-06-29 15:29:56 -0700282 generate_110_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800283}
284
285
286static void
287generate_130_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700288 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800289{
Ian Romanick22971e92010-06-29 15:29:56 -0700290 generate_120_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800291
292 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
293 add_builtin_variable(& builtin_130_vs_variables[i],
Ian Romanick22971e92010-06-29 15:29:56 -0700294 instructions, state->symbols);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800295 }
296
Eric Anholt271e1992010-04-02 23:47:06 -0700297 const glsl_type *const clip_distance_array_type =
Eric Anholt73df6362010-07-28 08:18:59 -0700298 glsl_type::get_array_instance(glsl_type::float_type,
299 state->Const.MaxClipPlanes);
Ian Romanicked0626e2010-06-21 11:42:57 -0700300
301 /* FINISHME: gl_ClipDistance needs a real location assigned. */
302 add_variable("gl_ClipDistance", ir_var_out, -1, clip_distance_array_type,
Ian Romanick22971e92010-06-29 15:29:56 -0700303 instructions, state->symbols);
Eric Anholt271e1992010-04-02 23:47:06 -0700304
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800305}
306
307
308static void
309initialize_vs_variables(exec_list *instructions,
310 struct _mesa_glsl_parse_state *state)
311{
312
313 switch (state->language_version) {
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700314 case 100:
315 generate_100ES_vs_variables(instructions, state);
316 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800317 case 110:
Ian Romanick22971e92010-06-29 15:29:56 -0700318 generate_110_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800319 break;
320 case 120:
Ian Romanick22971e92010-06-29 15:29:56 -0700321 generate_120_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800322 break;
323 case 130:
Ian Romanick22971e92010-06-29 15:29:56 -0700324 generate_130_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800325 break;
326 }
327}
328
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700329/* This function should only be called for ES, not desktop GL. */
330static void
331generate_100ES_fs_variables(exec_list *instructions,
332 struct _mesa_glsl_parse_state *state)
333{
334 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
335 add_builtin_variable(& builtin_core_fs_variables[i],
336 instructions, state->symbols);
337 }
338
339 for (unsigned i = 0; i < Elements(builtin_100ES_fs_variables); i++) {
340 add_builtin_variable(& builtin_100ES_fs_variables[i],
341 instructions, state->symbols);
342 }
343
344 generate_100ES_uniforms(instructions, state);
345
346 generate_ARB_draw_buffers_variables(instructions, state, false,
347 fragment_shader);
348}
349
Eric Anholtb3f743a2010-03-25 14:48:25 -0700350static void
351generate_110_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700352 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700353{
354 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
355 add_builtin_variable(& builtin_core_fs_variables[i],
Ian Romanick5e18b052010-06-29 14:21:05 -0700356 instructions, state->symbols);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700357 }
358
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700359 for (unsigned i = 0; i < Elements(builtin_110_fs_variables); i++) {
360 add_builtin_variable(& builtin_110_fs_variables[i],
361 instructions, state->symbols);
362 }
363
Eric Anholt0f09aea2010-03-27 12:48:57 -0700364 for (unsigned i = 0
365 ; i < Elements(builtin_110_deprecated_fs_variables)
366 ; i++) {
367 add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
Ian Romanick5e18b052010-06-29 14:21:05 -0700368 instructions, state->symbols);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700369 }
Ian Romanick127308b2010-07-01 13:30:50 -0700370 generate_110_uniforms(instructions, state);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700371
Ian Romanickcd00d5b2010-07-01 13:17:54 -0700372 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
373 *
374 * "As with all arrays, indices used to subscript gl_TexCoord must
375 * either be an integral constant expressions, or this array must be
376 * re-declared by the shader with a size. The size can be at most
377 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
378 * implementation in preserving varying resources."
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700379 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700380 const glsl_type *const vec4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700381 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700382
Ian Romanicked0626e2010-06-21 11:42:57 -0700383 add_variable("gl_TexCoord", ir_var_in, FRAG_ATTRIB_TEX0, vec4_array_type,
Ian Romanick5e18b052010-06-29 14:21:05 -0700384 instructions, state->symbols);
Ian Romanick9c4b1f22010-06-29 15:10:09 -0700385
Ian Romanick22971e92010-06-29 15:29:56 -0700386 generate_ARB_draw_buffers_variables(instructions, state, false,
387 fragment_shader);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700388}
389
Ian Romanickc77b2572010-04-07 16:59:46 -0700390
391static void
Ian Romanick22971e92010-06-29 15:29:56 -0700392generate_ARB_draw_buffers_variables(exec_list *instructions,
393 struct _mesa_glsl_parse_state *state,
394 bool warn, _mesa_glsl_parser_targets target)
Ian Romanickc77b2572010-04-07 16:59:46 -0700395{
Ian Romanick22971e92010-06-29 15:29:56 -0700396 /* gl_MaxDrawBuffers is available in all shader stages.
397 */
Ian Romanicke2f84f02010-06-29 15:19:11 -0700398 ir_variable *const mdb =
399 add_variable("gl_MaxDrawBuffers", ir_var_auto, -1,
400 glsl_type::int_type, instructions, state->symbols);
401
402 if (warn)
403 mdb->warn_extension = "GL_ARB_draw_buffers";
404
405 mdb->constant_value = new(mdb)
406 ir_constant(int(state->Const.MaxDrawBuffers));
407
Ian Romanickc77b2572010-04-07 16:59:46 -0700408
Ian Romanick22971e92010-06-29 15:29:56 -0700409 /* gl_FragData is only available in the fragment shader.
410 */
411 if (target == fragment_shader) {
412 const glsl_type *const vec4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700413 glsl_type::get_array_instance(glsl_type::vec4_type,
Ian Romanick22971e92010-06-29 15:29:56 -0700414 state->Const.MaxDrawBuffers);
Ian Romanickc77b2572010-04-07 16:59:46 -0700415
Ian Romanick22971e92010-06-29 15:29:56 -0700416 ir_variable *const fd =
417 add_variable("gl_FragData", ir_var_out, FRAG_RESULT_DATA0,
418 vec4_array_type, instructions, state->symbols);
419
420 if (warn)
421 fd->warn_extension = "GL_ARB_draw_buffers";
422 }
Ian Romanickc77b2572010-04-07 16:59:46 -0700423}
424
425
Eric Anholtb3f743a2010-03-25 14:48:25 -0700426static void
427generate_120_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700428 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700429{
Ian Romanick5e18b052010-06-29 14:21:05 -0700430 generate_110_fs_variables(instructions, state);
Eric Anholt152b55e2010-07-07 19:45:22 -0700431
432 for (unsigned i = 0
433 ; i < Elements(builtin_120_fs_variables)
434 ; i++) {
435 add_builtin_variable(& builtin_120_fs_variables[i],
436 instructions, state->symbols);
437 }
Eric Anholtb3f743a2010-03-25 14:48:25 -0700438}
439
440static void
441generate_130_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700442 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700443{
Ian Romanick5e18b052010-06-29 14:21:05 -0700444 generate_120_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700445
Ian Romanick8645a952010-04-07 16:47:44 -0700446 const glsl_type *const clip_distance_array_type =
Eric Anholt73df6362010-07-28 08:18:59 -0700447 glsl_type::get_array_instance(glsl_type::float_type,
448 state->Const.MaxClipPlanes);
Ian Romanicked0626e2010-06-21 11:42:57 -0700449
450 /* FINISHME: gl_ClipDistance needs a real location assigned. */
451 add_variable("gl_ClipDistance", ir_var_in, -1, clip_distance_array_type,
Ian Romanick5e18b052010-06-29 14:21:05 -0700452 instructions, state->symbols);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700453}
454
455static void
456initialize_fs_variables(exec_list *instructions,
457 struct _mesa_glsl_parse_state *state)
458{
459
460 switch (state->language_version) {
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700461 case 100:
462 generate_100ES_fs_variables(instructions, state);
463 break;
Eric Anholtb3f743a2010-03-25 14:48:25 -0700464 case 110:
Ian Romanick5e18b052010-06-29 14:21:05 -0700465 generate_110_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700466 break;
467 case 120:
Ian Romanick5e18b052010-06-29 14:21:05 -0700468 generate_120_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700469 break;
470 case 130:
Ian Romanick5e18b052010-06-29 14:21:05 -0700471 generate_130_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700472 break;
473 }
474}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800475
476void
477_mesa_glsl_initialize_variables(exec_list *instructions,
478 struct _mesa_glsl_parse_state *state)
479{
480 switch (state->target) {
481 case vertex_shader:
482 initialize_vs_variables(instructions, state);
483 break;
484 case geometry_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700485 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800486 case fragment_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700487 initialize_fs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800488 break;
Eric Anholt81f49a72010-04-29 17:57:28 -0700489 case ir_shader:
490 fprintf(stderr, "ir reader has no builtin variables");
491 exit(1);
492 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800493 }
494}