blob: 58be64bfaa6b6da703fac2071f335122b49c98ff [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
181#define MATRIX(name, statevar, modifier) \
182 static struct gl_builtin_uniform_element name ## _elements[] = { \
183 { NULL, { statevar, 0, 0, 0, modifier}, SWIZZLE_XYZW }, \
184 { NULL, { statevar, 0, 1, 1, modifier}, SWIZZLE_XYZW }, \
185 { NULL, { statevar, 0, 2, 2, modifier}, SWIZZLE_XYZW }, \
186 { NULL, { statevar, 0, 3, 3, modifier}, SWIZZLE_XYZW }, \
187 }
188
189MATRIX(gl_ModelViewMatrix,
190 STATE_MODELVIEW_MATRIX, STATE_MATRIX_TRANSPOSE);
191MATRIX(gl_ModelViewMatrixInverse,
192 STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVTRANS);
193MATRIX(gl_ModelViewMatrixTranspose,
194 STATE_MODELVIEW_MATRIX, 0);
195MATRIX(gl_ModelViewMatrixInverseTranspose,
196 STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVERSE);
197
198MATRIX(gl_ProjectionMatrix,
199 STATE_PROJECTION_MATRIX, STATE_MATRIX_TRANSPOSE);
200MATRIX(gl_ProjectionMatrixInverse,
201 STATE_PROJECTION_MATRIX, STATE_MATRIX_INVTRANS);
202MATRIX(gl_ProjectionMatrixTranspose,
203 STATE_PROJECTION_MATRIX, 0);
204MATRIX(gl_ProjectionMatrixInverseTranspose,
205 STATE_PROJECTION_MATRIX, STATE_MATRIX_INVERSE);
206
207MATRIX(gl_ModelViewProjectionMatrix,
208 STATE_MVP_MATRIX, STATE_MATRIX_TRANSPOSE);
209MATRIX(gl_ModelViewProjectionMatrixInverse,
210 STATE_MVP_MATRIX, STATE_MATRIX_INVTRANS);
211MATRIX(gl_ModelViewProjectionMatrixTranspose,
212 STATE_MVP_MATRIX, 0);
213MATRIX(gl_ModelViewProjectionMatrixInverseTranspose,
214 STATE_MVP_MATRIX, STATE_MATRIX_INVERSE);
215
216MATRIX(gl_TextureMatrix,
217 STATE_TEXTURE_MATRIX, STATE_MATRIX_TRANSPOSE);
218MATRIX(gl_TextureMatrixInverse,
219 STATE_TEXTURE_MATRIX, STATE_MATRIX_INVTRANS);
220MATRIX(gl_TextureMatrixTranspose,
221 STATE_TEXTURE_MATRIX, 0);
222MATRIX(gl_TextureMatrixInverseTranspose,
223 STATE_TEXTURE_MATRIX, STATE_MATRIX_INVERSE);
224
225static struct gl_builtin_uniform_element gl_NormalMatrix_elements[] = {
226 { NULL, { STATE_MODELVIEW_MATRIX, 0, 0, 0, STATE_MATRIX_INVERSE},
227 SWIZZLE_XYZW },
228 { NULL, { STATE_MODELVIEW_MATRIX, 0, 1, 1, STATE_MATRIX_INVERSE},
229 SWIZZLE_XYZW },
230 { NULL, { STATE_MODELVIEW_MATRIX, 0, 2, 2, STATE_MATRIX_INVERSE},
231 SWIZZLE_XYZW },
232};
233
234#undef MATRIX
235
236#define STATEVAR(name) {#name, name ## _elements, Elements(name ## _elements)}
237
238const struct gl_builtin_uniform_desc _mesa_builtin_uniform_desc[] = {
239 STATEVAR(gl_DepthRange),
240 STATEVAR(gl_ClipPlane),
241 STATEVAR(gl_Point),
242 STATEVAR(gl_FrontMaterial),
243 STATEVAR(gl_BackMaterial),
244 STATEVAR(gl_LightSource),
245 STATEVAR(gl_LightModel),
246 STATEVAR(gl_FrontLightModelProduct),
247 STATEVAR(gl_BackLightModelProduct),
248 STATEVAR(gl_FrontLightProduct),
249 STATEVAR(gl_BackLightProduct),
250 STATEVAR(gl_TextureEnvColor),
251 STATEVAR(gl_EyePlaneS),
252 STATEVAR(gl_EyePlaneT),
253 STATEVAR(gl_EyePlaneR),
254 STATEVAR(gl_EyePlaneQ),
255 STATEVAR(gl_ObjectPlaneS),
256 STATEVAR(gl_ObjectPlaneT),
257 STATEVAR(gl_ObjectPlaneR),
258 STATEVAR(gl_ObjectPlaneQ),
259 STATEVAR(gl_Fog),
260
261 STATEVAR(gl_ModelViewMatrix),
262 STATEVAR(gl_ModelViewMatrixInverse),
263 STATEVAR(gl_ModelViewMatrixTranspose),
264 STATEVAR(gl_ModelViewMatrixInverseTranspose),
265
266 STATEVAR(gl_ProjectionMatrix),
267 STATEVAR(gl_ProjectionMatrixInverse),
268 STATEVAR(gl_ProjectionMatrixTranspose),
269 STATEVAR(gl_ProjectionMatrixInverseTranspose),
270
271 STATEVAR(gl_ModelViewProjectionMatrix),
272 STATEVAR(gl_ModelViewProjectionMatrixInverse),
273 STATEVAR(gl_ModelViewProjectionMatrixTranspose),
274 STATEVAR(gl_ModelViewProjectionMatrixInverseTranspose),
275
276 STATEVAR(gl_TextureMatrix),
277 STATEVAR(gl_TextureMatrixInverse),
278 STATEVAR(gl_TextureMatrixTranspose),
279 STATEVAR(gl_TextureMatrixInverseTranspose),
280
281 STATEVAR(gl_NormalMatrix),
282 STATEVAR(gl_NormalScale),
283
284 STATEVAR(gl_MESABumpRotMatrix0),
285 STATEVAR(gl_MESABumpRotMatrix1),
286 STATEVAR(gl_MESAFogParamsOptimized),
287
288 {NULL, NULL, 0}
289};
290
Ian Romanickc77b2572010-04-07 16:59:46 -0700291static ir_variable *
Ian Romanickdfdff942011-01-24 16:45:11 -0800292add_variable(exec_list *instructions, glsl_symbol_table *symtab,
293 const char *name, const glsl_type *type,
294 enum ir_variable_mode mode, int slot)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800295{
Ian Romanick7e2aa912010-07-19 17:12:42 -0700296 ir_variable *var = new(symtab) ir_variable(type, name, mode);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800297
Eric Anholt71df19f2010-04-19 11:10:37 -0700298 switch (var->mode) {
Ian Romanicke2f84f02010-06-29 15:19:11 -0700299 case ir_var_auto:
Eric Anholt71df19f2010-04-19 11:10:37 -0700300 case ir_var_in:
Kenneth Graunke819d57f2011-01-12 15:37:37 -0800301 case ir_var_const_in:
Eric Anholt046bef22010-08-04 20:33:57 -0700302 case ir_var_uniform:
Brian Paul7ce18632010-12-08 18:25:38 -0700303 case ir_var_system_value:
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800304 var->read_only = true;
Eric Anholt71df19f2010-04-19 11:10:37 -0700305 break;
306 case ir_var_inout:
Eric Anholt71df19f2010-04-19 11:10:37 -0700307 case ir_var_out:
Eric Anholt71df19f2010-04-19 11:10:37 -0700308 break;
309 default:
310 assert(0);
311 break;
312 }
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800313
Ian Romanicked0626e2010-06-21 11:42:57 -0700314 var->location = slot;
Ian Romanick68a4fc92010-10-07 17:21:22 -0700315 var->explicit_location = (slot >= 0);
Ian Romanicked0626e2010-06-21 11:42:57 -0700316
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800317 /* Once the variable is created an initialized, add it to the symbol table
318 * and add the declaration to the IR stream.
319 */
320 instructions->push_tail(var);
321
Eric Anholt001eee52010-11-05 06:11:24 -0700322 symtab->add_variable(var);
Ian Romanickc77b2572010-04-07 16:59:46 -0700323 return var;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800324}
325
Eric Anholt85b5dba2010-07-28 12:23:51 -0700326static ir_variable *
Ian Romanickdfdff942011-01-24 16:45:11 -0800327add_uniform(exec_list *instructions, glsl_symbol_table *symtab,
Eric Anholt85b5dba2010-07-28 12:23:51 -0700328 const char *name, const glsl_type *type)
329{
Ian Romanick89d81ab2011-01-25 10:41:20 -0800330 ir_variable *const uni =
331 add_variable(instructions, symtab, name, type, ir_var_uniform, -1);
332
333 unsigned i;
334 for (i = 0; _mesa_builtin_uniform_desc[i].name != NULL; i++) {
335 if (strcmp(_mesa_builtin_uniform_desc[i].name, name) == 0) {
336 break;
337 }
338 }
339
340 assert(_mesa_builtin_uniform_desc[i].name != NULL);
341 const struct gl_builtin_uniform_desc* const statevar =
342 &_mesa_builtin_uniform_desc[i];
343
344 const unsigned array_count = type->is_array() ? type->length : 1;
345 uni->num_state_slots = array_count * statevar->num_elements;
346
347 ir_state_slot *slots =
348 ralloc_array(uni, ir_state_slot, uni->num_state_slots);
349
350 uni->state_slots = slots;
351
352 for (unsigned a = 0; a < array_count; a++) {
353 for (unsigned j = 0; j < statevar->num_elements; j++) {
354 struct gl_builtin_uniform_element *element = &statevar->elements[j];
355
356 memcpy(slots->tokens, element->tokens, sizeof(element->tokens));
357 if (type->is_array()) {
358 slots->tokens[1] = a;
359 }
360
361 slots->swizzle = element->swizzle;
362 slots++;
363 }
364 }
365
366 return uni;
Eric Anholt85b5dba2010-07-28 12:23:51 -0700367}
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700368
369static void
Ian Romanickdfdff942011-01-24 16:45:11 -0800370add_builtin_variable(exec_list *instructions, glsl_symbol_table *symtab,
371 const builtin_variable *proto)
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700372{
373 /* Create a new variable declaration from the description supplied by
374 * the caller.
375 */
376 const glsl_type *const type = symtab->get_type(proto->type);
377
378 assert(type != NULL);
379
Ian Romanick89d81ab2011-01-25 10:41:20 -0800380 if (proto->mode == ir_var_uniform) {
381 add_uniform(instructions, symtab, proto->name, type);
382 } else {
383 add_variable(instructions, symtab, proto->name, type, proto->mode,
384 proto->slot);
385 }
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700386}
387
Eric Anholtf8946692010-07-20 14:03:35 -0700388static void
Ian Romanickdfdff942011-01-24 16:45:11 -0800389add_builtin_constant(exec_list *instructions, glsl_symbol_table *symtab,
Eric Anholtf8946692010-07-20 14:03:35 -0700390 const char *name, int value)
391{
Ian Romanickdfdff942011-01-24 16:45:11 -0800392 ir_variable *const var = add_variable(instructions, symtab,
393 name, glsl_type::int_type,
394 ir_var_auto, -1);
Eric Anholtf8946692010-07-20 14:03:35 -0700395 var->constant_value = new(var) ir_constant(value);
396}
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700397
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700398/* Several constants in GLSL ES have different names than normal desktop GLSL.
399 * Therefore, this function should only be called on the ES path.
400 */
401static void
402generate_100ES_uniforms(exec_list *instructions,
403 struct _mesa_glsl_parse_state *state)
404{
Ian Romanickdfdff942011-01-24 16:45:11 -0800405 glsl_symbol_table *const symtab = state->symbols;
406
407 add_builtin_constant(instructions, symtab, "gl_MaxVertexAttribs",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700408 state->Const.MaxVertexAttribs);
Ian Romanickdfdff942011-01-24 16:45:11 -0800409 add_builtin_constant(instructions, symtab, "gl_MaxVertexUniformVectors",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700410 state->Const.MaxVertexUniformComponents);
Ian Romanickdfdff942011-01-24 16:45:11 -0800411 add_builtin_constant(instructions, symtab, "gl_MaxVaryingVectors",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700412 state->Const.MaxVaryingFloats / 4);
Ian Romanickdfdff942011-01-24 16:45:11 -0800413 add_builtin_constant(instructions, symtab, "gl_MaxVertexTextureImageUnits",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700414 state->Const.MaxVertexTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800415 add_builtin_constant(instructions, symtab, "gl_MaxCombinedTextureImageUnits",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700416 state->Const.MaxCombinedTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800417 add_builtin_constant(instructions, symtab, "gl_MaxTextureImageUnits",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700418 state->Const.MaxTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800419 add_builtin_constant(instructions, symtab, "gl_MaxFragmentUniformVectors",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700420 state->Const.MaxFragmentUniformComponents);
421
Ian Romanickdfdff942011-01-24 16:45:11 -0800422 add_uniform(instructions, symtab, "gl_DepthRange",
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700423 state->symbols->get_type("gl_DepthRangeParameters"));
424}
425
Eric Anholt78fe3c92010-03-28 01:46:48 -0700426static void
427generate_110_uniforms(exec_list *instructions,
Ian Romanick127308b2010-07-01 13:30:50 -0700428 struct _mesa_glsl_parse_state *state)
Eric Anholt78fe3c92010-03-28 01:46:48 -0700429{
Ian Romanickdfdff942011-01-24 16:45:11 -0800430 glsl_symbol_table *const symtab = state->symbols;
431
Eric Anholt78fe3c92010-03-28 01:46:48 -0700432 for (unsigned i = 0
433 ; i < Elements(builtin_110_deprecated_uniforms)
434 ; i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800435 add_builtin_variable(instructions, symtab,
436 & builtin_110_deprecated_uniforms[i]);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700437 }
438
Ian Romanickdfdff942011-01-24 16:45:11 -0800439 add_builtin_constant(instructions, symtab, "gl_MaxLights",
Eric Anholtf8946692010-07-20 14:03:35 -0700440 state->Const.MaxLights);
Ian Romanickdfdff942011-01-24 16:45:11 -0800441 add_builtin_constant(instructions, symtab, "gl_MaxClipPlanes",
Eric Anholtf8946692010-07-20 14:03:35 -0700442 state->Const.MaxClipPlanes);
Ian Romanickdfdff942011-01-24 16:45:11 -0800443 add_builtin_constant(instructions, symtab, "gl_MaxTextureUnits",
Eric Anholtf8946692010-07-20 14:03:35 -0700444 state->Const.MaxTextureUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800445 add_builtin_constant(instructions, symtab, "gl_MaxTextureCoords",
Eric Anholtf8946692010-07-20 14:03:35 -0700446 state->Const.MaxTextureCoords);
Ian Romanickdfdff942011-01-24 16:45:11 -0800447 add_builtin_constant(instructions, symtab, "gl_MaxVertexAttribs",
Eric Anholtf8946692010-07-20 14:03:35 -0700448 state->Const.MaxVertexAttribs);
Ian Romanickdfdff942011-01-24 16:45:11 -0800449 add_builtin_constant(instructions, symtab, "gl_MaxVertexUniformComponents",
Eric Anholtf8946692010-07-20 14:03:35 -0700450 state->Const.MaxVertexUniformComponents);
Ian Romanickdfdff942011-01-24 16:45:11 -0800451 add_builtin_constant(instructions, symtab, "gl_MaxVaryingFloats",
Eric Anholtf8946692010-07-20 14:03:35 -0700452 state->Const.MaxVaryingFloats);
Ian Romanickdfdff942011-01-24 16:45:11 -0800453 add_builtin_constant(instructions, symtab, "gl_MaxVertexTextureImageUnits",
Eric Anholtf8946692010-07-20 14:03:35 -0700454 state->Const.MaxVertexTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800455 add_builtin_constant(instructions, symtab, "gl_MaxCombinedTextureImageUnits",
Eric Anholtf8946692010-07-20 14:03:35 -0700456 state->Const.MaxCombinedTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800457 add_builtin_constant(instructions, symtab, "gl_MaxTextureImageUnits",
Eric Anholtf8946692010-07-20 14:03:35 -0700458 state->Const.MaxTextureImageUnits);
Ian Romanickdfdff942011-01-24 16:45:11 -0800459 add_builtin_constant(instructions, symtab, "gl_MaxFragmentUniformComponents",
Eric Anholtf8946692010-07-20 14:03:35 -0700460 state->Const.MaxFragmentUniformComponents);
Ian Romanick127308b2010-07-01 13:30:50 -0700461
Ian Romanick3eba5932010-04-26 14:59:32 -0700462 const glsl_type *const mat4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700463 glsl_type::get_array_instance(glsl_type::mat4_type,
Ian Romanick127308b2010-07-01 13:30:50 -0700464 state->Const.MaxTextureCoords);
Ian Romanick3eba5932010-04-26 14:59:32 -0700465
Ian Romanickdfdff942011-01-24 16:45:11 -0800466 add_uniform(instructions, symtab, "gl_TextureMatrix", mat4_array_type);
467 add_uniform(instructions, symtab, "gl_TextureMatrixInverse", mat4_array_type);
468 add_uniform(instructions, symtab, "gl_TextureMatrixTranspose", mat4_array_type);
469 add_uniform(instructions, symtab, "gl_TextureMatrixInverseTranspose", mat4_array_type);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700470
Ian Romanickdfdff942011-01-24 16:45:11 -0800471 add_uniform(instructions, symtab, "gl_DepthRange",
472 symtab->get_type("gl_DepthRangeParameters"));
Eric Anholt78fe3c92010-03-28 01:46:48 -0700473
Ian Romanickdfdff942011-01-24 16:45:11 -0800474 add_uniform(instructions, symtab, "gl_ClipPlane",
Eric Anholt85b5dba2010-07-28 12:23:51 -0700475 glsl_type::get_array_instance(glsl_type::vec4_type,
476 state->Const.MaxClipPlanes));
Ian Romanickdfdff942011-01-24 16:45:11 -0800477 add_uniform(instructions, symtab, "gl_Point",
478 symtab->get_type("gl_PointParameters"));
Eric Anholt85b5dba2010-07-28 12:23:51 -0700479
480 const glsl_type *const material_parameters_type =
Ian Romanickdfdff942011-01-24 16:45:11 -0800481 symtab->get_type("gl_MaterialParameters");
482 add_uniform(instructions, symtab, "gl_FrontMaterial", material_parameters_type);
483 add_uniform(instructions, symtab, "gl_BackMaterial", material_parameters_type);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700484
Eric Anholtaa579432010-05-19 14:09:04 -0700485 const glsl_type *const light_source_array_type =
Ian Romanickdfdff942011-01-24 16:45:11 -0800486 glsl_type::get_array_instance(symtab->get_type("gl_LightSourceParameters"), state->Const.MaxLights);
Eric Anholtaa579432010-05-19 14:09:04 -0700487
Ian Romanickdfdff942011-01-24 16:45:11 -0800488 add_uniform(instructions, symtab, "gl_LightSource", light_source_array_type);
Eric Anholtaa579432010-05-19 14:09:04 -0700489
Eric Anholt85b5dba2010-07-28 12:23:51 -0700490 const glsl_type *const light_model_products_type =
Ian Romanickdfdff942011-01-24 16:45:11 -0800491 symtab->get_type("gl_LightModelProducts");
492 add_uniform(instructions, symtab, "gl_FrontLightModelProduct",
Eric Anholt85b5dba2010-07-28 12:23:51 -0700493 light_model_products_type);
Ian Romanickdfdff942011-01-24 16:45:11 -0800494 add_uniform(instructions, symtab, "gl_BackLightModelProduct",
Eric Anholt85b5dba2010-07-28 12:23:51 -0700495 light_model_products_type);
496
497 const glsl_type *const light_products_type =
Ian Romanickdfdff942011-01-24 16:45:11 -0800498 glsl_type::get_array_instance(symtab->get_type("gl_LightProducts"),
Eric Anholt85b5dba2010-07-28 12:23:51 -0700499 state->Const.MaxLights);
Ian Romanickdfdff942011-01-24 16:45:11 -0800500 add_uniform(instructions, symtab, "gl_FrontLightProduct", light_products_type);
501 add_uniform(instructions, symtab, "gl_BackLightProduct", light_products_type);
Eric Anholt85b5dba2010-07-28 12:23:51 -0700502
Ian Romanickdfdff942011-01-24 16:45:11 -0800503 add_uniform(instructions, symtab, "gl_TextureEnvColor",
Eric Anholt85b5dba2010-07-28 12:23:51 -0700504 glsl_type::get_array_instance(glsl_type::vec4_type,
505 state->Const.MaxTextureUnits));
506
507 const glsl_type *const texcoords_vec4 =
508 glsl_type::get_array_instance(glsl_type::vec4_type,
509 state->Const.MaxTextureCoords);
Ian Romanickdfdff942011-01-24 16:45:11 -0800510 add_uniform(instructions, symtab, "gl_EyePlaneS", texcoords_vec4);
511 add_uniform(instructions, symtab, "gl_EyePlaneT", texcoords_vec4);
512 add_uniform(instructions, symtab, "gl_EyePlaneR", texcoords_vec4);
513 add_uniform(instructions, symtab, "gl_EyePlaneQ", texcoords_vec4);
514 add_uniform(instructions, symtab, "gl_ObjectPlaneS", texcoords_vec4);
515 add_uniform(instructions, symtab, "gl_ObjectPlaneT", texcoords_vec4);
516 add_uniform(instructions, symtab, "gl_ObjectPlaneR", texcoords_vec4);
517 add_uniform(instructions, symtab, "gl_ObjectPlaneQ", texcoords_vec4);
Eric Anholt85b5dba2010-07-28 12:23:51 -0700518
Ian Romanickdfdff942011-01-24 16:45:11 -0800519 add_uniform(instructions, symtab, "gl_Fog",
520 symtab->get_type("gl_FogParameters"));
Eric Anholt78fe3c92010-03-28 01:46:48 -0700521}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800522
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700523/* This function should only be called for ES, not desktop GL. */
524static void
525generate_100ES_vs_variables(exec_list *instructions,
526 struct _mesa_glsl_parse_state *state)
527{
528 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800529 add_builtin_variable(instructions, state->symbols,
530 & builtin_core_vs_variables[i]);
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700531 }
532
533 generate_100ES_uniforms(instructions, state);
534
535 generate_ARB_draw_buffers_variables(instructions, state, false,
536 vertex_shader);
537}
538
539
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800540static void
541generate_110_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700542 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800543{
544 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800545 add_builtin_variable(instructions, state->symbols,
546 & builtin_core_vs_variables[i]);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800547 }
548
549 for (unsigned i = 0
550 ; i < Elements(builtin_110_deprecated_vs_variables)
551 ; i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800552 add_builtin_variable(instructions, state->symbols,
553 & builtin_110_deprecated_vs_variables[i]);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800554 }
Ian Romanick127308b2010-07-01 13:30:50 -0700555 generate_110_uniforms(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800556
Ian Romanickcd00d5b2010-07-01 13:17:54 -0700557 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
558 *
559 * "As with all arrays, indices used to subscript gl_TexCoord must
560 * either be an integral constant expressions, or this array must be
561 * re-declared by the shader with a size. The size can be at most
562 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
563 * implementation in preserving varying resources."
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800564 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700565 const glsl_type *const vec4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700566 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700567
Ian Romanickdfdff942011-01-24 16:45:11 -0800568 add_variable(instructions, state->symbols,
569 "gl_TexCoord", vec4_array_type, ir_var_out, VERT_RESULT_TEX0);
Ian Romanick22971e92010-06-29 15:29:56 -0700570
571 generate_ARB_draw_buffers_variables(instructions, state, false,
572 vertex_shader);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800573}
574
575
576static void
577generate_120_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700578 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800579{
580 /* GLSL version 1.20 did not add any built-in variables in the vertex
581 * shader.
582 */
Ian Romanick22971e92010-06-29 15:29:56 -0700583 generate_110_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800584}
585
586
587static void
Paul Berry36c4b1a2011-08-11 17:33:06 -0700588generate_130_uniforms(exec_list *instructions,
589 struct _mesa_glsl_parse_state *state)
590{
591 glsl_symbol_table *const symtab = state->symbols;
592
593 add_builtin_constant(instructions, symtab, "gl_MaxClipDistances",
594 state->Const.MaxClipPlanes);
595}
596
597
598static void
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800599generate_130_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700600 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800601{
Ian Romanick22971e92010-06-29 15:29:56 -0700602 generate_120_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800603
604 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800605 add_builtin_variable(instructions, state->symbols,
606 & builtin_130_vs_variables[i]);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800607 }
608
Paul Berry36c4b1a2011-08-11 17:33:06 -0700609 generate_130_uniforms(instructions, state);
610
Paul Berryaf243b52011-08-11 15:03:19 -0700611 /* From the GLSL 1.30 spec, section 7.1 (Vertex Shader Special
612 * Variables):
613 *
614 * The gl_ClipDistance array is predeclared as unsized and must
615 * be sized by the shader either redeclaring it with a size or
616 * indexing it only with integral constant expressions.
617 *
618 * We represent this in Mesa by initially declaring the array as
619 * size 0.
620 */
Eric Anholt271e1992010-04-02 23:47:06 -0700621 const glsl_type *const clip_distance_array_type =
Paul Berryaf243b52011-08-11 15:03:19 -0700622 glsl_type::get_array_instance(glsl_type::float_type, 0);
Ian Romanicked0626e2010-06-21 11:42:57 -0700623
Ian Romanickdfdff942011-01-24 16:45:11 -0800624 add_variable(instructions, state->symbols,
Paul Berry3d7c5a42011-08-19 13:12:23 -0700625 "gl_ClipDistance", clip_distance_array_type, ir_var_out,
626 VERT_RESULT_CLIP_DIST0);
Eric Anholt271e1992010-04-02 23:47:06 -0700627
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800628}
629
630
631static void
632initialize_vs_variables(exec_list *instructions,
633 struct _mesa_glsl_parse_state *state)
634{
635
636 switch (state->language_version) {
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700637 case 100:
638 generate_100ES_vs_variables(instructions, state);
639 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800640 case 110:
Ian Romanick22971e92010-06-29 15:29:56 -0700641 generate_110_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800642 break;
643 case 120:
Ian Romanick22971e92010-06-29 15:29:56 -0700644 generate_120_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800645 break;
646 case 130:
Ian Romanick22971e92010-06-29 15:29:56 -0700647 generate_130_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800648 break;
649 }
Brian Paul7ce18632010-12-08 18:25:38 -0700650
651 if (state->ARB_draw_instanced_enable)
652 generate_ARB_draw_instanced_variables(instructions, state, false,
653 vertex_shader);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800654}
655
Brian Paul7ce18632010-12-08 18:25:38 -0700656
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700657/* This function should only be called for ES, not desktop GL. */
658static void
659generate_100ES_fs_variables(exec_list *instructions,
660 struct _mesa_glsl_parse_state *state)
661{
662 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800663 add_builtin_variable(instructions, state->symbols,
664 & builtin_core_fs_variables[i]);
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700665 }
666
667 for (unsigned i = 0; i < Elements(builtin_100ES_fs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800668 add_builtin_variable(instructions, state->symbols,
669 & builtin_100ES_fs_variables[i]);
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700670 }
671
672 generate_100ES_uniforms(instructions, state);
673
674 generate_ARB_draw_buffers_variables(instructions, state, false,
675 fragment_shader);
676}
677
Eric Anholtb3f743a2010-03-25 14:48:25 -0700678static void
679generate_110_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700680 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700681{
682 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800683 add_builtin_variable(instructions, state->symbols,
684 & builtin_core_fs_variables[i]);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700685 }
686
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700687 for (unsigned i = 0; i < Elements(builtin_110_fs_variables); i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800688 add_builtin_variable(instructions, state->symbols,
689 & builtin_110_fs_variables[i]);
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700690 }
691
Eric Anholt0f09aea2010-03-27 12:48:57 -0700692 for (unsigned i = 0
693 ; i < Elements(builtin_110_deprecated_fs_variables)
694 ; i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800695 add_builtin_variable(instructions, state->symbols,
696 & builtin_110_deprecated_fs_variables[i]);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700697 }
Ian Romanick127308b2010-07-01 13:30:50 -0700698 generate_110_uniforms(instructions, state);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700699
Ian Romanickcd00d5b2010-07-01 13:17:54 -0700700 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
701 *
702 * "As with all arrays, indices used to subscript gl_TexCoord must
703 * either be an integral constant expressions, or this array must be
704 * re-declared by the shader with a size. The size can be at most
705 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
706 * implementation in preserving varying resources."
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700707 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700708 const glsl_type *const vec4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700709 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700710
Ian Romanickdfdff942011-01-24 16:45:11 -0800711 add_variable(instructions, state->symbols,
712 "gl_TexCoord", vec4_array_type, ir_var_in, FRAG_ATTRIB_TEX0);
Ian Romanick9c4b1f22010-06-29 15:10:09 -0700713
Ian Romanick22971e92010-06-29 15:29:56 -0700714 generate_ARB_draw_buffers_variables(instructions, state, false,
715 fragment_shader);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700716}
717
Ian Romanickc77b2572010-04-07 16:59:46 -0700718
719static void
Ian Romanick22971e92010-06-29 15:29:56 -0700720generate_ARB_draw_buffers_variables(exec_list *instructions,
721 struct _mesa_glsl_parse_state *state,
722 bool warn, _mesa_glsl_parser_targets target)
Ian Romanickc77b2572010-04-07 16:59:46 -0700723{
Ian Romanick22971e92010-06-29 15:29:56 -0700724 /* gl_MaxDrawBuffers is available in all shader stages.
725 */
Ian Romanicke2f84f02010-06-29 15:19:11 -0700726 ir_variable *const mdb =
Ian Romanickdfdff942011-01-24 16:45:11 -0800727 add_variable(instructions, state->symbols,
728 "gl_MaxDrawBuffers", glsl_type::int_type, ir_var_auto, -1);
Ian Romanicke2f84f02010-06-29 15:19:11 -0700729
730 if (warn)
731 mdb->warn_extension = "GL_ARB_draw_buffers";
732
733 mdb->constant_value = new(mdb)
734 ir_constant(int(state->Const.MaxDrawBuffers));
735
Ian Romanickc77b2572010-04-07 16:59:46 -0700736
Ian Romanick22971e92010-06-29 15:29:56 -0700737 /* gl_FragData is only available in the fragment shader.
738 */
739 if (target == fragment_shader) {
740 const glsl_type *const vec4_array_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700741 glsl_type::get_array_instance(glsl_type::vec4_type,
Ian Romanick22971e92010-06-29 15:29:56 -0700742 state->Const.MaxDrawBuffers);
Ian Romanickc77b2572010-04-07 16:59:46 -0700743
Ian Romanick22971e92010-06-29 15:29:56 -0700744 ir_variable *const fd =
Ian Romanickdfdff942011-01-24 16:45:11 -0800745 add_variable(instructions, state->symbols,
746 "gl_FragData", vec4_array_type,
747 ir_var_out, FRAG_RESULT_DATA0);
Ian Romanick22971e92010-06-29 15:29:56 -0700748
749 if (warn)
750 fd->warn_extension = "GL_ARB_draw_buffers";
751 }
Ian Romanickc77b2572010-04-07 16:59:46 -0700752}
753
Brian Paul7ce18632010-12-08 18:25:38 -0700754
755static void
756generate_ARB_draw_instanced_variables(exec_list *instructions,
757 struct _mesa_glsl_parse_state *state,
758 bool warn,
759 _mesa_glsl_parser_targets target)
760{
761 /* gl_InstanceIDARB is only available in the vertex shader.
762 */
763 if (target == vertex_shader) {
764 ir_variable *const inst =
Ian Romanickdfdff942011-01-24 16:45:11 -0800765 add_variable(instructions, state->symbols,
766 "gl_InstanceIDARB", glsl_type::int_type,
767 ir_var_system_value, SYSTEM_VALUE_INSTANCE_ID);
Brian Paul7ce18632010-12-08 18:25:38 -0700768
769 if (warn)
770 inst->warn_extension = "GL_ARB_draw_instanced";
771 }
772}
773
774
Dave Airlied9671862010-10-06 09:36:02 +1000775static void
776generate_ARB_shader_stencil_export_variables(exec_list *instructions,
777 struct _mesa_glsl_parse_state *state,
778 bool warn)
779{
780 /* gl_FragStencilRefARB is only available in the fragment shader.
781 */
782 ir_variable *const fd =
Ian Romanickdfdff942011-01-24 16:45:11 -0800783 add_variable(instructions, state->symbols,
784 "gl_FragStencilRefARB", glsl_type::int_type,
785 ir_var_out, FRAG_RESULT_STENCIL);
Dave Airlied9671862010-10-06 09:36:02 +1000786
787 if (warn)
788 fd->warn_extension = "GL_ARB_shader_stencil_export";
789}
Ian Romanickc77b2572010-04-07 16:59:46 -0700790
Eric Anholtb3f743a2010-03-25 14:48:25 -0700791static void
Marek Olšák5ba2e7a2011-05-02 16:41:04 +0200792generate_AMD_shader_stencil_export_variables(exec_list *instructions,
793 struct _mesa_glsl_parse_state *state,
794 bool warn)
795{
796 /* gl_FragStencilRefAMD is only available in the fragment shader.
797 */
798 ir_variable *const fd =
799 add_variable(instructions, state->symbols,
800 "gl_FragStencilRefAMD", glsl_type::int_type,
801 ir_var_out, FRAG_RESULT_STENCIL);
802
803 if (warn)
804 fd->warn_extension = "GL_AMD_shader_stencil_export";
805}
806
807static void
Eric Anholtb3f743a2010-03-25 14:48:25 -0700808generate_120_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700809 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700810{
Ian Romanick5e18b052010-06-29 14:21:05 -0700811 generate_110_fs_variables(instructions, state);
Eric Anholt152b55e2010-07-07 19:45:22 -0700812
813 for (unsigned i = 0
814 ; i < Elements(builtin_120_fs_variables)
815 ; i++) {
Ian Romanickdfdff942011-01-24 16:45:11 -0800816 add_builtin_variable(instructions, state->symbols,
817 & builtin_120_fs_variables[i]);
Eric Anholt152b55e2010-07-07 19:45:22 -0700818 }
Eric Anholtb3f743a2010-03-25 14:48:25 -0700819}
820
821static void
822generate_130_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700823 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700824{
Ian Romanick5e18b052010-06-29 14:21:05 -0700825 generate_120_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700826
Paul Berry36c4b1a2011-08-11 17:33:06 -0700827 generate_130_uniforms(instructions, state);
828
Paul Berryaf243b52011-08-11 15:03:19 -0700829 /* From the GLSL 1.30 spec, section 7.2 (Fragment Shader Special
830 * Variables):
831 *
832 * The built-in input variable gl_ClipDistance array contains linearly
833 * interpolated values for the vertex values written by the vertex shader
834 * to the gl_ClipDistance vertex output variable. This array must be
835 * sized in the fragment shader either implicitly or explicitly to be the
836 * same size as it was sized in the vertex shader.
837 *
838 * In other words, the array must be pre-declared as implicitly sized. We
839 * represent this in Mesa by initially declaring the array as size 0.
840 */
Ian Romanick8645a952010-04-07 16:47:44 -0700841 const glsl_type *const clip_distance_array_type =
Paul Berryaf243b52011-08-11 15:03:19 -0700842 glsl_type::get_array_instance(glsl_type::float_type, 0);
Ian Romanicked0626e2010-06-21 11:42:57 -0700843
Ian Romanickdfdff942011-01-24 16:45:11 -0800844 add_variable(instructions, state->symbols,
Paul Berry3d7c5a42011-08-19 13:12:23 -0700845 "gl_ClipDistance", clip_distance_array_type, ir_var_in,
846 FRAG_ATTRIB_CLIP_DIST0);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700847}
848
849static void
850initialize_fs_variables(exec_list *instructions,
851 struct _mesa_glsl_parse_state *state)
852{
853
854 switch (state->language_version) {
Kenneth Graunkeb4fe4d52010-08-07 02:45:33 -0700855 case 100:
856 generate_100ES_fs_variables(instructions, state);
857 break;
Eric Anholtb3f743a2010-03-25 14:48:25 -0700858 case 110:
Ian Romanick5e18b052010-06-29 14:21:05 -0700859 generate_110_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700860 break;
861 case 120:
Ian Romanick5e18b052010-06-29 14:21:05 -0700862 generate_120_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700863 break;
864 case 130:
Ian Romanick5e18b052010-06-29 14:21:05 -0700865 generate_130_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700866 break;
867 }
Dave Airlied9671862010-10-06 09:36:02 +1000868
869 if (state->ARB_shader_stencil_export_enable)
870 generate_ARB_shader_stencil_export_variables(instructions, state,
871 state->ARB_shader_stencil_export_warn);
Marek Olšák5ba2e7a2011-05-02 16:41:04 +0200872
873 if (state->AMD_shader_stencil_export_enable)
874 generate_AMD_shader_stencil_export_variables(instructions, state,
875 state->AMD_shader_stencil_export_warn);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700876}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800877
878void
879_mesa_glsl_initialize_variables(exec_list *instructions,
880 struct _mesa_glsl_parse_state *state)
881{
882 switch (state->target) {
883 case vertex_shader:
884 initialize_vs_variables(instructions, state);
885 break;
886 case geometry_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700887 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800888 case fragment_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700889 initialize_fs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800890 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800891 }
892}