blob: 73da28faf4f8e664e64699590b410fdc0a95728f [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 Romanicked0626e2010-06-21 11:42:57 -070039add_variable(const char *name, enum ir_variable_mode mode, int slot,
Ian Romanick3f9a73d2010-04-02 11:59:57 -070040 const glsl_type *type, exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -070041 glsl_symbol_table *symtab)
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:
Eric Anholt046bef22010-08-04 20:33:57 -070048 case ir_var_uniform:
Brian Paul7ce18632010-12-08 18:25:38 -070049 case ir_var_system_value:
Ian Romanickadfb0cd2010-03-10 10:43:16 -080050 var->read_only = true;
Eric Anholt71df19f2010-04-19 11:10:37 -070051 break;
52 case ir_var_inout:
Eric Anholt71df19f2010-04-19 11:10:37 -070053 case ir_var_out:
Eric Anholt71df19f2010-04-19 11:10:37 -070054 break;
55 default:
56 assert(0);
57 break;
58 }
Ian Romanickadfb0cd2010-03-10 10:43:16 -080059
Ian Romanicked0626e2010-06-21 11:42:57 -070060 var->location = slot;
Ian Romanick68a4fc92010-10-07 17:21:22 -070061 var->explicit_location = (slot >= 0);
Ian Romanicked0626e2010-06-21 11:42:57 -070062
Ian Romanickadfb0cd2010-03-10 10:43:16 -080063 /* Once the variable is created an initialized, add it to the symbol table
64 * and add the declaration to the IR stream.
65 */
66 instructions->push_tail(var);
67
Eric Anholt001eee52010-11-05 06:11:24 -070068 symtab->add_variable(var);
Ian Romanickc77b2572010-04-07 16:59:46 -070069 return var;
Ian Romanickadfb0cd2010-03-10 10:43:16 -080070}
71
Eric Anholt85b5dba2010-07-28 12:23:51 -070072static ir_variable *
73add_uniform(exec_list *instructions,
74 struct _mesa_glsl_parse_state *state,
75 const char *name, const glsl_type *type)
76{
77 return add_variable(name, ir_var_uniform, -1, type, instructions,
78 state->symbols);
79}
Ian Romanick3f9a73d2010-04-02 11:59:57 -070080
81static void
82add_builtin_variable(const builtin_variable *proto, exec_list *instructions,
83 glsl_symbol_table *symtab)
84{
85 /* Create a new variable declaration from the description supplied by
86 * the caller.
87 */
88 const glsl_type *const type = symtab->get_type(proto->type);
89
90 assert(type != NULL);
91
Ian Romanicked0626e2010-06-21 11:42:57 -070092 add_variable(proto->name, proto->mode, proto->slot, type, instructions,
93 symtab);
Ian Romanick3f9a73d2010-04-02 11:59:57 -070094}
95
Eric Anholtf8946692010-07-20 14:03:35 -070096static void
97add_builtin_constant(exec_list *instructions,
98 struct _mesa_glsl_parse_state *state,
99 const char *name, int value)
100{
101 ir_variable *const var = add_variable(name, ir_var_auto,
102 -1, glsl_type::int_type,
103 instructions, state->symbols);
104 var->constant_value = new(var) ir_constant(value);
105}
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700106
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700107/* Several constants in GLSL ES have different names than normal desktop GLSL.
108 * Therefore, this function should only be called on the ES path.
109 */
110static void
111generate_100ES_uniforms(exec_list *instructions,
112 struct _mesa_glsl_parse_state *state)
113{
114 add_builtin_constant(instructions, state, "gl_MaxVertexAttribs",
115 state->Const.MaxVertexAttribs);
116 add_builtin_constant(instructions, state, "gl_MaxVertexUniformVectors",
117 state->Const.MaxVertexUniformComponents);
118 add_builtin_constant(instructions, state, "gl_MaxVaryingVectors",
119 state->Const.MaxVaryingFloats / 4);
120 add_builtin_constant(instructions, state, "gl_MaxVertexTextureImageUnits",
121 state->Const.MaxVertexTextureImageUnits);
122 add_builtin_constant(instructions, state, "gl_MaxCombinedTextureImageUnits",
123 state->Const.MaxCombinedTextureImageUnits);
124 add_builtin_constant(instructions, state, "gl_MaxTextureImageUnits",
125 state->Const.MaxTextureImageUnits);
126 add_builtin_constant(instructions, state, "gl_MaxFragmentUniformVectors",
127 state->Const.MaxFragmentUniformComponents);
128
129 add_uniform(instructions, state, "gl_DepthRange",
130 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{
137 for (unsigned i = 0
138 ; i < Elements(builtin_110_deprecated_uniforms)
139 ; i++) {
140 add_builtin_variable(& builtin_110_deprecated_uniforms[i],
Ian Romanick127308b2010-07-01 13:30:50 -0700141 instructions, state->symbols);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700142 }
143
Eric Anholtf8946692010-07-20 14:03:35 -0700144 add_builtin_constant(instructions, state, "gl_MaxLights",
145 state->Const.MaxLights);
146 add_builtin_constant(instructions, state, "gl_MaxClipPlanes",
147 state->Const.MaxClipPlanes);
148 add_builtin_constant(instructions, state, "gl_MaxTextureUnits",
149 state->Const.MaxTextureUnits);
150 add_builtin_constant(instructions, state, "gl_MaxTextureCoords",
151 state->Const.MaxTextureCoords);
152 add_builtin_constant(instructions, state, "gl_MaxVertexAttribs",
153 state->Const.MaxVertexAttribs);
154 add_builtin_constant(instructions, state, "gl_MaxVertexUniformComponents",
155 state->Const.MaxVertexUniformComponents);
156 add_builtin_constant(instructions, state, "gl_MaxVaryingFloats",
157 state->Const.MaxVaryingFloats);
158 add_builtin_constant(instructions, state, "gl_MaxVertexTextureImageUnits",
159 state->Const.MaxVertexTextureImageUnits);
160 add_builtin_constant(instructions, state, "gl_MaxCombinedTextureImageUnits",
161 state->Const.MaxCombinedTextureImageUnits);
162 add_builtin_constant(instructions, state, "gl_MaxTextureImageUnits",
163 state->Const.MaxTextureImageUnits);
164 add_builtin_constant(instructions, state, "gl_MaxFragmentUniformComponents",
165 state->Const.MaxFragmentUniformComponents);
Ian Romanick127308b2010-07-01 13:30:50 -0700166
Ian Romanick3eba5932010-04-26 14:59:32 -0700167 const glsl_type *const mat4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700168 glsl_type::get_array_instance(glsl_type::mat4_type,
Ian Romanick127308b2010-07-01 13:30:50 -0700169 state->Const.MaxTextureCoords);
Ian Romanick3eba5932010-04-26 14:59:32 -0700170
Eric Anholt85b5dba2010-07-28 12:23:51 -0700171 add_uniform(instructions, state, "gl_TextureMatrix", mat4_array_type);
Eric Anholtb5bb2152010-09-21 10:08:38 -0700172 add_uniform(instructions, state, "gl_TextureMatrixInverse", mat4_array_type);
173 add_uniform(instructions, state, "gl_TextureMatrixTranspose", mat4_array_type);
174 add_uniform(instructions, state, "gl_TextureMatrixInverseTranspose", mat4_array_type);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700175
Kenneth Graunkedbff7b52010-08-07 02:28:40 -0700176 add_uniform(instructions, state, "gl_DepthRange",
Eric Anholt85b5dba2010-07-28 12:23:51 -0700177 state->symbols->get_type("gl_DepthRangeParameters"));
Eric Anholt78fe3c92010-03-28 01:46:48 -0700178
Eric Anholt85b5dba2010-07-28 12:23:51 -0700179 add_uniform(instructions, state, "gl_ClipPlane",
180 glsl_type::get_array_instance(glsl_type::vec4_type,
181 state->Const.MaxClipPlanes));
182 add_uniform(instructions, state, "gl_Point",
183 state->symbols->get_type("gl_PointParameters"));
184
185 const glsl_type *const material_parameters_type =
186 state->symbols->get_type("gl_MaterialParameters");
187 add_uniform(instructions, state, "gl_FrontMaterial", material_parameters_type);
188 add_uniform(instructions, state, "gl_BackMaterial", material_parameters_type);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700189
Eric Anholtaa579432010-05-19 14:09:04 -0700190 const glsl_type *const light_source_array_type =
Eric Anholt73df6362010-07-28 08:18:59 -0700191 glsl_type::get_array_instance(state->symbols->get_type("gl_LightSourceParameters"), state->Const.MaxLights);
Eric Anholtaa579432010-05-19 14:09:04 -0700192
Eric Anholt85b5dba2010-07-28 12:23:51 -0700193 add_uniform(instructions, state, "gl_LightSource", light_source_array_type);
Eric Anholtaa579432010-05-19 14:09:04 -0700194
Eric Anholt85b5dba2010-07-28 12:23:51 -0700195 const glsl_type *const light_model_products_type =
196 state->symbols->get_type("gl_LightModelProducts");
197 add_uniform(instructions, state, "gl_FrontLightModelProduct",
198 light_model_products_type);
199 add_uniform(instructions, state, "gl_BackLightModelProduct",
200 light_model_products_type);
201
202 const glsl_type *const light_products_type =
203 glsl_type::get_array_instance(state->symbols->get_type("gl_LightProducts"),
204 state->Const.MaxLights);
205 add_uniform(instructions, state, "gl_FrontLightProduct", light_products_type);
206 add_uniform(instructions, state, "gl_BackLightProduct", light_products_type);
207
208 add_uniform(instructions, state, "gl_TextureEnvColor",
209 glsl_type::get_array_instance(glsl_type::vec4_type,
210 state->Const.MaxTextureUnits));
211
212 const glsl_type *const texcoords_vec4 =
213 glsl_type::get_array_instance(glsl_type::vec4_type,
214 state->Const.MaxTextureCoords);
215 add_uniform(instructions, state, "gl_EyePlaneS", texcoords_vec4);
216 add_uniform(instructions, state, "gl_EyePlaneT", texcoords_vec4);
217 add_uniform(instructions, state, "gl_EyePlaneR", texcoords_vec4);
218 add_uniform(instructions, state, "gl_EyePlaneQ", texcoords_vec4);
219 add_uniform(instructions, state, "gl_ObjectPlaneS", texcoords_vec4);
220 add_uniform(instructions, state, "gl_ObjectPlaneT", texcoords_vec4);
221 add_uniform(instructions, state, "gl_ObjectPlaneR", texcoords_vec4);
222 add_uniform(instructions, state, "gl_ObjectPlaneQ", texcoords_vec4);
223
224 add_uniform(instructions, state, "gl_Fog",
225 state->symbols->get_type("gl_FogParameters"));
Eric Anholt78fe3c92010-03-28 01:46:48 -0700226}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800227
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700228/* This function should only be called for ES, not desktop GL. */
229static void
230generate_100ES_vs_variables(exec_list *instructions,
231 struct _mesa_glsl_parse_state *state)
232{
233 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
234 add_builtin_variable(& builtin_core_vs_variables[i],
235 instructions, state->symbols);
236 }
237
238 generate_100ES_uniforms(instructions, state);
239
240 generate_ARB_draw_buffers_variables(instructions, state, false,
241 vertex_shader);
242}
243
244
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800245static void
246generate_110_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700247 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800248{
249 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
250 add_builtin_variable(& builtin_core_vs_variables[i],
Ian Romanick22971e92010-06-29 15:29:56 -0700251 instructions, state->symbols);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800252 }
253
254 for (unsigned i = 0
255 ; i < Elements(builtin_110_deprecated_vs_variables)
256 ; i++) {
257 add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
Ian Romanick22971e92010-06-29 15:29:56 -0700258 instructions, state->symbols);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800259 }
Ian Romanick127308b2010-07-01 13:30:50 -0700260 generate_110_uniforms(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800261
Ian Romanickcd00d5b2010-07-01 13:17:54 -0700262 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
263 *
264 * "As with all arrays, indices used to subscript gl_TexCoord must
265 * either be an integral constant expressions, or this array must be
266 * re-declared by the shader with a size. The size can be at most
267 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
268 * implementation in preserving varying resources."
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800269 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700270 const glsl_type *const vec4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700271 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700272
Ian Romanicked0626e2010-06-21 11:42:57 -0700273 add_variable("gl_TexCoord", ir_var_out, VERT_RESULT_TEX0, vec4_array_type,
Ian Romanick22971e92010-06-29 15:29:56 -0700274 instructions, state->symbols);
275
276 generate_ARB_draw_buffers_variables(instructions, state, false,
277 vertex_shader);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800278}
279
280
281static void
282generate_120_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700283 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800284{
285 /* GLSL version 1.20 did not add any built-in variables in the vertex
286 * shader.
287 */
Ian Romanick22971e92010-06-29 15:29:56 -0700288 generate_110_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800289}
290
291
292static void
293generate_130_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700294 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800295{
Ian Romanick22971e92010-06-29 15:29:56 -0700296 generate_120_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800297
298 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
299 add_builtin_variable(& builtin_130_vs_variables[i],
Ian Romanick22971e92010-06-29 15:29:56 -0700300 instructions, state->symbols);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800301 }
302
Eric Anholt271e1992010-04-02 23:47:06 -0700303 const glsl_type *const clip_distance_array_type =
Eric Anholt73df6362010-07-28 08:18:59 -0700304 glsl_type::get_array_instance(glsl_type::float_type,
305 state->Const.MaxClipPlanes);
Ian Romanicked0626e2010-06-21 11:42:57 -0700306
307 /* FINISHME: gl_ClipDistance needs a real location assigned. */
308 add_variable("gl_ClipDistance", ir_var_out, -1, clip_distance_array_type,
Ian Romanick22971e92010-06-29 15:29:56 -0700309 instructions, state->symbols);
Eric Anholt271e1992010-04-02 23:47:06 -0700310
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800311}
312
313
314static void
315initialize_vs_variables(exec_list *instructions,
316 struct _mesa_glsl_parse_state *state)
317{
318
319 switch (state->language_version) {
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700320 case 100:
321 generate_100ES_vs_variables(instructions, state);
322 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800323 case 110:
Ian Romanick22971e92010-06-29 15:29:56 -0700324 generate_110_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800325 break;
326 case 120:
Ian Romanick22971e92010-06-29 15:29:56 -0700327 generate_120_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800328 break;
329 case 130:
Ian Romanick22971e92010-06-29 15:29:56 -0700330 generate_130_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800331 break;
332 }
Brian Paul7ce18632010-12-08 18:25:38 -0700333
334 if (state->ARB_draw_instanced_enable)
335 generate_ARB_draw_instanced_variables(instructions, state, false,
336 vertex_shader);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800337}
338
Brian Paul7ce18632010-12-08 18:25:38 -0700339
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700340/* This function should only be called for ES, not desktop GL. */
341static void
342generate_100ES_fs_variables(exec_list *instructions,
343 struct _mesa_glsl_parse_state *state)
344{
345 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
346 add_builtin_variable(& builtin_core_fs_variables[i],
347 instructions, state->symbols);
348 }
349
350 for (unsigned i = 0; i < Elements(builtin_100ES_fs_variables); i++) {
351 add_builtin_variable(& builtin_100ES_fs_variables[i],
352 instructions, state->symbols);
353 }
354
355 generate_100ES_uniforms(instructions, state);
356
357 generate_ARB_draw_buffers_variables(instructions, state, false,
358 fragment_shader);
359}
360
Eric Anholtb3f743a2010-03-25 14:48:25 -0700361static void
362generate_110_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700363 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700364{
365 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
366 add_builtin_variable(& builtin_core_fs_variables[i],
Ian Romanick5e18b052010-06-29 14:21:05 -0700367 instructions, state->symbols);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700368 }
369
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700370 for (unsigned i = 0; i < Elements(builtin_110_fs_variables); i++) {
371 add_builtin_variable(& builtin_110_fs_variables[i],
372 instructions, state->symbols);
373 }
374
Eric Anholt0f09aea2010-03-27 12:48:57 -0700375 for (unsigned i = 0
376 ; i < Elements(builtin_110_deprecated_fs_variables)
377 ; i++) {
378 add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
Ian Romanick5e18b052010-06-29 14:21:05 -0700379 instructions, state->symbols);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700380 }
Ian Romanick127308b2010-07-01 13:30:50 -0700381 generate_110_uniforms(instructions, state);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700382
Ian Romanickcd00d5b2010-07-01 13:17:54 -0700383 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
384 *
385 * "As with all arrays, indices used to subscript gl_TexCoord must
386 * either be an integral constant expressions, or this array must be
387 * re-declared by the shader with a size. The size can be at most
388 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
389 * implementation in preserving varying resources."
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700390 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700391 const glsl_type *const vec4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700392 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700393
Ian Romanicked0626e2010-06-21 11:42:57 -0700394 add_variable("gl_TexCoord", ir_var_in, FRAG_ATTRIB_TEX0, vec4_array_type,
Ian Romanick5e18b052010-06-29 14:21:05 -0700395 instructions, state->symbols);
Ian Romanick9c4b1f22010-06-29 15:10:09 -0700396
Ian Romanick22971e92010-06-29 15:29:56 -0700397 generate_ARB_draw_buffers_variables(instructions, state, false,
398 fragment_shader);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700399}
400
Ian Romanickc77b2572010-04-07 16:59:46 -0700401
402static void
Ian Romanick22971e92010-06-29 15:29:56 -0700403generate_ARB_draw_buffers_variables(exec_list *instructions,
404 struct _mesa_glsl_parse_state *state,
405 bool warn, _mesa_glsl_parser_targets target)
Ian Romanickc77b2572010-04-07 16:59:46 -0700406{
Ian Romanick22971e92010-06-29 15:29:56 -0700407 /* gl_MaxDrawBuffers is available in all shader stages.
408 */
Ian Romanicke2f84f02010-06-29 15:19:11 -0700409 ir_variable *const mdb =
410 add_variable("gl_MaxDrawBuffers", ir_var_auto, -1,
411 glsl_type::int_type, instructions, state->symbols);
412
413 if (warn)
414 mdb->warn_extension = "GL_ARB_draw_buffers";
415
416 mdb->constant_value = new(mdb)
417 ir_constant(int(state->Const.MaxDrawBuffers));
418
Ian Romanickc77b2572010-04-07 16:59:46 -0700419
Ian Romanick22971e92010-06-29 15:29:56 -0700420 /* gl_FragData is only available in the fragment shader.
421 */
422 if (target == fragment_shader) {
423 const glsl_type *const vec4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700424 glsl_type::get_array_instance(glsl_type::vec4_type,
Ian Romanick22971e92010-06-29 15:29:56 -0700425 state->Const.MaxDrawBuffers);
Ian Romanickc77b2572010-04-07 16:59:46 -0700426
Ian Romanick22971e92010-06-29 15:29:56 -0700427 ir_variable *const fd =
428 add_variable("gl_FragData", ir_var_out, FRAG_RESULT_DATA0,
429 vec4_array_type, instructions, state->symbols);
430
431 if (warn)
432 fd->warn_extension = "GL_ARB_draw_buffers";
433 }
Ian Romanickc77b2572010-04-07 16:59:46 -0700434}
435
Brian Paul7ce18632010-12-08 18:25:38 -0700436
437static void
438generate_ARB_draw_instanced_variables(exec_list *instructions,
439 struct _mesa_glsl_parse_state *state,
440 bool warn,
441 _mesa_glsl_parser_targets target)
442{
443 /* gl_InstanceIDARB is only available in the vertex shader.
444 */
445 if (target == vertex_shader) {
446 ir_variable *const inst =
447 add_variable("gl_InstanceIDARB", ir_var_system_value,
448 SYSTEM_VALUE_INSTANCE_ID,
449 glsl_type::int_type, instructions, state->symbols);
450
451 if (warn)
452 inst->warn_extension = "GL_ARB_draw_instanced";
453 }
454}
455
456
Dave Airlied9671862010-10-06 09:36:02 +1000457static void
458generate_ARB_shader_stencil_export_variables(exec_list *instructions,
459 struct _mesa_glsl_parse_state *state,
460 bool warn)
461{
462 /* gl_FragStencilRefARB is only available in the fragment shader.
463 */
464 ir_variable *const fd =
465 add_variable("gl_FragStencilRefARB", ir_var_out, FRAG_RESULT_STENCIL,
466 glsl_type::int_type, instructions, state->symbols);
467
468 if (warn)
469 fd->warn_extension = "GL_ARB_shader_stencil_export";
470}
Ian Romanickc77b2572010-04-07 16:59:46 -0700471
Eric Anholtb3f743a2010-03-25 14:48:25 -0700472static void
473generate_120_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700474 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700475{
Ian Romanick5e18b052010-06-29 14:21:05 -0700476 generate_110_fs_variables(instructions, state);
Eric Anholt152b55e2010-07-07 19:45:22 -0700477
478 for (unsigned i = 0
479 ; i < Elements(builtin_120_fs_variables)
480 ; i++) {
481 add_builtin_variable(& builtin_120_fs_variables[i],
482 instructions, state->symbols);
483 }
Eric Anholtb3f743a2010-03-25 14:48:25 -0700484}
485
486static void
487generate_130_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700488 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700489{
Ian Romanick5e18b052010-06-29 14:21:05 -0700490 generate_120_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700491
Ian Romanick8645a952010-04-07 16:47:44 -0700492 const glsl_type *const clip_distance_array_type =
Eric Anholt73df6362010-07-28 08:18:59 -0700493 glsl_type::get_array_instance(glsl_type::float_type,
494 state->Const.MaxClipPlanes);
Ian Romanicked0626e2010-06-21 11:42:57 -0700495
496 /* FINISHME: gl_ClipDistance needs a real location assigned. */
497 add_variable("gl_ClipDistance", ir_var_in, -1, clip_distance_array_type,
Ian Romanick5e18b052010-06-29 14:21:05 -0700498 instructions, state->symbols);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700499}
500
501static void
502initialize_fs_variables(exec_list *instructions,
503 struct _mesa_glsl_parse_state *state)
504{
505
506 switch (state->language_version) {
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700507 case 100:
508 generate_100ES_fs_variables(instructions, state);
509 break;
Eric Anholtb3f743a2010-03-25 14:48:25 -0700510 case 110:
Ian Romanick5e18b052010-06-29 14:21:05 -0700511 generate_110_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700512 break;
513 case 120:
Ian Romanick5e18b052010-06-29 14:21:05 -0700514 generate_120_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700515 break;
516 case 130:
Ian Romanick5e18b052010-06-29 14:21:05 -0700517 generate_130_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700518 break;
519 }
Dave Airlied9671862010-10-06 09:36:02 +1000520
521 if (state->ARB_shader_stencil_export_enable)
522 generate_ARB_shader_stencil_export_variables(instructions, state,
523 state->ARB_shader_stencil_export_warn);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700524}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800525
526void
527_mesa_glsl_initialize_variables(exec_list *instructions,
528 struct _mesa_glsl_parse_state *state)
529{
530 switch (state->target) {
531 case vertex_shader:
532 initialize_vs_variables(instructions, state);
533 break;
534 case geometry_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700535 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800536 case fragment_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700537 initialize_fs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800538 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800539 }
540}