glsl: Don't allow vertex shader input arrays until GLSL 1.50.
[mesa.git] / src / glsl / builtin_variables.cpp
1 /*
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
24 #include "ir.h"
25 #include "glsl_parser_extras.h"
26 #include "glsl_symbol_table.h"
27 #include "main/core.h"
28 #include "main/uniforms.h"
29 #include "program/prog_parameter.h"
30 #include "program/prog_statevars.h"
31 #include "program/prog_instruction.h"
32
33 static void generate_ARB_draw_buffers_variables(exec_list *,
34 struct _mesa_glsl_parse_state *,
35 bool, _mesa_glsl_parser_targets);
36
37 static void
38 generate_ARB_draw_instanced_variables(exec_list *,
39 struct _mesa_glsl_parse_state *,
40 bool, _mesa_glsl_parser_targets);
41
42 static void
43 generate_AMD_vertex_shader_layer_variables(exec_list *instructions,
44 struct _mesa_glsl_parse_state *state,
45 bool warn,
46 _mesa_glsl_parser_targets target);
47
48 struct builtin_variable {
49 enum ir_variable_mode mode;
50 int slot;
51 const char *type;
52 const char *name;
53 };
54
55 static const builtin_variable builtin_core_vs_variables[] = {
56 { ir_var_shader_out, VARYING_SLOT_POS, "vec4", "gl_Position" },
57 { ir_var_shader_out, VARYING_SLOT_PSIZ, "float", "gl_PointSize" },
58 };
59
60 static const builtin_variable builtin_core_fs_variables[] = {
61 { ir_var_shader_in, VARYING_SLOT_POS, "vec4", "gl_FragCoord" },
62 { ir_var_shader_in, VARYING_SLOT_FACE, "bool", "gl_FrontFacing" },
63 { ir_var_shader_out, FRAG_RESULT_COLOR, "vec4", "gl_FragColor" },
64 };
65
66 static const builtin_variable builtin_100ES_fs_variables[] = {
67 { ir_var_shader_in, VARYING_SLOT_PNTC, "vec2", "gl_PointCoord" },
68 };
69
70 static const builtin_variable builtin_300ES_vs_variables[] = {
71 { ir_var_system_value, SYSTEM_VALUE_VERTEX_ID, "int", "gl_VertexID" },
72 };
73
74 static const builtin_variable builtin_300ES_fs_variables[] = {
75 { ir_var_shader_in, VARYING_SLOT_POS, "vec4", "gl_FragCoord" },
76 { ir_var_shader_in, VARYING_SLOT_FACE, "bool", "gl_FrontFacing" },
77 { ir_var_shader_out, FRAG_RESULT_DEPTH, "float", "gl_FragDepth" },
78 { ir_var_shader_in, VARYING_SLOT_PNTC, "vec2", "gl_PointCoord" },
79 };
80
81 static const builtin_variable builtin_110_fs_variables[] = {
82 { ir_var_shader_out, FRAG_RESULT_DEPTH, "float", "gl_FragDepth" },
83 };
84
85 static const builtin_variable builtin_110_deprecated_fs_variables[] = {
86 { ir_var_shader_in, VARYING_SLOT_COL0, "vec4", "gl_Color" },
87 { ir_var_shader_in, VARYING_SLOT_COL1, "vec4", "gl_SecondaryColor" },
88 { ir_var_shader_in, VARYING_SLOT_FOGC, "float", "gl_FogFragCoord" },
89 };
90
91 static const builtin_variable builtin_110_deprecated_vs_variables[] = {
92 { ir_var_shader_in, VERT_ATTRIB_POS, "vec4", "gl_Vertex" },
93 { ir_var_shader_in, VERT_ATTRIB_NORMAL, "vec3", "gl_Normal" },
94 { ir_var_shader_in, VERT_ATTRIB_COLOR0, "vec4", "gl_Color" },
95 { ir_var_shader_in, VERT_ATTRIB_COLOR1, "vec4", "gl_SecondaryColor" },
96 { ir_var_shader_in, VERT_ATTRIB_TEX0, "vec4", "gl_MultiTexCoord0" },
97 { ir_var_shader_in, VERT_ATTRIB_TEX1, "vec4", "gl_MultiTexCoord1" },
98 { ir_var_shader_in, VERT_ATTRIB_TEX2, "vec4", "gl_MultiTexCoord2" },
99 { ir_var_shader_in, VERT_ATTRIB_TEX3, "vec4", "gl_MultiTexCoord3" },
100 { ir_var_shader_in, VERT_ATTRIB_TEX4, "vec4", "gl_MultiTexCoord4" },
101 { ir_var_shader_in, VERT_ATTRIB_TEX5, "vec4", "gl_MultiTexCoord5" },
102 { ir_var_shader_in, VERT_ATTRIB_TEX6, "vec4", "gl_MultiTexCoord6" },
103 { ir_var_shader_in, VERT_ATTRIB_TEX7, "vec4", "gl_MultiTexCoord7" },
104 { ir_var_shader_in, VERT_ATTRIB_FOG, "float", "gl_FogCoord" },
105 { ir_var_shader_out, VARYING_SLOT_CLIP_VERTEX, "vec4", "gl_ClipVertex" },
106 { ir_var_shader_out, VARYING_SLOT_COL0, "vec4", "gl_FrontColor" },
107 { ir_var_shader_out, VARYING_SLOT_BFC0, "vec4", "gl_BackColor" },
108 { ir_var_shader_out, VARYING_SLOT_COL1, "vec4", "gl_FrontSecondaryColor" },
109 { ir_var_shader_out, VARYING_SLOT_BFC1, "vec4", "gl_BackSecondaryColor" },
110 { ir_var_shader_out, VARYING_SLOT_FOGC, "float", "gl_FogFragCoord" },
111 };
112
113 static const builtin_variable builtin_120_fs_variables[] = {
114 { ir_var_shader_in, VARYING_SLOT_PNTC, "vec2", "gl_PointCoord" },
115 };
116
117 static const builtin_variable builtin_130_vs_variables[] = {
118 { ir_var_system_value, SYSTEM_VALUE_VERTEX_ID, "int", "gl_VertexID" },
119 };
120
121 static const builtin_variable builtin_110_deprecated_uniforms[] = {
122 { ir_var_uniform, -1, "mat4", "gl_ModelViewMatrix" },
123 { ir_var_uniform, -1, "mat4", "gl_ProjectionMatrix" },
124 { ir_var_uniform, -1, "mat4", "gl_ModelViewProjectionMatrix" },
125 { ir_var_uniform, -1, "mat3", "gl_NormalMatrix" },
126 { ir_var_uniform, -1, "mat4", "gl_ModelViewMatrixInverse" },
127 { ir_var_uniform, -1, "mat4", "gl_ProjectionMatrixInverse" },
128 { ir_var_uniform, -1, "mat4", "gl_ModelViewProjectionMatrixInverse" },
129 { ir_var_uniform, -1, "mat4", "gl_ModelViewMatrixTranspose" },
130 { ir_var_uniform, -1, "mat4", "gl_ProjectionMatrixTranspose" },
131 { ir_var_uniform, -1, "mat4", "gl_ModelViewProjectionMatrixTranspose" },
132 { ir_var_uniform, -1, "mat4", "gl_ModelViewMatrixInverseTranspose" },
133 { ir_var_uniform, -1, "mat4", "gl_ProjectionMatrixInverseTranspose" },
134 { ir_var_uniform, -1, "mat4", "gl_ModelViewProjectionMatrixInverseTranspose" },
135 { ir_var_uniform, -1, "float", "gl_NormalScale" },
136 { ir_var_uniform, -1, "gl_LightModelParameters", "gl_LightModel"},
137
138 /* Mesa-internal ATI_envmap_bumpmap state. */
139 { ir_var_uniform, -1, "vec2", "gl_BumpRotMatrix0MESA"},
140 { ir_var_uniform, -1, "vec2", "gl_BumpRotMatrix1MESA"},
141 { ir_var_uniform, -1, "vec4", "gl_FogParamsOptimizedMESA"},
142 };
143
144 static struct gl_builtin_uniform_element gl_DepthRange_elements[] = {
145 {"near", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_XXXX},
146 {"far", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_YYYY},
147 {"diff", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_ZZZZ},
148 };
149
150 static struct gl_builtin_uniform_element gl_ClipPlane_elements[] = {
151 {NULL, {STATE_CLIPPLANE, 0, 0}, SWIZZLE_XYZW}
152 };
153
154 static struct gl_builtin_uniform_element gl_Point_elements[] = {
155 {"size", {STATE_POINT_SIZE}, SWIZZLE_XXXX},
156 {"sizeMin", {STATE_POINT_SIZE}, SWIZZLE_YYYY},
157 {"sizeMax", {STATE_POINT_SIZE}, SWIZZLE_ZZZZ},
158 {"fadeThresholdSize", {STATE_POINT_SIZE}, SWIZZLE_WWWW},
159 {"distanceConstantAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_XXXX},
160 {"distanceLinearAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_YYYY},
161 {"distanceQuadraticAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_ZZZZ},
162 };
163
164 static struct gl_builtin_uniform_element gl_FrontMaterial_elements[] = {
165 {"emission", {STATE_MATERIAL, 0, STATE_EMISSION}, SWIZZLE_XYZW},
166 {"ambient", {STATE_MATERIAL, 0, STATE_AMBIENT}, SWIZZLE_XYZW},
167 {"diffuse", {STATE_MATERIAL, 0, STATE_DIFFUSE}, SWIZZLE_XYZW},
168 {"specular", {STATE_MATERIAL, 0, STATE_SPECULAR}, SWIZZLE_XYZW},
169 {"shininess", {STATE_MATERIAL, 0, STATE_SHININESS}, SWIZZLE_XXXX},
170 };
171
172 static struct gl_builtin_uniform_element gl_BackMaterial_elements[] = {
173 {"emission", {STATE_MATERIAL, 1, STATE_EMISSION}, SWIZZLE_XYZW},
174 {"ambient", {STATE_MATERIAL, 1, STATE_AMBIENT}, SWIZZLE_XYZW},
175 {"diffuse", {STATE_MATERIAL, 1, STATE_DIFFUSE}, SWIZZLE_XYZW},
176 {"specular", {STATE_MATERIAL, 1, STATE_SPECULAR}, SWIZZLE_XYZW},
177 {"shininess", {STATE_MATERIAL, 1, STATE_SHININESS}, SWIZZLE_XXXX},
178 };
179
180 static struct gl_builtin_uniform_element gl_LightSource_elements[] = {
181 {"ambient", {STATE_LIGHT, 0, STATE_AMBIENT}, SWIZZLE_XYZW},
182 {"diffuse", {STATE_LIGHT, 0, STATE_DIFFUSE}, SWIZZLE_XYZW},
183 {"specular", {STATE_LIGHT, 0, STATE_SPECULAR}, SWIZZLE_XYZW},
184 {"position", {STATE_LIGHT, 0, STATE_POSITION}, SWIZZLE_XYZW},
185 {"halfVector", {STATE_LIGHT, 0, STATE_HALF_VECTOR}, SWIZZLE_XYZW},
186 {"spotDirection", {STATE_LIGHT, 0, STATE_SPOT_DIRECTION},
187 MAKE_SWIZZLE4(SWIZZLE_X,
188 SWIZZLE_Y,
189 SWIZZLE_Z,
190 SWIZZLE_Z)},
191 {"spotCosCutoff", {STATE_LIGHT, 0, STATE_SPOT_DIRECTION}, SWIZZLE_WWWW},
192 {"spotCutoff", {STATE_LIGHT, 0, STATE_SPOT_CUTOFF}, SWIZZLE_XXXX},
193 {"spotExponent", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_WWWW},
194 {"constantAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_XXXX},
195 {"linearAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_YYYY},
196 {"quadraticAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_ZZZZ},
197 };
198
199 static struct gl_builtin_uniform_element gl_LightModel_elements[] = {
200 {"ambient", {STATE_LIGHTMODEL_AMBIENT, 0}, SWIZZLE_XYZW},
201 };
202
203 static struct gl_builtin_uniform_element gl_FrontLightModelProduct_elements[] = {
204 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR, 0}, SWIZZLE_XYZW},
205 };
206
207 static struct gl_builtin_uniform_element gl_BackLightModelProduct_elements[] = {
208 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR, 1}, SWIZZLE_XYZW},
209 };
210
211 static struct gl_builtin_uniform_element gl_FrontLightProduct_elements[] = {
212 {"ambient", {STATE_LIGHTPROD, 0, 0, STATE_AMBIENT}, SWIZZLE_XYZW},
213 {"diffuse", {STATE_LIGHTPROD, 0, 0, STATE_DIFFUSE}, SWIZZLE_XYZW},
214 {"specular", {STATE_LIGHTPROD, 0, 0, STATE_SPECULAR}, SWIZZLE_XYZW},
215 };
216
217 static struct gl_builtin_uniform_element gl_BackLightProduct_elements[] = {
218 {"ambient", {STATE_LIGHTPROD, 0, 1, STATE_AMBIENT}, SWIZZLE_XYZW},
219 {"diffuse", {STATE_LIGHTPROD, 0, 1, STATE_DIFFUSE}, SWIZZLE_XYZW},
220 {"specular", {STATE_LIGHTPROD, 0, 1, STATE_SPECULAR}, SWIZZLE_XYZW},
221 };
222
223 static struct gl_builtin_uniform_element gl_TextureEnvColor_elements[] = {
224 {NULL, {STATE_TEXENV_COLOR, 0}, SWIZZLE_XYZW},
225 };
226
227 static struct gl_builtin_uniform_element gl_EyePlaneS_elements[] = {
228 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_S}, SWIZZLE_XYZW},
229 };
230
231 static struct gl_builtin_uniform_element gl_EyePlaneT_elements[] = {
232 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_T}, SWIZZLE_XYZW},
233 };
234
235 static struct gl_builtin_uniform_element gl_EyePlaneR_elements[] = {
236 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_R}, SWIZZLE_XYZW},
237 };
238
239 static struct gl_builtin_uniform_element gl_EyePlaneQ_elements[] = {
240 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_Q}, SWIZZLE_XYZW},
241 };
242
243 static struct gl_builtin_uniform_element gl_ObjectPlaneS_elements[] = {
244 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_S}, SWIZZLE_XYZW},
245 };
246
247 static struct gl_builtin_uniform_element gl_ObjectPlaneT_elements[] = {
248 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_T}, SWIZZLE_XYZW},
249 };
250
251 static struct gl_builtin_uniform_element gl_ObjectPlaneR_elements[] = {
252 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_R}, SWIZZLE_XYZW},
253 };
254
255 static struct gl_builtin_uniform_element gl_ObjectPlaneQ_elements[] = {
256 {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_Q}, SWIZZLE_XYZW},
257 };
258
259 static struct gl_builtin_uniform_element gl_Fog_elements[] = {
260 {"color", {STATE_FOG_COLOR}, SWIZZLE_XYZW},
261 {"density", {STATE_FOG_PARAMS}, SWIZZLE_XXXX},
262 {"start", {STATE_FOG_PARAMS}, SWIZZLE_YYYY},
263 {"end", {STATE_FOG_PARAMS}, SWIZZLE_ZZZZ},
264 {"scale", {STATE_FOG_PARAMS}, SWIZZLE_WWWW},
265 };
266
267 static struct gl_builtin_uniform_element gl_NormalScale_elements[] = {
268 {NULL, {STATE_NORMAL_SCALE}, SWIZZLE_XXXX},
269 };
270
271 static struct gl_builtin_uniform_element gl_BumpRotMatrix0MESA_elements[] = {
272 {NULL, {STATE_INTERNAL, STATE_ROT_MATRIX_0}, SWIZZLE_XYZW},
273 };
274
275 static struct gl_builtin_uniform_element gl_BumpRotMatrix1MESA_elements[] = {
276 {NULL, {STATE_INTERNAL, STATE_ROT_MATRIX_1}, SWIZZLE_XYZW},
277 };
278
279 static struct gl_builtin_uniform_element gl_FogParamsOptimizedMESA_elements[] = {
280 {NULL, {STATE_INTERNAL, STATE_FOG_PARAMS_OPTIMIZED}, SWIZZLE_XYZW},
281 };
282
283 static struct gl_builtin_uniform_element gl_CurrentAttribVertMESA_elements[] = {
284 {NULL, {STATE_INTERNAL, STATE_CURRENT_ATTRIB, 0}, SWIZZLE_XYZW},
285 };
286
287 static struct gl_builtin_uniform_element gl_CurrentAttribFragMESA_elements[] = {
288 {NULL, {STATE_INTERNAL, STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED, 0}, SWIZZLE_XYZW},
289 };
290
291 #define MATRIX(name, statevar, modifier) \
292 static struct gl_builtin_uniform_element name ## _elements[] = { \
293 { NULL, { statevar, 0, 0, 0, modifier}, SWIZZLE_XYZW }, \
294 { NULL, { statevar, 0, 1, 1, modifier}, SWIZZLE_XYZW }, \
295 { NULL, { statevar, 0, 2, 2, modifier}, SWIZZLE_XYZW }, \
296 { NULL, { statevar, 0, 3, 3, modifier}, SWIZZLE_XYZW }, \
297 }
298
299 MATRIX(gl_ModelViewMatrix,
300 STATE_MODELVIEW_MATRIX, STATE_MATRIX_TRANSPOSE);
301 MATRIX(gl_ModelViewMatrixInverse,
302 STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVTRANS);
303 MATRIX(gl_ModelViewMatrixTranspose,
304 STATE_MODELVIEW_MATRIX, 0);
305 MATRIX(gl_ModelViewMatrixInverseTranspose,
306 STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVERSE);
307
308 MATRIX(gl_ProjectionMatrix,
309 STATE_PROJECTION_MATRIX, STATE_MATRIX_TRANSPOSE);
310 MATRIX(gl_ProjectionMatrixInverse,
311 STATE_PROJECTION_MATRIX, STATE_MATRIX_INVTRANS);
312 MATRIX(gl_ProjectionMatrixTranspose,
313 STATE_PROJECTION_MATRIX, 0);
314 MATRIX(gl_ProjectionMatrixInverseTranspose,
315 STATE_PROJECTION_MATRIX, STATE_MATRIX_INVERSE);
316
317 MATRIX(gl_ModelViewProjectionMatrix,
318 STATE_MVP_MATRIX, STATE_MATRIX_TRANSPOSE);
319 MATRIX(gl_ModelViewProjectionMatrixInverse,
320 STATE_MVP_MATRIX, STATE_MATRIX_INVTRANS);
321 MATRIX(gl_ModelViewProjectionMatrixTranspose,
322 STATE_MVP_MATRIX, 0);
323 MATRIX(gl_ModelViewProjectionMatrixInverseTranspose,
324 STATE_MVP_MATRIX, STATE_MATRIX_INVERSE);
325
326 MATRIX(gl_TextureMatrix,
327 STATE_TEXTURE_MATRIX, STATE_MATRIX_TRANSPOSE);
328 MATRIX(gl_TextureMatrixInverse,
329 STATE_TEXTURE_MATRIX, STATE_MATRIX_INVTRANS);
330 MATRIX(gl_TextureMatrixTranspose,
331 STATE_TEXTURE_MATRIX, 0);
332 MATRIX(gl_TextureMatrixInverseTranspose,
333 STATE_TEXTURE_MATRIX, STATE_MATRIX_INVERSE);
334
335 static struct gl_builtin_uniform_element gl_NormalMatrix_elements[] = {
336 { NULL, { STATE_MODELVIEW_MATRIX, 0, 0, 0, STATE_MATRIX_INVERSE},
337 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) },
338 { NULL, { STATE_MODELVIEW_MATRIX, 0, 1, 1, STATE_MATRIX_INVERSE},
339 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) },
340 { NULL, { STATE_MODELVIEW_MATRIX, 0, 2, 2, STATE_MATRIX_INVERSE},
341 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) },
342 };
343
344 #undef MATRIX
345
346 #define STATEVAR(name) {#name, name ## _elements, Elements(name ## _elements)}
347
348 static const struct gl_builtin_uniform_desc _mesa_builtin_uniform_desc[] = {
349 STATEVAR(gl_DepthRange),
350 STATEVAR(gl_ClipPlane),
351 STATEVAR(gl_Point),
352 STATEVAR(gl_FrontMaterial),
353 STATEVAR(gl_BackMaterial),
354 STATEVAR(gl_LightSource),
355 STATEVAR(gl_LightModel),
356 STATEVAR(gl_FrontLightModelProduct),
357 STATEVAR(gl_BackLightModelProduct),
358 STATEVAR(gl_FrontLightProduct),
359 STATEVAR(gl_BackLightProduct),
360 STATEVAR(gl_TextureEnvColor),
361 STATEVAR(gl_EyePlaneS),
362 STATEVAR(gl_EyePlaneT),
363 STATEVAR(gl_EyePlaneR),
364 STATEVAR(gl_EyePlaneQ),
365 STATEVAR(gl_ObjectPlaneS),
366 STATEVAR(gl_ObjectPlaneT),
367 STATEVAR(gl_ObjectPlaneR),
368 STATEVAR(gl_ObjectPlaneQ),
369 STATEVAR(gl_Fog),
370
371 STATEVAR(gl_ModelViewMatrix),
372 STATEVAR(gl_ModelViewMatrixInverse),
373 STATEVAR(gl_ModelViewMatrixTranspose),
374 STATEVAR(gl_ModelViewMatrixInverseTranspose),
375
376 STATEVAR(gl_ProjectionMatrix),
377 STATEVAR(gl_ProjectionMatrixInverse),
378 STATEVAR(gl_ProjectionMatrixTranspose),
379 STATEVAR(gl_ProjectionMatrixInverseTranspose),
380
381 STATEVAR(gl_ModelViewProjectionMatrix),
382 STATEVAR(gl_ModelViewProjectionMatrixInverse),
383 STATEVAR(gl_ModelViewProjectionMatrixTranspose),
384 STATEVAR(gl_ModelViewProjectionMatrixInverseTranspose),
385
386 STATEVAR(gl_TextureMatrix),
387 STATEVAR(gl_TextureMatrixInverse),
388 STATEVAR(gl_TextureMatrixTranspose),
389 STATEVAR(gl_TextureMatrixInverseTranspose),
390
391 STATEVAR(gl_NormalMatrix),
392 STATEVAR(gl_NormalScale),
393
394 STATEVAR(gl_BumpRotMatrix0MESA),
395 STATEVAR(gl_BumpRotMatrix1MESA),
396 STATEVAR(gl_FogParamsOptimizedMESA),
397 STATEVAR(gl_CurrentAttribVertMESA),
398 STATEVAR(gl_CurrentAttribFragMESA),
399
400 {NULL, NULL, 0}
401 };
402
403 static ir_variable *
404 add_variable(exec_list *instructions, glsl_symbol_table *symtab,
405 const char *name, const glsl_type *type,
406 enum ir_variable_mode mode, int slot)
407 {
408 ir_variable *var = new(symtab) ir_variable(type, name, mode);
409
410 switch (var->mode) {
411 case ir_var_auto:
412 case ir_var_shader_in:
413 case ir_var_uniform:
414 case ir_var_system_value:
415 var->read_only = true;
416 break;
417 case ir_var_shader_out:
418 break;
419 default:
420 /* The only variables that are added using this function should be
421 * uniforms, shader inputs, and shader outputs, constants (which use
422 * ir_var_auto), and system values.
423 */
424 assert(0);
425 break;
426 }
427
428 var->location = slot;
429 var->explicit_location = (slot >= 0);
430 var->explicit_index = 0;
431
432 /* Once the variable is created an initialized, add it to the symbol table
433 * and add the declaration to the IR stream.
434 */
435 instructions->push_tail(var);
436
437 symtab->add_variable(var);
438 return var;
439 }
440
441 static ir_variable *
442 add_uniform(exec_list *instructions, glsl_symbol_table *symtab,
443 const char *name, const glsl_type *type)
444 {
445 ir_variable *const uni =
446 add_variable(instructions, symtab, name, type, ir_var_uniform, -1);
447
448 unsigned i;
449 for (i = 0; _mesa_builtin_uniform_desc[i].name != NULL; i++) {
450 if (strcmp(_mesa_builtin_uniform_desc[i].name, name) == 0) {
451 break;
452 }
453 }
454
455 assert(_mesa_builtin_uniform_desc[i].name != NULL);
456 const struct gl_builtin_uniform_desc* const statevar =
457 &_mesa_builtin_uniform_desc[i];
458
459 const unsigned array_count = type->is_array() ? type->length : 1;
460 uni->num_state_slots = array_count * statevar->num_elements;
461
462 ir_state_slot *slots =
463 ralloc_array(uni, ir_state_slot, uni->num_state_slots);
464
465 uni->state_slots = slots;
466
467 for (unsigned a = 0; a < array_count; a++) {
468 for (unsigned j = 0; j < statevar->num_elements; j++) {
469 struct gl_builtin_uniform_element *element = &statevar->elements[j];
470
471 memcpy(slots->tokens, element->tokens, sizeof(element->tokens));
472 if (type->is_array()) {
473 if (strcmp(name, "gl_CurrentAttribVertMESA") == 0 ||
474 strcmp(name, "gl_CurrentAttribFragMESA") == 0) {
475 slots->tokens[2] = a;
476 } else {
477 slots->tokens[1] = a;
478 }
479 }
480
481 slots->swizzle = element->swizzle;
482 slots++;
483 }
484 }
485
486 return uni;
487 }
488
489 static void
490 add_builtin_variable(exec_list *instructions, glsl_symbol_table *symtab,
491 const builtin_variable *proto)
492 {
493 /* Create a new variable declaration from the description supplied by
494 * the caller.
495 */
496 const glsl_type *const type = symtab->get_type(proto->type);
497
498 assert(type != NULL);
499
500 if (proto->mode == ir_var_uniform) {
501 add_uniform(instructions, symtab, proto->name, type);
502 } else {
503 add_variable(instructions, symtab, proto->name, type, proto->mode,
504 proto->slot);
505 }
506 }
507
508 static ir_variable *
509 add_builtin_constant(exec_list *instructions, glsl_symbol_table *symtab,
510 const char *name, int value)
511 {
512 ir_variable *const var = add_variable(instructions, symtab,
513 name, glsl_type::int_type,
514 ir_var_auto, -1);
515 var->constant_value = new(var) ir_constant(value);
516 var->constant_initializer = new(var) ir_constant(value);
517 var->has_initializer = true;
518 return var;
519 }
520
521 /**
522 * Uniforms that are common to all GLSL ES implementations.
523 *
524 * Several constants in GLSL ES have different names than normal desktop GLSL.
525 * Therefore, this function should only be called on the ES path.
526 */
527 static void
528 generate_common_ES_uniforms(exec_list *instructions,
529 struct _mesa_glsl_parse_state *state)
530 {
531 glsl_symbol_table *const symtab = state->symbols;
532
533 add_builtin_constant(instructions, symtab, "gl_MaxVertexAttribs",
534 state->Const.MaxVertexAttribs);
535 add_builtin_constant(instructions, symtab, "gl_MaxVertexUniformVectors",
536 state->Const.MaxVertexUniformComponents / 4);
537 add_builtin_constant(instructions, symtab, "gl_MaxVertexTextureImageUnits",
538 state->Const.MaxVertexTextureImageUnits);
539 add_builtin_constant(instructions, symtab, "gl_MaxCombinedTextureImageUnits",
540 state->Const.MaxCombinedTextureImageUnits);
541 add_builtin_constant(instructions, symtab, "gl_MaxTextureImageUnits",
542 state->Const.MaxTextureImageUnits);
543 add_builtin_constant(instructions, symtab, "gl_MaxFragmentUniformVectors",
544 state->Const.MaxFragmentUniformComponents / 4);
545
546 add_uniform(instructions, symtab, "gl_DepthRange",
547 state->symbols->get_type("gl_DepthRangeParameters"));
548 }
549
550 static void
551 generate_100ES_uniforms(exec_list *instructions,
552 struct _mesa_glsl_parse_state *state)
553 {
554 generate_common_ES_uniforms(instructions, state);
555
556 glsl_symbol_table *const symtab = state->symbols;
557
558 add_builtin_constant(instructions, symtab, "gl_MaxVaryingVectors",
559 state->Const.MaxVaryingFloats / 4);
560 }
561
562 static void
563 generate_300ES_uniforms(exec_list *instructions,
564 struct _mesa_glsl_parse_state *state)
565 {
566 generate_common_ES_uniforms(instructions, state);
567
568 glsl_symbol_table *const symtab = state->symbols;
569
570 add_builtin_constant(instructions, symtab, "gl_MaxVertexOutputVectors",
571 state->Const.MaxVaryingFloats / 4);
572 add_builtin_constant(instructions, symtab, "gl_MaxFragmentInputVectors",
573 state->Const.MaxVaryingFloats / 4);
574 add_builtin_constant(instructions, symtab, "gl_MinProgramTexelOffset",
575 state->Const.MinProgramTexelOffset);
576 add_builtin_constant(instructions, symtab, "gl_MaxProgramTexelOffset",
577 state->Const.MaxProgramTexelOffset);
578 }
579
580 static void
581 generate_110_uniforms(exec_list *instructions,
582 struct _mesa_glsl_parse_state *state,
583 bool add_deprecated)
584 {
585 glsl_symbol_table *const symtab = state->symbols;
586
587 if (add_deprecated) {
588 for (unsigned i = 0
589 ; i < Elements(builtin_110_deprecated_uniforms)
590 ; i++) {
591 add_builtin_variable(instructions, symtab,
592 & builtin_110_deprecated_uniforms[i]);
593 }
594 }
595
596 if (add_deprecated) {
597 add_builtin_constant(instructions, symtab, "gl_MaxLights",
598 state->Const.MaxLights);
599 add_builtin_constant(instructions, symtab, "gl_MaxClipPlanes",
600 state->Const.MaxClipPlanes);
601 add_builtin_constant(instructions, symtab, "gl_MaxTextureUnits",
602 state->Const.MaxTextureUnits);
603 add_builtin_constant(instructions, symtab, "gl_MaxTextureCoords",
604 state->Const.MaxTextureCoords);
605 }
606 add_builtin_constant(instructions, symtab, "gl_MaxVertexAttribs",
607 state->Const.MaxVertexAttribs);
608 add_builtin_constant(instructions, symtab, "gl_MaxVertexUniformComponents",
609 state->Const.MaxVertexUniformComponents);
610 add_builtin_constant(instructions, symtab, "gl_MaxVaryingFloats",
611 state->Const.MaxVaryingFloats);
612 add_builtin_constant(instructions, symtab, "gl_MaxVertexTextureImageUnits",
613 state->Const.MaxVertexTextureImageUnits);
614 add_builtin_constant(instructions, symtab, "gl_MaxCombinedTextureImageUnits",
615 state->Const.MaxCombinedTextureImageUnits);
616 add_builtin_constant(instructions, symtab, "gl_MaxTextureImageUnits",
617 state->Const.MaxTextureImageUnits);
618 add_builtin_constant(instructions, symtab, "gl_MaxFragmentUniformComponents",
619 state->Const.MaxFragmentUniformComponents);
620
621 if (add_deprecated) {
622 const glsl_type *const mat4_array_type =
623 glsl_type::get_array_instance(glsl_type::mat4_type,
624 state->Const.MaxTextureCoords);
625
626 add_uniform(instructions, symtab, "gl_TextureMatrix", mat4_array_type);
627 add_uniform(instructions, symtab, "gl_TextureMatrixInverse", mat4_array_type);
628 add_uniform(instructions, symtab, "gl_TextureMatrixTranspose", mat4_array_type);
629 add_uniform(instructions, symtab, "gl_TextureMatrixInverseTranspose", mat4_array_type);
630 }
631
632 add_uniform(instructions, symtab, "gl_DepthRange",
633 symtab->get_type("gl_DepthRangeParameters"));
634
635 if (add_deprecated) {
636 add_uniform(instructions, symtab, "gl_ClipPlane",
637 glsl_type::get_array_instance(glsl_type::vec4_type,
638 state->Const.MaxClipPlanes));
639 add_uniform(instructions, symtab, "gl_Point",
640 symtab->get_type("gl_PointParameters"));
641
642 const glsl_type *const material_parameters_type =
643 symtab->get_type("gl_MaterialParameters");
644 add_uniform(instructions, symtab, "gl_FrontMaterial", material_parameters_type);
645 add_uniform(instructions, symtab, "gl_BackMaterial", material_parameters_type);
646
647 const glsl_type *const light_source_array_type =
648 glsl_type::get_array_instance(symtab->get_type("gl_LightSourceParameters"), state->Const.MaxLights);
649
650 add_uniform(instructions, symtab, "gl_LightSource", light_source_array_type);
651
652 const glsl_type *const light_model_products_type =
653 symtab->get_type("gl_LightModelProducts");
654 add_uniform(instructions, symtab, "gl_FrontLightModelProduct",
655 light_model_products_type);
656 add_uniform(instructions, symtab, "gl_BackLightModelProduct",
657 light_model_products_type);
658
659 const glsl_type *const light_products_type =
660 glsl_type::get_array_instance(symtab->get_type("gl_LightProducts"),
661 state->Const.MaxLights);
662 add_uniform(instructions, symtab, "gl_FrontLightProduct", light_products_type);
663 add_uniform(instructions, symtab, "gl_BackLightProduct", light_products_type);
664
665 add_uniform(instructions, symtab, "gl_TextureEnvColor",
666 glsl_type::get_array_instance(glsl_type::vec4_type,
667 state->Const.MaxTextureUnits));
668
669 const glsl_type *const texcoords_vec4 =
670 glsl_type::get_array_instance(glsl_type::vec4_type,
671 state->Const.MaxTextureCoords);
672 add_uniform(instructions, symtab, "gl_EyePlaneS", texcoords_vec4);
673 add_uniform(instructions, symtab, "gl_EyePlaneT", texcoords_vec4);
674 add_uniform(instructions, symtab, "gl_EyePlaneR", texcoords_vec4);
675 add_uniform(instructions, symtab, "gl_EyePlaneQ", texcoords_vec4);
676 add_uniform(instructions, symtab, "gl_ObjectPlaneS", texcoords_vec4);
677 add_uniform(instructions, symtab, "gl_ObjectPlaneT", texcoords_vec4);
678 add_uniform(instructions, symtab, "gl_ObjectPlaneR", texcoords_vec4);
679 add_uniform(instructions, symtab, "gl_ObjectPlaneQ", texcoords_vec4);
680
681 add_uniform(instructions, symtab, "gl_Fog",
682 symtab->get_type("gl_FogParameters"));
683 }
684
685 /* Mesa-internal current attrib state */
686 const glsl_type *const vert_attribs =
687 glsl_type::get_array_instance(glsl_type::vec4_type, VERT_ATTRIB_MAX);
688 add_uniform(instructions, symtab, "gl_CurrentAttribVertMESA", vert_attribs);
689 const glsl_type *const frag_attribs =
690 glsl_type::get_array_instance(glsl_type::vec4_type, VARYING_SLOT_MAX);
691 add_uniform(instructions, symtab, "gl_CurrentAttribFragMESA", frag_attribs);
692 }
693
694 /* This function should only be called for ES, not desktop GL. */
695 static void
696 generate_100ES_vs_variables(exec_list *instructions,
697 struct _mesa_glsl_parse_state *state)
698 {
699 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
700 add_builtin_variable(instructions, state->symbols,
701 & builtin_core_vs_variables[i]);
702 }
703
704 generate_100ES_uniforms(instructions, state);
705
706 generate_ARB_draw_buffers_variables(instructions, state, false,
707 vertex_shader);
708 }
709
710 static void
711 generate_300ES_vs_variables(exec_list *instructions,
712 struct _mesa_glsl_parse_state *state)
713 {
714 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
715 add_builtin_variable(instructions, state->symbols,
716 & builtin_core_vs_variables[i]);
717 }
718
719 for (unsigned i = 0; i < Elements(builtin_300ES_vs_variables); i++) {
720 add_builtin_variable(instructions, state->symbols,
721 & builtin_300ES_vs_variables[i]);
722 }
723
724 generate_300ES_uniforms(instructions, state);
725
726 generate_ARB_draw_buffers_variables(instructions, state, false,
727 vertex_shader);
728 }
729
730
731 static void
732 generate_110_vs_variables(exec_list *instructions,
733 struct _mesa_glsl_parse_state *state,
734 bool add_deprecated)
735 {
736 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
737 add_builtin_variable(instructions, state->symbols,
738 & builtin_core_vs_variables[i]);
739 }
740
741 if (add_deprecated) {
742 for (unsigned i = 0
743 ; i < Elements(builtin_110_deprecated_vs_variables)
744 ; i++) {
745 add_builtin_variable(instructions, state->symbols,
746 & builtin_110_deprecated_vs_variables[i]);
747 }
748 }
749 generate_110_uniforms(instructions, state, add_deprecated);
750
751 if (add_deprecated) {
752 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
753 *
754 * "As with all arrays, indices used to subscript gl_TexCoord must
755 * either be an integral constant expressions, or this array must be
756 * re-declared by the shader with a size. The size can be at most
757 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
758 * implementation in preserving varying resources."
759 */
760 const glsl_type *const vec4_array_type =
761 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
762
763 add_variable(instructions, state->symbols,
764 "gl_TexCoord", vec4_array_type, ir_var_shader_out,
765 VARYING_SLOT_TEX0);
766 }
767
768 generate_ARB_draw_buffers_variables(instructions, state, false,
769 vertex_shader);
770 }
771
772
773 static void
774 generate_120_vs_variables(exec_list *instructions,
775 struct _mesa_glsl_parse_state *state,
776 bool add_deprecated)
777 {
778 /* GLSL version 1.20 did not add any built-in variables in the vertex
779 * shader.
780 */
781 generate_110_vs_variables(instructions, state, add_deprecated);
782 }
783
784
785 static void
786 generate_130_uniforms(exec_list *instructions,
787 struct _mesa_glsl_parse_state *state)
788 {
789 glsl_symbol_table *const symtab = state->symbols;
790
791 add_builtin_constant(instructions, symtab, "gl_MaxClipDistances",
792 state->Const.MaxClipPlanes);
793 add_builtin_constant(instructions, symtab, "gl_MaxVaryingComponents",
794 state->Const.MaxVaryingFloats);
795
796 if (state->ARB_shading_language_420pack_enable) {
797 add_builtin_constant(instructions, symtab, "gl_MinProgramTexelOffset",
798 state->Const.MinProgramTexelOffset);
799 add_builtin_constant(instructions, symtab, "gl_MaxProgramTexelOffset",
800 state->Const.MaxProgramTexelOffset);
801 }
802 }
803
804
805 static void
806 generate_130_vs_variables(exec_list *instructions,
807 struct _mesa_glsl_parse_state *state,
808 bool add_deprecated)
809 {
810 generate_120_vs_variables(instructions, state, add_deprecated);
811
812 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
813 add_builtin_variable(instructions, state->symbols,
814 & builtin_130_vs_variables[i]);
815 }
816
817 generate_130_uniforms(instructions, state);
818
819 /* From the GLSL 1.30 spec, section 7.1 (Vertex Shader Special
820 * Variables):
821 *
822 * The gl_ClipDistance array is predeclared as unsized and must
823 * be sized by the shader either redeclaring it with a size or
824 * indexing it only with integral constant expressions.
825 *
826 * We represent this in Mesa by initially declaring the array as
827 * size 0.
828 */
829 const glsl_type *const clip_distance_array_type =
830 glsl_type::get_array_instance(glsl_type::float_type, 0);
831
832 add_variable(instructions, state->symbols,
833 "gl_ClipDistance", clip_distance_array_type, ir_var_shader_out,
834 VARYING_SLOT_CLIP_DIST0);
835
836 generate_AMD_vertex_shader_layer_variables(instructions, state, false,
837 vertex_shader);
838 }
839
840
841 static void
842 initialize_vs_variables(exec_list *instructions,
843 struct _mesa_glsl_parse_state *state)
844 {
845 if (state->es_shader) {
846 switch (state->language_version) {
847 case 100:
848 generate_100ES_vs_variables(instructions, state);
849 break;
850 case 300:
851 generate_300ES_vs_variables(instructions, state);
852 break;
853 default:
854 assert(!"Unexpected language version");
855 break;
856 }
857 } else {
858 switch (state->language_version) {
859 case 110:
860 generate_110_vs_variables(instructions, state, true);
861 break;
862 case 120:
863 generate_120_vs_variables(instructions, state, true);
864 break;
865 case 130:
866 generate_130_vs_variables(instructions, state, true);
867 break;
868 case 140:
869 case 150:
870 generate_130_vs_variables(instructions, state, false);
871 break;
872 default:
873 assert(!"Unexpected language version");
874 break;
875 }
876 }
877
878 generate_ARB_draw_instanced_variables(instructions, state, false,
879 vertex_shader);
880 }
881
882
883 /* This function should only be called for ES, not desktop GL. */
884 static void
885 generate_100ES_fs_variables(exec_list *instructions,
886 struct _mesa_glsl_parse_state *state)
887 {
888 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
889 add_builtin_variable(instructions, state->symbols,
890 & builtin_core_fs_variables[i]);
891 }
892
893 for (unsigned i = 0; i < Elements(builtin_100ES_fs_variables); i++) {
894 add_builtin_variable(instructions, state->symbols,
895 & builtin_100ES_fs_variables[i]);
896 }
897
898 generate_100ES_uniforms(instructions, state);
899
900 generate_ARB_draw_buffers_variables(instructions, state, false,
901 fragment_shader);
902 }
903
904 static void
905 generate_300ES_fs_variables(exec_list *instructions,
906 struct _mesa_glsl_parse_state *state)
907 {
908 /* Note: we don't add builtin_core_fs_variables, because it contains
909 * gl_FragColor, which is not in GLSL 3.00 ES.
910 */
911
912 for (unsigned i = 0; i < Elements(builtin_300ES_fs_variables); i++) {
913 add_builtin_variable(instructions, state->symbols,
914 & builtin_300ES_fs_variables[i]);
915 }
916
917 generate_300ES_uniforms(instructions, state);
918
919 generate_ARB_draw_buffers_variables(instructions, state, false,
920 fragment_shader);
921 }
922
923 static void
924 generate_110_fs_variables(exec_list *instructions,
925 struct _mesa_glsl_parse_state *state,
926 bool add_deprecated)
927 {
928 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
929 add_builtin_variable(instructions, state->symbols,
930 & builtin_core_fs_variables[i]);
931 }
932
933 for (unsigned i = 0; i < Elements(builtin_110_fs_variables); i++) {
934 add_builtin_variable(instructions, state->symbols,
935 & builtin_110_fs_variables[i]);
936 }
937
938 if (add_deprecated) {
939 for (unsigned i = 0
940 ; i < Elements(builtin_110_deprecated_fs_variables)
941 ; i++) {
942 add_builtin_variable(instructions, state->symbols,
943 & builtin_110_deprecated_fs_variables[i]);
944 }
945 }
946
947 generate_110_uniforms(instructions, state, add_deprecated);
948
949 if (add_deprecated) {
950 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
951 *
952 * "As with all arrays, indices used to subscript gl_TexCoord must
953 * either be an integral constant expressions, or this array must be
954 * re-declared by the shader with a size. The size can be at most
955 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
956 * implementation in preserving varying resources."
957 */
958 const glsl_type *const vec4_array_type =
959 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
960
961 add_variable(instructions, state->symbols,
962 "gl_TexCoord", vec4_array_type, ir_var_shader_in,
963 VARYING_SLOT_TEX0);
964 }
965
966 generate_ARB_draw_buffers_variables(instructions, state, false,
967 fragment_shader);
968 }
969
970
971 static void
972 generate_ARB_draw_buffers_variables(exec_list *instructions,
973 struct _mesa_glsl_parse_state *state,
974 bool warn, _mesa_glsl_parser_targets target)
975 {
976 /* gl_MaxDrawBuffers is available in all shader stages.
977 */
978 ir_variable *const mdb =
979 add_builtin_constant(instructions, state->symbols, "gl_MaxDrawBuffers",
980 state->Const.MaxDrawBuffers);
981
982 if (warn)
983 mdb->warn_extension = "GL_ARB_draw_buffers";
984
985 /* gl_FragData is only available in the fragment shader.
986 * It is not present in GLSL 3.00 ES.
987 */
988 if (target == fragment_shader && !state->is_version(0, 300)) {
989 const glsl_type *const vec4_array_type =
990 glsl_type::get_array_instance(glsl_type::vec4_type,
991 state->Const.MaxDrawBuffers);
992
993 ir_variable *const fd =
994 add_variable(instructions, state->symbols,
995 "gl_FragData", vec4_array_type,
996 ir_var_shader_out, FRAG_RESULT_DATA0);
997
998 if (warn)
999 fd->warn_extension = "GL_ARB_draw_buffers";
1000 }
1001 }
1002
1003
1004 static void
1005 generate_ARB_draw_instanced_variables(exec_list *instructions,
1006 struct _mesa_glsl_parse_state *state,
1007 bool warn,
1008 _mesa_glsl_parser_targets target)
1009 {
1010 /* gl_InstanceIDARB is only available in the vertex shader.
1011 */
1012 if (target != vertex_shader)
1013 return;
1014
1015 if (state->ARB_draw_instanced_enable) {
1016 ir_variable *inst =
1017 add_variable(instructions, state->symbols,
1018 "gl_InstanceIDARB", glsl_type::int_type,
1019 ir_var_system_value, SYSTEM_VALUE_INSTANCE_ID);
1020
1021 if (warn)
1022 inst->warn_extension = "GL_ARB_draw_instanced";
1023 }
1024
1025 bool available_in_core = state->is_version(140, 300);
1026 if (state->ARB_draw_instanced_enable || available_in_core) {
1027 /* Originally ARB_draw_instanced only specified that ARB decorated name.
1028 * Since no vendor actually implemented that behavior and some apps use
1029 * the undecorated name, the extension now specifies that both names are
1030 * available.
1031 */
1032 ir_variable *inst =
1033 add_variable(instructions, state->symbols,
1034 "gl_InstanceID", glsl_type::int_type,
1035 ir_var_system_value, SYSTEM_VALUE_INSTANCE_ID);
1036
1037 if (!available_in_core && warn)
1038 inst->warn_extension = "GL_ARB_draw_instanced";
1039 }
1040 }
1041
1042 static void
1043 generate_AMD_vertex_shader_layer_variables(exec_list *instructions,
1044 struct _mesa_glsl_parse_state *state,
1045 bool warn,
1046 _mesa_glsl_parser_targets target)
1047 {
1048 /* gl_Layer is only available in the vertex shader for the
1049 * AMD_vertex_shader_layer extension. It will also be available in the
1050 * geometry shader when GLSL 1.50 is supported.
1051 */
1052 if (target != vertex_shader)
1053 return;
1054
1055 if (state->AMD_vertex_shader_layer_enable) {
1056 ir_variable *inst =
1057 add_variable(instructions, state->symbols,
1058 "gl_Layer", glsl_type::int_type,
1059 ir_var_shader_out, VARYING_SLOT_LAYER);
1060
1061 if (warn)
1062 inst->warn_extension = "GL_AMD_vertex_shader_layer";
1063 }
1064 }
1065
1066 static void
1067 generate_ARB_shader_stencil_export_variables(exec_list *instructions,
1068 struct _mesa_glsl_parse_state *state,
1069 bool warn)
1070 {
1071 /* gl_FragStencilRefARB is only available in the fragment shader.
1072 */
1073 ir_variable *const fd =
1074 add_variable(instructions, state->symbols,
1075 "gl_FragStencilRefARB", glsl_type::int_type,
1076 ir_var_shader_out, FRAG_RESULT_STENCIL);
1077
1078 if (warn)
1079 fd->warn_extension = "GL_ARB_shader_stencil_export";
1080 }
1081
1082 static void
1083 generate_AMD_shader_stencil_export_variables(exec_list *instructions,
1084 struct _mesa_glsl_parse_state *state,
1085 bool warn)
1086 {
1087 /* gl_FragStencilRefAMD is only available in the fragment shader.
1088 */
1089 ir_variable *const fd =
1090 add_variable(instructions, state->symbols,
1091 "gl_FragStencilRefAMD", glsl_type::int_type,
1092 ir_var_shader_out, FRAG_RESULT_STENCIL);
1093
1094 if (warn)
1095 fd->warn_extension = "GL_AMD_shader_stencil_export";
1096 }
1097
1098 static void
1099 generate_120_fs_variables(exec_list *instructions,
1100 struct _mesa_glsl_parse_state *state,
1101 bool add_deprecated)
1102 {
1103 generate_110_fs_variables(instructions, state, add_deprecated);
1104
1105 for (unsigned i = 0
1106 ; i < Elements(builtin_120_fs_variables)
1107 ; i++) {
1108 add_builtin_variable(instructions, state->symbols,
1109 & builtin_120_fs_variables[i]);
1110 }
1111 }
1112
1113 static void
1114 generate_fs_clipdistance(exec_list *instructions,
1115 struct _mesa_glsl_parse_state *state)
1116 {
1117 /* From the GLSL 1.30 spec, section 7.2 (Fragment Shader Special
1118 * Variables):
1119 *
1120 * The built-in input variable gl_ClipDistance array contains linearly
1121 * interpolated values for the vertex values written by the vertex shader
1122 * to the gl_ClipDistance vertex output variable. This array must be
1123 * sized in the fragment shader either implicitly or explicitly to be the
1124 * same size as it was sized in the vertex shader.
1125 *
1126 * In other words, the array must be pre-declared as implicitly sized. We
1127 * represent this in Mesa by initially declaring the array as size 0.
1128 */
1129 const glsl_type *const clip_distance_array_type =
1130 glsl_type::get_array_instance(glsl_type::float_type, 0);
1131
1132 add_variable(instructions, state->symbols,
1133 "gl_ClipDistance", clip_distance_array_type, ir_var_shader_in,
1134 VARYING_SLOT_CLIP_DIST0);
1135 }
1136
1137 static void
1138 generate_130_fs_variables(exec_list *instructions,
1139 struct _mesa_glsl_parse_state *state)
1140 {
1141 generate_120_fs_variables(instructions, state, true);
1142
1143 generate_130_uniforms(instructions, state);
1144 generate_fs_clipdistance(instructions, state);
1145 }
1146
1147
1148 static void
1149 generate_140_fs_variables(exec_list *instructions,
1150 struct _mesa_glsl_parse_state *state)
1151 {
1152 generate_120_fs_variables(instructions, state, false);
1153
1154 generate_130_uniforms(instructions, state);
1155 generate_fs_clipdistance(instructions, state);
1156 }
1157
1158 static void
1159 initialize_fs_variables(exec_list *instructions,
1160 struct _mesa_glsl_parse_state *state)
1161 {
1162 if (state->es_shader) {
1163 switch (state->language_version) {
1164 case 100:
1165 generate_100ES_fs_variables(instructions, state);
1166 break;
1167 case 300:
1168 generate_300ES_fs_variables(instructions, state);
1169 break;
1170 default:
1171 assert(!"Unexpected language version");
1172 break;
1173 }
1174 } else {
1175 switch (state->language_version) {
1176 case 110:
1177 generate_110_fs_variables(instructions, state, true);
1178 break;
1179 case 120:
1180 generate_120_fs_variables(instructions, state, true);
1181 break;
1182 case 130:
1183 generate_130_fs_variables(instructions, state);
1184 break;
1185 case 140:
1186 case 150:
1187 generate_140_fs_variables(instructions, state);
1188 break;
1189 default:
1190 assert(!"Unexpected language version");
1191 break;
1192 }
1193 }
1194
1195 if (state->ARB_shader_stencil_export_enable)
1196 generate_ARB_shader_stencil_export_variables(instructions, state,
1197 state->ARB_shader_stencil_export_warn);
1198
1199 if (state->AMD_shader_stencil_export_enable)
1200 generate_AMD_shader_stencil_export_variables(instructions, state,
1201 state->AMD_shader_stencil_export_warn);
1202 }
1203
1204 void
1205 _mesa_glsl_initialize_variables(exec_list *instructions,
1206 struct _mesa_glsl_parse_state *state)
1207 {
1208 switch (state->target) {
1209 case vertex_shader:
1210 initialize_vs_variables(instructions, state);
1211 break;
1212 case geometry_shader:
1213 break;
1214 case fragment_shader:
1215 initialize_fs_variables(instructions, state);
1216 break;
1217 }
1218 }