blob: 18a3e0fb0d9e120c0d2f1f0537708bd2c91e739b [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:
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 *
74add_uniform(exec_list *instructions,
75 struct _mesa_glsl_parse_state *state,
76 const char *name, const glsl_type *type)
77{
78 return add_variable(name, ir_var_uniform, -1, type, instructions,
79 state->symbols);
80}
Ian Romanick3f9a73d2010-04-02 11:59:57 -070081
82static void
83add_builtin_variable(const builtin_variable *proto, exec_list *instructions,
84 glsl_symbol_table *symtab)
85{
86 /* Create a new variable declaration from the description supplied by
87 * the caller.
88 */
89 const glsl_type *const type = symtab->get_type(proto->type);
90
91 assert(type != NULL);
92
Ian Romanicked0626e2010-06-21 11:42:57 -070093 add_variable(proto->name, proto->mode, proto->slot, type, instructions,
94 symtab);
Ian Romanick3f9a73d2010-04-02 11:59:57 -070095}
96
Eric Anholtf8946692010-07-20 14:03:35 -070097static void
98add_builtin_constant(exec_list *instructions,
99 struct _mesa_glsl_parse_state *state,
100 const char *name, int value)
101{
102 ir_variable *const var = add_variable(name, ir_var_auto,
103 -1, glsl_type::int_type,
104 instructions, state->symbols);
105 var->constant_value = new(var) ir_constant(value);
106}
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700107
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700108/* Several constants in GLSL ES have different names than normal desktop GLSL.
109 * Therefore, this function should only be called on the ES path.
110 */
111static void
112generate_100ES_uniforms(exec_list *instructions,
113 struct _mesa_glsl_parse_state *state)
114{
115 add_builtin_constant(instructions, state, "gl_MaxVertexAttribs",
116 state->Const.MaxVertexAttribs);
117 add_builtin_constant(instructions, state, "gl_MaxVertexUniformVectors",
118 state->Const.MaxVertexUniformComponents);
119 add_builtin_constant(instructions, state, "gl_MaxVaryingVectors",
120 state->Const.MaxVaryingFloats / 4);
121 add_builtin_constant(instructions, state, "gl_MaxVertexTextureImageUnits",
122 state->Const.MaxVertexTextureImageUnits);
123 add_builtin_constant(instructions, state, "gl_MaxCombinedTextureImageUnits",
124 state->Const.MaxCombinedTextureImageUnits);
125 add_builtin_constant(instructions, state, "gl_MaxTextureImageUnits",
126 state->Const.MaxTextureImageUnits);
127 add_builtin_constant(instructions, state, "gl_MaxFragmentUniformVectors",
128 state->Const.MaxFragmentUniformComponents);
129
130 add_uniform(instructions, state, "gl_DepthRange",
131 state->symbols->get_type("gl_DepthRangeParameters"));
132}
133
Eric Anholt78fe3c92010-03-28 01:46:48 -0700134static void
135generate_110_uniforms(exec_list *instructions,
Ian Romanick127308b2010-07-01 13:30:50 -0700136 struct _mesa_glsl_parse_state *state)
Eric Anholt78fe3c92010-03-28 01:46:48 -0700137{
138 for (unsigned i = 0
139 ; i < Elements(builtin_110_deprecated_uniforms)
140 ; i++) {
141 add_builtin_variable(& builtin_110_deprecated_uniforms[i],
Ian Romanick127308b2010-07-01 13:30:50 -0700142 instructions, state->symbols);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700143 }
144
Eric Anholtf8946692010-07-20 14:03:35 -0700145 add_builtin_constant(instructions, state, "gl_MaxLights",
146 state->Const.MaxLights);
147 add_builtin_constant(instructions, state, "gl_MaxClipPlanes",
148 state->Const.MaxClipPlanes);
149 add_builtin_constant(instructions, state, "gl_MaxTextureUnits",
150 state->Const.MaxTextureUnits);
151 add_builtin_constant(instructions, state, "gl_MaxTextureCoords",
152 state->Const.MaxTextureCoords);
153 add_builtin_constant(instructions, state, "gl_MaxVertexAttribs",
154 state->Const.MaxVertexAttribs);
155 add_builtin_constant(instructions, state, "gl_MaxVertexUniformComponents",
156 state->Const.MaxVertexUniformComponents);
157 add_builtin_constant(instructions, state, "gl_MaxVaryingFloats",
158 state->Const.MaxVaryingFloats);
159 add_builtin_constant(instructions, state, "gl_MaxVertexTextureImageUnits",
160 state->Const.MaxVertexTextureImageUnits);
161 add_builtin_constant(instructions, state, "gl_MaxCombinedTextureImageUnits",
162 state->Const.MaxCombinedTextureImageUnits);
163 add_builtin_constant(instructions, state, "gl_MaxTextureImageUnits",
164 state->Const.MaxTextureImageUnits);
165 add_builtin_constant(instructions, state, "gl_MaxFragmentUniformComponents",
166 state->Const.MaxFragmentUniformComponents);
Ian Romanick127308b2010-07-01 13:30:50 -0700167
Ian Romanick3eba5932010-04-26 14:59:32 -0700168 const glsl_type *const mat4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700169 glsl_type::get_array_instance(glsl_type::mat4_type,
Ian Romanick127308b2010-07-01 13:30:50 -0700170 state->Const.MaxTextureCoords);
Ian Romanick3eba5932010-04-26 14:59:32 -0700171
Eric Anholt85b5dba2010-07-28 12:23:51 -0700172 add_uniform(instructions, state, "gl_TextureMatrix", mat4_array_type);
Eric Anholtb5bb2152010-09-21 10:08:38 -0700173 add_uniform(instructions, state, "gl_TextureMatrixInverse", mat4_array_type);
174 add_uniform(instructions, state, "gl_TextureMatrixTranspose", mat4_array_type);
175 add_uniform(instructions, state, "gl_TextureMatrixInverseTranspose", mat4_array_type);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700176
Kenneth Graunkedbff7b52010-08-07 02:28:40 -0700177 add_uniform(instructions, state, "gl_DepthRange",
Eric Anholt85b5dba2010-07-28 12:23:51 -0700178 state->symbols->get_type("gl_DepthRangeParameters"));
Eric Anholt78fe3c92010-03-28 01:46:48 -0700179
Eric Anholt85b5dba2010-07-28 12:23:51 -0700180 add_uniform(instructions, state, "gl_ClipPlane",
181 glsl_type::get_array_instance(glsl_type::vec4_type,
182 state->Const.MaxClipPlanes));
183 add_uniform(instructions, state, "gl_Point",
184 state->symbols->get_type("gl_PointParameters"));
185
186 const glsl_type *const material_parameters_type =
187 state->symbols->get_type("gl_MaterialParameters");
188 add_uniform(instructions, state, "gl_FrontMaterial", material_parameters_type);
189 add_uniform(instructions, state, "gl_BackMaterial", material_parameters_type);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700190
Eric Anholtaa579432010-05-19 14:09:04 -0700191 const glsl_type *const light_source_array_type =
Eric Anholt73df6362010-07-28 08:18:59 -0700192 glsl_type::get_array_instance(state->symbols->get_type("gl_LightSourceParameters"), state->Const.MaxLights);
Eric Anholtaa579432010-05-19 14:09:04 -0700193
Eric Anholt85b5dba2010-07-28 12:23:51 -0700194 add_uniform(instructions, state, "gl_LightSource", light_source_array_type);
Eric Anholtaa579432010-05-19 14:09:04 -0700195
Eric Anholt85b5dba2010-07-28 12:23:51 -0700196 const glsl_type *const light_model_products_type =
197 state->symbols->get_type("gl_LightModelProducts");
198 add_uniform(instructions, state, "gl_FrontLightModelProduct",
199 light_model_products_type);
200 add_uniform(instructions, state, "gl_BackLightModelProduct",
201 light_model_products_type);
202
203 const glsl_type *const light_products_type =
204 glsl_type::get_array_instance(state->symbols->get_type("gl_LightProducts"),
205 state->Const.MaxLights);
206 add_uniform(instructions, state, "gl_FrontLightProduct", light_products_type);
207 add_uniform(instructions, state, "gl_BackLightProduct", light_products_type);
208
209 add_uniform(instructions, state, "gl_TextureEnvColor",
210 glsl_type::get_array_instance(glsl_type::vec4_type,
211 state->Const.MaxTextureUnits));
212
213 const glsl_type *const texcoords_vec4 =
214 glsl_type::get_array_instance(glsl_type::vec4_type,
215 state->Const.MaxTextureCoords);
216 add_uniform(instructions, state, "gl_EyePlaneS", texcoords_vec4);
217 add_uniform(instructions, state, "gl_EyePlaneT", texcoords_vec4);
218 add_uniform(instructions, state, "gl_EyePlaneR", texcoords_vec4);
219 add_uniform(instructions, state, "gl_EyePlaneQ", texcoords_vec4);
220 add_uniform(instructions, state, "gl_ObjectPlaneS", texcoords_vec4);
221 add_uniform(instructions, state, "gl_ObjectPlaneT", texcoords_vec4);
222 add_uniform(instructions, state, "gl_ObjectPlaneR", texcoords_vec4);
223 add_uniform(instructions, state, "gl_ObjectPlaneQ", texcoords_vec4);
224
225 add_uniform(instructions, state, "gl_Fog",
226 state->symbols->get_type("gl_FogParameters"));
Eric Anholt78fe3c92010-03-28 01:46:48 -0700227}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800228
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700229/* This function should only be called for ES, not desktop GL. */
230static void
231generate_100ES_vs_variables(exec_list *instructions,
232 struct _mesa_glsl_parse_state *state)
233{
234 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
235 add_builtin_variable(& builtin_core_vs_variables[i],
236 instructions, state->symbols);
237 }
238
239 generate_100ES_uniforms(instructions, state);
240
241 generate_ARB_draw_buffers_variables(instructions, state, false,
242 vertex_shader);
243}
244
245
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800246static void
247generate_110_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700248 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800249{
250 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
251 add_builtin_variable(& builtin_core_vs_variables[i],
Ian Romanick22971e92010-06-29 15:29:56 -0700252 instructions, state->symbols);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800253 }
254
255 for (unsigned i = 0
256 ; i < Elements(builtin_110_deprecated_vs_variables)
257 ; i++) {
258 add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
Ian Romanick22971e92010-06-29 15:29:56 -0700259 instructions, state->symbols);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800260 }
Ian Romanick127308b2010-07-01 13:30:50 -0700261 generate_110_uniforms(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800262
Ian Romanickcd00d5b2010-07-01 13:17:54 -0700263 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
264 *
265 * "As with all arrays, indices used to subscript gl_TexCoord must
266 * either be an integral constant expressions, or this array must be
267 * re-declared by the shader with a size. The size can be at most
268 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
269 * implementation in preserving varying resources."
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800270 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700271 const glsl_type *const vec4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700272 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700273
Ian Romanicked0626e2010-06-21 11:42:57 -0700274 add_variable("gl_TexCoord", ir_var_out, VERT_RESULT_TEX0, vec4_array_type,
Ian Romanick22971e92010-06-29 15:29:56 -0700275 instructions, state->symbols);
276
277 generate_ARB_draw_buffers_variables(instructions, state, false,
278 vertex_shader);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800279}
280
281
282static void
283generate_120_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700284 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800285{
286 /* GLSL version 1.20 did not add any built-in variables in the vertex
287 * shader.
288 */
Ian Romanick22971e92010-06-29 15:29:56 -0700289 generate_110_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800290}
291
292
293static void
294generate_130_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700295 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800296{
Ian Romanick22971e92010-06-29 15:29:56 -0700297 generate_120_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800298
299 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
300 add_builtin_variable(& builtin_130_vs_variables[i],
Ian Romanick22971e92010-06-29 15:29:56 -0700301 instructions, state->symbols);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800302 }
303
Eric Anholt271e1992010-04-02 23:47:06 -0700304 const glsl_type *const clip_distance_array_type =
Eric Anholt73df6362010-07-28 08:18:59 -0700305 glsl_type::get_array_instance(glsl_type::float_type,
306 state->Const.MaxClipPlanes);
Ian Romanicked0626e2010-06-21 11:42:57 -0700307
308 /* FINISHME: gl_ClipDistance needs a real location assigned. */
309 add_variable("gl_ClipDistance", ir_var_out, -1, clip_distance_array_type,
Ian Romanick22971e92010-06-29 15:29:56 -0700310 instructions, state->symbols);
Eric Anholt271e1992010-04-02 23:47:06 -0700311
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800312}
313
314
315static void
316initialize_vs_variables(exec_list *instructions,
317 struct _mesa_glsl_parse_state *state)
318{
319
320 switch (state->language_version) {
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700321 case 100:
322 generate_100ES_vs_variables(instructions, state);
323 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800324 case 110:
Ian Romanick22971e92010-06-29 15:29:56 -0700325 generate_110_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800326 break;
327 case 120:
Ian Romanick22971e92010-06-29 15:29:56 -0700328 generate_120_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800329 break;
330 case 130:
Ian Romanick22971e92010-06-29 15:29:56 -0700331 generate_130_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800332 break;
333 }
Brian Paul7ce18632010-12-08 18:25:38 -0700334
335 if (state->ARB_draw_instanced_enable)
336 generate_ARB_draw_instanced_variables(instructions, state, false,
337 vertex_shader);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800338}
339
Brian Paul7ce18632010-12-08 18:25:38 -0700340
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700341/* This function should only be called for ES, not desktop GL. */
342static void
343generate_100ES_fs_variables(exec_list *instructions,
344 struct _mesa_glsl_parse_state *state)
345{
346 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
347 add_builtin_variable(& builtin_core_fs_variables[i],
348 instructions, state->symbols);
349 }
350
351 for (unsigned i = 0; i < Elements(builtin_100ES_fs_variables); i++) {
352 add_builtin_variable(& builtin_100ES_fs_variables[i],
353 instructions, state->symbols);
354 }
355
356 generate_100ES_uniforms(instructions, state);
357
358 generate_ARB_draw_buffers_variables(instructions, state, false,
359 fragment_shader);
360}
361
Eric Anholtb3f743a2010-03-25 14:48:25 -0700362static void
363generate_110_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700364 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700365{
366 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
367 add_builtin_variable(& builtin_core_fs_variables[i],
Ian Romanick5e18b052010-06-29 14:21:05 -0700368 instructions, state->symbols);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700369 }
370
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700371 for (unsigned i = 0; i < Elements(builtin_110_fs_variables); i++) {
372 add_builtin_variable(& builtin_110_fs_variables[i],
373 instructions, state->symbols);
374 }
375
Eric Anholt0f09aea2010-03-27 12:48:57 -0700376 for (unsigned i = 0
377 ; i < Elements(builtin_110_deprecated_fs_variables)
378 ; i++) {
379 add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
Ian Romanick5e18b052010-06-29 14:21:05 -0700380 instructions, state->symbols);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700381 }
Ian Romanick127308b2010-07-01 13:30:50 -0700382 generate_110_uniforms(instructions, state);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700383
Ian Romanickcd00d5b2010-07-01 13:17:54 -0700384 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
385 *
386 * "As with all arrays, indices used to subscript gl_TexCoord must
387 * either be an integral constant expressions, or this array must be
388 * re-declared by the shader with a size. The size can be at most
389 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
390 * implementation in preserving varying resources."
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700391 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700392 const glsl_type *const vec4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700393 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700394
Ian Romanicked0626e2010-06-21 11:42:57 -0700395 add_variable("gl_TexCoord", ir_var_in, FRAG_ATTRIB_TEX0, vec4_array_type,
Ian Romanick5e18b052010-06-29 14:21:05 -0700396 instructions, state->symbols);
Ian Romanick9c4b1f22010-06-29 15:10:09 -0700397
Ian Romanick22971e92010-06-29 15:29:56 -0700398 generate_ARB_draw_buffers_variables(instructions, state, false,
399 fragment_shader);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700400}
401
Ian Romanickc77b2572010-04-07 16:59:46 -0700402
403static void
Ian Romanick22971e92010-06-29 15:29:56 -0700404generate_ARB_draw_buffers_variables(exec_list *instructions,
405 struct _mesa_glsl_parse_state *state,
406 bool warn, _mesa_glsl_parser_targets target)
Ian Romanickc77b2572010-04-07 16:59:46 -0700407{
Ian Romanick22971e92010-06-29 15:29:56 -0700408 /* gl_MaxDrawBuffers is available in all shader stages.
409 */
Ian Romanicke2f84f02010-06-29 15:19:11 -0700410 ir_variable *const mdb =
411 add_variable("gl_MaxDrawBuffers", ir_var_auto, -1,
412 glsl_type::int_type, instructions, state->symbols);
413
414 if (warn)
415 mdb->warn_extension = "GL_ARB_draw_buffers";
416
417 mdb->constant_value = new(mdb)
418 ir_constant(int(state->Const.MaxDrawBuffers));
419
Ian Romanickc77b2572010-04-07 16:59:46 -0700420
Ian Romanick22971e92010-06-29 15:29:56 -0700421 /* gl_FragData is only available in the fragment shader.
422 */
423 if (target == fragment_shader) {
424 const glsl_type *const vec4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700425 glsl_type::get_array_instance(glsl_type::vec4_type,
Ian Romanick22971e92010-06-29 15:29:56 -0700426 state->Const.MaxDrawBuffers);
Ian Romanickc77b2572010-04-07 16:59:46 -0700427
Ian Romanick22971e92010-06-29 15:29:56 -0700428 ir_variable *const fd =
429 add_variable("gl_FragData", ir_var_out, FRAG_RESULT_DATA0,
430 vec4_array_type, instructions, state->symbols);
431
432 if (warn)
433 fd->warn_extension = "GL_ARB_draw_buffers";
434 }
Ian Romanickc77b2572010-04-07 16:59:46 -0700435}
436
Brian Paul7ce18632010-12-08 18:25:38 -0700437
438static void
439generate_ARB_draw_instanced_variables(exec_list *instructions,
440 struct _mesa_glsl_parse_state *state,
441 bool warn,
442 _mesa_glsl_parser_targets target)
443{
444 /* gl_InstanceIDARB is only available in the vertex shader.
445 */
446 if (target == vertex_shader) {
447 ir_variable *const inst =
448 add_variable("gl_InstanceIDARB", ir_var_system_value,
449 SYSTEM_VALUE_INSTANCE_ID,
450 glsl_type::int_type, instructions, state->symbols);
451
452 if (warn)
453 inst->warn_extension = "GL_ARB_draw_instanced";
454 }
455}
456
457
Dave Airlied9671862010-10-06 09:36:02 +1000458static void
459generate_ARB_shader_stencil_export_variables(exec_list *instructions,
460 struct _mesa_glsl_parse_state *state,
461 bool warn)
462{
463 /* gl_FragStencilRefARB is only available in the fragment shader.
464 */
465 ir_variable *const fd =
466 add_variable("gl_FragStencilRefARB", ir_var_out, FRAG_RESULT_STENCIL,
467 glsl_type::int_type, instructions, state->symbols);
468
469 if (warn)
470 fd->warn_extension = "GL_ARB_shader_stencil_export";
471}
Ian Romanickc77b2572010-04-07 16:59:46 -0700472
Eric Anholtb3f743a2010-03-25 14:48:25 -0700473static void
474generate_120_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700475 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700476{
Ian Romanick5e18b052010-06-29 14:21:05 -0700477 generate_110_fs_variables(instructions, state);
Eric Anholt152b55e2010-07-07 19:45:22 -0700478
479 for (unsigned i = 0
480 ; i < Elements(builtin_120_fs_variables)
481 ; i++) {
482 add_builtin_variable(& builtin_120_fs_variables[i],
483 instructions, state->symbols);
484 }
Eric Anholtb3f743a2010-03-25 14:48:25 -0700485}
486
487static void
488generate_130_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700489 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700490{
Ian Romanick5e18b052010-06-29 14:21:05 -0700491 generate_120_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700492
Ian Romanick8645a952010-04-07 16:47:44 -0700493 const glsl_type *const clip_distance_array_type =
Eric Anholt73df6362010-07-28 08:18:59 -0700494 glsl_type::get_array_instance(glsl_type::float_type,
495 state->Const.MaxClipPlanes);
Ian Romanicked0626e2010-06-21 11:42:57 -0700496
497 /* FINISHME: gl_ClipDistance needs a real location assigned. */
498 add_variable("gl_ClipDistance", ir_var_in, -1, clip_distance_array_type,
Ian Romanick5e18b052010-06-29 14:21:05 -0700499 instructions, state->symbols);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700500}
501
502static void
503initialize_fs_variables(exec_list *instructions,
504 struct _mesa_glsl_parse_state *state)
505{
506
507 switch (state->language_version) {
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700508 case 100:
509 generate_100ES_fs_variables(instructions, state);
510 break;
Eric Anholtb3f743a2010-03-25 14:48:25 -0700511 case 110:
Ian Romanick5e18b052010-06-29 14:21:05 -0700512 generate_110_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700513 break;
514 case 120:
Ian Romanick5e18b052010-06-29 14:21:05 -0700515 generate_120_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700516 break;
517 case 130:
Ian Romanick5e18b052010-06-29 14:21:05 -0700518 generate_130_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700519 break;
520 }
Dave Airlied9671862010-10-06 09:36:02 +1000521
522 if (state->ARB_shader_stencil_export_enable)
523 generate_ARB_shader_stencil_export_variables(instructions, state,
524 state->ARB_shader_stencil_export_warn);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700525}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800526
527void
528_mesa_glsl_initialize_variables(exec_list *instructions,
529 struct _mesa_glsl_parse_state *state)
530{
531 switch (state->target) {
532 case vertex_shader:
533 initialize_vs_variables(instructions, state);
534 break;
535 case geometry_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700536 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800537 case fragment_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700538 initialize_fs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800539 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800540 }
541}