blob: 6ae3b1f9ecfb127cf8be5643c1266a91a8656ff7 [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},
235 SWIZZLE_XYZW },
236 { NULL, { STATE_MODELVIEW_MATRIX, 0, 1, 1, STATE_MATRIX_INVERSE},
237 SWIZZLE_XYZW },
238 { NULL, { STATE_MODELVIEW_MATRIX, 0, 2, 2, STATE_MATRIX_INVERSE},
239 SWIZZLE_XYZW },
240};
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);
618}
619
620
621static void
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800622generate_130_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700623 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800624{
Ian Romanick22971e92010-06-29 15:29:56 -0700625 generate_120_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800626
627 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800628 add_builtin_variable(instructions, state->symbols,
629 & builtin_130_vs_variables[i]);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800630 }
631
Paul Berry36c4b1a2011-08-11 17:33:06 -0700632 generate_130_uniforms(instructions, state);
633
Paul Berryaf243b52011-08-11 15:03:19 -0700634 /* From the GLSL 1.30 spec, section 7.1 (Vertex Shader Special
635 * Variables):
636 *
637 * The gl_ClipDistance array is predeclared as unsized and must
638 * be sized by the shader either redeclaring it with a size or
639 * indexing it only with integral constant expressions.
640 *
641 * We represent this in Mesa by initially declaring the array as
642 * size 0.
643 */
Eric Anholt271e1992010-04-02 23:47:06 -0700644 const glsl_type *const clip_distance_array_type =
Paul Berryaf243b52011-08-11 15:03:19 -0700645 glsl_type::get_array_instance(glsl_type::float_type, 0);
Ian Romanicked0626e2010-06-21 11:42:57 -0700646
Ian Romanickdfdff942011-01-24 16:45:11 -0800647 add_variable(instructions, state->symbols,
Paul Berry3d7c5a42011-08-19 13:12:23 -0700648 "gl_ClipDistance", clip_distance_array_type, ir_var_out,
649 VERT_RESULT_CLIP_DIST0);
Eric Anholt271e1992010-04-02 23:47:06 -0700650
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800651}
652
653
654static void
655initialize_vs_variables(exec_list *instructions,
656 struct _mesa_glsl_parse_state *state)
657{
658
659 switch (state->language_version) {
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700660 case 100:
661 generate_100ES_vs_variables(instructions, state);
662 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800663 case 110:
Ian Romanick22971e92010-06-29 15:29:56 -0700664 generate_110_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800665 break;
666 case 120:
Ian Romanick22971e92010-06-29 15:29:56 -0700667 generate_120_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800668 break;
669 case 130:
Ian Romanick22971e92010-06-29 15:29:56 -0700670 generate_130_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800671 break;
672 }
Brian Paul7ce18632010-12-08 18:25:38 -0700673
674 if (state->ARB_draw_instanced_enable)
675 generate_ARB_draw_instanced_variables(instructions, state, false,
676 vertex_shader);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800677}
678
Brian Paul7ce18632010-12-08 18:25:38 -0700679
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700680/* This function should only be called for ES, not desktop GL. */
681static void
682generate_100ES_fs_variables(exec_list *instructions,
683 struct _mesa_glsl_parse_state *state)
684{
685 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800686 add_builtin_variable(instructions, state->symbols,
687 & builtin_core_fs_variables[i]);
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700688 }
689
690 for (unsigned i = 0; i < Elements(builtin_100ES_fs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800691 add_builtin_variable(instructions, state->symbols,
692 & builtin_100ES_fs_variables[i]);
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700693 }
694
695 generate_100ES_uniforms(instructions, state);
696
697 generate_ARB_draw_buffers_variables(instructions, state, false,
698 fragment_shader);
699}
700
Eric Anholtb3f743a2010-03-25 14:48:25 -0700701static void
702generate_110_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700703 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700704{
705 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800706 add_builtin_variable(instructions, state->symbols,
707 & builtin_core_fs_variables[i]);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700708 }
709
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700710 for (unsigned i = 0; i < Elements(builtin_110_fs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800711 add_builtin_variable(instructions, state->symbols,
712 & builtin_110_fs_variables[i]);
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700713 }
714
Eric Anholt0f09aea2010-03-27 12:48:57 -0700715 for (unsigned i = 0
716 ; i < Elements(builtin_110_deprecated_fs_variables)
717 ; i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800718 add_builtin_variable(instructions, state->symbols,
719 & builtin_110_deprecated_fs_variables[i]);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700720 }
Ian Romanick127308b2010-07-01 13:30:50 -0700721 generate_110_uniforms(instructions, state);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700722
Ian Romanickcd00d5b2010-07-01 13:17:54 -0700723 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
724 *
725 * "As with all arrays, indices used to subscript gl_TexCoord must
726 * either be an integral constant expressions, or this array must be
727 * re-declared by the shader with a size. The size can be at most
728 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
729 * implementation in preserving varying resources."
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700730 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700731 const glsl_type *const vec4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700732 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700733
Ian Romanickdfdff942011-01-24 16:45:11 -0800734 add_variable(instructions, state->symbols,
735 "gl_TexCoord", vec4_array_type, ir_var_in, FRAG_ATTRIB_TEX0);
Ian Romanick9c4b1f22010-06-29 15:10:09 -0700736
Ian Romanick22971e92010-06-29 15:29:56 -0700737 generate_ARB_draw_buffers_variables(instructions, state, false,
738 fragment_shader);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700739}
740
Ian Romanickc77b2572010-04-07 16:59:46 -0700741
742static void
Ian Romanick22971e92010-06-29 15:29:56 -0700743generate_ARB_draw_buffers_variables(exec_list *instructions,
744 struct _mesa_glsl_parse_state *state,
745 bool warn, _mesa_glsl_parser_targets target)
Ian Romanickc77b2572010-04-07 16:59:46 -0700746{
Ian Romanick22971e92010-06-29 15:29:56 -0700747 /* gl_MaxDrawBuffers is available in all shader stages.
748 */
Ian Romanicke2f84f02010-06-29 15:19:11 -0700749 ir_variable *const mdb =
Ian Romanickdfdff942011-01-24 16:45:11 -0800750 add_variable(instructions, state->symbols,
751 "gl_MaxDrawBuffers", glsl_type::int_type, ir_var_auto, -1);
Ian Romanicke2f84f02010-06-29 15:19:11 -0700752
753 if (warn)
754 mdb->warn_extension = "GL_ARB_draw_buffers";
755
756 mdb->constant_value = new(mdb)
757 ir_constant(int(state->Const.MaxDrawBuffers));
758
Ian Romanickc77b2572010-04-07 16:59:46 -0700759
Ian Romanick22971e92010-06-29 15:29:56 -0700760 /* gl_FragData is only available in the fragment shader.
761 */
762 if (target == fragment_shader) {
763 const glsl_type *const vec4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700764 glsl_type::get_array_instance(glsl_type::vec4_type,
Ian Romanick22971e92010-06-29 15:29:56 -0700765 state->Const.MaxDrawBuffers);
Ian Romanickc77b2572010-04-07 16:59:46 -0700766
Ian Romanick22971e92010-06-29 15:29:56 -0700767 ir_variable *const fd =
Ian Romanickdfdff942011-01-24 16:45:11 -0800768 add_variable(instructions, state->symbols,
769 "gl_FragData", vec4_array_type,
770 ir_var_out, FRAG_RESULT_DATA0);
Ian Romanick22971e92010-06-29 15:29:56 -0700771
772 if (warn)
773 fd->warn_extension = "GL_ARB_draw_buffers";
774 }
Ian Romanickc77b2572010-04-07 16:59:46 -0700775}
776
Brian Paul7ce18632010-12-08 18:25:38 -0700777
778static void
779generate_ARB_draw_instanced_variables(exec_list *instructions,
780 struct _mesa_glsl_parse_state *state,
781 bool warn,
782 _mesa_glsl_parser_targets target)
783{
784 /* gl_InstanceIDARB is only available in the vertex shader.
785 */
786 if (target == vertex_shader) {
787 ir_variable *const inst =
Ian Romanickdfdff942011-01-24 16:45:11 -0800788 add_variable(instructions, state->symbols,
789 "gl_InstanceIDARB", glsl_type::int_type,
790 ir_var_system_value, SYSTEM_VALUE_INSTANCE_ID);
Brian Paul7ce18632010-12-08 18:25:38 -0700791
792 if (warn)
793 inst->warn_extension = "GL_ARB_draw_instanced";
794 }
795}
796
797
Dave Airlied9671862010-10-06 09:36:02 +1000798static void
799generate_ARB_shader_stencil_export_variables(exec_list *instructions,
800 struct _mesa_glsl_parse_state *state,
801 bool warn)
802{
803 /* gl_FragStencilRefARB is only available in the fragment shader.
804 */
805 ir_variable *const fd =
Ian Romanickdfdff942011-01-24 16:45:11 -0800806 add_variable(instructions, state->symbols,
807 "gl_FragStencilRefARB", glsl_type::int_type,
808 ir_var_out, FRAG_RESULT_STENCIL);
Dave Airlied9671862010-10-06 09:36:02 +1000809
810 if (warn)
811 fd->warn_extension = "GL_ARB_shader_stencil_export";
812}
Ian Romanickc77b2572010-04-07 16:59:46 -0700813
Eric Anholtb3f743a2010-03-25 14:48:25 -0700814static void
Marek Olšák5ba2e7a2011-05-02 16:41:04 +0200815generate_AMD_shader_stencil_export_variables(exec_list *instructions,
816 struct _mesa_glsl_parse_state *state,
817 bool warn)
818{
819 /* gl_FragStencilRefAMD is only available in the fragment shader.
820 */
821 ir_variable *const fd =
822 add_variable(instructions, state->symbols,
823 "gl_FragStencilRefAMD", glsl_type::int_type,
824 ir_var_out, FRAG_RESULT_STENCIL);
825
826 if (warn)
827 fd->warn_extension = "GL_AMD_shader_stencil_export";
828}
829
830static void
Eric Anholtb3f743a2010-03-25 14:48:25 -0700831generate_120_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700832 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700833{
Ian Romanick5e18b052010-06-29 14:21:05 -0700834 generate_110_fs_variables(instructions, state);
Eric Anholt152b55e2010-07-07 19:45:22 -0700835
836 for (unsigned i = 0
837 ; i < Elements(builtin_120_fs_variables)
838 ; i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800839 add_builtin_variable(instructions, state->symbols,
840 & builtin_120_fs_variables[i]);
Eric Anholt152b55e2010-07-07 19:45:22 -0700841 }
Eric Anholtb3f743a2010-03-25 14:48:25 -0700842}
843
844static void
845generate_130_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700846 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700847{
Ian Romanick5e18b052010-06-29 14:21:05 -0700848 generate_120_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700849
Paul Berry36c4b1a2011-08-11 17:33:06 -0700850 generate_130_uniforms(instructions, state);
851
Paul Berryaf243b52011-08-11 15:03:19 -0700852 /* From the GLSL 1.30 spec, section 7.2 (Fragment Shader Special
853 * Variables):
854 *
855 * The built-in input variable gl_ClipDistance array contains linearly
856 * interpolated values for the vertex values written by the vertex shader
857 * to the gl_ClipDistance vertex output variable. This array must be
858 * sized in the fragment shader either implicitly or explicitly to be the
859 * same size as it was sized in the vertex shader.
860 *
861 * In other words, the array must be pre-declared as implicitly sized. We
862 * represent this in Mesa by initially declaring the array as size 0.
863 */
Ian Romanick8645a952010-04-07 16:47:44 -0700864 const glsl_type *const clip_distance_array_type =
Paul Berryaf243b52011-08-11 15:03:19 -0700865 glsl_type::get_array_instance(glsl_type::float_type, 0);
Ian Romanicked0626e2010-06-21 11:42:57 -0700866
Ian Romanickdfdff942011-01-24 16:45:11 -0800867 add_variable(instructions, state->symbols,
Paul Berry3d7c5a42011-08-19 13:12:23 -0700868 "gl_ClipDistance", clip_distance_array_type, ir_var_in,
869 FRAG_ATTRIB_CLIP_DIST0);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700870}
871
872static void
873initialize_fs_variables(exec_list *instructions,
874 struct _mesa_glsl_parse_state *state)
875{
876
877 switch (state->language_version) {
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700878 case 100:
879 generate_100ES_fs_variables(instructions, state);
880 break;
Eric Anholtb3f743a2010-03-25 14:48:25 -0700881 case 110:
Ian Romanick5e18b052010-06-29 14:21:05 -0700882 generate_110_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700883 break;
884 case 120:
Ian Romanick5e18b052010-06-29 14:21:05 -0700885 generate_120_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700886 break;
887 case 130:
Ian Romanick5e18b052010-06-29 14:21:05 -0700888 generate_130_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700889 break;
890 }
Dave Airlied9671862010-10-06 09:36:02 +1000891
892 if (state->ARB_shader_stencil_export_enable)
893 generate_ARB_shader_stencil_export_variables(instructions, state,
894 state->ARB_shader_stencil_export_warn);
Marek Olšák5ba2e7a2011-05-02 16:41:04 +0200895
896 if (state->AMD_shader_stencil_export_enable)
897 generate_AMD_shader_stencil_export_variables(instructions, state,
898 state->AMD_shader_stencil_export_warn);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700899}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800900
901void
902_mesa_glsl_initialize_variables(exec_list *instructions,
903 struct _mesa_glsl_parse_state *state)
904{
905 switch (state->target) {
906 case vertex_shader:
907 initialize_vs_variables(instructions, state);
908 break;
909 case geometry_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700910 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800911 case fragment_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700912 initialize_fs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800913 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800914 }
915}