blob: 8337e15b8891aaa242437c2d1209e954eb2b31db [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"
Ian Romanick92e412e2011-01-24 16:55:50 -080028#include "main/uniforms.h"
29#include "program/prog_parameter.h"
30#include "program/prog_statevars.h"
31#include "program/prog_instruction.h"
Ian Romanickadfb0cd2010-03-10 10:43:16 -080032
Ian Romanick22971e92010-06-29 15:29:56 -070033static void generate_ARB_draw_buffers_variables(exec_list *,
34 struct _mesa_glsl_parse_state *,
35 bool, _mesa_glsl_parser_targets);
Ian Romanick9c4b1f22010-06-29 15:10:09 -070036
Brian Paul7ce18632010-12-08 18:25:38 -070037static void
38generate_ARB_draw_instanced_variables(exec_list *,
39 struct _mesa_glsl_parse_state *,
40 bool, _mesa_glsl_parser_targets);
41
Ian Romanick92e412e2011-01-24 16:55:50 -080042static struct gl_builtin_uniform_element gl_DepthRange_elements[] = {
43 {"near", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_XXXX},
44 {"far", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_YYYY},
45 {"diff", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_ZZZZ},
46};
47
48static struct gl_builtin_uniform_element gl_ClipPlane_elements[] = {
49 {NULL, {STATE_CLIPPLANE, 0, 0}, SWIZZLE_XYZW}
50};
51
52static struct gl_builtin_uniform_element gl_Point_elements[] = {
53 {"size", {STATE_POINT_SIZE}, SWIZZLE_XXXX},
54 {"sizeMin", {STATE_POINT_SIZE}, SWIZZLE_YYYY},
55 {"sizeMax", {STATE_POINT_SIZE}, SWIZZLE_ZZZZ},
56 {"fadeThresholdSize", {STATE_POINT_SIZE}, SWIZZLE_WWWW},
57 {"distanceConstantAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_XXXX},
58 {"distanceLinearAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_YYYY},
59 {"distanceQuadraticAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_ZZZZ},
60};
61
62static struct gl_builtin_uniform_element gl_FrontMaterial_elements[] = {
63 {"emission", {STATE_MATERIAL, 0, STATE_EMISSION}, SWIZZLE_XYZW},
64 {"ambient", {STATE_MATERIAL, 0, STATE_AMBIENT}, SWIZZLE_XYZW},
65 {"diffuse", {STATE_MATERIAL, 0, STATE_DIFFUSE}, SWIZZLE_XYZW},
66 {"specular", {STATE_MATERIAL, 0, STATE_SPECULAR}, SWIZZLE_XYZW},
67 {"shininess", {STATE_MATERIAL, 0, STATE_SHININESS}, SWIZZLE_XXXX},
68};
69
70static struct gl_builtin_uniform_element gl_BackMaterial_elements[] = {
71 {"emission", {STATE_MATERIAL, 1, STATE_EMISSION}, SWIZZLE_XYZW},
72 {"ambient", {STATE_MATERIAL, 1, STATE_AMBIENT}, SWIZZLE_XYZW},
73 {"diffuse", {STATE_MATERIAL, 1, STATE_DIFFUSE}, SWIZZLE_XYZW},
74 {"specular", {STATE_MATERIAL, 1, STATE_SPECULAR}, SWIZZLE_XYZW},
75 {"shininess", {STATE_MATERIAL, 1, STATE_SHININESS}, SWIZZLE_XXXX},
76};
77
78static struct gl_builtin_uniform_element gl_LightSource_elements[] = {
79 {"ambient", {STATE_LIGHT, 0, STATE_AMBIENT}, SWIZZLE_XYZW},
80 {"diffuse", {STATE_LIGHT, 0, STATE_DIFFUSE}, SWIZZLE_XYZW},
81 {"specular", {STATE_LIGHT, 0, STATE_SPECULAR}, SWIZZLE_XYZW},
82 {"position", {STATE_LIGHT, 0, STATE_POSITION}, SWIZZLE_XYZW},
83 {"halfVector", {STATE_LIGHT, 0, STATE_HALF_VECTOR}, SWIZZLE_XYZW},
84 {"spotDirection", {STATE_LIGHT, 0, STATE_SPOT_DIRECTION},
85 MAKE_SWIZZLE4(SWIZZLE_X,
86 SWIZZLE_Y,
87 SWIZZLE_Z,
88 SWIZZLE_Z)},
89 {"spotCosCutoff", {STATE_LIGHT, 0, STATE_SPOT_DIRECTION}, SWIZZLE_WWWW},
90 {"spotCutoff", {STATE_LIGHT, 0, STATE_SPOT_CUTOFF}, SWIZZLE_XXXX},
91 {"spotExponent", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_WWWW},
92 {"constantAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_XXXX},
93 {"linearAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_YYYY},
94 {"quadraticAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_ZZZZ},
95};
96
97static struct gl_builtin_uniform_element gl_LightModel_elements[] = {
98 {"ambient", {STATE_LIGHTMODEL_AMBIENT, 0}, SWIZZLE_XYZW},
99};
100
101static struct gl_builtin_uniform_element gl_FrontLightModelProduct_elements[] = {
102 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR, 0}, SWIZZLE_XYZW},
103};
104
105static struct gl_builtin_uniform_element gl_BackLightModelProduct_elements[] = {
106 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR, 1}, SWIZZLE_XYZW},
107};
108
109static struct gl_builtin_uniform_element gl_FrontLightProduct_elements[] = {
110 {"ambient", {STATE_LIGHTPROD, 0, 0, STATE_AMBIENT}, SWIZZLE_XYZW},
111 {"diffuse", {STATE_LIGHTPROD, 0, 0, STATE_DIFFUSE}, SWIZZLE_XYZW},
112 {"specular", {STATE_LIGHTPROD, 0, 0, STATE_SPECULAR}, SWIZZLE_XYZW},
113};
114
115static struct gl_builtin_uniform_element gl_BackLightProduct_elements[] = {
116 {"ambient", {STATE_LIGHTPROD, 0, 1, STATE_AMBIENT}, SWIZZLE_XYZW},
117 {"diffuse", {STATE_LIGHTPROD, 0, 1, STATE_DIFFUSE}, SWIZZLE_XYZW},
118 {"specular", {STATE_LIGHTPROD, 0, 1, STATE_SPECULAR}, SWIZZLE_XYZW},
119};
120
121static struct gl_builtin_uniform_element gl_TextureEnvColor_elements[] = {
122 {NULL, {STATE_TEXENV_COLOR, 0}, SWIZZLE_XYZW},
123};
124
125static struct gl_builtin_uniform_element gl_EyePlaneS_elements[] = {
126 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_S}, SWIZZLE_XYZW},
127};
128
129static struct gl_builtin_uniform_element gl_EyePlaneT_elements[] = {
130 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_T}, SWIZZLE_XYZW},
131};
132
133static struct gl_builtin_uniform_element gl_EyePlaneR_elements[] = {
134 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_R}, SWIZZLE_XYZW},
135};
136
137static struct gl_builtin_uniform_element gl_EyePlaneQ_elements[] = {
138 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_Q}, SWIZZLE_XYZW},
139};
140
141static struct gl_builtin_uniform_element gl_ObjectPlaneS_elements[] = {
142 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_S}, SWIZZLE_XYZW},
143};
144
145static struct gl_builtin_uniform_element gl_ObjectPlaneT_elements[] = {
146 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_T}, SWIZZLE_XYZW},
147};
148
149static struct gl_builtin_uniform_element gl_ObjectPlaneR_elements[] = {
150 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_R}, SWIZZLE_XYZW},
151};
152
153static struct gl_builtin_uniform_element gl_ObjectPlaneQ_elements[] = {
154 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_Q}, SWIZZLE_XYZW},
155};
156
157static struct gl_builtin_uniform_element gl_Fog_elements[] = {
158 {"color", {STATE_FOG_COLOR}, SWIZZLE_XYZW},
159 {"density", {STATE_FOG_PARAMS}, SWIZZLE_XXXX},
160 {"start", {STATE_FOG_PARAMS}, SWIZZLE_YYYY},
161 {"end", {STATE_FOG_PARAMS}, SWIZZLE_ZZZZ},
162 {"scale", {STATE_FOG_PARAMS}, SWIZZLE_WWWW},
163};
164
165static struct gl_builtin_uniform_element gl_NormalScale_elements[] = {
166 {NULL, {STATE_NORMAL_SCALE}, SWIZZLE_XXXX},
167};
168
169static struct gl_builtin_uniform_element gl_MESABumpRotMatrix0_elements[] = {
170 {NULL, {STATE_INTERNAL, STATE_ROT_MATRIX_0}, SWIZZLE_XYZW},
171};
172
173static struct gl_builtin_uniform_element gl_MESABumpRotMatrix1_elements[] = {
174 {NULL, {STATE_INTERNAL, STATE_ROT_MATRIX_1}, SWIZZLE_XYZW},
175};
176
177static struct gl_builtin_uniform_element gl_MESAFogParamsOptimized_elements[] = {
178 {NULL, {STATE_INTERNAL, STATE_FOG_PARAMS_OPTIMIZED}, SWIZZLE_XYZW},
179};
180
Eric Anholtf868cb02011-07-16 17:41:26 -0700181static struct gl_builtin_uniform_element gl_CurrentAttribVertMESA_elements[] = {
182 {NULL, {STATE_INTERNAL, STATE_CURRENT_ATTRIB, 0}, SWIZZLE_XYZW},
183};
184
185static struct gl_builtin_uniform_element gl_CurrentAttribFragMESA_elements[] = {
186 {NULL, {STATE_INTERNAL, STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED, 0}, SWIZZLE_XYZW},
187};
188
Ian Romanick92e412e2011-01-24 16:55:50 -0800189#define MATRIX(name, statevar, modifier) \
190 static struct gl_builtin_uniform_element name ## _elements[] = { \
191 { NULL, { statevar, 0, 0, 0, modifier}, SWIZZLE_XYZW }, \
192 { NULL, { statevar, 0, 1, 1, modifier}, SWIZZLE_XYZW }, \
193 { NULL, { statevar, 0, 2, 2, modifier}, SWIZZLE_XYZW }, \
194 { NULL, { statevar, 0, 3, 3, modifier}, SWIZZLE_XYZW }, \
195 }
196
197MATRIX(gl_ModelViewMatrix,
198 STATE_MODELVIEW_MATRIX, STATE_MATRIX_TRANSPOSE);
199MATRIX(gl_ModelViewMatrixInverse,
200 STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVTRANS);
201MATRIX(gl_ModelViewMatrixTranspose,
202 STATE_MODELVIEW_MATRIX, 0);
203MATRIX(gl_ModelViewMatrixInverseTranspose,
204 STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVERSE);
205
206MATRIX(gl_ProjectionMatrix,
207 STATE_PROJECTION_MATRIX, STATE_MATRIX_TRANSPOSE);
208MATRIX(gl_ProjectionMatrixInverse,
209 STATE_PROJECTION_MATRIX, STATE_MATRIX_INVTRANS);
210MATRIX(gl_ProjectionMatrixTranspose,
211 STATE_PROJECTION_MATRIX, 0);
212MATRIX(gl_ProjectionMatrixInverseTranspose,
213 STATE_PROJECTION_MATRIX, STATE_MATRIX_INVERSE);
214
215MATRIX(gl_ModelViewProjectionMatrix,
216 STATE_MVP_MATRIX, STATE_MATRIX_TRANSPOSE);
217MATRIX(gl_ModelViewProjectionMatrixInverse,
218 STATE_MVP_MATRIX, STATE_MATRIX_INVTRANS);
219MATRIX(gl_ModelViewProjectionMatrixTranspose,
220 STATE_MVP_MATRIX, 0);
221MATRIX(gl_ModelViewProjectionMatrixInverseTranspose,
222 STATE_MVP_MATRIX, STATE_MATRIX_INVERSE);
223
224MATRIX(gl_TextureMatrix,
225 STATE_TEXTURE_MATRIX, STATE_MATRIX_TRANSPOSE);
226MATRIX(gl_TextureMatrixInverse,
227 STATE_TEXTURE_MATRIX, STATE_MATRIX_INVTRANS);
228MATRIX(gl_TextureMatrixTranspose,
229 STATE_TEXTURE_MATRIX, 0);
230MATRIX(gl_TextureMatrixInverseTranspose,
231 STATE_TEXTURE_MATRIX, STATE_MATRIX_INVERSE);
232
233static struct gl_builtin_uniform_element gl_NormalMatrix_elements[] = {
234 { NULL, { STATE_MODELVIEW_MATRIX, 0, 0, 0, STATE_MATRIX_INVERSE},
Eric Anholtcc4ddc32011-10-18 17:17:28 -0700235 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) },
Ian Romanick92e412e2011-01-24 16:55:50 -0800236 { NULL, { STATE_MODELVIEW_MATRIX, 0, 1, 1, STATE_MATRIX_INVERSE},
Eric Anholtcc4ddc32011-10-18 17:17:28 -0700237 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) },
Ian Romanick92e412e2011-01-24 16:55:50 -0800238 { NULL, { STATE_MODELVIEW_MATRIX, 0, 2, 2, STATE_MATRIX_INVERSE},
Eric Anholtcc4ddc32011-10-18 17:17:28 -0700239 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) },
Ian Romanick92e412e2011-01-24 16:55:50 -0800240};
241
242#undef MATRIX
243
244#define STATEVAR(name) {#name, name ## _elements, Elements(name ## _elements)}
245
246const struct gl_builtin_uniform_desc _mesa_builtin_uniform_desc[] = {
247 STATEVAR(gl_DepthRange),
248 STATEVAR(gl_ClipPlane),
249 STATEVAR(gl_Point),
250 STATEVAR(gl_FrontMaterial),
251 STATEVAR(gl_BackMaterial),
252 STATEVAR(gl_LightSource),
253 STATEVAR(gl_LightModel),
254 STATEVAR(gl_FrontLightModelProduct),
255 STATEVAR(gl_BackLightModelProduct),
256 STATEVAR(gl_FrontLightProduct),
257 STATEVAR(gl_BackLightProduct),
258 STATEVAR(gl_TextureEnvColor),
259 STATEVAR(gl_EyePlaneS),
260 STATEVAR(gl_EyePlaneT),
261 STATEVAR(gl_EyePlaneR),
262 STATEVAR(gl_EyePlaneQ),
263 STATEVAR(gl_ObjectPlaneS),
264 STATEVAR(gl_ObjectPlaneT),
265 STATEVAR(gl_ObjectPlaneR),
266 STATEVAR(gl_ObjectPlaneQ),
267 STATEVAR(gl_Fog),
268
269 STATEVAR(gl_ModelViewMatrix),
270 STATEVAR(gl_ModelViewMatrixInverse),
271 STATEVAR(gl_ModelViewMatrixTranspose),
272 STATEVAR(gl_ModelViewMatrixInverseTranspose),
273
274 STATEVAR(gl_ProjectionMatrix),
275 STATEVAR(gl_ProjectionMatrixInverse),
276 STATEVAR(gl_ProjectionMatrixTranspose),
277 STATEVAR(gl_ProjectionMatrixInverseTranspose),
278
279 STATEVAR(gl_ModelViewProjectionMatrix),
280 STATEVAR(gl_ModelViewProjectionMatrixInverse),
281 STATEVAR(gl_ModelViewProjectionMatrixTranspose),
282 STATEVAR(gl_ModelViewProjectionMatrixInverseTranspose),
283
284 STATEVAR(gl_TextureMatrix),
285 STATEVAR(gl_TextureMatrixInverse),
286 STATEVAR(gl_TextureMatrixTranspose),
287 STATEVAR(gl_TextureMatrixInverseTranspose),
288
289 STATEVAR(gl_NormalMatrix),
290 STATEVAR(gl_NormalScale),
291
292 STATEVAR(gl_MESABumpRotMatrix0),
293 STATEVAR(gl_MESABumpRotMatrix1),
294 STATEVAR(gl_MESAFogParamsOptimized),
Eric Anholtf868cb02011-07-16 17:41:26 -0700295 STATEVAR(gl_CurrentAttribVertMESA),
296 STATEVAR(gl_CurrentAttribFragMESA),
Ian Romanick92e412e2011-01-24 16:55:50 -0800297
298 {NULL, NULL, 0}
299};
300
Ian Romanickc77b2572010-04-07 16:59:46 -0700301static ir_variable *
Ian Romanickdfdff942011-01-24 16:45:11 -0800302add_variable(exec_list *instructions, glsl_symbol_table *symtab,
303 const char *name, const glsl_type *type,
304 enum ir_variable_mode mode, int slot)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800305{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700306 ir_variable *var = new(symtab) ir_variable(type, name, mode);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800307
Eric Anholt71df19f2010-04-19 11:10:37 -0700308 switch (var->mode) {
Ian Romanicke2f84f02010-06-29 15:19:11 -0700309 case ir_var_auto:
Eric Anholt71df19f2010-04-19 11:10:37 -0700310 case ir_var_in:
Kenneth Graunke819d57f2011-01-12 15:37:37 -0800311 case ir_var_const_in:
Eric Anholt046bef22010-08-04 20:33:57 -0700312 case ir_var_uniform:
Brian Paul7ce18632010-12-08 18:25:38 -0700313 case ir_var_system_value:
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800314 var->read_only = true;
Eric Anholt71df19f2010-04-19 11:10:37 -0700315 break;
316 case ir_var_inout:
Eric Anholt71df19f2010-04-19 11:10:37 -0700317 case ir_var_out:
Eric Anholt71df19f2010-04-19 11:10:37 -0700318 break;
319 default:
320 assert(0);
321 break;
322 }
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800323
Ian Romanicked0626e2010-06-21 11:42:57 -0700324 var->location = slot;
Ian Romanick68a4fc92010-10-07 17:21:22 -0700325 var->explicit_location = (slot >= 0);
Ian Romanicked0626e2010-06-21 11:42:57 -0700326
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800327 /* Once the variable is created an initialized, add it to the symbol table
328 * and add the declaration to the IR stream.
329 */
330 instructions->push_tail(var);
331
Eric Anholt001eee52010-11-05 06:11:24 -0700332 symtab->add_variable(var);
Ian Romanickc77b2572010-04-07 16:59:46 -0700333 return var;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800334}
335
Eric Anholt85b5dba2010-07-28 12:23:51 -0700336static ir_variable *
Ian Romanickdfdff942011-01-24 16:45:11 -0800337add_uniform(exec_list *instructions, glsl_symbol_table *symtab,
Eric Anholt85b5dba2010-07-28 12:23:51 -0700338 const char *name, const glsl_type *type)
339{
Ian Romanick89d81ab2011-01-25 10:41:20 -0800340 ir_variable *const uni =
341 add_variable(instructions, symtab, name, type, ir_var_uniform, -1);
342
343 unsigned i;
344 for (i = 0; _mesa_builtin_uniform_desc[i].name != NULL; i++) {
345 if (strcmp(_mesa_builtin_uniform_desc[i].name, name) == 0) {
346 break;
347 }
348 }
349
350 assert(_mesa_builtin_uniform_desc[i].name != NULL);
351 const struct gl_builtin_uniform_desc* const statevar =
352 &_mesa_builtin_uniform_desc[i];
353
354 const unsigned array_count = type->is_array() ? type->length : 1;
355 uni->num_state_slots = array_count * statevar->num_elements;
356
357 ir_state_slot *slots =
358 ralloc_array(uni, ir_state_slot, uni->num_state_slots);
359
360 uni->state_slots = slots;
361
362 for (unsigned a = 0; a < array_count; a++) {
363 for (unsigned j = 0; j < statevar->num_elements; j++) {
364 struct gl_builtin_uniform_element *element = &statevar->elements[j];
365
366 memcpy(slots->tokens, element->tokens, sizeof(element->tokens));
367 if (type->is_array()) {
Eric Anholtf868cb02011-07-16 17:41:26 -0700368 if (strcmp(name, "gl_CurrentAttribVertMESA") == 0 ||
369 strcmp(name, "gl_CurrentAttribFragMESA") == 0) {
370 slots->tokens[2] = a;
371 } else {
372 slots->tokens[1] = a;
373 }
Ian Romanick89d81ab2011-01-25 10:41:20 -0800374 }
375
376 slots->swizzle = element->swizzle;
377 slots++;
378 }
379 }
380
381 return uni;
Eric Anholt85b5dba2010-07-28 12:23:51 -0700382}
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700383
384static void
Ian Romanickdfdff942011-01-24 16:45:11 -0800385add_builtin_variable(exec_list *instructions, glsl_symbol_table *symtab,
386 const builtin_variable *proto)
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700387{
388 /* Create a new variable declaration from the description supplied by
389 * the caller.
390 */
391 const glsl_type *const type = symtab->get_type(proto->type);
392
393 assert(type != NULL);
394
Ian Romanick89d81ab2011-01-25 10:41:20 -0800395 if (proto->mode == ir_var_uniform) {
396 add_uniform(instructions, symtab, proto->name, type);
397 } else {
398 add_variable(instructions, symtab, proto->name, type, proto->mode,
399 proto->slot);
400 }
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700401}
402
Eric Anholtf8946692010-07-20 14:03:35 -0700403static void
Ian Romanickdfdff942011-01-24 16:45:11 -0800404add_builtin_constant(exec_list *instructions, glsl_symbol_table *symtab,
Eric Anholtf8946692010-07-20 14:03:35 -0700405 const char *name, int value)
406{
Ian Romanickdfdff942011-01-24 16:45:11 -0800407 ir_variable *const var = add_variable(instructions, symtab,
408 name, glsl_type::int_type,
409 ir_var_auto, -1);
Eric Anholtf8946692010-07-20 14:03:35 -0700410 var->constant_value = new(var) ir_constant(value);
411}
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700412
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700413/* Several constants in GLSL ES have different names than normal desktop GLSL.
414 * Therefore, this function should only be called on the ES path.
415 */
416static void
417generate_100ES_uniforms(exec_list *instructions,
418 struct _mesa_glsl_parse_state *state)
419{
Ian Romanickdfdff942011-01-24 16:45:11 -0800420 glsl_symbol_table *const symtab = state->symbols;
421
422 add_builtin_constant(instructions, symtab, "gl_MaxVertexAttribs",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700423 state->Const.MaxVertexAttribs);
Ian Romanickdfdff942011-01-24 16:45:11 -0800424 add_builtin_constant(instructions, symtab, "gl_MaxVertexUniformVectors",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700425 state->Const.MaxVertexUniformComponents);
Ian Romanickdfdff942011-01-24 16:45:11 -0800426 add_builtin_constant(instructions, symtab, "gl_MaxVaryingVectors",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700427 state->Const.MaxVaryingFloats / 4);
Ian Romanickdfdff942011-01-24 16:45:11 -0800428 add_builtin_constant(instructions, symtab, "gl_MaxVertexTextureImageUnits",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700429 state->Const.MaxVertexTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800430 add_builtin_constant(instructions, symtab, "gl_MaxCombinedTextureImageUnits",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700431 state->Const.MaxCombinedTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800432 add_builtin_constant(instructions, symtab, "gl_MaxTextureImageUnits",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700433 state->Const.MaxTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800434 add_builtin_constant(instructions, symtab, "gl_MaxFragmentUniformVectors",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700435 state->Const.MaxFragmentUniformComponents);
436
Ian Romanickdfdff942011-01-24 16:45:11 -0800437 add_uniform(instructions, symtab, "gl_DepthRange",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700438 state->symbols->get_type("gl_DepthRangeParameters"));
439}
440
Eric Anholt78fe3c92010-03-28 01:46:48 -0700441static void
442generate_110_uniforms(exec_list *instructions,
Ian Romanick127308b2010-07-01 13:30:50 -0700443 struct _mesa_glsl_parse_state *state)
Eric Anholt78fe3c92010-03-28 01:46:48 -0700444{
Ian Romanickdfdff942011-01-24 16:45:11 -0800445 glsl_symbol_table *const symtab = state->symbols;
446
Eric Anholt78fe3c92010-03-28 01:46:48 -0700447 for (unsigned i = 0
448 ; i < Elements(builtin_110_deprecated_uniforms)
449 ; i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800450 add_builtin_variable(instructions, symtab,
451 & builtin_110_deprecated_uniforms[i]);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700452 }
453
Ian Romanickdfdff942011-01-24 16:45:11 -0800454 add_builtin_constant(instructions, symtab, "gl_MaxLights",
Eric Anholtf8946692010-07-20 14:03:35 -0700455 state->Const.MaxLights);
Ian Romanickdfdff942011-01-24 16:45:11 -0800456 add_builtin_constant(instructions, symtab, "gl_MaxClipPlanes",
Eric Anholtf8946692010-07-20 14:03:35 -0700457 state->Const.MaxClipPlanes);
Ian Romanickdfdff942011-01-24 16:45:11 -0800458 add_builtin_constant(instructions, symtab, "gl_MaxTextureUnits",
Eric Anholtf8946692010-07-20 14:03:35 -0700459 state->Const.MaxTextureUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800460 add_builtin_constant(instructions, symtab, "gl_MaxTextureCoords",
Eric Anholtf8946692010-07-20 14:03:35 -0700461 state->Const.MaxTextureCoords);
Ian Romanickdfdff942011-01-24 16:45:11 -0800462 add_builtin_constant(instructions, symtab, "gl_MaxVertexAttribs",
Eric Anholtf8946692010-07-20 14:03:35 -0700463 state->Const.MaxVertexAttribs);
Ian Romanickdfdff942011-01-24 16:45:11 -0800464 add_builtin_constant(instructions, symtab, "gl_MaxVertexUniformComponents",
Eric Anholtf8946692010-07-20 14:03:35 -0700465 state->Const.MaxVertexUniformComponents);
Ian Romanickdfdff942011-01-24 16:45:11 -0800466 add_builtin_constant(instructions, symtab, "gl_MaxVaryingFloats",
Eric Anholtf8946692010-07-20 14:03:35 -0700467 state->Const.MaxVaryingFloats);
Ian Romanickdfdff942011-01-24 16:45:11 -0800468 add_builtin_constant(instructions, symtab, "gl_MaxVertexTextureImageUnits",
Eric Anholtf8946692010-07-20 14:03:35 -0700469 state->Const.MaxVertexTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800470 add_builtin_constant(instructions, symtab, "gl_MaxCombinedTextureImageUnits",
Eric Anholtf8946692010-07-20 14:03:35 -0700471 state->Const.MaxCombinedTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800472 add_builtin_constant(instructions, symtab, "gl_MaxTextureImageUnits",
Eric Anholtf8946692010-07-20 14:03:35 -0700473 state->Const.MaxTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800474 add_builtin_constant(instructions, symtab, "gl_MaxFragmentUniformComponents",
Eric Anholtf8946692010-07-20 14:03:35 -0700475 state->Const.MaxFragmentUniformComponents);
Ian Romanick127308b2010-07-01 13:30:50 -0700476
Ian Romanick3eba5932010-04-26 14:59:32 -0700477 const glsl_type *const mat4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700478 glsl_type::get_array_instance(glsl_type::mat4_type,
Ian Romanick127308b2010-07-01 13:30:50 -0700479 state->Const.MaxTextureCoords);
Ian Romanick3eba5932010-04-26 14:59:32 -0700480
Ian Romanickdfdff942011-01-24 16:45:11 -0800481 add_uniform(instructions, symtab, "gl_TextureMatrix", mat4_array_type);
482 add_uniform(instructions, symtab, "gl_TextureMatrixInverse", mat4_array_type);
483 add_uniform(instructions, symtab, "gl_TextureMatrixTranspose", mat4_array_type);
484 add_uniform(instructions, symtab, "gl_TextureMatrixInverseTranspose", mat4_array_type);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700485
Ian Romanickdfdff942011-01-24 16:45:11 -0800486 add_uniform(instructions, symtab, "gl_DepthRange",
487 symtab->get_type("gl_DepthRangeParameters"));
Eric Anholt78fe3c92010-03-28 01:46:48 -0700488
Ian Romanickdfdff942011-01-24 16:45:11 -0800489 add_uniform(instructions, symtab, "gl_ClipPlane",
Eric Anholt85b5dba2010-07-28 12:23:51 -0700490 glsl_type::get_array_instance(glsl_type::vec4_type,
491 state->Const.MaxClipPlanes));
Ian Romanickdfdff942011-01-24 16:45:11 -0800492 add_uniform(instructions, symtab, "gl_Point",
493 symtab->get_type("gl_PointParameters"));
Eric Anholt85b5dba2010-07-28 12:23:51 -0700494
495 const glsl_type *const material_parameters_type =
Ian Romanickdfdff942011-01-24 16:45:11 -0800496 symtab->get_type("gl_MaterialParameters");
497 add_uniform(instructions, symtab, "gl_FrontMaterial", material_parameters_type);
498 add_uniform(instructions, symtab, "gl_BackMaterial", material_parameters_type);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700499
Eric Anholtaa579432010-05-19 14:09:04 -0700500 const glsl_type *const light_source_array_type =
Ian Romanickdfdff942011-01-24 16:45:11 -0800501 glsl_type::get_array_instance(symtab->get_type("gl_LightSourceParameters"), state->Const.MaxLights);
Eric Anholtaa579432010-05-19 14:09:04 -0700502
Ian Romanickdfdff942011-01-24 16:45:11 -0800503 add_uniform(instructions, symtab, "gl_LightSource", light_source_array_type);
Eric Anholtaa579432010-05-19 14:09:04 -0700504
Eric Anholt85b5dba2010-07-28 12:23:51 -0700505 const glsl_type *const light_model_products_type =
Ian Romanickdfdff942011-01-24 16:45:11 -0800506 symtab->get_type("gl_LightModelProducts");
507 add_uniform(instructions, symtab, "gl_FrontLightModelProduct",
Eric Anholt85b5dba2010-07-28 12:23:51 -0700508 light_model_products_type);
Ian Romanickdfdff942011-01-24 16:45:11 -0800509 add_uniform(instructions, symtab, "gl_BackLightModelProduct",
Eric Anholt85b5dba2010-07-28 12:23:51 -0700510 light_model_products_type);
511
512 const glsl_type *const light_products_type =
Ian Romanickdfdff942011-01-24 16:45:11 -0800513 glsl_type::get_array_instance(symtab->get_type("gl_LightProducts"),
Eric Anholt85b5dba2010-07-28 12:23:51 -0700514 state->Const.MaxLights);
Ian Romanickdfdff942011-01-24 16:45:11 -0800515 add_uniform(instructions, symtab, "gl_FrontLightProduct", light_products_type);
516 add_uniform(instructions, symtab, "gl_BackLightProduct", light_products_type);
Eric Anholt85b5dba2010-07-28 12:23:51 -0700517
Ian Romanickdfdff942011-01-24 16:45:11 -0800518 add_uniform(instructions, symtab, "gl_TextureEnvColor",
Eric Anholt85b5dba2010-07-28 12:23:51 -0700519 glsl_type::get_array_instance(glsl_type::vec4_type,
520 state->Const.MaxTextureUnits));
521
522 const glsl_type *const texcoords_vec4 =
523 glsl_type::get_array_instance(glsl_type::vec4_type,
524 state->Const.MaxTextureCoords);
Ian Romanickdfdff942011-01-24 16:45:11 -0800525 add_uniform(instructions, symtab, "gl_EyePlaneS", texcoords_vec4);
526 add_uniform(instructions, symtab, "gl_EyePlaneT", texcoords_vec4);
527 add_uniform(instructions, symtab, "gl_EyePlaneR", texcoords_vec4);
528 add_uniform(instructions, symtab, "gl_EyePlaneQ", texcoords_vec4);
529 add_uniform(instructions, symtab, "gl_ObjectPlaneS", texcoords_vec4);
530 add_uniform(instructions, symtab, "gl_ObjectPlaneT", texcoords_vec4);
531 add_uniform(instructions, symtab, "gl_ObjectPlaneR", texcoords_vec4);
532 add_uniform(instructions, symtab, "gl_ObjectPlaneQ", texcoords_vec4);
Eric Anholt85b5dba2010-07-28 12:23:51 -0700533
Ian Romanickdfdff942011-01-24 16:45:11 -0800534 add_uniform(instructions, symtab, "gl_Fog",
535 symtab->get_type("gl_FogParameters"));
Eric Anholtf868cb02011-07-16 17:41:26 -0700536
537 /* Mesa-internal current attrib state */
538 const glsl_type *const vert_attribs =
539 glsl_type::get_array_instance(glsl_type::vec4_type, VERT_ATTRIB_MAX);
540 add_uniform(instructions, symtab, "gl_CurrentAttribVertMESA", vert_attribs);
541 const glsl_type *const frag_attribs =
542 glsl_type::get_array_instance(glsl_type::vec4_type, FRAG_ATTRIB_MAX);
543 add_uniform(instructions, symtab, "gl_CurrentAttribFragMESA", frag_attribs);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700544}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800545
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700546/* This function should only be called for ES, not desktop GL. */
547static void
548generate_100ES_vs_variables(exec_list *instructions,
549 struct _mesa_glsl_parse_state *state)
550{
551 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800552 add_builtin_variable(instructions, state->symbols,
553 & builtin_core_vs_variables[i]);
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700554 }
555
556 generate_100ES_uniforms(instructions, state);
557
558 generate_ARB_draw_buffers_variables(instructions, state, false,
559 vertex_shader);
560}
561
562
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800563static void
564generate_110_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700565 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800566{
567 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800568 add_builtin_variable(instructions, state->symbols,
569 & builtin_core_vs_variables[i]);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800570 }
571
572 for (unsigned i = 0
573 ; i < Elements(builtin_110_deprecated_vs_variables)
574 ; i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800575 add_builtin_variable(instructions, state->symbols,
576 & builtin_110_deprecated_vs_variables[i]);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800577 }
Ian Romanick127308b2010-07-01 13:30:50 -0700578 generate_110_uniforms(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800579
Ian Romanickcd00d5b2010-07-01 13:17:54 -0700580 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
581 *
582 * "As with all arrays, indices used to subscript gl_TexCoord must
583 * either be an integral constant expressions, or this array must be
584 * re-declared by the shader with a size. The size can be at most
585 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
586 * implementation in preserving varying resources."
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800587 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700588 const glsl_type *const vec4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700589 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700590
Ian Romanickdfdff942011-01-24 16:45:11 -0800591 add_variable(instructions, state->symbols,
592 "gl_TexCoord", vec4_array_type, ir_var_out, VERT_RESULT_TEX0);
Ian Romanick22971e92010-06-29 15:29:56 -0700593
594 generate_ARB_draw_buffers_variables(instructions, state, false,
595 vertex_shader);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800596}
597
598
599static void
600generate_120_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700601 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800602{
603 /* GLSL version 1.20 did not add any built-in variables in the vertex
604 * shader.
605 */
Ian Romanick22971e92010-06-29 15:29:56 -0700606 generate_110_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800607}
608
609
610static void
Paul Berry36c4b1a2011-08-11 17:33:06 -0700611generate_130_uniforms(exec_list *instructions,
612 struct _mesa_glsl_parse_state *state)
613{
614 glsl_symbol_table *const symtab = state->symbols;
615
616 add_builtin_constant(instructions, symtab, "gl_MaxClipDistances",
617 state->Const.MaxClipPlanes);
Eric Anholt2ecfa882011-10-21 15:17:16 -0700618 add_builtin_constant(instructions, symtab, "gl_MaxVaryingComponents",
619 state->Const.MaxVaryingFloats);
Paul Berry36c4b1a2011-08-11 17:33:06 -0700620}
621
622
623static void
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800624generate_130_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700625 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800626{
Ian Romanick22971e92010-06-29 15:29:56 -0700627 generate_120_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800628
629 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800630 add_builtin_variable(instructions, state->symbols,
631 & builtin_130_vs_variables[i]);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800632 }
633
Paul Berry36c4b1a2011-08-11 17:33:06 -0700634 generate_130_uniforms(instructions, state);
635
Paul Berryaf243b52011-08-11 15:03:19 -0700636 /* From the GLSL 1.30 spec, section 7.1 (Vertex Shader Special
637 * Variables):
638 *
639 * The gl_ClipDistance array is predeclared as unsized and must
640 * be sized by the shader either redeclaring it with a size or
641 * indexing it only with integral constant expressions.
642 *
643 * We represent this in Mesa by initially declaring the array as
644 * size 0.
645 */
Eric Anholt271e1992010-04-02 23:47:06 -0700646 const glsl_type *const clip_distance_array_type =
Paul Berryaf243b52011-08-11 15:03:19 -0700647 glsl_type::get_array_instance(glsl_type::float_type, 0);
Ian Romanicked0626e2010-06-21 11:42:57 -0700648
Ian Romanickdfdff942011-01-24 16:45:11 -0800649 add_variable(instructions, state->symbols,
Paul Berry3d7c5a42011-08-19 13:12:23 -0700650 "gl_ClipDistance", clip_distance_array_type, ir_var_out,
651 VERT_RESULT_CLIP_DIST0);
Eric Anholt271e1992010-04-02 23:47:06 -0700652
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800653}
654
655
656static void
657initialize_vs_variables(exec_list *instructions,
658 struct _mesa_glsl_parse_state *state)
659{
660
661 switch (state->language_version) {
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700662 case 100:
663 generate_100ES_vs_variables(instructions, state);
664 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800665 case 110:
Ian Romanick22971e92010-06-29 15:29:56 -0700666 generate_110_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800667 break;
668 case 120:
Ian Romanick22971e92010-06-29 15:29:56 -0700669 generate_120_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800670 break;
671 case 130:
Ian Romanick22971e92010-06-29 15:29:56 -0700672 generate_130_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800673 break;
674 }
Brian Paul7ce18632010-12-08 18:25:38 -0700675
676 if (state->ARB_draw_instanced_enable)
677 generate_ARB_draw_instanced_variables(instructions, state, false,
678 vertex_shader);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800679}
680
Brian Paul7ce18632010-12-08 18:25:38 -0700681
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700682/* This function should only be called for ES, not desktop GL. */
683static void
684generate_100ES_fs_variables(exec_list *instructions,
685 struct _mesa_glsl_parse_state *state)
686{
687 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800688 add_builtin_variable(instructions, state->symbols,
689 & builtin_core_fs_variables[i]);
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700690 }
691
692 for (unsigned i = 0; i < Elements(builtin_100ES_fs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800693 add_builtin_variable(instructions, state->symbols,
694 & builtin_100ES_fs_variables[i]);
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700695 }
696
697 generate_100ES_uniforms(instructions, state);
698
699 generate_ARB_draw_buffers_variables(instructions, state, false,
700 fragment_shader);
701}
702
Eric Anholtb3f743a2010-03-25 14:48:25 -0700703static void
704generate_110_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700705 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700706{
707 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800708 add_builtin_variable(instructions, state->symbols,
709 & builtin_core_fs_variables[i]);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700710 }
711
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700712 for (unsigned i = 0; i < Elements(builtin_110_fs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800713 add_builtin_variable(instructions, state->symbols,
714 & builtin_110_fs_variables[i]);
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700715 }
716
Eric Anholt0f09aea2010-03-27 12:48:57 -0700717 for (unsigned i = 0
718 ; i < Elements(builtin_110_deprecated_fs_variables)
719 ; i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800720 add_builtin_variable(instructions, state->symbols,
721 & builtin_110_deprecated_fs_variables[i]);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700722 }
Ian Romanick127308b2010-07-01 13:30:50 -0700723 generate_110_uniforms(instructions, state);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700724
Ian Romanickcd00d5b2010-07-01 13:17:54 -0700725 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
726 *
727 * "As with all arrays, indices used to subscript gl_TexCoord must
728 * either be an integral constant expressions, or this array must be
729 * re-declared by the shader with a size. The size can be at most
730 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
731 * implementation in preserving varying resources."
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700732 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700733 const glsl_type *const vec4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700734 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700735
Ian Romanickdfdff942011-01-24 16:45:11 -0800736 add_variable(instructions, state->symbols,
737 "gl_TexCoord", vec4_array_type, ir_var_in, FRAG_ATTRIB_TEX0);
Ian Romanick9c4b1f22010-06-29 15:10:09 -0700738
Ian Romanick22971e92010-06-29 15:29:56 -0700739 generate_ARB_draw_buffers_variables(instructions, state, false,
740 fragment_shader);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700741}
742
Ian Romanickc77b2572010-04-07 16:59:46 -0700743
744static void
Ian Romanick22971e92010-06-29 15:29:56 -0700745generate_ARB_draw_buffers_variables(exec_list *instructions,
746 struct _mesa_glsl_parse_state *state,
747 bool warn, _mesa_glsl_parser_targets target)
Ian Romanickc77b2572010-04-07 16:59:46 -0700748{
Ian Romanick22971e92010-06-29 15:29:56 -0700749 /* gl_MaxDrawBuffers is available in all shader stages.
750 */
Ian Romanicke2f84f02010-06-29 15:19:11 -0700751 ir_variable *const mdb =
Ian Romanickdfdff942011-01-24 16:45:11 -0800752 add_variable(instructions, state->symbols,
753 "gl_MaxDrawBuffers", glsl_type::int_type, ir_var_auto, -1);
Ian Romanicke2f84f02010-06-29 15:19:11 -0700754
755 if (warn)
756 mdb->warn_extension = "GL_ARB_draw_buffers";
757
758 mdb->constant_value = new(mdb)
759 ir_constant(int(state->Const.MaxDrawBuffers));
760
Ian Romanickc77b2572010-04-07 16:59:46 -0700761
Ian Romanick22971e92010-06-29 15:29:56 -0700762 /* gl_FragData is only available in the fragment shader.
763 */
764 if (target == fragment_shader) {
765 const glsl_type *const vec4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700766 glsl_type::get_array_instance(glsl_type::vec4_type,
Ian Romanick22971e92010-06-29 15:29:56 -0700767 state->Const.MaxDrawBuffers);
Ian Romanickc77b2572010-04-07 16:59:46 -0700768
Ian Romanick22971e92010-06-29 15:29:56 -0700769 ir_variable *const fd =
Ian Romanickdfdff942011-01-24 16:45:11 -0800770 add_variable(instructions, state->symbols,
771 "gl_FragData", vec4_array_type,
772 ir_var_out, FRAG_RESULT_DATA0);
Ian Romanick22971e92010-06-29 15:29:56 -0700773
774 if (warn)
775 fd->warn_extension = "GL_ARB_draw_buffers";
776 }
Ian Romanickc77b2572010-04-07 16:59:46 -0700777}
778
Brian Paul7ce18632010-12-08 18:25:38 -0700779
780static void
781generate_ARB_draw_instanced_variables(exec_list *instructions,
782 struct _mesa_glsl_parse_state *state,
783 bool warn,
784 _mesa_glsl_parser_targets target)
785{
786 /* gl_InstanceIDARB is only available in the vertex shader.
787 */
788 if (target == vertex_shader) {
789 ir_variable *const inst =
Ian Romanickdfdff942011-01-24 16:45:11 -0800790 add_variable(instructions, state->symbols,
791 "gl_InstanceIDARB", glsl_type::int_type,
792 ir_var_system_value, SYSTEM_VALUE_INSTANCE_ID);
Brian Paul7ce18632010-12-08 18:25:38 -0700793
794 if (warn)
795 inst->warn_extension = "GL_ARB_draw_instanced";
796 }
797}
798
799
Dave Airlied9671862010-10-06 09:36:02 +1000800static void
801generate_ARB_shader_stencil_export_variables(exec_list *instructions,
802 struct _mesa_glsl_parse_state *state,
803 bool warn)
804{
805 /* gl_FragStencilRefARB is only available in the fragment shader.
806 */
807 ir_variable *const fd =
Ian Romanickdfdff942011-01-24 16:45:11 -0800808 add_variable(instructions, state->symbols,
809 "gl_FragStencilRefARB", glsl_type::int_type,
810 ir_var_out, FRAG_RESULT_STENCIL);
Dave Airlied9671862010-10-06 09:36:02 +1000811
812 if (warn)
813 fd->warn_extension = "GL_ARB_shader_stencil_export";
814}
Ian Romanickc77b2572010-04-07 16:59:46 -0700815
Eric Anholtb3f743a2010-03-25 14:48:25 -0700816static void
Marek Olšák5ba2e7a2011-05-02 16:41:04 +0200817generate_AMD_shader_stencil_export_variables(exec_list *instructions,
818 struct _mesa_glsl_parse_state *state,
819 bool warn)
820{
821 /* gl_FragStencilRefAMD is only available in the fragment shader.
822 */
823 ir_variable *const fd =
824 add_variable(instructions, state->symbols,
825 "gl_FragStencilRefAMD", glsl_type::int_type,
826 ir_var_out, FRAG_RESULT_STENCIL);
827
828 if (warn)
829 fd->warn_extension = "GL_AMD_shader_stencil_export";
830}
831
832static void
Eric Anholtb3f743a2010-03-25 14:48:25 -0700833generate_120_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700834 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700835{
Ian Romanick5e18b052010-06-29 14:21:05 -0700836 generate_110_fs_variables(instructions, state);
Eric Anholt152b55e2010-07-07 19:45:22 -0700837
838 for (unsigned i = 0
839 ; i < Elements(builtin_120_fs_variables)
840 ; i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800841 add_builtin_variable(instructions, state->symbols,
842 & builtin_120_fs_variables[i]);
Eric Anholt152b55e2010-07-07 19:45:22 -0700843 }
Eric Anholtb3f743a2010-03-25 14:48:25 -0700844}
845
846static void
847generate_130_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700848 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700849{
Ian Romanick5e18b052010-06-29 14:21:05 -0700850 generate_120_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700851
Paul Berry36c4b1a2011-08-11 17:33:06 -0700852 generate_130_uniforms(instructions, state);
853
Paul Berryaf243b52011-08-11 15:03:19 -0700854 /* From the GLSL 1.30 spec, section 7.2 (Fragment Shader Special
855 * Variables):
856 *
857 * The built-in input variable gl_ClipDistance array contains linearly
858 * interpolated values for the vertex values written by the vertex shader
859 * to the gl_ClipDistance vertex output variable. This array must be
860 * sized in the fragment shader either implicitly or explicitly to be the
861 * same size as it was sized in the vertex shader.
862 *
863 * In other words, the array must be pre-declared as implicitly sized. We
864 * represent this in Mesa by initially declaring the array as size 0.
865 */
Ian Romanick8645a952010-04-07 16:47:44 -0700866 const glsl_type *const clip_distance_array_type =
Paul Berryaf243b52011-08-11 15:03:19 -0700867 glsl_type::get_array_instance(glsl_type::float_type, 0);
Ian Romanicked0626e2010-06-21 11:42:57 -0700868
Ian Romanickdfdff942011-01-24 16:45:11 -0800869 add_variable(instructions, state->symbols,
Paul Berry3d7c5a42011-08-19 13:12:23 -0700870 "gl_ClipDistance", clip_distance_array_type, ir_var_in,
871 FRAG_ATTRIB_CLIP_DIST0);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700872}
873
874static void
875initialize_fs_variables(exec_list *instructions,
876 struct _mesa_glsl_parse_state *state)
877{
878
879 switch (state->language_version) {
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700880 case 100:
881 generate_100ES_fs_variables(instructions, state);
882 break;
Eric Anholtb3f743a2010-03-25 14:48:25 -0700883 case 110:
Ian Romanick5e18b052010-06-29 14:21:05 -0700884 generate_110_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700885 break;
886 case 120:
Ian Romanick5e18b052010-06-29 14:21:05 -0700887 generate_120_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700888 break;
889 case 130:
Ian Romanick5e18b052010-06-29 14:21:05 -0700890 generate_130_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700891 break;
892 }
Dave Airlied9671862010-10-06 09:36:02 +1000893
894 if (state->ARB_shader_stencil_export_enable)
895 generate_ARB_shader_stencil_export_variables(instructions, state,
896 state->ARB_shader_stencil_export_warn);
Marek Olšák5ba2e7a2011-05-02 16:41:04 +0200897
898 if (state->AMD_shader_stencil_export_enable)
899 generate_AMD_shader_stencil_export_variables(instructions, state,
900 state->AMD_shader_stencil_export_warn);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700901}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800902
903void
904_mesa_glsl_initialize_variables(exec_list *instructions,
905 struct _mesa_glsl_parse_state *state)
906{
907 switch (state->target) {
908 case vertex_shader:
909 initialize_vs_variables(instructions, state);
910 break;
911 case geometry_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700912 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800913 case fragment_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700914 initialize_fs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800915 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800916 }
917}